Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2014, Google Inc. |
| 2 | * |
| 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. |
| 6 | * |
| 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. */ |
| 14 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 15 | #include <openssl/ssl.h> |
| 16 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/stack.h> |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 24 | |
| 25 | #include "internal.h" |
| 26 | |
| 27 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 28 | namespace bssl { |
| 29 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 30 | void SSL_CUSTOM_EXTENSION_free(SSL_CUSTOM_EXTENSION *custom_extension) { |
| 31 | OPENSSL_free(custom_extension); |
| 32 | } |
| 33 | |
| 34 | static const SSL_CUSTOM_EXTENSION *custom_ext_find( |
| 35 | STACK_OF(SSL_CUSTOM_EXTENSION) *stack, |
| 36 | unsigned *out_index, uint16_t value) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 37 | for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 38 | const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i); |
| 39 | if (ext->value == value) { |
| 40 | if (out_index != NULL) { |
| 41 | *out_index = i; |
| 42 | } |
| 43 | return ext; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 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) { |
| 56 | if (ssl->server) { |
| 57 | return 0; |
| 58 | } |
| 59 | *out_len = 0; |
| 60 | return 1; |
| 61 | } |
| 62 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 63 | static int custom_ext_add_hello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 64 | SSL *const ssl = hs->ssl; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 65 | STACK_OF(SSL_CUSTOM_EXTENSION) *stack = ssl->ctx->client_custom_extensions; |
| 66 | if (ssl->server) { |
| 67 | stack = ssl->ctx->server_custom_extensions; |
| 68 | } |
| 69 | |
| 70 | if (stack == NULL) { |
| 71 | return 1; |
| 72 | } |
| 73 | |
Alessandro Ghedini | 67bb45f | 2017-03-30 16:33:24 -0500 | [diff] [blame] | 74 | if (ssl->cert->enable_early_data) { |
Steven Valdez | 2a07072 | 2017-03-25 20:54:16 -0500 | [diff] [blame] | 75 | /* TODO(svaldez): Support Custom Extensions with 0-RTT. For now the caller |
| 76 | * is expected not to configure both together. |
| 77 | * https://crbug.com/boringssl/173. */ |
| 78 | OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
| 79 | return 0; |
| 80 | } |
| 81 | |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 82 | for (size_t i = 0; i < sk_SSL_CUSTOM_EXTENSION_num(stack); i++) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 83 | const SSL_CUSTOM_EXTENSION *ext = sk_SSL_CUSTOM_EXTENSION_value(stack, i); |
| 84 | |
| 85 | if (ssl->server && |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 86 | !(hs->custom_extensions.received & (1u << i))) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 87 | /* Servers cannot echo extensions that the client didn't send. */ |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | const uint8_t *contents; |
| 92 | size_t contents_len; |
| 93 | int alert = SSL_AD_DECODE_ERROR; |
| 94 | CBB contents_cbb; |
| 95 | |
| 96 | switch (ext->add_callback(ssl, ext->value, &contents, &contents_len, &alert, |
| 97 | ext->add_arg)) { |
| 98 | case 1: |
| 99 | if (!CBB_add_u16(extensions, ext->value) || |
| 100 | !CBB_add_u16_length_prefixed(extensions, &contents_cbb) || |
| 101 | !CBB_add_bytes(&contents_cbb, contents, contents_len) || |
| 102 | !CBB_flush(extensions)) { |
| 103 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 104 | ERR_add_error_dataf("extension %u", (unsigned) ext->value); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 105 | if (ext->free_callback && 0 < contents_len) { |
| 106 | ext->free_callback(ssl, ext->value, contents, ext->add_arg); |
| 107 | } |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | if (ext->free_callback && 0 < contents_len) { |
| 112 | ext->free_callback(ssl, ext->value, contents, ext->add_arg); |
| 113 | } |
| 114 | |
| 115 | if (!ssl->server) { |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 116 | assert((hs->custom_extensions.sent & (1u << i)) == 0); |
| 117 | hs->custom_extensions.sent |= (1u << i); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 118 | } |
| 119 | break; |
| 120 | |
| 121 | case 0: |
| 122 | break; |
| 123 | |
| 124 | default: |
| 125 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 126 | OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 127 | ERR_add_error_dataf("extension %u", (unsigned) ext->value); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return 1; |
| 133 | } |
| 134 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 135 | int custom_ext_add_clienthello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 136 | return custom_ext_add_hello(hs, extensions); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 137 | } |
| 138 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 139 | int custom_ext_parse_serverhello(SSL_HANDSHAKE *hs, int *out_alert, |
| 140 | uint16_t value, const CBS *extension) { |
| 141 | SSL *const ssl = hs->ssl; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 142 | unsigned index; |
| 143 | const SSL_CUSTOM_EXTENSION *ext = |
| 144 | custom_ext_find(ssl->ctx->client_custom_extensions, &index, value); |
| 145 | |
| 146 | if (/* Unknown extensions are not allowed in a ServerHello. */ |
| 147 | ext == NULL || |
| 148 | /* Also, if we didn't send the extension, that's also unacceptable. */ |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 149 | !(hs->custom_extensions.sent & (1u << index))) { |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 150 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 151 | ERR_add_error_dataf("extension %u", (unsigned)value); |
David Benjamin | 0c40a96 | 2016-08-01 12:05:50 -0400 | [diff] [blame] | 152 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | if (ext->parse_callback != NULL && |
| 157 | !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension), |
| 158 | out_alert, ext->parse_arg)) { |
| 159 | OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 160 | ERR_add_error_dataf("extension %u", (unsigned)ext->value); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 167 | int custom_ext_parse_clienthello(SSL_HANDSHAKE *hs, int *out_alert, |
| 168 | uint16_t value, const CBS *extension) { |
| 169 | SSL *const ssl = hs->ssl; |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 170 | unsigned index; |
| 171 | const SSL_CUSTOM_EXTENSION *ext = |
| 172 | custom_ext_find(ssl->ctx->server_custom_extensions, &index, value); |
| 173 | |
| 174 | if (ext == NULL) { |
| 175 | return 1; |
| 176 | } |
| 177 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 178 | assert((hs->custom_extensions.received & (1u << index)) == 0); |
| 179 | hs->custom_extensions.received |= (1u << index); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 180 | |
| 181 | if (ext->parse_callback && |
| 182 | !ext->parse_callback(ssl, value, CBS_data(extension), CBS_len(extension), |
| 183 | out_alert, ext->parse_arg)) { |
| 184 | OPENSSL_PUT_ERROR(SSL, SSL_R_CUSTOM_EXTENSION_ERROR); |
Adam Langley | fbbef12 | 2016-11-17 12:55:14 -0800 | [diff] [blame] | 185 | ERR_add_error_dataf("extension %u", (unsigned)ext->value); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | return 1; |
| 190 | } |
| 191 | |
David Benjamin | 2bd1917 | 2016-11-17 16:47:15 +0900 | [diff] [blame] | 192 | int custom_ext_add_serverhello(SSL_HANDSHAKE *hs, CBB *extensions) { |
| 193 | return custom_ext_add_hello(hs, extensions); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* MAX_NUM_CUSTOM_EXTENSIONS is the maximum number of custom extensions that |
| 197 | * can be set on an |SSL_CTX|. It's determined by the size of the bitset used |
| 198 | * to track when an extension has been sent. */ |
| 199 | #define MAX_NUM_CUSTOM_EXTENSIONS \ |
David Benjamin | f5d2cd0 | 2016-10-06 19:39:20 -0400 | [diff] [blame] | 200 | (sizeof(((SSL_HANDSHAKE *)NULL)->custom_extensions.sent) * 8) |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 201 | |
| 202 | static int custom_ext_append(STACK_OF(SSL_CUSTOM_EXTENSION) **stack, |
| 203 | unsigned extension_value, |
| 204 | SSL_custom_ext_add_cb add_cb, |
| 205 | SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 206 | SSL_custom_ext_parse_cb parse_cb, |
| 207 | void *parse_arg) { |
| 208 | if (add_cb == NULL || |
| 209 | 0xffff < extension_value || |
| 210 | SSL_extension_supported(extension_value) || |
| 211 | /* Specifying a free callback without an add callback is nonsensical |
| 212 | * and an error. */ |
| 213 | (*stack != NULL && |
| 214 | (MAX_NUM_CUSTOM_EXTENSIONS <= sk_SSL_CUSTOM_EXTENSION_num(*stack) || |
| 215 | custom_ext_find(*stack, NULL, extension_value) != NULL))) { |
| 216 | return 0; |
| 217 | } |
| 218 | |
David Benjamin | e8703a3 | 2017-07-09 16:17:55 -0400 | [diff] [blame] | 219 | SSL_CUSTOM_EXTENSION *ext = |
| 220 | (SSL_CUSTOM_EXTENSION *)OPENSSL_malloc(sizeof(SSL_CUSTOM_EXTENSION)); |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 221 | if (ext == NULL) { |
| 222 | return 0; |
| 223 | } |
| 224 | ext->add_callback = add_cb; |
| 225 | ext->add_arg = add_arg; |
| 226 | ext->free_callback = free_cb; |
| 227 | ext->parse_callback = parse_cb; |
| 228 | ext->parse_arg = parse_arg; |
| 229 | ext->value = extension_value; |
| 230 | |
| 231 | if (*stack == NULL) { |
| 232 | *stack = sk_SSL_CUSTOM_EXTENSION_new_null(); |
| 233 | if (*stack == NULL) { |
| 234 | SSL_CUSTOM_EXTENSION_free(ext); |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (!sk_SSL_CUSTOM_EXTENSION_push(*stack, ext)) { |
| 240 | SSL_CUSTOM_EXTENSION_free(ext); |
| 241 | if (sk_SSL_CUSTOM_EXTENSION_num(*stack) == 0) { |
| 242 | sk_SSL_CUSTOM_EXTENSION_free(*stack); |
| 243 | *stack = NULL; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 251 | } // namespace bssl |
| 252 | |
| 253 | using namespace bssl; |
| 254 | |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 255 | int SSL_CTX_add_client_custom_ext(SSL_CTX *ctx, unsigned extension_value, |
| 256 | SSL_custom_ext_add_cb add_cb, |
| 257 | SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 258 | SSL_custom_ext_parse_cb parse_cb, |
| 259 | void *parse_arg) { |
| 260 | return custom_ext_append(&ctx->client_custom_extensions, extension_value, |
| 261 | add_cb ? add_cb : default_add_callback, free_cb, |
| 262 | add_arg, parse_cb, parse_arg); |
| 263 | } |
| 264 | |
| 265 | int SSL_CTX_add_server_custom_ext(SSL_CTX *ctx, unsigned extension_value, |
| 266 | SSL_custom_ext_add_cb add_cb, |
| 267 | SSL_custom_ext_free_cb free_cb, void *add_arg, |
| 268 | SSL_custom_ext_parse_cb parse_cb, |
| 269 | void *parse_arg) { |
| 270 | return custom_ext_append(&ctx->server_custom_extensions, extension_value, |
| 271 | add_cb ? add_cb : default_add_callback, free_cb, |
| 272 | add_arg, parse_cb, parse_arg); |
| 273 | } |