blob: 0d592d09d97aab94f862d90851aa66e9b8a2ac06 [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400117#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118
119#include <openssl/bn.h>
120#include <openssl/buf.h>
121#include <openssl/dh.h>
122#include <openssl/evp.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400123#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/md5.h>
125#include <openssl/mem.h>
126#include <openssl/obj.h>
127#include <openssl/rand.h>
128
David Benjamin2ee94aa2015-04-07 22:38:30 -0400129#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley95c29f32014-06-20 12:00:00 -0700131static int dtls1_get_hello_verify(SSL *s);
132
Adam Langley71d8a082014-12-13 16:28:18 -0800133int dtls1_connect(SSL *s) {
134 BUF_MEM *buf = NULL;
135 void (*cb)(const SSL *ssl, int type, int val) = NULL;
136 int ret = -1;
137 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700138
Adam Langley71d8a082014-12-13 16:28:18 -0800139 assert(s->handshake_func == dtls1_connect);
140 assert(!s->server);
141 assert(SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500142
Adam Langley71d8a082014-12-13 16:28:18 -0800143 ERR_clear_error();
144 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700145
Adam Langley71d8a082014-12-13 16:28:18 -0800146 if (s->info_callback != NULL) {
147 cb = s->info_callback;
148 } else if (s->ctx->info_callback != NULL) {
149 cb = s->ctx->info_callback;
150 }
Adam Langley95c29f32014-06-20 12:00:00 -0700151
Adam Langley71d8a082014-12-13 16:28:18 -0800152 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langley71d8a082014-12-13 16:28:18 -0800154 for (;;) {
155 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700156
Adam Langley71d8a082014-12-13 16:28:18 -0800157 switch (s->state) {
158 case SSL_ST_RENEGOTIATE:
159 s->renegotiate = 1;
160 s->state = SSL_ST_CONNECT;
Adam Langley71d8a082014-12-13 16:28:18 -0800161 /* break */
162 case SSL_ST_CONNECT:
163 case SSL_ST_BEFORE | SSL_ST_CONNECT:
164 if (cb != NULL) {
165 cb(s, SSL_CB_HANDSHAKE_START, 1);
166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
Adam Langley71d8a082014-12-13 16:28:18 -0800168 if (s->init_buf == NULL) {
169 buf = BUF_MEM_new();
170 if (buf == NULL ||
171 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
172 ret = -1;
173 goto end;
174 }
175 s->init_buf = buf;
176 buf = NULL;
177 }
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamin6a08da22015-05-08 22:58:12 -0400179 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800180 ret = -1;
181 goto end;
182 }
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley71d8a082014-12-13 16:28:18 -0800184 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700185
Adam Langley71d8a082014-12-13 16:28:18 -0800186 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800187 s->init_num = 0;
188 s->d1->send_cookie = 0;
189 s->hit = 0;
190 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700191
Adam Langley71d8a082014-12-13 16:28:18 -0800192 case SSL3_ST_CW_CLNT_HELLO_A:
193 case SSL3_ST_CW_CLNT_HELLO_B:
194 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley71d8a082014-12-13 16:28:18 -0800196 /* every DTLS ClientHello resets Finished MAC */
197 if (!ssl3_init_finished_mac(s)) {
198 OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
199 ret = -1;
200 goto end;
201 }
Adam Langley95c29f32014-06-20 12:00:00 -0700202
Adam Langley71d8a082014-12-13 16:28:18 -0800203 dtls1_start_timer(s);
204 ret = ssl3_send_client_hello(s);
205 if (ret <= 0) {
206 goto end;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langley71d8a082014-12-13 16:28:18 -0800209 if (s->d1->send_cookie) {
210 s->state = SSL3_ST_CW_FLUSH;
211 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
212 } else {
213 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
214 }
Adam Langley95c29f32014-06-20 12:00:00 -0700215
Adam Langley71d8a082014-12-13 16:28:18 -0800216 s->init_num = 0;
217 /* turn on buffering for the next lot of output */
218 if (s->bbio != s->wbio) {
219 s->wbio = BIO_push(s->bbio, s->wbio);
220 }
Adam Langley95c29f32014-06-20 12:00:00 -0700221
Adam Langley71d8a082014-12-13 16:28:18 -0800222 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley71d8a082014-12-13 16:28:18 -0800224 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
225 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
226 ret = dtls1_get_hello_verify(s);
227 if (ret <= 0) {
228 goto end;
229 }
230 if (s->d1->send_cookie) {
231 /* start again, with a cookie */
232 dtls1_stop_timer(s);
233 s->state = SSL3_ST_CW_CLNT_HELLO_A;
234 } else {
235 s->state = SSL3_ST_CR_SRVR_HELLO_A;
236 }
237 s->init_num = 0;
238 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
Adam Langley71d8a082014-12-13 16:28:18 -0800240 case SSL3_ST_CR_SRVR_HELLO_A:
241 case SSL3_ST_CR_SRVR_HELLO_B:
242 ret = ssl3_get_server_hello(s);
243 if (ret <= 0) {
244 goto end;
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langley71d8a082014-12-13 16:28:18 -0800247 if (s->hit) {
248 s->state = SSL3_ST_CR_FINISHED_A;
249 if (s->tlsext_ticket_expected) {
250 /* receive renewed session ticket */
251 s->state = SSL3_ST_CR_SESSION_TICKET_A;
252 }
253 } else {
254 s->state = SSL3_ST_CR_CERT_A;
255 }
256 s->init_num = 0;
257 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley71d8a082014-12-13 16:28:18 -0800259 case SSL3_ST_CR_CERT_A:
260 case SSL3_ST_CR_CERT_B:
261 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
262 ret = ssl3_get_server_certificate(s);
263 if (ret <= 0) {
264 goto end;
265 }
266 if (s->s3->tmp.certificate_status_expected) {
267 s->state = SSL3_ST_CR_CERT_STATUS_A;
268 } else {
269 s->state = SSL3_ST_CR_KEY_EXCH_A;
270 }
271 } else {
272 skip = 1;
273 s->state = SSL3_ST_CR_KEY_EXCH_A;
274 }
275 s->init_num = 0;
276 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
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;
David Benjaminf2fedef2014-08-16 01:37:34 -0400286
Adam Langley71d8a082014-12-13 16:28:18 -0800287 /* at this point we check that we have the
288 * required stuff from the server */
289 if (!ssl3_check_cert_and_algorithm(s)) {
290 ret = -1;
291 goto end;
292 }
293 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400294
Adam Langley71d8a082014-12-13 16:28:18 -0800295 case SSL3_ST_CR_CERT_REQ_A:
296 case SSL3_ST_CR_CERT_REQ_B:
297 ret = ssl3_get_certificate_request(s);
298 if (ret <= 0) {
299 goto end;
300 }
301 s->state = SSL3_ST_CR_SRVR_DONE_A;
302 s->init_num = 0;
303 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langley71d8a082014-12-13 16:28:18 -0800305 case SSL3_ST_CR_SRVR_DONE_A:
306 case SSL3_ST_CR_SRVR_DONE_B:
307 ret = ssl3_get_server_done(s);
308 if (ret <= 0) {
309 goto end;
310 }
311 dtls1_stop_timer(s);
312 if (s->s3->tmp.cert_req) {
313 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
314 } else {
315 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
316 }
317 s->init_num = 0;
318 s->state = s->s3->tmp.next_state;
319 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langley71d8a082014-12-13 16:28:18 -0800321 case SSL3_ST_CW_CERT_A:
322 case SSL3_ST_CW_CERT_B:
323 case SSL3_ST_CW_CERT_C:
324 case SSL3_ST_CW_CERT_D:
325 dtls1_start_timer(s);
326 ret = ssl3_send_client_certificate(s);
327 if (ret <= 0) {
328 goto end;
329 }
330 s->state = SSL3_ST_CW_KEY_EXCH_A;
331 s->init_num = 0;
332 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700333
Adam Langley71d8a082014-12-13 16:28:18 -0800334 case SSL3_ST_CW_KEY_EXCH_A:
335 case SSL3_ST_CW_KEY_EXCH_B:
336 dtls1_start_timer(s);
337 ret = ssl3_send_client_key_exchange(s);
338 if (ret <= 0) {
339 goto end;
340 }
341 /* For TLS, cert_req is set to 2, so a cert chain
342 * of nothing is sent, but no verify packet is sent */
343 if (s->s3->tmp.cert_req == 1) {
344 s->state = SSL3_ST_CW_CERT_VRFY_A;
345 } else {
346 s->state = SSL3_ST_CW_CHANGE_A;
347 s->s3->change_cipher_spec = 0;
348 }
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langley71d8a082014-12-13 16:28:18 -0800350 s->init_num = 0;
351 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley71d8a082014-12-13 16:28:18 -0800353 case SSL3_ST_CW_CERT_VRFY_A:
354 case SSL3_ST_CW_CERT_VRFY_B:
355 dtls1_start_timer(s);
356 ret = ssl3_send_cert_verify(s);
357 if (ret <= 0) {
358 goto end;
359 }
360 s->state = SSL3_ST_CW_CHANGE_A;
361 s->init_num = 0;
362 s->s3->change_cipher_spec = 0;
363 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langley71d8a082014-12-13 16:28:18 -0800365 case SSL3_ST_CW_CHANGE_A:
366 case SSL3_ST_CW_CHANGE_B:
367 if (!s->hit) {
368 dtls1_start_timer(s);
369 }
370 ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
371 SSL3_ST_CW_CHANGE_B);
372 if (ret <= 0) {
373 goto end;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langley71d8a082014-12-13 16:28:18 -0800376 s->state = SSL3_ST_CW_FINISHED_A;
377 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langley71d8a082014-12-13 16:28:18 -0800379 s->session->cipher = s->s3->tmp.new_cipher;
380 if (!s->enc_method->setup_key_block(s) ||
381 !s->enc_method->change_cipher_state(
382 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
383 ret = -1;
384 goto end;
385 }
Adam Langley95c29f32014-06-20 12:00:00 -0700386
Adam Langley71d8a082014-12-13 16:28:18 -0800387 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
388 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700389
Adam Langley71d8a082014-12-13 16:28:18 -0800390 case SSL3_ST_CW_FINISHED_A:
391 case SSL3_ST_CW_FINISHED_B:
392 if (!s->hit) {
393 dtls1_start_timer(s);
394 }
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langley71d8a082014-12-13 16:28:18 -0800396 ret =
397 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
398 s->enc_method->client_finished_label,
399 s->enc_method->client_finished_label_len);
400 if (ret <= 0) {
401 goto end;
402 }
403 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700404
Adam Langley71d8a082014-12-13 16:28:18 -0800405 if (s->hit) {
406 s->s3->tmp.next_state = SSL_ST_OK;
407 } else {
408 /* Allow NewSessionTicket if ticket expected */
409 if (s->tlsext_ticket_expected) {
410 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
411 } else {
412 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
413 }
414 }
415 s->init_num = 0;
416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley71d8a082014-12-13 16:28:18 -0800418 case SSL3_ST_CR_SESSION_TICKET_A:
419 case SSL3_ST_CR_SESSION_TICKET_B:
420 ret = ssl3_get_new_session_ticket(s);
421 if (ret <= 0) {
422 goto end;
423 }
424 s->state = SSL3_ST_CR_FINISHED_A;
425 s->init_num = 0;
426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langley71d8a082014-12-13 16:28:18 -0800428 case SSL3_ST_CR_CERT_STATUS_A:
429 case SSL3_ST_CR_CERT_STATUS_B:
430 ret = ssl3_get_cert_status(s);
431 if (ret <= 0) {
432 goto end;
433 }
434 s->state = SSL3_ST_CR_KEY_EXCH_A;
435 s->init_num = 0;
436 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700437
Adam Langley71d8a082014-12-13 16:28:18 -0800438 case SSL3_ST_CR_FINISHED_A:
439 case SSL3_ST_CR_FINISHED_B:
440 s->d1->change_cipher_spec_ok = 1;
441 ret =
442 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
443 if (ret <= 0) {
444 goto end;
445 }
446 dtls1_stop_timer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langley71d8a082014-12-13 16:28:18 -0800448 if (s->hit) {
449 s->state = SSL3_ST_CW_CHANGE_A;
450 } else {
451 s->state = SSL_ST_OK;
452 }
Adam Langley95c29f32014-06-20 12:00:00 -0700453
Adam Langley71d8a082014-12-13 16:28:18 -0800454 s->init_num = 0;
455 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langley71d8a082014-12-13 16:28:18 -0800457 case SSL3_ST_CW_FLUSH:
458 s->rwstate = SSL_WRITING;
459 if (BIO_flush(s->wbio) <= 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800460 ret = -1;
461 goto end;
462 }
463 s->rwstate = SSL_NOTHING;
464 s->state = s->s3->tmp.next_state;
465 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langley71d8a082014-12-13 16:28:18 -0800467 case SSL_ST_OK:
468 /* clean a few things up */
469 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700470
Adam Langley71d8a082014-12-13 16:28:18 -0800471 /* Remove write buffering now. */
472 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langley71d8a082014-12-13 16:28:18 -0800474 s->init_num = 0;
475 s->renegotiate = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400476 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langley71d8a082014-12-13 16:28:18 -0800478 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700479
Adam Langley71d8a082014-12-13 16:28:18 -0800480 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700481
David Benjamin6eb000d2015-02-11 01:17:41 -0500482 if (cb != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800483 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500484 }
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langley71d8a082014-12-13 16:28:18 -0800486 /* done with handshaking */
487 s->d1->handshake_read_seq = 0;
488 s->d1->next_handshake_write_seq = 0;
489 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700490
Adam Langley71d8a082014-12-13 16:28:18 -0800491 default:
492 OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
493 ret = -1;
494 goto end;
495 }
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langley71d8a082014-12-13 16:28:18 -0800497 /* did we do anything? */
498 if (!s->s3->tmp.reuse_message && !skip) {
499 if ((cb != NULL) && (s->state != state)) {
500 new_state = s->state;
501 s->state = state;
502 cb(s, SSL_CB_CONNECT_LOOP, 1);
503 s->state = new_state;
504 }
505 }
506 skip = 0;
507 }
Adam Langley95c29f32014-06-20 12:00:00 -0700508
Adam Langley95c29f32014-06-20 12:00:00 -0700509end:
Adam Langley71d8a082014-12-13 16:28:18 -0800510 s->in_handshake--;
Adam Langley95c29f32014-06-20 12:00:00 -0700511
David Benjamin2755a3e2015-04-22 16:17:58 -0400512 BUF_MEM_free(buf);
Adam Langley71d8a082014-12-13 16:28:18 -0800513 if (cb != NULL) {
514 cb(s, SSL_CB_CONNECT_EXIT, ret);
515 }
516 return ret;
517}
Adam Langley95c29f32014-06-20 12:00:00 -0700518
Adam Langley71d8a082014-12-13 16:28:18 -0800519static int dtls1_get_hello_verify(SSL *s) {
520 long n;
521 int al, ok = 0;
522 CBS hello_verify_request, cookie;
523 uint16_t server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700524
Adam Langley71d8a082014-12-13 16:28:18 -0800525 n = s->method->ssl_get_message(
526 s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
527 -1,
528 /* Use the same maximum size as ssl3_get_server_hello. */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500529 20000, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langley71d8a082014-12-13 16:28:18 -0800531 if (!ok) {
532 return n;
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langley71d8a082014-12-13 16:28:18 -0800535 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
536 s->d1->send_cookie = 0;
537 s->s3->tmp.reuse_message = 1;
538 return 1;
539 }
David Benjamin51e32832014-08-13 15:13:57 -0400540
Adam Langley71d8a082014-12-13 16:28:18 -0800541 CBS_init(&hello_verify_request, s->init_msg, n);
David Benjamin51e32832014-08-13 15:13:57 -0400542
Adam Langley71d8a082014-12-13 16:28:18 -0800543 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
544 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
545 CBS_len(&hello_verify_request) != 0) {
546 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700547 OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800548 goto f_err;
549 }
Adam Langley95c29f32014-06-20 12:00:00 -0700550
Adam Langley71d8a082014-12-13 16:28:18 -0800551 if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
552 al = SSL_AD_ILLEGAL_PARAMETER;
553 goto f_err;
554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
Adam Langley71d8a082014-12-13 16:28:18 -0800556 memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
557 s->d1->cookie_len = CBS_len(&cookie);
558
559 s->d1->send_cookie = 1;
560 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700561
562f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800563 ssl3_send_alert(s, SSL3_AL_FATAL, al);
564 return -1;
565}