1 /* $OpenBSD: d1_clnt.c,v 1.76 2017/05/07 04:22:24 beck Exp $ */
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.]
119 #include "ssl_locl.h"
121 #include <openssl/bn.h>
122 #include <openssl/buffer.h>
123 #include <openssl/dh.h>
124 #include <openssl/evp.h>
125 #include <openssl/md5.h>
126 #include <openssl/objects.h>
128 #include "bytestring.h"
130 static int dtls1_get_hello_verify(SSL
*s
);
132 static const SSL_METHOD_INTERNAL DTLSv1_client_method_internal_data
= {
133 .version
= DTLS1_VERSION
,
134 .min_version
= DTLS1_VERSION
,
135 .max_version
= DTLS1_VERSION
,
136 .ssl_new
= dtls1_new
,
137 .ssl_clear
= dtls1_clear
,
138 .ssl_free
= dtls1_free
,
139 .ssl_accept
= ssl_undefined_function
,
140 .ssl_connect
= dtls1_connect
,
141 .ssl_read
= ssl3_read
,
142 .ssl_peek
= ssl3_peek
,
143 .ssl_write
= ssl3_write
,
144 .ssl_shutdown
= dtls1_shutdown
,
145 .ssl_pending
= ssl3_pending
,
146 .get_ssl_method
= dtls1_get_client_method
,
147 .get_timeout
= dtls1_default_timeout
,
148 .ssl_version
= ssl_undefined_void_function
,
149 .ssl_renegotiate
= ssl3_renegotiate
,
150 .ssl_renegotiate_check
= ssl3_renegotiate_check
,
151 .ssl_get_message
= dtls1_get_message
,
152 .ssl_read_bytes
= dtls1_read_bytes
,
153 .ssl_write_bytes
= dtls1_write_app_data_bytes
,
154 .ssl3_enc
= &DTLSv1_enc_data
,
157 static const SSL_METHOD DTLSv1_client_method_data
= {
158 .ssl_dispatch_alert
= dtls1_dispatch_alert
,
159 .num_ciphers
= ssl3_num_ciphers
,
160 .get_cipher
= dtls1_get_cipher
,
161 .get_cipher_by_char
= ssl3_get_cipher_by_char
,
162 .put_cipher_by_char
= ssl3_put_cipher_by_char
,
163 .internal
= &DTLSv1_client_method_internal_data
,
167 DTLSv1_client_method(void)
169 return &DTLSv1_client_method_data
;
173 dtls1_get_client_method(int ver
)
175 if (ver
== DTLS1_VERSION
)
176 return (DTLSv1_client_method());
181 dtls1_connect(SSL
*s
)
183 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
185 int new_state
, state
, skip
= 0;
190 if (s
->internal
->info_callback
!= NULL
)
191 cb
= s
->internal
->info_callback
;
192 else if (s
->ctx
->internal
->info_callback
!= NULL
)
193 cb
= s
->ctx
->internal
->info_callback
;
195 s
->internal
->in_handshake
++;
196 if (!SSL_in_init(s
) || SSL_in_before(s
))
201 state
= S3I(s
)->hs
.state
;
203 switch (S3I(s
)->hs
.state
) {
204 case SSL_ST_RENEGOTIATE
:
205 s
->internal
->renegotiate
= 1;
206 S3I(s
)->hs
.state
= SSL_ST_CONNECT
;
207 s
->ctx
->internal
->stats
.sess_connect_renegotiate
++;
211 case SSL_ST_BEFORE
|SSL_ST_CONNECT
:
212 case SSL_ST_OK
|SSL_ST_CONNECT
:
216 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
218 if ((s
->version
& 0xff00 ) != (DTLS1_VERSION
& 0xff00)) {
219 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
224 /* s->version=SSL3_VERSION; */
225 s
->internal
->type
= SSL_ST_CONNECT
;
227 if (!ssl3_setup_init_buffer(s
)) {
231 if (!ssl3_setup_buffers(s
)) {
235 if (!ssl_init_wbio_buffer(s
, 0)) {
240 /* don't push the buffering BIO quite yet */
242 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_A
;
243 s
->ctx
->internal
->stats
.sess_connect
++;
244 s
->internal
->init_num
= 0;
245 /* mark client_random uninitialized */
246 memset(s
->s3
->client_random
, 0,
247 sizeof(s
->s3
->client_random
));
248 D1I(s
)->send_cookie
= 0;
249 s
->internal
->hit
= 0;
253 case SSL3_ST_CW_CLNT_HELLO_A
:
254 case SSL3_ST_CW_CLNT_HELLO_B
:
256 s
->internal
->shutdown
= 0;
258 /* every DTLS ClientHello resets Finished MAC */
259 if (!tls1_init_finished_mac(s
)) {
264 dtls1_start_timer(s
);
265 ret
= ssl3_client_hello(s
);
269 if (D1I(s
)->send_cookie
) {
270 S3I(s
)->hs
.state
= SSL3_ST_CW_FLUSH
;
271 S3I(s
)->hs
.next_state
= SSL3_ST_CR_SRVR_HELLO_A
;
273 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_HELLO_A
;
275 s
->internal
->init_num
= 0;
277 /* turn on buffering for the next lot of output */
278 if (s
->bbio
!= s
->wbio
)
279 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
283 case SSL3_ST_CR_SRVR_HELLO_A
:
284 case SSL3_ST_CR_SRVR_HELLO_B
:
285 ret
= ssl3_get_server_hello(s
);
289 if (s
->internal
->hit
) {
291 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
293 S3I(s
)->hs
.state
= DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A
;
295 s
->internal
->init_num
= 0;
298 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A
:
299 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B
:
301 ret
= dtls1_get_hello_verify(s
);
305 if ( D1I(s
)->send_cookie
) /* start again, with a cookie */
306 S3I(s
)->hs
.state
= SSL3_ST_CW_CLNT_HELLO_A
;
308 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_A
;
309 s
->internal
->init_num
= 0;
312 case SSL3_ST_CR_CERT_A
:
313 case SSL3_ST_CR_CERT_B
:
314 ret
= ssl3_check_finished(s
);
318 s
->internal
->hit
= 1;
319 if (s
->internal
->tlsext_ticket_expected
)
320 S3I(s
)->hs
.state
= SSL3_ST_CR_SESSION_TICKET_A
;
322 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
323 s
->internal
->init_num
= 0;
326 /* Check if it is anon DH. */
327 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
&
329 ret
= ssl3_get_server_certificate(s
);
332 if (s
->internal
->tlsext_status_expected
)
333 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_STATUS_A
;
335 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
338 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
340 s
->internal
->init_num
= 0;
343 case SSL3_ST_CR_KEY_EXCH_A
:
344 case SSL3_ST_CR_KEY_EXCH_B
:
345 ret
= ssl3_get_server_key_exchange(s
);
348 S3I(s
)->hs
.state
= SSL3_ST_CR_CERT_REQ_A
;
349 s
->internal
->init_num
= 0;
351 /* at this point we check that we have the
352 * required stuff from the server */
353 if (!ssl3_check_cert_and_algorithm(s
)) {
359 case SSL3_ST_CR_CERT_REQ_A
:
360 case SSL3_ST_CR_CERT_REQ_B
:
361 ret
= ssl3_get_certificate_request(s
);
364 S3I(s
)->hs
.state
= SSL3_ST_CR_SRVR_DONE_A
;
365 s
->internal
->init_num
= 0;
368 case SSL3_ST_CR_SRVR_DONE_A
:
369 case SSL3_ST_CR_SRVR_DONE_B
:
370 ret
= ssl3_get_server_done(s
);
374 if (S3I(s
)->tmp
.cert_req
)
375 S3I(s
)->hs
.next_state
= SSL3_ST_CW_CERT_A
;
377 S3I(s
)->hs
.next_state
= SSL3_ST_CW_KEY_EXCH_A
;
378 s
->internal
->init_num
= 0;
379 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
382 case SSL3_ST_CW_CERT_A
:
383 case SSL3_ST_CW_CERT_B
:
384 case SSL3_ST_CW_CERT_C
:
385 case SSL3_ST_CW_CERT_D
:
386 dtls1_start_timer(s
);
387 ret
= ssl3_send_client_certificate(s
);
390 S3I(s
)->hs
.state
= SSL3_ST_CW_KEY_EXCH_A
;
391 s
->internal
->init_num
= 0;
394 case SSL3_ST_CW_KEY_EXCH_A
:
395 case SSL3_ST_CW_KEY_EXCH_B
:
396 dtls1_start_timer(s
);
397 ret
= ssl3_send_client_key_exchange(s
);
401 /* EAY EAY EAY need to check for DH fix cert
403 /* For TLS, cert_req is set to 2, so a cert chain
404 * of nothing is sent, but no verify packet is sent */
405 if (S3I(s
)->tmp
.cert_req
== 1) {
406 S3I(s
)->hs
.state
= SSL3_ST_CW_CERT_VRFY_A
;
408 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
409 S3I(s
)->change_cipher_spec
= 0;
412 s
->internal
->init_num
= 0;
415 case SSL3_ST_CW_CERT_VRFY_A
:
416 case SSL3_ST_CW_CERT_VRFY_B
:
417 dtls1_start_timer(s
);
418 ret
= ssl3_send_client_verify(s
);
421 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
422 s
->internal
->init_num
= 0;
423 S3I(s
)->change_cipher_spec
= 0;
426 case SSL3_ST_CW_CHANGE_A
:
427 case SSL3_ST_CW_CHANGE_B
:
428 if (!s
->internal
->hit
)
429 dtls1_start_timer(s
);
430 ret
= dtls1_send_change_cipher_spec(s
,
431 SSL3_ST_CW_CHANGE_A
, SSL3_ST_CW_CHANGE_B
);
435 S3I(s
)->hs
.state
= SSL3_ST_CW_FINISHED_A
;
436 s
->internal
->init_num
= 0;
438 s
->session
->cipher
= S3I(s
)->hs
.new_cipher
;
439 if (!tls1_setup_key_block(s
)) {
444 if (!tls1_change_cipher_state(s
,
445 SSL3_CHANGE_CIPHER_CLIENT_WRITE
)) {
451 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
454 case SSL3_ST_CW_FINISHED_A
:
455 case SSL3_ST_CW_FINISHED_B
:
456 if (!s
->internal
->hit
)
457 dtls1_start_timer(s
);
458 ret
= ssl3_send_finished(s
,
459 SSL3_ST_CW_FINISHED_A
, SSL3_ST_CW_FINISHED_B
,
460 TLS_MD_CLIENT_FINISH_CONST
,
461 TLS_MD_CLIENT_FINISH_CONST_SIZE
);
464 S3I(s
)->hs
.state
= SSL3_ST_CW_FLUSH
;
467 s
->s3
->flags
&= ~SSL3_FLAGS_POP_BUFFER
;
468 if (s
->internal
->hit
) {
469 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
470 if (s
->s3
->flags
& SSL3_FLAGS_DELAY_CLIENT_FINISHED
) {
471 S3I(s
)->hs
.state
= SSL_ST_OK
;
472 s
->s3
->flags
|= SSL3_FLAGS_POP_BUFFER
;
473 S3I(s
)->delay_buf_pop_ret
= 0;
477 /* Allow NewSessionTicket if ticket expected */
478 if (s
->internal
->tlsext_ticket_expected
)
479 S3I(s
)->hs
.next_state
=
480 SSL3_ST_CR_SESSION_TICKET_A
;
482 S3I(s
)->hs
.next_state
=
483 SSL3_ST_CR_FINISHED_A
;
485 s
->internal
->init_num
= 0;
488 case SSL3_ST_CR_SESSION_TICKET_A
:
489 case SSL3_ST_CR_SESSION_TICKET_B
:
490 ret
= ssl3_get_new_session_ticket(s
);
493 S3I(s
)->hs
.state
= SSL3_ST_CR_FINISHED_A
;
494 s
->internal
->init_num
= 0;
497 case SSL3_ST_CR_CERT_STATUS_A
:
498 case SSL3_ST_CR_CERT_STATUS_B
:
499 ret
= ssl3_get_cert_status(s
);
502 S3I(s
)->hs
.state
= SSL3_ST_CR_KEY_EXCH_A
;
503 s
->internal
->init_num
= 0;
506 case SSL3_ST_CR_FINISHED_A
:
507 case SSL3_ST_CR_FINISHED_B
:
508 D1I(s
)->change_cipher_spec_ok
= 1;
509 ret
= ssl3_get_finished(s
, SSL3_ST_CR_FINISHED_A
,
510 SSL3_ST_CR_FINISHED_B
);
515 if (s
->internal
->hit
)
516 S3I(s
)->hs
.state
= SSL3_ST_CW_CHANGE_A
;
518 S3I(s
)->hs
.state
= SSL_ST_OK
;
521 s
->internal
->init_num
= 0;
524 case SSL3_ST_CW_FLUSH
:
525 s
->internal
->rwstate
= SSL_WRITING
;
526 if (BIO_flush(s
->wbio
) <= 0) {
527 /* If the write error was fatal, stop trying */
528 if (!BIO_should_retry(s
->wbio
)) {
529 s
->internal
->rwstate
= SSL_NOTHING
;
530 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
536 s
->internal
->rwstate
= SSL_NOTHING
;
537 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
541 /* clean a few things up */
542 tls1_cleanup_key_block(s
);
544 /* If we are not 'joining' the last two packets,
545 * remove the buffering now */
546 if (!(s
->s3
->flags
& SSL3_FLAGS_POP_BUFFER
))
547 ssl_free_wbio_buffer(s
);
548 /* else do it later in ssl3_write */
550 s
->internal
->init_num
= 0;
551 s
->internal
->renegotiate
= 0;
552 s
->internal
->new_session
= 0;
554 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
555 if (s
->internal
->hit
)
556 s
->ctx
->internal
->stats
.sess_hit
++;
560 s
->internal
->handshake_func
= dtls1_connect
;
561 s
->ctx
->internal
->stats
.sess_connect_good
++;
564 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
566 /* done with handshaking */
567 D1I(s
)->handshake_read_seq
= 0;
568 D1I(s
)->next_handshake_write_seq
= 0;
573 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
579 /* did we do anything */
580 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
581 if (s
->internal
->debug
) {
582 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
586 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
587 new_state
= S3I(s
)->hs
.state
;
588 S3I(s
)->hs
.state
= state
;
589 cb(s
, SSL_CB_CONNECT_LOOP
, 1);
590 S3I(s
)->hs
.state
= new_state
;
597 s
->internal
->in_handshake
--;
599 cb(s
, SSL_CB_CONNECT_EXIT
, ret
);
605 dtls1_get_hello_verify(SSL
*s
)
610 uint16_t ssl_version
;
611 CBS hello_verify_request
, cookie
;
613 n
= s
->method
->internal
->ssl_get_message(s
, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A
,
614 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B
, -1, s
->internal
->max_cert_list
, &ok
);
619 if (S3I(s
)->tmp
.message_type
!= DTLS1_MT_HELLO_VERIFY_REQUEST
) {
620 D1I(s
)->send_cookie
= 0;
621 S3I(s
)->tmp
.reuse_message
= 1;
628 CBS_init(&hello_verify_request
, s
->internal
->init_msg
, n
);
630 if (!CBS_get_u16(&hello_verify_request
, &ssl_version
))
633 if (ssl_version
!= s
->version
) {
634 SSLerror(s
, SSL_R_WRONG_SSL_VERSION
);
635 s
->version
= (s
->version
& 0xff00) | (ssl_version
& 0xff);
636 al
= SSL_AD_PROTOCOL_VERSION
;
640 if (!CBS_get_u8_length_prefixed(&hello_verify_request
, &cookie
))
643 if (!CBS_write_bytes(&cookie
, D1I(s
)->cookie
,
644 sizeof(D1I(s
)->cookie
), &cookie_len
)) {
645 D1I(s
)->cookie_len
= 0;
646 al
= SSL_AD_ILLEGAL_PARAMETER
;
649 D1I(s
)->cookie_len
= cookie_len
;
650 D1I(s
)->send_cookie
= 1;
655 al
= SSL_AD_DECODE_ERROR
;
657 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);