1/* ssl/s3_clnt.c */ 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#include "ssl_locl.h" 153#include "kssl_lcl.h" 154#include <openssl/buffer.h> 155#include <openssl/rand.h> 156#include <openssl/objects.h> 157#include <openssl/evp.h> 158#include <openssl/md5.h> 159#ifdef OPENSSL_FIPS 160# include <openssl/fips.h> 161#endif 162#ifndef OPENSSL_NO_DH 163# include <openssl/dh.h> 164#endif 165#include <openssl/bn.h> 166#ifndef OPENSSL_NO_ENGINE 167# include <openssl/engine.h> 168#endif 169 170static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b); 171#ifndef OPENSSL_NO_TLSEXT 172static int ssl3_check_finished(SSL *s); 173#endif 174 175#ifndef OPENSSL_NO_SSL3_METHOD 176static const SSL_METHOD *ssl3_get_client_method(int ver) 177{ 178 if (ver == SSL3_VERSION) 179 return (SSLv3_client_method()); 180 else 181 return (NULL); 182} 183 184IMPLEMENT_ssl3_meth_func(SSLv3_client_method, 185 ssl_undefined_function, 186 ssl3_connect, ssl3_get_client_method) 187#endif 188int ssl3_connect(SSL *s) 189{ 190 BUF_MEM *buf = NULL; 191 unsigned long Time = (unsigned long)time(NULL); 192 void (*cb) (const SSL *ssl, int type, int val) = NULL; 193 int ret = -1; 194 int new_state, state, skip = 0; 195 196 RAND_add(&Time, sizeof(Time), 0); 197 ERR_clear_error(); 198 clear_sys_error(); 199 200 if (s->info_callback != NULL) 201 cb = s->info_callback; 202 else if (s->ctx->info_callback != NULL) 203 cb = s->ctx->info_callback; 204 205 s->in_handshake++; 206 if (!SSL_in_init(s) || SSL_in_before(s)) 207 SSL_clear(s); 208 209#ifndef OPENSSL_NO_HEARTBEATS 210 /* 211 * If we're awaiting a HeartbeatResponse, pretend we already got and 212 * don't await it anymore, because Heartbeats don't make sense during 213 * handshakes anyway. 214 */ 215 if (s->tlsext_hb_pending) { 216 s->tlsext_hb_pending = 0; 217 s->tlsext_hb_seq++; 218 } 219#endif 220 221 for (;;) { 222 state = s->state; 223 224 switch (s->state) { 225 case SSL_ST_RENEGOTIATE: 226 s->renegotiate = 1; 227 s->state = SSL_ST_CONNECT; 228 s->ctx->stats.sess_connect_renegotiate++; 229 /* break */ 230 case SSL_ST_BEFORE: 231 case SSL_ST_CONNECT: 232 case SSL_ST_BEFORE | SSL_ST_CONNECT: 233 case SSL_ST_OK | SSL_ST_CONNECT: 234 235 s->server = 0; 236 if (cb != NULL) 237 cb(s, SSL_CB_HANDSHAKE_START, 1); 238 239 if ((s->version & 0xff00) != 0x0300) { 240 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); 241 s->state = SSL_ST_ERR; 242 ret = -1; 243 goto end; 244 } 245 246 /* s->version=SSL3_VERSION; */ 247 s->type = SSL_ST_CONNECT; 248 249 if (s->init_buf == NULL) { 250 if ((buf = BUF_MEM_new()) == NULL) { 251 ret = -1; 252 s->state = SSL_ST_ERR; 253 goto end; 254 } 255 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { 256 ret = -1; 257 s->state = SSL_ST_ERR; 258 goto end; 259 } 260 s->init_buf = buf; 261 buf = NULL; 262 } 263 264 if (!ssl3_setup_buffers(s)) { 265 ret = -1; 266 goto end; 267 } 268 269 /* setup buffing BIO */ 270 if (!ssl_init_wbio_buffer(s, 0)) { 271 ret = -1; 272 s->state = SSL_ST_ERR; 273 goto end; 274 } 275 276 /* don't push the buffering BIO quite yet */ 277 278 ssl3_init_finished_mac(s); 279 280 s->state = SSL3_ST_CW_CLNT_HELLO_A; 281 s->ctx->stats.sess_connect++; 282 s->init_num = 0; 283 s->s3->flags &= ~SSL3_FLAGS_CCS_OK; 284 /* 285 * Should have been reset by ssl3_get_finished, too. 286 */ 287 s->s3->change_cipher_spec = 0; 288 break; 289 290 case SSL3_ST_CW_CLNT_HELLO_A: 291 case SSL3_ST_CW_CLNT_HELLO_B: 292 293 s->shutdown = 0; 294 ret = ssl3_client_hello(s); 295 if (ret <= 0) 296 goto end; 297 s->state = SSL3_ST_CR_SRVR_HELLO_A; 298 s->init_num = 0; 299 300 /* turn on buffering for the next lot of output */ 301 if (s->bbio != s->wbio) 302 s->wbio = BIO_push(s->bbio, s->wbio); 303 304 break; 305 306 case SSL3_ST_CR_SRVR_HELLO_A: 307 case SSL3_ST_CR_SRVR_HELLO_B: 308 ret = ssl3_get_server_hello(s); 309 if (ret <= 0) 310 goto end; 311 312 if (s->hit) { 313 s->state = SSL3_ST_CR_FINISHED_A; 314#ifndef OPENSSL_NO_TLSEXT 315 if (s->tlsext_ticket_expected) { 316 /* receive renewed session ticket */ 317 s->state = SSL3_ST_CR_SESSION_TICKET_A; 318 } 319#endif 320 } else { 321 s->state = SSL3_ST_CR_CERT_A; 322 } 323 s->init_num = 0; 324 break; 325 case SSL3_ST_CR_CERT_A: 326 case SSL3_ST_CR_CERT_B: 327#ifndef OPENSSL_NO_TLSEXT 328 /* Noop (ret = 0) for everything but EAP-FAST. */ 329 ret = ssl3_check_finished(s); 330 if (ret < 0) 331 goto end; 332 if (ret == 1) { 333 s->hit = 1; 334 s->state = SSL3_ST_CR_FINISHED_A; 335 s->init_num = 0; 336 break; 337 } 338#endif 339 /* Check if it is anon DH/ECDH, SRP auth */ 340 /* or PSK */ 341 if (! 342 (s->s3->tmp. 343 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) 344 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { 345 ret = ssl3_get_server_certificate(s); 346 if (ret <= 0) 347 goto end; 348#ifndef OPENSSL_NO_TLSEXT 349 if (s->tlsext_status_expected) 350 s->state = SSL3_ST_CR_CERT_STATUS_A; 351 else 352 s->state = SSL3_ST_CR_KEY_EXCH_A; 353 } else { 354 skip = 1; 355 s->state = SSL3_ST_CR_KEY_EXCH_A; 356 } 357#else 358 } else 359 skip = 1; 360 361 s->state = SSL3_ST_CR_KEY_EXCH_A; 362#endif 363 s->init_num = 0; 364 break; 365 366 case SSL3_ST_CR_KEY_EXCH_A: 367 case SSL3_ST_CR_KEY_EXCH_B: 368 ret = ssl3_get_key_exchange(s); 369 if (ret <= 0) 370 goto end; 371 s->state = SSL3_ST_CR_CERT_REQ_A; 372 s->init_num = 0; 373 374 /* 375 * at this point we check that we have the required stuff from 376 * the server 377 */ 378 if (!ssl3_check_cert_and_algorithm(s)) { 379 ret = -1; 380 s->state = SSL_ST_ERR; 381 goto end; 382 } 383 break; 384 385 case SSL3_ST_CR_CERT_REQ_A: 386 case SSL3_ST_CR_CERT_REQ_B: 387 ret = ssl3_get_certificate_request(s); 388 if (ret <= 0) 389 goto end; 390 s->state = SSL3_ST_CR_SRVR_DONE_A; 391 s->init_num = 0; 392 break; 393 394 case SSL3_ST_CR_SRVR_DONE_A: 395 case SSL3_ST_CR_SRVR_DONE_B: 396 ret = ssl3_get_server_done(s); 397 if (ret <= 0) 398 goto end; 399#ifndef OPENSSL_NO_SRP 400 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) { 401 if ((ret = SRP_Calc_A_param(s)) <= 0) { 402 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC); 403 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 404 s->state = SSL_ST_ERR; 405 goto end; 406 } 407 } 408#endif 409 if (s->s3->tmp.cert_req) 410 s->state = SSL3_ST_CW_CERT_A; 411 else 412 s->state = SSL3_ST_CW_KEY_EXCH_A; 413 s->init_num = 0; 414 415 break; 416 417 case SSL3_ST_CW_CERT_A: 418 case SSL3_ST_CW_CERT_B: 419 case SSL3_ST_CW_CERT_C: 420 case SSL3_ST_CW_CERT_D: 421 ret = ssl3_send_client_certificate(s); 422 if (ret <= 0) 423 goto end; 424 s->state = SSL3_ST_CW_KEY_EXCH_A; 425 s->init_num = 0; 426 break; 427 428 case SSL3_ST_CW_KEY_EXCH_A: 429 case SSL3_ST_CW_KEY_EXCH_B: 430 ret = ssl3_send_client_key_exchange(s); 431 if (ret <= 0) 432 goto end; 433 /* 434 * EAY EAY EAY need to check for DH fix cert sent back 435 */ 436 /* 437 * For TLS, cert_req is set to 2, so a cert chain of nothing is 438 * sent, but no verify packet is sent 439 */ 440 /* 441 * XXX: For now, we do not support client authentication in ECDH 442 * cipher suites with ECDH (rather than ECDSA) certificates. We 443 * need to skip the certificate verify message when client's 444 * ECDH public key is sent inside the client certificate. 445 */ 446 if (s->s3->tmp.cert_req == 1) { 447 s->state = SSL3_ST_CW_CERT_VRFY_A; 448 } else { 449 s->state = SSL3_ST_CW_CHANGE_A; 450 } 451 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { 452 s->state = SSL3_ST_CW_CHANGE_A; 453 } 454 455 s->init_num = 0; 456 break; 457 458 case SSL3_ST_CW_CERT_VRFY_A: 459 case SSL3_ST_CW_CERT_VRFY_B: 460 ret = ssl3_send_client_verify(s); 461 if (ret <= 0) 462 goto end; 463 s->state = SSL3_ST_CW_CHANGE_A; 464 s->init_num = 0; 465 break; 466 467 case SSL3_ST_CW_CHANGE_A: 468 case SSL3_ST_CW_CHANGE_B: 469 ret = ssl3_send_change_cipher_spec(s, 470 SSL3_ST_CW_CHANGE_A, 471 SSL3_ST_CW_CHANGE_B); 472 if (ret <= 0) 473 goto end; 474 475#if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 476 s->state = SSL3_ST_CW_FINISHED_A; 477#else 478 if (s->s3->next_proto_neg_seen) 479 s->state = SSL3_ST_CW_NEXT_PROTO_A; 480 else 481 s->state = SSL3_ST_CW_FINISHED_A; 482#endif 483 s->init_num = 0; 484 485 s->session->cipher = s->s3->tmp.new_cipher; 486#ifdef OPENSSL_NO_COMP 487 s->session->compress_meth = 0; 488#else 489 if (s->s3->tmp.new_compression == NULL) 490 s->session->compress_meth = 0; 491 else 492 s->session->compress_meth = s->s3->tmp.new_compression->id; 493#endif 494 if (!s->method->ssl3_enc->setup_key_block(s)) { 495 ret = -1; 496 s->state = SSL_ST_ERR; 497 goto end; 498 } 499 500 if (!s->method->ssl3_enc->change_cipher_state(s, 501 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) 502 { 503 ret = -1; 504 s->state = SSL_ST_ERR; 505 goto end; 506 } 507 508 break; 509 510#if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 511 case SSL3_ST_CW_NEXT_PROTO_A: 512 case SSL3_ST_CW_NEXT_PROTO_B: 513 ret = ssl3_send_next_proto(s); 514 if (ret <= 0) 515 goto end; 516 s->state = SSL3_ST_CW_FINISHED_A; 517 break; 518#endif 519 520 case SSL3_ST_CW_FINISHED_A: 521 case SSL3_ST_CW_FINISHED_B: 522 ret = ssl3_send_finished(s, 523 SSL3_ST_CW_FINISHED_A, 524 SSL3_ST_CW_FINISHED_B, 525 s->method-> 526 ssl3_enc->client_finished_label, 527 s->method-> 528 ssl3_enc->client_finished_label_len); 529 if (ret <= 0) 530 goto end; 531 s->state = SSL3_ST_CW_FLUSH; 532 533 /* clear flags */ 534 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER; 535 if (s->hit) { 536 s->s3->tmp.next_state = SSL_ST_OK; 537 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) { 538 s->state = SSL_ST_OK; 539 s->s3->flags |= SSL3_FLAGS_POP_BUFFER; 540 s->s3->delay_buf_pop_ret = 0; 541 } 542 } else { 543#ifndef OPENSSL_NO_TLSEXT 544 /* 545 * Allow NewSessionTicket if ticket expected 546 */ 547 if (s->tlsext_ticket_expected) 548 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A; 549 else 550#endif 551 552 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A; 553 } 554 s->init_num = 0; 555 break; 556 557#ifndef OPENSSL_NO_TLSEXT 558 case SSL3_ST_CR_SESSION_TICKET_A: 559 case SSL3_ST_CR_SESSION_TICKET_B: 560 ret = ssl3_get_new_session_ticket(s); 561 if (ret <= 0) 562 goto end; 563 s->state = SSL3_ST_CR_FINISHED_A; 564 s->init_num = 0; 565 break; 566 567 case SSL3_ST_CR_CERT_STATUS_A: 568 case SSL3_ST_CR_CERT_STATUS_B: 569 ret = ssl3_get_cert_status(s); 570 if (ret <= 0) 571 goto end; 572 s->state = SSL3_ST_CR_KEY_EXCH_A; 573 s->init_num = 0; 574 break; 575#endif 576 577 case SSL3_ST_CR_FINISHED_A: 578 case SSL3_ST_CR_FINISHED_B: 579 if (!s->s3->change_cipher_spec) 580 s->s3->flags |= SSL3_FLAGS_CCS_OK; 581 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, 582 SSL3_ST_CR_FINISHED_B); 583 if (ret <= 0) 584 goto end; 585 586 if (s->hit) 587 s->state = SSL3_ST_CW_CHANGE_A; 588 else 589 s->state = SSL_ST_OK; 590 s->init_num = 0; 591 break; 592 593 case SSL3_ST_CW_FLUSH: 594 s->rwstate = SSL_WRITING; 595 if (BIO_flush(s->wbio) <= 0) { 596 ret = -1; 597 goto end; 598 } 599 s->rwstate = SSL_NOTHING; 600 s->state = s->s3->tmp.next_state; 601 break; 602 603 case SSL_ST_OK: 604 /* clean a few things up */ 605 ssl3_cleanup_key_block(s); 606 607 if (s->init_buf != NULL) { 608 BUF_MEM_free(s->init_buf); 609 s->init_buf = NULL; 610 } 611 612 /* 613 * If we are not 'joining' the last two packets, remove the 614 * buffering now 615 */ 616 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) 617 ssl_free_wbio_buffer(s); 618 /* else do it later in ssl3_write */ 619 620 s->init_num = 0; 621 s->renegotiate = 0; 622 s->new_session = 0; 623 624 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); 625 if (s->hit) 626 s->ctx->stats.sess_hit++; 627 628 ret = 1; 629 /* s->server=0; */ 630 s->handshake_func = ssl3_connect; 631 s->ctx->stats.sess_connect_good++; 632 633 if (cb != NULL) 634 cb(s, SSL_CB_HANDSHAKE_DONE, 1); 635 636 goto end; 637 /* break; */ 638 639 case SSL_ST_ERR: 640 default: 641 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE); 642 ret = -1; 643 goto end; 644 /* break; */ 645 } 646 647 /* did we do anything */ 648 if (!s->s3->tmp.reuse_message && !skip) { 649 if (s->debug) { 650 if ((ret = BIO_flush(s->wbio)) <= 0) 651 goto end; 652 } 653 654 if ((cb != NULL) && (s->state != state)) { 655 new_state = s->state; 656 s->state = state; 657 cb(s, SSL_CB_CONNECT_LOOP, 1); 658 s->state = new_state; 659 } 660 } 661 skip = 0; 662 } 663 end: 664 s->in_handshake--; 665 if (buf != NULL) 666 BUF_MEM_free(buf); 667 if (cb != NULL) 668 cb(s, SSL_CB_CONNECT_EXIT, ret); 669 return (ret); 670} 671 672int ssl3_client_hello(SSL *s) 673{ 674 unsigned char *buf; 675 unsigned char *p, *d; 676 int i; 677 unsigned long l; 678 int al = 0; 679#ifndef OPENSSL_NO_COMP 680 int j; 681 SSL_COMP *comp; 682#endif 683 684 buf = (unsigned char *)s->init_buf->data; 685 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) { 686 SSL_SESSION *sess = s->session; 687 if ((sess == NULL) || (sess->ssl_version != s->version) || 688#ifdef OPENSSL_NO_TLSEXT 689 !sess->session_id_length || 690#else 691 /* 692 * In the case of EAP-FAST, we can have a pre-shared 693 * "ticket" without a session ID. 694 */ 695 (!sess->session_id_length && !sess->tlsext_tick) || 696#endif 697 (sess->not_resumable)) { 698 if (!ssl_get_new_session(s, 0)) 699 goto err; 700 } 701 if (s->method->version == DTLS_ANY_VERSION) { 702 /* Determine which DTLS version to use */ 703 int options = s->options; 704 /* If DTLS 1.2 disabled correct the version number */ 705 if (options & SSL_OP_NO_DTLSv1_2) { 706 if (tls1_suiteb(s)) { 707 SSLerr(SSL_F_SSL3_CLIENT_HELLO, 708 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); 709 goto err; 710 } 711 /* 712 * Disabling all versions is silly: return an error. 713 */ 714 if (options & SSL_OP_NO_DTLSv1) { 715 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_WRONG_SSL_VERSION); 716 goto err; 717 } 718 /* 719 * Update method so we don't use any DTLS 1.2 features. 720 */ 721 s->method = DTLSv1_client_method(); 722 s->version = DTLS1_VERSION; 723 } else { 724 /* 725 * We only support one version: update method 726 */ 727 if (options & SSL_OP_NO_DTLSv1) 728 s->method = DTLSv1_2_client_method(); 729 s->version = DTLS1_2_VERSION; 730 } 731 s->client_version = s->version; 732 } 733 /* else use the pre-loaded session */ 734 735 p = s->s3->client_random; 736 737 /* 738 * for DTLS if client_random is initialized, reuse it, we are 739 * required to use same upon reply to HelloVerify 740 */ 741 if (SSL_IS_DTLS(s)) { 742 size_t idx; 743 i = 1; 744 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) { 745 if (p[idx]) { 746 i = 0; 747 break; 748 } 749 } 750 } else 751 i = 1; 752 753 if (i && ssl_fill_hello_random(s, 0, p, 754 sizeof(s->s3->client_random)) <= 0) 755 goto err; 756 757 /* Do the message type and length last */ 758 d = p = ssl_handshake_start(s); 759 760 /*- 761 * version indicates the negotiated version: for example from 762 * an SSLv2/v3 compatible client hello). The client_version 763 * field is the maximum version we permit and it is also 764 * used in RSA encrypted premaster secrets. Some servers can 765 * choke if we initially report a higher version then 766 * renegotiate to a lower one in the premaster secret. This 767 * didn't happen with TLS 1.0 as most servers supported it 768 * but it can with TLS 1.1 or later if the server only supports 769 * 1.0. 770 * 771 * Possible scenario with previous logic: 772 * 1. Client hello indicates TLS 1.2 773 * 2. Server hello says TLS 1.0 774 * 3. RSA encrypted premaster secret uses 1.2. 775 * 4. Handhaked proceeds using TLS 1.0. 776 * 5. Server sends hello request to renegotiate. 777 * 6. Client hello indicates TLS v1.0 as we now 778 * know that is maximum server supports. 779 * 7. Server chokes on RSA encrypted premaster secret 780 * containing version 1.0. 781 * 782 * For interoperability it should be OK to always use the 783 * maximum version we support in client hello and then rely 784 * on the checking of version to ensure the servers isn't 785 * being inconsistent: for example initially negotiating with 786 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using 787 * client_version in client hello and not resetting it to 788 * the negotiated version. 789 */ 790#if 0 791 *(p++) = s->version >> 8; 792 *(p++) = s->version & 0xff; 793 s->client_version = s->version; 794#else 795 *(p++) = s->client_version >> 8; 796 *(p++) = s->client_version & 0xff; 797#endif 798 799 /* Random stuff */ 800 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE); 801 p += SSL3_RANDOM_SIZE; 802 803 /* Session ID */ 804 if (s->new_session) 805 i = 0; 806 else 807 i = s->session->session_id_length; 808 *(p++) = i; 809 if (i != 0) { 810 if (i > (int)sizeof(s->session->session_id)) { 811 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 812 goto err; 813 } 814 memcpy(p, s->session->session_id, i); 815 p += i; 816 } 817 818 /* cookie stuff for DTLS */ 819 if (SSL_IS_DTLS(s)) { 820 if (s->d1->cookie_len > sizeof(s->d1->cookie)) { 821 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 822 goto err; 823 } 824 *(p++) = s->d1->cookie_len; 825 memcpy(p, s->d1->cookie, s->d1->cookie_len); 826 p += s->d1->cookie_len; 827 } 828 829 /* Ciphers supported */ 830 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0); 831 if (i == 0) { 832 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE); 833 goto err; 834 } 835#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH 836 /* 837 * Some servers hang if client hello > 256 bytes as hack workaround 838 * chop number of supported ciphers to keep it well below this if we 839 * use TLS v1.2 840 */ 841 if (TLS1_get_version(s) >= TLS1_2_VERSION 842 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH) 843 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; 844#endif 845 s2n(i, p); 846 p += i; 847 848 /* COMPRESSION */ 849#ifdef OPENSSL_NO_COMP 850 *(p++) = 1; 851#else 852 853 if ((s->options & SSL_OP_NO_COMPRESSION) 854 || !s->ctx->comp_methods) 855 j = 0; 856 else 857 j = sk_SSL_COMP_num(s->ctx->comp_methods); 858 *(p++) = 1 + j; 859 for (i = 0; i < j; i++) { 860 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i); 861 *(p++) = comp->id; 862 } 863#endif 864 *(p++) = 0; /* Add the NULL method */ 865 866#ifndef OPENSSL_NO_TLSEXT 867 /* TLS extensions */ 868 if (ssl_prepare_clienthello_tlsext(s) <= 0) { 869 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); 870 goto err; 871 } 872 if ((p = 873 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH, 874 &al)) == NULL) { 875 ssl3_send_alert(s, SSL3_AL_FATAL, al); 876 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 877 goto err; 878 } 879#endif 880 881 l = p - d; 882 ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l); 883 s->state = SSL3_ST_CW_CLNT_HELLO_B; 884 } 885 886 /* SSL3_ST_CW_CLNT_HELLO_B */ 887 return ssl_do_write(s); 888 err: 889 s->state = SSL_ST_ERR; 890 return (-1); 891} 892 893int ssl3_get_server_hello(SSL *s) 894{ 895 STACK_OF(SSL_CIPHER) *sk; 896 const SSL_CIPHER *c; 897 CERT *ct = s->cert; 898 unsigned char *p, *d; 899 int i, al = SSL_AD_INTERNAL_ERROR, ok; 900 unsigned int j; 901 long n; 902#ifndef OPENSSL_NO_COMP 903 SSL_COMP *comp; 904#endif 905 /* 906 * Hello verify request and/or server hello version may not match so set 907 * first packet if we're negotiating version. 908 */ 909 if (SSL_IS_DTLS(s)) 910 s->first_packet = 1; 911 912 n = s->method->ssl_get_message(s, 913 SSL3_ST_CR_SRVR_HELLO_A, 914 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok); 915 916 if (!ok) 917 return ((int)n); 918 919 if (SSL_IS_DTLS(s)) { 920 s->first_packet = 0; 921 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) { 922 if (s->d1->send_cookie == 0) { 923 s->s3->tmp.reuse_message = 1; 924 return 1; 925 } else { /* already sent a cookie */ 926 927 al = SSL_AD_UNEXPECTED_MESSAGE; 928 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); 929 goto f_err; 930 } 931 } 932 } 933 934 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) { 935 al = SSL_AD_UNEXPECTED_MESSAGE; 936 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE); 937 goto f_err; 938 } 939 940 d = p = (unsigned char *)s->init_msg; 941 if (s->method->version == DTLS_ANY_VERSION) { 942 /* Work out correct protocol version to use */ 943 int hversion = (p[0] << 8) | p[1]; 944 int options = s->options; 945 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2)) 946 s->method = DTLSv1_2_client_method(); 947 else if (tls1_suiteb(s)) { 948 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 949 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE); 950 s->version = hversion; 951 al = SSL_AD_PROTOCOL_VERSION; 952 goto f_err; 953 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1)) 954 s->method = DTLSv1_client_method(); 955 else { 956 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); 957 s->version = hversion; 958 al = SSL_AD_PROTOCOL_VERSION; 959 goto f_err; 960 } 961 s->session->ssl_version = s->version = s->method->version; 962 } 963 964 if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) { 965 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION); 966 s->version = (s->version & 0xff00) | p[1]; 967 al = SSL_AD_PROTOCOL_VERSION; 968 goto f_err; 969 } 970 p += 2; 971 972 /* load the server hello data */ 973 /* load the server random */ 974 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE); 975 p += SSL3_RANDOM_SIZE; 976 977 s->hit = 0; 978 979 /* get the session-id */ 980 j = *(p++); 981 982 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) { 983 al = SSL_AD_ILLEGAL_PARAMETER; 984 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG); 985 goto f_err; 986 } 987#ifndef OPENSSL_NO_TLSEXT 988 /* 989 * Check if we can resume the session based on external pre-shared secret. 990 * EAP-FAST (RFC 4851) supports two types of session resumption. 991 * Resumption based on server-side state works with session IDs. 992 * Resumption based on pre-shared Protected Access Credentials (PACs) 993 * works by overriding the SessionTicket extension at the application 994 * layer, and does not send a session ID. (We do not know whether EAP-FAST 995 * servers would honour the session ID.) Therefore, the session ID alone 996 * is not a reliable indicator of session resumption, so we first check if 997 * we can resume, and later peek at the next handshake message to see if the 998 * server wants to resume. 999 */ 1000 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb && 1001 s->session->tlsext_tick) { 1002 SSL_CIPHER *pref_cipher = NULL; 1003 s->session->master_key_length = sizeof(s->session->master_key); 1004 if (s->tls_session_secret_cb(s, s->session->master_key, 1005 &s->session->master_key_length, 1006 NULL, &pref_cipher, 1007 s->tls_session_secret_cb_arg)) { 1008 s->session->cipher = pref_cipher ? 1009 pref_cipher : ssl_get_cipher_by_char(s, p + j); 1010 } else { 1011 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 1012 al = SSL_AD_INTERNAL_ERROR; 1013 goto f_err; 1014 } 1015 } 1016#endif /* OPENSSL_NO_TLSEXT */ 1017 1018 if (j != 0 && j == s->session->session_id_length 1019 && memcmp(p, s->session->session_id, j) == 0) { 1020 if (s->sid_ctx_length != s->session->sid_ctx_length 1021 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { 1022 /* actually a client application bug */ 1023 al = SSL_AD_ILLEGAL_PARAMETER; 1024 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 1025 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 1026 goto f_err; 1027 } 1028 s->hit = 1; 1029 } else { 1030 /* 1031 * If we were trying for session-id reuse but the server 1032 * didn't echo the ID, make a new SSL_SESSION. 1033 * In the case of EAP-FAST and PAC, we do not send a session ID, 1034 * so the PAC-based session secret is always preserved. It'll be 1035 * overwritten if the server refuses resumption. 1036 */ 1037 if (s->session->session_id_length > 0) { 1038 if (!ssl_get_new_session(s, 0)) { 1039 goto f_err; 1040 } 1041 } 1042 s->session->session_id_length = j; 1043 memcpy(s->session->session_id, p, j); /* j could be 0 */ 1044 } 1045 p += j; 1046 c = ssl_get_cipher_by_char(s, p); 1047 if (c == NULL) { 1048 /* unknown cipher */ 1049 al = SSL_AD_ILLEGAL_PARAMETER; 1050 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED); 1051 goto f_err; 1052 } 1053 /* Set version disabled mask now we know version */ 1054 if (!SSL_USE_TLS1_2_CIPHERS(s)) 1055 ct->mask_ssl = SSL_TLSV1_2; 1056 else 1057 ct->mask_ssl = 0; 1058 /* 1059 * If it is a disabled cipher we didn't send it in client hello, so 1060 * return an error. 1061 */ 1062 if (c->algorithm_ssl & ct->mask_ssl || 1063 c->algorithm_mkey & ct->mask_k || c->algorithm_auth & ct->mask_a) { 1064 al = SSL_AD_ILLEGAL_PARAMETER; 1065 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); 1066 goto f_err; 1067 } 1068 p += ssl_put_cipher_by_char(s, NULL, NULL); 1069 1070 sk = ssl_get_ciphers_by_id(s); 1071 i = sk_SSL_CIPHER_find(sk, c); 1072 if (i < 0) { 1073 /* we did not say we would use this cipher */ 1074 al = SSL_AD_ILLEGAL_PARAMETER; 1075 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED); 1076 goto f_err; 1077 } 1078 1079 /* 1080 * Depending on the session caching (internal/external), the cipher 1081 * and/or cipher_id values may not be set. Make sure that cipher_id is 1082 * set and use it for comparison. 1083 */ 1084 if (s->session->cipher) 1085 s->session->cipher_id = s->session->cipher->id; 1086 if (s->hit && (s->session->cipher_id != c->id)) { 1087/* Workaround is now obsolete */ 1088#if 0 1089 if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) 1090#endif 1091 { 1092 al = SSL_AD_ILLEGAL_PARAMETER; 1093 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 1094 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 1095 goto f_err; 1096 } 1097 } 1098 s->s3->tmp.new_cipher = c; 1099 /* 1100 * Don't digest cached records if no sigalgs: we may need them for client 1101 * authentication. 1102 */ 1103 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s)) 1104 goto f_err; 1105 /* lets get the compression algorithm */ 1106 /* COMPRESSION */ 1107#ifdef OPENSSL_NO_COMP 1108 if (*(p++) != 0) { 1109 al = SSL_AD_ILLEGAL_PARAMETER; 1110 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 1111 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 1112 goto f_err; 1113 } 1114 /* 1115 * If compression is disabled we'd better not try to resume a session 1116 * using compression. 1117 */ 1118 if (s->session->compress_meth != 0) { 1119 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION); 1120 goto f_err; 1121 } 1122#else 1123 j = *(p++); 1124 if (s->hit && j != s->session->compress_meth) { 1125 al = SSL_AD_ILLEGAL_PARAMETER; 1126 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 1127 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); 1128 goto f_err; 1129 } 1130 if (j == 0) 1131 comp = NULL; 1132 else if (s->options & SSL_OP_NO_COMPRESSION) { 1133 al = SSL_AD_ILLEGAL_PARAMETER; 1134 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED); 1135 goto f_err; 1136 } else 1137 comp = ssl3_comp_find(s->ctx->comp_methods, j); 1138 1139 if ((j != 0) && (comp == NULL)) { 1140 al = SSL_AD_ILLEGAL_PARAMETER; 1141 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, 1142 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 1143 goto f_err; 1144 } else { 1145 s->s3->tmp.new_compression = comp; 1146 } 1147#endif 1148 1149#ifndef OPENSSL_NO_TLSEXT 1150 /* TLS extensions */ 1151 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) { 1152 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT); 1153 goto err; 1154 } 1155#endif 1156 1157 if (p != (d + n)) { 1158 /* wrong packet length */ 1159 al = SSL_AD_DECODE_ERROR; 1160 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH); 1161 goto f_err; 1162 } 1163 1164 return (1); 1165 f_err: 1166 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1167 err: 1168 s->state = SSL_ST_ERR; 1169 return (-1); 1170} 1171 1172int ssl3_get_server_certificate(SSL *s) 1173{ 1174 int al, i, ok, ret = -1; 1175 unsigned long n, nc, llen, l; 1176 X509 *x = NULL; 1177 const unsigned char *q, *p; 1178 unsigned char *d; 1179 STACK_OF(X509) *sk = NULL; 1180 SESS_CERT *sc; 1181 EVP_PKEY *pkey = NULL; 1182 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == 1183 * KRB5 */ 1184 1185 n = s->method->ssl_get_message(s, 1186 SSL3_ST_CR_CERT_A, 1187 SSL3_ST_CR_CERT_B, 1188 -1, s->max_cert_list, &ok); 1189 1190 if (!ok) 1191 return ((int)n); 1192 1193 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || 1194 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && 1195 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) { 1196 s->s3->tmp.reuse_message = 1; 1197 return (1); 1198 } 1199 1200 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { 1201 al = SSL_AD_UNEXPECTED_MESSAGE; 1202 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE); 1203 goto f_err; 1204 } 1205 p = d = (unsigned char *)s->init_msg; 1206 1207 if ((sk = sk_X509_new_null()) == NULL) { 1208 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); 1209 goto err; 1210 } 1211 1212 n2l3(p, llen); 1213 if (llen + 3 != n) { 1214 al = SSL_AD_DECODE_ERROR; 1215 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH); 1216 goto f_err; 1217 } 1218 for (nc = 0; nc < llen;) { 1219 n2l3(p, l); 1220 if ((l + nc + 3) > llen) { 1221 al = SSL_AD_DECODE_ERROR; 1222 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1223 SSL_R_CERT_LENGTH_MISMATCH); 1224 goto f_err; 1225 } 1226 1227 q = p; 1228 x = d2i_X509(NULL, &q, l); 1229 if (x == NULL) { 1230 al = SSL_AD_BAD_CERTIFICATE; 1231 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB); 1232 goto f_err; 1233 } 1234 if (q != (p + l)) { 1235 al = SSL_AD_DECODE_ERROR; 1236 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1237 SSL_R_CERT_LENGTH_MISMATCH); 1238 goto f_err; 1239 } 1240 if (!sk_X509_push(sk, x)) { 1241 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE); 1242 goto err; 1243 } 1244 x = NULL; 1245 nc += l + 3; 1246 p = q; 1247 } 1248 1249 i = ssl_verify_cert_chain(s, sk); 1250 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) 1251#ifndef OPENSSL_NO_KRB5 1252 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 1253 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 1254#endif /* OPENSSL_NO_KRB5 */ 1255 ) { 1256 al = ssl_verify_alarm_type(s->verify_result); 1257 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1258 SSL_R_CERTIFICATE_VERIFY_FAILED); 1259 goto f_err; 1260 } 1261 ERR_clear_error(); /* but we keep s->verify_result */ 1262 1263 sc = ssl_sess_cert_new(); 1264 if (sc == NULL) 1265 goto err; 1266 1267 if (s->session->sess_cert) 1268 ssl_sess_cert_free(s->session->sess_cert); 1269 s->session->sess_cert = sc; 1270 1271 sc->cert_chain = sk; 1272 /* 1273 * Inconsistency alert: cert_chain does include the peer's certificate, 1274 * which we don't include in s3_srvr.c 1275 */ 1276 x = sk_X509_value(sk, 0); 1277 sk = NULL; 1278 /* 1279 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end 1280 */ 1281 1282 pkey = X509_get_pubkey(x); 1283 1284 /* VRS: allow null cert if auth == KRB5 */ 1285 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 1286 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 1287 ? 0 : 1; 1288 1289#ifdef KSSL_DEBUG 1290 fprintf(stderr, "pkey,x = %p, %p\n", pkey, x); 1291 fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey)); 1292 fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n", 1293 s->s3->tmp.new_cipher->name, 1294 s->s3->tmp.new_cipher->algorithm_mkey, 1295 s->s3->tmp.new_cipher->algorithm_auth, need_cert); 1296#endif /* KSSL_DEBUG */ 1297 1298 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) { 1299 x = NULL; 1300 al = SSL3_AL_FATAL; 1301 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1302 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 1303 goto f_err; 1304 } 1305 1306 i = ssl_cert_type(x, pkey); 1307 if (need_cert && i < 0) { 1308 x = NULL; 1309 al = SSL3_AL_FATAL; 1310 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1311 SSL_R_UNKNOWN_CERTIFICATE_TYPE); 1312 goto f_err; 1313 } 1314 1315 if (need_cert) { 1316 int exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher); 1317 if (exp_idx >= 0 && i != exp_idx) { 1318 x = NULL; 1319 al = SSL_AD_ILLEGAL_PARAMETER; 1320 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1321 SSL_R_WRONG_CERTIFICATE_TYPE); 1322 goto f_err; 1323 } 1324 sc->peer_cert_type = i; 1325 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 1326 /* 1327 * Why would the following ever happen? We just created sc a couple 1328 * of lines ago. 1329 */ 1330 if (sc->peer_pkeys[i].x509 != NULL) 1331 X509_free(sc->peer_pkeys[i].x509); 1332 sc->peer_pkeys[i].x509 = x; 1333 sc->peer_key = &(sc->peer_pkeys[i]); 1334 1335 if (s->session->peer != NULL) 1336 X509_free(s->session->peer); 1337 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509); 1338 s->session->peer = x; 1339 } else { 1340 sc->peer_cert_type = i; 1341 sc->peer_key = NULL; 1342 1343 if (s->session->peer != NULL) 1344 X509_free(s->session->peer); 1345 s->session->peer = NULL; 1346 } 1347 s->session->verify_result = s->verify_result; 1348 1349 x = NULL; 1350 ret = 1; 1351 if (0) { 1352 f_err: 1353 ssl3_send_alert(s, SSL3_AL_FATAL, al); 1354 err: 1355 s->state = SSL_ST_ERR; 1356 } 1357 1358 EVP_PKEY_free(pkey); 1359 X509_free(x); 1360 sk_X509_pop_free(sk, X509_free); 1361 return (ret); 1362} 1363 1364int ssl3_get_key_exchange(SSL *s) 1365{ 1366#ifndef OPENSSL_NO_RSA 1367 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2]; 1368#endif 1369 EVP_MD_CTX md_ctx; 1370 unsigned char *param, *p; 1371 int al, j, ok; 1372 long i, param_len, n, alg_k, alg_a; 1373 EVP_PKEY *pkey = NULL; 1374 const EVP_MD *md = NULL; 1375#ifndef OPENSSL_NO_RSA 1376 RSA *rsa = NULL; 1377#endif 1378#ifndef OPENSSL_NO_DH 1379 DH *dh = NULL; 1380#endif 1381#ifndef OPENSSL_NO_ECDH 1382 EC_KEY *ecdh = NULL; 1383 BN_CTX *bn_ctx = NULL; 1384 EC_POINT *srvr_ecpoint = NULL; 1385 int curve_nid = 0; 1386 int encoded_pt_len = 0; 1387#endif 1388 1389 EVP_MD_CTX_init(&md_ctx); 1390 1391 /* 1392 * use same message size as in ssl3_get_certificate_request() as 1393 * ServerKeyExchange message may be skipped 1394 */ 1395 n = s->method->ssl_get_message(s, 1396 SSL3_ST_CR_KEY_EXCH_A, 1397 SSL3_ST_CR_KEY_EXCH_B, 1398 -1, s->max_cert_list, &ok); 1399 if (!ok) 1400 return ((int)n); 1401 1402 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 1403 1404 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) { 1405 /* 1406 * Can't skip server key exchange if this is an ephemeral 1407 * ciphersuite. 1408 */ 1409 if (alg_k & (SSL_kDHE | SSL_kECDHE)) { 1410 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 1411 al = SSL_AD_UNEXPECTED_MESSAGE; 1412 goto f_err; 1413 } 1414#ifndef OPENSSL_NO_PSK 1415 /* 1416 * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no 1417 * identity hint is sent. Set session->sess_cert anyway to avoid 1418 * problems later. 1419 */ 1420 if (alg_k & SSL_kPSK) { 1421 s->session->sess_cert = ssl_sess_cert_new(); 1422 if (s->ctx->psk_identity_hint) 1423 OPENSSL_free(s->ctx->psk_identity_hint); 1424 s->ctx->psk_identity_hint = NULL; 1425 } 1426#endif 1427 s->s3->tmp.reuse_message = 1; 1428 return (1); 1429 } 1430 1431 param = p = (unsigned char *)s->init_msg; 1432 if (s->session->sess_cert != NULL) { 1433#ifndef OPENSSL_NO_RSA 1434 if (s->session->sess_cert->peer_rsa_tmp != NULL) { 1435 RSA_free(s->session->sess_cert->peer_rsa_tmp); 1436 s->session->sess_cert->peer_rsa_tmp = NULL; 1437 } 1438#endif 1439#ifndef OPENSSL_NO_DH 1440 if (s->session->sess_cert->peer_dh_tmp) { 1441 DH_free(s->session->sess_cert->peer_dh_tmp); 1442 s->session->sess_cert->peer_dh_tmp = NULL; 1443 } 1444#endif 1445#ifndef OPENSSL_NO_ECDH 1446 if (s->session->sess_cert->peer_ecdh_tmp) { 1447 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); 1448 s->session->sess_cert->peer_ecdh_tmp = NULL; 1449 } 1450#endif 1451 } else { 1452 s->session->sess_cert = ssl_sess_cert_new(); 1453 } 1454 1455 /* Total length of the parameters including the length prefix */ 1456 param_len = 0; 1457 1458 alg_a = s->s3->tmp.new_cipher->algorithm_auth; 1459 1460 al = SSL_AD_DECODE_ERROR; 1461 1462#ifndef OPENSSL_NO_PSK 1463 if (alg_k & SSL_kPSK) { 1464 param_len = 2; 1465 if (param_len > n) { 1466 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1467 goto f_err; 1468 } 1469 n2s(p, i); 1470 1471 /* 1472 * Store PSK identity hint for later use, hint is used in 1473 * ssl3_send_client_key_exchange. Assume that the maximum length of 1474 * a PSK identity hint can be as long as the maximum length of a PSK 1475 * identity. 1476 */ 1477 if (i > PSK_MAX_IDENTITY_LEN) { 1478 al = SSL_AD_HANDSHAKE_FAILURE; 1479 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG); 1480 goto f_err; 1481 } 1482 if (i > n - param_len) { 1483 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1484 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); 1485 goto f_err; 1486 } 1487 param_len += i; 1488 1489 s->session->psk_identity_hint = BUF_strndup((char *)p, i); 1490 if (s->session->psk_identity_hint == NULL) { 1491 al = SSL_AD_HANDSHAKE_FAILURE; 1492 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 1493 goto f_err; 1494 } 1495 1496 p += i; 1497 n -= param_len; 1498 } else 1499#endif /* !OPENSSL_NO_PSK */ 1500#ifndef OPENSSL_NO_SRP 1501 if (alg_k & SSL_kSRP) { 1502 param_len = 2; 1503 if (param_len > n) { 1504 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1505 goto f_err; 1506 } 1507 n2s(p, i); 1508 1509 if (i > n - param_len) { 1510 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH); 1511 goto f_err; 1512 } 1513 param_len += i; 1514 1515 if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) { 1516 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1517 goto err; 1518 } 1519 p += i; 1520 1521 if (2 > n - param_len) { 1522 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1523 goto f_err; 1524 } 1525 param_len += 2; 1526 1527 n2s(p, i); 1528 1529 if (i > n - param_len) { 1530 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH); 1531 goto f_err; 1532 } 1533 param_len += i; 1534 1535 if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) { 1536 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1537 goto err; 1538 } 1539 p += i; 1540 1541 if (1 > n - param_len) { 1542 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1543 goto f_err; 1544 } 1545 param_len += 1; 1546 1547 i = (unsigned int)(p[0]); 1548 p++; 1549 1550 if (i > n - param_len) { 1551 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH); 1552 goto f_err; 1553 } 1554 param_len += i; 1555 1556 if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) { 1557 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1558 goto err; 1559 } 1560 p += i; 1561 1562 if (2 > n - param_len) { 1563 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1564 goto f_err; 1565 } 1566 param_len += 2; 1567 1568 n2s(p, i); 1569 1570 if (i > n - param_len) { 1571 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH); 1572 goto f_err; 1573 } 1574 param_len += i; 1575 1576 if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) { 1577 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1578 goto err; 1579 } 1580 p += i; 1581 n -= param_len; 1582 1583 if (!srp_verify_server_param(s, &al)) { 1584 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS); 1585 goto f_err; 1586 } 1587 1588/* We must check if there is a certificate */ 1589# ifndef OPENSSL_NO_RSA 1590 if (alg_a & SSL_aRSA) 1591 pkey = 1592 X509_get_pubkey(s->session-> 1593 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1594# else 1595 if (0) ; 1596# endif 1597# ifndef OPENSSL_NO_DSA 1598 else if (alg_a & SSL_aDSS) 1599 pkey = 1600 X509_get_pubkey(s->session-> 1601 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. 1602 x509); 1603# endif 1604 } else 1605#endif /* !OPENSSL_NO_SRP */ 1606#ifndef OPENSSL_NO_RSA 1607 if (alg_k & SSL_kRSA) { 1608 /* Temporary RSA keys only allowed in export ciphersuites */ 1609 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { 1610 al = SSL_AD_UNEXPECTED_MESSAGE; 1611 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 1612 goto f_err; 1613 } 1614 if ((rsa = RSA_new()) == NULL) { 1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 1616 goto err; 1617 } 1618 1619 param_len = 2; 1620 if (param_len > n) { 1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1622 goto f_err; 1623 } 1624 n2s(p, i); 1625 1626 if (i > n - param_len) { 1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH); 1628 goto f_err; 1629 } 1630 param_len += i; 1631 1632 if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) { 1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1634 goto err; 1635 } 1636 p += i; 1637 1638 if (2 > n - param_len) { 1639 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1640 goto f_err; 1641 } 1642 param_len += 2; 1643 1644 n2s(p, i); 1645 1646 if (i > n - param_len) { 1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH); 1648 goto f_err; 1649 } 1650 param_len += i; 1651 1652 if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) { 1653 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1654 goto err; 1655 } 1656 p += i; 1657 n -= param_len; 1658 1659 /* this should be because we are using an export cipher */ 1660 if (alg_a & SSL_aRSA) 1661 pkey = 1662 X509_get_pubkey(s->session-> 1663 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1664 else { 1665 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1666 goto err; 1667 } 1668 1669 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 1670 al = SSL_AD_UNEXPECTED_MESSAGE; 1671 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 1672 goto f_err; 1673 } 1674 1675 s->session->sess_cert->peer_rsa_tmp = rsa; 1676 rsa = NULL; 1677 } 1678#else /* OPENSSL_NO_RSA */ 1679 if (0) ; 1680#endif 1681#ifndef OPENSSL_NO_DH 1682 else if (alg_k & SSL_kEDH) { 1683 if ((dh = DH_new()) == NULL) { 1684 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB); 1685 goto err; 1686 } 1687 1688 param_len = 2; 1689 if (param_len > n) { 1690 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1691 goto f_err; 1692 } 1693 n2s(p, i); 1694 1695 if (i > n - param_len) { 1696 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH); 1697 goto f_err; 1698 } 1699 param_len += i; 1700 1701 if (!(dh->p = BN_bin2bn(p, i, NULL))) { 1702 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1703 goto err; 1704 } 1705 p += i; 1706 1707 if (BN_is_zero(dh->p)) { 1708 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE); 1709 goto f_err; 1710 } 1711 1712 1713 if (2 > n - param_len) { 1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1715 goto f_err; 1716 } 1717 param_len += 2; 1718 1719 n2s(p, i); 1720 1721 if (i > n - param_len) { 1722 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH); 1723 goto f_err; 1724 } 1725 param_len += i; 1726 1727 if (!(dh->g = BN_bin2bn(p, i, NULL))) { 1728 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1729 goto err; 1730 } 1731 p += i; 1732 1733 if (BN_is_zero(dh->g)) { 1734 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE); 1735 goto f_err; 1736 } 1737 1738 if (2 > n - param_len) { 1739 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1740 goto f_err; 1741 } 1742 param_len += 2; 1743 1744 n2s(p, i); 1745 1746 if (i > n - param_len) { 1747 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH); 1748 goto f_err; 1749 } 1750 param_len += i; 1751 1752 if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) { 1753 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB); 1754 goto err; 1755 } 1756 p += i; 1757 n -= param_len; 1758 1759 if (BN_is_zero(dh->pub_key)) { 1760 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE); 1761 goto f_err; 1762 } 1763 1764# ifndef OPENSSL_NO_RSA 1765 if (alg_a & SSL_aRSA) 1766 pkey = 1767 X509_get_pubkey(s->session-> 1768 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1769# else 1770 if (0) ; 1771# endif 1772# ifndef OPENSSL_NO_DSA 1773 else if (alg_a & SSL_aDSS) 1774 pkey = 1775 X509_get_pubkey(s->session-> 1776 sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN]. 1777 x509); 1778# endif 1779 /* else anonymous DH, so no certificate or pkey. */ 1780 1781 s->session->sess_cert->peer_dh_tmp = dh; 1782 dh = NULL; 1783 } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) { 1784 al = SSL_AD_ILLEGAL_PARAMETER; 1785 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1786 SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 1787 goto f_err; 1788 } 1789#endif /* !OPENSSL_NO_DH */ 1790 1791#ifndef OPENSSL_NO_ECDH 1792 else if (alg_k & SSL_kEECDH) { 1793 EC_GROUP *ngroup; 1794 const EC_GROUP *group; 1795 1796 if ((ecdh = EC_KEY_new()) == NULL) { 1797 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 1798 goto err; 1799 } 1800 1801 /* 1802 * Extract elliptic curve parameters and the server's ephemeral ECDH 1803 * public key. Keep accumulating lengths of various components in 1804 * param_len and make sure it never exceeds n. 1805 */ 1806 1807 /* 1808 * XXX: For now we only support named (not generic) curves and the 1809 * ECParameters in this case is just three bytes. We also need one 1810 * byte for the length of the encoded point 1811 */ 1812 param_len = 4; 1813 if (param_len > n) { 1814 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1815 goto f_err; 1816 } 1817 /* 1818 * Check curve is one of our preferences, if not server has sent an 1819 * invalid curve. ECParameters is 3 bytes. 1820 */ 1821 if (!tls1_check_curve(s, p, 3)) { 1822 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE); 1823 goto f_err; 1824 } 1825 1826 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) { 1827 al = SSL_AD_INTERNAL_ERROR; 1828 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1829 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); 1830 goto f_err; 1831 } 1832 1833 ngroup = EC_GROUP_new_by_curve_name(curve_nid); 1834 if (ngroup == NULL) { 1835 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); 1836 goto err; 1837 } 1838 if (EC_KEY_set_group(ecdh, ngroup) == 0) { 1839 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB); 1840 goto err; 1841 } 1842 EC_GROUP_free(ngroup); 1843 1844 group = EC_KEY_get0_group(ecdh); 1845 1846 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 1847 (EC_GROUP_get_degree(group) > 163)) { 1848 al = SSL_AD_EXPORT_RESTRICTION; 1849 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1850 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 1851 goto f_err; 1852 } 1853 1854 p += 3; 1855 1856 /* Next, get the encoded ECPoint */ 1857 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || 1858 ((bn_ctx = BN_CTX_new()) == NULL)) { 1859 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 1860 goto err; 1861 } 1862 1863 encoded_pt_len = *p; /* length of encoded point */ 1864 p += 1; 1865 1866 if ((encoded_pt_len > n - param_len) || 1867 (EC_POINT_oct2point(group, srvr_ecpoint, 1868 p, encoded_pt_len, bn_ctx) == 0)) { 1869 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT); 1870 goto f_err; 1871 } 1872 param_len += encoded_pt_len; 1873 1874 n -= param_len; 1875 p += encoded_pt_len; 1876 1877 /* 1878 * The ECC/TLS specification does not mention the use of DSA to sign 1879 * ECParameters in the server key exchange message. We do support RSA 1880 * and ECDSA. 1881 */ 1882 if (0) ; 1883# ifndef OPENSSL_NO_RSA 1884 else if (alg_a & SSL_aRSA) 1885 pkey = 1886 X509_get_pubkey(s->session-> 1887 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1888# endif 1889# ifndef OPENSSL_NO_ECDSA 1890 else if (alg_a & SSL_aECDSA) 1891 pkey = 1892 X509_get_pubkey(s->session-> 1893 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 1894# endif 1895 /* else anonymous ECDH, so no certificate or pkey. */ 1896 EC_KEY_set_public_key(ecdh, srvr_ecpoint); 1897 s->session->sess_cert->peer_ecdh_tmp = ecdh; 1898 ecdh = NULL; 1899 BN_CTX_free(bn_ctx); 1900 bn_ctx = NULL; 1901 EC_POINT_free(srvr_ecpoint); 1902 srvr_ecpoint = NULL; 1903 } else if (alg_k) { 1904 al = SSL_AD_UNEXPECTED_MESSAGE; 1905 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE); 1906 goto f_err; 1907 } 1908#endif /* !OPENSSL_NO_ECDH */ 1909 1910 /* p points to the next byte, there are 'n' bytes left */ 1911 1912 /* if it was signed, check the signature */ 1913 if (pkey != NULL) { 1914 if (SSL_USE_SIGALGS(s)) { 1915 int rv; 1916 if (2 > n) { 1917 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1918 goto f_err; 1919 } 1920 rv = tls12_check_peer_sigalg(&md, s, p, pkey); 1921 if (rv == -1) 1922 goto err; 1923 else if (rv == 0) { 1924 goto f_err; 1925 } 1926#ifdef SSL_DEBUG 1927 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); 1928#endif 1929 p += 2; 1930 n -= 2; 1931 } else 1932 md = EVP_sha1(); 1933 1934 if (2 > n) { 1935 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT); 1936 goto f_err; 1937 } 1938 n2s(p, i); 1939 n -= 2; 1940 j = EVP_PKEY_size(pkey); 1941 1942 /* 1943 * Check signature length. If n is 0 then signature is empty 1944 */ 1945 if ((i != n) || (n > j) || (n <= 0)) { 1946 /* wrong packet length */ 1947 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH); 1948 goto f_err; 1949 } 1950#ifndef OPENSSL_NO_RSA 1951 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) { 1952 int num; 1953 unsigned int size; 1954 1955 j = 0; 1956 q = md_buf; 1957 for (num = 2; num > 0; num--) { 1958 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 1959 if (EVP_DigestInit_ex(&md_ctx, 1960 (num == 2) ? s->ctx->md5 : s->ctx->sha1, 1961 NULL) <= 0 1962 || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]), 1963 SSL3_RANDOM_SIZE) <= 0 1964 || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]), 1965 SSL3_RANDOM_SIZE) <= 0 1966 || EVP_DigestUpdate(&md_ctx, param, param_len) <= 0 1967 || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) { 1968 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1969 ERR_R_INTERNAL_ERROR); 1970 al = SSL_AD_INTERNAL_ERROR; 1971 goto f_err; 1972 } 1973 q += size; 1974 j += size; 1975 } 1976 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa); 1977 if (i < 0) { 1978 al = SSL_AD_DECRYPT_ERROR; 1979 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT); 1980 goto f_err; 1981 } 1982 if (i == 0) { 1983 /* bad signature */ 1984 al = SSL_AD_DECRYPT_ERROR; 1985 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); 1986 goto f_err; 1987 } 1988 } else 1989#endif 1990 { 1991 if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0 1992 || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]), 1993 SSL3_RANDOM_SIZE) <= 0 1994 || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]), 1995 SSL3_RANDOM_SIZE) <= 0 1996 || EVP_VerifyUpdate(&md_ctx, param, param_len) <= 0) { 1997 al = SSL_AD_INTERNAL_ERROR; 1998 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EVP_LIB); 1999 goto f_err; 2000 } 2001 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) { 2002 /* bad signature */ 2003 al = SSL_AD_DECRYPT_ERROR; 2004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE); 2005 goto f_err; 2006 } 2007 } 2008 } else { 2009 /* aNULL, aSRP or kPSK do not need public keys */ 2010 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) { 2011 /* Might be wrong key type, check it */ 2012 if (ssl3_check_cert_and_algorithm(s)) 2013 /* Otherwise this shouldn't happen */ 2014 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 2015 goto err; 2016 } 2017 /* still data left over */ 2018 if (n != 0) { 2019 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE); 2020 goto f_err; 2021 } 2022 } 2023 EVP_PKEY_free(pkey); 2024 EVP_MD_CTX_cleanup(&md_ctx); 2025 return (1); 2026 f_err: 2027 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2028 err: 2029 EVP_PKEY_free(pkey); 2030#ifndef OPENSSL_NO_RSA 2031 if (rsa != NULL) 2032 RSA_free(rsa); 2033#endif 2034#ifndef OPENSSL_NO_DH 2035 if (dh != NULL) 2036 DH_free(dh); 2037#endif 2038#ifndef OPENSSL_NO_ECDH 2039 BN_CTX_free(bn_ctx); 2040 EC_POINT_free(srvr_ecpoint); 2041 if (ecdh != NULL) 2042 EC_KEY_free(ecdh); 2043#endif 2044 EVP_MD_CTX_cleanup(&md_ctx); 2045 s->state = SSL_ST_ERR; 2046 return (-1); 2047} 2048 2049int ssl3_get_certificate_request(SSL *s) 2050{ 2051 int ok, ret = 0; 2052 unsigned long n, nc, l; 2053 unsigned int llen, ctype_num, i; 2054 X509_NAME *xn = NULL; 2055 const unsigned char *p, *q; 2056 unsigned char *d; 2057 STACK_OF(X509_NAME) *ca_sk = NULL; 2058 2059 n = s->method->ssl_get_message(s, 2060 SSL3_ST_CR_CERT_REQ_A, 2061 SSL3_ST_CR_CERT_REQ_B, 2062 -1, s->max_cert_list, &ok); 2063 2064 if (!ok) 2065 return ((int)n); 2066 2067 s->s3->tmp.cert_req = 0; 2068 2069 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) { 2070 s->s3->tmp.reuse_message = 1; 2071 /* 2072 * If we get here we don't need any cached handshake records as we 2073 * wont be doing client auth. 2074 */ 2075 if (s->s3->handshake_buffer) { 2076 if (!ssl3_digest_cached_records(s)) 2077 goto err; 2078 } 2079 return (1); 2080 } 2081 2082 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) { 2083 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2084 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE); 2085 goto err; 2086 } 2087 2088 /* TLS does not like anon-DH with client cert */ 2089 if (s->version > SSL3_VERSION) { 2090 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) { 2091 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2092 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 2093 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 2094 goto err; 2095 } 2096 } 2097 2098 p = d = (unsigned char *)s->init_msg; 2099 2100 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) { 2101 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); 2102 goto err; 2103 } 2104 2105 /* get the certificate types */ 2106 ctype_num = *(p++); 2107 if (s->cert->ctypes) { 2108 OPENSSL_free(s->cert->ctypes); 2109 s->cert->ctypes = NULL; 2110 } 2111 if (ctype_num > SSL3_CT_NUMBER) { 2112 /* If we exceed static buffer copy all to cert structure */ 2113 s->cert->ctypes = OPENSSL_malloc(ctype_num); 2114 memcpy(s->cert->ctypes, p, ctype_num); 2115 s->cert->ctype_num = (size_t)ctype_num; 2116 ctype_num = SSL3_CT_NUMBER; 2117 } 2118 for (i = 0; i < ctype_num; i++) 2119 s->s3->tmp.ctype[i] = p[i]; 2120 p += p[-1]; 2121 if (SSL_USE_SIGALGS(s)) { 2122 n2s(p, llen); 2123 /* 2124 * Check we have enough room for signature algorithms and following 2125 * length value. 2126 */ 2127 if ((unsigned long)(p - d + llen + 2) > n) { 2128 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2129 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 2130 SSL_R_DATA_LENGTH_TOO_LONG); 2131 goto err; 2132 } 2133 /* Clear certificate digests and validity flags */ 2134 for (i = 0; i < SSL_PKEY_NUM; i++) { 2135 s->cert->pkeys[i].digest = NULL; 2136 s->cert->pkeys[i].valid_flags = 0; 2137 } 2138 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) { 2139 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2140 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 2141 SSL_R_SIGNATURE_ALGORITHMS_ERROR); 2142 goto err; 2143 } 2144 if (!tls1_process_sigalgs(s)) { 2145 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 2146 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); 2147 goto err; 2148 } 2149 p += llen; 2150 } 2151 2152 /* get the CA RDNs */ 2153 n2s(p, llen); 2154#if 0 2155 { 2156 FILE *out; 2157 out = fopen("/tmp/vsign.der", "w"); 2158 fwrite(p, 1, llen, out); 2159 fclose(out); 2160 } 2161#endif 2162 2163 if ((unsigned long)(p - d + llen) != n) { 2164 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2165 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH); 2166 goto err; 2167 } 2168 2169 for (nc = 0; nc < llen;) { 2170 n2s(p, l); 2171 if ((l + nc + 2) > llen) { 2172 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 2173 goto cont; /* netscape bugs */ 2174 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2175 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG); 2176 goto err; 2177 } 2178 2179 q = p; 2180 2181 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) { 2182 /* If netscape tolerance is on, ignore errors */ 2183 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) 2184 goto cont; 2185 else { 2186 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2187 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB); 2188 goto err; 2189 } 2190 } 2191 2192 if (q != (p + l)) { 2193 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2194 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, 2195 SSL_R_CA_DN_LENGTH_MISMATCH); 2196 goto err; 2197 } 2198 if (!sk_X509_NAME_push(ca_sk, xn)) { 2199 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE); 2200 goto err; 2201 } 2202 xn = NULL; 2203 2204 p += l; 2205 nc += l + 2; 2206 } 2207 2208 if (0) { 2209 cont: 2210 ERR_clear_error(); 2211 } 2212 2213 /* we should setup a certificate to return.... */ 2214 s->s3->tmp.cert_req = 1; 2215 s->s3->tmp.ctype_num = ctype_num; 2216 if (s->s3->tmp.ca_names != NULL) 2217 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free); 2218 s->s3->tmp.ca_names = ca_sk; 2219 ca_sk = NULL; 2220 2221 ret = 1; 2222 goto done; 2223 err: 2224 s->state = SSL_ST_ERR; 2225 done: 2226 X509_NAME_free(xn); 2227 if (ca_sk != NULL) 2228 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free); 2229 return (ret); 2230} 2231 2232static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b) 2233{ 2234 return (X509_NAME_cmp(*a, *b)); 2235} 2236 2237#ifndef OPENSSL_NO_TLSEXT 2238int ssl3_get_new_session_ticket(SSL *s) 2239{ 2240 int ok, al, ret = 0, ticklen; 2241 long n; 2242 const unsigned char *p; 2243 unsigned char *d; 2244 unsigned long ticket_lifetime_hint; 2245 2246 n = s->method->ssl_get_message(s, 2247 SSL3_ST_CR_SESSION_TICKET_A, 2248 SSL3_ST_CR_SESSION_TICKET_B, 2249 SSL3_MT_NEWSESSION_TICKET, 16384, &ok); 2250 2251 if (!ok) 2252 return ((int)n); 2253 2254 if (n < 6) { 2255 /* need at least ticket_lifetime_hint + ticket length */ 2256 al = SSL_AD_DECODE_ERROR; 2257 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); 2258 goto f_err; 2259 } 2260 2261 p = d = (unsigned char *)s->init_msg; 2262 2263 n2l(p, ticket_lifetime_hint); 2264 n2s(p, ticklen); 2265 /* ticket_lifetime_hint + ticket_length + ticket */ 2266 if (ticklen + 6 != n) { 2267 al = SSL_AD_DECODE_ERROR; 2268 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH); 2269 goto f_err; 2270 } 2271 2272 /* Server is allowed to change its mind and send an empty ticket. */ 2273 if (ticklen == 0) 2274 return 1; 2275 2276 if (s->session->session_id_length > 0) { 2277 int i = s->session_ctx->session_cache_mode; 2278 SSL_SESSION *new_sess; 2279 /* 2280 * We reused an existing session, so we need to replace it with a new 2281 * one 2282 */ 2283 if (i & SSL_SESS_CACHE_CLIENT) { 2284 /* 2285 * Remove the old session from the cache 2286 */ 2287 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) { 2288 if (s->session_ctx->remove_session_cb != NULL) 2289 s->session_ctx->remove_session_cb(s->session_ctx, 2290 s->session); 2291 } else { 2292 /* We carry on if this fails */ 2293 SSL_CTX_remove_session(s->session_ctx, s->session); 2294 } 2295 } 2296 2297 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { 2298 al = SSL_AD_INTERNAL_ERROR; 2299 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); 2300 goto f_err; 2301 } 2302 2303 SSL_SESSION_free(s->session); 2304 s->session = new_sess; 2305 } 2306 2307 if (s->session->tlsext_tick) { 2308 OPENSSL_free(s->session->tlsext_tick); 2309 s->session->tlsext_ticklen = 0; 2310 } 2311 s->session->tlsext_tick = OPENSSL_malloc(ticklen); 2312 if (!s->session->tlsext_tick) { 2313 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE); 2314 goto err; 2315 } 2316 memcpy(s->session->tlsext_tick, p, ticklen); 2317 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint; 2318 s->session->tlsext_ticklen = ticklen; 2319 /* 2320 * There are two ways to detect a resumed ticket session. One is to set 2321 * an appropriate session ID and then the server must return a match in 2322 * ServerHello. This allows the normal client session ID matching to work 2323 * and we know much earlier that the ticket has been accepted. The 2324 * other way is to set zero length session ID when the ticket is 2325 * presented and rely on the handshake to determine session resumption. 2326 * We choose the former approach because this fits in with assumptions 2327 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is 2328 * SHA256 is disabled) hash of the ticket. 2329 */ 2330 EVP_Digest(p, ticklen, 2331 s->session->session_id, &s->session->session_id_length, 2332# ifndef OPENSSL_NO_SHA256 2333 EVP_sha256(), NULL); 2334# else 2335 EVP_sha1(), NULL); 2336# endif 2337 ret = 1; 2338 return (ret); 2339 f_err: 2340 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2341 err: 2342 s->state = SSL_ST_ERR; 2343 return (-1); 2344} 2345 2346int ssl3_get_cert_status(SSL *s) 2347{ 2348 int ok, al; 2349 unsigned long resplen, n; 2350 const unsigned char *p; 2351 2352 n = s->method->ssl_get_message(s, 2353 SSL3_ST_CR_CERT_STATUS_A, 2354 SSL3_ST_CR_CERT_STATUS_B, 2355 -1, 16384, &ok); 2356 2357 if (!ok) 2358 return ((int)n); 2359 2360 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_STATUS) { 2361 /* 2362 * The CertificateStatus message is optional even if 2363 * tlsext_status_expected is set 2364 */ 2365 s->s3->tmp.reuse_message = 1; 2366 } else { 2367 if (n < 4) { 2368 /* need at least status type + length */ 2369 al = SSL_AD_DECODE_ERROR; 2370 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); 2371 goto f_err; 2372 } 2373 p = (unsigned char *)s->init_msg; 2374 if (*p++ != TLSEXT_STATUSTYPE_ocsp) { 2375 al = SSL_AD_DECODE_ERROR; 2376 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE); 2377 goto f_err; 2378 } 2379 n2l3(p, resplen); 2380 if (resplen + 4 != n) { 2381 al = SSL_AD_DECODE_ERROR; 2382 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH); 2383 goto f_err; 2384 } 2385 s->tlsext_ocsp_resp = BUF_memdup(p, resplen); 2386 if (s->tlsext_ocsp_resp == NULL) { 2387 al = SSL_AD_INTERNAL_ERROR; 2388 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); 2389 goto f_err; 2390 } 2391 s->tlsext_ocsp_resplen = resplen; 2392 } 2393 if (s->ctx->tlsext_status_cb) { 2394 int ret; 2395 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); 2396 if (ret == 0) { 2397 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 2398 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE); 2399 goto f_err; 2400 } 2401 if (ret < 0) { 2402 al = SSL_AD_INTERNAL_ERROR; 2403 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE); 2404 goto f_err; 2405 } 2406 } 2407 return 1; 2408 f_err: 2409 ssl3_send_alert(s, SSL3_AL_FATAL, al); 2410 s->state = SSL_ST_ERR; 2411 return (-1); 2412} 2413#endif 2414 2415int ssl3_get_server_done(SSL *s) 2416{ 2417 int ok, ret = 0; 2418 long n; 2419 2420 /* Second to last param should be very small, like 0 :-) */ 2421 n = s->method->ssl_get_message(s, 2422 SSL3_ST_CR_SRVR_DONE_A, 2423 SSL3_ST_CR_SRVR_DONE_B, 2424 SSL3_MT_SERVER_DONE, 30, &ok); 2425 2426 if (!ok) 2427 return ((int)n); 2428 if (n > 0) { 2429 /* should contain no data */ 2430 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); 2431 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH); 2432 s->state = SSL_ST_ERR; 2433 return -1; 2434 } 2435 ret = 1; 2436 return (ret); 2437} 2438 2439#ifndef OPENSSL_NO_DH 2440static DH *get_server_static_dh_key(SESS_CERT *scert) 2441{ 2442 DH *dh_srvr = NULL; 2443 EVP_PKEY *spkey = NULL; 2444 int idx = scert->peer_cert_type; 2445 2446 if (idx >= 0) 2447 spkey = X509_get_pubkey(scert->peer_pkeys[idx].x509); 2448 if (spkey) { 2449 dh_srvr = EVP_PKEY_get1_DH(spkey); 2450 EVP_PKEY_free(spkey); 2451 } 2452 if (dh_srvr == NULL) 2453 SSLerr(SSL_F_GET_SERVER_STATIC_DH_KEY, ERR_R_INTERNAL_ERROR); 2454 return dh_srvr; 2455} 2456#endif 2457 2458int ssl3_send_client_key_exchange(SSL *s) 2459{ 2460 unsigned char *p; 2461 int n; 2462 unsigned long alg_k; 2463#ifndef OPENSSL_NO_RSA 2464 unsigned char *q; 2465 EVP_PKEY *pkey = NULL; 2466#endif 2467#ifndef OPENSSL_NO_KRB5 2468 KSSL_ERR kssl_err; 2469#endif /* OPENSSL_NO_KRB5 */ 2470#ifndef OPENSSL_NO_ECDH 2471 EC_KEY *clnt_ecdh = NULL; 2472 const EC_POINT *srvr_ecpoint = NULL; 2473 EVP_PKEY *srvr_pub_pkey = NULL; 2474 unsigned char *encodedPoint = NULL; 2475 int encoded_pt_len = 0; 2476 BN_CTX *bn_ctx = NULL; 2477#endif 2478 2479 if (s->state == SSL3_ST_CW_KEY_EXCH_A) { 2480 p = ssl_handshake_start(s); 2481 2482 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 2483 2484 /* Fool emacs indentation */ 2485 if (0) { 2486 } 2487#ifndef OPENSSL_NO_RSA 2488 else if (alg_k & SSL_kRSA) { 2489 RSA *rsa; 2490 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2491 2492 if (s->session->sess_cert == NULL) { 2493 /* 2494 * We should always have a server certificate with SSL_kRSA. 2495 */ 2496 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2497 ERR_R_INTERNAL_ERROR); 2498 goto err; 2499 } 2500 2501 if (s->session->sess_cert->peer_rsa_tmp != NULL) 2502 rsa = s->session->sess_cert->peer_rsa_tmp; 2503 else { 2504 pkey = 2505 X509_get_pubkey(s->session-> 2506 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC]. 2507 x509); 2508 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA) 2509 || (pkey->pkey.rsa == NULL)) { 2510 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2511 ERR_R_INTERNAL_ERROR); 2512 EVP_PKEY_free(pkey); 2513 goto err; 2514 } 2515 rsa = pkey->pkey.rsa; 2516 EVP_PKEY_free(pkey); 2517 } 2518 2519 tmp_buf[0] = s->client_version >> 8; 2520 tmp_buf[1] = s->client_version & 0xff; 2521 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0) 2522 goto err; 2523 2524 s->session->master_key_length = sizeof tmp_buf; 2525 2526 q = p; 2527 /* Fix buf for TLS and beyond */ 2528 if (s->version > SSL3_VERSION) 2529 p += 2; 2530 n = RSA_public_encrypt(sizeof tmp_buf, 2531 tmp_buf, p, rsa, RSA_PKCS1_PADDING); 2532# ifdef PKCS1_CHECK 2533 if (s->options & SSL_OP_PKCS1_CHECK_1) 2534 p[1]++; 2535 if (s->options & SSL_OP_PKCS1_CHECK_2) 2536 tmp_buf[0] = 0x70; 2537# endif 2538 if (n <= 0) { 2539 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2540 SSL_R_BAD_RSA_ENCRYPT); 2541 goto err; 2542 } 2543 2544 /* Fix buf for TLS and beyond */ 2545 if (s->version > SSL3_VERSION) { 2546 s2n(n, q); 2547 n += 2; 2548 } 2549 2550 s->session->master_key_length = 2551 s->method->ssl3_enc->generate_master_secret(s, 2552 s-> 2553 session->master_key, 2554 tmp_buf, 2555 sizeof tmp_buf); 2556 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 2557 } 2558#endif 2559#ifndef OPENSSL_NO_KRB5 2560 else if (alg_k & SSL_kKRB5) { 2561 krb5_error_code krb5rc; 2562 KSSL_CTX *kssl_ctx = s->kssl_ctx; 2563 /* krb5_data krb5_ap_req; */ 2564 krb5_data *enc_ticket; 2565 krb5_data authenticator, *authp = NULL; 2566 EVP_CIPHER_CTX ciph_ctx; 2567 const EVP_CIPHER *enc = NULL; 2568 unsigned char iv[EVP_MAX_IV_LENGTH]; 2569 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2570 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH]; 2571 int padl, outl = sizeof(epms); 2572 2573 EVP_CIPHER_CTX_init(&ciph_ctx); 2574 2575# ifdef KSSL_DEBUG 2576 fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n", 2577 alg_k, SSL_kKRB5); 2578# endif /* KSSL_DEBUG */ 2579 2580 authp = NULL; 2581# ifdef KRB5SENDAUTH 2582 if (KRB5SENDAUTH) 2583 authp = &authenticator; 2584# endif /* KRB5SENDAUTH */ 2585 2586 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err); 2587 enc = kssl_map_enc(kssl_ctx->enctype); 2588 if (enc == NULL) 2589 goto err; 2590# ifdef KSSL_DEBUG 2591 { 2592 fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc); 2593 if (krb5rc && kssl_err.text) 2594 fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n", 2595 kssl_err.text); 2596 } 2597# endif /* KSSL_DEBUG */ 2598 2599 if (krb5rc) { 2600 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 2601 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason); 2602 goto err; 2603 } 2604 2605 /*- 2606 * 20010406 VRS - Earlier versions used KRB5 AP_REQ 2607 * in place of RFC 2712 KerberosWrapper, as in: 2608 * 2609 * Send ticket (copy to *p, set n = length) 2610 * n = krb5_ap_req.length; 2611 * memcpy(p, krb5_ap_req.data, krb5_ap_req.length); 2612 * if (krb5_ap_req.data) 2613 * kssl_krb5_free_data_contents(NULL,&krb5_ap_req); 2614 * 2615 * Now using real RFC 2712 KerberosWrapper 2616 * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) 2617 * Note: 2712 "opaque" types are here replaced 2618 * with a 2-byte length followed by the value. 2619 * Example: 2620 * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms 2621 * Where "xx xx" = length bytes. Shown here with 2622 * optional authenticator omitted. 2623 */ 2624 2625 /* KerberosWrapper.Ticket */ 2626 s2n(enc_ticket->length, p); 2627 memcpy(p, enc_ticket->data, enc_ticket->length); 2628 p += enc_ticket->length; 2629 n = enc_ticket->length + 2; 2630 2631 /* KerberosWrapper.Authenticator */ 2632 if (authp && authp->length) { 2633 s2n(authp->length, p); 2634 memcpy(p, authp->data, authp->length); 2635 p += authp->length; 2636 n += authp->length + 2; 2637 2638 free(authp->data); 2639 authp->data = NULL; 2640 authp->length = 0; 2641 } else { 2642 s2n(0, p); /* null authenticator length */ 2643 n += 2; 2644 } 2645 2646 tmp_buf[0] = s->client_version >> 8; 2647 tmp_buf[1] = s->client_version & 0xff; 2648 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0) 2649 goto err; 2650 2651 /*- 2652 * 20010420 VRS. Tried it this way; failed. 2653 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); 2654 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx, 2655 * kssl_ctx->length); 2656 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); 2657 */ 2658 2659 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 2660 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv); 2661 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf, 2662 sizeof tmp_buf); 2663 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl); 2664 outl += padl; 2665 if (outl > (int)sizeof epms) { 2666 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2667 ERR_R_INTERNAL_ERROR); 2668 goto err; 2669 } 2670 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 2671 2672 /* KerberosWrapper.EncryptedPreMasterSecret */ 2673 s2n(outl, p); 2674 memcpy(p, epms, outl); 2675 p += outl; 2676 n += outl + 2; 2677 2678 s->session->master_key_length = 2679 s->method->ssl3_enc->generate_master_secret(s, 2680 s-> 2681 session->master_key, 2682 tmp_buf, 2683 sizeof tmp_buf); 2684 2685 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 2686 OPENSSL_cleanse(epms, outl); 2687 } 2688#endif 2689#ifndef OPENSSL_NO_DH 2690 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) { 2691 DH *dh_srvr, *dh_clnt; 2692 SESS_CERT *scert = s->session->sess_cert; 2693 2694 if (scert == NULL) { 2695 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2696 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2697 SSL_R_UNEXPECTED_MESSAGE); 2698 goto err; 2699 } 2700 2701 if (scert->peer_dh_tmp != NULL) { 2702 dh_srvr = scert->peer_dh_tmp; 2703 } else { 2704 dh_srvr = get_server_static_dh_key(scert); 2705 if (dh_srvr == NULL) 2706 goto err; 2707 } 2708 2709 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { 2710 /* Use client certificate key */ 2711 EVP_PKEY *clkey = s->cert->key->privatekey; 2712 dh_clnt = NULL; 2713 if (clkey) 2714 dh_clnt = EVP_PKEY_get1_DH(clkey); 2715 if (dh_clnt == NULL) { 2716 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2717 ERR_R_INTERNAL_ERROR); 2718 goto err; 2719 } 2720 } else { 2721 /* generate a new random key */ 2722 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) { 2723 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2724 goto err; 2725 } 2726 if (!DH_generate_key(dh_clnt)) { 2727 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2728 DH_free(dh_clnt); 2729 goto err; 2730 } 2731 } 2732 2733 /* 2734 * use the 'p' output buffer for the DH key, but make sure to 2735 * clear it out afterwards 2736 */ 2737 2738 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt); 2739 if (scert->peer_dh_tmp == NULL) 2740 DH_free(dh_srvr); 2741 2742 if (n <= 0) { 2743 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB); 2744 DH_free(dh_clnt); 2745 goto err; 2746 } 2747 2748 /* generate master key from the result */ 2749 s->session->master_key_length = 2750 s->method->ssl3_enc->generate_master_secret(s, 2751 s-> 2752 session->master_key, 2753 p, n); 2754 /* clean up */ 2755 memset(p, 0, n); 2756 2757 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) 2758 n = 0; 2759 else { 2760 /* send off the data */ 2761 n = BN_num_bytes(dh_clnt->pub_key); 2762 s2n(n, p); 2763 BN_bn2bin(dh_clnt->pub_key, p); 2764 n += 2; 2765 } 2766 2767 DH_free(dh_clnt); 2768 } 2769#endif 2770 2771#ifndef OPENSSL_NO_ECDH 2772 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) { 2773 const EC_GROUP *srvr_group = NULL; 2774 EC_KEY *tkey; 2775 int ecdh_clnt_cert = 0; 2776 int field_size = 0; 2777 2778 if (s->session->sess_cert == NULL) { 2779 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 2780 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2781 SSL_R_UNEXPECTED_MESSAGE); 2782 goto err; 2783 } 2784 2785 /* 2786 * Did we send out the client's ECDH share for use in premaster 2787 * computation as part of client certificate? If so, set 2788 * ecdh_clnt_cert to 1. 2789 */ 2790 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) { 2791 /*- 2792 * XXX: For now, we do not support client 2793 * authentication using ECDH certificates. 2794 * To add such support, one needs to add 2795 * code that checks for appropriate 2796 * conditions and sets ecdh_clnt_cert to 1. 2797 * For example, the cert have an ECC 2798 * key on the same curve as the server's 2799 * and the key should be authorized for 2800 * key agreement. 2801 * 2802 * One also needs to add code in ssl3_connect 2803 * to skip sending the certificate verify 2804 * message. 2805 * 2806 * if ((s->cert->key->privatekey != NULL) && 2807 * (s->cert->key->privatekey->type == 2808 * EVP_PKEY_EC) && ...) 2809 * ecdh_clnt_cert = 1; 2810 */ 2811 } 2812 2813 if (s->session->sess_cert->peer_ecdh_tmp != NULL) { 2814 tkey = s->session->sess_cert->peer_ecdh_tmp; 2815 } else { 2816 /* Get the Server Public Key from Cert */ 2817 srvr_pub_pkey = 2818 X509_get_pubkey(s->session-> 2819 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 2820 if ((srvr_pub_pkey == NULL) 2821 || (srvr_pub_pkey->type != EVP_PKEY_EC) 2822 || (srvr_pub_pkey->pkey.ec == NULL)) { 2823 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2824 ERR_R_INTERNAL_ERROR); 2825 goto err; 2826 } 2827 2828 tkey = srvr_pub_pkey->pkey.ec; 2829 } 2830 2831 srvr_group = EC_KEY_get0_group(tkey); 2832 srvr_ecpoint = EC_KEY_get0_public_key(tkey); 2833 2834 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) { 2835 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2836 ERR_R_INTERNAL_ERROR); 2837 goto err; 2838 } 2839 2840 if ((clnt_ecdh = EC_KEY_new()) == NULL) { 2841 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2842 ERR_R_MALLOC_FAILURE); 2843 goto err; 2844 } 2845 2846 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) { 2847 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); 2848 goto err; 2849 } 2850 if (ecdh_clnt_cert) { 2851 /* 2852 * Reuse key info from our certificate We only need our 2853 * private key to perform the ECDH computation. 2854 */ 2855 const BIGNUM *priv_key; 2856 tkey = s->cert->key->privatekey->pkey.ec; 2857 priv_key = EC_KEY_get0_private_key(tkey); 2858 if (priv_key == NULL) { 2859 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2860 ERR_R_MALLOC_FAILURE); 2861 goto err; 2862 } 2863 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) { 2864 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB); 2865 goto err; 2866 } 2867 } else { 2868 /* Generate a new ECDH key pair */ 2869 if (!(EC_KEY_generate_key(clnt_ecdh))) { 2870 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2871 ERR_R_ECDH_LIB); 2872 goto err; 2873 } 2874 } 2875 2876 /* 2877 * use the 'p' output buffer for the ECDH key, but make sure to 2878 * clear it out afterwards 2879 */ 2880 2881 field_size = EC_GROUP_get_degree(srvr_group); 2882 if (field_size <= 0) { 2883 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); 2884 goto err; 2885 } 2886 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint, 2887 clnt_ecdh, NULL); 2888 if (n <= 0) { 2889 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); 2890 goto err; 2891 } 2892 2893 /* generate master key from the result */ 2894 s->session->master_key_length = 2895 s->method->ssl3_enc->generate_master_secret(s, 2896 s-> 2897 session->master_key, 2898 p, n); 2899 2900 memset(p, 0, n); /* clean up */ 2901 2902 if (ecdh_clnt_cert) { 2903 /* Send empty client key exch message */ 2904 n = 0; 2905 } else { 2906 /* 2907 * First check the size of encoding and allocate memory 2908 * accordingly. 2909 */ 2910 encoded_pt_len = 2911 EC_POINT_point2oct(srvr_group, 2912 EC_KEY_get0_public_key(clnt_ecdh), 2913 POINT_CONVERSION_UNCOMPRESSED, 2914 NULL, 0, NULL); 2915 2916 encodedPoint = (unsigned char *) 2917 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char)); 2918 bn_ctx = BN_CTX_new(); 2919 if ((encodedPoint == NULL) || (bn_ctx == NULL)) { 2920 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2921 ERR_R_MALLOC_FAILURE); 2922 goto err; 2923 } 2924 2925 /* Encode the public key */ 2926 n = EC_POINT_point2oct(srvr_group, 2927 EC_KEY_get0_public_key(clnt_ecdh), 2928 POINT_CONVERSION_UNCOMPRESSED, 2929 encodedPoint, encoded_pt_len, bn_ctx); 2930 2931 *p = n; /* length of encoded point */ 2932 /* Encoded point will be copied here */ 2933 p += 1; 2934 /* copy the point */ 2935 memcpy((unsigned char *)p, encodedPoint, n); 2936 /* increment n to account for length field */ 2937 n += 1; 2938 } 2939 2940 /* Free allocated memory */ 2941 BN_CTX_free(bn_ctx); 2942 if (encodedPoint != NULL) 2943 OPENSSL_free(encodedPoint); 2944 if (clnt_ecdh != NULL) 2945 EC_KEY_free(clnt_ecdh); 2946 EVP_PKEY_free(srvr_pub_pkey); 2947 } 2948#endif /* !OPENSSL_NO_ECDH */ 2949 else if (alg_k & SSL_kGOST) { 2950 /* GOST key exchange message creation */ 2951 EVP_PKEY_CTX *pkey_ctx; 2952 X509 *peer_cert; 2953 size_t msglen; 2954 unsigned int md_len; 2955 int keytype; 2956 unsigned char premaster_secret[32], shared_ukm[32], tmp[256]; 2957 EVP_MD_CTX *ukm_hash; 2958 EVP_PKEY *pub_key; 2959 2960 /* 2961 * Get server sertificate PKEY and create ctx from it 2962 */ 2963 peer_cert = 2964 s->session-> 2965 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509; 2966 if (!peer_cert) 2967 peer_cert = 2968 s->session-> 2969 sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509; 2970 if (!peer_cert) { 2971 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2972 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); 2973 goto err; 2974 } 2975 2976 pkey_ctx = EVP_PKEY_CTX_new(pub_key = 2977 X509_get_pubkey(peer_cert), NULL); 2978 if (pkey_ctx == NULL) { 2979 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2980 ERR_R_MALLOC_FAILURE); 2981 goto err; 2982 } 2983 /* 2984 * If we have send a certificate, and certificate key 2985 * 2986 * * parameters match those of server certificate, use 2987 * certificate key for key exchange 2988 */ 2989 2990 /* Otherwise, generate ephemeral key pair */ 2991 2992 if (pkey_ctx == NULL 2993 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0 2994 /* Generate session key */ 2995 || RAND_bytes(premaster_secret, 32) <= 0) { 2996 EVP_PKEY_CTX_free(pkey_ctx); 2997 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2998 ERR_R_INTERNAL_ERROR); 2999 goto err; 3000 } 3001 /* 3002 * If we have client certificate, use its secret as peer key 3003 */ 3004 if (s->s3->tmp.cert_req && s->cert->key->privatekey) { 3005 if (EVP_PKEY_derive_set_peer 3006 (pkey_ctx, s->cert->key->privatekey) <= 0) { 3007 /* 3008 * If there was an error - just ignore it. Ephemeral key 3009 * * would be used 3010 */ 3011 ERR_clear_error(); 3012 } 3013 } 3014 /* 3015 * Compute shared IV and store it in algorithm-specific context 3016 * data 3017 */ 3018 ukm_hash = EVP_MD_CTX_create(); 3019 if (EVP_DigestInit(ukm_hash, 3020 EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0 3021 || EVP_DigestUpdate(ukm_hash, s->s3->client_random, 3022 SSL3_RANDOM_SIZE) <= 0 3023 || EVP_DigestUpdate(ukm_hash, s->s3->server_random, 3024 SSL3_RANDOM_SIZE) <= 0 3025 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { 3026 EVP_MD_CTX_destroy(ukm_hash); 3027 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3028 ERR_R_INTERNAL_ERROR); 3029 goto err; 3030 } 3031 EVP_MD_CTX_destroy(ukm_hash); 3032 if (EVP_PKEY_CTX_ctrl 3033 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8, 3034 shared_ukm) < 0) { 3035 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3036 SSL_R_LIBRARY_BUG); 3037 goto err; 3038 } 3039 /* Make GOST keytransport blob message */ 3040 /* 3041 * Encapsulate it into sequence 3042 */ 3043 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; 3044 msglen = 255; 3045 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32) 3046 <= 0) { 3047 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3048 SSL_R_LIBRARY_BUG); 3049 goto err; 3050 } 3051 if (msglen >= 0x80) { 3052 *(p++) = 0x81; 3053 *(p++) = msglen & 0xff; 3054 n = msglen + 3; 3055 } else { 3056 *(p++) = msglen & 0xff; 3057 n = msglen + 2; 3058 } 3059 memcpy(p, tmp, msglen); 3060 /* Check if pubkey from client certificate was used */ 3061 if (EVP_PKEY_CTX_ctrl 3062 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) { 3063 /* Set flag "skip certificate verify" */ 3064 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; 3065 } 3066 EVP_PKEY_CTX_free(pkey_ctx); 3067 s->session->master_key_length = 3068 s->method->ssl3_enc->generate_master_secret(s, 3069 s-> 3070 session->master_key, 3071 premaster_secret, 3072 32); 3073 EVP_PKEY_free(pub_key); 3074 3075 } 3076#ifndef OPENSSL_NO_SRP 3077 else if (alg_k & SSL_kSRP) { 3078 if (s->srp_ctx.A != NULL) { 3079 /* send off the data */ 3080 n = BN_num_bytes(s->srp_ctx.A); 3081 s2n(n, p); 3082 BN_bn2bin(s->srp_ctx.A, p); 3083 n += 2; 3084 } else { 3085 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3086 ERR_R_INTERNAL_ERROR); 3087 goto err; 3088 } 3089 if (s->session->srp_username != NULL) 3090 OPENSSL_free(s->session->srp_username); 3091 s->session->srp_username = BUF_strdup(s->srp_ctx.login); 3092 if (s->session->srp_username == NULL) { 3093 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3094 ERR_R_MALLOC_FAILURE); 3095 goto err; 3096 } 3097 3098 if ((s->session->master_key_length = 3099 SRP_generate_client_master_secret(s, 3100 s->session->master_key)) < 3101 0) { 3102 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3103 ERR_R_INTERNAL_ERROR); 3104 goto err; 3105 } 3106 } 3107#endif 3108#ifndef OPENSSL_NO_PSK 3109 else if (alg_k & SSL_kPSK) { 3110 /* 3111 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a 3112 * \0-terminated identity. The last byte is for us for simulating 3113 * strnlen. 3114 */ 3115 char identity[PSK_MAX_IDENTITY_LEN + 2]; 3116 size_t identity_len; 3117 unsigned char *t = NULL; 3118 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4]; 3119 unsigned int pre_ms_len = 0, psk_len = 0; 3120 int psk_err = 1; 3121 3122 n = 0; 3123 if (s->psk_client_callback == NULL) { 3124 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3125 SSL_R_PSK_NO_CLIENT_CB); 3126 goto err; 3127 } 3128 3129 memset(identity, 0, sizeof(identity)); 3130 psk_len = s->psk_client_callback(s, s->session->psk_identity_hint, 3131 identity, sizeof(identity) - 1, 3132 psk_or_pre_ms, 3133 sizeof(psk_or_pre_ms)); 3134 if (psk_len > PSK_MAX_PSK_LEN) { 3135 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3136 ERR_R_INTERNAL_ERROR); 3137 goto psk_err; 3138 } else if (psk_len == 0) { 3139 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3140 SSL_R_PSK_IDENTITY_NOT_FOUND); 3141 goto psk_err; 3142 } 3143 identity[PSK_MAX_IDENTITY_LEN + 1] = '\0'; 3144 identity_len = strlen(identity); 3145 if (identity_len > PSK_MAX_IDENTITY_LEN) { 3146 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3147 ERR_R_INTERNAL_ERROR); 3148 goto psk_err; 3149 } 3150 /* create PSK pre_master_secret */ 3151 pre_ms_len = 2 + psk_len + 2 + psk_len; 3152 t = psk_or_pre_ms; 3153 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len); 3154 s2n(psk_len, t); 3155 memset(t, 0, psk_len); 3156 t += psk_len; 3157 s2n(psk_len, t); 3158 3159 if (s->session->psk_identity_hint != NULL) 3160 OPENSSL_free(s->session->psk_identity_hint); 3161 s->session->psk_identity_hint = 3162 BUF_strdup(s->ctx->psk_identity_hint); 3163 if (s->ctx->psk_identity_hint != NULL 3164 && s->session->psk_identity_hint == NULL) { 3165 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3166 ERR_R_MALLOC_FAILURE); 3167 goto psk_err; 3168 } 3169 3170 if (s->session->psk_identity != NULL) 3171 OPENSSL_free(s->session->psk_identity); 3172 s->session->psk_identity = BUF_strdup(identity); 3173 if (s->session->psk_identity == NULL) { 3174 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 3175 ERR_R_MALLOC_FAILURE); 3176 goto psk_err; 3177 } 3178 3179 s->session->master_key_length = 3180 s->method->ssl3_enc->generate_master_secret(s, 3181 s-> 3182 session->master_key, 3183 psk_or_pre_ms, 3184 pre_ms_len); 3185 s2n(identity_len, p); 3186 memcpy(p, identity, identity_len); 3187 n = 2 + identity_len; 3188 psk_err = 0; 3189 psk_err: 3190 OPENSSL_cleanse(identity, sizeof(identity)); 3191 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); 3192 if (psk_err != 0) { 3193 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 3194 goto err; 3195 } 3196 } 3197#endif 3198 else { 3199 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 3200 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 3201 goto err; 3202 } 3203 3204 ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n); 3205 s->state = SSL3_ST_CW_KEY_EXCH_B; 3206 } 3207 3208 /* SSL3_ST_CW_KEY_EXCH_B */ 3209 return ssl_do_write(s); 3210 err: 3211#ifndef OPENSSL_NO_ECDH 3212 BN_CTX_free(bn_ctx); 3213 if (encodedPoint != NULL) 3214 OPENSSL_free(encodedPoint); 3215 if (clnt_ecdh != NULL) 3216 EC_KEY_free(clnt_ecdh); 3217 EVP_PKEY_free(srvr_pub_pkey); 3218#endif 3219 s->state = SSL_ST_ERR; 3220 return (-1); 3221} 3222 3223int ssl3_send_client_verify(SSL *s) 3224{ 3225 unsigned char *p; 3226 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH]; 3227 EVP_PKEY *pkey; 3228 EVP_PKEY_CTX *pctx = NULL; 3229 EVP_MD_CTX mctx; 3230 unsigned u = 0; 3231 unsigned long n; 3232 int j; 3233 3234 EVP_MD_CTX_init(&mctx); 3235 3236 if (s->state == SSL3_ST_CW_CERT_VRFY_A) { 3237 p = ssl_handshake_start(s); 3238 pkey = s->cert->key->privatekey; 3239/* Create context from key and test if sha1 is allowed as digest */ 3240 pctx = EVP_PKEY_CTX_new(pkey, NULL); 3241 if (pctx == NULL || EVP_PKEY_sign_init(pctx) <= 0) { 3242 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3243 goto err; 3244 } 3245 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) { 3246 if (!SSL_USE_SIGALGS(s)) 3247 s->method->ssl3_enc->cert_verify_mac(s, 3248 NID_sha1, 3249 &(data 3250 [MD5_DIGEST_LENGTH])); 3251 } else { 3252 ERR_clear_error(); 3253 } 3254 /* 3255 * For TLS v1.2 send signature algorithm and signature using agreed 3256 * digest and cached handshake records. 3257 */ 3258 if (SSL_USE_SIGALGS(s)) { 3259 long hdatalen = 0; 3260 void *hdata; 3261 const EVP_MD *md = s->cert->key->digest; 3262 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); 3263 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) { 3264 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3265 goto err; 3266 } 3267 p += 2; 3268#ifdef SSL_DEBUG 3269 fprintf(stderr, "Using TLS 1.2 with client alg %s\n", 3270 EVP_MD_name(md)); 3271#endif 3272 if (!EVP_SignInit_ex(&mctx, md, NULL) 3273 || !EVP_SignUpdate(&mctx, hdata, hdatalen) 3274 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) { 3275 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB); 3276 goto err; 3277 } 3278 s2n(u, p); 3279 n = u + 4; 3280 if (!ssl3_digest_cached_records(s)) 3281 goto err; 3282 } else 3283#ifndef OPENSSL_NO_RSA 3284 if (pkey->type == EVP_PKEY_RSA) { 3285 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0])); 3286 if (RSA_sign(NID_md5_sha1, data, 3287 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, 3288 &(p[2]), &u, pkey->pkey.rsa) <= 0) { 3289 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB); 3290 goto err; 3291 } 3292 s2n(u, p); 3293 n = u + 2; 3294 } else 3295#endif 3296#ifndef OPENSSL_NO_DSA 3297 if (pkey->type == EVP_PKEY_DSA) { 3298 if (!DSA_sign(pkey->save_type, 3299 &(data[MD5_DIGEST_LENGTH]), 3300 SHA_DIGEST_LENGTH, &(p[2]), 3301 (unsigned int *)&j, pkey->pkey.dsa)) { 3302 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB); 3303 goto err; 3304 } 3305 s2n(j, p); 3306 n = j + 2; 3307 } else 3308#endif 3309#ifndef OPENSSL_NO_ECDSA 3310 if (pkey->type == EVP_PKEY_EC) { 3311 if (!ECDSA_sign(pkey->save_type, 3312 &(data[MD5_DIGEST_LENGTH]), 3313 SHA_DIGEST_LENGTH, &(p[2]), 3314 (unsigned int *)&j, pkey->pkey.ec)) { 3315 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB); 3316 goto err; 3317 } 3318 s2n(j, p); 3319 n = j + 2; 3320 } else 3321#endif 3322 if (pkey->type == NID_id_GostR3410_94 3323 || pkey->type == NID_id_GostR3410_2001) { 3324 unsigned char signbuf[64]; 3325 int i; 3326 size_t sigsize = 64; 3327 s->method->ssl3_enc->cert_verify_mac(s, 3328 NID_id_GostR3411_94, data); 3329 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { 3330 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3331 goto err; 3332 } 3333 for (i = 63, j = 0; i >= 0; j++, i--) { 3334 p[2 + j] = signbuf[i]; 3335 } 3336 s2n(j, p); 3337 n = j + 2; 3338 } else { 3339 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR); 3340 goto err; 3341 } 3342 ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n); 3343 s->state = SSL3_ST_CW_CERT_VRFY_B; 3344 } 3345 EVP_MD_CTX_cleanup(&mctx); 3346 EVP_PKEY_CTX_free(pctx); 3347 return ssl_do_write(s); 3348 err: 3349 EVP_MD_CTX_cleanup(&mctx); 3350 EVP_PKEY_CTX_free(pctx); 3351 s->state = SSL_ST_ERR; 3352 return (-1); 3353} 3354 3355/* 3356 * Check a certificate can be used for client authentication. Currently check 3357 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client 3358 * certificates can be used and optionally checks suitability for Suite B. 3359 */ 3360static int ssl3_check_client_certificate(SSL *s) 3361{ 3362 unsigned long alg_k; 3363 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey) 3364 return 0; 3365 /* If no suitable signature algorithm can't use certificate */ 3366 if (SSL_USE_SIGALGS(s) && !s->cert->key->digest) 3367 return 0; 3368 /* 3369 * If strict mode check suitability of chain before using it. This also 3370 * adjusts suite B digest if necessary. 3371 */ 3372 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && 3373 !tls1_check_chain(s, NULL, NULL, NULL, -2)) 3374 return 0; 3375 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 3376 /* See if we can use client certificate for fixed DH */ 3377 if (alg_k & (SSL_kDHr | SSL_kDHd)) { 3378 SESS_CERT *scert = s->session->sess_cert; 3379 int i = scert->peer_cert_type; 3380 EVP_PKEY *clkey = NULL, *spkey = NULL; 3381 clkey = s->cert->key->privatekey; 3382 /* If client key not DH assume it can be used */ 3383 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH) 3384 return 1; 3385 if (i >= 0) 3386 spkey = X509_get_pubkey(scert->peer_pkeys[i].x509); 3387 if (spkey) { 3388 /* Compare server and client parameters */ 3389 i = EVP_PKEY_cmp_parameters(clkey, spkey); 3390 EVP_PKEY_free(spkey); 3391 if (i != 1) 3392 return 0; 3393 } 3394 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; 3395 } 3396 return 1; 3397} 3398 3399int ssl3_send_client_certificate(SSL *s) 3400{ 3401 X509 *x509 = NULL; 3402 EVP_PKEY *pkey = NULL; 3403 int i; 3404 3405 if (s->state == SSL3_ST_CW_CERT_A) { 3406 /* Let cert callback update client certificates if required */ 3407 if (s->cert->cert_cb) { 3408 i = s->cert->cert_cb(s, s->cert->cert_cb_arg); 3409 if (i < 0) { 3410 s->rwstate = SSL_X509_LOOKUP; 3411 return -1; 3412 } 3413 if (i == 0) { 3414 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 3415 s->state = SSL_ST_ERR; 3416 return 0; 3417 } 3418 s->rwstate = SSL_NOTHING; 3419 } 3420 if (ssl3_check_client_certificate(s)) 3421 s->state = SSL3_ST_CW_CERT_C; 3422 else 3423 s->state = SSL3_ST_CW_CERT_B; 3424 } 3425 3426 /* We need to get a client cert */ 3427 if (s->state == SSL3_ST_CW_CERT_B) { 3428 /* 3429 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; 3430 * return(-1); We then get retied later 3431 */ 3432 i = ssl_do_client_cert_cb(s, &x509, &pkey); 3433 if (i < 0) { 3434 s->rwstate = SSL_X509_LOOKUP; 3435 return (-1); 3436 } 3437 s->rwstate = SSL_NOTHING; 3438 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { 3439 s->state = SSL3_ST_CW_CERT_B; 3440 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey)) 3441 i = 0; 3442 } else if (i == 1) { 3443 i = 0; 3444 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, 3445 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 3446 } 3447 3448 if (x509 != NULL) 3449 X509_free(x509); 3450 if (pkey != NULL) 3451 EVP_PKEY_free(pkey); 3452 if (i && !ssl3_check_client_certificate(s)) 3453 i = 0; 3454 if (i == 0) { 3455 if (s->version == SSL3_VERSION) { 3456 s->s3->tmp.cert_req = 0; 3457 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); 3458 return (1); 3459 } else { 3460 s->s3->tmp.cert_req = 2; 3461 } 3462 } 3463 3464 /* Ok, we have a cert */ 3465 s->state = SSL3_ST_CW_CERT_C; 3466 } 3467 3468 if (s->state == SSL3_ST_CW_CERT_C) { 3469 s->state = SSL3_ST_CW_CERT_D; 3470 if (!ssl3_output_cert_chain(s, 3471 (s->s3->tmp.cert_req == 3472 2) ? NULL : s->cert->key)) { 3473 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR); 3474 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); 3475 s->state = SSL_ST_ERR; 3476 return 0; 3477 } 3478 } 3479 /* SSL3_ST_CW_CERT_D */ 3480 return ssl_do_write(s); 3481} 3482 3483#define has_bits(i,m) (((i)&(m)) == (m)) 3484 3485int ssl3_check_cert_and_algorithm(SSL *s) 3486{ 3487 int i, idx; 3488 long alg_k, alg_a; 3489 EVP_PKEY *pkey = NULL; 3490 int pkey_bits; 3491 SESS_CERT *sc; 3492#ifndef OPENSSL_NO_RSA 3493 RSA *rsa; 3494#endif 3495#ifndef OPENSSL_NO_DH 3496 DH *dh; 3497#endif 3498 int al = SSL_AD_HANDSHAKE_FAILURE; 3499 3500 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 3501 alg_a = s->s3->tmp.new_cipher->algorithm_auth; 3502 3503 /* we don't have a certificate */ 3504 if ((alg_a & (SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK)) 3505 return (1); 3506 3507 sc = s->session->sess_cert; 3508 if (sc == NULL) { 3509 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3510 goto err; 3511 } 3512#ifndef OPENSSL_NO_RSA 3513 rsa = s->session->sess_cert->peer_rsa_tmp; 3514#endif 3515#ifndef OPENSSL_NO_DH 3516 dh = s->session->sess_cert->peer_dh_tmp; 3517#endif 3518 3519 /* This is the passed certificate */ 3520 3521 idx = sc->peer_cert_type; 3522#ifndef OPENSSL_NO_ECDH 3523 if (idx == SSL_PKEY_ECC) { 3524 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) { 3525 /* check failed */ 3526 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT); 3527 goto f_err; 3528 } else { 3529 return 1; 3530 } 3531 } else if (alg_a & SSL_aECDSA) { 3532 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3533 SSL_R_MISSING_ECDSA_SIGNING_CERT); 3534 goto f_err; 3535 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) { 3536 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT); 3537 goto f_err; 3538 } 3539#endif 3540 pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509); 3541 pkey_bits = EVP_PKEY_bits(pkey); 3542 i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey); 3543 EVP_PKEY_free(pkey); 3544 3545 /* Check that we have a certificate if we require one */ 3546 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) { 3547 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3548 SSL_R_MISSING_RSA_SIGNING_CERT); 3549 goto f_err; 3550 } 3551#ifndef OPENSSL_NO_DSA 3552 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) { 3553 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3554 SSL_R_MISSING_DSA_SIGNING_CERT); 3555 goto f_err; 3556 } 3557#endif 3558#ifndef OPENSSL_NO_RSA 3559 if (alg_k & SSL_kRSA) { 3560 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 3561 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { 3562 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3563 SSL_R_MISSING_RSA_ENCRYPTING_CERT); 3564 goto f_err; 3565 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) { 3566 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3567 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) { 3568 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3569 SSL_R_MISSING_RSA_ENCRYPTING_CERT); 3570 goto f_err; 3571 } 3572 if (rsa != NULL) { 3573 /* server key exchange is not allowed. */ 3574 al = SSL_AD_INTERNAL_ERROR; 3575 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3576 goto f_err; 3577 } 3578 } 3579 } 3580 } 3581#endif 3582#ifndef OPENSSL_NO_DH 3583 if ((alg_k & SSL_kEDH) && dh == NULL) { 3584 al = SSL_AD_INTERNAL_ERROR; 3585 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR); 3586 goto f_err; 3587 } 3588 if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) && 3589 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) { 3590 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3591 SSL_R_MISSING_DH_RSA_CERT); 3592 goto f_err; 3593 } 3594# ifndef OPENSSL_NO_DSA 3595 if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) && 3596 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) { 3597 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3598 SSL_R_MISSING_DH_DSA_CERT); 3599 goto f_err; 3600 } 3601# endif 3602 3603 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) { 3604 int dh_size; 3605 if (alg_k & SSL_kDHE) { 3606 dh_size = BN_num_bits(dh->p); 3607 } else { 3608 DH *dh_srvr = get_server_static_dh_key(sc); 3609 if (dh_srvr == NULL) 3610 goto f_err; 3611 dh_size = BN_num_bits(dh_srvr->p); 3612 DH_free(dh_srvr); 3613 } 3614 3615 if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 1024) 3616 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) { 3617 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL); 3618 goto f_err; 3619 } 3620 } 3621#endif /* !OPENSSL_NO_DH */ 3622 3623 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 3624 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3625#ifndef OPENSSL_NO_RSA 3626 if (alg_k & SSL_kRSA) { 3627 if (rsa == NULL) { 3628 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3629 SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 3630 goto f_err; 3631 } else if (BN_num_bits(rsa->n) > 3632 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3633 /* We have a temporary RSA key but it's too large. */ 3634 al = SSL_AD_EXPORT_RESTRICTION; 3635 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3636 SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 3637 goto f_err; 3638 } 3639 } else 3640#endif 3641#ifndef OPENSSL_NO_DH 3642 if (alg_k & SSL_kDHE) { 3643 if (BN_num_bits(dh->p) > 3644 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) { 3645 /* We have a temporary DH key but it's too large. */ 3646 al = SSL_AD_EXPORT_RESTRICTION; 3647 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3648 SSL_R_MISSING_EXPORT_TMP_DH_KEY); 3649 goto f_err; 3650 } 3651 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) { 3652 /* The cert should have had an export DH key. */ 3653 al = SSL_AD_EXPORT_RESTRICTION; 3654 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3655 SSL_R_MISSING_EXPORT_TMP_DH_KEY); 3656 goto f_err; 3657 } else 3658#endif 3659 { 3660 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, 3661 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 3662 goto f_err; 3663 } 3664 } 3665 return (1); 3666 f_err: 3667 ssl3_send_alert(s, SSL3_AL_FATAL, al); 3668 err: 3669 return (0); 3670} 3671 3672#ifndef OPENSSL_NO_TLSEXT 3673/* 3674 * Normally, we can tell if the server is resuming the session from 3675 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server 3676 * message after the ServerHello to determine if the server is resuming. 3677 * Therefore, we allow EAP-FAST to peek ahead. 3678 * ssl3_check_finished returns 1 if we are resuming from an external 3679 * pre-shared secret, we have a "ticket" and the next server handshake message 3680 * is Finished; and 0 otherwise. It returns -1 upon an error. 3681 */ 3682static int ssl3_check_finished(SSL *s) 3683{ 3684 int ok = 0; 3685 3686 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb || 3687 !s->session->tlsext_tick) 3688 return 0; 3689 3690 /* Need to permit this temporarily, in case the next message is Finished. */ 3691 s->s3->flags |= SSL3_FLAGS_CCS_OK; 3692 /* 3693 * This function is called when we might get a Certificate message instead, 3694 * so permit appropriate message length. 3695 * We ignore the return value as we're only interested in the message type 3696 * and not its length. 3697 */ 3698 s->method->ssl_get_message(s, 3699 SSL3_ST_CR_CERT_A, 3700 SSL3_ST_CR_CERT_B, 3701 -1, s->max_cert_list, &ok); 3702 s->s3->flags &= ~SSL3_FLAGS_CCS_OK; 3703 3704 if (!ok) 3705 return -1; 3706 3707 s->s3->tmp.reuse_message = 1; 3708 3709 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) 3710 return 1; 3711 3712 /* If we're not done, then the CCS arrived early and we should bail. */ 3713 if (s->s3->change_cipher_spec) { 3714 SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY); 3715 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); 3716 return -1; 3717 } 3718 3719 return 0; 3720} 3721 3722# ifndef OPENSSL_NO_NEXTPROTONEG 3723int ssl3_send_next_proto(SSL *s) 3724{ 3725 unsigned int len, padding_len; 3726 unsigned char *d; 3727 3728 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) { 3729 len = s->next_proto_negotiated_len; 3730 padding_len = 32 - ((len + 2) % 32); 3731 d = (unsigned char *)s->init_buf->data; 3732 d[4] = len; 3733 memcpy(d + 5, s->next_proto_negotiated, len); 3734 d[5 + len] = padding_len; 3735 memset(d + 6 + len, 0, padding_len); 3736 *(d++) = SSL3_MT_NEXT_PROTO; 3737 l2n3(2 + len + padding_len, d); 3738 s->state = SSL3_ST_CW_NEXT_PROTO_B; 3739 s->init_num = 4 + 2 + len + padding_len; 3740 s->init_off = 0; 3741 } 3742 3743 return ssl3_do_write(s, SSL3_RT_HANDSHAKE); 3744} 3745#endif /* !OPENSSL_NO_NEXTPROTONEG */ 3746#endif /* !OPENSSL_NO_TLSEXT */ 3747 3748int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) 3749{ 3750 int i = 0; 3751#ifndef OPENSSL_NO_ENGINE 3752 if (s->ctx->client_cert_engine) { 3753 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, 3754 SSL_get_client_CA_list(s), 3755 px509, ppkey, NULL, NULL, NULL); 3756 if (i != 0) 3757 return i; 3758 } 3759#endif 3760 if (s->ctx->client_cert_cb) 3761 i = s->ctx->client_cert_cb(s, px509, ppkey); 3762 return i; 3763} 3764