1/* ssl/d1_clnt.c */ 2/* 3 * DTLS implementation written by Nagendra Modadugu 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5 */ 6/* ==================================================================== 7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgment: 23 * "This product includes software developed by the OpenSSL Project 24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25 * 26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27 * endorse or promote products derived from this software without 28 * prior written permission. For written permission, please contact 29 * openssl-core@OpenSSL.org. 30 * 31 * 5. Products derived from this software may not be called "OpenSSL" 32 * nor may "OpenSSL" appear in their names without prior written 33 * permission of the OpenSSL Project. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51 * OF THE POSSIBILITY OF SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This product includes cryptographic software written by Eric Young 55 * (eay@cryptsoft.com). This product includes software written by Tim 56 * Hudson (tjh@cryptsoft.com). 57 * 58 */ 59/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 60 * All rights reserved. 61 * 62 * This package is an SSL implementation written 63 * by Eric Young (eay@cryptsoft.com). 64 * The implementation was written so as to conform with Netscapes SSL. 65 * 66 * This library is free for commercial and non-commercial use as long as 67 * the following conditions are aheared to. The following conditions 68 * apply to all code found in this distribution, be it the RC4, RSA, 69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 70 * included with this distribution is covered by the same copyright terms 71 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 72 * 73 * Copyright remains Eric Young's, and as such any Copyright notices in 74 * the code are not to be removed. 75 * If this package is used in a product, Eric Young should be given attribution 76 * as the author of the parts of the library used. 77 * This can be in the form of a textual message at program startup or 78 * in documentation (online or textual) provided with the package. 79 * 80 * Redistribution and use in source and binary forms, with or without 81 * modification, are permitted provided that the following conditions 82 * are met: 83 * 1. Redistributions of source code must retain the copyright 84 * notice, this list of conditions and the following disclaimer. 85 * 2. Redistributions in binary form must reproduce the above copyright 86 * notice, this list of conditions and the following disclaimer in the 87 * documentation and/or other materials provided with the distribution. 88 * 3. All advertising materials mentioning features or use of this software 89 * must display the following acknowledgement: 90 * "This product includes cryptographic software written by 91 * Eric Young (eay@cryptsoft.com)" 92 * The word 'cryptographic' can be left out if the rouines from the library 93 * being used are not cryptographic related :-). 94 * 4. If you include any Windows specific code (or a derivative thereof) from 95 * the apps directory (application code) you must include an acknowledgement: 96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 97 * 98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 108 * SUCH DAMAGE. 109 * 110 * The licence and distribution terms for any publically available version or 111 * derivative of this code cannot be changed. i.e. this code cannot simply be 112 * copied and put under another distribution licence 113 * [including the GNU Public Licence.] 114 */ 115 116#include <stdio.h> 117#include "ssl_locl.h" 118#include "kssl_lcl.h" 119#include <openssl/buffer.h> 120#include <openssl/rand.h> 121#include <openssl/objects.h> 122#include <openssl/evp.h> 123#include <openssl/md5.h> 124#ifndef OPENSSL_NO_DH 125#include <openssl/dh.h> 126#endif 127 128static SSL_METHOD *dtls1_get_client_method(int ver); 129static int dtls1_get_hello_verify(SSL *s); 130 131static SSL_METHOD *dtls1_get_client_method(int ver) 132 { 133 if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER) 134 return(DTLSv1_client_method()); 135 else 136 return(NULL); 137 } 138 139IMPLEMENT_dtls1_meth_func(DTLSv1_client_method, 140 ssl_undefined_function, 141 dtls1_connect, 142 dtls1_get_client_method) 143 144int dtls1_connect(SSL *s) 145 { 146 BUF_MEM *buf=NULL; 147 unsigned long Time=(unsigned long)time(NULL); 148 void (*cb)(const SSL *ssl,int type,int val)=NULL; 149 int ret= -1; 150 int new_state,state,skip=0;; 151 152 RAND_add(&Time,sizeof(Time),0); 153 ERR_clear_error(); 154 clear_sys_error(); 155 156 if (s->info_callback != NULL) 157 cb=s->info_callback; 158 else if (s->ctx->info_callback != NULL) 159 cb=s->ctx->info_callback; 160 161 s->in_handshake++; 162 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 163 164 for (;;) 165 { 166 state=s->state; 167 168 switch(s->state) 169 { 170 case SSL_ST_RENEGOTIATE: 171 s->new_session=1; 172 s->state=SSL_ST_CONNECT; 173 s->ctx->stats.sess_connect_renegotiate++; 174 /* break */ 175 case SSL_ST_BEFORE: 176 case SSL_ST_CONNECT: 177 case SSL_ST_BEFORE|SSL_ST_CONNECT: 178 case SSL_ST_OK|SSL_ST_CONNECT: 179 180 s->server=0; 181 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 182 183 if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) && 184 (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00)) 185 { 186 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR); 187 ret = -1; 188 goto end; 189 } 190 191 /* s->version=SSL3_VERSION; */ 192 s->type=SSL_ST_CONNECT; 193 194 if (s->init_buf == NULL) 195 { 196 if ((buf=BUF_MEM_new()) == NULL) 197 { 198 ret= -1; 199 goto end; 200 } 201 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 202 { 203 ret= -1; 204 goto end; 205 } 206 s->init_buf=buf; 207 buf=NULL; 208 } 209 210 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; } 211 212 /* setup buffing BIO */ 213 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; } 214 215 /* don't push the buffering BIO quite yet */ 216 217 s->state=SSL3_ST_CW_CLNT_HELLO_A; 218 s->ctx->stats.sess_connect++; 219 s->init_num=0; 220 /* mark client_random uninitialized */ 221 memset(s->s3->client_random,0,sizeof(s->s3->client_random)); 222 s->d1->send_cookie = 0; 223 s->hit = 0; 224 break; 225 226 case SSL3_ST_CW_CLNT_HELLO_A: 227 case SSL3_ST_CW_CLNT_HELLO_B: 228 229 s->shutdown=0; 230 231 /* every DTLS ClientHello resets Finished MAC */ 232 ssl3_init_finished_mac(s); 233 234 dtls1_start_timer(s); 235 ret=dtls1_client_hello(s); 236 if (ret <= 0) goto end; 237 238 if ( s->d1->send_cookie) 239 { 240 s->state=SSL3_ST_CW_FLUSH; 241 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A; 242 } 243 else 244 s->state=SSL3_ST_CR_SRVR_HELLO_A; 245 246 s->init_num=0; 247 248 /* turn on buffering for the next lot of output */ 249 if (s->bbio != s->wbio) 250 s->wbio=BIO_push(s->bbio,s->wbio); 251 252 break; 253 254 case SSL3_ST_CR_SRVR_HELLO_A: 255 case SSL3_ST_CR_SRVR_HELLO_B: 256 ret=ssl3_get_server_hello(s); 257 if (ret <= 0) goto end; 258 else 259 { 260 if (s->hit) 261 s->state=SSL3_ST_CR_FINISHED_A; 262 else 263 s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A; 264 } 265 s->init_num=0; 266 break; 267 268 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A: 269 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B: 270 271 ret = dtls1_get_hello_verify(s); 272 if ( ret <= 0) 273 goto end; 274 dtls1_stop_timer(s); 275 if ( s->d1->send_cookie) /* start again, with a cookie */ 276 s->state=SSL3_ST_CW_CLNT_HELLO_A; 277 else 278 s->state = SSL3_ST_CR_CERT_A; 279 s->init_num = 0; 280 break; 281 282 case SSL3_ST_CR_CERT_A: 283 case SSL3_ST_CR_CERT_B: 284#ifndef OPENSSL_NO_TLSEXT 285 ret=ssl3_check_finished(s); 286 if (ret <= 0) goto end; 287 if (ret == 2) 288 { 289 s->hit = 1; 290 if (s->tlsext_ticket_expected) 291 s->state=SSL3_ST_CR_SESSION_TICKET_A; 292 else 293 s->state=SSL3_ST_CR_FINISHED_A; 294 s->init_num=0; 295 break; 296 } 297#endif 298 /* Check if it is anon DH */ 299 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL)) 300 { 301 ret=ssl3_get_server_certificate(s); 302 if (ret <= 0) goto end; 303#ifndef OPENSSL_NO_TLSEXT 304 if (s->tlsext_status_expected) 305 s->state=SSL3_ST_CR_CERT_STATUS_A; 306 else 307 s->state=SSL3_ST_CR_KEY_EXCH_A; 308 } 309 else 310 { 311 skip = 1; 312 s->state=SSL3_ST_CR_KEY_EXCH_A; 313 } 314#else 315 } 316 else 317 skip=1; 318 319 s->state=SSL3_ST_CR_KEY_EXCH_A; 320#endif 321 s->init_num=0; 322 break; 323 324 case SSL3_ST_CR_KEY_EXCH_A: 325 case SSL3_ST_CR_KEY_EXCH_B: 326 ret=ssl3_get_key_exchange(s); 327 if (ret <= 0) goto end; 328 s->state=SSL3_ST_CR_CERT_REQ_A; 329 s->init_num=0; 330 331 /* at this point we check that we have the 332 * required stuff from the server */ 333 if (!ssl3_check_cert_and_algorithm(s)) 334 { 335 ret= -1; 336 goto end; 337 } 338 break; 339 340 case SSL3_ST_CR_CERT_REQ_A: 341 case SSL3_ST_CR_CERT_REQ_B: 342 ret=ssl3_get_certificate_request(s); 343 if (ret <= 0) goto end; 344 s->state=SSL3_ST_CR_SRVR_DONE_A; 345 s->init_num=0; 346 break; 347 348 case SSL3_ST_CR_SRVR_DONE_A: 349 case SSL3_ST_CR_SRVR_DONE_B: 350 ret=ssl3_get_server_done(s); 351 if (ret <= 0) goto end; 352 dtls1_stop_timer(s); 353 if (s->s3->tmp.cert_req) 354 s->state=SSL3_ST_CW_CERT_A; 355 else 356 s->state=SSL3_ST_CW_KEY_EXCH_A; 357 s->init_num=0; 358 359 break; 360 361 case SSL3_ST_CW_CERT_A: 362 case SSL3_ST_CW_CERT_B: 363 case SSL3_ST_CW_CERT_C: 364 case SSL3_ST_CW_CERT_D: 365 dtls1_start_timer(s); 366 ret=dtls1_send_client_certificate(s); 367 if (ret <= 0) goto end; 368 s->state=SSL3_ST_CW_KEY_EXCH_A; 369 s->init_num=0; 370 break; 371 372 case SSL3_ST_CW_KEY_EXCH_A: 373 case SSL3_ST_CW_KEY_EXCH_B: 374 dtls1_start_timer(s); 375 ret=dtls1_send_client_key_exchange(s); 376 if (ret <= 0) goto end; 377 /* EAY EAY EAY need to check for DH fix cert 378 * sent back */ 379 /* For TLS, cert_req is set to 2, so a cert chain 380 * of nothing is sent, but no verify packet is sent */ 381 if (s->s3->tmp.cert_req == 1) 382 { 383 s->state=SSL3_ST_CW_CERT_VRFY_A; 384 } 385 else 386 { 387 s->state=SSL3_ST_CW_CHANGE_A; 388 s->s3->change_cipher_spec=0; 389 } 390 391 s->init_num=0; 392 break; 393 394 case SSL3_ST_CW_CERT_VRFY_A: 395 case SSL3_ST_CW_CERT_VRFY_B: 396 dtls1_start_timer(s); 397 ret=dtls1_send_client_verify(s); 398 if (ret <= 0) goto end; 399 s->state=SSL3_ST_CW_CHANGE_A; 400 s->init_num=0; 401 s->s3->change_cipher_spec=0; 402 break; 403 404 case SSL3_ST_CW_CHANGE_A: 405 case SSL3_ST_CW_CHANGE_B: 406 if (!s->hit) 407 dtls1_start_timer(s); 408 ret=dtls1_send_change_cipher_spec(s, 409 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B); 410 if (ret <= 0) goto end; 411 s->state=SSL3_ST_CW_FINISHED_A; 412 s->init_num=0; 413 414 s->session->cipher=s->s3->tmp.new_cipher; 415#ifdef OPENSSL_NO_COMP 416 s->session->compress_meth=0; 417#else 418 if (s->s3->tmp.new_compression == NULL) 419 s->session->compress_meth=0; 420 else 421 s->session->compress_meth= 422 s->s3->tmp.new_compression->id; 423#endif 424 if (!s->method->ssl3_enc->setup_key_block(s)) 425 { 426 ret= -1; 427 goto end; 428 } 429 430 if (!s->method->ssl3_enc->change_cipher_state(s, 431 SSL3_CHANGE_CIPHER_CLIENT_WRITE)) 432 { 433 ret= -1; 434 goto end; 435 } 436 437 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); 438 break; 439 440 case SSL3_ST_CW_FINISHED_A: 441 case SSL3_ST_CW_FINISHED_B: 442 if (!s->hit) 443 dtls1_start_timer(s); 444 ret=dtls1_send_finished(s, 445 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B, 446 s->method->ssl3_enc->client_finished_label, 447 s->method->ssl3_enc->client_finished_label_len); 448 if (ret <= 0) goto end; 449 s->state=SSL3_ST_CW_FLUSH; 450 451 /* clear flags */ 452 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER; 453 if (s->hit) 454 { 455 s->s3->tmp.next_state=SSL_ST_OK; 456 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) 457 { 458 s->state=SSL_ST_OK; 459 s->s3->flags|=SSL3_FLAGS_POP_BUFFER; 460 s->s3->delay_buf_pop_ret=0; 461 } 462 } 463 else 464 { 465#ifndef OPENSSL_NO_TLSEXT 466 /* Allow NewSessionTicket if ticket expected */ 467 if (s->tlsext_ticket_expected) 468 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A; 469 else 470#endif 471 472 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A; 473 } 474 s->init_num=0; 475 476 break; 477 478#ifndef OPENSSL_NO_TLSEXT 479 case SSL3_ST_CR_SESSION_TICKET_A: 480 case SSL3_ST_CR_SESSION_TICKET_B: 481 ret=ssl3_get_new_session_ticket(s); 482 if (ret <= 0) goto end; 483 s->state=SSL3_ST_CR_FINISHED_A; 484 s->init_num=0; 485 break; 486 487 case SSL3_ST_CR_CERT_STATUS_A: 488 case SSL3_ST_CR_CERT_STATUS_B: 489 ret=ssl3_get_cert_status(s); 490 if (ret <= 0) goto end; 491 s->state=SSL3_ST_CR_KEY_EXCH_A; 492 s->init_num=0; 493 break; 494#endif 495 496 case SSL3_ST_CR_FINISHED_A: 497 case SSL3_ST_CR_FINISHED_B: 498 s->d1->change_cipher_spec_ok = 1; 499 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A, 500 SSL3_ST_CR_FINISHED_B); 501 if (ret <= 0) goto end; 502 dtls1_stop_timer(s); 503 504 if (s->hit) 505 s->state=SSL3_ST_CW_CHANGE_A; 506 else 507 s->state=SSL_ST_OK; 508 s->init_num=0; 509 break; 510 511 case SSL3_ST_CW_FLUSH: 512 s->rwstate=SSL_WRITING; 513 if (BIO_flush(s->wbio) <= 0) 514 { 515 ret= -1; 516 goto end; 517 } 518 s->rwstate=SSL_NOTHING; 519 s->state=s->s3->tmp.next_state; 520 break; 521 522 case SSL_ST_OK: 523 /* clean a few things up */ 524 ssl3_cleanup_key_block(s); 525 526#if 0 527 if (s->init_buf != NULL) 528 { 529 BUF_MEM_free(s->init_buf); 530 s->init_buf=NULL; 531 } 532#endif 533 534 /* If we are not 'joining' the last two packets, 535 * remove the buffering now */ 536 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER)) 537 ssl_free_wbio_buffer(s); 538 /* else do it later in ssl3_write */ 539 540 s->init_num=0; 541 s->new_session=0; 542 543 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT); 544 if (s->hit) s->ctx->stats.sess_hit++; 545 546 ret=1; 547 /* s->server=0; */ 548 s->handshake_func=dtls1_connect; 549 s->ctx->stats.sess_connect_good++; 550 551 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 552 553 /* done with handshaking */ 554 s->d1->handshake_read_seq = 0; 555 s->d1->next_handshake_write_seq = 0; 556 goto end; 557 /* break; */ 558 559 default: 560 SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE); 561 ret= -1; 562 goto end; 563 /* break; */ 564 } 565 566 /* did we do anything */ 567 if (!s->s3->tmp.reuse_message && !skip) 568 { 569 if (s->debug) 570 { 571 if ((ret=BIO_flush(s->wbio)) <= 0) 572 goto end; 573 } 574 575 if ((cb != NULL) && (s->state != state)) 576 { 577 new_state=s->state; 578 s->state=state; 579 cb(s,SSL_CB_CONNECT_LOOP,1); 580 s->state=new_state; 581 } 582 } 583 skip=0; 584 } 585end: 586 s->in_handshake--; 587 if (buf != NULL) 588 BUF_MEM_free(buf); 589 if (cb != NULL) 590 cb(s,SSL_CB_CONNECT_EXIT,ret); 591 return(ret); 592 } 593 594int dtls1_client_hello(SSL *s) 595 { 596 unsigned char *buf; 597 unsigned char *p,*d; 598 unsigned int i,j; 599 unsigned long Time,l; 600 SSL_COMP *comp; 601 602 buf=(unsigned char *)s->init_buf->data; 603 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) 604 { 605 SSL_SESSION *sess = s->session; 606 if ((s->session == NULL) || 607 (s->session->ssl_version != s->version) || 608#ifdef OPENSSL_NO_TLSEXT 609 !sess->session_id_length || 610#else 611 (!sess->session_id_length && !sess->tlsext_tick) || 612#endif 613 (s->session->not_resumable)) 614 { 615 if (!ssl_get_new_session(s,0)) 616 goto err; 617 } 618 /* else use the pre-loaded session */ 619 620 p=s->s3->client_random; 621 /* if client_random is initialized, reuse it, we are 622 * required to use same upon reply to HelloVerify */ 623 for (i=0;p[i]=='\0' && i<sizeof(s->s3->client_random);i++) ; 624 if (i==sizeof(s->s3->client_random)) 625 { 626 Time=(unsigned long)time(NULL); /* Time */ 627 l2n(Time,p); 628 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4); 629 } 630 631 /* Do the message type and length last */ 632 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); 633 634 *(p++)=s->version>>8; 635 *(p++)=s->version&0xff; 636 s->client_version=s->version; 637 638 /* Random stuff */ 639 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE); 640 p+=SSL3_RANDOM_SIZE; 641 642 /* Session ID */ 643 if (s->new_session) 644 i=0; 645 else 646 i=s->session->session_id_length; 647 *(p++)=i; 648 if (i != 0) 649 { 650 if (i > sizeof s->session->session_id) 651 { 652 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 653 goto err; 654 } 655 memcpy(p,s->session->session_id,i); 656 p+=i; 657 } 658 659 /* cookie stuff */ 660 if ( s->d1->cookie_len > sizeof(s->d1->cookie)) 661 { 662 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR); 663 goto err; 664 } 665 *(p++) = s->d1->cookie_len; 666 memcpy(p, s->d1->cookie, s->d1->cookie_len); 667 p += s->d1->cookie_len; 668 669 /* Ciphers supported */ 670 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),0); 671 if (i == 0) 672 { 673 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE); 674 goto err; 675 } 676 s2n(i,p); 677 p+=i; 678 679 /* COMPRESSION */ 680 if (s->ctx->comp_methods == NULL) 681 j=0; 682 else 683 j=sk_SSL_COMP_num(s->ctx->comp_methods); 684 *(p++)=1+j; 685 for (i=0; i<j; i++) 686 { 687 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i); 688 *(p++)=comp->id; 689 } 690 *(p++)=0; /* Add the NULL method */ 691 692#ifndef OPENSSL_NO_TLSEXT 693 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) 694 { 695 SSLerr(SSL_F_DTLS1_CLIENT_HELLO,ERR_R_INTERNAL_ERROR); 696 goto err; 697 } 698#endif 699 700 l=(p-d); 701 d=buf; 702 703 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l); 704 705 s->state=SSL3_ST_CW_CLNT_HELLO_B; 706 /* number of bytes to write */ 707 s->init_num=p-buf; 708 s->init_off=0; 709 710 /* buffer the message to handle re-xmits */ 711 dtls1_buffer_message(s, 0); 712 } 713 714 /* SSL3_ST_CW_CLNT_HELLO_B */ 715 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 716err: 717 return(-1); 718 } 719 720static int dtls1_get_hello_verify(SSL *s) 721 { 722 int n, al, ok = 0; 723 unsigned char *data; 724 unsigned int cookie_len; 725 726 n=s->method->ssl_get_message(s, 727 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, 728 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, 729 -1, 730 s->max_cert_list, 731 &ok); 732 733 if (!ok) return((int)n); 734 735 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) 736 { 737 s->d1->send_cookie = 0; 738 s->s3->tmp.reuse_message=1; 739 return(1); 740 } 741 742 data = (unsigned char *)s->init_msg; 743 744 if ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))) 745 { 746 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION); 747 s->version=(s->version&0xff00)|data[1]; 748 al = SSL_AD_PROTOCOL_VERSION; 749 goto f_err; 750 } 751 data+=2; 752 753 cookie_len = *(data++); 754 if ( cookie_len > sizeof(s->d1->cookie)) 755 { 756 al=SSL_AD_ILLEGAL_PARAMETER; 757 goto f_err; 758 } 759 760 memcpy(s->d1->cookie, data, cookie_len); 761 s->d1->cookie_len = cookie_len; 762 763 s->d1->send_cookie = 1; 764 return 1; 765 766f_err: 767 ssl3_send_alert(s, SSL3_AL_FATAL, al); 768 return -1; 769 } 770 771int dtls1_send_client_key_exchange(SSL *s) 772 { 773 unsigned char *p,*d; 774 int n; 775 unsigned long l; 776#ifndef OPENSSL_NO_RSA 777 unsigned char *q; 778 EVP_PKEY *pkey=NULL; 779#endif 780#ifndef OPENSSL_NO_KRB5 781 KSSL_ERR kssl_err; 782#endif /* OPENSSL_NO_KRB5 */ 783 784 if (s->state == SSL3_ST_CW_KEY_EXCH_A) 785 { 786 d=(unsigned char *)s->init_buf->data; 787 p= &(d[DTLS1_HM_HEADER_LENGTH]); 788 789 l=s->s3->tmp.new_cipher->algorithms; 790 791 /* Fool emacs indentation */ 792 if (0) {} 793#ifndef OPENSSL_NO_RSA 794 else if (l & SSL_kRSA) 795 { 796 RSA *rsa; 797 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 798 799 if (s->session->sess_cert->peer_rsa_tmp != NULL) 800 rsa=s->session->sess_cert->peer_rsa_tmp; 801 else 802 { 803 pkey=X509_get_pubkey(s->session->sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509); 804 if ((pkey == NULL) || 805 (pkey->type != EVP_PKEY_RSA) || 806 (pkey->pkey.rsa == NULL)) 807 { 808 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 809 goto err; 810 } 811 rsa=pkey->pkey.rsa; 812 EVP_PKEY_free(pkey); 813 } 814 815 tmp_buf[0]=s->client_version>>8; 816 tmp_buf[1]=s->client_version&0xff; 817 if (RAND_bytes(&(tmp_buf[2]),sizeof tmp_buf-2) <= 0) 818 goto err; 819 820 s->session->master_key_length=sizeof tmp_buf; 821 822 q=p; 823 /* Fix buf for TLS and [incidentally] DTLS */ 824 if (s->version > SSL3_VERSION) 825 p+=2; 826 n=RSA_public_encrypt(sizeof tmp_buf, 827 tmp_buf,p,rsa,RSA_PKCS1_PADDING); 828#ifdef PKCS1_CHECK 829 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++; 830 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70; 831#endif 832 if (n <= 0) 833 { 834 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT); 835 goto err; 836 } 837 838 /* Fix buf for TLS and [incidentally] DTLS */ 839 if (s->version > SSL3_VERSION) 840 { 841 s2n(n,q); 842 n+=2; 843 } 844 845 s->session->master_key_length= 846 s->method->ssl3_enc->generate_master_secret(s, 847 s->session->master_key, 848 tmp_buf,sizeof tmp_buf); 849 OPENSSL_cleanse(tmp_buf,sizeof tmp_buf); 850 } 851#endif 852#ifndef OPENSSL_NO_KRB5 853 else if (l & SSL_kKRB5) 854 { 855 krb5_error_code krb5rc; 856 KSSL_CTX *kssl_ctx = s->kssl_ctx; 857 /* krb5_data krb5_ap_req; */ 858 krb5_data *enc_ticket; 859 krb5_data authenticator, *authp = NULL; 860 EVP_CIPHER_CTX ciph_ctx; 861 EVP_CIPHER *enc = NULL; 862 unsigned char iv[EVP_MAX_IV_LENGTH]; 863 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH]; 864 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH 865 + EVP_MAX_IV_LENGTH]; 866 int padl, outl = sizeof(epms); 867 868 EVP_CIPHER_CTX_init(&ciph_ctx); 869 870#ifdef KSSL_DEBUG 871 printf("ssl3_send_client_key_exchange(%lx & %lx)\n", 872 l, SSL_kKRB5); 873#endif /* KSSL_DEBUG */ 874 875 authp = NULL; 876#ifdef KRB5SENDAUTH 877 if (KRB5SENDAUTH) authp = &authenticator; 878#endif /* KRB5SENDAUTH */ 879 880 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, 881 &kssl_err); 882 enc = kssl_map_enc(kssl_ctx->enctype); 883 if (enc == NULL) 884 goto err; 885#ifdef KSSL_DEBUG 886 { 887 printf("kssl_cget_tkt rtn %d\n", krb5rc); 888 if (krb5rc && kssl_err.text) 889 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text); 890 } 891#endif /* KSSL_DEBUG */ 892 893 if (krb5rc) 894 { 895 ssl3_send_alert(s,SSL3_AL_FATAL, 896 SSL_AD_HANDSHAKE_FAILURE); 897 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, 898 kssl_err.reason); 899 goto err; 900 } 901 902 /* 20010406 VRS - Earlier versions used KRB5 AP_REQ 903 ** in place of RFC 2712 KerberosWrapper, as in: 904 ** 905 ** Send ticket (copy to *p, set n = length) 906 ** n = krb5_ap_req.length; 907 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length); 908 ** if (krb5_ap_req.data) 909 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req); 910 ** 911 ** Now using real RFC 2712 KerberosWrapper 912 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>) 913 ** Note: 2712 "opaque" types are here replaced 914 ** with a 2-byte length followed by the value. 915 ** Example: 916 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms 917 ** Where "xx xx" = length bytes. Shown here with 918 ** optional authenticator omitted. 919 */ 920 921 /* KerberosWrapper.Ticket */ 922 s2n(enc_ticket->length,p); 923 memcpy(p, enc_ticket->data, enc_ticket->length); 924 p+= enc_ticket->length; 925 n = enc_ticket->length + 2; 926 927 /* KerberosWrapper.Authenticator */ 928 if (authp && authp->length) 929 { 930 s2n(authp->length,p); 931 memcpy(p, authp->data, authp->length); 932 p+= authp->length; 933 n+= authp->length + 2; 934 935 free(authp->data); 936 authp->data = NULL; 937 authp->length = 0; 938 } 939 else 940 { 941 s2n(0,p);/* null authenticator length */ 942 n+=2; 943 } 944 945 if (RAND_bytes(tmp_buf,sizeof tmp_buf) <= 0) 946 goto err; 947 948 /* 20010420 VRS. Tried it this way; failed. 949 ** EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL); 950 ** EVP_CIPHER_CTX_set_key_length(&ciph_ctx, 951 ** kssl_ctx->length); 952 ** EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv); 953 */ 954 955 memset(iv, 0, sizeof iv); /* per RFC 1510 */ 956 EVP_EncryptInit_ex(&ciph_ctx,enc, NULL, 957 kssl_ctx->key,iv); 958 EVP_EncryptUpdate(&ciph_ctx,epms,&outl,tmp_buf, 959 sizeof tmp_buf); 960 EVP_EncryptFinal_ex(&ciph_ctx,&(epms[outl]),&padl); 961 outl += padl; 962 if (outl > sizeof epms) 963 { 964 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 965 goto err; 966 } 967 EVP_CIPHER_CTX_cleanup(&ciph_ctx); 968 969 /* KerberosWrapper.EncryptedPreMasterSecret */ 970 s2n(outl,p); 971 memcpy(p, epms, outl); 972 p+=outl; 973 n+=outl + 2; 974 975 s->session->master_key_length= 976 s->method->ssl3_enc->generate_master_secret(s, 977 s->session->master_key, 978 tmp_buf, sizeof tmp_buf); 979 980 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf); 981 OPENSSL_cleanse(epms, outl); 982 } 983#endif 984#ifndef OPENSSL_NO_DH 985 else if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 986 { 987 DH *dh_srvr,*dh_clnt; 988 989 if (s->session->sess_cert->peer_dh_tmp != NULL) 990 dh_srvr=s->session->sess_cert->peer_dh_tmp; 991 else 992 { 993 /* we get them from the cert */ 994 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 995 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS); 996 goto err; 997 } 998 999 /* generate a new random key */ 1000 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL) 1001 { 1002 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1003 goto err; 1004 } 1005 if (!DH_generate_key(dh_clnt)) 1006 { 1007 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1008 goto err; 1009 } 1010 1011 /* use the 'p' output buffer for the DH key, but 1012 * make sure to clear it out afterwards */ 1013 1014 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt); 1015 1016 if (n <= 0) 1017 { 1018 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB); 1019 goto err; 1020 } 1021 1022 /* generate master key from the result */ 1023 s->session->master_key_length= 1024 s->method->ssl3_enc->generate_master_secret(s, 1025 s->session->master_key,p,n); 1026 /* clean up */ 1027 memset(p,0,n); 1028 1029 /* send off the data */ 1030 n=BN_num_bytes(dh_clnt->pub_key); 1031 s2n(n,p); 1032 BN_bn2bin(dh_clnt->pub_key,p); 1033 n+=2; 1034 1035 DH_free(dh_clnt); 1036 1037 /* perhaps clean things up a bit EAY EAY EAY EAY*/ 1038 } 1039#endif 1040 else 1041 { 1042 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE); 1043 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,ERR_R_INTERNAL_ERROR); 1044 goto err; 1045 } 1046 1047 d = dtls1_set_message_header(s, d, 1048 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n); 1049 /* 1050 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE; 1051 l2n3(n,d); 1052 l2n(s->d1->handshake_write_seq,d); 1053 s->d1->handshake_write_seq++; 1054 */ 1055 1056 s->state=SSL3_ST_CW_KEY_EXCH_B; 1057 /* number of bytes to write */ 1058 s->init_num=n+DTLS1_HM_HEADER_LENGTH; 1059 s->init_off=0; 1060 1061 /* buffer the message to handle re-xmits */ 1062 dtls1_buffer_message(s, 0); 1063 } 1064 1065 /* SSL3_ST_CW_KEY_EXCH_B */ 1066 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1067err: 1068 return(-1); 1069 } 1070 1071int dtls1_send_client_verify(SSL *s) 1072 { 1073 unsigned char *p,*d; 1074 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1075 EVP_PKEY *pkey; 1076#ifndef OPENSSL_NO_RSA 1077 unsigned u=0; 1078#endif 1079 unsigned long n; 1080#ifndef OPENSSL_NO_DSA 1081 int j; 1082#endif 1083 1084 if (s->state == SSL3_ST_CW_CERT_VRFY_A) 1085 { 1086 d=(unsigned char *)s->init_buf->data; 1087 p= &(d[DTLS1_HM_HEADER_LENGTH]); 1088 pkey=s->cert->key->privatekey; 1089 1090 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2), 1091 &(data[MD5_DIGEST_LENGTH])); 1092 1093#ifndef OPENSSL_NO_RSA 1094 if (pkey->type == EVP_PKEY_RSA) 1095 { 1096 s->method->ssl3_enc->cert_verify_mac(s, 1097 &(s->s3->finish_dgst1),&(data[0])); 1098 if (RSA_sign(NID_md5_sha1, data, 1099 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, 1100 &(p[2]), &u, pkey->pkey.rsa) <= 0 ) 1101 { 1102 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB); 1103 goto err; 1104 } 1105 s2n(u,p); 1106 n=u+2; 1107 } 1108 else 1109#endif 1110#ifndef OPENSSL_NO_DSA 1111 if (pkey->type == EVP_PKEY_DSA) 1112 { 1113 if (!DSA_sign(pkey->save_type, 1114 &(data[MD5_DIGEST_LENGTH]), 1115 SHA_DIGEST_LENGTH,&(p[2]), 1116 (unsigned int *)&j,pkey->pkey.dsa)) 1117 { 1118 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB); 1119 goto err; 1120 } 1121 s2n(j,p); 1122 n=j+2; 1123 } 1124 else 1125#endif 1126 { 1127 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY,ERR_R_INTERNAL_ERROR); 1128 goto err; 1129 } 1130 1131 d = dtls1_set_message_header(s, d, 1132 SSL3_MT_CERTIFICATE_VERIFY, n, 0, n) ; 1133 1134 s->init_num=(int)n+DTLS1_HM_HEADER_LENGTH; 1135 s->init_off=0; 1136 1137 /* buffer the message to handle re-xmits */ 1138 dtls1_buffer_message(s, 0); 1139 1140 s->state = SSL3_ST_CW_CERT_VRFY_B; 1141 } 1142 1143 /* s->state = SSL3_ST_CW_CERT_VRFY_B */ 1144 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1145err: 1146 return(-1); 1147 } 1148 1149int dtls1_send_client_certificate(SSL *s) 1150 { 1151 X509 *x509=NULL; 1152 EVP_PKEY *pkey=NULL; 1153 int i; 1154 unsigned long l; 1155 1156 if (s->state == SSL3_ST_CW_CERT_A) 1157 { 1158 if ((s->cert == NULL) || 1159 (s->cert->key->x509 == NULL) || 1160 (s->cert->key->privatekey == NULL)) 1161 s->state=SSL3_ST_CW_CERT_B; 1162 else 1163 s->state=SSL3_ST_CW_CERT_C; 1164 } 1165 1166 /* We need to get a client cert */ 1167 if (s->state == SSL3_ST_CW_CERT_B) 1168 { 1169 /* If we get an error, we need to 1170 * ssl->rwstate=SSL_X509_LOOKUP; return(-1); 1171 * We then get retied later */ 1172 i=0; 1173 i = ssl_do_client_cert_cb(s, &x509, &pkey); 1174 if (i < 0) 1175 { 1176 s->rwstate=SSL_X509_LOOKUP; 1177 return(-1); 1178 } 1179 s->rwstate=SSL_NOTHING; 1180 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) 1181 { 1182 s->state=SSL3_ST_CW_CERT_B; 1183 if ( !SSL_use_certificate(s,x509) || 1184 !SSL_use_PrivateKey(s,pkey)) 1185 i=0; 1186 } 1187 else if (i == 1) 1188 { 1189 i=0; 1190 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); 1191 } 1192 1193 if (x509 != NULL) X509_free(x509); 1194 if (pkey != NULL) EVP_PKEY_free(pkey); 1195 if (i == 0) 1196 { 1197 if (s->version == SSL3_VERSION) 1198 { 1199 s->s3->tmp.cert_req=0; 1200 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE); 1201 return(1); 1202 } 1203 else 1204 { 1205 s->s3->tmp.cert_req=2; 1206 } 1207 } 1208 1209 /* Ok, we have a cert */ 1210 s->state=SSL3_ST_CW_CERT_C; 1211 } 1212 1213 if (s->state == SSL3_ST_CW_CERT_C) 1214 { 1215 s->state=SSL3_ST_CW_CERT_D; 1216 l=dtls1_output_cert_chain(s, 1217 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509); 1218 s->init_num=(int)l; 1219 s->init_off=0; 1220 1221 /* set header called by dtls1_output_cert_chain() */ 1222 1223 /* buffer the message to handle re-xmits */ 1224 dtls1_buffer_message(s, 0); 1225 } 1226 /* SSL3_ST_CW_CERT_D */ 1227 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1228 } 1229 1230 1231