1/* ssl/s3_clnt.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-2002 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#include <stdio.h> 113#include "ssl_locl.h" 114#include "kssl_lcl.h" 115#include <openssl/buffer.h> 116#include <openssl/rand.h> 117#include <openssl/objects.h> 118#include <openssl/evp.h> 119#include <openssl/md5.h> 120#include <openssl/fips.h> 121 122static SSL_METHOD *ssl3_get_client_method(int ver); 123static int ssl3_client_hello(SSL *s); 124static int ssl3_get_server_hello(SSL *s); 125static int ssl3_get_certificate_request(SSL *s); 126static int ca_dn_cmp(const X509_NAME * const *a,const X509_NAME * const *b); 127static int ssl3_get_server_done(SSL *s); 128static int ssl3_send_client_verify(SSL *s); 129static int ssl3_send_client_certificate(SSL *s); 130static int ssl3_send_client_key_exchange(SSL *s); 131static int ssl3_get_key_exchange(SSL *s); 132static int ssl3_get_server_certificate(SSL *s); 133static int ssl3_check_cert_and_algorithm(SSL *s); 134static SSL_METHOD *ssl3_get_client_method(int ver) 135 { 136 if (ver == SSL3_VERSION) 137 return(SSLv3_client_method()); 138 else 139 return(NULL); 140 } 141 142SSL_METHOD *SSLv3_client_method(void) 143 { 144 static int init=1; 145 static SSL_METHOD SSLv3_client_data; 146 147 if (init) 148 { 149 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD); 150 151 if (init) 152 { 153 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(), 154 sizeof(SSL_METHOD)); 155 SSLv3_client_data.ssl_connect=ssl3_connect; 156 SSLv3_client_data.get_ssl_method=ssl3_get_client_method; 157 init=0; 158 } 159 160 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD); 161 } 162 return(&SSLv3_client_data); 163 } 164 165int ssl3_connect(SSL *s) 166 { 167 BUF_MEM *buf=NULL; 168 unsigned long Time=time(NULL),l; 169 long num1; 170 void (*cb)(const SSL *ssl,int type,int val)=NULL; 171 int ret= -1; 172 int new_state,state,skip=0;; 173 174 RAND_add(&Time,sizeof(Time),0); 175 ERR_clear_error(); 176 clear_sys_error(); 177 178 if (s->info_callback != NULL) 179 cb=s->info_callback; 180 else if (s->ctx->info_callback != NULL) 181 cb=s->ctx->info_callback; 182 183 s->in_handshake++; 184 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 185 186 for (;;) 187 { 188 state=s->state; 189 190 switch(s->state) 191 { 192 case SSL_ST_RENEGOTIATE: 193 s->new_session=1; 194 s->state=SSL_ST_CONNECT; 195 s->ctx->stats.sess_connect_renegotiate++; 196 /* break */ 197 case SSL_ST_BEFORE: 198 case SSL_ST_CONNECT: 199 case SSL_ST_BEFORE|SSL_ST_CONNECT: 200 case SSL_ST_OK|SSL_ST_CONNECT: 201 202 s->server=0; 203 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 204 205 if ((s->version & 0xff00 ) != 0x0300) 206 { 207 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR); 208 ret = -1; 209 goto end; 210 } 211 212 /* s->version=SSL3_VERSION; */ 213 s->type=SSL_ST_CONNECT; 214 215 if (s->init_buf == NULL) 216 { 217 if ((buf=BUF_MEM_new()) == NULL) 218 { 219 ret= -1; 220 goto end; 221 } 222 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 223 { 224 ret= -1; 225 goto end; 226 } 227 s->init_buf=buf; 228 buf=NULL; 229 } 230 231 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } 232 233 /* setup buffing BIO */ 234 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } 235 236 /* don't push the buffering BIO quite yet */ 237 238 ssl3_init_finished_mac(s); 239 240 s->state=SSL3_ST_CW_CLNT_HELLO_A; 241 s->ctx->stats.sess_connect++; 242 s->init_num=0; 243 break; 244 245 case SSL3_ST_CW_CLNT_HELLO_A: 246 case SSL3_ST_CW_CLNT_HELLO_B: 247 248 s->shutdown=0; 249 ret=ssl3_client_hello(s); 250 if (ret <= 0) goto end; 251 s->state=SSL3_ST_CR_SRVR_HELLO_A; 252 s->init_num=0; 253 254 /* turn on buffering for the next lot of output */ 255 if (s->bbio != s->wbio) 256 s->wbio=BIO_push(s->bbio,s->wbio); 257 258 break; 259 260 case SSL3_ST_CR_SRVR_HELLO_A: 261 case SSL3_ST_CR_SRVR_HELLO_B: 262 ret=ssl3_get_server_hello(s); 263 if (ret <= 0) goto end; 264 if (s->hit) 265 s->state=SSL3_ST_CR_FINISHED_A; 266 else 267 s->state=SSL3_ST_CR_CERT_A; 268 s->init_num=0; 269 break; 270 271 case SSL3_ST_CR_CERT_A: 272 case SSL3_ST_CR_CERT_B: 273 /* Check if it is anon DH */ 274 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 275 { 276 ret=ssl3_get_server_certificate(s); 277 if (ret <= 0) goto end; 278 } 279 else 280 skip=1; 281 s->state=SSL3_ST_CR_KEY_EXCH_A; 282 s->init_num=0; 283 break; 284 285 case SSL3_ST_CR_KEY_EXCH_A: 286 case SSL3_ST_CR_KEY_EXCH_B: 287 ret=ssl3_get_key_exchange(s); 288 if (ret <= 0) goto end; 289 s->state=SSL3_ST_CR_CERT_REQ_A; 290 s->init_num=0; 291 292 /* at this point we check that we have the 293 * required stuff from the server */ 294 if (!ssl3_check_cert_and_algorithm(s)) 295 { 296 ret= -1; 297 goto end; 298 } 299 break; 300 301 case SSL3_ST_CR_CERT_REQ_A: 302 case SSL3_ST_CR_CERT_REQ_B: 303 ret=ssl3_get_certificate_request(s); 304 if (ret <= 0) goto end; 305 s->state=SSL3_ST_CR_SRVR_DONE_A; 306 s->init_num=0; 307 break; 308 309 case SSL3_ST_CR_SRVR_DONE_A: 310 case SSL3_ST_CR_SRVR_DONE_B: 311 ret=ssl3_get_server_done(s); 312 if (ret <= 0) goto end; 313 if (s->s3->tmp.cert_req) 314 s->state=SSL3_ST_CW_CERT_A; 315 else 316 s->state=SSL3_ST_CW_KEY_EXCH_A; 317 s->init_num=0; 318 319 break; 320 321 case SSL3_ST_CW_CERT_A: 322 case SSL3_ST_CW_CERT_B: 323 case SSL3_ST_CW_CERT_C: 324 case SSL3_ST_CW_CERT_D: 325 ret=ssl3_send_client_certificate(s); 326 if (ret <= 0) goto end; 327 s->state=SSL3_ST_CW_KEY_EXCH_A; 328 s->init_num=0; 329 break; 330 331 case SSL3_ST_CW_KEY_EXCH_A: 332 case SSL3_ST_CW_KEY_EXCH_B: 333 ret=ssl3_send_client_key_exchange(s); 334 if (ret <= 0) goto end; 335 l=s->s3->tmp.new_cipher->algorithms; 336 /* EAY EAY EAY need to check for DH fix cert 337 * sent back */ 338 /* For TLS, cert_req is set to 2, so a cert chain 339 * of nothing is sent, but no verify packet is sent */ 340 if (s->s3->tmp.cert_req == 1) 341 { 342 s->state=SSL3_ST_CW_CERT_VRFY_A; 343 } 344 else 345 { 346 s->state=SSL3_ST_CW_CHANGE_A; 347 s->s3->change_cipher_spec=0; 348 } 349 350 s->init_num=0; 351 break; 352 353 case SSL3_ST_CW_CERT_VRFY_A: 354 case SSL3_ST_CW_CERT_VRFY_B: 355 ret=ssl3_send_client_verify(s); 356 if (ret <= 0) goto end; 357 s->state=SSL3_ST_CW_CHANGE_A; 358 s->init_num=0; 359 s->s3->change_cipher_spec=0; 360 break; 361 362 case SSL3_ST_CW_CHANGE_A: 363 case SSL3_ST_CW_CHANGE_B: 364 ret=ssl3_send_change_cipher_spec(s, 365 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); 366 if (ret <= 0) goto end; 367 s->state=SSL3_ST_CW_FINISHED_A; 368 s->init_num=0; 369 370 s->session->cipher=s->s3->tmp.new_cipher; 371 if (s->s3->tmp.new_compression == NULL) 372 s->session->compress_meth=0; 373 else 374 s->session->compress_meth= 375 s->s3->tmp.new_compression->id; 376 if (!s->method->ssl3_enc->setup_key_block(s)) 377 { 378 ret= -1; 379 goto end; 380 } 381 382 if (!s->method->ssl3_enc->change_cipher_state(s, 383 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) 384 { 385 ret= -1; 386 goto end; 387 } 388 389 break; 390 391 case SSL3_ST_CW_FINISHED_A: 392 case SSL3_ST_CW_FINISHED_B: 393 ret=ssl3_send_finished(s, 394 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, 395 s->method->ssl3_enc->client_finished_label, 396 s->method->ssl3_enc->client_finished_label_len); 397 if (ret <= 0) goto end; 398 s->state=SSL3_ST_CW_FLUSH; 399 400 /* clear flags */ 401 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; 402 if (s->hit) 403 { 404 s->s3->tmp.next_state=SSL_ST_OK; 405 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 406 { 407 s->state=SSL_ST_OK; 408 s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 409 s->s3->delay_buf_pop_ret=0; 410 } 411 } 412 else 413 { 414 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 415 } 416 s->init_num=0; 417 break; 418 419 case SSL3_ST_CR_FINISHED_A: 420 case SSL3_ST_CR_FINISHED_B: 421 422 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, 423 SSL3_ST_CR_FINISHED_B); 424 if (ret <= 0) goto end; 425 426 if (s->hit) 427 s->state=SSL3_ST_CW_CHANGE_A; 428 else 429 s->state=SSL_ST_OK; 430 s->init_num=0; 431 break; 432 433 case SSL3_ST_CW_FLUSH: 434 /* number of bytes to be flushed */ 435 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); 436 if (num1 > 0) 437 { 438 s->rwstate=SSL_WRITING; 439 num1=BIO_flush(s->wbio); 440 if (num1 <= 0) { ret= -1; goto end; } 441 s->rwstate=SSL_NOTHING; 442 } 443 444 s->state=s->s3->tmp.next_state; 445 break; 446 447 case SSL_ST_OK: 448 /* clean a few things up */ 449 ssl3_cleanup_key_block(s); 450 451 if (s->init_buf != NULL) 452 { 453 BUF_MEM_free(s->init_buf); 454 s->init_buf=NULL; 455 } 456 457 /* If we are not 'joining' the last two packets, 458 * remove the buffering now */ 459 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) 460 ssl_free_wbio_buffer(s); 461 /* else do it later in ssl3_write */ 462 463 s->init_num=0; 464 s->new_session=0; 465 466 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); 467 if (s->hit) s->ctx->stats.sess_hit++; 468 469 ret=1; 470 /* s->server=0; */ 471 s->handshake_func=ssl3_connect; 472 s->ctx->stats.sess_connect_good++; 473 474 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 475 476 goto end; 477 /* break; */ 478 479 default: 480 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE); 481 ret= -1; 482 goto end; 483 /* break; */ 484 } 485 486 /* did we do anything */ 487 if (!s->s3->tmp.reuse_message && !skip) 488 { 489 if (s->debug) 490 { 491 if ((ret=BIO_flush(s->wbio)) <= 0) 492 goto end; 493 } 494 495 if ((cb != NULL) && (s->state != state)) 496 { 497 new_state=s->state; 498 s->state=state; 499 cb(s,SSL_CB_CONNECT_LOOP,1); 500 s->state=new_state; 501 } 502 } 503 skip=0; 504 } 505end: 506 s->in_handshake--; 507 if (buf != NULL) 508 BUF_MEM_free(buf); 509 if (cb != NULL) 510 cb(s,SSL_CB_CONNECT_EXIT,ret); 511 return(ret); 512 } 513 514 515static int ssl3_client_hello(SSL *s) 516 { 517 unsigned char *buf; 518 unsigned char *p,*d; 519 int i,j; 520 unsigned long Time,l; 521 SSL_COMP *comp; 522 523 buf=(unsigned char *)s->init_buf->data; 524 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) 525 { 526 if ((s->session == NULL) || 527 (s->session->ssl_version != s->version) || 528 (s->session->not_resumable)) 529 { 530 if (!ssl_get_new_session(s,0)) 531 goto err; 532 } 533 /* else use the pre-loaded session */ 534 535 p=s->s3->client_random; 536 Time=time(NULL); /* Time */ 537 l2n(Time,p); 538 if(RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0) 539 goto err; 540 541 /* Do the message type and length last */ 542 d=p= &(buf[4]); 543 544 *(p++)=s->version>>8; 545 *(p++)=s->version&0xff; 546 s->client_version=s->version; 547 548 /* Random stuff */ 549 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); 550 p+=SSL3_RANDOM_SIZE; 551 552 /* Session ID */ 553 if (s->new_session) 554 i=0; 555 else 556 i=s->session->session_id_length; 557 *(p++)=i; 558 if (i != 0) 559 { 560 if (i > sizeof s->session->session_id) 561 { 562 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 563 goto err; 564 } 565 memcpy(p,s->session->session_id,i); 566 p+=i; 567 } 568 569 /* Ciphers supported */ 570 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2])); 571 if (i == 0) 572 { 573 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); 574 goto err; 575 } 576 s2n(i,p); 577 p+=i; 578 579 /* COMPRESSION */ 580 if (s->ctx->comp_methods == NULL) 581 j=0; 582 else 583 j=sk_SSL_COMP_num(s->ctx->comp_methods); 584 *(p++)=1+j; 585 for (i=0; i<j; i++) 586 { 587 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); 588 *(p++)=comp->id; 589 } 590 *(p++)=0; /* Add the NULL method */ 591 592 l=(p-d); 593 d=buf; 594 *(d++)=SSL3_MT_CLIENT_HELLO; 595 l2n3(l,d); 596 597 s->state=SSL3_ST_CW_CLNT_HELLO_B; 598 /* number of bytes to write */ 599 s->init_num=p-buf; 600 s->init_off=0; 601 } 602 603 /* SSL3_ST_CW_CLNT_HELLO_B */ 604 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 605err: 606 return(-1); 607 } 608 609static int ssl3_get_server_hello(SSL *s) 610 { 611 STACK_OF(SSL_CIPHER) *sk; 612 SSL_CIPHER *c; 613 unsigned char *p,*d; 614 int i,al,ok; 615 unsigned int j; 616 long n; 617 SSL_COMP *comp; 618 619 n=ssl3_get_message(s, 620 SSL3_ST_CR_SRVR_HELLO_A, 621 SSL3_ST_CR_SRVR_HELLO_B, 622 SSL3_MT_SERVER_HELLO, 623 300, /* ?? */ 624 &ok); 625 626 if (!ok) return((int)n); 627 d=p=(unsigned char *)s->init_msg; 628 629 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff))) 630 { 631 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION); 632 s->version=(s->version&0xff00)|p[1]; 633 al=SSL_AD_PROTOCOL_VERSION; 634 goto f_err; 635 } 636 p+=2; 637 638 /* load the server hello data */ 639 /* load the server random */ 640 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE); 641 p+=SSL3_RANDOM_SIZE; 642 643 /* get the session-id */ 644 j= *(p++); 645 646 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) 647 { 648 al=SSL_AD_ILLEGAL_PARAMETER; 649 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_LONG); 650 goto f_err; 651 } 652 653 if (j != 0 && j == s->session->session_id_length 654 && memcmp(p,s->session->session_id,j) == 0) 655 { 656 if(s->sid_ctx_length != s->session->sid_ctx_length 657 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length)) 658 { 659 /* actually a client application bug */ 660 al=SSL_AD_ILLEGAL_PARAMETER; 661 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); 662 goto f_err; 663 } 664 s->hit=1; 665 } 666 else /* a miss or crap from the other end */ 667 { 668 /* If we were trying for session-id reuse, make a new 669 * SSL_SESSION so we don't stuff up other people */ 670 s->hit=0; 671 if (s->session->session_id_length > 0) 672 { 673 if (!ssl_get_new_session(s,0)) 674 { 675 al=SSL_AD_INTERNAL_ERROR; 676 goto f_err; 677 } 678 } 679 s->session->session_id_length=j; 680 memcpy(s->session->session_id,p,j); /* j could be 0 */ 681 } 682 p+=j; 683 c=ssl_get_cipher_by_char(s,p); 684 if (c == NULL) 685 { 686 /* unknown cipher */ 687 al=SSL_AD_ILLEGAL_PARAMETER; 688 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED); 689 goto f_err; 690 } 691 p+=ssl_put_cipher_by_char(s,NULL,NULL); 692 693 sk=ssl_get_ciphers_by_id(s); 694 i=sk_SSL_CIPHER_find(sk,c); 695 if (i < 0) 696 { 697 /* we did not say we would use this cipher */ 698 al=SSL_AD_ILLEGAL_PARAMETER; 699 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED); 700 goto f_err; 701 } 702 703 /* Depending on the session caching (internal/external), the cipher 704 and/or cipher_id values may not be set. Make sure that 705 cipher_id is set and use it for comparison. */ 706 if (s->session->cipher) 707 s->session->cipher_id = s->session->cipher->id; 708 if (s->hit && (s->session->cipher_id != c->id)) 709 { 710 if (!(s->options & 711 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)) 712 { 713 al=SSL_AD_ILLEGAL_PARAMETER; 714 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); 715 goto f_err; 716 } 717 } 718 s->s3->tmp.new_cipher=c; 719 720 /* lets get the compression algorithm */ 721 /* COMPRESSION */ 722 j= *(p++); 723 if (j == 0) 724 comp=NULL; 725 else 726 comp=ssl3_comp_find(s->ctx->comp_methods,j); 727 728 if ((j != 0) && (comp == NULL)) 729 { 730 al=SSL_AD_ILLEGAL_PARAMETER; 731 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 732 goto f_err; 733 } 734 else 735 { 736 s->s3->tmp.new_compression=comp; 737 } 738 739 if (p != (d+n)) 740 { 741 /* wrong packet length */ 742 al=SSL_AD_DECODE_ERROR; 743 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH); 744 goto err; 745 } 746 747 return(1); 748f_err: 749 ssl3_send_alert(s,SSL3_AL_FATAL,al); 750err: 751 return(-1); 752 } 753 754static int ssl3_get_server_certificate(SSL *s) 755 { 756 int al,i,ok,ret= -1; 757 unsigned long n,nc,llen,l; 758 X509 *x=NULL; 759 unsigned char *p,*d,*q; 760 STACK_OF(X509) *sk=NULL; 761 SESS_CERT *sc; 762 EVP_PKEY *pkey=NULL; 763 int need_cert = 1; /* VRS: 0=> will allow null cert if auth == KRB5 */ 764 765 n=ssl3_get_message(s, 766 SSL3_ST_CR_CERT_A, 767 SSL3_ST_CR_CERT_B, 768 -1, 769 s->max_cert_list, 770 &ok); 771 772 if (!ok) return((int)n); 773 774 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) 775 { 776 s->s3->tmp.reuse_message=1; 777 return(1); 778 } 779 780 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 781 { 782 al=SSL_AD_UNEXPECTED_MESSAGE; 783 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE); 784 goto f_err; 785 } 786 d=p=(unsigned char *)s->init_msg; 787 788 if ((sk=sk_X509_new_null()) == NULL) 789 { 790 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 791 goto err; 792 } 793 794 n2l3(p,llen); 795 if (llen+3 != n) 796 { 797 al=SSL_AD_DECODE_ERROR; 798 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 799 goto f_err; 800 } 801 for (nc=0; nc<llen; ) 802 { 803 n2l3(p,l); 804 if ((l+nc+3) > llen) 805 { 806 al=SSL_AD_DECODE_ERROR; 807 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 808 goto f_err; 809 } 810 811 q=p; 812 x=d2i_X509(NULL,&q,l); 813 if (x == NULL) 814 { 815 al=SSL_AD_BAD_CERTIFICATE; 816 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB); 817 goto f_err; 818 } 819 if (q != (p+l)) 820 { 821 al=SSL_AD_DECODE_ERROR; 822 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 823 goto f_err; 824 } 825 if (!sk_X509_push(sk,x)) 826 { 827 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE); 828 goto err; 829 } 830 x=NULL; 831 nc+=l+3; 832 p=q; 833 } 834 835 i=ssl_verify_cert_chain(s,sk); 836 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i) 837#ifndef OPENSSL_NO_KRB5 838 && (s->s3->tmp.new_cipher->algorithms & (SSL_MKEY_MASK|SSL_AUTH_MASK)) 839 != (SSL_aKRB5|SSL_kKRB5) 840#endif /* OPENSSL_NO_KRB5 */ 841 ) 842 { 843 al=ssl_verify_alarm_type(s->verify_result); 844 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED); 845 goto f_err; 846 } 847 ERR_clear_error(); /* but we keep s->verify_result */ 848 849 sc=ssl_sess_cert_new(); 850 if (sc == NULL) goto err; 851 852 if (s->session->sess_cert) ssl_sess_cert_free(s->session->sess_cert); 853 s->session->sess_cert=sc; 854 855 sc->cert_chain=sk; 856 /* Inconsistency alert: cert_chain does include the peer's 857 * certificate, which we don't include in s3_srvr.c */ 858 x=sk_X509_value(sk,0); 859 sk=NULL; 860 /* VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end*/ 861 862 pkey=X509_get_pubkey(x); 863 864 /* VRS: allow null cert if auth == KRB5 */ 865 need_cert = ((s->s3->tmp.new_cipher->algorithms 866 & (SSL_MKEY_MASK|SSL_AUTH_MASK)) 867 == (SSL_aKRB5|SSL_kKRB5))? 0: 1; 868 869#ifdef KSSL_DEBUG 870 printf("pkey,x = %p, %p\n", pkey,x); 871 printf("ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x,pkey)); 872 printf("cipher, alg, nc = %s, %lx, %d\n", s->s3->tmp.new_cipher->name, 873 s->s3->tmp.new_cipher->algorithms, need_cert); 874#endif /* KSSL_DEBUG */ 875 876 if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) 877 { 878 x=NULL; 879 al=SSL3_AL_FATAL; 880 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 881 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); 882 goto f_err; 883 } 884 885 i=ssl_cert_type(x,pkey); 886 if (need_cert && i < 0) 887 { 888 x=NULL; 889 al=SSL3_AL_FATAL; 890 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, 891 SSL_R_UNKNOWN_CERTIFICATE_TYPE); 892 goto f_err; 893 } 894 895 if (need_cert) 896 { 897 sc->peer_cert_type=i; 898 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 899 /* Why would the following ever happen? 900 * We just created sc a couple of lines ago. */ 901 if (sc->peer_pkeys[i].x509 != NULL) 902 X509_free(sc->peer_pkeys[i].x509); 903 sc->peer_pkeys[i].x509=x; 904 sc->peer_key= &(sc->peer_pkeys[i]); 905 906 if (s->session->peer != NULL) 907 X509_free(s->session->peer); 908 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509); 909 s->session->peer=x; 910 } 911 else 912 { 913 sc->peer_cert_type=i; 914 sc->peer_key= NULL; 915 916 if (s->session->peer != NULL) 917 X509_free(s->session->peer); 918 s->session->peer=NULL; 919 } 920 s->session->verify_result = s->verify_result; 921 922 x=NULL; 923 ret=1; 924 925 if (0) 926 { 927f_err: 928 ssl3_send_alert(s,SSL3_AL_FATAL,al); 929 } 930err: 931 EVP_PKEY_free(pkey); 932 X509_free(x); 933 sk_X509_pop_free(sk,X509_free); 934 return(ret); 935 } 936 937static int ssl3_get_key_exchange(SSL *s) 938 { 939#ifndef OPENSSL_NO_RSA 940 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2]; 941#endif 942 EVP_MD_CTX md_ctx; 943 unsigned char *param,*p; 944 int al,i,j,param_len,ok; 945 long n,alg; 946 EVP_PKEY *pkey=NULL; 947#ifndef OPENSSL_NO_RSA 948 RSA *rsa=NULL; 949#endif 950#ifndef OPENSSL_NO_DH 951 DH *dh=NULL; 952#endif 953 954 /* use same message size as in ssl3_get_certificate_request() 955 * as ServerKeyExchange message may be skipped */ 956 n=ssl3_get_message(s, 957 SSL3_ST_CR_KEY_EXCH_A, 958 SSL3_ST_CR_KEY_EXCH_B, 959 -1, 960 s->max_cert_list, 961 &ok); 962 963 if (!ok) return((int)n); 964 965 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) 966 { 967 s->s3->tmp.reuse_message=1; 968 return(1); 969 } 970 971 param=p=(unsigned char *)s->init_msg; 972 973 if (s->session->sess_cert != NULL) 974 { 975#ifndef OPENSSL_NO_RSA 976 if (s->session->sess_cert->peer_rsa_tmp != NULL) 977 { 978 RSA_free(s->session->sess_cert->peer_rsa_tmp); 979 s->session->sess_cert->peer_rsa_tmp=NULL; 980 } 981#endif 982#ifndef OPENSSL_NO_DH 983 if (s->session->sess_cert->peer_dh_tmp) 984 { 985 DH_free(s->session->sess_cert->peer_dh_tmp); 986 s->session->sess_cert->peer_dh_tmp=NULL; 987 } 988#endif 989 } 990 else 991 { 992 s->session->sess_cert=ssl_sess_cert_new(); 993 } 994 995 param_len=0; 996 alg=s->s3->tmp.new_cipher->algorithms; 997 EVP_MD_CTX_init(&md_ctx); 998 999#ifndef OPENSSL_NO_RSA 1000 if (alg & SSL_kRSA) 1001 { 1002 if ((rsa=RSA_new()) == NULL) 1003 { 1004 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1005 goto err; 1006 } 1007 n2s(p,i); 1008 param_len=i+2; 1009 if (param_len > n) 1010 { 1011 al=SSL_AD_DECODE_ERROR; 1012 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH); 1013 goto f_err; 1014 } 1015 if (!(rsa->n=BN_bin2bn(p,i,rsa->n))) 1016 { 1017 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1018 goto err; 1019 } 1020 p+=i; 1021 1022 n2s(p,i); 1023 param_len+=i+2; 1024 if (param_len > n) 1025 { 1026 al=SSL_AD_DECODE_ERROR; 1027 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH); 1028 goto f_err; 1029 } 1030 if (!(rsa->e=BN_bin2bn(p,i,rsa->e))) 1031 { 1032 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1033 goto err; 1034 } 1035 p+=i; 1036 n-=param_len; 1037 1038 /* this should be because we are using an export cipher */ 1039 if (alg & SSL_aRSA) 1040 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1041 else 1042 { 1043 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1044 goto err; 1045 } 1046 s->session->sess_cert->peer_rsa_tmp=rsa; 1047 rsa=NULL; 1048 } 1049#else /* OPENSSL_NO_RSA */ 1050 if (0) 1051 ; 1052#endif 1053#ifndef OPENSSL_NO_DH 1054 else if (alg & SSL_kEDH) 1055 { 1056 if ((dh=DH_new()) == NULL) 1057 { 1058 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB); 1059 goto err; 1060 } 1061 n2s(p,i); 1062 param_len=i+2; 1063 if (param_len > n) 1064 { 1065 al=SSL_AD_DECODE_ERROR; 1066 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH); 1067 goto f_err; 1068 } 1069 if (!(dh->p=BN_bin2bn(p,i,NULL))) 1070 { 1071 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1072 goto err; 1073 } 1074 p+=i; 1075 1076 n2s(p,i); 1077 param_len+=i+2; 1078 if (param_len > n) 1079 { 1080 al=SSL_AD_DECODE_ERROR; 1081 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH); 1082 goto f_err; 1083 } 1084 if (!(dh->g=BN_bin2bn(p,i,NULL))) 1085 { 1086 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1087 goto err; 1088 } 1089 p+=i; 1090 1091 n2s(p,i); 1092 param_len+=i+2; 1093 if (param_len > n) 1094 { 1095 al=SSL_AD_DECODE_ERROR; 1096 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH); 1097 goto f_err; 1098 } 1099 if (!(dh->pub_key=BN_bin2bn(p,i,NULL))) 1100 { 1101 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB); 1102 goto err; 1103 } 1104 p+=i; 1105 n-=param_len; 1106 1107#ifndef OPENSSL_NO_RSA 1108 if (alg & SSL_aRSA) 1109 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1110#else 1111 if (0) 1112 ; 1113#endif 1114#ifndef OPENSSL_NO_DSA 1115 else if (alg & SSL_aDSS) 1116 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].x509); 1117#endif 1118 /* else anonymous DH, so no certificate or pkey. */ 1119 1120 s->session->sess_cert->peer_dh_tmp=dh; 1121 dh=NULL; 1122 } 1123 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd)) 1124 { 1125 al=SSL_AD_ILLEGAL_PARAMETER; 1126 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 1127 goto f_err; 1128 } 1129#endif /* !OPENSSL_NO_DH */ 1130 if (alg & SSL_aFZA) 1131 { 1132 al=SSL_AD_HANDSHAKE_FAILURE; 1133 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER); 1134 goto f_err; 1135 } 1136 1137 1138 /* p points to the next byte, there are 'n' bytes left */ 1139 1140 1141 /* if it was signed, check the signature */ 1142 if (pkey != NULL) 1143 { 1144 n2s(p,i); 1145 n-=2; 1146 j=EVP_PKEY_size(pkey); 1147 1148 if ((i != n) || (n > j) || (n <= 0)) 1149 { 1150 /* wrong packet length */ 1151 al=SSL_AD_DECODE_ERROR; 1152 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH); 1153 goto f_err; 1154 } 1155 1156#ifndef OPENSSL_NO_RSA 1157 if (pkey->type == EVP_PKEY_RSA) 1158 { 1159 int num; 1160 1161 j=0; 1162 q=md_buf; 1163 for (num=2; num > 0; num--) 1164 { 1165 EVP_MD_CTX_set_flags(&md_ctx, 1166 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW); 1167 EVP_DigestInit_ex(&md_ctx,(num == 2) 1168 ?s->ctx->md5:s->ctx->sha1, NULL); 1169 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1170 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1171 EVP_DigestUpdate(&md_ctx,param,param_len); 1172 1173 EVP_DigestFinal_ex(&md_ctx,q,(unsigned int *)&i); 1174 q+=i; 1175 j+=i; 1176 } 1177 i=RSA_verify(NID_md5_sha1, md_buf, j, p, n, 1178 pkey->pkey.rsa); 1179 if (i < 0) 1180 { 1181 al=SSL_AD_DECRYPT_ERROR; 1182 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); 1183 goto f_err; 1184 } 1185 if (i == 0) 1186 { 1187 /* bad signature */ 1188 al=SSL_AD_DECRYPT_ERROR; 1189 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); 1190 goto f_err; 1191 } 1192 } 1193 else 1194#endif 1195#ifndef OPENSSL_NO_DSA 1196 if (pkey->type == EVP_PKEY_DSA) 1197 { 1198 /* lets do DSS */ 1199 EVP_VerifyInit_ex(&md_ctx,EVP_dss1(), NULL); 1200 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1201 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1202 EVP_VerifyUpdate(&md_ctx,param,param_len); 1203 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey)) 1204 { 1205 /* bad signature */ 1206 al=SSL_AD_DECRYPT_ERROR; 1207 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE); 1208 goto f_err; 1209 } 1210 } 1211 else 1212#endif 1213 { 1214 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1215 goto err; 1216 } 1217 } 1218 else 1219 { 1220 /* still data left over */ 1221 if (!(alg & SSL_aNULL)) 1222 { 1223 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1224 goto err; 1225 } 1226 if (n != 0) 1227 { 1228 al=SSL_AD_DECODE_ERROR; 1229 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE); 1230 goto f_err; 1231 } 1232 } 1233 EVP_PKEY_free(pkey); 1234 EVP_MD_CTX_cleanup(&md_ctx); 1235 return(1); 1236f_err: 1237 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1238err: 1239 EVP_PKEY_free(pkey); 1240#ifndef OPENSSL_NO_RSA 1241 if (rsa != NULL) 1242 RSA_free(rsa); 1243#endif 1244#ifndef OPENSSL_NO_DH 1245 if (dh != NULL) 1246 DH_free(dh); 1247#endif 1248 EVP_MD_CTX_cleanup(&md_ctx); 1249 return(-1); 1250 } 1251 1252static int ssl3_get_certificate_request(SSL *s) 1253 { 1254 int ok,ret=0; 1255 unsigned long n,nc,l; 1256 unsigned int llen,ctype_num,i; 1257 X509_NAME *xn=NULL; 1258 unsigned char *p,*d,*q; 1259 STACK_OF(X509_NAME) *ca_sk=NULL; 1260 1261 n=ssl3_get_message(s, 1262 SSL3_ST_CR_CERT_REQ_A, 1263 SSL3_ST_CR_CERT_REQ_B, 1264 -1, 1265 s->max_cert_list, 1266 &ok); 1267 1268 if (!ok) return((int)n); 1269 1270 s->s3->tmp.cert_req=0; 1271 1272 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) 1273 { 1274 s->s3->tmp.reuse_message=1; 1275 return(1); 1276 } 1277 1278 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) 1279 { 1280 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 1281 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE); 1282 goto err; 1283 } 1284 1285 /* TLS does not like anon-DH with client cert */ 1286 if (s->version > SSL3_VERSION) 1287 { 1288 l=s->s3->tmp.new_cipher->algorithms; 1289 if (l & SSL_aNULL) 1290 { 1291 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE); 1292 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER); 1293 goto err; 1294 } 1295 } 1296 1297 d=p=(unsigned char *)s->init_msg; 1298 1299 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL) 1300 { 1301 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1302 goto err; 1303 } 1304 1305 /* get the certificate types */ 1306 ctype_num= *(p++); 1307 if (ctype_num > SSL3_CT_NUMBER) 1308 ctype_num=SSL3_CT_NUMBER; 1309 for (i=0; i<ctype_num; i++) 1310 s->s3->tmp.ctype[i]= p[i]; 1311 p+=ctype_num; 1312 1313 /* get the CA RDNs */ 1314 n2s(p,llen); 1315#if 0 1316{ 1317FILE *out; 1318out=fopen("/tmp/vsign.der","w"); 1319fwrite(p,1,llen,out); 1320fclose(out); 1321} 1322#endif 1323 1324 if ((llen+ctype_num+2+1) != n) 1325 { 1326 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1327 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH); 1328 goto err; 1329 } 1330 1331 for (nc=0; nc<llen; ) 1332 { 1333 n2s(p,l); 1334 if ((l+nc+2) > llen) 1335 { 1336 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1337 goto cont; /* netscape bugs */ 1338 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1339 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG); 1340 goto err; 1341 } 1342 1343 q=p; 1344 1345 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL) 1346 { 1347 /* If netscape tolerance is on, ignore errors */ 1348 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG) 1349 goto cont; 1350 else 1351 { 1352 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1353 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB); 1354 goto err; 1355 } 1356 } 1357 1358 if (q != (p+l)) 1359 { 1360 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1361 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH); 1362 goto err; 1363 } 1364 if (!sk_X509_NAME_push(ca_sk,xn)) 1365 { 1366 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE); 1367 goto err; 1368 } 1369 1370 p+=l; 1371 nc+=l+2; 1372 } 1373 1374 if (0) 1375 { 1376cont: 1377 ERR_clear_error(); 1378 } 1379 1380 /* we should setup a certificate to return.... */ 1381 s->s3->tmp.cert_req=1; 1382 s->s3->tmp.ctype_num=ctype_num; 1383 if (s->s3->tmp.ca_names != NULL) 1384 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free); 1385 s->s3->tmp.ca_names=ca_sk; 1386 ca_sk=NULL; 1387 1388 ret=1; 1389err: 1390 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free); 1391 return(ret); 1392 } 1393 1394static int ca_dn_cmp(const X509_NAME * const *a, const X509_NAME * const *b) 1395 { 1396 return(X509_NAME_cmp(*a,*b)); 1397 } 1398 1399static int ssl3_get_server_done(SSL *s) 1400 { 1401 int ok,ret=0; 1402 long n; 1403 1404 n=ssl3_get_message(s, 1405 SSL3_ST_CR_SRVR_DONE_A, 1406 SSL3_ST_CR_SRVR_DONE_B, 1407 SSL3_MT_SERVER_DONE, 1408 30, /* should be very small, like 0 :-) */ 1409 &ok); 1410 1411 if (!ok) return((int)n); 1412 if (n > 0) 1413 { 1414 /* should contain no data */ 1415 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR); 1416 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH); 1417 return -1; 1418 } 1419 ret=1; 1420 return(ret); 1421 } 1422 1423static int ssl3_send_client_key_exchange(SSL *s) 1424 { 1425 unsigned char *p,*d; 1426 int n; 1427 unsigned long l; 1428#ifndef OPENSSL_NO_RSA 1429 unsigned char *q; 1430 EVP_PKEY *pkey=NULL; 1431#endif 1432#ifndef OPENSSL_NO_KRB5 1433 KSSL_ERR kssl_err; 1434#endif /* OPENSSL_NO_KRB5 */ 1435 1436 if (s->state == SSL3_ST_CW_KEY_EXCH_A) 1437 { 1438 d=(unsigned char *)s->init_buf->data; 1439 p= &(d[4]); 1440 1441 l=s->s3->tmp.new_cipher->algorithms; 1442 1443 /* Fool emacs indentation */ 1444 if (0) {} 1445#ifndef OPENSSL_NO_RSA 1446 else if (l & SSL_kRSA) 1447 { 1448 RSA *rsa; 1449 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 1450 1451 if (s->session->sess_cert->peer_rsa_tmp != NULL) 1452 rsa=s->session->sess_cert->peer_rsa_tmp; 1453 else 1454 { 1455 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 1456 if ((pkey == NULL) || 1457 (pkey->type != EVP_PKEY_RSA) || 1458 (pkey->pkey.rsa == NULL)) 1459 { 1460 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1461 goto err; 1462 } 1463 rsa=pkey->pkey.rsa; 1464 EVP_PKEY_free(pkey); 1465 } 1466 1467 tmp_buf[0]=s->client_version>>8; 1468 tmp_buf[1]=s->client_version&0xff; 1469 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) 1470 goto err; 1471 1472 s->session->master_key_length=sizeof tmp_buf; 1473 1474 q=p; 1475 /* Fix buf for TLS and beyond */ 1476 if (s->version > SSL3_VERSION) 1477 p+=2; 1478 n=RSA_public_encrypt(sizeof tmp_buf, 1479 tmp_buf,p,rsa,RSA_PKCS1_PADDING); 1480#ifdef PKCS1_CHECK 1481 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; 1482 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; 1483#endif 1484 if (n <= 0) 1485 { 1486 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); 1487 goto err; 1488 } 1489 1490 /* Fix buf for TLS and beyond */ 1491 if (s->version > SSL3_VERSION) 1492 { 1493 s2n(n,q); 1494 n+=2; 1495 } 1496 1497 s->session->master_key_length= 1498 s->method->ssl3_enc->generate_master_secret(s, 1499 s->session->master_key, 1500 tmp_buf,sizeof tmp_buf); 1501 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); 1502 } 1503#endif 1504#ifndef OPENSSL_NO_KRB5 1505 else if (l & SSL_kKRB5) 1506 { 1507 krb5_error_code krb5rc; 1508 KSSL_CTX *kssl_ctx = s->kssl_ctx; 1509 /* krb5_data krb5_ap_req; */ 1510 krb5_data *enc_ticket; 1511 krb5_data authenticator, *authp = NULL; 1512 EVP_CIPHER_CTX ciph_ctx; 1513 EVP_CIPHER *enc = NULL; 1514 unsigned char iv[EVP_MAX_IV_LENGTH]; 1515 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 1516 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH 1517 + EVP_MAX_IV_LENGTH]; 1518 int padl, outl = sizeof(epms); 1519 1520 EVP_CIPHER_CTX_init(&ciph_ctx); 1521 1522#ifdef KSSL_DEBUG 1523 printf("ssl3_send_client_key_exchange(%lx & %lx)\n", 1524 l, SSL_kKRB5); 1525#endif /* KSSL_DEBUG */ 1526 1527 authp = NULL; 1528#ifdef KRB5SENDAUTH 1529 if (KRB5SENDAUTH) authp = &authenticator; 1530#endif /* KRB5SENDAUTH */ 1531 1532 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, 1533 &kssl_err); 1534 enc = kssl_map_enc(kssl_ctx->enctype); 1535 if (enc == NULL) 1536 goto err; 1537#ifdef KSSL_DEBUG 1538 { 1539 printf("kssl_cget_tkt rtn %d\n", krb5rc); 1540 if (krb5rc && kssl_err.text) 1541 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); 1542 } 1543#endif /* KSSL_DEBUG */ 1544 1545 if (krb5rc) 1546 { 1547 ssl3_send_alert(s,SSL3_AL_FATAL, 1548 SSL_AD_HANDSHAKE_FAILURE); 1549 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, 1550 kssl_err.reason); 1551 goto err; 1552 } 1553 1554 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ 1555 ** in place of RFC 2712 KerberosWrapper, as in: 1556 ** 1557 ** Send ticket (copy to *p, set n = length) 1558 ** n = krb5_ap_req.length; 1559 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); 1560 ** if (krb5_ap_req.data) 1561 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); 1562 ** 1563 ** Now using real RFC 2712 KerberosWrapper 1564 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) 1565 ** Note: 2712 "opaque" types are here replaced 1566 ** with a 2-byte length followed by the value. 1567 ** Example: 1568 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms 1569 ** Where "xx xx" = length bytes. Shown here with 1570 ** optional authenticator omitted. 1571 */ 1572 1573 /* KerberosWrapper.Ticket */ 1574 s2n(enc_ticket->length,p); 1575 memcpy(p, enc_ticket->data, enc_ticket->length); 1576 p+= enc_ticket->length; 1577 n = enc_ticket->length + 2; 1578 1579 /* KerberosWrapper.Authenticator */ 1580 if (authp && authp->length) 1581 { 1582 s2n(authp->length,p); 1583 memcpy(p, authp->data, authp->length); 1584 p+= authp->length; 1585 n+= authp->length + 2; 1586 1587 free(authp->data); 1588 authp->data = NULL; 1589 authp->length = 0; 1590 } 1591 else 1592 { 1593 s2n(0,p);/* null authenticator length */ 1594 n+=2; 1595 } 1596 1597 if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0) 1598 goto err; 1599 1600 /* 20010420 VRS. Tried it this way; failed. 1601 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); 1602 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, 1603 ** kssl_ctx->length); 1604 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); 1605 */ 1606 1607 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 1608 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, 1609 kssl_ctx->key,iv); 1610 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, 1611 sizeof tmp_buf); 1612 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); 1613 outl += padl; 1614 if (outl > sizeof epms) 1615 { 1616 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1617 goto err; 1618 } 1619 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 1620 1621 /* KerberosWrapper.EncryptedPreMasterSecret */ 1622 s2n(outl,p); 1623 memcpy(p, epms, outl); 1624 p+=outl; 1625 n+=outl + 2; 1626 1627 s->session->master_key_length= 1628 s->method->ssl3_enc->generate_master_secret(s, 1629 s->session->master_key, 1630 tmp_buf, sizeof tmp_buf); 1631 1632 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 1633 OPENSSL_cleanse(epms, outl); 1634 } 1635#endif 1636#ifndef OPENSSL_NO_DH 1637 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1638 { 1639 DH *dh_srvr,*dh_clnt; 1640 1641 if (s->session->sess_cert->peer_dh_tmp != NULL) 1642 dh_srvr=s->session->sess_cert->peer_dh_tmp; 1643 else 1644 { 1645 /* we get them from the cert */ 1646 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1647 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 1648 goto err; 1649 } 1650 1651 /* generate a new random key */ 1652 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) 1653 { 1654 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1655 goto err; 1656 } 1657 if (!DH_generate_key(dh_clnt)) 1658 { 1659 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1660 goto err; 1661 } 1662 1663 /* use the 'p' output buffer for the DH key, but 1664 * make sure to clear it out afterwards */ 1665 1666 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); 1667 1668 if (n <= 0) 1669 { 1670 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1671 goto err; 1672 } 1673 1674 /* generate master key from the result */ 1675 s->session->master_key_length= 1676 s->method->ssl3_enc->generate_master_secret(s, 1677 s->session->master_key,p,n); 1678 /* clean up */ 1679 memset(p,0,n); 1680 1681 /* send off the data */ 1682 n=BN_num_bytes(dh_clnt->pub_key); 1683 s2n(n,p); 1684 BN_bn2bin(dh_clnt->pub_key,p); 1685 n+=2; 1686 1687 DH_free(dh_clnt); 1688 1689 /* perhaps clean things up a bit EAY EAY EAY EAY*/ 1690 } 1691#endif 1692 else 1693 { 1694 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1695 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1696 goto err; 1697 } 1698 1699 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; 1700 l2n3(n,d); 1701 1702 s->state=SSL3_ST_CW_KEY_EXCH_B; 1703 /* number of bytes to write */ 1704 s->init_num=n+4; 1705 s->init_off=0; 1706 } 1707 1708 /* SSL3_ST_CW_KEY_EXCH_B */ 1709 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1710err: 1711 return(-1); 1712 } 1713 1714static int ssl3_send_client_verify(SSL *s) 1715 { 1716 unsigned char *p,*d; 1717 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1718 EVP_PKEY *pkey; 1719#ifndef OPENSSL_NO_RSA 1720 unsigned u=0; 1721#endif 1722 unsigned long n; 1723#ifndef OPENSSL_NO_DSA 1724 int j; 1725#endif 1726 1727 if (s->state == SSL3_ST_CW_CERT_VRFY_A) 1728 { 1729 d=(unsigned char *)s->init_buf->data; 1730 p= &(d[4]); 1731 pkey=s->cert->key->privatekey; 1732 1733 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), 1734 &(data[MD5_DIGEST_LENGTH])); 1735 1736#ifndef OPENSSL_NO_RSA 1737 if (pkey->type == EVP_PKEY_RSA) 1738 { 1739 s->method->ssl3_enc->cert_verify_mac(s, 1740 &(s->s3->finish_dgst1),&(data[0])); 1741 if (RSA_sign(NID_md5_sha1, data, 1742 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, 1743 &(p[2]), &u, pkey->pkey.rsa) <= 0 ) 1744 { 1745 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); 1746 goto err; 1747 } 1748 s2n(u,p); 1749 n=u+2; 1750 } 1751 else 1752#endif 1753#ifndef OPENSSL_NO_DSA 1754 if (pkey->type == EVP_PKEY_DSA) 1755 { 1756 if (!DSA_sign(pkey->save_type, 1757 &(data[MD5_DIGEST_LENGTH]), 1758 SHA_DIGEST_LENGTH,&(p[2]), 1759 (unsigned int *)&j,pkey->pkey.dsa)) 1760 { 1761 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); 1762 goto err; 1763 } 1764 s2n(j,p); 1765 n=j+2; 1766 } 1767 else 1768#endif 1769 { 1770 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); 1771 goto err; 1772 } 1773 *(d++)=SSL3_MT_CERTIFICATE_VERIFY; 1774 l2n3(n,d); 1775 1776 s->state=SSL3_ST_CW_CERT_VRFY_B; 1777 s->init_num=(int)n+4; 1778 s->init_off=0; 1779 } 1780 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1781err: 1782 return(-1); 1783 } 1784 1785static int ssl3_send_client_certificate(SSL *s) 1786 { 1787 X509 *x509=NULL; 1788 EVP_PKEY *pkey=NULL; 1789 int i; 1790 unsigned long l; 1791 1792 if (s->state == SSL3_ST_CW_CERT_A) 1793 { 1794 if ((s->cert == NULL) || 1795 (s->cert->key->x509 == NULL) || 1796 (s->cert->key->privatekey == NULL)) 1797 s->state=SSL3_ST_CW_CERT_B; 1798 else 1799 s->state=SSL3_ST_CW_CERT_C; 1800 } 1801 1802 /* We need to get a client cert */ 1803 if (s->state == SSL3_ST_CW_CERT_B) 1804 { 1805 /* If we get an error, we need to 1806 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); 1807 * We then get retied later */ 1808 i=0; 1809 if (s->ctx->client_cert_cb != NULL) 1810 i=s->ctx->client_cert_cb(s,&(x509),&(pkey)); 1811 if (i < 0) 1812 { 1813 s->rwstate=SSL_X509_LOOKUP; 1814 return(-1); 1815 } 1816 s->rwstate=SSL_NOTHING; 1817 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) 1818 { 1819 s->state=SSL3_ST_CW_CERT_B; 1820 if ( !SSL_use_certificate(s,x509) || 1821 !SSL_use_PrivateKey(s,pkey)) 1822 i=0; 1823 } 1824 else if (i == 1) 1825 { 1826 i=0; 1827 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 1828 } 1829 1830 if (x509 != NULL) X509_free(x509); 1831 if (pkey != NULL) EVP_PKEY_free(pkey); 1832 if (i == 0) 1833 { 1834 if (s->version == SSL3_VERSION) 1835 { 1836 s->s3->tmp.cert_req=0; 1837 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); 1838 return(1); 1839 } 1840 else 1841 { 1842 s->s3->tmp.cert_req=2; 1843 } 1844 } 1845 1846 /* Ok, we have a cert */ 1847 s->state=SSL3_ST_CW_CERT_C; 1848 } 1849 1850 if (s->state == SSL3_ST_CW_CERT_C) 1851 { 1852 s->state=SSL3_ST_CW_CERT_D; 1853 l=ssl3_output_cert_chain(s, 1854 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); 1855 s->init_num=(int)l; 1856 s->init_off=0; 1857 } 1858 /* SSL3_ST_CW_CERT_D */ 1859 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1860 } 1861 1862#define has_bits(i,m) (((i)&(m)) == (m)) 1863 1864static int ssl3_check_cert_and_algorithm(SSL *s) 1865 { 1866 int i,idx; 1867 long algs; 1868 EVP_PKEY *pkey=NULL; 1869 SESS_CERT *sc; 1870#ifndef OPENSSL_NO_RSA 1871 RSA *rsa; 1872#endif 1873#ifndef OPENSSL_NO_DH 1874 DH *dh; 1875#endif 1876 1877 sc=s->session->sess_cert; 1878 1879 if (sc == NULL) 1880 { 1881 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,ERR_R_INTERNAL_ERROR); 1882 goto err; 1883 } 1884 1885 algs=s->s3->tmp.new_cipher->algorithms; 1886 1887 /* we don't have a certificate */ 1888 if (algs & (SSL_aDH|SSL_aNULL|SSL_aKRB5)) 1889 return(1); 1890 1891#ifndef OPENSSL_NO_RSA 1892 rsa=s->session->sess_cert->peer_rsa_tmp; 1893#endif 1894#ifndef OPENSSL_NO_DH 1895 dh=s->session->sess_cert->peer_dh_tmp; 1896#endif 1897 1898 /* This is the passed certificate */ 1899 1900 idx=sc->peer_cert_type; 1901 pkey=X509_get_pubkey(sc->peer_pkeys[idx].x509); 1902 i=X509_certificate_type(sc->peer_pkeys[idx].x509,pkey); 1903 EVP_PKEY_free(pkey); 1904 1905 1906 /* Check that we have a certificate if we require one */ 1907 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN)) 1908 { 1909 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT); 1910 goto f_err; 1911 } 1912#ifndef OPENSSL_NO_DSA 1913 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN)) 1914 { 1915 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT); 1916 goto f_err; 1917 } 1918#endif 1919#ifndef OPENSSL_NO_RSA 1920 if ((algs & SSL_kRSA) && 1921 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL))) 1922 { 1923 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT); 1924 goto f_err; 1925 } 1926#endif 1927#ifndef OPENSSL_NO_DH 1928 if ((algs & SSL_kEDH) && 1929 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL))) 1930 { 1931 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY); 1932 goto f_err; 1933 } 1934 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA)) 1935 { 1936 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT); 1937 goto f_err; 1938 } 1939#ifndef OPENSSL_NO_DSA 1940 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA)) 1941 { 1942 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT); 1943 goto f_err; 1944 } 1945#endif 1946#endif 1947 1948 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && !has_bits(i,EVP_PKT_EXP)) 1949 { 1950#ifndef OPENSSL_NO_RSA 1951 if (algs & SSL_kRSA) 1952 { 1953 if (rsa == NULL 1954 || RSA_size(rsa)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) 1955 { 1956 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY); 1957 goto f_err; 1958 } 1959 } 1960 else 1961#endif 1962#ifndef OPENSSL_NO_DH 1963 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1964 { 1965 if (dh == NULL 1966 || DH_size(dh)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) 1967 { 1968 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY); 1969 goto f_err; 1970 } 1971 } 1972 else 1973#endif 1974 { 1975 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1976 goto f_err; 1977 } 1978 } 1979 return(1); 1980f_err: 1981 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1982err: 1983 return(0); 1984 } 1985 1986