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