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