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