blob: 5f25dfc3401873b5056824b9277bd1e0f9899a89 [file] [log] [blame]
Ben Laurie36d16f82005-04-26 16:02:40 +00001/* ssl/d1_clnt.c */
2/*
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 */
6/* ====================================================================
Bodo Möller52b8dad2007-02-17 06:45:38 +00007 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
Ben Laurie36d16f82005-04-26 16:02:40 +00008 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 *
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
30 *
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
34 *
35 * 6. Redistributions of any form whatsoever must retain the following
36 * acknowledgment:
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
53 *
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
57 *
58 */
59/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
61 *
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
65 *
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72 *
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
79 *
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
82 * are met:
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97 *
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
114 */
115
116#include <stdio.h>
117#include "ssl_locl.h"
Dr. Stephen Hensonef236ec2009-04-23 16:32:42 +0000118#ifndef OPENSSL_NO_KRB5
119#include "kssl_lcl.h"
120#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000121#include <openssl/buffer.h>
122#include <openssl/rand.h>
123#include <openssl/objects.h>
124#include <openssl/evp.h>
125#include <openssl/md5.h>
Geoff Thorpe1e26a8b2008-03-16 21:05:46 +0000126#include <openssl/bn.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +0000127#ifndef OPENSSL_NO_DH
128#include <openssl/dh.h>
129#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000130
Nils Larsch4ebb3422005-08-14 21:48:33 +0000131static const SSL_METHOD *dtls1_get_client_method(int ver);
Ben Laurie36d16f82005-04-26 16:02:40 +0000132static int dtls1_get_hello_verify(SSL *s);
133
Nils Larsch4ebb3422005-08-14 21:48:33 +0000134static const SSL_METHOD *dtls1_get_client_method(int ver)
Ben Laurie36d16f82005-04-26 16:02:40 +0000135 {
Dr. Stephen Henson8711efb2009-04-20 11:33:12 +0000136 if (ver == DTLS1_VERSION || ver == DTLS1_BAD_VER)
Ben Laurie36d16f82005-04-26 16:02:40 +0000137 return(DTLSv1_client_method());
Dr. Stephen Hensonc3b344e2013-03-20 15:49:14 +0000138 else if (ver == DTLS1_2_VERSION)
139 return(DTLSv1_2_client_method());
Ben Laurie36d16f82005-04-26 16:02:40 +0000140 else
141 return(NULL);
142 }
143
Dr. Stephen Hensoncfd298b2013-03-19 15:51:26 +0000144IMPLEMENT_dtls1_meth_func(DTLS1_VERSION,
145 DTLSv1_client_method,
Dr. Stephen Hensonf3b656b2005-08-05 23:56:11 +0000146 ssl_undefined_function,
147 dtls1_connect,
Dr. Stephen Hensoncfd298b2013-03-19 15:51:26 +0000148 dtls1_get_client_method,
149 DTLSv1_enc_data)
Ben Laurie36d16f82005-04-26 16:02:40 +0000150
Dr. Stephen Hensonc3b344e2013-03-20 15:49:14 +0000151IMPLEMENT_dtls1_meth_func(DTLS1_2_VERSION,
152 DTLSv1_2_client_method,
153 ssl_undefined_function,
154 dtls1_connect,
155 dtls1_get_client_method,
156 DTLSv1_2_enc_data)
157
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100158IMPLEMENT_dtls1_meth_func(DTLS_ANY_VERSION,
159 DTLS_client_method,
160 ssl_undefined_function,
161 dtls1_connect,
162 dtls1_get_client_method,
163 DTLSv1_2_enc_data)
164
Ben Laurie36d16f82005-04-26 16:02:40 +0000165int dtls1_connect(SSL *s)
166 {
167 BUF_MEM *buf=NULL;
Bodo Möller7e695652007-02-19 14:53:18 +0000168 unsigned long Time=(unsigned long)time(NULL);
Ben Laurie36d16f82005-04-26 16:02:40 +0000169 void (*cb)(const SSL *ssl,int type,int val)=NULL;
170 int ret= -1;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000171 int new_state,state,skip=0;
172#ifndef OPENSSL_NO_SCTP
173 unsigned char sctpauthkey[64];
174 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
175#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000176
177 RAND_add(&Time,sizeof(Time),0);
178 ERR_clear_error();
179 clear_sys_error();
180
181 if (s->info_callback != NULL)
182 cb=s->info_callback;
183 else if (s->ctx->info_callback != NULL)
184 cb=s->ctx->info_callback;
185
186 s->in_handshake++;
187 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
188
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000189#ifndef OPENSSL_NO_SCTP
190 /* Notify SCTP BIO socket to enter handshake
191 * mode and prevent stream identifier other
192 * than 0. Will be ignored if no SCTP is used.
193 */
194 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
195#endif
196
Dr. Stephen Henson48175042011-12-31 22:59:57 +0000197#ifndef OPENSSL_NO_HEARTBEATS
198 /* If we're awaiting a HeartbeatResponse, pretend we
199 * already got and don't await it anymore, because
200 * Heartbeats don't make sense during handshakes anyway.
201 */
202 if (s->tlsext_hb_pending)
203 {
204 dtls1_stop_timer(s);
205 s->tlsext_hb_pending = 0;
206 s->tlsext_hb_seq++;
207 }
208#endif
209
Ben Laurie36d16f82005-04-26 16:02:40 +0000210 for (;;)
211 {
212 state=s->state;
213
214 switch(s->state)
215 {
216 case SSL_ST_RENEGOTIATE:
Dr. Stephen Henson44959ee2010-08-26 14:23:52 +0000217 s->renegotiate=1;
Ben Laurie36d16f82005-04-26 16:02:40 +0000218 s->state=SSL_ST_CONNECT;
219 s->ctx->stats.sess_connect_renegotiate++;
220 /* break */
221 case SSL_ST_BEFORE:
222 case SSL_ST_CONNECT:
223 case SSL_ST_BEFORE|SSL_ST_CONNECT:
224 case SSL_ST_OK|SSL_ST_CONNECT:
225
226 s->server=0;
227 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
228
Dr. Stephen Henson8711efb2009-04-20 11:33:12 +0000229 if ((s->version & 0xff00 ) != (DTLS1_VERSION & 0xff00) &&
230 (s->version & 0xff00 ) != (DTLS1_BAD_VER & 0xff00))
Ben Laurie36d16f82005-04-26 16:02:40 +0000231 {
Bodo Möllerbeb056b2005-04-26 18:08:00 +0000232 SSLerr(SSL_F_DTLS1_CONNECT, ERR_R_INTERNAL_ERROR);
Ben Laurie36d16f82005-04-26 16:02:40 +0000233 ret = -1;
234 goto end;
235 }
236
237 /* s->version=SSL3_VERSION; */
238 s->type=SSL_ST_CONNECT;
239
240 if (s->init_buf == NULL)
241 {
242 if ((buf=BUF_MEM_new()) == NULL)
243 {
244 ret= -1;
245 goto end;
246 }
247 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
248 {
249 ret= -1;
250 goto end;
251 }
252 s->init_buf=buf;
253 buf=NULL;
254 }
255
256 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
257
258 /* setup buffing BIO */
259 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
260
261 /* don't push the buffering BIO quite yet */
262
Ben Laurie36d16f82005-04-26 16:02:40 +0000263 s->state=SSL3_ST_CW_CLNT_HELLO_A;
264 s->ctx->stats.sess_connect++;
265 s->init_num=0;
Andy Polyakove7adda52007-09-30 19:15:27 +0000266 /* mark client_random uninitialized */
267 memset(s->s3->client_random,0,sizeof(s->s3->client_random));
Dr. Stephen Henson0f65d5e2009-07-24 11:52:55 +0000268 s->d1->send_cookie = 0;
269 s->hit = 0;
Ben Laurie36d16f82005-04-26 16:02:40 +0000270 break;
271
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000272#ifndef OPENSSL_NO_SCTP
273 case DTLS1_SCTP_ST_CR_READ_SOCK:
274
275 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)))
276 {
277 s->s3->in_read_app_data=2;
278 s->rwstate=SSL_READING;
279 BIO_clear_retry_flags(SSL_get_rbio(s));
280 BIO_set_retry_read(SSL_get_rbio(s));
281 ret = -1;
282 goto end;
283 }
284
285 s->state=s->s3->tmp.next_state;
286 break;
287
288 case DTLS1_SCTP_ST_CW_WRITE_SOCK:
289 /* read app data until dry event */
290
291 ret = BIO_dgram_sctp_wait_for_dry(SSL_get_wbio(s));
292 if (ret < 0) goto end;
293
294 if (ret == 0)
295 {
296 s->s3->in_read_app_data=2;
297 s->rwstate=SSL_READING;
298 BIO_clear_retry_flags(SSL_get_rbio(s));
299 BIO_set_retry_read(SSL_get_rbio(s));
300 ret = -1;
301 goto end;
302 }
303
304 s->state=s->d1->next_state;
305 break;
306#endif
307
Ben Laurie36d16f82005-04-26 16:02:40 +0000308 case SSL3_ST_CW_CLNT_HELLO_A:
309 case SSL3_ST_CW_CLNT_HELLO_B:
310
311 s->shutdown=0;
Andy Polyakov0d97d002007-09-30 19:34:36 +0000312
313 /* every DTLS ClientHello resets Finished MAC */
314 ssl3_init_finished_mac(s);
315
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000316 dtls1_start_timer(s);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000317 ret=ssl3_client_hello(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000318 if (ret <= 0) goto end;
319
320 if ( s->d1->send_cookie)
321 {
322 s->state=SSL3_ST_CW_FLUSH;
323 s->s3->tmp.next_state=SSL3_ST_CR_SRVR_HELLO_A;
324 }
325 else
326 s->state=SSL3_ST_CR_SRVR_HELLO_A;
327
328 s->init_num=0;
329
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000330#ifndef OPENSSL_NO_SCTP
331 /* Disable buffering for SCTP */
332 if (!BIO_dgram_is_sctp(SSL_get_wbio(s)))
333 {
334#endif
335 /* turn on buffering for the next lot of output */
336 if (s->bbio != s->wbio)
337 s->wbio=BIO_push(s->bbio,s->wbio);
338#ifndef OPENSSL_NO_SCTP
339 }
340#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000341
342 break;
343
344 case SSL3_ST_CR_SRVR_HELLO_A:
345 case SSL3_ST_CR_SRVR_HELLO_B:
346 ret=ssl3_get_server_hello(s);
347 if (ret <= 0) goto end;
348 else
349 {
350 if (s->hit)
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000351 {
352#ifndef OPENSSL_NO_SCTP
353 /* Add new shared key for SCTP-Auth,
354 * will be ignored if no SCTP used.
355 */
356 snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
357 DTLS1_SCTP_AUTH_LABEL);
358
359 SSL_export_keying_material(s, sctpauthkey,
360 sizeof(sctpauthkey), labelbuffer,
361 sizeof(labelbuffer), NULL, 0, 0);
362
363 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
364 sizeof(sctpauthkey), sctpauthkey);
365#endif
366
Ben Laurie36d16f82005-04-26 16:02:40 +0000367 s->state=SSL3_ST_CR_FINISHED_A;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000368 }
Ben Laurie36d16f82005-04-26 16:02:40 +0000369 else
370 s->state=DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
371 }
372 s->init_num=0;
373 break;
374
375 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
376 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
377
378 ret = dtls1_get_hello_verify(s);
379 if ( ret <= 0)
380 goto end;
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000381 dtls1_stop_timer(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000382 if ( s->d1->send_cookie) /* start again, with a cookie */
383 s->state=SSL3_ST_CW_CLNT_HELLO_A;
384 else
385 s->state = SSL3_ST_CR_CERT_A;
386 s->init_num = 0;
387 break;
388
389 case SSL3_ST_CR_CERT_A:
390 case SSL3_ST_CR_CERT_B:
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000391#ifndef OPENSSL_NO_TLSEXT
392 ret=ssl3_check_finished(s);
393 if (ret <= 0) goto end;
394 if (ret == 2)
395 {
396 s->hit = 1;
397 if (s->tlsext_ticket_expected)
398 s->state=SSL3_ST_CR_SESSION_TICKET_A;
399 else
400 s->state=SSL3_ST_CR_FINISHED_A;
401 s->init_num=0;
402 break;
403 }
404#endif
Dr. Stephen Henson6cf41fe2009-05-31 17:13:09 +0000405 /* Check if it is anon DH or PSK */
406 if (!(s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
407 !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK))
Ben Laurie36d16f82005-04-26 16:02:40 +0000408 {
409 ret=ssl3_get_server_certificate(s);
410 if (ret <= 0) goto end;
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000411#ifndef OPENSSL_NO_TLSEXT
412 if (s->tlsext_status_expected)
413 s->state=SSL3_ST_CR_CERT_STATUS_A;
414 else
415 s->state=SSL3_ST_CR_KEY_EXCH_A;
416 }
417 else
418 {
419 skip = 1;
420 s->state=SSL3_ST_CR_KEY_EXCH_A;
421 }
422#else
Ben Laurie36d16f82005-04-26 16:02:40 +0000423 }
424 else
425 skip=1;
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000426
Ben Laurie36d16f82005-04-26 16:02:40 +0000427 s->state=SSL3_ST_CR_KEY_EXCH_A;
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000428#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000429 s->init_num=0;
430 break;
431
432 case SSL3_ST_CR_KEY_EXCH_A:
433 case SSL3_ST_CR_KEY_EXCH_B:
434 ret=ssl3_get_key_exchange(s);
435 if (ret <= 0) goto end;
436 s->state=SSL3_ST_CR_CERT_REQ_A;
437 s->init_num=0;
438
439 /* at this point we check that we have the
440 * required stuff from the server */
441 if (!ssl3_check_cert_and_algorithm(s))
442 {
443 ret= -1;
444 goto end;
445 }
446 break;
447
448 case SSL3_ST_CR_CERT_REQ_A:
449 case SSL3_ST_CR_CERT_REQ_B:
450 ret=ssl3_get_certificate_request(s);
451 if (ret <= 0) goto end;
452 s->state=SSL3_ST_CR_SRVR_DONE_A;
453 s->init_num=0;
454 break;
455
456 case SSL3_ST_CR_SRVR_DONE_A:
457 case SSL3_ST_CR_SRVR_DONE_B:
458 ret=ssl3_get_server_done(s);
459 if (ret <= 0) goto end;
Dr. Stephen Henson0fbf8b92012-03-06 13:26:15 +0000460 dtls1_stop_timer(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000461 if (s->s3->tmp.cert_req)
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000462 s->s3->tmp.next_state=SSL3_ST_CW_CERT_A;
Ben Laurie36d16f82005-04-26 16:02:40 +0000463 else
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000464 s->s3->tmp.next_state=SSL3_ST_CW_KEY_EXCH_A;
Ben Laurie36d16f82005-04-26 16:02:40 +0000465 s->init_num=0;
466
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000467#ifndef OPENSSL_NO_SCTP
468 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
469 state == SSL_ST_RENEGOTIATE)
470 s->state=DTLS1_SCTP_ST_CR_READ_SOCK;
471 else
472#endif
473 s->state=s->s3->tmp.next_state;
Ben Laurie36d16f82005-04-26 16:02:40 +0000474 break;
475
476 case SSL3_ST_CW_CERT_A:
477 case SSL3_ST_CW_CERT_B:
478 case SSL3_ST_CW_CERT_C:
479 case SSL3_ST_CW_CERT_D:
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000480 dtls1_start_timer(s);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000481 ret=ssl3_send_client_certificate(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000482 if (ret <= 0) goto end;
483 s->state=SSL3_ST_CW_KEY_EXCH_A;
484 s->init_num=0;
485 break;
486
487 case SSL3_ST_CW_KEY_EXCH_A:
488 case SSL3_ST_CW_KEY_EXCH_B:
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000489 dtls1_start_timer(s);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000490 ret=ssl3_send_client_key_exchange(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000491 if (ret <= 0) goto end;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000492
493#ifndef OPENSSL_NO_SCTP
494 /* Add new shared key for SCTP-Auth,
495 * will be ignored if no SCTP used.
496 */
497 snprintf((char*) labelbuffer, sizeof(DTLS1_SCTP_AUTH_LABEL),
498 DTLS1_SCTP_AUTH_LABEL);
499
500 SSL_export_keying_material(s, sctpauthkey,
501 sizeof(sctpauthkey), labelbuffer,
502 sizeof(labelbuffer), NULL, 0, 0);
503
504 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
505 sizeof(sctpauthkey), sctpauthkey);
506#endif
507
Ben Laurie36d16f82005-04-26 16:02:40 +0000508 /* EAY EAY EAY need to check for DH fix cert
509 * sent back */
510 /* For TLS, cert_req is set to 2, so a cert chain
511 * of nothing is sent, but no verify packet is sent */
512 if (s->s3->tmp.cert_req == 1)
513 {
514 s->state=SSL3_ST_CW_CERT_VRFY_A;
515 }
516 else
517 {
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000518#ifndef OPENSSL_NO_SCTP
519 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
520 {
521 s->d1->next_state=SSL3_ST_CW_CHANGE_A;
522 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
523 }
524 else
525#endif
526 s->state=SSL3_ST_CW_CHANGE_A;
Ben Laurie36d16f82005-04-26 16:02:40 +0000527 s->s3->change_cipher_spec=0;
528 }
529
530 s->init_num=0;
531 break;
532
533 case SSL3_ST_CW_CERT_VRFY_A:
534 case SSL3_ST_CW_CERT_VRFY_B:
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000535 dtls1_start_timer(s);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000536 ret=ssl3_send_client_verify(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000537 if (ret <= 0) goto end;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000538#ifndef OPENSSL_NO_SCTP
539 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
540 {
541 s->d1->next_state=SSL3_ST_CW_CHANGE_A;
542 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
543 }
544 else
545#endif
546 s->state=SSL3_ST_CW_CHANGE_A;
Ben Laurie36d16f82005-04-26 16:02:40 +0000547 s->init_num=0;
548 s->s3->change_cipher_spec=0;
549 break;
550
551 case SSL3_ST_CW_CHANGE_A:
552 case SSL3_ST_CW_CHANGE_B:
Dr. Stephen Hensonee4b5ce2011-05-25 12:25:01 +0000553 if (!s->hit)
554 dtls1_start_timer(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000555 ret=dtls1_send_change_cipher_spec(s,
556 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
557 if (ret <= 0) goto end;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000558
Ben Laurie36d16f82005-04-26 16:02:40 +0000559 s->state=SSL3_ST_CW_FINISHED_A;
560 s->init_num=0;
561
562 s->session->cipher=s->s3->tmp.new_cipher;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000563#ifdef OPENSSL_NO_COMP
564 s->session->compress_meth=0;
565#else
Ben Laurie36d16f82005-04-26 16:02:40 +0000566 if (s->s3->tmp.new_compression == NULL)
567 s->session->compress_meth=0;
568 else
569 s->session->compress_meth=
570 s->s3->tmp.new_compression->id;
Dr. Stephen Henson09b6c2e2005-09-30 23:35:33 +0000571#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000572 if (!s->method->ssl3_enc->setup_key_block(s))
573 {
574 ret= -1;
575 goto end;
576 }
577
578 if (!s->method->ssl3_enc->change_cipher_state(s,
579 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
580 {
581 ret= -1;
582 goto end;
583 }
584
Robin Seggelmannb9ef52b2012-05-09 19:28:41 +0200585#ifndef OPENSSL_NO_SCTP
586 if (s->hit)
587 {
588 /* Change to new shared key of SCTP-Auth,
589 * will be ignored if no SCTP used.
590 */
591 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
592 }
593#endif
594
Ben Laurie36d16f82005-04-26 16:02:40 +0000595 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
596 break;
597
598 case SSL3_ST_CW_FINISHED_A:
599 case SSL3_ST_CW_FINISHED_B:
Dr. Stephen Hensonee4b5ce2011-05-25 12:25:01 +0000600 if (!s->hit)
601 dtls1_start_timer(s);
Dr. Stephen Henson173e72e2013-03-11 15:34:28 +0000602 ret=ssl3_send_finished(s,
Ben Laurie36d16f82005-04-26 16:02:40 +0000603 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
604 s->method->ssl3_enc->client_finished_label,
605 s->method->ssl3_enc->client_finished_label_len);
606 if (ret <= 0) goto end;
607 s->state=SSL3_ST_CW_FLUSH;
608
609 /* clear flags */
610 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
611 if (s->hit)
612 {
613 s->s3->tmp.next_state=SSL_ST_OK;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000614#ifndef OPENSSL_NO_SCTP
615 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
616 {
617 s->d1->next_state = s->s3->tmp.next_state;
618 s->s3->tmp.next_state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
619 }
620#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000621 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
622 {
623 s->state=SSL_ST_OK;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000624#ifndef OPENSSL_NO_SCTP
625 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
626 {
627 s->d1->next_state = SSL_ST_OK;
628 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
629 }
630#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000631 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
632 s->s3->delay_buf_pop_ret=0;
633 }
634 }
635 else
636 {
Robin Seggelmannb9ef52b2012-05-09 19:28:41 +0200637#ifndef OPENSSL_NO_SCTP
638 /* Change to new shared key of SCTP-Auth,
639 * will be ignored if no SCTP used.
640 */
641 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 0, NULL);
642#endif
643
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000644#ifndef OPENSSL_NO_TLSEXT
645 /* Allow NewSessionTicket if ticket expected */
646 if (s->tlsext_ticket_expected)
647 s->s3->tmp.next_state=SSL3_ST_CR_SESSION_TICKET_A;
648 else
649#endif
650
Ben Laurie36d16f82005-04-26 16:02:40 +0000651 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
652 }
653 s->init_num=0;
654 break;
655
Dr. Stephen Henson8025e252009-12-08 11:37:40 +0000656#ifndef OPENSSL_NO_TLSEXT
657 case SSL3_ST_CR_SESSION_TICKET_A:
658 case SSL3_ST_CR_SESSION_TICKET_B:
659 ret=ssl3_get_new_session_ticket(s);
660 if (ret <= 0) goto end;
661 s->state=SSL3_ST_CR_FINISHED_A;
662 s->init_num=0;
663 break;
664
665 case SSL3_ST_CR_CERT_STATUS_A:
666 case SSL3_ST_CR_CERT_STATUS_B:
667 ret=ssl3_get_cert_status(s);
668 if (ret <= 0) goto end;
669 s->state=SSL3_ST_CR_KEY_EXCH_A;
670 s->init_num=0;
671 break;
672#endif
673
Ben Laurie36d16f82005-04-26 16:02:40 +0000674 case SSL3_ST_CR_FINISHED_A:
675 case SSL3_ST_CR_FINISHED_B:
Dr. Stephen Henson58f41a92009-06-05 14:59:26 +0000676 s->d1->change_cipher_spec_ok = 1;
Ben Laurie36d16f82005-04-26 16:02:40 +0000677 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
678 SSL3_ST_CR_FINISHED_B);
679 if (ret <= 0) goto end;
Dr. Stephen Hensoneb38b262009-05-15 22:58:40 +0000680 dtls1_stop_timer(s);
Ben Laurie36d16f82005-04-26 16:02:40 +0000681
682 if (s->hit)
683 s->state=SSL3_ST_CW_CHANGE_A;
684 else
685 s->state=SSL_ST_OK;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000686
687#ifndef OPENSSL_NO_SCTP
688 if (BIO_dgram_is_sctp(SSL_get_wbio(s)) &&
689 state == SSL_ST_RENEGOTIATE)
690 {
691 s->d1->next_state=s->state;
692 s->state=DTLS1_SCTP_ST_CW_WRITE_SOCK;
693 }
694#endif
695
Ben Laurie36d16f82005-04-26 16:02:40 +0000696 s->init_num=0;
697 break;
698
699 case SSL3_ST_CW_FLUSH:
Dr. Stephen Hensond5e7f2f2010-01-26 19:47:37 +0000700 s->rwstate=SSL_WRITING;
701 if (BIO_flush(s->wbio) <= 0)
Ben Laurie36d16f82005-04-26 16:02:40 +0000702 {
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000703 /* If the write error was fatal, stop trying */
704 if (!BIO_should_retry(s->wbio))
705 {
706 s->rwstate=SSL_NOTHING;
707 s->state=s->s3->tmp.next_state;
708 }
709
Dr. Stephen Hensond5e7f2f2010-01-26 19:47:37 +0000710 ret= -1;
711 goto end;
Ben Laurie36d16f82005-04-26 16:02:40 +0000712 }
Dr. Stephen Hensond5e7f2f2010-01-26 19:47:37 +0000713 s->rwstate=SSL_NOTHING;
Ben Laurie36d16f82005-04-26 16:02:40 +0000714 s->state=s->s3->tmp.next_state;
715 break;
716
717 case SSL_ST_OK:
718 /* clean a few things up */
719 ssl3_cleanup_key_block(s);
720
721#if 0
722 if (s->init_buf != NULL)
723 {
724 BUF_MEM_free(s->init_buf);
725 s->init_buf=NULL;
726 }
727#endif
728
729 /* If we are not 'joining' the last two packets,
730 * remove the buffering now */
731 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
732 ssl_free_wbio_buffer(s);
733 /* else do it later in ssl3_write */
734
735 s->init_num=0;
Dr. Stephen Henson44959ee2010-08-26 14:23:52 +0000736 s->renegotiate=0;
Ben Laurie36d16f82005-04-26 16:02:40 +0000737 s->new_session=0;
738
739 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
740 if (s->hit) s->ctx->stats.sess_hit++;
741
742 ret=1;
743 /* s->server=0; */
744 s->handshake_func=dtls1_connect;
745 s->ctx->stats.sess_connect_good++;
746
747 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
748
749 /* done with handshaking */
750 s->d1->handshake_read_seq = 0;
Dr. Stephen Hensone5fa8642009-04-15 15:27:03 +0000751 s->d1->next_handshake_write_seq = 0;
Ben Laurie36d16f82005-04-26 16:02:40 +0000752 goto end;
753 /* break; */
754
755 default:
Bodo Möllerbeb056b2005-04-26 18:08:00 +0000756 SSLerr(SSL_F_DTLS1_CONNECT,SSL_R_UNKNOWN_STATE);
Ben Laurie36d16f82005-04-26 16:02:40 +0000757 ret= -1;
758 goto end;
759 /* break; */
760 }
761
762 /* did we do anything */
763 if (!s->s3->tmp.reuse_message && !skip)
764 {
765 if (s->debug)
766 {
767 if ((ret=BIO_flush(s->wbio)) <= 0)
768 goto end;
769 }
770
771 if ((cb != NULL) && (s->state != state))
772 {
773 new_state=s->state;
774 s->state=state;
775 cb(s,SSL_CB_CONNECT_LOOP,1);
776 s->state=new_state;
777 }
778 }
779 skip=0;
780 }
781end:
782 s->in_handshake--;
Dr. Stephen Henson7e159e02011-12-25 14:45:15 +0000783
784#ifndef OPENSSL_NO_SCTP
785 /* Notify SCTP BIO socket to leave handshake
786 * mode and allow stream identifier other
787 * than 0. Will be ignored if no SCTP is used.
788 */
789 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, s->in_handshake, NULL);
790#endif
791
Ben Laurie36d16f82005-04-26 16:02:40 +0000792 if (buf != NULL)
793 BUF_MEM_free(buf);
794 if (cb != NULL)
795 cb(s,SSL_CB_CONNECT_EXIT,ret);
796 return(ret);
797 }
798
Ben Laurie36d16f82005-04-26 16:02:40 +0000799static int dtls1_get_hello_verify(SSL *s)
800 {
801 int n, al, ok = 0;
802 unsigned char *data;
803 unsigned int cookie_len;
804
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100805 s->first_packet = 1;
Ben Laurie36d16f82005-04-26 16:02:40 +0000806 n=s->method->ssl_get_message(s,
807 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
808 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
809 -1,
810 s->max_cert_list,
811 &ok);
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100812 s->first_packet = 0;
Ben Laurie36d16f82005-04-26 16:02:40 +0000813
814 if (!ok) return((int)n);
815
816 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST)
817 {
818 s->d1->send_cookie = 0;
819 s->s3->tmp.reuse_message=1;
820 return(1);
821 }
822
823 data = (unsigned char *)s->init_msg;
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100824#if 0
825 if (s->method->version != DTLS_ANY_VERSION &&
826 ((data[0] != (s->version>>8)) || (data[1] != (s->version&0xff))))
Ben Laurie36d16f82005-04-26 16:02:40 +0000827 {
Bodo Möllerbeb056b2005-04-26 18:08:00 +0000828 SSLerr(SSL_F_DTLS1_GET_HELLO_VERIFY,SSL_R_WRONG_SSL_VERSION);
Ben Laurie36d16f82005-04-26 16:02:40 +0000829 s->version=(s->version&0xff00)|data[1];
830 al = SSL_AD_PROTOCOL_VERSION;
831 goto f_err;
832 }
Dr. Stephen Hensonc6913ee2013-04-06 15:50:12 +0100833#endif
Ben Laurie36d16f82005-04-26 16:02:40 +0000834 data+=2;
835
836 cookie_len = *(data++);
837 if ( cookie_len > sizeof(s->d1->cookie))
838 {
839 al=SSL_AD_ILLEGAL_PARAMETER;
840 goto f_err;
841 }
842
843 memcpy(s->d1->cookie, data, cookie_len);
844 s->d1->cookie_len = cookie_len;
845
846 s->d1->send_cookie = 1;
847 return 1;
848
849f_err:
850 ssl3_send_alert(s, SSL3_AL_FATAL, al);
851 return -1;
852 }
853