blob: ad5eb50638b92c1e082db2d85d1c4be267013cfc [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/*
2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
59 * All rights reserved.
60 *
61 * This package is an SSL implementation written
62 * by Eric Young (eay@cryptsoft.com).
63 * The implementation was written so as to conform with Netscapes SSL.
64 *
65 * This library is free for commercial and non-commercial use as long as
66 * the following conditions are aheared to. The following conditions
67 * apply to all code found in this distribution, be it the RC4, RSA,
68 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
69 * included with this distribution is covered by the same copyright terms
70 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
71 *
72 * Copyright remains Eric Young's, and as such any Copyright notices in
73 * the code are not to be removed.
74 * If this package is used in a product, Eric Young should be given attribution
75 * as the author of the parts of the library used.
76 * This can be in the form of a textual message at program startup or
77 * in documentation (online or textual) provided with the package.
78 *
79 * Redistribution and use in source and binary forms, with or without
80 * modification, are permitted provided that the following conditions
81 * are met:
82 * 1. Redistributions of source code must retain the copyright
83 * notice, this list of conditions and the following disclaimer.
84 * 2. Redistributions in binary form must reproduce the above copyright
85 * notice, this list of conditions and the following disclaimer in the
86 * documentation and/or other materials provided with the distribution.
87 * 3. All advertising materials mentioning features or use of this software
88 * must display the following acknowledgement:
89 * "This product includes cryptographic software written by
90 * Eric Young (eay@cryptsoft.com)"
91 * The word 'cryptographic' can be left out if the rouines from the library
92 * being used are not cryptographic related :-).
93 * 4. If you include any Windows specific code (or a derivative thereof) from
94 * the apps directory (application code) you must include an acknowledgement:
95 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
96 *
97 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
98 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
99 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
100 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
101 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
102 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
103 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
104 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
105 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
106 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
107 * SUCH DAMAGE.
108 *
109 * The licence and distribution terms for any publically available version or
110 * derivative of this code cannot be changed. i.e. this code cannot simply be
111 * copied and put under another distribution licence
112 * [including the GNU Public Licence.]
113 */
114
David Benjamin9e4e01e2015-09-15 01:48:04 -0400115#include <openssl/ssl.h>
116
David Benjamin0b145c22014-11-26 20:10:09 -0500117#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400119#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120
121#include <openssl/bn.h>
122#include <openssl/buf.h>
123#include <openssl/dh.h>
124#include <openssl/evp.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400125#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700126#include <openssl/md5.h>
127#include <openssl/mem.h>
128#include <openssl/obj.h>
129#include <openssl/rand.h>
130
David Benjamin2ee94aa2015-04-07 22:38:30 -0400131#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700132
David Benjamin9e4e01e2015-09-15 01:48:04 -0400133
David Benjamin0d56f882015-12-19 17:05:56 -0500134static int dtls1_get_hello_verify(SSL *ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700135
David Benjamin0d56f882015-12-19 17:05:56 -0500136int dtls1_connect(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800137 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400138 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800139 int ret = -1;
140 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjamin0d56f882015-12-19 17:05:56 -0500142 assert(ssl->handshake_func == dtls1_connect);
143 assert(!ssl->server);
144 assert(SSL_IS_DTLS(ssl));
David Benjaminbeb47022014-11-30 02:58:52 -0500145
Adam Langley71d8a082014-12-13 16:28:18 -0800146 ERR_clear_error();
147 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700148
David Benjamin0d56f882015-12-19 17:05:56 -0500149 if (ssl->info_callback != NULL) {
150 cb = ssl->info_callback;
151 } else if (ssl->ctx->info_callback != NULL) {
152 cb = ssl->ctx->info_callback;
Adam Langley71d8a082014-12-13 16:28:18 -0800153 }
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjamin0d56f882015-12-19 17:05:56 -0500155 ssl->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700156
Adam Langley71d8a082014-12-13 16:28:18 -0800157 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500158 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700159
David Benjamin0d56f882015-12-19 17:05:56 -0500160 switch (ssl->state) {
Adam Langley71d8a082014-12-13 16:28:18 -0800161 case SSL_ST_CONNECT:
Adam Langley71d8a082014-12-13 16:28:18 -0800162 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500163 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley71d8a082014-12-13 16:28:18 -0800164 }
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjamin0d56f882015-12-19 17:05:56 -0500166 if (ssl->init_buf == NULL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800167 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 }
David Benjamin0d56f882015-12-19 17:05:56 -0500173 ssl->init_buf = buf;
Adam Langley71d8a082014-12-13 16:28:18 -0800174 buf = NULL;
175 }
Adam Langley95c29f32014-06-20 12:00:00 -0700176
David Benjamin0d56f882015-12-19 17:05:56 -0500177 if (!ssl_init_wbio_buffer(ssl, 0)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800178 ret = -1;
179 goto end;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley71d8a082014-12-13 16:28:18 -0800182 /* don't push the buffering BIO quite yet */
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjamin0d56f882015-12-19 17:05:56 -0500184 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
185 ssl->init_num = 0;
186 ssl->d1->send_cookie = 0;
187 ssl->hit = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800188 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190 case SSL3_ST_CW_CLNT_HELLO_A:
191 case SSL3_ST_CW_CLNT_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500192 ssl->shutdown = 0;
193 dtls1_start_timer(ssl);
194 ret = ssl3_send_client_hello(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800195 if (ret <= 0) {
196 goto end;
197 }
Adam Langley95c29f32014-06-20 12:00:00 -0700198
David Benjamin0d56f882015-12-19 17:05:56 -0500199 if (ssl->d1->send_cookie) {
200 ssl->state = SSL3_ST_CW_FLUSH;
201 ssl->s3->tmp.next_state = SSL3_ST_CR_SRVR_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800202 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500203 ssl->state = DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800204 }
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin0d56f882015-12-19 17:05:56 -0500206 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800207 /* turn on buffering for the next lot of output */
David Benjamin0d56f882015-12-19 17:05:56 -0500208 if (ssl->bbio != ssl->wbio) {
209 ssl->wbio = BIO_push(ssl->bbio, ssl->wbio);
Adam Langley71d8a082014-12-13 16:28:18 -0800210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
Adam Langley71d8a082014-12-13 16:28:18 -0800212 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langley71d8a082014-12-13 16:28:18 -0800214 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A:
215 case DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500216 ret = dtls1_get_hello_verify(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800217 if (ret <= 0) {
218 goto end;
219 }
David Benjamin0d56f882015-12-19 17:05:56 -0500220 if (ssl->d1->send_cookie) {
Adam Langley71d8a082014-12-13 16:28:18 -0800221 /* start again, with a cookie */
David Benjamin0d56f882015-12-19 17:05:56 -0500222 dtls1_stop_timer(ssl);
223 ssl->state = SSL3_ST_CW_CLNT_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800224 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500225 ssl->state = SSL3_ST_CR_SRVR_HELLO_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800226 }
David Benjamin0d56f882015-12-19 17:05:56 -0500227 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800228 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langley71d8a082014-12-13 16:28:18 -0800230 case SSL3_ST_CR_SRVR_HELLO_A:
231 case SSL3_ST_CR_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500232 ret = ssl3_get_server_hello(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800233 if (ret <= 0) {
234 goto end;
235 }
Adam Langley95c29f32014-06-20 12:00:00 -0700236
David Benjamin0d56f882015-12-19 17:05:56 -0500237 if (ssl->hit) {
238 ssl->state = SSL3_ST_CR_CHANGE;
239 if (ssl->tlsext_ticket_expected) {
Adam Langley71d8a082014-12-13 16:28:18 -0800240 /* receive renewed session ticket */
David Benjamin0d56f882015-12-19 17:05:56 -0500241 ssl->state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800242 }
243 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500244 ssl->state = SSL3_ST_CR_CERT_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800245 }
David Benjamin0d56f882015-12-19 17:05:56 -0500246 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800247 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Adam Langley71d8a082014-12-13 16:28:18 -0800249 case SSL3_ST_CR_CERT_A:
250 case SSL3_ST_CR_CERT_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500251 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
252 ret = ssl3_get_server_certificate(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800253 if (ret <= 0) {
254 goto end;
255 }
David Benjamin0d56f882015-12-19 17:05:56 -0500256 if (ssl->s3->tmp.certificate_status_expected) {
257 ssl->state = SSL3_ST_CR_CERT_STATUS_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800258 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500259 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
Adam Langley71d8a082014-12-13 16:28:18 -0800260 }
261 } else {
262 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500263 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800264 }
David Benjamin0d56f882015-12-19 17:05:56 -0500265 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800266 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Paul Lietar8f1c2682015-08-18 12:21:54 +0100268 case SSL3_ST_VERIFY_SERVER_CERT:
David Benjamin0d56f882015-12-19 17:05:56 -0500269 ret = ssl3_verify_server_cert(ssl);
Paul Lietar8f1c2682015-08-18 12:21:54 +0100270 if (ret <= 0) {
271 goto end;
272 }
273
David Benjamin0d56f882015-12-19 17:05:56 -0500274 ssl->state = SSL3_ST_CR_KEY_EXCH_A;
275 ssl->init_num = 0;
Paul Lietar8f1c2682015-08-18 12:21:54 +0100276 break;
277
Adam Langley71d8a082014-12-13 16:28:18 -0800278 case SSL3_ST_CR_KEY_EXCH_A:
279 case SSL3_ST_CR_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500280 ret = ssl3_get_server_key_exchange(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800281 if (ret <= 0) {
282 goto end;
283 }
David Benjamin0d56f882015-12-19 17:05:56 -0500284 ssl->state = SSL3_ST_CR_CERT_REQ_A;
285 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800286 break;
David Benjaminf2fedef2014-08-16 01:37:34 -0400287
Adam Langley71d8a082014-12-13 16:28:18 -0800288 case SSL3_ST_CR_CERT_REQ_A:
289 case SSL3_ST_CR_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500290 ret = ssl3_get_certificate_request(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800291 if (ret <= 0) {
292 goto end;
293 }
David Benjamin0d56f882015-12-19 17:05:56 -0500294 ssl->state = SSL3_ST_CR_SRVR_DONE_A;
295 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800296 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298 case SSL3_ST_CR_SRVR_DONE_A:
299 case SSL3_ST_CR_SRVR_DONE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500300 ret = ssl3_get_server_done(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800301 if (ret <= 0) {
302 goto end;
303 }
David Benjamin0d56f882015-12-19 17:05:56 -0500304 dtls1_stop_timer(ssl);
305 if (ssl->s3->tmp.cert_req) {
306 ssl->s3->tmp.next_state = SSL3_ST_CW_CERT_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800307 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500308 ssl->s3->tmp.next_state = SSL3_ST_CW_KEY_EXCH_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800309 }
David Benjamin0d56f882015-12-19 17:05:56 -0500310 ssl->init_num = 0;
311 ssl->state = ssl->s3->tmp.next_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800312 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langley71d8a082014-12-13 16:28:18 -0800314 case SSL3_ST_CW_CERT_A:
315 case SSL3_ST_CW_CERT_B:
316 case SSL3_ST_CW_CERT_C:
317 case SSL3_ST_CW_CERT_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500318 dtls1_start_timer(ssl);
319 ret = ssl3_send_client_certificate(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800320 if (ret <= 0) {
321 goto end;
322 }
David Benjamin0d56f882015-12-19 17:05:56 -0500323 ssl->state = SSL3_ST_CW_KEY_EXCH_A;
324 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800325 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langley71d8a082014-12-13 16:28:18 -0800327 case SSL3_ST_CW_KEY_EXCH_A:
328 case SSL3_ST_CW_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500329 dtls1_start_timer(ssl);
330 ret = ssl3_send_client_key_exchange(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800331 if (ret <= 0) {
332 goto end;
333 }
334 /* For TLS, cert_req is set to 2, so a cert chain
335 * of nothing is sent, but no verify packet is sent */
David Benjamin0d56f882015-12-19 17:05:56 -0500336 if (ssl->s3->tmp.cert_req == 1) {
337 ssl->state = SSL3_ST_CW_CERT_VRFY_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800338 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500339 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800340 }
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800343 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langley71d8a082014-12-13 16:28:18 -0800345 case SSL3_ST_CW_CERT_VRFY_A:
346 case SSL3_ST_CW_CERT_VRFY_B:
David Benjaminc81ee8b2015-11-01 14:22:34 -0500347 case SSL3_ST_CW_CERT_VRFY_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500348 dtls1_start_timer(ssl);
349 ret = ssl3_send_cert_verify(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800350 if (ret <= 0) {
351 goto end;
352 }
David Benjamin0d56f882015-12-19 17:05:56 -0500353 ssl->state = SSL3_ST_CW_CHANGE_A;
354 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800355 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langley71d8a082014-12-13 16:28:18 -0800357 case SSL3_ST_CW_CHANGE_A:
358 case SSL3_ST_CW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500359 if (!ssl->hit) {
360 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800361 }
David Benjamin0d56f882015-12-19 17:05:56 -0500362 ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_CW_CHANGE_A,
Adam Langley71d8a082014-12-13 16:28:18 -0800363 SSL3_ST_CW_CHANGE_B);
364 if (ret <= 0) {
365 goto end;
366 }
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin0d56f882015-12-19 17:05:56 -0500368 ssl->state = SSL3_ST_CW_FINISHED_A;
369 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700370
David Benjamin0d56f882015-12-19 17:05:56 -0500371 ssl->session->cipher = ssl->s3->tmp.new_cipher;
372 if (!ssl->enc_method->setup_key_block(ssl) ||
373 !ssl->enc_method->change_cipher_state(
374 ssl, SSL3_CHANGE_CIPHER_CLIENT_WRITE)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800375 ret = -1;
376 goto end;
377 }
Adam Langley71d8a082014-12-13 16:28:18 -0800378 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langley71d8a082014-12-13 16:28:18 -0800380 case SSL3_ST_CW_FINISHED_A:
381 case SSL3_ST_CW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500382 if (!ssl->hit) {
383 dtls1_start_timer(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley71d8a082014-12-13 16:28:18 -0800386 ret =
David Benjamin0d56f882015-12-19 17:05:56 -0500387 ssl3_send_finished(ssl, SSL3_ST_CW_FINISHED_A, SSL3_ST_CW_FINISHED_B,
388 ssl->enc_method->client_finished_label,
389 ssl->enc_method->client_finished_label_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800390 if (ret <= 0) {
391 goto end;
392 }
David Benjamin0d56f882015-12-19 17:05:56 -0500393 ssl->state = SSL3_ST_CW_FLUSH;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
David Benjamin0d56f882015-12-19 17:05:56 -0500395 if (ssl->hit) {
396 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley71d8a082014-12-13 16:28:18 -0800397 } else {
398 /* Allow NewSessionTicket if ticket expected */
David Benjamin0d56f882015-12-19 17:05:56 -0500399 if (ssl->tlsext_ticket_expected) {
400 ssl->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800401 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500402 ssl->s3->tmp.next_state = SSL3_ST_CR_CHANGE;
Adam Langley71d8a082014-12-13 16:28:18 -0800403 }
404 }
David Benjamin0d56f882015-12-19 17:05:56 -0500405 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800406 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700407
Adam Langley71d8a082014-12-13 16:28:18 -0800408 case SSL3_ST_CR_SESSION_TICKET_A:
409 case SSL3_ST_CR_SESSION_TICKET_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500410 ret = ssl3_get_new_session_ticket(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800411 if (ret <= 0) {
412 goto end;
413 }
David Benjamin0d56f882015-12-19 17:05:56 -0500414 ssl->state = SSL3_ST_CR_CHANGE;
415 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800416 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langley71d8a082014-12-13 16:28:18 -0800418 case SSL3_ST_CR_CERT_STATUS_A:
419 case SSL3_ST_CR_CERT_STATUS_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500420 ret = ssl3_get_cert_status(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800421 if (ret <= 0) {
422 goto end;
423 }
David Benjamin0d56f882015-12-19 17:05:56 -0500424 ssl->state = SSL3_ST_VERIFY_SERVER_CERT;
425 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
David Benjamina41280d2015-11-26 02:16:49 -0500428 case SSL3_ST_CR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500430 if (ret <= 0) {
431 goto end;
432 }
433
David Benjamin0d56f882015-12-19 17:05:56 -0500434 if (!ssl3_do_change_cipher_spec(ssl)) {
David Benjamina41280d2015-11-26 02:16:49 -0500435 ret = -1;
436 goto end;
437 }
David Benjamin0d56f882015-12-19 17:05:56 -0500438 ssl->state = SSL3_ST_CR_FINISHED_A;
David Benjamina41280d2015-11-26 02:16:49 -0500439 break;
440
Adam Langley71d8a082014-12-13 16:28:18 -0800441 case SSL3_ST_CR_FINISHED_A:
442 case SSL3_ST_CR_FINISHED_B:
Adam Langley71d8a082014-12-13 16:28:18 -0800443 ret =
David Benjamin0d56f882015-12-19 17:05:56 -0500444 ssl3_get_finished(ssl, SSL3_ST_CR_FINISHED_A, SSL3_ST_CR_FINISHED_B);
Adam Langley71d8a082014-12-13 16:28:18 -0800445 if (ret <= 0) {
446 goto end;
447 }
David Benjamin0d56f882015-12-19 17:05:56 -0500448 dtls1_stop_timer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700449
David Benjamin0d56f882015-12-19 17:05:56 -0500450 if (ssl->hit) {
451 ssl->state = SSL3_ST_CW_CHANGE_A;
Adam Langley71d8a082014-12-13 16:28:18 -0800452 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500453 ssl->state = SSL_ST_OK;
Adam Langley71d8a082014-12-13 16:28:18 -0800454 }
Adam Langley95c29f32014-06-20 12:00:00 -0700455
David Benjamin0d56f882015-12-19 17:05:56 -0500456 ssl->init_num = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800457 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700458
Adam Langley71d8a082014-12-13 16:28:18 -0800459 case SSL3_ST_CW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500460 ssl->rwstate = SSL_WRITING;
461 if (BIO_flush(ssl->wbio) <= 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800462 ret = -1;
463 goto end;
464 }
David Benjamin0d56f882015-12-19 17:05:56 -0500465 ssl->rwstate = SSL_NOTHING;
466 ssl->state = ssl->s3->tmp.next_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800467 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley71d8a082014-12-13 16:28:18 -0800469 case SSL_ST_OK:
470 /* clean a few things up */
David Benjamin0d56f882015-12-19 17:05:56 -0500471 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langley71d8a082014-12-13 16:28:18 -0800473 /* Remove write buffering now. */
David Benjamin0d56f882015-12-19 17:05:56 -0500474 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700475
David Benjamin0d56f882015-12-19 17:05:56 -0500476 ssl->init_num = 0;
477 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700478
David Benjamin0d56f882015-12-19 17:05:56 -0500479 ssl_update_cache(ssl, SSL_SESS_CACHE_CLIENT);
Adam Langley95c29f32014-06-20 12:00:00 -0700480
Adam Langley71d8a082014-12-13 16:28:18 -0800481 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin6eb000d2015-02-11 01:17:41 -0500483 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500484 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
David Benjamin6eb000d2015-02-11 01:17:41 -0500485 }
Adam Langley95c29f32014-06-20 12:00:00 -0700486
Adam Langley71d8a082014-12-13 16:28:18 -0800487 /* done with handshaking */
David Benjamin0d56f882015-12-19 17:05:56 -0500488 ssl->d1->handshake_read_seq = 0;
489 ssl->d1->next_handshake_write_seq = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800490 goto end;
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langley71d8a082014-12-13 16:28:18 -0800492 default:
David Benjamin3570d732015-06-29 00:28:17 -0400493 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley71d8a082014-12-13 16:28:18 -0800494 ret = -1;
495 goto end;
496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497
Adam Langley71d8a082014-12-13 16:28:18 -0800498 /* did we do anything? */
David Benjamin0d56f882015-12-19 17:05:56 -0500499 if (!ssl->s3->tmp.reuse_message && !skip) {
500 if ((cb != NULL) && (ssl->state != state)) {
501 new_state = ssl->state;
502 ssl->state = state;
503 cb(ssl, SSL_CB_CONNECT_LOOP, 1);
504 ssl->state = new_state;
Adam Langley71d8a082014-12-13 16:28:18 -0800505 }
506 }
507 skip = 0;
508 }
Adam Langley95c29f32014-06-20 12:00:00 -0700509
Adam Langley95c29f32014-06-20 12:00:00 -0700510end:
David Benjamin0d56f882015-12-19 17:05:56 -0500511 ssl->in_handshake--;
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjamin2755a3e2015-04-22 16:17:58 -0400513 BUF_MEM_free(buf);
Adam Langley71d8a082014-12-13 16:28:18 -0800514 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500515 cb(ssl, SSL_CB_CONNECT_EXIT, ret);
Adam Langley71d8a082014-12-13 16:28:18 -0800516 }
517 return ret;
518}
Adam Langley95c29f32014-06-20 12:00:00 -0700519
David Benjamin0d56f882015-12-19 17:05:56 -0500520static int dtls1_get_hello_verify(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800521 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
David Benjamin0d56f882015-12-19 17:05:56 -0500526 n = ssl->method->ssl_get_message(
527 ssl, DTLS1_ST_CR_HELLO_VERIFY_REQUEST_A,
528 DTLS1_ST_CR_HELLO_VERIFY_REQUEST_B, -1,
Adam Langley71d8a082014-12-13 16:28:18 -0800529 /* 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
David Benjamin0d56f882015-12-19 17:05:56 -0500536 if (ssl->s3->tmp.message_type != DTLS1_MT_HELLO_VERIFY_REQUEST) {
537 ssl->d1->send_cookie = 0;
538 ssl->s3->tmp.reuse_message = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800539 return 1;
540 }
David Benjamin51e32832014-08-13 15:13:57 -0400541
David Benjamin0d56f882015-12-19 17:05:56 -0500542 CBS_init(&hello_verify_request, ssl->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;
David Benjamin3570d732015-06-29 00:28:17 -0400548 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800549 goto f_err;
550 }
Adam Langley95c29f32014-06-20 12:00:00 -0700551
David Benjamin0d56f882015-12-19 17:05:56 -0500552 if (CBS_len(&cookie) > sizeof(ssl->d1->cookie)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800553 al = SSL_AD_ILLEGAL_PARAMETER;
554 goto f_err;
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
David Benjamin0d56f882015-12-19 17:05:56 -0500557 memcpy(ssl->d1->cookie, CBS_data(&cookie), CBS_len(&cookie));
558 ssl->d1->cookie_len = CBS_len(&cookie);
Adam Langley71d8a082014-12-13 16:28:18 -0800559
David Benjamin0d56f882015-12-19 17:05:56 -0500560 ssl->d1->send_cookie = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800561 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700562
563f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500564 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800565 return -1;
566}