1 /* $OpenBSD: ssl_clnt.c,v 1.151 2022/07/03 14:52:39 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 <stdint.h> 153 #include <stdio.h> 154 155 #include <openssl/bn.h> 156 #include <openssl/buffer.h> 157 #include <openssl/curve25519.h> 158 #include <openssl/dh.h> 159 #include <openssl/evp.h> 160 #include <openssl/md5.h> 161 #include <openssl/objects.h> 162 #include <openssl/opensslconf.h> 163 164 #ifndef OPENSSL_NO_ENGINE 165 #include <openssl/engine.h> 166 #endif 167 #ifndef OPENSSL_NO_GOST 168 #include <openssl/gost.h> 169 #endif 170 171 #include "bytestring.h" 172 #include "dtls_locl.h" 173 #include "ssl_locl.h" 174 #include "ssl_sigalgs.h" 175 #include "ssl_tlsext.h" 176 177 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b); 178 179 int 180 ssl3_connect(SSL *s) 181 { 182 int new_state, state, skip = 0; 183 int ret = -1; 184 185 ERR_clear_error(); 186 errno = 0; 187 188 s->internal->in_handshake++; 189 if (!SSL_in_init(s) || SSL_in_before(s)) 190 SSL_clear(s); 191 192 for (;;) { 193 state = s->s3->hs.state; 194 195 switch (s->s3->hs.state) { 196 case SSL_ST_RENEGOTIATE: 197 s->internal->renegotiate = 1; 198 s->s3->hs.state = SSL_ST_CONNECT; 199 s->ctx->internal->stats.sess_connect_renegotiate++; 200 /* break */ 201 case SSL_ST_BEFORE: 202 case SSL_ST_CONNECT: 203 case SSL_ST_BEFORE|SSL_ST_CONNECT: 204 case SSL_ST_OK|SSL_ST_CONNECT: 205 206 s->server = 0; 207 208 ssl_info_callback(s, SSL_CB_HANDSHAKE_START, 1); 209 210 if (!ssl_legacy_stack_version(s, s->version)) { 211 SSLerror(s, ERR_R_INTERNAL_ERROR); 212 ret = -1; 213 goto end; 214 } 215 216 if (!ssl_supported_tls_version_range(s, 217 &s->s3->hs.our_min_tls_version, 218 &s->s3->hs.our_max_tls_version)) { 219 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 220 ret = -1; 221 goto end; 222 } 223 224 if (!ssl_security_version(s, 225 s->s3->hs.our_min_tls_version)) { 226 SSLerror(s, SSL_R_VERSION_TOO_LOW); 227 ret = -1; 228 goto end; 229 } 230 231 if (!ssl3_setup_init_buffer(s)) { 232 ret = -1; 233 goto end; 234 } 235 if (!ssl3_setup_buffers(s)) { 236 ret = -1; 237 goto end; 238 } 239 if (!ssl_init_wbio_buffer(s, 0)) { 240 ret = -1; 241 goto end; 242 } 243 244 /* don't push the buffering BIO quite yet */ 245 246 if (!tls1_transcript_init(s)) { 247 ret = -1; 248 goto end; 249 } 250 251 s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_A; 252 s->ctx->internal->stats.sess_connect++; 253 s->internal->init_num = 0; 254 255 if (SSL_is_dtls(s)) { 256 /* mark client_random uninitialized */ 257 memset(s->s3->client_random, 0, 258 sizeof(s->s3->client_random)); 259 s->d1->send_cookie = 0; 260 s->internal->hit = 0; 261 } 262 break; 263 264 case SSL3_ST_CW_CLNT_HELLO_A: 265 case SSL3_ST_CW_CLNT_HELLO_B: 266 s->internal->shutdown = 0; 267 268 if (SSL_is_dtls(s)) { 269 /* every DTLS ClientHello resets Finished MAC */ 270 tls1_transcript_reset(s); 271 272 dtls1_start_timer(s); 273 } 274 275 ret = ssl3_send_client_hello(s); 276 if (ret <= 0) 277 goto end; 278 279 if (SSL_is_dtls(s) && s->d1->send_cookie) { 280 s->s3->hs.state = SSL3_ST_CW_FLUSH; 281 s->s3->hs.tls12.next_state = SSL3_ST_CR_SRVR_HELLO_A; 282 } else 283 s->s3->hs.state = SSL3_ST_CR_SRVR_HELLO_A; 284 285 s->internal->init_num = 0; 286 287 /* turn on buffering for the next lot of output */ 288 if (s->bbio != s->wbio) 289 s->wbio = BIO_push(s->bbio, s->wbio); 290 291 break; 292 293 case SSL3_ST_CR_SRVR_HELLO_A: 294 case SSL3_ST_CR_SRVR_HELLO_B: 295 ret = ssl3_get_server_hello(s); 296 if (ret <= 0) 297 goto end; 298 299 if (s->internal->hit) { 300 s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 301 if (!SSL_is_dtls(s)) { 302 if (s->internal->tlsext_ticket_expected) { 303 /* receive renewed session ticket */ 304 s->s3->hs.state = SSL3_ST_CR_SESSION_TICKET_A; 305 } 306 307 /* No client certificate verification. */ 308 tls1_transcript_free(s); 309 } 310 } else if (SSL_is_dtls(s)) { 311 s->s3->hs.state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; 312 } else { 313 s->s3->hs.state = SSL3_ST_CR_CERT_A; 314 } 315 s->internal->init_num = 0; 316 break; 317 318 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: 319 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: 320 ret = ssl3_get_dtls_hello_verify(s); 321 if (ret <= 0) 322 goto end; 323 dtls1_stop_timer(s); 324 if (s->d1->send_cookie) /* start again, with a cookie */ 325 s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_A; 326 else 327 s->s3->hs.state = SSL3_ST_CR_CERT_A; 328 s->internal->init_num = 0; 329 break; 330 331 case SSL3_ST_CR_CERT_A: 332 case SSL3_ST_CR_CERT_B: 333 ret = ssl3_check_finished(s); 334 if (ret <= 0) 335 goto end; 336 if (ret == 2) { 337 s->internal->hit = 1; 338 if (s->internal->tlsext_ticket_expected) 339 s->s3->hs.state = SSL3_ST_CR_SESSION_TICKET_A; 340 else 341 s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 342 s->internal->init_num = 0; 343 break; 344 } 345 /* Check if it is anon DH/ECDH. */ 346 if (!(s->s3->hs.cipher->algorithm_auth & 347 SSL_aNULL)) { 348 ret = ssl3_get_server_certificate(s); 349 if (ret <= 0) 350 goto end; 351 if (s->internal->tlsext_status_expected) 352 s->s3->hs.state = SSL3_ST_CR_CERT_STATUS_A; 353 else 354 s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 355 } else { 356 skip = 1; 357 s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 358 } 359 s->internal->init_num = 0; 360 break; 361 362 case SSL3_ST_CR_KEY_EXCH_A: 363 case SSL3_ST_CR_KEY_EXCH_B: 364 ret = ssl3_get_server_key_exchange(s); 365 if (ret <= 0) 366 goto end; 367 s->s3->hs.state = SSL3_ST_CR_CERT_REQ_A; 368 s->internal->init_num = 0; 369 370 /* 371 * At this point we check that we have the 372 * required stuff from the server. 373 */ 374 if (!ssl3_check_cert_and_algorithm(s)) { 375 ret = -1; 376 goto end; 377 } 378 break; 379 380 case SSL3_ST_CR_CERT_REQ_A: 381 case SSL3_ST_CR_CERT_REQ_B: 382 ret = ssl3_get_certificate_request(s); 383 if (ret <= 0) 384 goto end; 385 s->s3->hs.state = SSL3_ST_CR_SRVR_DONE_A; 386 s->internal->init_num = 0; 387 break; 388 389 case SSL3_ST_CR_SRVR_DONE_A: 390 case SSL3_ST_CR_SRVR_DONE_B: 391 ret = ssl3_get_server_done(s); 392 if (ret <= 0) 393 goto end; 394 if (SSL_is_dtls(s)) 395 dtls1_stop_timer(s); 396 if (s->s3->hs.tls12.cert_request) 397 s->s3->hs.state = SSL3_ST_CW_CERT_A; 398 else 399 s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_A; 400 s->internal->init_num = 0; 401 402 break; 403 404 case SSL3_ST_CW_CERT_A: 405 case SSL3_ST_CW_CERT_B: 406 case SSL3_ST_CW_CERT_C: 407 case SSL3_ST_CW_CERT_D: 408 if (SSL_is_dtls(s)) 409 dtls1_start_timer(s); 410 ret = ssl3_send_client_certificate(s); 411 if (ret <= 0) 412 goto end; 413 s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_A; 414 s->internal->init_num = 0; 415 break; 416 417 case SSL3_ST_CW_KEY_EXCH_A: 418 case SSL3_ST_CW_KEY_EXCH_B: 419 if (SSL_is_dtls(s)) 420 dtls1_start_timer(s); 421 ret = ssl3_send_client_key_exchange(s); 422 if (ret <= 0) 423 goto end; 424 /* 425 * EAY EAY EAY need to check for DH fix cert 426 * sent back 427 */ 428 /* 429 * For TLS, cert_req is set to 2, so a cert chain 430 * of nothing is sent, but no verify packet is sent 431 */ 432 /* 433 * XXX: For now, we do not support client 434 * authentication in ECDH cipher suites with 435 * ECDH (rather than ECDSA) certificates. 436 * We need to skip the certificate verify 437 * message when client's ECDH public key is sent 438 * inside the client certificate. 439 */ 440 if (s->s3->hs.tls12.cert_request == 1) { 441 s->s3->hs.state = SSL3_ST_CW_CERT_VRFY_A; 442 } else { 443 s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 444 s->s3->change_cipher_spec = 0; 445 } 446 if (!SSL_is_dtls(s)) { 447 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { 448 s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 449 s->s3->change_cipher_spec = 0; 450 } 451 } 452 453 s->internal->init_num = 0; 454 break; 455 456 case SSL3_ST_CW_CERT_VRFY_A: 457 case SSL3_ST_CW_CERT_VRFY_B: 458 if (SSL_is_dtls(s)) 459 dtls1_start_timer(s); 460 ret = ssl3_send_client_verify(s); 461 if (ret <= 0) 462 goto end; 463 s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 464 s->internal->init_num = 0; 465 s->s3->change_cipher_spec = 0; 466 break; 467 468 case SSL3_ST_CW_CHANGE_A: 469 case SSL3_ST_CW_CHANGE_B: 470 if (SSL_is_dtls(s) && !s->internal->hit) 471 dtls1_start_timer(s); 472 ret = ssl3_send_change_cipher_spec(s, 473 SSL3_ST_CW_CHANGE_A, SSL3_ST_CW_CHANGE_B); 474 if (ret <= 0) 475 goto end; 476 477 s->s3->hs.state = SSL3_ST_CW_FINISHED_A; 478 s->internal->init_num = 0; 479 s->session->cipher = s->s3->hs.cipher; 480 481 if (!tls1_setup_key_block(s)) { 482 ret = -1; 483 goto end; 484 } 485 if (!tls1_change_write_cipher_state(s)) { 486 ret = -1; 487 goto end; 488 } 489 break; 490 491 case SSL3_ST_CW_FINISHED_A: 492 case SSL3_ST_CW_FINISHED_B: 493 if (SSL_is_dtls(s) && !s->internal->hit) 494 dtls1_start_timer(s); 495 ret = ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, 496 SSL3_ST_CW_FINISHED_B); 497 if (ret <= 0) 498 goto end; 499 if (!SSL_is_dtls(s)) 500 s->s3->flags |= SSL3_FLAGS_CCS_OK; 501 s->s3->hs.state = SSL3_ST_CW_FLUSH; 502 503 /* clear flags */ 504 if (s->internal->hit) { 505 s->s3->hs.tls12.next_state = SSL_ST_OK; 506 } else { 507 /* Allow NewSessionTicket if ticket expected */ 508 if (s->internal->tlsext_ticket_expected) 509 s->s3->hs.tls12.next_state = 510 SSL3_ST_CR_SESSION_TICKET_A; 511 else 512 s->s3->hs.tls12.next_state = 513 SSL3_ST_CR_FINISHED_A; 514 } 515 s->internal->init_num = 0; 516 break; 517 518 case SSL3_ST_CR_SESSION_TICKET_A: 519 case SSL3_ST_CR_SESSION_TICKET_B: 520 ret = ssl3_get_new_session_ticket(s); 521 if (ret <= 0) 522 goto end; 523 s->s3->hs.state = SSL3_ST_CR_FINISHED_A; 524 s->internal->init_num = 0; 525 break; 526 527 case SSL3_ST_CR_CERT_STATUS_A: 528 case SSL3_ST_CR_CERT_STATUS_B: 529 ret = ssl3_get_cert_status(s); 530 if (ret <= 0) 531 goto end; 532 s->s3->hs.state = SSL3_ST_CR_KEY_EXCH_A; 533 s->internal->init_num = 0; 534 break; 535 536 case SSL3_ST_CR_FINISHED_A: 537 case SSL3_ST_CR_FINISHED_B: 538 if (SSL_is_dtls(s)) 539 s->d1->change_cipher_spec_ok = 1; 540 else 541 s->s3->flags |= SSL3_FLAGS_CCS_OK; 542 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, 543 SSL3_ST_CR_FINISHED_B); 544 if (ret <= 0) 545 goto end; 546 if (SSL_is_dtls(s)) 547 dtls1_stop_timer(s); 548 549 if (s->internal->hit) 550 s->s3->hs.state = SSL3_ST_CW_CHANGE_A; 551 else 552 s->s3->hs.state = SSL_ST_OK; 553 s->internal->init_num = 0; 554 break; 555 556 case SSL3_ST_CW_FLUSH: 557 s->internal->rwstate = SSL_WRITING; 558 if (BIO_flush(s->wbio) <= 0) { 559 if (SSL_is_dtls(s)) { 560 /* If the write error was fatal, stop trying */ 561 if (!BIO_should_retry(s->wbio)) { 562 s->internal->rwstate = SSL_NOTHING; 563 s->s3->hs.state = s->s3->hs.tls12.next_state; 564 } 565 } 566 ret = -1; 567 goto end; 568 } 569 s->internal->rwstate = SSL_NOTHING; 570 s->s3->hs.state = s->s3->hs.tls12.next_state; 571 break; 572 573 case SSL_ST_OK: 574 /* clean a few things up */ 575 tls1_cleanup_key_block(s); 576 577 if (s->s3->handshake_transcript != NULL) { 578 SSLerror(s, ERR_R_INTERNAL_ERROR); 579 ret = -1; 580 goto end; 581 } 582 583 if (!SSL_is_dtls(s)) 584 ssl3_release_init_buffer(s); 585 586 ssl_free_wbio_buffer(s); 587 588 s->internal->init_num = 0; 589 s->internal->renegotiate = 0; 590 s->internal->new_session = 0; 591 592 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); 593 if (s->internal->hit) 594 s->ctx->internal->stats.sess_hit++; 595 596 ret = 1; 597 /* s->server=0; */ 598 s->internal->handshake_func = ssl3_connect; 599 s->ctx->internal->stats.sess_connect_good++; 600 601 ssl_info_callback(s, SSL_CB_HANDSHAKE_DONE, 1); 602 603 if (SSL_is_dtls(s)) { 604 /* done with handshaking */ 605 s->d1->handshake_read_seq = 0; 606 s->d1->next_handshake_write_seq = 0; 607 } 608 609 goto end; 610 /* break; */ 611 612 default: 613 SSLerror(s, SSL_R_UNKNOWN_STATE); 614 ret = -1; 615 goto end; 616 /* break; */ 617 } 618 619 /* did we do anything */ 620 if (!s->s3->hs.tls12.reuse_message && !skip) { 621 if (s->internal->debug) { 622 if ((ret = BIO_flush(s->wbio)) <= 0) 623 goto end; 624 } 625 626 if (s->s3->hs.state != state) { 627 new_state = s->s3->hs.state; 628 s->s3->hs.state = state; 629 ssl_info_callback(s, SSL_CB_CONNECT_LOOP, 1); 630 s->s3->hs.state = new_state; 631 } 632 } 633 skip = 0; 634 } 635 636 end: 637 s->internal->in_handshake--; 638 ssl_info_callback(s, SSL_CB_CONNECT_EXIT, ret); 639 640 return (ret); 641 } 642 643 int 644 ssl3_send_client_hello(SSL *s) 645 { 646 CBB cbb, client_hello, session_id, cookie, cipher_suites; 647 CBB compression_methods; 648 uint16_t max_version; 649 size_t sl; 650 651 memset(&cbb, 0, sizeof(cbb)); 652 653 if (s->s3->hs.state == SSL3_ST_CW_CLNT_HELLO_A) { 654 SSL_SESSION *sess = s->session; 655 656 if (!ssl_max_supported_version(s, &max_version)) { 657 SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); 658 return (-1); 659 } 660 s->version = max_version; 661 662 if (sess == NULL || sess->ssl_version != s->version || 663 (sess->session_id_length == 0 && sess->tlsext_tick == NULL) || 664 sess->not_resumable) { 665 if (!ssl_get_new_session(s, 0)) 666 goto err; 667 } 668 /* else use the pre-loaded session */ 669 670 /* 671 * If a DTLS ClientHello message is being resent after a 672 * HelloVerifyRequest, we must retain the original client 673 * random value. 674 */ 675 if (!SSL_is_dtls(s) || s->d1->send_cookie == 0) 676 arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE); 677 678 if (!ssl3_handshake_msg_start(s, &cbb, &client_hello, 679 SSL3_MT_CLIENT_HELLO)) 680 goto err; 681 682 if (!CBB_add_u16(&client_hello, s->version)) 683 goto err; 684 685 /* Random stuff */ 686 if (!CBB_add_bytes(&client_hello, s->s3->client_random, 687 sizeof(s->s3->client_random))) 688 goto err; 689 690 /* Session ID */ 691 if (!CBB_add_u8_length_prefixed(&client_hello, &session_id)) 692 goto err; 693 if (!s->internal->new_session && 694 s->session->session_id_length > 0) { 695 sl = s->session->session_id_length; 696 if (sl > sizeof(s->session->session_id)) { 697 SSLerror(s, ERR_R_INTERNAL_ERROR); 698 goto err; 699 } 700 if (!CBB_add_bytes(&session_id, 701 s->session->session_id, sl)) 702 goto err; 703 } 704 705 /* DTLS Cookie. */ 706 if (SSL_is_dtls(s)) { 707 if (s->d1->cookie_len > sizeof(s->d1->cookie)) { 708 SSLerror(s, ERR_R_INTERNAL_ERROR); 709 goto err; 710 } 711 if (!CBB_add_u8_length_prefixed(&client_hello, &cookie)) 712 goto err; 713 if (!CBB_add_bytes(&cookie, s->d1->cookie, 714 s->d1->cookie_len)) 715 goto err; 716 } 717 718 /* Ciphers supported */ 719 if (!CBB_add_u16_length_prefixed(&client_hello, &cipher_suites)) 720 return 0; 721 if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), 722 &cipher_suites)) { 723 SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE); 724 goto err; 725 } 726 727 /* Add in compression methods (null) */ 728 if (!CBB_add_u8_length_prefixed(&client_hello, 729 &compression_methods)) 730 goto err; 731 if (!CBB_add_u8(&compression_methods, 0)) 732 goto err; 733 734 /* TLS extensions */ 735 if (!tlsext_client_build(s, SSL_TLSEXT_MSG_CH, &client_hello)) { 736 SSLerror(s, ERR_R_INTERNAL_ERROR); 737 goto err; 738 } 739 740 if (!ssl3_handshake_msg_finish(s, &cbb)) 741 goto err; 742 743 s->s3->hs.state = SSL3_ST_CW_CLNT_HELLO_B; 744 } 745 746 /* SSL3_ST_CW_CLNT_HELLO_B */ 747 return (ssl3_handshake_write(s)); 748 749 err: 750 CBB_cleanup(&cbb); 751 752 return (-1); 753 } 754 755 int 756 ssl3_get_dtls_hello_verify(SSL *s) 757 { 758 CBS hello_verify_request, cookie; 759 size_t cookie_len; 760 uint16_t ssl_version; 761 int al, ret; 762 763 if ((ret = ssl3_get_message(s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, 764 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, -1, s->internal->max_cert_list)) <= 0) 765 return ret; 766 767 if (s->s3->hs.tls12.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) { 768 s->d1->send_cookie = 0; 769 s->s3->hs.tls12.reuse_message = 1; 770 return (1); 771 } 772 773 if (s->internal->init_num < 0) 774 goto decode_err; 775 776 CBS_init(&hello_verify_request, s->internal->init_msg, 777 s->internal->init_num); 778 779 if (!CBS_get_u16(&hello_verify_request, &ssl_version)) 780 goto decode_err; 781 if (!CBS_get_u8_length_prefixed(&hello_verify_request, &cookie)) 782 goto decode_err; 783 if (CBS_len(&hello_verify_request) != 0) 784 goto decode_err; 785 786 /* 787 * Per RFC 6347 section 4.2.1, the HelloVerifyRequest should always 788 * contain DTLSv1.0 the version that is going to be negotiated. 789 * Tolerate DTLSv1.2 just in case. 790 */ 791 if (ssl_version != DTLS1_VERSION && ssl_version != DTLS1_2_VERSION) { 792 SSLerror(s, SSL_R_WRONG_SSL_VERSION); 793 s->version = (s->version & 0xff00) | (ssl_version & 0xff); 794 al = SSL_AD_PROTOCOL_VERSION; 795 goto fatal_err; 796 } 797 798 if (!CBS_write_bytes(&cookie, s->d1->cookie, 799 sizeof(s->d1->cookie), &cookie_len)) { 800 s->d1->cookie_len = 0; 801 al = SSL_AD_ILLEGAL_PARAMETER; 802 goto fatal_err; 803 } 804 s->d1->cookie_len = cookie_len; 805 s->d1->send_cookie = 1; 806 807 return 1; 808 809 decode_err: 810 al = SSL_AD_DECODE_ERROR; 811 fatal_err: 812 ssl3_send_alert(s, SSL3_AL_FATAL, al); 813 return -1; 814 } 815 816 int 817 ssl3_get_server_hello(SSL *s) 818 { 819 CBS cbs, server_random, session_id; 820 uint16_t server_version, cipher_suite; 821 uint8_t compression_method; 822 const SSL_CIPHER *cipher; 823 const SSL_METHOD *method; 824 unsigned long alg_k; 825 int al, ret; 826 827 s->internal->first_packet = 1; 828 if ((ret = ssl3_get_message(s, SSL3_ST_CR_SRVR_HELLO_A, 829 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000 /* ?? */)) <= 0) 830 return ret; 831 s->internal->first_packet = 0; 832 833 if (s->internal->init_num < 0) 834 goto decode_err; 835 836 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 837 838 if (SSL_is_dtls(s)) { 839 if (s->s3->hs.tls12.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { 840 if (s->d1->send_cookie == 0) { 841 s->s3->hs.tls12.reuse_message = 1; 842 return (1); 843 } else { 844 /* Already sent a cookie. */ 845 al = SSL_AD_UNEXPECTED_MESSAGE; 846 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 847 goto fatal_err; 848 } 849 } 850 } 851 852 if (s->s3->hs.tls12.message_type != SSL3_MT_SERVER_HELLO) { 853 al = SSL_AD_UNEXPECTED_MESSAGE; 854 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 855 goto fatal_err; 856 } 857 858 if (!CBS_get_u16(&cbs, &server_version)) 859 goto decode_err; 860 861 if (!ssl_check_version_from_server(s, server_version)) { 862 SSLerror(s, SSL_R_WRONG_SSL_VERSION); 863 s->version = (s->version & 0xff00) | (server_version & 0xff); 864 al = SSL_AD_PROTOCOL_VERSION; 865 goto fatal_err; 866 } 867 s->s3->hs.peer_legacy_version = server_version; 868 s->version = server_version; 869 870 s->s3->hs.negotiated_tls_version = ssl_tls_version(server_version); 871 if (s->s3->hs.negotiated_tls_version == 0) { 872 SSLerror(s, ERR_R_INTERNAL_ERROR); 873 goto err; 874 } 875 876 if ((method = ssl_get_method(server_version)) == NULL) { 877 SSLerror(s, ERR_R_INTERNAL_ERROR); 878 goto err; 879 } 880 s->method = method; 881 882 /* Server random. */ 883 if (!CBS_get_bytes(&cbs, &server_random, SSL3_RANDOM_SIZE)) 884 goto decode_err; 885 if (!CBS_write_bytes(&server_random, s->s3->server_random, 886 sizeof(s->s3->server_random), NULL)) 887 goto err; 888 889 if (s->s3->hs.our_max_tls_version >= TLS1_2_VERSION && 890 s->s3->hs.negotiated_tls_version < s->s3->hs.our_max_tls_version) { 891 /* 892 * RFC 8446 section 4.1.3. We must not downgrade if the server 893 * random value contains the TLS 1.2 or TLS 1.1 magical value. 894 */ 895 if (!CBS_skip(&server_random, 896 CBS_len(&server_random) - sizeof(tls13_downgrade_12))) 897 goto err; 898 if (s->s3->hs.negotiated_tls_version == TLS1_2_VERSION && 899 CBS_mem_equal(&server_random, tls13_downgrade_12, 900 sizeof(tls13_downgrade_12))) { 901 al = SSL_AD_ILLEGAL_PARAMETER; 902 SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK); 903 goto fatal_err; 904 } 905 if (CBS_mem_equal(&server_random, tls13_downgrade_11, 906 sizeof(tls13_downgrade_11))) { 907 al = SSL_AD_ILLEGAL_PARAMETER; 908 SSLerror(s, SSL_R_INAPPROPRIATE_FALLBACK); 909 goto fatal_err; 910 } 911 } 912 913 /* Session ID. */ 914 if (!CBS_get_u8_length_prefixed(&cbs, &session_id)) 915 goto decode_err; 916 917 if (CBS_len(&session_id) > SSL3_SESSION_ID_SIZE) { 918 al = SSL_AD_ILLEGAL_PARAMETER; 919 SSLerror(s, SSL_R_SSL3_SESSION_ID_TOO_LONG); 920 goto fatal_err; 921 } 922 923 /* Cipher suite. */ 924 if (!CBS_get_u16(&cbs, &cipher_suite)) 925 goto decode_err; 926 927 /* 928 * Check if we want to resume the session based on external 929 * pre-shared secret. 930 */ 931 if (s->internal->tls_session_secret_cb != NULL) { 932 SSL_CIPHER *pref_cipher = NULL; 933 int master_key_length = sizeof(s->session->master_key); 934 935 if (!s->internal->tls_session_secret_cb(s, 936 s->session->master_key, &master_key_length, NULL, 937 &pref_cipher, s->internal->tls_session_secret_cb_arg)) { 938 SSLerror(s, ERR_R_INTERNAL_ERROR); 939 goto err; 940 } 941 if (master_key_length <= 0) { 942 SSLerror(s, ERR_R_INTERNAL_ERROR); 943 goto err; 944 } 945 s->session->master_key_length = master_key_length; 946 947 if ((s->session->cipher = pref_cipher) == NULL) 948 s->session->cipher = 949 ssl3_get_cipher_by_value(cipher_suite); 950 s->s3->flags |= SSL3_FLAGS_CCS_OK; 951 } 952 953 if (s->session->session_id_length != 0 && 954 CBS_mem_equal(&session_id, s->session->session_id, 955 s->session->session_id_length)) { 956 if (s->sid_ctx_length != s->session->sid_ctx_length || 957 timingsafe_memcmp(s->session->sid_ctx, 958 s->sid_ctx, s->sid_ctx_length) != 0) { 959 /* actually a client application bug */ 960 al = SSL_AD_ILLEGAL_PARAMETER; 961 SSLerror(s, SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 962 goto fatal_err; 963 } 964 s->s3->flags |= SSL3_FLAGS_CCS_OK; 965 s->internal->hit = 1; 966 } else { 967 /* a miss or crap from the other end */ 968 969 /* If we were trying for session-id reuse, make a new 970 * SSL_SESSION so we don't stuff up other people */ 971 s->internal->hit = 0; 972 if (s->session->session_id_length > 0) { 973 if (!ssl_get_new_session(s, 0)) { 974 al = SSL_AD_INTERNAL_ERROR; 975 goto fatal_err; 976 } 977 } 978 979 /* 980 * XXX - improve the handling for the case where there is a 981 * zero length session identifier. 982 */ 983 if (!CBS_write_bytes(&session_id, s->session->session_id, 984 sizeof(s->session->session_id), 985 &s->session->session_id_length)) 986 goto err; 987 988 s->session->ssl_version = s->version; 989 } 990 991 if ((cipher = ssl3_get_cipher_by_value(cipher_suite)) == NULL) { 992 al = SSL_AD_ILLEGAL_PARAMETER; 993 SSLerror(s, SSL_R_UNKNOWN_CIPHER_RETURNED); 994 goto fatal_err; 995 } 996 997 /* TLS v1.2 only ciphersuites require v1.2 or later. */ 998 if ((cipher->algorithm_ssl & SSL_TLSV1_2) && 999 s->s3->hs.negotiated_tls_version < TLS1_2_VERSION) { 1000 al = SSL_AD_ILLEGAL_PARAMETER; 1001 SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED); 1002 goto fatal_err; 1003 } 1004 1005 if (!ssl_cipher_in_list(SSL_get_ciphers(s), cipher)) { 1006 /* we did not say we would use this cipher */ 1007 al = SSL_AD_ILLEGAL_PARAMETER; 1008 SSLerror(s, SSL_R_WRONG_CIPHER_RETURNED); 1009 goto fatal_err; 1010 } 1011 1012 /* 1013 * Depending on the session caching (internal/external), the cipher 1014 * and/or cipher_id values may not be set. Make sure that 1015 * cipher_id is set and use it for comparison. 1016 */ 1017 if (s->session->cipher) 1018 s->session->cipher_id = s->session->cipher->id; 1019 if (s->internal->hit && (s->session->cipher_id != cipher->id)) { 1020 al = SSL_AD_ILLEGAL_PARAMETER; 1021 SSLerror(s, SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 1022 goto fatal_err; 1023 } 1024 s->s3->hs.cipher = cipher; 1025 1026 if (!tls1_transcript_hash_init(s)) 1027 goto err; 1028 1029 /* 1030 * Don't digest cached records if no sigalgs: we may need them for 1031 * client authentication. 1032 */ 1033 alg_k = s->s3->hs.cipher->algorithm_mkey; 1034 if (!(SSL_USE_SIGALGS(s) || (alg_k & SSL_kGOST))) 1035 tls1_transcript_free(s); 1036 1037 if (!CBS_get_u8(&cbs, &compression_method)) 1038 goto decode_err; 1039 1040 if (compression_method != 0) { 1041 al = SSL_AD_ILLEGAL_PARAMETER; 1042 SSLerror(s, SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 1043 goto fatal_err; 1044 } 1045 1046 if (!tlsext_client_parse(s, SSL_TLSEXT_MSG_SH, &cbs, &al)) { 1047 SSLerror(s, SSL_R_PARSE_TLSEXT); 1048 goto fatal_err; 1049 } 1050 1051 if (CBS_len(&cbs) != 0) 1052 goto decode_err; 1053 1054 /* 1055 * Determine if we need to see RI. Strictly speaking if we want to 1056 * avoid an attack we should *always* see RI even on initial server 1057 * hello because the client doesn't see any renegotiation during an 1058 * attack. However this would mean we could not connect to any server 1059 * which doesn't support RI so for the immediate future tolerate RI 1060 * absence on initial connect only. 1061 */ 1062 if (!s->s3->renegotiate_seen && 1063 !(s->internal->options & SSL_OP_LEGACY_SERVER_CONNECT)) { 1064 al = SSL_AD_HANDSHAKE_FAILURE; 1065 SSLerror(s, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 1066 goto fatal_err; 1067 } 1068 1069 if (ssl_check_serverhello_tlsext(s) <= 0) { 1070 SSLerror(s, SSL_R_SERVERHELLO_TLSEXT); 1071 goto err; 1072 } 1073 1074 return (1); 1075 1076 decode_err: 1077 /* wrong packet length */ 1078 al = SSL_AD_DECODE_ERROR; 1079 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1080 fatal_err: 1081 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1082 err: 1083 return (-1); 1084 } 1085 1086 int 1087 ssl3_get_server_certificate(SSL *s) 1088 { 1089 CBS cbs, cert_list, cert_data; 1090 STACK_OF(X509) *certs = NULL; 1091 X509 *cert = NULL; 1092 const uint8_t *p; 1093 EVP_PKEY *pkey; 1094 int cert_type; 1095 int al, ret; 1096 1097 if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_A, 1098 SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list)) <= 0) 1099 return ret; 1100 1101 ret = -1; 1102 1103 if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { 1104 s->s3->hs.tls12.reuse_message = 1; 1105 return (1); 1106 } 1107 1108 if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE) { 1109 al = SSL_AD_UNEXPECTED_MESSAGE; 1110 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1111 goto fatal_err; 1112 } 1113 1114 if ((certs = sk_X509_new_null()) == NULL) { 1115 SSLerror(s, ERR_R_MALLOC_FAILURE); 1116 goto err; 1117 } 1118 1119 if (s->internal->init_num < 0) 1120 goto decode_err; 1121 1122 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 1123 1124 if (!CBS_get_u24_length_prefixed(&cbs, &cert_list)) 1125 goto decode_err; 1126 if (CBS_len(&cbs) != 0) 1127 goto decode_err; 1128 1129 while (CBS_len(&cert_list) > 0) { 1130 if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data)) 1131 goto decode_err; 1132 p = CBS_data(&cert_data); 1133 if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL) { 1134 al = SSL_AD_BAD_CERTIFICATE; 1135 SSLerror(s, ERR_R_ASN1_LIB); 1136 goto fatal_err; 1137 } 1138 if (p != CBS_data(&cert_data) + CBS_len(&cert_data)) 1139 goto decode_err; 1140 if (!sk_X509_push(certs, cert)) { 1141 SSLerror(s, ERR_R_MALLOC_FAILURE); 1142 goto err; 1143 } 1144 cert = NULL; 1145 } 1146 1147 /* A server must always provide a non-empty certificate list. */ 1148 if (sk_X509_num(certs) < 1) { 1149 SSLerror(s, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 1150 goto decode_err; 1151 } 1152 1153 if (ssl_verify_cert_chain(s, certs) <= 0 && 1154 s->verify_mode != SSL_VERIFY_NONE) { 1155 al = ssl_verify_alarm_type(s->verify_result); 1156 SSLerror(s, SSL_R_CERTIFICATE_VERIFY_FAILED); 1157 goto fatal_err; 1158 } 1159 ERR_clear_error(); /* but we keep s->verify_result */ 1160 1161 /* 1162 * Inconsistency alert: cert_chain does include the peer's 1163 * certificate, which we don't include in s3_srvr.c 1164 */ 1165 cert = sk_X509_value(certs, 0); 1166 X509_up_ref(cert); 1167 1168 if ((pkey = X509_get0_pubkey(cert)) == NULL || 1169 EVP_PKEY_missing_parameters(pkey)) { 1170 al = SSL3_AL_FATAL; 1171 SSLerror(s, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 1172 goto fatal_err; 1173 } 1174 if ((cert_type = ssl_cert_type(pkey)) < 0) { 1175 al = SSL3_AL_FATAL; 1176 SSLerror(s, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 1177 goto fatal_err; 1178 } 1179 1180 X509_free(s->session->peer_cert); 1181 X509_up_ref(cert); 1182 s->session->peer_cert = cert; 1183 s->session->peer_cert_type = cert_type; 1184 1185 s->session->verify_result = s->verify_result; 1186 1187 sk_X509_pop_free(s->session->cert_chain, X509_free); 1188 s->session->cert_chain = certs; 1189 certs = NULL; 1190 1191 ret = 1; 1192 1193 if (0) { 1194 decode_err: 1195 /* wrong packet length */ 1196 al = SSL_AD_DECODE_ERROR; 1197 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1198 fatal_err: 1199 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1200 } 1201 err: 1202 sk_X509_pop_free(certs, X509_free); 1203 X509_free(cert); 1204 1205 return (ret); 1206 } 1207 1208 static int 1209 ssl3_get_server_kex_dhe(SSL *s, CBS *cbs) 1210 { 1211 int decode_error, invalid_params, invalid_key; 1212 int nid = NID_dhKeyAgreement; 1213 1214 tls_key_share_free(s->s3->hs.key_share); 1215 if ((s->s3->hs.key_share = tls_key_share_new_nid(nid)) == NULL) 1216 goto err; 1217 1218 if (!tls_key_share_peer_params(s->s3->hs.key_share, cbs, 1219 &decode_error, &invalid_params)) { 1220 if (decode_error) { 1221 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1222 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1223 } 1224 goto err; 1225 } 1226 if (!tls_key_share_peer_public(s->s3->hs.key_share, cbs, 1227 &decode_error, &invalid_key)) { 1228 if (decode_error) { 1229 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1230 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1231 } 1232 goto err; 1233 } 1234 1235 if (invalid_params) { 1236 SSLerror(s, SSL_R_BAD_DH_P_LENGTH); 1237 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1238 goto err; 1239 } 1240 if (invalid_key) { 1241 SSLerror(s, SSL_R_BAD_DH_PUB_KEY_LENGTH); 1242 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1243 goto err; 1244 } 1245 1246 if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 1247 SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 1248 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1249 return 0; 1250 } 1251 1252 return 1; 1253 1254 err: 1255 return 0; 1256 } 1257 1258 static int 1259 ssl3_get_server_kex_ecdhe(SSL *s, CBS *cbs) 1260 { 1261 uint8_t curve_type; 1262 uint16_t group_id; 1263 int decode_error; 1264 CBS public; 1265 1266 if (!CBS_get_u8(cbs, &curve_type)) 1267 goto decode_err; 1268 if (!CBS_get_u16(cbs, &group_id)) 1269 goto decode_err; 1270 1271 /* Only named curves are supported. */ 1272 if (curve_type != NAMED_CURVE_TYPE) { 1273 SSLerror(s, SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1274 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1275 goto err; 1276 } 1277 1278 if (!CBS_get_u8_length_prefixed(cbs, &public)) 1279 goto decode_err; 1280 1281 /* 1282 * Check that the group is one of our preferences - if it is not, 1283 * the server has sent us an invalid group. 1284 */ 1285 if (!tls1_check_group(s, group_id)) { 1286 SSLerror(s, SSL_R_WRONG_CURVE); 1287 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); 1288 goto err; 1289 } 1290 1291 tls_key_share_free(s->s3->hs.key_share); 1292 if ((s->s3->hs.key_share = tls_key_share_new(group_id)) == NULL) 1293 goto err; 1294 1295 if (!tls_key_share_peer_public(s->s3->hs.key_share, &public, 1296 &decode_error, NULL)) { 1297 if (decode_error) 1298 goto decode_err; 1299 goto err; 1300 } 1301 1302 return 1; 1303 1304 decode_err: 1305 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1306 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1307 err: 1308 return 0; 1309 } 1310 1311 int 1312 ssl3_get_server_key_exchange(SSL *s) 1313 { 1314 CBS cbs, signature; 1315 EVP_MD_CTX *md_ctx; 1316 const unsigned char *param; 1317 size_t param_len; 1318 long alg_k, alg_a; 1319 int al, ret; 1320 1321 alg_k = s->s3->hs.cipher->algorithm_mkey; 1322 alg_a = s->s3->hs.cipher->algorithm_auth; 1323 1324 /* 1325 * Use same message size as in ssl3_get_certificate_request() 1326 * as ServerKeyExchange message may be skipped. 1327 */ 1328 if ((ret = ssl3_get_message(s, SSL3_ST_CR_KEY_EXCH_A, 1329 SSL3_ST_CR_KEY_EXCH_B, -1, s->internal->max_cert_list)) <= 0) 1330 return ret; 1331 1332 if ((md_ctx = EVP_MD_CTX_new()) == NULL) 1333 goto err; 1334 1335 if (s->internal->init_num < 0) 1336 goto err; 1337 1338 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 1339 1340 if (s->s3->hs.tls12.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { 1341 /* 1342 * Do not skip server key exchange if this cipher suite uses 1343 * ephemeral keys. 1344 */ 1345 if (alg_k & (SSL_kDHE|SSL_kECDHE)) { 1346 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 1347 al = SSL_AD_UNEXPECTED_MESSAGE; 1348 goto fatal_err; 1349 } 1350 1351 s->s3->hs.tls12.reuse_message = 1; 1352 EVP_MD_CTX_free(md_ctx); 1353 return (1); 1354 } 1355 1356 param = CBS_data(&cbs); 1357 param_len = CBS_len(&cbs); 1358 1359 if (alg_k & SSL_kDHE) { 1360 if (!ssl3_get_server_kex_dhe(s, &cbs)) 1361 goto err; 1362 } else if (alg_k & SSL_kECDHE) { 1363 if (!ssl3_get_server_kex_ecdhe(s, &cbs)) 1364 goto err; 1365 } else if (alg_k != 0) { 1366 al = SSL_AD_UNEXPECTED_MESSAGE; 1367 SSLerror(s, SSL_R_UNEXPECTED_MESSAGE); 1368 goto fatal_err; 1369 } 1370 1371 param_len -= CBS_len(&cbs); 1372 1373 /* if it was signed, check the signature */ 1374 if ((alg_a & SSL_aNULL) == 0) { 1375 uint16_t sigalg_value = SIGALG_NONE; 1376 const struct ssl_sigalg *sigalg; 1377 EVP_PKEY_CTX *pctx; 1378 EVP_PKEY *pkey = NULL; 1379 1380 if ((alg_a & SSL_aRSA) != 0 && 1381 s->session->peer_cert_type == SSL_PKEY_RSA) { 1382 pkey = X509_get0_pubkey(s->session->peer_cert); 1383 } else if ((alg_a & SSL_aECDSA) != 0 && 1384 s->session->peer_cert_type == SSL_PKEY_ECC) { 1385 pkey = X509_get0_pubkey(s->session->peer_cert); 1386 } 1387 if (pkey == NULL) { 1388 al = SSL_AD_ILLEGAL_PARAMETER; 1389 SSLerror(s, SSL_R_UNKNOWN_CERTIFICATE_TYPE); 1390 goto fatal_err; 1391 } 1392 1393 if (SSL_USE_SIGALGS(s)) { 1394 if (!CBS_get_u16(&cbs, &sigalg_value)) 1395 goto decode_err; 1396 } 1397 if (!CBS_get_u16_length_prefixed(&cbs, &signature)) 1398 goto decode_err; 1399 if (CBS_len(&signature) > EVP_PKEY_size(pkey)) { 1400 al = SSL_AD_DECODE_ERROR; 1401 SSLerror(s, SSL_R_WRONG_SIGNATURE_LENGTH); 1402 goto fatal_err; 1403 } 1404 1405 if ((sigalg = ssl_sigalg_for_peer(s, pkey, 1406 sigalg_value)) == NULL) { 1407 al = SSL_AD_DECODE_ERROR; 1408 goto fatal_err; 1409 } 1410 s->s3->hs.peer_sigalg = sigalg; 1411 1412 if (!EVP_DigestVerifyInit(md_ctx, &pctx, sigalg->md(), 1413 NULL, pkey)) 1414 goto err; 1415 if (!EVP_DigestVerifyUpdate(md_ctx, s->s3->client_random, 1416 SSL3_RANDOM_SIZE)) 1417 goto err; 1418 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 1419 (!EVP_PKEY_CTX_set_rsa_padding(pctx, 1420 RSA_PKCS1_PSS_PADDING) || 1421 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) 1422 goto err; 1423 if (!EVP_DigestVerifyUpdate(md_ctx, s->s3->server_random, 1424 SSL3_RANDOM_SIZE)) 1425 goto err; 1426 if (!EVP_DigestVerifyUpdate(md_ctx, param, param_len)) 1427 goto err; 1428 if (EVP_DigestVerifyFinal(md_ctx, CBS_data(&signature), 1429 CBS_len(&signature)) <= 0) { 1430 al = SSL_AD_DECRYPT_ERROR; 1431 SSLerror(s, SSL_R_BAD_SIGNATURE); 1432 goto fatal_err; 1433 } 1434 } 1435 1436 if (CBS_len(&cbs) != 0) { 1437 al = SSL_AD_DECODE_ERROR; 1438 SSLerror(s, SSL_R_EXTRA_DATA_IN_MESSAGE); 1439 goto fatal_err; 1440 } 1441 1442 EVP_MD_CTX_free(md_ctx); 1443 1444 return (1); 1445 1446 decode_err: 1447 al = SSL_AD_DECODE_ERROR; 1448 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1449 1450 fatal_err: 1451 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1452 1453 err: 1454 EVP_MD_CTX_free(md_ctx); 1455 1456 return (-1); 1457 } 1458 1459 int 1460 ssl3_get_certificate_request(SSL *s) 1461 { 1462 CBS cert_request, cert_types, rdn_list; 1463 X509_NAME *xn = NULL; 1464 const unsigned char *q; 1465 STACK_OF(X509_NAME) *ca_sk = NULL; 1466 int ret; 1467 1468 if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_REQ_A, 1469 SSL3_ST_CR_CERT_REQ_B, -1, s->internal->max_cert_list)) <= 0) 1470 return ret; 1471 1472 ret = 0; 1473 1474 s->s3->hs.tls12.cert_request = 0; 1475 1476 if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_DONE) { 1477 s->s3->hs.tls12.reuse_message = 1; 1478 /* 1479 * If we get here we don't need any cached handshake records 1480 * as we wont be doing client auth. 1481 */ 1482 tls1_transcript_free(s); 1483 return (1); 1484 } 1485 1486 if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_REQUEST) { 1487 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 1488 SSLerror(s, SSL_R_WRONG_MESSAGE_TYPE); 1489 goto err; 1490 } 1491 1492 /* TLS does not like anon-DH with client cert */ 1493 if (s->s3->hs.cipher->algorithm_auth & SSL_aNULL) { 1494 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 1495 SSLerror(s, SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 1496 goto err; 1497 } 1498 1499 if (s->internal->init_num < 0) 1500 goto decode_err; 1501 CBS_init(&cert_request, s->internal->init_msg, s->internal->init_num); 1502 1503 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { 1504 SSLerror(s, ERR_R_MALLOC_FAILURE); 1505 goto err; 1506 } 1507 1508 if (!CBS_get_u8_length_prefixed(&cert_request, &cert_types)) 1509 goto decode_err; 1510 1511 if (SSL_USE_SIGALGS(s)) { 1512 CBS sigalgs; 1513 1514 if (CBS_len(&cert_request) < 2) { 1515 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 1516 goto err; 1517 } 1518 if (!CBS_get_u16_length_prefixed(&cert_request, &sigalgs)) { 1519 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1520 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 1521 goto err; 1522 } 1523 if (CBS_len(&sigalgs) % 2 != 0 || CBS_len(&sigalgs) > 64) { 1524 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1525 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 1526 goto err; 1527 } 1528 if (!CBS_stow(&sigalgs, &s->s3->hs.sigalgs, 1529 &s->s3->hs.sigalgs_len)) 1530 goto err; 1531 } 1532 1533 /* get the CA RDNs */ 1534 if (CBS_len(&cert_request) < 2) { 1535 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 1536 goto err; 1537 } 1538 1539 if (!CBS_get_u16_length_prefixed(&cert_request, &rdn_list) || 1540 CBS_len(&cert_request) != 0) { 1541 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1542 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1543 goto err; 1544 } 1545 1546 while (CBS_len(&rdn_list) > 0) { 1547 CBS rdn; 1548 1549 if (CBS_len(&rdn_list) < 2) { 1550 SSLerror(s, SSL_R_DATA_LENGTH_TOO_LONG); 1551 goto err; 1552 } 1553 1554 if (!CBS_get_u16_length_prefixed(&rdn_list, &rdn)) { 1555 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1556 SSLerror(s, SSL_R_CA_DN_TOO_LONG); 1557 goto err; 1558 } 1559 1560 q = CBS_data(&rdn); 1561 if ((xn = d2i_X509_NAME(NULL, &q, CBS_len(&rdn))) == NULL) { 1562 ssl3_send_alert(s, SSL3_AL_FATAL, 1563 SSL_AD_DECODE_ERROR); 1564 SSLerror(s, ERR_R_ASN1_LIB); 1565 goto err; 1566 } 1567 1568 if (q != CBS_data(&rdn) + CBS_len(&rdn)) { 1569 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1570 SSLerror(s, SSL_R_CA_DN_LENGTH_MISMATCH); 1571 goto err; 1572 } 1573 if (!sk_X509_NAME_push(ca_sk, xn)) { 1574 SSLerror(s, ERR_R_MALLOC_FAILURE); 1575 goto err; 1576 } 1577 xn = NULL; /* avoid free in err block */ 1578 } 1579 1580 /* we should setup a certificate to return.... */ 1581 s->s3->hs.tls12.cert_request = 1; 1582 sk_X509_NAME_pop_free(s->s3->hs.tls12.ca_names, X509_NAME_free); 1583 s->s3->hs.tls12.ca_names = ca_sk; 1584 ca_sk = NULL; 1585 1586 ret = 1; 1587 if (0) { 1588 decode_err: 1589 SSLerror(s, SSL_R_BAD_PACKET_LENGTH); 1590 } 1591 err: 1592 X509_NAME_free(xn); 1593 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); 1594 return (ret); 1595 } 1596 1597 static int 1598 ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 1599 { 1600 return (X509_NAME_cmp(*a, *b)); 1601 } 1602 1603 int 1604 ssl3_get_new_session_ticket(SSL *s) 1605 { 1606 uint32_t lifetime_hint; 1607 CBS cbs, session_ticket; 1608 unsigned int session_id_length = 0; 1609 int al, ret; 1610 1611 if ((ret = ssl3_get_message(s, SSL3_ST_CR_SESSION_TICKET_A, 1612 SSL3_ST_CR_SESSION_TICKET_B, -1, 16384)) <= 0) 1613 return ret; 1614 1615 if (s->s3->hs.tls12.message_type == SSL3_MT_FINISHED) { 1616 s->s3->hs.tls12.reuse_message = 1; 1617 return (1); 1618 } 1619 if (s->s3->hs.tls12.message_type != SSL3_MT_NEWSESSION_TICKET) { 1620 al = SSL_AD_UNEXPECTED_MESSAGE; 1621 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1622 goto fatal_err; 1623 } 1624 1625 if (s->internal->init_num < 0) { 1626 al = SSL_AD_DECODE_ERROR; 1627 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1628 goto fatal_err; 1629 } 1630 1631 CBS_init(&cbs, s->internal->init_msg, s->internal->init_num); 1632 if (!CBS_get_u32(&cbs, &lifetime_hint) || 1633 !CBS_get_u16_length_prefixed(&cbs, &session_ticket) || 1634 CBS_len(&cbs) != 0) { 1635 al = SSL_AD_DECODE_ERROR; 1636 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1637 goto fatal_err; 1638 } 1639 s->session->tlsext_tick_lifetime_hint = lifetime_hint; 1640 1641 if (!CBS_stow(&session_ticket, &s->session->tlsext_tick, 1642 &s->session->tlsext_ticklen)) { 1643 SSLerror(s, ERR_R_MALLOC_FAILURE); 1644 goto err; 1645 } 1646 1647 /* 1648 * There are two ways to detect a resumed ticket sesion. 1649 * One is to set an appropriate session ID and then the server 1650 * must return a match in ServerHello. This allows the normal 1651 * client session ID matching to work and we know much 1652 * earlier that the ticket has been accepted. 1653 * 1654 * The other way is to set zero length session ID when the 1655 * ticket is presented and rely on the handshake to determine 1656 * session resumption. 1657 * 1658 * We choose the former approach because this fits in with 1659 * assumptions elsewhere in OpenSSL. The session ID is set 1660 * to the SHA256 hash of the ticket. 1661 */ 1662 if (!EVP_Digest(CBS_data(&session_ticket), CBS_len(&session_ticket), 1663 s->session->session_id, &session_id_length, EVP_sha256(), NULL)) { 1664 al = SSL_AD_INTERNAL_ERROR; 1665 SSLerror(s, ERR_R_EVP_LIB); 1666 goto fatal_err; 1667 } 1668 s->session->session_id_length = session_id_length; 1669 1670 return (1); 1671 1672 fatal_err: 1673 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1674 err: 1675 return (-1); 1676 } 1677 1678 int 1679 ssl3_get_cert_status(SSL *s) 1680 { 1681 CBS cert_status, response; 1682 uint8_t status_type; 1683 int al, ret; 1684 1685 if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_STATUS_A, 1686 SSL3_ST_CR_CERT_STATUS_B, -1, 16384)) <= 0) 1687 return ret; 1688 1689 if (s->s3->hs.tls12.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) { 1690 /* 1691 * Tell the callback the server did not send us an OSCP 1692 * response, and has decided to head directly to key exchange. 1693 */ 1694 if (s->ctx->internal->tlsext_status_cb) { 1695 free(s->internal->tlsext_ocsp_resp); 1696 s->internal->tlsext_ocsp_resp = NULL; 1697 s->internal->tlsext_ocsp_resp_len = 0; 1698 1699 ret = s->ctx->internal->tlsext_status_cb(s, 1700 s->ctx->internal->tlsext_status_arg); 1701 if (ret == 0) { 1702 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 1703 SSLerror(s, SSL_R_INVALID_STATUS_RESPONSE); 1704 goto fatal_err; 1705 } 1706 if (ret < 0) { 1707 al = SSL_AD_INTERNAL_ERROR; 1708 SSLerror(s, ERR_R_MALLOC_FAILURE); 1709 goto fatal_err; 1710 } 1711 } 1712 s->s3->hs.tls12.reuse_message = 1; 1713 return (1); 1714 } 1715 1716 if (s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE && 1717 s->s3->hs.tls12.message_type != SSL3_MT_CERTIFICATE_STATUS) { 1718 al = SSL_AD_UNEXPECTED_MESSAGE; 1719 SSLerror(s, SSL_R_BAD_MESSAGE_TYPE); 1720 goto fatal_err; 1721 } 1722 1723 if (s->internal->init_num < 0) { 1724 /* need at least status type + length */ 1725 al = SSL_AD_DECODE_ERROR; 1726 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1727 goto fatal_err; 1728 } 1729 1730 CBS_init(&cert_status, s->internal->init_msg, s->internal->init_num); 1731 if (!CBS_get_u8(&cert_status, &status_type) || 1732 CBS_len(&cert_status) < 3) { 1733 /* need at least status type + length */ 1734 al = SSL_AD_DECODE_ERROR; 1735 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1736 goto fatal_err; 1737 } 1738 1739 if (status_type != TLSEXT_STATUSTYPE_ocsp) { 1740 al = SSL_AD_DECODE_ERROR; 1741 SSLerror(s, SSL_R_UNSUPPORTED_STATUS_TYPE); 1742 goto fatal_err; 1743 } 1744 1745 if (!CBS_get_u24_length_prefixed(&cert_status, &response) || 1746 CBS_len(&cert_status) != 0) { 1747 al = SSL_AD_DECODE_ERROR; 1748 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1749 goto fatal_err; 1750 } 1751 1752 if (!CBS_stow(&response, &s->internal->tlsext_ocsp_resp, 1753 &s->internal->tlsext_ocsp_resp_len)) { 1754 al = SSL_AD_INTERNAL_ERROR; 1755 SSLerror(s, ERR_R_MALLOC_FAILURE); 1756 goto fatal_err; 1757 } 1758 1759 if (s->ctx->internal->tlsext_status_cb) { 1760 int ret; 1761 ret = s->ctx->internal->tlsext_status_cb(s, 1762 s->ctx->internal->tlsext_status_arg); 1763 if (ret == 0) { 1764 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 1765 SSLerror(s, SSL_R_INVALID_STATUS_RESPONSE); 1766 goto fatal_err; 1767 } 1768 if (ret < 0) { 1769 al = SSL_AD_INTERNAL_ERROR; 1770 SSLerror(s, ERR_R_MALLOC_FAILURE); 1771 goto fatal_err; 1772 } 1773 } 1774 return (1); 1775 fatal_err: 1776 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1777 return (-1); 1778 } 1779 1780 int 1781 ssl3_get_server_done(SSL *s) 1782 { 1783 int ret; 1784 1785 if ((ret = ssl3_get_message(s, SSL3_ST_CR_SRVR_DONE_A, 1786 SSL3_ST_CR_SRVR_DONE_B, SSL3_MT_SERVER_DONE, 1787 30 /* should be very small, like 0 :-) */)) <= 0) 1788 return ret; 1789 1790 if (s->internal->init_num != 0) { 1791 /* should contain no data */ 1792 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 1793 SSLerror(s, SSL_R_LENGTH_MISMATCH); 1794 return -1; 1795 } 1796 1797 return 1; 1798 } 1799 1800 static int 1801 ssl3_send_client_kex_rsa(SSL *s, CBB *cbb) 1802 { 1803 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH]; 1804 unsigned char *enc_pms = NULL; 1805 uint16_t max_legacy_version; 1806 EVP_PKEY *pkey; 1807 RSA *rsa; 1808 int ret = 0; 1809 int enc_len; 1810 CBB epms; 1811 1812 /* 1813 * RSA-Encrypted Premaster Secret Message - RFC 5246 section 7.4.7.1. 1814 */ 1815 1816 pkey = X509_get0_pubkey(s->session->peer_cert); 1817 if (pkey == NULL || (rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) { 1818 SSLerror(s, ERR_R_INTERNAL_ERROR); 1819 goto err; 1820 } 1821 1822 /* 1823 * Our maximum legacy protocol version - while RFC 5246 section 7.4.7.1 1824 * says "The latest (newest) version supported by the client", if we're 1825 * doing RSA key exchange then we have to presume that we're talking to 1826 * a server that does not understand the supported versions extension 1827 * and therefore our maximum version is that sent in the ClientHello. 1828 */ 1829 if (!ssl_max_legacy_version(s, &max_legacy_version)) 1830 goto err; 1831 pms[0] = max_legacy_version >> 8; 1832 pms[1] = max_legacy_version & 0xff; 1833 arc4random_buf(&pms[2], sizeof(pms) - 2); 1834 1835 if ((enc_pms = malloc(RSA_size(rsa))) == NULL) { 1836 SSLerror(s, ERR_R_MALLOC_FAILURE); 1837 goto err; 1838 } 1839 1840 enc_len = RSA_public_encrypt(sizeof(pms), pms, enc_pms, rsa, 1841 RSA_PKCS1_PADDING); 1842 if (enc_len <= 0) { 1843 SSLerror(s, SSL_R_BAD_RSA_ENCRYPT); 1844 goto err; 1845 } 1846 1847 if (!CBB_add_u16_length_prefixed(cbb, &epms)) 1848 goto err; 1849 if (!CBB_add_bytes(&epms, enc_pms, enc_len)) 1850 goto err; 1851 if (!CBB_flush(cbb)) 1852 goto err; 1853 1854 if (!tls12_derive_master_secret(s, pms, sizeof(pms))) 1855 goto err; 1856 1857 ret = 1; 1858 1859 err: 1860 explicit_bzero(pms, sizeof(pms)); 1861 free(enc_pms); 1862 1863 return ret; 1864 } 1865 1866 static int 1867 ssl3_send_client_kex_dhe(SSL *s, CBB *cbb) 1868 { 1869 uint8_t *key = NULL; 1870 size_t key_len = 0; 1871 int ret = 0; 1872 1873 /* Ensure that we have an ephemeral key from the server for DHE. */ 1874 if (s->s3->hs.key_share == NULL) { 1875 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1876 SSLerror(s, SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 1877 goto err; 1878 } 1879 1880 if (!tls_key_share_generate(s->s3->hs.key_share)) 1881 goto err; 1882 if (!tls_key_share_public(s->s3->hs.key_share, cbb)) 1883 goto err; 1884 if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1885 goto err; 1886 1887 if (!tls_key_share_peer_security(s, s->s3->hs.key_share)) { 1888 SSLerror(s, SSL_R_DH_KEY_TOO_SMALL); 1889 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1890 return 0; 1891 } 1892 1893 if (!tls12_derive_master_secret(s, key, key_len)) 1894 goto err; 1895 1896 ret = 1; 1897 1898 err: 1899 freezero(key, key_len); 1900 1901 return ret; 1902 } 1903 1904 static int 1905 ssl3_send_client_kex_ecdhe(SSL *s, CBB *cbb) 1906 { 1907 uint8_t *key = NULL; 1908 size_t key_len = 0; 1909 CBB public; 1910 int ret = 0; 1911 1912 /* Ensure that we have an ephemeral key for ECDHE. */ 1913 if (s->s3->hs.key_share == NULL) { 1914 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 1915 SSLerror(s, ERR_R_INTERNAL_ERROR); 1916 goto err; 1917 } 1918 1919 if (!tls_key_share_generate(s->s3->hs.key_share)) 1920 goto err; 1921 1922 if (!CBB_add_u8_length_prefixed(cbb, &public)) 1923 return 0; 1924 if (!tls_key_share_public(s->s3->hs.key_share, &public)) 1925 goto err; 1926 if (!CBB_flush(cbb)) 1927 goto err; 1928 1929 if (!tls_key_share_derive(s->s3->hs.key_share, &key, &key_len)) 1930 goto err; 1931 1932 if (!tls12_derive_master_secret(s, key, key_len)) 1933 goto err; 1934 1935 ret = 1; 1936 1937 err: 1938 freezero(key, key_len); 1939 1940 return ret; 1941 } 1942 1943 static int 1944 ssl3_send_client_kex_gost(SSL *s, CBB *cbb) 1945 { 1946 unsigned char premaster_secret[32], shared_ukm[32], tmp[256]; 1947 EVP_PKEY_CTX *pkey_ctx = NULL; 1948 EVP_MD_CTX *ukm_hash = NULL; 1949 EVP_PKEY *pkey; 1950 size_t msglen; 1951 unsigned int md_len; 1952 CBB gostblob; 1953 int nid; 1954 int ret = 0; 1955 1956 /* Get server sertificate PKEY and create ctx from it */ 1957 pkey = X509_get0_pubkey(s->session->peer_cert); 1958 if (pkey == NULL || s->session->peer_cert_type != SSL_PKEY_GOST01) { 1959 SSLerror(s, SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); 1960 goto err; 1961 } 1962 if ((pkey_ctx = EVP_PKEY_CTX_new(pkey, NULL)) == NULL) { 1963 SSLerror(s, ERR_R_MALLOC_FAILURE); 1964 goto err; 1965 } 1966 1967 /* 1968 * If we have send a certificate, and certificate key parameters match 1969 * those of server certificate, use certificate key for key exchange. 1970 * Otherwise, generate ephemeral key pair. 1971 */ 1972 if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) 1973 goto err; 1974 1975 /* Generate session key. */ 1976 arc4random_buf(premaster_secret, sizeof(premaster_secret)); 1977 1978 /* 1979 * If we have client certificate, use its secret as peer key. 1980 * XXX - this presumably lacks PFS. 1981 */ 1982 if (s->s3->hs.tls12.cert_request != 0 && 1983 s->cert->key->privatekey != NULL) { 1984 if (EVP_PKEY_derive_set_peer(pkey_ctx, 1985 s->cert->key->privatekey) <=0) { 1986 /* 1987 * If there was an error - just ignore it. 1988 * Ephemeral key would be used. 1989 */ 1990 ERR_clear_error(); 1991 } 1992 } 1993 1994 /* 1995 * Compute shared IV and store it in algorithm-specific context data. 1996 */ 1997 if ((ukm_hash = EVP_MD_CTX_new()) == NULL) { 1998 SSLerror(s, ERR_R_MALLOC_FAILURE); 1999 goto err; 2000 } 2001 2002 /* XXX check handshake hash instead. */ 2003 if (s->s3->hs.cipher->algorithm2 & SSL_HANDSHAKE_MAC_GOST94) 2004 nid = NID_id_GostR3411_94; 2005 else 2006 nid = NID_id_tc26_gost3411_2012_256; 2007 if (!EVP_DigestInit(ukm_hash, EVP_get_digestbynid(nid))) 2008 goto err; 2009 if (!EVP_DigestUpdate(ukm_hash, s->s3->client_random, SSL3_RANDOM_SIZE)) 2010 goto err; 2011 if (!EVP_DigestUpdate(ukm_hash, s->s3->server_random, SSL3_RANDOM_SIZE)) 2012 goto err; 2013 if (!EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len)) 2014 goto err; 2015 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, 2016 EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) < 0) { 2017 SSLerror(s, SSL_R_LIBRARY_BUG); 2018 goto err; 2019 } 2020 2021 /* 2022 * Make GOST keytransport blob message, encapsulate it into sequence. 2023 */ 2024 msglen = 255; 2025 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 2026 sizeof(premaster_secret)) < 0) { 2027 SSLerror(s, SSL_R_LIBRARY_BUG); 2028 goto err; 2029 } 2030 2031 if (!CBB_add_asn1(cbb, &gostblob, CBS_ASN1_SEQUENCE)) 2032 goto err; 2033 if (!CBB_add_bytes(&gostblob, tmp, msglen)) 2034 goto err; 2035 if (!CBB_flush(cbb)) 2036 goto err; 2037 2038 /* Check if pubkey from client certificate was used. */ 2039 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, 2040 NULL) > 0) 2041 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; 2042 2043 if (!tls12_derive_master_secret(s, premaster_secret, 32)) 2044 goto err; 2045 2046 ret = 1; 2047 2048 err: 2049 explicit_bzero(premaster_secret, sizeof(premaster_secret)); 2050 EVP_PKEY_CTX_free(pkey_ctx); 2051 EVP_MD_CTX_free(ukm_hash); 2052 2053 return ret; 2054 } 2055 2056 int 2057 ssl3_send_client_key_exchange(SSL *s) 2058 { 2059 unsigned long alg_k; 2060 CBB cbb, kex; 2061 2062 memset(&cbb, 0, sizeof(cbb)); 2063 2064 if (s->s3->hs.state == SSL3_ST_CW_KEY_EXCH_A) { 2065 alg_k = s->s3->hs.cipher->algorithm_mkey; 2066 2067 if (!ssl3_handshake_msg_start(s, &cbb, &kex, 2068 SSL3_MT_CLIENT_KEY_EXCHANGE)) 2069 goto err; 2070 2071 if (alg_k & SSL_kRSA) { 2072 if (!ssl3_send_client_kex_rsa(s, &kex)) 2073 goto err; 2074 } else if (alg_k & SSL_kDHE) { 2075 if (!ssl3_send_client_kex_dhe(s, &kex)) 2076 goto err; 2077 } else if (alg_k & SSL_kECDHE) { 2078 if (!ssl3_send_client_kex_ecdhe(s, &kex)) 2079 goto err; 2080 } else if (alg_k & SSL_kGOST) { 2081 if (!ssl3_send_client_kex_gost(s, &kex)) 2082 goto err; 2083 } else { 2084 ssl3_send_alert(s, SSL3_AL_FATAL, 2085 SSL_AD_HANDSHAKE_FAILURE); 2086 SSLerror(s, ERR_R_INTERNAL_ERROR); 2087 goto err; 2088 } 2089 2090 if (!ssl3_handshake_msg_finish(s, &cbb)) 2091 goto err; 2092 2093 s->s3->hs.state = SSL3_ST_CW_KEY_EXCH_B; 2094 } 2095 2096 /* SSL3_ST_CW_KEY_EXCH_B */ 2097 return (ssl3_handshake_write(s)); 2098 2099 err: 2100 CBB_cleanup(&cbb); 2101 2102 return (-1); 2103 } 2104 2105 static int 2106 ssl3_send_client_verify_sigalgs(SSL *s, EVP_PKEY *pkey, 2107 const struct ssl_sigalg *sigalg, CBB *cert_verify) 2108 { 2109 CBB cbb_signature; 2110 EVP_PKEY_CTX *pctx = NULL; 2111 EVP_MD_CTX *mctx = NULL; 2112 const unsigned char *hdata; 2113 unsigned char *signature = NULL; 2114 size_t signature_len, hdata_len; 2115 int ret = 0; 2116 2117 if ((mctx = EVP_MD_CTX_new()) == NULL) 2118 goto err; 2119 2120 if (!tls1_transcript_data(s, &hdata, &hdata_len)) { 2121 SSLerror(s, ERR_R_INTERNAL_ERROR); 2122 goto err; 2123 } 2124 if (!EVP_DigestSignInit(mctx, &pctx, sigalg->md(), NULL, pkey)) { 2125 SSLerror(s, ERR_R_EVP_LIB); 2126 goto err; 2127 } 2128 if (sigalg->key_type == EVP_PKEY_GOSTR01 && 2129 EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, 2130 EVP_PKEY_CTRL_GOST_SIG_FORMAT, GOST_SIG_FORMAT_RS_LE, NULL) <= 0) { 2131 SSLerror(s, ERR_R_EVP_LIB); 2132 goto err; 2133 } 2134 if ((sigalg->flags & SIGALG_FLAG_RSA_PSS) && 2135 (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) || 2136 !EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))) { 2137 SSLerror(s, ERR_R_EVP_LIB); 2138 goto err; 2139 } 2140 if (!EVP_DigestSignUpdate(mctx, hdata, hdata_len)) { 2141 SSLerror(s, ERR_R_EVP_LIB); 2142 goto err; 2143 } 2144 if (!EVP_DigestSignFinal(mctx, NULL, &signature_len) || 2145 signature_len == 0) { 2146 SSLerror(s, ERR_R_EVP_LIB); 2147 goto err; 2148 } 2149 if ((signature = calloc(1, signature_len)) == NULL) { 2150 SSLerror(s, ERR_R_MALLOC_FAILURE); 2151 goto err; 2152 } 2153 if (!EVP_DigestSignFinal(mctx, signature, &signature_len)) { 2154 SSLerror(s, ERR_R_EVP_LIB); 2155 goto err; 2156 } 2157 2158 if (!CBB_add_u16(cert_verify, sigalg->value)) 2159 goto err; 2160 if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2161 goto err; 2162 if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2163 goto err; 2164 if (!CBB_flush(cert_verify)) 2165 goto err; 2166 2167 ret = 1; 2168 2169 err: 2170 EVP_MD_CTX_free(mctx); 2171 free(signature); 2172 return ret; 2173 } 2174 2175 static int 2176 ssl3_send_client_verify_rsa(SSL *s, EVP_PKEY *pkey, CBB *cert_verify) 2177 { 2178 CBB cbb_signature; 2179 RSA *rsa; 2180 unsigned char data[EVP_MAX_MD_SIZE]; 2181 unsigned char *signature = NULL; 2182 unsigned int signature_len; 2183 size_t data_len; 2184 int ret = 0; 2185 2186 if (!tls1_transcript_hash_value(s, data, sizeof(data), &data_len)) 2187 goto err; 2188 if ((signature = calloc(1, EVP_PKEY_size(pkey))) == NULL) 2189 goto err; 2190 if ((rsa = EVP_PKEY_get0_RSA(pkey)) == NULL) 2191 goto err; 2192 if (RSA_sign(NID_md5_sha1, data, data_len, signature, &signature_len, 2193 rsa) <= 0 ) { 2194 SSLerror(s, ERR_R_RSA_LIB); 2195 goto err; 2196 } 2197 2198 if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2199 goto err; 2200 if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2201 goto err; 2202 if (!CBB_flush(cert_verify)) 2203 goto err; 2204 2205 ret = 1; 2206 err: 2207 free(signature); 2208 return ret; 2209 } 2210 2211 static int 2212 ssl3_send_client_verify_ec(SSL *s, EVP_PKEY *pkey, CBB *cert_verify) 2213 { 2214 CBB cbb_signature; 2215 EC_KEY *eckey; 2216 unsigned char data[EVP_MAX_MD_SIZE]; 2217 unsigned char *signature = NULL; 2218 unsigned int signature_len; 2219 int ret = 0; 2220 2221 if (!tls1_transcript_hash_value(s, data, sizeof(data), NULL)) 2222 goto err; 2223 if ((signature = calloc(1, EVP_PKEY_size(pkey))) == NULL) 2224 goto err; 2225 if ((eckey = EVP_PKEY_get0_EC_KEY(pkey)) == NULL) 2226 goto err; 2227 if (!ECDSA_sign(0, &data[MD5_DIGEST_LENGTH], SHA_DIGEST_LENGTH, 2228 signature, &signature_len, eckey)) { 2229 SSLerror(s, ERR_R_ECDSA_LIB); 2230 goto err; 2231 } 2232 2233 if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2234 goto err; 2235 if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2236 goto err; 2237 if (!CBB_flush(cert_verify)) 2238 goto err; 2239 2240 ret = 1; 2241 err: 2242 free(signature); 2243 return ret; 2244 } 2245 2246 #ifndef OPENSSL_NO_GOST 2247 static int 2248 ssl3_send_client_verify_gost(SSL *s, EVP_PKEY *pkey, CBB *cert_verify) 2249 { 2250 CBB cbb_signature; 2251 EVP_MD_CTX *mctx; 2252 EVP_PKEY_CTX *pctx; 2253 const EVP_MD *md; 2254 const unsigned char *hdata; 2255 unsigned char *signature = NULL; 2256 size_t signature_len; 2257 size_t hdata_len; 2258 int nid; 2259 int ret = 0; 2260 2261 if ((mctx = EVP_MD_CTX_new()) == NULL) 2262 goto err; 2263 2264 if (!tls1_transcript_data(s, &hdata, &hdata_len)) { 2265 SSLerror(s, ERR_R_INTERNAL_ERROR); 2266 goto err; 2267 } 2268 if (!EVP_PKEY_get_default_digest_nid(pkey, &nid) || 2269 (md = EVP_get_digestbynid(nid)) == NULL) { 2270 SSLerror(s, ERR_R_EVP_LIB); 2271 goto err; 2272 } 2273 if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey)) { 2274 SSLerror(s, ERR_R_EVP_LIB); 2275 goto err; 2276 } 2277 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, 2278 EVP_PKEY_CTRL_GOST_SIG_FORMAT, GOST_SIG_FORMAT_RS_LE, NULL) <= 0) { 2279 SSLerror(s, ERR_R_EVP_LIB); 2280 goto err; 2281 } 2282 if (!EVP_DigestSignUpdate(mctx, hdata, hdata_len)) { 2283 SSLerror(s, ERR_R_EVP_LIB); 2284 goto err; 2285 } 2286 if (!EVP_DigestSignFinal(mctx, NULL, &signature_len) || 2287 signature_len == 0) { 2288 SSLerror(s, ERR_R_EVP_LIB); 2289 goto err; 2290 } 2291 if ((signature = calloc(1, signature_len)) == NULL) { 2292 SSLerror(s, ERR_R_MALLOC_FAILURE); 2293 goto err; 2294 } 2295 if (!EVP_DigestSignFinal(mctx, signature, &signature_len)) { 2296 SSLerror(s, ERR_R_EVP_LIB); 2297 goto err; 2298 } 2299 2300 if (!CBB_add_u16_length_prefixed(cert_verify, &cbb_signature)) 2301 goto err; 2302 if (!CBB_add_bytes(&cbb_signature, signature, signature_len)) 2303 goto err; 2304 if (!CBB_flush(cert_verify)) 2305 goto err; 2306 2307 ret = 1; 2308 err: 2309 EVP_MD_CTX_free(mctx); 2310 free(signature); 2311 return ret; 2312 } 2313 #endif 2314 2315 int 2316 ssl3_send_client_verify(SSL *s) 2317 { 2318 const struct ssl_sigalg *sigalg; 2319 CBB cbb, cert_verify; 2320 EVP_PKEY *pkey; 2321 2322 memset(&cbb, 0, sizeof(cbb)); 2323 2324 if (s->s3->hs.state == SSL3_ST_CW_CERT_VRFY_A) { 2325 if (!ssl3_handshake_msg_start(s, &cbb, &cert_verify, 2326 SSL3_MT_CERTIFICATE_VERIFY)) 2327 goto err; 2328 2329 pkey = s->cert->key->privatekey; 2330 if ((sigalg = ssl_sigalg_select(s, pkey)) == NULL) { 2331 SSLerror(s, SSL_R_SIGNATURE_ALGORITHMS_ERROR); 2332 goto err; 2333 } 2334 s->s3->hs.our_sigalg = sigalg; 2335 2336 /* 2337 * For TLS v1.2 send signature algorithm and signature using 2338 * agreed digest and cached handshake records. 2339 */ 2340 if (SSL_USE_SIGALGS(s)) { 2341 if (!ssl3_send_client_verify_sigalgs(s, pkey, sigalg, 2342 &cert_verify)) 2343 goto err; 2344 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { 2345 if (!ssl3_send_client_verify_rsa(s, pkey, &cert_verify)) 2346 goto err; 2347 } else if (EVP_PKEY_id(pkey) == EVP_PKEY_EC) { 2348 if (!ssl3_send_client_verify_ec(s, pkey, &cert_verify)) 2349 goto err; 2350 #ifndef OPENSSL_NO_GOST 2351 } else if (EVP_PKEY_id(pkey) == NID_id_GostR3410_94 || 2352 EVP_PKEY_id(pkey) == NID_id_GostR3410_2001) { 2353 if (!ssl3_send_client_verify_gost(s, pkey, &cert_verify)) 2354 goto err; 2355 #endif 2356 } else { 2357 SSLerror(s, ERR_R_INTERNAL_ERROR); 2358 goto err; 2359 } 2360 2361 tls1_transcript_free(s); 2362 2363 if (!ssl3_handshake_msg_finish(s, &cbb)) 2364 goto err; 2365 2366 s->s3->hs.state = SSL3_ST_CW_CERT_VRFY_B; 2367 } 2368 2369 return (ssl3_handshake_write(s)); 2370 2371 err: 2372 CBB_cleanup(&cbb); 2373 2374 return (-1); 2375 } 2376 2377 int 2378 ssl3_send_client_certificate(SSL *s) 2379 { 2380 EVP_PKEY *pkey = NULL; 2381 X509 *x509 = NULL; 2382 CBB cbb, client_cert; 2383 int i; 2384 2385 memset(&cbb, 0, sizeof(cbb)); 2386 2387 if (s->s3->hs.state == SSL3_ST_CW_CERT_A) { 2388 if (s->cert->key->x509 == NULL || 2389 s->cert->key->privatekey == NULL) 2390 s->s3->hs.state = SSL3_ST_CW_CERT_B; 2391 else 2392 s->s3->hs.state = SSL3_ST_CW_CERT_C; 2393 } 2394 2395 /* We need to get a client cert */ 2396 if (s->s3->hs.state == SSL3_ST_CW_CERT_B) { 2397 /* 2398 * If we get an error, we need to 2399 * ssl->internal->rwstate = SSL_X509_LOOKUP; return(-1); 2400 * We then get retried later. 2401 */ 2402 i = ssl_do_client_cert_cb(s, &x509, &pkey); 2403 if (i < 0) { 2404 s->internal->rwstate = SSL_X509_LOOKUP; 2405 return (-1); 2406 } 2407 s->internal->rwstate = SSL_NOTHING; 2408 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { 2409 s->s3->hs.state = SSL3_ST_CW_CERT_B; 2410 if (!SSL_use_certificate(s, x509) || 2411 !SSL_use_PrivateKey(s, pkey)) 2412 i = 0; 2413 } else if (i == 1) { 2414 i = 0; 2415 SSLerror(s, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 2416 } 2417 2418 X509_free(x509); 2419 EVP_PKEY_free(pkey); 2420 if (i == 0) { 2421 s->s3->hs.tls12.cert_request = 2; 2422 2423 /* There is no client certificate to verify. */ 2424 tls1_transcript_free(s); 2425 } 2426 2427 /* Ok, we have a cert */ 2428 s->s3->hs.state = SSL3_ST_CW_CERT_C; 2429 } 2430 2431 if (s->s3->hs.state == SSL3_ST_CW_CERT_C) { 2432 if (!ssl3_handshake_msg_start(s, &cbb, &client_cert, 2433 SSL3_MT_CERTIFICATE)) 2434 goto err; 2435 if (!ssl3_output_cert_chain(s, &client_cert, 2436 (s->s3->hs.tls12.cert_request == 2) ? NULL : s->cert->key)) 2437 goto err; 2438 if (!ssl3_handshake_msg_finish(s, &cbb)) 2439 goto err; 2440 2441 s->s3->hs.state = SSL3_ST_CW_CERT_D; 2442 } 2443 2444 /* SSL3_ST_CW_CERT_D */ 2445 return (ssl3_handshake_write(s)); 2446 2447 err: 2448 CBB_cleanup(&cbb); 2449 2450 return (0); 2451 } 2452 2453 #define has_bits(i,m) (((i)&(m)) == (m)) 2454 2455 int 2456 ssl3_check_cert_and_algorithm(SSL *s) 2457 { 2458 long alg_k, alg_a; 2459 int nid = NID_undef; 2460 int i; 2461 2462 alg_k = s->s3->hs.cipher->algorithm_mkey; 2463 alg_a = s->s3->hs.cipher->algorithm_auth; 2464 2465 /* We don't have a certificate. */ 2466 if (alg_a & SSL_aNULL) 2467 return (1); 2468 2469 if (s->s3->hs.key_share != NULL) 2470 nid = tls_key_share_nid(s->s3->hs.key_share); 2471 2472 /* This is the passed certificate. */ 2473 2474 if (s->session->peer_cert_type == SSL_PKEY_ECC) { 2475 if (!ssl_check_srvr_ecc_cert_and_alg(s, s->session->peer_cert)) { 2476 SSLerror(s, SSL_R_BAD_ECC_CERT); 2477 goto fatal_err; 2478 } 2479 return (1); 2480 } 2481 2482 i = X509_certificate_type(s->session->peer_cert, NULL); 2483 2484 /* Check that we have a certificate if we require one. */ 2485 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_SIGN)) { 2486 SSLerror(s, SSL_R_MISSING_RSA_SIGNING_CERT); 2487 goto fatal_err; 2488 } 2489 if ((alg_k & SSL_kRSA) && !has_bits(i, EVP_PK_RSA|EVP_PKT_ENC)) { 2490 SSLerror(s, SSL_R_MISSING_RSA_ENCRYPTING_CERT); 2491 goto fatal_err; 2492 } 2493 if ((alg_k & SSL_kDHE) && 2494 !(has_bits(i, EVP_PK_DH|EVP_PKT_EXCH) || (nid == NID_dhKeyAgreement))) { 2495 SSLerror(s, SSL_R_MISSING_DH_KEY); 2496 goto fatal_err; 2497 } 2498 2499 return (1); 2500 2501 fatal_err: 2502 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 2503 2504 return (0); 2505 } 2506 2507 /* 2508 * Check to see if handshake is full or resumed. Usually this is just a 2509 * case of checking to see if a cache hit has occurred. In the case of 2510 * session tickets we have to check the next message to be sure. 2511 */ 2512 2513 int 2514 ssl3_check_finished(SSL *s) 2515 { 2516 int ret; 2517 2518 /* If we have no ticket it cannot be a resumed session. */ 2519 if (!s->session->tlsext_tick) 2520 return (1); 2521 /* this function is called when we really expect a Certificate 2522 * message, so permit appropriate message length */ 2523 if ((ret = ssl3_get_message(s, SSL3_ST_CR_CERT_A, 2524 SSL3_ST_CR_CERT_B, -1, s->internal->max_cert_list)) <= 0) 2525 return ret; 2526 2527 s->s3->hs.tls12.reuse_message = 1; 2528 if ((s->s3->hs.tls12.message_type == SSL3_MT_FINISHED) || 2529 (s->s3->hs.tls12.message_type == SSL3_MT_NEWSESSION_TICKET)) 2530 return (2); 2531 2532 return (1); 2533 } 2534 2535 int 2536 ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) 2537 { 2538 int i = 0; 2539 2540 #ifndef OPENSSL_NO_ENGINE 2541 if (s->ctx->internal->client_cert_engine) { 2542 i = ENGINE_load_ssl_client_cert( 2543 s->ctx->internal->client_cert_engine, s, 2544 SSL_get_client_CA_list(s), px509, ppkey, NULL, NULL, NULL); 2545 if (i != 0) 2546 return (i); 2547 } 2548 #endif 2549 if (s->ctx->internal->client_cert_cb) 2550 i = s->ctx->internal->client_cert_cb(s, px509, ppkey); 2551 return (i); 2552 } 2553