1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) cryptsoft.com). 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 (tjh (at) cryptsoft.com). 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 (eay (at) cryptsoft.com)" 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 (tjh (at) cryptsoft.com)" 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-2002 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 * openssl-core (at) openssl.org. 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 * (eay (at) cryptsoft.com). This product includes software written by Tim 107 * Hudson (tjh (at) cryptsoft.com). */ 108 /* ==================================================================== 109 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 110 * ECC cipher suite support in OpenSSL originally developed by 111 * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ 112 113 #include <openssl/ssl.h> 114 115 #include <assert.h> 116 #include <limits.h> 117 #include <string.h> 118 119 #include <openssl/buf.h> 120 #include <openssl/bytestring.h> 121 #include <openssl/err.h> 122 #include <openssl/evp.h> 123 #include <openssl/mem.h> 124 #include <openssl/md5.h> 125 #include <openssl/nid.h> 126 #include <openssl/rand.h> 127 #include <openssl/sha.h> 128 129 #include "../crypto/internal.h" 130 #include "internal.h" 131 132 133 SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) { 134 SSL_HANDSHAKE *hs = OPENSSL_malloc(sizeof(SSL_HANDSHAKE)); 135 if (hs == NULL) { 136 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); 137 return NULL; 138 } 139 OPENSSL_memset(hs, 0, sizeof(SSL_HANDSHAKE)); 140 hs->ssl = ssl; 141 hs->wait = ssl_hs_ok; 142 hs->state = SSL_ST_INIT; 143 if (!SSL_TRANSCRIPT_init(&hs->transcript)) { 144 ssl_handshake_free(hs); 145 return NULL; 146 } 147 return hs; 148 } 149 150 void ssl_handshake_free(SSL_HANDSHAKE *hs) { 151 if (hs == NULL) { 152 return; 153 } 154 155 OPENSSL_cleanse(hs->secret, sizeof(hs->secret)); 156 OPENSSL_cleanse(hs->early_traffic_secret, sizeof(hs->early_traffic_secret)); 157 OPENSSL_cleanse(hs->client_handshake_secret, 158 sizeof(hs->client_handshake_secret)); 159 OPENSSL_cleanse(hs->server_handshake_secret, 160 sizeof(hs->server_handshake_secret)); 161 OPENSSL_cleanse(hs->client_traffic_secret_0, 162 sizeof(hs->client_traffic_secret_0)); 163 OPENSSL_cleanse(hs->server_traffic_secret_0, 164 sizeof(hs->server_traffic_secret_0)); 165 SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); 166 SSL_TRANSCRIPT_cleanup(&hs->transcript); 167 OPENSSL_free(hs->cookie); 168 OPENSSL_free(hs->key_share_bytes); 169 OPENSSL_free(hs->public_key); 170 SSL_SESSION_free(hs->new_session); 171 OPENSSL_free(hs->peer_sigalgs); 172 OPENSSL_free(hs->peer_supported_group_list); 173 OPENSSL_free(hs->peer_key); 174 OPENSSL_free(hs->server_params); 175 OPENSSL_free(hs->peer_psk_identity_hint); 176 sk_CRYPTO_BUFFER_pop_free(hs->ca_names, CRYPTO_BUFFER_free); 177 hs->ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); 178 OPENSSL_free(hs->certificate_types); 179 180 if (hs->key_block != NULL) { 181 OPENSSL_cleanse(hs->key_block, hs->key_block_len); 182 OPENSSL_free(hs->key_block); 183 } 184 185 OPENSSL_free(hs->hostname); 186 EVP_PKEY_free(hs->peer_pubkey); 187 OPENSSL_free(hs); 188 } 189 190 int ssl_check_message_type(SSL *ssl, int type) { 191 if (ssl->s3->tmp.message_type != type) { 192 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 193 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); 194 ERR_add_error_dataf("got type %d, wanted type %d", 195 ssl->s3->tmp.message_type, type); 196 return 0; 197 } 198 199 return 1; 200 } 201 202 static int add_record_to_flight(SSL *ssl, uint8_t type, const uint8_t *in, 203 size_t in_len) { 204 /* We'll never add a flight while in the process of writing it out. */ 205 assert(ssl->s3->pending_flight_offset == 0); 206 207 if (ssl->s3->pending_flight == NULL) { 208 ssl->s3->pending_flight = BUF_MEM_new(); 209 if (ssl->s3->pending_flight == NULL) { 210 return 0; 211 } 212 } 213 214 size_t max_out = in_len + SSL_max_seal_overhead(ssl); 215 size_t new_cap = ssl->s3->pending_flight->length + max_out; 216 if (max_out < in_len || new_cap < max_out) { 217 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); 218 return 0; 219 } 220 221 size_t len; 222 if (!BUF_MEM_reserve(ssl->s3->pending_flight, new_cap) || 223 !tls_seal_record(ssl, (uint8_t *)ssl->s3->pending_flight->data + 224 ssl->s3->pending_flight->length, 225 &len, max_out, type, in, in_len)) { 226 return 0; 227 } 228 229 ssl->s3->pending_flight->length += len; 230 return 1; 231 } 232 233 int ssl3_init_message(SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { 234 /* Pick a modest size hint to save most of the |realloc| calls. */ 235 if (!CBB_init(cbb, 64) || 236 !CBB_add_u8(cbb, type) || 237 !CBB_add_u24_length_prefixed(cbb, body)) { 238 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 239 CBB_cleanup(cbb); 240 return 0; 241 } 242 243 return 1; 244 } 245 246 int ssl3_finish_message(SSL *ssl, CBB *cbb, uint8_t **out_msg, 247 size_t *out_len) { 248 if (!CBB_finish(cbb, out_msg, out_len)) { 249 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 250 return 0; 251 } 252 253 return 1; 254 } 255 256 int ssl3_add_message(SSL *ssl, uint8_t *msg, size_t len) { 257 /* Add the message to the current flight, splitting into several records if 258 * needed. */ 259 int ret = 0; 260 size_t added = 0; 261 do { 262 size_t todo = len - added; 263 if (todo > ssl->max_send_fragment) { 264 todo = ssl->max_send_fragment; 265 } 266 267 if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, msg + added, todo)) { 268 goto err; 269 } 270 added += todo; 271 } while (added < len); 272 273 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg, len); 274 /* TODO(svaldez): Move this up a layer to fix abstraction for SSL_TRANSCRIPT 275 * on hs. */ 276 if (ssl->s3->hs != NULL && 277 !SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, msg, len)) { 278 goto err; 279 } 280 ret = 1; 281 282 err: 283 OPENSSL_free(msg); 284 return ret; 285 } 286 287 int ssl3_add_change_cipher_spec(SSL *ssl) { 288 static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; 289 290 if (!add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, kChangeCipherSpec, 291 sizeof(kChangeCipherSpec))) { 292 return 0; 293 } 294 295 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, 296 kChangeCipherSpec, sizeof(kChangeCipherSpec)); 297 return 1; 298 } 299 300 int ssl3_add_alert(SSL *ssl, uint8_t level, uint8_t desc) { 301 uint8_t alert[2] = {level, desc}; 302 if (!add_record_to_flight(ssl, SSL3_RT_ALERT, alert, sizeof(alert))) { 303 return 0; 304 } 305 306 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, alert, sizeof(alert)); 307 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, ((int)level << 8) | desc); 308 return 1; 309 } 310 311 int ssl_add_message_cbb(SSL *ssl, CBB *cbb) { 312 uint8_t *msg; 313 size_t len; 314 if (!ssl->method->finish_message(ssl, cbb, &msg, &len) || 315 !ssl->method->add_message(ssl, msg, len)) { 316 return 0; 317 } 318 319 return 1; 320 } 321 322 int ssl3_flush_flight(SSL *ssl) { 323 if (ssl->s3->pending_flight == NULL) { 324 return 1; 325 } 326 327 if (ssl->s3->pending_flight->length > 0xffffffff || 328 ssl->s3->pending_flight->length > INT_MAX) { 329 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 330 return -1; 331 } 332 333 /* The handshake flight buffer is mutually exclusive with application data. 334 * 335 * TODO(davidben): This will not be true when closure alerts use this. */ 336 if (ssl_write_buffer_is_pending(ssl)) { 337 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 338 return -1; 339 } 340 341 /* Write the pending flight. */ 342 while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) { 343 int ret = BIO_write( 344 ssl->wbio, 345 ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, 346 ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset); 347 if (ret <= 0) { 348 ssl->rwstate = SSL_WRITING; 349 return ret; 350 } 351 352 ssl->s3->pending_flight_offset += ret; 353 } 354 355 if (BIO_flush(ssl->wbio) <= 0) { 356 ssl->rwstate = SSL_WRITING; 357 return -1; 358 } 359 360 BUF_MEM_free(ssl->s3->pending_flight); 361 ssl->s3->pending_flight = NULL; 362 ssl->s3->pending_flight_offset = 0; 363 return 1; 364 } 365 366 int ssl3_send_finished(SSL_HANDSHAKE *hs) { 367 SSL *const ssl = hs->ssl; 368 const SSL_SESSION *session = SSL_get_session(ssl); 369 370 uint8_t finished[EVP_MAX_MD_SIZE]; 371 size_t finished_len; 372 if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len, 373 session, ssl->server, 374 ssl3_protocol_version(ssl))) { 375 return 0; 376 } 377 378 /* Log the master secret, if logging is enabled. */ 379 if (!ssl_log_secret(ssl, "CLIENT_RANDOM", 380 session->master_key, 381 session->master_key_length)) { 382 return 0; 383 } 384 385 /* Copy the Finished so we can use it for renegotiation checks. */ 386 if (ssl->version != SSL3_VERSION) { 387 if (finished_len > sizeof(ssl->s3->previous_client_finished) || 388 finished_len > sizeof(ssl->s3->previous_server_finished)) { 389 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 390 return -1; 391 } 392 393 if (ssl->server) { 394 OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len); 395 ssl->s3->previous_server_finished_len = finished_len; 396 } else { 397 OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len); 398 ssl->s3->previous_client_finished_len = finished_len; 399 } 400 } 401 402 CBB cbb, body; 403 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) || 404 !CBB_add_bytes(&body, finished, finished_len) || 405 !ssl_add_message_cbb(ssl, &cbb)) { 406 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 407 CBB_cleanup(&cbb); 408 return -1; 409 } 410 411 return 1; 412 } 413 414 int ssl3_get_finished(SSL_HANDSHAKE *hs) { 415 SSL *const ssl = hs->ssl; 416 int ret = ssl->method->ssl_get_message(ssl); 417 if (ret <= 0) { 418 return ret; 419 } 420 421 if (!ssl_check_message_type(ssl, SSL3_MT_FINISHED)) { 422 return -1; 423 } 424 425 /* Snapshot the finished hash before incorporating the new message. */ 426 uint8_t finished[EVP_MAX_MD_SIZE]; 427 size_t finished_len; 428 if (!SSL_TRANSCRIPT_finish_mac(&hs->transcript, finished, &finished_len, 429 SSL_get_session(ssl), !ssl->server, 430 ssl3_protocol_version(ssl)) || 431 !ssl_hash_current_message(hs)) { 432 return -1; 433 } 434 435 int finished_ok = ssl->init_num == finished_len && 436 CRYPTO_memcmp(ssl->init_msg, finished, finished_len) == 0; 437 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) 438 finished_ok = 1; 439 #endif 440 if (!finished_ok) { 441 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); 442 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); 443 return -1; 444 } 445 446 /* Copy the Finished so we can use it for renegotiation checks. */ 447 if (ssl->version != SSL3_VERSION) { 448 if (finished_len > sizeof(ssl->s3->previous_client_finished) || 449 finished_len > sizeof(ssl->s3->previous_server_finished)) { 450 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 451 return -1; 452 } 453 454 if (ssl->server) { 455 OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len); 456 ssl->s3->previous_client_finished_len = finished_len; 457 } else { 458 OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len); 459 ssl->s3->previous_server_finished_len = finished_len; 460 } 461 } 462 463 return 1; 464 } 465 466 int ssl3_output_cert_chain(SSL *ssl) { 467 CBB cbb, body; 468 if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) || 469 !ssl_add_cert_chain(ssl, &body) || 470 !ssl_add_message_cbb(ssl, &cbb)) { 471 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 472 CBB_cleanup(&cbb); 473 return 0; 474 } 475 476 return 1; 477 } 478 479 size_t ssl_max_handshake_message_len(const SSL *ssl) { 480 /* kMaxMessageLen is the default maximum message size for handshakes which do 481 * not accept peer certificate chains. */ 482 static const size_t kMaxMessageLen = 16384; 483 484 if (SSL_in_init(ssl)) { 485 if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) && 486 kMaxMessageLen < ssl->max_cert_list) { 487 return ssl->max_cert_list; 488 } 489 return kMaxMessageLen; 490 } 491 492 if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) { 493 /* In TLS 1.2 and below, the largest acceptable post-handshake message is 494 * a HelloRequest. */ 495 return 0; 496 } 497 498 if (ssl->server) { 499 /* The largest acceptable post-handshake message for a server is a 500 * KeyUpdate. We will never initiate post-handshake auth. */ 501 return 1; 502 } 503 504 /* Clients must accept NewSessionTicket and CertificateRequest, so allow the 505 * default size. */ 506 return kMaxMessageLen; 507 } 508 509 static int extend_handshake_buffer(SSL *ssl, size_t length) { 510 if (!BUF_MEM_reserve(ssl->init_buf, length)) { 511 return -1; 512 } 513 while (ssl->init_buf->length < length) { 514 int ret = ssl3_read_handshake_bytes( 515 ssl, (uint8_t *)ssl->init_buf->data + ssl->init_buf->length, 516 length - ssl->init_buf->length); 517 if (ret <= 0) { 518 return ret; 519 } 520 ssl->init_buf->length += (size_t)ret; 521 } 522 return 1; 523 } 524 525 static int read_v2_client_hello(SSL *ssl) { 526 /* Read the first 5 bytes, the size of the TLS record header. This is 527 * sufficient to detect a V2ClientHello and ensures that we never read beyond 528 * the first record. */ 529 int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH); 530 if (ret <= 0) { 531 return ret; 532 } 533 const uint8_t *p = ssl_read_buffer(ssl); 534 535 /* Some dedicated error codes for protocol mixups should the application wish 536 * to interpret them differently. (These do not overlap with ClientHello or 537 * V2ClientHello.) */ 538 if (strncmp("GET ", (const char *)p, 4) == 0 || 539 strncmp("POST ", (const char *)p, 5) == 0 || 540 strncmp("HEAD ", (const char *)p, 5) == 0 || 541 strncmp("PUT ", (const char *)p, 4) == 0) { 542 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); 543 return -1; 544 } 545 if (strncmp("CONNE", (const char *)p, 5) == 0) { 546 OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); 547 return -1; 548 } 549 550 if ((p[0] & 0x80) == 0 || p[2] != SSL2_MT_CLIENT_HELLO || 551 p[3] != SSL3_VERSION_MAJOR) { 552 /* Not a V2ClientHello. */ 553 return 1; 554 } 555 556 /* Determine the length of the V2ClientHello. */ 557 size_t msg_length = ((p[0] & 0x7f) << 8) | p[1]; 558 if (msg_length > (1024 * 4)) { 559 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); 560 return -1; 561 } 562 if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { 563 /* Reject lengths that are too short early. We have already read 564 * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an 565 * (invalid) V2ClientHello which would be shorter than that. */ 566 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); 567 return -1; 568 } 569 570 /* Read the remainder of the V2ClientHello. */ 571 ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length); 572 if (ret <= 0) { 573 return ret; 574 } 575 576 CBS v2_client_hello; 577 CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length); 578 579 /* The V2ClientHello without the length is incorporated into the handshake 580 * hash. This is only ever called at the start of the handshake, so hs is 581 * guaranteed to be non-NULL. */ 582 if (!SSL_TRANSCRIPT_update(&ssl->s3->hs->transcript, 583 CBS_data(&v2_client_hello), 584 CBS_len(&v2_client_hello))) { 585 return -1; 586 } 587 588 ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */, 589 CBS_data(&v2_client_hello), CBS_len(&v2_client_hello)); 590 591 uint8_t msg_type; 592 uint16_t version, cipher_spec_length, session_id_length, challenge_length; 593 CBS cipher_specs, session_id, challenge; 594 if (!CBS_get_u8(&v2_client_hello, &msg_type) || 595 !CBS_get_u16(&v2_client_hello, &version) || 596 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || 597 !CBS_get_u16(&v2_client_hello, &session_id_length) || 598 !CBS_get_u16(&v2_client_hello, &challenge_length) || 599 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || 600 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || 601 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || 602 CBS_len(&v2_client_hello) != 0) { 603 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); 604 return -1; 605 } 606 607 /* msg_type has already been checked. */ 608 assert(msg_type == SSL2_MT_CLIENT_HELLO); 609 610 /* The client_random is the V2ClientHello challenge. Truncate or 611 * left-pad with zeros as needed. */ 612 size_t rand_len = CBS_len(&challenge); 613 if (rand_len > SSL3_RANDOM_SIZE) { 614 rand_len = SSL3_RANDOM_SIZE; 615 } 616 uint8_t random[SSL3_RANDOM_SIZE]; 617 OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE); 618 OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), 619 rand_len); 620 621 /* Write out an equivalent SSLv3 ClientHello. */ 622 size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ + 623 SSL3_RANDOM_SIZE + 1 /* session ID length */ + 624 2 /* cipher list length */ + 625 CBS_len(&cipher_specs) / 3 * 2 + 626 1 /* compression length */ + 1 /* compression */; 627 CBB client_hello, hello_body, cipher_suites; 628 CBB_zero(&client_hello); 629 if (!BUF_MEM_reserve(ssl->init_buf, max_v3_client_hello) || 630 !CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data, 631 ssl->init_buf->max) || 632 !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || 633 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || 634 !CBB_add_u16(&hello_body, version) || 635 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || 636 /* No session id. */ 637 !CBB_add_u8(&hello_body, 0) || 638 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { 639 CBB_cleanup(&client_hello); 640 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); 641 return -1; 642 } 643 644 /* Copy the cipher suites. */ 645 while (CBS_len(&cipher_specs) > 0) { 646 uint32_t cipher_spec; 647 if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { 648 CBB_cleanup(&client_hello); 649 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); 650 return -1; 651 } 652 653 /* Skip SSLv2 ciphers. */ 654 if ((cipher_spec & 0xff0000) != 0) { 655 continue; 656 } 657 if (!CBB_add_u16(&cipher_suites, cipher_spec)) { 658 CBB_cleanup(&client_hello); 659 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 660 return -1; 661 } 662 } 663 664 /* Add the null compression scheme and finish. */ 665 if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || 666 !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) { 667 CBB_cleanup(&client_hello); 668 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); 669 return -1; 670 } 671 672 /* Consume and discard the V2ClientHello. */ 673 ssl_read_buffer_consume(ssl, 2 + msg_length); 674 ssl_read_buffer_discard(ssl); 675 676 ssl->s3->is_v2_hello = 1; 677 /* This is the first message, so hs must be non-NULL. */ 678 ssl->s3->hs->v2_clienthello = 1; 679 return 1; 680 } 681 682 int ssl3_get_message(SSL *ssl) { 683 /* Re-create the handshake buffer if needed. */ 684 if (ssl->init_buf == NULL) { 685 ssl->init_buf = BUF_MEM_new(); 686 if (ssl->init_buf == NULL) { 687 return -1; 688 } 689 } 690 691 if (ssl->server && !ssl->s3->v2_hello_done) { 692 /* Bypass the record layer for the first message to handle V2ClientHello. */ 693 int ret = read_v2_client_hello(ssl); 694 if (ret <= 0) { 695 return ret; 696 } 697 ssl->s3->v2_hello_done = 1; 698 } 699 700 if (ssl->s3->tmp.reuse_message) { 701 /* There must be a current message. */ 702 assert(ssl->init_msg != NULL); 703 ssl->s3->tmp.reuse_message = 0; 704 } else { 705 ssl3_release_current_message(ssl, 0 /* don't free buffer */); 706 } 707 708 /* Read the message header, if we haven't yet. */ 709 int ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH); 710 if (ret <= 0) { 711 return ret; 712 } 713 714 /* Parse out the length. Cap it so the peer cannot force us to buffer up to 715 * 2^24 bytes. */ 716 const uint8_t *p = (uint8_t *)ssl->init_buf->data; 717 size_t msg_len = (((uint32_t)p[1]) << 16) | (((uint32_t)p[2]) << 8) | p[3]; 718 if (msg_len > ssl_max_handshake_message_len(ssl)) { 719 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 720 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); 721 return -1; 722 } 723 724 /* Read the message body, if we haven't yet. */ 725 ret = extend_handshake_buffer(ssl, SSL3_HM_HEADER_LENGTH + msg_len); 726 if (ret <= 0) { 727 return ret; 728 } 729 730 /* We have now received a complete message. */ 731 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, ssl->init_buf->data, 732 ssl->init_buf->length); 733 734 ssl->s3->tmp.message_type = ((const uint8_t *)ssl->init_buf->data)[0]; 735 ssl->init_msg = (uint8_t*)ssl->init_buf->data + SSL3_HM_HEADER_LENGTH; 736 ssl->init_num = ssl->init_buf->length - SSL3_HM_HEADER_LENGTH; 737 return 1; 738 } 739 740 void ssl3_get_current_message(const SSL *ssl, CBS *out) { 741 CBS_init(out, (uint8_t *)ssl->init_buf->data, ssl->init_buf->length); 742 } 743 744 int ssl_hash_current_message(SSL_HANDSHAKE *hs) { 745 /* V2ClientHellos are hashed implicitly. */ 746 if (hs->ssl->s3->is_v2_hello) { 747 return 1; 748 } 749 750 CBS cbs; 751 hs->ssl->method->get_current_message(hs->ssl, &cbs); 752 return SSL_TRANSCRIPT_update(&hs->transcript, CBS_data(&cbs), CBS_len(&cbs)); 753 } 754 755 void ssl3_release_current_message(SSL *ssl, int free_buffer) { 756 if (ssl->init_msg != NULL) { 757 /* |init_buf| never contains data beyond the current message. */ 758 assert(SSL3_HM_HEADER_LENGTH + ssl->init_num == ssl->init_buf->length); 759 760 /* Clear the current message. */ 761 ssl->init_msg = NULL; 762 ssl->init_num = 0; 763 ssl->init_buf->length = 0; 764 ssl->s3->is_v2_hello = 0; 765 } 766 767 if (free_buffer) { 768 BUF_MEM_free(ssl->init_buf); 769 ssl->init_buf = NULL; 770 } 771 } 772 773 int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, 774 const SSL_EXTENSION_TYPE *ext_types, 775 size_t num_ext_types, int ignore_unknown) { 776 /* Reset everything. */ 777 for (size_t i = 0; i < num_ext_types; i++) { 778 *ext_types[i].out_present = 0; 779 CBS_init(ext_types[i].out_data, NULL, 0); 780 } 781 782 CBS copy = *cbs; 783 while (CBS_len(©) != 0) { 784 uint16_t type; 785 CBS data; 786 if (!CBS_get_u16(©, &type) || 787 !CBS_get_u16_length_prefixed(©, &data)) { 788 OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); 789 *out_alert = SSL_AD_DECODE_ERROR; 790 return 0; 791 } 792 793 const SSL_EXTENSION_TYPE *ext_type = NULL; 794 for (size_t i = 0; i < num_ext_types; i++) { 795 if (type == ext_types[i].type) { 796 ext_type = &ext_types[i]; 797 break; 798 } 799 } 800 801 if (ext_type == NULL) { 802 if (ignore_unknown) { 803 continue; 804 } 805 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); 806 *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; 807 return 0; 808 } 809 810 /* Duplicate ext_types are forbidden. */ 811 if (*ext_type->out_present) { 812 OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); 813 *out_alert = SSL_AD_ILLEGAL_PARAMETER; 814 return 0; 815 } 816 817 *ext_type->out_present = 1; 818 *ext_type->out_data = data; 819 } 820 821 return 1; 822 } 823