blob: 5c9624a3cb3892a860bdba89e1932f172cdd8b98 [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>
119
120#include <openssl/bn.h>
121#include <openssl/buf.h>
122#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400123#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700124#include <openssl/evp.h>
125#include <openssl/md5.h>
126#include <openssl/obj.h>
127#include <openssl/rand.h>
128#include <openssl/x509.h>
129
David Benjamin2ee94aa2015-04-07 22:38:30 -0400130#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700131
Adam Langley24819752014-12-15 18:42:07 -0800132
David Benjamin0d56f882015-12-19 17:05:56 -0500133int dtls1_accept(SSL *ssl) {
Adam Langley24819752014-12-15 18:42:07 -0800134 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400135 void (*cb)(const SSL *ssl, int type, int value) = NULL;
David Benjamin107db582015-04-08 00:41:59 -0400136 uint32_t alg_a;
Adam Langley24819752014-12-15 18:42:07 -0800137 int ret = -1;
138 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700139
David Benjamin0d56f882015-12-19 17:05:56 -0500140 assert(ssl->handshake_func == dtls1_accept);
141 assert(ssl->server);
142 assert(SSL_IS_DTLS(ssl));
David Benjaminbeb47022014-11-30 02:58:52 -0500143
Adam Langley24819752014-12-15 18:42:07 -0800144 ERR_clear_error();
145 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700146
David Benjamin0d56f882015-12-19 17:05:56 -0500147 if (ssl->info_callback != NULL) {
148 cb = ssl->info_callback;
149 } else if (ssl->ctx->info_callback != NULL) {
150 cb = ssl->ctx->info_callback;
Adam Langley24819752014-12-15 18:42:07 -0800151 }
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjamin0d56f882015-12-19 17:05:56 -0500153 ssl->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700154
Adam Langley24819752014-12-15 18:42:07 -0800155 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500156 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700157
David Benjamin0d56f882015-12-19 17:05:56 -0500158 switch (ssl->state) {
Adam Langley24819752014-12-15 18:42:07 -0800159 case SSL_ST_ACCEPT:
Adam Langley24819752014-12-15 18:42:07 -0800160 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500161 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley24819752014-12-15 18:42:07 -0800162 }
Adam Langley95c29f32014-06-20 12:00:00 -0700163
David Benjamin0d56f882015-12-19 17:05:56 -0500164 if (ssl->init_buf == NULL) {
Adam Langley24819752014-12-15 18:42:07 -0800165 buf = BUF_MEM_new();
166 if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
167 ret = -1;
168 goto end;
169 }
David Benjamin0d56f882015-12-19 17:05:56 -0500170 ssl->init_buf = buf;
Adam Langley24819752014-12-15 18:42:07 -0800171 buf = NULL;
172 }
Adam Langley95c29f32014-06-20 12:00:00 -0700173
David Benjamin0d56f882015-12-19 17:05:56 -0500174 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin0d56f882015-12-19 17:05:56 -0500176 if (!ssl_init_wbio_buffer(ssl, 1)) {
David Benjamind23d5a52015-05-15 21:48:48 -0400177 ret = -1;
Adam Langley24819752014-12-15 18:42:07 -0800178 goto end;
179 }
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjamin0d56f882015-12-19 17:05:56 -0500181 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400182 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley24819752014-12-15 18:42:07 -0800183 ret = -1;
184 goto end;
185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
David Benjamin0d56f882015-12-19 17:05:56 -0500187 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800188 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley24819752014-12-15 18:42:07 -0800190 case SSL3_ST_SR_CLNT_HELLO_A:
191 case SSL3_ST_SR_CLNT_HELLO_B:
192 case SSL3_ST_SR_CLNT_HELLO_C:
193 case SSL3_ST_SR_CLNT_HELLO_D:
David Benjamin0d56f882015-12-19 17:05:56 -0500194 ssl->shutdown = 0;
195 ret = ssl3_get_client_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800196 if (ret <= 0) {
197 goto end;
198 }
David Benjamin0d56f882015-12-19 17:05:56 -0500199 dtls1_stop_timer(ssl);
200 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
201 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800202 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langley24819752014-12-15 18:42:07 -0800204 case SSL3_ST_SW_SRVR_HELLO_A:
205 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500206 dtls1_start_timer(ssl);
207 ret = ssl3_send_server_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800208 if (ret <= 0) {
209 goto end;
210 }
Adam Langley95c29f32014-06-20 12:00:00 -0700211
David Benjamin0d56f882015-12-19 17:05:56 -0500212 if (ssl->hit) {
213 if (ssl->tlsext_ticket_expected) {
214 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800215 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500216 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800217 }
218 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500219 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langley24819752014-12-15 18:42:07 -0800220 }
David Benjamin0d56f882015-12-19 17:05:56 -0500221 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800222 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700223
Adam Langley24819752014-12-15 18:42:07 -0800224 case SSL3_ST_SW_CERT_A:
225 case SSL3_ST_SW_CERT_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500226 if (ssl_cipher_has_server_public_key(ssl->s3->tmp.new_cipher)) {
227 dtls1_start_timer(ssl);
228 ret = ssl3_send_server_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800229 if (ret <= 0) {
230 goto end;
231 }
David Benjamin0d56f882015-12-19 17:05:56 -0500232 if (ssl->s3->tmp.certificate_status_expected) {
233 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langley24819752014-12-15 18:42:07 -0800234 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500235 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800236 }
237 } else {
238 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500239 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800240 }
David Benjamin0d56f882015-12-19 17:05:56 -0500241 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800242 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700243
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100244 case SSL3_ST_SW_CERT_STATUS_A:
245 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500246 ret = ssl3_send_certificate_status(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100247 if (ret <= 0) {
248 goto end;
249 }
David Benjamin0d56f882015-12-19 17:05:56 -0500250 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
251 ssl->init_num = 0;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100252 break;
253
Adam Langley24819752014-12-15 18:42:07 -0800254 case SSL3_ST_SW_KEY_EXCH_A:
255 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700256 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500257 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley24819752014-12-15 18:42:07 -0800259 /* Send a ServerKeyExchange message if:
260 * - The key exchange is ephemeral or anonymous
261 * Diffie-Hellman.
262 * - There is a PSK identity hint.
263 *
264 * TODO(davidben): This logic is currently duplicated
265 * in s3_srvr.c. Fix this. In the meantime, keep them
266 * in sync. */
David Benjamin0d56f882015-12-19 17:05:56 -0500267 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
268 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
269 dtls1_start_timer(ssl);
270 ret = ssl3_send_server_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800271 if (ret <= 0) {
272 goto end;
273 }
274 } else {
275 skip = 1;
276 }
Adam Langley95c29f32014-06-20 12:00:00 -0700277
David Benjamin0d56f882015-12-19 17:05:56 -0500278 ssl->state = SSL3_ST_SW_CERT_REQ_A;
279 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800280 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley24819752014-12-15 18:42:07 -0800282 case SSL3_ST_SW_CERT_REQ_A:
283 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500284 if (ssl->s3->tmp.cert_request) {
285 dtls1_start_timer(ssl);
286 ret = ssl3_send_certificate_request(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800287 if (ret <= 0) {
288 goto end;
289 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400290 } else {
291 skip = 1;
Adam Langley24819752014-12-15 18:42:07 -0800292 }
David Benjamin0d56f882015-12-19 17:05:56 -0500293 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
294 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800295 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley24819752014-12-15 18:42:07 -0800297 case SSL3_ST_SW_SRVR_DONE_A:
298 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500299 dtls1_start_timer(ssl);
300 ret = ssl3_send_server_done(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800301 if (ret <= 0) {
302 goto end;
303 }
David Benjamin0d56f882015-12-19 17:05:56 -0500304 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
305 ssl->state = SSL3_ST_SW_FLUSH;
306 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800307 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley24819752014-12-15 18:42:07 -0800309 case SSL3_ST_SW_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500310 ssl->rwstate = SSL_WRITING;
311 if (BIO_flush(ssl->wbio) <= 0) {
Adam Langley24819752014-12-15 18:42:07 -0800312 ret = -1;
313 goto end;
314 }
David Benjamin0d56f882015-12-19 17:05:56 -0500315 ssl->rwstate = SSL_NOTHING;
316 ssl->state = ssl->s3->tmp.next_state;
Adam Langley24819752014-12-15 18:42:07 -0800317 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley24819752014-12-15 18:42:07 -0800319 case SSL3_ST_SR_CERT_A:
320 case SSL3_ST_SR_CERT_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500321 if (ssl->s3->tmp.cert_request) {
322 ret = ssl3_get_client_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800323 if (ret <= 0) {
324 goto end;
325 }
326 }
David Benjamin0d56f882015-12-19 17:05:56 -0500327 ssl->init_num = 0;
328 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800329 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langley24819752014-12-15 18:42:07 -0800331 case SSL3_ST_SR_KEY_EXCH_A:
332 case SSL3_ST_SR_KEY_EXCH_B:
nagendra modadugu3398dbf2015-08-07 14:07:52 -0700333 case SSL3_ST_SR_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500334 ret = ssl3_get_client_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800335 if (ret <= 0) {
336 goto end;
337 }
David Benjamin0d56f882015-12-19 17:05:56 -0500338 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
339 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800340 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langley24819752014-12-15 18:42:07 -0800342 case SSL3_ST_SR_CERT_VRFY_A:
343 case SSL3_ST_SR_CERT_VRFY_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500344 ret = ssl3_get_cert_verify(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800345 if (ret <= 0) {
346 goto end;
347 }
David Benjamin0d56f882015-12-19 17:05:56 -0500348 ssl->state = SSL3_ST_SR_CHANGE;
349 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800350 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700351
David Benjamina41280d2015-11-26 02:16:49 -0500352 case SSL3_ST_SR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500353 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500354 if (ret <= 0) {
355 goto end;
356 }
357
David Benjamin57997da2015-12-25 15:48:39 -0500358 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
David Benjamina41280d2015-11-26 02:16:49 -0500359 ret = -1;
360 goto end;
361 }
362
David Benjamin0d56f882015-12-19 17:05:56 -0500363 ssl->state = SSL3_ST_SR_FINISHED_A;
David Benjamina41280d2015-11-26 02:16:49 -0500364 break;
365
Adam Langley24819752014-12-15 18:42:07 -0800366 case SSL3_ST_SR_FINISHED_A:
367 case SSL3_ST_SR_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500368 ret = ssl3_get_finished(ssl, SSL3_ST_SR_FINISHED_A,
369 SSL3_ST_SR_FINISHED_B);
Adam Langley24819752014-12-15 18:42:07 -0800370 if (ret <= 0) {
371 goto end;
372 }
David Benjamin0d56f882015-12-19 17:05:56 -0500373 dtls1_stop_timer(ssl);
374 if (ssl->hit) {
375 ssl->state = SSL_ST_OK;
376 } else if (ssl->tlsext_ticket_expected) {
377 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800378 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500379 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800380 }
David Benjamin0d56f882015-12-19 17:05:56 -0500381 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800382 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700383
Adam Langley24819752014-12-15 18:42:07 -0800384 case SSL3_ST_SW_SESSION_TICKET_A:
385 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500386 ret = ssl3_send_new_session_ticket(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800387 if (ret <= 0) {
388 goto end;
389 }
David Benjamin0d56f882015-12-19 17:05:56 -0500390 ssl->state = SSL3_ST_SW_CHANGE_A;
391 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800392 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langley24819752014-12-15 18:42:07 -0800394 case SSL3_ST_SW_CHANGE_A:
395 case SSL3_ST_SW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500396 ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
Adam Langley24819752014-12-15 18:42:07 -0800397 SSL3_ST_SW_CHANGE_B);
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langley24819752014-12-15 18:42:07 -0800399 if (ret <= 0) {
400 goto end;
401 }
Adam Langley95c29f32014-06-20 12:00:00 -0700402
David Benjamin0d56f882015-12-19 17:05:56 -0500403 ssl->state = SSL3_ST_SW_FINISHED_A;
404 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
David Benjamin57997da2015-12-25 15:48:39 -0500406 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langley24819752014-12-15 18:42:07 -0800407 ret = -1;
408 goto end;
409 }
Adam Langley24819752014-12-15 18:42:07 -0800410 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langley24819752014-12-15 18:42:07 -0800412 case SSL3_ST_SW_FINISHED_A:
413 case SSL3_ST_SW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500414 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500415 SSL3_ST_SW_FINISHED_B);
Adam Langley24819752014-12-15 18:42:07 -0800416 if (ret <= 0) {
417 goto end;
418 }
David Benjamin0d56f882015-12-19 17:05:56 -0500419 ssl->state = SSL3_ST_SW_FLUSH;
420 if (ssl->hit) {
421 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langley24819752014-12-15 18:42:07 -0800422 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500423 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley24819752014-12-15 18:42:07 -0800424 }
David Benjamin0d56f882015-12-19 17:05:56 -0500425 ssl->init_num = 0;
Adam Langley24819752014-12-15 18:42:07 -0800426 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langley24819752014-12-15 18:42:07 -0800428 case SSL_ST_OK:
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700430
Adam Langley24819752014-12-15 18:42:07 -0800431 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500432 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700433
David Benjamin0d56f882015-12-19 17:05:56 -0500434 ssl->init_num = 0;
435 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700436
David Benjamin0d56f882015-12-19 17:05:56 -0500437 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700438
David Benjamind23d5a52015-05-15 21:48:48 -0400439 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500440 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley24819752014-12-15 18:42:07 -0800441 }
Adam Langley95c29f32014-06-20 12:00:00 -0700442
Adam Langley24819752014-12-15 18:42:07 -0800443 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley24819752014-12-15 18:42:07 -0800445 /* done handshaking, next message is client hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500446 ssl->d1->handshake_read_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800447 /* next message is server hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500448 ssl->d1->handshake_write_seq = 0;
449 ssl->d1->next_handshake_write_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800450 goto end;
451
452 default:
David Benjamin3570d732015-06-29 00:28:17 -0400453 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley24819752014-12-15 18:42:07 -0800454 ret = -1;
455 goto end;
456 }
457
David Benjamin0d56f882015-12-19 17:05:56 -0500458 if (!ssl->s3->tmp.reuse_message && !skip) {
459 if (cb != NULL && ssl->state != state) {
460 new_state = ssl->state;
461 ssl->state = state;
462 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
463 ssl->state = new_state;
Adam Langley24819752014-12-15 18:42:07 -0800464 }
465 }
466 skip = 0;
467 }
468
Adam Langley95c29f32014-06-20 12:00:00 -0700469end:
David Benjamin0d56f882015-12-19 17:05:56 -0500470 ssl->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400471 BUF_MEM_free(buf);
Adam Langley24819752014-12-15 18:42:07 -0800472 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500473 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langley24819752014-12-15 18:42:07 -0800474 }
475 return ret;
476}