1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
15 #include <openssl/ssl.h>
20 #include <openssl/bytestring.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 #include <openssl/stack.h>
30 void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) {
31 OPENSSL_free(custom_extension);
34 static const SSL_CUSTOM_EXTENSION *custom_ext_find(
35 STACK_OF(SSL_CUSTOM_EXTENSION) *stack,
36 unsigned *out_index, uint16_t value) {
37 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
38 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
39 if (ext->value == value) {
40 if (out_index != NULL) {
50 // default_add_callback is used as the |add_callback| when the user doesn't
51 // provide one. For servers, it does nothing while, for clients, it causes an
52 // empty extension to be included.
53 static int default_add_callback(SSL *ssl, unsigned extension_value,
54 const uint8_t **out, size_t *out_len,
55 int *out_alert_value, void *add_arg) {
63 static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) {
64 SSL *const ssl = hs->ssl;
65 STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions;
67 stack = ssl->ctx->server_custom_extensions;
74 for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) {
75 const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i);
78 !(hs->custom_extensions.received & (1u << i))) {
79 // Servers cannot echo extensions that the client didn't send.
83 const uint8_t *contents;
85 int alert = SSL_AD_DECODE_ERROR;
88 switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert,
91 if (!CBB_add_u16(extensions, ext->value) ||
92 !CBB_add_u16_length_prefixed(extensions, &contents_cbb) ||
93 !CBB_add_bytes(&contents_cbb, contents, contents_len) ||
94 !CBB_flush(extensions)) {
95 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
96 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
97 if (ext->free_callback && 0 < contents_len) {
98 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
103 if (ext->free_callback && 0 < contents_len) {
104 ext->free_callback(ssl, ext->value, contents, ext->add_arg);
108 assert((hs->custom_extensions.sent & (1u << i)) == 0);
109 hs->custom_extensions.sent |= (1u << i);
117 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
118 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
119 ERR_add_error_dataf("extension %u", (unsigned) ext->value);
127 int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) {
128 return custom_ext_add_hello(hs, extensions);
131 int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert,
132 uint16_t value, const CBS *extension) {
133 SSL *const ssl = hs->ssl;
135 const SSL_CUSTOM_EXTENSION *ext =
136 custom_ext_find(ssl->ctx->client_custom_extensions, &index, value);
138 if (// Unknown extensions are not allowed in a ServerHello.
140 // Also, if we didn't send the extension, that's also unacceptable.
141 !(hs->custom_extensions.sent & (1u << index))) {
142 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
143 ERR_add_error_dataf("extension %u", (unsigned)value);
144 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
148 if (ext->parse_callback != NULL &&
149 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
150 out_alert, ext->parse_arg)) {
151 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
152 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
159 int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert,
160 uint16_t value, const CBS *extension) {
161 SSL *const ssl = hs->ssl;
163 const SSL_CUSTOM_EXTENSION *ext =
164 custom_ext_find(ssl->ctx->server_custom_extensions, &index, value);
170 assert((hs->custom_extensions.received & (1u << index)) == 0);
171 hs->custom_extensions.received |= (1u << index);
173 if (ext->parse_callback &&
174 !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension),
175 out_alert, ext->parse_arg)) {
176 OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR);
177 ERR_add_error_dataf("extension %u", (unsigned)ext->value);
184 int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) {
185 return custom_ext_add_hello(hs, extensions);
188 // MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that
189 // can be set on an |SSL_CTX|. It's determined by the size of the bitset used
190 // to track when an extension has been sent.
191 #define MAX_NUM_CUSTOM_EXTENSIONS \
192 (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8)
194 static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack,
195 unsigned extension_value,
196 SSL_custom_ext_add_cb add_cb,
197 SSL_custom_ext_free_cb free_cb, void *add_arg,
198 SSL_custom_ext_parse_cb parse_cb,
200 if (add_cb == NULL ||
201 0xffff < extension_value ||
202 SSL_extension_supported(extension_value) ||
203 // Specifying a free callback without an add callback is nonsensical
206 (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) ||
207 custom_ext_find(*stack, NULL, extension_value) != NULL))) {
211 SSL_CUSTOM_EXTENSION *ext =
212 (SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION));
216 ext->add_callback = add_cb;
217 ext->add_arg = add_arg;
218 ext->free_callback = free_cb;
219 ext->parse_callback = parse_cb;
220 ext->parse_arg = parse_arg;
221 ext->value = extension_value;
223 if (*stack == NULL) {
224 *stack = sk_SSL_CUSTOM_EXTENSION_new_null();
225 if (*stack == NULL) {
226 SSL_CUSTOM_EXTENSION_free(ext);
231 if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) {
232 SSL_CUSTOM_EXTENSION_free(ext);
233 if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) {
234 sk_SSL_CUSTOM_EXTENSION_free(*stack);
245 using namespace bssl;
247 int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value,
248 SSL_custom_ext_add_cb add_cb,
249 SSL_custom_ext_free_cb free_cb, void *add_arg,
250 SSL_custom_ext_parse_cb parse_cb,
252 return custom_ext_append(&ctx->client_custom_extensions, extension_value,
253 add_cb ? add_cb : default_add_callback, free_cb,
254 add_arg, parse_cb, parse_arg);
257 int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value,
258 SSL_custom_ext_add_cb add_cb,
259 SSL_custom_ext_free_cb free_cb, void *add_arg,
260 SSL_custom_ext_parse_cb parse_cb,
262 return custom_ext_append(&ctx->server_custom_extensions, extension_value,
263 add_cb ? add_cb : default_add_callback, free_cb,
264 add_arg, parse_cb, parse_arg);