d1_srvr.c revision 277195
1/* ssl/d1_srvr.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-2007 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 <openssl/buffer.h> 119#include <openssl/rand.h> 120#include <openssl/objects.h> 121#include <openssl/evp.h> 122#include <openssl/x509.h> 123#include <openssl/md5.h> 124#include <openssl/bn.h> 125#ifndef OPENSSL_NO_DH 126#include <openssl/dh.h> 127#endif 128 129static const SSL_METHOD *dtls1_get_server_method(int ver); 130static int dtls1_send_hello_verify_request(SSL *s); 131 132static const SSL_METHOD *dtls1_get_server_method(int ver) 133 { 134 if (ver == DTLS1_VERSION) 135 return(DTLSv1_server_method()); 136 else 137 return(NULL); 138 } 139 140IMPLEMENT_dtls1_meth_func(DTLSv1_server_method, 141 dtls1_accept, 142 ssl_undefined_function, 143 dtls1_get_server_method) 144 145int dtls1_accept(SSL *s) 146 { 147 BUF_MEM *buf; 148 unsigned long Time=(unsigned long)time(NULL); 149 void (*cb)(const SSL *ssl,int type,int val)=NULL; 150 unsigned long alg_k; 151 int ret= -1; 152 int new_state,state,skip=0; 153 int listen; 154#ifndef OPENSSL_NO_SCTP 155 unsigned char sctpauthkey[64]; 156 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; 157#endif 158 159 RAND_add(&Time,sizeof(Time),0); 160 ERR_clear_error(); 161 clear_sys_error(); 162 163 if (s->info_callback != NULL) 164 cb=s->info_callback; 165 else if (s->ctx->info_callback != NULL) 166 cb=s->ctx->info_callback; 167 168 listen = s->d1->listen; 169 170 /* init things to blank */ 171 s->in_handshake++; 172 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 173 174 s->d1->listen = listen; 175#ifndef OPENSSL_NO_SCTP 176 /* Notify SCTP BIO socket to enter handshake 177 * mode and prevent stream identifier other 178 * than 0. Will be ignored if no SCTP is used. 179 */ 180 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL); 181#endif 182 183 if (s->cert == NULL) 184 { 185 SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_NO_CERTIFICATE_SET); 186 return(-1); 187 } 188 189#ifndef OPENSSL_NO_HEARTBEATS 190 /* If we're awaiting a HeartbeatResponse, pretend we 191 * already got and don't await it anymore, because 192 * Heartbeats don't make sense during handshakes anyway. 193 */ 194 if (s->tlsext_hb_pending) 195 { 196 dtls1_stop_timer(s); 197 s->tlsext_hb_pending = 0; 198 s->tlsext_hb_seq++; 199 } 200#endif 201 202 for (;;) 203 { 204 state=s->state; 205 206 switch (s->state) 207 { 208 case SSL_ST_RENEGOTIATE: 209 s->renegotiate=1; 210 /* s->state=SSL_ST_ACCEPT; */ 211 212 case SSL_ST_BEFORE: 213 case SSL_ST_ACCEPT: 214 case SSL_ST_BEFORE|SSL_ST_ACCEPT: 215 case SSL_ST_OK|SSL_ST_ACCEPT: 216 217 s->server=1; 218 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); 219 220 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) 221 { 222 SSLerr(SSL_F_DTLS1_ACCEPT, ERR_R_INTERNAL_ERROR); 223 return -1; 224 } 225 s->type=SSL_ST_ACCEPT; 226 227 if (s->init_buf == NULL) 228 { 229 if ((buf=BUF_MEM_new()) == NULL) 230 { 231 ret= -1; 232 goto end; 233 } 234 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) 235 { 236 ret= -1; 237 goto end; 238 } 239 s->init_buf=buf; 240 } 241 242 if (!ssl3_setup_buffers(s)) 243 { 244 ret= -1; 245 goto end; 246 } 247 248 s->init_num=0; 249 250 if (s->state != SSL_ST_RENEGOTIATE) 251 { 252 /* Ok, we now need to push on a buffering BIO so that 253 * the output is sent in a way that TCP likes :-) 254 * ...but not with SCTP :-) 255 */ 256#ifndef OPENSSL_NO_SCTP 257 if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) 258#endif 259 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; } 260 261 ssl3_init_finished_mac(s); 262 s->state=SSL3_ST_SR_CLNT_HELLO_A; 263 s->ctx->stats.sess_accept++; 264 } 265 else 266 { 267 /* s->state == SSL_ST_RENEGOTIATE, 268 * we will just send a HelloRequest */ 269 s->ctx->stats.sess_accept_renegotiate++; 270 s->state=SSL3_ST_SW_HELLO_REQ_A; 271 } 272 273 break; 274 275 case SSL3_ST_SW_HELLO_REQ_A: 276 case SSL3_ST_SW_HELLO_REQ_B: 277 278 s->shutdown=0; 279 dtls1_start_timer(s); 280 ret=dtls1_send_hello_request(s); 281 if (ret <= 0) goto end; 282 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C; 283 s->state=SSL3_ST_SW_FLUSH; 284 s->init_num=0; 285 286 ssl3_init_finished_mac(s); 287 break; 288 289 case SSL3_ST_SW_HELLO_REQ_C: 290 s->state=SSL_ST_OK; 291 break; 292 293 case SSL3_ST_SR_CLNT_HELLO_A: 294 case SSL3_ST_SR_CLNT_HELLO_B: 295 case SSL3_ST_SR_CLNT_HELLO_C: 296 297 s->shutdown=0; 298 ret=ssl3_get_client_hello(s); 299 if (ret <= 0) goto end; 300 dtls1_stop_timer(s); 301 302 if (ret == 1 && (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)) 303 s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A; 304 else 305 s->state = SSL3_ST_SW_SRVR_HELLO_A; 306 307 s->init_num=0; 308 309 /* Reflect ClientHello sequence to remain stateless while listening */ 310 if (listen) 311 { 312 memcpy(s->s3->write_sequence, s->s3->read_sequence, sizeof(s->s3->write_sequence)); 313 } 314 315 /* If we're just listening, stop here */ 316 if (listen && s->state == SSL3_ST_SW_SRVR_HELLO_A) 317 { 318 ret = 2; 319 s->d1->listen = 0; 320 /* Set expected sequence numbers 321 * to continue the handshake. 322 */ 323 s->d1->handshake_read_seq = 2; 324 s->d1->handshake_write_seq = 1; 325 s->d1->next_handshake_write_seq = 1; 326 goto end; 327 } 328 329 break; 330 331 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A: 332 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B: 333 334 ret = dtls1_send_hello_verify_request(s); 335 if ( ret <= 0) goto end; 336 s->state=SSL3_ST_SW_FLUSH; 337 s->s3->tmp.next_state=SSL3_ST_SR_CLNT_HELLO_A; 338 339 /* HelloVerifyRequest resets Finished MAC */ 340 if (s->version != DTLS1_BAD_VER) 341 ssl3_init_finished_mac(s); 342 break; 343 344#ifndef OPENSSL_NO_SCTP 345 case DTLS1_SCTP_ST_SR_READ_SOCK: 346 347 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) 348 { 349 s->s3->in_read_app_data=2; 350 s->rwstate=SSL_READING; 351 BIO_clear_retry_flags(SSL_get_rbio(s)); 352 BIO_set_retry_read(SSL_get_rbio(s)); 353 ret = -1; 354 goto end; 355 } 356 357 s->state=SSL3_ST_SR_FINISHED_A; 358 break; 359 360 case DTLS1_SCTP_ST_SW_WRITE_SOCK: 361 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s)); 362 if (ret < 0) goto end; 363 364 if (ret == 0) 365 { 366 if (s->d1->next_state != SSL_ST_OK) 367 { 368 s->s3->in_read_app_data=2; 369 s->rwstate=SSL_READING; 370 BIO_clear_retry_flags(SSL_get_rbio(s)); 371 BIO_set_retry_read(SSL_get_rbio(s)); 372 ret = -1; 373 goto end; 374 } 375 } 376 377 s->state=s->d1->next_state; 378 break; 379#endif 380 381 case SSL3_ST_SW_SRVR_HELLO_A: 382 case SSL3_ST_SW_SRVR_HELLO_B: 383 s->renegotiate = 2; 384 dtls1_start_timer(s); 385 ret=dtls1_send_server_hello(s); 386 if (ret <= 0) goto end; 387 388 if (s->hit) 389 { 390#ifndef OPENSSL_NO_SCTP 391 /* Add new shared key for SCTP-Auth, 392 * will be ignored if no SCTP used. 393 */ 394 snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL), 395 DTLS1_SCTP_AUTH_LABEL); 396 397 SSL_export_keying_material(s, sctpauthkey, 398 sizeof(sctpauthkey), labelbuffer, 399 sizeof(labelbuffer), NULL, 0, 0); 400 401 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, 402 sizeof(sctpauthkey), sctpauthkey); 403#endif 404#ifndef OPENSSL_NO_TLSEXT 405 if (s->tlsext_ticket_expected) 406 s->state=SSL3_ST_SW_SESSION_TICKET_A; 407 else 408 s->state=SSL3_ST_SW_CHANGE_A; 409#else 410 s->state=SSL3_ST_SW_CHANGE_A; 411#endif 412 } 413 else 414 s->state=SSL3_ST_SW_CERT_A; 415 s->init_num=0; 416 break; 417 418 case SSL3_ST_SW_CERT_A: 419 case SSL3_ST_SW_CERT_B: 420 /* Check if it is anon DH or normal PSK */ 421 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) 422 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 423 { 424 dtls1_start_timer(s); 425 ret=dtls1_send_server_certificate(s); 426 if (ret <= 0) goto end; 427#ifndef OPENSSL_NO_TLSEXT 428 if (s->tlsext_status_expected) 429 s->state=SSL3_ST_SW_CERT_STATUS_A; 430 else 431 s->state=SSL3_ST_SW_KEY_EXCH_A; 432 } 433 else 434 { 435 skip = 1; 436 s->state=SSL3_ST_SW_KEY_EXCH_A; 437 } 438#else 439 } 440 else 441 skip=1; 442 443 s->state=SSL3_ST_SW_KEY_EXCH_A; 444#endif 445 s->init_num=0; 446 break; 447 448 case SSL3_ST_SW_KEY_EXCH_A: 449 case SSL3_ST_SW_KEY_EXCH_B: 450 alg_k = s->s3->tmp.new_cipher->algorithm_mkey; 451 452 /* 453 * clear this, it may get reset by 454 * send_server_key_exchange 455 */ 456 s->s3->tmp.use_rsa_tmp=0; 457 458 /* only send if a DH key exchange or 459 * RSA but we have a sign only certificate */ 460 if (0 461 /* PSK: send ServerKeyExchange if PSK identity 462 * hint if provided */ 463#ifndef OPENSSL_NO_PSK 464 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint) 465#endif 466 || (alg_k & (SSL_kEDH|SSL_kDHr|SSL_kDHd)) 467 || (alg_k & SSL_kEECDH) 468 || ((alg_k & SSL_kRSA) 469 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL 470 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) 471 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher) 472 ) 473 ) 474 ) 475 ) 476 { 477 dtls1_start_timer(s); 478 ret=dtls1_send_server_key_exchange(s); 479 if (ret <= 0) goto end; 480 } 481 else 482 skip=1; 483 484 s->state=SSL3_ST_SW_CERT_REQ_A; 485 s->init_num=0; 486 break; 487 488 case SSL3_ST_SW_CERT_REQ_A: 489 case SSL3_ST_SW_CERT_REQ_B: 490 if (/* don't request cert unless asked for it: */ 491 !(s->verify_mode & SSL_VERIFY_PEER) || 492 /* if SSL_VERIFY_CLIENT_ONCE is set, 493 * don't request cert during re-negotiation: */ 494 ((s->session->peer != NULL) && 495 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || 496 /* never request cert in anonymous ciphersuites 497 * (see section "Certificate request" in SSL 3 drafts 498 * and in RFC 2246): */ 499 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) && 500 /* ... except when the application insists on verification 501 * (against the specs, but s3_clnt.c accepts this for SSL 3) */ 502 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) || 503 /* never request cert in Kerberos ciphersuites */ 504 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) 505 /* With normal PSK Certificates and 506 * Certificate Requests are omitted */ 507 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 508 { 509 /* no cert request */ 510 skip=1; 511 s->s3->tmp.cert_request=0; 512 s->state=SSL3_ST_SW_SRVR_DONE_A; 513#ifndef OPENSSL_NO_SCTP 514 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) 515 { 516 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A; 517 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK; 518 } 519#endif 520 } 521 else 522 { 523 s->s3->tmp.cert_request=1; 524 dtls1_start_timer(s); 525 ret=dtls1_send_certificate_request(s); 526 if (ret <= 0) goto end; 527#ifndef NETSCAPE_HANG_BUG 528 s->state=SSL3_ST_SW_SRVR_DONE_A; 529#ifndef OPENSSL_NO_SCTP 530 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) 531 { 532 s->d1->next_state = SSL3_ST_SW_SRVR_DONE_A; 533 s->state = DTLS1_SCTP_ST_SW_WRITE_SOCK; 534 } 535#endif 536#else 537 s->state=SSL3_ST_SW_FLUSH; 538 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 539#ifndef OPENSSL_NO_SCTP 540 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) 541 { 542 s->d1->next_state = s->s3->tmp.next_state; 543 s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK; 544 } 545#endif 546#endif 547 s->init_num=0; 548 } 549 break; 550 551 case SSL3_ST_SW_SRVR_DONE_A: 552 case SSL3_ST_SW_SRVR_DONE_B: 553 dtls1_start_timer(s); 554 ret=dtls1_send_server_done(s); 555 if (ret <= 0) goto end; 556 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A; 557 s->state=SSL3_ST_SW_FLUSH; 558 s->init_num=0; 559 break; 560 561 case SSL3_ST_SW_FLUSH: 562 s->rwstate=SSL_WRITING; 563 if (BIO_flush(s->wbio) <= 0) 564 { 565 /* If the write error was fatal, stop trying */ 566 if (!BIO_should_retry(s->wbio)) 567 { 568 s->rwstate=SSL_NOTHING; 569 s->state=s->s3->tmp.next_state; 570 } 571 572 ret= -1; 573 goto end; 574 } 575 s->rwstate=SSL_NOTHING; 576 s->state=s->s3->tmp.next_state; 577 break; 578 579 case SSL3_ST_SR_CERT_A: 580 case SSL3_ST_SR_CERT_B: 581 /* Check for second client hello (MS SGC) */ 582 ret = ssl3_check_client_hello(s); 583 if (ret <= 0) 584 goto end; 585 if (ret == 2) 586 { 587 dtls1_stop_timer(s); 588 s->state = SSL3_ST_SR_CLNT_HELLO_C; 589 } 590 else { 591 /* could be sent for a DH cert, even if we 592 * have not asked for it :-) */ 593 ret=ssl3_get_client_certificate(s); 594 if (ret <= 0) goto end; 595 s->init_num=0; 596 s->state=SSL3_ST_SR_KEY_EXCH_A; 597 } 598 break; 599 600 case SSL3_ST_SR_KEY_EXCH_A: 601 case SSL3_ST_SR_KEY_EXCH_B: 602 ret=ssl3_get_client_key_exchange(s); 603 if (ret <= 0) goto end; 604#ifndef OPENSSL_NO_SCTP 605 /* Add new shared key for SCTP-Auth, 606 * will be ignored if no SCTP used. 607 */ 608 snprintf((char *) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL), 609 DTLS1_SCTP_AUTH_LABEL); 610 611 SSL_export_keying_material(s, sctpauthkey, 612 sizeof(sctpauthkey), labelbuffer, 613 sizeof(labelbuffer), NULL, 0, 0); 614 615 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, 616 sizeof(sctpauthkey), sctpauthkey); 617#endif 618 619 s->state=SSL3_ST_SR_CERT_VRFY_A; 620 s->init_num=0; 621 622 if (ret == 2) 623 { 624 /* For the ECDH ciphersuites when 625 * the client sends its ECDH pub key in 626 * a certificate, the CertificateVerify 627 * message is not sent. 628 */ 629 s->state=SSL3_ST_SR_FINISHED_A; 630 s->init_num = 0; 631 } 632 else 633 { 634 s->state=SSL3_ST_SR_CERT_VRFY_A; 635 s->init_num=0; 636 637 /* We need to get hashes here so if there is 638 * a client cert, it can be verified */ 639 s->method->ssl3_enc->cert_verify_mac(s, 640 NID_md5, 641 &(s->s3->tmp.cert_verify_md[0])); 642 s->method->ssl3_enc->cert_verify_mac(s, 643 NID_sha1, 644 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH])); 645 } 646 break; 647 648 case SSL3_ST_SR_CERT_VRFY_A: 649 case SSL3_ST_SR_CERT_VRFY_B: 650 651 s->d1->change_cipher_spec_ok = 1; 652 /* we should decide if we expected this one */ 653 ret=ssl3_get_cert_verify(s); 654 if (ret <= 0) goto end; 655#ifndef OPENSSL_NO_SCTP 656 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) && 657 state == SSL_ST_RENEGOTIATE) 658 s->state=DTLS1_SCTP_ST_SR_READ_SOCK; 659 else 660#endif 661 s->state=SSL3_ST_SR_FINISHED_A; 662 s->init_num=0; 663 break; 664 665 case SSL3_ST_SR_FINISHED_A: 666 case SSL3_ST_SR_FINISHED_B: 667 s->d1->change_cipher_spec_ok = 1; 668 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A, 669 SSL3_ST_SR_FINISHED_B); 670 if (ret <= 0) goto end; 671 dtls1_stop_timer(s); 672 if (s->hit) 673 s->state=SSL_ST_OK; 674#ifndef OPENSSL_NO_TLSEXT 675 else if (s->tlsext_ticket_expected) 676 s->state=SSL3_ST_SW_SESSION_TICKET_A; 677#endif 678 else 679 s->state=SSL3_ST_SW_CHANGE_A; 680 s->init_num=0; 681 break; 682 683#ifndef OPENSSL_NO_TLSEXT 684 case SSL3_ST_SW_SESSION_TICKET_A: 685 case SSL3_ST_SW_SESSION_TICKET_B: 686 ret=dtls1_send_newsession_ticket(s); 687 if (ret <= 0) goto end; 688 s->state=SSL3_ST_SW_CHANGE_A; 689 s->init_num=0; 690 break; 691 692 case SSL3_ST_SW_CERT_STATUS_A: 693 case SSL3_ST_SW_CERT_STATUS_B: 694 ret=ssl3_send_cert_status(s); 695 if (ret <= 0) goto end; 696 s->state=SSL3_ST_SW_KEY_EXCH_A; 697 s->init_num=0; 698 break; 699 700#endif 701 702 case SSL3_ST_SW_CHANGE_A: 703 case SSL3_ST_SW_CHANGE_B: 704 705 s->session->cipher=s->s3->tmp.new_cipher; 706 if (!s->method->ssl3_enc->setup_key_block(s)) 707 { ret= -1; goto end; } 708 709 ret=dtls1_send_change_cipher_spec(s, 710 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B); 711 712 if (ret <= 0) goto end; 713 714#ifndef OPENSSL_NO_SCTP 715 /* Change to new shared key of SCTP-Auth, 716 * will be ignored if no SCTP used. 717 */ 718 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL); 719#endif 720 721 s->state=SSL3_ST_SW_FINISHED_A; 722 s->init_num=0; 723 724 if (!s->method->ssl3_enc->change_cipher_state(s, 725 SSL3_CHANGE_CIPHER_SERVER_WRITE)) 726 { 727 ret= -1; 728 goto end; 729 } 730 731 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE); 732 break; 733 734 case SSL3_ST_SW_FINISHED_A: 735 case SSL3_ST_SW_FINISHED_B: 736 ret=dtls1_send_finished(s, 737 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B, 738 s->method->ssl3_enc->server_finished_label, 739 s->method->ssl3_enc->server_finished_label_len); 740 if (ret <= 0) goto end; 741 s->state=SSL3_ST_SW_FLUSH; 742 if (s->hit) 743 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A; 744 else 745 { 746 s->s3->tmp.next_state=SSL_ST_OK; 747#ifndef OPENSSL_NO_SCTP 748 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) 749 { 750 s->d1->next_state = s->s3->tmp.next_state; 751 s->s3->tmp.next_state=DTLS1_SCTP_ST_SW_WRITE_SOCK; 752 } 753#endif 754 } 755 s->init_num=0; 756 break; 757 758 case SSL_ST_OK: 759 /* clean a few things up */ 760 ssl3_cleanup_key_block(s); 761 762#if 0 763 BUF_MEM_free(s->init_buf); 764 s->init_buf=NULL; 765#endif 766 767 /* remove buffering on output */ 768 ssl_free_wbio_buffer(s); 769 770 s->init_num=0; 771 772 if (s->renegotiate == 2) /* skipped if we just sent a HelloRequest */ 773 { 774 s->renegotiate=0; 775 s->new_session=0; 776 777 ssl_update_cache(s,SSL_SESS_CACHE_SERVER); 778 779 s->ctx->stats.sess_accept_good++; 780 /* s->server=1; */ 781 s->handshake_func=dtls1_accept; 782 783 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1); 784 } 785 786 ret = 1; 787 788 /* done handshaking, next message is client hello */ 789 s->d1->handshake_read_seq = 0; 790 /* next message is server hello */ 791 s->d1->handshake_write_seq = 0; 792 s->d1->next_handshake_write_seq = 0; 793 goto end; 794 /* break; */ 795 796 default: 797 SSLerr(SSL_F_DTLS1_ACCEPT,SSL_R_UNKNOWN_STATE); 798 ret= -1; 799 goto end; 800 /* break; */ 801 } 802 803 if (!s->s3->tmp.reuse_message && !skip) 804 { 805 if (s->debug) 806 { 807 if ((ret=BIO_flush(s->wbio)) <= 0) 808 goto end; 809 } 810 811 812 if ((cb != NULL) && (s->state != state)) 813 { 814 new_state=s->state; 815 s->state=state; 816 cb(s,SSL_CB_ACCEPT_LOOP,1); 817 s->state=new_state; 818 } 819 } 820 skip=0; 821 } 822end: 823 /* BIO_flush(s->wbio); */ 824 825 s->in_handshake--; 826#ifndef OPENSSL_NO_SCTP 827 /* Notify SCTP BIO socket to leave handshake 828 * mode and prevent stream identifier other 829 * than 0. Will be ignored if no SCTP is used. 830 */ 831 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL); 832#endif 833 834 if (cb != NULL) 835 cb(s,SSL_CB_ACCEPT_EXIT,ret); 836 return(ret); 837 } 838 839int dtls1_send_hello_request(SSL *s) 840 { 841 unsigned char *p; 842 843 if (s->state == SSL3_ST_SW_HELLO_REQ_A) 844 { 845 p=(unsigned char *)s->init_buf->data; 846 p = dtls1_set_message_header(s, p, SSL3_MT_HELLO_REQUEST, 0, 0, 0); 847 848 s->state=SSL3_ST_SW_HELLO_REQ_B; 849 /* number of bytes to write */ 850 s->init_num=DTLS1_HM_HEADER_LENGTH; 851 s->init_off=0; 852 853 /* no need to buffer this message, since there are no retransmit 854 * requests for it */ 855 } 856 857 /* SSL3_ST_SW_HELLO_REQ_B */ 858 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 859 } 860 861int dtls1_send_hello_verify_request(SSL *s) 862 { 863 unsigned int msg_len; 864 unsigned char *msg, *buf, *p; 865 866 if (s->state == DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A) 867 { 868 buf = (unsigned char *)s->init_buf->data; 869 870 msg = p = &(buf[DTLS1_HM_HEADER_LENGTH]); 871 *(p++) = s->version >> 8; 872 *(p++) = s->version & 0xFF; 873 874 if (s->ctx->app_gen_cookie_cb == NULL || 875 s->ctx->app_gen_cookie_cb(s, s->d1->cookie, 876 &(s->d1->cookie_len)) == 0) 877 { 878 SSLerr(SSL_F_DTLS1_SEND_HELLO_VERIFY_REQUEST,ERR_R_INTERNAL_ERROR); 879 return 0; 880 } 881 882 *(p++) = (unsigned char) s->d1->cookie_len; 883 memcpy(p, s->d1->cookie, s->d1->cookie_len); 884 p += s->d1->cookie_len; 885 msg_len = p - msg; 886 887 dtls1_set_message_header(s, buf, 888 DTLS1_MT_HELLO_VERIFY_REQUEST, msg_len, 0, msg_len); 889 890 s->state=DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B; 891 /* number of bytes to write */ 892 s->init_num=p-buf; 893 s->init_off=0; 894 } 895 896 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 897 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 898 } 899 900int dtls1_send_server_hello(SSL *s) 901 { 902 unsigned char *buf; 903 unsigned char *p,*d; 904 int i; 905 unsigned int sl; 906 unsigned long l,Time; 907 908 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) 909 { 910 buf=(unsigned char *)s->init_buf->data; 911 p=s->s3->server_random; 912 Time=(unsigned long)time(NULL); /* Time */ 913 l2n(Time,p); 914 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4); 915 /* Do the message type and length last */ 916 d=p= &(buf[DTLS1_HM_HEADER_LENGTH]); 917 918 *(p++)=s->version>>8; 919 *(p++)=s->version&0xff; 920 921 /* Random stuff */ 922 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE); 923 p+=SSL3_RANDOM_SIZE; 924 925 /* now in theory we have 3 options to sending back the 926 * session id. If it is a re-use, we send back the 927 * old session-id, if it is a new session, we send 928 * back the new session-id or we send back a 0 length 929 * session-id if we want it to be single use. 930 * Currently I will not implement the '0' length session-id 931 * 12-Jan-98 - I'll now support the '0' length stuff. 932 */ 933 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) 934 s->session->session_id_length=0; 935 936 sl=s->session->session_id_length; 937 if (sl > sizeof s->session->session_id) 938 { 939 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR); 940 return -1; 941 } 942 *(p++)=sl; 943 memcpy(p,s->session->session_id,sl); 944 p+=sl; 945 946 /* put the cipher */ 947 if (s->s3->tmp.new_cipher == NULL) 948 return -1; 949 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p); 950 p+=i; 951 952 /* put the compression method */ 953#ifdef OPENSSL_NO_COMP 954 *(p++)=0; 955#else 956 if (s->s3->tmp.new_compression == NULL) 957 *(p++)=0; 958 else 959 *(p++)=s->s3->tmp.new_compression->id; 960#endif 961 962#ifndef OPENSSL_NO_TLSEXT 963 if ((p = ssl_add_serverhello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH)) == NULL) 964 { 965 SSLerr(SSL_F_DTLS1_SEND_SERVER_HELLO,ERR_R_INTERNAL_ERROR); 966 return -1; 967 } 968#endif 969 970 /* do the header */ 971 l=(p-d); 972 d=buf; 973 974 d = dtls1_set_message_header(s, d, SSL3_MT_SERVER_HELLO, l, 0, l); 975 976 s->state=SSL3_ST_SW_SRVR_HELLO_B; 977 /* number of bytes to write */ 978 s->init_num=p-buf; 979 s->init_off=0; 980 981 /* buffer the message to handle re-xmits */ 982 dtls1_buffer_message(s, 0); 983 } 984 985 /* SSL3_ST_SW_SRVR_HELLO_B */ 986 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 987 } 988 989int dtls1_send_server_done(SSL *s) 990 { 991 unsigned char *p; 992 993 if (s->state == SSL3_ST_SW_SRVR_DONE_A) 994 { 995 p=(unsigned char *)s->init_buf->data; 996 997 /* do the header */ 998 p = dtls1_set_message_header(s, p, SSL3_MT_SERVER_DONE, 0, 0, 0); 999 1000 s->state=SSL3_ST_SW_SRVR_DONE_B; 1001 /* number of bytes to write */ 1002 s->init_num=DTLS1_HM_HEADER_LENGTH; 1003 s->init_off=0; 1004 1005 /* buffer the message to handle re-xmits */ 1006 dtls1_buffer_message(s, 0); 1007 } 1008 1009 /* SSL3_ST_SW_SRVR_DONE_B */ 1010 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1011 } 1012 1013int dtls1_send_server_key_exchange(SSL *s) 1014 { 1015#ifndef OPENSSL_NO_RSA 1016 unsigned char *q; 1017 int j,num; 1018 RSA *rsa; 1019 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH]; 1020 unsigned int u; 1021#endif 1022#ifndef OPENSSL_NO_DH 1023 DH *dh=NULL,*dhp; 1024#endif 1025#ifndef OPENSSL_NO_ECDH 1026 EC_KEY *ecdh=NULL, *ecdhp; 1027 unsigned char *encodedPoint = NULL; 1028 int encodedlen = 0; 1029 int curve_id = 0; 1030 BN_CTX *bn_ctx = NULL; 1031#endif 1032 EVP_PKEY *pkey; 1033 unsigned char *p,*d; 1034 int al,i; 1035 unsigned long type; 1036 int n; 1037 CERT *cert; 1038 BIGNUM *r[4]; 1039 int nr[4],kn; 1040 BUF_MEM *buf; 1041 EVP_MD_CTX md_ctx; 1042 1043 EVP_MD_CTX_init(&md_ctx); 1044 if (s->state == SSL3_ST_SW_KEY_EXCH_A) 1045 { 1046 type=s->s3->tmp.new_cipher->algorithm_mkey; 1047 cert=s->cert; 1048 1049 buf=s->init_buf; 1050 1051 r[0]=r[1]=r[2]=r[3]=NULL; 1052 n=0; 1053#ifndef OPENSSL_NO_RSA 1054 if (type & SSL_kRSA) 1055 { 1056 rsa=cert->rsa_tmp; 1057 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) 1058 { 1059 rsa=s->cert->rsa_tmp_cb(s, 1060 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1061 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1062 if(rsa == NULL) 1063 { 1064 al=SSL_AD_HANDSHAKE_FAILURE; 1065 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY); 1066 goto f_err; 1067 } 1068 RSA_up_ref(rsa); 1069 cert->rsa_tmp=rsa; 1070 } 1071 if (rsa == NULL) 1072 { 1073 al=SSL_AD_HANDSHAKE_FAILURE; 1074 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY); 1075 goto f_err; 1076 } 1077 r[0]=rsa->n; 1078 r[1]=rsa->e; 1079 s->s3->tmp.use_rsa_tmp=1; 1080 } 1081 else 1082#endif 1083#ifndef OPENSSL_NO_DH 1084 if (type & SSL_kEDH) 1085 { 1086 dhp=cert->dh_tmp; 1087 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL)) 1088 dhp=s->cert->dh_tmp_cb(s, 1089 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1090 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1091 if (dhp == NULL) 1092 { 1093 al=SSL_AD_HANDSHAKE_FAILURE; 1094 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY); 1095 goto f_err; 1096 } 1097 1098 if (s->s3->tmp.dh != NULL) 1099 { 1100 DH_free(dh); 1101 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1102 goto err; 1103 } 1104 1105 if ((dh=DHparams_dup(dhp)) == NULL) 1106 { 1107 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1108 goto err; 1109 } 1110 1111 s->s3->tmp.dh=dh; 1112 if ((dhp->pub_key == NULL || 1113 dhp->priv_key == NULL || 1114 (s->options & SSL_OP_SINGLE_DH_USE))) 1115 { 1116 if(!DH_generate_key(dh)) 1117 { 1118 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, 1119 ERR_R_DH_LIB); 1120 goto err; 1121 } 1122 } 1123 else 1124 { 1125 dh->pub_key=BN_dup(dhp->pub_key); 1126 dh->priv_key=BN_dup(dhp->priv_key); 1127 if ((dh->pub_key == NULL) || 1128 (dh->priv_key == NULL)) 1129 { 1130 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB); 1131 goto err; 1132 } 1133 } 1134 r[0]=dh->p; 1135 r[1]=dh->g; 1136 r[2]=dh->pub_key; 1137 } 1138 else 1139#endif 1140#ifndef OPENSSL_NO_ECDH 1141 if (type & SSL_kEECDH) 1142 { 1143 const EC_GROUP *group; 1144 1145 ecdhp=cert->ecdh_tmp; 1146 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) 1147 { 1148 ecdhp=s->cert->ecdh_tmp_cb(s, 1149 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher), 1150 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)); 1151 } 1152 if (ecdhp == NULL) 1153 { 1154 al=SSL_AD_HANDSHAKE_FAILURE; 1155 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY); 1156 goto f_err; 1157 } 1158 1159 if (s->s3->tmp.ecdh != NULL) 1160 { 1161 EC_KEY_free(s->s3->tmp.ecdh); 1162 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR); 1163 goto err; 1164 } 1165 1166 /* Duplicate the ECDH structure. */ 1167 if (ecdhp == NULL) 1168 { 1169 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1170 goto err; 1171 } 1172 if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) 1173 { 1174 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1175 goto err; 1176 } 1177 1178 s->s3->tmp.ecdh=ecdh; 1179 if ((EC_KEY_get0_public_key(ecdh) == NULL) || 1180 (EC_KEY_get0_private_key(ecdh) == NULL) || 1181 (s->options & SSL_OP_SINGLE_ECDH_USE)) 1182 { 1183 if(!EC_KEY_generate_key(ecdh)) 1184 { 1185 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1186 goto err; 1187 } 1188 } 1189 1190 if (((group = EC_KEY_get0_group(ecdh)) == NULL) || 1191 (EC_KEY_get0_public_key(ecdh) == NULL) || 1192 (EC_KEY_get0_private_key(ecdh) == NULL)) 1193 { 1194 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1195 goto err; 1196 } 1197 1198 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && 1199 (EC_GROUP_get_degree(group) > 163)) 1200 { 1201 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER); 1202 goto err; 1203 } 1204 1205 /* XXX: For now, we only support ephemeral ECDH 1206 * keys over named (not generic) curves. For 1207 * supported named curves, curve_id is non-zero. 1208 */ 1209 if ((curve_id = 1210 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group))) 1211 == 0) 1212 { 1213 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 1214 goto err; 1215 } 1216 1217 /* Encode the public key. 1218 * First check the size of encoding and 1219 * allocate memory accordingly. 1220 */ 1221 encodedlen = EC_POINT_point2oct(group, 1222 EC_KEY_get0_public_key(ecdh), 1223 POINT_CONVERSION_UNCOMPRESSED, 1224 NULL, 0, NULL); 1225 1226 encodedPoint = (unsigned char *) 1227 OPENSSL_malloc(encodedlen*sizeof(unsigned char)); 1228 bn_ctx = BN_CTX_new(); 1229 if ((encodedPoint == NULL) || (bn_ctx == NULL)) 1230 { 1231 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE); 1232 goto err; 1233 } 1234 1235 1236 encodedlen = EC_POINT_point2oct(group, 1237 EC_KEY_get0_public_key(ecdh), 1238 POINT_CONVERSION_UNCOMPRESSED, 1239 encodedPoint, encodedlen, bn_ctx); 1240 1241 if (encodedlen == 0) 1242 { 1243 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB); 1244 goto err; 1245 } 1246 1247 BN_CTX_free(bn_ctx); bn_ctx=NULL; 1248 1249 /* XXX: For now, we only support named (not 1250 * generic) curves in ECDH ephemeral key exchanges. 1251 * In this situation, we need four additional bytes 1252 * to encode the entire ServerECDHParams 1253 * structure. 1254 */ 1255 n = 4 + encodedlen; 1256 1257 /* We'll generate the serverKeyExchange message 1258 * explicitly so we can set these to NULLs 1259 */ 1260 r[0]=NULL; 1261 r[1]=NULL; 1262 r[2]=NULL; 1263 r[3]=NULL; 1264 } 1265 else 1266#endif /* !OPENSSL_NO_ECDH */ 1267#ifndef OPENSSL_NO_PSK 1268 if (type & SSL_kPSK) 1269 { 1270 /* reserve size for record length and PSK identity hint*/ 1271 n+=2+strlen(s->ctx->psk_identity_hint); 1272 } 1273 else 1274#endif /* !OPENSSL_NO_PSK */ 1275 { 1276 al=SSL_AD_HANDSHAKE_FAILURE; 1277 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 1278 goto f_err; 1279 } 1280 for (i=0; r[i] != NULL; i++) 1281 { 1282 nr[i]=BN_num_bytes(r[i]); 1283 n+=2+nr[i]; 1284 } 1285 1286 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) 1287 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) 1288 { 1289 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher, NULL)) 1290 == NULL) 1291 { 1292 al=SSL_AD_DECODE_ERROR; 1293 goto f_err; 1294 } 1295 kn=EVP_PKEY_size(pkey); 1296 } 1297 else 1298 { 1299 pkey=NULL; 1300 kn=0; 1301 } 1302 1303 if (!BUF_MEM_grow_clean(buf,n+DTLS1_HM_HEADER_LENGTH+kn)) 1304 { 1305 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF); 1306 goto err; 1307 } 1308 d=(unsigned char *)s->init_buf->data; 1309 p= &(d[DTLS1_HM_HEADER_LENGTH]); 1310 1311 for (i=0; r[i] != NULL; i++) 1312 { 1313 s2n(nr[i],p); 1314 BN_bn2bin(r[i],p); 1315 p+=nr[i]; 1316 } 1317 1318#ifndef OPENSSL_NO_ECDH 1319 if (type & SSL_kEECDH) 1320 { 1321 /* XXX: For now, we only support named (not generic) curves. 1322 * In this situation, the serverKeyExchange message has: 1323 * [1 byte CurveType], [2 byte CurveName] 1324 * [1 byte length of encoded point], followed by 1325 * the actual encoded point itself 1326 */ 1327 *p = NAMED_CURVE_TYPE; 1328 p += 1; 1329 *p = 0; 1330 p += 1; 1331 *p = curve_id; 1332 p += 1; 1333 *p = encodedlen; 1334 p += 1; 1335 memcpy((unsigned char*)p, 1336 (unsigned char *)encodedPoint, 1337 encodedlen); 1338 OPENSSL_free(encodedPoint); 1339 p += encodedlen; 1340 } 1341#endif 1342 1343#ifndef OPENSSL_NO_PSK 1344 if (type & SSL_kPSK) 1345 { 1346 /* copy PSK identity hint */ 1347 s2n(strlen(s->ctx->psk_identity_hint), p); 1348 strncpy((char *)p, s->ctx->psk_identity_hint, strlen(s->ctx->psk_identity_hint)); 1349 p+=strlen(s->ctx->psk_identity_hint); 1350 } 1351#endif 1352 1353 /* not anonymous */ 1354 if (pkey != NULL) 1355 { 1356 /* n is the length of the params, they start at 1357 * &(d[DTLS1_HM_HEADER_LENGTH]) and p points to the space 1358 * at the end. */ 1359#ifndef OPENSSL_NO_RSA 1360 if (pkey->type == EVP_PKEY_RSA) 1361 { 1362 q=md_buf; 1363 j=0; 1364 for (num=2; num > 0; num--) 1365 { 1366 EVP_DigestInit_ex(&md_ctx,(num == 2) 1367 ?s->ctx->md5:s->ctx->sha1, NULL); 1368 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1369 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1370 EVP_DigestUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n); 1371 EVP_DigestFinal_ex(&md_ctx,q, 1372 (unsigned int *)&i); 1373 q+=i; 1374 j+=i; 1375 } 1376 if (RSA_sign(NID_md5_sha1, md_buf, j, 1377 &(p[2]), &u, pkey->pkey.rsa) <= 0) 1378 { 1379 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA); 1380 goto err; 1381 } 1382 s2n(u,p); 1383 n+=u+2; 1384 } 1385 else 1386#endif 1387#if !defined(OPENSSL_NO_DSA) 1388 if (pkey->type == EVP_PKEY_DSA) 1389 { 1390 /* lets do DSS */ 1391 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL); 1392 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1393 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1394 EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n); 1395 if (!EVP_SignFinal(&md_ctx,&(p[2]), 1396 (unsigned int *)&i,pkey)) 1397 { 1398 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA); 1399 goto err; 1400 } 1401 s2n(i,p); 1402 n+=i+2; 1403 } 1404 else 1405#endif 1406#if !defined(OPENSSL_NO_ECDSA) 1407 if (pkey->type == EVP_PKEY_EC) 1408 { 1409 /* let's do ECDSA */ 1410 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL); 1411 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE); 1412 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE); 1413 EVP_SignUpdate(&md_ctx,&(d[DTLS1_HM_HEADER_LENGTH]),n); 1414 if (!EVP_SignFinal(&md_ctx,&(p[2]), 1415 (unsigned int *)&i,pkey)) 1416 { 1417 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA); 1418 goto err; 1419 } 1420 s2n(i,p); 1421 n+=i+2; 1422 } 1423 else 1424#endif 1425 { 1426 /* Is this error check actually needed? */ 1427 al=SSL_AD_HANDSHAKE_FAILURE; 1428 SSLerr(SSL_F_DTLS1_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE); 1429 goto f_err; 1430 } 1431 } 1432 1433 d = dtls1_set_message_header(s, d, 1434 SSL3_MT_SERVER_KEY_EXCHANGE, n, 0, n); 1435 1436 /* we should now have things packed up, so lets send 1437 * it off */ 1438 s->init_num=n+DTLS1_HM_HEADER_LENGTH; 1439 s->init_off=0; 1440 1441 /* buffer the message to handle re-xmits */ 1442 dtls1_buffer_message(s, 0); 1443 } 1444 1445 s->state = SSL3_ST_SW_KEY_EXCH_B; 1446 EVP_MD_CTX_cleanup(&md_ctx); 1447 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1448f_err: 1449 ssl3_send_alert(s,SSL3_AL_FATAL,al); 1450err: 1451#ifndef OPENSSL_NO_ECDH 1452 if (encodedPoint != NULL) OPENSSL_free(encodedPoint); 1453 BN_CTX_free(bn_ctx); 1454#endif 1455 EVP_MD_CTX_cleanup(&md_ctx); 1456 return(-1); 1457 } 1458 1459int dtls1_send_certificate_request(SSL *s) 1460 { 1461 unsigned char *p,*d; 1462 int i,j,nl,off,n; 1463 STACK_OF(X509_NAME) *sk=NULL; 1464 X509_NAME *name; 1465 BUF_MEM *buf; 1466 unsigned int msg_len; 1467 1468 if (s->state == SSL3_ST_SW_CERT_REQ_A) 1469 { 1470 buf=s->init_buf; 1471 1472 d=p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]); 1473 1474 /* get the list of acceptable cert types */ 1475 p++; 1476 n=ssl3_get_req_cert_type(s,p); 1477 d[0]=n; 1478 p+=n; 1479 n++; 1480 1481 off=n; 1482 p+=2; 1483 n+=2; 1484 1485 sk=SSL_get_client_CA_list(s); 1486 nl=0; 1487 if (sk != NULL) 1488 { 1489 for (i=0; i<sk_X509_NAME_num(sk); i++) 1490 { 1491 name=sk_X509_NAME_value(sk,i); 1492 j=i2d_X509_NAME(name,NULL); 1493 if (!BUF_MEM_grow_clean(buf,DTLS1_HM_HEADER_LENGTH+n+j+2)) 1494 { 1495 SSLerr(SSL_F_DTLS1_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB); 1496 goto err; 1497 } 1498 p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+n]); 1499 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) 1500 { 1501 s2n(j,p); 1502 i2d_X509_NAME(name,&p); 1503 n+=2+j; 1504 nl+=2+j; 1505 } 1506 else 1507 { 1508 d=p; 1509 i2d_X509_NAME(name,&p); 1510 j-=2; s2n(j,d); j+=2; 1511 n+=j; 1512 nl+=j; 1513 } 1514 } 1515 } 1516 /* else no CA names */ 1517 p=(unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH+off]); 1518 s2n(nl,p); 1519 1520 d=(unsigned char *)buf->data; 1521 *(d++)=SSL3_MT_CERTIFICATE_REQUEST; 1522 l2n3(n,d); 1523 s2n(s->d1->handshake_write_seq,d); 1524 s->d1->handshake_write_seq++; 1525 1526 /* we should now have things packed up, so lets send 1527 * it off */ 1528 1529 s->init_num=n+DTLS1_HM_HEADER_LENGTH; 1530 s->init_off=0; 1531#ifdef NETSCAPE_HANG_BUG 1532/* XXX: what to do about this? */ 1533 p=(unsigned char *)s->init_buf->data + s->init_num; 1534 1535 /* do the header */ 1536 *(p++)=SSL3_MT_SERVER_DONE; 1537 *(p++)=0; 1538 *(p++)=0; 1539 *(p++)=0; 1540 s->init_num += 4; 1541#endif 1542 1543 /* XDTLS: set message header ? */ 1544 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; 1545 dtls1_set_message_header(s, (void *)s->init_buf->data, 1546 SSL3_MT_CERTIFICATE_REQUEST, msg_len, 0, msg_len); 1547 1548 /* buffer the message to handle re-xmits */ 1549 dtls1_buffer_message(s, 0); 1550 1551 s->state = SSL3_ST_SW_CERT_REQ_B; 1552 } 1553 1554 /* SSL3_ST_SW_CERT_REQ_B */ 1555 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1556err: 1557 return(-1); 1558 } 1559 1560int dtls1_send_server_certificate(SSL *s) 1561 { 1562 unsigned long l; 1563 X509 *x; 1564 1565 if (s->state == SSL3_ST_SW_CERT_A) 1566 { 1567 x=ssl_get_server_send_cert(s); 1568 if (x == NULL) 1569 { 1570 /* VRS: allow null cert if auth == KRB5 */ 1571 if ((s->s3->tmp.new_cipher->algorithm_mkey != SSL_kKRB5) || 1572 (s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5)) 1573 { 1574 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR); 1575 return(0); 1576 } 1577 } 1578 1579 l=dtls1_output_cert_chain(s,x); 1580 s->state=SSL3_ST_SW_CERT_B; 1581 s->init_num=(int)l; 1582 s->init_off=0; 1583 1584 /* buffer the message to handle re-xmits */ 1585 dtls1_buffer_message(s, 0); 1586 } 1587 1588 /* SSL3_ST_SW_CERT_B */ 1589 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1590 } 1591 1592#ifndef OPENSSL_NO_TLSEXT 1593int dtls1_send_newsession_ticket(SSL *s) 1594 { 1595 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) 1596 { 1597 unsigned char *p, *senc, *macstart; 1598 int len, slen; 1599 unsigned int hlen, msg_len; 1600 EVP_CIPHER_CTX ctx; 1601 HMAC_CTX hctx; 1602 SSL_CTX *tctx = s->initial_ctx; 1603 unsigned char iv[EVP_MAX_IV_LENGTH]; 1604 unsigned char key_name[16]; 1605 1606 /* get session encoding length */ 1607 slen = i2d_SSL_SESSION(s->session, NULL); 1608 /* Some length values are 16 bits, so forget it if session is 1609 * too long 1610 */ 1611 if (slen > 0xFF00) 1612 return -1; 1613 /* Grow buffer if need be: the length calculation is as 1614 * follows 12 (DTLS handshake message header) + 1615 * 4 (ticket lifetime hint) + 2 (ticket length) + 1616 * 16 (key name) + max_iv_len (iv length) + 1617 * session_length + max_enc_block_size (max encrypted session 1618 * length) + max_md_size (HMAC). 1619 */ 1620 if (!BUF_MEM_grow(s->init_buf, 1621 DTLS1_HM_HEADER_LENGTH + 22 + EVP_MAX_IV_LENGTH + 1622 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen)) 1623 return -1; 1624 senc = OPENSSL_malloc(slen); 1625 if (!senc) 1626 return -1; 1627 p = senc; 1628 i2d_SSL_SESSION(s->session, &p); 1629 1630 p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]); 1631 EVP_CIPHER_CTX_init(&ctx); 1632 HMAC_CTX_init(&hctx); 1633 /* Initialize HMAC and cipher contexts. If callback present 1634 * it does all the work otherwise use generated values 1635 * from parent ctx. 1636 */ 1637 if (tctx->tlsext_ticket_key_cb) 1638 { 1639 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, 1640 &hctx, 1) < 0) 1641 { 1642 OPENSSL_free(senc); 1643 return -1; 1644 } 1645 } 1646 else 1647 { 1648 RAND_pseudo_bytes(iv, 16); 1649 EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, 1650 tctx->tlsext_tick_aes_key, iv); 1651 HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, 1652 tlsext_tick_md(), NULL); 1653 memcpy(key_name, tctx->tlsext_tick_key_name, 16); 1654 } 1655 l2n(s->session->tlsext_tick_lifetime_hint, p); 1656 /* Skip ticket length for now */ 1657 p += 2; 1658 /* Output key name */ 1659 macstart = p; 1660 memcpy(p, key_name, 16); 1661 p += 16; 1662 /* output IV */ 1663 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); 1664 p += EVP_CIPHER_CTX_iv_length(&ctx); 1665 /* Encrypt session data */ 1666 EVP_EncryptUpdate(&ctx, p, &len, senc, slen); 1667 p += len; 1668 EVP_EncryptFinal(&ctx, p, &len); 1669 p += len; 1670 EVP_CIPHER_CTX_cleanup(&ctx); 1671 1672 HMAC_Update(&hctx, macstart, p - macstart); 1673 HMAC_Final(&hctx, p, &hlen); 1674 HMAC_CTX_cleanup(&hctx); 1675 1676 p += hlen; 1677 /* Now write out lengths: p points to end of data written */ 1678 /* Total length */ 1679 len = p - (unsigned char *)(s->init_buf->data); 1680 /* Ticket length */ 1681 p=(unsigned char *)&(s->init_buf->data[DTLS1_HM_HEADER_LENGTH]) + 4; 1682 s2n(len - DTLS1_HM_HEADER_LENGTH - 6, p); 1683 1684 /* number of bytes to write */ 1685 s->init_num= len; 1686 s->state=SSL3_ST_SW_SESSION_TICKET_B; 1687 s->init_off=0; 1688 OPENSSL_free(senc); 1689 1690 /* XDTLS: set message header ? */ 1691 msg_len = s->init_num - DTLS1_HM_HEADER_LENGTH; 1692 dtls1_set_message_header(s, (void *)s->init_buf->data, 1693 SSL3_MT_NEWSESSION_TICKET, msg_len, 0, msg_len); 1694 1695 /* buffer the message to handle re-xmits */ 1696 dtls1_buffer_message(s, 0); 1697 } 1698 1699 /* SSL3_ST_SW_SESSION_TICKET_B */ 1700 return(dtls1_do_write(s,SSL3_RT_HANDSHAKE)); 1701 } 1702#endif 1703