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