1 /* $OpenBSD: ssl_srvr.c,v 1.165 2024/07/22 14:47:15 jsing Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 /* ==================================================================== 59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. 60 * 61 * Redistribution and use in source and binary forms, with or without 62 * modification, are permitted provided that the following conditions 63 * are met: 64 * 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 68 * 2. Redistributions in binary form must reproduce the above copyright 69 * notice, this list of conditions and the following disclaimer in 70 * the documentation and/or other materials provided with the 71 * distribution. 72 * 73 * 3. All advertising materials mentioning features or use of this 74 * software must display the following acknowledgment: 75 * "This product includes software developed by the OpenSSL Project 76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 77 * 78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 79 * endorse or promote products derived from this software without 80 * prior written permission. For written permission, please contact 81 * openssl-core@openssl.org. 82 * 83 * 5. Products derived from this software may not be called "OpenSSL" 84 * nor may "OpenSSL" appear in their names without prior written 85 * permission of the OpenSSL Project. 86 * 87 * 6. Redistributions of any form whatsoever must retain the following 88 * acknowledgment: 89 * "This product includes software developed by the OpenSSL Project 90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 91 * 92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 103 * OF THE POSSIBILITY OF SUCH DAMAGE. 104 * ==================================================================== 105 * 106 * This product includes cryptographic software written by Eric Young 107 * (eay@cryptsoft.com). This product includes software written by Tim 108 * Hudson (tjh@cryptsoft.com). 109 * 110 */ 111 /* ==================================================================== 112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 113 * 114 * Portions of the attached software ("Contribution") are developed by 115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. 116 * 117 * The Contribution is licensed pursuant to the OpenSSL open source 118 * license provided above. 119 * 120 * ECC cipher suite support in OpenSSL originally written by 121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. 122 * 123 */ 124 /* ==================================================================== 125 * Copyright 2005 Nokia. All rights reserved. 126 * 127 * The portions of the attached software ("Contribution") is developed by 128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source 129 * license. 130 * 131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of 132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites 133 * support (see RFC 4279) to OpenSSL. 134 * 135 * No patent licenses or other rights except those expressly stated in 136 * the OpenSSL open source license shall be deemed granted or received 137 * expressly, by implication, estoppel, or otherwise. 138 * 139 * No assurances are provided by Nokia that the Contribution does not 140 * infringe the patent or other intellectual property rights of any third 141 * party or that the license provides you with all the necessary rights 142 * to make use of the Contribution. 143 * 144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN 145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA 146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY 147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR 148 * OTHERWISE. 149 */ 150 151 #include <limits.h> 152 #include <stdio.h> 153 154 #include <openssl/bn.h> 155 #include <openssl/buffer.h> 156 #include <openssl/curve25519.h> 157 #include <openssl/evp.h> 158 #include <openssl/dh.h> 159 #include <openssl/hmac.h> 160 #include <openssl/md5.h> 161 #include <openssl/objects.h> 162 #include <openssl/opensslconf.h> 163 #include <openssl/x509.h> 164 165 #include "bytestring.h" 166 #include "crypto_internal.h" 167 #include "dtls_local.h" 168 #include "ssl_local.h" 169 #include "ssl_sigalgs.h" 170 #include "ssl_tlsext.h" 171 172 static int ssl3_get_client_hello(SSL *s); 173 static int ssl3_send_dtls_hello_verify_request(SSL *s); 174 static int ssl3_send_server_hello(SSL *s); 175 static int ssl3_send_hello_request(SSL *s); 176 static int ssl3_send_server_certificate(SSL *s); 177 static int ssl3_send_server_key_exchange(SSL *s); 178 static int ssl3_send_certificate_request(SSL *s); 179 static int ssl3_send_server_done(SSL *s); 180 static int ssl3_get_client_certificate(SSL *s); 181 static int ssl3_get_client_key_exchange(SSL *s); 182 static int ssl3_get_cert_verify(SSL *s); 183 static int ssl3_send_newsession_ticket(SSL *s); 184 static int ssl3_send_cert_status(SSL *s); 185 static int ssl3_send_server_change_cipher_spec(SSL *s); 186 static int ssl3_send_server_finished(SSL *s); 187 static int ssl3_get_client_finished(SSL *s); 188 189 int 190 ssl3_accept(SSL *s) 191 { 192 unsigned long alg_k; 193 int new_state, state, skip = 0; 194 int listen = 0; 195 int ret = -1; 196 197 ERR_clear_error(); 198 errno = 0; 199 200 if (SSL_is_dtls(s)) 201 listen = s->d1->listen; 202 203 /* init things to blank */ 204 s->in_handshake++; 205 if (!SSL_in_init(s) || SSL_in_before(s)) 206 SSL_clear(s); 207 208 if (SSL_is_dtls(s)) 209 s->d1->listen = listen; 210 211 for (;;) { 212 state = s->s3->hs.state; 213 214 switch (s->s3->hs.state) { 215 case SSL_ST_RENEGOTIATE: 216 s->renegotiate = 1; 217 /* s->s3->hs.state=SSL_ST_ACCEPT; */ 218 219 case SSL_ST_BEFORE: 220 case SSL_ST_ACCEPT: 221 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 222 case SSL_ST_OK|SSL_ST_ACCEPT: 223 s->server = 1; 224 225 ssl_info_callback(s, SSL_CB_HANDSHAKE_START, 1); 226 227 if (!ssl_legacy_stack_version(s, s->version)) { 228 SSLerror(s, ERR_R_INTERNAL_ERROR); 229 ret = -1; 230 goto end; 231 } 232 233 if (!ssl_supported_tls_version_range(s, 234 &s->s3->hs.our_min_tls_version, 235 &s->s3->hs.our_max_tls_version)) { 236 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 237 ret = -1; 238 goto end; 239 } 240 241 if (!ssl_security_version(s, 242 s->s3->hs.our_min_tls_version)) { 243 SSLerror(s, SSL_R_VERSION_TOO_LOW); 244 ret = -1; 245 goto end; 246 } 247 248 if (!ssl3_setup_init_buffer(s)) { 249 ret = -1; 250 goto end; 251 } 252 if (!ssl3_setup_buffers(s)) { 253 ret = -1; 254 goto end; 255 } 256 257 s->init_num = 0; 258 259 if (s->s3->hs.state != SSL_ST_RENEGOTIATE) { 260 /* 261 * Ok, we now need to push on a buffering BIO 262 * so that the output is sent in a way that 263 * TCP likes :-) 264 */ 265 if (!ssl_init_wbio_buffer(s, 1)) { 266 ret = -1; 267 goto end; 268 } 269 270 if (!tls1_transcript_init(s)) { 271 ret = -1; 272 goto end; 273 } 274 275 s->s3->hs.state = SSL3_ST_SR_CLNT_HELLO_A; 276 s->ctx->stats.sess_accept++; 277 } else if (!SSL_is_dtls(s) && !s->s3->send_connection_binding) { 278 /* 279 * Server attempting to renegotiate with 280 * client that doesn't support secure 281 * renegotiation. 282 */ 283 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 284 ssl3_send_alert(s, SSL3_AL_FATAL, 285 SSL_AD_HANDSHAKE_FAILURE); 286 ret = -1; 287 goto end; 288 } else { 289 /* 290 * s->s3->hs.state == SSL_ST_RENEGOTIATE, 291 * we will just send a HelloRequest. 292 */ 293 s->ctx->stats.sess_accept_renegotiate++; 294 s->s3->hs.state = SSL3_ST_SW_HELLO_REQ_A; 295 } 296 break; 297 298 case SSL3_ST_SW_HELLO_REQ_A: 299 case SSL3_ST_SW_HELLO_REQ_B: 300 s->shutdown = 0; 301 if (SSL_is_dtls(s)) { 302 dtls1_clear_record_buffer(s); 303 dtls1_start_timer(s); 304 } 305 ret = ssl3_send_hello_request(s); 306 if (ret <= 0) 307 goto end; 308 if (SSL_is_dtls(s)) 309 s->s3->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 310 else 311 s->s3->hs.tls12.next_state = SSL3_ST_SW_HELLO_REQ_C; 312 s->s3->hs.state = SSL3_ST_SW_FLUSH; 313 s->init_num = 0; 314 315 if (SSL_is_dtls(s)) { 316 if (!tls1_transcript_init(s)) { 317 ret = -1; 318 goto end; 319 } 320 } 321 break; 322 323 case SSL3_ST_SW_HELLO_REQ_C: 324 s->s3->hs.state = SSL_ST_OK; 325 break; 326 327 case SSL3_ST_SR_CLNT_HELLO_A: 328 case SSL3_ST_SR_CLNT_HELLO_B: 329 case SSL3_ST_SR_CLNT_HELLO_C: 330 s->shutdown = 0; 331 if (SSL_is_dtls(s)) { 332 ret = ssl3_get_client_hello(s); 333 if (ret <= 0) 334 goto end; 335 dtls1_stop_timer(s); 336 337 if (ret == 1 && 338 (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) 339 s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; 340 else 341 s->s3->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 342 343 s->init_num = 0; 344 345 /* 346 * Reflect ClientHello sequence to remain 347 * stateless while listening. 348 */ 349 if (listen) { 350 tls12_record_layer_reflect_seq_num( 351 s->rl); 352 } 353 354 /* If we're just listening, stop here */ 355 if (listen && s->s3->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 356 ret = 2; 357 s->d1->listen = 0; 358 /* 359 * Set expected sequence numbers to 360 * continue the handshake. 361 */ 362 s->d1->handshake_read_seq = 2; 363 s->d1->handshake_write_seq = 1; 364 s->d1->next_handshake_write_seq = 1; 365 goto end; 366 } 367 } else { 368 if (s->rwstate != SSL_X509_LOOKUP) { 369 ret = ssl3_get_client_hello(s); 370 if (ret <= 0) 371 goto end; 372 } 373 374 s->renegotiate = 2; 375 s->s3->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 376 s->init_num = 0; 377 } 378 break; 379 380 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: 381 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: 382 ret = ssl3_send_dtls_hello_verify_request(s); 383 if (ret <= 0) 384 goto end; 385 s->s3->hs.state = SSL3_ST_SW_FLUSH; 386 s->s3->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 387 388 /* HelloVerifyRequest resets Finished MAC. */ 389 tls1_transcript_reset(s); 390 break; 391 392 case SSL3_ST_SW_SRVR_HELLO_A: 393 case SSL3_ST_SW_SRVR_HELLO_B: 394 if (SSL_is_dtls(s)) { 395 s->renegotiate = 2; 396 dtls1_start_timer(s); 397 } 398 ret = ssl3_send_server_hello(s); 399 if (ret <= 0) 400 goto end; 401 if (s->hit) { 402 if (s->tlsext_ticket_expected) 403 s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 404 else 405 s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 406 } else { 407 s->s3->hs.state = SSL3_ST_SW_CERT_A; 408 } 409 s->init_num = 0; 410 break; 411 412 case SSL3_ST_SW_CERT_A: 413 case SSL3_ST_SW_CERT_B: 414 /* Check if it is anon DH or anon ECDH. */ 415 if (!(s->s3->hs.cipher->algorithm_auth & 416 SSL_aNULL)) { 417 if (SSL_is_dtls(s)) 418 dtls1_start_timer(s); 419 ret = ssl3_send_server_certificate(s); 420 if (ret <= 0) 421 goto end; 422 if (s->tlsext_status_expected) 423 s->s3->hs.state = SSL3_ST_SW_CERT_STATUS_A; 424 else 425 s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 426 } else { 427 skip = 1; 428 s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 429 } 430 s->init_num = 0; 431 break; 432 433 case SSL3_ST_SW_KEY_EXCH_A: 434 case SSL3_ST_SW_KEY_EXCH_B: 435 alg_k = s->s3->hs.cipher->algorithm_mkey; 436 437 /* 438 * Only send if using a DH key exchange. 439 * 440 * For ECC ciphersuites, we send a ServerKeyExchange 441 * message only if the cipher suite is ECDHE. In other 442 * cases, the server certificate contains the server's 443 * public key for key exchange. 444 */ 445 if (alg_k & (SSL_kDHE|SSL_kECDHE)) { 446 if (SSL_is_dtls(s)) 447 dtls1_start_timer(s); 448 ret = ssl3_send_server_key_exchange(s); 449 if (ret <= 0) 450 goto end; 451 } else 452 skip = 1; 453 454 s->s3->hs.state = SSL3_ST_SW_CERT_REQ_A; 455 s->init_num = 0; 456 break; 457 458 case SSL3_ST_SW_CERT_REQ_A: 459 case SSL3_ST_SW_CERT_REQ_B: 460 /* 461 * Determine whether or not we need to request a 462 * certificate. 463 * 464 * Do not request a certificate if: 465 * 466 * - We did not ask for it (SSL_VERIFY_PEER is unset). 467 * 468 * - SSL_VERIFY_CLIENT_ONCE is set and we are 469 * renegotiating. 470 * 471 * - We are using an anonymous ciphersuites 472 * (see section "Certificate request" in SSL 3 drafts 473 * and in RFC 2246) ... except when the application 474 * insists on verification (against the specs, but 475 * s3_clnt.c accepts this for SSL 3). 476 */ 477 if (!(s->verify_mode & SSL_VERIFY_PEER) || 478 ((s->session->peer_cert != NULL) && 479 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 480 ((s->s3->hs.cipher->algorithm_auth & 481 SSL_aNULL) && !(s->verify_mode & 482 SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { 483 /* No cert request. */ 484 skip = 1; 485 s->s3->hs.tls12.cert_request = 0; 486 s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_A; 487 488 if (!SSL_is_dtls(s)) 489 tls1_transcript_free(s); 490 } else { 491 s->s3->hs.tls12.cert_request = 1; 492 if (SSL_is_dtls(s)) 493 dtls1_start_timer(s); 494 ret = ssl3_send_certificate_request(s); 495 if (ret <= 0) 496 goto end; 497 s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_A; 498 s->init_num = 0; 499 } 500 break; 501 502 case SSL3_ST_SW_SRVR_DONE_A: 503 case SSL3_ST_SW_SRVR_DONE_B: 504 if (SSL_is_dtls(s)) 505 dtls1_start_timer(s); 506 ret = ssl3_send_server_done(s); 507 if (ret <= 0) 508 goto end; 509 s->s3->hs.tls12.next_state = SSL3_ST_SR_CERT_A; 510 s->s3->hs.state = SSL3_ST_SW_FLUSH; 511 s->init_num = 0; 512 break; 513 514 case SSL3_ST_SW_FLUSH: 515 /* 516 * This code originally checked to see if 517 * any data was pending using BIO_CTRL_INFO 518 * and then flushed. This caused problems 519 * as documented in PR#1939. The proposed 520 * fix doesn't completely resolve this issue 521 * as buggy implementations of BIO_CTRL_PENDING 522 * still exist. So instead we just flush 523 * unconditionally. 524 */ 525 s->rwstate = SSL_WRITING; 526 if (BIO_flush(s->wbio) <= 0) { 527 if (SSL_is_dtls(s)) { 528 /* If the write error was fatal, stop trying. */ 529 if (!BIO_should_retry(s->wbio)) { 530 s->rwstate = SSL_NOTHING; 531 s->s3->hs.state = s->s3->hs.tls12.next_state; 532 } 533 } 534 ret = -1; 535 goto end; 536 } 537 s->rwstate = SSL_NOTHING; 538 s->s3->hs.state = s->s3->hs.tls12.next_state; 539 break; 540 541 case SSL3_ST_SR_CERT_A: 542 case SSL3_ST_SR_CERT_B: 543 if (s->s3->hs.tls12.cert_request != 0) { 544 ret = ssl3_get_client_certificate(s); 545 if (ret <= 0) 546 goto end; 547 } 548 s->init_num = 0; 549 s->s3->hs.state = SSL3_ST_SR_KEY_EXCH_A; 550 break; 551 552 case SSL3_ST_SR_KEY_EXCH_A: 553 case SSL3_ST_SR_KEY_EXCH_B: 554 ret = ssl3_get_client_key_exchange(s); 555 if (ret <= 0) 556 goto end; 557 558 if (SSL_is_dtls(s)) { 559 s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 560 s->init_num = 0; 561 } 562 563 alg_k = s->s3->hs.cipher->algorithm_mkey; 564 if (SSL_USE_SIGALGS(s)) { 565 s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 566 s->init_num = 0; 567 if (!s->session->peer_cert) 568 break; 569 /* 570 * Freeze the transcript for use during client 571 * certificate verification. 572 */ 573 tls1_transcript_freeze(s); 574 } else { 575 s->s3->hs.state = SSL3_ST_SR_CERT_VRFY_A; 576 s->init_num = 0; 577 578 tls1_transcript_free(s); 579 580 /* 581 * We need to get hashes here so if there is 582 * a client cert, it can be verified. 583 */ 584 if (!tls1_transcript_hash_value(s, 585 s->s3->hs.tls12.cert_verify, 586 sizeof(s->s3->hs.tls12.cert_verify), 587 NULL)) { 588 ret = -1; 589 goto end; 590 } 591 } 592 break; 593 594 case SSL3_ST_SR_CERT_VRFY_A: 595 case SSL3_ST_SR_CERT_VRFY_B: 596 if (SSL_is_dtls(s)) 597 s->d1->change_cipher_spec_ok = 1; 598 else 599 s->s3->flags |= SSL3_FLAGS_CCS_OK; 600 601 /* we should decide if we expected this one */ 602 ret = ssl3_get_cert_verify(s); 603 if (ret <= 0) 604 goto end; 605 s->s3->hs.state = SSL3_ST_SR_FINISHED_A; 606 s->init_num = 0; 607 break; 608 609 case SSL3_ST_SR_FINISHED_A: 610 case SSL3_ST_SR_FINISHED_B: 611 if (SSL_is_dtls(s)) 612 s->d1->change_cipher_spec_ok = 1; 613 else 614 s->s3->flags |= SSL3_FLAGS_CCS_OK; 615 ret = ssl3_get_client_finished(s); 616 if (ret <= 0) 617 goto end; 618 if (SSL_is_dtls(s)) 619 dtls1_stop_timer(s); 620 if (s->hit) 621 s->s3->hs.state = SSL_ST_OK; 622 else if (s->tlsext_ticket_expected) 623 s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 624 else 625 s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 626 s->init_num = 0; 627 break; 628 629 case SSL3_ST_SW_SESSION_TICKET_A: 630 case SSL3_ST_SW_SESSION_TICKET_B: 631 ret = ssl3_send_newsession_ticket(s); 632 if (ret <= 0) 633 goto end; 634 s->s3->hs.state = SSL3_ST_SW_CHANGE_A; 635 s->init_num = 0; 636 break; 637 638 case SSL3_ST_SW_CERT_STATUS_A: 639 case SSL3_ST_SW_CERT_STATUS_B: 640 ret = ssl3_send_cert_status(s); 641 if (ret <= 0) 642 goto end; 643 s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_A; 644 s->init_num = 0; 645 break; 646 647 case SSL3_ST_SW_CHANGE_A: 648 case SSL3_ST_SW_CHANGE_B: 649 ret = ssl3_send_server_change_cipher_spec(s); 650 if (ret <= 0) 651 goto end; 652 s->s3->hs.state = SSL3_ST_SW_FINISHED_A; 653 s->init_num = 0; 654 s->session->cipher_value = s->s3->hs.cipher->value; 655 656 if (!tls1_setup_key_block(s)) { 657 ret = -1; 658 goto end; 659 } 660 if (!tls1_change_write_cipher_state(s)) { 661 ret = -1; 662 goto end; 663 } 664 break; 665 666 case SSL3_ST_SW_FINISHED_A: 667 case SSL3_ST_SW_FINISHED_B: 668 ret = ssl3_send_server_finished(s); 669 if (ret <= 0) 670 goto end; 671 s->s3->hs.state = SSL3_ST_SW_FLUSH; 672 if (s->hit) { 673 s->s3->hs.tls12.next_state = SSL3_ST_SR_FINISHED_A; 674 tls1_transcript_free(s); 675 } else 676 s->s3->hs.tls12.next_state = SSL_ST_OK; 677 s->init_num = 0; 678 break; 679 680 case SSL_ST_OK: 681 /* clean a few things up */ 682 tls1_cleanup_key_block(s); 683 684 if (s->s3->handshake_transcript != NULL) { 685 SSLerror(s, ERR_R_INTERNAL_ERROR); 686 ret = -1; 687 goto end; 688 } 689 690 if (!SSL_is_dtls(s)) 691 ssl3_release_init_buffer(s); 692 693 /* remove buffering on output */ 694 ssl_free_wbio_buffer(s); 695 696 s->init_num = 0; 697 698 /* Skipped if we just sent a HelloRequest. */ 699 if (s->renegotiate == 2) { 700 s->renegotiate = 0; 701 s->new_session = 0; 702 703 ssl_update_cache(s, SSL_SESS_CACHE_SERVER); 704 705 s->ctx->stats.sess_accept_good++; 706 /* s->server=1; */ 707 s->handshake_func = ssl3_accept; 708 709 ssl_info_callback(s, SSL_CB_HANDSHAKE_DONE, 1); 710 } 711 712 ret = 1; 713 714 if (SSL_is_dtls(s)) { 715 /* Done handshaking, next message is client hello. */ 716 s->d1->handshake_read_seq = 0; 717 /* Next message is server hello. */ 718 s->d1->handshake_write_seq = 0; 719 s->d1->next_handshake_write_seq = 0; 720 } 721 goto end; 722 /* break; */ 723 724 default: 725 SSLerror(s, SSL_R_UNKNOWN_STATE); 726 ret = -1; 727 goto end; 728 /* break; */ 729 } 730 731 if (!s->s3->hs.tls12.reuse_message && !skip) { 732 if (s->s3->hs.state != state) { 733 new_state = s->s3->hs.state; 734 s->s3->hs.state = state; 735 ssl_info_callback(s, SSL_CB_ACCEPT_LOOP, 1); 736 s->s3->hs.state = new_state; 737 } 738 } 739 skip = 0; 740 } 741 end: 742 /* BIO_flush(s->wbio); */ 743 s->in_handshake--; 744 ssl_info_callback(s, SSL_CB_ACCEPT_EXIT, ret); 745 746 return (ret); 747 } 748 749 static int 750 ssl3_send_hello_request(SSL *s) 751 { 752 CBB cbb, hello; 753 754 memset(&cbb, 0, sizeof(cbb)); 755 756 if (s->s3->hs.state == SSL3_ST_SW_HELLO_REQ_A) { 757 if (!ssl3_handshake_msg_start(s, &cbb, &hello, 758 SSL3_MT_HELLO_REQUEST)) 759 goto err; 760 if (!ssl3_handshake_msg_finish(s, &cbb)) 761 goto err; 762 763 s->s3->hs.state = SSL3_ST_SW_HELLO_REQ_B; 764 } 765 766 /* SSL3_ST_SW_HELLO_REQ_B */ 767 return (ssl3_handshake_write(s)); 768 769 err: 770 CBB_cleanup(&cbb); 771 772 return (-1); 773 } 774 775 static int 776 ssl3_get_client_hello(SSL *s) 777 { 778 CBS cbs, client_random, session_id, cookie, cipher_suites; 779 CBS compression_methods; 780 uint16_t client_version; 781 uint8_t comp_method; 782 int comp_null; 783 int i, j, al, ret, cookie_valid = 0; 784 SSL_CIPHER *c; 785 STACK_OF(SSL_CIPHER) *ciphers = NULL; 786 const SSL_METHOD *method; 787 uint16_t shared_version; 788 789 /* 790 * We do this so that we will respond with our native type. 791 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 792 * This down switching should be handled by a different method. 793 * If we are SSLv3, we will respond with SSLv3, even if prompted with 794 * TLSv1. 795 */ 796 if (s->s3->hs.state == SSL3_ST_SR_CLNT_HELLO_A) 797 s->s3->hs.state = SSL3_ST_SR_CLNT_HELLO_B; 798 799 s->first_packet = 1; 800 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CLNT_HELLO_B, 801 SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO, 802 SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 803 return ret; 804 s->first_packet = 0; 805 806 ret = -1; 807 808 if (s->init_num < 0) 809 goto err; 810 811 CBS_init(&cbs, s->init_msg, s->init_num); 812 813 /* Parse client hello up until the extensions (if any). */ 814 if (!CBS_get_u16(&cbs, &client_version)) 815 goto decode_err; 816 if (!CBS_get_bytes(&cbs, &client_random, SSL3_RANDOM_SIZE)) 817 goto decode_err; 818 if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) 819 goto decode_err; 820 if (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE) { 821 al = SSL_AD_ILLEGAL_PARAMETER; 822 SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG); 823 goto fatal_err; 824 } 825 if (SSL_is_dtls(s)) { 826 if (!CBS_get_u8_length_prefixed(&cbs, &cookie)) 827 goto decode_err; 828 } 829 if (!CBS_get_u16_length_prefixed(&cbs, &cipher_suites)) 830 goto decode_err; 831 if (!CBS_get_u8_length_prefixed(&cbs, &compression_methods)) 832 goto decode_err; 833 834 /* 835 * Use version from inside client hello, not from record header. 836 * (may differ: see RFC 2246, Appendix E, second paragraph) 837 */ 838 if (!ssl_max_shared_version(s, client_version, &shared_version)) { 839 if ((client_version >> 8) == SSL3_VERSION_MAJOR && 840 !tls12_record_layer_write_protected(s->rl)) { 841 /* 842 * Similar to ssl3_get_record, send alert using remote 843 * version number. 844 */ 845 s->version = client_version; 846 } 847 SSLerror(s, SSL_R_WRONG_VERSION_NUMBER); 848 al = SSL_AD_PROTOCOL_VERSION; 849 goto fatal_err; 850 } 851 s->s3->hs.peer_legacy_version = client_version; 852 s->version = shared_version; 853 854 s->s3->hs.negotiated_tls_version = ssl_tls_version(shared_version); 855 if (s->s3->hs.negotiated_tls_version == 0) { 856 SSLerror(s, ERR_R_INTERNAL_ERROR); 857 goto err; 858 } 859 860 if ((method = ssl_get_method(shared_version)) == NULL) { 861 SSLerror(s, ERR_R_INTERNAL_ERROR); 862 goto err; 863 } 864 s->method = method; 865 866 /* 867 * If we require cookies (DTLS) and this ClientHello does not contain 868 * one, just return since we do not want to allocate any memory yet. 869 * So check cookie length... 870 */ 871 if (SSL_is_dtls(s)) { 872 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { 873 if (CBS_len(&cookie) == 0) 874 return (1); 875 } 876 } 877 878 if (!CBS_write_bytes(&client_random, s->s3->client_random, 879 sizeof(s->s3->client_random), NULL)) 880 goto err; 881 882 s->hit = 0; 883 884 /* 885 * Versions before 0.9.7 always allow clients to resume sessions in 886 * renegotiation. 0.9.7 and later allow this by default, but optionally 887 * ignore resumption requests with flag 888 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag 889 * rather than a change to default behavior so that applications 890 * relying on this for security won't even compile against older 891 * library versions). 892 * 893 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() 894 * to request renegotiation but not a new session (s->new_session 895 * remains unset): for servers, this essentially just means that the 896 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be 897 * ignored. 898 */ 899 if ((s->new_session && (s->options & 900 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { 901 if (!ssl_get_new_session(s, 1)) 902 goto err; 903 } else { 904 CBS ext_block; 905 906 CBS_dup(&cbs, &ext_block); 907 908 i = ssl_get_prev_session(s, &session_id, &ext_block, &al); 909 if (i == 1) { /* previous session */ 910 s->hit = 1; 911 } else if (i == -1) 912 goto fatal_err; 913 else { 914 /* i == 0 */ 915 if (!ssl_get_new_session(s, 1)) 916 goto err; 917 } 918 } 919 920 if (SSL_is_dtls(s)) { 921 /* 922 * The ClientHello may contain a cookie even if the HelloVerify 923 * message has not been sent - make sure that it does not cause 924 * an overflow. 925 */ 926 if (CBS_len(&cookie) > sizeof(s->d1->rcvd_cookie)) { 927 al = SSL_AD_DECODE_ERROR; 928 SSLerror(s, SSL_R_COOKIE_MISMATCH); 929 goto fatal_err; 930 } 931 932 /* Verify the cookie if appropriate option is set. */ 933 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 934 CBS_len(&cookie) > 0) { 935 size_t cookie_len; 936 937 /* XXX - rcvd_cookie seems to only be used here... */ 938 if (!CBS_write_bytes(&cookie, s->d1->rcvd_cookie, 939 sizeof(s->d1->rcvd_cookie), &cookie_len)) 940 goto err; 941 942 if (s->ctx->app_verify_cookie_cb != NULL) { 943 if (s->ctx->app_verify_cookie_cb(s, 944 s->d1->rcvd_cookie, cookie_len) == 0) { 945 al = SSL_AD_HANDSHAKE_FAILURE; 946 SSLerror(s, SSL_R_COOKIE_MISMATCH); 947 goto fatal_err; 948 } 949 /* else cookie verification succeeded */ 950 /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */ 951 } else if (timingsafe_memcmp(s->d1->rcvd_cookie, 952 s->d1->cookie, s->d1->cookie_len) != 0) { 953 /* default verification */ 954 al = SSL_AD_HANDSHAKE_FAILURE; 955 SSLerror(s, SSL_R_COOKIE_MISMATCH); 956 goto fatal_err; 957 } 958 cookie_valid = 1; 959 } 960 } 961 962 /* XXX - This logic seems wrong... */ 963 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) { 964 /* we need a cipher if we are not resuming a session */ 965 al = SSL_AD_ILLEGAL_PARAMETER; 966 SSLerror(s, SSL_R_NO_CIPHERS_SPECIFIED); 967 goto fatal_err; 968 } 969 970 if (CBS_len(&cipher_suites) > 0) { 971 if ((ciphers = ssl_bytes_to_cipher_list(s, 972 &cipher_suites)) == NULL) 973 goto err; 974 } 975 976 /* If it is a hit, check that the cipher is in the list */ 977 /* XXX - CBS_len(&cipher_suites) will always be zero here... */ 978 if (s->hit && CBS_len(&cipher_suites) > 0) { 979 j = 0; 980 981 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 982 c = sk_SSL_CIPHER_value(ciphers, i); 983 if (c->value == s->session->cipher_value) { 984 j = 1; 985 break; 986 } 987 } 988 if (j == 0) { 989 /* 990 * We need to have the cipher in the cipher 991 * list if we are asked to reuse it 992 */ 993 al = SSL_AD_ILLEGAL_PARAMETER; 994 SSLerror(s, SSL_R_REQUIRED_CIPHER_MISSING); 995 goto fatal_err; 996 } 997 } 998 999 comp_null = 0; 1000 while (CBS_len(&compression_methods) > 0) { 1001 if (!CBS_get_u8(&compression_methods, &comp_method)) 1002 goto decode_err; 1003 if (comp_method == 0) 1004 comp_null = 1; 1005 } 1006 if (comp_null == 0) { 1007 al = SSL_AD_DECODE_ERROR; 1008 SSLerror(s, SSL_R_NO_COMPRESSION_SPECIFIED); 1009 goto fatal_err; 1010 } 1011 1012 if (!tlsext_server_parse(s, SSL_TLSEXT_MSG_CH, &cbs, &al)) { 1013 SSLerror(s, SSL_R_PARSE_TLSEXT); 1014 goto fatal_err; 1015 } 1016 1017 if (CBS_len(&cbs) != 0) 1018 goto decode_err; 1019 1020 if (!s->s3->renegotiate_seen && s->renegotiate) { 1021 al = SSL_AD_HANDSHAKE_FAILURE; 1022 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1023 goto fatal_err; 1024 } 1025 1026 if (ssl_check_clienthello_tlsext_early(s) <= 0) { 1027 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 1028 goto err; 1029 } 1030 1031 /* 1032 * Check if we want to use external pre-shared secret for this 1033 * handshake for not reused session only. We need to generate 1034 * server_random before calling tls_session_secret_cb in order to allow 1035 * SessionTicket processing to use it in key derivation. 1036 */ 1037 arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); 1038 1039 if (s->s3->hs.our_max_tls_version >= TLS1_2_VERSION && 1040 s->s3->hs.negotiated_tls_version < s->s3->hs.our_max_tls_version) { 1041 /* 1042 * RFC 8446 section 4.1.3. If we are downgrading from TLS 1.3 1043 * we must set the last 8 bytes of the server random to magical 1044 * values to indicate we meant to downgrade. For TLS 1.2 it is 1045 * recommended that we do the same. 1046 */ 1047 size_t index = SSL3_RANDOM_SIZE - sizeof(tls13_downgrade_12); 1048 uint8_t *magic = &s->s3->server_random[index]; 1049 if (s->s3->hs.negotiated_tls_version == TLS1_2_VERSION) { 1050 /* Indicate we chose to downgrade to 1.2. */ 1051 memcpy(magic, tls13_downgrade_12, 1052 sizeof(tls13_downgrade_12)); 1053 } else { 1054 /* Indicate we chose to downgrade to 1.1 or lower */ 1055 memcpy(magic, tls13_downgrade_11, 1056 sizeof(tls13_downgrade_11)); 1057 } 1058 } 1059 1060 if (!s->hit && s->tls_session_secret_cb != NULL) { 1061 SSL_CIPHER *pref_cipher = NULL; 1062 int master_key_length = sizeof(s->session->master_key); 1063 1064 if (!s->tls_session_secret_cb(s, 1065 s->session->master_key, &master_key_length, ciphers, 1066 &pref_cipher, s->tls_session_secret_cb_arg)) { 1067 SSLerror(s, ERR_R_INTERNAL_ERROR); 1068 goto err; 1069 } 1070 if (master_key_length <= 0) { 1071 SSLerror(s, ERR_R_INTERNAL_ERROR); 1072 goto err; 1073 } 1074 s->session->master_key_length = master_key_length; 1075 1076 s->hit = 1; 1077 s->session->verify_result = X509_V_OK; 1078 1079 sk_SSL_CIPHER_free(s->s3->hs.client_ciphers); 1080 s->s3->hs.client_ciphers = ciphers; 1081 ciphers = NULL; 1082 1083 /* 1084 * XXX - this allows the callback to use any client cipher and 1085 * completely ignore the server cipher list. We should ensure 1086 * that the pref_cipher is in both the client list and the 1087 * server list. 1088 */ 1089 1090 /* Check if some cipher was preferred by the callback. */ 1091 if (pref_cipher == NULL) 1092 pref_cipher = ssl3_choose_cipher(s, s->s3->hs.client_ciphers, 1093 SSL_get_ciphers(s)); 1094 if (pref_cipher == NULL) { 1095 al = SSL_AD_HANDSHAKE_FAILURE; 1096 SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1097 goto fatal_err; 1098 } 1099 s->s3->hs.cipher = pref_cipher; 1100 1101 /* XXX - why? */ 1102 sk_SSL_CIPHER_free(s->cipher_list); 1103 s->cipher_list = sk_SSL_CIPHER_dup(s->s3->hs.client_ciphers); 1104 } 1105 1106 /* 1107 * Given s->session->ciphers and SSL_get_ciphers, we must 1108 * pick a cipher 1109 */ 1110 1111 if (!s->hit) { 1112 if (ciphers == NULL) { 1113 al = SSL_AD_ILLEGAL_PARAMETER; 1114 SSLerror(s, SSL_R_NO_CIPHERS_PASSED); 1115 goto fatal_err; 1116 } 1117 sk_SSL_CIPHER_free(s->s3->hs.client_ciphers); 1118 s->s3->hs.client_ciphers = ciphers; 1119 ciphers = NULL; 1120 1121 if ((c = ssl3_choose_cipher(s, s->s3->hs.client_ciphers, 1122 SSL_get_ciphers(s))) == NULL) { 1123 al = SSL_AD_HANDSHAKE_FAILURE; 1124 SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1125 goto fatal_err; 1126 } 1127 s->s3->hs.cipher = c; 1128 s->session->cipher_value = s->s3->hs.cipher->value; 1129 } else { 1130 s->s3->hs.cipher = ssl3_get_cipher_by_value(s->session->cipher_value); 1131 if (s->s3->hs.cipher == NULL) 1132 goto fatal_err; 1133 } 1134 1135 if (!tls1_transcript_hash_init(s)) 1136 goto err; 1137 1138 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) 1139 tls1_transcript_free(s); 1140 1141 /* 1142 * We now have the following setup. 1143 * client_random 1144 * cipher_list - our preferred list of ciphers 1145 * ciphers - the clients preferred list of ciphers 1146 * compression - basically ignored right now 1147 * ssl version is set - sslv3 1148 * s->session - The ssl session has been setup. 1149 * s->hit - session reuse flag 1150 * s->hs.cipher - the new cipher to use. 1151 */ 1152 1153 /* Handles TLS extensions that we couldn't check earlier */ 1154 if (ssl_check_clienthello_tlsext_late(s) <= 0) { 1155 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 1156 goto err; 1157 } 1158 1159 ret = cookie_valid ? 2 : 1; 1160 1161 if (0) { 1162 decode_err: 1163 al = SSL_AD_DECODE_ERROR; 1164 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1165 fatal_err: 1166 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1167 } 1168 err: 1169 sk_SSL_CIPHER_free(ciphers); 1170 1171 return (ret); 1172 } 1173 1174 static int 1175 ssl3_send_dtls_hello_verify_request(SSL *s) 1176 { 1177 CBB cbb, verify, cookie; 1178 1179 memset(&cbb, 0, sizeof(cbb)); 1180 1181 if (s->s3->hs.state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { 1182 if (s->ctx->app_gen_cookie_cb == NULL || 1183 s->ctx->app_gen_cookie_cb(s, s->d1->cookie, 1184 &(s->d1->cookie_len)) == 0) { 1185 SSLerror(s, ERR_R_INTERNAL_ERROR); 1186 return 0; 1187 } 1188 1189 /* 1190 * Per RFC 6347 section 4.2.1, the HelloVerifyRequest should 1191 * always contain DTLSv1.0 regardless of the version that is 1192 * going to be negotiated. 1193 */ 1194 if (!ssl3_handshake_msg_start(s, &cbb, &verify, 1195 DTLS1_MT_HELLO_VERIFY_REQUEST)) 1196 goto err; 1197 if (!CBB_add_u16(&verify, DTLS1_VERSION)) 1198 goto err; 1199 if (!CBB_add_u8_length_prefixed(&verify, &cookie)) 1200 goto err; 1201 if (!CBB_add_bytes(&cookie, s->d1->cookie, s->d1->cookie_len)) 1202 goto err; 1203 if (!ssl3_handshake_msg_finish(s, &cbb)) 1204 goto err; 1205 1206 s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; 1207 } 1208 1209 /* s->s3->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 1210 return (ssl3_handshake_write(s)); 1211 1212 err: 1213 CBB_cleanup(&cbb); 1214 1215 return (-1); 1216 } 1217 1218 static int 1219 ssl3_send_server_hello(SSL *s) 1220 { 1221 CBB cbb, server_hello, session_id; 1222 size_t sl; 1223 1224 memset(&cbb, 0, sizeof(cbb)); 1225 1226 if (s->s3->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 1227 if (!ssl3_handshake_msg_start(s, &cbb, &server_hello, 1228 SSL3_MT_SERVER_HELLO)) 1229 goto err; 1230 1231 if (!CBB_add_u16(&server_hello, s->version)) 1232 goto err; 1233 if (!CBB_add_bytes(&server_hello, s->s3->server_random, 1234 sizeof(s->s3->server_random))) 1235 goto err; 1236 1237 /* 1238 * There are several cases for the session ID to send 1239 * back in the server hello: 1240 * 1241 * - For session reuse from the session cache, 1242 * we send back the old session ID. 1243 * - If stateless session reuse (using a session ticket) 1244 * is successful, we send back the client's "session ID" 1245 * (which doesn't actually identify the session). 1246 * - If it is a new session, we send back the new 1247 * session ID. 1248 * - However, if we want the new session to be single-use, 1249 * we send back a 0-length session ID. 1250 * 1251 * s->hit is non-zero in either case of session reuse, 1252 * so the following won't overwrite an ID that we're supposed 1253 * to send back. 1254 */ 1255 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) 1256 && !s->hit) 1257 s->session->session_id_length = 0; 1258 1259 sl = s->session->session_id_length; 1260 if (sl > sizeof(s->session->session_id)) { 1261 SSLerror(s, ERR_R_INTERNAL_ERROR); 1262 goto err; 1263 } 1264 if (!CBB_add_u8_length_prefixed(&server_hello, &session_id)) 1265 goto err; 1266 if (!CBB_add_bytes(&session_id, s->session->session_id, sl)) 1267 goto err; 1268 1269 /* Cipher suite. */ 1270 if (!CBB_add_u16(&server_hello, s->s3->hs.cipher->value)) 1271 goto err; 1272 1273 /* Compression method (null). */ 1274 if (!CBB_add_u8(&server_hello, 0)) 1275 goto err; 1276 1277 /* TLS extensions */ 1278 if (!tlsext_server_build(s, SSL_TLSEXT_MSG_SH, &server_hello)) { 1279 SSLerror(s, ERR_R_INTERNAL_ERROR); 1280 goto err; 1281 } 1282 1283 if (!ssl3_handshake_msg_finish(s, &cbb)) 1284 goto err; 1285 } 1286 1287 /* SSL3_ST_SW_SRVR_HELLO_B */ 1288 return (ssl3_handshake_write(s)); 1289 1290 err: 1291 CBB_cleanup(&cbb); 1292 1293 return (-1); 1294 } 1295 1296 static int 1297 ssl3_send_server_done(SSL *s) 1298 { 1299 CBB cbb, done; 1300 1301 memset(&cbb, 0, sizeof(cbb)); 1302 1303 if (s->s3->hs.state == SSL3_ST_SW_SRVR_DONE_A) { 1304 if (!ssl3_handshake_msg_start(s, &cbb, &done, 1305 SSL3_MT_SERVER_DONE)) 1306 goto err; 1307 if (!ssl3_handshake_msg_finish(s, &cbb)) 1308 goto err; 1309 1310 s->s3->hs.state = SSL3_ST_SW_SRVR_DONE_B; 1311 } 1312 1313 /* SSL3_ST_SW_SRVR_DONE_B */ 1314 return (ssl3_handshake_write(s)); 1315 1316 err: 1317 CBB_cleanup(&cbb); 1318 1319 return (-1); 1320 } 1321 1322 static int 1323 ssl3_send_server_kex_dhe(SSL *s, CBB *cbb) 1324 { 1325 int nid = NID_dhKeyAgreement; 1326 1327 tls_key_share_free(s->s3->hs.key_share); 1328 if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 1329 goto err; 1330 1331 if (s->cert->dhe_params_auto != 0) { 1332 size_t key_bits; 1333 1334 if ((key_bits = ssl_dhe_params_auto_key_bits(s)) == 0) { 1335 SSLerror(s, ERR_R_INTERNAL_ERROR); 1336 ssl3_send_alert(s, SSL3_AL_FATAL, 1337 SSL_AD_INTERNAL_ERROR); 1338 goto err; 1339 } 1340 tls_key_share_set_key_bits(s->s3->hs.key_share, 1341 key_bits); 1342 } else { 1343 DH *dh_params = s->cert->dhe_params; 1344 1345 if (dh_params == NULL && s->cert->dhe_params_cb != NULL) 1346 dh_params = s->cert->dhe_params_cb(s, 0, 1347 SSL_C_PKEYLENGTH(s->s3->hs.cipher)); 1348 1349 if (dh_params == NULL) { 1350 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1351 ssl3_send_alert(s, SSL3_AL_FATAL, 1352 SSL_AD_HANDSHAKE_FAILURE); 1353 goto err; 1354 } 1355 1356 if (!tls_key_share_set_dh_params(s->s3->hs.key_share, dh_params)) 1357 goto err; 1358 } 1359 1360 if (!tls_key_share_generate(s->s3->hs.key_share)) 1361 goto err; 1362 1363 if (!tls_key_share_params(s->s3->hs.key_share, cbb)) 1364 goto err; 1365 if (!tls_key_share_public(s->s3->hs.key_share, cbb)) 1366 goto err; 1367 1368 if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 1369 SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 1370 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1371 return 0; 1372 } 1373 1374 return 1; 1375 1376 err: 1377 return 0; 1378 } 1379 1380 static int 1381 ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb) 1382 { 1383 CBB public; 1384 int nid; 1385 1386 if (!tls1_get_supported_group(s, &nid)) { 1387 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1388 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1389 goto err; 1390 } 1391 1392 tls_key_share_free(s->s3->hs.key_share); 1393 if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 1394 goto err; 1395 1396 if (!tls_key_share_generate(s->s3->hs.key_share)) 1397 goto err; 1398 1399 /* 1400 * ECC key exchange - see RFC 8422, section 5.4. 1401 */ 1402 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) 1403 goto err; 1404 if (!CBB_add_u16(cbb, tls_key_share_group(s->s3->hs.key_share))) 1405 goto err; 1406 if (!CBB_add_u8_length_prefixed(cbb, &public)) 1407 goto err; 1408 if (!tls_key_share_public(s->s3->hs.key_share, &public)) 1409 goto err; 1410 if (!CBB_flush(cbb)) 1411 goto err; 1412 1413 return 1; 1414 1415 err: 1416 return 0; 1417 } 1418 1419 static int 1420 ssl3_send_server_key_exchange(SSL *s) 1421 { 1422 CBB cbb, cbb_signature, cbb_signed_params, server_kex; 1423 CBS params; 1424 const struct ssl_sigalg *sigalg = NULL; 1425 unsigned char *signed_params = NULL; 1426 size_t signed_params_len; 1427 unsigned char *signature = NULL; 1428 size_t signature_len = 0; 1429 const EVP_MD *md = NULL; 1430 unsigned long type; 1431 EVP_MD_CTX *md_ctx = NULL; 1432 EVP_PKEY_CTX *pctx; 1433 EVP_PKEY *pkey; 1434 int al; 1435 1436 memset(&cbb, 0, sizeof(cbb)); 1437 memset(&cbb_signed_params, 0, sizeof(cbb_signed_params)); 1438 1439 if ((md_ctx = EVP_MD_CTX_new()) == NULL) 1440 goto err; 1441 1442 if (s->s3->hs.state == SSL3_ST_SW_KEY_EXCH_A) { 1443 1444 if (!ssl3_handshake_msg_start(s, &cbb, &server_kex, 1445 SSL3_MT_SERVER_KEY_EXCHANGE)) 1446 goto err; 1447 1448 if (!CBB_init(&cbb_signed_params, 0)) 1449 goto err; 1450 1451 if (!CBB_add_bytes(&cbb_signed_params, s->s3->client_random, 1452 SSL3_RANDOM_SIZE)) { 1453 SSLerror(s, ERR_R_INTERNAL_ERROR); 1454 goto err; 1455 } 1456 if (!CBB_add_bytes(&cbb_signed_params, s->s3->server_random, 1457 SSL3_RANDOM_SIZE)) { 1458 SSLerror(s, ERR_R_INTERNAL_ERROR); 1459 goto err; 1460 } 1461 1462 type = s->s3->hs.cipher->algorithm_mkey; 1463 if (type & SSL_kDHE) { 1464 if (!ssl3_send_server_kex_dhe(s, &cbb_signed_params)) 1465 goto err; 1466 } else if (type & SSL_kECDHE) { 1467 if (!ssl3_send_server_kex_ecdhe(s, &cbb_signed_params)) 1468 goto err; 1469 } else { 1470 al = SSL_AD_HANDSHAKE_FAILURE; 1471 SSLerror(s, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1472 goto fatal_err; 1473 } 1474 1475 if (!CBB_finish(&cbb_signed_params, &signed_params, 1476 &signed_params_len)) 1477 goto err; 1478 1479 CBS_init(¶ms, signed_params, signed_params_len); 1480 if (!CBS_skip(¶ms, 2 * SSL3_RANDOM_SIZE)) 1481 goto err; 1482 1483 if (!CBB_add_bytes(&server_kex, CBS_data(¶ms), 1484 CBS_len(¶ms))) 1485 goto err; 1486 1487 /* Add signature unless anonymous. */ 1488 if (!(s->s3->hs.cipher->algorithm_auth & SSL_aNULL)) { 1489 if ((pkey = ssl_get_sign_pkey(s, s->s3->hs.cipher, 1490 &md, &sigalg)) == NULL) { 1491 al = SSL_AD_DECODE_ERROR; 1492 goto fatal_err; 1493 } 1494 s->s3->hs.our_sigalg = sigalg; 1495 1496 /* Send signature algorithm. */ 1497 if (SSL_USE_SIGALGS(s)) { 1498 if (!CBB_add_u16(&server_kex, sigalg->value)) { 1499 al = SSL_AD_INTERNAL_ERROR; 1500 SSLerror(s, ERR_R_INTERNAL_ERROR); 1501 goto fatal_err; 1502 } 1503 } 1504 1505 if (!EVP_DigestSignInit(md_ctx, &pctx, md, NULL, pkey)) { 1506 SSLerror(s, ERR_R_EVP_LIB); 1507 goto err; 1508 } 1509 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1510 (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1511 RSA_PKCS1_PSS_PADDING) || 1512 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1513 SSLerror(s, ERR_R_EVP_LIB); 1514 goto err; 1515 } 1516 if (!EVP_DigestSign(md_ctx, NULL, &signature_len, 1517 signed_params, signed_params_len)) { 1518 SSLerror(s, ERR_R_EVP_LIB); 1519 goto err; 1520 } 1521 if ((signature = calloc(1, signature_len)) == NULL) { 1522 SSLerror(s, ERR_R_MALLOC_FAILURE); 1523 goto err; 1524 } 1525 if (!EVP_DigestSign(md_ctx, signature, &signature_len, 1526 signed_params, signed_params_len)) { 1527 SSLerror(s, ERR_R_EVP_LIB); 1528 goto err; 1529 } 1530 1531 if (!CBB_add_u16_length_prefixed(&server_kex, 1532 &cbb_signature)) 1533 goto err; 1534 if (!CBB_add_bytes(&cbb_signature, signature, 1535 signature_len)) 1536 goto err; 1537 } 1538 1539 if (!ssl3_handshake_msg_finish(s, &cbb)) 1540 goto err; 1541 1542 s->s3->hs.state = SSL3_ST_SW_KEY_EXCH_B; 1543 } 1544 1545 EVP_MD_CTX_free(md_ctx); 1546 free(signature); 1547 free(signed_params); 1548 1549 return (ssl3_handshake_write(s)); 1550 1551 fatal_err: 1552 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1553 err: 1554 CBB_cleanup(&cbb_signed_params); 1555 CBB_cleanup(&cbb); 1556 EVP_MD_CTX_free(md_ctx); 1557 free(signature); 1558 free(signed_params); 1559 1560 return (-1); 1561 } 1562 1563 static int 1564 ssl3_send_certificate_request(SSL *s) 1565 { 1566 CBB cbb, cert_request, cert_types, sigalgs, cert_auth, dn; 1567 STACK_OF(X509_NAME) *sk = NULL; 1568 X509_NAME *name; 1569 int i; 1570 1571 /* 1572 * Certificate Request - RFC 5246 section 7.4.4. 1573 */ 1574 1575 memset(&cbb, 0, sizeof(cbb)); 1576 1577 if (s->s3->hs.state == SSL3_ST_SW_CERT_REQ_A) { 1578 if (!ssl3_handshake_msg_start(s, &cbb, &cert_request, 1579 SSL3_MT_CERTIFICATE_REQUEST)) 1580 goto err; 1581 1582 if (!CBB_add_u8_length_prefixed(&cert_request, &cert_types)) 1583 goto err; 1584 if (!ssl3_get_req_cert_types(s, &cert_types)) 1585 goto err; 1586 1587 if (SSL_USE_SIGALGS(s)) { 1588 if (!CBB_add_u16_length_prefixed(&cert_request, 1589 &sigalgs)) 1590 goto err; 1591 if (!ssl_sigalgs_build(s->s3->hs.negotiated_tls_version, 1592 &sigalgs, SSL_get_security_level(s))) 1593 goto err; 1594 } 1595 1596 if (!CBB_add_u16_length_prefixed(&cert_request, &cert_auth)) 1597 goto err; 1598 1599 sk = SSL_get_client_CA_list(s); 1600 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 1601 unsigned char *name_data; 1602 size_t name_len; 1603 1604 name = sk_X509_NAME_value(sk, i); 1605 name_len = i2d_X509_NAME(name, NULL); 1606 1607 if (!CBB_add_u16_length_prefixed(&cert_auth, &dn)) 1608 goto err; 1609 if (!CBB_add_space(&dn, &name_data, name_len)) 1610 goto err; 1611 if (i2d_X509_NAME(name, &name_data) != name_len) 1612 goto err; 1613 } 1614 1615 if (!ssl3_handshake_msg_finish(s, &cbb)) 1616 goto err; 1617 1618 s->s3->hs.state = SSL3_ST_SW_CERT_REQ_B; 1619 } 1620 1621 /* SSL3_ST_SW_CERT_REQ_B */ 1622 return (ssl3_handshake_write(s)); 1623 1624 err: 1625 CBB_cleanup(&cbb); 1626 1627 return (-1); 1628 } 1629 1630 static int 1631 ssl3_get_client_kex_rsa(SSL *s, CBS *cbs) 1632 { 1633 uint8_t fakepms[SSL_MAX_MASTER_KEY_LENGTH]; 1634 uint8_t *pms = NULL; 1635 size_t pms_len = 0; 1636 size_t pad_len; 1637 EVP_PKEY *pkey = NULL; 1638 RSA *rsa = NULL; 1639 CBS enc_pms; 1640 int decrypt_len; 1641 uint8_t mask; 1642 size_t i; 1643 int valid = 1; 1644 int ret = 0; 1645 1646 /* 1647 * Handle key exchange in the form of an RSA-Encrypted Premaster Secret 1648 * Message. See RFC 5246, section 7.4.7.1. 1649 */ 1650 1651 arc4random_buf(fakepms, sizeof(fakepms)); 1652 1653 fakepms[0] = s->s3->hs.peer_legacy_version >> 8; 1654 fakepms[1] = s->s3->hs.peer_legacy_version & 0xff; 1655 1656 pkey = s->cert->pkeys[SSL_PKEY_RSA].privatekey; 1657 if (pkey == NULL || (rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 1658 SSLerror(s, SSL_R_MISSING_RSA_CERTIFICATE); 1659 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1660 goto err; 1661 } 1662 1663 /* 1664 * The minimum size of an encrypted premaster secret is 11 bytes of 1665 * padding (00 02 <8 or more non-zero bytes> 00) (RFC 8017, section 1666 * 9.2) and 48 bytes of premaster secret (RFC 5246, section 7.4.7.1). 1667 * This means an RSA key size of at least 472 bits. 1668 */ 1669 pms_len = RSA_size(rsa); 1670 if (pms_len < 11 + SSL_MAX_MASTER_KEY_LENGTH) { 1671 SSLerror(s, SSL_R_DECRYPTION_FAILED); 1672 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); 1673 goto err; 1674 } 1675 pad_len = pms_len - SSL_MAX_MASTER_KEY_LENGTH; 1676 1677 if (!CBS_get_u16_length_prefixed(cbs, &enc_pms)) { 1678 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1679 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1680 goto err; 1681 } 1682 if (CBS_len(&enc_pms) != pms_len || CBS_len(cbs) != 0) { 1683 SSLerror(s, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 1684 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1685 goto err; 1686 } 1687 1688 if ((pms = calloc(1, pms_len)) == NULL) 1689 goto err; 1690 1691 decrypt_len = RSA_private_decrypt(CBS_len(&enc_pms), CBS_data(&enc_pms), 1692 pms, rsa, RSA_NO_PADDING); 1693 1694 if (decrypt_len != pms_len) { 1695 SSLerror(s, SSL_R_DECRYPTION_FAILED); 1696 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); 1697 goto err; 1698 } 1699 1700 /* 1701 * All processing from here on needs to avoid leaking any information 1702 * about the decrypted content, in order to prevent oracle attacks and 1703 * minimise timing attacks. 1704 */ 1705 1706 /* Check padding - 00 02 <8 or more non-zero bytes> 00 */ 1707 valid &= crypto_ct_eq_u8(pms[0], 0x00); 1708 valid &= crypto_ct_eq_u8(pms[1], 0x02); 1709 for (i = 2; i < pad_len - 1; i++) 1710 valid &= crypto_ct_ne_u8(pms[i], 0x00); 1711 valid &= crypto_ct_eq_u8(pms[pad_len - 1], 0x00); 1712 1713 /* Ensure client version in premaster secret matches ClientHello version. */ 1714 valid &= crypto_ct_eq_u8(pms[pad_len + 0], s->s3->hs.peer_legacy_version >> 8); 1715 valid &= crypto_ct_eq_u8(pms[pad_len + 1], s->s3->hs.peer_legacy_version & 0xff); 1716 1717 /* Use the premaster secret if padding is correct, if not use the fake. */ 1718 mask = crypto_ct_eq_mask_u8(valid, 1); 1719 for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) 1720 pms[i] = (pms[pad_len + i] & mask) | (fakepms[i] & ~mask); 1721 1722 if (!tls12_derive_master_secret(s, pms, SSL_MAX_MASTER_KEY_LENGTH)) 1723 goto err; 1724 1725 ret = 1; 1726 1727 err: 1728 freezero(pms, pms_len); 1729 1730 return ret; 1731 } 1732 1733 static int 1734 ssl3_get_client_kex_dhe(SSL *s, CBS *cbs) 1735 { 1736 uint8_t *key = NULL; 1737 size_t key_len = 0; 1738 int decode_error, invalid_key; 1739 int ret = 0; 1740 1741 if (s->s3->hs.key_share == NULL) { 1742 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1743 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1744 goto err; 1745 } 1746 1747 if (!tls_key_share_peer_public(s->s3->hs.key_share, cbs, 1748 &decode_error, &invalid_key)) { 1749 if (decode_error) { 1750 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1751 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1752 } 1753 goto err; 1754 } 1755 if (invalid_key) { 1756 SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH); 1757 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1758 goto err; 1759 } 1760 1761 if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1762 goto err; 1763 1764 if (!tls12_derive_master_secret(s, key, key_len)) 1765 goto err; 1766 1767 ret = 1; 1768 1769 err: 1770 freezero(key, key_len); 1771 1772 return ret; 1773 } 1774 1775 static int 1776 ssl3_get_client_kex_ecdhe(SSL *s, CBS *cbs) 1777 { 1778 uint8_t *key = NULL; 1779 size_t key_len = 0; 1780 int decode_error; 1781 CBS public; 1782 int ret = 0; 1783 1784 if (s->s3->hs.key_share == NULL) { 1785 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1786 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1787 goto err; 1788 } 1789 1790 if (!CBS_get_u8_length_prefixed(cbs, &public)) { 1791 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1792 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1793 goto err; 1794 } 1795 if (!tls_key_share_peer_public(s->s3->hs.key_share, &public, 1796 &decode_error, NULL)) { 1797 if (decode_error) { 1798 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1799 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1800 } 1801 goto err; 1802 } 1803 1804 if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1805 goto err; 1806 1807 if (!tls12_derive_master_secret(s, key, key_len)) 1808 goto err; 1809 1810 ret = 1; 1811 1812 err: 1813 freezero(key, key_len); 1814 1815 return ret; 1816 } 1817 1818 static int 1819 ssl3_get_client_key_exchange(SSL *s) 1820 { 1821 unsigned long alg_k; 1822 int al, ret; 1823 CBS cbs; 1824 1825 /* 2048 maxlen is a guess. How long a key does that permit? */ 1826 if ((ret = ssl3_get_message(s, SSL3_ST_SR_KEY_EXCH_A, 1827 SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048)) <= 0) 1828 return ret; 1829 1830 if (s->init_num < 0) 1831 goto err; 1832 1833 CBS_init(&cbs, s->init_msg, s->init_num); 1834 1835 alg_k = s->s3->hs.cipher->algorithm_mkey; 1836 1837 if (alg_k & SSL_kRSA) { 1838 if (!ssl3_get_client_kex_rsa(s, &cbs)) 1839 goto err; 1840 } else if (alg_k & SSL_kDHE) { 1841 if (!ssl3_get_client_kex_dhe(s, &cbs)) 1842 goto err; 1843 } else if (alg_k & SSL_kECDHE) { 1844 if (!ssl3_get_client_kex_ecdhe(s, &cbs)) 1845 goto err; 1846 } else { 1847 al = SSL_AD_HANDSHAKE_FAILURE; 1848 SSLerror(s, SSL_R_UNKNOWN_CIPHER_TYPE); 1849 goto fatal_err; 1850 } 1851 1852 if (CBS_len(&cbs) != 0) { 1853 al = SSL_AD_DECODE_ERROR; 1854 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1855 goto fatal_err; 1856 } 1857 1858 return (1); 1859 1860 fatal_err: 1861 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1862 err: 1863 return (-1); 1864 } 1865 1866 static int 1867 ssl3_get_cert_verify(SSL *s) 1868 { 1869 CBS cbs, signature; 1870 const struct ssl_sigalg *sigalg = NULL; 1871 uint16_t sigalg_value = SIGALG_NONE; 1872 EVP_PKEY *pkey; 1873 X509 *peer_cert = NULL; 1874 EVP_MD_CTX *mctx = NULL; 1875 int al, verify; 1876 const unsigned char *hdata; 1877 size_t hdatalen; 1878 int type = 0; 1879 int ret; 1880 1881 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_VRFY_A, 1882 SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 1883 return ret; 1884 1885 ret = 0; 1886 1887 if (s->init_num < 0) 1888 goto err; 1889 1890 if ((mctx = EVP_MD_CTX_new()) == NULL) 1891 goto err; 1892 1893 CBS_init(&cbs, s->init_msg, s->init_num); 1894 1895 peer_cert = s->session->peer_cert; 1896 pkey = X509_get0_pubkey(peer_cert); 1897 type = X509_certificate_type(peer_cert, pkey); 1898 1899 if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_VERIFY) { 1900 s->s3->hs.tls12.reuse_message = 1; 1901 if (peer_cert != NULL) { 1902 al = SSL_AD_UNEXPECTED_MESSAGE; 1903 SSLerror(s, SSL_R_MISSING_VERIFY_MESSAGE); 1904 goto fatal_err; 1905 } 1906 ret = 1; 1907 goto end; 1908 } 1909 1910 if (peer_cert == NULL) { 1911 SSLerror(s, SSL_R_NO_CLIENT_CERT_RECEIVED); 1912 al = SSL_AD_UNEXPECTED_MESSAGE; 1913 goto fatal_err; 1914 } 1915 1916 if (!(type & EVP_PKT_SIGN)) { 1917 SSLerror(s, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 1918 al = SSL_AD_ILLEGAL_PARAMETER; 1919 goto fatal_err; 1920 } 1921 1922 if (s->s3->change_cipher_spec) { 1923 SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); 1924 al = SSL_AD_UNEXPECTED_MESSAGE; 1925 goto fatal_err; 1926 } 1927 1928 if (SSL_USE_SIGALGS(s)) { 1929 if (!CBS_get_u16(&cbs, &sigalg_value)) 1930 goto decode_err; 1931 } 1932 if (!CBS_get_u16_length_prefixed(&cbs, &signature)) 1933 goto err; 1934 if (CBS_len(&cbs) != 0) { 1935 al = SSL_AD_DECODE_ERROR; 1936 SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE); 1937 goto fatal_err; 1938 } 1939 1940 if (CBS_len(&signature) > EVP_PKEY_size(pkey)) { 1941 SSLerror(s, SSL_R_WRONG_SIGNATURE_SIZE); 1942 al = SSL_AD_DECODE_ERROR; 1943 goto fatal_err; 1944 } 1945 1946 if ((sigalg = ssl_sigalg_for_peer(s, pkey, 1947 sigalg_value)) == NULL) { 1948 al = SSL_AD_DECODE_ERROR; 1949 goto fatal_err; 1950 } 1951 s->s3->hs.peer_sigalg = sigalg; 1952 1953 if (SSL_USE_SIGALGS(s)) { 1954 EVP_PKEY_CTX *pctx; 1955 1956 if (!tls1_transcript_data(s, &hdata, &hdatalen)) { 1957 SSLerror(s, ERR_R_INTERNAL_ERROR); 1958 al = SSL_AD_INTERNAL_ERROR; 1959 goto fatal_err; 1960 } 1961 if (!EVP_DigestVerifyInit(mctx, &pctx, sigalg->md(), 1962 NULL, pkey)) { 1963 SSLerror(s, ERR_R_EVP_LIB); 1964 al = SSL_AD_INTERNAL_ERROR; 1965 goto fatal_err; 1966 } 1967 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1968 (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1969 RSA_PKCS1_PSS_PADDING) || 1970 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1971 al = SSL_AD_INTERNAL_ERROR; 1972 goto fatal_err; 1973 } 1974 if (EVP_DigestVerify(mctx, CBS_data(&signature), 1975 CBS_len(&signature), hdata, hdatalen) <= 0) { 1976 SSLerror(s, ERR_R_EVP_LIB); 1977 al = SSL_AD_INTERNAL_ERROR; 1978 goto fatal_err; 1979 } 1980 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { 1981 RSA *rsa; 1982 1983 if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 1984 al = SSL_AD_INTERNAL_ERROR; 1985 SSLerror(s, ERR_R_EVP_LIB); 1986 goto fatal_err; 1987 } 1988 verify = RSA_verify(NID_md5_sha1, s->s3->hs.tls12.cert_verify, 1989 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, CBS_data(&signature), 1990 CBS_len(&signature), rsa); 1991 if (verify < 0) { 1992 al = SSL_AD_DECRYPT_ERROR; 1993 SSLerror(s, SSL_R_BAD_RSA_DECRYPT); 1994 goto fatal_err; 1995 } 1996 if (verify == 0) { 1997 al = SSL_AD_DECRYPT_ERROR; 1998 SSLerror(s, SSL_R_BAD_RSA_SIGNATURE); 1999 goto fatal_err; 2000 } 2001 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { 2002 EC_KEY *eckey; 2003 2004 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) { 2005 al = SSL_AD_INTERNAL_ERROR; 2006 SSLerror(s, ERR_R_EVP_LIB); 2007 goto fatal_err; 2008 } 2009 verify = ECDSA_verify(0, 2010 &(s->s3->hs.tls12.cert_verify[MD5_DIGEST_LENGTH]), 2011 SHA_DIGEST_LENGTH, CBS_data(&signature), 2012 CBS_len(&signature), eckey); 2013 if (verify <= 0) { 2014 al = SSL_AD_DECRYPT_ERROR; 2015 SSLerror(s, SSL_R_BAD_ECDSA_SIGNATURE); 2016 goto fatal_err; 2017 } 2018 } else { 2019 SSLerror(s, ERR_R_INTERNAL_ERROR); 2020 al = SSL_AD_UNSUPPORTED_CERTIFICATE; 2021 goto fatal_err; 2022 } 2023 2024 ret = 1; 2025 if (0) { 2026 decode_err: 2027 al = SSL_AD_DECODE_ERROR; 2028 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2029 fatal_err: 2030 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2031 } 2032 end: 2033 tls1_transcript_free(s); 2034 err: 2035 EVP_MD_CTX_free(mctx); 2036 2037 return (ret); 2038 } 2039 2040 static int 2041 ssl3_get_client_certificate(SSL *s) 2042 { 2043 CBS cbs, cert_list, cert_data; 2044 STACK_OF(X509) *certs = NULL; 2045 X509 *cert = NULL; 2046 const uint8_t *p; 2047 int al, ret; 2048 2049 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, 2050 -1, s->max_cert_list)) <= 0) 2051 return ret; 2052 2053 ret = -1; 2054 2055 if (s->s3->hs.tls12.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { 2056 if ((s->verify_mode & SSL_VERIFY_PEER) && 2057 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2058 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 2059 al = SSL_AD_HANDSHAKE_FAILURE; 2060 goto fatal_err; 2061 } 2062 2063 /* 2064 * If we asked for a client certificate and the client has none, 2065 * it must respond with a certificate list of length zero. 2066 */ 2067 if (s->s3->hs.tls12.cert_request != 0) { 2068 SSLerror(s, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 2069 al = SSL_AD_UNEXPECTED_MESSAGE; 2070 goto fatal_err; 2071 } 2072 s->s3->hs.tls12.reuse_message = 1; 2073 return (1); 2074 } 2075 2076 if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE) { 2077 al = SSL_AD_UNEXPECTED_MESSAGE; 2078 SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE); 2079 goto fatal_err; 2080 } 2081 2082 if (s->init_num < 0) 2083 goto decode_err; 2084 2085 CBS_init(&cbs, s->init_msg, s->init_num); 2086 2087 if (!CBS_get_u24_length_prefixed(&cbs, &cert_list)) 2088 goto decode_err; 2089 if (CBS_len(&cbs) != 0) 2090 goto decode_err; 2091 2092 /* 2093 * A TLS client must send an empty certificate list, if no suitable 2094 * certificate is available (rather than omitting the Certificate 2095 * handshake message) - see RFC 5246 section 7.4.6. 2096 */ 2097 if (CBS_len(&cert_list) == 0) { 2098 if ((s->verify_mode & SSL_VERIFY_PEER) && 2099 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2100 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 2101 al = SSL_AD_HANDSHAKE_FAILURE; 2102 goto fatal_err; 2103 } 2104 /* No client certificate so free transcript. */ 2105 tls1_transcript_free(s); 2106 goto done; 2107 } 2108 2109 if ((certs = sk_X509_new_null()) == NULL) { 2110 SSLerror(s, ERR_R_MALLOC_FAILURE); 2111 goto err; 2112 } 2113 2114 while (CBS_len(&cert_list) > 0) { 2115 if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data)) 2116 goto decode_err; 2117 p = CBS_data(&cert_data); 2118 if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL) { 2119 SSLerror(s, ERR_R_ASN1_LIB); 2120 goto err; 2121 } 2122 if (p != CBS_data(&cert_data) + CBS_len(&cert_data)) 2123 goto decode_err; 2124 if (!sk_X509_push(certs, cert)) { 2125 SSLerror(s, ERR_R_MALLOC_FAILURE); 2126 goto err; 2127 } 2128 cert = NULL; 2129 } 2130 2131 if (ssl_verify_cert_chain(s, certs) <= 0) { 2132 al = ssl_verify_alarm_type(s->verify_result); 2133 SSLerror(s, SSL_R_NO_CERTIFICATE_RETURNED); 2134 goto fatal_err; 2135 } 2136 s->session->verify_result = s->verify_result; 2137 ERR_clear_error(); 2138 2139 if (!tls_process_peer_certs(s, certs)) 2140 goto err; 2141 2142 done: 2143 ret = 1; 2144 if (0) { 2145 decode_err: 2146 al = SSL_AD_DECODE_ERROR; 2147 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2148 fatal_err: 2149 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2150 } 2151 err: 2152 sk_X509_pop_free(certs, X509_free); 2153 X509_free(cert); 2154 2155 return (ret); 2156 } 2157 2158 static int 2159 ssl3_send_server_certificate(SSL *s) 2160 { 2161 CBB cbb, server_cert; 2162 SSL_CERT_PKEY *cpk; 2163 2164 /* 2165 * Server Certificate - RFC 5246, section 7.4.2. 2166 */ 2167 2168 memset(&cbb, 0, sizeof(cbb)); 2169 2170 if (s->s3->hs.state == SSL3_ST_SW_CERT_A) { 2171 if ((cpk = ssl_get_server_send_pkey(s)) == NULL) { 2172 SSLerror(s, ERR_R_INTERNAL_ERROR); 2173 return (0); 2174 } 2175 2176 if (!ssl3_handshake_msg_start(s, &cbb, &server_cert, 2177 SSL3_MT_CERTIFICATE)) 2178 goto err; 2179 if (!ssl3_output_cert_chain(s, &server_cert, cpk)) 2180 goto err; 2181 if (!ssl3_handshake_msg_finish(s, &cbb)) 2182 goto err; 2183 2184 s->s3->hs.state = SSL3_ST_SW_CERT_B; 2185 } 2186 2187 /* SSL3_ST_SW_CERT_B */ 2188 return (ssl3_handshake_write(s)); 2189 2190 err: 2191 CBB_cleanup(&cbb); 2192 2193 return (0); 2194 } 2195 2196 /* send a new session ticket (not necessarily for a new session) */ 2197 static int 2198 ssl3_send_newsession_ticket(SSL *s) 2199 { 2200 CBB cbb, session_ticket, ticket; 2201 SSL_CTX *tctx = s->initial_ctx; 2202 size_t enc_session_len, enc_session_max_len, hmac_len; 2203 size_t session_len = 0; 2204 unsigned char *enc_session = NULL, *session = NULL; 2205 unsigned char iv[EVP_MAX_IV_LENGTH]; 2206 unsigned char key_name[16]; 2207 unsigned char *hmac; 2208 unsigned int hlen; 2209 EVP_CIPHER_CTX *ctx = NULL; 2210 HMAC_CTX *hctx = NULL; 2211 int iv_len, len; 2212 2213 /* 2214 * New Session Ticket - RFC 5077, section 3.3. 2215 */ 2216 2217 memset(&cbb, 0, sizeof(cbb)); 2218 2219 if ((ctx = EVP_CIPHER_CTX_new()) == NULL) 2220 goto err; 2221 if ((hctx = HMAC_CTX_new()) == NULL) 2222 goto err; 2223 2224 if (s->s3->hs.state == SSL3_ST_SW_SESSION_TICKET_A) { 2225 if (!ssl3_handshake_msg_start(s, &cbb, &session_ticket, 2226 SSL3_MT_NEWSESSION_TICKET)) 2227 goto err; 2228 2229 if (!SSL_SESSION_ticket(s->session, &session, &session_len)) 2230 goto err; 2231 if (session_len > 0xffff) 2232 goto err; 2233 2234 /* 2235 * Initialize HMAC and cipher contexts. If callback is present 2236 * it does all the work, otherwise use generated values from 2237 * parent context. 2238 */ 2239 if (tctx->tlsext_ticket_key_cb != NULL) { 2240 if (tctx->tlsext_ticket_key_cb(s, 2241 key_name, iv, ctx, hctx, 1) < 0) 2242 goto err; 2243 } else { 2244 arc4random_buf(iv, 16); 2245 EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, 2246 tctx->tlsext_tick_aes_key, iv); 2247 HMAC_Init_ex(hctx, tctx->tlsext_tick_hmac_key, 2248 16, EVP_sha256(), NULL); 2249 memcpy(key_name, tctx->tlsext_tick_key_name, 16); 2250 } 2251 2252 /* Encrypt the session state. */ 2253 enc_session_max_len = session_len + EVP_MAX_BLOCK_LENGTH; 2254 if ((enc_session = calloc(1, enc_session_max_len)) == NULL) 2255 goto err; 2256 enc_session_len = 0; 2257 if (!EVP_EncryptUpdate(ctx, enc_session, &len, session, 2258 session_len)) 2259 goto err; 2260 enc_session_len += len; 2261 if (!EVP_EncryptFinal_ex(ctx, enc_session + enc_session_len, 2262 &len)) 2263 goto err; 2264 enc_session_len += len; 2265 2266 if (enc_session_len > enc_session_max_len) 2267 goto err; 2268 2269 /* Generate the HMAC. */ 2270 if (!HMAC_Update(hctx, key_name, sizeof(key_name))) 2271 goto err; 2272 if (!HMAC_Update(hctx, iv, EVP_CIPHER_CTX_iv_length(ctx))) 2273 goto err; 2274 if (!HMAC_Update(hctx, enc_session, enc_session_len)) 2275 goto err; 2276 2277 if ((hmac_len = HMAC_size(hctx)) <= 0) 2278 goto err; 2279 2280 /* 2281 * Ticket lifetime hint (advisory only): 2282 * We leave this unspecified for resumed session 2283 * (for simplicity), and guess that tickets for new 2284 * sessions will live as long as their sessions. 2285 */ 2286 if (!CBB_add_u32(&session_ticket, 2287 s->hit ? 0 : s->session->timeout)) 2288 goto err; 2289 2290 if (!CBB_add_u16_length_prefixed(&session_ticket, &ticket)) 2291 goto err; 2292 if (!CBB_add_bytes(&ticket, key_name, sizeof(key_name))) 2293 goto err; 2294 if ((iv_len = EVP_CIPHER_CTX_iv_length(ctx)) < 0) 2295 goto err; 2296 if (!CBB_add_bytes(&ticket, iv, iv_len)) 2297 goto err; 2298 if (!CBB_add_bytes(&ticket, enc_session, enc_session_len)) 2299 goto err; 2300 if (!CBB_add_space(&ticket, &hmac, hmac_len)) 2301 goto err; 2302 2303 if (!HMAC_Final(hctx, hmac, &hlen)) 2304 goto err; 2305 if (hlen != hmac_len) 2306 goto err; 2307 2308 if (!ssl3_handshake_msg_finish(s, &cbb)) 2309 goto err; 2310 2311 s->s3->hs.state = SSL3_ST_SW_SESSION_TICKET_B; 2312 } 2313 2314 EVP_CIPHER_CTX_free(ctx); 2315 HMAC_CTX_free(hctx); 2316 freezero(session, session_len); 2317 free(enc_session); 2318 2319 /* SSL3_ST_SW_SESSION_TICKET_B */ 2320 return (ssl3_handshake_write(s)); 2321 2322 err: 2323 CBB_cleanup(&cbb); 2324 EVP_CIPHER_CTX_free(ctx); 2325 HMAC_CTX_free(hctx); 2326 freezero(session, session_len); 2327 free(enc_session); 2328 2329 return (-1); 2330 } 2331 2332 static int 2333 ssl3_send_cert_status(SSL *s) 2334 { 2335 CBB cbb, certstatus, ocspresp; 2336 2337 memset(&cbb, 0, sizeof(cbb)); 2338 2339 if (s->s3->hs.state == SSL3_ST_SW_CERT_STATUS_A) { 2340 if (!ssl3_handshake_msg_start(s, &cbb, &certstatus, 2341 SSL3_MT_CERTIFICATE_STATUS)) 2342 goto err; 2343 if (!CBB_add_u8(&certstatus, s->tlsext_status_type)) 2344 goto err; 2345 if (!CBB_add_u24_length_prefixed(&certstatus, &ocspresp)) 2346 goto err; 2347 if (!CBB_add_bytes(&ocspresp, s->tlsext_ocsp_resp, 2348 s->tlsext_ocsp_resp_len)) 2349 goto err; 2350 if (!ssl3_handshake_msg_finish(s, &cbb)) 2351 goto err; 2352 2353 s->s3->hs.state = SSL3_ST_SW_CERT_STATUS_B; 2354 } 2355 2356 /* SSL3_ST_SW_CERT_STATUS_B */ 2357 return (ssl3_handshake_write(s)); 2358 2359 err: 2360 CBB_cleanup(&cbb); 2361 2362 return (-1); 2363 } 2364 2365 static int 2366 ssl3_send_server_change_cipher_spec(SSL *s) 2367 { 2368 size_t outlen; 2369 CBB cbb; 2370 2371 memset(&cbb, 0, sizeof(cbb)); 2372 2373 if (s->s3->hs.state == SSL3_ST_SW_CHANGE_A) { 2374 if (!CBB_init_fixed(&cbb, s->init_buf->data, 2375 s->init_buf->length)) 2376 goto err; 2377 if (!CBB_add_u8(&cbb, SSL3_MT_CCS)) 2378 goto err; 2379 if (!CBB_finish(&cbb, NULL, &outlen)) 2380 goto err; 2381 2382 if (outlen > INT_MAX) 2383 goto err; 2384 2385 s->init_num = (int)outlen; 2386 s->init_off = 0; 2387 2388 if (SSL_is_dtls(s)) { 2389 s->d1->handshake_write_seq = 2390 s->d1->next_handshake_write_seq; 2391 dtls1_set_message_header_int(s, SSL3_MT_CCS, 0, 2392 s->d1->handshake_write_seq, 0, 0); 2393 dtls1_buffer_message(s, 1); 2394 } 2395 2396 s->s3->hs.state = SSL3_ST_SW_CHANGE_B; 2397 } 2398 2399 /* SSL3_ST_SW_CHANGE_B */ 2400 return ssl3_record_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); 2401 2402 err: 2403 CBB_cleanup(&cbb); 2404 2405 return -1; 2406 } 2407 2408 static int 2409 ssl3_get_client_finished(SSL *s) 2410 { 2411 int al, md_len, ret; 2412 CBS cbs; 2413 2414 /* should actually be 36+4 :-) */ 2415 if ((ret = ssl3_get_message(s, SSL3_ST_SR_FINISHED_A, 2416 SSL3_ST_SR_FINISHED_B, SSL3_MT_FINISHED, 64)) <= 0) 2417 return ret; 2418 2419 /* If this occurs, we have missed a message */ 2420 if (!s->s3->change_cipher_spec) { 2421 al = SSL_AD_UNEXPECTED_MESSAGE; 2422 SSLerror(s, SSL_R_GOT_A_FIN_BEFORE_A_CCS); 2423 goto fatal_err; 2424 } 2425 s->s3->change_cipher_spec = 0; 2426 2427 md_len = TLS1_FINISH_MAC_LENGTH; 2428 2429 if (s->init_num < 0) { 2430 al = SSL_AD_DECODE_ERROR; 2431 SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2432 goto fatal_err; 2433 } 2434 2435 CBS_init(&cbs, s->init_msg, s->init_num); 2436 2437 if (s->s3->hs.peer_finished_len != md_len || 2438 CBS_len(&cbs) != md_len) { 2439 al = SSL_AD_DECODE_ERROR; 2440 SSLerror(s, SSL_R_BAD_DIGEST_LENGTH); 2441 goto fatal_err; 2442 } 2443 2444 if (!CBS_mem_equal(&cbs, s->s3->hs.peer_finished, CBS_len(&cbs))) { 2445 al = SSL_AD_DECRYPT_ERROR; 2446 SSLerror(s, SSL_R_DIGEST_CHECK_FAILED); 2447 goto fatal_err; 2448 } 2449 2450 /* Copy finished so we can use it for renegotiation checks. */ 2451 OPENSSL_assert(md_len <= EVP_MAX_MD_SIZE); 2452 memcpy(s->s3->previous_client_finished, 2453 s->s3->hs.peer_finished, md_len); 2454 s->s3->previous_client_finished_len = md_len; 2455 2456 return (1); 2457 fatal_err: 2458 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2459 return (0); 2460 } 2461 2462 static int 2463 ssl3_send_server_finished(SSL *s) 2464 { 2465 CBB cbb, finished; 2466 2467 memset(&cbb, 0, sizeof(cbb)); 2468 2469 if (s->s3->hs.state == SSL3_ST_SW_FINISHED_A) { 2470 if (!tls12_derive_finished(s)) 2471 goto err; 2472 2473 /* Copy finished so we can use it for renegotiation checks. */ 2474 memcpy(s->s3->previous_server_finished, 2475 s->s3->hs.finished, s->s3->hs.finished_len); 2476 s->s3->previous_server_finished_len = s->s3->hs.finished_len; 2477 2478 if (!ssl3_handshake_msg_start(s, &cbb, &finished, 2479 SSL3_MT_FINISHED)) 2480 goto err; 2481 if (!CBB_add_bytes(&finished, s->s3->hs.finished, 2482 s->s3->hs.finished_len)) 2483 goto err; 2484 if (!ssl3_handshake_msg_finish(s, &cbb)) 2485 goto err; 2486 2487 s->s3->hs.state = SSL3_ST_SW_FINISHED_B; 2488 } 2489 2490 return (ssl3_handshake_write(s)); 2491 2492 err: 2493 CBB_cleanup(&cbb); 2494 2495 return (-1); 2496 } 2497