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