blob: 0ed1aea4a4fea19b6b3af061edc9a026a064e55e [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/*
2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
59 * All rights reserved.
60 *
61 * This package is an SSL implementation written
62 * by Eric Young (eay@cryptsoft.com).
63 * The implementation was written so as to conform with Netscapes SSL.
64 *
65 * This library is free for commercial and non-commercial use as long as
66 * the following conditions are aheared to. The following conditions
67 * apply to all code found in this distribution, be it the RC4, RSA,
68 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
69 * included with this distribution is covered by the same copyright terms
70 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
71 *
72 * Copyright remains Eric Young's, and as such any Copyright notices in
73 * the code are not to be removed.
74 * If this package is used in a product, Eric Young should be given attribution
75 * as the author of the parts of the library used.
76 * This can be in the form of a textual message at program startup or
77 * in documentation (online or textual) provided with the package.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * "This product includes cryptographic software written by
90 * Eric Young (eay@cryptsoft.com)"
91 * The word 'cryptographic' can be left out if the rouines from the library
92 * being used are not cryptographic related :-).
93 * 4. If you include any Windows specific code (or a derivative thereof) from
94 * the apps directory (application code) you must include an acknowledgement:
95 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
96 *
97 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
108 *
109 * The licence and distribution terms for any publically available version or
110 * derivative of this code cannot be changed. i.e. this code cannot simply be
111 * copied and put under another distribution licence
112 * [including the GNU Public Licence.]
113 */
114
David Benjamin9e4e01e2015-09-15 01:48:04 -0400115#include <openssl/ssl.h>
116
David Benjamin0b145c22014-11-26 20:10:09 -0500117#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120
121#include <openssl/bn.h>
122#include <openssl/buf.h>
123#include <openssl/dh.h>
124#include <openssl/evp.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400125#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/md5.h>
127#include <openssl/mem.h>
128#include <openssl/obj.h>
129#include <openssl/rand.h>
130
David Benjamin2ee94aa2015-04-07 22:38:30 -0400131#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin9e4e01e2015-09-15 01:48:04 -0400133
Adam Langley95c29f32014-06-20 12:00:00 -0700134static int dtls1_get_hello_verify(SSL *s);
135
Adam Langley71d8a082014-12-13 16:28:18 -0800136int dtls1_connect(SSL *s) {
137 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400138 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800139 int ret = -1;
140 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700141
Adam Langley71d8a082014-12-13 16:28:18 -0800142 assert(s->handshake_func == dtls1_connect);
143 assert(!s->server);
144 assert(SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500145
Adam Langley71d8a082014-12-13 16:28:18 -0800146 ERR_clear_error();
147 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700148
Adam Langley71d8a082014-12-13 16:28:18 -0800149 if (s->info_callback != NULL) {
150 cb = s->info_callback;
151 } else if (s->ctx->info_callback != NULL) {
152 cb = s->ctx->info_callback;
153 }
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley71d8a082014-12-13 16:28:18 -0800155 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700156
Adam Langley71d8a082014-12-13 16:28:18 -0800157 for (;;) {
158 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700159
Adam Langley71d8a082014-12-13 16:28:18 -0800160 switch (s->state) {
Adam Langley71d8a082014-12-13 16:28:18 -0800161 case SSL_ST_CONNECT:
Adam Langley71d8a082014-12-13 16:28:18 -0800162 if (cb != NULL) {
163 cb(s, SSL_CB_HANDSHAKE_START, 1);
164 }
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langley71d8a082014-12-13 16:28:18 -0800166 if (s->init_buf == NULL) {
167 buf = BUF_MEM_new();
168 if (buf == NULL ||
169 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
170 ret = -1;
171 goto end;
172 }
173 s->init_buf = buf;
174 buf = NULL;
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjamin6a08da22015-05-08 22:58:12 -0400177 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800178 ret = -1;
179 goto end;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley71d8a082014-12-13 16:28:18 -0800182 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley71d8a082014-12-13 16:28:18 -0800184 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800185 s->init_num = 0;
186 s->d1->send_cookie = 0;
187 s->hit = 0;
188 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190 case SSL3_ST_CW_CLNT_HELLO_A:
191 case SSL3_ST_CW_CLNT_HELLO_B:
192 s->shutdown = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800193 dtls1_start_timer(s);
194 ret = ssl3_send_client_hello(s);
195 if (ret <= 0) {
196 goto end;
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
Adam Langley71d8a082014-12-13 16:28:18 -0800199 if (s->d1->send_cookie) {
200 s->state = SSL3_ST_CW_FLUSH;
201 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
202 } else {
203 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langley71d8a082014-12-13 16:28:18 -0800206 s->init_num = 0;
207 /* turn on buffering for the next lot of output */
208 if (s->bbio != s->wbio) {
209 s->wbio = BIO_push(s->bbio, s->wbio);
210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
Adam Langley71d8a082014-12-13 16:28:18 -0800212 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langley71d8a082014-12-13 16:28:18 -0800214 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
215 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
216 ret = dtls1_get_hello_verify(s);
217 if (ret <= 0) {
218 goto end;
219 }
220 if (s->d1->send_cookie) {
221 /* start again, with a cookie */
222 dtls1_stop_timer(s);
223 s->state = SSL3_ST_CW_CLNT_HELLO_A;
224 } else {
225 s->state = SSL3_ST_CR_SRVR_HELLO_A;
226 }
227 s->init_num = 0;
228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langley71d8a082014-12-13 16:28:18 -0800230 case SSL3_ST_CR_SRVR_HELLO_A:
231 case SSL3_ST_CR_SRVR_HELLO_B:
232 ret = ssl3_get_server_hello(s);
233 if (ret <= 0) {
234 goto end;
235 }
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langley71d8a082014-12-13 16:28:18 -0800237 if (s->hit) {
238 s->state = SSL3_ST_CR_FINISHED_A;
239 if (s->tlsext_ticket_expected) {
240 /* receive renewed session ticket */
241 s->state = SSL3_ST_CR_SESSION_TICKET_A;
242 }
243 } else {
244 s->state = SSL3_ST_CR_CERT_A;
245 }
246 s->init_num = 0;
247 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langley71d8a082014-12-13 16:28:18 -0800249 case SSL3_ST_CR_CERT_A:
250 case SSL3_ST_CR_CERT_B:
251 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
252 ret = ssl3_get_server_certificate(s);
253 if (ret <= 0) {
254 goto end;
255 }
256 if (s->s3->tmp.certificate_status_expected) {
257 s->state = SSL3_ST_CR_CERT_STATUS_A;
258 } else {
Paul Lietar8f1c2682015-08-18 12:21:54 +0100259 s->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley71d8a082014-12-13 16:28:18 -0800260 }
261 } else {
262 skip = 1;
263 s->state = SSL3_ST_CR_KEY_EXCH_A;
264 }
265 s->init_num = 0;
266 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Paul Lietar8f1c2682015-08-18 12:21:54 +0100268 case SSL3_ST_VERIFY_SERVER_CERT:
269 ret = ssl3_verify_server_cert(s);
270 if (ret <= 0) {
271 goto end;
272 }
273
274 s->state = SSL3_ST_CR_KEY_EXCH_A;
275 s->init_num = 0;
276 break;
277
Adam Langley71d8a082014-12-13 16:28:18 -0800278 case SSL3_ST_CR_KEY_EXCH_A:
279 case SSL3_ST_CR_KEY_EXCH_B:
280 ret = ssl3_get_server_key_exchange(s);
281 if (ret <= 0) {
282 goto end;
283 }
284 s->state = SSL3_ST_CR_CERT_REQ_A;
285 s->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800286 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400287
Adam Langley71d8a082014-12-13 16:28:18 -0800288 case SSL3_ST_CR_CERT_REQ_A:
289 case SSL3_ST_CR_CERT_REQ_B:
290 ret = ssl3_get_certificate_request(s);
291 if (ret <= 0) {
292 goto end;
293 }
294 s->state = SSL3_ST_CR_SRVR_DONE_A;
295 s->init_num = 0;
296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298 case SSL3_ST_CR_SRVR_DONE_A:
299 case SSL3_ST_CR_SRVR_DONE_B:
300 ret = ssl3_get_server_done(s);
301 if (ret <= 0) {
302 goto end;
303 }
304 dtls1_stop_timer(s);
305 if (s->s3->tmp.cert_req) {
306 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
307 } else {
308 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
309 }
310 s->init_num = 0;
311 s->state = s->s3->tmp.next_state;
312 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langley71d8a082014-12-13 16:28:18 -0800314 case SSL3_ST_CW_CERT_A:
315 case SSL3_ST_CW_CERT_B:
316 case SSL3_ST_CW_CERT_C:
317 case SSL3_ST_CW_CERT_D:
318 dtls1_start_timer(s);
319 ret = ssl3_send_client_certificate(s);
320 if (ret <= 0) {
321 goto end;
322 }
323 s->state = SSL3_ST_CW_KEY_EXCH_A;
324 s->init_num = 0;
325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley71d8a082014-12-13 16:28:18 -0800327 case SSL3_ST_CW_KEY_EXCH_A:
328 case SSL3_ST_CW_KEY_EXCH_B:
329 dtls1_start_timer(s);
330 ret = ssl3_send_client_key_exchange(s);
331 if (ret <= 0) {
332 goto end;
333 }
334 /* For TLS, cert_req is set to 2, so a cert chain
335 * of nothing is sent, but no verify packet is sent */
336 if (s->s3->tmp.cert_req == 1) {
337 s->state = SSL3_ST_CW_CERT_VRFY_A;
338 } else {
339 s->state = SSL3_ST_CW_CHANGE_A;
340 s->s3->change_cipher_spec = 0;
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langley71d8a082014-12-13 16:28:18 -0800343 s->init_num = 0;
344 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langley71d8a082014-12-13 16:28:18 -0800346 case SSL3_ST_CW_CERT_VRFY_A:
347 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminc81ee8b2015-11-01 14:22:34 -0500348 case SSL3_ST_CW_CERT_VRFY_C:
Adam Langley71d8a082014-12-13 16:28:18 -0800349 dtls1_start_timer(s);
350 ret = ssl3_send_cert_verify(s);
351 if (ret <= 0) {
352 goto end;
353 }
354 s->state = SSL3_ST_CW_CHANGE_A;
355 s->init_num = 0;
356 s->s3->change_cipher_spec = 0;
357 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700358
Adam Langley71d8a082014-12-13 16:28:18 -0800359 case SSL3_ST_CW_CHANGE_A:
360 case SSL3_ST_CW_CHANGE_B:
361 if (!s->hit) {
362 dtls1_start_timer(s);
363 }
364 ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
365 SSL3_ST_CW_CHANGE_B);
366 if (ret <= 0) {
367 goto end;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langley71d8a082014-12-13 16:28:18 -0800370 s->state = SSL3_ST_CW_FINISHED_A;
371 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langley71d8a082014-12-13 16:28:18 -0800373 s->session->cipher = s->s3->tmp.new_cipher;
374 if (!s->enc_method->setup_key_block(s) ||
375 !s->enc_method->change_cipher_state(
376 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
377 ret = -1;
378 goto end;
379 }
Adam Langley71d8a082014-12-13 16:28:18 -0800380 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley71d8a082014-12-13 16:28:18 -0800382 case SSL3_ST_CW_FINISHED_A:
383 case SSL3_ST_CW_FINISHED_B:
384 if (!s->hit) {
385 dtls1_start_timer(s);
386 }
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langley71d8a082014-12-13 16:28:18 -0800388 ret =
389 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
390 s->enc_method->client_finished_label,
391 s->enc_method->client_finished_label_len);
392 if (ret <= 0) {
393 goto end;
394 }
395 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langley71d8a082014-12-13 16:28:18 -0800397 if (s->hit) {
398 s->s3->tmp.next_state = SSL_ST_OK;
399 } else {
400 /* Allow NewSessionTicket if ticket expected */
401 if (s->tlsext_ticket_expected) {
402 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
403 } else {
404 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
405 }
406 }
407 s->init_num = 0;
408 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langley71d8a082014-12-13 16:28:18 -0800410 case SSL3_ST_CR_SESSION_TICKET_A:
411 case SSL3_ST_CR_SESSION_TICKET_B:
412 ret = ssl3_get_new_session_ticket(s);
413 if (ret <= 0) {
414 goto end;
415 }
416 s->state = SSL3_ST_CR_FINISHED_A;
417 s->init_num = 0;
418 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langley71d8a082014-12-13 16:28:18 -0800420 case SSL3_ST_CR_CERT_STATUS_A:
421 case SSL3_ST_CR_CERT_STATUS_B:
422 ret = ssl3_get_cert_status(s);
423 if (ret <= 0) {
424 goto end;
425 }
Paul Lietar8f1c2682015-08-18 12:21:54 +0100426 s->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley71d8a082014-12-13 16:28:18 -0800427 s->init_num = 0;
428 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700429
Adam Langley71d8a082014-12-13 16:28:18 -0800430 case SSL3_ST_CR_FINISHED_A:
431 case SSL3_ST_CR_FINISHED_B:
432 s->d1->change_cipher_spec_ok = 1;
433 ret =
434 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
435 if (ret <= 0) {
436 goto end;
437 }
438 dtls1_stop_timer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langley71d8a082014-12-13 16:28:18 -0800440 if (s->hit) {
441 s->state = SSL3_ST_CW_CHANGE_A;
442 } else {
443 s->state = SSL_ST_OK;
444 }
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langley71d8a082014-12-13 16:28:18 -0800446 s->init_num = 0;
447 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700448
Adam Langley71d8a082014-12-13 16:28:18 -0800449 case SSL3_ST_CW_FLUSH:
450 s->rwstate = SSL_WRITING;
451 if (BIO_flush(s->wbio) <= 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800452 ret = -1;
453 goto end;
454 }
455 s->rwstate = SSL_NOTHING;
456 s->state = s->s3->tmp.next_state;
457 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langley71d8a082014-12-13 16:28:18 -0800459 case SSL_ST_OK:
460 /* clean a few things up */
461 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langley71d8a082014-12-13 16:28:18 -0800463 /* Remove write buffering now. */
464 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langley71d8a082014-12-13 16:28:18 -0800466 s->init_num = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400467 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley71d8a082014-12-13 16:28:18 -0800469 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langley71d8a082014-12-13 16:28:18 -0800471 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700472
David Benjamin6eb000d2015-02-11 01:17:41 -0500473 if (cb != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800474 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langley71d8a082014-12-13 16:28:18 -0800477 /* done with handshaking */
478 s->d1->handshake_read_seq = 0;
479 s->d1->next_handshake_write_seq = 0;
480 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langley71d8a082014-12-13 16:28:18 -0800482 default:
David Benjamin3570d732015-06-29 00:28:17 -0400483 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley71d8a082014-12-13 16:28:18 -0800484 ret = -1;
485 goto end;
486 }
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langley71d8a082014-12-13 16:28:18 -0800488 /* did we do anything? */
489 if (!s->s3->tmp.reuse_message && !skip) {
490 if ((cb != NULL) && (s->state != state)) {
491 new_state = s->state;
492 s->state = state;
493 cb(s, SSL_CB_CONNECT_LOOP, 1);
494 s->state = new_state;
495 }
496 }
497 skip = 0;
498 }
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langley95c29f32014-06-20 12:00:00 -0700500end:
Adam Langley71d8a082014-12-13 16:28:18 -0800501 s->in_handshake--;
Adam Langley95c29f32014-06-20 12:00:00 -0700502
David Benjamin2755a3e2015-04-22 16:17:58 -0400503 BUF_MEM_free(buf);
Adam Langley71d8a082014-12-13 16:28:18 -0800504 if (cb != NULL) {
505 cb(s, SSL_CB_CONNECT_EXIT, ret);
506 }
507 return ret;
508}
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langley71d8a082014-12-13 16:28:18 -0800510static int dtls1_get_hello_verify(SSL *s) {
511 long n;
512 int al, ok = 0;
513 CBS hello_verify_request, cookie;
514 uint16_t server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700515
Adam Langley71d8a082014-12-13 16:28:18 -0800516 n = s->method->ssl_get_message(
517 s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
518 -1,
519 /* Use the same maximum size as ssl3_get_server_hello. */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500520 20000, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langley71d8a082014-12-13 16:28:18 -0800522 if (!ok) {
523 return n;
524 }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley71d8a082014-12-13 16:28:18 -0800526 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
527 s->d1->send_cookie = 0;
528 s->s3->tmp.reuse_message = 1;
529 return 1;
530 }
David Benjamin51e32832014-08-13 15:13:57 -0400531
Adam Langley71d8a082014-12-13 16:28:18 -0800532 CBS_init(&hello_verify_request, s->init_msg, n);
David Benjamin51e32832014-08-13 15:13:57 -0400533
Adam Langley71d8a082014-12-13 16:28:18 -0800534 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
535 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
536 CBS_len(&hello_verify_request) != 0) {
537 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400538 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800539 goto f_err;
540 }
Adam Langley95c29f32014-06-20 12:00:00 -0700541
Adam Langley71d8a082014-12-13 16:28:18 -0800542 if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
543 al = SSL_AD_ILLEGAL_PARAMETER;
544 goto f_err;
545 }
Adam Langley95c29f32014-06-20 12:00:00 -0700546
Adam Langley71d8a082014-12-13 16:28:18 -0800547 memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
548 s->d1->cookie_len = CBS_len(&cookie);
549
550 s->d1->send_cookie = 1;
551 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700552
553f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800554 ssl3_send_alert(s, SSL3_AL_FATAL, al);
555 return -1;
556}