1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */ 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 #define REUSE_CIPHER_BUG 152 #define NETSCAPE_HANG_BUG 153 154 #include <stdio.h> 155 #include "ssl_locl.h" 156 #include "kssl_lcl.h" 157 #include <openssl/buffer.h> 158 #include <openssl/rand.h> 159 #include <openssl/objects.h> 160 #include <openssl/evp.h> 161 #include <openssl/hmac.h> 162 #include <openssl/x509.h> 163 #ifndef OPENSSL_NO_DH 164 #include <openssl/dh.h> 165 #endif 166 #include <openssl/bn.h> 167 #ifndef OPENSSL_NO_KRB5 168 #include <openssl/krb5_asn.h> 169 #endif 170 #include <openssl/md5.h> 171 172 static const SSL_METHOD *ssl3_get_server_method(int ver); 173 174 static const SSL_METHOD *ssl3_get_server_method(int ver) 175 { 176 if (ver == SSL3_VERSION) 177 return(SSLv3_server_method()); 178 else 179 return(NULL); 180 } 181 182 #ifndef OPENSSL_NO_SRP 183 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al) 184 { 185 int ret = SSL_ERROR_NONE; 186 187 *al = SSL_AD_UNRECOGNIZED_NAME; 188 189 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) && 190 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) 191 { 192 if(s->srp_ctx.login == NULL) 193 { 194 /* RFC 5054 says SHOULD reject, 195 we do so if There is no srp login name */ 196 ret = SSL3_AL_FATAL; 197 *al = SSL_AD_UNKNOWN_PSK_IDENTITY; 198 } 199 else 200 { 201 ret = SSL_srp_server_param_with_username(s,al); 202 } 203 } 204 return ret; 205 } 206 #endif 207 208 IMPLEMENT_ssl3_meth_func(SSLv3_server_method, 209 ssl3_accept, 210 ssl_undefined_function, 211 ssl3_get_server_method) 212 213 int ssl3_accept(SSL *s) 214 { 215 BUF_MEM *buf; 216 unsigned long alg_k,Time=(unsigned long)time(NULL); 217 void (*cb)(const SSL *ssl,int type,int val)=NULL; 218 int ret= -1; 219 int new_state,state,skip=0; 220 221 RAND_add(&Time,sizeof(Time),0); 222 ERR_clear_error(); 223 clear_sys_error(); 224 225 if (s->info_callback != NULL) 226 cb=s->info_callback; 227 else if (s->ctx->info_callback != NULL) 228 cb=s->ctx->info_callback; 229 230 /* init things to blank */ 231 s->in_handshake++; 232 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 233 234 if (s->cert == NULL) 235 { 236 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); 237 return(-1); 238 } 239 240 #ifndef OPENSSL_NO_HEARTBEATS 241 /* If we're awaiting a HeartbeatResponse, pretend we 242 * already got and don't await it anymore, because 243 * Heartbeats don't make sense during handshakes anyway. 244 */ 245 if (s->tlsext_hb_pending) 246 { 247 s->tlsext_hb_pending = 0; 248 s->tlsext_hb_seq++; 249 } 250 #endif 251 252 for (;;) 253 { 254 state=s->state; 255 256 switch (s->state) 257 { 258 case SSL_ST_RENEGOTIATE: 259 s->renegotiate=1; 260 /* s->state=SSL_ST_ACCEPT; */ 261 262 case SSL_ST_BEFORE: 263 case SSL_ST_ACCEPT: 264 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 265 case SSL_ST_OK|SSL_ST_ACCEPT: 266 267 s->server=1; 268 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 269 270 if ((s->version>>8) != 3) 271 { 272 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR); 273 return -1; 274 } 275 s->type=SSL_ST_ACCEPT; 276 277 if (s->init_buf == NULL) 278 { 279 if ((buf=BUF_MEM_new()) == NULL) 280 { 281 ret= -1; 282 goto end; 283 } 284 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 285 { 286 ret= -1; 287 goto end; 288 } 289 s->init_buf=buf; 290 } 291 292 if (!ssl3_setup_buffers(s)) 293 { 294 ret= -1; 295 goto end; 296 } 297 298 s->init_num=0; 299 s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE; 300 301 if (s->state != SSL_ST_RENEGOTIATE) 302 { 303 /* Ok, we now need to push on a buffering BIO so that 304 * the output is sent in a way that TCP likes :-) 305 */ 306 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } 307 308 ssl3_init_finished_mac(s); 309 s->state=SSL3_ST_SR_CLNT_HELLO_A; 310 s->ctx->stats.sess_accept++; 311 } 312 else if (!s->s3->send_connection_binding && 313 !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) 314 { 315 /* Server attempting to renegotiate with 316 * client that doesn't support secure 317 * renegotiation. 318 */ 319 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); 320 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 321 ret = -1; 322 goto end; 323 } 324 else 325 { 326 /* s->state == SSL_ST_RENEGOTIATE, 327 * we will just send a HelloRequest */ 328 s->ctx->stats.sess_accept_renegotiate++; 329 s->state=SSL3_ST_SW_HELLO_REQ_A; 330 } 331 break; 332 333 case SSL3_ST_SW_HELLO_REQ_A: 334 case SSL3_ST_SW_HELLO_REQ_B: 335 336 s->shutdown=0; 337 ret=ssl3_send_hello_request(s); 338 if (ret <= 0) goto end; 339 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; 340 s->state=SSL3_ST_SW_FLUSH; 341 s->init_num=0; 342 343 ssl3_init_finished_mac(s); 344 break; 345 346 case SSL3_ST_SW_HELLO_REQ_C: 347 s->state=SSL_ST_OK; 348 break; 349 350 case SSL3_ST_SR_CLNT_HELLO_A: 351 case SSL3_ST_SR_CLNT_HELLO_B: 352 case SSL3_ST_SR_CLNT_HELLO_C: 353 354 s->shutdown=0; 355 if (s->rwstate != SSL_X509_LOOKUP) 356 { 357 ret=ssl3_get_client_hello(s); 358 if (ret <= 0) goto end; 359 } 360 #ifndef OPENSSL_NO_SRP 361 { 362 int al; 363 if ((ret = ssl_check_srp_ext_ClientHello(s,&al)) < 0) 364 { 365 /* callback indicates firther work to be done */ 366 s->rwstate=SSL_X509_LOOKUP; 367 goto end; 368 } 369 if (ret != SSL_ERROR_NONE) 370 { 371 ssl3_send_alert(s,SSL3_AL_FATAL,al); 372 /* This is not really an error but the only means to 373 for a client to detect whether srp is supported. */ 374 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY) 375 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_CLIENTHELLO_TLSEXT); 376 ret = SSL_TLSEXT_ERR_ALERT_FATAL; 377 ret= -1; 378 goto end; 379 } 380 } 381 #endif 382 383 s->renegotiate = 2; 384 s->state=SSL3_ST_SW_SRVR_HELLO_A; 385 s->init_num=0; 386 break; 387 388 case SSL3_ST_SW_SRVR_HELLO_A: 389 case SSL3_ST_SW_SRVR_HELLO_B: 390 ret=ssl3_send_server_hello(s); 391 if (ret <= 0) goto end; 392 #ifndef OPENSSL_NO_TLSEXT 393 if (s->hit) 394 { 395 if (s->tlsext_ticket_expected) 396 s->state=SSL3_ST_SW_SESSION_TICKET_A; 397 else 398 s->state=SSL3_ST_SW_CHANGE_A; 399 } 400 #else 401 if (s->hit) 402 s->state=SSL3_ST_SW_CHANGE_A; 403 #endif 404 else 405 s->state=SSL3_ST_SW_CERT_A; 406 s->init_num=0; 407 break; 408 409 case SSL3_ST_SW_CERT_A: 410 case SSL3_ST_SW_CERT_B: 411 /* Check if it is anon DH or anon ECDH, */ 412 /* normal PSK or KRB5 or SRP */ 413 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) 414 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) 415 && !(s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 416 { 417 ret=ssl3_send_server_certificate(s); 418 if (ret <= 0) goto end; 419 #ifndef OPENSSL_NO_TLSEXT 420 if (s->tlsext_status_expected) 421 s->state=SSL3_ST_SW_CERT_STATUS_A; 422 else 423 s->state=SSL3_ST_SW_KEY_EXCH_A; 424 } 425 else 426 { 427 skip = 1; 428 s->state=SSL3_ST_SW_KEY_EXCH_A; 429 } 430 #else 431 } 432 else 433 skip=1; 434 435 s->state=SSL3_ST_SW_KEY_EXCH_A; 436 #endif 437 s->init_num=0; 438 break; 439 440 case SSL3_ST_SW_KEY_EXCH_A: 441 case SSL3_ST_SW_KEY_EXCH_B: 442 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 443 444 /* clear this, it may get reset by 445 * send_server_key_exchange */ 446 if ((s->options & SSL_OP_EPHEMERAL_RSA) 447 #ifndef OPENSSL_NO_KRB5 448 && !(alg_k & SSL_kKRB5) 449 #endif /* OPENSSL_NO_KRB5 */ 450 ) 451 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key 452 * even when forbidden by protocol specs 453 * (handshake may fail as clients are not required to 454 * be able to handle this) */ 455 s->s3->tmp.use_rsa_tmp=1; 456 else 457 s->s3->tmp.use_rsa_tmp=0; 458 459 460 /* only send if a DH key exchange, fortezza or 461 * RSA but we have a sign only certificate 462 * 463 * PSK: may send PSK identity hints 464 * 465 * For ECC ciphersuites, we send a serverKeyExchange 466 * message only if the cipher suite is either 467 * ECDH-anon or ECDHE. In other cases, the 468 * server certificate contains the server's 469 * public key for key exchange. 470 */ 471 if (s->s3->tmp.use_rsa_tmp 472 /* PSK: send ServerKeyExchange if PSK identity 473 * hint if provided */ 474 #ifndef OPENSSL_NO_PSK 475 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) 476 #endif 477 #ifndef OPENSSL_NO_SRP 478 /* SRP: send ServerKeyExchange */ 479 || (alg_k & SSL_kSRP) 480 #endif 481 || (alg_k & (SSL_kDHr|SSL_kDHd|SSL_kEDH)) 482 || (alg_k & SSL_kEECDH) 483 || ((alg_k & SSL_kRSA) 484 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL 485 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) 486 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) 487 ) 488 ) 489 ) 490 ) 491 { 492 ret=ssl3_send_server_key_exchange(s); 493 if (ret <= 0) goto end; 494 } 495 else 496 skip=1; 497 498 s->state=SSL3_ST_SW_CERT_REQ_A; 499 s->init_num=0; 500 break; 501 502 case SSL3_ST_SW_CERT_REQ_A: 503 case SSL3_ST_SW_CERT_REQ_B: 504 if (/* don't request cert unless asked for it: */ 505 !(s->verify_mode & SSL_VERIFY_PEER) || 506 /* if SSL_VERIFY_CLIENT_ONCE is set, 507 * don't request cert during re-negotiation: */ 508 ((s->session->peer != NULL) && 509 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 510 /* never request cert in anonymous ciphersuites 511 * (see section "Certificate request" in SSL 3 drafts 512 * and in RFC 2246): */ 513 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && 514 /* ... except when the application insists on verification 515 * (against the specs, but s3_clnt.c accepts this for SSL 3) */ 516 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || 517 /* never request cert in Kerberos ciphersuites */ 518 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) 519 /* With normal PSK Certificates and 520 * Certificate Requests are omitted */ 521 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 522 { 523 /* no cert request */ 524 skip=1; 525 s->s3->tmp.cert_request=0; 526 s->state=SSL3_ST_SW_SRVR_DONE_A; 527 if (s->s3->handshake_buffer) 528 if (!ssl3_digest_cached_records(s)) 529 return -1; 530 } 531 else 532 { 533 s->s3->tmp.cert_request=1; 534 ret=ssl3_send_certificate_request(s); 535 if (ret <= 0) goto end; 536 #ifndef NETSCAPE_HANG_BUG 537 s->state=SSL3_ST_SW_SRVR_DONE_A; 538 #else 539 s->state=SSL3_ST_SW_FLUSH; 540 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 541 #endif 542 s->init_num=0; 543 } 544 break; 545 546 case SSL3_ST_SW_SRVR_DONE_A: 547 case SSL3_ST_SW_SRVR_DONE_B: 548 ret=ssl3_send_server_done(s); 549 if (ret <= 0) goto end; 550 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 551 s->state=SSL3_ST_SW_FLUSH; 552 s->init_num=0; 553 break; 554 555 case SSL3_ST_SW_FLUSH: 556 557 /* This code originally checked to see if 558 * any data was pending using BIO_CTRL_INFO 559 * and then flushed. This caused problems 560 * as documented in PR#1939. The proposed 561 * fix doesn't completely resolve this issue 562 * as buggy implementations of BIO_CTRL_PENDING 563 * still exist. So instead we just flush 564 * unconditionally. 565 */ 566 567 s->rwstate=SSL_WRITING; 568 if (BIO_flush(s->wbio) <= 0) 569 { 570 ret= -1; 571 goto end; 572 } 573 s->rwstate=SSL_NOTHING; 574 575 s->state=s->s3->tmp.next_state; 576 break; 577 578 case SSL3_ST_SR_CERT_A: 579 case SSL3_ST_SR_CERT_B: 580 /* Check for second client hello (MS SGC) */ 581 ret = ssl3_check_client_hello(s); 582 if (ret <= 0) 583 goto end; 584 if (ret == 2) 585 s->state = SSL3_ST_SR_CLNT_HELLO_C; 586 else { 587 if (s->s3->tmp.cert_request) 588 { 589 ret=ssl3_get_client_certificate(s); 590 if (ret <= 0) goto end; 591 } 592 s->init_num=0; 593 s->state=SSL3_ST_SR_KEY_EXCH_A; 594 } 595 break; 596 597 case SSL3_ST_SR_KEY_EXCH_A: 598 case SSL3_ST_SR_KEY_EXCH_B: 599 ret=ssl3_get_client_key_exchange(s); 600 if (ret <= 0) 601 goto end; 602 if (ret == 2) 603 { 604 /* For the ECDH ciphersuites when 605 * the client sends its ECDH pub key in 606 * a certificate, the CertificateVerify 607 * message is not sent. 608 * Also for GOST ciphersuites when 609 * the client uses its key from the certificate 610 * for key exchange. 611 */ 612 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 613 s->state=SSL3_ST_SR_FINISHED_A; 614 #else 615 if (s->s3->next_proto_neg_seen) 616 s->state=SSL3_ST_SR_NEXT_PROTO_A; 617 else 618 s->state=SSL3_ST_SR_FINISHED_A; 619 #endif 620 s->init_num = 0; 621 } 622 else if (TLS1_get_version(s) >= TLS1_2_VERSION) 623 { 624 s->state=SSL3_ST_SR_CERT_VRFY_A; 625 s->init_num=0; 626 if (!s->session->peer) 627 break; 628 /* For TLS v1.2 freeze the handshake buffer 629 * at this point and digest cached records. 630 */ 631 if (!s->s3->handshake_buffer) 632 { 633 SSLerr(SSL_F_SSL3_ACCEPT,ERR_R_INTERNAL_ERROR); 634 return -1; 635 } 636 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE; 637 if (!ssl3_digest_cached_records(s)) 638 return -1; 639 } 640 else 641 { 642 int offset=0; 643 int dgst_num; 644 645 s->state=SSL3_ST_SR_CERT_VRFY_A; 646 s->init_num=0; 647 648 /* We need to get hashes here so if there is 649 * a client cert, it can be verified 650 * FIXME - digest processing for CertificateVerify 651 * should be generalized. But it is next step 652 */ 653 if (s->s3->handshake_buffer) 654 if (!ssl3_digest_cached_records(s)) 655 return -1; 656 if (s->s3->handshake_dgst != NULL) { 657 for (dgst_num=0; dgst_num<SSL_MAX_DIGEST;dgst_num++) 658 if (s->s3->handshake_dgst[dgst_num]) 659 { 660 int dgst_size; 661 662 s->method->ssl3_enc->cert_verify_mac(s,EVP_MD_CTX_type(s->s3->handshake_dgst[dgst_num]),&(s->s3->tmp.cert_verify_md[offset])); 663 dgst_size=EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]); 664 if (dgst_size < 0) 665 { 666 ret = -1; 667 goto end; 668 } 669 offset+=dgst_size; 670 } 671 } 672 } 673 break; 674 675 case SSL3_ST_SR_CERT_VRFY_A: 676 case SSL3_ST_SR_CERT_VRFY_B: 677 678 /* we should decide if we expected this one */ 679 ret=ssl3_get_cert_verify(s); 680 if (ret <= 0) goto end; 681 682 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 683 s->state=SSL3_ST_SR_FINISHED_A; 684 #else 685 if (s->s3->next_proto_neg_seen) 686 s->state=SSL3_ST_SR_NEXT_PROTO_A; 687 else 688 s->state=SSL3_ST_SR_FINISHED_A; 689 #endif 690 s->init_num=0; 691 break; 692 693 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 694 case SSL3_ST_SR_NEXT_PROTO_A: 695 case SSL3_ST_SR_NEXT_PROTO_B: 696 ret=ssl3_get_next_proto(s); 697 if (ret <= 0) goto end; 698 s->init_num = 0; 699 s->state=SSL3_ST_SR_FINISHED_A; 700 break; 701 #endif 702 703 case SSL3_ST_SR_FINISHED_A: 704 case SSL3_ST_SR_FINISHED_B: 705 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, 706 SSL3_ST_SR_FINISHED_B); 707 if (ret <= 0) goto end; 708 if (s->hit) 709 s->state=SSL_ST_OK; 710 #ifndef OPENSSL_NO_TLSEXT 711 else if (s->tlsext_ticket_expected) 712 s->state=SSL3_ST_SW_SESSION_TICKET_A; 713 #endif 714 else 715 s->state=SSL3_ST_SW_CHANGE_A; 716 s->init_num=0; 717 break; 718 719 #ifndef OPENSSL_NO_TLSEXT 720 case SSL3_ST_SW_SESSION_TICKET_A: 721 case SSL3_ST_SW_SESSION_TICKET_B: 722 ret=ssl3_send_newsession_ticket(s); 723 if (ret <= 0) goto end; 724 s->state=SSL3_ST_SW_CHANGE_A; 725 s->init_num=0; 726 break; 727 728 case SSL3_ST_SW_CERT_STATUS_A: 729 case SSL3_ST_SW_CERT_STATUS_B: 730 ret=ssl3_send_cert_status(s); 731 if (ret <= 0) goto end; 732 s->state=SSL3_ST_SW_KEY_EXCH_A; 733 s->init_num=0; 734 break; 735 736 #endif 737 738 case SSL3_ST_SW_CHANGE_A: 739 case SSL3_ST_SW_CHANGE_B: 740 741 s->session->cipher=s->s3->tmp.new_cipher; 742 if (!s->method->ssl3_enc->setup_key_block(s)) 743 { ret= -1; goto end; } 744 745 ret=ssl3_send_change_cipher_spec(s, 746 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); 747 748 if (ret <= 0) goto end; 749 s->state=SSL3_ST_SW_FINISHED_A; 750 s->init_num=0; 751 752 if (!s->method->ssl3_enc->change_cipher_state(s, 753 SSL3_CHANGE_CIPHER_SERVER_WRITE)) 754 { 755 ret= -1; 756 goto end; 757 } 758 759 break; 760 761 case SSL3_ST_SW_FINISHED_A: 762 case SSL3_ST_SW_FINISHED_B: 763 ret=ssl3_send_finished(s, 764 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, 765 s->method->ssl3_enc->server_finished_label, 766 s->method->ssl3_enc->server_finished_label_len); 767 if (ret <= 0) goto end; 768 s->state=SSL3_ST_SW_FLUSH; 769 if (s->hit) 770 { 771 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 772 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 773 #else 774 if (s->s3->next_proto_neg_seen) 775 s->s3->tmp.next_state=SSL3_ST_SR_NEXT_PROTO_A; 776 else 777 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 778 #endif 779 } 780 else 781 s->s3->tmp.next_state=SSL_ST_OK; 782 s->init_num=0; 783 break; 784 785 case SSL_ST_OK: 786 /* clean a few things up */ 787 ssl3_cleanup_key_block(s); 788 789 BUF_MEM_free(s->init_buf); 790 s->init_buf=NULL; 791 792 /* remove buffering on output */ 793 ssl_free_wbio_buffer(s); 794 795 s->init_num=0; 796 797 if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ 798 { 799 s->renegotiate=0; 800 s->new_session=0; 801 802 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); 803 804 s->ctx->stats.sess_accept_good++; 805 /* s->server=1; */ 806 s->handshake_func=ssl3_accept; 807 808 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 809 } 810 811 ret = 1; 812 goto end; 813 /* break; */ 814 815 default: 816 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); 817 ret= -1; 818 goto end; 819 /* break; */ 820 } 821 822 if (!s->s3->tmp.reuse_message && !skip) 823 { 824 if (s->debug) 825 { 826 if ((ret=BIO_flush(s->wbio)) <= 0) 827 goto end; 828 } 829 830 831 if ((cb != NULL) && (s->state != state)) 832 { 833 new_state=s->state; 834 s->state=state; 835 cb(s,SSL_CB_ACCEPT_LOOP,1); 836 s->state=new_state; 837 } 838 } 839 skip=0; 840 } 841 end: 842 /* BIO_flush(s->wbio); */ 843 844 s->in_handshake--; 845 if (cb != NULL) 846 cb(s,SSL_CB_ACCEPT_EXIT,ret); 847 return(ret); 848 } 849 850 int ssl3_send_hello_request(SSL *s) 851 { 852 unsigned char *p; 853 854 if (s->state == SSL3_ST_SW_HELLO_REQ_A) 855 { 856 p=(unsigned char *)s->init_buf->data; 857 *(p++)=SSL3_MT_HELLO_REQUEST; 858 *(p++)=0; 859 *(p++)=0; 860 *(p++)=0; 861 862 s->state=SSL3_ST_SW_HELLO_REQ_B; 863 /* number of bytes to write */ 864 s->init_num=4; 865 s->init_off=0; 866 } 867 868 /* SSL3_ST_SW_HELLO_REQ_B */ 869 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 870 } 871 872 int ssl3_check_client_hello(SSL *s) 873 { 874 int ok; 875 long n; 876 877 /* We only allow the client to restart the handshake once per 878 * negotiation. */ 879 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) 880 { 881 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC_RESTARTS); 882 return -1; 883 } 884 885 /* this function is called when we really expect a Certificate message, 886 * so permit appropriate message length */ 887 n=s->method->ssl_get_message(s, 888 SSL3_ST_SR_CERT_A, 889 SSL3_ST_SR_CERT_B, 890 -1, 891 s->max_cert_list, 892 &ok); 893 if (!ok) return((int)n); 894 s->s3->tmp.reuse_message = 1; 895 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) 896 { 897 /* We only allow the client to restart the handshake once per 898 * negotiation. */ 899 if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) 900 { 901 SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC_RESTARTS); 902 return -1; 903 } 904 /* Throw away what we have done so far in the current handshake, 905 * which will now be aborted. (A full SSL_clear would be too much.) */ 906 #ifndef OPENSSL_NO_DH 907 if (s->s3->tmp.dh != NULL) 908 { 909 DH_free(s->s3->tmp.dh); 910 s->s3->tmp.dh = NULL; 911 } 912 #endif 913 #ifndef OPENSSL_NO_ECDH 914 if (s->s3->tmp.ecdh != NULL) 915 { 916 EC_KEY_free(s->s3->tmp.ecdh); 917 s->s3->tmp.ecdh = NULL; 918 } 919 #endif 920 s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE; 921 return 2; 922 } 923 return 1; 924 } 925 926 int ssl3_get_client_hello(SSL *s) 927 { 928 int i,j,ok,al,ret= -1; 929 unsigned int cookie_len; 930 long n; 931 unsigned long id; 932 unsigned char *p,*d,*q; 933 SSL_CIPHER *c; 934 #ifndef OPENSSL_NO_COMP 935 SSL_COMP *comp=NULL; 936 #endif 937 STACK_OF(SSL_CIPHER) *ciphers=NULL; 938 939 /* We do this so that we will respond with our native type. 940 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 941 * This down switching should be handled by a different method. 942 * If we are SSLv3, we will respond with SSLv3, even if prompted with 943 * TLSv1. 944 */ 945 if (s->state == SSL3_ST_SR_CLNT_HELLO_A 946 ) 947 { 948 s->state=SSL3_ST_SR_CLNT_HELLO_B; 949 } 950 s->first_packet=1; 951 n=s->method->ssl_get_message(s, 952 SSL3_ST_SR_CLNT_HELLO_B, 953 SSL3_ST_SR_CLNT_HELLO_C, 954 SSL3_MT_CLIENT_HELLO, 955 SSL3_RT_MAX_PLAIN_LENGTH, 956 &ok); 957 958 if (!ok) return((int)n); 959 s->first_packet=0; 960 d=p=(unsigned char *)s->init_msg; 961 962 /* use version from inside client hello, not from record header 963 * (may differ: see RFC 2246, Appendix E, second paragraph) */ 964 s->client_version=(((int)p[0])<<8)|(int)p[1]; 965 p+=2; 966 967 if ((s->version == DTLS1_VERSION && s->client_version > s->version) || 968 (s->version != DTLS1_VERSION && s->client_version < s->version)) 969 { 970 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); 971 if ((s->client_version>>8) == SSL3_VERSION_MAJOR) 972 { 973 /* similar to ssl3_get_record, send alert using remote version number */ 974 s->version = s->client_version; 975 } 976 al = SSL_AD_PROTOCOL_VERSION; 977 goto f_err; 978 } 979 980 /* If we require cookies and this ClientHello doesn't 981 * contain one, just return since we do not want to 982 * allocate any memory yet. So check cookie length... 983 */ 984 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) 985 { 986 unsigned int session_length, cookie_length; 987 988 session_length = *(p + SSL3_RANDOM_SIZE); 989 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1); 990 991 if (cookie_length == 0) 992 return 1; 993 } 994 995 /* load the client random */ 996 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); 997 p+=SSL3_RANDOM_SIZE; 998 999 /* get the session-id */ 1000 j= *(p++); 1001 1002 s->hit=0; 1003 /* Versions before 0.9.7 always allow clients to resume sessions in renegotiation. 1004 * 0.9.7 and later allow this by default, but optionally ignore resumption requests 1005 * with flag SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather 1006 * than a change to default behavior so that applications relying on this for security 1007 * won't even compile against older library versions). 1008 * 1009 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to request 1010 * renegotiation but not a new session (s->new_session remains unset): for servers, 1011 * this essentially just means that the SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 1012 * setting will be ignored. 1013 */ 1014 if ((s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) 1015 { 1016 if (!ssl_get_new_session(s,1)) 1017 goto err; 1018 } 1019 else 1020 { 1021 i=ssl_get_prev_session(s, p, j, d + n); 1022 if (i == 1) 1023 { /* previous session */ 1024 s->hit=1; 1025 } 1026 else if (i == -1) 1027 goto err; 1028 else /* i == 0 */ 1029 { 1030 if (!ssl_get_new_session(s,1)) 1031 goto err; 1032 } 1033 } 1034 1035 p+=j; 1036 1037 if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) 1038 { 1039 /* cookie stuff */ 1040 cookie_len = *(p++); 1041 1042 /* 1043 * The ClientHello may contain a cookie even if the 1044 * HelloVerify message has not been sent--make sure that it 1045 * does not cause an overflow. 1046 */ 1047 if ( cookie_len > sizeof(s->d1->rcvd_cookie)) 1048 { 1049 /* too much data */ 1050 al = SSL_AD_DECODE_ERROR; 1051 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH); 1052 goto f_err; 1053 } 1054 1055 /* verify the cookie if appropriate option is set. */ 1056 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && 1057 cookie_len > 0) 1058 { 1059 memcpy(s->d1->rcvd_cookie, p, cookie_len); 1060 1061 if ( s->ctx->app_verify_cookie_cb != NULL) 1062 { 1063 if ( s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie, 1064 cookie_len) == 0) 1065 { 1066 al=SSL_AD_HANDSHAKE_FAILURE; 1067 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 1068 SSL_R_COOKIE_MISMATCH); 1069 goto f_err; 1070 } 1071 /* else cookie verification succeeded */ 1072 } 1073 else if ( memcmp(s->d1->rcvd_cookie, s->d1->cookie, 1074 s->d1->cookie_len) != 0) /* default verification */ 1075 { 1076 al=SSL_AD_HANDSHAKE_FAILURE; 1077 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, 1078 SSL_R_COOKIE_MISMATCH); 1079 goto f_err; 1080 } 1081 1082 ret = 2; 1083 } 1084 1085 p += cookie_len; 1086 } 1087 1088 n2s(p,i); 1089 if ((i == 0) && (j != 0)) 1090 { 1091 /* we need a cipher if we are not resuming a session */ 1092 al=SSL_AD_ILLEGAL_PARAMETER; 1093 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); 1094 goto f_err; 1095 } 1096 if ((p+i) >= (d+n)) 1097 { 1098 /* not enough data */ 1099 al=SSL_AD_DECODE_ERROR; 1100 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 1101 goto f_err; 1102 } 1103 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) 1104 == NULL)) 1105 { 1106 goto err; 1107 } 1108 p+=i; 1109 1110 /* If it is a hit, check that the cipher is in the list */ 1111 if ((s->hit) && (i > 0)) 1112 { 1113 j=0; 1114 id=s->session->cipher->id; 1115 1116 #ifdef CIPHER_DEBUG 1117 printf("client sent %d ciphers\n",sk_num(ciphers)); 1118 #endif 1119 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) 1120 { 1121 c=sk_SSL_CIPHER_value(ciphers,i); 1122 #ifdef CIPHER_DEBUG 1123 printf("client [%2d of %2d]:%s\n", 1124 i,sk_num(ciphers),SSL_CIPHER_get_name(c)); 1125 #endif 1126 if (c->id == id) 1127 { 1128 j=1; 1129 break; 1130 } 1131 } 1132 /* Disabled because it can be used in a ciphersuite downgrade 1133 * attack: CVE-2010-4180. 1134 */ 1135 #if 0 1136 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) 1137 { 1138 /* Special case as client bug workaround: the previously used cipher may 1139 * not be in the current list, the client instead might be trying to 1140 * continue using a cipher that before wasn't chosen due to server 1141 * preferences. We'll have to reject the connection if the cipher is not 1142 * enabled, though. */ 1143 c = sk_SSL_CIPHER_value(ciphers, 0); 1144 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) 1145 { 1146 s->session->cipher = c; 1147 j = 1; 1148 } 1149 } 1150 #endif 1151 if (j == 0) 1152 { 1153 /* we need to have the cipher in the cipher 1154 * list if we are asked to reuse it */ 1155 al=SSL_AD_ILLEGAL_PARAMETER; 1156 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); 1157 goto f_err; 1158 } 1159 } 1160 1161 /* compression */ 1162 i= *(p++); 1163 if ((p+i) > (d+n)) 1164 { 1165 /* not enough data */ 1166 al=SSL_AD_DECODE_ERROR; 1167 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 1168 goto f_err; 1169 } 1170 q=p; 1171 for (j=0; j<i; j++) 1172 { 1173 if (p[j] == 0) break; 1174 } 1175 1176 p+=i; 1177 if (j >= i) 1178 { 1179 /* no compress */ 1180 al=SSL_AD_DECODE_ERROR; 1181 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); 1182 goto f_err; 1183 } 1184 1185 #ifndef OPENSSL_NO_TLSEXT 1186 /* TLS extensions*/ 1187 if (s->version >= SSL3_VERSION) 1188 { 1189 if (!ssl_parse_clienthello_tlsext(s,&p,d,n, &al)) 1190 { 1191 /* 'al' set by ssl_parse_clienthello_tlsext */ 1192 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_PARSE_TLSEXT); 1193 goto f_err; 1194 } 1195 } 1196 if (ssl_check_clienthello_tlsext_early(s) <= 0) { 1197 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT); 1198 goto err; 1199 } 1200 1201 /* Check if we want to use external pre-shared secret for this 1202 * handshake for not reused session only. We need to generate 1203 * server_random before calling tls_session_secret_cb in order to allow 1204 * SessionTicket processing to use it in key derivation. */ 1205 { 1206 unsigned long Time; 1207 unsigned char *pos; 1208 Time=(unsigned long)time(NULL); /* Time */ 1209 pos=s->s3->server_random; 1210 l2n(Time,pos); 1211 if (RAND_pseudo_bytes(pos,SSL3_RANDOM_SIZE-4) <= 0) 1212 { 1213 al=SSL_AD_INTERNAL_ERROR; 1214 goto f_err; 1215 } 1216 } 1217 1218 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) 1219 { 1220 SSL_CIPHER *pref_cipher=NULL; 1221 1222 s->session->master_key_length=sizeof(s->session->master_key); 1223 if(s->tls_session_secret_cb(s, s->session->master_key, &s->session->master_key_length, 1224 ciphers, &pref_cipher, s->tls_session_secret_cb_arg)) 1225 { 1226 s->hit=1; 1227 s->session->ciphers=ciphers; 1228 s->session->verify_result=X509_V_OK; 1229 1230 ciphers=NULL; 1231 1232 /* check if some cipher was preferred by call back */ 1233 pref_cipher=pref_cipher ? pref_cipher : ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s)); 1234 if (pref_cipher == NULL) 1235 { 1236 al=SSL_AD_HANDSHAKE_FAILURE; 1237 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 1238 goto f_err; 1239 } 1240 1241 s->session->cipher=pref_cipher; 1242 1243 if (s->cipher_list) 1244 sk_SSL_CIPHER_free(s->cipher_list); 1245 1246 if (s->cipher_list_by_id) 1247 sk_SSL_CIPHER_free(s->cipher_list_by_id); 1248 1249 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers); 1250 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers); 1251 } 1252 } 1253 #endif 1254 1255 /* Worst case, we will use the NULL compression, but if we have other 1256 * options, we will now look for them. We have i-1 compression 1257 * algorithms from the client, starting at q. */ 1258 s->s3->tmp.new_compression=NULL; 1259 #ifndef OPENSSL_NO_COMP 1260 /* This only happens if we have a cache hit */ 1261 if (s->session->compress_meth != 0) 1262 { 1263 int m, comp_id = s->session->compress_meth; 1264 /* Perform sanity checks on resumed compression algorithm */ 1265 /* Can't disable compression */ 1266 if (s->options & SSL_OP_NO_COMPRESSION) 1267 { 1268 al=SSL_AD_INTERNAL_ERROR; 1269 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION); 1270 goto f_err; 1271 } 1272 /* Look for resumed compression method */ 1273 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) 1274 { 1275 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); 1276 if (comp_id == comp->id) 1277 { 1278 s->s3->tmp.new_compression=comp; 1279 break; 1280 } 1281 } 1282 if (s->s3->tmp.new_compression == NULL) 1283 { 1284 al=SSL_AD_INTERNAL_ERROR; 1285 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INVALID_COMPRESSION_ALGORITHM); 1286 goto f_err; 1287 } 1288 /* Look for resumed method in compression list */ 1289 for (m = 0; m < i; m++) 1290 { 1291 if (q[m] == comp_id) 1292 break; 1293 } 1294 if (m >= i) 1295 { 1296 al=SSL_AD_ILLEGAL_PARAMETER; 1297 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING); 1298 goto f_err; 1299 } 1300 } 1301 else if (s->hit) 1302 comp = NULL; 1303 else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) 1304 { /* See if we have a match */ 1305 int m,nn,o,v,done=0; 1306 1307 nn=sk_SSL_COMP_num(s->ctx->comp_methods); 1308 for (m=0; m<nn; m++) 1309 { 1310 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); 1311 v=comp->id; 1312 for (o=0; o<i; o++) 1313 { 1314 if (v == q[o]) 1315 { 1316 done=1; 1317 break; 1318 } 1319 } 1320 if (done) break; 1321 } 1322 if (done) 1323 s->s3->tmp.new_compression=comp; 1324 else 1325 comp=NULL; 1326 } 1327 #else 1328 /* If compression is disabled we'd better not try to resume a session 1329 * using compression. 1330 */ 1331 if (s->session->compress_meth != 0) 1332 { 1333 al=SSL_AD_INTERNAL_ERROR; 1334 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_INCONSISTENT_COMPRESSION); 1335 goto f_err; 1336 } 1337 #endif 1338 1339 /* Given s->session->ciphers and SSL_get_ciphers, we must 1340 * pick a cipher */ 1341 1342 if (!s->hit) 1343 { 1344 #ifdef OPENSSL_NO_COMP 1345 s->session->compress_meth=0; 1346 #else 1347 s->session->compress_meth=(comp == NULL)?0:comp->id; 1348 #endif 1349 if (s->session->ciphers != NULL) 1350 sk_SSL_CIPHER_free(s->session->ciphers); 1351 s->session->ciphers=ciphers; 1352 if (ciphers == NULL) 1353 { 1354 al=SSL_AD_ILLEGAL_PARAMETER; 1355 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); 1356 goto f_err; 1357 } 1358 ciphers=NULL; 1359 c=ssl3_choose_cipher(s,s->session->ciphers, 1360 SSL_get_ciphers(s)); 1361 1362 if (c == NULL) 1363 { 1364 al=SSL_AD_HANDSHAKE_FAILURE; 1365 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 1366 goto f_err; 1367 } 1368 s->s3->tmp.new_cipher=c; 1369 } 1370 else 1371 { 1372 /* Session-id reuse */ 1373 #ifdef REUSE_CIPHER_BUG 1374 STACK_OF(SSL_CIPHER) *sk; 1375 SSL_CIPHER *nc=NULL; 1376 SSL_CIPHER *ec=NULL; 1377 1378 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) 1379 { 1380 sk=s->session->ciphers; 1381 for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 1382 { 1383 c=sk_SSL_CIPHER_value(sk,i); 1384 if (c->algorithm_enc & SSL_eNULL) 1385 nc=c; 1386 if (SSL_C_IS_EXPORT(c)) 1387 ec=c; 1388 } 1389 if (nc != NULL) 1390 s->s3->tmp.new_cipher=nc; 1391 else if (ec != NULL) 1392 s->s3->tmp.new_cipher=ec; 1393 else 1394 s->s3->tmp.new_cipher=s->session->cipher; 1395 } 1396 else 1397 #endif 1398 s->s3->tmp.new_cipher=s->session->cipher; 1399 } 1400 1401 if (TLS1_get_version(s) < TLS1_2_VERSION || !(s->verify_mode & SSL_VERIFY_PEER)) 1402 { 1403 if (!ssl3_digest_cached_records(s)) 1404 { 1405 al = SSL_AD_INTERNAL_ERROR; 1406 goto f_err; 1407 } 1408 } 1409 1410 /* we now have the following setup. 1411 * client_random 1412 * cipher_list - our prefered list of ciphers 1413 * ciphers - the clients prefered list of ciphers 1414 * compression - basically ignored right now 1415 * ssl version is set - sslv3 1416 * s->session - The ssl session has been setup. 1417 * s->hit - session reuse flag 1418 * s->tmp.new_cipher - the new cipher to use. 1419 */ 1420 1421 /* Handles TLS extensions that we couldn't check earlier */ 1422 if (s->version >= SSL3_VERSION) 1423 { 1424 if (ssl_check_clienthello_tlsext_late(s) <= 0) 1425 { 1426 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT); 1427 goto err; 1428 } 1429 } 1430 1431 if (ret < 0) ret=1; 1432 if (0) 1433 { 1434 f_err: 1435 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1436 } 1437 err: 1438 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); 1439 return(ret); 1440 } 1441 1442 int ssl3_send_server_hello(SSL *s) 1443 { 1444 unsigned char *buf; 1445 unsigned char *p,*d; 1446 int i,sl; 1447 unsigned long l; 1448 #ifdef OPENSSL_NO_TLSEXT 1449 unsigned long Time; 1450 #endif 1451 1452 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 1453 { 1454 buf=(unsigned char *)s->init_buf->data; 1455 #ifdef OPENSSL_NO_TLSEXT 1456 p=s->s3->server_random; 1457 /* Generate server_random if it was not needed previously */ 1458 Time=(unsigned long)time(NULL); /* Time */ 1459 l2n(Time,p); 1460 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) 1461 return -1; 1462 #endif 1463 /* Do the message type and length last */ 1464 d=p= &(buf[4]); 1465 1466 *(p++)=s->version>>8; 1467 *(p++)=s->version&0xff; 1468 1469 /* Random stuff */ 1470 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 1471 p+=SSL3_RANDOM_SIZE; 1472 1473 /* There are several cases for the session ID to send 1474 * back in the server hello: 1475 * - For session reuse from the session cache, 1476 * we send back the old session ID. 1477 * - If stateless session reuse (using a session ticket) 1478 * is successful, we send back the client's "session ID" 1479 * (which doesn't actually identify the session). 1480 * - If it is a new session, we send back the new 1481 * session ID. 1482 * - However, if we want the new session to be single-use, 1483 * we send back a 0-length session ID. 1484 * s->hit is non-zero in either case of session reuse, 1485 * so the following won't overwrite an ID that we're supposed 1486 * to send back. 1487 */ 1488 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) 1489 && !s->hit) 1490 s->session->session_id_length=0; 1491 1492 sl=s->session->session_id_length; 1493 if (sl > (int)sizeof(s->session->session_id)) 1494 { 1495 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 1496 return -1; 1497 } 1498 *(p++)=sl; 1499 memcpy(p,s->session->session_id,sl); 1500 p+=sl; 1501 1502 /* put the cipher */ 1503 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 1504 p+=i; 1505 1506 /* put the compression method */ 1507 #ifdef OPENSSL_NO_COMP 1508 *(p++)=0; 1509 #else 1510 if (s->s3->tmp.new_compression == NULL) 1511 *(p++)=0; 1512 else 1513 *(p++)=s->s3->tmp.new_compression->id; 1514 #endif 1515 #ifndef OPENSSL_NO_TLSEXT 1516 if (ssl_prepare_serverhello_tlsext(s) <= 0) 1517 { 1518 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT); 1519 return -1; 1520 } 1521 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) 1522 { 1523 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR); 1524 return -1; 1525 } 1526 #endif 1527 /* do the header */ 1528 l=(p-d); 1529 d=buf; 1530 *(d++)=SSL3_MT_SERVER_HELLO; 1531 l2n3(l,d); 1532 1533 s->state=SSL3_ST_SW_SRVR_HELLO_B; 1534 /* number of bytes to write */ 1535 s->init_num=p-buf; 1536 s->init_off=0; 1537 } 1538 1539 /* SSL3_ST_SW_SRVR_HELLO_B */ 1540 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1541 } 1542 1543 int ssl3_send_server_done(SSL *s) 1544 { 1545 unsigned char *p; 1546 1547 if (s->state == SSL3_ST_SW_SRVR_DONE_A) 1548 { 1549 p=(unsigned char *)s->init_buf->data; 1550 1551 /* do the header */ 1552 *(p++)=SSL3_MT_SERVER_DONE; 1553 *(p++)=0; 1554 *(p++)=0; 1555 *(p++)=0; 1556 1557 s->state=SSL3_ST_SW_SRVR_DONE_B; 1558 /* number of bytes to write */ 1559 s->init_num=4; 1560 s->init_off=0; 1561 } 1562 1563 /* SSL3_ST_SW_SRVR_DONE_B */ 1564 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1565 } 1566 1567 int ssl3_send_server_key_exchange(SSL *s) 1568 { 1569 #ifndef OPENSSL_NO_RSA 1570 unsigned char *q; 1571 int j,num; 1572 RSA *rsa; 1573 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1574 unsigned int u; 1575 #endif 1576 #ifndef OPENSSL_NO_DH 1577 DH *dh=NULL,*dhp; 1578 #endif 1579 #ifndef OPENSSL_NO_ECDH 1580 EC_KEY *ecdh=NULL, *ecdhp; 1581 unsigned char *encodedPoint = NULL; 1582 int encodedlen = 0; 1583 int curve_id = 0; 1584 BN_CTX *bn_ctx = NULL; 1585 #endif 1586 EVP_PKEY *pkey; 1587 const EVP_MD *md = NULL; 1588 unsigned char *p,*d; 1589 int al,i; 1590 unsigned long type; 1591 int n; 1592 CERT *cert; 1593 BIGNUM *r[4]; 1594 int nr[4],kn; 1595 BUF_MEM *buf; 1596 EVP_MD_CTX md_ctx; 1597 1598 EVP_MD_CTX_init(&md_ctx); 1599 if (s->state == SSL3_ST_SW_KEY_EXCH_A) 1600 { 1601 type=s->s3->tmp.new_cipher->algorithm_mkey; 1602 cert=s->cert; 1603 1604 buf=s->init_buf; 1605 1606 r[0]=r[1]=r[2]=r[3]=NULL; 1607 n=0; 1608 #ifndef OPENSSL_NO_RSA 1609 if (type & SSL_kRSA) 1610 { 1611 rsa=cert->rsa_tmp; 1612 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 1613 { 1614 rsa=s->cert->rsa_tmp_cb(s, 1615 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1616 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1617 if(rsa == NULL) 1618 { 1619 al=SSL_AD_HANDSHAKE_FAILURE; 1620 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 1621 goto f_err; 1622 } 1623 RSA_up_ref(rsa); 1624 cert->rsa_tmp=rsa; 1625 } 1626 if (rsa == NULL) 1627 { 1628 al=SSL_AD_HANDSHAKE_FAILURE; 1629 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 1630 goto f_err; 1631 } 1632 r[0]=rsa->n; 1633 r[1]=rsa->e; 1634 s->s3->tmp.use_rsa_tmp=1; 1635 } 1636 else 1637 #endif 1638 #ifndef OPENSSL_NO_DH 1639 if (type & SSL_kEDH) 1640 { 1641 dhp=cert->dh_tmp; 1642 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 1643 dhp=s->cert->dh_tmp_cb(s, 1644 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1645 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1646 if (dhp == NULL) 1647 { 1648 al=SSL_AD_HANDSHAKE_FAILURE; 1649 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1650 goto f_err; 1651 } 1652 1653 if (s->s3->tmp.dh != NULL) 1654 { 1655 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1656 goto err; 1657 } 1658 1659 if ((dh=DHparams_dup(dhp)) == NULL) 1660 { 1661 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1662 goto err; 1663 } 1664 1665 s->s3->tmp.dh=dh; 1666 if ((dhp->pub_key == NULL || 1667 dhp->priv_key == NULL || 1668 (s->options & SSL_OP_SINGLE_DH_USE))) 1669 { 1670 if(!DH_generate_key(dh)) 1671 { 1672 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, 1673 ERR_R_DH_LIB); 1674 goto err; 1675 } 1676 } 1677 else 1678 { 1679 dh->pub_key=BN_dup(dhp->pub_key); 1680 dh->priv_key=BN_dup(dhp->priv_key); 1681 if ((dh->pub_key == NULL) || 1682 (dh->priv_key == NULL)) 1683 { 1684 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1685 goto err; 1686 } 1687 } 1688 r[0]=dh->p; 1689 r[1]=dh->g; 1690 r[2]=dh->pub_key; 1691 } 1692 else 1693 #endif 1694 #ifndef OPENSSL_NO_ECDH 1695 if (type & SSL_kEECDH) 1696 { 1697 const EC_GROUP *group; 1698 1699 ecdhp=cert->ecdh_tmp; 1700 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) 1701 { 1702 ecdhp=s->cert->ecdh_tmp_cb(s, 1703 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1704 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1705 } 1706 if (ecdhp == NULL) 1707 { 1708 al=SSL_AD_HANDSHAKE_FAILURE; 1709 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); 1710 goto f_err; 1711 } 1712 1713 if (s->s3->tmp.ecdh != NULL) 1714 { 1715 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1716 goto err; 1717 } 1718 1719 /* Duplicate the ECDH structure. */ 1720 if (ecdhp == NULL) 1721 { 1722 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1723 goto err; 1724 } 1725 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) 1726 { 1727 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1728 goto err; 1729 } 1730 1731 s->s3->tmp.ecdh=ecdh; 1732 if ((EC_KEY_get0_public_key(ecdh) == NULL) || 1733 (EC_KEY_get0_private_key(ecdh) == NULL) || 1734 (s->options & SSL_OP_SINGLE_ECDH_USE)) 1735 { 1736 if(!EC_KEY_generate_key(ecdh)) 1737 { 1738 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1739 goto err; 1740 } 1741 } 1742 1743 if (((group = EC_KEY_get0_group(ecdh)) == NULL) || 1744 (EC_KEY_get0_public_key(ecdh) == NULL) || 1745 (EC_KEY_get0_private_key(ecdh) == NULL)) 1746 { 1747 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1748 goto err; 1749 } 1750 1751 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 1752 (EC_GROUP_get_degree(group) > 163)) 1753 { 1754 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 1755 goto err; 1756 } 1757 1758 /* XXX: For now, we only support ephemeral ECDH 1759 * keys over named (not generic) curves. For 1760 * supported named curves, curve_id is non-zero. 1761 */ 1762 if ((curve_id = 1763 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) 1764 == 0) 1765 { 1766 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1767 goto err; 1768 } 1769 1770 /* Encode the public key. 1771 * First check the size of encoding and 1772 * allocate memory accordingly. 1773 */ 1774 encodedlen = EC_POINT_point2oct(group, 1775 EC_KEY_get0_public_key(ecdh), 1776 POINT_CONVERSION_UNCOMPRESSED, 1777 NULL, 0, NULL); 1778 1779 encodedPoint = (unsigned char *) 1780 OPENSSL_malloc(encodedlen*sizeof(unsigned char)); 1781 bn_ctx = BN_CTX_new(); 1782 if ((encodedPoint == NULL) || (bn_ctx == NULL)) 1783 { 1784 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1785 goto err; 1786 } 1787 1788 1789 encodedlen = EC_POINT_point2oct(group, 1790 EC_KEY_get0_public_key(ecdh), 1791 POINT_CONVERSION_UNCOMPRESSED, 1792 encodedPoint, encodedlen, bn_ctx); 1793 1794 if (encodedlen == 0) 1795 { 1796 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1797 goto err; 1798 } 1799 1800 BN_CTX_free(bn_ctx); bn_ctx=NULL; 1801 1802 /* XXX: For now, we only support named (not 1803 * generic) curves in ECDH ephemeral key exchanges. 1804 * In this situation, we need four additional bytes 1805 * to encode the entire ServerECDHParams 1806 * structure. 1807 */ 1808 n = 4 + encodedlen; 1809 1810 /* We'll generate the serverKeyExchange message 1811 * explicitly so we can set these to NULLs 1812 */ 1813 r[0]=NULL; 1814 r[1]=NULL; 1815 r[2]=NULL; 1816 r[3]=NULL; 1817 } 1818 else 1819 #endif /* !OPENSSL_NO_ECDH */ 1820 #ifndef OPENSSL_NO_PSK 1821 if (type & SSL_kPSK) 1822 { 1823 /* reserve size for record length and PSK identity hint*/ 1824 n+=2+strlen(s->ctx->psk_identity_hint); 1825 } 1826 else 1827 #endif /* !OPENSSL_NO_PSK */ 1828 #ifndef OPENSSL_NO_SRP 1829 if (type & SSL_kSRP) 1830 { 1831 if ((s->srp_ctx.N == NULL) || 1832 (s->srp_ctx.g == NULL) || 1833 (s->srp_ctx.s == NULL) || 1834 (s->srp_ctx.B == NULL)) 1835 { 1836 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_SRP_PARAM); 1837 goto err; 1838 } 1839 r[0]=s->srp_ctx.N; 1840 r[1]=s->srp_ctx.g; 1841 r[2]=s->srp_ctx.s; 1842 r[3]=s->srp_ctx.B; 1843 } 1844 else 1845 #endif 1846 { 1847 al=SSL_AD_HANDSHAKE_FAILURE; 1848 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1849 goto f_err; 1850 } 1851 for (i=0; r[i] != NULL && i<4; i++) 1852 { 1853 nr[i]=BN_num_bytes(r[i]); 1854 #ifndef OPENSSL_NO_SRP 1855 if ((i == 2) && (type & SSL_kSRP)) 1856 n+=1+nr[i]; 1857 else 1858 #endif 1859 n+=2+nr[i]; 1860 } 1861 1862 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) 1863 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 1864 { 1865 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher,&md)) 1866 == NULL) 1867 { 1868 al=SSL_AD_DECODE_ERROR; 1869 goto f_err; 1870 } 1871 kn=EVP_PKEY_size(pkey); 1872 } 1873 else 1874 { 1875 pkey=NULL; 1876 kn=0; 1877 } 1878 1879 if (!BUF_MEM_grow_clean(buf,n+4+kn)) 1880 { 1881 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 1882 goto err; 1883 } 1884 d=(unsigned char *)s->init_buf->data; 1885 p= &(d[4]); 1886 1887 for (i=0; r[i] != NULL && i<4; i++) 1888 { 1889 #ifndef OPENSSL_NO_SRP 1890 if ((i == 2) && (type & SSL_kSRP)) 1891 { 1892 *p = nr[i]; 1893 p++; 1894 } 1895 else 1896 #endif 1897 s2n(nr[i],p); 1898 BN_bn2bin(r[i],p); 1899 p+=nr[i]; 1900 } 1901 1902 #ifndef OPENSSL_NO_ECDH 1903 if (type & SSL_kEECDH) 1904 { 1905 /* XXX: For now, we only support named (not generic) curves. 1906 * In this situation, the serverKeyExchange message has: 1907 * [1 byte CurveType], [2 byte CurveName] 1908 * [1 byte length of encoded point], followed by 1909 * the actual encoded point itself 1910 */ 1911 *p = NAMED_CURVE_TYPE; 1912 p += 1; 1913 *p = 0; 1914 p += 1; 1915 *p = curve_id; 1916 p += 1; 1917 *p = encodedlen; 1918 p += 1; 1919 memcpy((unsigned char*)p, 1920 (unsigned char *)encodedPoint, 1921 encodedlen); 1922 OPENSSL_free(encodedPoint); 1923 encodedPoint = NULL; 1924 p += encodedlen; 1925 } 1926 #endif 1927 1928 #ifndef OPENSSL_NO_PSK 1929 if (type & SSL_kPSK) 1930 { 1931 /* copy PSK identity hint */ 1932 s2n(strlen(s->ctx->psk_identity_hint), p); 1933 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint)); 1934 p+=strlen(s->ctx->psk_identity_hint); 1935 } 1936 #endif 1937 1938 /* not anonymous */ 1939 if (pkey != NULL) 1940 { 1941 /* n is the length of the params, they start at &(d[4]) 1942 * and p points to the space at the end. */ 1943 #ifndef OPENSSL_NO_RSA 1944 if (pkey->type == EVP_PKEY_RSA 1945 && TLS1_get_version(s) < TLS1_2_VERSION) 1946 { 1947 q=md_buf; 1948 j=0; 1949 for (num=2; num > 0; num--) 1950 { 1951 EVP_MD_CTX_set_flags(&md_ctx, 1952 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 1953 EVP_DigestInit_ex(&md_ctx,(num == 2) 1954 ?s->ctx->md5:s->ctx->sha1, NULL); 1955 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1956 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1957 EVP_DigestUpdate(&md_ctx,&(d[4]),n); 1958 EVP_DigestFinal_ex(&md_ctx,q, 1959 (unsigned int *)&i); 1960 q+=i; 1961 j+=i; 1962 } 1963 if (RSA_sign(NID_md5_sha1, md_buf, j, 1964 &(p[2]), &u, pkey->pkey.rsa) <= 0) 1965 { 1966 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 1967 goto err; 1968 } 1969 s2n(u,p); 1970 n+=u+2; 1971 } 1972 else 1973 #endif 1974 if (md) 1975 { 1976 /* For TLS1.2 and later send signature 1977 * algorithm */ 1978 if (TLS1_get_version(s) >= TLS1_2_VERSION) 1979 { 1980 if (!tls12_get_sigandhash(p, pkey, md)) 1981 { 1982 /* Should never happen */ 1983 al=SSL_AD_INTERNAL_ERROR; 1984 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1985 goto f_err; 1986 } 1987 p+=2; 1988 } 1989 #ifdef SSL_DEBUG 1990 fprintf(stderr, "Using hash %s\n", 1991 EVP_MD_name(md)); 1992 #endif 1993 EVP_SignInit_ex(&md_ctx, md, NULL); 1994 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1995 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1996 EVP_SignUpdate(&md_ctx,&(d[4]),n); 1997 if (!EVP_SignFinal(&md_ctx,&(p[2]), 1998 (unsigned int *)&i,pkey)) 1999 { 2000 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_EVP); 2001 goto err; 2002 } 2003 s2n(i,p); 2004 n+=i+2; 2005 if (TLS1_get_version(s) >= TLS1_2_VERSION) 2006 n+= 2; 2007 } 2008 else 2009 { 2010 /* Is this error check actually needed? */ 2011 al=SSL_AD_HANDSHAKE_FAILURE; 2012 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 2013 goto f_err; 2014 } 2015 } 2016 2017 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; 2018 l2n3(n,d); 2019 2020 /* we should now have things packed up, so lets send 2021 * it off */ 2022 s->init_num=n+4; 2023 s->init_off=0; 2024 } 2025 2026 s->state = SSL3_ST_SW_KEY_EXCH_B; 2027 EVP_MD_CTX_cleanup(&md_ctx); 2028 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 2029 f_err: 2030 ssl3_send_alert(s,SSL3_AL_FATAL,al); 2031 err: 2032 #ifndef OPENSSL_NO_ECDH 2033 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); 2034 BN_CTX_free(bn_ctx); 2035 #endif 2036 EVP_MD_CTX_cleanup(&md_ctx); 2037 return(-1); 2038 } 2039 2040 int ssl3_send_certificate_request(SSL *s) 2041 { 2042 unsigned char *p,*d; 2043 int i,j,nl,off,n; 2044 STACK_OF(X509_NAME) *sk=NULL; 2045 X509_NAME *name; 2046 BUF_MEM *buf; 2047 2048 if (s->state == SSL3_ST_SW_CERT_REQ_A) 2049 { 2050 buf=s->init_buf; 2051 2052 d=p=(unsigned char *)&(buf->data[4]); 2053 2054 /* get the list of acceptable cert types */ 2055 p++; 2056 n=ssl3_get_req_cert_type(s,p); 2057 d[0]=n; 2058 p+=n; 2059 n++; 2060 2061 if (TLS1_get_version(s) >= TLS1_2_VERSION) 2062 { 2063 nl = tls12_get_req_sig_algs(s, p + 2); 2064 s2n(nl, p); 2065 p += nl + 2; 2066 n += nl + 2; 2067 } 2068 2069 off=n; 2070 p+=2; 2071 n+=2; 2072 2073 sk=SSL_get_client_CA_list(s); 2074 nl=0; 2075 if (sk != NULL) 2076 { 2077 for (i=0; i<sk_X509_NAME_num(sk); i++) 2078 { 2079 name=sk_X509_NAME_value(sk,i); 2080 j=i2d_X509_NAME(name,NULL); 2081 if (!BUF_MEM_grow_clean(buf,4+n+j+2)) 2082 { 2083 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 2084 goto err; 2085 } 2086 p=(unsigned char *)&(buf->data[4+n]); 2087 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 2088 { 2089 s2n(j,p); 2090 i2d_X509_NAME(name,&p); 2091 n+=2+j; 2092 nl+=2+j; 2093 } 2094 else 2095 { 2096 d=p; 2097 i2d_X509_NAME(name,&p); 2098 j-=2; s2n(j,d); j+=2; 2099 n+=j; 2100 nl+=j; 2101 } 2102 } 2103 } 2104 /* else no CA names */ 2105 p=(unsigned char *)&(buf->data[4+off]); 2106 s2n(nl,p); 2107 2108 d=(unsigned char *)buf->data; 2109 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 2110 l2n3(n,d); 2111 2112 /* we should now have things packed up, so lets send 2113 * it off */ 2114 2115 s->init_num=n+4; 2116 s->init_off=0; 2117 #ifdef NETSCAPE_HANG_BUG 2118 p=(unsigned char *)s->init_buf->data + s->init_num; 2119 2120 /* do the header */ 2121 *(p++)=SSL3_MT_SERVER_DONE; 2122 *(p++)=0; 2123 *(p++)=0; 2124 *(p++)=0; 2125 s->init_num += 4; 2126 #endif 2127 2128 s->state = SSL3_ST_SW_CERT_REQ_B; 2129 } 2130 2131 /* SSL3_ST_SW_CERT_REQ_B */ 2132 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 2133 err: 2134 return(-1); 2135 } 2136 2137 int ssl3_get_client_key_exchange(SSL *s) 2138 { 2139 int i,al,ok; 2140 long n; 2141 unsigned long alg_k; 2142 unsigned char *p; 2143 #ifndef OPENSSL_NO_RSA 2144 RSA *rsa=NULL; 2145 EVP_PKEY *pkey=NULL; 2146 #endif 2147 #ifndef OPENSSL_NO_DH 2148 BIGNUM *pub=NULL; 2149 DH *dh_srvr; 2150 #endif 2151 #ifndef OPENSSL_NO_KRB5 2152 KSSL_ERR kssl_err; 2153 #endif /* OPENSSL_NO_KRB5 */ 2154 2155 #ifndef OPENSSL_NO_ECDH 2156 EC_KEY *srvr_ecdh = NULL; 2157 EVP_PKEY *clnt_pub_pkey = NULL; 2158 EC_POINT *clnt_ecpoint = NULL; 2159 BN_CTX *bn_ctx = NULL; 2160 #endif 2161 2162 n=s->method->ssl_get_message(s, 2163 SSL3_ST_SR_KEY_EXCH_A, 2164 SSL3_ST_SR_KEY_EXCH_B, 2165 SSL3_MT_CLIENT_KEY_EXCHANGE, 2166 2048, /* ??? */ 2167 &ok); 2168 2169 if (!ok) return((int)n); 2170 p=(unsigned char *)s->init_msg; 2171 2172 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; 2173 2174 #ifndef OPENSSL_NO_RSA 2175 if (alg_k & SSL_kRSA) 2176 { 2177 /* FIX THIS UP EAY EAY EAY EAY */ 2178 if (s->s3->tmp.use_rsa_tmp) 2179 { 2180 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) 2181 rsa=s->cert->rsa_tmp; 2182 /* Don't do a callback because rsa_tmp should 2183 * be sent already */ 2184 if (rsa == NULL) 2185 { 2186 al=SSL_AD_HANDSHAKE_FAILURE; 2187 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); 2188 goto f_err; 2189 2190 } 2191 } 2192 else 2193 { 2194 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; 2195 if ( (pkey == NULL) || 2196 (pkey->type != EVP_PKEY_RSA) || 2197 (pkey->pkey.rsa == NULL)) 2198 { 2199 al=SSL_AD_HANDSHAKE_FAILURE; 2200 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); 2201 goto f_err; 2202 } 2203 rsa=pkey->pkey.rsa; 2204 } 2205 2206 /* TLS and [incidentally] DTLS{0xFEFF} */ 2207 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) 2208 { 2209 n2s(p,i); 2210 if (n != i+2) 2211 { 2212 if (!(s->options & SSL_OP_TLS_D5_BUG)) 2213 { 2214 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 2215 goto err; 2216 } 2217 else 2218 p-=2; 2219 } 2220 else 2221 n=i; 2222 } 2223 2224 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); 2225 2226 al = -1; 2227 2228 if (i != SSL_MAX_MASTER_KEY_LENGTH) 2229 { 2230 al=SSL_AD_DECODE_ERROR; 2231 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */ 2232 } 2233 2234 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff)))) 2235 { 2236 /* The premaster secret must contain the same version number as the 2237 * ClientHello to detect version rollback attacks (strangely, the 2238 * protocol does not offer such protection for DH ciphersuites). 2239 * However, buggy clients exist that send the negotiated protocol 2240 * version instead if the server does not support the requested 2241 * protocol version. 2242 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ 2243 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && 2244 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff)))) 2245 { 2246 al=SSL_AD_DECODE_ERROR; 2247 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */ 2248 2249 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack 2250 * (http://eprint.iacr.org/2003/052/) exploits the version 2251 * number check as a "bad version oracle" -- an alert would 2252 * reveal that the plaintext corresponding to some ciphertext 2253 * made up by the adversary is properly formatted except 2254 * that the version number is wrong. To avoid such attacks, 2255 * we should treat this just like any other decryption error. */ 2256 } 2257 } 2258 2259 if (al != -1) 2260 { 2261 /* Some decryption failure -- use random value instead as countermeasure 2262 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding 2263 * (see RFC 2246, section 7.4.7.1). */ 2264 ERR_clear_error(); 2265 i = SSL_MAX_MASTER_KEY_LENGTH; 2266 p[0] = s->client_version >> 8; 2267 p[1] = s->client_version & 0xff; 2268 if (RAND_pseudo_bytes(p+2, i-2) <= 0) /* should be RAND_bytes, but we cannot work around a failure */ 2269 goto err; 2270 } 2271 2272 s->session->master_key_length= 2273 s->method->ssl3_enc->generate_master_secret(s, 2274 s->session->master_key, 2275 p,i); 2276 OPENSSL_cleanse(p,i); 2277 } 2278 else 2279 #endif 2280 #ifndef OPENSSL_NO_DH 2281 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 2282 { 2283 n2s(p,i); 2284 if (n != i+2) 2285 { 2286 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) 2287 { 2288 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); 2289 goto err; 2290 } 2291 else 2292 { 2293 p-=2; 2294 i=(int)n; 2295 } 2296 } 2297 2298 if (n == 0L) /* the parameters are in the cert */ 2299 { 2300 al=SSL_AD_HANDSHAKE_FAILURE; 2301 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); 2302 goto f_err; 2303 } 2304 else 2305 { 2306 if (s->s3->tmp.dh == NULL) 2307 { 2308 al=SSL_AD_HANDSHAKE_FAILURE; 2309 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 2310 goto f_err; 2311 } 2312 else 2313 dh_srvr=s->s3->tmp.dh; 2314 } 2315 2316 pub=BN_bin2bn(p,i,NULL); 2317 if (pub == NULL) 2318 { 2319 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); 2320 goto err; 2321 } 2322 2323 i=DH_compute_key(p,pub,dh_srvr); 2324 2325 if (i <= 0) 2326 { 2327 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 2328 BN_clear_free(pub); 2329 goto err; 2330 } 2331 2332 DH_free(s->s3->tmp.dh); 2333 s->s3->tmp.dh=NULL; 2334 2335 BN_clear_free(pub); 2336 pub=NULL; 2337 s->session->master_key_length= 2338 s->method->ssl3_enc->generate_master_secret(s, 2339 s->session->master_key,p,i); 2340 OPENSSL_cleanse(p,i); 2341 } 2342 else 2343 #endif 2344 #ifndef OPENSSL_NO_KRB5 2345 if (alg_k & SSL_kKRB5) 2346 { 2347 krb5_error_code krb5rc; 2348 krb5_data enc_ticket; 2349 krb5_data authenticator; 2350 krb5_data enc_pms; 2351 KSSL_CTX *kssl_ctx = s->kssl_ctx; 2352 EVP_CIPHER_CTX ciph_ctx; 2353 const EVP_CIPHER *enc = NULL; 2354 unsigned char iv[EVP_MAX_IV_LENGTH]; 2355 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH 2356 + EVP_MAX_BLOCK_LENGTH]; 2357 int padl, outl; 2358 krb5_timestamp authtime = 0; 2359 krb5_ticket_times ttimes; 2360 2361 EVP_CIPHER_CTX_init(&ciph_ctx); 2362 2363 if (!kssl_ctx) kssl_ctx = kssl_ctx_new(); 2364 2365 n2s(p,i); 2366 enc_ticket.length = i; 2367 2368 if (n < (long)(enc_ticket.length + 6)) 2369 { 2370 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2371 SSL_R_DATA_LENGTH_TOO_LONG); 2372 goto err; 2373 } 2374 2375 enc_ticket.data = (char *)p; 2376 p+=enc_ticket.length; 2377 2378 n2s(p,i); 2379 authenticator.length = i; 2380 2381 if (n < (long)(enc_ticket.length + authenticator.length + 6)) 2382 { 2383 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2384 SSL_R_DATA_LENGTH_TOO_LONG); 2385 goto err; 2386 } 2387 2388 authenticator.data = (char *)p; 2389 p+=authenticator.length; 2390 2391 n2s(p,i); 2392 enc_pms.length = i; 2393 enc_pms.data = (char *)p; 2394 p+=enc_pms.length; 2395 2396 /* Note that the length is checked again below, 2397 ** after decryption 2398 */ 2399 if(enc_pms.length > sizeof pms) 2400 { 2401 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2402 SSL_R_DATA_LENGTH_TOO_LONG); 2403 goto err; 2404 } 2405 2406 if (n != (long)(enc_ticket.length + authenticator.length + 2407 enc_pms.length + 6)) 2408 { 2409 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2410 SSL_R_DATA_LENGTH_TOO_LONG); 2411 goto err; 2412 } 2413 2414 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes, 2415 &kssl_err)) != 0) 2416 { 2417 #ifdef KSSL_DEBUG 2418 printf("kssl_sget_tkt rtn %d [%d]\n", 2419 krb5rc, kssl_err.reason); 2420 if (kssl_err.text) 2421 printf("kssl_err text= %s\n", kssl_err.text); 2422 #endif /* KSSL_DEBUG */ 2423 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2424 kssl_err.reason); 2425 goto err; 2426 } 2427 2428 /* Note: no authenticator is not considered an error, 2429 ** but will return authtime == 0. 2430 */ 2431 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator, 2432 &authtime, &kssl_err)) != 0) 2433 { 2434 #ifdef KSSL_DEBUG 2435 printf("kssl_check_authent rtn %d [%d]\n", 2436 krb5rc, kssl_err.reason); 2437 if (kssl_err.text) 2438 printf("kssl_err text= %s\n", kssl_err.text); 2439 #endif /* KSSL_DEBUG */ 2440 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2441 kssl_err.reason); 2442 goto err; 2443 } 2444 2445 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) 2446 { 2447 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc); 2448 goto err; 2449 } 2450 2451 #ifdef KSSL_DEBUG 2452 kssl_ctx_show(kssl_ctx); 2453 #endif /* KSSL_DEBUG */ 2454 2455 enc = kssl_map_enc(kssl_ctx->enctype); 2456 if (enc == NULL) 2457 goto err; 2458 2459 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 2460 2461 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv)) 2462 { 2463 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2464 SSL_R_DECRYPTION_FAILED); 2465 goto err; 2466 } 2467 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl, 2468 (unsigned char *)enc_pms.data, enc_pms.length)) 2469 { 2470 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2471 SSL_R_DECRYPTION_FAILED); 2472 goto err; 2473 } 2474 if (outl > SSL_MAX_MASTER_KEY_LENGTH) 2475 { 2476 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2477 SSL_R_DATA_LENGTH_TOO_LONG); 2478 goto err; 2479 } 2480 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl)) 2481 { 2482 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2483 SSL_R_DECRYPTION_FAILED); 2484 goto err; 2485 } 2486 outl += padl; 2487 if (outl > SSL_MAX_MASTER_KEY_LENGTH) 2488 { 2489 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2490 SSL_R_DATA_LENGTH_TOO_LONG); 2491 goto err; 2492 } 2493 if (!((pms[0] == (s->client_version>>8)) && (pms[1] == (s->client_version & 0xff)))) 2494 { 2495 /* The premaster secret must contain the same version number as the 2496 * ClientHello to detect version rollback attacks (strangely, the 2497 * protocol does not offer such protection for DH ciphersuites). 2498 * However, buggy clients exist that send random bytes instead of 2499 * the protocol version. 2500 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. 2501 * (Perhaps we should have a separate BUG value for the Kerberos cipher) 2502 */ 2503 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) 2504 { 2505 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2506 SSL_AD_DECODE_ERROR); 2507 goto err; 2508 } 2509 } 2510 2511 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 2512 2513 s->session->master_key_length= 2514 s->method->ssl3_enc->generate_master_secret(s, 2515 s->session->master_key, pms, outl); 2516 2517 if (kssl_ctx->client_princ) 2518 { 2519 size_t len = strlen(kssl_ctx->client_princ); 2520 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH ) 2521 { 2522 s->session->krb5_client_princ_len = len; 2523 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len); 2524 } 2525 } 2526 2527 2528 /* Was doing kssl_ctx_free() here, 2529 ** but it caused problems for apache. 2530 ** kssl_ctx = kssl_ctx_free(kssl_ctx); 2531 ** if (s->kssl_ctx) s->kssl_ctx = NULL; 2532 */ 2533 } 2534 else 2535 #endif /* OPENSSL_NO_KRB5 */ 2536 2537 #ifndef OPENSSL_NO_ECDH 2538 if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) 2539 { 2540 int ret = 1; 2541 int field_size = 0; 2542 const EC_KEY *tkey; 2543 const EC_GROUP *group; 2544 const BIGNUM *priv_key; 2545 2546 /* initialize structures for server's ECDH key pair */ 2547 if ((srvr_ecdh = EC_KEY_new()) == NULL) 2548 { 2549 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2550 ERR_R_MALLOC_FAILURE); 2551 goto err; 2552 } 2553 2554 /* Let's get server private key and group information */ 2555 if (alg_k & (SSL_kECDHr|SSL_kECDHe)) 2556 { 2557 /* use the certificate */ 2558 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec; 2559 } 2560 else 2561 { 2562 /* use the ephermeral values we saved when 2563 * generating the ServerKeyExchange msg. 2564 */ 2565 tkey = s->s3->tmp.ecdh; 2566 } 2567 2568 group = EC_KEY_get0_group(tkey); 2569 priv_key = EC_KEY_get0_private_key(tkey); 2570 2571 if (!EC_KEY_set_group(srvr_ecdh, group) || 2572 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) 2573 { 2574 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2575 ERR_R_EC_LIB); 2576 goto err; 2577 } 2578 2579 /* Let's get client's public key */ 2580 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) 2581 { 2582 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2583 ERR_R_MALLOC_FAILURE); 2584 goto err; 2585 } 2586 2587 if (n == 0L) 2588 { 2589 /* Client Publickey was in Client Certificate */ 2590 2591 if (alg_k & SSL_kEECDH) 2592 { 2593 al=SSL_AD_HANDSHAKE_FAILURE; 2594 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); 2595 goto f_err; 2596 } 2597 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer)) 2598 == NULL) || 2599 (clnt_pub_pkey->type != EVP_PKEY_EC)) 2600 { 2601 /* XXX: For now, we do not support client 2602 * authentication using ECDH certificates 2603 * so this branch (n == 0L) of the code is 2604 * never executed. When that support is 2605 * added, we ought to ensure the key 2606 * received in the certificate is 2607 * authorized for key agreement. 2608 * ECDH_compute_key implicitly checks that 2609 * the two ECDH shares are for the same 2610 * group. 2611 */ 2612 al=SSL_AD_HANDSHAKE_FAILURE; 2613 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2614 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS); 2615 goto f_err; 2616 } 2617 2618 if (EC_POINT_copy(clnt_ecpoint, 2619 EC_KEY_get0_public_key(clnt_pub_pkey->pkey.ec)) == 0) 2620 { 2621 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2622 ERR_R_EC_LIB); 2623 goto err; 2624 } 2625 ret = 2; /* Skip certificate verify processing */ 2626 } 2627 else 2628 { 2629 /* Get client's public key from encoded point 2630 * in the ClientKeyExchange message. 2631 */ 2632 if ((bn_ctx = BN_CTX_new()) == NULL) 2633 { 2634 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2635 ERR_R_MALLOC_FAILURE); 2636 goto err; 2637 } 2638 2639 /* Get encoded point length */ 2640 i = *p; 2641 p += 1; 2642 if (n != 1 + i) 2643 { 2644 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2645 ERR_R_EC_LIB); 2646 goto err; 2647 } 2648 if (EC_POINT_oct2point(group, 2649 clnt_ecpoint, p, i, bn_ctx) == 0) 2650 { 2651 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2652 ERR_R_EC_LIB); 2653 goto err; 2654 } 2655 /* p is pointing to somewhere in the buffer 2656 * currently, so set it to the start 2657 */ 2658 p=(unsigned char *)s->init_buf->data; 2659 } 2660 2661 /* Compute the shared pre-master secret */ 2662 field_size = EC_GROUP_get_degree(group); 2663 if (field_size <= 0) 2664 { 2665 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2666 ERR_R_ECDH_LIB); 2667 goto err; 2668 } 2669 i = ECDH_compute_key(p, (field_size+7)/8, clnt_ecpoint, srvr_ecdh, NULL); 2670 if (i <= 0) 2671 { 2672 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2673 ERR_R_ECDH_LIB); 2674 goto err; 2675 } 2676 2677 EVP_PKEY_free(clnt_pub_pkey); 2678 EC_POINT_free(clnt_ecpoint); 2679 EC_KEY_free(srvr_ecdh); 2680 BN_CTX_free(bn_ctx); 2681 EC_KEY_free(s->s3->tmp.ecdh); 2682 s->s3->tmp.ecdh = NULL; 2683 2684 /* Compute the master secret */ 2685 s->session->master_key_length = s->method->ssl3_enc-> \ 2686 generate_master_secret(s, s->session->master_key, p, i); 2687 2688 OPENSSL_cleanse(p, i); 2689 return (ret); 2690 } 2691 else 2692 #endif 2693 #ifndef OPENSSL_NO_PSK 2694 if (alg_k & SSL_kPSK) 2695 { 2696 unsigned char *t = NULL; 2697 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; 2698 unsigned int pre_ms_len = 0, psk_len = 0; 2699 int psk_err = 1; 2700 char tmp_id[PSK_MAX_IDENTITY_LEN+1]; 2701 2702 al=SSL_AD_HANDSHAKE_FAILURE; 2703 2704 n2s(p,i); 2705 if (n != i+2) 2706 { 2707 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2708 SSL_R_LENGTH_MISMATCH); 2709 goto psk_err; 2710 } 2711 if (i > PSK_MAX_IDENTITY_LEN) 2712 { 2713 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2714 SSL_R_DATA_LENGTH_TOO_LONG); 2715 goto psk_err; 2716 } 2717 if (s->psk_server_callback == NULL) 2718 { 2719 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2720 SSL_R_PSK_NO_SERVER_CB); 2721 goto psk_err; 2722 } 2723 2724 /* Create guaranteed NULL-terminated identity 2725 * string for the callback */ 2726 memcpy(tmp_id, p, i); 2727 memset(tmp_id+i, 0, PSK_MAX_IDENTITY_LEN+1-i); 2728 psk_len = s->psk_server_callback(s, tmp_id, 2729 psk_or_pre_ms, sizeof(psk_or_pre_ms)); 2730 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN+1); 2731 2732 if (psk_len > PSK_MAX_PSK_LEN) 2733 { 2734 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2735 ERR_R_INTERNAL_ERROR); 2736 goto psk_err; 2737 } 2738 else if (psk_len == 0) 2739 { 2740 /* PSK related to the given identity not found */ 2741 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2742 SSL_R_PSK_IDENTITY_NOT_FOUND); 2743 al=SSL_AD_UNKNOWN_PSK_IDENTITY; 2744 goto psk_err; 2745 } 2746 2747 /* create PSK pre_master_secret */ 2748 pre_ms_len=2+psk_len+2+psk_len; 2749 t = psk_or_pre_ms; 2750 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len); 2751 s2n(psk_len, t); 2752 memset(t, 0, psk_len); 2753 t+=psk_len; 2754 s2n(psk_len, t); 2755 2756 if (s->session->psk_identity != NULL) 2757 OPENSSL_free(s->session->psk_identity); 2758 s->session->psk_identity = BUF_strdup((char *)p); 2759 if (s->session->psk_identity == NULL) 2760 { 2761 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2762 ERR_R_MALLOC_FAILURE); 2763 goto psk_err; 2764 } 2765 2766 if (s->session->psk_identity_hint != NULL) 2767 OPENSSL_free(s->session->psk_identity_hint); 2768 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint); 2769 if (s->ctx->psk_identity_hint != NULL && 2770 s->session->psk_identity_hint == NULL) 2771 { 2772 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2773 ERR_R_MALLOC_FAILURE); 2774 goto psk_err; 2775 } 2776 2777 s->session->master_key_length= 2778 s->method->ssl3_enc->generate_master_secret(s, 2779 s->session->master_key, psk_or_pre_ms, pre_ms_len); 2780 psk_err = 0; 2781 psk_err: 2782 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); 2783 if (psk_err != 0) 2784 goto f_err; 2785 } 2786 else 2787 #endif 2788 #ifndef OPENSSL_NO_SRP 2789 if (alg_k & SSL_kSRP) 2790 { 2791 int param_len; 2792 2793 n2s(p,i); 2794 param_len=i+2; 2795 if (param_len > n) 2796 { 2797 al=SSL_AD_DECODE_ERROR; 2798 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_SRP_A_LENGTH); 2799 goto f_err; 2800 } 2801 if (!(s->srp_ctx.A=BN_bin2bn(p,i,NULL))) 2802 { 2803 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_BN_LIB); 2804 goto err; 2805 } 2806 if (s->session->srp_username != NULL) 2807 OPENSSL_free(s->session->srp_username); 2808 s->session->srp_username = BUF_strdup(s->srp_ctx.login); 2809 if (s->session->srp_username == NULL) 2810 { 2811 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2812 ERR_R_MALLOC_FAILURE); 2813 goto err; 2814 } 2815 2816 if ((s->session->master_key_length = SRP_generate_server_master_secret(s,s->session->master_key))<0) 2817 { 2818 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 2819 goto err; 2820 } 2821 2822 p+=i; 2823 } 2824 else 2825 #endif /* OPENSSL_NO_SRP */ 2826 if (alg_k & SSL_kGOST) 2827 { 2828 int ret = 0; 2829 EVP_PKEY_CTX *pkey_ctx; 2830 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; 2831 unsigned char premaster_secret[32], *start; 2832 size_t outlen=32, inlen; 2833 unsigned long alg_a; 2834 2835 /* Get our certificate private key*/ 2836 alg_a = s->s3->tmp.new_cipher->algorithm_auth; 2837 if (alg_a & SSL_aGOST94) 2838 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey; 2839 else if (alg_a & SSL_aGOST01) 2840 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; 2841 2842 pkey_ctx = EVP_PKEY_CTX_new(pk,NULL); 2843 EVP_PKEY_decrypt_init(pkey_ctx); 2844 /* If client certificate is present and is of the same type, maybe 2845 * use it for key exchange. Don't mind errors from 2846 * EVP_PKEY_derive_set_peer, because it is completely valid to use 2847 * a client certificate for authorization only. */ 2848 client_pub_pkey = X509_get_pubkey(s->session->peer); 2849 if (client_pub_pkey) 2850 { 2851 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0) 2852 ERR_clear_error(); 2853 } 2854 /* Decrypt session key */ 2855 if ((*p!=( V_ASN1_SEQUENCE| V_ASN1_CONSTRUCTED))) 2856 { 2857 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED); 2858 goto gerr; 2859 } 2860 if (p[1] == 0x81) 2861 { 2862 start = p+3; 2863 inlen = p[2]; 2864 } 2865 else if (p[1] < 0x80) 2866 { 2867 start = p+2; 2868 inlen = p[1]; 2869 } 2870 else 2871 { 2872 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED); 2873 goto gerr; 2874 } 2875 if (EVP_PKEY_decrypt(pkey_ctx,premaster_secret,&outlen,start,inlen) <=0) 2876 2877 { 2878 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DECRYPTION_FAILED); 2879 goto gerr; 2880 } 2881 /* Generate master secret */ 2882 s->session->master_key_length= 2883 s->method->ssl3_enc->generate_master_secret(s, 2884 s->session->master_key,premaster_secret,32); 2885 /* Check if pubkey from client certificate was used */ 2886 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) 2887 ret = 2; 2888 else 2889 ret = 1; 2890 gerr: 2891 EVP_PKEY_free(client_pub_pkey); 2892 EVP_PKEY_CTX_free(pkey_ctx); 2893 if (ret) 2894 return ret; 2895 else 2896 goto err; 2897 } 2898 else 2899 { 2900 al=SSL_AD_HANDSHAKE_FAILURE; 2901 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, 2902 SSL_R_UNKNOWN_CIPHER_TYPE); 2903 goto f_err; 2904 } 2905 2906 return(1); 2907 f_err: 2908 ssl3_send_alert(s,SSL3_AL_FATAL,al); 2909 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP) 2910 err: 2911 #endif 2912 #ifndef OPENSSL_NO_ECDH 2913 EVP_PKEY_free(clnt_pub_pkey); 2914 EC_POINT_free(clnt_ecpoint); 2915 if (srvr_ecdh != NULL) 2916 EC_KEY_free(srvr_ecdh); 2917 BN_CTX_free(bn_ctx); 2918 #endif 2919 return(-1); 2920 } 2921 2922 int ssl3_get_cert_verify(SSL *s) 2923 { 2924 EVP_PKEY *pkey=NULL; 2925 unsigned char *p; 2926 int al,ok,ret=0; 2927 long n; 2928 int type=0,i,j; 2929 X509 *peer; 2930 const EVP_MD *md = NULL; 2931 EVP_MD_CTX mctx; 2932 EVP_MD_CTX_init(&mctx); 2933 2934 n=s->method->ssl_get_message(s, 2935 SSL3_ST_SR_CERT_VRFY_A, 2936 SSL3_ST_SR_CERT_VRFY_B, 2937 -1, 2938 516, /* Enough for 4096 bit RSA key with TLS v1.2 */ 2939 &ok); 2940 2941 if (!ok) return((int)n); 2942 2943 if (s->session->peer != NULL) 2944 { 2945 peer=s->session->peer; 2946 pkey=X509_get_pubkey(peer); 2947 type=X509_certificate_type(peer,pkey); 2948 } 2949 else 2950 { 2951 peer=NULL; 2952 pkey=NULL; 2953 } 2954 2955 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) 2956 { 2957 s->s3->tmp.reuse_message=1; 2958 if ((peer != NULL) && (type & EVP_PKT_SIGN)) 2959 { 2960 al=SSL_AD_UNEXPECTED_MESSAGE; 2961 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); 2962 goto f_err; 2963 } 2964 ret=1; 2965 goto end; 2966 } 2967 2968 if (peer == NULL) 2969 { 2970 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); 2971 al=SSL_AD_UNEXPECTED_MESSAGE; 2972 goto f_err; 2973 } 2974 2975 if (!(type & EVP_PKT_SIGN)) 2976 { 2977 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 2978 al=SSL_AD_ILLEGAL_PARAMETER; 2979 goto f_err; 2980 } 2981 2982 if (s->s3->change_cipher_spec) 2983 { 2984 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); 2985 al=SSL_AD_UNEXPECTED_MESSAGE; 2986 goto f_err; 2987 } 2988 2989 /* we now have a signature that we need to verify */ 2990 p=(unsigned char *)s->init_msg; 2991 /* Check for broken implementations of GOST ciphersuites */ 2992 /* If key is GOST and n is exactly 64, it is bare 2993 * signature without length field */ 2994 if (n==64 && (pkey->type==NID_id_GostR3410_94 || 2995 pkey->type == NID_id_GostR3410_2001) ) 2996 { 2997 i=64; 2998 } 2999 else 3000 { 3001 if (TLS1_get_version(s) >= TLS1_2_VERSION) 3002 { 3003 int sigalg = tls12_get_sigid(pkey); 3004 /* Should never happen */ 3005 if (sigalg == -1) 3006 { 3007 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR); 3008 al=SSL_AD_INTERNAL_ERROR; 3009 goto f_err; 3010 } 3011 /* Check key type is consistent with signature */ 3012 if (sigalg != (int)p[1]) 3013 { 3014 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_TYPE); 3015 al=SSL_AD_DECODE_ERROR; 3016 goto f_err; 3017 } 3018 md = tls12_get_hash(p[0]); 3019 if (md == NULL) 3020 { 3021 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_UNKNOWN_DIGEST); 3022 al=SSL_AD_DECODE_ERROR; 3023 goto f_err; 3024 } 3025 #ifdef SSL_DEBUG 3026 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md)); 3027 #endif 3028 p += 2; 3029 n -= 2; 3030 } 3031 n2s(p,i); 3032 n-=2; 3033 if (i > n) 3034 { 3035 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); 3036 al=SSL_AD_DECODE_ERROR; 3037 goto f_err; 3038 } 3039 } 3040 j=EVP_PKEY_size(pkey); 3041 if ((i > j) || (n > j) || (n <= 0)) 3042 { 3043 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); 3044 al=SSL_AD_DECODE_ERROR; 3045 goto f_err; 3046 } 3047 3048 if (TLS1_get_version(s) >= TLS1_2_VERSION) 3049 { 3050 long hdatalen = 0; 3051 void *hdata; 3052 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); 3053 if (hdatalen <= 0) 3054 { 3055 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR); 3056 al=SSL_AD_INTERNAL_ERROR; 3057 goto f_err; 3058 } 3059 #ifdef SSL_DEBUG 3060 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n", 3061 EVP_MD_name(md)); 3062 #endif 3063 if (!EVP_VerifyInit_ex(&mctx, md, NULL) 3064 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) 3065 { 3066 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB); 3067 al=SSL_AD_INTERNAL_ERROR; 3068 goto f_err; 3069 } 3070 3071 if (EVP_VerifyFinal(&mctx, p , i, pkey) <= 0) 3072 { 3073 al=SSL_AD_DECRYPT_ERROR; 3074 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_SIGNATURE); 3075 goto f_err; 3076 } 3077 } 3078 else 3079 #ifndef OPENSSL_NO_RSA 3080 if (pkey->type == EVP_PKEY_RSA) 3081 { 3082 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, 3083 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 3084 pkey->pkey.rsa); 3085 if (i < 0) 3086 { 3087 al=SSL_AD_DECRYPT_ERROR; 3088 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); 3089 goto f_err; 3090 } 3091 if (i == 0) 3092 { 3093 al=SSL_AD_DECRYPT_ERROR; 3094 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); 3095 goto f_err; 3096 } 3097 } 3098 else 3099 #endif 3100 #ifndef OPENSSL_NO_DSA 3101 if (pkey->type == EVP_PKEY_DSA) 3102 { 3103 j=DSA_verify(pkey->save_type, 3104 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 3105 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); 3106 if (j <= 0) 3107 { 3108 /* bad signature */ 3109 al=SSL_AD_DECRYPT_ERROR; 3110 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); 3111 goto f_err; 3112 } 3113 } 3114 else 3115 #endif 3116 #ifndef OPENSSL_NO_ECDSA 3117 if (pkey->type == EVP_PKEY_EC) 3118 { 3119 j=ECDSA_verify(pkey->save_type, 3120 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 3121 SHA_DIGEST_LENGTH,p,i,pkey->pkey.ec); 3122 if (j <= 0) 3123 { 3124 /* bad signature */ 3125 al=SSL_AD_DECRYPT_ERROR; 3126 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, 3127 SSL_R_BAD_ECDSA_SIGNATURE); 3128 goto f_err; 3129 } 3130 } 3131 else 3132 #endif 3133 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001) 3134 { unsigned char signature[64]; 3135 int idx; 3136 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey,NULL); 3137 EVP_PKEY_verify_init(pctx); 3138 if (i!=64) { 3139 fprintf(stderr,"GOST signature length is %d",i); 3140 } 3141 for (idx=0;idx<64;idx++) { 3142 signature[63-idx]=p[idx]; 3143 } 3144 j=EVP_PKEY_verify(pctx,signature,64,s->s3->tmp.cert_verify_md,32); 3145 EVP_PKEY_CTX_free(pctx); 3146 if (j<=0) 3147 { 3148 al=SSL_AD_DECRYPT_ERROR; 3149 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, 3150 SSL_R_BAD_ECDSA_SIGNATURE); 3151 goto f_err; 3152 } 3153 } 3154 else 3155 { 3156 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR); 3157 al=SSL_AD_UNSUPPORTED_CERTIFICATE; 3158 goto f_err; 3159 } 3160 3161 3162 ret=1; 3163 if (0) 3164 { 3165 f_err: 3166 ssl3_send_alert(s,SSL3_AL_FATAL,al); 3167 } 3168 end: 3169 if (s->s3->handshake_buffer) 3170 { 3171 BIO_free(s->s3->handshake_buffer); 3172 s->s3->handshake_buffer = NULL; 3173 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE; 3174 } 3175 EVP_MD_CTX_cleanup(&mctx); 3176 EVP_PKEY_free(pkey); 3177 return(ret); 3178 } 3179 3180 int ssl3_get_client_certificate(SSL *s) 3181 { 3182 int i,ok,al,ret= -1; 3183 X509 *x=NULL; 3184 unsigned long l,nc,llen,n; 3185 const unsigned char *p,*q; 3186 unsigned char *d; 3187 STACK_OF(X509) *sk=NULL; 3188 3189 n=s->method->ssl_get_message(s, 3190 SSL3_ST_SR_CERT_A, 3191 SSL3_ST_SR_CERT_B, 3192 -1, 3193 s->max_cert_list, 3194 &ok); 3195 3196 if (!ok) return((int)n); 3197 3198 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) 3199 { 3200 if ( (s->verify_mode & SSL_VERIFY_PEER) && 3201 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 3202 { 3203 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 3204 al=SSL_AD_HANDSHAKE_FAILURE; 3205 goto f_err; 3206 } 3207 /* If tls asked for a client cert, the client must return a 0 list */ 3208 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) 3209 { 3210 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 3211 al=SSL_AD_UNEXPECTED_MESSAGE; 3212 goto f_err; 3213 } 3214 s->s3->tmp.reuse_message=1; 3215 return(1); 3216 } 3217 3218 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 3219 { 3220 al=SSL_AD_UNEXPECTED_MESSAGE; 3221 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); 3222 goto f_err; 3223 } 3224 p=d=(unsigned char *)s->init_msg; 3225 3226 if ((sk=sk_X509_new_null()) == NULL) 3227 { 3228 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 3229 goto err; 3230 } 3231 3232 n2l3(p,llen); 3233 if (llen+3 != n) 3234 { 3235 al=SSL_AD_DECODE_ERROR; 3236 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 3237 goto f_err; 3238 } 3239 for (nc=0; nc<llen; ) 3240 { 3241 n2l3(p,l); 3242 if ((l+nc+3) > llen) 3243 { 3244 al=SSL_AD_DECODE_ERROR; 3245 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 3246 goto f_err; 3247 } 3248 3249 q=p; 3250 x=d2i_X509(NULL,&p,l); 3251 if (x == NULL) 3252 { 3253 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); 3254 goto err; 3255 } 3256 if (p != (q+l)) 3257 { 3258 al=SSL_AD_DECODE_ERROR; 3259 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 3260 goto f_err; 3261 } 3262 if (!sk_X509_push(sk,x)) 3263 { 3264 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 3265 goto err; 3266 } 3267 x=NULL; 3268 nc+=l+3; 3269 } 3270 3271 if (sk_X509_num(sk) <= 0) 3272 { 3273 /* TLS does not mind 0 certs returned */ 3274 if (s->version == SSL3_VERSION) 3275 { 3276 al=SSL_AD_HANDSHAKE_FAILURE; 3277 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); 3278 goto f_err; 3279 } 3280 /* Fail for TLS only if we required a certificate */ 3281 else if ((s->verify_mode & SSL_VERIFY_PEER) && 3282 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 3283 { 3284 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 3285 al=SSL_AD_HANDSHAKE_FAILURE; 3286 goto f_err; 3287 } 3288 /* No client certificate so digest cached records */ 3289 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) 3290 { 3291 al=SSL_AD_INTERNAL_ERROR; 3292 goto f_err; 3293 } 3294 } 3295 else 3296 { 3297 i=ssl_verify_cert_chain(s,sk); 3298 if (i <= 0) 3299 { 3300 al=ssl_verify_alarm_type(s->verify_result); 3301 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); 3302 goto f_err; 3303 } 3304 } 3305 3306 if (s->session->peer != NULL) /* This should not be needed */ 3307 X509_free(s->session->peer); 3308 s->session->peer=sk_X509_shift(sk); 3309 s->session->verify_result = s->verify_result; 3310 3311 /* With the current implementation, sess_cert will always be NULL 3312 * when we arrive here. */ 3313 if (s->session->sess_cert == NULL) 3314 { 3315 s->session->sess_cert = ssl_sess_cert_new(); 3316 if (s->session->sess_cert == NULL) 3317 { 3318 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); 3319 goto err; 3320 } 3321 } 3322 if (s->session->sess_cert->cert_chain != NULL) 3323 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); 3324 s->session->sess_cert->cert_chain=sk; 3325 /* Inconsistency alert: cert_chain does *not* include the 3326 * peer's own certificate, while we do include it in s3_clnt.c */ 3327 3328 sk=NULL; 3329 3330 ret=1; 3331 if (0) 3332 { 3333 f_err: 3334 ssl3_send_alert(s,SSL3_AL_FATAL,al); 3335 } 3336 err: 3337 if (x != NULL) X509_free(x); 3338 if (sk != NULL) sk_X509_pop_free(sk,X509_free); 3339 return(ret); 3340 } 3341 3342 int ssl3_send_server_certificate(SSL *s) 3343 { 3344 unsigned long l; 3345 X509 *x; 3346 3347 if (s->state == SSL3_ST_SW_CERT_A) 3348 { 3349 x=ssl_get_server_send_cert(s); 3350 if (x == NULL) 3351 { 3352 /* VRS: allow null cert if auth == KRB5 */ 3353 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) || 3354 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) 3355 { 3356 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); 3357 return(0); 3358 } 3359 } 3360 3361 l=ssl3_output_cert_chain(s,x); 3362 s->state=SSL3_ST_SW_CERT_B; 3363 s->init_num=(int)l; 3364 s->init_off=0; 3365 } 3366 3367 /* SSL3_ST_SW_CERT_B */ 3368 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 3369 } 3370 3371 #ifndef OPENSSL_NO_TLSEXT 3372 /* send a new session ticket (not necessarily for a new session) */ 3373 int ssl3_send_newsession_ticket(SSL *s) 3374 { 3375 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) 3376 { 3377 unsigned char *p, *senc, *macstart; 3378 const unsigned char *const_p; 3379 int len, slen_full, slen; 3380 SSL_SESSION *sess; 3381 unsigned int hlen; 3382 EVP_CIPHER_CTX ctx; 3383 HMAC_CTX hctx; 3384 SSL_CTX *tctx = s->initial_ctx; 3385 unsigned char iv[EVP_MAX_IV_LENGTH]; 3386 unsigned char key_name[16]; 3387 3388 /* get session encoding length */ 3389 slen_full = i2d_SSL_SESSION(s->session, NULL); 3390 /* Some length values are 16 bits, so forget it if session is 3391 * too long 3392 */ 3393 if (slen_full > 0xFF00) 3394 return -1; 3395 senc = OPENSSL_malloc(slen_full); 3396 if (!senc) 3397 return -1; 3398 p = senc; 3399 i2d_SSL_SESSION(s->session, &p); 3400 3401 /* create a fresh copy (not shared with other threads) to clean up */ 3402 const_p = senc; 3403 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full); 3404 if (sess == NULL) 3405 { 3406 OPENSSL_free(senc); 3407 return -1; 3408 } 3409 sess->session_id_length = 0; /* ID is irrelevant for the ticket */ 3410 3411 slen = i2d_SSL_SESSION(sess, NULL); 3412 if (slen > slen_full) /* shouldn't ever happen */ 3413 { 3414 OPENSSL_free(senc); 3415 return -1; 3416 } 3417 p = senc; 3418 i2d_SSL_SESSION(sess, &p); 3419 SSL_SESSION_free(sess); 3420 3421 /* Grow buffer if need be: the length calculation is as 3422 * follows 1 (size of message name) + 3 (message length 3423 * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) + 3424 * 16 (key name) + max_iv_len (iv length) + 3425 * session_length + max_enc_block_size (max encrypted session 3426 * length) + max_md_size (HMAC). 3427 */ 3428 if (!BUF_MEM_grow(s->init_buf, 3429 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + 3430 EVP_MAX_MD_SIZE + slen)) 3431 return -1; 3432 3433 p=(unsigned char *)s->init_buf->data; 3434 /* do the header */ 3435 *(p++)=SSL3_MT_NEWSESSION_TICKET; 3436 /* Skip message length for now */ 3437 p += 3; 3438 EVP_CIPHER_CTX_init(&ctx); 3439 HMAC_CTX_init(&hctx); 3440 /* Initialize HMAC and cipher contexts. If callback present 3441 * it does all the work otherwise use generated values 3442 * from parent ctx. 3443 */ 3444 if (tctx->tlsext_ticket_key_cb) 3445 { 3446 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, 3447 &hctx, 1) < 0) 3448 { 3449 OPENSSL_free(senc); 3450 return -1; 3451 } 3452 } 3453 else 3454 { 3455 RAND_pseudo_bytes(iv, 16); 3456 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, 3457 tctx->tlsext_tick_aes_key, iv); 3458 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, 3459 tlsext_tick_md(), NULL); 3460 memcpy(key_name, tctx->tlsext_tick_key_name, 16); 3461 } 3462 3463 /* Ticket lifetime hint (advisory only): 3464 * We leave this unspecified for resumed session (for simplicity), 3465 * and guess that tickets for new sessions will live as long 3466 * as their sessions. */ 3467 l2n(s->hit ? 0 : s->session->timeout, p); 3468 3469 /* Skip ticket length for now */ 3470 p += 2; 3471 /* Output key name */ 3472 macstart = p; 3473 memcpy(p, key_name, 16); 3474 p += 16; 3475 /* output IV */ 3476 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); 3477 p += EVP_CIPHER_CTX_iv_length(&ctx); 3478 /* Encrypt session data */ 3479 EVP_EncryptUpdate(&ctx, p, &len, senc, slen); 3480 p += len; 3481 EVP_EncryptFinal(&ctx, p, &len); 3482 p += len; 3483 EVP_CIPHER_CTX_cleanup(&ctx); 3484 3485 HMAC_Update(&hctx, macstart, p - macstart); 3486 HMAC_Final(&hctx, p, &hlen); 3487 HMAC_CTX_cleanup(&hctx); 3488 3489 p += hlen; 3490 /* Now write out lengths: p points to end of data written */ 3491 /* Total length */ 3492 len = p - (unsigned char *)s->init_buf->data; 3493 p=(unsigned char *)s->init_buf->data + 1; 3494 l2n3(len - 4, p); /* Message length */ 3495 p += 4; 3496 s2n(len - 10, p); /* Ticket length */ 3497 3498 /* number of bytes to write */ 3499 s->init_num= len; 3500 s->state=SSL3_ST_SW_SESSION_TICKET_B; 3501 s->init_off=0; 3502 OPENSSL_free(senc); 3503 } 3504 3505 /* SSL3_ST_SW_SESSION_TICKET_B */ 3506 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 3507 } 3508 3509 int ssl3_send_cert_status(SSL *s) 3510 { 3511 if (s->state == SSL3_ST_SW_CERT_STATUS_A) 3512 { 3513 unsigned char *p; 3514 /* Grow buffer if need be: the length calculation is as 3515 * follows 1 (message type) + 3 (message length) + 3516 * 1 (ocsp response type) + 3 (ocsp response length) 3517 * + (ocsp response) 3518 */ 3519 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) 3520 return -1; 3521 3522 p=(unsigned char *)s->init_buf->data; 3523 3524 /* do the header */ 3525 *(p++)=SSL3_MT_CERTIFICATE_STATUS; 3526 /* message length */ 3527 l2n3(s->tlsext_ocsp_resplen + 4, p); 3528 /* status type */ 3529 *(p++)= s->tlsext_status_type; 3530 /* length of OCSP response */ 3531 l2n3(s->tlsext_ocsp_resplen, p); 3532 /* actual response */ 3533 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen); 3534 /* number of bytes to write */ 3535 s->init_num = 8 + s->tlsext_ocsp_resplen; 3536 s->state=SSL3_ST_SW_CERT_STATUS_B; 3537 s->init_off = 0; 3538 } 3539 3540 /* SSL3_ST_SW_CERT_STATUS_B */ 3541 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 3542 } 3543 3544 # ifndef OPENSSL_NO_NEXTPROTONEG 3545 /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It 3546 * sets the next_proto member in s if found */ 3547 int ssl3_get_next_proto(SSL *s) 3548 { 3549 int ok; 3550 int proto_len, padding_len; 3551 long n; 3552 const unsigned char *p; 3553 3554 /* Clients cannot send a NextProtocol message if we didn't see the 3555 * extension in their ClientHello */ 3556 if (!s->s3->next_proto_neg_seen) 3557 { 3558 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); 3559 return -1; 3560 } 3561 3562 n=s->method->ssl_get_message(s, 3563 SSL3_ST_SR_NEXT_PROTO_A, 3564 SSL3_ST_SR_NEXT_PROTO_B, 3565 SSL3_MT_NEXT_PROTO, 3566 514, /* See the payload format below */ 3567 &ok); 3568 3569 if (!ok) 3570 return((int)n); 3571 3572 /* s->state doesn't reflect whether ChangeCipherSpec has been received 3573 * in this handshake, but s->s3->change_cipher_spec does (will be reset 3574 * by ssl3_get_finished). */ 3575 if (!s->s3->change_cipher_spec) 3576 { 3577 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); 3578 return -1; 3579 } 3580 3581 if (n < 2) 3582 return 0; /* The body must be > 1 bytes long */ 3583 3584 p=(unsigned char *)s->init_msg; 3585 3586 /* The payload looks like: 3587 * uint8 proto_len; 3588 * uint8 proto[proto_len]; 3589 * uint8 padding_len; 3590 * uint8 padding[padding_len]; 3591 */ 3592 proto_len = p[0]; 3593 if (proto_len + 2 > s->init_num) 3594 return 0; 3595 padding_len = p[proto_len + 1]; 3596 if (proto_len + padding_len + 2 != s->init_num) 3597 return 0; 3598 3599 s->next_proto_negotiated = OPENSSL_malloc(proto_len); 3600 if (!s->next_proto_negotiated) 3601 { 3602 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,ERR_R_MALLOC_FAILURE); 3603 return 0; 3604 } 3605 memcpy(s->next_proto_negotiated, p + 1, proto_len); 3606 s->next_proto_negotiated_len = proto_len; 3607 3608 return 1; 3609 } 3610 # endif 3611 #endif 3612