blob: d4212fbbbf5f28914c46448b4d3774ab72e1d4d0 [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
119#include <openssl/bn.h>
120#include <openssl/buf.h>
121#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400122#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700123#include <openssl/evp.h>
124#include <openssl/md5.h>
125#include <openssl/obj.h>
126#include <openssl/rand.h>
127#include <openssl/x509.h>
128
David Benjamin2ee94aa2015-04-07 22:38:30 -0400129#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700130
Adam Langley24819752014-12-15 18:42:07 -0800131
David Benjamin0d56f882015-12-19 17:05:56 -0500132int dtls1_accept(SSL *ssl) {
Adam Langley24819752014-12-15 18:42:07 -0800133 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400134 void (*cb)(const SSL *ssl, int type, int value) = NULL;
David Benjamin107db582015-04-08 00:41:59 -0400135 uint32_t alg_a;
Adam Langley24819752014-12-15 18:42:07 -0800136 int ret = -1;
137 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700138
David Benjamin0d56f882015-12-19 17:05:56 -0500139 assert(ssl->handshake_func == dtls1_accept);
140 assert(ssl->server);
141 assert(SSL_IS_DTLS(ssl));
David Benjaminbeb47022014-11-30 02:58:52 -0500142
Adam Langley24819752014-12-15 18:42:07 -0800143 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700144
David Benjamin0d56f882015-12-19 17:05:56 -0500145 if (ssl->info_callback != NULL) {
146 cb = ssl->info_callback;
147 } else if (ssl->ctx->info_callback != NULL) {
148 cb = ssl->ctx->info_callback;
Adam Langley24819752014-12-15 18:42:07 -0800149 }
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjamin0d56f882015-12-19 17:05:56 -0500151 ssl->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langley24819752014-12-15 18:42:07 -0800153 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500154 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700155
David Benjamin0d56f882015-12-19 17:05:56 -0500156 switch (ssl->state) {
Adam Langley24819752014-12-15 18:42:07 -0800157 case SSL_ST_ACCEPT:
Adam Langley24819752014-12-15 18:42:07 -0800158 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500159 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley24819752014-12-15 18:42:07 -0800160 }
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjamin0d56f882015-12-19 17:05:56 -0500162 if (ssl->init_buf == NULL) {
Adam Langley24819752014-12-15 18:42:07 -0800163 buf = BUF_MEM_new();
164 if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
165 ret = -1;
166 goto end;
167 }
David Benjamin0d56f882015-12-19 17:05:56 -0500168 ssl->init_buf = buf;
Adam Langley24819752014-12-15 18:42:07 -0800169 buf = NULL;
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamin0d56f882015-12-19 17:05:56 -0500172 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin0d56f882015-12-19 17:05:56 -0500174 if (!ssl_init_wbio_buffer(ssl, 1)) {
David Benjamind23d5a52015-05-15 21:48:48 -0400175 ret = -1;
Adam Langley24819752014-12-15 18:42:07 -0800176 goto end;
177 }
Adam Langley95c29f32014-06-20 12:00:00 -0700178
David Benjamin0d56f882015-12-19 17:05:56 -0500179 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400180 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley24819752014-12-15 18:42:07 -0800181 ret = -1;
182 goto end;
183 }
Adam Langley95c29f32014-06-20 12:00:00 -0700184
David Benjamin0d56f882015-12-19 17:05:56 -0500185 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800186 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langley24819752014-12-15 18:42:07 -0800188 case SSL3_ST_SR_CLNT_HELLO_A:
189 case SSL3_ST_SR_CLNT_HELLO_B:
190 case SSL3_ST_SR_CLNT_HELLO_C:
191 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500192 ssl->shutdown = 0;
193 ret = ssl3_get_client_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800194 if (ret <= 0) {
195 goto end;
196 }
David Benjamin0d56f882015-12-19 17:05:56 -0500197 dtls1_stop_timer(ssl);
198 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
199 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800200 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700201
Adam Langley24819752014-12-15 18:42:07 -0800202 case SSL3_ST_SW_SRVR_HELLO_A:
203 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500204 dtls1_start_timer(ssl);
205 ret = ssl3_send_server_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800206 if (ret <= 0) {
207 goto end;
208 }
Adam Langley95c29f32014-06-20 12:00:00 -0700209
David Benjamin0d56f882015-12-19 17:05:56 -0500210 if (ssl->hit) {
211 if (ssl->tlsext_ticket_expected) {
212 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800213 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500214 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800215 }
216 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500217 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langley24819752014-12-15 18:42:07 -0800218 }
David Benjamin0d56f882015-12-19 17:05:56 -0500219 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800220 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700221
Adam Langley24819752014-12-15 18:42:07 -0800222 case SSL3_ST_SW_CERT_A:
223 case SSL3_ST_SW_CERT_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500224 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
225 dtls1_start_timer(ssl);
226 ret = ssl3_send_server_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800227 if (ret <= 0) {
228 goto end;
229 }
David Benjamin0d56f882015-12-19 17:05:56 -0500230 if (ssl->s3->tmp.certificate_status_expected) {
231 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langley24819752014-12-15 18:42:07 -0800232 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500233 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800234 }
235 } else {
236 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500237 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800238 }
David Benjamin0d56f882015-12-19 17:05:56 -0500239 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800240 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100242 case SSL3_ST_SW_CERT_STATUS_A:
243 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500244 ret = ssl3_send_certificate_status(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100245 if (ret <= 0) {
246 goto end;
247 }
David Benjamin0d56f882015-12-19 17:05:56 -0500248 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
249 ssl->init_num = 0;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100250 break;
251
Adam Langley24819752014-12-15 18:42:07 -0800252 case SSL3_ST_SW_KEY_EXCH_A:
253 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700254 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500255 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700256
Adam Langley24819752014-12-15 18:42:07 -0800257 /* Send a ServerKeyExchange message if:
258 * - The key exchange is ephemeral or anonymous
259 * Diffie-Hellman.
260 * - There is a PSK identity hint.
261 *
262 * TODO(davidben): This logic is currently duplicated
263 * in s3_srvr.c. Fix this. In the meantime, keep them
264 * in sync. */
David Benjamin0d56f882015-12-19 17:05:56 -0500265 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
266 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
267 dtls1_start_timer(ssl);
268 ret = ssl3_send_server_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800269 if (ret <= 0) {
270 goto end;
271 }
272 } else {
273 skip = 1;
274 }
Adam Langley95c29f32014-06-20 12:00:00 -0700275
David Benjamin0d56f882015-12-19 17:05:56 -0500276 ssl->state = SSL3_ST_SW_CERT_REQ_A;
277 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800278 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langley24819752014-12-15 18:42:07 -0800280 case SSL3_ST_SW_CERT_REQ_A:
281 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500282 if (ssl->s3->tmp.cert_request) {
283 dtls1_start_timer(ssl);
284 ret = ssl3_send_certificate_request(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800285 if (ret <= 0) {
286 goto end;
287 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400288 } else {
289 skip = 1;
Adam Langley24819752014-12-15 18:42:07 -0800290 }
David Benjamin0d56f882015-12-19 17:05:56 -0500291 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
292 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800293 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langley24819752014-12-15 18:42:07 -0800295 case SSL3_ST_SW_SRVR_DONE_A:
296 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500297 dtls1_start_timer(ssl);
298 ret = ssl3_send_server_done(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800299 if (ret <= 0) {
300 goto end;
301 }
David Benjamin0d56f882015-12-19 17:05:56 -0500302 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
303 ssl->state = SSL3_ST_SW_FLUSH;
304 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800305 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley24819752014-12-15 18:42:07 -0800307 case SSL3_ST_SW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500308 ssl->rwstate = SSL_WRITING;
309 if (BIO_flush(ssl->wbio) <= 0) {
Adam Langley24819752014-12-15 18:42:07 -0800310 ret = -1;
311 goto end;
312 }
David Benjamin0d56f882015-12-19 17:05:56 -0500313 ssl->rwstate = SSL_NOTHING;
314 ssl->state = ssl->s3->tmp.next_state;
Adam Langley24819752014-12-15 18:42:07 -0800315 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700316
Adam Langley24819752014-12-15 18:42:07 -0800317 case SSL3_ST_SR_CERT_A:
318 case SSL3_ST_SR_CERT_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500319 if (ssl->s3->tmp.cert_request) {
320 ret = ssl3_get_client_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800321 if (ret <= 0) {
322 goto end;
323 }
324 }
David Benjamin0d56f882015-12-19 17:05:56 -0500325 ssl->init_num = 0;
326 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800327 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langley24819752014-12-15 18:42:07 -0800329 case SSL3_ST_SR_KEY_EXCH_A:
330 case SSL3_ST_SR_KEY_EXCH_B:
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700331 case SSL3_ST_SR_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500332 ret = ssl3_get_client_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800333 if (ret <= 0) {
334 goto end;
335 }
David Benjamin0d56f882015-12-19 17:05:56 -0500336 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
337 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800338 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley24819752014-12-15 18:42:07 -0800340 case SSL3_ST_SR_CERT_VRFY_A:
341 case SSL3_ST_SR_CERT_VRFY_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ret = ssl3_get_cert_verify(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800343 if (ret <= 0) {
344 goto end;
345 }
David Benjamin0d56f882015-12-19 17:05:56 -0500346 ssl->state = SSL3_ST_SR_CHANGE;
347 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800348 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamina41280d2015-11-26 02:16:49 -0500350 case SSL3_ST_SR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500351 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500352 if (ret <= 0) {
353 goto end;
354 }
355
David Benjamin57997da2015-12-25 15:48:39 -0500356 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
David Benjamina41280d2015-11-26 02:16:49 -0500357 ret = -1;
358 goto end;
359 }
360
David Benjamin0d56f882015-12-19 17:05:56 -0500361 ssl->state = SSL3_ST_SR_FINISHED_A;
David Benjamina41280d2015-11-26 02:16:49 -0500362 break;
363
Adam Langley24819752014-12-15 18:42:07 -0800364 case SSL3_ST_SR_FINISHED_A:
365 case SSL3_ST_SR_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500366 ret = ssl3_get_finished(ssl, SSL3_ST_SR_FINISHED_A,
367 SSL3_ST_SR_FINISHED_B);
Adam Langley24819752014-12-15 18:42:07 -0800368 if (ret <= 0) {
369 goto end;
370 }
David Benjamin0d56f882015-12-19 17:05:56 -0500371 dtls1_stop_timer(ssl);
372 if (ssl->hit) {
373 ssl->state = SSL_ST_OK;
374 } else if (ssl->tlsext_ticket_expected) {
375 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800376 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500377 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800378 }
David Benjamin0d56f882015-12-19 17:05:56 -0500379 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800380 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley24819752014-12-15 18:42:07 -0800382 case SSL3_ST_SW_SESSION_TICKET_A:
383 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500384 ret = ssl3_send_new_session_ticket(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800385 if (ret <= 0) {
386 goto end;
387 }
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ssl->state = SSL3_ST_SW_CHANGE_A;
389 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800390 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley24819752014-12-15 18:42:07 -0800392 case SSL3_ST_SW_CHANGE_A:
393 case SSL3_ST_SW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500394 ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
Adam Langley24819752014-12-15 18:42:07 -0800395 SSL3_ST_SW_CHANGE_B);
Adam Langley95c29f32014-06-20 12:00:00 -0700396
Adam Langley24819752014-12-15 18:42:07 -0800397 if (ret <= 0) {
398 goto end;
399 }
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjamin0d56f882015-12-19 17:05:56 -0500401 ssl->state = SSL3_ST_SW_FINISHED_A;
402 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
David Benjamin57997da2015-12-25 15:48:39 -0500404 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langley24819752014-12-15 18:42:07 -0800405 ret = -1;
406 goto end;
407 }
Adam Langley24819752014-12-15 18:42:07 -0800408 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langley24819752014-12-15 18:42:07 -0800410 case SSL3_ST_SW_FINISHED_A:
411 case SSL3_ST_SW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500412 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500413 SSL3_ST_SW_FINISHED_B);
Adam Langley24819752014-12-15 18:42:07 -0800414 if (ret <= 0) {
415 goto end;
416 }
David Benjamin0d56f882015-12-19 17:05:56 -0500417 ssl->state = SSL3_ST_SW_FLUSH;
418 if (ssl->hit) {
419 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langley24819752014-12-15 18:42:07 -0800420 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500421 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley24819752014-12-15 18:42:07 -0800422 }
David Benjamin0d56f882015-12-19 17:05:56 -0500423 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800424 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700425
Adam Langley24819752014-12-15 18:42:07 -0800426 case SSL_ST_OK:
David Benjamin0d56f882015-12-19 17:05:56 -0500427 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley24819752014-12-15 18:42:07 -0800429 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500430 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700431
David Benjamin0d56f882015-12-19 17:05:56 -0500432 ssl->init_num = 0;
433 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700434
David Benjamin0d56f882015-12-19 17:05:56 -0500435 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700436
David Benjamind23d5a52015-05-15 21:48:48 -0400437 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500438 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley24819752014-12-15 18:42:07 -0800439 }
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langley24819752014-12-15 18:42:07 -0800441 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langley24819752014-12-15 18:42:07 -0800443 /* done handshaking, next message is client hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500444 ssl->d1->handshake_read_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800445 /* next message is server hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ssl->d1->handshake_write_seq = 0;
447 ssl->d1->next_handshake_write_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800448 goto end;
449
450 default:
David Benjamin3570d732015-06-29 00:28:17 -0400451 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley24819752014-12-15 18:42:07 -0800452 ret = -1;
453 goto end;
454 }
455
David Benjamin0d56f882015-12-19 17:05:56 -0500456 if (!ssl->s3->tmp.reuse_message && !skip) {
457 if (cb != NULL && ssl->state != state) {
458 new_state = ssl->state;
459 ssl->state = state;
460 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
461 ssl->state = new_state;
Adam Langley24819752014-12-15 18:42:07 -0800462 }
463 }
464 skip = 0;
465 }
466
Adam Langley95c29f32014-06-20 12:00:00 -0700467end:
David Benjamin0d56f882015-12-19 17:05:56 -0500468 ssl->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400469 BUF_MEM_free(buf);
Adam Langley24819752014-12-15 18:42:07 -0800470 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500471 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langley24819752014-12-15 18:42:07 -0800472 }
473 return ret;
474}