1 /* $OpenBSD: ssl_srvr.c,v 1.120 2021/10/23 08:34:36 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 <stdio.h> 152 153 #include <openssl/bn.h> 154 #include <openssl/buffer.h> 155 #include <openssl/curve25519.h> 156 #include <openssl/evp.h> 157 #include <openssl/dh.h> 158 #include <openssl/hmac.h> 159 #include <openssl/md5.h> 160 #include <openssl/objects.h> 161 #include <openssl/opensslconf.h> 162 #include <openssl/x509.h> 163 164 #ifndef OPENSSL_NO_GOST 165 #include <openssl/gost.h> 166 #endif 167 168 #include "bytestring.h" 169 #include "dtls_locl.h" 170 #include "ssl_locl.h" 171 #include "ssl_sigalgs.h" 172 #include "ssl_tlsext.h" 173 174 int 175 ssl3_accept(SSL *s) 176 { 177 unsigned long alg_k; 178 int new_state, state, skip = 0; 179 int listen = 0; 180 int ret = -1; 181 182 ERR_clear_error(); 183 errno = 0; 184 185 if (SSL_is_dtls(s)) 186 listen = D1I(s)->listen; 187 188 /* init things to blank */ 189 s->internal->in_handshake++; 190 if (!SSL_in_init(s) || SSL_in_before(s)) 191 SSL_clear(s); 192 193 if (SSL_is_dtls(s)) 194 D1I(s)->listen = listen; 195 196 for (;;) { 197 state = S3I(s)->hs.state; 198 199 switch (S3I(s)->hs.state) { 200 case SSL_ST_RENEGOTIATE: 201 s->internal->renegotiate = 1; 202 /* S3I(s)->hs.state=SSL_ST_ACCEPT; */ 203 204 case SSL_ST_BEFORE: 205 case SSL_ST_ACCEPT: 206 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 207 case SSL_ST_OK|SSL_ST_ACCEPT: 208 s->server = 1; 209 210 ssl_info_callback(s, SSL_CB_HANDSHAKE_START, 1); 211 212 if (!ssl_legacy_stack_version(s, s->version)) { 213 SSLerror(s, ERR_R_INTERNAL_ERROR); 214 ret = -1; 215 goto end; 216 } 217 218 if (!ssl_supported_tls_version_range(s, 219 &S3I(s)->hs.our_min_tls_version, 220 &S3I(s)->hs.our_max_tls_version)) { 221 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 222 ret = -1; 223 goto end; 224 } 225 226 if (!ssl3_setup_init_buffer(s)) { 227 ret = -1; 228 goto end; 229 } 230 if (!ssl3_setup_buffers(s)) { 231 ret = -1; 232 goto end; 233 } 234 235 s->internal->init_num = 0; 236 237 if (S3I(s)->hs.state != SSL_ST_RENEGOTIATE) { 238 /* 239 * Ok, we now need to push on a buffering BIO 240 * so that the output is sent in a way that 241 * TCP likes :-) 242 */ 243 if (!ssl_init_wbio_buffer(s, 1)) { 244 ret = -1; 245 goto end; 246 } 247 248 if (!tls1_transcript_init(s)) { 249 ret = -1; 250 goto end; 251 } 252 253 S3I(s)->hs.state = SSL3_ST_SR_CLNT_HELLO_A; 254 s->ctx->internal->stats.sess_accept++; 255 } else if (!SSL_is_dtls(s) && !S3I(s)->send_connection_binding) { 256 /* 257 * Server attempting to renegotiate with 258 * client that doesn't support secure 259 * renegotiation. 260 */ 261 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 262 ssl3_send_alert(s, SSL3_AL_FATAL, 263 SSL_AD_HANDSHAKE_FAILURE); 264 ret = -1; 265 goto end; 266 } else { 267 /* 268 * S3I(s)->hs.state == SSL_ST_RENEGOTIATE, 269 * we will just send a HelloRequest. 270 */ 271 s->ctx->internal->stats.sess_accept_renegotiate++; 272 S3I(s)->hs.state = SSL3_ST_SW_HELLO_REQ_A; 273 } 274 break; 275 276 case SSL3_ST_SW_HELLO_REQ_A: 277 case SSL3_ST_SW_HELLO_REQ_B: 278 s->internal->shutdown = 0; 279 if (SSL_is_dtls(s)) { 280 dtls1_clear_record_buffer(s); 281 dtls1_start_timer(s); 282 } 283 ret = ssl3_send_hello_request(s); 284 if (ret <= 0) 285 goto end; 286 if (SSL_is_dtls(s)) 287 S3I(s)->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 288 else 289 S3I(s)->hs.tls12.next_state = SSL3_ST_SW_HELLO_REQ_C; 290 S3I(s)->hs.state = SSL3_ST_SW_FLUSH; 291 s->internal->init_num = 0; 292 293 if (SSL_is_dtls(s)) { 294 if (!tls1_transcript_init(s)) { 295 ret = -1; 296 goto end; 297 } 298 } 299 break; 300 301 case SSL3_ST_SW_HELLO_REQ_C: 302 S3I(s)->hs.state = SSL_ST_OK; 303 break; 304 305 case SSL3_ST_SR_CLNT_HELLO_A: 306 case SSL3_ST_SR_CLNT_HELLO_B: 307 case SSL3_ST_SR_CLNT_HELLO_C: 308 s->internal->shutdown = 0; 309 if (SSL_is_dtls(s)) { 310 ret = ssl3_get_client_hello(s); 311 if (ret <= 0) 312 goto end; 313 dtls1_stop_timer(s); 314 315 if (ret == 1 && 316 (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) 317 S3I(s)->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; 318 else 319 S3I(s)->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 320 321 s->internal->init_num = 0; 322 323 /* 324 * Reflect ClientHello sequence to remain 325 * stateless while listening. 326 */ 327 if (listen) { 328 tls12_record_layer_reflect_seq_num( 329 s->internal->rl); 330 } 331 332 /* If we're just listening, stop here */ 333 if (listen && S3I(s)->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 334 ret = 2; 335 D1I(s)->listen = 0; 336 /* 337 * Set expected sequence numbers to 338 * continue the handshake. 339 */ 340 D1I(s)->handshake_read_seq = 2; 341 D1I(s)->handshake_write_seq = 1; 342 D1I(s)->next_handshake_write_seq = 1; 343 goto end; 344 } 345 } else { 346 if (s->internal->rwstate != SSL_X509_LOOKUP) { 347 ret = ssl3_get_client_hello(s); 348 if (ret <= 0) 349 goto end; 350 } 351 352 s->internal->renegotiate = 2; 353 S3I(s)->hs.state = SSL3_ST_SW_SRVR_HELLO_A; 354 s->internal->init_num = 0; 355 } 356 break; 357 358 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: 359 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: 360 ret = ssl3_send_dtls_hello_verify_request(s); 361 if (ret <= 0) 362 goto end; 363 S3I(s)->hs.state = SSL3_ST_SW_FLUSH; 364 S3I(s)->hs.tls12.next_state = SSL3_ST_SR_CLNT_HELLO_A; 365 366 /* HelloVerifyRequest resets Finished MAC. */ 367 tls1_transcript_reset(s); 368 break; 369 370 case SSL3_ST_SW_SRVR_HELLO_A: 371 case SSL3_ST_SW_SRVR_HELLO_B: 372 if (SSL_is_dtls(s)) { 373 s->internal->renegotiate = 2; 374 dtls1_start_timer(s); 375 } 376 ret = ssl3_send_server_hello(s); 377 if (ret <= 0) 378 goto end; 379 if (s->internal->hit) { 380 if (s->internal->tlsext_ticket_expected) 381 S3I(s)->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 382 else 383 S3I(s)->hs.state = SSL3_ST_SW_CHANGE_A; 384 } else { 385 S3I(s)->hs.state = SSL3_ST_SW_CERT_A; 386 } 387 s->internal->init_num = 0; 388 break; 389 390 case SSL3_ST_SW_CERT_A: 391 case SSL3_ST_SW_CERT_B: 392 /* Check if it is anon DH or anon ECDH. */ 393 if (!(S3I(s)->hs.cipher->algorithm_auth & 394 SSL_aNULL)) { 395 if (SSL_is_dtls(s)) 396 dtls1_start_timer(s); 397 ret = ssl3_send_server_certificate(s); 398 if (ret <= 0) 399 goto end; 400 if (s->internal->tlsext_status_expected) 401 S3I(s)->hs.state = SSL3_ST_SW_CERT_STATUS_A; 402 else 403 S3I(s)->hs.state = SSL3_ST_SW_KEY_EXCH_A; 404 } else { 405 skip = 1; 406 S3I(s)->hs.state = SSL3_ST_SW_KEY_EXCH_A; 407 } 408 s->internal->init_num = 0; 409 break; 410 411 case SSL3_ST_SW_KEY_EXCH_A: 412 case SSL3_ST_SW_KEY_EXCH_B: 413 alg_k = S3I(s)->hs.cipher->algorithm_mkey; 414 415 /* 416 * Only send if using a DH key exchange. 417 * 418 * For ECC ciphersuites, we send a ServerKeyExchange 419 * message only if the cipher suite is ECDHE. In other 420 * cases, the server certificate contains the server's 421 * public key for key exchange. 422 */ 423 if (alg_k & (SSL_kDHE|SSL_kECDHE)) { 424 if (SSL_is_dtls(s)) 425 dtls1_start_timer(s); 426 ret = ssl3_send_server_key_exchange(s); 427 if (ret <= 0) 428 goto end; 429 } else 430 skip = 1; 431 432 S3I(s)->hs.state = SSL3_ST_SW_CERT_REQ_A; 433 s->internal->init_num = 0; 434 break; 435 436 case SSL3_ST_SW_CERT_REQ_A: 437 case SSL3_ST_SW_CERT_REQ_B: 438 /* 439 * Determine whether or not we need to request a 440 * certificate. 441 * 442 * Do not request a certificate if: 443 * 444 * - We did not ask for it (SSL_VERIFY_PEER is unset). 445 * 446 * - SSL_VERIFY_CLIENT_ONCE is set and we are 447 * renegotiating. 448 * 449 * - We are using an anonymous ciphersuites 450 * (see section "Certificate request" in SSL 3 drafts 451 * and in RFC 2246) ... except when the application 452 * insists on verification (against the specs, but 453 * s3_clnt.c accepts this for SSL 3). 454 */ 455 if (!(s->verify_mode & SSL_VERIFY_PEER) || 456 ((s->session->peer != NULL) && 457 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 458 ((S3I(s)->hs.cipher->algorithm_auth & 459 SSL_aNULL) && !(s->verify_mode & 460 SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) { 461 /* No cert request. */ 462 skip = 1; 463 S3I(s)->hs.tls12.cert_request = 0; 464 S3I(s)->hs.state = SSL3_ST_SW_SRVR_DONE_A; 465 466 if (!SSL_is_dtls(s)) 467 tls1_transcript_free(s); 468 } else { 469 S3I(s)->hs.tls12.cert_request = 1; 470 if (SSL_is_dtls(s)) 471 dtls1_start_timer(s); 472 ret = ssl3_send_certificate_request(s); 473 if (ret <= 0) 474 goto end; 475 S3I(s)->hs.state = SSL3_ST_SW_SRVR_DONE_A; 476 s->internal->init_num = 0; 477 } 478 break; 479 480 case SSL3_ST_SW_SRVR_DONE_A: 481 case SSL3_ST_SW_SRVR_DONE_B: 482 if (SSL_is_dtls(s)) 483 dtls1_start_timer(s); 484 ret = ssl3_send_server_done(s); 485 if (ret <= 0) 486 goto end; 487 S3I(s)->hs.tls12.next_state = SSL3_ST_SR_CERT_A; 488 S3I(s)->hs.state = SSL3_ST_SW_FLUSH; 489 s->internal->init_num = 0; 490 break; 491 492 case SSL3_ST_SW_FLUSH: 493 /* 494 * This code originally checked to see if 495 * any data was pending using BIO_CTRL_INFO 496 * and then flushed. This caused problems 497 * as documented in PR#1939. The proposed 498 * fix doesn't completely resolve this issue 499 * as buggy implementations of BIO_CTRL_PENDING 500 * still exist. So instead we just flush 501 * unconditionally. 502 */ 503 s->internal->rwstate = SSL_WRITING; 504 if (BIO_flush(s->wbio) <= 0) { 505 if (SSL_is_dtls(s)) { 506 /* If the write error was fatal, stop trying. */ 507 if (!BIO_should_retry(s->wbio)) { 508 s->internal->rwstate = SSL_NOTHING; 509 S3I(s)->hs.state = S3I(s)->hs.tls12.next_state; 510 } 511 } 512 ret = -1; 513 goto end; 514 } 515 s->internal->rwstate = SSL_NOTHING; 516 S3I(s)->hs.state = S3I(s)->hs.tls12.next_state; 517 break; 518 519 case SSL3_ST_SR_CERT_A: 520 case SSL3_ST_SR_CERT_B: 521 if (S3I(s)->hs.tls12.cert_request) { 522 ret = ssl3_get_client_certificate(s); 523 if (ret <= 0) 524 goto end; 525 } 526 s->internal->init_num = 0; 527 S3I(s)->hs.state = SSL3_ST_SR_KEY_EXCH_A; 528 break; 529 530 case SSL3_ST_SR_KEY_EXCH_A: 531 case SSL3_ST_SR_KEY_EXCH_B: 532 ret = ssl3_get_client_key_exchange(s); 533 if (ret <= 0) 534 goto end; 535 536 if (SSL_is_dtls(s)) { 537 S3I(s)->hs.state = SSL3_ST_SR_CERT_VRFY_A; 538 s->internal->init_num = 0; 539 } 540 541 alg_k = S3I(s)->hs.cipher->algorithm_mkey; 542 if (ret == 2) { 543 /* 544 * For the ECDH ciphersuites when 545 * the client sends its ECDH pub key in 546 * a certificate, the CertificateVerify 547 * message is not sent. 548 * Also for GOST ciphersuites when 549 * the client uses its key from the certificate 550 * for key exchange. 551 */ 552 S3I(s)->hs.state = SSL3_ST_SR_FINISHED_A; 553 s->internal->init_num = 0; 554 } else if (SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) { 555 S3I(s)->hs.state = SSL3_ST_SR_CERT_VRFY_A; 556 s->internal->init_num = 0; 557 if (!s->session->peer) 558 break; 559 /* 560 * Freeze the transcript for use during client 561 * certificate verification. 562 */ 563 tls1_transcript_freeze(s); 564 } else { 565 S3I(s)->hs.state = SSL3_ST_SR_CERT_VRFY_A; 566 s->internal->init_num = 0; 567 568 tls1_transcript_free(s); 569 570 /* 571 * We need to get hashes here so if there is 572 * a client cert, it can be verified. 573 */ 574 if (!tls1_transcript_hash_value(s, 575 S3I(s)->hs.tls12.cert_verify, 576 sizeof(S3I(s)->hs.tls12.cert_verify), 577 NULL)) { 578 ret = -1; 579 goto end; 580 } 581 } 582 break; 583 584 case SSL3_ST_SR_CERT_VRFY_A: 585 case SSL3_ST_SR_CERT_VRFY_B: 586 if (SSL_is_dtls(s)) 587 D1I(s)->change_cipher_spec_ok = 1; 588 else 589 s->s3->flags |= SSL3_FLAGS_CCS_OK; 590 591 /* we should decide if we expected this one */ 592 ret = ssl3_get_cert_verify(s); 593 if (ret <= 0) 594 goto end; 595 S3I(s)->hs.state = SSL3_ST_SR_FINISHED_A; 596 s->internal->init_num = 0; 597 break; 598 599 case SSL3_ST_SR_FINISHED_A: 600 case SSL3_ST_SR_FINISHED_B: 601 if (SSL_is_dtls(s)) 602 D1I(s)->change_cipher_spec_ok = 1; 603 else 604 s->s3->flags |= SSL3_FLAGS_CCS_OK; 605 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, 606 SSL3_ST_SR_FINISHED_B); 607 if (ret <= 0) 608 goto end; 609 if (SSL_is_dtls(s)) 610 dtls1_stop_timer(s); 611 if (s->internal->hit) 612 S3I(s)->hs.state = SSL_ST_OK; 613 else if (s->internal->tlsext_ticket_expected) 614 S3I(s)->hs.state = SSL3_ST_SW_SESSION_TICKET_A; 615 else 616 S3I(s)->hs.state = SSL3_ST_SW_CHANGE_A; 617 s->internal->init_num = 0; 618 break; 619 620 case SSL3_ST_SW_SESSION_TICKET_A: 621 case SSL3_ST_SW_SESSION_TICKET_B: 622 ret = ssl3_send_newsession_ticket(s); 623 if (ret <= 0) 624 goto end; 625 S3I(s)->hs.state = SSL3_ST_SW_CHANGE_A; 626 s->internal->init_num = 0; 627 break; 628 629 case SSL3_ST_SW_CERT_STATUS_A: 630 case SSL3_ST_SW_CERT_STATUS_B: 631 ret = ssl3_send_cert_status(s); 632 if (ret <= 0) 633 goto end; 634 S3I(s)->hs.state = SSL3_ST_SW_KEY_EXCH_A; 635 s->internal->init_num = 0; 636 break; 637 638 case SSL3_ST_SW_CHANGE_A: 639 case SSL3_ST_SW_CHANGE_B: 640 ret = ssl3_send_change_cipher_spec(s, 641 SSL3_ST_SW_CHANGE_A, SSL3_ST_SW_CHANGE_B); 642 if (ret <= 0) 643 goto end; 644 S3I(s)->hs.state = SSL3_ST_SW_FINISHED_A; 645 s->internal->init_num = 0; 646 s->session->cipher = S3I(s)->hs.cipher; 647 648 if (!tls1_setup_key_block(s)) { 649 ret = -1; 650 goto end; 651 } 652 if (!tls1_change_write_cipher_state(s)) { 653 ret = -1; 654 goto end; 655 } 656 break; 657 658 case SSL3_ST_SW_FINISHED_A: 659 case SSL3_ST_SW_FINISHED_B: 660 ret = ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, 661 SSL3_ST_SW_FINISHED_B); 662 if (ret <= 0) 663 goto end; 664 S3I(s)->hs.state = SSL3_ST_SW_FLUSH; 665 if (s->internal->hit) { 666 S3I(s)->hs.tls12.next_state = SSL3_ST_SR_FINISHED_A; 667 tls1_transcript_free(s); 668 } else 669 S3I(s)->hs.tls12.next_state = SSL_ST_OK; 670 s->internal->init_num = 0; 671 break; 672 673 case SSL_ST_OK: 674 /* clean a few things up */ 675 tls1_cleanup_key_block(s); 676 677 if (S3I(s)->handshake_transcript != NULL) { 678 SSLerror(s, ERR_R_INTERNAL_ERROR); 679 ret = -1; 680 goto end; 681 } 682 683 if (!SSL_is_dtls(s)) 684 ssl3_release_init_buffer(s); 685 686 /* remove buffering on output */ 687 ssl_free_wbio_buffer(s); 688 689 s->internal->init_num = 0; 690 691 /* Skipped if we just sent a HelloRequest. */ 692 if (s->internal->renegotiate == 2) { 693 s->internal->renegotiate = 0; 694 s->internal->new_session = 0; 695 696 ssl_update_cache(s, SSL_SESS_CACHE_SERVER); 697 698 s->ctx->internal->stats.sess_accept_good++; 699 /* s->server=1; */ 700 s->internal->handshake_func = ssl3_accept; 701 702 ssl_info_callback(s, SSL_CB_HANDSHAKE_DONE, 1); 703 } 704 705 ret = 1; 706 707 if (SSL_is_dtls(s)) { 708 /* Done handshaking, next message is client hello. */ 709 D1I(s)->handshake_read_seq = 0; 710 /* Next message is server hello. */ 711 D1I(s)->handshake_write_seq = 0; 712 D1I(s)->next_handshake_write_seq = 0; 713 } 714 goto end; 715 /* break; */ 716 717 default: 718 SSLerror(s, SSL_R_UNKNOWN_STATE); 719 ret = -1; 720 goto end; 721 /* break; */ 722 } 723 724 if (!S3I(s)->hs.tls12.reuse_message && !skip) { 725 if (s->internal->debug) { 726 if ((ret = BIO_flush(s->wbio)) <= 0) 727 goto end; 728 } 729 730 731 if (S3I(s)->hs.state != state) { 732 new_state = S3I(s)->hs.state; 733 S3I(s)->hs.state = state; 734 ssl_info_callback(s, SSL_CB_ACCEPT_LOOP, 1); 735 S3I(s)->hs.state = new_state; 736 } 737 } 738 skip = 0; 739 } 740 end: 741 /* BIO_flush(s->wbio); */ 742 s->internal->in_handshake--; 743 ssl_info_callback(s, SSL_CB_ACCEPT_EXIT, ret); 744 745 return (ret); 746 } 747 748 int 749 ssl3_send_hello_request(SSL *s) 750 { 751 CBB cbb, hello; 752 753 memset(&cbb, 0, sizeof(cbb)); 754 755 if (S3I(s)->hs.state == SSL3_ST_SW_HELLO_REQ_A) { 756 if (!ssl3_handshake_msg_start(s, &cbb, &hello, 757 SSL3_MT_HELLO_REQUEST)) 758 goto err; 759 if (!ssl3_handshake_msg_finish(s, &cbb)) 760 goto err; 761 762 S3I(s)->hs.state = SSL3_ST_SW_HELLO_REQ_B; 763 } 764 765 /* SSL3_ST_SW_HELLO_REQ_B */ 766 return (ssl3_handshake_write(s)); 767 768 err: 769 CBB_cleanup(&cbb); 770 771 return (-1); 772 } 773 774 int 775 ssl3_get_client_hello(SSL *s) 776 { 777 CBS cbs, client_random, session_id, cookie, cipher_suites; 778 CBS compression_methods; 779 uint16_t client_version; 780 uint8_t comp_method; 781 int comp_null; 782 int i, j, al, ret, cookie_valid = 0; 783 unsigned long id; 784 SSL_CIPHER *c; 785 STACK_OF(SSL_CIPHER) *ciphers = NULL; 786 unsigned long alg_k; 787 const SSL_METHOD *method; 788 uint16_t shared_version; 789 790 /* 791 * We do this so that we will respond with our native type. 792 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 793 * This down switching should be handled by a different method. 794 * If we are SSLv3, we will respond with SSLv3, even if prompted with 795 * TLSv1. 796 */ 797 if (S3I(s)->hs.state == SSL3_ST_SR_CLNT_HELLO_A) 798 S3I(s)->hs.state = SSL3_ST_SR_CLNT_HELLO_B; 799 800 s->internal->first_packet = 1; 801 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CLNT_HELLO_B, 802 SSL3_ST_SR_CLNT_HELLO_C, SSL3_MT_CLIENT_HELLO, 803 SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 804 return ret; 805 s->internal->first_packet = 0; 806 807 ret = -1; 808 809 if (s->internal->init_num < 0) 810 goto err; 811 812 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 813 814 /* Parse client hello up until the extensions (if any). */ 815 if (!CBS_get_u16(&cbs, &client_version)) 816 goto decode_err; 817 if (!CBS_get_bytes(&cbs, &client_random, SSL3_RANDOM_SIZE)) 818 goto decode_err; 819 if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) 820 goto decode_err; 821 if (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE) { 822 al = SSL_AD_ILLEGAL_PARAMETER; 823 SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG); 824 goto fatal_err; 825 } 826 if (SSL_is_dtls(s)) { 827 if (!CBS_get_u8_length_prefixed(&cbs, &cookie)) 828 goto decode_err; 829 } 830 if (!CBS_get_u16_length_prefixed(&cbs, &cipher_suites)) 831 goto decode_err; 832 if (!CBS_get_u8_length_prefixed(&cbs, &compression_methods)) 833 goto decode_err; 834 835 /* 836 * Use version from inside client hello, not from record header. 837 * (may differ: see RFC 2246, Appendix E, second paragraph) 838 */ 839 if (!ssl_max_shared_version(s, client_version, &shared_version)) { 840 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR && 841 !tls12_record_layer_write_protected(s->internal->rl)) { 842 /* 843 * Similar to ssl3_get_record, send alert using remote 844 * version number. 845 */ 846 s->version = s->client_version; 847 } 848 SSLerror(s, SSL_R_WRONG_VERSION_NUMBER); 849 al = SSL_AD_PROTOCOL_VERSION; 850 goto fatal_err; 851 } 852 s->client_version = client_version; 853 s->version = shared_version; 854 855 S3I(s)->hs.negotiated_tls_version = ssl_tls_version(shared_version); 856 if (S3I(s)->hs.negotiated_tls_version == 0) { 857 SSLerror(s, ERR_R_INTERNAL_ERROR); 858 goto err; 859 } 860 861 if ((method = ssl_get_method(shared_version)) == NULL) { 862 SSLerror(s, ERR_R_INTERNAL_ERROR); 863 goto err; 864 } 865 s->method = method; 866 867 /* 868 * If we require cookies (DTLS) and this ClientHello does not contain 869 * one, just return since we do not want to allocate any memory yet. 870 * So check cookie length... 871 */ 872 if (SSL_is_dtls(s)) { 873 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) { 874 if (CBS_len(&cookie) == 0) 875 return (1); 876 } 877 } 878 879 if (!CBS_write_bytes(&client_random, s->s3->client_random, 880 sizeof(s->s3->client_random), NULL)) 881 goto err; 882 883 s->internal->hit = 0; 884 885 /* 886 * Versions before 0.9.7 always allow clients to resume sessions in 887 * renegotiation. 0.9.7 and later allow this by default, but optionally 888 * ignore resumption requests with flag 889 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag 890 * rather than a change to default behavior so that applications 891 * relying on this for security won't even compile against older 892 * library versions). 893 * 894 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() 895 * to request renegotiation but not a new session (s->internal->new_session 896 * remains unset): for servers, this essentially just means that the 897 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be 898 * ignored. 899 */ 900 if ((s->internal->new_session && (s->internal->options & 901 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { 902 if (!ssl_get_new_session(s, 1)) 903 goto err; 904 } else { 905 CBS ext_block; 906 907 CBS_dup(&cbs, &ext_block); 908 909 i = ssl_get_prev_session(s, &session_id, &ext_block, &al); 910 if (i == 1) { /* previous session */ 911 s->internal->hit = 1; 912 } else if (i == -1) 913 goto fatal_err; 914 else { 915 /* i == 0 */ 916 if (!ssl_get_new_session(s, 1)) 917 goto err; 918 } 919 } 920 921 if (SSL_is_dtls(s)) { 922 /* 923 * The ClientHello may contain a cookie even if the HelloVerify 924 * message has not been sent - make sure that it does not cause 925 * an overflow. 926 */ 927 if (CBS_len(&cookie) > sizeof(D1I(s)->rcvd_cookie)) { 928 al = SSL_AD_DECODE_ERROR; 929 SSLerror(s, SSL_R_COOKIE_MISMATCH); 930 goto fatal_err; 931 } 932 933 /* Verify the cookie if appropriate option is set. */ 934 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 935 CBS_len(&cookie) > 0) { 936 size_t cookie_len; 937 938 /* XXX - rcvd_cookie seems to only be used here... */ 939 if (!CBS_write_bytes(&cookie, D1I(s)->rcvd_cookie, 940 sizeof(D1I(s)->rcvd_cookie), &cookie_len)) 941 goto err; 942 943 if (s->ctx->internal->app_verify_cookie_cb != NULL) { 944 if (s->ctx->internal->app_verify_cookie_cb(s, 945 D1I(s)->rcvd_cookie, cookie_len) == 0) { 946 al = SSL_AD_HANDSHAKE_FAILURE; 947 SSLerror(s, SSL_R_COOKIE_MISMATCH); 948 goto fatal_err; 949 } 950 /* else cookie verification succeeded */ 951 /* XXX - can d1->cookie_len > sizeof(rcvd_cookie) ? */ 952 } else if (timingsafe_memcmp(D1I(s)->rcvd_cookie, 953 D1I(s)->cookie, D1I(s)->cookie_len) != 0) { 954 /* default verification */ 955 al = SSL_AD_HANDSHAKE_FAILURE; 956 SSLerror(s, SSL_R_COOKIE_MISMATCH); 957 goto fatal_err; 958 } 959 cookie_valid = 1; 960 } 961 } 962 963 /* XXX - This logic seems wrong... */ 964 if (CBS_len(&cipher_suites) == 0 && CBS_len(&session_id) != 0) { 965 /* we need a cipher if we are not resuming a session */ 966 al = SSL_AD_ILLEGAL_PARAMETER; 967 SSLerror(s, SSL_R_NO_CIPHERS_SPECIFIED); 968 goto fatal_err; 969 } 970 971 if (CBS_len(&cipher_suites) > 0) { 972 if ((ciphers = ssl_bytes_to_cipher_list(s, 973 &cipher_suites)) == NULL) 974 goto err; 975 } 976 977 /* If it is a hit, check that the cipher is in the list */ 978 /* XXX - CBS_len(&cipher_suites) will always be zero here... */ 979 if (s->internal->hit && CBS_len(&cipher_suites) > 0) { 980 j = 0; 981 id = s->session->cipher->id; 982 983 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 984 c = sk_SSL_CIPHER_value(ciphers, i); 985 if (c->id == id) { 986 j = 1; 987 break; 988 } 989 } 990 if (j == 0) { 991 /* 992 * We need to have the cipher in the cipher 993 * list if we are asked to reuse it 994 */ 995 al = SSL_AD_ILLEGAL_PARAMETER; 996 SSLerror(s, SSL_R_REQUIRED_CIPHER_MISSING); 997 goto fatal_err; 998 } 999 } 1000 1001 comp_null = 0; 1002 while (CBS_len(&compression_methods) > 0) { 1003 if (!CBS_get_u8(&compression_methods, &comp_method)) 1004 goto decode_err; 1005 if (comp_method == 0) 1006 comp_null = 1; 1007 } 1008 if (comp_null == 0) { 1009 al = SSL_AD_DECODE_ERROR; 1010 SSLerror(s, SSL_R_NO_COMPRESSION_SPECIFIED); 1011 goto fatal_err; 1012 } 1013 1014 if (!tlsext_server_parse(s, SSL_TLSEXT_MSG_CH, &cbs, &al)) { 1015 SSLerror(s, SSL_R_PARSE_TLSEXT); 1016 goto fatal_err; 1017 } 1018 1019 if (CBS_len(&cbs) != 0) 1020 goto decode_err; 1021 1022 if (!S3I(s)->renegotiate_seen && s->internal->renegotiate) { 1023 al = SSL_AD_HANDSHAKE_FAILURE; 1024 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1025 goto fatal_err; 1026 } 1027 1028 if (ssl_check_clienthello_tlsext_early(s) <= 0) { 1029 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 1030 goto err; 1031 } 1032 1033 /* 1034 * Check if we want to use external pre-shared secret for this 1035 * handshake for not reused session only. We need to generate 1036 * server_random before calling tls_session_secret_cb in order to allow 1037 * SessionTicket processing to use it in key derivation. 1038 */ 1039 arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); 1040 1041 if (S3I(s)->hs.our_max_tls_version >= TLS1_2_VERSION && 1042 S3I(s)->hs.negotiated_tls_version < S3I(s)->hs.our_max_tls_version) { 1043 /* 1044 * RFC 8446 section 4.1.3. If we are downgrading from TLS 1.3 1045 * we must set the last 8 bytes of the server random to magical 1046 * values to indicate we meant to downgrade. For TLS 1.2 it is 1047 * recommended that we do the same. 1048 */ 1049 size_t index = SSL3_RANDOM_SIZE - sizeof(tls13_downgrade_12); 1050 uint8_t *magic = &s->s3->server_random[index]; 1051 if (S3I(s)->hs.negotiated_tls_version == TLS1_2_VERSION) { 1052 /* Indicate we chose to downgrade to 1.2. */ 1053 memcpy(magic, tls13_downgrade_12, 1054 sizeof(tls13_downgrade_12)); 1055 } else { 1056 /* Indicate we chose to downgrade to 1.1 or lower */ 1057 memcpy(magic, tls13_downgrade_11, 1058 sizeof(tls13_downgrade_11)); 1059 } 1060 } 1061 1062 if (!s->internal->hit && s->internal->tls_session_secret_cb) { 1063 SSL_CIPHER *pref_cipher = NULL; 1064 1065 s->session->master_key_length = sizeof(s->session->master_key); 1066 if (s->internal->tls_session_secret_cb(s, s->session->master_key, 1067 &s->session->master_key_length, ciphers, &pref_cipher, 1068 s->internal->tls_session_secret_cb_arg)) { 1069 s->internal->hit = 1; 1070 s->session->ciphers = ciphers; 1071 s->session->verify_result = X509_V_OK; 1072 1073 ciphers = NULL; 1074 1075 /* check if some cipher was preferred by call back */ 1076 pref_cipher = pref_cipher ? pref_cipher : 1077 ssl3_choose_cipher(s, s->session->ciphers, 1078 SSL_get_ciphers(s)); 1079 if (pref_cipher == NULL) { 1080 al = SSL_AD_HANDSHAKE_FAILURE; 1081 SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1082 goto fatal_err; 1083 } 1084 1085 s->session->cipher = pref_cipher; 1086 1087 sk_SSL_CIPHER_free(s->cipher_list); 1088 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); 1089 } 1090 } 1091 1092 /* 1093 * Given s->session->ciphers and SSL_get_ciphers, we must 1094 * pick a cipher 1095 */ 1096 1097 if (!s->internal->hit) { 1098 sk_SSL_CIPHER_free(s->session->ciphers); 1099 s->session->ciphers = ciphers; 1100 if (ciphers == NULL) { 1101 al = SSL_AD_ILLEGAL_PARAMETER; 1102 SSLerror(s, SSL_R_NO_CIPHERS_PASSED); 1103 goto fatal_err; 1104 } 1105 ciphers = NULL; 1106 c = ssl3_choose_cipher(s, s->session->ciphers, 1107 SSL_get_ciphers(s)); 1108 1109 if (c == NULL) { 1110 al = SSL_AD_HANDSHAKE_FAILURE; 1111 SSLerror(s, SSL_R_NO_SHARED_CIPHER); 1112 goto fatal_err; 1113 } 1114 S3I(s)->hs.cipher = c; 1115 } else { 1116 S3I(s)->hs.cipher = s->session->cipher; 1117 } 1118 1119 if (!tls1_transcript_hash_init(s)) 1120 goto err; 1121 1122 alg_k = S3I(s)->hs.cipher->algorithm_mkey; 1123 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST)) || 1124 !(s->verify_mode & SSL_VERIFY_PEER)) 1125 tls1_transcript_free(s); 1126 1127 /* 1128 * We now have the following setup. 1129 * client_random 1130 * cipher_list - our prefered list of ciphers 1131 * ciphers - the clients prefered list of ciphers 1132 * compression - basically ignored right now 1133 * ssl version is set - sslv3 1134 * s->session - The ssl session has been setup. 1135 * s->internal->hit - session reuse flag 1136 * s->hs.cipher - the new cipher to use. 1137 */ 1138 1139 /* Handles TLS extensions that we couldn't check earlier */ 1140 if (ssl_check_clienthello_tlsext_late(s) <= 0) { 1141 SSLerror(s, SSL_R_CLIENTHELLO_TLSEXT); 1142 goto err; 1143 } 1144 1145 ret = cookie_valid ? 2 : 1; 1146 1147 if (0) { 1148 decode_err: 1149 al = SSL_AD_DECODE_ERROR; 1150 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1151 fatal_err: 1152 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1153 } 1154 err: 1155 sk_SSL_CIPHER_free(ciphers); 1156 1157 return (ret); 1158 } 1159 1160 int 1161 ssl3_send_dtls_hello_verify_request(SSL *s) 1162 { 1163 CBB cbb, verify, cookie; 1164 1165 memset(&cbb, 0, sizeof(cbb)); 1166 1167 if (S3I(s)->hs.state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) { 1168 if (s->ctx->internal->app_gen_cookie_cb == NULL || 1169 s->ctx->internal->app_gen_cookie_cb(s, D1I(s)->cookie, 1170 &(D1I(s)->cookie_len)) == 0) { 1171 SSLerror(s, ERR_R_INTERNAL_ERROR); 1172 return 0; 1173 } 1174 1175 /* 1176 * Per RFC 6347 section 4.2.1, the HelloVerifyRequest should 1177 * always contain DTLSv1.0 regardless of the version that is 1178 * going to be negotiated. 1179 */ 1180 if (!ssl3_handshake_msg_start(s, &cbb, &verify, 1181 DTLS1_MT_HELLO_VERIFY_REQUEST)) 1182 goto err; 1183 if (!CBB_add_u16(&verify, DTLS1_VERSION)) 1184 goto err; 1185 if (!CBB_add_u8_length_prefixed(&verify, &cookie)) 1186 goto err; 1187 if (!CBB_add_bytes(&cookie, D1I(s)->cookie, D1I(s)->cookie_len)) 1188 goto err; 1189 if (!ssl3_handshake_msg_finish(s, &cbb)) 1190 goto err; 1191 1192 S3I(s)->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; 1193 } 1194 1195 /* S3I(s)->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 1196 return (ssl3_handshake_write(s)); 1197 1198 err: 1199 CBB_cleanup(&cbb); 1200 1201 return (-1); 1202 } 1203 1204 int 1205 ssl3_send_server_hello(SSL *s) 1206 { 1207 CBB cbb, server_hello, session_id; 1208 size_t sl; 1209 1210 memset(&cbb, 0, sizeof(cbb)); 1211 1212 if (S3I(s)->hs.state == SSL3_ST_SW_SRVR_HELLO_A) { 1213 if (!ssl3_handshake_msg_start(s, &cbb, &server_hello, 1214 SSL3_MT_SERVER_HELLO)) 1215 goto err; 1216 1217 if (!CBB_add_u16(&server_hello, s->version)) 1218 goto err; 1219 if (!CBB_add_bytes(&server_hello, s->s3->server_random, 1220 sizeof(s->s3->server_random))) 1221 goto err; 1222 1223 /* 1224 * There are several cases for the session ID to send 1225 * back in the server hello: 1226 * 1227 * - For session reuse from the session cache, 1228 * we send back the old session ID. 1229 * - If stateless session reuse (using a session ticket) 1230 * is successful, we send back the client's "session ID" 1231 * (which doesn't actually identify the session). 1232 * - If it is a new session, we send back the new 1233 * session ID. 1234 * - However, if we want the new session to be single-use, 1235 * we send back a 0-length session ID. 1236 * 1237 * s->internal->hit is non-zero in either case of session reuse, 1238 * so the following won't overwrite an ID that we're supposed 1239 * to send back. 1240 */ 1241 if (!(s->ctx->internal->session_cache_mode & SSL_SESS_CACHE_SERVER) 1242 && !s->internal->hit) 1243 s->session->session_id_length = 0; 1244 1245 sl = s->session->session_id_length; 1246 if (sl > sizeof(s->session->session_id)) { 1247 SSLerror(s, ERR_R_INTERNAL_ERROR); 1248 goto err; 1249 } 1250 if (!CBB_add_u8_length_prefixed(&server_hello, &session_id)) 1251 goto err; 1252 if (!CBB_add_bytes(&session_id, s->session->session_id, sl)) 1253 goto err; 1254 1255 /* Cipher suite. */ 1256 if (!CBB_add_u16(&server_hello, 1257 ssl3_cipher_get_value(S3I(s)->hs.cipher))) 1258 goto err; 1259 1260 /* Compression method (null). */ 1261 if (!CBB_add_u8(&server_hello, 0)) 1262 goto err; 1263 1264 /* TLS extensions */ 1265 if (!tlsext_server_build(s, SSL_TLSEXT_MSG_SH, &server_hello)) { 1266 SSLerror(s, ERR_R_INTERNAL_ERROR); 1267 goto err; 1268 } 1269 1270 if (!ssl3_handshake_msg_finish(s, &cbb)) 1271 goto err; 1272 } 1273 1274 /* SSL3_ST_SW_SRVR_HELLO_B */ 1275 return (ssl3_handshake_write(s)); 1276 1277 err: 1278 CBB_cleanup(&cbb); 1279 1280 return (-1); 1281 } 1282 1283 int 1284 ssl3_send_server_done(SSL *s) 1285 { 1286 CBB cbb, done; 1287 1288 memset(&cbb, 0, sizeof(cbb)); 1289 1290 if (S3I(s)->hs.state == SSL3_ST_SW_SRVR_DONE_A) { 1291 if (!ssl3_handshake_msg_start(s, &cbb, &done, 1292 SSL3_MT_SERVER_DONE)) 1293 goto err; 1294 if (!ssl3_handshake_msg_finish(s, &cbb)) 1295 goto err; 1296 1297 S3I(s)->hs.state = SSL3_ST_SW_SRVR_DONE_B; 1298 } 1299 1300 /* SSL3_ST_SW_SRVR_DONE_B */ 1301 return (ssl3_handshake_write(s)); 1302 1303 err: 1304 CBB_cleanup(&cbb); 1305 1306 return (-1); 1307 } 1308 1309 static int 1310 ssl3_send_server_kex_dhe(SSL *s, CBB *cbb) 1311 { 1312 CBB dh_p, dh_g, dh_Ys; 1313 DH *dh = NULL, *dhp; 1314 unsigned char *data; 1315 int al; 1316 1317 if (s->cert->dh_tmp_auto != 0) { 1318 if ((dhp = ssl_get_auto_dh(s)) == NULL) { 1319 al = SSL_AD_INTERNAL_ERROR; 1320 SSLerror(s, ERR_R_INTERNAL_ERROR); 1321 goto fatal_err; 1322 } 1323 } else 1324 dhp = s->cert->dh_tmp; 1325 1326 if (dhp == NULL && s->cert->dh_tmp_cb != NULL) 1327 dhp = s->cert->dh_tmp_cb(s, 0, 1328 SSL_C_PKEYLENGTH(S3I(s)->hs.cipher)); 1329 1330 if (dhp == NULL) { 1331 al = SSL_AD_HANDSHAKE_FAILURE; 1332 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1333 goto fatal_err; 1334 } 1335 1336 if (S3I(s)->tmp.dh != NULL) { 1337 SSLerror(s, ERR_R_INTERNAL_ERROR); 1338 goto err; 1339 } 1340 1341 if (s->cert->dh_tmp_auto != 0) { 1342 dh = dhp; 1343 } else if ((dh = DHparams_dup(dhp)) == NULL) { 1344 SSLerror(s, ERR_R_DH_LIB); 1345 goto err; 1346 } 1347 S3I(s)->tmp.dh = dh; 1348 if (!DH_generate_key(dh)) { 1349 SSLerror(s, ERR_R_DH_LIB); 1350 goto err; 1351 } 1352 1353 /* 1354 * Serialize the DH parameters and public key. 1355 */ 1356 if (!CBB_add_u16_length_prefixed(cbb, &dh_p)) 1357 goto err; 1358 if (!CBB_add_space(&dh_p, &data, BN_num_bytes(dh->p))) 1359 goto err; 1360 BN_bn2bin(dh->p, data); 1361 1362 if (!CBB_add_u16_length_prefixed(cbb, &dh_g)) 1363 goto err; 1364 if (!CBB_add_space(&dh_g, &data, BN_num_bytes(dh->g))) 1365 goto err; 1366 BN_bn2bin(dh->g, data); 1367 1368 if (!CBB_add_u16_length_prefixed(cbb, &dh_Ys)) 1369 goto err; 1370 if (!CBB_add_space(&dh_Ys, &data, BN_num_bytes(dh->pub_key))) 1371 goto err; 1372 BN_bn2bin(dh->pub_key, data); 1373 1374 if (!CBB_flush(cbb)) 1375 goto err; 1376 1377 return (1); 1378 1379 fatal_err: 1380 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1381 err: 1382 return (-1); 1383 } 1384 1385 static int 1386 ssl3_send_server_kex_ecdhe_ecp(SSL *s, int nid, CBB *cbb) 1387 { 1388 uint16_t curve_id; 1389 EC_KEY *ecdh; 1390 CBB ecpoint; 1391 int al; 1392 1393 /* 1394 * Only named curves are supported in ECDH ephemeral key exchanges. 1395 * For supported named curves, curve_id is non-zero. 1396 */ 1397 if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) { 1398 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1399 goto err; 1400 } 1401 1402 if (S3I(s)->tmp.ecdh != NULL) { 1403 SSLerror(s, ERR_R_INTERNAL_ERROR); 1404 goto err; 1405 } 1406 1407 if ((S3I(s)->tmp.ecdh = EC_KEY_new()) == NULL) { 1408 al = SSL_AD_HANDSHAKE_FAILURE; 1409 SSLerror(s, SSL_R_MISSING_TMP_ECDH_KEY); 1410 goto fatal_err; 1411 } 1412 S3I(s)->tmp.ecdh_nid = nid; 1413 ecdh = S3I(s)->tmp.ecdh; 1414 1415 if (!ssl_kex_generate_ecdhe_ecp(ecdh, nid)) 1416 goto err; 1417 1418 /* 1419 * Encode the public key. 1420 * 1421 * Only named curves are supported in ECDH ephemeral key exchanges. 1422 * In this case the ServerKeyExchange message has: 1423 * [1 byte CurveType], [2 byte CurveName] 1424 * [1 byte length of encoded point], followed by 1425 * the actual encoded point itself. 1426 */ 1427 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) 1428 goto err; 1429 if (!CBB_add_u16(cbb, curve_id)) 1430 goto err; 1431 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) 1432 goto err; 1433 if (!ssl_kex_public_ecdhe_ecp(ecdh, &ecpoint)) 1434 goto err; 1435 if (!CBB_flush(cbb)) 1436 goto err; 1437 1438 return (1); 1439 1440 fatal_err: 1441 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1442 err: 1443 return (-1); 1444 } 1445 1446 static int 1447 ssl3_send_server_kex_ecdhe_ecx(SSL *s, int nid, CBB *cbb) 1448 { 1449 uint8_t *public_key = NULL, *private_key = NULL; 1450 uint16_t curve_id; 1451 CBB ecpoint; 1452 int ret = -1; 1453 1454 /* Generate an X25519 key pair. */ 1455 if (S3I(s)->tmp.x25519 != NULL) { 1456 SSLerror(s, ERR_R_INTERNAL_ERROR); 1457 goto err; 1458 } 1459 if ((private_key = malloc(X25519_KEY_LENGTH)) == NULL) 1460 goto err; 1461 if ((public_key = malloc(X25519_KEY_LENGTH)) == NULL) 1462 goto err; 1463 X25519_keypair(public_key, private_key); 1464 1465 /* Serialize public key. */ 1466 if ((curve_id = tls1_ec_nid2curve_id(nid)) == 0) { 1467 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1468 goto err; 1469 } 1470 1471 if (!CBB_add_u8(cbb, NAMED_CURVE_TYPE)) 1472 goto err; 1473 if (!CBB_add_u16(cbb, curve_id)) 1474 goto err; 1475 if (!CBB_add_u8_length_prefixed(cbb, &ecpoint)) 1476 goto err; 1477 if (!CBB_add_bytes(&ecpoint, public_key, X25519_KEY_LENGTH)) 1478 goto err; 1479 if (!CBB_flush(cbb)) 1480 goto err; 1481 1482 S3I(s)->tmp.x25519 = private_key; 1483 private_key = NULL; 1484 ret = 1; 1485 1486 err: 1487 free(public_key); 1488 freezero(private_key, X25519_KEY_LENGTH); 1489 1490 return (ret); 1491 } 1492 1493 static int 1494 ssl3_send_server_kex_ecdhe(SSL *s, CBB *cbb) 1495 { 1496 int nid; 1497 1498 nid = tls1_get_shared_curve(s); 1499 1500 if (nid == NID_X25519) 1501 return ssl3_send_server_kex_ecdhe_ecx(s, nid, cbb); 1502 1503 return ssl3_send_server_kex_ecdhe_ecp(s, nid, cbb); 1504 } 1505 1506 int 1507 ssl3_send_server_key_exchange(SSL *s) 1508 { 1509 CBB cbb, cbb_params, cbb_signature, server_kex; 1510 const struct ssl_sigalg *sigalg = NULL; 1511 unsigned char *signature = NULL; 1512 size_t signature_len = 0; 1513 unsigned char *params = NULL; 1514 size_t params_len; 1515 const EVP_MD *md = NULL; 1516 unsigned long type; 1517 EVP_MD_CTX md_ctx; 1518 EVP_PKEY_CTX *pctx; 1519 EVP_PKEY *pkey; 1520 int al; 1521 1522 memset(&cbb, 0, sizeof(cbb)); 1523 memset(&cbb_params, 0, sizeof(cbb_params)); 1524 1525 EVP_MD_CTX_init(&md_ctx); 1526 1527 if (S3I(s)->hs.state == SSL3_ST_SW_KEY_EXCH_A) { 1528 1529 if (!ssl3_handshake_msg_start(s, &cbb, &server_kex, 1530 SSL3_MT_SERVER_KEY_EXCHANGE)) 1531 goto err; 1532 1533 if (!CBB_init(&cbb_params, 0)) 1534 goto err; 1535 1536 type = S3I(s)->hs.cipher->algorithm_mkey; 1537 if (type & SSL_kDHE) { 1538 if (ssl3_send_server_kex_dhe(s, &cbb_params) != 1) 1539 goto err; 1540 } else if (type & SSL_kECDHE) { 1541 if (ssl3_send_server_kex_ecdhe(s, &cbb_params) != 1) 1542 goto err; 1543 } else { 1544 al = SSL_AD_HANDSHAKE_FAILURE; 1545 SSLerror(s, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1546 goto fatal_err; 1547 } 1548 1549 if (!CBB_finish(&cbb_params, ¶ms, ¶ms_len)) 1550 goto err; 1551 1552 if (!CBB_add_bytes(&server_kex, params, params_len)) 1553 goto err; 1554 1555 /* Add signature unless anonymous. */ 1556 if (!(S3I(s)->hs.cipher->algorithm_auth & SSL_aNULL)) { 1557 if ((pkey = ssl_get_sign_pkey(s, S3I(s)->hs.cipher, 1558 &md, &sigalg)) == NULL) { 1559 al = SSL_AD_DECODE_ERROR; 1560 goto fatal_err; 1561 } 1562 S3I(s)->hs.our_sigalg = sigalg; 1563 1564 /* Send signature algorithm. */ 1565 if (SSL_USE_SIGALGS(s)) { 1566 if (!CBB_add_u16(&server_kex, sigalg->value)) { 1567 al = SSL_AD_INTERNAL_ERROR; 1568 SSLerror(s, ERR_R_INTERNAL_ERROR); 1569 goto fatal_err; 1570 } 1571 } 1572 1573 if (!EVP_DigestSignInit(&md_ctx, &pctx, md, NULL, pkey)) { 1574 SSLerror(s, ERR_R_EVP_LIB); 1575 goto err; 1576 } 1577 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1578 (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1579 RSA_PKCS1_PSS_PADDING) || 1580 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 1581 SSLerror(s, ERR_R_EVP_LIB); 1582 goto err; 1583 } 1584 if (!EVP_DigestSignUpdate(&md_ctx, s->s3->client_random, 1585 SSL3_RANDOM_SIZE)) { 1586 SSLerror(s, ERR_R_EVP_LIB); 1587 goto err; 1588 } 1589 if (!EVP_DigestSignUpdate(&md_ctx, s->s3->server_random, 1590 SSL3_RANDOM_SIZE)) { 1591 SSLerror(s, ERR_R_EVP_LIB); 1592 goto err; 1593 } 1594 if (!EVP_DigestSignUpdate(&md_ctx, params, params_len)) { 1595 SSLerror(s, ERR_R_EVP_LIB); 1596 goto err; 1597 } 1598 if (!EVP_DigestSignFinal(&md_ctx, NULL, &signature_len) || 1599 !signature_len) { 1600 SSLerror(s, ERR_R_EVP_LIB); 1601 goto err; 1602 } 1603 if ((signature = calloc(1, signature_len)) == NULL) { 1604 SSLerror(s, ERR_R_MALLOC_FAILURE); 1605 goto err; 1606 } 1607 if (!EVP_DigestSignFinal(&md_ctx, signature, &signature_len)) { 1608 SSLerror(s, ERR_R_EVP_LIB); 1609 goto err; 1610 } 1611 1612 if (!CBB_add_u16_length_prefixed(&server_kex, 1613 &cbb_signature)) 1614 goto err; 1615 if (!CBB_add_bytes(&cbb_signature, signature, 1616 signature_len)) 1617 goto err; 1618 } 1619 1620 if (!ssl3_handshake_msg_finish(s, &cbb)) 1621 goto err; 1622 1623 S3I(s)->hs.state = SSL3_ST_SW_KEY_EXCH_B; 1624 } 1625 1626 EVP_MD_CTX_cleanup(&md_ctx); 1627 free(params); 1628 free(signature); 1629 1630 return (ssl3_handshake_write(s)); 1631 1632 fatal_err: 1633 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1634 err: 1635 CBB_cleanup(&cbb_params); 1636 CBB_cleanup(&cbb); 1637 EVP_MD_CTX_cleanup(&md_ctx); 1638 free(params); 1639 free(signature); 1640 1641 return (-1); 1642 } 1643 1644 int 1645 ssl3_send_certificate_request(SSL *s) 1646 { 1647 CBB cbb, cert_request, cert_types, sigalgs, cert_auth, dn; 1648 STACK_OF(X509_NAME) *sk = NULL; 1649 X509_NAME *name; 1650 int i; 1651 1652 /* 1653 * Certificate Request - RFC 5246 section 7.4.4. 1654 */ 1655 1656 memset(&cbb, 0, sizeof(cbb)); 1657 1658 if (S3I(s)->hs.state == SSL3_ST_SW_CERT_REQ_A) { 1659 if (!ssl3_handshake_msg_start(s, &cbb, &cert_request, 1660 SSL3_MT_CERTIFICATE_REQUEST)) 1661 goto err; 1662 1663 if (!CBB_add_u8_length_prefixed(&cert_request, &cert_types)) 1664 goto err; 1665 if (!ssl3_get_req_cert_types(s, &cert_types)) 1666 goto err; 1667 1668 if (SSL_USE_SIGALGS(s)) { 1669 if (!CBB_add_u16_length_prefixed(&cert_request, 1670 &sigalgs)) 1671 goto err; 1672 if (!ssl_sigalgs_build( 1673 S3I(s)->hs.negotiated_tls_version, &sigalgs)) 1674 goto err; 1675 } 1676 1677 if (!CBB_add_u16_length_prefixed(&cert_request, &cert_auth)) 1678 goto err; 1679 1680 sk = SSL_get_client_CA_list(s); 1681 for (i = 0; i < sk_X509_NAME_num(sk); i++) { 1682 unsigned char *name_data; 1683 size_t name_len; 1684 1685 name = sk_X509_NAME_value(sk, i); 1686 name_len = i2d_X509_NAME(name, NULL); 1687 1688 if (!CBB_add_u16_length_prefixed(&cert_auth, &dn)) 1689 goto err; 1690 if (!CBB_add_space(&dn, &name_data, name_len)) 1691 goto err; 1692 if (i2d_X509_NAME(name, &name_data) != name_len) 1693 goto err; 1694 } 1695 1696 if (!ssl3_handshake_msg_finish(s, &cbb)) 1697 goto err; 1698 1699 S3I(s)->hs.state = SSL3_ST_SW_CERT_REQ_B; 1700 } 1701 1702 /* SSL3_ST_SW_CERT_REQ_B */ 1703 return (ssl3_handshake_write(s)); 1704 1705 err: 1706 CBB_cleanup(&cbb); 1707 1708 return (-1); 1709 } 1710 1711 static int 1712 ssl3_get_client_kex_rsa(SSL *s, CBS *cbs) 1713 { 1714 unsigned char fakekey[SSL_MAX_MASTER_KEY_LENGTH]; 1715 unsigned char *pms = NULL; 1716 unsigned char *p; 1717 size_t pms_len = 0; 1718 EVP_PKEY *pkey = NULL; 1719 RSA *rsa = NULL; 1720 CBS enc_pms; 1721 int decrypt_len; 1722 int al = -1; 1723 1724 arc4random_buf(fakekey, sizeof(fakekey)); 1725 1726 /* XXX - peer max protocol version. */ 1727 fakekey[0] = s->client_version >> 8; 1728 fakekey[1] = s->client_version & 0xff; 1729 1730 pkey = s->cert->pkeys[SSL_PKEY_RSA].privatekey; 1731 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) || 1732 (pkey->pkey.rsa == NULL)) { 1733 al = SSL_AD_HANDSHAKE_FAILURE; 1734 SSLerror(s, SSL_R_MISSING_RSA_CERTIFICATE); 1735 goto fatal_err; 1736 } 1737 rsa = pkey->pkey.rsa; 1738 1739 pms_len = RSA_size(rsa); 1740 if (pms_len < SSL_MAX_MASTER_KEY_LENGTH) 1741 goto err; 1742 if ((pms = malloc(pms_len)) == NULL) 1743 goto err; 1744 p = pms; 1745 1746 if (!CBS_get_u16_length_prefixed(cbs, &enc_pms)) 1747 goto decode_err; 1748 if (CBS_len(cbs) != 0 || CBS_len(&enc_pms) != RSA_size(rsa)) { 1749 SSLerror(s, SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 1750 goto err; 1751 } 1752 1753 decrypt_len = RSA_private_decrypt(CBS_len(&enc_pms), CBS_data(&enc_pms), 1754 pms, rsa, RSA_PKCS1_PADDING); 1755 1756 ERR_clear_error(); 1757 1758 if (decrypt_len != SSL_MAX_MASTER_KEY_LENGTH) { 1759 al = SSL_AD_DECODE_ERROR; 1760 /* SSLerror(s, SSL_R_BAD_RSA_DECRYPT); */ 1761 } 1762 1763 /* XXX - peer max version. */ 1764 if ((al == -1) && !((pms[0] == (s->client_version >> 8)) && 1765 (pms[1] == (s->client_version & 0xff)))) { 1766 /* 1767 * The premaster secret must contain the same version number 1768 * as the ClientHello to detect version rollback attacks 1769 * (strangely, the protocol does not offer such protection for 1770 * DH ciphersuites). 1771 * 1772 * The Klima-Pokorny-Rosa extension of Bleichenbacher's attack 1773 * (http://eprint.iacr.org/2003/052/) exploits the version 1774 * number check as a "bad version oracle" -- an alert would 1775 * reveal that the plaintext corresponding to some ciphertext 1776 * made up by the adversary is properly formatted except that 1777 * the version number is wrong. To avoid such attacks, we should 1778 * treat this just like any other decryption error. 1779 */ 1780 al = SSL_AD_DECODE_ERROR; 1781 /* SSLerror(s, SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */ 1782 } 1783 1784 if (al != -1) { 1785 /* 1786 * Some decryption failure -- use random value instead 1787 * as countermeasure against Bleichenbacher's attack 1788 * on PKCS #1 v1.5 RSA padding (see RFC 2246, 1789 * section 7.4.7.1). 1790 */ 1791 p = fakekey; 1792 } 1793 1794 if (!tls12_derive_master_secret(s, p, SSL_MAX_MASTER_KEY_LENGTH)) 1795 goto err; 1796 1797 freezero(pms, pms_len); 1798 1799 return (1); 1800 1801 decode_err: 1802 al = SSL_AD_DECODE_ERROR; 1803 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1804 fatal_err: 1805 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1806 err: 1807 freezero(pms, pms_len); 1808 1809 return (-1); 1810 } 1811 1812 static int 1813 ssl3_get_client_kex_dhe(SSL *s, CBS *cbs) 1814 { 1815 int key_size = 0; 1816 int key_is_invalid, key_len, al; 1817 unsigned char *key = NULL; 1818 BIGNUM *bn = NULL; 1819 CBS dh_Yc; 1820 DH *dh; 1821 1822 if (!CBS_get_u16_length_prefixed(cbs, &dh_Yc)) 1823 goto decode_err; 1824 if (CBS_len(cbs) != 0) 1825 goto decode_err; 1826 1827 if (S3I(s)->tmp.dh == NULL) { 1828 al = SSL_AD_HANDSHAKE_FAILURE; 1829 SSLerror(s, SSL_R_MISSING_TMP_DH_KEY); 1830 goto fatal_err; 1831 } 1832 dh = S3I(s)->tmp.dh; 1833 1834 if ((bn = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL)) == NULL) { 1835 SSLerror(s, SSL_R_BN_LIB); 1836 goto err; 1837 } 1838 1839 if ((key_size = DH_size(dh)) <= 0) { 1840 SSLerror(s, ERR_R_DH_LIB); 1841 goto err; 1842 } 1843 if ((key = malloc(key_size)) == NULL) { 1844 SSLerror(s, ERR_R_MALLOC_FAILURE); 1845 goto err; 1846 } 1847 if (!DH_check_pub_key(dh, bn, &key_is_invalid)) { 1848 al = SSL_AD_INTERNAL_ERROR; 1849 SSLerror(s, ERR_R_DH_LIB); 1850 goto fatal_err; 1851 } 1852 if (key_is_invalid) { 1853 al = SSL_AD_ILLEGAL_PARAMETER; 1854 SSLerror(s, ERR_R_DH_LIB); 1855 goto fatal_err; 1856 } 1857 if ((key_len = DH_compute_key(key, bn, dh)) <= 0) { 1858 al = SSL_AD_INTERNAL_ERROR; 1859 SSLerror(s, ERR_R_DH_LIB); 1860 goto fatal_err; 1861 } 1862 1863 if (!tls12_derive_master_secret(s, key, key_len)) 1864 goto err; 1865 1866 DH_free(S3I(s)->tmp.dh); 1867 S3I(s)->tmp.dh = NULL; 1868 1869 freezero(key, key_size); 1870 BN_clear_free(bn); 1871 1872 return (1); 1873 1874 decode_err: 1875 al = SSL_AD_DECODE_ERROR; 1876 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1877 fatal_err: 1878 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1879 err: 1880 freezero(key, key_size); 1881 BN_clear_free(bn); 1882 1883 return (-1); 1884 } 1885 1886 static int 1887 ssl3_get_client_kex_ecdhe_ecp(SSL *s, CBS *cbs) 1888 { 1889 uint8_t *key = NULL; 1890 size_t key_len = 0; 1891 EC_KEY *ecdh_peer = NULL; 1892 EC_KEY *ecdh; 1893 CBS public; 1894 int ret = -1; 1895 1896 /* 1897 * Use the ephemeral values we saved when generating the 1898 * ServerKeyExchange message. 1899 */ 1900 if ((ecdh = S3I(s)->tmp.ecdh) == NULL) { 1901 SSLerror(s, ERR_R_INTERNAL_ERROR); 1902 goto err; 1903 } 1904 1905 /* 1906 * Get client's public key from encoded point in the ClientKeyExchange 1907 * message. 1908 */ 1909 if (!CBS_get_u8_length_prefixed(cbs, &public)) 1910 goto err; 1911 if (CBS_len(cbs) != 0) 1912 goto err; 1913 1914 if ((ecdh_peer = EC_KEY_new()) == NULL) 1915 goto err; 1916 1917 if (!ssl_kex_peer_public_ecdhe_ecp(ecdh_peer, S3I(s)->tmp.ecdh_nid, 1918 &public)) 1919 goto err; 1920 1921 /* Derive the shared secret and compute master secret. */ 1922 if (!ssl_kex_derive_ecdhe_ecp(ecdh, ecdh_peer, &key, &key_len)) 1923 goto err; 1924 if (!tls12_derive_master_secret(s, key, key_len)) 1925 goto err; 1926 1927 EC_KEY_free(S3I(s)->tmp.ecdh); 1928 S3I(s)->tmp.ecdh = NULL; 1929 S3I(s)->tmp.ecdh_nid = NID_undef; 1930 1931 ret = 1; 1932 1933 err: 1934 freezero(key, key_len); 1935 EC_KEY_free(ecdh_peer); 1936 1937 return (ret); 1938 } 1939 1940 static int 1941 ssl3_get_client_kex_ecdhe_ecx(SSL *s, CBS *cbs) 1942 { 1943 uint8_t *shared_key = NULL; 1944 CBS ecpoint; 1945 int ret = -1; 1946 1947 if (!CBS_get_u8_length_prefixed(cbs, &ecpoint)) 1948 goto err; 1949 if (CBS_len(cbs) != 0) 1950 goto err; 1951 if (CBS_len(&ecpoint) != X25519_KEY_LENGTH) 1952 goto err; 1953 1954 if ((shared_key = malloc(X25519_KEY_LENGTH)) == NULL) 1955 goto err; 1956 if (!X25519(shared_key, S3I(s)->tmp.x25519, CBS_data(&ecpoint))) 1957 goto err; 1958 1959 freezero(S3I(s)->tmp.x25519, X25519_KEY_LENGTH); 1960 S3I(s)->tmp.x25519 = NULL; 1961 1962 if (!tls12_derive_master_secret(s, shared_key, X25519_KEY_LENGTH)) 1963 goto err; 1964 1965 ret = 1; 1966 1967 err: 1968 freezero(shared_key, X25519_KEY_LENGTH); 1969 1970 return (ret); 1971 } 1972 1973 static int 1974 ssl3_get_client_kex_ecdhe(SSL *s, CBS *cbs) 1975 { 1976 if (S3I(s)->tmp.x25519 != NULL) 1977 return ssl3_get_client_kex_ecdhe_ecx(s, cbs); 1978 1979 return ssl3_get_client_kex_ecdhe_ecp(s, cbs); 1980 } 1981 1982 static int 1983 ssl3_get_client_kex_gost(SSL *s, CBS *cbs) 1984 { 1985 EVP_PKEY_CTX *pkey_ctx; 1986 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; 1987 unsigned char premaster_secret[32]; 1988 unsigned long alg_a; 1989 size_t outlen = 32; 1990 CBS gostblob; 1991 int al; 1992 int ret = 0; 1993 1994 /* Get our certificate private key*/ 1995 alg_a = S3I(s)->hs.cipher->algorithm_auth; 1996 if (alg_a & SSL_aGOST01) 1997 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; 1998 1999 if ((pkey_ctx = EVP_PKEY_CTX_new(pk, NULL)) == NULL) 2000 goto err; 2001 if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) 2002 goto gerr; 2003 2004 /* 2005 * If client certificate is present and is of the same type, 2006 * maybe use it for key exchange. 2007 * Don't mind errors from EVP_PKEY_derive_set_peer, because 2008 * it is completely valid to use a client certificate for 2009 * authorization only. 2010 */ 2011 if ((client_pub_pkey = X509_get_pubkey(s->session->peer)) != NULL) { 2012 if (EVP_PKEY_derive_set_peer(pkey_ctx, 2013 client_pub_pkey) <= 0) 2014 ERR_clear_error(); 2015 } 2016 2017 /* Decrypt session key */ 2018 if (!CBS_get_asn1(cbs, &gostblob, CBS_ASN1_SEQUENCE)) 2019 goto decode_err; 2020 if (CBS_len(cbs) != 0) 2021 goto decode_err; 2022 if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, 2023 CBS_data(&gostblob), CBS_len(&gostblob)) <= 0) { 2024 SSLerror(s, SSL_R_DECRYPTION_FAILED); 2025 goto gerr; 2026 } 2027 2028 if (!tls12_derive_master_secret(s, premaster_secret, 32)) 2029 goto err; 2030 2031 /* Check if pubkey from client certificate was used */ 2032 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, 2033 EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) 2034 ret = 2; 2035 else 2036 ret = 1; 2037 gerr: 2038 EVP_PKEY_free(client_pub_pkey); 2039 EVP_PKEY_CTX_free(pkey_ctx); 2040 if (ret) 2041 return (ret); 2042 else 2043 goto err; 2044 2045 decode_err: 2046 al = SSL_AD_DECODE_ERROR; 2047 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2048 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2049 err: 2050 return (-1); 2051 } 2052 2053 int 2054 ssl3_get_client_key_exchange(SSL *s) 2055 { 2056 unsigned long alg_k; 2057 int al, ret; 2058 CBS cbs; 2059 2060 /* 2048 maxlen is a guess. How long a key does that permit? */ 2061 if ((ret = ssl3_get_message(s, SSL3_ST_SR_KEY_EXCH_A, 2062 SSL3_ST_SR_KEY_EXCH_B, SSL3_MT_CLIENT_KEY_EXCHANGE, 2048)) <= 0) 2063 return ret; 2064 2065 if (s->internal->init_num < 0) 2066 goto err; 2067 2068 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 2069 2070 alg_k = S3I(s)->hs.cipher->algorithm_mkey; 2071 2072 if (alg_k & SSL_kRSA) { 2073 if (ssl3_get_client_kex_rsa(s, &cbs) != 1) 2074 goto err; 2075 } else if (alg_k & SSL_kDHE) { 2076 if (ssl3_get_client_kex_dhe(s, &cbs) != 1) 2077 goto err; 2078 } else if (alg_k & SSL_kECDHE) { 2079 if (ssl3_get_client_kex_ecdhe(s, &cbs) != 1) 2080 goto err; 2081 } else if (alg_k & SSL_kGOST) { 2082 if (ssl3_get_client_kex_gost(s, &cbs) != 1) 2083 goto err; 2084 } else { 2085 al = SSL_AD_HANDSHAKE_FAILURE; 2086 SSLerror(s, SSL_R_UNKNOWN_CIPHER_TYPE); 2087 goto fatal_err; 2088 } 2089 2090 if (CBS_len(&cbs) != 0) { 2091 al = SSL_AD_DECODE_ERROR; 2092 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2093 goto fatal_err; 2094 } 2095 2096 return (1); 2097 2098 fatal_err: 2099 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2100 err: 2101 return (-1); 2102 } 2103 2104 int 2105 ssl3_get_cert_verify(SSL *s) 2106 { 2107 CBS cbs, signature; 2108 const struct ssl_sigalg *sigalg = NULL; 2109 uint16_t sigalg_value = SIGALG_NONE; 2110 EVP_PKEY *pkey = NULL; 2111 X509 *peer = NULL; 2112 EVP_MD_CTX mctx; 2113 int al, verify; 2114 const unsigned char *hdata; 2115 size_t hdatalen; 2116 int type = 0; 2117 int ret; 2118 2119 EVP_MD_CTX_init(&mctx); 2120 2121 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_VRFY_A, 2122 SSL3_ST_SR_CERT_VRFY_B, -1, SSL3_RT_MAX_PLAIN_LENGTH)) <= 0) 2123 return ret; 2124 2125 ret = 0; 2126 2127 if (s->internal->init_num < 0) 2128 goto err; 2129 2130 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 2131 2132 if (s->session->peer != NULL) { 2133 peer = s->session->peer; 2134 pkey = X509_get_pubkey(peer); 2135 type = X509_certificate_type(peer, pkey); 2136 } 2137 2138 if (S3I(s)->hs.tls12.message_type != SSL3_MT_CERTIFICATE_VERIFY) { 2139 S3I(s)->hs.tls12.reuse_message = 1; 2140 if (peer != NULL) { 2141 al = SSL_AD_UNEXPECTED_MESSAGE; 2142 SSLerror(s, SSL_R_MISSING_VERIFY_MESSAGE); 2143 goto fatal_err; 2144 } 2145 ret = 1; 2146 goto end; 2147 } 2148 2149 if (peer == NULL) { 2150 SSLerror(s, SSL_R_NO_CLIENT_CERT_RECEIVED); 2151 al = SSL_AD_UNEXPECTED_MESSAGE; 2152 goto fatal_err; 2153 } 2154 2155 if (!(type & EVP_PKT_SIGN)) { 2156 SSLerror(s, SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 2157 al = SSL_AD_ILLEGAL_PARAMETER; 2158 goto fatal_err; 2159 } 2160 2161 if (S3I(s)->change_cipher_spec) { 2162 SSLerror(s, SSL_R_CCS_RECEIVED_EARLY); 2163 al = SSL_AD_UNEXPECTED_MESSAGE; 2164 goto fatal_err; 2165 } 2166 2167 if (SSL_USE_SIGALGS(s)) { 2168 if (!CBS_get_u16(&cbs, &sigalg_value)) 2169 goto decode_err; 2170 } 2171 if (!CBS_get_u16_length_prefixed(&cbs, &signature)) 2172 goto err; 2173 if (CBS_len(&cbs) != 0) { 2174 al = SSL_AD_DECODE_ERROR; 2175 SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE); 2176 goto fatal_err; 2177 } 2178 2179 if (CBS_len(&signature) > EVP_PKEY_size(pkey)) { 2180 SSLerror(s, SSL_R_WRONG_SIGNATURE_SIZE); 2181 al = SSL_AD_DECODE_ERROR; 2182 goto fatal_err; 2183 } 2184 2185 if ((sigalg = ssl_sigalg_for_peer(s, pkey, 2186 sigalg_value)) == NULL) { 2187 al = SSL_AD_DECODE_ERROR; 2188 goto fatal_err; 2189 } 2190 S3I(s)->hs.peer_sigalg = sigalg; 2191 2192 if (SSL_USE_SIGALGS(s)) { 2193 EVP_PKEY_CTX *pctx; 2194 2195 if (!tls1_transcript_data(s, &hdata, &hdatalen)) { 2196 SSLerror(s, ERR_R_INTERNAL_ERROR); 2197 al = SSL_AD_INTERNAL_ERROR; 2198 goto fatal_err; 2199 } 2200 if (!EVP_DigestVerifyInit(&mctx, &pctx, sigalg->md(), 2201 NULL, pkey)) { 2202 SSLerror(s, ERR_R_EVP_LIB); 2203 al = SSL_AD_INTERNAL_ERROR; 2204 goto fatal_err; 2205 } 2206 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 2207 (!EVP_PKEY_CTX_set_rsa_padding(pctx, 2208 RSA_PKCS1_PSS_PADDING) || 2209 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 2210 al = SSL_AD_INTERNAL_ERROR; 2211 goto fatal_err; 2212 } 2213 if (sigalg->key_type == EVP_PKEY_GOSTR01 && 2214 EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_VERIFY, 2215 EVP_PKEY_CTRL_GOST_SIG_FORMAT, GOST_SIG_FORMAT_RS_LE, 2216 NULL) <= 0) { 2217 al = SSL_AD_INTERNAL_ERROR; 2218 goto fatal_err; 2219 } 2220 if (!EVP_DigestVerifyUpdate(&mctx, hdata, hdatalen)) { 2221 SSLerror(s, ERR_R_EVP_LIB); 2222 al = SSL_AD_INTERNAL_ERROR; 2223 goto fatal_err; 2224 } 2225 if (EVP_DigestVerifyFinal(&mctx, CBS_data(&signature), 2226 CBS_len(&signature)) <= 0) { 2227 al = SSL_AD_DECRYPT_ERROR; 2228 SSLerror(s, SSL_R_BAD_SIGNATURE); 2229 goto fatal_err; 2230 } 2231 } else if (pkey->type == EVP_PKEY_RSA) { 2232 verify = RSA_verify(NID_md5_sha1, S3I(s)->hs.tls12.cert_verify, 2233 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, CBS_data(&signature), 2234 CBS_len(&signature), pkey->pkey.rsa); 2235 if (verify < 0) { 2236 al = SSL_AD_DECRYPT_ERROR; 2237 SSLerror(s, SSL_R_BAD_RSA_DECRYPT); 2238 goto fatal_err; 2239 } 2240 if (verify == 0) { 2241 al = SSL_AD_DECRYPT_ERROR; 2242 SSLerror(s, SSL_R_BAD_RSA_SIGNATURE); 2243 goto fatal_err; 2244 } 2245 } else if (pkey->type == EVP_PKEY_EC) { 2246 verify = ECDSA_verify(pkey->save_type, 2247 &(S3I(s)->hs.tls12.cert_verify[MD5_DIGEST_LENGTH]), 2248 SHA_DIGEST_LENGTH, CBS_data(&signature), 2249 CBS_len(&signature), pkey->pkey.ec); 2250 if (verify <= 0) { 2251 al = SSL_AD_DECRYPT_ERROR; 2252 SSLerror(s, SSL_R_BAD_ECDSA_SIGNATURE); 2253 goto fatal_err; 2254 } 2255 #ifndef OPENSSL_NO_GOST 2256 } else if (pkey->type == NID_id_GostR3410_94 || 2257 pkey->type == NID_id_GostR3410_2001) { 2258 unsigned char sigbuf[128]; 2259 unsigned int siglen = sizeof(sigbuf); 2260 EVP_PKEY_CTX *pctx; 2261 const EVP_MD *md; 2262 int nid; 2263 2264 if (!tls1_transcript_data(s, &hdata, &hdatalen)) { 2265 SSLerror(s, ERR_R_INTERNAL_ERROR); 2266 al = SSL_AD_INTERNAL_ERROR; 2267 goto fatal_err; 2268 } 2269 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) || 2270 !(md = EVP_get_digestbynid(nid))) { 2271 SSLerror(s, ERR_R_EVP_LIB); 2272 al = SSL_AD_INTERNAL_ERROR; 2273 goto fatal_err; 2274 } 2275 if ((pctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) { 2276 SSLerror(s, ERR_R_EVP_LIB); 2277 al = SSL_AD_INTERNAL_ERROR; 2278 goto fatal_err; 2279 } 2280 if (!EVP_DigestInit_ex(&mctx, md, NULL) || 2281 !EVP_DigestUpdate(&mctx, hdata, hdatalen) || 2282 !EVP_DigestFinal(&mctx, sigbuf, &siglen) || 2283 (EVP_PKEY_verify_init(pctx) <= 0) || 2284 (EVP_PKEY_CTX_set_signature_md(pctx, md) <= 0) || 2285 (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_VERIFY, 2286 EVP_PKEY_CTRL_GOST_SIG_FORMAT, 2287 GOST_SIG_FORMAT_RS_LE, NULL) <= 0)) { 2288 SSLerror(s, ERR_R_EVP_LIB); 2289 al = SSL_AD_INTERNAL_ERROR; 2290 EVP_PKEY_CTX_free(pctx); 2291 goto fatal_err; 2292 } 2293 if (EVP_PKEY_verify(pctx, CBS_data(&signature), 2294 CBS_len(&signature), sigbuf, siglen) <= 0) { 2295 al = SSL_AD_DECRYPT_ERROR; 2296 SSLerror(s, SSL_R_BAD_SIGNATURE); 2297 EVP_PKEY_CTX_free(pctx); 2298 goto fatal_err; 2299 } 2300 2301 EVP_PKEY_CTX_free(pctx); 2302 #endif 2303 } else { 2304 SSLerror(s, ERR_R_INTERNAL_ERROR); 2305 al = SSL_AD_UNSUPPORTED_CERTIFICATE; 2306 goto fatal_err; 2307 } 2308 2309 ret = 1; 2310 if (0) { 2311 decode_err: 2312 al = SSL_AD_DECODE_ERROR; 2313 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2314 fatal_err: 2315 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2316 } 2317 end: 2318 tls1_transcript_free(s); 2319 err: 2320 EVP_MD_CTX_cleanup(&mctx); 2321 EVP_PKEY_free(pkey); 2322 return (ret); 2323 } 2324 2325 int 2326 ssl3_get_client_certificate(SSL *s) 2327 { 2328 CBS cbs, client_certs; 2329 X509 *x = NULL; 2330 const unsigned char *q; 2331 STACK_OF(X509) *sk = NULL; 2332 int i, al, ret; 2333 2334 if ((ret = ssl3_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, 2335 -1, s->internal->max_cert_list)) <= 0) 2336 return ret; 2337 2338 ret = -1; 2339 2340 if (S3I(s)->hs.tls12.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { 2341 if ((s->verify_mode & SSL_VERIFY_PEER) && 2342 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2343 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 2344 al = SSL_AD_HANDSHAKE_FAILURE; 2345 goto fatal_err; 2346 } 2347 /* 2348 * If tls asked for a client cert, 2349 * the client must return a 0 list. 2350 */ 2351 if (S3I(s)->hs.tls12.cert_request) { 2352 SSLerror(s, SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST 2353 ); 2354 al = SSL_AD_UNEXPECTED_MESSAGE; 2355 goto fatal_err; 2356 } 2357 S3I(s)->hs.tls12.reuse_message = 1; 2358 return (1); 2359 } 2360 2361 if (S3I(s)->hs.tls12.message_type != SSL3_MT_CERTIFICATE) { 2362 al = SSL_AD_UNEXPECTED_MESSAGE; 2363 SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE); 2364 goto fatal_err; 2365 } 2366 2367 if (s->internal->init_num < 0) 2368 goto decode_err; 2369 2370 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 2371 2372 if ((sk = sk_X509_new_null()) == NULL) { 2373 SSLerror(s, ERR_R_MALLOC_FAILURE); 2374 goto err; 2375 } 2376 2377 if (!CBS_get_u24_length_prefixed(&cbs, &client_certs) || 2378 CBS_len(&cbs) != 0) 2379 goto decode_err; 2380 2381 while (CBS_len(&client_certs) > 0) { 2382 CBS cert; 2383 2384 if (!CBS_get_u24_length_prefixed(&client_certs, &cert)) { 2385 al = SSL_AD_DECODE_ERROR; 2386 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH); 2387 goto fatal_err; 2388 } 2389 2390 q = CBS_data(&cert); 2391 x = d2i_X509(NULL, &q, CBS_len(&cert)); 2392 if (x == NULL) { 2393 SSLerror(s, ERR_R_ASN1_LIB); 2394 goto err; 2395 } 2396 if (q != CBS_data(&cert) + CBS_len(&cert)) { 2397 al = SSL_AD_DECODE_ERROR; 2398 SSLerror(s, SSL_R_CERT_LENGTH_MISMATCH); 2399 goto fatal_err; 2400 } 2401 if (!sk_X509_push(sk, x)) { 2402 SSLerror(s, ERR_R_MALLOC_FAILURE); 2403 goto err; 2404 } 2405 x = NULL; 2406 } 2407 2408 if (sk_X509_num(sk) <= 0) { 2409 /* 2410 * TLS does not mind 0 certs returned. 2411 * Fail for TLS only if we required a certificate. 2412 */ 2413 if ((s->verify_mode & SSL_VERIFY_PEER) && 2414 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 2415 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 2416 al = SSL_AD_HANDSHAKE_FAILURE; 2417 goto fatal_err; 2418 } 2419 /* No client certificate so free transcript. */ 2420 tls1_transcript_free(s); 2421 } else { 2422 i = ssl_verify_cert_chain(s, sk); 2423 if (i <= 0) { 2424 al = ssl_verify_alarm_type(s->verify_result); 2425 SSLerror(s, SSL_R_NO_CERTIFICATE_RETURNED); 2426 goto fatal_err; 2427 } 2428 } 2429 2430 X509_free(s->session->peer); 2431 s->session->peer = sk_X509_shift(sk); 2432 s->session->verify_result = s->verify_result; 2433 2434 /* 2435 * With the current implementation, sess_cert will always be NULL 2436 * when we arrive here 2437 */ 2438 if (SSI(s)->sess_cert == NULL) { 2439 SSI(s)->sess_cert = ssl_sess_cert_new(); 2440 if (SSI(s)->sess_cert == NULL) { 2441 SSLerror(s, ERR_R_MALLOC_FAILURE); 2442 goto err; 2443 } 2444 } 2445 sk_X509_pop_free(SSI(s)->sess_cert->cert_chain, X509_free); 2446 SSI(s)->sess_cert->cert_chain = sk; 2447 2448 /* 2449 * Inconsistency alert: cert_chain does *not* include the 2450 * peer's own certificate, while we do include it in s3_clnt.c 2451 */ 2452 2453 sk = NULL; 2454 2455 ret = 1; 2456 if (0) { 2457 decode_err: 2458 al = SSL_AD_DECODE_ERROR; 2459 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 2460 fatal_err: 2461 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2462 } 2463 err: 2464 X509_free(x); 2465 sk_X509_pop_free(sk, X509_free); 2466 2467 return (ret); 2468 } 2469 2470 int 2471 ssl3_send_server_certificate(SSL *s) 2472 { 2473 CBB cbb, server_cert; 2474 CERT_PKEY *cpk; 2475 2476 /* 2477 * Server Certificate - RFC 5246, section 7.4.2. 2478 */ 2479 2480 memset(&cbb, 0, sizeof(cbb)); 2481 2482 if (S3I(s)->hs.state == SSL3_ST_SW_CERT_A) { 2483 if ((cpk = ssl_get_server_send_pkey(s)) == NULL) { 2484 SSLerror(s, ERR_R_INTERNAL_ERROR); 2485 return (0); 2486 } 2487 2488 if (!ssl3_handshake_msg_start(s, &cbb, &server_cert, 2489 SSL3_MT_CERTIFICATE)) 2490 goto err; 2491 if (!ssl3_output_cert_chain(s, &server_cert, cpk)) 2492 goto err; 2493 if (!ssl3_handshake_msg_finish(s, &cbb)) 2494 goto err; 2495 2496 S3I(s)->hs.state = SSL3_ST_SW_CERT_B; 2497 } 2498 2499 /* SSL3_ST_SW_CERT_B */ 2500 return (ssl3_handshake_write(s)); 2501 2502 err: 2503 CBB_cleanup(&cbb); 2504 2505 return (0); 2506 } 2507 2508 /* send a new session ticket (not necessarily for a new session) */ 2509 int 2510 ssl3_send_newsession_ticket(SSL *s) 2511 { 2512 CBB cbb, session_ticket, ticket; 2513 SSL_CTX *tctx = s->initial_ctx; 2514 size_t enc_session_len, enc_session_max_len, hmac_len; 2515 size_t session_len = 0; 2516 unsigned char *enc_session = NULL, *session = NULL; 2517 unsigned char iv[EVP_MAX_IV_LENGTH]; 2518 unsigned char key_name[16]; 2519 unsigned char *hmac; 2520 unsigned int hlen; 2521 EVP_CIPHER_CTX ctx; 2522 HMAC_CTX hctx; 2523 int len; 2524 2525 /* 2526 * New Session Ticket - RFC 5077, section 3.3. 2527 */ 2528 2529 EVP_CIPHER_CTX_init(&ctx); 2530 HMAC_CTX_init(&hctx); 2531 2532 memset(&cbb, 0, sizeof(cbb)); 2533 2534 if (S3I(s)->hs.state == SSL3_ST_SW_SESSION_TICKET_A) { 2535 if (!ssl3_handshake_msg_start(s, &cbb, &session_ticket, 2536 SSL3_MT_NEWSESSION_TICKET)) 2537 goto err; 2538 2539 if (!SSL_SESSION_ticket(s->session, &session, &session_len)) 2540 goto err; 2541 if (session_len > 0xffff) 2542 goto err; 2543 2544 /* 2545 * Initialize HMAC and cipher contexts. If callback is present 2546 * it does all the work, otherwise use generated values from 2547 * parent context. 2548 */ 2549 if (tctx->internal->tlsext_ticket_key_cb != NULL) { 2550 if (tctx->internal->tlsext_ticket_key_cb(s, 2551 key_name, iv, &ctx, &hctx, 1) < 0) { 2552 EVP_CIPHER_CTX_cleanup(&ctx); 2553 goto err; 2554 } 2555 } else { 2556 arc4random_buf(iv, 16); 2557 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, 2558 tctx->internal->tlsext_tick_aes_key, iv); 2559 HMAC_Init_ex(&hctx, tctx->internal->tlsext_tick_hmac_key, 2560 16, EVP_sha256(), NULL); 2561 memcpy(key_name, tctx->internal->tlsext_tick_key_name, 16); 2562 } 2563 2564 /* Encrypt the session state. */ 2565 enc_session_max_len = session_len + EVP_MAX_BLOCK_LENGTH; 2566 if ((enc_session = calloc(1, enc_session_max_len)) == NULL) 2567 goto err; 2568 enc_session_len = 0; 2569 if (!EVP_EncryptUpdate(&ctx, enc_session, &len, session, 2570 session_len)) 2571 goto err; 2572 enc_session_len += len; 2573 if (!EVP_EncryptFinal_ex(&ctx, enc_session + enc_session_len, 2574 &len)) 2575 goto err; 2576 enc_session_len += len; 2577 2578 if (enc_session_len > enc_session_max_len) 2579 goto err; 2580 2581 /* Generate the HMAC. */ 2582 if (!HMAC_Update(&hctx, key_name, sizeof(key_name))) 2583 goto err; 2584 if (!HMAC_Update(&hctx, iv, EVP_CIPHER_CTX_iv_length(&ctx))) 2585 goto err; 2586 if (!HMAC_Update(&hctx, enc_session, enc_session_len)) 2587 goto err; 2588 2589 if ((hmac_len = HMAC_size(&hctx)) <= 0) 2590 goto err; 2591 2592 /* 2593 * Ticket lifetime hint (advisory only): 2594 * We leave this unspecified for resumed session 2595 * (for simplicity), and guess that tickets for new 2596 * sessions will live as long as their sessions. 2597 */ 2598 if (!CBB_add_u32(&session_ticket, 2599 s->internal->hit ? 0 : s->session->timeout)) 2600 goto err; 2601 2602 if (!CBB_add_u16_length_prefixed(&session_ticket, &ticket)) 2603 goto err; 2604 if (!CBB_add_bytes(&ticket, key_name, sizeof(key_name))) 2605 goto err; 2606 if (!CBB_add_bytes(&ticket, iv, EVP_CIPHER_CTX_iv_length(&ctx))) 2607 goto err; 2608 if (!CBB_add_bytes(&ticket, enc_session, enc_session_len)) 2609 goto err; 2610 if (!CBB_add_space(&ticket, &hmac, hmac_len)) 2611 goto err; 2612 2613 if (!HMAC_Final(&hctx, hmac, &hlen)) 2614 goto err; 2615 if (hlen != hmac_len) 2616 goto err; 2617 2618 if (!ssl3_handshake_msg_finish(s, &cbb)) 2619 goto err; 2620 2621 S3I(s)->hs.state = SSL3_ST_SW_SESSION_TICKET_B; 2622 } 2623 2624 EVP_CIPHER_CTX_cleanup(&ctx); 2625 HMAC_CTX_cleanup(&hctx); 2626 freezero(session, session_len); 2627 free(enc_session); 2628 2629 /* SSL3_ST_SW_SESSION_TICKET_B */ 2630 return (ssl3_handshake_write(s)); 2631 2632 err: 2633 CBB_cleanup(&cbb); 2634 EVP_CIPHER_CTX_cleanup(&ctx); 2635 HMAC_CTX_cleanup(&hctx); 2636 freezero(session, session_len); 2637 free(enc_session); 2638 2639 return (-1); 2640 } 2641 2642 int 2643 ssl3_send_cert_status(SSL *s) 2644 { 2645 CBB cbb, certstatus, ocspresp; 2646 2647 memset(&cbb, 0, sizeof(cbb)); 2648 2649 if (S3I(s)->hs.state == SSL3_ST_SW_CERT_STATUS_A) { 2650 if (!ssl3_handshake_msg_start(s, &cbb, &certstatus, 2651 SSL3_MT_CERTIFICATE_STATUS)) 2652 goto err; 2653 if (!CBB_add_u8(&certstatus, s->tlsext_status_type)) 2654 goto err; 2655 if (!CBB_add_u24_length_prefixed(&certstatus, &ocspresp)) 2656 goto err; 2657 if (!CBB_add_bytes(&ocspresp, s->internal->tlsext_ocsp_resp, 2658 s->internal->tlsext_ocsp_resp_len)) 2659 goto err; 2660 if (!ssl3_handshake_msg_finish(s, &cbb)) 2661 goto err; 2662 2663 S3I(s)->hs.state = SSL3_ST_SW_CERT_STATUS_B; 2664 } 2665 2666 /* SSL3_ST_SW_CERT_STATUS_B */ 2667 return (ssl3_handshake_write(s)); 2668 2669 err: 2670 CBB_cleanup(&cbb); 2671 2672 return (-1); 2673 } 2674