s3_srvr.c revision 89837
1/* ssl/s3_srvr.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58/* ==================================================================== 59 * Copyright (c) 1998-2001 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#define REUSE_CIPHER_BUG 113#define NETSCAPE_HANG_BUG 114 115 116#include <stdio.h> 117#include <openssl/buffer.h> 118#include <openssl/rand.h> 119#include <openssl/objects.h> 120#include <openssl/md5.h> 121#include <openssl/sha.h> 122#include <openssl/evp.h> 123#include <openssl/x509.h> 124#include "ssl_locl.h" 125 126static SSL_METHOD *ssl3_get_server_method(int ver); 127static int ssl3_get_client_hello(SSL *s); 128static int ssl3_check_client_hello(SSL *s); 129static int ssl3_send_server_hello(SSL *s); 130static int ssl3_send_server_key_exchange(SSL *s); 131static int ssl3_send_certificate_request(SSL *s); 132static int ssl3_send_server_done(SSL *s); 133static int ssl3_get_client_key_exchange(SSL *s); 134static int ssl3_get_client_certificate(SSL *s); 135static int ssl3_get_cert_verify(SSL *s); 136static int ssl3_send_hello_request(SSL *s); 137 138static SSL_METHOD *ssl3_get_server_method(int ver) 139 { 140 if (ver == SSL3_VERSION) 141 return(SSLv3_server_method()); 142 else 143 return(NULL); 144 } 145 146SSL_METHOD *SSLv3_server_method(void) 147 { 148 static int init=1; 149 static SSL_METHOD SSLv3_server_data; 150 151 if (init) 152 { 153 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(), 154 sizeof(SSL_METHOD)); 155 SSLv3_server_data.ssl_accept=ssl3_accept; 156 SSLv3_server_data.get_ssl_method=ssl3_get_server_method; 157 init=0; 158 } 159 return(&SSLv3_server_data); 160 } 161 162int ssl3_accept(SSL *s) 163 { 164 BUF_MEM *buf; 165 unsigned long l,Time=time(NULL); 166 void (*cb)()=NULL; 167 long num1; 168 int ret= -1; 169 int new_state,state,skip=0; 170 int got_new_session=0; 171 172 RAND_add(&Time,sizeof(Time),0); 173 ERR_clear_error(); 174 clear_sys_error(); 175 176 if (s->info_callback != NULL) 177 cb=s->info_callback; 178 else if (s->ctx->info_callback != NULL) 179 cb=s->ctx->info_callback; 180 181 /* init things to blank */ 182 s->in_handshake++; 183 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 184 185 if (s->cert == NULL) 186 { 187 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET); 188 return(-1); 189 } 190 191 for (;;) 192 { 193 state=s->state; 194 195 switch (s->state) 196 { 197 case SSL_ST_RENEGOTIATE: 198 s->new_session=1; 199 /* s->state=SSL_ST_ACCEPT; */ 200 201 case SSL_ST_BEFORE: 202 case SSL_ST_ACCEPT: 203 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 204 case SSL_ST_OK|SSL_ST_ACCEPT: 205 206 s->server=1; 207 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 208 209 if ((s->version>>8) != 3) 210 { 211 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR); 212 return -1; 213 } 214 s->type=SSL_ST_ACCEPT; 215 216 if (s->init_buf == NULL) 217 { 218 if ((buf=BUF_MEM_new()) == NULL) 219 { 220 ret= -1; 221 goto end; 222 } 223 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 224 { 225 ret= -1; 226 goto end; 227 } 228 s->init_buf=buf; 229 } 230 231 if (!ssl3_setup_buffers(s)) 232 { 233 ret= -1; 234 goto end; 235 } 236 237 s->init_num=0; 238 239 if (s->state != SSL_ST_RENEGOTIATE) 240 { 241 /* Ok, we now need to push on a buffering BIO so that 242 * the output is sent in a way that TCP likes :-) 243 */ 244 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } 245 246 ssl3_init_finished_mac(s); 247 s->state=SSL3_ST_SR_CLNT_HELLO_A; 248 s->ctx->stats.sess_accept++; 249 } 250 else 251 { 252 /* s->state == SSL_ST_RENEGOTIATE, 253 * we will just send a HelloRequest */ 254 s->ctx->stats.sess_accept_renegotiate++; 255 s->state=SSL3_ST_SW_HELLO_REQ_A; 256 } 257 break; 258 259 case SSL3_ST_SW_HELLO_REQ_A: 260 case SSL3_ST_SW_HELLO_REQ_B: 261 262 s->shutdown=0; 263 ret=ssl3_send_hello_request(s); 264 if (ret <= 0) goto end; 265 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; 266 s->state=SSL3_ST_SW_FLUSH; 267 s->init_num=0; 268 269 ssl3_init_finished_mac(s); 270 break; 271 272 case SSL3_ST_SW_HELLO_REQ_C: 273 s->state=SSL_ST_OK; 274 break; 275 276 case SSL3_ST_SR_CLNT_HELLO_A: 277 case SSL3_ST_SR_CLNT_HELLO_B: 278 case SSL3_ST_SR_CLNT_HELLO_C: 279 280 s->shutdown=0; 281 ret=ssl3_get_client_hello(s); 282 if (ret <= 0) goto end; 283 got_new_session=1; 284 s->state=SSL3_ST_SW_SRVR_HELLO_A; 285 s->init_num=0; 286 break; 287 288 case SSL3_ST_SW_SRVR_HELLO_A: 289 case SSL3_ST_SW_SRVR_HELLO_B: 290 ret=ssl3_send_server_hello(s); 291 if (ret <= 0) goto end; 292 293 if (s->hit) 294 s->state=SSL3_ST_SW_CHANGE_A; 295 else 296 s->state=SSL3_ST_SW_CERT_A; 297 s->init_num=0; 298 break; 299 300 case SSL3_ST_SW_CERT_A: 301 case SSL3_ST_SW_CERT_B: 302 /* Check if it is anon DH */ 303 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 304 { 305 ret=ssl3_send_server_certificate(s); 306 if (ret <= 0) goto end; 307 } 308 else 309 skip=1; 310 s->state=SSL3_ST_SW_KEY_EXCH_A; 311 s->init_num=0; 312 break; 313 314 case SSL3_ST_SW_KEY_EXCH_A: 315 case SSL3_ST_SW_KEY_EXCH_B: 316 l=s->s3->tmp.new_cipher->algorithms; 317 318 /* clear this, it may get reset by 319 * send_server_key_exchange */ 320 if (s->options & SSL_OP_EPHEMERAL_RSA) 321 s->s3->tmp.use_rsa_tmp=1; 322 else 323 s->s3->tmp.use_rsa_tmp=0; 324 325 /* only send if a DH key exchange, fortezza or 326 * RSA but we have a sign only certificate */ 327 if (s->s3->tmp.use_rsa_tmp 328 || (l & (SSL_DH|SSL_kFZA)) 329 || ((l & SSL_kRSA) 330 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL 331 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) 332 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) 333 ) 334 ) 335 ) 336 ) 337 { 338 ret=ssl3_send_server_key_exchange(s); 339 if (ret <= 0) goto end; 340 } 341 else 342 skip=1; 343 344 s->state=SSL3_ST_SW_CERT_REQ_A; 345 s->init_num=0; 346 break; 347 348 case SSL3_ST_SW_CERT_REQ_A: 349 case SSL3_ST_SW_CERT_REQ_B: 350 if (/* don't request cert unless asked for it: */ 351 !(s->verify_mode & SSL_VERIFY_PEER) || 352 /* if SSL_VERIFY_CLIENT_ONCE is set, 353 * don't request cert during re-negotiation: */ 354 ((s->session->peer != NULL) && 355 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 356 /* never request cert in anonymous ciphersuites 357 * (see section "Certificate request" in SSL 3 drafts 358 * and in RFC 2246): */ 359 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) && 360 /* ... except when the application insists on verification 361 * (against the specs, but s3_clnt.c accepts this for SSL 3) */ 362 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))) 363 { 364 /* no cert request */ 365 skip=1; 366 s->s3->tmp.cert_request=0; 367 s->state=SSL3_ST_SW_SRVR_DONE_A; 368 } 369 else 370 { 371 s->s3->tmp.cert_request=1; 372 ret=ssl3_send_certificate_request(s); 373 if (ret <= 0) goto end; 374#ifndef NETSCAPE_HANG_BUG 375 s->state=SSL3_ST_SW_SRVR_DONE_A; 376#else 377 s->state=SSL3_ST_SW_FLUSH; 378 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 379#endif 380 s->init_num=0; 381 } 382 break; 383 384 case SSL3_ST_SW_SRVR_DONE_A: 385 case SSL3_ST_SW_SRVR_DONE_B: 386 ret=ssl3_send_server_done(s); 387 if (ret <= 0) goto end; 388 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 389 s->state=SSL3_ST_SW_FLUSH; 390 s->init_num=0; 391 break; 392 393 case SSL3_ST_SW_FLUSH: 394 /* number of bytes to be flushed */ 395 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL); 396 if (num1 > 0) 397 { 398 s->rwstate=SSL_WRITING; 399 num1=BIO_flush(s->wbio); 400 if (num1 <= 0) { ret= -1; goto end; } 401 s->rwstate=SSL_NOTHING; 402 } 403 404 s->state=s->s3->tmp.next_state; 405 break; 406 407 case SSL3_ST_SR_CERT_A: 408 case SSL3_ST_SR_CERT_B: 409 /* Check for second client hello (MS SGC) */ 410 ret = ssl3_check_client_hello(s); 411 if (ret <= 0) 412 goto end; 413 if (ret == 2) 414 s->state = SSL3_ST_SR_CLNT_HELLO_C; 415 else { 416 /* could be sent for a DH cert, even if we 417 * have not asked for it :-) */ 418 ret=ssl3_get_client_certificate(s); 419 if (ret <= 0) goto end; 420 s->init_num=0; 421 s->state=SSL3_ST_SR_KEY_EXCH_A; 422 } 423 break; 424 425 case SSL3_ST_SR_KEY_EXCH_A: 426 case SSL3_ST_SR_KEY_EXCH_B: 427 ret=ssl3_get_client_key_exchange(s); 428 if (ret <= 0) goto end; 429 s->state=SSL3_ST_SR_CERT_VRFY_A; 430 s->init_num=0; 431 432 /* We need to get hashes here so if there is 433 * a client cert, it can be verified */ 434 s->method->ssl3_enc->cert_verify_mac(s, 435 &(s->s3->finish_dgst1), 436 &(s->s3->tmp.cert_verify_md[0])); 437 s->method->ssl3_enc->cert_verify_mac(s, 438 &(s->s3->finish_dgst2), 439 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); 440 441 break; 442 443 case SSL3_ST_SR_CERT_VRFY_A: 444 case SSL3_ST_SR_CERT_VRFY_B: 445 446 /* we should decide if we expected this one */ 447 ret=ssl3_get_cert_verify(s); 448 if (ret <= 0) goto end; 449 450 s->state=SSL3_ST_SR_FINISHED_A; 451 s->init_num=0; 452 break; 453 454 case SSL3_ST_SR_FINISHED_A: 455 case SSL3_ST_SR_FINISHED_B: 456 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, 457 SSL3_ST_SR_FINISHED_B); 458 if (ret <= 0) goto end; 459 if (s->hit) 460 s->state=SSL_ST_OK; 461 else 462 s->state=SSL3_ST_SW_CHANGE_A; 463 s->init_num=0; 464 break; 465 466 case SSL3_ST_SW_CHANGE_A: 467 case SSL3_ST_SW_CHANGE_B: 468 469 s->session->cipher=s->s3->tmp.new_cipher; 470 if (!s->method->ssl3_enc->setup_key_block(s)) 471 { ret= -1; goto end; } 472 473 ret=ssl3_send_change_cipher_spec(s, 474 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); 475 476 if (ret <= 0) goto end; 477 s->state=SSL3_ST_SW_FINISHED_A; 478 s->init_num=0; 479 480 if (!s->method->ssl3_enc->change_cipher_state(s, 481 SSL3_CHANGE_CIPHER_SERVER_WRITE)) 482 { 483 ret= -1; 484 goto end; 485 } 486 487 break; 488 489 case SSL3_ST_SW_FINISHED_A: 490 case SSL3_ST_SW_FINISHED_B: 491 ret=ssl3_send_finished(s, 492 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, 493 s->method->ssl3_enc->server_finished_label, 494 s->method->ssl3_enc->server_finished_label_len); 495 if (ret <= 0) goto end; 496 s->state=SSL3_ST_SW_FLUSH; 497 if (s->hit) 498 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 499 else 500 s->s3->tmp.next_state=SSL_ST_OK; 501 s->init_num=0; 502 break; 503 504 case SSL_ST_OK: 505 /* clean a few things up */ 506 ssl3_cleanup_key_block(s); 507 508 BUF_MEM_free(s->init_buf); 509 s->init_buf=NULL; 510 511 /* remove buffering on output */ 512 ssl_free_wbio_buffer(s); 513 514 s->init_num=0; 515 516 if (got_new_session) /* skipped if we just sent a HelloRequest */ 517 { 518 /* actually not necessarily a 'new' session */ 519 520 s->new_session=0; 521 522 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); 523 524 s->ctx->stats.sess_accept_good++; 525 /* s->server=1; */ 526 s->handshake_func=ssl3_accept; 527 528 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 529 } 530 531 ret = 1; 532 goto end; 533 /* break; */ 534 535 default: 536 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE); 537 ret= -1; 538 goto end; 539 /* break; */ 540 } 541 542 if (!s->s3->tmp.reuse_message && !skip) 543 { 544 if (s->debug) 545 { 546 if ((ret=BIO_flush(s->wbio)) <= 0) 547 goto end; 548 } 549 550 551 if ((cb != NULL) && (s->state != state)) 552 { 553 new_state=s->state; 554 s->state=state; 555 cb(s,SSL_CB_ACCEPT_LOOP,1); 556 s->state=new_state; 557 } 558 } 559 skip=0; 560 } 561end: 562 /* BIO_flush(s->wbio); */ 563 564 s->in_handshake--; 565 if (cb != NULL) 566 cb(s,SSL_CB_ACCEPT_EXIT,ret); 567 return(ret); 568 } 569 570static int ssl3_send_hello_request(SSL *s) 571 { 572 unsigned char *p; 573 574 if (s->state == SSL3_ST_SW_HELLO_REQ_A) 575 { 576 p=(unsigned char *)s->init_buf->data; 577 *(p++)=SSL3_MT_HELLO_REQUEST; 578 *(p++)=0; 579 *(p++)=0; 580 *(p++)=0; 581 582 s->state=SSL3_ST_SW_HELLO_REQ_B; 583 /* number of bytes to write */ 584 s->init_num=4; 585 s->init_off=0; 586 } 587 588 /* SSL3_ST_SW_HELLO_REQ_B */ 589 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 590 } 591 592static int ssl3_check_client_hello(SSL *s) 593 { 594 int ok; 595 long n; 596 597 /* this function is called when we really expect a Certificate message, 598 * so permit appropriate message length */ 599 n=ssl3_get_message(s, 600 SSL3_ST_SR_CERT_A, 601 SSL3_ST_SR_CERT_B, 602 -1, 603#if defined(MSDOS) && !defined(WIN32) 604 1024*30, /* 30k max cert list :-) */ 605#else 606 1024*100, /* 100k max cert list :-) */ 607#endif 608 &ok); 609 if (!ok) return((int)n); 610 s->s3->tmp.reuse_message = 1; 611 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) 612 { 613 /* Throw away what we have done so far in the current handshake, 614 * which will now be aborted. (A full SSL_clear would be too much.) 615 * I hope that tmp.dh is the only thing that may need to be cleared 616 * when a handshake is not completed ... */ 617#ifndef NO_DH 618 if (s->s3->tmp.dh != NULL) 619 { 620 DH_free(s->s3->tmp.dh); 621 s->s3->tmp.dh = NULL; 622 } 623#endif 624 return 2; 625 } 626 return 1; 627} 628 629static int ssl3_get_client_hello(SSL *s) 630 { 631 int i,j,ok,al,ret= -1; 632 long n; 633 unsigned long id; 634 unsigned char *p,*d,*q; 635 SSL_CIPHER *c; 636 SSL_COMP *comp=NULL; 637 STACK_OF(SSL_CIPHER) *ciphers=NULL; 638 639 /* We do this so that we will respond with our native type. 640 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1, 641 * This down switching should be handled by a different method. 642 * If we are SSLv3, we will respond with SSLv3, even if prompted with 643 * TLSv1. 644 */ 645 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) 646 { 647 s->first_packet=1; 648 s->state=SSL3_ST_SR_CLNT_HELLO_B; 649 } 650 n=ssl3_get_message(s, 651 SSL3_ST_SR_CLNT_HELLO_B, 652 SSL3_ST_SR_CLNT_HELLO_C, 653 SSL3_MT_CLIENT_HELLO, 654 SSL3_RT_MAX_PLAIN_LENGTH, 655 &ok); 656 657 if (!ok) return((int)n); 658 d=p=(unsigned char *)s->init_buf->data; 659 660 /* use version from inside client hello, not from record header 661 * (may differ: see RFC 2246, Appendix E, second paragraph) */ 662 s->client_version=(((int)p[0])<<8)|(int)p[1]; 663 p+=2; 664 665 if (s->client_version < s->version) 666 { 667 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER); 668 if ((s->client_version>>8) == SSL3_VERSION_MAJOR) 669 { 670 /* similar to ssl3_get_record, send alert using remote version number */ 671 s->version = s->client_version; 672 } 673 al = SSL_AD_PROTOCOL_VERSION; 674 goto f_err; 675 } 676 677 /* load the client random */ 678 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE); 679 p+=SSL3_RANDOM_SIZE; 680 681 /* get the session-id */ 682 j= *(p++); 683 684 s->hit=0; 685 if (j == 0) 686 { 687 if (!ssl_get_new_session(s,1)) 688 goto err; 689 } 690 else 691 { 692 i=ssl_get_prev_session(s,p,j); 693 if (i == 1) 694 { /* previous session */ 695 s->hit=1; 696 } 697 else if (i == -1) 698 goto err; 699 else /* i == 0 */ 700 { 701 if (!ssl_get_new_session(s,1)) 702 goto err; 703 } 704 } 705 706 p+=j; 707 n2s(p,i); 708 if ((i == 0) && (j != 0)) 709 { 710 /* we need a cipher if we are not resuming a session */ 711 al=SSL_AD_ILLEGAL_PARAMETER; 712 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED); 713 goto f_err; 714 } 715 if ((i+p) > (d+n)) 716 { 717 /* not enough data */ 718 al=SSL_AD_DECODE_ERROR; 719 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 720 goto f_err; 721 } 722 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers)) 723 == NULL)) 724 { 725 goto err; 726 } 727 p+=i; 728 729 /* If it is a hit, check that the cipher is in the list */ 730 if ((s->hit) && (i > 0)) 731 { 732 j=0; 733 id=s->session->cipher->id; 734 735#ifdef CIPHER_DEBUG 736 printf("client sent %d ciphers\n",sk_num(ciphers)); 737#endif 738 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++) 739 { 740 c=sk_SSL_CIPHER_value(ciphers,i); 741#ifdef CIPHER_DEBUG 742 printf("client [%2d of %2d]:%s\n", 743 i,sk_num(ciphers),SSL_CIPHER_get_name(c)); 744#endif 745 if (c->id == id) 746 { 747 j=1; 748 break; 749 } 750 } 751 if (j == 0) 752 { 753 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1)) 754 { 755 /* Very bad for multi-threading.... */ 756 s->session->cipher=sk_SSL_CIPHER_value(ciphers, 757 0); 758 } 759 else 760 { 761 /* we need to have the cipher in the cipher 762 * list if we are asked to reuse it */ 763 al=SSL_AD_ILLEGAL_PARAMETER; 764 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING); 765 goto f_err; 766 } 767 } 768 } 769 770 /* compression */ 771 i= *(p++); 772 q=p; 773 for (j=0; j<i; j++) 774 { 775 if (p[j] == 0) break; 776 } 777 778 p+=i; 779 if (j >= i) 780 { 781 /* no compress */ 782 al=SSL_AD_DECODE_ERROR; 783 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); 784 goto f_err; 785 } 786 787 /* Worst case, we will use the NULL compression, but if we have other 788 * options, we will now look for them. We have i-1 compression 789 * algorithms from the client, starting at q. */ 790 s->s3->tmp.new_compression=NULL; 791 if (s->ctx->comp_methods != NULL) 792 { /* See if we have a match */ 793 int m,nn,o,v,done=0; 794 795 nn=sk_SSL_COMP_num(s->ctx->comp_methods); 796 for (m=0; m<nn; m++) 797 { 798 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); 799 v=comp->id; 800 for (o=0; o<i; o++) 801 { 802 if (v == q[o]) 803 { 804 done=1; 805 break; 806 } 807 } 808 if (done) break; 809 } 810 if (done) 811 s->s3->tmp.new_compression=comp; 812 else 813 comp=NULL; 814 } 815 816 /* TLS does not mind if there is extra stuff */ 817 if (s->version == SSL3_VERSION) 818 { 819 if (p > (d+n)) 820 { 821 /* wrong number of bytes, 822 * there could be more to follow */ 823 al=SSL_AD_DECODE_ERROR; 824 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 825 goto f_err; 826 } 827 } 828 829 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must 830 * pick a cipher */ 831 832 if (!s->hit) 833 { 834 s->session->compress_meth=(comp == NULL)?0:comp->id; 835 if (s->session->ciphers != NULL) 836 sk_SSL_CIPHER_free(s->session->ciphers); 837 s->session->ciphers=ciphers; 838 if (ciphers == NULL) 839 { 840 al=SSL_AD_ILLEGAL_PARAMETER; 841 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); 842 goto f_err; 843 } 844 ciphers=NULL; 845 c=ssl3_choose_cipher(s,s->session->ciphers, 846 ssl_get_ciphers_by_id(s)); 847 848 if (c == NULL) 849 { 850 al=SSL_AD_HANDSHAKE_FAILURE; 851 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 852 goto f_err; 853 } 854 s->s3->tmp.new_cipher=c; 855 } 856 else 857 { 858 /* Session-id reuse */ 859#ifdef REUSE_CIPHER_BUG 860 STACK_OF(SSL_CIPHER) *sk; 861 SSL_CIPHER *nc=NULL; 862 SSL_CIPHER *ec=NULL; 863 864 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) 865 { 866 sk=s->session->ciphers; 867 for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 868 { 869 c=sk_SSL_CIPHER_value(sk,i); 870 if (c->algorithms & SSL_eNULL) 871 nc=c; 872 if (SSL_C_IS_EXPORT(c)) 873 ec=c; 874 } 875 if (nc != NULL) 876 s->s3->tmp.new_cipher=nc; 877 else if (ec != NULL) 878 s->s3->tmp.new_cipher=ec; 879 else 880 s->s3->tmp.new_cipher=s->session->cipher; 881 } 882 else 883#endif 884 s->s3->tmp.new_cipher=s->session->cipher; 885 } 886 887 /* we now have the following setup. 888 * client_random 889 * cipher_list - our prefered list of ciphers 890 * ciphers - the clients prefered list of ciphers 891 * compression - basically ignored right now 892 * ssl version is set - sslv3 893 * s->session - The ssl session has been setup. 894 * s->hit - session reuse flag 895 * s->tmp.new_cipher - the new cipher to use. 896 */ 897 898 ret=1; 899 if (0) 900 { 901f_err: 902 ssl3_send_alert(s,SSL3_AL_FATAL,al); 903 } 904err: 905 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); 906 return(ret); 907 } 908 909static int ssl3_send_server_hello(SSL *s) 910 { 911 unsigned char *buf; 912 unsigned char *p,*d; 913 int i,sl; 914 unsigned long l,Time; 915 916 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 917 { 918 buf=(unsigned char *)s->init_buf->data; 919 p=s->s3->server_random; 920 Time=time(NULL); /* Time */ 921 l2n(Time,p); 922 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); 923 /* Do the message type and length last */ 924 d=p= &(buf[4]); 925 926 *(p++)=s->version>>8; 927 *(p++)=s->version&0xff; 928 929 /* Random stuff */ 930 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 931 p+=SSL3_RANDOM_SIZE; 932 933 /* now in theory we have 3 options to sending back the 934 * session id. If it is a re-use, we send back the 935 * old session-id, if it is a new session, we send 936 * back the new session-id or we send back a 0 length 937 * session-id if we want it to be single use. 938 * Currently I will not implement the '0' length session-id 939 * 12-Jan-98 - I'll now support the '0' length stuff. 940 */ 941 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) 942 s->session->session_id_length=0; 943 944 sl=s->session->session_id_length; 945 *(p++)=sl; 946 memcpy(p,s->session->session_id,sl); 947 p+=sl; 948 949 /* put the cipher */ 950 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 951 p+=i; 952 953 /* put the compression method */ 954 if (s->s3->tmp.new_compression == NULL) 955 *(p++)=0; 956 else 957 *(p++)=s->s3->tmp.new_compression->id; 958 959 /* do the header */ 960 l=(p-d); 961 d=buf; 962 *(d++)=SSL3_MT_SERVER_HELLO; 963 l2n3(l,d); 964 965 s->state=SSL3_ST_CW_CLNT_HELLO_B; 966 /* number of bytes to write */ 967 s->init_num=p-buf; 968 s->init_off=0; 969 } 970 971 /* SSL3_ST_CW_CLNT_HELLO_B */ 972 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 973 } 974 975static int ssl3_send_server_done(SSL *s) 976 { 977 unsigned char *p; 978 979 if (s->state == SSL3_ST_SW_SRVR_DONE_A) 980 { 981 p=(unsigned char *)s->init_buf->data; 982 983 /* do the header */ 984 *(p++)=SSL3_MT_SERVER_DONE; 985 *(p++)=0; 986 *(p++)=0; 987 *(p++)=0; 988 989 s->state=SSL3_ST_SW_SRVR_DONE_B; 990 /* number of bytes to write */ 991 s->init_num=4; 992 s->init_off=0; 993 } 994 995 /* SSL3_ST_CW_CLNT_HELLO_B */ 996 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 997 } 998 999static int ssl3_send_server_key_exchange(SSL *s) 1000 { 1001#ifndef NO_RSA 1002 unsigned char *q; 1003 int j,num; 1004 RSA *rsa; 1005 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1006 unsigned int u; 1007#endif 1008#ifndef NO_DH 1009 DH *dh=NULL,*dhp; 1010#endif 1011 EVP_PKEY *pkey; 1012 unsigned char *p,*d; 1013 int al,i; 1014 unsigned long type; 1015 int n; 1016 CERT *cert; 1017 BIGNUM *r[4]; 1018 int nr[4],kn; 1019 BUF_MEM *buf; 1020 EVP_MD_CTX md_ctx; 1021 1022 if (s->state == SSL3_ST_SW_KEY_EXCH_A) 1023 { 1024 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; 1025 cert=s->cert; 1026 1027 buf=s->init_buf; 1028 1029 r[0]=r[1]=r[2]=r[3]=NULL; 1030 n=0; 1031#ifndef NO_RSA 1032 if (type & SSL_kRSA) 1033 { 1034 rsa=cert->rsa_tmp; 1035 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 1036 { 1037 rsa=s->cert->rsa_tmp_cb(s, 1038 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1039 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1040 if(rsa == NULL) 1041 { 1042 al=SSL_AD_HANDSHAKE_FAILURE; 1043 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 1044 goto f_err; 1045 } 1046 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA); 1047 cert->rsa_tmp=rsa; 1048 } 1049 if (rsa == NULL) 1050 { 1051 al=SSL_AD_HANDSHAKE_FAILURE; 1052 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 1053 goto f_err; 1054 } 1055 r[0]=rsa->n; 1056 r[1]=rsa->e; 1057 s->s3->tmp.use_rsa_tmp=1; 1058 } 1059 else 1060#endif 1061#ifndef NO_DH 1062 if (type & SSL_kEDH) 1063 { 1064 dhp=cert->dh_tmp; 1065 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 1066 dhp=s->cert->dh_tmp_cb(s, 1067 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1068 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1069 if (dhp == NULL) 1070 { 1071 al=SSL_AD_HANDSHAKE_FAILURE; 1072 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1073 goto f_err; 1074 } 1075 1076 if (s->s3->tmp.dh != NULL) 1077 { 1078 DH_free(dh); 1079 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR); 1080 goto err; 1081 } 1082 1083 if ((dh=DHparams_dup(dhp)) == NULL) 1084 { 1085 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1086 goto err; 1087 } 1088 1089 s->s3->tmp.dh=dh; 1090 if ((dhp->pub_key == NULL || 1091 dhp->priv_key == NULL || 1092 (s->options & SSL_OP_SINGLE_DH_USE))) 1093 { 1094 if(!DH_generate_key(dh)) 1095 { 1096 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, 1097 ERR_R_DH_LIB); 1098 goto err; 1099 } 1100 } 1101 else 1102 { 1103 dh->pub_key=BN_dup(dhp->pub_key); 1104 dh->priv_key=BN_dup(dhp->priv_key); 1105 if ((dh->pub_key == NULL) || 1106 (dh->priv_key == NULL)) 1107 { 1108 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1109 goto err; 1110 } 1111 } 1112 r[0]=dh->p; 1113 r[1]=dh->g; 1114 r[2]=dh->pub_key; 1115 } 1116 else 1117#endif 1118 { 1119 al=SSL_AD_HANDSHAKE_FAILURE; 1120 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1121 goto f_err; 1122 } 1123 for (i=0; r[i] != NULL; i++) 1124 { 1125 nr[i]=BN_num_bytes(r[i]); 1126 n+=2+nr[i]; 1127 } 1128 1129 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 1130 { 1131 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) 1132 == NULL) 1133 { 1134 al=SSL_AD_DECODE_ERROR; 1135 goto f_err; 1136 } 1137 kn=EVP_PKEY_size(pkey); 1138 } 1139 else 1140 { 1141 pkey=NULL; 1142 kn=0; 1143 } 1144 1145 if (!BUF_MEM_grow(buf,n+4+kn)) 1146 { 1147 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 1148 goto err; 1149 } 1150 d=(unsigned char *)s->init_buf->data; 1151 p= &(d[4]); 1152 1153 for (i=0; r[i] != NULL; i++) 1154 { 1155 s2n(nr[i],p); 1156 BN_bn2bin(r[i],p); 1157 p+=nr[i]; 1158 } 1159 1160 /* not anonymous */ 1161 if (pkey != NULL) 1162 { 1163 /* n is the length of the params, they start at &(d[4]) 1164 * and p points to the space at the end. */ 1165#ifndef NO_RSA 1166 if (pkey->type == EVP_PKEY_RSA) 1167 { 1168 q=md_buf; 1169 j=0; 1170 for (num=2; num > 0; num--) 1171 { 1172 EVP_DigestInit(&md_ctx,(num == 2) 1173 ?s->ctx->md5:s->ctx->sha1); 1174 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1175 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1176 EVP_DigestUpdate(&md_ctx,&(d[4]),n); 1177 EVP_DigestFinal(&md_ctx,q, 1178 (unsigned int *)&i); 1179 q+=i; 1180 j+=i; 1181 } 1182 if (RSA_sign(NID_md5_sha1, md_buf, j, 1183 &(p[2]), &u, pkey->pkey.rsa) <= 0) 1184 { 1185 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 1186 goto err; 1187 } 1188 s2n(u,p); 1189 n+=u+2; 1190 } 1191 else 1192#endif 1193#if !defined(NO_DSA) 1194 if (pkey->type == EVP_PKEY_DSA) 1195 { 1196 /* lets do DSS */ 1197 EVP_SignInit(&md_ctx,EVP_dss1()); 1198 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1199 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1200 EVP_SignUpdate(&md_ctx,&(d[4]),n); 1201 if (!EVP_SignFinal(&md_ctx,&(p[2]), 1202 (unsigned int *)&i,pkey)) 1203 { 1204 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); 1205 goto err; 1206 } 1207 s2n(i,p); 1208 n+=i+2; 1209 } 1210 else 1211#endif 1212 { 1213 /* Is this error check actually needed? */ 1214 al=SSL_AD_HANDSHAKE_FAILURE; 1215 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 1216 goto f_err; 1217 } 1218 } 1219 1220 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; 1221 l2n3(n,d); 1222 1223 /* we should now have things packed up, so lets send 1224 * it off */ 1225 s->init_num=n+4; 1226 s->init_off=0; 1227 } 1228 1229 s->state = SSL3_ST_SW_KEY_EXCH_B; 1230 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1231f_err: 1232 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1233err: 1234 return(-1); 1235 } 1236 1237static int ssl3_send_certificate_request(SSL *s) 1238 { 1239 unsigned char *p,*d; 1240 int i,j,nl,off,n; 1241 STACK_OF(X509_NAME) *sk=NULL; 1242 X509_NAME *name; 1243 BUF_MEM *buf; 1244 1245 if (s->state == SSL3_ST_SW_CERT_REQ_A) 1246 { 1247 buf=s->init_buf; 1248 1249 d=p=(unsigned char *)&(buf->data[4]); 1250 1251 /* get the list of acceptable cert types */ 1252 p++; 1253 n=ssl3_get_req_cert_type(s,p); 1254 d[0]=n; 1255 p+=n; 1256 n++; 1257 1258 off=n; 1259 p+=2; 1260 n+=2; 1261 1262 sk=SSL_get_client_CA_list(s); 1263 nl=0; 1264 if (sk != NULL) 1265 { 1266 for (i=0; i<sk_X509_NAME_num(sk); i++) 1267 { 1268 name=sk_X509_NAME_value(sk,i); 1269 j=i2d_X509_NAME(name,NULL); 1270 if (!BUF_MEM_grow(buf,4+n+j+2)) 1271 { 1272 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 1273 goto err; 1274 } 1275 p=(unsigned char *)&(buf->data[4+n]); 1276 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1277 { 1278 s2n(j,p); 1279 i2d_X509_NAME(name,&p); 1280 n+=2+j; 1281 nl+=2+j; 1282 } 1283 else 1284 { 1285 d=p; 1286 i2d_X509_NAME(name,&p); 1287 j-=2; s2n(j,d); j+=2; 1288 n+=j; 1289 nl+=j; 1290 } 1291 } 1292 } 1293 /* else no CA names */ 1294 p=(unsigned char *)&(buf->data[4+off]); 1295 s2n(nl,p); 1296 1297 d=(unsigned char *)buf->data; 1298 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 1299 l2n3(n,d); 1300 1301 /* we should now have things packed up, so lets send 1302 * it off */ 1303 1304 s->init_num=n+4; 1305 s->init_off=0; 1306#ifdef NETSCAPE_HANG_BUG 1307 p=(unsigned char *)s->init_buf->data + s->init_num; 1308 1309 /* do the header */ 1310 *(p++)=SSL3_MT_SERVER_DONE; 1311 *(p++)=0; 1312 *(p++)=0; 1313 *(p++)=0; 1314 s->init_num += 4; 1315#endif 1316 1317 } 1318 1319 /* SSL3_ST_SW_CERT_REQ_B */ 1320 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1321err: 1322 return(-1); 1323 } 1324 1325static int ssl3_get_client_key_exchange(SSL *s) 1326 { 1327 int i,al,ok; 1328 long n; 1329 unsigned long l; 1330 unsigned char *p; 1331#ifndef NO_RSA 1332 RSA *rsa=NULL; 1333 EVP_PKEY *pkey=NULL; 1334#endif 1335#ifndef NO_DH 1336 BIGNUM *pub=NULL; 1337 DH *dh_srvr; 1338#endif 1339 1340 n=ssl3_get_message(s, 1341 SSL3_ST_SR_KEY_EXCH_A, 1342 SSL3_ST_SR_KEY_EXCH_B, 1343 SSL3_MT_CLIENT_KEY_EXCHANGE, 1344 2048, /* ???? */ 1345 &ok); 1346 1347 if (!ok) return((int)n); 1348 p=(unsigned char *)s->init_buf->data; 1349 1350 l=s->s3->tmp.new_cipher->algorithms; 1351 1352#ifndef NO_RSA 1353 if (l & SSL_kRSA) 1354 { 1355 /* FIX THIS UP EAY EAY EAY EAY */ 1356 if (s->s3->tmp.use_rsa_tmp) 1357 { 1358 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) 1359 rsa=s->cert->rsa_tmp; 1360 /* Don't do a callback because rsa_tmp should 1361 * be sent already */ 1362 if (rsa == NULL) 1363 { 1364 al=SSL_AD_HANDSHAKE_FAILURE; 1365 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); 1366 goto f_err; 1367 1368 } 1369 } 1370 else 1371 { 1372 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; 1373 if ( (pkey == NULL) || 1374 (pkey->type != EVP_PKEY_RSA) || 1375 (pkey->pkey.rsa == NULL)) 1376 { 1377 al=SSL_AD_HANDSHAKE_FAILURE; 1378 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); 1379 goto f_err; 1380 } 1381 rsa=pkey->pkey.rsa; 1382 } 1383 1384 /* TLS */ 1385 if (s->version > SSL3_VERSION) 1386 { 1387 n2s(p,i); 1388 if (n != i+2) 1389 { 1390 if (!(s->options & SSL_OP_TLS_D5_BUG)) 1391 { 1392 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 1393 goto err; 1394 } 1395 else 1396 p-=2; 1397 } 1398 else 1399 n=i; 1400 } 1401 1402 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); 1403 1404 al = -1; 1405 1406 if (i != SSL_MAX_MASTER_KEY_LENGTH) 1407 { 1408 al=SSL_AD_DECODE_ERROR; 1409 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); 1410 } 1411 1412 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff)))) 1413 { 1414 /* The premaster secret must contain the same version number as the 1415 * ClientHello to detect version rollback attacks (strangely, the 1416 * protocol does not offer such protection for DH ciphersuites). 1417 * However, buggy clients exist that send the negotiated protocol 1418 * version instead if the server does not support the requested 1419 * protocol version. 1420 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ 1421 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && 1422 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff)))) 1423 { 1424 al=SSL_AD_DECODE_ERROR; 1425 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); 1426 goto f_err; 1427 } 1428 } 1429 1430 if (al != -1) 1431 { 1432#if 0 1433 goto f_err; 1434#else 1435 /* Some decryption failure -- use random value instead as countermeasure 1436 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding 1437 * (see RFC 2246, section 7.4.7.1). 1438 * But note that due to length and protocol version checking, the 1439 * attack is impractical anyway (see section 5 in D. Bleichenbacher: 1440 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA 1441 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12). 1442 */ 1443 ERR_clear_error(); 1444 i = SSL_MAX_MASTER_KEY_LENGTH; 1445 p[0] = s->client_version >> 8; 1446 p[1] = s->client_version & 0xff; 1447 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */ 1448#endif 1449 } 1450 1451 s->session->master_key_length= 1452 s->method->ssl3_enc->generate_master_secret(s, 1453 s->session->master_key, 1454 p,i); 1455 memset(p,0,i); 1456 } 1457 else 1458#endif 1459#ifndef NO_DH 1460 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1461 { 1462 n2s(p,i); 1463 if (n != i+2) 1464 { 1465 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) 1466 { 1467 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); 1468 goto err; 1469 } 1470 else 1471 { 1472 p-=2; 1473 i=(int)n; 1474 } 1475 } 1476 1477 if (n == 0L) /* the parameters are in the cert */ 1478 { 1479 al=SSL_AD_HANDSHAKE_FAILURE; 1480 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); 1481 goto f_err; 1482 } 1483 else 1484 { 1485 if (s->s3->tmp.dh == NULL) 1486 { 1487 al=SSL_AD_HANDSHAKE_FAILURE; 1488 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1489 goto f_err; 1490 } 1491 else 1492 dh_srvr=s->s3->tmp.dh; 1493 } 1494 1495 pub=BN_bin2bn(p,i,NULL); 1496 if (pub == NULL) 1497 { 1498 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); 1499 goto err; 1500 } 1501 1502 i=DH_compute_key(p,pub,dh_srvr); 1503 1504 if (i <= 0) 1505 { 1506 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1507 goto err; 1508 } 1509 1510 DH_free(s->s3->tmp.dh); 1511 s->s3->tmp.dh=NULL; 1512 1513 BN_clear_free(pub); 1514 pub=NULL; 1515 s->session->master_key_length= 1516 s->method->ssl3_enc->generate_master_secret(s, 1517 s->session->master_key,p,i); 1518 memset(p,0,i); 1519 } 1520 else 1521#endif 1522 { 1523 al=SSL_AD_HANDSHAKE_FAILURE; 1524 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE); 1525 goto f_err; 1526 } 1527 1528 return(1); 1529f_err: 1530 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1531#if !defined(NO_DH) || !defined(NO_RSA) 1532err: 1533#endif 1534 return(-1); 1535 } 1536 1537static int ssl3_get_cert_verify(SSL *s) 1538 { 1539 EVP_PKEY *pkey=NULL; 1540 unsigned char *p; 1541 int al,ok,ret=0; 1542 long n; 1543 int type=0,i,j; 1544 X509 *peer; 1545 1546 n=ssl3_get_message(s, 1547 SSL3_ST_SR_CERT_VRFY_A, 1548 SSL3_ST_SR_CERT_VRFY_B, 1549 -1, 1550 512, /* 512? */ 1551 &ok); 1552 1553 if (!ok) return((int)n); 1554 1555 if (s->session->peer != NULL) 1556 { 1557 peer=s->session->peer; 1558 pkey=X509_get_pubkey(peer); 1559 type=X509_certificate_type(peer,pkey); 1560 } 1561 else 1562 { 1563 peer=NULL; 1564 pkey=NULL; 1565 } 1566 1567 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) 1568 { 1569 s->s3->tmp.reuse_message=1; 1570 if ((peer != NULL) && (type | EVP_PKT_SIGN)) 1571 { 1572 al=SSL_AD_UNEXPECTED_MESSAGE; 1573 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); 1574 goto f_err; 1575 } 1576 ret=1; 1577 goto end; 1578 } 1579 1580 if (peer == NULL) 1581 { 1582 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); 1583 al=SSL_AD_UNEXPECTED_MESSAGE; 1584 goto f_err; 1585 } 1586 1587 if (!(type & EVP_PKT_SIGN)) 1588 { 1589 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 1590 al=SSL_AD_ILLEGAL_PARAMETER; 1591 goto f_err; 1592 } 1593 1594 if (s->s3->change_cipher_spec) 1595 { 1596 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); 1597 al=SSL_AD_UNEXPECTED_MESSAGE; 1598 goto f_err; 1599 } 1600 1601 /* we now have a signature that we need to verify */ 1602 p=(unsigned char *)s->init_buf->data; 1603 n2s(p,i); 1604 n-=2; 1605 if (i > n) 1606 { 1607 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); 1608 al=SSL_AD_DECODE_ERROR; 1609 goto f_err; 1610 } 1611 1612 j=EVP_PKEY_size(pkey); 1613 if ((i > j) || (n > j) || (n <= 0)) 1614 { 1615 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); 1616 al=SSL_AD_DECODE_ERROR; 1617 goto f_err; 1618 } 1619 1620#ifndef NO_RSA 1621 if (pkey->type == EVP_PKEY_RSA) 1622 { 1623 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, 1624 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 1625 pkey->pkey.rsa); 1626 if (i < 0) 1627 { 1628 al=SSL_AD_DECRYPT_ERROR; 1629 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); 1630 goto f_err; 1631 } 1632 if (i == 0) 1633 { 1634 al=SSL_AD_DECRYPT_ERROR; 1635 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); 1636 goto f_err; 1637 } 1638 } 1639 else 1640#endif 1641#ifndef NO_DSA 1642 if (pkey->type == EVP_PKEY_DSA) 1643 { 1644 j=DSA_verify(pkey->save_type, 1645 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 1646 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); 1647 if (j <= 0) 1648 { 1649 /* bad signature */ 1650 al=SSL_AD_DECRYPT_ERROR; 1651 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); 1652 goto f_err; 1653 } 1654 } 1655 else 1656#endif 1657 { 1658 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR); 1659 al=SSL_AD_UNSUPPORTED_CERTIFICATE; 1660 goto f_err; 1661 } 1662 1663 1664 ret=1; 1665 if (0) 1666 { 1667f_err: 1668 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1669 } 1670end: 1671 EVP_PKEY_free(pkey); 1672 return(ret); 1673 } 1674 1675static int ssl3_get_client_certificate(SSL *s) 1676 { 1677 int i,ok,al,ret= -1; 1678 X509 *x=NULL; 1679 unsigned long l,nc,llen,n; 1680 unsigned char *p,*d,*q; 1681 STACK_OF(X509) *sk=NULL; 1682 1683 n=ssl3_get_message(s, 1684 SSL3_ST_SR_CERT_A, 1685 SSL3_ST_SR_CERT_B, 1686 -1, 1687#if defined(MSDOS) && !defined(WIN32) 1688 1024*30, /* 30k max cert list :-) */ 1689#else 1690 1024*100, /* 100k max cert list :-) */ 1691#endif 1692 &ok); 1693 1694 if (!ok) return((int)n); 1695 1696 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) 1697 { 1698 if ( (s->verify_mode & SSL_VERIFY_PEER) && 1699 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 1700 { 1701 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 1702 al=SSL_AD_HANDSHAKE_FAILURE; 1703 goto f_err; 1704 } 1705 /* If tls asked for a client cert, the client must return a 0 list */ 1706 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) 1707 { 1708 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 1709 al=SSL_AD_UNEXPECTED_MESSAGE; 1710 goto f_err; 1711 } 1712 s->s3->tmp.reuse_message=1; 1713 return(1); 1714 } 1715 1716 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 1717 { 1718 al=SSL_AD_UNEXPECTED_MESSAGE; 1719 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); 1720 goto f_err; 1721 } 1722 d=p=(unsigned char *)s->init_buf->data; 1723 1724 if ((sk=sk_X509_new_null()) == NULL) 1725 { 1726 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1727 goto err; 1728 } 1729 1730 n2l3(p,llen); 1731 if (llen+3 != n) 1732 { 1733 al=SSL_AD_DECODE_ERROR; 1734 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 1735 goto f_err; 1736 } 1737 for (nc=0; nc<llen; ) 1738 { 1739 n2l3(p,l); 1740 if ((l+nc+3) > llen) 1741 { 1742 al=SSL_AD_DECODE_ERROR; 1743 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1744 goto f_err; 1745 } 1746 1747 q=p; 1748 x=d2i_X509(NULL,&p,l); 1749 if (x == NULL) 1750 { 1751 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); 1752 goto err; 1753 } 1754 if (p != (q+l)) 1755 { 1756 al=SSL_AD_DECODE_ERROR; 1757 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1758 goto f_err; 1759 } 1760 if (!sk_X509_push(sk,x)) 1761 { 1762 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1763 goto err; 1764 } 1765 x=NULL; 1766 nc+=l+3; 1767 } 1768 1769 if (sk_X509_num(sk) <= 0) 1770 { 1771 /* TLS does not mind 0 certs returned */ 1772 if (s->version == SSL3_VERSION) 1773 { 1774 al=SSL_AD_HANDSHAKE_FAILURE; 1775 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); 1776 goto f_err; 1777 } 1778 /* Fail for TLS only if we required a certificate */ 1779 else if ((s->verify_mode & SSL_VERIFY_PEER) && 1780 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 1781 { 1782 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 1783 al=SSL_AD_HANDSHAKE_FAILURE; 1784 goto f_err; 1785 } 1786 } 1787 else 1788 { 1789 i=ssl_verify_cert_chain(s,sk); 1790 if (!i) 1791 { 1792 al=ssl_verify_alarm_type(s->verify_result); 1793 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); 1794 goto f_err; 1795 } 1796 } 1797 1798 if (s->session->peer != NULL) /* This should not be needed */ 1799 X509_free(s->session->peer); 1800 s->session->peer=sk_X509_shift(sk); 1801 s->session->verify_result = s->verify_result; 1802 1803 /* With the current implementation, sess_cert will always be NULL 1804 * when we arrive here. */ 1805 if (s->session->sess_cert == NULL) 1806 { 1807 s->session->sess_cert = ssl_sess_cert_new(); 1808 if (s->session->sess_cert == NULL) 1809 { 1810 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); 1811 goto err; 1812 } 1813 } 1814 if (s->session->sess_cert->cert_chain != NULL) 1815 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); 1816 s->session->sess_cert->cert_chain=sk; 1817 /* Inconsistency alert: cert_chain does *not* include the 1818 * peer's own certificate, while we do include it in s3_clnt.c */ 1819 1820 sk=NULL; 1821 1822 ret=1; 1823 if (0) 1824 { 1825f_err: 1826 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1827 } 1828err: 1829 if (x != NULL) X509_free(x); 1830 if (sk != NULL) sk_X509_pop_free(sk,X509_free); 1831 return(ret); 1832 } 1833 1834int ssl3_send_server_certificate(SSL *s) 1835 { 1836 unsigned long l; 1837 X509 *x; 1838 1839 if (s->state == SSL3_ST_SW_CERT_A) 1840 { 1841 x=ssl_get_server_send_cert(s); 1842 if (x == NULL) 1843 { 1844 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR); 1845 return(0); 1846 } 1847 1848 l=ssl3_output_cert_chain(s,x); 1849 s->state=SSL3_ST_SW_CERT_B; 1850 s->init_num=(int)l; 1851 s->init_off=0; 1852 } 1853 1854 /* SSL3_ST_SW_CERT_B */ 1855 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1856 } 1857