| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 6 |
* The implementation was written so as to conform with Netscapes SSL. |
| 7 |
* |
| 8 |
* This library is free for commercial and non-commercial use as long as |
| 9 |
* the following conditions are aheared to. The following conditions |
| 10 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 11 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 |
* included with this distribution is covered by the same copyright terms |
| 13 |
* except that the holder is Tim Hudson ([email protected]). |
| 14 |
* |
| 15 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 |
* the code are not to be removed. |
| 17 |
* If this package is used in a product, Eric Young should be given attribution |
| 18 |
* as the author of the parts of the library used. |
| 19 |
* This can be in the form of a textual message at program startup or |
| 20 |
* in documentation (online or textual) provided with the package. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 3. All advertising materials mentioning features or use of this software |
| 31 |
* must display the following acknowledgement: |
| 32 |
* "This product includes cryptographic software written by |
| 33 |
* Eric Young ([email protected])" |
| 34 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 35 |
* being used are not cryptographic related :-). |
| 36 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 |
* the apps directory (application code) you must include an acknowledgement: |
| 38 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 |
* SUCH DAMAGE. |
| 51 |
* |
| 52 |
* The licence and distribution terms for any publically available version or |
| 53 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 |
* copied and put under another distribution licence |
| 55 |
* [including the GNU Public Licence.] |
| 56 |
*/ |
| 57 |
/* ==================================================================== |
| 58 |
* Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 |
* |
| 60 |
* Redistribution and use in source and binary forms, with or without |
| 61 |
* modification, are permitted provided that the following conditions |
| 62 |
* are met: |
| 63 |
* |
| 64 |
* 1. Redistributions of source code must retain the above copyright |
| 65 |
* notice, this list of conditions and the following disclaimer. |
| 66 |
* |
| 67 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 68 |
* notice, this list of conditions and the following disclaimer in |
| 69 |
* the documentation and/or other materials provided with the |
| 70 |
* distribution. |
| 71 |
* |
| 72 |
* 3. All advertising materials mentioning features or use of this |
| 73 |
* software must display the following acknowledgment: |
| 74 |
* "This product includes software developed by the OpenSSL Project |
| 75 |
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 |
* |
| 77 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 |
* endorse or promote products derived from this software without |
| 79 |
* prior written permission. For written permission, please contact |
| 80 |
* [email protected]. |
| 81 |
* |
| 82 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 83 |
* nor may "OpenSSL" appear in their names without prior written |
| 84 |
* permission of the OpenSSL Project. |
| 85 |
* |
| 86 |
* 6. Redistributions of any form whatsoever must retain the following |
| 87 |
* acknowledgment: |
| 88 |
* "This product includes software developed by the OpenSSL Project |
| 89 |
* for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 |
* |
| 91 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 |
* ==================================================================== |
| 104 |
* |
| 105 |
* This product includes cryptographic software written by Eric Young |
| 106 |
* ([email protected]). This product includes software written by Tim |
| 107 |
* Hudson ([email protected]). */ |
| 108 |
|
| 109 |
#include <openssl/ssl.h> |
| 110 |
|
| 111 |
#include <assert.h> |
| 112 |
#include <limits.h> |
| 113 |
#include <stdlib.h> |
| 114 |
#include <string.h> |
| 115 |
|
| 116 |
#include <openssl/bytestring.h> |
| 117 |
#include <openssl/digest.h> |
| 118 |
#include <openssl/err.h> |
| 119 |
#include <openssl/evp.h> |
| 120 |
#include <openssl/hmac.h> |
| 121 |
#include <openssl/mem.h> |
| 122 |
#include <openssl/nid.h> |
| 123 |
#include <openssl/rand.h> |
| 124 |
#include <openssl/type_check.h> |
| 125 |
|
| 126 |
#include "internal.h" |
| 127 |
|
| 128 |
|
| 129 |
static int ssl_check_clienthello_tlsext(SSL *ssl); |
| 130 |
static int ssl_check_serverhello_tlsext(SSL *ssl); |
| 131 |
|
| 132 |
static int compare_uint16_t(const void *p1, const void *p2) { |
| 133 |
uint16_t u1 = *((const uint16_t *)p1); |
| 134 |
uint16_t u2 = *((const uint16_t *)p2); |
| 135 |
if (u1 < u2) { |
| 136 |
return -1; |
| 137 |
} else if (u1 > u2) { |
| 138 |
return 1; |
| 139 |
} else { |
| 140 |
return 0; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be |
| 145 |
* more than one extension of the same type in a ClientHello or ServerHello. |
| 146 |
* This function does an initial scan over the extensions block to filter those |
| 147 |
* out. */ |
| 148 |
static int tls1_check_duplicate_extensions(const CBS *cbs) { |
| 149 |
CBS extensions = *cbs; |
| 150 |
size_t num_extensions = 0, i = 0; |
| 151 |
uint16_t *extension_types = NULL; |
| 152 |
int ret = 0; |
| 153 |
|
| 154 |
/* First pass: count the extensions. */ |
| 155 |
while (CBS_len(&extensions) > 0) { |
| 156 |
uint16_t type; |
| 157 |
CBS extension; |
| 158 |
|
| 159 |
if (!CBS_get_u16(&extensions, &type) || |
| 160 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 161 |
goto done; |
| 162 |
} |
| 163 |
|
| 164 |
num_extensions++; |
| 165 |
} |
| 166 |
|
| 167 |
if (num_extensions == 0) { |
| 168 |
return 1; |
| 169 |
} |
| 170 |
|
| 171 |
extension_types = OPENSSL_malloc(sizeof(uint16_t) * num_extensions); |
| 172 |
if (extension_types == NULL) { |
| 173 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 174 |
goto done; |
| 175 |
} |
| 176 |
|
| 177 |
/* Second pass: gather the extension types. */ |
| 178 |
extensions = *cbs; |
| 179 |
for (i = 0; i < num_extensions; i++) { |
| 180 |
CBS extension; |
| 181 |
|
| 182 |
if (!CBS_get_u16(&extensions, &extension_types[i]) || |
| 183 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 184 |
/* This should not happen. */ |
| 185 |
goto done; |
| 186 |
} |
| 187 |
} |
| 188 |
assert(CBS_len(&extensions) == 0); |
| 189 |
|
| 190 |
/* Sort the extensions and make sure there are no duplicates. */ |
| 191 |
qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t); |
| 192 |
for (i = 1; i < num_extensions; i++) { |
| 193 |
if (extension_types[i - 1] == extension_types[i]) { |
| 194 |
goto done; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
ret = 1; |
| 199 |
|
| 200 |
done: |
| 201 |
OPENSSL_free(extension_types); |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
char ssl_early_callback_init(struct ssl_early_callback_ctx *ctx) { |
| 206 |
CBS client_hello, session_id, cipher_suites, compression_methods, extensions; |
| 207 |
|
| 208 |
CBS_init(&client_hello, ctx->client_hello, ctx->client_hello_len); |
| 209 |
|
| 210 |
if (/* Skip client version. */ |
| 211 |
!CBS_skip(&client_hello, 2) || |
| 212 |
/* Skip client nonce. */ |
| 213 |
!CBS_skip(&client_hello, 32) || |
| 214 |
/* Extract session_id. */ |
| 215 |
!CBS_get_u8_length_prefixed(&client_hello, &session_id)) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
ctx->session_id = CBS_data(&session_id); |
| 220 |
ctx->session_id_len = CBS_len(&session_id); |
| 221 |
|
| 222 |
/* Skip past DTLS cookie */ |
| 223 |
if (SSL_IS_DTLS(ctx->ssl)) { |
| 224 |
CBS cookie; |
| 225 |
|
| 226 |
if (!CBS_get_u8_length_prefixed(&client_hello, &cookie)) { |
| 227 |
return 0; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/* Extract cipher_suites. */ |
| 232 |
if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
| 233 |
CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0) { |
| 234 |
return 0; |
| 235 |
} |
| 236 |
ctx->cipher_suites = CBS_data(&cipher_suites); |
| 237 |
ctx->cipher_suites_len = CBS_len(&cipher_suites); |
| 238 |
|
| 239 |
/* Extract compression_methods. */ |
| 240 |
if (!CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 241 |
CBS_len(&compression_methods) < 1) { |
| 242 |
return 0; |
| 243 |
} |
| 244 |
ctx->compression_methods = CBS_data(&compression_methods); |
| 245 |
ctx->compression_methods_len = CBS_len(&compression_methods); |
| 246 |
|
| 247 |
/* If the ClientHello ends here then it's valid, but doesn't have any |
| 248 |
* extensions. (E.g. SSLv3.) */ |
| 249 |
if (CBS_len(&client_hello) == 0) { |
| 250 |
ctx->extensions = NULL; |
| 251 |
ctx->extensions_len = 0; |
| 252 |
return 1; |
| 253 |
} |
| 254 |
|
| 255 |
/* Extract extensions and check it is valid. */ |
| 256 |
if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) || |
| 257 |
!tls1_check_duplicate_extensions(&extensions) || |
| 258 |
CBS_len(&client_hello) != 0) { |
| 259 |
return 0; |
| 260 |
} |
| 261 |
ctx->extensions = CBS_data(&extensions); |
| 262 |
ctx->extensions_len = CBS_len(&extensions); |
| 263 |
|
| 264 |
return 1; |
| 265 |
} |
| 266 |
|
| 267 |
int SSL_early_callback_ctx_extension_get( |
| 268 |
const struct ssl_early_callback_ctx *ctx, uint16_t extension_type, |
| 269 |
const uint8_t **out_data, size_t *out_len) { |
| 270 |
CBS extensions; |
| 271 |
|
| 272 |
CBS_init(&extensions, ctx->extensions, ctx->extensions_len); |
| 273 |
|
| 274 |
while (CBS_len(&extensions) != 0) { |
| 275 |
uint16_t type; |
| 276 |
CBS extension; |
| 277 |
|
| 278 |
/* Decode the next extension. */ |
| 279 |
if (!CBS_get_u16(&extensions, &type) || |
| 280 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
if (type == extension_type) { |
| 285 |
*out_data = CBS_data(&extension); |
| 286 |
*out_len = CBS_len(&extension); |
| 287 |
return 1; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
static const uint16_t kDefaultGroups[] = { |
| 295 |
SSL_GROUP_X25519, |
| 296 |
SSL_GROUP_SECP256R1, |
| 297 |
SSL_GROUP_SECP384R1, |
| 298 |
#if defined(BORINGSSL_ANDROID_SYSTEM) |
| 299 |
SSL_GROUP_SECP521R1, |
| 300 |
#endif |
| 301 |
}; |
| 302 |
|
| 303 |
/* tls1_get_grouplist sets |*out_group_ids| and |*out_group_ids_len| to the |
| 304 |
* list of allowed group IDs. If |get_peer_groups| is non-zero, return the |
| 305 |
* peer's group list. Otherwise, return the preferred list. */ |
| 306 |
static void tls1_get_grouplist(SSL *ssl, int get_peer_groups, |
| 307 |
const uint16_t **out_group_ids, |
| 308 |
size_t *out_group_ids_len) { |
| 309 |
if (get_peer_groups) { |
| 310 |
/* Only clients send a supported group list, so this function is only |
| 311 |
* called on the server. */ |
| 312 |
assert(ssl->server); |
| 313 |
*out_group_ids = ssl->s3->tmp.peer_supported_group_list; |
| 314 |
*out_group_ids_len = ssl->s3->tmp.peer_supported_group_list_len; |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
*out_group_ids = ssl->supported_group_list; |
| 319 |
*out_group_ids_len = ssl->supported_group_list_len; |
| 320 |
if (!*out_group_ids) { |
| 321 |
*out_group_ids = kDefaultGroups; |
| 322 |
*out_group_ids_len = sizeof(kDefaultGroups) / sizeof(kDefaultGroups[0]); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
int tls1_get_shared_group(SSL *ssl, uint16_t *out_group_id) { |
| 327 |
const uint16_t *groups, *peer_groups, *pref, *supp; |
| 328 |
size_t groups_len, peer_groups_len, pref_len, supp_len, i, j; |
| 329 |
|
| 330 |
/* Can't do anything on client side */ |
| 331 |
if (ssl->server == 0) { |
| 332 |
return 0; |
| 333 |
} |
| 334 |
|
| 335 |
tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len); |
| 336 |
tls1_get_grouplist(ssl, 1 /* peer groups */, &peer_groups, &peer_groups_len); |
| 337 |
|
| 338 |
if (peer_groups_len == 0) { |
| 339 |
/* Clients are not required to send a supported_groups extension. In this |
| 340 |
* case, the server is free to pick any group it likes. See RFC 4492, |
| 341 |
* section 4, paragraph 3. |
| 342 |
* |
| 343 |
* However, in the interests of compatibility, we will skip ECDH if the |
| 344 |
* client didn't send an extension because we can't be sure that they'll |
| 345 |
* support our favoured group. */ |
| 346 |
return 0; |
| 347 |
} |
| 348 |
|
| 349 |
if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 350 |
pref = groups; |
| 351 |
pref_len = groups_len; |
| 352 |
supp = peer_groups; |
| 353 |
supp_len = peer_groups_len; |
| 354 |
} else { |
| 355 |
pref = peer_groups; |
| 356 |
pref_len = peer_groups_len; |
| 357 |
supp = groups; |
| 358 |
supp_len = groups_len; |
| 359 |
} |
| 360 |
|
| 361 |
for (i = 0; i < pref_len; i++) { |
| 362 |
for (j = 0; j < supp_len; j++) { |
| 363 |
if (pref[i] == supp[j]) { |
| 364 |
*out_group_id = pref[i]; |
| 365 |
return 1; |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
return 0; |
| 371 |
} |
| 372 |
|
| 373 |
int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len, |
| 374 |
const int *curves, size_t ncurves) { |
| 375 |
uint16_t *group_ids; |
| 376 |
size_t i; |
| 377 |
|
| 378 |
group_ids = OPENSSL_malloc(ncurves * sizeof(uint16_t)); |
| 379 |
if (group_ids == NULL) { |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
for (i = 0; i < ncurves; i++) { |
| 384 |
if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) { |
| 385 |
OPENSSL_free(group_ids); |
| 386 |
return 0; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
OPENSSL_free(*out_group_ids); |
| 391 |
*out_group_ids = group_ids; |
| 392 |
*out_group_ids_len = ncurves; |
| 393 |
|
| 394 |
return 1; |
| 395 |
} |
| 396 |
|
| 397 |
/* tls1_curve_params_from_ec_key sets |*out_group_id| and |*out_comp_id| to the |
| 398 |
* TLS group ID and point format, respectively, for |ec|. It returns one on |
| 399 |
* success and zero on failure. */ |
| 400 |
static int tls1_curve_params_from_ec_key(uint16_t *out_group_id, |
| 401 |
uint8_t *out_comp_id, EC_KEY *ec) { |
| 402 |
int nid; |
| 403 |
uint16_t id; |
| 404 |
const EC_GROUP *grp; |
| 405 |
|
| 406 |
if (ec == NULL) { |
| 407 |
return 0; |
| 408 |
} |
| 409 |
|
| 410 |
grp = EC_KEY_get0_group(ec); |
| 411 |
if (grp == NULL) { |
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
/* Determine group ID */ |
| 416 |
nid = EC_GROUP_get_curve_name(grp); |
| 417 |
if (!ssl_nid_to_group_id(&id, nid)) { |
| 418 |
return 0; |
| 419 |
} |
| 420 |
|
| 421 |
/* Set the named group ID. Arbitrary explicit groups are not supported. */ |
| 422 |
*out_group_id = id; |
| 423 |
|
| 424 |
if (out_comp_id) { |
| 425 |
if (EC_KEY_get0_public_key(ec) == NULL) { |
| 426 |
return 0; |
| 427 |
} |
| 428 |
if (EC_KEY_get_conv_form(ec) == POINT_CONVERSION_COMPRESSED) { |
| 429 |
*out_comp_id = TLSEXT_ECPOINTFORMAT_ansiX962_compressed_prime; |
| 430 |
} else { |
| 431 |
*out_comp_id = TLSEXT_ECPOINTFORMAT_uncompressed; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
/* tls1_check_group_id returns one if |group_id| is consistent with both our |
| 439 |
* and the peer's group preferences. Note: if called as the client, only our |
| 440 |
* preferences are checked; the peer (the server) does not send preferences. */ |
| 441 |
int tls1_check_group_id(SSL *ssl, uint16_t group_id) { |
| 442 |
const uint16_t *groups; |
| 443 |
size_t groups_len, i, get_peer_groups; |
| 444 |
|
| 445 |
/* Check against our list, then the peer's list. */ |
| 446 |
for (get_peer_groups = 0; get_peer_groups <= 1; get_peer_groups++) { |
| 447 |
if (get_peer_groups && !ssl->server) { |
| 448 |
/* Servers do not present a preference list so, if we are a client, only |
| 449 |
* check our list. */ |
| 450 |
continue; |
| 451 |
} |
| 452 |
|
| 453 |
tls1_get_grouplist(ssl, get_peer_groups, &groups, &groups_len); |
| 454 |
if (get_peer_groups && groups_len == 0) { |
| 455 |
/* Clients are not required to send a supported_groups extension. In this |
| 456 |
* case, the server is free to pick any group it likes. See RFC 4492, |
| 457 |
* section 4, paragraph 3. */ |
| 458 |
continue; |
| 459 |
} |
| 460 |
for (i = 0; i < groups_len; i++) { |
| 461 |
if (groups[i] == group_id) { |
| 462 |
break; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if (i == groups_len) { |
| 467 |
return 0; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
return 1; |
| 472 |
} |
| 473 |
|
| 474 |
int tls1_check_ec_cert(SSL *ssl, X509 *x) { |
| 475 |
int ret = 0; |
| 476 |
EVP_PKEY *pkey = X509_get_pubkey(x); |
| 477 |
uint16_t group_id; |
| 478 |
uint8_t comp_id; |
| 479 |
|
| 480 |
if (!pkey) { |
| 481 |
goto done; |
| 482 |
} |
| 483 |
EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey); |
| 484 |
if (ec_key == NULL || |
| 485 |
!tls1_curve_params_from_ec_key(&group_id, &comp_id, ec_key) || |
| 486 |
!tls1_check_group_id(ssl, group_id) || |
| 487 |
comp_id != TLSEXT_ECPOINTFORMAT_uncompressed) { |
| 488 |
goto done; |
| 489 |
} |
| 490 |
|
| 491 |
ret = 1; |
| 492 |
|
| 493 |
done: |
| 494 |
EVP_PKEY_free(pkey); |
| 495 |
return ret; |
| 496 |
} |
| 497 |
|
| 498 |
/* List of supported signature algorithms and hashes. Should make this |
| 499 |
* customisable at some point, for now include everything we support. */ |
| 500 |
|
| 501 |
#define tlsext_sigalg_rsa(md) md, TLSEXT_signature_rsa, |
| 502 |
|
| 503 |
#define tlsext_sigalg_ecdsa(md) md, TLSEXT_signature_ecdsa, |
| 504 |
|
| 505 |
#define tlsext_sigalg(md) tlsext_sigalg_rsa(md) tlsext_sigalg_ecdsa(md) |
| 506 |
|
| 507 |
static const uint8_t tls12_sigalgs[] = { |
| 508 |
tlsext_sigalg(TLSEXT_hash_sha512) |
| 509 |
tlsext_sigalg(TLSEXT_hash_sha384) |
| 510 |
tlsext_sigalg(TLSEXT_hash_sha256) |
| 511 |
tlsext_sigalg(TLSEXT_hash_sha1) |
| 512 |
}; |
| 513 |
|
| 514 |
size_t tls12_get_psigalgs(SSL *ssl, const uint8_t **psigs) { |
| 515 |
*psigs = tls12_sigalgs; |
| 516 |
return sizeof(tls12_sigalgs); |
| 517 |
} |
| 518 |
|
| 519 |
int tls12_check_peer_sigalg(SSL *ssl, const EVP_MD **out_md, int *out_alert, |
| 520 |
uint8_t hash, uint8_t signature, EVP_PKEY *pkey) { |
| 521 |
const uint8_t *sent_sigs; |
| 522 |
size_t sent_sigslen, i; |
| 523 |
int sigalg = tls12_get_sigid(pkey->type); |
| 524 |
|
| 525 |
/* Should never happen */ |
| 526 |
if (sigalg == -1) { |
| 527 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 528 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
/* Check key type is consistent with signature */ |
| 533 |
if (sigalg != signature) { |
| 534 |
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 535 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 536 |
return 0; |
| 537 |
} |
| 538 |
|
| 539 |
/* Check signature matches a type we sent */ |
| 540 |
sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs); |
| 541 |
for (i = 0; i < sent_sigslen; i += 2, sent_sigs += 2) { |
| 542 |
if (hash == sent_sigs[0] && signature == sent_sigs[1]) { |
| 543 |
break; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if (i == sent_sigslen) { |
| 548 |
OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE); |
| 549 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 550 |
return 0; |
| 551 |
} |
| 552 |
|
| 553 |
*out_md = tls12_get_hash(hash); |
| 554 |
if (*out_md == NULL) { |
| 555 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_DIGEST); |
| 556 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 557 |
return 0; |
| 558 |
} |
| 559 |
|
| 560 |
return 1; |
| 561 |
} |
| 562 |
|
| 563 |
/* Get a mask of disabled algorithms: an algorithm is disabled if it isn't |
| 564 |
* supported or doesn't appear in supported signature algorithms. Unlike |
| 565 |
* ssl_cipher_get_disabled this applies to a specific session and not global |
| 566 |
* settings. */ |
| 567 |
void ssl_set_client_disabled(SSL *ssl) { |
| 568 |
CERT *c = ssl->cert; |
| 569 |
const uint8_t *sigalgs; |
| 570 |
size_t i, sigalgslen; |
| 571 |
int have_rsa = 0, have_ecdsa = 0; |
| 572 |
c->mask_a = 0; |
| 573 |
c->mask_k = 0; |
| 574 |
|
| 575 |
/* Now go through all signature algorithms seeing if we support any for RSA, |
| 576 |
* DSA, ECDSA. Do this for all versions not just TLS 1.2. */ |
| 577 |
sigalgslen = tls12_get_psigalgs(ssl, &sigalgs); |
| 578 |
for (i = 0; i < sigalgslen; i += 2, sigalgs += 2) { |
| 579 |
switch (sigalgs[1]) { |
| 580 |
case TLSEXT_signature_rsa: |
| 581 |
have_rsa = 1; |
| 582 |
break; |
| 583 |
|
| 584 |
case TLSEXT_signature_ecdsa: |
| 585 |
have_ecdsa = 1; |
| 586 |
break; |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/* Disable auth if we don't include any appropriate signature algorithms. */ |
| 591 |
if (!have_rsa) { |
| 592 |
c->mask_a |= SSL_aRSA; |
| 593 |
} |
| 594 |
if (!have_ecdsa) { |
| 595 |
c->mask_a |= SSL_aECDSA; |
| 596 |
} |
| 597 |
|
| 598 |
/* with PSK there must be client callback set */ |
| 599 |
if (!ssl->psk_client_callback) { |
| 600 |
c->mask_a |= SSL_aPSK; |
| 601 |
c->mask_k |= SSL_kPSK; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
/* tls_extension represents a TLS extension that is handled internally. The |
| 606 |
* |init| function is called for each handshake, before any other functions of |
| 607 |
* the extension. Then the add and parse callbacks are called as needed. |
| 608 |
* |
| 609 |
* The parse callbacks receive a |CBS| that contains the contents of the |
| 610 |
* extension (i.e. not including the type and length bytes). If an extension is |
| 611 |
* not received then the parse callbacks will be called with a NULL CBS so that |
| 612 |
* they can do any processing needed to handle the absence of an extension. |
| 613 |
* |
| 614 |
* The add callbacks receive a |CBB| to which the extension can be appended but |
| 615 |
* the function is responsible for appending the type and length bytes too. |
| 616 |
* |
| 617 |
* All callbacks return one for success and zero for error. If a parse function |
| 618 |
* returns zero then a fatal alert with value |*out_alert| will be sent. If |
| 619 |
* |*out_alert| isn't set, then a |decode_error| alert will be sent. */ |
| 620 |
struct tls_extension { |
| 621 |
uint16_t value; |
| 622 |
void (*init)(SSL *ssl); |
| 623 |
|
| 624 |
int (*add_clienthello)(SSL *ssl, CBB *out); |
| 625 |
int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 626 |
|
| 627 |
int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents); |
| 628 |
int (*add_serverhello)(SSL *ssl, CBB *out); |
| 629 |
}; |
| 630 |
|
| 631 |
|
| 632 |
/* Server name indication (SNI). |
| 633 |
* |
| 634 |
* https://tools.ietf.org/html/rfc6066#section-3. */ |
| 635 |
|
| 636 |
static void ext_sni_init(SSL *ssl) { |
| 637 |
ssl->s3->tmp.should_ack_sni = 0; |
| 638 |
} |
| 639 |
|
| 640 |
static int ext_sni_add_clienthello(SSL *ssl, CBB *out) { |
| 641 |
if (ssl->tlsext_hostname == NULL) { |
| 642 |
return 1; |
| 643 |
} |
| 644 |
|
| 645 |
CBB contents, server_name_list, name; |
| 646 |
if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 647 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 648 |
!CBB_add_u16_length_prefixed(&contents, &server_name_list) || |
| 649 |
!CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) || |
| 650 |
!CBB_add_u16_length_prefixed(&server_name_list, &name) || |
| 651 |
!CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname, |
| 652 |
strlen(ssl->tlsext_hostname)) || |
| 653 |
!CBB_flush(out)) { |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
return 1; |
| 658 |
} |
| 659 |
|
| 660 |
static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 661 |
CBS *contents) { |
| 662 |
if (contents == NULL) { |
| 663 |
return 1; |
| 664 |
} |
| 665 |
|
| 666 |
if (CBS_len(contents) != 0) { |
| 667 |
return 0; |
| 668 |
} |
| 669 |
|
| 670 |
assert(ssl->tlsext_hostname != NULL); |
| 671 |
|
| 672 |
if (!ssl->hit) { |
| 673 |
assert(ssl->session->tlsext_hostname == NULL); |
| 674 |
ssl->session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname); |
| 675 |
if (!ssl->session->tlsext_hostname) { |
| 676 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 677 |
return 0; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
return 1; |
| 682 |
} |
| 683 |
|
| 684 |
static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 685 |
CBS *contents) { |
| 686 |
if (contents == NULL) { |
| 687 |
return 1; |
| 688 |
} |
| 689 |
|
| 690 |
CBS server_name_list, host_name; |
| 691 |
uint8_t name_type; |
| 692 |
if (!CBS_get_u16_length_prefixed(contents, &server_name_list) || |
| 693 |
!CBS_get_u8(&server_name_list, &name_type) || |
| 694 |
/* Although the server_name extension was intended to be extensible to |
| 695 |
* new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
| 696 |
* different name types will cause an error. Further, RFC 4366 originally |
| 697 |
* defined syntax inextensibly. RFC 6066 corrected this mistake, but |
| 698 |
* adding new name types is no longer feasible. |
| 699 |
* |
| 700 |
* Act as if the extensibility does not exist to simplify parsing. */ |
| 701 |
!CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
| 702 |
CBS_len(&server_name_list) != 0 || |
| 703 |
CBS_len(contents) != 0) { |
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
if (name_type != TLSEXT_NAMETYPE_host_name || |
| 708 |
CBS_len(&host_name) == 0 || |
| 709 |
CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
| 710 |
CBS_contains_zero_byte(&host_name)) { |
| 711 |
*out_alert = SSL_AD_UNRECOGNIZED_NAME; |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
/* TODO(davidben): SNI should be resolved before resumption. We have the |
| 716 |
* early callback as a replacement, but we should fix the current callback |
| 717 |
* and avoid the need for |SSL_CTX_set_session_id_context|. */ |
| 718 |
if (!ssl->hit) { |
| 719 |
assert(ssl->session->tlsext_hostname == NULL); |
| 720 |
|
| 721 |
/* Copy the hostname as a string. */ |
| 722 |
if (!CBS_strdup(&host_name, &ssl->session->tlsext_hostname)) { |
| 723 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 724 |
return 0; |
| 725 |
} |
| 726 |
|
| 727 |
ssl->s3->tmp.should_ack_sni = 1; |
| 728 |
} |
| 729 |
|
| 730 |
return 1; |
| 731 |
} |
| 732 |
|
| 733 |
static int ext_sni_add_serverhello(SSL *ssl, CBB *out) { |
| 734 |
if (ssl->hit || |
| 735 |
!ssl->s3->tmp.should_ack_sni || |
| 736 |
ssl->session->tlsext_hostname == NULL) { |
| 737 |
return 1; |
| 738 |
} |
| 739 |
|
| 740 |
if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) || |
| 741 |
!CBB_add_u16(out, 0 /* length */)) { |
| 742 |
return 0; |
| 743 |
} |
| 744 |
|
| 745 |
return 1; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
/* Renegotiation indication. |
| 750 |
* |
| 751 |
* https://tools.ietf.org/html/rfc5746 */ |
| 752 |
|
| 753 |
static int ext_ri_add_clienthello(SSL *ssl, CBB *out) { |
| 754 |
CBB contents, prev_finished; |
| 755 |
if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 756 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 757 |
!CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 758 |
!CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 759 |
ssl->s3->previous_client_finished_len) || |
| 760 |
!CBB_flush(out)) { |
| 761 |
return 0; |
| 762 |
} |
| 763 |
|
| 764 |
return 1; |
| 765 |
} |
| 766 |
|
| 767 |
static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 768 |
CBS *contents) { |
| 769 |
/* Servers may not switch between omitting the extension and supporting it. |
| 770 |
* See RFC 5746, sections 3.5 and 4.2. */ |
| 771 |
if (ssl->s3->initial_handshake_complete && |
| 772 |
(contents != NULL) != ssl->s3->send_connection_binding) { |
| 773 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 774 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 775 |
return 0; |
| 776 |
} |
| 777 |
|
| 778 |
if (contents == NULL) { |
| 779 |
/* Strictly speaking, if we want to avoid an attack we should *always* see |
| 780 |
* RI even on initial ServerHello because the client doesn't see any |
| 781 |
* renegotiation during an attack. However this would mean we could not |
| 782 |
* connect to any server which doesn't support RI. |
| 783 |
* |
| 784 |
* OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in |
| 785 |
* practical terms every client sets it so it's just assumed here. */ |
| 786 |
return 1; |
| 787 |
} |
| 788 |
|
| 789 |
const size_t expected_len = ssl->s3->previous_client_finished_len + |
| 790 |
ssl->s3->previous_server_finished_len; |
| 791 |
|
| 792 |
/* Check for logic errors */ |
| 793 |
assert(!expected_len || ssl->s3->previous_client_finished_len); |
| 794 |
assert(!expected_len || ssl->s3->previous_server_finished_len); |
| 795 |
|
| 796 |
/* Parse out the extension contents. */ |
| 797 |
CBS renegotiated_connection; |
| 798 |
if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 799 |
CBS_len(contents) != 0) { |
| 800 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 801 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 802 |
return 0; |
| 803 |
} |
| 804 |
|
| 805 |
/* Check that the extension matches. */ |
| 806 |
if (CBS_len(&renegotiated_connection) != expected_len) { |
| 807 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 808 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 809 |
return 0; |
| 810 |
} |
| 811 |
|
| 812 |
const uint8_t *d = CBS_data(&renegotiated_connection); |
| 813 |
if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished, |
| 814 |
ssl->s3->previous_client_finished_len)) { |
| 815 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 816 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 817 |
return 0; |
| 818 |
} |
| 819 |
d += ssl->s3->previous_client_finished_len; |
| 820 |
|
| 821 |
if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished, |
| 822 |
ssl->s3->previous_server_finished_len)) { |
| 823 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 824 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 825 |
return 0; |
| 826 |
} |
| 827 |
ssl->s3->send_connection_binding = 1; |
| 828 |
|
| 829 |
return 1; |
| 830 |
} |
| 831 |
|
| 832 |
static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 833 |
CBS *contents) { |
| 834 |
/* Renegotiation isn't supported as a server so this function should never be |
| 835 |
* called after the initial handshake. */ |
| 836 |
assert(!ssl->s3->initial_handshake_complete); |
| 837 |
|
| 838 |
CBS fake_contents; |
| 839 |
static const uint8_t kFakeExtension[] = {0}; |
| 840 |
|
| 841 |
if (contents == NULL) { |
| 842 |
if (ssl->s3->send_connection_binding) { |
| 843 |
/* The renegotiation SCSV was received so pretend that we received a |
| 844 |
* renegotiation extension. */ |
| 845 |
CBS_init(&fake_contents, kFakeExtension, sizeof(kFakeExtension)); |
| 846 |
contents = &fake_contents; |
| 847 |
/* We require that the renegotiation extension is at index zero of |
| 848 |
* kExtensions. */ |
| 849 |
ssl->s3->tmp.extensions.received |= (1u << 0); |
| 850 |
} else { |
| 851 |
return 1; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
CBS renegotiated_connection; |
| 856 |
|
| 857 |
if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) || |
| 858 |
CBS_len(contents) != 0) { |
| 859 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR); |
| 860 |
return 0; |
| 861 |
} |
| 862 |
|
| 863 |
/* Check that the extension matches */ |
| 864 |
if (!CBS_mem_equal(&renegotiated_connection, |
| 865 |
ssl->s3->previous_client_finished, |
| 866 |
ssl->s3->previous_client_finished_len)) { |
| 867 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH); |
| 868 |
*out_alert = SSL_AD_HANDSHAKE_FAILURE; |
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
ssl->s3->send_connection_binding = 1; |
| 873 |
|
| 874 |
return 1; |
| 875 |
} |
| 876 |
|
| 877 |
static int ext_ri_add_serverhello(SSL *ssl, CBB *out) { |
| 878 |
CBB contents, prev_finished; |
| 879 |
if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) || |
| 880 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 881 |
!CBB_add_u8_length_prefixed(&contents, &prev_finished) || |
| 882 |
!CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished, |
| 883 |
ssl->s3->previous_client_finished_len) || |
| 884 |
!CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished, |
| 885 |
ssl->s3->previous_server_finished_len) || |
| 886 |
!CBB_flush(out)) { |
| 887 |
return 0; |
| 888 |
} |
| 889 |
|
| 890 |
return 1; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
/* Extended Master Secret. |
| 895 |
* |
| 896 |
* https://tools.ietf.org/html/rfc7627 */ |
| 897 |
|
| 898 |
static void ext_ems_init(SSL *ssl) { |
| 899 |
ssl->s3->tmp.extended_master_secret = 0; |
| 900 |
} |
| 901 |
|
| 902 |
static int ext_ems_add_clienthello(SSL *ssl, CBB *out) { |
| 903 |
if (ssl->version == SSL3_VERSION) { |
| 904 |
return 1; |
| 905 |
} |
| 906 |
|
| 907 |
if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 908 |
!CBB_add_u16(out, 0 /* length */)) { |
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
return 1; |
| 913 |
} |
| 914 |
|
| 915 |
static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 916 |
CBS *contents) { |
| 917 |
if (contents == NULL) { |
| 918 |
return 1; |
| 919 |
} |
| 920 |
|
| 921 |
if (ssl->version == SSL3_VERSION || CBS_len(contents) != 0) { |
| 922 |
return 0; |
| 923 |
} |
| 924 |
|
| 925 |
ssl->s3->tmp.extended_master_secret = 1; |
| 926 |
return 1; |
| 927 |
} |
| 928 |
|
| 929 |
static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 930 |
CBS *contents) { |
| 931 |
if (ssl->version == SSL3_VERSION || contents == NULL) { |
| 932 |
return 1; |
| 933 |
} |
| 934 |
|
| 935 |
if (CBS_len(contents) != 0) { |
| 936 |
return 0; |
| 937 |
} |
| 938 |
|
| 939 |
ssl->s3->tmp.extended_master_secret = 1; |
| 940 |
return 1; |
| 941 |
} |
| 942 |
|
| 943 |
static int ext_ems_add_serverhello(SSL *ssl, CBB *out) { |
| 944 |
if (!ssl->s3->tmp.extended_master_secret) { |
| 945 |
return 1; |
| 946 |
} |
| 947 |
|
| 948 |
if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) || |
| 949 |
!CBB_add_u16(out, 0 /* length */)) { |
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
return 1; |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
/* Session tickets. |
| 958 |
* |
| 959 |
* https://tools.ietf.org/html/rfc5077 */ |
| 960 |
|
| 961 |
static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) { |
| 962 |
if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) { |
| 963 |
return 1; |
| 964 |
} |
| 965 |
|
| 966 |
const uint8_t *ticket_data = NULL; |
| 967 |
int ticket_len = 0; |
| 968 |
|
| 969 |
/* Renegotiation does not participate in session resumption. However, still |
| 970 |
* advertise the extension to avoid potentially breaking servers which carry |
| 971 |
* over the state from the previous handshake, such as OpenSSL servers |
| 972 |
* without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */ |
| 973 |
if (!ssl->s3->initial_handshake_complete && |
| 974 |
ssl->session != NULL && |
| 975 |
ssl->session->tlsext_tick != NULL) { |
| 976 |
ticket_data = ssl->session->tlsext_tick; |
| 977 |
ticket_len = ssl->session->tlsext_ticklen; |
| 978 |
} |
| 979 |
|
| 980 |
CBB ticket; |
| 981 |
if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 982 |
!CBB_add_u16_length_prefixed(out, &ticket) || |
| 983 |
!CBB_add_bytes(&ticket, ticket_data, ticket_len) || |
| 984 |
!CBB_flush(out)) { |
| 985 |
return 0; |
| 986 |
} |
| 987 |
|
| 988 |
return 1; |
| 989 |
} |
| 990 |
|
| 991 |
static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 992 |
CBS *contents) { |
| 993 |
ssl->tlsext_ticket_expected = 0; |
| 994 |
|
| 995 |
if (contents == NULL) { |
| 996 |
return 1; |
| 997 |
} |
| 998 |
|
| 999 |
/* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and |
| 1000 |
* this function should never be called, even if the server tries to send the |
| 1001 |
* extension. */ |
| 1002 |
assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1003 |
|
| 1004 |
if (CBS_len(contents) != 0) { |
| 1005 |
return 0; |
| 1006 |
} |
| 1007 |
|
| 1008 |
ssl->tlsext_ticket_expected = 1; |
| 1009 |
return 1; |
| 1010 |
} |
| 1011 |
|
| 1012 |
static int ext_ticket_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1013 |
CBS *contents) { |
| 1014 |
/* This function isn't used because the ticket extension from the client is |
| 1015 |
* handled in ssl_session.c. */ |
| 1016 |
return 1; |
| 1017 |
} |
| 1018 |
|
| 1019 |
static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) { |
| 1020 |
if (!ssl->tlsext_ticket_expected) { |
| 1021 |
return 1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be |
| 1025 |
* true. */ |
| 1026 |
assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0); |
| 1027 |
|
| 1028 |
if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) || |
| 1029 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
return 1; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
/* Signature Algorithms. |
| 1038 |
* |
| 1039 |
* https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */ |
| 1040 |
|
| 1041 |
static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) { |
| 1042 |
if (ssl3_version_from_wire(ssl, ssl->client_version) < TLS1_2_VERSION) { |
| 1043 |
return 1; |
| 1044 |
} |
| 1045 |
|
| 1046 |
const uint8_t *sigalgs_data; |
| 1047 |
const size_t sigalgs_len = tls12_get_psigalgs(ssl, &sigalgs_data); |
| 1048 |
|
| 1049 |
CBB contents, sigalgs; |
| 1050 |
if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) || |
| 1051 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1052 |
!CBB_add_u16_length_prefixed(&contents, &sigalgs) || |
| 1053 |
!CBB_add_bytes(&sigalgs, sigalgs_data, sigalgs_len) || |
| 1054 |
!CBB_flush(out)) { |
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
return 1; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static int ext_sigalgs_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1062 |
CBS *contents) { |
| 1063 |
if (contents != NULL) { |
| 1064 |
/* Servers MUST NOT send this extension. */ |
| 1065 |
*out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 1066 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SIGNATURE_ALGORITHMS_EXTENSION_SENT_BY_SERVER); |
| 1067 |
return 0; |
| 1068 |
} |
| 1069 |
|
| 1070 |
return 1; |
| 1071 |
} |
| 1072 |
|
| 1073 |
static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1074 |
CBS *contents) { |
| 1075 |
OPENSSL_free(ssl->cert->peer_sigalgs); |
| 1076 |
ssl->cert->peer_sigalgs = NULL; |
| 1077 |
ssl->cert->peer_sigalgslen = 0; |
| 1078 |
|
| 1079 |
if (contents == NULL) { |
| 1080 |
return 1; |
| 1081 |
} |
| 1082 |
|
| 1083 |
CBS supported_signature_algorithms; |
| 1084 |
if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) || |
| 1085 |
CBS_len(contents) != 0 || |
| 1086 |
CBS_len(&supported_signature_algorithms) == 0 || |
| 1087 |
!tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) { |
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
|
| 1091 |
return 1; |
| 1092 |
} |
| 1093 |
|
| 1094 |
static int ext_sigalgs_add_serverhello(SSL *ssl, CBB *out) { |
| 1095 |
/* Servers MUST NOT send this extension. */ |
| 1096 |
return 1; |
| 1097 |
} |
| 1098 |
|
| 1099 |
|
| 1100 |
/* OCSP Stapling. |
| 1101 |
* |
| 1102 |
* https://tools.ietf.org/html/rfc6066#section-8 */ |
| 1103 |
|
| 1104 |
static void ext_ocsp_init(SSL *ssl) { |
| 1105 |
ssl->s3->tmp.certificate_status_expected = 0; |
| 1106 |
ssl->tlsext_status_type = -1; |
| 1107 |
} |
| 1108 |
|
| 1109 |
static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) { |
| 1110 |
if (!ssl->ocsp_stapling_enabled) { |
| 1111 |
return 1; |
| 1112 |
} |
| 1113 |
|
| 1114 |
CBB contents; |
| 1115 |
if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) || |
| 1116 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1117 |
!CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) || |
| 1118 |
!CBB_add_u16(&contents, 0 /* empty responder ID list */) || |
| 1119 |
!CBB_add_u16(&contents, 0 /* empty request extensions */) || |
| 1120 |
!CBB_flush(out)) { |
| 1121 |
return 0; |
| 1122 |
} |
| 1123 |
|
| 1124 |
ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp; |
| 1125 |
return 1; |
| 1126 |
} |
| 1127 |
|
| 1128 |
static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1129 |
CBS *contents) { |
| 1130 |
if (contents == NULL) { |
| 1131 |
return 1; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (CBS_len(contents) != 0) { |
| 1135 |
return 0; |
| 1136 |
} |
| 1137 |
|
| 1138 |
ssl->s3->tmp.certificate_status_expected = 1; |
| 1139 |
return 1; |
| 1140 |
} |
| 1141 |
|
| 1142 |
static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1143 |
CBS *contents) { |
| 1144 |
if (contents == NULL) { |
| 1145 |
return 1; |
| 1146 |
} |
| 1147 |
|
| 1148 |
uint8_t status_type; |
| 1149 |
if (!CBS_get_u8(contents, &status_type)) { |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* We cannot decide whether OCSP stapling will occur yet because the correct |
| 1154 |
* SSL_CTX might not have been selected. */ |
| 1155 |
ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp; |
| 1156 |
|
| 1157 |
return 1; |
| 1158 |
} |
| 1159 |
|
| 1160 |
static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) { |
| 1161 |
/* The extension shouldn't be sent when resuming sessions. */ |
| 1162 |
if (ssl->hit || |
| 1163 |
!ssl->s3->tmp.ocsp_stapling_requested || |
| 1164 |
ssl->ctx->ocsp_response_length == 0) { |
| 1165 |
return 1; |
| 1166 |
} |
| 1167 |
|
| 1168 |
ssl->s3->tmp.certificate_status_expected = 1; |
| 1169 |
|
| 1170 |
return CBB_add_u16(out, TLSEXT_TYPE_status_request) && |
| 1171 |
CBB_add_u16(out, 0 /* length */); |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
/* Next protocol negotiation. |
| 1176 |
* |
| 1177 |
* https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */ |
| 1178 |
|
| 1179 |
static void ext_npn_init(SSL *ssl) { |
| 1180 |
ssl->s3->next_proto_neg_seen = 0; |
| 1181 |
} |
| 1182 |
|
| 1183 |
static int ext_npn_add_clienthello(SSL *ssl, CBB *out) { |
| 1184 |
if (ssl->s3->initial_handshake_complete || |
| 1185 |
ssl->ctx->next_proto_select_cb == NULL || |
| 1186 |
(ssl->options & SSL_OP_DISABLE_NPN) || |
| 1187 |
SSL_IS_DTLS(ssl)) { |
| 1188 |
return 1; |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1192 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1193 |
return 0; |
| 1194 |
} |
| 1195 |
|
| 1196 |
return 1; |
| 1197 |
} |
| 1198 |
|
| 1199 |
static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1200 |
CBS *contents) { |
| 1201 |
if (contents == NULL) { |
| 1202 |
return 1; |
| 1203 |
} |
| 1204 |
|
| 1205 |
/* If any of these are false then we should never have sent the NPN |
| 1206 |
* extension in the ClientHello and thus this function should never have been |
| 1207 |
* called. */ |
| 1208 |
assert(!ssl->s3->initial_handshake_complete); |
| 1209 |
assert(!SSL_IS_DTLS(ssl)); |
| 1210 |
assert(ssl->ctx->next_proto_select_cb != NULL); |
| 1211 |
assert(!(ssl->options & SSL_OP_DISABLE_NPN)); |
| 1212 |
|
| 1213 |
if (ssl->s3->alpn_selected != NULL) { |
| 1214 |
/* NPN and ALPN may not be negotiated in the same connection. */ |
| 1215 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1216 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1217 |
return 0; |
| 1218 |
} |
| 1219 |
|
| 1220 |
const uint8_t *const orig_contents = CBS_data(contents); |
| 1221 |
const size_t orig_len = CBS_len(contents); |
| 1222 |
|
| 1223 |
while (CBS_len(contents) != 0) { |
| 1224 |
CBS proto; |
| 1225 |
if (!CBS_get_u8_length_prefixed(contents, &proto) || |
| 1226 |
CBS_len(&proto) == 0) { |
| 1227 |
return 0; |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
uint8_t *selected; |
| 1232 |
uint8_t selected_len; |
| 1233 |
if (ssl->ctx->next_proto_select_cb( |
| 1234 |
ssl, &selected, &selected_len, orig_contents, orig_len, |
| 1235 |
ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) { |
| 1236 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1237 |
return 0; |
| 1238 |
} |
| 1239 |
|
| 1240 |
OPENSSL_free(ssl->s3->next_proto_negotiated); |
| 1241 |
ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len); |
| 1242 |
if (ssl->s3->next_proto_negotiated == NULL) { |
| 1243 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1244 |
return 0; |
| 1245 |
} |
| 1246 |
|
| 1247 |
ssl->s3->next_proto_negotiated_len = selected_len; |
| 1248 |
ssl->s3->next_proto_neg_seen = 1; |
| 1249 |
|
| 1250 |
return 1; |
| 1251 |
} |
| 1252 |
|
| 1253 |
static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1254 |
CBS *contents) { |
| 1255 |
if (contents != NULL && CBS_len(contents) != 0) { |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (contents == NULL || |
| 1260 |
ssl->s3->initial_handshake_complete || |
| 1261 |
/* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen |
| 1262 |
* afterwards, parsing the ALPN extension will clear |
| 1263 |
* |next_proto_neg_seen|. */ |
| 1264 |
ssl->s3->alpn_selected != NULL || |
| 1265 |
ssl->ctx->next_protos_advertised_cb == NULL || |
| 1266 |
SSL_IS_DTLS(ssl)) { |
| 1267 |
return 1; |
| 1268 |
} |
| 1269 |
|
| 1270 |
ssl->s3->next_proto_neg_seen = 1; |
| 1271 |
return 1; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static int ext_npn_add_serverhello(SSL *ssl, CBB *out) { |
| 1275 |
/* |next_proto_neg_seen| might have been cleared when an ALPN extension was |
| 1276 |
* parsed. */ |
| 1277 |
if (!ssl->s3->next_proto_neg_seen) { |
| 1278 |
return 1; |
| 1279 |
} |
| 1280 |
|
| 1281 |
const uint8_t *npa; |
| 1282 |
unsigned npa_len; |
| 1283 |
|
| 1284 |
if (ssl->ctx->next_protos_advertised_cb( |
| 1285 |
ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) != |
| 1286 |
SSL_TLSEXT_ERR_OK) { |
| 1287 |
ssl->s3->next_proto_neg_seen = 0; |
| 1288 |
return 1; |
| 1289 |
} |
| 1290 |
|
| 1291 |
CBB contents; |
| 1292 |
if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) || |
| 1293 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1294 |
!CBB_add_bytes(&contents, npa, npa_len) || |
| 1295 |
!CBB_flush(out)) { |
| 1296 |
return 0; |
| 1297 |
} |
| 1298 |
|
| 1299 |
return 1; |
| 1300 |
} |
| 1301 |
|
| 1302 |
|
| 1303 |
/* Signed certificate timestamps. |
| 1304 |
* |
| 1305 |
* https://tools.ietf.org/html/rfc6962#section-3.3.1 */ |
| 1306 |
|
| 1307 |
static int ext_sct_add_clienthello(SSL *ssl, CBB *out) { |
| 1308 |
if (!ssl->signed_cert_timestamps_enabled) { |
| 1309 |
return 1; |
| 1310 |
} |
| 1311 |
|
| 1312 |
if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) || |
| 1313 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1314 |
return 0; |
| 1315 |
} |
| 1316 |
|
| 1317 |
return 1; |
| 1318 |
} |
| 1319 |
|
| 1320 |
static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1321 |
CBS *contents) { |
| 1322 |
if (contents == NULL) { |
| 1323 |
return 1; |
| 1324 |
} |
| 1325 |
|
| 1326 |
/* If this is false then we should never have sent the SCT extension in the |
| 1327 |
* ClientHello and thus this function should never have been called. */ |
| 1328 |
assert(ssl->signed_cert_timestamps_enabled); |
| 1329 |
|
| 1330 |
if (CBS_len(contents) == 0) { |
| 1331 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 1332 |
return 0; |
| 1333 |
} |
| 1334 |
|
| 1335 |
/* Session resumption uses the original session information. */ |
| 1336 |
if (!ssl->hit && |
| 1337 |
!CBS_stow(contents, &ssl->session->tlsext_signed_cert_timestamp_list, |
| 1338 |
&ssl->session->tlsext_signed_cert_timestamp_list_length)) { |
| 1339 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1340 |
return 0; |
| 1341 |
} |
| 1342 |
|
| 1343 |
return 1; |
| 1344 |
} |
| 1345 |
|
| 1346 |
static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1347 |
CBS *contents) { |
| 1348 |
return contents == NULL || CBS_len(contents) == 0; |
| 1349 |
} |
| 1350 |
|
| 1351 |
static int ext_sct_add_serverhello(SSL *ssl, CBB *out) { |
| 1352 |
/* The extension shouldn't be sent when resuming sessions. */ |
| 1353 |
if (ssl->hit || |
| 1354 |
ssl->ctx->signed_cert_timestamp_list_length == 0) { |
| 1355 |
return 1; |
| 1356 |
} |
| 1357 |
|
| 1358 |
CBB contents; |
| 1359 |
return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) && |
| 1360 |
CBB_add_u16_length_prefixed(out, &contents) && |
| 1361 |
CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list, |
| 1362 |
ssl->ctx->signed_cert_timestamp_list_length) && |
| 1363 |
CBB_flush(out); |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
/* Application-level Protocol Negotiation. |
| 1368 |
* |
| 1369 |
* https://tools.ietf.org/html/rfc7301 */ |
| 1370 |
|
| 1371 |
static void ext_alpn_init(SSL *ssl) { |
| 1372 |
OPENSSL_free(ssl->s3->alpn_selected); |
| 1373 |
ssl->s3->alpn_selected = NULL; |
| 1374 |
} |
| 1375 |
|
| 1376 |
static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) { |
| 1377 |
if (ssl->alpn_client_proto_list == NULL || |
| 1378 |
ssl->s3->initial_handshake_complete) { |
| 1379 |
return 1; |
| 1380 |
} |
| 1381 |
|
| 1382 |
CBB contents, proto_list; |
| 1383 |
if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1384 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1385 |
!CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1386 |
!CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list, |
| 1387 |
ssl->alpn_client_proto_list_len) || |
| 1388 |
!CBB_flush(out)) { |
| 1389 |
return 0; |
| 1390 |
} |
| 1391 |
|
| 1392 |
return 1; |
| 1393 |
} |
| 1394 |
|
| 1395 |
static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1396 |
CBS *contents) { |
| 1397 |
if (contents == NULL) { |
| 1398 |
return 1; |
| 1399 |
} |
| 1400 |
|
| 1401 |
assert(!ssl->s3->initial_handshake_complete); |
| 1402 |
assert(ssl->alpn_client_proto_list != NULL); |
| 1403 |
|
| 1404 |
if (ssl->s3->next_proto_neg_seen) { |
| 1405 |
/* NPN and ALPN may not be negotiated in the same connection. */ |
| 1406 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1407 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN); |
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* The extension data consists of a ProtocolNameList which must have |
| 1412 |
* exactly one ProtocolName. Each of these is length-prefixed. */ |
| 1413 |
CBS protocol_name_list, protocol_name; |
| 1414 |
if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1415 |
CBS_len(contents) != 0 || |
| 1416 |
!CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) || |
| 1417 |
/* Empty protocol names are forbidden. */ |
| 1418 |
CBS_len(&protocol_name) == 0 || |
| 1419 |
CBS_len(&protocol_name_list) != 0) { |
| 1420 |
return 0; |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected, |
| 1424 |
&ssl->s3->alpn_selected_len)) { |
| 1425 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1426 |
return 0; |
| 1427 |
} |
| 1428 |
|
| 1429 |
return 1; |
| 1430 |
} |
| 1431 |
|
| 1432 |
static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1433 |
CBS *contents) { |
| 1434 |
if (contents == NULL) { |
| 1435 |
return 1; |
| 1436 |
} |
| 1437 |
|
| 1438 |
if (ssl->ctx->alpn_select_cb == NULL || |
| 1439 |
ssl->s3->initial_handshake_complete) { |
| 1440 |
return 1; |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* ALPN takes precedence over NPN. */ |
| 1444 |
ssl->s3->next_proto_neg_seen = 0; |
| 1445 |
|
| 1446 |
CBS protocol_name_list; |
| 1447 |
if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) || |
| 1448 |
CBS_len(contents) != 0 || |
| 1449 |
CBS_len(&protocol_name_list) < 2) { |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
|
| 1453 |
/* Validate the protocol list. */ |
| 1454 |
CBS protocol_name_list_copy = protocol_name_list; |
| 1455 |
while (CBS_len(&protocol_name_list_copy) > 0) { |
| 1456 |
CBS protocol_name; |
| 1457 |
|
| 1458 |
if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) || |
| 1459 |
/* Empty protocol names are forbidden. */ |
| 1460 |
CBS_len(&protocol_name) == 0) { |
| 1461 |
return 0; |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
const uint8_t *selected; |
| 1466 |
uint8_t selected_len; |
| 1467 |
if (ssl->ctx->alpn_select_cb( |
| 1468 |
ssl, &selected, &selected_len, CBS_data(&protocol_name_list), |
| 1469 |
CBS_len(&protocol_name_list), |
| 1470 |
ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) { |
| 1471 |
OPENSSL_free(ssl->s3->alpn_selected); |
| 1472 |
ssl->s3->alpn_selected = BUF_memdup(selected, selected_len); |
| 1473 |
if (ssl->s3->alpn_selected == NULL) { |
| 1474 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1475 |
return 0; |
| 1476 |
} |
| 1477 |
ssl->s3->alpn_selected_len = selected_len; |
| 1478 |
} |
| 1479 |
|
| 1480 |
return 1; |
| 1481 |
} |
| 1482 |
|
| 1483 |
static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) { |
| 1484 |
if (ssl->s3->alpn_selected == NULL) { |
| 1485 |
return 1; |
| 1486 |
} |
| 1487 |
|
| 1488 |
CBB contents, proto_list, proto; |
| 1489 |
if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) || |
| 1490 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1491 |
!CBB_add_u16_length_prefixed(&contents, &proto_list) || |
| 1492 |
!CBB_add_u8_length_prefixed(&proto_list, &proto) || |
| 1493 |
!CBB_add_bytes(&proto, ssl->s3->alpn_selected, |
| 1494 |
ssl->s3->alpn_selected_len) || |
| 1495 |
!CBB_flush(out)) { |
| 1496 |
return 0; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return 1; |
| 1500 |
} |
| 1501 |
|
| 1502 |
|
| 1503 |
/* Channel ID. |
| 1504 |
* |
| 1505 |
* https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */ |
| 1506 |
|
| 1507 |
static void ext_channel_id_init(SSL *ssl) { |
| 1508 |
ssl->s3->tlsext_channel_id_valid = 0; |
| 1509 |
} |
| 1510 |
|
| 1511 |
static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) { |
| 1512 |
if (!ssl->tlsext_channel_id_enabled || |
| 1513 |
SSL_IS_DTLS(ssl)) { |
| 1514 |
return 1; |
| 1515 |
} |
| 1516 |
|
| 1517 |
if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1518 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1519 |
return 0; |
| 1520 |
} |
| 1521 |
|
| 1522 |
return 1; |
| 1523 |
} |
| 1524 |
|
| 1525 |
static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1526 |
CBS *contents) { |
| 1527 |
if (contents == NULL) { |
| 1528 |
return 1; |
| 1529 |
} |
| 1530 |
|
| 1531 |
assert(!SSL_IS_DTLS(ssl)); |
| 1532 |
assert(ssl->tlsext_channel_id_enabled); |
| 1533 |
|
| 1534 |
if (CBS_len(contents) != 0) { |
| 1535 |
return 0; |
| 1536 |
} |
| 1537 |
|
| 1538 |
ssl->s3->tlsext_channel_id_valid = 1; |
| 1539 |
return 1; |
| 1540 |
} |
| 1541 |
|
| 1542 |
static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1543 |
CBS *contents) { |
| 1544 |
if (contents == NULL || |
| 1545 |
!ssl->tlsext_channel_id_enabled || |
| 1546 |
SSL_IS_DTLS(ssl)) { |
| 1547 |
return 1; |
| 1548 |
} |
| 1549 |
|
| 1550 |
if (CBS_len(contents) != 0) { |
| 1551 |
return 0; |
| 1552 |
} |
| 1553 |
|
| 1554 |
ssl->s3->tlsext_channel_id_valid = 1; |
| 1555 |
return 1; |
| 1556 |
} |
| 1557 |
|
| 1558 |
static int ext_channel_id_add_serverhello(SSL *ssl, CBB *out) { |
| 1559 |
if (!ssl->s3->tlsext_channel_id_valid) { |
| 1560 |
return 1; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) || |
| 1564 |
!CBB_add_u16(out, 0 /* length */)) { |
| 1565 |
return 0; |
| 1566 |
} |
| 1567 |
|
| 1568 |
return 1; |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
/* Secure Real-time Transport Protocol (SRTP) extension. |
| 1573 |
* |
| 1574 |
* https://tools.ietf.org/html/rfc5764 */ |
| 1575 |
|
| 1576 |
|
| 1577 |
static void ext_srtp_init(SSL *ssl) { |
| 1578 |
ssl->srtp_profile = NULL; |
| 1579 |
} |
| 1580 |
|
| 1581 |
static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) { |
| 1582 |
STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1583 |
if (profiles == NULL) { |
| 1584 |
return 1; |
| 1585 |
} |
| 1586 |
const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles); |
| 1587 |
if (num_profiles == 0) { |
| 1588 |
return 1; |
| 1589 |
} |
| 1590 |
|
| 1591 |
CBB contents, profile_ids; |
| 1592 |
if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1593 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1594 |
!CBB_add_u16_length_prefixed(&contents, &profile_ids)) { |
| 1595 |
return 0; |
| 1596 |
} |
| 1597 |
|
| 1598 |
size_t i; |
| 1599 |
for (i = 0; i < num_profiles; i++) { |
| 1600 |
if (!CBB_add_u16(&profile_ids, |
| 1601 |
sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) { |
| 1602 |
return 0; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
|
| 1606 |
if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) || |
| 1607 |
!CBB_flush(out)) { |
| 1608 |
return 0; |
| 1609 |
} |
| 1610 |
|
| 1611 |
return 1; |
| 1612 |
} |
| 1613 |
|
| 1614 |
static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1615 |
CBS *contents) { |
| 1616 |
if (contents == NULL) { |
| 1617 |
return 1; |
| 1618 |
} |
| 1619 |
|
| 1620 |
/* The extension consists of a u16-prefixed profile ID list containing a |
| 1621 |
* single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field. |
| 1622 |
* |
| 1623 |
* See https://tools.ietf.org/html/rfc5764#section-4.1.1 */ |
| 1624 |
CBS profile_ids, srtp_mki; |
| 1625 |
uint16_t profile_id; |
| 1626 |
if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1627 |
!CBS_get_u16(&profile_ids, &profile_id) || |
| 1628 |
CBS_len(&profile_ids) != 0 || |
| 1629 |
!CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1630 |
CBS_len(contents) != 0) { |
| 1631 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1632 |
return 0; |
| 1633 |
} |
| 1634 |
|
| 1635 |
if (CBS_len(&srtp_mki) != 0) { |
| 1636 |
/* Must be no MKI, since we never offer one. */ |
| 1637 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE); |
| 1638 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1639 |
return 0; |
| 1640 |
} |
| 1641 |
|
| 1642 |
STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl); |
| 1643 |
|
| 1644 |
/* Check to see if the server gave us something we support (and presumably |
| 1645 |
* offered). */ |
| 1646 |
size_t i; |
| 1647 |
for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) { |
| 1648 |
const SRTP_PROTECTION_PROFILE *profile = |
| 1649 |
sk_SRTP_PROTECTION_PROFILE_value(profiles, i); |
| 1650 |
|
| 1651 |
if (profile->id == profile_id) { |
| 1652 |
ssl->srtp_profile = profile; |
| 1653 |
return 1; |
| 1654 |
} |
| 1655 |
} |
| 1656 |
|
| 1657 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1658 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1659 |
return 0; |
| 1660 |
} |
| 1661 |
|
| 1662 |
static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1663 |
CBS *contents) { |
| 1664 |
if (contents == NULL) { |
| 1665 |
return 1; |
| 1666 |
} |
| 1667 |
|
| 1668 |
CBS profile_ids, srtp_mki; |
| 1669 |
if (!CBS_get_u16_length_prefixed(contents, &profile_ids) || |
| 1670 |
CBS_len(&profile_ids) < 2 || |
| 1671 |
!CBS_get_u8_length_prefixed(contents, &srtp_mki) || |
| 1672 |
CBS_len(contents) != 0) { |
| 1673 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
| 1674 |
return 0; |
| 1675 |
} |
| 1676 |
/* Discard the MKI value for now. */ |
| 1677 |
|
| 1678 |
const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles = |
| 1679 |
SSL_get_srtp_profiles(ssl); |
| 1680 |
|
| 1681 |
/* Pick the server's most preferred profile. */ |
| 1682 |
size_t i; |
| 1683 |
for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) { |
| 1684 |
const SRTP_PROTECTION_PROFILE *server_profile = |
| 1685 |
sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i); |
| 1686 |
|
| 1687 |
CBS profile_ids_tmp; |
| 1688 |
CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids)); |
| 1689 |
|
| 1690 |
while (CBS_len(&profile_ids_tmp) > 0) { |
| 1691 |
uint16_t profile_id; |
| 1692 |
if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) { |
| 1693 |
return 0; |
| 1694 |
} |
| 1695 |
|
| 1696 |
if (server_profile->id == profile_id) { |
| 1697 |
ssl->srtp_profile = server_profile; |
| 1698 |
return 1; |
| 1699 |
} |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
return 1; |
| 1704 |
} |
| 1705 |
|
| 1706 |
static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) { |
| 1707 |
if (ssl->srtp_profile == NULL) { |
| 1708 |
return 1; |
| 1709 |
} |
| 1710 |
|
| 1711 |
CBB contents, profile_ids; |
| 1712 |
if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) || |
| 1713 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1714 |
!CBB_add_u16_length_prefixed(&contents, &profile_ids) || |
| 1715 |
!CBB_add_u16(&profile_ids, ssl->srtp_profile->id) || |
| 1716 |
!CBB_add_u8(&contents, 0 /* empty MKI */) || |
| 1717 |
!CBB_flush(out)) { |
| 1718 |
return 0; |
| 1719 |
} |
| 1720 |
|
| 1721 |
return 1; |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
/* EC point formats. |
| 1726 |
* |
| 1727 |
* https://tools.ietf.org/html/rfc4492#section-5.1.2 */ |
| 1728 |
|
| 1729 |
static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) { |
| 1730 |
if (ssl->version < TLS1_VERSION && !SSL_IS_DTLS(ssl)) { |
| 1731 |
return 0; |
| 1732 |
} |
| 1733 |
|
| 1734 |
const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl); |
| 1735 |
|
| 1736 |
size_t i; |
| 1737 |
for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) { |
| 1738 |
const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i); |
| 1739 |
|
| 1740 |
const uint32_t alg_k = cipher->algorithm_mkey; |
| 1741 |
const uint32_t alg_a = cipher->algorithm_auth; |
| 1742 |
if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) { |
| 1743 |
return 1; |
| 1744 |
} |
| 1745 |
} |
| 1746 |
|
| 1747 |
return 0; |
| 1748 |
} |
| 1749 |
|
| 1750 |
static int ext_ec_point_add_extension(SSL *ssl, CBB *out) { |
| 1751 |
CBB contents, formats; |
| 1752 |
if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) || |
| 1753 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1754 |
!CBB_add_u8_length_prefixed(&contents, &formats) || |
| 1755 |
!CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) || |
| 1756 |
!CBB_flush(out)) { |
| 1757 |
return 0; |
| 1758 |
} |
| 1759 |
|
| 1760 |
return 1; |
| 1761 |
} |
| 1762 |
|
| 1763 |
static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) { |
| 1764 |
if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 1765 |
return 1; |
| 1766 |
} |
| 1767 |
|
| 1768 |
return ext_ec_point_add_extension(ssl, out); |
| 1769 |
} |
| 1770 |
|
| 1771 |
static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1772 |
CBS *contents) { |
| 1773 |
if (contents == NULL) { |
| 1774 |
return 1; |
| 1775 |
} |
| 1776 |
|
| 1777 |
CBS ec_point_format_list; |
| 1778 |
if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) || |
| 1779 |
CBS_len(contents) != 0) { |
| 1780 |
return 0; |
| 1781 |
} |
| 1782 |
|
| 1783 |
/* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed |
| 1784 |
* point format. */ |
| 1785 |
if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed, |
| 1786 |
CBS_len(&ec_point_format_list)) == NULL) { |
| 1787 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 1788 |
return 0; |
| 1789 |
} |
| 1790 |
|
| 1791 |
return 1; |
| 1792 |
} |
| 1793 |
|
| 1794 |
static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1795 |
CBS *contents) { |
| 1796 |
return ext_ec_point_parse_serverhello(ssl, out_alert, contents); |
| 1797 |
} |
| 1798 |
|
| 1799 |
static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) { |
| 1800 |
const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1801 |
const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
| 1802 |
const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA); |
| 1803 |
|
| 1804 |
if (!using_ecc) { |
| 1805 |
return 1; |
| 1806 |
} |
| 1807 |
|
| 1808 |
return ext_ec_point_add_extension(ssl, out); |
| 1809 |
} |
| 1810 |
|
| 1811 |
|
| 1812 |
/* Negotiated Groups |
| 1813 |
* |
| 1814 |
* https://tools.ietf.org/html/rfc4492#section-5.1.2 |
| 1815 |
* https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */ |
| 1816 |
|
| 1817 |
static void ext_supported_groups_init(SSL *ssl) { |
| 1818 |
OPENSSL_free(ssl->s3->tmp.peer_supported_group_list); |
| 1819 |
ssl->s3->tmp.peer_supported_group_list = NULL; |
| 1820 |
ssl->s3->tmp.peer_supported_group_list_len = 0; |
| 1821 |
} |
| 1822 |
|
| 1823 |
static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) { |
| 1824 |
if (!ssl_any_ec_cipher_suites_enabled(ssl)) { |
| 1825 |
return 1; |
| 1826 |
} |
| 1827 |
|
| 1828 |
CBB contents, groups_bytes; |
| 1829 |
if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) || |
| 1830 |
!CBB_add_u16_length_prefixed(out, &contents) || |
| 1831 |
!CBB_add_u16_length_prefixed(&contents, &groups_bytes)) { |
| 1832 |
return 0; |
| 1833 |
} |
| 1834 |
|
| 1835 |
const uint16_t *groups; |
| 1836 |
size_t groups_len; |
| 1837 |
tls1_get_grouplist(ssl, 0, &groups, &groups_len); |
| 1838 |
|
| 1839 |
size_t i; |
| 1840 |
for (i = 0; i < groups_len; i++) { |
| 1841 |
if (!CBB_add_u16(&groups_bytes, groups[i])) { |
| 1842 |
return 0; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
|
| 1846 |
return CBB_flush(out); |
| 1847 |
} |
| 1848 |
|
| 1849 |
static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert, |
| 1850 |
CBS *contents) { |
| 1851 |
/* This extension is not expected to be echoed by servers and is ignored. */ |
| 1852 |
return 1; |
| 1853 |
} |
| 1854 |
|
| 1855 |
static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert, |
| 1856 |
CBS *contents) { |
| 1857 |
if (contents == NULL) { |
| 1858 |
return 1; |
| 1859 |
} |
| 1860 |
|
| 1861 |
CBS supported_group_list; |
| 1862 |
if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) || |
| 1863 |
CBS_len(&supported_group_list) == 0 || |
| 1864 |
(CBS_len(&supported_group_list) & 1) != 0 || |
| 1865 |
CBS_len(contents) != 0) { |
| 1866 |
return 0; |
| 1867 |
} |
| 1868 |
|
| 1869 |
ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc( |
| 1870 |
CBS_len(&supported_group_list)); |
| 1871 |
if (ssl->s3->tmp.peer_supported_group_list == NULL) { |
| 1872 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1873 |
return 0; |
| 1874 |
} |
| 1875 |
|
| 1876 |
const size_t num_groups = CBS_len(&supported_group_list) / 2; |
| 1877 |
size_t i; |
| 1878 |
for (i = 0; i < num_groups; i++) { |
| 1879 |
if (!CBS_get_u16(&supported_group_list, |
| 1880 |
&ssl->s3->tmp.peer_supported_group_list[i])) { |
| 1881 |
goto err; |
| 1882 |
} |
| 1883 |
} |
| 1884 |
|
| 1885 |
assert(CBS_len(&supported_group_list) == 0); |
| 1886 |
ssl->s3->tmp.peer_supported_group_list_len = num_groups; |
| 1887 |
|
| 1888 |
return 1; |
| 1889 |
|
| 1890 |
err: |
| 1891 |
OPENSSL_free(ssl->s3->tmp.peer_supported_group_list); |
| 1892 |
ssl->s3->tmp.peer_supported_group_list = NULL; |
| 1893 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 1894 |
return 0; |
| 1895 |
} |
| 1896 |
|
| 1897 |
static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) { |
| 1898 |
/* Servers don't echo this extension. */ |
| 1899 |
return 1; |
| 1900 |
} |
| 1901 |
|
| 1902 |
|
| 1903 |
/* kExtensions contains all the supported extensions. */ |
| 1904 |
static const struct tls_extension kExtensions[] = { |
| 1905 |
{ |
| 1906 |
/* The renegotiation extension must always be at index zero because the |
| 1907 |
* |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 1908 |
* sent as an SCSV. */ |
| 1909 |
TLSEXT_TYPE_renegotiate, |
| 1910 |
NULL, |
| 1911 |
ext_ri_add_clienthello, |
| 1912 |
ext_ri_parse_serverhello, |
| 1913 |
ext_ri_parse_clienthello, |
| 1914 |
ext_ri_add_serverhello, |
| 1915 |
}, |
| 1916 |
{ |
| 1917 |
TLSEXT_TYPE_server_name, |
| 1918 |
ext_sni_init, |
| 1919 |
ext_sni_add_clienthello, |
| 1920 |
ext_sni_parse_serverhello, |
| 1921 |
ext_sni_parse_clienthello, |
| 1922 |
ext_sni_add_serverhello, |
| 1923 |
}, |
| 1924 |
{ |
| 1925 |
TLSEXT_TYPE_extended_master_secret, |
| 1926 |
ext_ems_init, |
| 1927 |
ext_ems_add_clienthello, |
| 1928 |
ext_ems_parse_serverhello, |
| 1929 |
ext_ems_parse_clienthello, |
| 1930 |
ext_ems_add_serverhello, |
| 1931 |
}, |
| 1932 |
{ |
| 1933 |
TLSEXT_TYPE_session_ticket, |
| 1934 |
NULL, |
| 1935 |
ext_ticket_add_clienthello, |
| 1936 |
ext_ticket_parse_serverhello, |
| 1937 |
ext_ticket_parse_clienthello, |
| 1938 |
ext_ticket_add_serverhello, |
| 1939 |
}, |
| 1940 |
{ |
| 1941 |
TLSEXT_TYPE_signature_algorithms, |
| 1942 |
NULL, |
| 1943 |
ext_sigalgs_add_clienthello, |
| 1944 |
ext_sigalgs_parse_serverhello, |
| 1945 |
ext_sigalgs_parse_clienthello, |
| 1946 |
ext_sigalgs_add_serverhello, |
| 1947 |
}, |
| 1948 |
{ |
| 1949 |
TLSEXT_TYPE_status_request, |
| 1950 |
ext_ocsp_init, |
| 1951 |
ext_ocsp_add_clienthello, |
| 1952 |
ext_ocsp_parse_serverhello, |
| 1953 |
ext_ocsp_parse_clienthello, |
| 1954 |
ext_ocsp_add_serverhello, |
| 1955 |
}, |
| 1956 |
{ |
| 1957 |
TLSEXT_TYPE_next_proto_neg, |
| 1958 |
ext_npn_init, |
| 1959 |
ext_npn_add_clienthello, |
| 1960 |
ext_npn_parse_serverhello, |
| 1961 |
ext_npn_parse_clienthello, |
| 1962 |
ext_npn_add_serverhello, |
| 1963 |
}, |
| 1964 |
{ |
| 1965 |
TLSEXT_TYPE_certificate_timestamp, |
| 1966 |
NULL, |
| 1967 |
ext_sct_add_clienthello, |
| 1968 |
ext_sct_parse_serverhello, |
| 1969 |
ext_sct_parse_clienthello, |
| 1970 |
ext_sct_add_serverhello, |
| 1971 |
}, |
| 1972 |
{ |
| 1973 |
TLSEXT_TYPE_application_layer_protocol_negotiation, |
| 1974 |
ext_alpn_init, |
| 1975 |
ext_alpn_add_clienthello, |
| 1976 |
ext_alpn_parse_serverhello, |
| 1977 |
ext_alpn_parse_clienthello, |
| 1978 |
ext_alpn_add_serverhello, |
| 1979 |
}, |
| 1980 |
{ |
| 1981 |
TLSEXT_TYPE_channel_id, |
| 1982 |
ext_channel_id_init, |
| 1983 |
ext_channel_id_add_clienthello, |
| 1984 |
ext_channel_id_parse_serverhello, |
| 1985 |
ext_channel_id_parse_clienthello, |
| 1986 |
ext_channel_id_add_serverhello, |
| 1987 |
}, |
| 1988 |
{ |
| 1989 |
TLSEXT_TYPE_srtp, |
| 1990 |
ext_srtp_init, |
| 1991 |
ext_srtp_add_clienthello, |
| 1992 |
ext_srtp_parse_serverhello, |
| 1993 |
ext_srtp_parse_clienthello, |
| 1994 |
ext_srtp_add_serverhello, |
| 1995 |
}, |
| 1996 |
{ |
| 1997 |
TLSEXT_TYPE_ec_point_formats, |
| 1998 |
NULL, |
| 1999 |
ext_ec_point_add_clienthello, |
| 2000 |
ext_ec_point_parse_serverhello, |
| 2001 |
ext_ec_point_parse_clienthello, |
| 2002 |
ext_ec_point_add_serverhello, |
| 2003 |
}, |
| 2004 |
/* The final extension must be non-empty. WebSphere Application Server 7.0 is |
| 2005 |
* intolerant to the last extension being zero-length. See |
| 2006 |
* https://crbug.com/363583. */ |
| 2007 |
{ |
| 2008 |
TLSEXT_TYPE_supported_groups, |
| 2009 |
ext_supported_groups_init, |
| 2010 |
ext_supported_groups_add_clienthello, |
| 2011 |
ext_supported_groups_parse_serverhello, |
| 2012 |
ext_supported_groups_parse_clienthello, |
| 2013 |
ext_supported_groups_add_serverhello, |
| 2014 |
}, |
| 2015 |
}; |
| 2016 |
|
| 2017 |
#define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension)) |
| 2018 |
|
| 2019 |
OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2020 |
sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8, |
| 2021 |
too_many_extensions_for_sent_bitset); |
| 2022 |
OPENSSL_COMPILE_ASSERT(kNumExtensions <= |
| 2023 |
sizeof(((SSL *)NULL)->s3->tmp.extensions.received) * |
| 2024 |
8, |
| 2025 |
too_many_extensions_for_received_bitset); |
| 2026 |
|
| 2027 |
static const struct tls_extension *tls_extension_find(uint32_t *out_index, |
| 2028 |
uint16_t value) { |
| 2029 |
unsigned i; |
| 2030 |
for (i = 0; i < kNumExtensions; i++) { |
| 2031 |
if (kExtensions[i].value == value) { |
| 2032 |
*out_index = i; |
| 2033 |
return &kExtensions[i]; |
| 2034 |
} |
| 2035 |
} |
| 2036 |
|
| 2037 |
return NULL; |
| 2038 |
} |
| 2039 |
|
| 2040 |
int SSL_extension_supported(unsigned extension_value) { |
| 2041 |
uint32_t index; |
| 2042 |
return extension_value == TLSEXT_TYPE_padding || |
| 2043 |
tls_extension_find(&index, extension_value) != NULL; |
| 2044 |
} |
| 2045 |
|
| 2046 |
int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) { |
| 2047 |
/* don't add extensions for SSLv3 unless doing secure renegotiation */ |
| 2048 |
if (ssl->client_version == SSL3_VERSION && |
| 2049 |
!ssl->s3->send_connection_binding) { |
| 2050 |
return 1; |
| 2051 |
} |
| 2052 |
|
| 2053 |
CBB extensions; |
| 2054 |
if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
| 2055 |
goto err; |
| 2056 |
} |
| 2057 |
|
| 2058 |
ssl->s3->tmp.extensions.sent = 0; |
| 2059 |
ssl->s3->tmp.custom_extensions.sent = 0; |
| 2060 |
|
| 2061 |
size_t i; |
| 2062 |
for (i = 0; i < kNumExtensions; i++) { |
| 2063 |
if (kExtensions[i].init != NULL) { |
| 2064 |
kExtensions[i].init(ssl); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
for (i = 0; i < kNumExtensions; i++) { |
| 2069 |
const size_t len_before = CBB_len(&extensions); |
| 2070 |
if (!kExtensions[i].add_clienthello(ssl, &extensions)) { |
| 2071 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2072 |
ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2073 |
goto err; |
| 2074 |
} |
| 2075 |
|
| 2076 |
if (CBB_len(&extensions) != len_before) { |
| 2077 |
ssl->s3->tmp.extensions.sent |= (1u << i); |
| 2078 |
} |
| 2079 |
} |
| 2080 |
|
| 2081 |
if (!custom_ext_add_clienthello(ssl, &extensions)) { |
| 2082 |
goto err; |
| 2083 |
} |
| 2084 |
|
| 2085 |
if (!SSL_IS_DTLS(ssl)) { |
| 2086 |
header_len += 2 + CBB_len(&extensions); |
| 2087 |
if (header_len > 0xff && header_len < 0x200) { |
| 2088 |
/* Add padding to workaround bugs in F5 terminators. See RFC 7685. |
| 2089 |
* |
| 2090 |
* NB: because this code works out the length of all existing extensions |
| 2091 |
* it MUST always appear last. */ |
| 2092 |
size_t padding_len = 0x200 - header_len; |
| 2093 |
/* Extensions take at least four bytes to encode. Always include at least |
| 2094 |
* one byte of data if including the extension. WebSphere Application |
| 2095 |
* Server 7.0 is intolerant to the last extension being zero-length. See |
| 2096 |
* https://crbug.com/363583. */ |
| 2097 |
if (padding_len >= 4 + 1) { |
| 2098 |
padding_len -= 4; |
| 2099 |
} else { |
| 2100 |
padding_len = 1; |
| 2101 |
} |
| 2102 |
|
| 2103 |
uint8_t *padding_bytes; |
| 2104 |
if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) || |
| 2105 |
!CBB_add_u16(&extensions, padding_len) || |
| 2106 |
!CBB_add_space(&extensions, &padding_bytes, padding_len)) { |
| 2107 |
goto err; |
| 2108 |
} |
| 2109 |
|
| 2110 |
memset(padding_bytes, 0, padding_len); |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
/* Discard empty extensions blocks. */ |
| 2115 |
if (CBB_len(&extensions) == 0) { |
| 2116 |
CBB_discard_child(out); |
| 2117 |
} |
| 2118 |
|
| 2119 |
return CBB_flush(out); |
| 2120 |
|
| 2121 |
err: |
| 2122 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2123 |
return 0; |
| 2124 |
} |
| 2125 |
|
| 2126 |
int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) { |
| 2127 |
CBB extensions; |
| 2128 |
if (!CBB_add_u16_length_prefixed(out, &extensions)) { |
| 2129 |
goto err; |
| 2130 |
} |
| 2131 |
|
| 2132 |
unsigned i; |
| 2133 |
for (i = 0; i < kNumExtensions; i++) { |
| 2134 |
if (!(ssl->s3->tmp.extensions.received & (1u << i))) { |
| 2135 |
/* Don't send extensions that were not received. */ |
| 2136 |
continue; |
| 2137 |
} |
| 2138 |
|
| 2139 |
if (!kExtensions[i].add_serverhello(ssl, &extensions)) { |
| 2140 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION); |
| 2141 |
ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2142 |
goto err; |
| 2143 |
} |
| 2144 |
} |
| 2145 |
|
| 2146 |
if (!custom_ext_add_serverhello(ssl, &extensions)) { |
| 2147 |
goto err; |
| 2148 |
} |
| 2149 |
|
| 2150 |
/* Discard empty extensions blocks. */ |
| 2151 |
if (CBB_len(&extensions) == 0) { |
| 2152 |
CBB_discard_child(out); |
| 2153 |
} |
| 2154 |
|
| 2155 |
return CBB_flush(out); |
| 2156 |
|
| 2157 |
err: |
| 2158 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2159 |
return 0; |
| 2160 |
} |
| 2161 |
|
| 2162 |
static int ssl_scan_clienthello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) { |
| 2163 |
size_t i; |
| 2164 |
for (i = 0; i < kNumExtensions; i++) { |
| 2165 |
if (kExtensions[i].init != NULL) { |
| 2166 |
kExtensions[i].init(ssl); |
| 2167 |
} |
| 2168 |
} |
| 2169 |
|
| 2170 |
ssl->s3->tmp.extensions.received = 0; |
| 2171 |
ssl->s3->tmp.custom_extensions.received = 0; |
| 2172 |
/* The renegotiation extension must always be at index zero because the |
| 2173 |
* |received| and |sent| bitsets need to be tweaked when the "extension" is |
| 2174 |
* sent as an SCSV. */ |
| 2175 |
assert(kExtensions[0].value == TLSEXT_TYPE_renegotiate); |
| 2176 |
|
| 2177 |
/* There may be no extensions. */ |
| 2178 |
if (CBS_len(cbs) != 0) { |
| 2179 |
/* Decode the extensions block and check it is valid. */ |
| 2180 |
CBS extensions; |
| 2181 |
if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2182 |
!tls1_check_duplicate_extensions(&extensions)) { |
| 2183 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2184 |
return 0; |
| 2185 |
} |
| 2186 |
|
| 2187 |
while (CBS_len(&extensions) != 0) { |
| 2188 |
uint16_t type; |
| 2189 |
CBS extension; |
| 2190 |
|
| 2191 |
/* Decode the next extension. */ |
| 2192 |
if (!CBS_get_u16(&extensions, &type) || |
| 2193 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 2194 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2195 |
return 0; |
| 2196 |
} |
| 2197 |
|
| 2198 |
/* RFC 5746 made the existence of extensions in SSL 3.0 somewhat |
| 2199 |
* ambiguous. Ignore all but the renegotiation_info extension. */ |
| 2200 |
if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) { |
| 2201 |
continue; |
| 2202 |
} |
| 2203 |
|
| 2204 |
unsigned ext_index; |
| 2205 |
const struct tls_extension *const ext = |
| 2206 |
tls_extension_find(&ext_index, type); |
| 2207 |
|
| 2208 |
if (ext == NULL) { |
| 2209 |
if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) { |
| 2210 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2211 |
return 0; |
| 2212 |
} |
| 2213 |
continue; |
| 2214 |
} |
| 2215 |
|
| 2216 |
ssl->s3->tmp.extensions.received |= (1u << ext_index); |
| 2217 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2218 |
if (!ext->parse_clienthello(ssl, &alert, &extension)) { |
| 2219 |
*out_alert = alert; |
| 2220 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2221 |
ERR_add_error_dataf("extension: %u", (unsigned)type); |
| 2222 |
return 0; |
| 2223 |
} |
| 2224 |
} |
| 2225 |
} |
| 2226 |
|
| 2227 |
for (i = 0; i < kNumExtensions; i++) { |
| 2228 |
if (!(ssl->s3->tmp.extensions.received & (1u << i))) { |
| 2229 |
/* Extension wasn't observed so call the callback with a NULL |
| 2230 |
* parameter. */ |
| 2231 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2232 |
if (!kExtensions[i].parse_clienthello(ssl, &alert, NULL)) { |
| 2233 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2234 |
ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2235 |
*out_alert = alert; |
| 2236 |
return 0; |
| 2237 |
} |
| 2238 |
} |
| 2239 |
} |
| 2240 |
|
| 2241 |
return 1; |
| 2242 |
} |
| 2243 |
|
| 2244 |
int ssl_parse_clienthello_tlsext(SSL *ssl, CBS *cbs) { |
| 2245 |
int alert = -1; |
| 2246 |
if (ssl_scan_clienthello_tlsext(ssl, cbs, &alert) <= 0) { |
| 2247 |
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 2248 |
return 0; |
| 2249 |
} |
| 2250 |
|
| 2251 |
if (ssl_check_clienthello_tlsext(ssl) <= 0) { |
| 2252 |
OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT); |
| 2253 |
return 0; |
| 2254 |
} |
| 2255 |
|
| 2256 |
return 1; |
| 2257 |
} |
| 2258 |
|
| 2259 |
OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits); |
| 2260 |
|
| 2261 |
static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) { |
| 2262 |
uint32_t received = 0; |
| 2263 |
|
| 2264 |
if (CBS_len(cbs) != 0) { |
| 2265 |
/* Decode the extensions block and check it is valid. */ |
| 2266 |
CBS extensions; |
| 2267 |
if (!CBS_get_u16_length_prefixed(cbs, &extensions) || |
| 2268 |
!tls1_check_duplicate_extensions(&extensions)) { |
| 2269 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2270 |
return 0; |
| 2271 |
} |
| 2272 |
|
| 2273 |
|
| 2274 |
while (CBS_len(&extensions) != 0) { |
| 2275 |
uint16_t type; |
| 2276 |
CBS extension; |
| 2277 |
|
| 2278 |
/* Decode the next extension. */ |
| 2279 |
if (!CBS_get_u16(&extensions, &type) || |
| 2280 |
!CBS_get_u16_length_prefixed(&extensions, &extension)) { |
| 2281 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2282 |
return 0; |
| 2283 |
} |
| 2284 |
|
| 2285 |
unsigned ext_index; |
| 2286 |
const struct tls_extension *const ext = |
| 2287 |
tls_extension_find(&ext_index, type); |
| 2288 |
|
| 2289 |
if (ext == NULL) { |
| 2290 |
if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) { |
| 2291 |
return 0; |
| 2292 |
} |
| 2293 |
continue; |
| 2294 |
} |
| 2295 |
|
| 2296 |
if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index))) { |
| 2297 |
/* If the extension was never sent then it is illegal. */ |
| 2298 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 2299 |
ERR_add_error_dataf("extension :%u", (unsigned)type); |
| 2300 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 2301 |
return 0; |
| 2302 |
} |
| 2303 |
|
| 2304 |
received |= (1u << ext_index); |
| 2305 |
|
| 2306 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2307 |
if (!ext->parse_serverhello(ssl, &alert, &extension)) { |
| 2308 |
OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION); |
| 2309 |
ERR_add_error_dataf("extension: %u", (unsigned)type); |
| 2310 |
*out_alert = alert; |
| 2311 |
return 0; |
| 2312 |
} |
| 2313 |
} |
| 2314 |
} |
| 2315 |
|
| 2316 |
size_t i; |
| 2317 |
for (i = 0; i < kNumExtensions; i++) { |
| 2318 |
if (!(received & (1u << i))) { |
| 2319 |
/* Extension wasn't observed so call the callback with a NULL |
| 2320 |
* parameter. */ |
| 2321 |
uint8_t alert = SSL_AD_DECODE_ERROR; |
| 2322 |
if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) { |
| 2323 |
OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 2324 |
ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value); |
| 2325 |
*out_alert = alert; |
| 2326 |
return 0; |
| 2327 |
} |
| 2328 |
} |
| 2329 |
} |
| 2330 |
|
| 2331 |
return 1; |
| 2332 |
} |
| 2333 |
|
| 2334 |
static int ssl_check_clienthello_tlsext(SSL *ssl) { |
| 2335 |
int ret = SSL_TLSEXT_ERR_NOACK; |
| 2336 |
int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2337 |
|
| 2338 |
if (ssl->ctx->tlsext_servername_callback != 0) { |
| 2339 |
ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
| 2340 |
ssl->ctx->tlsext_servername_arg); |
| 2341 |
} else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
| 2342 |
ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2343 |
ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
| 2344 |
} |
| 2345 |
|
| 2346 |
switch (ret) { |
| 2347 |
case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2348 |
ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 2349 |
return -1; |
| 2350 |
|
| 2351 |
case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2352 |
ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
| 2353 |
return 1; |
| 2354 |
|
| 2355 |
case SSL_TLSEXT_ERR_NOACK: |
| 2356 |
ssl->s3->tmp.should_ack_sni = 0; |
| 2357 |
return 1; |
| 2358 |
|
| 2359 |
default: |
| 2360 |
return 1; |
| 2361 |
} |
| 2362 |
} |
| 2363 |
|
| 2364 |
static int ssl_check_serverhello_tlsext(SSL *ssl) { |
| 2365 |
int ret = SSL_TLSEXT_ERR_OK; |
| 2366 |
int al = SSL_AD_UNRECOGNIZED_NAME; |
| 2367 |
|
| 2368 |
if (ssl->ctx->tlsext_servername_callback != 0) { |
| 2369 |
ret = ssl->ctx->tlsext_servername_callback(ssl, &al, |
| 2370 |
ssl->ctx->tlsext_servername_arg); |
| 2371 |
} else if (ssl->initial_ctx->tlsext_servername_callback != 0) { |
| 2372 |
ret = ssl->initial_ctx->tlsext_servername_callback( |
| 2373 |
ssl, &al, ssl->initial_ctx->tlsext_servername_arg); |
| 2374 |
} |
| 2375 |
|
| 2376 |
switch (ret) { |
| 2377 |
case SSL_TLSEXT_ERR_ALERT_FATAL: |
| 2378 |
ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
| 2379 |
return -1; |
| 2380 |
|
| 2381 |
case SSL_TLSEXT_ERR_ALERT_WARNING: |
| 2382 |
ssl3_send_alert(ssl, SSL3_AL_WARNING, al); |
| 2383 |
return 1; |
| 2384 |
|
| 2385 |
default: |
| 2386 |
return 1; |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) { |
| 2391 |
int alert = -1; |
| 2392 |
if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) { |
| 2393 |
ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 2394 |
return 0; |
| 2395 |
} |
| 2396 |
|
| 2397 |
if (ssl_check_serverhello_tlsext(ssl) <= 0) { |
| 2398 |
OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT); |
| 2399 |
return 0; |
| 2400 |
} |
| 2401 |
|
| 2402 |
return 1; |
| 2403 |
} |
| 2404 |
|
| 2405 |
int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session, |
| 2406 |
int *out_renew_ticket, const uint8_t *ticket, |
| 2407 |
size_t ticket_len, const uint8_t *session_id, |
| 2408 |
size_t session_id_len) { |
| 2409 |
int ret = 1; /* Most errors are non-fatal. */ |
| 2410 |
SSL_CTX *ssl_ctx = ssl->initial_ctx; |
| 2411 |
uint8_t *plaintext = NULL; |
| 2412 |
|
| 2413 |
HMAC_CTX hmac_ctx; |
| 2414 |
HMAC_CTX_init(&hmac_ctx); |
| 2415 |
EVP_CIPHER_CTX cipher_ctx; |
| 2416 |
EVP_CIPHER_CTX_init(&cipher_ctx); |
| 2417 |
|
| 2418 |
*out_renew_ticket = 0; |
| 2419 |
*out_session = NULL; |
| 2420 |
|
| 2421 |
if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 2422 |
goto done; |
| 2423 |
} |
| 2424 |
|
| 2425 |
/* Ensure there is room for the key name and the largest IV |
| 2426 |
* |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but |
| 2427 |
* the maximum IV length should be well under the minimum size for the |
| 2428 |
* session material and HMAC. */ |
| 2429 |
if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) { |
| 2430 |
goto done; |
| 2431 |
} |
| 2432 |
const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN; |
| 2433 |
|
| 2434 |
if (ssl_ctx->tlsext_ticket_key_cb != NULL) { |
| 2435 |
int cb_ret = ssl_ctx->tlsext_ticket_key_cb( |
| 2436 |
ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx, |
| 2437 |
&hmac_ctx, 0 /* decrypt */); |
| 2438 |
if (cb_ret < 0) { |
| 2439 |
ret = 0; |
| 2440 |
goto done; |
| 2441 |
} |
| 2442 |
if (cb_ret == 0) { |
| 2443 |
goto done; |
| 2444 |
} |
| 2445 |
if (cb_ret == 2) { |
| 2446 |
*out_renew_ticket = 1; |
| 2447 |
} |
| 2448 |
} else { |
| 2449 |
/* Check the key name matches. */ |
| 2450 |
if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name, |
| 2451 |
SSL_TICKET_KEY_NAME_LEN) != 0) { |
| 2452 |
goto done; |
| 2453 |
} |
| 2454 |
if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key, |
| 2455 |
sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(), |
| 2456 |
NULL) || |
| 2457 |
!EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL, |
| 2458 |
ssl_ctx->tlsext_tick_aes_key, iv)) { |
| 2459 |
ret = 0; |
| 2460 |
goto done; |
| 2461 |
} |
| 2462 |
} |
| 2463 |
size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx); |
| 2464 |
|
| 2465 |
/* Check the MAC at the end of the ticket. */ |
| 2466 |
uint8_t mac[EVP_MAX_MD_SIZE]; |
| 2467 |
size_t mac_len = HMAC_size(&hmac_ctx); |
| 2468 |
if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) { |
| 2469 |
/* The ticket must be large enough for key name, IV, data, and MAC. */ |
| 2470 |
goto done; |
| 2471 |
} |
| 2472 |
HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len); |
| 2473 |
HMAC_Final(&hmac_ctx, mac, NULL); |
| 2474 |
if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) { |
| 2475 |
goto done; |
| 2476 |
} |
| 2477 |
|
| 2478 |
/* Decrypt the session data. */ |
| 2479 |
const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len; |
| 2480 |
size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len - |
| 2481 |
mac_len; |
| 2482 |
plaintext = OPENSSL_malloc(ciphertext_len); |
| 2483 |
if (plaintext == NULL) { |
| 2484 |
ret = 0; |
| 2485 |
goto done; |
| 2486 |
} |
| 2487 |
if (ciphertext_len >= INT_MAX) { |
| 2488 |
goto done; |
| 2489 |
} |
| 2490 |
int len1, len2; |
| 2491 |
if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext, |
| 2492 |
(int)ciphertext_len) || |
| 2493 |
!EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) { |
| 2494 |
ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2495 |
goto done; |
| 2496 |
} |
| 2497 |
|
| 2498 |
/* Decode the session. */ |
| 2499 |
SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2); |
| 2500 |
if (session == NULL) { |
| 2501 |
ERR_clear_error(); /* Don't leave an error on the queue. */ |
| 2502 |
goto done; |
| 2503 |
} |
| 2504 |
|
| 2505 |
/* Copy the client's session ID into the new session, to denote the ticket has |
| 2506 |
* been accepted. */ |
| 2507 |
memcpy(session->session_id, session_id, session_id_len); |
| 2508 |
session->session_id_length = session_id_len; |
| 2509 |
|
| 2510 |
*out_session = session; |
| 2511 |
|
| 2512 |
done: |
| 2513 |
OPENSSL_free(plaintext); |
| 2514 |
HMAC_CTX_cleanup(&hmac_ctx); |
| 2515 |
EVP_CIPHER_CTX_cleanup(&cipher_ctx); |
| 2516 |
return ret; |
| 2517 |
} |
| 2518 |
|
| 2519 |
/* Tables to translate from NIDs to TLS v1.2 ids */ |
| 2520 |
typedef struct { |
| 2521 |
int nid; |
| 2522 |
int id; |
| 2523 |
} tls12_lookup; |
| 2524 |
|
| 2525 |
static const tls12_lookup tls12_md[] = { |
| 2526 |
{NID_sha1, TLSEXT_hash_sha1}, |
| 2527 |
{NID_sha256, TLSEXT_hash_sha256}, |
| 2528 |
{NID_sha384, TLSEXT_hash_sha384}, |
| 2529 |
{NID_sha512, TLSEXT_hash_sha512}, |
| 2530 |
}; |
| 2531 |
|
| 2532 |
static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa}, |
| 2533 |
{EVP_PKEY_EC, TLSEXT_signature_ecdsa}}; |
| 2534 |
|
| 2535 |
static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) { |
| 2536 |
size_t i; |
| 2537 |
for (i = 0; i < tlen; i++) { |
| 2538 |
if (table[i].nid == nid) { |
| 2539 |
return table[i].id; |
| 2540 |
} |
| 2541 |
} |
| 2542 |
|
| 2543 |
return -1; |
| 2544 |
} |
| 2545 |
|
| 2546 |
int tls12_get_sigid(int pkey_type) { |
| 2547 |
return tls12_find_id(pkey_type, tls12_sig, |
| 2548 |
sizeof(tls12_sig) / sizeof(tls12_lookup)); |
| 2549 |
} |
| 2550 |
|
| 2551 |
int tls12_add_sigandhash(SSL *ssl, CBB *out, const EVP_MD *md) { |
| 2552 |
int md_id = tls12_find_id(EVP_MD_type(md), tls12_md, |
| 2553 |
sizeof(tls12_md) / sizeof(tls12_lookup)); |
| 2554 |
int sig_id = tls12_get_sigid(ssl_private_key_type(ssl)); |
| 2555 |
|
| 2556 |
return md_id != -1 && |
| 2557 |
sig_id != -1 && |
| 2558 |
CBB_add_u8(out, (uint8_t)md_id) && |
| 2559 |
CBB_add_u8(out, (uint8_t)sig_id); |
| 2560 |
} |
| 2561 |
|
| 2562 |
const EVP_MD *tls12_get_hash(uint8_t hash_alg) { |
| 2563 |
switch (hash_alg) { |
| 2564 |
case TLSEXT_hash_sha1: |
| 2565 |
return EVP_sha1(); |
| 2566 |
|
| 2567 |
case TLSEXT_hash_sha256: |
| 2568 |
return EVP_sha256(); |
| 2569 |
|
| 2570 |
case TLSEXT_hash_sha384: |
| 2571 |
return EVP_sha384(); |
| 2572 |
|
| 2573 |
case TLSEXT_hash_sha512: |
| 2574 |
return EVP_sha512(); |
| 2575 |
|
| 2576 |
default: |
| 2577 |
return NULL; |
| 2578 |
} |
| 2579 |
} |
| 2580 |
|
| 2581 |
/* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature |
| 2582 |
* algorithm |sig_alg|. It returns -1 if the type is unknown. */ |
| 2583 |
static int tls12_get_pkey_type(uint8_t sig_alg) { |
| 2584 |
switch (sig_alg) { |
| 2585 |
case TLSEXT_signature_rsa: |
| 2586 |
return EVP_PKEY_RSA; |
| 2587 |
|
| 2588 |
case TLSEXT_signature_ecdsa: |
| 2589 |
return EVP_PKEY_EC; |
| 2590 |
|
| 2591 |
default: |
| 2592 |
return -1; |
| 2593 |
} |
| 2594 |
} |
| 2595 |
|
| 2596 |
OPENSSL_COMPILE_ASSERT(sizeof(TLS_SIGALGS) == 2, |
| 2597 |
sizeof_tls_sigalgs_is_not_two); |
| 2598 |
|
| 2599 |
int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) { |
| 2600 |
/* Extension ignored for inappropriate versions */ |
| 2601 |
if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
| 2602 |
return 1; |
| 2603 |
} |
| 2604 |
|
| 2605 |
CERT *const cert = ssl->cert; |
| 2606 |
OPENSSL_free(cert->peer_sigalgs); |
| 2607 |
cert->peer_sigalgs = NULL; |
| 2608 |
cert->peer_sigalgslen = 0; |
| 2609 |
|
| 2610 |
size_t num_sigalgs = CBS_len(in_sigalgs); |
| 2611 |
|
| 2612 |
if (num_sigalgs % 2 != 0) { |
| 2613 |
return 0; |
| 2614 |
} |
| 2615 |
num_sigalgs /= 2; |
| 2616 |
|
| 2617 |
/* supported_signature_algorithms in the certificate request is |
| 2618 |
* allowed to be empty. */ |
| 2619 |
if (num_sigalgs == 0) { |
| 2620 |
return 1; |
| 2621 |
} |
| 2622 |
|
| 2623 |
/* This multiplication doesn't overflow because sizeof(TLS_SIGALGS) is two |
| 2624 |
* (statically asserted above) and we just divided |num_sigalgs| by two. */ |
| 2625 |
cert->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(TLS_SIGALGS)); |
| 2626 |
if (cert->peer_sigalgs == NULL) { |
| 2627 |
return 0; |
| 2628 |
} |
| 2629 |
cert->peer_sigalgslen = num_sigalgs; |
| 2630 |
|
| 2631 |
CBS sigalgs; |
| 2632 |
CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs)); |
| 2633 |
|
| 2634 |
size_t i; |
| 2635 |
for (i = 0; i < num_sigalgs; i++) { |
| 2636 |
TLS_SIGALGS *const sigalg = &cert->peer_sigalgs[i]; |
| 2637 |
if (!CBS_get_u8(&sigalgs, &sigalg->rhash) || |
| 2638 |
!CBS_get_u8(&sigalgs, &sigalg->rsign)) { |
| 2639 |
return 0; |
| 2640 |
} |
| 2641 |
} |
| 2642 |
|
| 2643 |
return 1; |
| 2644 |
} |
| 2645 |
|
| 2646 |
const EVP_MD *tls1_choose_signing_digest(SSL *ssl) { |
| 2647 |
CERT *cert = ssl->cert; |
| 2648 |
int type = ssl_private_key_type(ssl); |
| 2649 |
size_t i, j; |
| 2650 |
|
| 2651 |
static const int kDefaultDigestList[] = {NID_sha256, NID_sha384, NID_sha512, |
| 2652 |
NID_sha1}; |
| 2653 |
|
| 2654 |
const int *digest_nids = kDefaultDigestList; |
| 2655 |
size_t num_digest_nids = |
| 2656 |
sizeof(kDefaultDigestList) / sizeof(kDefaultDigestList[0]); |
| 2657 |
if (cert->digest_nids != NULL) { |
| 2658 |
digest_nids = cert->digest_nids; |
| 2659 |
num_digest_nids = cert->num_digest_nids; |
| 2660 |
} |
| 2661 |
|
| 2662 |
for (i = 0; i < num_digest_nids; i++) { |
| 2663 |
const int digest_nid = digest_nids[i]; |
| 2664 |
for (j = 0; j < cert->peer_sigalgslen; j++) { |
| 2665 |
const EVP_MD *md = tls12_get_hash(cert->peer_sigalgs[j].rhash); |
| 2666 |
if (md == NULL || |
| 2667 |
digest_nid != EVP_MD_type(md) || |
| 2668 |
tls12_get_pkey_type(cert->peer_sigalgs[j].rsign) != type) { |
| 2669 |
continue; |
| 2670 |
} |
| 2671 |
|
| 2672 |
return md; |
| 2673 |
} |
| 2674 |
} |
| 2675 |
|
| 2676 |
/* If no suitable digest may be found, default to SHA-1. */ |
| 2677 |
return EVP_sha1(); |
| 2678 |
} |
| 2679 |
|
| 2680 |
int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) { |
| 2681 |
int ret = 0; |
| 2682 |
EVP_MD_CTX ctx; |
| 2683 |
|
| 2684 |
EVP_MD_CTX_init(&ctx); |
| 2685 |
if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) { |
| 2686 |
goto err; |
| 2687 |
} |
| 2688 |
|
| 2689 |
static const char kClientIDMagic[] = "TLS Channel ID signature"; |
| 2690 |
EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic)); |
| 2691 |
|
| 2692 |
if (ssl->hit) { |
| 2693 |
static const char kResumptionMagic[] = "Resumption"; |
| 2694 |
EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic)); |
| 2695 |
if (ssl->session->original_handshake_hash_len == 0) { |
| 2696 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 2697 |
goto err; |
| 2698 |
} |
| 2699 |
EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash, |
| 2700 |
ssl->session->original_handshake_hash_len); |
| 2701 |
} |
| 2702 |
|
| 2703 |
uint8_t handshake_hash[EVP_MAX_MD_SIZE]; |
| 2704 |
int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash, |
| 2705 |
sizeof(handshake_hash)); |
| 2706 |
if (handshake_hash_len < 0) { |
| 2707 |
goto err; |
| 2708 |
} |
| 2709 |
EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len); |
| 2710 |
unsigned len_u; |
| 2711 |
EVP_DigestFinal_ex(&ctx, out, &len_u); |
| 2712 |
*out_len = len_u; |
| 2713 |
|
| 2714 |
ret = 1; |
| 2715 |
|
| 2716 |
err: |
| 2717 |
EVP_MD_CTX_cleanup(&ctx); |
| 2718 |
return ret; |
| 2719 |
} |
| 2720 |
|
| 2721 |
/* tls1_record_handshake_hashes_for_channel_id records the current handshake |
| 2722 |
* hashes in |ssl->session| so that Channel ID resumptions can sign that |
| 2723 |
* data. */ |
| 2724 |
int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) { |
| 2725 |
int digest_len; |
| 2726 |
/* This function should never be called for a resumed session because the |
| 2727 |
* handshake hashes that we wish to record are for the original, full |
| 2728 |
* handshake. */ |
| 2729 |
if (ssl->hit) { |
| 2730 |
return -1; |
| 2731 |
} |
| 2732 |
|
| 2733 |
digest_len = |
| 2734 |
tls1_handshake_digest(ssl, ssl->session->original_handshake_hash, |
| 2735 |
sizeof(ssl->session->original_handshake_hash)); |
| 2736 |
if (digest_len < 0) { |
| 2737 |
return -1; |
| 2738 |
} |
| 2739 |
|
| 2740 |
ssl->session->original_handshake_hash_len = digest_len; |
| 2741 |
|
| 2742 |
return 1; |
| 2743 |
} |