1 /* ssl/s3_clnt.c */ 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 #include <stdio.h> 152 #include "ssl_locl.h" 153 #include "kssl_lcl.h" 154 #include <openssl/buffer.h> 155 #include <openssl/rand.h> 156 #include <openssl/objects.h> 157 #include <openssl/evp.h> 158 #include <openssl/md5.h> 159 #ifndef OPENSSL_NO_DH 160 #include <openssl/dh.h> 161 #endif 162 #include <openssl/bn.h> 163 #ifndef OPENSSL_NO_ENGINE 164 #include <openssl/engine.h> 165 #endif 166 167 static const SSL_METHOD *ssl3_get_client_method(int ver); 168 static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); 169 170 static const SSL_METHOD *ssl3_get_client_method(int ver) 171 { 172 if (ver == SSL3_VERSION) 173 return(SSLv3_client_method()); 174 else 175 return(NULL); 176 } 177 178 IMPLEMENT_ssl3_meth_func(SSLv3_client_method, 179 ssl_undefined_function, 180 ssl3_connect, 181 ssl3_get_client_method) 182 183 int ssl3_connect(SSL *s) 184 { 185 BUF_MEM *buf=NULL; 186 unsigned long Time=(unsigned long)time(NULL); 187 void (*cb)(const SSL *ssl,int type,int val)=NULL; 188 int ret= -1; 189 int new_state,state,skip=0; 190 191 RAND_add(&Time,sizeof(Time),0); 192 ERR_clear_error(); 193 clear_sys_error(); 194 195 if (s->info_callback != NULL) 196 cb=s->info_callback; 197 else if (s->ctx->info_callback != NULL) 198 cb=s->ctx->info_callback; 199 200 s->in_handshake++; 201 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 202 #if 0 /* Send app data in separate packet, otherwise, some particular site 203 * (only one site so far) closes the socket. 204 * Note: there is a very small chance that two TCP packets 205 * could be arriving at server combined into a single TCP packet, 206 * then trigger that site to break. We haven't encounter that though. 207 */ 208 if (SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) 209 { 210 /* Send app data along with CCS/Finished */ 211 s->s3->flags |= SSL3_FLAGS_DELAY_CLIENT_FINISHED; 212 } 213 #endif 214 215 for (;;) 216 { 217 state=s->state; 218 219 switch(s->state) 220 { 221 case SSL_ST_RENEGOTIATE: 222 s->new_session=1; 223 s->state=SSL_ST_CONNECT; 224 s->ctx->stats.sess_connect_renegotiate++; 225 /* break */ 226 case SSL_ST_BEFORE: 227 case SSL_ST_CONNECT: 228 case SSL_ST_BEFORE|SSL_ST_CONNECT: 229 case SSL_ST_OK|SSL_ST_CONNECT: 230 231 s->server=0; 232 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 233 234 if ((s->version & 0xff00 ) != 0x0300) 235 { 236 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); 237 ret = -1; 238 goto end; 239 } 240 241 /* s->version=SSL3_VERSION; */ 242 s->type=SSL_ST_CONNECT; 243 244 if (s->init_buf == NULL) 245 { 246 if ((buf=BUF_MEM_new()) == NULL) 247 { 248 ret= -1; 249 goto end; 250 } 251 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 252 { 253 ret= -1; 254 goto end; 255 } 256 s->init_buf=buf; 257 buf=NULL; 258 } 259 260 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } 261 262 /* setup buffing BIO */ 263 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } 264 265 /* don't push the buffering BIO quite yet */ 266 267 ssl3_init_finished_mac(s); 268 269 s->state=SSL3_ST_CW_CLNT_HELLO_A; 270 s->ctx->stats.sess_connect++; 271 s->init_num=0; 272 break; 273 274 case SSL3_ST_CW_CLNT_HELLO_A: 275 case SSL3_ST_CW_CLNT_HELLO_B: 276 277 s->shutdown=0; 278 ret=ssl3_client_hello(s); 279 if (ret <= 0) goto end; 280 s->state=SSL3_ST_CR_SRVR_HELLO_A; 281 s->init_num=0; 282 283 /* turn on buffering for the next lot of output */ 284 if (s->bbio != s->wbio) 285 s->wbio=BIO_push(s->bbio,s->wbio); 286 287 break; 288 289 case SSL3_ST_CR_SRVR_HELLO_A: 290 case SSL3_ST_CR_SRVR_HELLO_B: 291 ret=ssl3_get_server_hello(s); 292 if (ret <= 0) goto end; 293 294 if (s->hit) 295 s->state=SSL3_ST_CR_FINISHED_A; 296 else 297 s->state=SSL3_ST_CR_CERT_A; 298 s->init_num=0; 299 break; 300 301 case SSL3_ST_CR_CERT_A: 302 case SSL3_ST_CR_CERT_B: 303 #ifndef OPENSSL_NO_TLSEXT 304 ret=ssl3_check_finished(s); 305 if (ret <= 0) goto end; 306 if (ret == 2) 307 { 308 s->hit = 1; 309 if (s->tlsext_ticket_expected) 310 s->state=SSL3_ST_CR_SESSION_TICKET_A; 311 else 312 s->state=SSL3_ST_CR_FINISHED_A; 313 s->init_num=0; 314 break; 315 } 316 #endif 317 /* Check if it is anon DH/ECDH */ 318 /* or PSK */ 319 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && 320 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 321 { 322 ret=ssl3_get_server_certificate(s); 323 if (ret <= 0) goto end; 324 #ifndef OPENSSL_NO_TLSEXT 325 if (s->tlsext_status_expected) 326 s->state=SSL3_ST_CR_CERT_STATUS_A; 327 else 328 s->state=SSL3_ST_CR_KEY_EXCH_A; 329 } 330 else 331 { 332 skip = 1; 333 s->state=SSL3_ST_CR_KEY_EXCH_A; 334 } 335 #else 336 } 337 else 338 skip=1; 339 340 s->state=SSL3_ST_CR_KEY_EXCH_A; 341 #endif 342 s->init_num=0; 343 break; 344 345 case SSL3_ST_CR_KEY_EXCH_A: 346 case SSL3_ST_CR_KEY_EXCH_B: 347 ret=ssl3_get_key_exchange(s); 348 if (ret <= 0) goto end; 349 s->state=SSL3_ST_CR_CERT_REQ_A; 350 s->init_num=0; 351 352 /* at this point we check that we have the 353 * required stuff from the server */ 354 if (!ssl3_check_cert_and_algorithm(s)) 355 { 356 ret= -1; 357 goto end; 358 } 359 break; 360 361 case SSL3_ST_CR_CERT_REQ_A: 362 case SSL3_ST_CR_CERT_REQ_B: 363 ret=ssl3_get_certificate_request(s); 364 if (ret <= 0) goto end; 365 s->state=SSL3_ST_CR_SRVR_DONE_A; 366 s->init_num=0; 367 break; 368 369 case SSL3_ST_CR_SRVR_DONE_A: 370 case SSL3_ST_CR_SRVR_DONE_B: 371 ret=ssl3_get_server_done(s); 372 if (ret <= 0) goto end; 373 if (s->s3->tmp.cert_req) 374 s->state=SSL3_ST_CW_CERT_A; 375 else 376 s->state=SSL3_ST_CW_KEY_EXCH_A; 377 s->init_num=0; 378 379 break; 380 381 case SSL3_ST_CW_CERT_A: 382 case SSL3_ST_CW_CERT_B: 383 case SSL3_ST_CW_CERT_C: 384 case SSL3_ST_CW_CERT_D: 385 ret=ssl3_send_client_certificate(s); 386 if (ret <= 0) goto end; 387 s->state=SSL3_ST_CW_KEY_EXCH_A; 388 s->init_num=0; 389 break; 390 391 case SSL3_ST_CW_KEY_EXCH_A: 392 case SSL3_ST_CW_KEY_EXCH_B: 393 ret=ssl3_send_client_key_exchange(s); 394 if (ret <= 0) goto end; 395 /* EAY EAY EAY need to check for DH fix cert 396 * sent back */ 397 /* For TLS, cert_req is set to 2, so a cert chain 398 * of nothing is sent, but no verify packet is sent */ 399 /* XXX: For now, we do not support client 400 * authentication in ECDH cipher suites with 401 * ECDH (rather than ECDSA) certificates. 402 * We need to skip the certificate verify 403 * message when client's ECDH public key is sent 404 * inside the client certificate. 405 */ 406 if (s->s3->tmp.cert_req == 1) 407 { 408 s->state=SSL3_ST_CW_CERT_VRFY_A; 409 } 410 else 411 { 412 s->state=SSL3_ST_CW_CHANGE_A; 413 s->s3->change_cipher_spec=0; 414 } 415 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) 416 { 417 s->state=SSL3_ST_CW_CHANGE_A; 418 s->s3->change_cipher_spec=0; 419 } 420 421 s->init_num=0; 422 break; 423 424 case SSL3_ST_CW_CERT_VRFY_A: 425 case SSL3_ST_CW_CERT_VRFY_B: 426 ret=ssl3_send_client_verify(s); 427 if (ret <= 0) goto end; 428 s->state=SSL3_ST_CW_CHANGE_A; 429 s->init_num=0; 430 s->s3->change_cipher_spec=0; 431 break; 432 433 case SSL3_ST_CW_CHANGE_A: 434 case SSL3_ST_CW_CHANGE_B: 435 ret=ssl3_send_change_cipher_spec(s, 436 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); 437 if (ret <= 0) goto end; 438 439 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG) 440 s->state=SSL3_ST_CW_FINISHED_A; 441 #else 442 if (s->next_proto_negotiated) 443 s->state=SSL3_ST_CW_NEXT_PROTO_A; 444 else 445 s->state=SSL3_ST_CW_FINISHED_A; 446 #endif 447 448 s->init_num=0; 449 450 s->session->cipher=s->s3->tmp.new_cipher; 451 #ifdef OPENSSL_NO_COMP 452 s->session->compress_meth=0; 453 #else 454 if (s->s3->tmp.new_compression == NULL) 455 s->session->compress_meth=0; 456 else 457 s->session->compress_meth= 458 s->s3->tmp.new_compression->id; 459 #endif 460 if (!s->method->ssl3_enc->setup_key_block(s)) 461 { 462 ret= -1; 463 goto end; 464 } 465 466 if (!s->method->ssl3_enc->change_cipher_state(s, 467 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) 468 { 469 ret= -1; 470 goto end; 471 } 472 473 break; 474 475 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG) 476 case SSL3_ST_CW_NEXT_PROTO_A: 477 case SSL3_ST_CW_NEXT_PROTO_B: 478 ret=ssl3_send_next_proto(s); 479 if (ret <= 0) goto end; 480 s->state=SSL3_ST_CW_FINISHED_A; 481 break; 482 #endif 483 484 case SSL3_ST_CW_FINISHED_A: 485 case SSL3_ST_CW_FINISHED_B: 486 ret=ssl3_send_finished(s, 487 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, 488 s->method->ssl3_enc->client_finished_label, 489 s->method->ssl3_enc->client_finished_label_len); 490 if (ret <= 0) goto end; 491 s->state=SSL3_ST_CW_FLUSH; 492 493 /* clear flags */ 494 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; 495 if (s->hit) 496 { 497 s->s3->tmp.next_state=SSL_ST_OK; 498 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 499 { 500 s->state=SSL_ST_OK; 501 s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 502 s->s3->delay_buf_pop_ret=0; 503 } 504 } 505 else 506 { 507 if ((SSL_get_mode(s) & SSL_MODE_HANDSHAKE_CUTTHROUGH) && SSL_get_cipher_bits(s, NULL) >= 128 508 && s->s3->previous_server_finished_len == 0 /* no cutthrough on renegotiation (would complicate the state machine) */ 509 ) 510 { 511 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 512 { 513 s->state=SSL3_ST_CUTTHROUGH_COMPLETE; 514 s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 515 s->s3->delay_buf_pop_ret=0; 516 } 517 else 518 { 519 s->s3->tmp.next_state=SSL3_ST_CUTTHROUGH_COMPLETE; 520 } 521 } 522 else 523 { 524 #ifndef OPENSSL_NO_TLSEXT 525 /* Allow NewSessionTicket if ticket expected */ 526 if (s->tlsext_ticket_expected) 527 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 528 else 529 #endif 530 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 531 } 532 } 533 s->init_num=0; 534 break; 535 536 #ifndef OPENSSL_NO_TLSEXT 537 case SSL3_ST_CR_SESSION_TICKET_A: 538 case SSL3_ST_CR_SESSION_TICKET_B: 539 ret=ssl3_get_new_session_ticket(s); 540 if (ret <= 0) goto end; 541 s->state=SSL3_ST_CR_FINISHED_A; 542 s->init_num=0; 543 break; 544 545 case SSL3_ST_CR_CERT_STATUS_A: 546 case SSL3_ST_CR_CERT_STATUS_B: 547 ret=ssl3_get_cert_status(s); 548 if (ret <= 0) goto end; 549 s->state=SSL3_ST_CR_KEY_EXCH_A; 550 s->init_num=0; 551 break; 552 #endif 553 554 case SSL3_ST_CR_FINISHED_A: 555 case SSL3_ST_CR_FINISHED_B: 556 557 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, 558 SSL3_ST_CR_FINISHED_B); 559 if (ret <= 0) goto end; 560 561 if (s->hit) 562 s->state=SSL3_ST_CW_CHANGE_A; 563 else 564 s->state=SSL_ST_OK; 565 s->init_num=0; 566 break; 567 568 case SSL3_ST_CW_FLUSH: 569 s->rwstate=SSL_WRITING; 570 if (BIO_flush(s->wbio) <= 0) 571 { 572 ret= -1; 573 goto end; 574 } 575 s->rwstate=SSL_NOTHING; 576 s->state=s->s3->tmp.next_state; 577 break; 578 579 case SSL3_ST_CUTTHROUGH_COMPLETE: 580 #ifndef OPENSSL_NO_TLSEXT 581 /* Allow NewSessionTicket if ticket expected */ 582 if (s->tlsext_ticket_expected) 583 s->state=SSL3_ST_CR_SESSION_TICKET_A; 584 else 585 #endif 586 s->state=SSL3_ST_CR_FINISHED_A; 587 588 /* SSL_write() will take care of flushing buffered data if 589 * DELAY_CLIENT_FINISHED is set. 590 */ 591 if (!(s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)) 592 ssl_free_wbio_buffer(s); 593 ret = 1; 594 goto end; 595 /* break; */ 596 597 case SSL_ST_OK: 598 /* clean a few things up */ 599 ssl3_cleanup_key_block(s); 600 601 if (s->init_buf != NULL) 602 { 603 BUF_MEM_free(s->init_buf); 604 s->init_buf=NULL; 605 } 606 607 /* If we are not 'joining' the last two packets, 608 * remove the buffering now */ 609 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) 610 ssl_free_wbio_buffer(s); 611 /* else do it later in ssl3_write */ 612 613 s->init_num=0; 614 s->new_session=0; 615 616 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); 617 if (s->hit) s->ctx->stats.sess_hit++; 618 619 ret=1; 620 /* s->server=0; */ 621 s->handshake_func=ssl3_connect; 622 s->ctx->stats.sess_connect_good++; 623 624 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 625 626 goto end; 627 /* break; */ 628 629 default: 630 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); 631 ret= -1; 632 goto end; 633 /* break; */ 634 } 635 636 /* did we do anything */ 637 if (!s->s3->tmp.reuse_message && !skip) 638 { 639 if (s->debug) 640 { 641 if ((ret=BIO_flush(s->wbio)) <= 0) 642 goto end; 643 } 644 645 if ((cb != NULL) && (s->state != state)) 646 { 647 new_state=s->state; 648 s->state=state; 649 cb(s,SSL_CB_CONNECT_LOOP,1); 650 s->state=new_state; 651 } 652 } 653 skip=0; 654 } 655 end: 656 s->in_handshake--; 657 if (buf != NULL) 658 BUF_MEM_free(buf); 659 if (cb != NULL) 660 cb(s,SSL_CB_CONNECT_EXIT,ret); 661 return(ret); 662 } 663 664 665 int ssl3_client_hello(SSL *s) 666 { 667 unsigned char *buf; 668 unsigned char *p,*d; 669 int i; 670 unsigned long Time,l; 671 #ifndef OPENSSL_NO_COMP 672 int j; 673 SSL_COMP *comp; 674 #endif 675 676 buf=(unsigned char *)s->init_buf->data; 677 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) 678 { 679 SSL_SESSION *sess = s->session; 680 if ((sess == NULL) || 681 (sess->ssl_version != s->version) || 682 #ifdef OPENSSL_NO_TLSEXT 683 !sess->session_id_length || 684 #else 685 (!sess->session_id_length && !sess->tlsext_tick) || 686 #endif 687 (sess->not_resumable)) 688 { 689 if (!s->session_creation_enabled) 690 { 691 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 692 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_SESSION_MAY_NOT_BE_CREATED); 693 goto err; 694 } 695 if (!ssl_get_new_session(s,0)) 696 goto err; 697 } 698 /* else use the pre-loaded session */ 699 700 p=s->s3->client_random; 701 Time=(unsigned long)time(NULL); /* Time */ 702 l2n(Time,p); 703 if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) 704 goto err; 705 706 /* Do the message type and length last */ 707 d=p= &(buf[4]); 708 709 *(p++)=s->version>>8; 710 *(p++)=s->version&0xff; 711 s->client_version=s->version; 712 713 /* Random stuff */ 714 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); 715 p+=SSL3_RANDOM_SIZE; 716 717 /* Session ID */ 718 if (s->new_session) 719 i=0; 720 else 721 i=s->session->session_id_length; 722 *(p++)=i; 723 if (i != 0) 724 { 725 if (i > (int)sizeof(s->session->session_id)) 726 { 727 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 728 goto err; 729 } 730 memcpy(p,s->session->session_id,i); 731 p+=i; 732 } 733 734 /* Ciphers supported */ 735 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); 736 if (i == 0) 737 { 738 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); 739 goto err; 740 } 741 s2n(i,p); 742 p+=i; 743 744 /* COMPRESSION */ 745 #ifdef OPENSSL_NO_COMP 746 *(p++)=1; 747 #else 748 749 if ((s->options & SSL_OP_NO_COMPRESSION) 750 || !s->ctx->comp_methods) 751 j=0; 752 else 753 j=sk_SSL_COMP_num(s->ctx->comp_methods); 754 *(p++)=1+j; 755 for (i=0; i<j; i++) 756 { 757 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); 758 *(p++)=comp->id; 759 } 760 #endif 761 *(p++)=0; /* Add the NULL method */ 762 763 #ifndef OPENSSL_NO_TLSEXT 764 /* TLS extensions*/ 765 if (ssl_prepare_clienthello_tlsext(s) <= 0) 766 { 767 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_CLIENTHELLO_TLSEXT); 768 goto err; 769 } 770 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) 771 { 772 SSLerr(SSL_F_SSL3_CLIENT_HELLO,ERR_R_INTERNAL_ERROR); 773 goto err; 774 } 775 #endif 776 777 l=(p-d); 778 d=buf; 779 *(d++)=SSL3_MT_CLIENT_HELLO; 780 l2n3(l,d); 781 782 s->state=SSL3_ST_CW_CLNT_HELLO_B; 783 /* number of bytes to write */ 784 s->init_num=p-buf; 785 s->init_off=0; 786 } 787 788 /* SSL3_ST_CW_CLNT_HELLO_B */ 789 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 790 err: 791 return(-1); 792 } 793 794 int ssl3_get_server_hello(SSL *s) 795 { 796 STACK_OF(SSL_CIPHER) *sk; 797 const SSL_CIPHER *c; 798 unsigned char *p,*d; 799 int i,al,ok; 800 unsigned int j; 801 long n; 802 #ifndef OPENSSL_NO_COMP 803 SSL_COMP *comp; 804 #endif 805 806 n=s->method->ssl_get_message(s, 807 SSL3_ST_CR_SRVR_HELLO_A, 808 SSL3_ST_CR_SRVR_HELLO_B, 809 -1, 810 20000, /* ?? */ 811 &ok); 812 813 if (!ok) return((int)n); 814 815 if ( SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) 816 { 817 if ( s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) 818 { 819 if ( s->d1->send_cookie == 0) 820 { 821 s->s3->tmp.reuse_message = 1; 822 return 1; 823 } 824 else /* already sent a cookie */ 825 { 826 al=SSL_AD_UNEXPECTED_MESSAGE; 827 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE); 828 goto f_err; 829 } 830 } 831 } 832 833 if ( s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) 834 { 835 al=SSL_AD_UNEXPECTED_MESSAGE; 836 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_MESSAGE_TYPE); 837 goto f_err; 838 } 839 840 d=p=(unsigned char *)s->init_msg; 841 842 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) 843 { 844 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION); 845 s->version=(s->version&0xff00)|p[1]; 846 al=SSL_AD_PROTOCOL_VERSION; 847 goto f_err; 848 } 849 p+=2; 850 851 /* load the server hello data */ 852 /* load the server random */ 853 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); 854 p+=SSL3_RANDOM_SIZE; 855 856 /* get the session-id */ 857 j= *(p++); 858 859 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) 860 { 861 al=SSL_AD_ILLEGAL_PARAMETER; 862 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG); 863 goto f_err; 864 } 865 866 #ifndef OPENSSL_NO_TLSEXT 867 /* check if we want to resume the session based on external pre-shared secret */ 868 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb) 869 { 870 SSL_CIPHER *pref_cipher=NULL; 871 s->session->master_key_length=sizeof(s->session->master_key); 872 if (s->tls_session_secret_cb(s, s->session->master_key, 873 &s->session->master_key_length, 874 NULL, &pref_cipher, 875 s->tls_session_secret_cb_arg)) 876 { 877 s->session->cipher = pref_cipher ? 878 pref_cipher : ssl_get_cipher_by_char(s, p+j); 879 } 880 } 881 #endif /* OPENSSL_NO_TLSEXT */ 882 883 if (j != 0 && j == s->session->session_id_length 884 && memcmp(p,s->session->session_id,j) == 0) 885 { 886 if(s->sid_ctx_length != s->session->sid_ctx_length 887 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) 888 { 889 /* actually a client application bug */ 890 al=SSL_AD_ILLEGAL_PARAMETER; 891 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 892 goto f_err; 893 } 894 s->hit=1; 895 } 896 else /* a miss or crap from the other end */ 897 { 898 /* If we were trying for session-id reuse, make a new 899 * SSL_SESSION so we don't stuff up other people */ 900 s->hit=0; 901 if (s->session->session_id_length > 0) 902 { 903 if (!s->session_creation_enabled) 904 { 905 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 906 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SESSION_MAY_NOT_BE_CREATED); 907 goto err; 908 } 909 if (!ssl_get_new_session(s,0)) 910 { 911 al=SSL_AD_INTERNAL_ERROR; 912 goto f_err; 913 } 914 } 915 s->session->session_id_length=j; 916 memcpy(s->session->session_id,p,j); /* j could be 0 */ 917 } 918 p+=j; 919 c=ssl_get_cipher_by_char(s,p); 920 if (c == NULL) 921 { 922 /* unknown cipher */ 923 al=SSL_AD_ILLEGAL_PARAMETER; 924 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED); 925 goto f_err; 926 } 927 p+=ssl_put_cipher_by_char(s,NULL,NULL); 928 929 sk=ssl_get_ciphers_by_id(s); 930 i=sk_SSL_CIPHER_find(sk,c); 931 if (i < 0) 932 { 933 /* we did not say we would use this cipher */ 934 al=SSL_AD_ILLEGAL_PARAMETER; 935 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED); 936 goto f_err; 937 } 938 939 /* Depending on the session caching (internal/external), the cipher 940 and/or cipher_id values may not be set. Make sure that 941 cipher_id is set and use it for comparison. */ 942 if (s->session->cipher) 943 s->session->cipher_id = s->session->cipher->id; 944 if (s->hit && (s->session->cipher_id != c->id)) 945 { 946 /* Workaround is now obsolete */ 947 #if 0 948 if (!(s->options & 949 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) 950 #endif 951 { 952 al=SSL_AD_ILLEGAL_PARAMETER; 953 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 954 goto f_err; 955 } 956 } 957 s->s3->tmp.new_cipher=c; 958 if (!ssl3_digest_cached_records(s)) 959 goto f_err; 960 961 /* lets get the compression algorithm */ 962 /* COMPRESSION */ 963 #ifdef OPENSSL_NO_COMP 964 if (*(p++) != 0) 965 { 966 al=SSL_AD_ILLEGAL_PARAMETER; 967 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 968 goto f_err; 969 } 970 /* If compression is disabled we'd better not try to resume a session 971 * using compression. 972 */ 973 if (s->session->compress_meth != 0) 974 { 975 al=SSL_AD_INTERNAL_ERROR; 976 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_INCONSISTENT_COMPRESSION); 977 goto f_err; 978 } 979 #else 980 j= *(p++); 981 if (s->hit && j != s->session->compress_meth) 982 { 983 al=SSL_AD_ILLEGAL_PARAMETER; 984 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); 985 goto f_err; 986 } 987 if (j == 0) 988 comp=NULL; 989 else if (s->options & SSL_OP_NO_COMPRESSION) 990 { 991 al=SSL_AD_ILLEGAL_PARAMETER; 992 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_COMPRESSION_DISABLED); 993 goto f_err; 994 } 995 else 996 comp=ssl3_comp_find(s->ctx->comp_methods,j); 997 998 if ((j != 0) && (comp == NULL)) 999 { 1000 al=SSL_AD_ILLEGAL_PARAMETER; 1001 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 1002 goto f_err; 1003 } 1004 else 1005 { 1006 s->s3->tmp.new_compression=comp; 1007 } 1008 #endif 1009 1010 #ifndef OPENSSL_NO_TLSEXT 1011 /* TLS extensions*/ 1012 if (s->version >= SSL3_VERSION) 1013 { 1014 if (!ssl_parse_serverhello_tlsext(s,&p,d,n, &al)) 1015 { 1016 /* 'al' set by ssl_parse_serverhello_tlsext */ 1017 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_PARSE_TLSEXT); 1018 goto f_err; 1019 } 1020 if (ssl_check_serverhello_tlsext(s) <= 0) 1021 { 1022 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SERVERHELLO_TLSEXT); 1023 goto err; 1024 } 1025 } 1026 #endif 1027 1028 if (p != (d+n)) 1029 { 1030 /* wrong packet length */ 1031 al=SSL_AD_DECODE_ERROR; 1032 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH); 1033 goto err; 1034 } 1035 1036 return(1); 1037 f_err: 1038 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1039 err: 1040 return(-1); 1041 } 1042 1043 int ssl3_get_server_certificate(SSL *s) 1044 { 1045 int al,i,ok,ret= -1; 1046 unsigned long n,nc,llen,l; 1047 X509 *x=NULL; 1048 const unsigned char *q,*p; 1049 unsigned char *d; 1050 STACK_OF(X509) *sk=NULL; 1051 SESS_CERT *sc; 1052 EVP_PKEY *pkey=NULL; 1053 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ 1054 1055 n=s->method->ssl_get_message(s, 1056 SSL3_ST_CR_CERT_A, 1057 SSL3_ST_CR_CERT_B, 1058 -1, 1059 s->max_cert_list, 1060 &ok); 1061 1062 if (!ok) return((int)n); 1063 1064 if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) || 1065 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) && 1066 (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) 1067 { 1068 s->s3->tmp.reuse_message=1; 1069 return(1); 1070 } 1071 1072 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 1073 { 1074 al=SSL_AD_UNEXPECTED_MESSAGE; 1075 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE); 1076 goto f_err; 1077 } 1078 p=d=(unsigned char *)s->init_msg; 1079 1080 if ((sk=sk_X509_new_null()) == NULL) 1081 { 1082 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1083 goto err; 1084 } 1085 1086 n2l3(p,llen); 1087 if (llen+3 != n) 1088 { 1089 al=SSL_AD_DECODE_ERROR; 1090 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 1091 goto f_err; 1092 } 1093 for (nc=0; nc<llen; ) 1094 { 1095 n2l3(p,l); 1096 if ((l+nc+3) > llen) 1097 { 1098 al=SSL_AD_DECODE_ERROR; 1099 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1100 goto f_err; 1101 } 1102 1103 q=p; 1104 x=d2i_X509(NULL,&q,l); 1105 if (x == NULL) 1106 { 1107 al=SSL_AD_BAD_CERTIFICATE; 1108 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB); 1109 goto f_err; 1110 } 1111 if (q != (p+l)) 1112 { 1113 al=SSL_AD_DECODE_ERROR; 1114 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1115 goto f_err; 1116 } 1117 if (!sk_X509_push(sk,x)) 1118 { 1119 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1120 goto err; 1121 } 1122 x=NULL; 1123 nc+=l+3; 1124 p=q; 1125 } 1126 1127 i=ssl_verify_cert_chain(s,sk); 1128 if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0) 1129 #ifndef OPENSSL_NO_KRB5 1130 && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 1131 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 1132 #endif /* OPENSSL_NO_KRB5 */ 1133 ) 1134 { 1135 al=ssl_verify_alarm_type(s->verify_result); 1136 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); 1137 goto f_err; 1138 } 1139 ERR_clear_error(); /* but we keep s->verify_result */ 1140 1141 sc=ssl_sess_cert_new(); 1142 if (sc == NULL) goto err; 1143 1144 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); 1145 s->session->sess_cert=sc; 1146 1147 sc->cert_chain=sk; 1148 /* Inconsistency alert: cert_chain does include the peer's 1149 * certificate, which we don't include in s3_srvr.c */ 1150 x=sk_X509_value(sk,0); 1151 sk=NULL; 1152 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ 1153 1154 pkey=X509_get_pubkey(x); 1155 1156 /* VRS: allow null cert if auth == KRB5 */ 1157 need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) && 1158 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5)) 1159 ? 0 : 1; 1160 1161 #ifdef KSSL_DEBUG 1162 printf("pkey,x = %p, %p\n", pkey,x); 1163 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); 1164 printf("cipher, alg, nc = %s, %lx, %lx, %d\n", s->s3->tmp.new_cipher->name, 1165 s->s3->tmp.new_cipher->algorithm_mkey, s->s3->tmp.new_cipher->algorithm_auth, need_cert); 1166 #endif /* KSSL_DEBUG */ 1167 1168 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) 1169 { 1170 x=NULL; 1171 al=SSL3_AL_FATAL; 1172 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1173 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 1174 goto f_err; 1175 } 1176 1177 i=ssl_cert_type(x,pkey); 1178 if (need_cert && i < 0) 1179 { 1180 x=NULL; 1181 al=SSL3_AL_FATAL; 1182 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 1183 SSL_R_UNKNOWN_CERTIFICATE_TYPE); 1184 goto f_err; 1185 } 1186 1187 if (need_cert) 1188 { 1189 sc->peer_cert_type=i; 1190 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 1191 /* Why would the following ever happen? 1192 * We just created sc a couple of lines ago. */ 1193 if (sc->peer_pkeys[i].x509 != NULL) 1194 X509_free(sc->peer_pkeys[i].x509); 1195 sc->peer_pkeys[i].x509=x; 1196 sc->peer_key= &(sc->peer_pkeys[i]); 1197 1198 if (s->session->peer != NULL) 1199 X509_free(s->session->peer); 1200 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 1201 s->session->peer=x; 1202 } 1203 else 1204 { 1205 sc->peer_cert_type=i; 1206 sc->peer_key= NULL; 1207 1208 if (s->session->peer != NULL) 1209 X509_free(s->session->peer); 1210 s->session->peer=NULL; 1211 } 1212 s->session->verify_result = s->verify_result; 1213 1214 x=NULL; 1215 ret=1; 1216 1217 if (0) 1218 { 1219 f_err: 1220 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1221 } 1222 err: 1223 EVP_PKEY_free(pkey); 1224 X509_free(x); 1225 sk_X509_pop_free(sk,X509_free); 1226 return(ret); 1227 } 1228 1229 int ssl3_get_key_exchange(SSL *s) 1230 { 1231 #ifndef OPENSSL_NO_RSA 1232 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; 1233 #endif 1234 EVP_MD_CTX md_ctx; 1235 unsigned char *param,*p; 1236 int al,i,j,param_len,ok; 1237 long n,alg_k,alg_a; 1238 EVP_PKEY *pkey=NULL; 1239 #ifndef OPENSSL_NO_RSA 1240 RSA *rsa=NULL; 1241 #endif 1242 #ifndef OPENSSL_NO_DH 1243 DH *dh=NULL; 1244 #endif 1245 #ifndef OPENSSL_NO_ECDH 1246 EC_KEY *ecdh = NULL; 1247 BN_CTX *bn_ctx = NULL; 1248 EC_POINT *srvr_ecpoint = NULL; 1249 int curve_nid = 0; 1250 int encoded_pt_len = 0; 1251 #endif 1252 1253 /* use same message size as in ssl3_get_certificate_request() 1254 * as ServerKeyExchange message may be skipped */ 1255 n=s->method->ssl_get_message(s, 1256 SSL3_ST_CR_KEY_EXCH_A, 1257 SSL3_ST_CR_KEY_EXCH_B, 1258 -1, 1259 s->max_cert_list, 1260 &ok); 1261 if (!ok) return((int)n); 1262 1263 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) 1264 { 1265 #ifndef OPENSSL_NO_PSK 1266 /* In plain PSK ciphersuite, ServerKeyExchange can be 1267 omitted if no identity hint is sent. Set 1268 session->sess_cert anyway to avoid problems 1269 later.*/ 1270 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK) 1271 { 1272 s->session->sess_cert=ssl_sess_cert_new(); 1273 if (s->ctx->psk_identity_hint) 1274 OPENSSL_free(s->ctx->psk_identity_hint); 1275 s->ctx->psk_identity_hint = NULL; 1276 } 1277 #endif 1278 s->s3->tmp.reuse_message=1; 1279 return(1); 1280 } 1281 1282 param=p=(unsigned char *)s->init_msg; 1283 if (s->session->sess_cert != NULL) 1284 { 1285 #ifndef OPENSSL_NO_RSA 1286 if (s->session->sess_cert->peer_rsa_tmp != NULL) 1287 { 1288 RSA_free(s->session->sess_cert->peer_rsa_tmp); 1289 s->session->sess_cert->peer_rsa_tmp=NULL; 1290 } 1291 #endif 1292 #ifndef OPENSSL_NO_DH 1293 if (s->session->sess_cert->peer_dh_tmp) 1294 { 1295 DH_free(s->session->sess_cert->peer_dh_tmp); 1296 s->session->sess_cert->peer_dh_tmp=NULL; 1297 } 1298 #endif 1299 #ifndef OPENSSL_NO_ECDH 1300 if (s->session->sess_cert->peer_ecdh_tmp) 1301 { 1302 EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp); 1303 s->session->sess_cert->peer_ecdh_tmp=NULL; 1304 } 1305 #endif 1306 } 1307 else 1308 { 1309 s->session->sess_cert=ssl_sess_cert_new(); 1310 } 1311 1312 param_len=0; 1313 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; 1314 alg_a=s->s3->tmp.new_cipher->algorithm_auth; 1315 EVP_MD_CTX_init(&md_ctx); 1316 1317 #ifndef OPENSSL_NO_PSK 1318 if (alg_k & SSL_kPSK) 1319 { 1320 char tmp_id_hint[PSK_MAX_IDENTITY_LEN+1]; 1321 1322 al=SSL_AD_HANDSHAKE_FAILURE; 1323 n2s(p,i); 1324 param_len=i+2; 1325 /* Store PSK identity hint for later use, hint is used 1326 * in ssl3_send_client_key_exchange. Assume that the 1327 * maximum length of a PSK identity hint can be as 1328 * long as the maximum length of a PSK identity. */ 1329 if (i > PSK_MAX_IDENTITY_LEN) 1330 { 1331 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1332 SSL_R_DATA_LENGTH_TOO_LONG); 1333 goto f_err; 1334 } 1335 if (param_len > n) 1336 { 1337 al=SSL_AD_DECODE_ERROR; 1338 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, 1339 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH); 1340 goto f_err; 1341 } 1342 /* If received PSK identity hint contains NULL 1343 * characters, the hint is truncated from the first 1344 * NULL. p may not be ending with NULL, so create a 1345 * NULL-terminated string. */ 1346 memcpy(tmp_id_hint, p, i); 1347 memset(tmp_id_hint+i, 0, PSK_MAX_IDENTITY_LEN+1-i); 1348 if (s->ctx->psk_identity_hint != NULL) 1349 OPENSSL_free(s->ctx->psk_identity_hint); 1350 s->ctx->psk_identity_hint = BUF_strdup(tmp_id_hint); 1351 if (s->ctx->psk_identity_hint == NULL) 1352 { 1353 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE); 1354 goto f_err; 1355 } 1356 1357 p+=i; 1358 n-=param_len; 1359 } 1360 else 1361 #endif /* !OPENSSL_NO_PSK */ 1362 #ifndef OPENSSL_NO_RSA 1363 if (alg_k & SSL_kRSA) 1364 { 1365 if ((rsa=RSA_new()) == NULL) 1366 { 1367 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1368 goto err; 1369 } 1370 n2s(p,i); 1371 param_len=i+2; 1372 if (param_len > n) 1373 { 1374 al=SSL_AD_DECODE_ERROR; 1375 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH); 1376 goto f_err; 1377 } 1378 if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) 1379 { 1380 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1381 goto err; 1382 } 1383 p+=i; 1384 1385 n2s(p,i); 1386 param_len+=i+2; 1387 if (param_len > n) 1388 { 1389 al=SSL_AD_DECODE_ERROR; 1390 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH); 1391 goto f_err; 1392 } 1393 if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) 1394 { 1395 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1396 goto err; 1397 } 1398 p+=i; 1399 n-=param_len; 1400 1401 /* this should be because we are using an export cipher */ 1402 if (alg_a & SSL_aRSA) 1403 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1404 else 1405 { 1406 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1407 goto err; 1408 } 1409 s->session->sess_cert->peer_rsa_tmp=rsa; 1410 rsa=NULL; 1411 } 1412 #else /* OPENSSL_NO_RSA */ 1413 if (0) 1414 ; 1415 #endif 1416 #ifndef OPENSSL_NO_DH 1417 else if (alg_k & SSL_kEDH) 1418 { 1419 if ((dh=DH_new()) == NULL) 1420 { 1421 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB); 1422 goto err; 1423 } 1424 n2s(p,i); 1425 param_len=i+2; 1426 if (param_len > n) 1427 { 1428 al=SSL_AD_DECODE_ERROR; 1429 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH); 1430 goto f_err; 1431 } 1432 if (!(dh->p=BN_bin2bn(p,i,NULL))) 1433 { 1434 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1435 goto err; 1436 } 1437 p+=i; 1438 1439 n2s(p,i); 1440 param_len+=i+2; 1441 if (param_len > n) 1442 { 1443 al=SSL_AD_DECODE_ERROR; 1444 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH); 1445 goto f_err; 1446 } 1447 if (!(dh->g=BN_bin2bn(p,i,NULL))) 1448 { 1449 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1450 goto err; 1451 } 1452 p+=i; 1453 1454 n2s(p,i); 1455 param_len+=i+2; 1456 if (param_len > n) 1457 { 1458 al=SSL_AD_DECODE_ERROR; 1459 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH); 1460 goto f_err; 1461 } 1462 if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) 1463 { 1464 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1465 goto err; 1466 } 1467 p+=i; 1468 n-=param_len; 1469 1470 #ifndef OPENSSL_NO_RSA 1471 if (alg_a & SSL_aRSA) 1472 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1473 #else 1474 if (0) 1475 ; 1476 #endif 1477 #ifndef OPENSSL_NO_DSA 1478 else if (alg_a & SSL_aDSS) 1479 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); 1480 #endif 1481 /* else anonymous DH, so no certificate or pkey. */ 1482 1483 s->session->sess_cert->peer_dh_tmp=dh; 1484 dh=NULL; 1485 } 1486 else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) 1487 { 1488 al=SSL_AD_ILLEGAL_PARAMETER; 1489 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 1490 goto f_err; 1491 } 1492 #endif /* !OPENSSL_NO_DH */ 1493 1494 #ifndef OPENSSL_NO_ECDH 1495 else if (alg_k & SSL_kEECDH) 1496 { 1497 EC_GROUP *ngroup; 1498 const EC_GROUP *group; 1499 1500 if ((ecdh=EC_KEY_new()) == NULL) 1501 { 1502 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1503 goto err; 1504 } 1505 1506 /* Extract elliptic curve parameters and the 1507 * server's ephemeral ECDH public key. 1508 * Keep accumulating lengths of various components in 1509 * param_len and make sure it never exceeds n. 1510 */ 1511 1512 /* XXX: For now we only support named (not generic) curves 1513 * and the ECParameters in this case is just three bytes. 1514 */ 1515 param_len=3; 1516 if ((param_len > n) || 1517 (*p != NAMED_CURVE_TYPE) || 1518 ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) 1519 { 1520 al=SSL_AD_INTERNAL_ERROR; 1521 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); 1522 goto f_err; 1523 } 1524 1525 ngroup = EC_GROUP_new_by_curve_name(curve_nid); 1526 if (ngroup == NULL) 1527 { 1528 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); 1529 goto err; 1530 } 1531 if (EC_KEY_set_group(ecdh, ngroup) == 0) 1532 { 1533 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_EC_LIB); 1534 goto err; 1535 } 1536 EC_GROUP_free(ngroup); 1537 1538 group = EC_KEY_get0_group(ecdh); 1539 1540 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 1541 (EC_GROUP_get_degree(group) > 163)) 1542 { 1543 al=SSL_AD_EXPORT_RESTRICTION; 1544 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 1545 goto f_err; 1546 } 1547 1548 p+=3; 1549 1550 /* Next, get the encoded ECPoint */ 1551 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) || 1552 ((bn_ctx = BN_CTX_new()) == NULL)) 1553 { 1554 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1555 goto err; 1556 } 1557 1558 encoded_pt_len = *p; /* length of encoded point */ 1559 p+=1; 1560 param_len += (1 + encoded_pt_len); 1561 if ((param_len > n) || 1562 (EC_POINT_oct2point(group, srvr_ecpoint, 1563 p, encoded_pt_len, bn_ctx) == 0)) 1564 { 1565 al=SSL_AD_DECODE_ERROR; 1566 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_ECPOINT); 1567 goto f_err; 1568 } 1569 1570 n-=param_len; 1571 p+=encoded_pt_len; 1572 1573 /* The ECC/TLS specification does not mention 1574 * the use of DSA to sign ECParameters in the server 1575 * key exchange message. We do support RSA and ECDSA. 1576 */ 1577 if (0) ; 1578 #ifndef OPENSSL_NO_RSA 1579 else if (alg_a & SSL_aRSA) 1580 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1581 #endif 1582 #ifndef OPENSSL_NO_ECDSA 1583 else if (alg_a & SSL_aECDSA) 1584 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 1585 #endif 1586 /* else anonymous ECDH, so no certificate or pkey. */ 1587 EC_KEY_set_public_key(ecdh, srvr_ecpoint); 1588 s->session->sess_cert->peer_ecdh_tmp=ecdh; 1589 ecdh=NULL; 1590 BN_CTX_free(bn_ctx); 1591 bn_ctx = NULL; 1592 EC_POINT_free(srvr_ecpoint); 1593 srvr_ecpoint = NULL; 1594 } 1595 else if (alg_k) 1596 { 1597 al=SSL_AD_UNEXPECTED_MESSAGE; 1598 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE); 1599 goto f_err; 1600 } 1601 #endif /* !OPENSSL_NO_ECDH */ 1602 1603 1604 /* p points to the next byte, there are 'n' bytes left */ 1605 1606 /* if it was signed, check the signature */ 1607 if (pkey != NULL) 1608 { 1609 n2s(p,i); 1610 n-=2; 1611 j=EVP_PKEY_size(pkey); 1612 1613 if ((i != n) || (n > j) || (n <= 0)) 1614 { 1615 /* wrong packet length */ 1616 al=SSL_AD_DECODE_ERROR; 1617 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH); 1618 goto f_err; 1619 } 1620 1621 #ifndef OPENSSL_NO_RSA 1622 if (pkey->type == EVP_PKEY_RSA) 1623 { 1624 int num; 1625 1626 j=0; 1627 q=md_buf; 1628 for (num=2; num > 0; num--) 1629 { 1630 EVP_DigestInit_ex(&md_ctx,(num == 2) 1631 ?s->ctx->md5:s->ctx->sha1, NULL); 1632 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1633 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1634 EVP_DigestUpdate(&md_ctx,param,param_len); 1635 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i); 1636 q+=i; 1637 j+=i; 1638 } 1639 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, 1640 pkey->pkey.rsa); 1641 if (i < 0) 1642 { 1643 al=SSL_AD_DECRYPT_ERROR; 1644 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); 1645 goto f_err; 1646 } 1647 if (i == 0) 1648 { 1649 /* bad signature */ 1650 al=SSL_AD_DECRYPT_ERROR; 1651 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); 1652 goto f_err; 1653 } 1654 } 1655 else 1656 #endif 1657 #ifndef OPENSSL_NO_DSA 1658 if (pkey->type == EVP_PKEY_DSA) 1659 { 1660 /* lets do DSS */ 1661 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL); 1662 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1663 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1664 EVP_VerifyUpdate(&md_ctx,param,param_len); 1665 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0) 1666 { 1667 /* bad signature */ 1668 al=SSL_AD_DECRYPT_ERROR; 1669 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); 1670 goto f_err; 1671 } 1672 } 1673 else 1674 #endif 1675 #ifndef OPENSSL_NO_ECDSA 1676 if (pkey->type == EVP_PKEY_EC) 1677 { 1678 /* let's do ECDSA */ 1679 EVP_VerifyInit_ex(&md_ctx,EVP_ecdsa(), NULL); 1680 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1681 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1682 EVP_VerifyUpdate(&md_ctx,param,param_len); 1683 if (EVP_VerifyFinal(&md_ctx,p,(int)n,pkey) <= 0) 1684 { 1685 /* bad signature */ 1686 al=SSL_AD_DECRYPT_ERROR; 1687 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); 1688 goto f_err; 1689 } 1690 } 1691 else 1692 #endif 1693 { 1694 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1695 goto err; 1696 } 1697 } 1698 else 1699 { 1700 if (!(alg_a & SSL_aNULL) && !(alg_k & SSL_kPSK)) 1701 /* aNULL or kPSK do not need public keys */ 1702 { 1703 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1704 goto err; 1705 } 1706 /* still data left over */ 1707 if (n != 0) 1708 { 1709 al=SSL_AD_DECODE_ERROR; 1710 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE); 1711 goto f_err; 1712 } 1713 } 1714 EVP_PKEY_free(pkey); 1715 EVP_MD_CTX_cleanup(&md_ctx); 1716 return(1); 1717 f_err: 1718 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1719 err: 1720 EVP_PKEY_free(pkey); 1721 #ifndef OPENSSL_NO_RSA 1722 if (rsa != NULL) 1723 RSA_free(rsa); 1724 #endif 1725 #ifndef OPENSSL_NO_DH 1726 if (dh != NULL) 1727 DH_free(dh); 1728 #endif 1729 #ifndef OPENSSL_NO_ECDH 1730 BN_CTX_free(bn_ctx); 1731 EC_POINT_free(srvr_ecpoint); 1732 if (ecdh != NULL) 1733 EC_KEY_free(ecdh); 1734 #endif 1735 EVP_MD_CTX_cleanup(&md_ctx); 1736 return(-1); 1737 } 1738 1739 int ssl3_get_certificate_request(SSL *s) 1740 { 1741 int ok,ret=0; 1742 unsigned long n,nc,l; 1743 unsigned int llen,ctype_num,i; 1744 X509_NAME *xn=NULL; 1745 const unsigned char *p,*q; 1746 unsigned char *d; 1747 STACK_OF(X509_NAME) *ca_sk=NULL; 1748 1749 n=s->method->ssl_get_message(s, 1750 SSL3_ST_CR_CERT_REQ_A, 1751 SSL3_ST_CR_CERT_REQ_B, 1752 -1, 1753 s->max_cert_list, 1754 &ok); 1755 1756 if (!ok) return((int)n); 1757 1758 s->s3->tmp.cert_req=0; 1759 1760 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) 1761 { 1762 s->s3->tmp.reuse_message=1; 1763 return(1); 1764 } 1765 1766 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) 1767 { 1768 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 1769 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE); 1770 goto err; 1771 } 1772 1773 /* TLS does not like anon-DH with client cert */ 1774 if (s->version > SSL3_VERSION) 1775 { 1776 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) 1777 { 1778 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 1779 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 1780 goto err; 1781 } 1782 } 1783 1784 p=d=(unsigned char *)s->init_msg; 1785 1786 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL) 1787 { 1788 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1789 goto err; 1790 } 1791 1792 /* get the certificate types */ 1793 ctype_num= *(p++); 1794 if (ctype_num > SSL3_CT_NUMBER) 1795 ctype_num=SSL3_CT_NUMBER; 1796 for (i=0; i<ctype_num; i++) 1797 s->s3->tmp.ctype[i]= p[i]; 1798 p+=ctype_num; 1799 1800 /* get the CA RDNs */ 1801 n2s(p,llen); 1802 #if 0 1803 { 1804 FILE *out; 1805 out=fopen("/tmp/vsign.der","w"); 1806 fwrite(p,1,llen,out); 1807 fclose(out); 1808 } 1809 #endif 1810 1811 if ((llen+ctype_num+2+1) != n) 1812 { 1813 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1814 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH); 1815 goto err; 1816 } 1817 1818 for (nc=0; nc<llen; ) 1819 { 1820 n2s(p,l); 1821 if ((l+nc+2) > llen) 1822 { 1823 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1824 goto cont; /* netscape bugs */ 1825 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1826 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG); 1827 goto err; 1828 } 1829 1830 q=p; 1831 1832 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) 1833 { 1834 /* If netscape tolerance is on, ignore errors */ 1835 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) 1836 goto cont; 1837 else 1838 { 1839 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1840 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB); 1841 goto err; 1842 } 1843 } 1844 1845 if (q != (p+l)) 1846 { 1847 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1848 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH); 1849 goto err; 1850 } 1851 if (!sk_X509_NAME_push(ca_sk,xn)) 1852 { 1853 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1854 goto err; 1855 } 1856 1857 p+=l; 1858 nc+=l+2; 1859 } 1860 1861 if (0) 1862 { 1863 cont: 1864 ERR_clear_error(); 1865 } 1866 1867 /* we should setup a certificate to return.... */ 1868 s->s3->tmp.cert_req=1; 1869 s->s3->tmp.ctype_num=ctype_num; 1870 if (s->s3->tmp.ca_names != NULL) 1871 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); 1872 s->s3->tmp.ca_names=ca_sk; 1873 ca_sk=NULL; 1874 1875 ret=1; 1876 err: 1877 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); 1878 return(ret); 1879 } 1880 1881 static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 1882 { 1883 return(X509_NAME_cmp(*a,*b)); 1884 } 1885 #ifndef OPENSSL_NO_TLSEXT 1886 int ssl3_get_new_session_ticket(SSL *s) 1887 { 1888 int ok,al,ret=0, ticklen; 1889 long n; 1890 const unsigned char *p; 1891 unsigned char *d; 1892 1893 n=s->method->ssl_get_message(s, 1894 SSL3_ST_CR_SESSION_TICKET_A, 1895 SSL3_ST_CR_SESSION_TICKET_B, 1896 -1, 1897 16384, 1898 &ok); 1899 1900 if (!ok) 1901 return((int)n); 1902 1903 if (s->s3->tmp.message_type == SSL3_MT_FINISHED) 1904 { 1905 s->s3->tmp.reuse_message=1; 1906 return(1); 1907 } 1908 if (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET) 1909 { 1910 al=SSL_AD_UNEXPECTED_MESSAGE; 1911 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_BAD_MESSAGE_TYPE); 1912 goto f_err; 1913 } 1914 if (n < 6) 1915 { 1916 /* need at least ticket_lifetime_hint + ticket length */ 1917 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR; 1918 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH); 1919 goto f_err; 1920 } 1921 1922 p=d=(unsigned char *)s->init_msg; 1923 n2l(p, s->session->tlsext_tick_lifetime_hint); 1924 n2s(p, ticklen); 1925 /* ticket_lifetime_hint + ticket_length + ticket */ 1926 if (ticklen + 6 != n) 1927 { 1928 al = SSL3_AL_FATAL,SSL_AD_DECODE_ERROR; 1929 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,SSL_R_LENGTH_MISMATCH); 1930 goto f_err; 1931 } 1932 if (s->session->tlsext_tick) 1933 { 1934 OPENSSL_free(s->session->tlsext_tick); 1935 s->session->tlsext_ticklen = 0; 1936 } 1937 s->session->tlsext_tick = OPENSSL_malloc(ticklen); 1938 if (!s->session->tlsext_tick) 1939 { 1940 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET,ERR_R_MALLOC_FAILURE); 1941 goto err; 1942 } 1943 memcpy(s->session->tlsext_tick, p, ticklen); 1944 s->session->tlsext_ticklen = ticklen; 1945 /* There are two ways to detect a resumed ticket sesion. 1946 * One is to set an appropriate session ID and then the server 1947 * must return a match in ServerHello. This allows the normal 1948 * client session ID matching to work and we know much 1949 * earlier that the ticket has been accepted. 1950 * 1951 * The other way is to set zero length session ID when the 1952 * ticket is presented and rely on the handshake to determine 1953 * session resumption. 1954 * 1955 * We choose the former approach because this fits in with 1956 * assumptions elsewhere in OpenSSL. The session ID is set 1957 * to the SHA256 (or SHA1 is SHA256 is disabled) hash of the 1958 * ticket. 1959 */ 1960 EVP_Digest(p, ticklen, 1961 s->session->session_id, &s->session->session_id_length, 1962 #ifndef OPENSSL_NO_SHA256 1963 EVP_sha256(), NULL); 1964 #else 1965 EVP_sha1(), NULL); 1966 #endif 1967 ret=1; 1968 return(ret); 1969 f_err: 1970 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1971 err: 1972 return(-1); 1973 } 1974 1975 int ssl3_get_cert_status(SSL *s) 1976 { 1977 int ok, al; 1978 unsigned long resplen,n; 1979 const unsigned char *p; 1980 1981 n=s->method->ssl_get_message(s, 1982 SSL3_ST_CR_CERT_STATUS_A, 1983 SSL3_ST_CR_CERT_STATUS_B, 1984 SSL3_MT_CERTIFICATE_STATUS, 1985 16384, 1986 &ok); 1987 1988 if (!ok) return((int)n); 1989 if (n < 4) 1990 { 1991 /* need at least status type + length */ 1992 al = SSL_AD_DECODE_ERROR; 1993 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH); 1994 goto f_err; 1995 } 1996 p = (unsigned char *)s->init_msg; 1997 if (*p++ != TLSEXT_STATUSTYPE_ocsp) 1998 { 1999 al = SSL_AD_DECODE_ERROR; 2000 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_UNSUPPORTED_STATUS_TYPE); 2001 goto f_err; 2002 } 2003 n2l3(p, resplen); 2004 if (resplen + 4 != n) 2005 { 2006 al = SSL_AD_DECODE_ERROR; 2007 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_LENGTH_MISMATCH); 2008 goto f_err; 2009 } 2010 if (s->tlsext_ocsp_resp) 2011 OPENSSL_free(s->tlsext_ocsp_resp); 2012 s->tlsext_ocsp_resp = BUF_memdup(p, resplen); 2013 if (!s->tlsext_ocsp_resp) 2014 { 2015 al = SSL_AD_INTERNAL_ERROR; 2016 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE); 2017 goto f_err; 2018 } 2019 s->tlsext_ocsp_resplen = resplen; 2020 if (s->ctx->tlsext_status_cb) 2021 { 2022 int ret; 2023 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg); 2024 if (ret == 0) 2025 { 2026 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE; 2027 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,SSL_R_INVALID_STATUS_RESPONSE); 2028 goto f_err; 2029 } 2030 if (ret < 0) 2031 { 2032 al = SSL_AD_INTERNAL_ERROR; 2033 SSLerr(SSL_F_SSL3_GET_CERT_STATUS,ERR_R_MALLOC_FAILURE); 2034 goto f_err; 2035 } 2036 } 2037 return 1; 2038 f_err: 2039 ssl3_send_alert(s,SSL3_AL_FATAL,al); 2040 return(-1); 2041 } 2042 #endif 2043 2044 int ssl3_get_server_done(SSL *s) 2045 { 2046 int ok,ret=0; 2047 long n; 2048 2049 n=s->method->ssl_get_message(s, 2050 SSL3_ST_CR_SRVR_DONE_A, 2051 SSL3_ST_CR_SRVR_DONE_B, 2052 SSL3_MT_SERVER_DONE, 2053 30, /* should be very small, like 0 :-) */ 2054 &ok); 2055 2056 if (!ok) return((int)n); 2057 if (n > 0) 2058 { 2059 /* should contain no data */ 2060 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 2061 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); 2062 return -1; 2063 } 2064 ret=1; 2065 return(ret); 2066 } 2067 2068 2069 int ssl3_send_client_key_exchange(SSL *s) 2070 { 2071 unsigned char *p,*d; 2072 int n; 2073 unsigned long alg_k; 2074 #ifndef OPENSSL_NO_RSA 2075 unsigned char *q; 2076 EVP_PKEY *pkey=NULL; 2077 #endif 2078 #ifndef OPENSSL_NO_KRB5 2079 KSSL_ERR kssl_err; 2080 #endif /* OPENSSL_NO_KRB5 */ 2081 #ifndef OPENSSL_NO_ECDH 2082 EC_KEY *clnt_ecdh = NULL; 2083 const EC_POINT *srvr_ecpoint = NULL; 2084 EVP_PKEY *srvr_pub_pkey = NULL; 2085 unsigned char *encodedPoint = NULL; 2086 int encoded_pt_len = 0; 2087 BN_CTX * bn_ctx = NULL; 2088 #endif 2089 2090 if (s->state == SSL3_ST_CW_KEY_EXCH_A) 2091 { 2092 d=(unsigned char *)s->init_buf->data; 2093 p= &(d[4]); 2094 2095 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; 2096 2097 /* Fool emacs indentation */ 2098 if (0) {} 2099 #ifndef OPENSSL_NO_RSA 2100 else if (alg_k & SSL_kRSA) 2101 { 2102 RSA *rsa; 2103 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2104 2105 if (s->session->sess_cert->peer_rsa_tmp != NULL) 2106 rsa=s->session->sess_cert->peer_rsa_tmp; 2107 else 2108 { 2109 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 2110 if ((pkey == NULL) || 2111 (pkey->type != EVP_PKEY_RSA) || 2112 (pkey->pkey.rsa == NULL)) 2113 { 2114 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 2115 goto err; 2116 } 2117 rsa=pkey->pkey.rsa; 2118 EVP_PKEY_free(pkey); 2119 } 2120 2121 tmp_buf[0]=s->client_version>>8; 2122 tmp_buf[1]=s->client_version&0xff; 2123 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) 2124 goto err; 2125 2126 s->session->master_key_length=sizeof tmp_buf; 2127 2128 q=p; 2129 /* Fix buf for TLS and beyond */ 2130 if (s->version > SSL3_VERSION) 2131 p+=2; 2132 n=RSA_public_encrypt(sizeof tmp_buf, 2133 tmp_buf,p,rsa,RSA_PKCS1_PADDING); 2134 #ifdef PKCS1_CHECK 2135 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; 2136 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; 2137 #endif 2138 if (n <= 0) 2139 { 2140 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); 2141 goto err; 2142 } 2143 2144 /* Fix buf for TLS and beyond */ 2145 if (s->version > SSL3_VERSION) 2146 { 2147 s2n(n,q); 2148 n+=2; 2149 } 2150 2151 s->session->master_key_length= 2152 s->method->ssl3_enc->generate_master_secret(s, 2153 s->session->master_key, 2154 tmp_buf,sizeof tmp_buf); 2155 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); 2156 } 2157 #endif 2158 #ifndef OPENSSL_NO_KRB5 2159 else if (alg_k & SSL_kKRB5) 2160 { 2161 krb5_error_code krb5rc; 2162 KSSL_CTX *kssl_ctx = s->kssl_ctx; 2163 /* krb5_data krb5_ap_req; */ 2164 krb5_data *enc_ticket; 2165 krb5_data authenticator, *authp = NULL; 2166 EVP_CIPHER_CTX ciph_ctx; 2167 const EVP_CIPHER *enc = NULL; 2168 unsigned char iv[EVP_MAX_IV_LENGTH]; 2169 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 2170 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH 2171 + EVP_MAX_IV_LENGTH]; 2172 int padl, outl = sizeof(epms); 2173 2174 EVP_CIPHER_CTX_init(&ciph_ctx); 2175 2176 #ifdef KSSL_DEBUG 2177 printf("ssl3_send_client_key_exchange(%lx & %lx)\n", 2178 alg_k, SSL_kKRB5); 2179 #endif /* KSSL_DEBUG */ 2180 2181 authp = NULL; 2182 #ifdef KRB5SENDAUTH 2183 if (KRB5SENDAUTH) authp = &authenticator; 2184 #endif /* KRB5SENDAUTH */ 2185 2186 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, 2187 &kssl_err); 2188 enc = kssl_map_enc(kssl_ctx->enctype); 2189 if (enc == NULL) 2190 goto err; 2191 #ifdef KSSL_DEBUG 2192 { 2193 printf("kssl_cget_tkt rtn %d\n", krb5rc); 2194 if (krb5rc && kssl_err.text) 2195 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); 2196 } 2197 #endif /* KSSL_DEBUG */ 2198 2199 if (krb5rc) 2200 { 2201 ssl3_send_alert(s,SSL3_AL_FATAL, 2202 SSL_AD_HANDSHAKE_FAILURE); 2203 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2204 kssl_err.reason); 2205 goto err; 2206 } 2207 2208 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ 2209 ** in place of RFC 2712 KerberosWrapper, as in: 2210 ** 2211 ** Send ticket (copy to *p, set n = length) 2212 ** n = krb5_ap_req.length; 2213 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); 2214 ** if (krb5_ap_req.data) 2215 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); 2216 ** 2217 ** Now using real RFC 2712 KerberosWrapper 2218 ** (Thanks to Simon Wilkinson <sxw (at) sxw.org.uk>) 2219 ** Note: 2712 "opaque" types are here replaced 2220 ** with a 2-byte length followed by the value. 2221 ** Example: 2222 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms 2223 ** Where "xx xx" = length bytes. Shown here with 2224 ** optional authenticator omitted. 2225 */ 2226 2227 /* KerberosWrapper.Ticket */ 2228 s2n(enc_ticket->length,p); 2229 memcpy(p, enc_ticket->data, enc_ticket->length); 2230 p+= enc_ticket->length; 2231 n = enc_ticket->length + 2; 2232 2233 /* KerberosWrapper.Authenticator */ 2234 if (authp && authp->length) 2235 { 2236 s2n(authp->length,p); 2237 memcpy(p, authp->data, authp->length); 2238 p+= authp->length; 2239 n+= authp->length + 2; 2240 2241 free(authp->data); 2242 authp->data = NULL; 2243 authp->length = 0; 2244 } 2245 else 2246 { 2247 s2n(0,p);/* null authenticator length */ 2248 n+=2; 2249 } 2250 2251 tmp_buf[0]=s->client_version>>8; 2252 tmp_buf[1]=s->client_version&0xff; 2253 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) 2254 goto err; 2255 2256 /* 20010420 VRS. Tried it this way; failed. 2257 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); 2258 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, 2259 ** kssl_ctx->length); 2260 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); 2261 */ 2262 2263 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 2264 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, 2265 kssl_ctx->key,iv); 2266 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, 2267 sizeof tmp_buf); 2268 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); 2269 outl += padl; 2270 if (outl > (int)sizeof epms) 2271 { 2272 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 2273 goto err; 2274 } 2275 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 2276 2277 /* KerberosWrapper.EncryptedPreMasterSecret */ 2278 s2n(outl,p); 2279 memcpy(p, epms, outl); 2280 p+=outl; 2281 n+=outl + 2; 2282 2283 s->session->master_key_length= 2284 s->method->ssl3_enc->generate_master_secret(s, 2285 s->session->master_key, 2286 tmp_buf, sizeof tmp_buf); 2287 2288 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 2289 OPENSSL_cleanse(epms, outl); 2290 } 2291 #endif 2292 #ifndef OPENSSL_NO_DH 2293 else if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 2294 { 2295 DH *dh_srvr,*dh_clnt; 2296 2297 if (s->session->sess_cert == NULL) 2298 { 2299 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 2300 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNEXPECTED_MESSAGE); 2301 goto err; 2302 } 2303 2304 if (s->session->sess_cert->peer_dh_tmp != NULL) 2305 dh_srvr=s->session->sess_cert->peer_dh_tmp; 2306 else 2307 { 2308 /* we get them from the cert */ 2309 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 2310 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 2311 goto err; 2312 } 2313 2314 /* generate a new random key */ 2315 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) 2316 { 2317 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 2318 goto err; 2319 } 2320 if (!DH_generate_key(dh_clnt)) 2321 { 2322 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 2323 DH_free(dh_clnt); 2324 goto err; 2325 } 2326 2327 /* use the 'p' output buffer for the DH key, but 2328 * make sure to clear it out afterwards */ 2329 2330 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); 2331 2332 if (n <= 0) 2333 { 2334 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 2335 DH_free(dh_clnt); 2336 goto err; 2337 } 2338 2339 /* generate master key from the result */ 2340 s->session->master_key_length= 2341 s->method->ssl3_enc->generate_master_secret(s, 2342 s->session->master_key,p,n); 2343 /* clean up */ 2344 memset(p,0,n); 2345 2346 /* send off the data */ 2347 n=BN_num_bytes(dh_clnt->pub_key); 2348 s2n(n,p); 2349 BN_bn2bin(dh_clnt->pub_key,p); 2350 n+=2; 2351 2352 DH_free(dh_clnt); 2353 2354 /* perhaps clean things up a bit EAY EAY EAY EAY*/ 2355 } 2356 #endif 2357 2358 #ifndef OPENSSL_NO_ECDH 2359 else if (alg_k & (SSL_kEECDH|SSL_kECDHr|SSL_kECDHe)) 2360 { 2361 const EC_GROUP *srvr_group = NULL; 2362 EC_KEY *tkey; 2363 int ecdh_clnt_cert = 0; 2364 int field_size = 0; 2365 2366 /* Did we send out the client's 2367 * ECDH share for use in premaster 2368 * computation as part of client certificate? 2369 * If so, set ecdh_clnt_cert to 1. 2370 */ 2371 if ((alg_k & (SSL_kECDHr|SSL_kECDHe)) && (s->cert != NULL)) 2372 { 2373 /* XXX: For now, we do not support client 2374 * authentication using ECDH certificates. 2375 * To add such support, one needs to add 2376 * code that checks for appropriate 2377 * conditions and sets ecdh_clnt_cert to 1. 2378 * For example, the cert have an ECC 2379 * key on the same curve as the server's 2380 * and the key should be authorized for 2381 * key agreement. 2382 * 2383 * One also needs to add code in ssl3_connect 2384 * to skip sending the certificate verify 2385 * message. 2386 * 2387 * if ((s->cert->key->privatekey != NULL) && 2388 * (s->cert->key->privatekey->type == 2389 * EVP_PKEY_EC) && ...) 2390 * ecdh_clnt_cert = 1; 2391 */ 2392 } 2393 2394 if (s->session->sess_cert->peer_ecdh_tmp != NULL) 2395 { 2396 tkey = s->session->sess_cert->peer_ecdh_tmp; 2397 } 2398 else 2399 { 2400 /* Get the Server Public Key from Cert */ 2401 srvr_pub_pkey = X509_get_pubkey(s->session-> \ 2402 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509); 2403 if ((srvr_pub_pkey == NULL) || 2404 (srvr_pub_pkey->type != EVP_PKEY_EC) || 2405 (srvr_pub_pkey->pkey.ec == NULL)) 2406 { 2407 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2408 ERR_R_INTERNAL_ERROR); 2409 goto err; 2410 } 2411 2412 tkey = srvr_pub_pkey->pkey.ec; 2413 } 2414 2415 srvr_group = EC_KEY_get0_group(tkey); 2416 srvr_ecpoint = EC_KEY_get0_public_key(tkey); 2417 2418 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) 2419 { 2420 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2421 ERR_R_INTERNAL_ERROR); 2422 goto err; 2423 } 2424 2425 if ((clnt_ecdh=EC_KEY_new()) == NULL) 2426 { 2427 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 2428 goto err; 2429 } 2430 2431 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) 2432 { 2433 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); 2434 goto err; 2435 } 2436 if (ecdh_clnt_cert) 2437 { 2438 /* Reuse key info from our certificate 2439 * We only need our private key to perform 2440 * the ECDH computation. 2441 */ 2442 const BIGNUM *priv_key; 2443 tkey = s->cert->key->privatekey->pkey.ec; 2444 priv_key = EC_KEY_get0_private_key(tkey); 2445 if (priv_key == NULL) 2446 { 2447 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 2448 goto err; 2449 } 2450 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) 2451 { 2452 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_EC_LIB); 2453 goto err; 2454 } 2455 } 2456 else 2457 { 2458 /* Generate a new ECDH key pair */ 2459 if (!(EC_KEY_generate_key(clnt_ecdh))) 2460 { 2461 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB); 2462 goto err; 2463 } 2464 } 2465 2466 /* use the 'p' output buffer for the ECDH key, but 2467 * make sure to clear it out afterwards 2468 */ 2469 2470 field_size = EC_GROUP_get_degree(srvr_group); 2471 if (field_size <= 0) 2472 { 2473 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2474 ERR_R_ECDH_LIB); 2475 goto err; 2476 } 2477 n=ECDH_compute_key(p, (field_size+7)/8, srvr_ecpoint, clnt_ecdh, NULL); 2478 if (n <= 0) 2479 { 2480 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2481 ERR_R_ECDH_LIB); 2482 goto err; 2483 } 2484 2485 /* generate master key from the result */ 2486 s->session->master_key_length = s->method->ssl3_enc \ 2487 -> generate_master_secret(s, 2488 s->session->master_key, 2489 p, n); 2490 2491 memset(p, 0, n); /* clean up */ 2492 2493 if (ecdh_clnt_cert) 2494 { 2495 /* Send empty client key exch message */ 2496 n = 0; 2497 } 2498 else 2499 { 2500 /* First check the size of encoding and 2501 * allocate memory accordingly. 2502 */ 2503 encoded_pt_len = 2504 EC_POINT_point2oct(srvr_group, 2505 EC_KEY_get0_public_key(clnt_ecdh), 2506 POINT_CONVERSION_UNCOMPRESSED, 2507 NULL, 0, NULL); 2508 2509 encodedPoint = (unsigned char *) 2510 OPENSSL_malloc(encoded_pt_len * 2511 sizeof(unsigned char)); 2512 bn_ctx = BN_CTX_new(); 2513 if ((encodedPoint == NULL) || 2514 (bn_ctx == NULL)) 2515 { 2516 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 2517 goto err; 2518 } 2519 2520 /* Encode the public key */ 2521 n = EC_POINT_point2oct(srvr_group, 2522 EC_KEY_get0_public_key(clnt_ecdh), 2523 POINT_CONVERSION_UNCOMPRESSED, 2524 encodedPoint, encoded_pt_len, bn_ctx); 2525 2526 *p = n; /* length of encoded point */ 2527 /* Encoded point will be copied here */ 2528 p += 1; 2529 /* copy the point */ 2530 memcpy((unsigned char *)p, encodedPoint, n); 2531 /* increment n to account for length field */ 2532 n += 1; 2533 } 2534 2535 /* Free allocated memory */ 2536 BN_CTX_free(bn_ctx); 2537 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); 2538 if (clnt_ecdh != NULL) 2539 EC_KEY_free(clnt_ecdh); 2540 EVP_PKEY_free(srvr_pub_pkey); 2541 } 2542 #endif /* !OPENSSL_NO_ECDH */ 2543 else if (alg_k & SSL_kGOST) 2544 { 2545 /* GOST key exchange message creation */ 2546 EVP_PKEY_CTX *pkey_ctx; 2547 X509 *peer_cert; 2548 size_t msglen; 2549 unsigned int md_len; 2550 int keytype; 2551 unsigned char premaster_secret[32],shared_ukm[32], tmp[256]; 2552 EVP_MD_CTX *ukm_hash; 2553 EVP_PKEY *pub_key; 2554 2555 /* Get server sertificate PKEY and create ctx from it */ 2556 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST01)].x509; 2557 if (!peer_cert) 2558 peer_cert=s->session->sess_cert->peer_pkeys[(keytype=SSL_PKEY_GOST94)].x509; 2559 if (!peer_cert) { 2560 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); 2561 goto err; 2562 } 2563 2564 pkey_ctx=EVP_PKEY_CTX_new(pub_key=X509_get_pubkey(peer_cert),NULL); 2565 /* If we have send a certificate, and certificate key 2566 2567 * parameters match those of server certificate, use 2568 * certificate key for key exchange 2569 */ 2570 2571 /* Otherwise, generate ephemeral key pair */ 2572 2573 EVP_PKEY_encrypt_init(pkey_ctx); 2574 /* Generate session key */ 2575 RAND_bytes(premaster_secret,32); 2576 /* If we have client certificate, use its secret as peer key */ 2577 if (s->s3->tmp.cert_req && s->cert->key->privatekey) { 2578 if (EVP_PKEY_derive_set_peer(pkey_ctx,s->cert->key->privatekey) <=0) { 2579 /* If there was an error - just ignore it. Ephemeral key 2580 * would be used 2581 */ 2582 ERR_clear_error(); 2583 } 2584 } 2585 /* Compute shared IV and store it in algorithm-specific 2586 * context data */ 2587 ukm_hash = EVP_MD_CTX_create(); 2588 EVP_DigestInit(ukm_hash,EVP_get_digestbynid(NID_id_GostR3411_94)); 2589 EVP_DigestUpdate(ukm_hash,s->s3->client_random,SSL3_RANDOM_SIZE); 2590 EVP_DigestUpdate(ukm_hash,s->s3->server_random,SSL3_RANDOM_SIZE); 2591 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len); 2592 EVP_MD_CTX_destroy(ukm_hash); 2593 if (EVP_PKEY_CTX_ctrl(pkey_ctx,-1,EVP_PKEY_OP_ENCRYPT,EVP_PKEY_CTRL_SET_IV, 2594 8,shared_ukm)<0) { 2595 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2596 SSL_R_LIBRARY_BUG); 2597 goto err; 2598 } 2599 /* Make GOST keytransport blob message */ 2600 /*Encapsulate it into sequence */ 2601 *(p++)=V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED; 2602 msglen=255; 2603 if (EVP_PKEY_encrypt(pkey_ctx,tmp,&msglen,premaster_secret,32)<0) { 2604 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2605 SSL_R_LIBRARY_BUG); 2606 goto err; 2607 } 2608 if (msglen >= 0x80) 2609 { 2610 *(p++)=0x81; 2611 *(p++)= msglen & 0xff; 2612 n=msglen+3; 2613 } 2614 else 2615 { 2616 *(p++)= msglen & 0xff; 2617 n=msglen+2; 2618 } 2619 memcpy(p, tmp, msglen); 2620 /* Check if pubkey from client certificate was used */ 2621 if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) 2622 { 2623 /* Set flag "skip certificate verify" */ 2624 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY; 2625 } 2626 EVP_PKEY_CTX_free(pkey_ctx); 2627 s->session->master_key_length= 2628 s->method->ssl3_enc->generate_master_secret(s, 2629 s->session->master_key,premaster_secret,32); 2630 EVP_PKEY_free(pub_key); 2631 2632 } 2633 #ifndef OPENSSL_NO_PSK 2634 else if (alg_k & SSL_kPSK) 2635 { 2636 char identity[PSK_MAX_IDENTITY_LEN]; 2637 unsigned char *t = NULL; 2638 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN*2+4]; 2639 unsigned int pre_ms_len = 0, psk_len = 0; 2640 int psk_err = 1; 2641 2642 n = 0; 2643 if (s->psk_client_callback == NULL) 2644 { 2645 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2646 SSL_R_PSK_NO_CLIENT_CB); 2647 goto err; 2648 } 2649 2650 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint, 2651 identity, PSK_MAX_IDENTITY_LEN, 2652 psk_or_pre_ms, sizeof(psk_or_pre_ms)); 2653 if (psk_len > PSK_MAX_PSK_LEN) 2654 { 2655 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2656 ERR_R_INTERNAL_ERROR); 2657 goto psk_err; 2658 } 2659 else if (psk_len == 0) 2660 { 2661 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2662 SSL_R_PSK_IDENTITY_NOT_FOUND); 2663 goto psk_err; 2664 } 2665 2666 /* create PSK pre_master_secret */ 2667 pre_ms_len = 2+psk_len+2+psk_len; 2668 t = psk_or_pre_ms; 2669 memmove(psk_or_pre_ms+psk_len+4, psk_or_pre_ms, psk_len); 2670 s2n(psk_len, t); 2671 memset(t, 0, psk_len); 2672 t+=psk_len; 2673 s2n(psk_len, t); 2674 2675 if (s->session->psk_identity_hint != NULL) 2676 OPENSSL_free(s->session->psk_identity_hint); 2677 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint); 2678 if (s->ctx->psk_identity_hint != NULL && 2679 s->session->psk_identity_hint == NULL) 2680 { 2681 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2682 ERR_R_MALLOC_FAILURE); 2683 goto psk_err; 2684 } 2685 2686 if (s->session->psk_identity != NULL) 2687 OPENSSL_free(s->session->psk_identity); 2688 s->session->psk_identity = BUF_strdup(identity); 2689 if (s->session->psk_identity == NULL) 2690 { 2691 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2692 ERR_R_MALLOC_FAILURE); 2693 goto psk_err; 2694 } 2695 2696 s->session->master_key_length = 2697 s->method->ssl3_enc->generate_master_secret(s, 2698 s->session->master_key, 2699 psk_or_pre_ms, pre_ms_len); 2700 n = strlen(identity); 2701 s2n(n, p); 2702 memcpy(p, identity, n); 2703 n+=2; 2704 psk_err = 0; 2705 psk_err: 2706 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN); 2707 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms)); 2708 if (psk_err != 0) 2709 { 2710 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); 2711 goto err; 2712 } 2713 } 2714 #endif 2715 else 2716 { 2717 ssl3_send_alert(s, SSL3_AL_FATAL, 2718 SSL_AD_HANDSHAKE_FAILURE); 2719 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 2720 ERR_R_INTERNAL_ERROR); 2721 goto err; 2722 } 2723 2724 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; 2725 l2n3(n,d); 2726 2727 s->state=SSL3_ST_CW_KEY_EXCH_B; 2728 /* number of bytes to write */ 2729 s->init_num=n+4; 2730 s->init_off=0; 2731 } 2732 2733 /* SSL3_ST_CW_KEY_EXCH_B */ 2734 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 2735 err: 2736 #ifndef OPENSSL_NO_ECDH 2737 BN_CTX_free(bn_ctx); 2738 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); 2739 if (clnt_ecdh != NULL) 2740 EC_KEY_free(clnt_ecdh); 2741 EVP_PKEY_free(srvr_pub_pkey); 2742 #endif 2743 return(-1); 2744 } 2745 2746 int ssl3_send_client_verify(SSL *s) 2747 { 2748 unsigned char *p,*d; 2749 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 2750 EVP_PKEY *pkey; 2751 EVP_PKEY_CTX *pctx=NULL; 2752 #ifndef OPENSSL_NO_RSA 2753 unsigned u=0; 2754 #endif 2755 unsigned long n; 2756 int j; 2757 2758 if (s->state == SSL3_ST_CW_CERT_VRFY_A) 2759 { 2760 d=(unsigned char *)s->init_buf->data; 2761 p= &(d[4]); 2762 pkey=s->cert->key->privatekey; 2763 /* Create context from key and test if sha1 is allowed as digest */ 2764 pctx = EVP_PKEY_CTX_new(pkey,NULL); 2765 EVP_PKEY_sign_init(pctx); 2766 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1())>0) 2767 { 2768 s->method->ssl3_enc->cert_verify_mac(s, 2769 NID_sha1, 2770 &(data[MD5_DIGEST_LENGTH])); 2771 } 2772 else 2773 { 2774 ERR_clear_error(); 2775 } 2776 #ifndef OPENSSL_NO_RSA 2777 if (pkey->type == EVP_PKEY_RSA) 2778 { 2779 s->method->ssl3_enc->cert_verify_mac(s, 2780 NID_md5, 2781 &(data[0])); 2782 if (RSA_sign(NID_md5_sha1, data, 2783 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, 2784 &(p[2]), &u, pkey->pkey.rsa) <= 0 ) 2785 { 2786 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); 2787 goto err; 2788 } 2789 s2n(u,p); 2790 n=u+2; 2791 } 2792 else 2793 #endif 2794 #ifndef OPENSSL_NO_DSA 2795 if (pkey->type == EVP_PKEY_DSA) 2796 { 2797 if (!DSA_sign(pkey->save_type, 2798 &(data[MD5_DIGEST_LENGTH]), 2799 SHA_DIGEST_LENGTH,&(p[2]), 2800 (unsigned int *)&j,pkey->pkey.dsa)) 2801 { 2802 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); 2803 goto err; 2804 } 2805 s2n(j,p); 2806 n=j+2; 2807 } 2808 else 2809 #endif 2810 #ifndef OPENSSL_NO_ECDSA 2811 if (pkey->type == EVP_PKEY_EC) 2812 { 2813 if (!ECDSA_sign(pkey->save_type, 2814 &(data[MD5_DIGEST_LENGTH]), 2815 SHA_DIGEST_LENGTH,&(p[2]), 2816 (unsigned int *)&j,pkey->pkey.ec)) 2817 { 2818 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, 2819 ERR_R_ECDSA_LIB); 2820 goto err; 2821 } 2822 s2n(j,p); 2823 n=j+2; 2824 } 2825 else 2826 #endif 2827 if (pkey->type == NID_id_GostR3410_94 || pkey->type == NID_id_GostR3410_2001) 2828 { 2829 unsigned char signbuf[64]; 2830 int i; 2831 size_t sigsize=64; 2832 s->method->ssl3_enc->cert_verify_mac(s, 2833 NID_id_GostR3411_94, 2834 data); 2835 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) { 2836 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, 2837 ERR_R_INTERNAL_ERROR); 2838 goto err; 2839 } 2840 for (i=63,j=0; i>=0; j++, i--) { 2841 p[2+j]=signbuf[i]; 2842 } 2843 s2n(j,p); 2844 n=j+2; 2845 } 2846 else 2847 { 2848 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); 2849 goto err; 2850 } 2851 *(d++)=SSL3_MT_CERTIFICATE_VERIFY; 2852 l2n3(n,d); 2853 2854 s->state=SSL3_ST_CW_CERT_VRFY_B; 2855 s->init_num=(int)n+4; 2856 s->init_off=0; 2857 } 2858 EVP_PKEY_CTX_free(pctx); 2859 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 2860 err: 2861 EVP_PKEY_CTX_free(pctx); 2862 return(-1); 2863 } 2864 2865 int ssl3_send_client_certificate(SSL *s) 2866 { 2867 X509 *x509=NULL; 2868 EVP_PKEY *pkey=NULL; 2869 int i; 2870 unsigned long l; 2871 2872 if (s->state == SSL3_ST_CW_CERT_A) 2873 { 2874 if ((s->cert == NULL) || 2875 (s->cert->key->x509 == NULL) || 2876 (s->cert->key->privatekey == NULL)) 2877 s->state=SSL3_ST_CW_CERT_B; 2878 else 2879 s->state=SSL3_ST_CW_CERT_C; 2880 } 2881 2882 /* We need to get a client cert */ 2883 if (s->state == SSL3_ST_CW_CERT_B) 2884 { 2885 /* If we get an error, we need to 2886 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); 2887 * We then get retied later */ 2888 i=0; 2889 i = ssl_do_client_cert_cb(s, &x509, &pkey); 2890 if (i < 0) 2891 { 2892 s->rwstate=SSL_X509_LOOKUP; 2893 return(-1); 2894 } 2895 s->rwstate=SSL_NOTHING; 2896 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) 2897 { 2898 s->state=SSL3_ST_CW_CERT_B; 2899 if ( !SSL_use_certificate(s,x509) || 2900 !SSL_use_PrivateKey(s,pkey)) 2901 i=0; 2902 } 2903 else if (i == 1) 2904 { 2905 i=0; 2906 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 2907 } 2908 2909 if (x509 != NULL) X509_free(x509); 2910 if (pkey != NULL) EVP_PKEY_free(pkey); 2911 if (i == 0) 2912 { 2913 if (s->version == SSL3_VERSION) 2914 { 2915 s->s3->tmp.cert_req=0; 2916 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); 2917 return(1); 2918 } 2919 else 2920 { 2921 s->s3->tmp.cert_req=2; 2922 } 2923 } 2924 2925 /* Ok, we have a cert */ 2926 s->state=SSL3_ST_CW_CERT_C; 2927 } 2928 2929 if (s->state == SSL3_ST_CW_CERT_C) 2930 { 2931 s->state=SSL3_ST_CW_CERT_D; 2932 l=ssl3_output_cert_chain(s, 2933 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); 2934 s->init_num=(int)l; 2935 s->init_off=0; 2936 } 2937 /* SSL3_ST_CW_CERT_D */ 2938 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 2939 } 2940 2941 #define has_bits(i,m) (((i)&(m)) == (m)) 2942 2943 int ssl3_check_cert_and_algorithm(SSL *s) 2944 { 2945 int i,idx; 2946 long alg_k,alg_a; 2947 EVP_PKEY *pkey=NULL; 2948 SESS_CERT *sc; 2949 #ifndef OPENSSL_NO_RSA 2950 RSA *rsa; 2951 #endif 2952 #ifndef OPENSSL_NO_DH 2953 DH *dh; 2954 #endif 2955 2956 alg_k=s->s3->tmp.new_cipher->algorithm_mkey; 2957 alg_a=s->s3->tmp.new_cipher->algorithm_auth; 2958 2959 /* we don't have a certificate */ 2960 if ((alg_a & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) || (alg_k & SSL_kPSK)) 2961 return(1); 2962 2963 sc=s->session->sess_cert; 2964 if (sc == NULL) 2965 { 2966 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR); 2967 goto err; 2968 } 2969 2970 #ifndef OPENSSL_NO_RSA 2971 rsa=s->session->sess_cert->peer_rsa_tmp; 2972 #endif 2973 #ifndef OPENSSL_NO_DH 2974 dh=s->session->sess_cert->peer_dh_tmp; 2975 #endif 2976 2977 /* This is the passed certificate */ 2978 2979 idx=sc->peer_cert_type; 2980 #ifndef OPENSSL_NO_ECDH 2981 if (idx == SSL_PKEY_ECC) 2982 { 2983 if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, 2984 s->s3->tmp.new_cipher) == 0) 2985 { /* check failed */ 2986 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_BAD_ECC_CERT); 2987 goto f_err; 2988 } 2989 else 2990 { 2991 return 1; 2992 } 2993 } 2994 #endif 2995 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); 2996 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); 2997 EVP_PKEY_free(pkey); 2998 2999 3000 /* Check that we have a certificate if we require one */ 3001 if ((alg_a & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) 3002 { 3003 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT); 3004 goto f_err; 3005 } 3006 #ifndef OPENSSL_NO_DSA 3007 else if ((alg_a & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) 3008 { 3009 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT); 3010 goto f_err; 3011 } 3012 #endif 3013 #ifndef OPENSSL_NO_RSA 3014 if ((alg_k & SSL_kRSA) && 3015 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) 3016 { 3017 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT); 3018 goto f_err; 3019 } 3020 #endif 3021 #ifndef OPENSSL_NO_DH 3022 if ((alg_k & SSL_kEDH) && 3023 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) 3024 { 3025 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY); 3026 goto f_err; 3027 } 3028 else if ((alg_k & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) 3029 { 3030 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT); 3031 goto f_err; 3032 } 3033 #ifndef OPENSSL_NO_DSA 3034 else if ((alg_k & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) 3035 { 3036 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT); 3037 goto f_err; 3038 } 3039 #endif 3040 #endif 3041 3042 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) 3043 { 3044 #ifndef OPENSSL_NO_RSA 3045 if (alg_k & SSL_kRSA) 3046 { 3047 if (rsa == NULL 3048 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) 3049 { 3050 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 3051 goto f_err; 3052 } 3053 } 3054 else 3055 #endif 3056 #ifndef OPENSSL_NO_DH 3057 if (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 3058 { 3059 if (dh == NULL 3060 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) 3061 { 3062 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY); 3063 goto f_err; 3064 } 3065 } 3066 else 3067 #endif 3068 { 3069 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 3070 goto f_err; 3071 } 3072 } 3073 return(1); 3074 f_err: 3075 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 3076 err: 3077 return(0); 3078 } 3079 3080 /* Check to see if handshake is full or resumed. Usually this is just a 3081 * case of checking to see if a cache hit has occurred. In the case of 3082 * session tickets we have to check the next message to be sure. 3083 */ 3084 3085 #ifndef OPENSSL_NO_TLSEXT 3086 # ifndef OPENSSL_NO_NEXTPROTONEG 3087 int ssl3_send_next_proto(SSL *s) 3088 { 3089 unsigned int len, padding_len; 3090 unsigned char *d; 3091 3092 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) 3093 { 3094 len = s->next_proto_negotiated_len; 3095 padding_len = 32 - ((len + 2) % 32); 3096 d = (unsigned char *)s->init_buf->data; 3097 d[4] = len; 3098 memcpy(d + 5, s->next_proto_negotiated, len); 3099 d[5 + len] = padding_len; 3100 memset(d + 6 + len, 0, padding_len); 3101 *(d++)=SSL3_MT_NEXT_PROTO; 3102 l2n3(2 + len + padding_len, d); 3103 s->state = SSL3_ST_CW_NEXT_PROTO_B; 3104 s->init_num = 4 + 2 + len + padding_len; 3105 s->init_off = 0; 3106 } 3107 3108 return ssl3_do_write(s, SSL3_RT_HANDSHAKE); 3109 } 3110 # endif 3111 3112 int ssl3_check_finished(SSL *s) 3113 { 3114 int ok; 3115 long n; 3116 /* If we have no ticket it cannot be a resumed session. */ 3117 if (!s->session->tlsext_tick) 3118 return 1; 3119 /* this function is called when we really expect a Certificate 3120 * message, so permit appropriate message length */ 3121 n=s->method->ssl_get_message(s, 3122 SSL3_ST_CR_CERT_A, 3123 SSL3_ST_CR_CERT_B, 3124 -1, 3125 s->max_cert_list, 3126 &ok); 3127 if (!ok) return((int)n); 3128 s->s3->tmp.reuse_message = 1; 3129 if ((s->s3->tmp.message_type == SSL3_MT_FINISHED) 3130 || (s->s3->tmp.message_type == SSL3_MT_NEWSESSION_TICKET)) 3131 return 2; 3132 3133 return 1; 3134 } 3135 #endif 3136 3137 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey) 3138 { 3139 int i = 0; 3140 #ifndef OPENSSL_NO_ENGINE 3141 if (s->ctx->client_cert_engine) 3142 { 3143 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s, 3144 SSL_get_client_CA_list(s), 3145 px509, ppkey, NULL, NULL, NULL); 3146 if (i != 0) 3147 return i; 3148 } 3149 #endif 3150 if (s->ctx->client_cert_cb) 3151 i = s->ctx->client_cert_cb(s,px509,ppkey); 3152 return i; 3153 } 3154