3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
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.
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).
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.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
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)"
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
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.]
117 #include "ssl_locl.h"
118 #ifndef OPENSSL_NO_KRB5
119 # include "kssl_lcl.h"
121 #include <openssl/buffer.h>
122 #include <openssl/rand.h>
123 #include <openssl/objects.h>
124 #include <openssl/evp.h>
125 #include <openssl/md5.h>
126 #include <openssl/bn.h>
127 #ifndef OPENSSL_NO_DH
128 # include <openssl/dh.h>
131 static const SSL_METHOD *dtls1_get_client_method(int ver);
132 static int dtls1_get_hello_verify(SSL *s);
134 static const SSL_METHOD *dtls1_get_client_method(int ver)
136 if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
137 return (DTLSv1_client_method());
142 IMPLEMENT_dtls1_meth_func(DTLSv1_client_method,
143 ssl_undefined_function,
144 dtls1_connect, dtls1_get_client_method)
146 int dtls1_connect(SSL *s)
149 unsigned long Time = (unsigned long)time(NULL);
150 void (*cb) (const SSL *ssl, int type, int val) = NULL;
152 int new_state, state, skip = 0;
153 #ifndef OPENSSL_NO_SCTP
154 unsigned char sctpauthkey[64];
155 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
158 RAND_add(&Time, sizeof(Time), 0);
162 if (s->info_callback != NULL)
163 cb = s->info_callback;
164 else if (s->ctx->info_callback != NULL)
165 cb = s->ctx->info_callback;
168 if (!SSL_in_init(s) || SSL_in_before(s))
171 #ifndef OPENSSL_NO_SCTP
173 * Notify SCTP BIO socket to enter handshake mode and prevent stream
174 * identifier other than 0. Will be ignored if no SCTP is used.
176 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
177 s->in_handshake, NULL);
180 #ifndef OPENSSL_NO_HEARTBEATS
182 * If we're awaiting a HeartbeatResponse, pretend we already got and
183 * don't await it anymore, because Heartbeats don't make sense during
186 if (s->tlsext_hb_pending) {
188 s->tlsext_hb_pending = 0;
197 case SSL_ST_RENEGOTIATE:
199 s->state = SSL_ST_CONNECT;
200 s->ctx->stats.sess_connect_renegotiate++;
204 case SSL_ST_BEFORE | SSL_ST_CONNECT:
205 case SSL_ST_OK | SSL_ST_CONNECT:
209 cb(s, SSL_CB_HANDSHAKE_START, 1);
211 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
212 (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00)) {
213 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
215 s->state = SSL_ST_ERR;
219 /* s->version=SSL3_VERSION; */
220 s->type = SSL_ST_CONNECT;
222 if (s->init_buf == NULL) {
223 if ((buf = BUF_MEM_new()) == NULL) {
225 s->state = SSL_ST_ERR;
228 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
230 s->state = SSL_ST_ERR;
237 if (!ssl3_setup_buffers(s)) {
239 s->state = SSL_ST_ERR;
243 /* setup buffing BIO */
244 if (!ssl_init_wbio_buffer(s, 0)) {
246 s->state = SSL_ST_ERR;
250 /* don't push the buffering BIO quite yet */
252 s->state = SSL3_ST_CW_CLNT_HELLO_A;
253 s->ctx->stats.sess_connect++;
255 /* mark client_random uninitialized */
256 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
257 s->d1->send_cookie = 0;
259 s->d1->change_cipher_spec_ok = 0;
261 * Should have been reset by ssl3_get_finished, too.
263 s->s3->change_cipher_spec = 0;
266 #ifndef OPENSSL_NO_SCTP
267 case DTLS1_SCTP_ST_CR_READ_SOCK:
269 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
270 s->s3->in_read_app_data = 2;
271 s->rwstate = SSL_READING;
272 BIO_clear_retry_flags(SSL_get_rbio(s));
273 BIO_set_retry_read(SSL_get_rbio(s));
278 s->state = s->s3->tmp.next_state;
281 case DTLS1_SCTP_ST_CW_WRITE_SOCK:
282 /* read app data until dry event */
284 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
289 s->s3->in_read_app_data = 2;
290 s->rwstate = SSL_READING;
291 BIO_clear_retry_flags(SSL_get_rbio(s));
292 BIO_set_retry_read(SSL_get_rbio(s));
297 s->state = s->d1->next_state;
301 case SSL3_ST_CW_CLNT_HELLO_A:
304 /* every DTLS ClientHello resets Finished MAC */
305 ssl3_init_finished_mac(s);
307 case SSL3_ST_CW_CLNT_HELLO_B:
308 dtls1_start_timer(s);
309 ret = dtls1_client_hello(s);
313 if (s->d1->send_cookie) {
314 s->state = SSL3_ST_CW_FLUSH;
315 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
317 s->state = SSL3_ST_CR_SRVR_HELLO_A;
321 #ifndef OPENSSL_NO_SCTP
322 /* Disable buffering for SCTP */
323 if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) {
326 * turn on buffering for the next lot of output
328 if (s->bbio != s->wbio)
329 s->wbio = BIO_push(s->bbio, s->wbio);
330 #ifndef OPENSSL_NO_SCTP
336 case SSL3_ST_CR_SRVR_HELLO_A:
337 case SSL3_ST_CR_SRVR_HELLO_B:
338 ret = ssl3_get_server_hello(s);
343 #ifndef OPENSSL_NO_SCTP
345 * Add new shared key for SCTP-Auth, will be ignored if
348 snprintf((char *)labelbuffer,
349 sizeof(DTLS1_SCTP_AUTH_LABEL),
350 DTLS1_SCTP_AUTH_LABEL);
352 if (SSL_export_keying_material(s, sctpauthkey,
355 sizeof(labelbuffer), NULL, 0,
358 s->state = SSL_ST_ERR;
362 BIO_ctrl(SSL_get_wbio(s),
363 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
364 sizeof(sctpauthkey), sctpauthkey);
367 s->state = SSL3_ST_CR_FINISHED_A;
368 if (s->tlsext_ticket_expected) {
369 /* receive renewed session ticket */
370 s->state = SSL3_ST_CR_SESSION_TICKET_A;
373 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
378 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
379 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
381 ret = dtls1_get_hello_verify(s);
385 if (s->d1->send_cookie) /* start again, with a cookie */
386 s->state = SSL3_ST_CW_CLNT_HELLO_A;
388 s->state = SSL3_ST_CR_CERT_A;
392 case SSL3_ST_CR_CERT_A:
393 case SSL3_ST_CR_CERT_B:
394 /* Check if it is anon DH or PSK */
395 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
396 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
397 ret = ssl3_get_server_certificate(s);
400 #ifndef OPENSSL_NO_TLSEXT
401 if (s->tlsext_status_expected)
402 s->state = SSL3_ST_CR_CERT_STATUS_A;
404 s->state = SSL3_ST_CR_KEY_EXCH_A;
407 s->state = SSL3_ST_CR_KEY_EXCH_A;
413 s->state = SSL3_ST_CR_KEY_EXCH_A;
418 case SSL3_ST_CR_KEY_EXCH_A:
419 case SSL3_ST_CR_KEY_EXCH_B:
420 ret = ssl3_get_key_exchange(s);
423 s->state = SSL3_ST_CR_CERT_REQ_A;
427 * at this point we check that we have the required stuff from
430 if (!ssl3_check_cert_and_algorithm(s)) {
432 s->state = SSL_ST_ERR;
437 case SSL3_ST_CR_CERT_REQ_A:
438 case SSL3_ST_CR_CERT_REQ_B:
439 ret = ssl3_get_certificate_request(s);
442 s->state = SSL3_ST_CR_SRVR_DONE_A;
446 case SSL3_ST_CR_SRVR_DONE_A:
447 case SSL3_ST_CR_SRVR_DONE_B:
448 ret = ssl3_get_server_done(s);
452 if (s->s3->tmp.cert_req)
453 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
455 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
458 #ifndef OPENSSL_NO_SCTP
459 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
460 state == SSL_ST_RENEGOTIATE)
461 s->state = DTLS1_SCTP_ST_CR_READ_SOCK;
464 s->state = s->s3->tmp.next_state;
467 case SSL3_ST_CW_CERT_A:
468 case SSL3_ST_CW_CERT_B:
469 case SSL3_ST_CW_CERT_C:
470 case SSL3_ST_CW_CERT_D:
471 dtls1_start_timer(s);
472 ret = dtls1_send_client_certificate(s);
475 s->state = SSL3_ST_CW_KEY_EXCH_A;
479 case SSL3_ST_CW_KEY_EXCH_A:
480 case SSL3_ST_CW_KEY_EXCH_B:
481 dtls1_start_timer(s);
482 ret = dtls1_send_client_key_exchange(s);
486 #ifndef OPENSSL_NO_SCTP
488 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
491 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
492 DTLS1_SCTP_AUTH_LABEL);
494 if (SSL_export_keying_material(s, sctpauthkey,
495 sizeof(sctpauthkey), labelbuffer,
496 sizeof(labelbuffer), NULL, 0, 0) <= 0) {
498 s->state = SSL_ST_ERR;
502 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
503 sizeof(sctpauthkey), sctpauthkey);
507 * EAY EAY EAY need to check for DH fix cert sent back
510 * For TLS, cert_req is set to 2, so a cert chain of nothing is
511 * sent, but no verify packet is sent
513 if (s->s3->tmp.cert_req == 1) {
514 s->state = SSL3_ST_CW_CERT_VRFY_A;
516 #ifndef OPENSSL_NO_SCTP
517 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
518 s->d1->next_state = SSL3_ST_CW_CHANGE_A;
519 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
522 s->state = SSL3_ST_CW_CHANGE_A;
528 case SSL3_ST_CW_CERT_VRFY_A:
529 case SSL3_ST_CW_CERT_VRFY_B:
530 dtls1_start_timer(s);
531 ret = dtls1_send_client_verify(s);
534 #ifndef OPENSSL_NO_SCTP
535 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
536 s->d1->next_state = SSL3_ST_CW_CHANGE_A;
537 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
540 s->state = SSL3_ST_CW_CHANGE_A;
544 case SSL3_ST_CW_CHANGE_A:
545 case SSL3_ST_CW_CHANGE_B:
547 dtls1_start_timer(s);
548 ret = dtls1_send_change_cipher_spec(s,
550 SSL3_ST_CW_CHANGE_B);
554 s->state = SSL3_ST_CW_FINISHED_A;
557 s->session->cipher = s->s3->tmp.new_cipher;
558 #ifdef OPENSSL_NO_COMP
559 s->session->compress_meth = 0;
561 if (s->s3->tmp.new_compression == NULL)
562 s->session->compress_meth = 0;
564 s->session->compress_meth = s->s3->tmp.new_compression->id;
566 if (!s->method->ssl3_enc->setup_key_block(s)) {
568 s->state = SSL_ST_ERR;
572 if (!s->method->ssl3_enc->change_cipher_state(s,
573 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
576 s->state = SSL_ST_ERR;
579 #ifndef OPENSSL_NO_SCTP
582 * Change to new shared key of SCTP-Auth, will be ignored if
585 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
590 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
593 case SSL3_ST_CW_FINISHED_A:
594 case SSL3_ST_CW_FINISHED_B:
596 dtls1_start_timer(s);
597 ret = dtls1_send_finished(s,
598 SSL3_ST_CW_FINISHED_A,
599 SSL3_ST_CW_FINISHED_B,
601 ssl3_enc->client_finished_label,
603 ssl3_enc->client_finished_label_len);
606 s->state = SSL3_ST_CW_FLUSH;
609 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
611 s->s3->tmp.next_state = SSL_ST_OK;
612 #ifndef OPENSSL_NO_SCTP
613 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
614 s->d1->next_state = s->s3->tmp.next_state;
615 s->s3->tmp.next_state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
618 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
619 s->state = SSL_ST_OK;
620 #ifndef OPENSSL_NO_SCTP
621 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
622 s->d1->next_state = SSL_ST_OK;
623 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
626 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
627 s->s3->delay_buf_pop_ret = 0;
630 #ifndef OPENSSL_NO_SCTP
632 * Change to new shared key of SCTP-Auth, will be ignored if
635 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
639 #ifndef OPENSSL_NO_TLSEXT
641 * Allow NewSessionTicket if ticket expected
643 if (s->tlsext_ticket_expected)
644 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
648 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
653 #ifndef OPENSSL_NO_TLSEXT
654 case SSL3_ST_CR_SESSION_TICKET_A:
655 case SSL3_ST_CR_SESSION_TICKET_B:
656 ret = ssl3_get_new_session_ticket(s);
659 s->state = SSL3_ST_CR_FINISHED_A;
663 case SSL3_ST_CR_CERT_STATUS_A:
664 case SSL3_ST_CR_CERT_STATUS_B:
665 ret = ssl3_get_cert_status(s);
668 s->state = SSL3_ST_CR_KEY_EXCH_A;
673 case SSL3_ST_CR_FINISHED_A:
674 case SSL3_ST_CR_FINISHED_B:
675 s->d1->change_cipher_spec_ok = 1;
676 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
677 SSL3_ST_CR_FINISHED_B);
683 s->state = SSL3_ST_CW_CHANGE_A;
685 s->state = SSL_ST_OK;
687 #ifndef OPENSSL_NO_SCTP
688 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
689 state == SSL_ST_RENEGOTIATE) {
690 s->d1->next_state = s->state;
691 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
698 case SSL3_ST_CW_FLUSH:
699 s->rwstate = SSL_WRITING;
700 if (BIO_flush(s->wbio) <= 0) {
702 * If the write error was fatal, stop trying
704 if (!BIO_should_retry(s->wbio)) {
705 s->rwstate = SSL_NOTHING;
706 s->state = s->s3->tmp.next_state;
712 s->rwstate = SSL_NOTHING;
713 s->state = s->s3->tmp.next_state;
717 /* clean a few things up */
718 ssl3_cleanup_key_block(s);
721 if (s->init_buf != NULL) {
722 BUF_MEM_free(s->init_buf);
728 * If we are not 'joining' the last two packets, remove the
731 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
732 ssl_free_wbio_buffer(s);
733 /* else do it later in ssl3_write */
739 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
741 s->ctx->stats.sess_hit++;
745 s->handshake_func = dtls1_connect;
746 s->ctx->stats.sess_connect_good++;
749 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
751 /* done with handshaking */
752 s->d1->handshake_read_seq = 0;
753 s->d1->next_handshake_write_seq = 0;
759 SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);
765 /* did we do anything */
766 if (!s->s3->tmp.reuse_message && !skip) {
768 if ((ret = BIO_flush(s->wbio)) <= 0)
772 if ((cb != NULL) && (s->state != state)) {
773 new_state = s->state;
775 cb(s, SSL_CB_CONNECT_LOOP, 1);
776 s->state = new_state;
784 #ifndef OPENSSL_NO_SCTP
786 * Notify SCTP BIO socket to leave handshake mode and allow stream
787 * identifier other than 0. Will be ignored if no SCTP is used.
789 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
790 s->in_handshake, NULL);
796 cb(s, SSL_CB_CONNECT_EXIT, ret);
800 int dtls1_client_hello(SSL *s)
803 unsigned char *p, *d;
808 buf = (unsigned char *)s->init_buf->data;
809 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
810 SSL_SESSION *sess = s->session;
811 if ((s->session == NULL) || (s->session->ssl_version != s->version) ||
812 #ifdef OPENSSL_NO_TLSEXT
813 !sess->session_id_length ||
815 (!sess->session_id_length && !sess->tlsext_tick) ||
817 (s->session->not_resumable)) {
818 if (!ssl_get_new_session(s, 0))
821 /* else use the pre-loaded session */
823 p = s->s3->client_random;
826 * if client_random is initialized, reuse it, we are required to use
827 * same upon reply to HelloVerify
829 for (i = 0; p[i] == '\0' && i < sizeof(s->s3->client_random); i++) ;
830 if (i == sizeof(s->s3->client_random))
831 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
833 /* Do the message type and length last */
834 d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
836 *(p++) = s->version >> 8;
837 *(p++) = s->version & 0xff;
838 s->client_version = s->version;
841 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
842 p += SSL3_RANDOM_SIZE;
848 i = s->session->session_id_length;
851 if (i > sizeof s->session->session_id) {
852 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
855 memcpy(p, s->session->session_id, i);
860 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
861 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
864 *(p++) = s->d1->cookie_len;
865 memcpy(p, s->d1->cookie, s->d1->cookie_len);
866 p += s->d1->cookie_len;
868 /* Ciphers supported */
869 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
871 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
878 if (s->ctx->comp_methods == NULL)
881 j = sk_SSL_COMP_num(s->ctx->comp_methods);
883 for (i = 0; i < j; i++) {
884 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
887 *(p++) = 0; /* Add the NULL method */
889 #ifndef OPENSSL_NO_TLSEXT
891 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
892 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
896 ssl_add_clienthello_tlsext(s, p,
897 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
899 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
907 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
909 s->state = SSL3_ST_CW_CLNT_HELLO_B;
910 /* number of bytes to write */
911 s->init_num = p - buf;
914 /* buffer the message to handle re-xmits */
915 dtls1_buffer_message(s, 0);
918 /* SSL3_ST_CW_CLNT_HELLO_B */
919 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
924 static int dtls1_get_hello_verify(SSL *s)
928 unsigned int cookie_len;
930 n = s->method->ssl_get_message(s,
931 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
932 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
933 -1, s->max_cert_list, &ok);
938 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
939 s->d1->send_cookie = 0;
940 s->s3->tmp.reuse_message = 1;
944 data = (unsigned char *)s->init_msg;
946 if ((data[0] != (s->version >> 8)) || (data[1] != (s->version & 0xff))) {
947 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY, SSL_R_WRONG_SSL_VERSION);
948 s->version = (s->version & 0xff00) | data[1];
949 al = SSL_AD_PROTOCOL_VERSION;
954 cookie_len = *(data++);
955 if (cookie_len > sizeof(s->d1->cookie)) {
956 al = SSL_AD_ILLEGAL_PARAMETER;
960 memcpy(s->d1->cookie, data, cookie_len);
961 s->d1->cookie_len = cookie_len;
963 s->d1->send_cookie = 1;
967 ssl3_send_alert(s, SSL3_AL_FATAL, al);
968 s->state = SSL_ST_ERR;
972 int dtls1_send_client_key_exchange(SSL *s)
974 unsigned char *p, *d;
977 #ifndef OPENSSL_NO_RSA
979 EVP_PKEY *pkey = NULL;
981 #ifndef OPENSSL_NO_KRB5
983 #endif /* OPENSSL_NO_KRB5 */
984 #ifndef OPENSSL_NO_ECDH
985 EC_KEY *clnt_ecdh = NULL;
986 const EC_POINT *srvr_ecpoint = NULL;
987 EVP_PKEY *srvr_pub_pkey = NULL;
988 unsigned char *encodedPoint = NULL;
989 int encoded_pt_len = 0;
990 BN_CTX *bn_ctx = NULL;
993 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
994 d = (unsigned char *)s->init_buf->data;
995 p = &(d[DTLS1_HM_HEADER_LENGTH]);
997 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
999 /* Fool emacs indentation */
1002 #ifndef OPENSSL_NO_RSA
1003 else if (alg_k & SSL_kRSA) {
1005 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1007 if (s->session->sess_cert == NULL) {
1009 * We should always have a server certificate with SSL_kRSA.
1011 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1012 ERR_R_INTERNAL_ERROR);
1016 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1017 rsa = s->session->sess_cert->peer_rsa_tmp;
1020 X509_get_pubkey(s->session->
1021 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
1023 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
1024 || (pkey->pkey.rsa == NULL)) {
1025 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1026 ERR_R_INTERNAL_ERROR);
1029 rsa = pkey->pkey.rsa;
1030 EVP_PKEY_free(pkey);
1033 tmp_buf[0] = s->client_version >> 8;
1034 tmp_buf[1] = s->client_version & 0xff;
1035 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
1038 s->session->master_key_length = sizeof tmp_buf;
1041 /* Fix buf for TLS and [incidentally] DTLS */
1042 if (s->version > SSL3_VERSION)
1044 n = RSA_public_encrypt(sizeof tmp_buf,
1045 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
1047 if (s->options & SSL_OP_PKCS1_CHECK_1)
1049 if (s->options & SSL_OP_PKCS1_CHECK_2)
1053 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1054 SSL_R_BAD_RSA_ENCRYPT);
1058 /* Fix buf for TLS and [incidentally] DTLS */
1059 if (s->version > SSL3_VERSION) {
1064 s->session->master_key_length =
1065 s->method->ssl3_enc->generate_master_secret(s,
1067 session->master_key,
1070 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1073 #ifndef OPENSSL_NO_KRB5
1074 else if (alg_k & SSL_kKRB5) {
1075 krb5_error_code krb5rc;
1076 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1077 /* krb5_data krb5_ap_req; */
1078 krb5_data *enc_ticket;
1079 krb5_data authenticator, *authp = NULL;
1080 EVP_CIPHER_CTX ciph_ctx;
1081 const EVP_CIPHER *enc = NULL;
1082 unsigned char iv[EVP_MAX_IV_LENGTH];
1083 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1084 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
1085 int padl, outl = sizeof(epms);
1087 EVP_CIPHER_CTX_init(&ciph_ctx);
1090 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1092 # endif /* KSSL_DEBUG */
1095 # ifdef KRB5SENDAUTH
1097 authp = &authenticator;
1098 # endif /* KRB5SENDAUTH */
1100 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
1101 enc = kssl_map_enc(kssl_ctx->enctype);
1106 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1107 if (krb5rc && kssl_err.text)
1108 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1110 # endif /* KSSL_DEBUG */
1113 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1114 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
1119 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
1120 ** in place of RFC 2712 KerberosWrapper, as in:
1122 ** Send ticket (copy to *p, set n = length)
1123 ** n = krb5_ap_req.length;
1124 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1125 ** if (krb5_ap_req.data)
1126 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1128 ** Now using real RFC 2712 KerberosWrapper
1129 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1130 ** Note: 2712 "opaque" types are here replaced
1131 ** with a 2-byte length followed by the value.
1133 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1134 ** Where "xx xx" = length bytes. Shown here with
1135 ** optional authenticator omitted.
1138 /* KerberosWrapper.Ticket */
1139 s2n(enc_ticket->length, p);
1140 memcpy(p, enc_ticket->data, enc_ticket->length);
1141 p += enc_ticket->length;
1142 n = enc_ticket->length + 2;
1144 /* KerberosWrapper.Authenticator */
1145 if (authp && authp->length) {
1146 s2n(authp->length, p);
1147 memcpy(p, authp->data, authp->length);
1149 n += authp->length + 2;
1155 s2n(0, p); /* null authenticator length */
1159 if (RAND_bytes(tmp_buf, sizeof tmp_buf) <= 0)
1163 * 20010420 VRS. Tried it this way; failed.
1164 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1165 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1166 * kssl_ctx->length);
1167 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1170 memset(iv, 0, sizeof iv); /* per RFC 1510 */
1171 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
1172 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
1174 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
1176 if (outl > (int)sizeof epms) {
1177 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1178 ERR_R_INTERNAL_ERROR);
1181 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1183 /* KerberosWrapper.EncryptedPreMasterSecret */
1185 memcpy(p, epms, outl);
1189 s->session->master_key_length =
1190 s->method->ssl3_enc->generate_master_secret(s,
1192 session->master_key,
1196 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1197 OPENSSL_cleanse(epms, outl);
1200 #ifndef OPENSSL_NO_DH
1201 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1202 DH *dh_srvr, *dh_clnt;
1204 if (s->session->sess_cert == NULL) {
1205 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1206 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1207 SSL_R_UNEXPECTED_MESSAGE);
1211 if (s->session->sess_cert->peer_dh_tmp != NULL)
1212 dh_srvr = s->session->sess_cert->peer_dh_tmp;
1214 /* we get them from the cert */
1215 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1216 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1217 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1221 /* generate a new random key */
1222 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1223 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1226 if (!DH_generate_key(dh_clnt)) {
1227 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1232 * use the 'p' output buffer for the DH key, but make sure to
1233 * clear it out afterwards
1236 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
1239 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1243 /* generate master key from the result */
1244 s->session->master_key_length =
1245 s->method->ssl3_enc->generate_master_secret(s,
1247 session->master_key,
1252 /* send off the data */
1253 n = BN_num_bytes(dh_clnt->pub_key);
1255 BN_bn2bin(dh_clnt->pub_key, p);
1260 /* perhaps clean things up a bit EAY EAY EAY EAY */
1263 #ifndef OPENSSL_NO_ECDH
1264 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
1265 const EC_GROUP *srvr_group = NULL;
1267 int ecdh_clnt_cert = 0;
1270 if (s->session->sess_cert == NULL) {
1271 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1272 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1273 SSL_R_UNEXPECTED_MESSAGE);
1278 * Did we send out the client's ECDH share for use in premaster
1279 * computation as part of client certificate? If so, set
1280 * ecdh_clnt_cert to 1.
1282 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
1284 * XXX: For now, we do not support client authentication
1285 * using ECDH certificates. To add such support, one needs to
1286 * add code that checks for appropriate conditions and sets
1287 * ecdh_clnt_cert to 1. For example, the cert have an ECC key
1288 * on the same curve as the server's and the key should be
1289 * authorized for key agreement. One also needs to add code
1290 * in ssl3_connect to skip sending the certificate verify
1291 * message. if ((s->cert->key->privatekey != NULL) &&
1292 * (s->cert->key->privatekey->type == EVP_PKEY_EC) && ...)
1293 * ecdh_clnt_cert = 1;
1297 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
1298 tkey = s->session->sess_cert->peer_ecdh_tmp;
1300 /* Get the Server Public Key from Cert */
1302 X509_get_pubkey(s->session->
1303 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1304 if ((srvr_pub_pkey == NULL)
1305 || (srvr_pub_pkey->type != EVP_PKEY_EC)
1306 || (srvr_pub_pkey->pkey.ec == NULL)) {
1307 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1308 ERR_R_INTERNAL_ERROR);
1312 tkey = srvr_pub_pkey->pkey.ec;
1315 srvr_group = EC_KEY_get0_group(tkey);
1316 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1318 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
1319 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1320 ERR_R_INTERNAL_ERROR);
1324 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
1325 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1326 ERR_R_MALLOC_FAILURE);
1330 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1331 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
1334 if (ecdh_clnt_cert) {
1336 * Reuse key info from our certificate We only need our
1337 * private key to perform the ECDH computation.
1339 const BIGNUM *priv_key;
1340 tkey = s->cert->key->privatekey->pkey.ec;
1341 priv_key = EC_KEY_get0_private_key(tkey);
1342 if (priv_key == NULL) {
1343 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1344 ERR_R_MALLOC_FAILURE);
1347 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
1348 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1353 /* Generate a new ECDH key pair */
1354 if (!(EC_KEY_generate_key(clnt_ecdh))) {
1355 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1362 * use the 'p' output buffer for the ECDH key, but make sure to
1363 * clear it out afterwards
1366 field_size = EC_GROUP_get_degree(srvr_group);
1367 if (field_size <= 0) {
1368 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1371 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
1374 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1378 /* generate master key from the result */
1379 s->session->master_key_length =
1380 s->method->ssl3_enc->generate_master_secret(s,
1382 session->master_key,
1385 memset(p, 0, n); /* clean up */
1387 if (ecdh_clnt_cert) {
1388 /* Send empty client key exch message */
1392 * First check the size of encoding and allocate memory
1396 EC_POINT_point2oct(srvr_group,
1397 EC_KEY_get0_public_key(clnt_ecdh),
1398 POINT_CONVERSION_UNCOMPRESSED,
1401 encodedPoint = (unsigned char *)
1402 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
1403 bn_ctx = BN_CTX_new();
1404 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1405 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1406 ERR_R_MALLOC_FAILURE);
1410 /* Encode the public key */
1411 n = EC_POINT_point2oct(srvr_group,
1412 EC_KEY_get0_public_key(clnt_ecdh),
1413 POINT_CONVERSION_UNCOMPRESSED,
1414 encodedPoint, encoded_pt_len, bn_ctx);
1416 *p = n; /* length of encoded point */
1417 /* Encoded point will be copied here */
1419 /* copy the point */
1420 memcpy((unsigned char *)p, encodedPoint, n);
1421 /* increment n to account for length field */
1425 /* Free allocated memory */
1426 BN_CTX_free(bn_ctx);
1427 if (encodedPoint != NULL)
1428 OPENSSL_free(encodedPoint);
1429 if (clnt_ecdh != NULL)
1430 EC_KEY_free(clnt_ecdh);
1431 EVP_PKEY_free(srvr_pub_pkey);
1433 #endif /* !OPENSSL_NO_ECDH */
1435 #ifndef OPENSSL_NO_PSK
1436 else if (alg_k & SSL_kPSK) {
1437 char identity[PSK_MAX_IDENTITY_LEN];
1438 unsigned char *t = NULL;
1439 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
1440 unsigned int pre_ms_len = 0, psk_len = 0;
1444 if (s->psk_client_callback == NULL) {
1445 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1446 SSL_R_PSK_NO_CLIENT_CB);
1450 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
1451 identity, PSK_MAX_IDENTITY_LEN,
1453 sizeof(psk_or_pre_ms));
1454 if (psk_len > PSK_MAX_PSK_LEN) {
1455 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1456 ERR_R_INTERNAL_ERROR);
1458 } else if (psk_len == 0) {
1459 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1460 SSL_R_PSK_IDENTITY_NOT_FOUND);
1464 /* create PSK pre_master_secret */
1465 pre_ms_len = 2 + psk_len + 2 + psk_len;
1467 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
1469 memset(t, 0, psk_len);
1473 if (s->session->psk_identity_hint != NULL)
1474 OPENSSL_free(s->session->psk_identity_hint);
1475 s->session->psk_identity_hint =
1476 BUF_strdup(s->ctx->psk_identity_hint);
1477 if (s->ctx->psk_identity_hint != NULL
1478 && s->session->psk_identity_hint == NULL) {
1479 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1480 ERR_R_MALLOC_FAILURE);
1484 if (s->session->psk_identity != NULL)
1485 OPENSSL_free(s->session->psk_identity);
1486 s->session->psk_identity = BUF_strdup(identity);
1487 if (s->session->psk_identity == NULL) {
1488 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1489 ERR_R_MALLOC_FAILURE);
1493 s->session->master_key_length =
1494 s->method->ssl3_enc->generate_master_secret(s,
1496 session->master_key,
1499 n = strlen(identity);
1501 memcpy(p, identity, n);
1505 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
1506 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
1508 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1514 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1515 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1516 ERR_R_INTERNAL_ERROR);
1520 d = dtls1_set_message_header(s, d,
1521 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1523 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1525 l2n(s->d1->handshake_write_seq,d);
1526 s->d1->handshake_write_seq++;
1529 s->state = SSL3_ST_CW_KEY_EXCH_B;
1530 /* number of bytes to write */
1531 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1534 /* buffer the message to handle re-xmits */
1535 dtls1_buffer_message(s, 0);
1538 /* SSL3_ST_CW_KEY_EXCH_B */
1539 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1541 #ifndef OPENSSL_NO_ECDH
1542 BN_CTX_free(bn_ctx);
1543 if (encodedPoint != NULL)
1544 OPENSSL_free(encodedPoint);
1545 if (clnt_ecdh != NULL)
1546 EC_KEY_free(clnt_ecdh);
1547 EVP_PKEY_free(srvr_pub_pkey);
1552 int dtls1_send_client_verify(SSL *s)
1554 unsigned char *p, *d;
1555 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1557 #ifndef OPENSSL_NO_RSA
1561 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1565 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1566 d = (unsigned char *)s->init_buf->data;
1567 p = &(d[DTLS1_HM_HEADER_LENGTH]);
1568 pkey = s->cert->key->privatekey;
1570 s->method->ssl3_enc->cert_verify_mac(s,
1572 &(data[MD5_DIGEST_LENGTH]));
1574 #ifndef OPENSSL_NO_RSA
1575 if (pkey->type == EVP_PKEY_RSA) {
1576 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
1577 if (RSA_sign(NID_md5_sha1, data,
1578 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
1579 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1580 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
1587 #ifndef OPENSSL_NO_DSA
1588 if (pkey->type == EVP_PKEY_DSA) {
1589 if (!DSA_sign(pkey->save_type,
1590 &(data[MD5_DIGEST_LENGTH]),
1591 SHA_DIGEST_LENGTH, &(p[2]),
1592 (unsigned int *)&j, pkey->pkey.dsa)) {
1593 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
1600 #ifndef OPENSSL_NO_ECDSA
1601 if (pkey->type == EVP_PKEY_EC) {
1602 if (!ECDSA_sign(pkey->save_type,
1603 &(data[MD5_DIGEST_LENGTH]),
1604 SHA_DIGEST_LENGTH, &(p[2]),
1605 (unsigned int *)&j, pkey->pkey.ec)) {
1606 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
1614 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
1618 d = dtls1_set_message_header(s, d,
1619 SSL3_MT_CERTIFICATE_VERIFY, n, 0, n);
1621 s->init_num = (int)n + DTLS1_HM_HEADER_LENGTH;
1624 /* buffer the message to handle re-xmits */
1625 dtls1_buffer_message(s, 0);
1627 s->state = SSL3_ST_CW_CERT_VRFY_B;
1630 /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1631 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1636 int dtls1_send_client_certificate(SSL *s)
1639 EVP_PKEY *pkey = NULL;
1643 if (s->state == SSL3_ST_CW_CERT_A) {
1644 if ((s->cert == NULL) ||
1645 (s->cert->key->x509 == NULL) ||
1646 (s->cert->key->privatekey == NULL))
1647 s->state = SSL3_ST_CW_CERT_B;
1649 s->state = SSL3_ST_CW_CERT_C;
1652 /* We need to get a client cert */
1653 if (s->state == SSL3_ST_CW_CERT_B) {
1655 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
1656 * return(-1); We then get retied later
1659 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1661 s->rwstate = SSL_X509_LOOKUP;
1664 s->rwstate = SSL_NOTHING;
1665 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
1666 s->state = SSL3_ST_CW_CERT_B;
1667 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
1669 } else if (i == 1) {
1671 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
1672 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1678 EVP_PKEY_free(pkey);
1680 if (s->version == SSL3_VERSION) {
1681 s->s3->tmp.cert_req = 0;
1682 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1685 s->s3->tmp.cert_req = 2;
1689 /* Ok, we have a cert */
1690 s->state = SSL3_ST_CW_CERT_C;
1693 if (s->state == SSL3_ST_CW_CERT_C) {
1694 s->state = SSL3_ST_CW_CERT_D;
1695 l = dtls1_output_cert_chain(s,
1696 (s->s3->tmp.cert_req ==
1697 2) ? NULL : s->cert->key->x509);
1699 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1700 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1703 s->init_num = (int)l;
1706 /* set header called by dtls1_output_cert_chain() */
1708 /* buffer the message to handle re-xmits */
1709 dtls1_buffer_message(s, 0);
1711 /* SSL3_ST_CW_CERT_D */
1712 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));