blob: 7ccb68eb7d0e01709104bca7b546f28b1bc9f39c [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) {
Adam Langley71d8a082014-12-13 16:28:18 -0800158 case SSL_ST_CONNECT:
Adam Langley71d8a082014-12-13 16:28:18 -0800159 if (cb != NULL) {
160 cb(s, SSL_CB_HANDSHAKE_START, 1);
161 }
Adam Langley95c29f32014-06-20 12:00:00 -0700162
Adam Langley71d8a082014-12-13 16:28:18 -0800163 if (s->init_buf == NULL) {
164 buf = BUF_MEM_new();
165 if (buf == NULL ||
166 !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
167 ret = -1;
168 goto end;
169 }
170 s->init_buf = buf;
171 buf = NULL;
172 }
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin6a08da22015-05-08 22:58:12 -0400174 if (!ssl_init_wbio_buffer(s, 0)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800175 ret = -1;
176 goto end;
177 }
Adam Langley95c29f32014-06-20 12:00:00 -0700178
Adam Langley71d8a082014-12-13 16:28:18 -0800179 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700180
Adam Langley71d8a082014-12-13 16:28:18 -0800181 s->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800182 s->init_num = 0;
183 s->d1->send_cookie = 0;
184 s->hit = 0;
185 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langley71d8a082014-12-13 16:28:18 -0800187 case SSL3_ST_CW_CLNT_HELLO_A:
188 case SSL3_ST_CW_CLNT_HELLO_B:
189 s->shutdown = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langley71d8a082014-12-13 16:28:18 -0800191 /* every DTLS ClientHello resets Finished MAC */
192 if (!ssl3_init_finished_mac(s)) {
193 OPENSSL_PUT_ERROR(SSL, dtls1_connect, ERR_R_INTERNAL_ERROR);
194 ret = -1;
195 goto end;
196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langley71d8a082014-12-13 16:28:18 -0800198 dtls1_start_timer(s);
199 ret = ssl3_send_client_hello(s);
200 if (ret <= 0) {
201 goto end;
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley71d8a082014-12-13 16:28:18 -0800204 if (s->d1->send_cookie) {
205 s->state = SSL3_ST_CW_FLUSH;
206 s->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
207 } else {
208 s->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
209 }
Adam Langley95c29f32014-06-20 12:00:00 -0700210
Adam Langley71d8a082014-12-13 16:28:18 -0800211 s->init_num = 0;
212 /* turn on buffering for the next lot of output */
213 if (s->bbio != s->wbio) {
214 s->wbio = BIO_push(s->bbio, s->wbio);
215 }
Adam Langley95c29f32014-06-20 12:00:00 -0700216
Adam Langley71d8a082014-12-13 16:28:18 -0800217 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langley71d8a082014-12-13 16:28:18 -0800219 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
220 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
221 ret = dtls1_get_hello_verify(s);
222 if (ret <= 0) {
223 goto end;
224 }
225 if (s->d1->send_cookie) {
226 /* start again, with a cookie */
227 dtls1_stop_timer(s);
228 s->state = SSL3_ST_CW_CLNT_HELLO_A;
229 } else {
230 s->state = SSL3_ST_CR_SRVR_HELLO_A;
231 }
232 s->init_num = 0;
233 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700234
Adam Langley71d8a082014-12-13 16:28:18 -0800235 case SSL3_ST_CR_SRVR_HELLO_A:
236 case SSL3_ST_CR_SRVR_HELLO_B:
237 ret = ssl3_get_server_hello(s);
238 if (ret <= 0) {
239 goto end;
240 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langley71d8a082014-12-13 16:28:18 -0800242 if (s->hit) {
243 s->state = SSL3_ST_CR_FINISHED_A;
244 if (s->tlsext_ticket_expected) {
245 /* receive renewed session ticket */
246 s->state = SSL3_ST_CR_SESSION_TICKET_A;
247 }
248 } else {
249 s->state = SSL3_ST_CR_CERT_A;
250 }
251 s->init_num = 0;
252 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley71d8a082014-12-13 16:28:18 -0800254 case SSL3_ST_CR_CERT_A:
255 case SSL3_ST_CR_CERT_B:
256 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
257 ret = ssl3_get_server_certificate(s);
258 if (ret <= 0) {
259 goto end;
260 }
261 if (s->s3->tmp.certificate_status_expected) {
262 s->state = SSL3_ST_CR_CERT_STATUS_A;
263 } else {
264 s->state = SSL3_ST_CR_KEY_EXCH_A;
265 }
266 } else {
267 skip = 1;
268 s->state = SSL3_ST_CR_KEY_EXCH_A;
269 }
270 s->init_num = 0;
271 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langley71d8a082014-12-13 16:28:18 -0800273 case SSL3_ST_CR_KEY_EXCH_A:
274 case SSL3_ST_CR_KEY_EXCH_B:
275 ret = ssl3_get_server_key_exchange(s);
276 if (ret <= 0) {
277 goto end;
278 }
279 s->state = SSL3_ST_CR_CERT_REQ_A;
280 s->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800281 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400282
Adam Langley71d8a082014-12-13 16:28:18 -0800283 case SSL3_ST_CR_CERT_REQ_A:
284 case SSL3_ST_CR_CERT_REQ_B:
285 ret = ssl3_get_certificate_request(s);
286 if (ret <= 0) {
287 goto end;
288 }
289 s->state = SSL3_ST_CR_SRVR_DONE_A;
290 s->init_num = 0;
291 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700292
Adam Langley71d8a082014-12-13 16:28:18 -0800293 case SSL3_ST_CR_SRVR_DONE_A:
294 case SSL3_ST_CR_SRVR_DONE_B:
295 ret = ssl3_get_server_done(s);
296 if (ret <= 0) {
297 goto end;
298 }
299 dtls1_stop_timer(s);
300 if (s->s3->tmp.cert_req) {
301 s->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
302 } else {
303 s->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
304 }
305 s->init_num = 0;
306 s->state = s->s3->tmp.next_state;
307 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 case SSL3_ST_CW_CERT_A:
310 case SSL3_ST_CW_CERT_B:
311 case SSL3_ST_CW_CERT_C:
312 case SSL3_ST_CW_CERT_D:
313 dtls1_start_timer(s);
314 ret = ssl3_send_client_certificate(s);
315 if (ret <= 0) {
316 goto end;
317 }
318 s->state = SSL3_ST_CW_KEY_EXCH_A;
319 s->init_num = 0;
320 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langley71d8a082014-12-13 16:28:18 -0800322 case SSL3_ST_CW_KEY_EXCH_A:
323 case SSL3_ST_CW_KEY_EXCH_B:
324 dtls1_start_timer(s);
325 ret = ssl3_send_client_key_exchange(s);
326 if (ret <= 0) {
327 goto end;
328 }
329 /* For TLS, cert_req is set to 2, so a cert chain
330 * of nothing is sent, but no verify packet is sent */
331 if (s->s3->tmp.cert_req == 1) {
332 s->state = SSL3_ST_CW_CERT_VRFY_A;
333 } else {
334 s->state = SSL3_ST_CW_CHANGE_A;
335 s->s3->change_cipher_spec = 0;
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langley71d8a082014-12-13 16:28:18 -0800338 s->init_num = 0;
339 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langley71d8a082014-12-13 16:28:18 -0800341 case SSL3_ST_CW_CERT_VRFY_A:
342 case SSL3_ST_CW_CERT_VRFY_B:
343 dtls1_start_timer(s);
344 ret = ssl3_send_cert_verify(s);
345 if (ret <= 0) {
346 goto end;
347 }
348 s->state = SSL3_ST_CW_CHANGE_A;
349 s->init_num = 0;
350 s->s3->change_cipher_spec = 0;
351 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley71d8a082014-12-13 16:28:18 -0800353 case SSL3_ST_CW_CHANGE_A:
354 case SSL3_ST_CW_CHANGE_B:
355 if (!s->hit) {
356 dtls1_start_timer(s);
357 }
358 ret = dtls1_send_change_cipher_spec(s, SSL3_ST_CW_CHANGE_A,
359 SSL3_ST_CW_CHANGE_B);
360 if (ret <= 0) {
361 goto end;
362 }
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langley71d8a082014-12-13 16:28:18 -0800364 s->state = SSL3_ST_CW_FINISHED_A;
365 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700366
Adam Langley71d8a082014-12-13 16:28:18 -0800367 s->session->cipher = s->s3->tmp.new_cipher;
368 if (!s->enc_method->setup_key_block(s) ||
369 !s->enc_method->change_cipher_state(
370 s, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
371 ret = -1;
372 goto end;
373 }
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langley71d8a082014-12-13 16:28:18 -0800375 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
Adam Langley71d8a082014-12-13 16:28:18 -0800378 case SSL3_ST_CW_FINISHED_A:
379 case SSL3_ST_CW_FINISHED_B:
380 if (!s->hit) {
381 dtls1_start_timer(s);
382 }
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langley71d8a082014-12-13 16:28:18 -0800384 ret =
385 ssl3_send_finished(s, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
386 s->enc_method->client_finished_label,
387 s->enc_method->client_finished_label_len);
388 if (ret <= 0) {
389 goto end;
390 }
391 s->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langley71d8a082014-12-13 16:28:18 -0800393 if (s->hit) {
394 s->s3->tmp.next_state = SSL_ST_OK;
395 } else {
396 /* Allow NewSessionTicket if ticket expected */
397 if (s->tlsext_ticket_expected) {
398 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
399 } else {
400 s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
401 }
402 }
403 s->init_num = 0;
404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley71d8a082014-12-13 16:28:18 -0800406 case SSL3_ST_CR_SESSION_TICKET_A:
407 case SSL3_ST_CR_SESSION_TICKET_B:
408 ret = ssl3_get_new_session_ticket(s);
409 if (ret <= 0) {
410 goto end;
411 }
412 s->state = SSL3_ST_CR_FINISHED_A;
413 s->init_num = 0;
414 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700415
Adam Langley71d8a082014-12-13 16:28:18 -0800416 case SSL3_ST_CR_CERT_STATUS_A:
417 case SSL3_ST_CR_CERT_STATUS_B:
418 ret = ssl3_get_cert_status(s);
419 if (ret <= 0) {
420 goto end;
421 }
422 s->state = SSL3_ST_CR_KEY_EXCH_A;
423 s->init_num = 0;
424 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langley71d8a082014-12-13 16:28:18 -0800426 case SSL3_ST_CR_FINISHED_A:
427 case SSL3_ST_CR_FINISHED_B:
428 s->d1->change_cipher_spec_ok = 1;
429 ret =
430 ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
431 if (ret <= 0) {
432 goto end;
433 }
434 dtls1_stop_timer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley71d8a082014-12-13 16:28:18 -0800436 if (s->hit) {
437 s->state = SSL3_ST_CW_CHANGE_A;
438 } else {
439 s->state = SSL_ST_OK;
440 }
Adam Langley95c29f32014-06-20 12:00:00 -0700441
Adam Langley71d8a082014-12-13 16:28:18 -0800442 s->init_num = 0;
443 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley71d8a082014-12-13 16:28:18 -0800445 case SSL3_ST_CW_FLUSH:
446 s->rwstate = SSL_WRITING;
447 if (BIO_flush(s->wbio) <= 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800448 ret = -1;
449 goto end;
450 }
451 s->rwstate = SSL_NOTHING;
452 s->state = s->s3->tmp.next_state;
453 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700454
Adam Langley71d8a082014-12-13 16:28:18 -0800455 case SSL_ST_OK:
456 /* clean a few things up */
457 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langley71d8a082014-12-13 16:28:18 -0800459 /* Remove write buffering now. */
460 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langley71d8a082014-12-13 16:28:18 -0800462 s->init_num = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400463 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langley71d8a082014-12-13 16:28:18 -0800465 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langley71d8a082014-12-13 16:28:18 -0800467 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
David Benjamin6eb000d2015-02-11 01:17:41 -0500469 if (cb != NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800470 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langley71d8a082014-12-13 16:28:18 -0800473 /* done with handshaking */
474 s->d1->handshake_read_seq = 0;
475 s->d1->next_handshake_write_seq = 0;
476 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langley71d8a082014-12-13 16:28:18 -0800478 default:
479 OPENSSL_PUT_ERROR(SSL, dtls1_connect, SSL_R_UNKNOWN_STATE);
480 ret = -1;
481 goto end;
482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langley71d8a082014-12-13 16:28:18 -0800484 /* did we do anything? */
485 if (!s->s3->tmp.reuse_message && !skip) {
486 if ((cb != NULL) && (s->state != state)) {
487 new_state = s->state;
488 s->state = state;
489 cb(s, SSL_CB_CONNECT_LOOP, 1);
490 s->state = new_state;
491 }
492 }
493 skip = 0;
494 }
Adam Langley95c29f32014-06-20 12:00:00 -0700495
Adam Langley95c29f32014-06-20 12:00:00 -0700496end:
Adam Langley71d8a082014-12-13 16:28:18 -0800497 s->in_handshake--;
Adam Langley95c29f32014-06-20 12:00:00 -0700498
David Benjamin2755a3e2015-04-22 16:17:58 -0400499 BUF_MEM_free(buf);
Adam Langley71d8a082014-12-13 16:28:18 -0800500 if (cb != NULL) {
501 cb(s, SSL_CB_CONNECT_EXIT, ret);
502 }
503 return ret;
504}
Adam Langley95c29f32014-06-20 12:00:00 -0700505
Adam Langley71d8a082014-12-13 16:28:18 -0800506static int dtls1_get_hello_verify(SSL *s) {
507 long n;
508 int al, ok = 0;
509 CBS hello_verify_request, cookie;
510 uint16_t server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langley71d8a082014-12-13 16:28:18 -0800512 n = s->method->ssl_get_message(
513 s, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B,
514 -1,
515 /* Use the same maximum size as ssl3_get_server_hello. */
David Benjamin5ca39fb2015-03-01 23:57:54 -0500516 20000, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langley71d8a082014-12-13 16:28:18 -0800518 if (!ok) {
519 return n;
520 }
Adam Langley95c29f32014-06-20 12:00:00 -0700521
Adam Langley71d8a082014-12-13 16:28:18 -0800522 if (s->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
523 s->d1->send_cookie = 0;
524 s->s3->tmp.reuse_message = 1;
525 return 1;
526 }
David Benjamin51e32832014-08-13 15:13:57 -0400527
Adam Langley71d8a082014-12-13 16:28:18 -0800528 CBS_init(&hello_verify_request, s->init_msg, n);
David Benjamin51e32832014-08-13 15:13:57 -0400529
Adam Langley71d8a082014-12-13 16:28:18 -0800530 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
531 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
532 CBS_len(&hello_verify_request) != 0) {
533 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700534 OPENSSL_PUT_ERROR(SSL, dtls1_get_hello_verify, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800535 goto f_err;
536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langley71d8a082014-12-13 16:28:18 -0800538 if (CBS_len(&cookie) > sizeof(s->d1->cookie)) {
539 al = SSL_AD_ILLEGAL_PARAMETER;
540 goto f_err;
541 }
Adam Langley95c29f32014-06-20 12:00:00 -0700542
Adam Langley71d8a082014-12-13 16:28:18 -0800543 memcpy(s->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
544 s->d1->cookie_len = CBS_len(&cookie);
545
546 s->d1->send_cookie = 1;
547 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700548
549f_err:
Adam Langley71d8a082014-12-13 16:28:18 -0800550 ssl3_send_alert(s, SSL3_AL_FATAL, al);
551 return -1;
552}