blob: eeb85946d52bb9ea00230759ad1de7898d4a223a [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 Benjamin0b145c22014-11-26 20:10:09 -0500115#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <stdio.h>
117
118#include <openssl/bn.h>
119#include <openssl/buf.h>
120#include <openssl/dh.h>
121#include <openssl/evp.h>
122#include <openssl/md5.h>
123#include <openssl/mem.h>
124#include <openssl/obj.h>
125#include <openssl/rand.h>
126
David Benjamin2ee94aa2015-04-07 22:38:30 -0400127#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley95c29f32014-06-20 12:00:00 -0700129static int dtls1_get_hello_verify(SSL *s);
130
Adam Langley71d8a082014-12-13 16:28:18 -0800131int dtls1_connect(SSL *s) {
132 BUF_MEM *buf = NULL;
133 void (*cb)(const SSL *ssl, int type, int val) = NULL;
134 int ret = -1;
135 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langley71d8a082014-12-13 16:28:18 -0800137 assert(s->handshake_func == dtls1_connect);
138 assert(!s->server);
139 assert(SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500140
Adam Langley71d8a082014-12-13 16:28:18 -0800141 ERR_clear_error();
142 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700143
Adam Langley71d8a082014-12-13 16:28:18 -0800144 if (s->info_callback != NULL) {
145 cb = s->info_callback;
146 } else if (s->ctx->info_callback != NULL) {
147 cb = s->ctx->info_callback;
148 }
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley71d8a082014-12-13 16:28:18 -0800150 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langley71d8a082014-12-13 16:28:18 -0800152 for (;;) {
153 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley71d8a082014-12-13 16:28:18 -0800155 switch (s->state) {
156 case SSL_ST_RENEGOTIATE:
157 s->renegotiate = 1;
158 s->state = SSL_ST_CONNECT;
Adam Langley71d8a082014-12-13 16:28:18 -0800159 /* break */
160 case SSL_ST_CONNECT:
161 case SSL_ST_BEFORE | SSL_ST_CONNECT:
162 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
Adam Langley71d8a082014-12-13 16:28:18 -0800177 if (!ssl3_setup_buffers(s) ||
178 !ssl_init_wbio_buffer(s, 0)) {
179 ret = -1;
180 goto end;
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langley71d8a082014-12-13 16:28:18 -0800183 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley71d8a082014-12-13 16:28:18 -0800185 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800186 s->init_num = 0;
187 s->d1->send_cookie = 0;
188 s->hit = 0;
189 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley71d8a082014-12-13 16:28:18 -0800191 case SSL3_ST_CW_CLNT_HELLO_A:
192 case SSL3_ST_CW_CLNT_HELLO_B:
193 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langley71d8a082014-12-13 16:28:18 -0800195 /* every DTLS ClientHello resets Finished MAC */
196 if (!ssl3_init_finished_mac(s)) {
197 OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
198 ret = -1;
199 goto end;
200 }
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley71d8a082014-12-13 16:28:18 -0800202 dtls1_start_timer(s);
203 ret = ssl3_send_client_hello(s);
204 if (ret <= 0) {
205 goto end;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langley71d8a082014-12-13 16:28:18 -0800208 if (s->d1->send_cookie) {
209 s->state = SSL3_ST_CW_FLUSH;
210 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
211 } else {
212 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
213 }
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley71d8a082014-12-13 16:28:18 -0800215 s->init_num = 0;
216 /* turn on buffering for the next lot of output */
217 if (s->bbio != s->wbio) {
218 s->wbio = BIO_push(s->bbio, s->wbio);
219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langley71d8a082014-12-13 16:28:18 -0800221 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700222
Adam Langley71d8a082014-12-13 16:28:18 -0800223 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
224 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
225 ret = dtls1_get_hello_verify(s);
226 if (ret <= 0) {
227 goto end;
228 }
229 if (s->d1->send_cookie) {
230 /* start again, with a cookie */
231 dtls1_stop_timer(s);
232 s->state = SSL3_ST_CW_CLNT_HELLO_A;
233 } else {
234 s->state = SSL3_ST_CR_SRVR_HELLO_A;
235 }
236 s->init_num = 0;
237 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langley71d8a082014-12-13 16:28:18 -0800239 case SSL3_ST_CR_SRVR_HELLO_A:
240 case SSL3_ST_CR_SRVR_HELLO_B:
241 ret = ssl3_get_server_hello(s);
242 if (ret <= 0) {
243 goto end;
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langley71d8a082014-12-13 16:28:18 -0800246 if (s->hit) {
247 s->state = SSL3_ST_CR_FINISHED_A;
248 if (s->tlsext_ticket_expected) {
249 /* receive renewed session ticket */
250 s->state = SSL3_ST_CR_SESSION_TICKET_A;
251 }
252 } else {
253 s->state = SSL3_ST_CR_CERT_A;
254 }
255 s->init_num = 0;
256 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700257
Adam Langley71d8a082014-12-13 16:28:18 -0800258 case SSL3_ST_CR_CERT_A:
259 case SSL3_ST_CR_CERT_B:
260 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
261 ret = ssl3_get_server_certificate(s);
262 if (ret <= 0) {
263 goto end;
264 }
265 if (s->s3->tmp.certificate_status_expected) {
266 s->state = SSL3_ST_CR_CERT_STATUS_A;
267 } else {
268 s->state = SSL3_ST_CR_KEY_EXCH_A;
269 }
270 } else {
271 skip = 1;
272 s->state = SSL3_ST_CR_KEY_EXCH_A;
273 }
274 s->init_num = 0;
275 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Adam Langley71d8a082014-12-13 16:28:18 -0800277 case SSL3_ST_CR_KEY_EXCH_A:
278 case SSL3_ST_CR_KEY_EXCH_B:
279 ret = ssl3_get_server_key_exchange(s);
280 if (ret <= 0) {
281 goto end;
282 }
283 s->state = SSL3_ST_CR_CERT_REQ_A;
284 s->init_num = 0;
David Benjaminf2fedef2014-08-16 01:37:34 -0400285
Adam Langley71d8a082014-12-13 16:28:18 -0800286 /* at this point we check that we have the
287 * required stuff from the server */
288 if (!ssl3_check_cert_and_algorithm(s)) {
289 ret = -1;
290 goto end;
291 }
292 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400293
Adam Langley71d8a082014-12-13 16:28:18 -0800294 case SSL3_ST_CR_CERT_REQ_A:
295 case SSL3_ST_CR_CERT_REQ_B:
296 ret = ssl3_get_certificate_request(s);
297 if (ret <= 0) {
298 goto end;
299 }
300 s->state = SSL3_ST_CR_SRVR_DONE_A;
301 s->init_num = 0;
302 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langley71d8a082014-12-13 16:28:18 -0800304 case SSL3_ST_CR_SRVR_DONE_A:
305 case SSL3_ST_CR_SRVR_DONE_B:
306 ret = ssl3_get_server_done(s);
307 if (ret <= 0) {
308 goto end;
309 }
310 dtls1_stop_timer(s);
311 if (s->s3->tmp.cert_req) {
312 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
313 } else {
314 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
315 }
316 s->init_num = 0;
317 s->state = s->s3->tmp.next_state;
318 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langley71d8a082014-12-13 16:28:18 -0800320 case SSL3_ST_CW_CERT_A:
321 case SSL3_ST_CW_CERT_B:
322 case SSL3_ST_CW_CERT_C:
323 case SSL3_ST_CW_CERT_D:
324 dtls1_start_timer(s);
325 ret = ssl3_send_client_certificate(s);
326 if (ret <= 0) {
327 goto end;
328 }
329 s->state = SSL3_ST_CW_KEY_EXCH_A;
330 s->init_num = 0;
331 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley71d8a082014-12-13 16:28:18 -0800333 case SSL3_ST_CW_KEY_EXCH_A:
334 case SSL3_ST_CW_KEY_EXCH_B:
335 dtls1_start_timer(s);
336 ret = ssl3_send_client_key_exchange(s);
337 if (ret <= 0) {
338 goto end;
339 }
340 /* For TLS, cert_req is set to 2, so a cert chain
341 * of nothing is sent, but no verify packet is sent */
342 if (s->s3->tmp.cert_req == 1) {
343 s->state = SSL3_ST_CW_CERT_VRFY_A;
344 } else {
345 s->state = SSL3_ST_CW_CHANGE_A;
346 s->s3->change_cipher_spec = 0;
347 }
Adam Langley95c29f32014-06-20 12:00:00 -0700348
Adam Langley71d8a082014-12-13 16:28:18 -0800349 s->init_num = 0;
350 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langley71d8a082014-12-13 16:28:18 -0800352 case SSL3_ST_CW_CERT_VRFY_A:
353 case SSL3_ST_CW_CERT_VRFY_B:
354 dtls1_start_timer(s);
355 ret = ssl3_send_cert_verify(s);
356 if (ret <= 0) {
357 goto end;
358 }
359 s->state = SSL3_ST_CW_CHANGE_A;
360 s->init_num = 0;
361 s->s3->change_cipher_spec = 0;
362 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley71d8a082014-12-13 16:28:18 -0800364 case SSL3_ST_CW_CHANGE_A:
365 case SSL3_ST_CW_CHANGE_B:
366 if (!s->hit) {
367 dtls1_start_timer(s);
368 }
369 ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
370 SSL3_ST_CW_CHANGE_B);
371 if (ret <= 0) {
372 goto end;
373 }
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langley71d8a082014-12-13 16:28:18 -0800375 s->state = SSL3_ST_CW_FINISHED_A;
376 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley71d8a082014-12-13 16:28:18 -0800378 s->session->cipher = s->s3->tmp.new_cipher;
379 if (!s->enc_method->setup_key_block(s) ||
380 !s->enc_method->change_cipher_state(
381 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
382 ret = -1;
383 goto end;
384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley71d8a082014-12-13 16:28:18 -0800386 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
387 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley71d8a082014-12-13 16:28:18 -0800389 case SSL3_ST_CW_FINISHED_A:
390 case SSL3_ST_CW_FINISHED_B:
391 if (!s->hit) {
392 dtls1_start_timer(s);
393 }
Adam Langley95c29f32014-06-20 12:00:00 -0700394
Adam Langley71d8a082014-12-13 16:28:18 -0800395 ret =
396 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
397 s->enc_method->client_finished_label,
398 s->enc_method->client_finished_label_len);
399 if (ret <= 0) {
400 goto end;
401 }
402 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley71d8a082014-12-13 16:28:18 -0800404 if (s->hit) {
405 s->s3->tmp.next_state = SSL_ST_OK;
406 } else {
407 /* Allow NewSessionTicket if ticket expected */
408 if (s->tlsext_ticket_expected) {
409 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
410 } else {
411 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
412 }
413 }
414 s->init_num = 0;
415 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langley71d8a082014-12-13 16:28:18 -0800417 case SSL3_ST_CR_SESSION_TICKET_A:
418 case SSL3_ST_CR_SESSION_TICKET_B:
419 ret = ssl3_get_new_session_ticket(s);
420 if (ret <= 0) {
421 goto end;
422 }
423 s->state = SSL3_ST_CR_FINISHED_A;
424 s->init_num = 0;
425 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700426
Adam Langley71d8a082014-12-13 16:28:18 -0800427 case SSL3_ST_CR_CERT_STATUS_A:
428 case SSL3_ST_CR_CERT_STATUS_B:
429 ret = ssl3_get_cert_status(s);
430 if (ret <= 0) {
431 goto end;
432 }
433 s->state = SSL3_ST_CR_KEY_EXCH_A;
434 s->init_num = 0;
435 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langley71d8a082014-12-13 16:28:18 -0800437 case SSL3_ST_CR_FINISHED_A:
438 case SSL3_ST_CR_FINISHED_B:
439 s->d1->change_cipher_spec_ok = 1;
440 ret =
441 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
442 if (ret <= 0) {
443 goto end;
444 }
445 dtls1_stop_timer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700446
Adam Langley71d8a082014-12-13 16:28:18 -0800447 if (s->hit) {
448 s->state = SSL3_ST_CW_CHANGE_A;
449 } else {
450 s->state = SSL_ST_OK;
451 }
Adam Langley95c29f32014-06-20 12:00:00 -0700452
Adam Langley71d8a082014-12-13 16:28:18 -0800453 s->init_num = 0;
454 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley71d8a082014-12-13 16:28:18 -0800456 case SSL3_ST_CW_FLUSH:
457 s->rwstate = SSL_WRITING;
458 if (BIO_flush(s->wbio) <= 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800459 ret = -1;
460 goto end;
461 }
462 s->rwstate = SSL_NOTHING;
463 s->state = s->s3->tmp.next_state;
464 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langley71d8a082014-12-13 16:28:18 -0800466 case SSL_ST_OK:
467 /* clean a few things up */
468 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langley71d8a082014-12-13 16:28:18 -0800470 /* Remove write buffering now. */
471 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langley71d8a082014-12-13 16:28:18 -0800473 s->init_num = 0;
474 s->renegotiate = 0;
475 s->new_session = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langley71d8a082014-12-13 16:28:18 -0800477 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langley71d8a082014-12-13 16:28:18 -0800479 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700480
David Benjamin6eb000d2015-02-11 01:17:41 -0500481 if (cb != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800482 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500483 }
Adam Langley95c29f32014-06-20 12:00:00 -0700484
Adam Langley71d8a082014-12-13 16:28:18 -0800485 /* done with handshaking */
486 s->d1->handshake_read_seq = 0;
487 s->d1->next_handshake_write_seq = 0;
488 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langley71d8a082014-12-13 16:28:18 -0800490 default:
491 OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
492 ret = -1;
493 goto end;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langley71d8a082014-12-13 16:28:18 -0800496 /* did we do anything? */
497 if (!s->s3->tmp.reuse_message && !skip) {
498 if ((cb != NULL) && (s->state != state)) {
499 new_state = s->state;
500 s->state = state;
501 cb(s, SSL_CB_CONNECT_LOOP, 1);
502 s->state = new_state;
503 }
504 }
505 skip = 0;
506 }
Adam Langley95c29f32014-06-20 12:00:00 -0700507
Adam Langley95c29f32014-06-20 12:00:00 -0700508end:
Adam Langley71d8a082014-12-13 16:28:18 -0800509 s->in_handshake--;
Adam Langley95c29f32014-06-20 12:00:00 -0700510
Adam Langley71d8a082014-12-13 16:28:18 -0800511 if (buf != NULL) {
512 BUF_MEM_free(buf);
513 }
514 if (cb != NULL) {
515 cb(s, SSL_CB_CONNECT_EXIT, ret);
516 }
517 return ret;
518}
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langley71d8a082014-12-13 16:28:18 -0800520static int dtls1_get_hello_verify(SSL *s) {
521 long n;
522 int al, ok = 0;
523 CBS hello_verify_request, cookie;
524 uint16_t server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley71d8a082014-12-13 16:28:18 -0800526 n = s->method->ssl_get_message(
527 s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
528 -1,
529 /* Use the same maximum size as ssl3_get_server_hello. */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500530 20000, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700531
Adam Langley71d8a082014-12-13 16:28:18 -0800532 if (!ok) {
533 return n;
534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley71d8a082014-12-13 16:28:18 -0800536 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
537 s->d1->send_cookie = 0;
538 s->s3->tmp.reuse_message = 1;
539 return 1;
540 }
David Benjamin51e32832014-08-13 15:13:57 -0400541
Adam Langley71d8a082014-12-13 16:28:18 -0800542 CBS_init(&hello_verify_request, s->init_msg, n);
David Benjamin51e32832014-08-13 15:13:57 -0400543
Adam Langley71d8a082014-12-13 16:28:18 -0800544 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
545 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
546 CBS_len(&hello_verify_request) != 0) {
547 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700548 OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800549 goto f_err;
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
Adam Langley71d8a082014-12-13 16:28:18 -0800552 if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
553 al = SSL_AD_ILLEGAL_PARAMETER;
554 goto f_err;
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langley71d8a082014-12-13 16:28:18 -0800557 memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
558 s->d1->cookie_len = CBS_len(&cookie);
559
560 s->d1->send_cookie = 1;
561 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700562
563f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800564 ssl3_send_alert(s, SSL3_AL_FATAL, al);
565 return -1;
566}