blob: b61a139365f6a2fc99b2032f993f2978468ac724 [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>
David Benjaminf0ae1702015-04-07 23:05:04 -0400118#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700119
120#include <openssl/bn.h>
121#include <openssl/buf.h>
122#include <openssl/dh.h>
123#include <openssl/evp.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400124#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700125#include <openssl/md5.h>
126#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700127#include <openssl/rand.h>
128
David Benjamin2ee94aa2015-04-07 22:38:30 -0400129#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjamin9e4e01e2015-09-15 01:48:04 -0400131
David Benjamin0d56f882015-12-19 17:05:56 -0500132static int dtls1_get_hello_verify(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700133
David Benjamin0d56f882015-12-19 17:05:56 -0500134int dtls1_connect(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800135 BUF_MEM *buf = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800136 int ret = -1;
David Benjamin4e9cc712016-06-01 20:16:03 -0400137 int state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamin0d56f882015-12-19 17:05:56 -0500139 assert(ssl->handshake_func == dtls1_connect);
140 assert(!ssl->server);
141 assert(SSL_IS_DTLS(ssl));
David Benjaminbeb47022014-11-30 02:58:52 -0500142
Adam Langley71d8a082014-12-13 16:28:18 -0800143 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500144 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700145
David Benjamin0d56f882015-12-19 17:05:56 -0500146 switch (ssl->state) {
Adam Langley71d8a082014-12-13 16:28:18 -0800147 case SSL_ST_CONNECT:
David Benjamin4e9cc712016-06-01 20:16:03 -0400148 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjamin0d56f882015-12-19 17:05:56 -0500150 if (ssl->init_buf == NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800151 buf = BUF_MEM_new();
152 if (buf == NULL ||
David Benjamina6338be2016-05-13 18:12:19 -0400153 !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800154 ret = -1;
155 goto end;
156 }
David Benjamin0d56f882015-12-19 17:05:56 -0500157 ssl->init_buf = buf;
Adam Langley71d8a082014-12-13 16:28:18 -0800158 buf = NULL;
159 }
David Benjamin1d64afd2016-05-15 13:46:07 -0400160 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjaminb095f0f2016-05-05 21:50:24 -0400162 if (!ssl_init_wbio_buffer(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800163 ret = -1;
164 goto end;
165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjamin0d56f882015-12-19 17:05:56 -0500167 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
David Benjamin0d56f882015-12-19 17:05:56 -0500168 ssl->d1->send_cookie = 0;
169 ssl->hit = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800170 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley71d8a082014-12-13 16:28:18 -0800172 case SSL3_ST_CW_CLNT_HELLO_A:
173 case SSL3_ST_CW_CLNT_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500174 dtls1_start_timer(ssl);
175 ret = ssl3_send_client_hello(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800176 if (ret <= 0) {
177 goto end;
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjamin0d56f882015-12-19 17:05:56 -0500180 if (ssl->d1->send_cookie) {
David Benjamin0d56f882015-12-19 17:05:56 -0500181 ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800182 } else {
David Benjaminb095f0f2016-05-05 21:50:24 -0400183 ssl->s3->tmp.next_state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800184 }
David Benjaminb095f0f2016-05-05 21:50:24 -0400185 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley71d8a082014-12-13 16:28:18 -0800186 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langley71d8a082014-12-13 16:28:18 -0800188 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500189 ret = dtls1_get_hello_verify(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800190 if (ret <= 0) {
191 goto end;
192 }
David Benjamin0d56f882015-12-19 17:05:56 -0500193 if (ssl->d1->send_cookie) {
Adam Langley71d8a082014-12-13 16:28:18 -0800194 /* start again, with a cookie */
David Benjamin0d56f882015-12-19 17:05:56 -0500195 dtls1_stop_timer(ssl);
196 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800197 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500198 ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800199 }
Adam Langley71d8a082014-12-13 16:28:18 -0800200 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley71d8a082014-12-13 16:28:18 -0800202 case SSL3_ST_CR_SRVR_HELLO_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500203 ret = ssl3_get_server_hello(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800204 if (ret <= 0) {
205 goto end;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
David Benjamin0d56f882015-12-19 17:05:56 -0500208 if (ssl->hit) {
209 ssl->state = SSL3_ST_CR_CHANGE;
210 if (ssl->tlsext_ticket_expected) {
Adam Langley71d8a082014-12-13 16:28:18 -0800211 /* receive renewed session ticket */
David Benjamin0d56f882015-12-19 17:05:56 -0500212 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800213 }
214 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500215 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800216 }
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 SSL3_ST_CR_CERT_A:
David Benjaminc032dfa2016-05-12 14:54:57 -0400220 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500221 ret = ssl3_get_server_certificate(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800222 if (ret <= 0) {
223 goto end;
224 }
David Benjamin0d56f882015-12-19 17:05:56 -0500225 if (ssl->s3->tmp.certificate_status_expected) {
226 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800227 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500228 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley71d8a082014-12-13 16:28:18 -0800229 }
230 } else {
231 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500232 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800233 }
Adam Langley71d8a082014-12-13 16:28:18 -0800234 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700235
Paul Lietar8f1c2682015-08-18 12:21:54 +0100236 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin0d56f882015-12-19 17:05:56 -0500237 ret = ssl3_verify_server_cert(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100238 if (ret <= 0) {
239 goto end;
240 }
241
David Benjamin0d56f882015-12-19 17:05:56 -0500242 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100243 break;
244
Adam Langley71d8a082014-12-13 16:28:18 -0800245 case SSL3_ST_CR_KEY_EXCH_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500246 ret = ssl3_get_server_key_exchange(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800247 if (ret <= 0) {
248 goto end;
249 }
David Benjaminc032dfa2016-05-12 14:54:57 -0400250 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
251 ssl->state = SSL3_ST_CR_CERT_REQ_A;
252 } else {
253 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
254 }
Adam Langley71d8a082014-12-13 16:28:18 -0800255 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400256
Adam Langley71d8a082014-12-13 16:28:18 -0800257 case SSL3_ST_CR_CERT_REQ_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500258 ret = ssl3_get_certificate_request(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800259 if (ret <= 0) {
260 goto end;
261 }
David Benjamin0d56f882015-12-19 17:05:56 -0500262 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800263 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langley71d8a082014-12-13 16:28:18 -0800265 case SSL3_ST_CR_SRVR_DONE_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500266 ret = ssl3_get_server_done(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800267 if (ret <= 0) {
268 goto end;
269 }
David Benjamin0d56f882015-12-19 17:05:56 -0500270 dtls1_stop_timer(ssl);
271 if (ssl->s3->tmp.cert_req) {
272 ssl->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800273 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500274 ssl->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800275 }
David Benjamin0d56f882015-12-19 17:05:56 -0500276 ssl->state = ssl->s3->tmp.next_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800277 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langley71d8a082014-12-13 16:28:18 -0800279 case SSL3_ST_CW_CERT_A:
280 case SSL3_ST_CW_CERT_B:
281 case SSL3_ST_CW_CERT_C:
282 case SSL3_ST_CW_CERT_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500283 dtls1_start_timer(ssl);
284 ret = ssl3_send_client_certificate(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800285 if (ret <= 0) {
286 goto end;
287 }
David Benjamin0d56f882015-12-19 17:05:56 -0500288 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800289 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700290
Adam Langley71d8a082014-12-13 16:28:18 -0800291 case SSL3_ST_CW_KEY_EXCH_A:
292 case SSL3_ST_CW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500293 dtls1_start_timer(ssl);
294 ret = ssl3_send_client_key_exchange(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800295 if (ret <= 0) {
296 goto end;
297 }
298 /* For TLS, cert_req is set to 2, so a cert chain
299 * of nothing is sent, but no verify packet is sent */
David Benjamin0d56f882015-12-19 17:05:56 -0500300 if (ssl->s3->tmp.cert_req == 1) {
301 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800302 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500303 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800304 }
Adam Langley71d8a082014-12-13 16:28:18 -0800305 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley71d8a082014-12-13 16:28:18 -0800307 case SSL3_ST_CW_CERT_VRFY_A:
308 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminc81ee8b2015-11-01 14:22:34 -0500309 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500310 dtls1_start_timer(ssl);
311 ret = ssl3_send_cert_verify(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800312 if (ret <= 0) {
313 goto end;
314 }
David Benjamin0d56f882015-12-19 17:05:56 -0500315 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800316 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langley71d8a082014-12-13 16:28:18 -0800318 case SSL3_ST_CW_CHANGE_A:
319 case SSL3_ST_CW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500320 if (!ssl->hit) {
321 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800322 }
David Benjamin0d56f882015-12-19 17:05:56 -0500323 ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
Adam Langley71d8a082014-12-13 16:28:18 -0800324 SSL3_ST_CW_CHANGE_B);
325 if (ret <= 0) {
326 goto end;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin0d56f882015-12-19 17:05:56 -0500329 ssl->state = SSL3_ST_CW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamin57997da2015-12-25 15:48:39 -0500331 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800332 ret = -1;
333 goto end;
334 }
Adam Langley71d8a082014-12-13 16:28:18 -0800335 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700336
Adam Langley71d8a082014-12-13 16:28:18 -0800337 case SSL3_ST_CW_FINISHED_A:
338 case SSL3_ST_CW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500339 if (!ssl->hit) {
340 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjaminbaa12162015-12-29 19:13:58 -0500343 ret = ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A,
344 SSL3_ST_CW_FINISHED_B);
Adam Langley71d8a082014-12-13 16:28:18 -0800345 if (ret <= 0) {
346 goto end;
347 }
David Benjamin0d56f882015-12-19 17:05:56 -0500348 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamin0d56f882015-12-19 17:05:56 -0500350 if (ssl->hit) {
351 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley71d8a082014-12-13 16:28:18 -0800352 } else {
353 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500354 if (ssl->tlsext_ticket_expected) {
355 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800356 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500357 ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
Adam Langley71d8a082014-12-13 16:28:18 -0800358 }
359 }
Adam Langley71d8a082014-12-13 16:28:18 -0800360 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langley71d8a082014-12-13 16:28:18 -0800362 case SSL3_ST_CR_SESSION_TICKET_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500363 ret = ssl3_get_new_session_ticket(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800364 if (ret <= 0) {
365 goto end;
366 }
David Benjamin0d56f882015-12-19 17:05:56 -0500367 ssl->state = SSL3_ST_CR_CHANGE;
Adam Langley71d8a082014-12-13 16:28:18 -0800368 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langley71d8a082014-12-13 16:28:18 -0800370 case SSL3_ST_CR_CERT_STATUS_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500371 ret = ssl3_get_cert_status(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800372 if (ret <= 0) {
373 goto end;
374 }
David Benjamin0d56f882015-12-19 17:05:56 -0500375 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley71d8a082014-12-13 16:28:18 -0800376 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700377
David Benjamina41280d2015-11-26 02:16:49 -0500378 case SSL3_ST_CR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500379 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500380 if (ret <= 0) {
381 goto end;
382 }
383
David Benjamin57997da2015-12-25 15:48:39 -0500384 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_CLIENT_READ)) {
David Benjamina41280d2015-11-26 02:16:49 -0500385 ret = -1;
386 goto end;
387 }
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ssl->state = SSL3_ST_CR_FINISHED_A;
David Benjamina41280d2015-11-26 02:16:49 -0500389 break;
390
Adam Langley71d8a082014-12-13 16:28:18 -0800391 case SSL3_ST_CR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400392 ret = ssl3_get_finished(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800393 if (ret <= 0) {
394 goto end;
395 }
David Benjamin0d56f882015-12-19 17:05:56 -0500396 dtls1_stop_timer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700397
David Benjamin0d56f882015-12-19 17:05:56 -0500398 if (ssl->hit) {
399 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800400 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500401 ssl->state = SSL_ST_OK;
Adam Langley71d8a082014-12-13 16:28:18 -0800402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley71d8a082014-12-13 16:28:18 -0800404 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langley71d8a082014-12-13 16:28:18 -0800406 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500407 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500408 ssl->rwstate = SSL_WRITING;
Adam Langley71d8a082014-12-13 16:28:18 -0800409 ret = -1;
410 goto end;
411 }
David Benjamin0d56f882015-12-19 17:05:56 -0500412 ssl->state = ssl->s3->tmp.next_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800413 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
Adam Langley71d8a082014-12-13 16:28:18 -0800415 case SSL_ST_OK:
416 /* clean a few things up */
David Benjamin0d56f882015-12-19 17:05:56 -0500417 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700418
Adam Langley71d8a082014-12-13 16:28:18 -0800419 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500420 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700421
David Benjaminfce37b02016-05-15 13:51:35 -0400422 /* |init_buf| cannot be released because post-handshake retransmit
423 * relies on that buffer being available as scratch space.
424 *
425 * TODO(davidben): Fix this. */
David Benjamin0d56f882015-12-19 17:05:56 -0500426 ssl->init_num = 0;
427 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langley71d8a082014-12-13 16:28:18 -0800431 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamin4e9cc712016-06-01 20:16:03 -0400433 ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700434
Adam Langley71d8a082014-12-13 16:28:18 -0800435 /* done with handshaking */
David Benjamin0d56f882015-12-19 17:05:56 -0500436 ssl->d1->handshake_read_seq = 0;
437 ssl->d1->next_handshake_write_seq = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800438 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700439
Adam Langley71d8a082014-12-13 16:28:18 -0800440 default:
David Benjamin3570d732015-06-29 00:28:17 -0400441 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley71d8a082014-12-13 16:28:18 -0800442 ret = -1;
443 goto end;
444 }
Adam Langley95c29f32014-06-20 12:00:00 -0700445
Adam Langley71d8a082014-12-13 16:28:18 -0800446 /* did we do anything? */
David Benjamin4e9cc712016-06-01 20:16:03 -0400447 if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) {
448 int new_state = ssl->state;
449 ssl->state = state;
450 ssl_do_info_callback(ssl, SSL_CB_CONNECT_LOOP, 1);
451 ssl->state = new_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800452 }
453 skip = 0;
454 }
Adam Langley95c29f32014-06-20 12:00:00 -0700455
Adam Langley95c29f32014-06-20 12:00:00 -0700456end:
David Benjamin2755a3e2015-04-22 16:17:58 -0400457 BUF_MEM_free(buf);
David Benjamin4e9cc712016-06-01 20:16:03 -0400458 ssl_do_info_callback(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley71d8a082014-12-13 16:28:18 -0800459 return ret;
460}
Adam Langley95c29f32014-06-20 12:00:00 -0700461
David Benjamin0d56f882015-12-19 17:05:56 -0500462static int dtls1_get_hello_verify(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800463 long n;
464 int al, ok = 0;
465 CBS hello_verify_request, cookie;
466 uint16_t server_version;
Adam Langley95c29f32014-06-20 12:00:00 -0700467
David Benjamin1e6d6df2016-05-13 18:28:17 -0400468 n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok);
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langley71d8a082014-12-13 16:28:18 -0800470 if (!ok) {
471 return n;
472 }
Adam Langley95c29f32014-06-20 12:00:00 -0700473
David Benjamin0d56f882015-12-19 17:05:56 -0500474 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
475 ssl->d1->send_cookie = 0;
476 ssl->s3->tmp.reuse_message = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800477 return 1;
478 }
David Benjamin51e32832014-08-13 15:13:57 -0400479
David Benjamin0d56f882015-12-19 17:05:56 -0500480 CBS_init(&hello_verify_request, ssl->init_msg, n);
David Benjamin51e32832014-08-13 15:13:57 -0400481
Adam Langley71d8a082014-12-13 16:28:18 -0800482 if (!CBS_get_u16(&hello_verify_request, &server_version) ||
483 !CBS_get_u8_length_prefixed(&hello_verify_request, &cookie) ||
484 CBS_len(&hello_verify_request) != 0) {
485 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400486 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800487 goto f_err;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjamin0d56f882015-12-19 17:05:56 -0500490 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800491 al = SSL_AD_ILLEGAL_PARAMETER;
492 goto f_err;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
David Benjamin0d56f882015-12-19 17:05:56 -0500495 memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
496 ssl->d1->cookie_len = CBS_len(&cookie);
Adam Langley71d8a082014-12-13 16:28:18 -0800497
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl->d1->send_cookie = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800499 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700500
501f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500502 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800503 return -1;
504}