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:
302 case SSL3_ST_CW_CLNT_HELLO_B:
306 /* every DTLS ClientHello resets Finished MAC */
307 ssl3_init_finished_mac(s);
309 dtls1_start_timer(s);
310 ret = dtls1_client_hello(s);
314 if (s->d1->send_cookie) {
315 s->state = SSL3_ST_CW_FLUSH;
316 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
318 s->state = SSL3_ST_CR_SRVR_HELLO_A;
322 #ifndef OPENSSL_NO_SCTP
323 /* Disable buffering for SCTP */
324 if (!BIO_dgram_is_sctp(SSL_get_wbio(s))) {
327 * turn on buffering for the next lot of output
329 if (s->bbio != s->wbio)
330 s->wbio = BIO_push(s->bbio, s->wbio);
331 #ifndef OPENSSL_NO_SCTP
337 case SSL3_ST_CR_SRVR_HELLO_A:
338 case SSL3_ST_CR_SRVR_HELLO_B:
339 ret = ssl3_get_server_hello(s);
344 #ifndef OPENSSL_NO_SCTP
346 * Add new shared key for SCTP-Auth, will be ignored if
349 snprintf((char *)labelbuffer,
350 sizeof(DTLS1_SCTP_AUTH_LABEL),
351 DTLS1_SCTP_AUTH_LABEL);
353 SSL_export_keying_material(s, sctpauthkey,
356 sizeof(labelbuffer), NULL, 0,
359 BIO_ctrl(SSL_get_wbio(s),
360 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
361 sizeof(sctpauthkey), sctpauthkey);
364 s->state = SSL3_ST_CR_FINISHED_A;
366 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
371 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
372 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
374 ret = dtls1_get_hello_verify(s);
378 if (s->d1->send_cookie) /* start again, with a cookie */
379 s->state = SSL3_ST_CW_CLNT_HELLO_A;
381 s->state = SSL3_ST_CR_CERT_A;
385 case SSL3_ST_CR_CERT_A:
386 case SSL3_ST_CR_CERT_B:
387 /* Check if it is anon DH or PSK */
388 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
389 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
390 ret = ssl3_get_server_certificate(s);
393 #ifndef OPENSSL_NO_TLSEXT
394 if (s->tlsext_status_expected)
395 s->state = SSL3_ST_CR_CERT_STATUS_A;
397 s->state = SSL3_ST_CR_KEY_EXCH_A;
400 s->state = SSL3_ST_CR_KEY_EXCH_A;
406 s->state = SSL3_ST_CR_KEY_EXCH_A;
411 case SSL3_ST_CR_KEY_EXCH_A:
412 case SSL3_ST_CR_KEY_EXCH_B:
413 ret = ssl3_get_key_exchange(s);
416 s->state = SSL3_ST_CR_CERT_REQ_A;
420 * at this point we check that we have the required stuff from
423 if (!ssl3_check_cert_and_algorithm(s)) {
425 s->state = SSL_ST_ERR;
430 case SSL3_ST_CR_CERT_REQ_A:
431 case SSL3_ST_CR_CERT_REQ_B:
432 ret = ssl3_get_certificate_request(s);
435 s->state = SSL3_ST_CR_SRVR_DONE_A;
439 case SSL3_ST_CR_SRVR_DONE_A:
440 case SSL3_ST_CR_SRVR_DONE_B:
441 ret = ssl3_get_server_done(s);
445 if (s->s3->tmp.cert_req)
446 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
448 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
451 #ifndef OPENSSL_NO_SCTP
452 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
453 state == SSL_ST_RENEGOTIATE)
454 s->state = DTLS1_SCTP_ST_CR_READ_SOCK;
457 s->state = s->s3->tmp.next_state;
460 case SSL3_ST_CW_CERT_A:
461 case SSL3_ST_CW_CERT_B:
462 case SSL3_ST_CW_CERT_C:
463 case SSL3_ST_CW_CERT_D:
464 dtls1_start_timer(s);
465 ret = dtls1_send_client_certificate(s);
468 s->state = SSL3_ST_CW_KEY_EXCH_A;
472 case SSL3_ST_CW_KEY_EXCH_A:
473 case SSL3_ST_CW_KEY_EXCH_B:
474 dtls1_start_timer(s);
475 ret = dtls1_send_client_key_exchange(s);
479 #ifndef OPENSSL_NO_SCTP
481 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
484 snprintf((char *)labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
485 DTLS1_SCTP_AUTH_LABEL);
487 SSL_export_keying_material(s, sctpauthkey,
488 sizeof(sctpauthkey), labelbuffer,
489 sizeof(labelbuffer), NULL, 0, 0);
491 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
492 sizeof(sctpauthkey), sctpauthkey);
496 * EAY EAY EAY need to check for DH fix cert sent back
499 * For TLS, cert_req is set to 2, so a cert chain of nothing is
500 * sent, but no verify packet is sent
502 if (s->s3->tmp.cert_req == 1) {
503 s->state = SSL3_ST_CW_CERT_VRFY_A;
505 #ifndef OPENSSL_NO_SCTP
506 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
507 s->d1->next_state = SSL3_ST_CW_CHANGE_A;
508 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
511 s->state = SSL3_ST_CW_CHANGE_A;
517 case SSL3_ST_CW_CERT_VRFY_A:
518 case SSL3_ST_CW_CERT_VRFY_B:
519 dtls1_start_timer(s);
520 ret = dtls1_send_client_verify(s);
523 #ifndef OPENSSL_NO_SCTP
524 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
525 s->d1->next_state = SSL3_ST_CW_CHANGE_A;
526 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
529 s->state = SSL3_ST_CW_CHANGE_A;
533 case SSL3_ST_CW_CHANGE_A:
534 case SSL3_ST_CW_CHANGE_B:
536 dtls1_start_timer(s);
537 ret = dtls1_send_change_cipher_spec(s,
539 SSL3_ST_CW_CHANGE_B);
543 s->state = SSL3_ST_CW_FINISHED_A;
546 s->session->cipher = s->s3->tmp.new_cipher;
547 #ifdef OPENSSL_NO_COMP
548 s->session->compress_meth = 0;
550 if (s->s3->tmp.new_compression == NULL)
551 s->session->compress_meth = 0;
553 s->session->compress_meth = s->s3->tmp.new_compression->id;
555 if (!s->method->ssl3_enc->setup_key_block(s)) {
557 s->state = SSL_ST_ERR;
561 if (!s->method->ssl3_enc->change_cipher_state(s,
562 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
565 s->state = SSL_ST_ERR;
568 #ifndef OPENSSL_NO_SCTP
571 * Change to new shared key of SCTP-Auth, will be ignored if
574 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
579 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
582 case SSL3_ST_CW_FINISHED_A:
583 case SSL3_ST_CW_FINISHED_B:
585 dtls1_start_timer(s);
586 ret = dtls1_send_finished(s,
587 SSL3_ST_CW_FINISHED_A,
588 SSL3_ST_CW_FINISHED_B,
590 ssl3_enc->client_finished_label,
592 ssl3_enc->client_finished_label_len);
595 s->state = SSL3_ST_CW_FLUSH;
598 s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
600 s->s3->tmp.next_state = SSL_ST_OK;
601 #ifndef OPENSSL_NO_SCTP
602 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
603 s->d1->next_state = s->s3->tmp.next_state;
604 s->s3->tmp.next_state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
607 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
608 s->state = SSL_ST_OK;
609 #ifndef OPENSSL_NO_SCTP
610 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
611 s->d1->next_state = SSL_ST_OK;
612 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
615 s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
616 s->s3->delay_buf_pop_ret = 0;
619 #ifndef OPENSSL_NO_SCTP
621 * Change to new shared key of SCTP-Auth, will be ignored if
624 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
628 #ifndef OPENSSL_NO_TLSEXT
630 * Allow NewSessionTicket if ticket expected
632 if (s->tlsext_ticket_expected)
633 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
637 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
642 #ifndef OPENSSL_NO_TLSEXT
643 case SSL3_ST_CR_SESSION_TICKET_A:
644 case SSL3_ST_CR_SESSION_TICKET_B:
645 ret = ssl3_get_new_session_ticket(s);
648 s->state = SSL3_ST_CR_FINISHED_A;
652 case SSL3_ST_CR_CERT_STATUS_A:
653 case SSL3_ST_CR_CERT_STATUS_B:
654 ret = ssl3_get_cert_status(s);
657 s->state = SSL3_ST_CR_KEY_EXCH_A;
662 case SSL3_ST_CR_FINISHED_A:
663 case SSL3_ST_CR_FINISHED_B:
664 s->d1->change_cipher_spec_ok = 1;
665 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
666 SSL3_ST_CR_FINISHED_B);
672 s->state = SSL3_ST_CW_CHANGE_A;
674 s->state = SSL_ST_OK;
676 #ifndef OPENSSL_NO_SCTP
677 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
678 state == SSL_ST_RENEGOTIATE) {
679 s->d1->next_state = s->state;
680 s->state = DTLS1_SCTP_ST_CW_WRITE_SOCK;
687 case SSL3_ST_CW_FLUSH:
688 s->rwstate = SSL_WRITING;
689 if (BIO_flush(s->wbio) <= 0) {
691 * If the write error was fatal, stop trying
693 if (!BIO_should_retry(s->wbio)) {
694 s->rwstate = SSL_NOTHING;
695 s->state = s->s3->tmp.next_state;
701 s->rwstate = SSL_NOTHING;
702 s->state = s->s3->tmp.next_state;
706 /* clean a few things up */
707 ssl3_cleanup_key_block(s);
710 if (s->init_buf != NULL) {
711 BUF_MEM_free(s->init_buf);
717 * If we are not 'joining' the last two packets, remove the
720 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
721 ssl_free_wbio_buffer(s);
722 /* else do it later in ssl3_write */
728 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
730 s->ctx->stats.sess_hit++;
734 s->handshake_func = dtls1_connect;
735 s->ctx->stats.sess_connect_good++;
738 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
740 /* done with handshaking */
741 s->d1->handshake_read_seq = 0;
742 s->d1->next_handshake_write_seq = 0;
743 dtls1_clear_received_buffer(s);
749 SSLerr(SSL_F_DTLS1_CONNECT, SSL_R_UNKNOWN_STATE);
755 /* did we do anything */
756 if (!s->s3->tmp.reuse_message && !skip) {
758 if ((ret = BIO_flush(s->wbio)) <= 0)
762 if ((cb != NULL) && (s->state != state)) {
763 new_state = s->state;
765 cb(s, SSL_CB_CONNECT_LOOP, 1);
766 s->state = new_state;
774 #ifndef OPENSSL_NO_SCTP
776 * Notify SCTP BIO socket to leave handshake mode and allow stream
777 * identifier other than 0. Will be ignored if no SCTP is used.
779 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
780 s->in_handshake, NULL);
786 cb(s, SSL_CB_CONNECT_EXIT, ret);
790 int dtls1_client_hello(SSL *s)
793 unsigned char *p, *d;
798 buf = (unsigned char *)s->init_buf->data;
799 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
800 SSL_SESSION *sess = s->session;
801 if ((s->session == NULL) || (s->session->ssl_version != s->version) ||
802 #ifdef OPENSSL_NO_TLSEXT
803 !sess->session_id_length ||
805 (!sess->session_id_length && !sess->tlsext_tick) ||
807 (s->session->not_resumable)) {
808 if (!ssl_get_new_session(s, 0))
811 /* else use the pre-loaded session */
813 p = s->s3->client_random;
816 * if client_random is initialized, reuse it, we are required to use
817 * same upon reply to HelloVerify
819 for (i = 0; p[i] == '\0' && i < sizeof(s->s3->client_random); i++) ;
820 if (i == sizeof(s->s3->client_random))
821 ssl_fill_hello_random(s, 0, p, sizeof(s->s3->client_random));
823 /* Do the message type and length last */
824 d = p = &(buf[DTLS1_HM_HEADER_LENGTH]);
826 *(p++) = s->version >> 8;
827 *(p++) = s->version & 0xff;
828 s->client_version = s->version;
831 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
832 p += SSL3_RANDOM_SIZE;
838 i = s->session->session_id_length;
841 if (i > sizeof s->session->session_id) {
842 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
845 memcpy(p, s->session->session_id, i);
850 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
851 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
854 *(p++) = s->d1->cookie_len;
855 memcpy(p, s->d1->cookie, s->d1->cookie_len);
856 p += s->d1->cookie_len;
858 /* Ciphers supported */
859 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
861 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
868 if (s->ctx->comp_methods == NULL)
871 j = sk_SSL_COMP_num(s->ctx->comp_methods);
873 for (i = 0; i < j; i++) {
874 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
877 *(p++) = 0; /* Add the NULL method */
879 #ifndef OPENSSL_NO_TLSEXT
881 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
882 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
886 ssl_add_clienthello_tlsext(s, p,
887 buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
889 SSLerr(SSL_F_DTLS1_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
897 d = dtls1_set_message_header(s, d, SSL3_MT_CLIENT_HELLO, l, 0, l);
899 s->state = SSL3_ST_CW_CLNT_HELLO_B;
900 /* number of bytes to write */
901 s->init_num = p - buf;
904 /* buffer the message to handle re-xmits */
905 dtls1_buffer_message(s, 0);
908 /* SSL3_ST_CW_CLNT_HELLO_B */
909 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
914 static int dtls1_get_hello_verify(SSL *s)
918 unsigned int cookie_len;
920 n = s->method->ssl_get_message(s,
921 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
922 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
923 -1, s->max_cert_list, &ok);
928 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
929 s->d1->send_cookie = 0;
930 s->s3->tmp.reuse_message = 1;
934 data = (unsigned char *)s->init_msg;
936 if ((data[0] != (s->version >> 8)) || (data[1] != (s->version & 0xff))) {
937 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY, SSL_R_WRONG_SSL_VERSION);
938 s->version = (s->version & 0xff00) | data[1];
939 al = SSL_AD_PROTOCOL_VERSION;
944 cookie_len = *(data++);
945 if (cookie_len > sizeof(s->d1->cookie)) {
946 al = SSL_AD_ILLEGAL_PARAMETER;
950 memcpy(s->d1->cookie, data, cookie_len);
951 s->d1->cookie_len = cookie_len;
953 s->d1->send_cookie = 1;
957 ssl3_send_alert(s, SSL3_AL_FATAL, al);
958 s->state = SSL_ST_ERR;
962 int dtls1_send_client_key_exchange(SSL *s)
964 unsigned char *p, *d;
967 #ifndef OPENSSL_NO_RSA
969 EVP_PKEY *pkey = NULL;
971 #ifndef OPENSSL_NO_KRB5
973 #endif /* OPENSSL_NO_KRB5 */
974 #ifndef OPENSSL_NO_ECDH
975 EC_KEY *clnt_ecdh = NULL;
976 const EC_POINT *srvr_ecpoint = NULL;
977 EVP_PKEY *srvr_pub_pkey = NULL;
978 unsigned char *encodedPoint = NULL;
979 int encoded_pt_len = 0;
980 BN_CTX *bn_ctx = NULL;
983 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
984 d = (unsigned char *)s->init_buf->data;
985 p = &(d[DTLS1_HM_HEADER_LENGTH]);
987 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
989 /* Fool emacs indentation */
992 #ifndef OPENSSL_NO_RSA
993 else if (alg_k & SSL_kRSA) {
995 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
997 if (s->session->sess_cert == NULL) {
999 * We should always have a server certificate with SSL_kRSA.
1001 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1002 ERR_R_INTERNAL_ERROR);
1006 if (s->session->sess_cert->peer_rsa_tmp != NULL)
1007 rsa = s->session->sess_cert->peer_rsa_tmp;
1010 X509_get_pubkey(s->session->
1011 sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
1013 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
1014 || (pkey->pkey.rsa == NULL)) {
1015 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1016 ERR_R_INTERNAL_ERROR);
1019 rsa = pkey->pkey.rsa;
1020 EVP_PKEY_free(pkey);
1023 tmp_buf[0] = s->client_version >> 8;
1024 tmp_buf[1] = s->client_version & 0xff;
1025 if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
1028 s->session->master_key_length = sizeof tmp_buf;
1031 /* Fix buf for TLS and [incidentally] DTLS */
1032 if (s->version > SSL3_VERSION)
1034 n = RSA_public_encrypt(sizeof tmp_buf,
1035 tmp_buf, p, rsa, RSA_PKCS1_PADDING);
1037 if (s->options & SSL_OP_PKCS1_CHECK_1)
1039 if (s->options & SSL_OP_PKCS1_CHECK_2)
1043 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1044 SSL_R_BAD_RSA_ENCRYPT);
1048 /* Fix buf for TLS and [incidentally] DTLS */
1049 if (s->version > SSL3_VERSION) {
1054 s->session->master_key_length =
1055 s->method->ssl3_enc->generate_master_secret(s,
1057 session->master_key,
1060 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1063 #ifndef OPENSSL_NO_KRB5
1064 else if (alg_k & SSL_kKRB5) {
1065 krb5_error_code krb5rc;
1066 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1067 /* krb5_data krb5_ap_req; */
1068 krb5_data *enc_ticket;
1069 krb5_data authenticator, *authp = NULL;
1070 EVP_CIPHER_CTX ciph_ctx;
1071 const EVP_CIPHER *enc = NULL;
1072 unsigned char iv[EVP_MAX_IV_LENGTH];
1073 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
1074 unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
1075 int padl, outl = sizeof(epms);
1077 EVP_CIPHER_CTX_init(&ciph_ctx);
1080 printf("ssl3_send_client_key_exchange(%lx & %lx)\n",
1082 # endif /* KSSL_DEBUG */
1085 # ifdef KRB5SENDAUTH
1087 authp = &authenticator;
1088 # endif /* KRB5SENDAUTH */
1090 krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
1091 enc = kssl_map_enc(kssl_ctx->enctype);
1096 printf("kssl_cget_tkt rtn %d\n", krb5rc);
1097 if (krb5rc && kssl_err.text)
1098 printf("kssl_cget_tkt kssl_err=%s\n", kssl_err.text);
1100 # endif /* KSSL_DEBUG */
1103 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1104 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
1109 * 20010406 VRS - Earlier versions used KRB5 AP_REQ
1110 ** in place of RFC 2712 KerberosWrapper, as in:
1112 ** Send ticket (copy to *p, set n = length)
1113 ** n = krb5_ap_req.length;
1114 ** memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
1115 ** if (krb5_ap_req.data)
1116 ** kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
1118 ** Now using real RFC 2712 KerberosWrapper
1119 ** (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
1120 ** Note: 2712 "opaque" types are here replaced
1121 ** with a 2-byte length followed by the value.
1123 ** KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
1124 ** Where "xx xx" = length bytes. Shown here with
1125 ** optional authenticator omitted.
1128 /* KerberosWrapper.Ticket */
1129 s2n(enc_ticket->length, p);
1130 memcpy(p, enc_ticket->data, enc_ticket->length);
1131 p += enc_ticket->length;
1132 n = enc_ticket->length + 2;
1134 /* KerberosWrapper.Authenticator */
1135 if (authp && authp->length) {
1136 s2n(authp->length, p);
1137 memcpy(p, authp->data, authp->length);
1139 n += authp->length + 2;
1145 s2n(0, p); /* null authenticator length */
1149 if (RAND_bytes(tmp_buf, sizeof tmp_buf) <= 0)
1153 * 20010420 VRS. Tried it this way; failed.
1154 * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
1155 * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
1156 * kssl_ctx->length);
1157 * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
1160 memset(iv, 0, sizeof iv); /* per RFC 1510 */
1161 EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
1162 EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
1164 EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
1166 if (outl > (int)sizeof epms) {
1167 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1168 ERR_R_INTERNAL_ERROR);
1171 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1173 /* KerberosWrapper.EncryptedPreMasterSecret */
1175 memcpy(p, epms, outl);
1179 s->session->master_key_length =
1180 s->method->ssl3_enc->generate_master_secret(s,
1182 session->master_key,
1186 OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
1187 OPENSSL_cleanse(epms, outl);
1190 #ifndef OPENSSL_NO_DH
1191 else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
1192 DH *dh_srvr, *dh_clnt;
1194 if (s->session->sess_cert == NULL) {
1195 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1196 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1197 SSL_R_UNEXPECTED_MESSAGE);
1201 if (s->session->sess_cert->peer_dh_tmp != NULL)
1202 dh_srvr = s->session->sess_cert->peer_dh_tmp;
1204 /* we get them from the cert */
1205 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1206 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1207 SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1211 /* generate a new random key */
1212 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
1213 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1216 if (!DH_generate_key(dh_clnt)) {
1217 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1222 * use the 'p' output buffer for the DH key, but make sure to
1223 * clear it out afterwards
1226 n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
1229 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
1233 /* generate master key from the result */
1234 s->session->master_key_length =
1235 s->method->ssl3_enc->generate_master_secret(s,
1237 session->master_key,
1242 /* send off the data */
1243 n = BN_num_bytes(dh_clnt->pub_key);
1245 BN_bn2bin(dh_clnt->pub_key, p);
1250 /* perhaps clean things up a bit EAY EAY EAY EAY */
1253 #ifndef OPENSSL_NO_ECDH
1254 else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
1255 const EC_GROUP *srvr_group = NULL;
1257 int ecdh_clnt_cert = 0;
1260 if (s->session->sess_cert == NULL) {
1261 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1262 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1263 SSL_R_UNEXPECTED_MESSAGE);
1268 * Did we send out the client's ECDH share for use in premaster
1269 * computation as part of client certificate? If so, set
1270 * ecdh_clnt_cert to 1.
1272 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
1274 * XXX: For now, we do not support client authentication
1275 * using ECDH certificates. To add such support, one needs to
1276 * add code that checks for appropriate conditions and sets
1277 * ecdh_clnt_cert to 1. For example, the cert have an ECC key
1278 * on the same curve as the server's and the key should be
1279 * authorized for key agreement. One also needs to add code
1280 * in ssl3_connect to skip sending the certificate verify
1281 * message. if ((s->cert->key->privatekey != NULL) &&
1282 * (s->cert->key->privatekey->type == EVP_PKEY_EC) && ...)
1283 * ecdh_clnt_cert = 1;
1287 if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
1288 tkey = s->session->sess_cert->peer_ecdh_tmp;
1290 /* Get the Server Public Key from Cert */
1292 X509_get_pubkey(s->session->
1293 sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1294 if ((srvr_pub_pkey == NULL)
1295 || (srvr_pub_pkey->type != EVP_PKEY_EC)
1296 || (srvr_pub_pkey->pkey.ec == NULL)) {
1297 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1298 ERR_R_INTERNAL_ERROR);
1302 tkey = srvr_pub_pkey->pkey.ec;
1305 srvr_group = EC_KEY_get0_group(tkey);
1306 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
1308 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
1309 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1310 ERR_R_INTERNAL_ERROR);
1314 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
1315 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1316 ERR_R_MALLOC_FAILURE);
1320 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
1321 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
1324 if (ecdh_clnt_cert) {
1326 * Reuse key info from our certificate We only need our
1327 * private key to perform the ECDH computation.
1329 const BIGNUM *priv_key;
1330 tkey = s->cert->key->privatekey->pkey.ec;
1331 priv_key = EC_KEY_get0_private_key(tkey);
1332 if (priv_key == NULL) {
1333 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1334 ERR_R_MALLOC_FAILURE);
1337 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
1338 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1343 /* Generate a new ECDH key pair */
1344 if (!(EC_KEY_generate_key(clnt_ecdh))) {
1345 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1352 * use the 'p' output buffer for the ECDH key, but make sure to
1353 * clear it out afterwards
1356 field_size = EC_GROUP_get_degree(srvr_group);
1357 if (field_size <= 0) {
1358 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1361 n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
1364 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1368 /* generate master key from the result */
1369 s->session->master_key_length =
1370 s->method->ssl3_enc->generate_master_secret(s,
1372 session->master_key,
1375 memset(p, 0, n); /* clean up */
1377 if (ecdh_clnt_cert) {
1378 /* Send empty client key exch message */
1382 * First check the size of encoding and allocate memory
1386 EC_POINT_point2oct(srvr_group,
1387 EC_KEY_get0_public_key(clnt_ecdh),
1388 POINT_CONVERSION_UNCOMPRESSED,
1391 encodedPoint = (unsigned char *)
1392 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
1393 bn_ctx = BN_CTX_new();
1394 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1395 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1396 ERR_R_MALLOC_FAILURE);
1400 /* Encode the public key */
1401 n = EC_POINT_point2oct(srvr_group,
1402 EC_KEY_get0_public_key(clnt_ecdh),
1403 POINT_CONVERSION_UNCOMPRESSED,
1404 encodedPoint, encoded_pt_len, bn_ctx);
1406 *p = n; /* length of encoded point */
1407 /* Encoded point will be copied here */
1409 /* copy the point */
1410 memcpy((unsigned char *)p, encodedPoint, n);
1411 /* increment n to account for length field */
1415 /* Free allocated memory */
1416 BN_CTX_free(bn_ctx);
1417 if (encodedPoint != NULL)
1418 OPENSSL_free(encodedPoint);
1419 if (clnt_ecdh != NULL)
1420 EC_KEY_free(clnt_ecdh);
1421 EVP_PKEY_free(srvr_pub_pkey);
1423 #endif /* !OPENSSL_NO_ECDH */
1425 #ifndef OPENSSL_NO_PSK
1426 else if (alg_k & SSL_kPSK) {
1427 char identity[PSK_MAX_IDENTITY_LEN];
1428 unsigned char *t = NULL;
1429 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
1430 unsigned int pre_ms_len = 0, psk_len = 0;
1434 if (s->psk_client_callback == NULL) {
1435 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1436 SSL_R_PSK_NO_CLIENT_CB);
1440 psk_len = s->psk_client_callback(s, s->ctx->psk_identity_hint,
1441 identity, PSK_MAX_IDENTITY_LEN,
1443 sizeof(psk_or_pre_ms));
1444 if (psk_len > PSK_MAX_PSK_LEN) {
1445 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1446 ERR_R_INTERNAL_ERROR);
1448 } else if (psk_len == 0) {
1449 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1450 SSL_R_PSK_IDENTITY_NOT_FOUND);
1454 /* create PSK pre_master_secret */
1455 pre_ms_len = 2 + psk_len + 2 + psk_len;
1457 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
1459 memset(t, 0, psk_len);
1463 if (s->session->psk_identity_hint != NULL)
1464 OPENSSL_free(s->session->psk_identity_hint);
1465 s->session->psk_identity_hint =
1466 BUF_strdup(s->ctx->psk_identity_hint);
1467 if (s->ctx->psk_identity_hint != NULL
1468 && s->session->psk_identity_hint == NULL) {
1469 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1470 ERR_R_MALLOC_FAILURE);
1474 if (s->session->psk_identity != NULL)
1475 OPENSSL_free(s->session->psk_identity);
1476 s->session->psk_identity = BUF_strdup(identity);
1477 if (s->session->psk_identity == NULL) {
1478 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1479 ERR_R_MALLOC_FAILURE);
1483 s->session->master_key_length =
1484 s->method->ssl3_enc->generate_master_secret(s,
1486 session->master_key,
1489 n = strlen(identity);
1491 memcpy(p, identity, n);
1495 OPENSSL_cleanse(identity, PSK_MAX_IDENTITY_LEN);
1496 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
1498 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1504 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1505 SSLerr(SSL_F_DTLS1_SEND_CLIENT_KEY_EXCHANGE,
1506 ERR_R_INTERNAL_ERROR);
1510 d = dtls1_set_message_header(s, d,
1511 SSL3_MT_CLIENT_KEY_EXCHANGE, n, 0, n);
1513 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1515 l2n(s->d1->handshake_write_seq,d);
1516 s->d1->handshake_write_seq++;
1519 s->state = SSL3_ST_CW_KEY_EXCH_B;
1520 /* number of bytes to write */
1521 s->init_num = n + DTLS1_HM_HEADER_LENGTH;
1524 /* buffer the message to handle re-xmits */
1525 dtls1_buffer_message(s, 0);
1528 /* SSL3_ST_CW_KEY_EXCH_B */
1529 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1531 #ifndef OPENSSL_NO_ECDH
1532 BN_CTX_free(bn_ctx);
1533 if (encodedPoint != NULL)
1534 OPENSSL_free(encodedPoint);
1535 if (clnt_ecdh != NULL)
1536 EC_KEY_free(clnt_ecdh);
1537 EVP_PKEY_free(srvr_pub_pkey);
1542 int dtls1_send_client_verify(SSL *s)
1544 unsigned char *p, *d;
1545 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1547 #ifndef OPENSSL_NO_RSA
1551 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
1555 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
1556 d = (unsigned char *)s->init_buf->data;
1557 p = &(d[DTLS1_HM_HEADER_LENGTH]);
1558 pkey = s->cert->key->privatekey;
1560 s->method->ssl3_enc->cert_verify_mac(s,
1562 &(data[MD5_DIGEST_LENGTH]));
1564 #ifndef OPENSSL_NO_RSA
1565 if (pkey->type == EVP_PKEY_RSA) {
1566 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
1567 if (RSA_sign(NID_md5_sha1, data,
1568 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
1569 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1570 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
1577 #ifndef OPENSSL_NO_DSA
1578 if (pkey->type == EVP_PKEY_DSA) {
1579 if (!DSA_sign(pkey->save_type,
1580 &(data[MD5_DIGEST_LENGTH]),
1581 SHA_DIGEST_LENGTH, &(p[2]),
1582 (unsigned int *)&j, pkey->pkey.dsa)) {
1583 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
1590 #ifndef OPENSSL_NO_ECDSA
1591 if (pkey->type == EVP_PKEY_EC) {
1592 if (!ECDSA_sign(pkey->save_type,
1593 &(data[MD5_DIGEST_LENGTH]),
1594 SHA_DIGEST_LENGTH, &(p[2]),
1595 (unsigned int *)&j, pkey->pkey.ec)) {
1596 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
1604 SSLerr(SSL_F_DTLS1_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
1608 d = dtls1_set_message_header(s, d,
1609 SSL3_MT_CERTIFICATE_VERIFY, n, 0, n);
1611 s->init_num = (int)n + DTLS1_HM_HEADER_LENGTH;
1614 /* buffer the message to handle re-xmits */
1615 dtls1_buffer_message(s, 0);
1617 s->state = SSL3_ST_CW_CERT_VRFY_B;
1620 /* s->state = SSL3_ST_CW_CERT_VRFY_B */
1621 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
1626 int dtls1_send_client_certificate(SSL *s)
1629 EVP_PKEY *pkey = NULL;
1633 if (s->state == SSL3_ST_CW_CERT_A) {
1634 if ((s->cert == NULL) ||
1635 (s->cert->key->x509 == NULL) ||
1636 (s->cert->key->privatekey == NULL))
1637 s->state = SSL3_ST_CW_CERT_B;
1639 s->state = SSL3_ST_CW_CERT_C;
1642 /* We need to get a client cert */
1643 if (s->state == SSL3_ST_CW_CERT_B) {
1645 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
1646 * return(-1); We then get retied later
1649 i = ssl_do_client_cert_cb(s, &x509, &pkey);
1651 s->rwstate = SSL_X509_LOOKUP;
1654 s->rwstate = SSL_NOTHING;
1655 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
1656 s->state = SSL3_ST_CW_CERT_B;
1657 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
1659 } else if (i == 1) {
1661 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
1662 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1668 EVP_PKEY_free(pkey);
1670 if (s->version == SSL3_VERSION) {
1671 s->s3->tmp.cert_req = 0;
1672 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
1675 s->s3->tmp.cert_req = 2;
1679 /* Ok, we have a cert */
1680 s->state = SSL3_ST_CW_CERT_C;
1683 if (s->state == SSL3_ST_CW_CERT_C) {
1684 s->state = SSL3_ST_CW_CERT_D;
1685 l = dtls1_output_cert_chain(s,
1686 (s->s3->tmp.cert_req ==
1687 2) ? NULL : s->cert->key->x509);
1689 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1690 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
1693 s->init_num = (int)l;
1696 /* set header called by dtls1_output_cert_chain() */
1698 /* buffer the message to handle re-xmits */
1699 dtls1_buffer_message(s, 0);
1701 /* SSL3_ST_CW_CERT_D */
1702 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));