s3_srvr.c revision 101615
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-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#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#include "cryptlib.h" 126 127static SSL_METHOD *ssl3_get_server_method(int ver); 128static int ssl3_get_client_hello(SSL *s); 129static int ssl3_check_client_hello(SSL *s); 130static int ssl3_send_server_hello(SSL *s); 131static int ssl3_send_server_key_exchange(SSL *s); 132static int ssl3_send_certificate_request(SSL *s); 133static int ssl3_send_server_done(SSL *s); 134static int ssl3_get_client_key_exchange(SSL *s); 135static int ssl3_get_client_certificate(SSL *s); 136static int ssl3_get_cert_verify(SSL *s); 137static int ssl3_send_hello_request(SSL *s); 138 139static SSL_METHOD *ssl3_get_server_method(int ver) 140 { 141 if (ver == SSL3_VERSION) 142 return(SSLv3_server_method()); 143 else 144 return(NULL); 145 } 146 147SSL_METHOD *SSLv3_server_method(void) 148 { 149 static int init=1; 150 static SSL_METHOD SSLv3_server_data; 151 152 if (init) 153 { 154 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(), 155 sizeof(SSL_METHOD)); 156 SSLv3_server_data.ssl_accept=ssl3_accept; 157 SSLv3_server_data.get_ssl_method=ssl3_get_server_method; 158 init=0; 159 } 160 return(&SSLv3_server_data); 161 } 162 163int ssl3_accept(SSL *s) 164 { 165 BUF_MEM *buf; 166 unsigned long l,Time=time(NULL); 167 void (*cb)()=NULL; 168 long num1; 169 int ret= -1; 170 int new_state,state,skip=0; 171 172 RAND_add(&Time,sizeof(Time),0); 173 ERR_clear_error(); 174 clear_sys_error(); 175 176 if (s->info_callback != NULL) 177 cb=s->info_callback; 178 else if (s->ctx->info_callback != NULL) 179 cb=s->ctx->info_callback; 180 181 /* init things to blank */ 182 s->in_handshake++; 183 if (!SSL_in_init(s) || SSL_in_before(s)) 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 s->new_session = 2; 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 (s->new_session == 2) /* 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 ((p+i) >= (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 if ((p+i) > (d+n)) 773 { 774 /* not enough data */ 775 al=SSL_AD_DECODE_ERROR; 776 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 777 goto f_err; 778 } 779 q=p; 780 for (j=0; j<i; j++) 781 { 782 if (p[j] == 0) break; 783 } 784 785 p+=i; 786 if (j >= i) 787 { 788 /* no compress */ 789 al=SSL_AD_DECODE_ERROR; 790 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED); 791 goto f_err; 792 } 793 794 /* Worst case, we will use the NULL compression, but if we have other 795 * options, we will now look for them. We have i-1 compression 796 * algorithms from the client, starting at q. */ 797 s->s3->tmp.new_compression=NULL; 798 if (s->ctx->comp_methods != NULL) 799 { /* See if we have a match */ 800 int m,nn,o,v,done=0; 801 802 nn=sk_SSL_COMP_num(s->ctx->comp_methods); 803 for (m=0; m<nn; m++) 804 { 805 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m); 806 v=comp->id; 807 for (o=0; o<i; o++) 808 { 809 if (v == q[o]) 810 { 811 done=1; 812 break; 813 } 814 } 815 if (done) break; 816 } 817 if (done) 818 s->s3->tmp.new_compression=comp; 819 else 820 comp=NULL; 821 } 822 823 /* TLS does not mind if there is extra stuff */ 824 if (s->version == SSL3_VERSION) 825 { 826 if (p < (d+n)) 827 { 828 /* wrong number of bytes, 829 * there could be more to follow */ 830 al=SSL_AD_DECODE_ERROR; 831 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH); 832 goto f_err; 833 } 834 } 835 836 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must 837 * pick a cipher */ 838 839 if (!s->hit) 840 { 841 s->session->compress_meth=(comp == NULL)?0:comp->id; 842 if (s->session->ciphers != NULL) 843 sk_SSL_CIPHER_free(s->session->ciphers); 844 s->session->ciphers=ciphers; 845 if (ciphers == NULL) 846 { 847 al=SSL_AD_ILLEGAL_PARAMETER; 848 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED); 849 goto f_err; 850 } 851 ciphers=NULL; 852 c=ssl3_choose_cipher(s,s->session->ciphers, 853 ssl_get_ciphers_by_id(s)); 854 855 if (c == NULL) 856 { 857 al=SSL_AD_HANDSHAKE_FAILURE; 858 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER); 859 goto f_err; 860 } 861 s->s3->tmp.new_cipher=c; 862 } 863 else 864 { 865 /* Session-id reuse */ 866#ifdef REUSE_CIPHER_BUG 867 STACK_OF(SSL_CIPHER) *sk; 868 SSL_CIPHER *nc=NULL; 869 SSL_CIPHER *ec=NULL; 870 871 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) 872 { 873 sk=s->session->ciphers; 874 for (i=0; i<sk_SSL_CIPHER_num(sk); i++) 875 { 876 c=sk_SSL_CIPHER_value(sk,i); 877 if (c->algorithms & SSL_eNULL) 878 nc=c; 879 if (SSL_C_IS_EXPORT(c)) 880 ec=c; 881 } 882 if (nc != NULL) 883 s->s3->tmp.new_cipher=nc; 884 else if (ec != NULL) 885 s->s3->tmp.new_cipher=ec; 886 else 887 s->s3->tmp.new_cipher=s->session->cipher; 888 } 889 else 890#endif 891 s->s3->tmp.new_cipher=s->session->cipher; 892 } 893 894 /* we now have the following setup. 895 * client_random 896 * cipher_list - our prefered list of ciphers 897 * ciphers - the clients prefered list of ciphers 898 * compression - basically ignored right now 899 * ssl version is set - sslv3 900 * s->session - The ssl session has been setup. 901 * s->hit - session reuse flag 902 * s->tmp.new_cipher - the new cipher to use. 903 */ 904 905 ret=1; 906 if (0) 907 { 908f_err: 909 ssl3_send_alert(s,SSL3_AL_FATAL,al); 910 } 911err: 912 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers); 913 return(ret); 914 } 915 916static int ssl3_send_server_hello(SSL *s) 917 { 918 unsigned char *buf; 919 unsigned char *p,*d; 920 int i,sl; 921 unsigned long l,Time; 922 923 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 924 { 925 buf=(unsigned char *)s->init_buf->data; 926 p=s->s3->server_random; 927 Time=time(NULL); /* Time */ 928 l2n(Time,p); 929 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time)); 930 /* Do the message type and length last */ 931 d=p= &(buf[4]); 932 933 *(p++)=s->version>>8; 934 *(p++)=s->version&0xff; 935 936 /* Random stuff */ 937 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 938 p+=SSL3_RANDOM_SIZE; 939 940 /* now in theory we have 3 options to sending back the 941 * session id. If it is a re-use, we send back the 942 * old session-id, if it is a new session, we send 943 * back the new session-id or we send back a 0 length 944 * session-id if we want it to be single use. 945 * Currently I will not implement the '0' length session-id 946 * 12-Jan-98 - I'll now support the '0' length stuff. 947 */ 948 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) 949 s->session->session_id_length=0; 950 951 sl=s->session->session_id_length; 952 if (sl > sizeof s->session->session_id) 953 { 954 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_INTERNAL_ERROR); 955 return -1; 956 } 957 *(p++)=sl; 958 memcpy(p,s->session->session_id,sl); 959 p+=sl; 960 961 /* put the cipher */ 962 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 963 p+=i; 964 965 /* put the compression method */ 966 if (s->s3->tmp.new_compression == NULL) 967 *(p++)=0; 968 else 969 *(p++)=s->s3->tmp.new_compression->id; 970 971 /* do the header */ 972 l=(p-d); 973 d=buf; 974 *(d++)=SSL3_MT_SERVER_HELLO; 975 l2n3(l,d); 976 977 s->state=SSL3_ST_CW_CLNT_HELLO_B; 978 /* number of bytes to write */ 979 s->init_num=p-buf; 980 s->init_off=0; 981 } 982 983 /* SSL3_ST_CW_CLNT_HELLO_B */ 984 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 985 } 986 987static int ssl3_send_server_done(SSL *s) 988 { 989 unsigned char *p; 990 991 if (s->state == SSL3_ST_SW_SRVR_DONE_A) 992 { 993 p=(unsigned char *)s->init_buf->data; 994 995 /* do the header */ 996 *(p++)=SSL3_MT_SERVER_DONE; 997 *(p++)=0; 998 *(p++)=0; 999 *(p++)=0; 1000 1001 s->state=SSL3_ST_SW_SRVR_DONE_B; 1002 /* number of bytes to write */ 1003 s->init_num=4; 1004 s->init_off=0; 1005 } 1006 1007 /* SSL3_ST_CW_CLNT_HELLO_B */ 1008 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1009 } 1010 1011static int ssl3_send_server_key_exchange(SSL *s) 1012 { 1013#ifndef NO_RSA 1014 unsigned char *q; 1015 int j,num; 1016 RSA *rsa; 1017 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1018 unsigned int u; 1019#endif 1020#ifndef NO_DH 1021 DH *dh=NULL,*dhp; 1022#endif 1023 EVP_PKEY *pkey; 1024 unsigned char *p,*d; 1025 int al,i; 1026 unsigned long type; 1027 int n; 1028 CERT *cert; 1029 BIGNUM *r[4]; 1030 int nr[4],kn; 1031 BUF_MEM *buf; 1032 EVP_MD_CTX md_ctx; 1033 1034 if (s->state == SSL3_ST_SW_KEY_EXCH_A) 1035 { 1036 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK; 1037 cert=s->cert; 1038 1039 buf=s->init_buf; 1040 1041 r[0]=r[1]=r[2]=r[3]=NULL; 1042 n=0; 1043#ifndef NO_RSA 1044 if (type & SSL_kRSA) 1045 { 1046 rsa=cert->rsa_tmp; 1047 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 1048 { 1049 rsa=s->cert->rsa_tmp_cb(s, 1050 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1051 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1052 if(rsa == NULL) 1053 { 1054 al=SSL_AD_HANDSHAKE_FAILURE; 1055 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 1056 goto f_err; 1057 } 1058 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA); 1059 cert->rsa_tmp=rsa; 1060 } 1061 if (rsa == NULL) 1062 { 1063 al=SSL_AD_HANDSHAKE_FAILURE; 1064 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 1065 goto f_err; 1066 } 1067 r[0]=rsa->n; 1068 r[1]=rsa->e; 1069 s->s3->tmp.use_rsa_tmp=1; 1070 } 1071 else 1072#endif 1073#ifndef NO_DH 1074 if (type & SSL_kEDH) 1075 { 1076 dhp=cert->dh_tmp; 1077 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 1078 dhp=s->cert->dh_tmp_cb(s, 1079 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1080 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1081 if (dhp == NULL) 1082 { 1083 al=SSL_AD_HANDSHAKE_FAILURE; 1084 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1085 goto f_err; 1086 } 1087 1088 if (s->s3->tmp.dh != NULL) 1089 { 1090 DH_free(dh); 1091 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR); 1092 goto err; 1093 } 1094 1095 if ((dh=DHparams_dup(dhp)) == NULL) 1096 { 1097 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1098 goto err; 1099 } 1100 1101 s->s3->tmp.dh=dh; 1102 if ((dhp->pub_key == NULL || 1103 dhp->priv_key == NULL || 1104 (s->options & SSL_OP_SINGLE_DH_USE))) 1105 { 1106 if(!DH_generate_key(dh)) 1107 { 1108 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, 1109 ERR_R_DH_LIB); 1110 goto err; 1111 } 1112 } 1113 else 1114 { 1115 dh->pub_key=BN_dup(dhp->pub_key); 1116 dh->priv_key=BN_dup(dhp->priv_key); 1117 if ((dh->pub_key == NULL) || 1118 (dh->priv_key == NULL)) 1119 { 1120 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1121 goto err; 1122 } 1123 } 1124 r[0]=dh->p; 1125 r[1]=dh->g; 1126 r[2]=dh->pub_key; 1127 } 1128 else 1129#endif 1130 { 1131 al=SSL_AD_HANDSHAKE_FAILURE; 1132 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1133 goto f_err; 1134 } 1135 for (i=0; r[i] != NULL; i++) 1136 { 1137 nr[i]=BN_num_bytes(r[i]); 1138 n+=2+nr[i]; 1139 } 1140 1141 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 1142 { 1143 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher)) 1144 == NULL) 1145 { 1146 al=SSL_AD_DECODE_ERROR; 1147 goto f_err; 1148 } 1149 kn=EVP_PKEY_size(pkey); 1150 } 1151 else 1152 { 1153 pkey=NULL; 1154 kn=0; 1155 } 1156 1157 if (!BUF_MEM_grow(buf,n+4+kn)) 1158 { 1159 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 1160 goto err; 1161 } 1162 d=(unsigned char *)s->init_buf->data; 1163 p= &(d[4]); 1164 1165 for (i=0; r[i] != NULL; i++) 1166 { 1167 s2n(nr[i],p); 1168 BN_bn2bin(r[i],p); 1169 p+=nr[i]; 1170 } 1171 1172 /* not anonymous */ 1173 if (pkey != NULL) 1174 { 1175 /* n is the length of the params, they start at &(d[4]) 1176 * and p points to the space at the end. */ 1177#ifndef NO_RSA 1178 if (pkey->type == EVP_PKEY_RSA) 1179 { 1180 q=md_buf; 1181 j=0; 1182 for (num=2; num > 0; num--) 1183 { 1184 EVP_DigestInit(&md_ctx,(num == 2) 1185 ?s->ctx->md5:s->ctx->sha1); 1186 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1187 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1188 EVP_DigestUpdate(&md_ctx,&(d[4]),n); 1189 EVP_DigestFinal(&md_ctx,q, 1190 (unsigned int *)&i); 1191 q+=i; 1192 j+=i; 1193 } 1194 if (RSA_sign(NID_md5_sha1, md_buf, j, 1195 &(p[2]), &u, pkey->pkey.rsa) <= 0) 1196 { 1197 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 1198 goto err; 1199 } 1200 s2n(u,p); 1201 n+=u+2; 1202 } 1203 else 1204#endif 1205#if !defined(NO_DSA) 1206 if (pkey->type == EVP_PKEY_DSA) 1207 { 1208 /* lets do DSS */ 1209 EVP_SignInit(&md_ctx,EVP_dss1()); 1210 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1211 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1212 EVP_SignUpdate(&md_ctx,&(d[4]),n); 1213 if (!EVP_SignFinal(&md_ctx,&(p[2]), 1214 (unsigned int *)&i,pkey)) 1215 { 1216 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); 1217 goto err; 1218 } 1219 s2n(i,p); 1220 n+=i+2; 1221 } 1222 else 1223#endif 1224 { 1225 /* Is this error check actually needed? */ 1226 al=SSL_AD_HANDSHAKE_FAILURE; 1227 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 1228 goto f_err; 1229 } 1230 } 1231 1232 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE; 1233 l2n3(n,d); 1234 1235 /* we should now have things packed up, so lets send 1236 * it off */ 1237 s->init_num=n+4; 1238 s->init_off=0; 1239 } 1240 1241 s->state = SSL3_ST_SW_KEY_EXCH_B; 1242 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1243f_err: 1244 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1245err: 1246 return(-1); 1247 } 1248 1249static int ssl3_send_certificate_request(SSL *s) 1250 { 1251 unsigned char *p,*d; 1252 int i,j,nl,off,n; 1253 STACK_OF(X509_NAME) *sk=NULL; 1254 X509_NAME *name; 1255 BUF_MEM *buf; 1256 1257 if (s->state == SSL3_ST_SW_CERT_REQ_A) 1258 { 1259 buf=s->init_buf; 1260 1261 d=p=(unsigned char *)&(buf->data[4]); 1262 1263 /* get the list of acceptable cert types */ 1264 p++; 1265 n=ssl3_get_req_cert_type(s,p); 1266 d[0]=n; 1267 p+=n; 1268 n++; 1269 1270 off=n; 1271 p+=2; 1272 n+=2; 1273 1274 sk=SSL_get_client_CA_list(s); 1275 nl=0; 1276 if (sk != NULL) 1277 { 1278 for (i=0; i<sk_X509_NAME_num(sk); i++) 1279 { 1280 name=sk_X509_NAME_value(sk,i); 1281 j=i2d_X509_NAME(name,NULL); 1282 if (!BUF_MEM_grow(buf,4+n+j+2)) 1283 { 1284 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 1285 goto err; 1286 } 1287 p=(unsigned char *)&(buf->data[4+n]); 1288 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1289 { 1290 s2n(j,p); 1291 i2d_X509_NAME(name,&p); 1292 n+=2+j; 1293 nl+=2+j; 1294 } 1295 else 1296 { 1297 d=p; 1298 i2d_X509_NAME(name,&p); 1299 j-=2; s2n(j,d); j+=2; 1300 n+=j; 1301 nl+=j; 1302 } 1303 } 1304 } 1305 /* else no CA names */ 1306 p=(unsigned char *)&(buf->data[4+off]); 1307 s2n(nl,p); 1308 1309 d=(unsigned char *)buf->data; 1310 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 1311 l2n3(n,d); 1312 1313 /* we should now have things packed up, so lets send 1314 * it off */ 1315 1316 s->init_num=n+4; 1317 s->init_off=0; 1318#ifdef NETSCAPE_HANG_BUG 1319 p=(unsigned char *)s->init_buf->data + s->init_num; 1320 1321 /* do the header */ 1322 *(p++)=SSL3_MT_SERVER_DONE; 1323 *(p++)=0; 1324 *(p++)=0; 1325 *(p++)=0; 1326 s->init_num += 4; 1327#endif 1328 1329 } 1330 1331 /* SSL3_ST_SW_CERT_REQ_B */ 1332 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1333err: 1334 return(-1); 1335 } 1336 1337static int ssl3_get_client_key_exchange(SSL *s) 1338 { 1339 int i,al,ok; 1340 long n; 1341 unsigned long l; 1342 unsigned char *p; 1343#ifndef NO_RSA 1344 RSA *rsa=NULL; 1345 EVP_PKEY *pkey=NULL; 1346#endif 1347#ifndef NO_DH 1348 BIGNUM *pub=NULL; 1349 DH *dh_srvr; 1350#endif 1351 1352 n=ssl3_get_message(s, 1353 SSL3_ST_SR_KEY_EXCH_A, 1354 SSL3_ST_SR_KEY_EXCH_B, 1355 SSL3_MT_CLIENT_KEY_EXCHANGE, 1356 2048, /* ???? */ 1357 &ok); 1358 1359 if (!ok) return((int)n); 1360 p=(unsigned char *)s->init_buf->data; 1361 1362 l=s->s3->tmp.new_cipher->algorithms; 1363 1364#ifndef NO_RSA 1365 if (l & SSL_kRSA) 1366 { 1367 /* FIX THIS UP EAY EAY EAY EAY */ 1368 if (s->s3->tmp.use_rsa_tmp) 1369 { 1370 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL)) 1371 rsa=s->cert->rsa_tmp; 1372 /* Don't do a callback because rsa_tmp should 1373 * be sent already */ 1374 if (rsa == NULL) 1375 { 1376 al=SSL_AD_HANDSHAKE_FAILURE; 1377 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY); 1378 goto f_err; 1379 1380 } 1381 } 1382 else 1383 { 1384 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; 1385 if ( (pkey == NULL) || 1386 (pkey->type != EVP_PKEY_RSA) || 1387 (pkey->pkey.rsa == NULL)) 1388 { 1389 al=SSL_AD_HANDSHAKE_FAILURE; 1390 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE); 1391 goto f_err; 1392 } 1393 rsa=pkey->pkey.rsa; 1394 } 1395 1396 /* TLS */ 1397 if (s->version > SSL3_VERSION) 1398 { 1399 n2s(p,i); 1400 if (n != i+2) 1401 { 1402 if (!(s->options & SSL_OP_TLS_D5_BUG)) 1403 { 1404 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); 1405 goto err; 1406 } 1407 else 1408 p-=2; 1409 } 1410 else 1411 n=i; 1412 } 1413 1414 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING); 1415 1416 al = -1; 1417 1418 if (i != SSL_MAX_MASTER_KEY_LENGTH) 1419 { 1420 al=SSL_AD_DECODE_ERROR; 1421 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); 1422 } 1423 1424 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff)))) 1425 { 1426 /* The premaster secret must contain the same version number as the 1427 * ClientHello to detect version rollback attacks (strangely, the 1428 * protocol does not offer such protection for DH ciphersuites). 1429 * However, buggy clients exist that send the negotiated protocol 1430 * version instead if the server does not support the requested 1431 * protocol version. 1432 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */ 1433 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) && 1434 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff)))) 1435 { 1436 al=SSL_AD_DECODE_ERROR; 1437 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); 1438 goto f_err; 1439 } 1440 } 1441 1442 if (al != -1) 1443 { 1444#if 0 1445 goto f_err; 1446#else 1447 /* Some decryption failure -- use random value instead as countermeasure 1448 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding 1449 * (see RFC 2246, section 7.4.7.1). 1450 * But note that due to length and protocol version checking, the 1451 * attack is impractical anyway (see section 5 in D. Bleichenbacher: 1452 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA 1453 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12). 1454 */ 1455 ERR_clear_error(); 1456 i = SSL_MAX_MASTER_KEY_LENGTH; 1457 p[0] = s->client_version >> 8; 1458 p[1] = s->client_version & 0xff; 1459 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */ 1460#endif 1461 } 1462 1463 s->session->master_key_length= 1464 s->method->ssl3_enc->generate_master_secret(s, 1465 s->session->master_key, 1466 p,i); 1467 memset(p,0,i); 1468 } 1469 else 1470#endif 1471#ifndef NO_DH 1472 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 1473 { 1474 n2s(p,i); 1475 if (n != i+2) 1476 { 1477 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) 1478 { 1479 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); 1480 goto err; 1481 } 1482 else 1483 { 1484 p-=2; 1485 i=(int)n; 1486 } 1487 } 1488 1489 if (n == 0L) /* the parameters are in the cert */ 1490 { 1491 al=SSL_AD_HANDSHAKE_FAILURE; 1492 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS); 1493 goto f_err; 1494 } 1495 else 1496 { 1497 if (s->s3->tmp.dh == NULL) 1498 { 1499 al=SSL_AD_HANDSHAKE_FAILURE; 1500 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1501 goto f_err; 1502 } 1503 else 1504 dh_srvr=s->s3->tmp.dh; 1505 } 1506 1507 pub=BN_bin2bn(p,i,NULL); 1508 if (pub == NULL) 1509 { 1510 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB); 1511 goto err; 1512 } 1513 1514 i=DH_compute_key(p,pub,dh_srvr); 1515 1516 if (i <= 0) 1517 { 1518 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1519 goto err; 1520 } 1521 1522 DH_free(s->s3->tmp.dh); 1523 s->s3->tmp.dh=NULL; 1524 1525 BN_clear_free(pub); 1526 pub=NULL; 1527 s->session->master_key_length= 1528 s->method->ssl3_enc->generate_master_secret(s, 1529 s->session->master_key,p,i); 1530 memset(p,0,i); 1531 } 1532 else 1533#endif 1534 { 1535 al=SSL_AD_HANDSHAKE_FAILURE; 1536 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE); 1537 goto f_err; 1538 } 1539 1540 return(1); 1541f_err: 1542 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1543#if !defined(NO_DH) || !defined(NO_RSA) 1544err: 1545#endif 1546 return(-1); 1547 } 1548 1549static int ssl3_get_cert_verify(SSL *s) 1550 { 1551 EVP_PKEY *pkey=NULL; 1552 unsigned char *p; 1553 int al,ok,ret=0; 1554 long n; 1555 int type=0,i,j; 1556 X509 *peer; 1557 1558 n=ssl3_get_message(s, 1559 SSL3_ST_SR_CERT_VRFY_A, 1560 SSL3_ST_SR_CERT_VRFY_B, 1561 -1, 1562 512, /* 512? */ 1563 &ok); 1564 1565 if (!ok) return((int)n); 1566 1567 if (s->session->peer != NULL) 1568 { 1569 peer=s->session->peer; 1570 pkey=X509_get_pubkey(peer); 1571 type=X509_certificate_type(peer,pkey); 1572 } 1573 else 1574 { 1575 peer=NULL; 1576 pkey=NULL; 1577 } 1578 1579 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) 1580 { 1581 s->s3->tmp.reuse_message=1; 1582 if ((peer != NULL) && (type | EVP_PKT_SIGN)) 1583 { 1584 al=SSL_AD_UNEXPECTED_MESSAGE; 1585 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE); 1586 goto f_err; 1587 } 1588 ret=1; 1589 goto end; 1590 } 1591 1592 if (peer == NULL) 1593 { 1594 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED); 1595 al=SSL_AD_UNEXPECTED_MESSAGE; 1596 goto f_err; 1597 } 1598 1599 if (!(type & EVP_PKT_SIGN)) 1600 { 1601 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE); 1602 al=SSL_AD_ILLEGAL_PARAMETER; 1603 goto f_err; 1604 } 1605 1606 if (s->s3->change_cipher_spec) 1607 { 1608 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY); 1609 al=SSL_AD_UNEXPECTED_MESSAGE; 1610 goto f_err; 1611 } 1612 1613 /* we now have a signature that we need to verify */ 1614 p=(unsigned char *)s->init_buf->data; 1615 n2s(p,i); 1616 n-=2; 1617 if (i > n) 1618 { 1619 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH); 1620 al=SSL_AD_DECODE_ERROR; 1621 goto f_err; 1622 } 1623 1624 j=EVP_PKEY_size(pkey); 1625 if ((i > j) || (n > j) || (n <= 0)) 1626 { 1627 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE); 1628 al=SSL_AD_DECODE_ERROR; 1629 goto f_err; 1630 } 1631 1632#ifndef NO_RSA 1633 if (pkey->type == EVP_PKEY_RSA) 1634 { 1635 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md, 1636 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i, 1637 pkey->pkey.rsa); 1638 if (i < 0) 1639 { 1640 al=SSL_AD_DECRYPT_ERROR; 1641 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT); 1642 goto f_err; 1643 } 1644 if (i == 0) 1645 { 1646 al=SSL_AD_DECRYPT_ERROR; 1647 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE); 1648 goto f_err; 1649 } 1650 } 1651 else 1652#endif 1653#ifndef NO_DSA 1654 if (pkey->type == EVP_PKEY_DSA) 1655 { 1656 j=DSA_verify(pkey->save_type, 1657 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]), 1658 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa); 1659 if (j <= 0) 1660 { 1661 /* bad signature */ 1662 al=SSL_AD_DECRYPT_ERROR; 1663 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE); 1664 goto f_err; 1665 } 1666 } 1667 else 1668#endif 1669 { 1670 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR); 1671 al=SSL_AD_UNSUPPORTED_CERTIFICATE; 1672 goto f_err; 1673 } 1674 1675 1676 ret=1; 1677 if (0) 1678 { 1679f_err: 1680 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1681 } 1682end: 1683 EVP_PKEY_free(pkey); 1684 return(ret); 1685 } 1686 1687static int ssl3_get_client_certificate(SSL *s) 1688 { 1689 int i,ok,al,ret= -1; 1690 X509 *x=NULL; 1691 unsigned long l,nc,llen,n; 1692 unsigned char *p,*d,*q; 1693 STACK_OF(X509) *sk=NULL; 1694 1695 n=ssl3_get_message(s, 1696 SSL3_ST_SR_CERT_A, 1697 SSL3_ST_SR_CERT_B, 1698 -1, 1699#if defined(MSDOS) && !defined(WIN32) 1700 1024*30, /* 30k max cert list :-) */ 1701#else 1702 1024*100, /* 100k max cert list :-) */ 1703#endif 1704 &ok); 1705 1706 if (!ok) return((int)n); 1707 1708 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) 1709 { 1710 if ( (s->verify_mode & SSL_VERIFY_PEER) && 1711 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 1712 { 1713 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 1714 al=SSL_AD_HANDSHAKE_FAILURE; 1715 goto f_err; 1716 } 1717 /* If tls asked for a client cert, the client must return a 0 list */ 1718 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) 1719 { 1720 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); 1721 al=SSL_AD_UNEXPECTED_MESSAGE; 1722 goto f_err; 1723 } 1724 s->s3->tmp.reuse_message=1; 1725 return(1); 1726 } 1727 1728 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) 1729 { 1730 al=SSL_AD_UNEXPECTED_MESSAGE; 1731 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE); 1732 goto f_err; 1733 } 1734 d=p=(unsigned char *)s->init_buf->data; 1735 1736 if ((sk=sk_X509_new_null()) == NULL) 1737 { 1738 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1739 goto err; 1740 } 1741 1742 n2l3(p,llen); 1743 if (llen+3 != n) 1744 { 1745 al=SSL_AD_DECODE_ERROR; 1746 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH); 1747 goto f_err; 1748 } 1749 for (nc=0; nc<llen; ) 1750 { 1751 n2l3(p,l); 1752 if ((l+nc+3) > llen) 1753 { 1754 al=SSL_AD_DECODE_ERROR; 1755 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1756 goto f_err; 1757 } 1758 1759 q=p; 1760 x=d2i_X509(NULL,&p,l); 1761 if (x == NULL) 1762 { 1763 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB); 1764 goto err; 1765 } 1766 if (p != (q+l)) 1767 { 1768 al=SSL_AD_DECODE_ERROR; 1769 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH); 1770 goto f_err; 1771 } 1772 if (!sk_X509_push(sk,x)) 1773 { 1774 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE); 1775 goto err; 1776 } 1777 x=NULL; 1778 nc+=l+3; 1779 } 1780 1781 if (sk_X509_num(sk) <= 0) 1782 { 1783 /* TLS does not mind 0 certs returned */ 1784 if (s->version == SSL3_VERSION) 1785 { 1786 al=SSL_AD_HANDSHAKE_FAILURE; 1787 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED); 1788 goto f_err; 1789 } 1790 /* Fail for TLS only if we required a certificate */ 1791 else if ((s->verify_mode & SSL_VERIFY_PEER) && 1792 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 1793 { 1794 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 1795 al=SSL_AD_HANDSHAKE_FAILURE; 1796 goto f_err; 1797 } 1798 } 1799 else 1800 { 1801 i=ssl_verify_cert_chain(s,sk); 1802 if (!i) 1803 { 1804 al=ssl_verify_alarm_type(s->verify_result); 1805 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED); 1806 goto f_err; 1807 } 1808 } 1809 1810 if (s->session->peer != NULL) /* This should not be needed */ 1811 X509_free(s->session->peer); 1812 s->session->peer=sk_X509_shift(sk); 1813 s->session->verify_result = s->verify_result; 1814 1815 /* With the current implementation, sess_cert will always be NULL 1816 * when we arrive here. */ 1817 if (s->session->sess_cert == NULL) 1818 { 1819 s->session->sess_cert = ssl_sess_cert_new(); 1820 if (s->session->sess_cert == NULL) 1821 { 1822 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE); 1823 goto err; 1824 } 1825 } 1826 if (s->session->sess_cert->cert_chain != NULL) 1827 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); 1828 s->session->sess_cert->cert_chain=sk; 1829 /* Inconsistency alert: cert_chain does *not* include the 1830 * peer's own certificate, while we do include it in s3_clnt.c */ 1831 1832 sk=NULL; 1833 1834 ret=1; 1835 if (0) 1836 { 1837f_err: 1838 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1839 } 1840err: 1841 if (x != NULL) X509_free(x); 1842 if (sk != NULL) sk_X509_pop_free(sk,X509_free); 1843 return(ret); 1844 } 1845 1846int ssl3_send_server_certificate(SSL *s) 1847 { 1848 unsigned long l; 1849 X509 *x; 1850 1851 if (s->state == SSL3_ST_SW_CERT_A) 1852 { 1853 x=ssl_get_server_send_cert(s); 1854 if (x == NULL) 1855 { 1856 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR); 1857 return(0); 1858 } 1859 1860 l=ssl3_output_cert_chain(s,x); 1861 s->state=SSL3_ST_SW_CERT_B; 1862 s->init_num=(int)l; 1863 s->init_off=0; 1864 } 1865 1866 /* SSL3_ST_SW_CERT_B */ 1867 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE)); 1868 } 1869