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