blob: 9913e735e41ebf76d0ade76a80a7aa41ec0a8065 [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>
Adam Langley95c29f32014-06-20 12:00:00 -0700125#include <openssl/rand.h>
126#include <openssl/x509.h>
127
David Benjamin2ee94aa2015-04-07 22:38:30 -0400128#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700129
Adam Langley24819752014-12-15 18:42:07 -0800130
David Benjamin0d56f882015-12-19 17:05:56 -0500131int dtls1_accept(SSL *ssl) {
Adam Langley24819752014-12-15 18:42:07 -0800132 BUF_MEM *buf = NULL;
David Benjamin82170242015-10-17 22:51:17 -0400133 void (*cb)(const SSL *ssl, int type, int value) = NULL;
David Benjamin107db582015-04-08 00:41:59 -0400134 uint32_t alg_a;
Adam Langley24819752014-12-15 18:42:07 -0800135 int ret = -1;
136 int new_state, state, skip = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700137
David Benjamin0d56f882015-12-19 17:05:56 -0500138 assert(ssl->handshake_func == dtls1_accept);
139 assert(ssl->server);
140 assert(SSL_IS_DTLS(ssl));
David Benjaminbeb47022014-11-30 02:58:52 -0500141
Adam Langley24819752014-12-15 18:42:07 -0800142 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjamin0d56f882015-12-19 17:05:56 -0500144 if (ssl->info_callback != NULL) {
145 cb = ssl->info_callback;
146 } else if (ssl->ctx->info_callback != NULL) {
147 cb = ssl->ctx->info_callback;
Adam Langley24819752014-12-15 18:42:07 -0800148 }
Adam Langley95c29f32014-06-20 12:00:00 -0700149
Adam Langley24819752014-12-15 18:42:07 -0800150 for (;;) {
David Benjamin0d56f882015-12-19 17:05:56 -0500151 state = ssl->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjamin0d56f882015-12-19 17:05:56 -0500153 switch (ssl->state) {
Adam Langley24819752014-12-15 18:42:07 -0800154 case SSL_ST_ACCEPT:
Adam Langley24819752014-12-15 18:42:07 -0800155 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500156 cb(ssl, SSL_CB_HANDSHAKE_START, 1);
Adam Langley24819752014-12-15 18:42:07 -0800157 }
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjamin0d56f882015-12-19 17:05:56 -0500159 if (ssl->init_buf == NULL) {
Adam Langley24819752014-12-15 18:42:07 -0800160 buf = BUF_MEM_new();
David Benjamina6338be2016-05-13 18:12:19 -0400161 if (buf == NULL || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
Adam Langley24819752014-12-15 18:42:07 -0800162 ret = -1;
163 goto end;
164 }
David Benjamin0d56f882015-12-19 17:05:56 -0500165 ssl->init_buf = buf;
Adam Langley24819752014-12-15 18:42:07 -0800166 buf = NULL;
167 }
Adam Langley95c29f32014-06-20 12:00:00 -0700168
David Benjamin0d56f882015-12-19 17:05:56 -0500169 ssl->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700170
David Benjaminb095f0f2016-05-05 21:50:24 -0400171 if (!ssl_init_wbio_buffer(ssl)) {
David Benjamind23d5a52015-05-15 21:48:48 -0400172 ret = -1;
Adam Langley24819752014-12-15 18:42:07 -0800173 goto end;
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
David Benjamin0d56f882015-12-19 17:05:56 -0500176 if (!ssl3_init_handshake_buffer(ssl)) {
David Benjamin3570d732015-06-29 00:28:17 -0400177 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley24819752014-12-15 18:42:07 -0800178 ret = -1;
179 goto end;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjamin0d56f882015-12-19 17:05:56 -0500182 ssl->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800183 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700184
Adam Langley24819752014-12-15 18:42:07 -0800185 case SSL3_ST_SR_CLNT_HELLO_A:
186 case SSL3_ST_SR_CLNT_HELLO_B:
187 case SSL3_ST_SR_CLNT_HELLO_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500188 ret = ssl3_get_client_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800189 if (ret <= 0) {
190 goto end;
191 }
David Benjamin0d56f882015-12-19 17:05:56 -0500192 dtls1_stop_timer(ssl);
193 ssl->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800194 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley24819752014-12-15 18:42:07 -0800196 case SSL3_ST_SW_SRVR_HELLO_A:
197 case SSL3_ST_SW_SRVR_HELLO_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500198 dtls1_start_timer(ssl);
199 ret = ssl3_send_server_hello(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800200 if (ret <= 0) {
201 goto end;
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
David Benjamin0d56f882015-12-19 17:05:56 -0500204 if (ssl->hit) {
205 if (ssl->tlsext_ticket_expected) {
206 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800207 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500208 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800209 }
210 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500211 ssl->state = SSL3_ST_SW_CERT_A;
Adam Langley24819752014-12-15 18:42:07 -0800212 }
Adam Langley24819752014-12-15 18:42:07 -0800213 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700214
Adam Langley24819752014-12-15 18:42:07 -0800215 case SSL3_ST_SW_CERT_A:
216 case SSL3_ST_SW_CERT_B:
David Benjaminc032dfa2016-05-12 14:54:57 -0400217 if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500218 dtls1_start_timer(ssl);
219 ret = ssl3_send_server_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800220 if (ret <= 0) {
221 goto end;
222 }
David Benjamin0d56f882015-12-19 17:05:56 -0500223 if (ssl->s3->tmp.certificate_status_expected) {
224 ssl->state = SSL3_ST_SW_CERT_STATUS_A;
Adam Langley24819752014-12-15 18:42:07 -0800225 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500226 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800227 }
228 } else {
229 skip = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500230 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800231 }
Adam Langley24819752014-12-15 18:42:07 -0800232 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700233
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100234 case SSL3_ST_SW_CERT_STATUS_A:
235 case SSL3_ST_SW_CERT_STATUS_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500236 ret = ssl3_send_certificate_status(ssl);
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100237 if (ret <= 0) {
238 goto end;
239 }
David Benjamin0d56f882015-12-19 17:05:56 -0500240 ssl->state = SSL3_ST_SW_KEY_EXCH_A;
Paul Lietaraeeff2c2015-08-12 11:47:11 +0100241 break;
242
Adam Langley24819752014-12-15 18:42:07 -0800243 case SSL3_ST_SW_KEY_EXCH_A:
244 case SSL3_ST_SW_KEY_EXCH_B:
nagendra modadugu601448a2015-07-24 09:31:31 -0700245 case SSL3_ST_SW_KEY_EXCH_C:
David Benjamin0d56f882015-12-19 17:05:56 -0500246 alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700247
Adam Langley24819752014-12-15 18:42:07 -0800248 /* Send a ServerKeyExchange message if:
249 * - The key exchange is ephemeral or anonymous
250 * Diffie-Hellman.
251 * - There is a PSK identity hint.
252 *
253 * TODO(davidben): This logic is currently duplicated
254 * in s3_srvr.c. Fix this. In the meantime, keep them
255 * in sync. */
David Benjamin0d56f882015-12-19 17:05:56 -0500256 if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) ||
257 ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) {
258 dtls1_start_timer(ssl);
259 ret = ssl3_send_server_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800260 if (ret <= 0) {
261 goto end;
262 }
263 } else {
264 skip = 1;
265 }
Adam Langley95c29f32014-06-20 12:00:00 -0700266
David Benjamin0d56f882015-12-19 17:05:56 -0500267 ssl->state = SSL3_ST_SW_CERT_REQ_A;
Adam Langley24819752014-12-15 18:42:07 -0800268 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700269
Adam Langley24819752014-12-15 18:42:07 -0800270 case SSL3_ST_SW_CERT_REQ_A:
271 case SSL3_ST_SW_CERT_REQ_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500272 if (ssl->s3->tmp.cert_request) {
273 dtls1_start_timer(ssl);
274 ret = ssl3_send_certificate_request(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800275 if (ret <= 0) {
276 goto end;
277 }
David Benjamin5c1ce292015-05-26 16:59:43 -0400278 } else {
279 skip = 1;
Adam Langley24819752014-12-15 18:42:07 -0800280 }
David Benjamin0d56f882015-12-19 17:05:56 -0500281 ssl->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langley24819752014-12-15 18:42:07 -0800282 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langley24819752014-12-15 18:42:07 -0800284 case SSL3_ST_SW_SRVR_DONE_A:
285 case SSL3_ST_SW_SRVR_DONE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500286 dtls1_start_timer(ssl);
287 ret = ssl3_send_server_done(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800288 if (ret <= 0) {
289 goto end;
290 }
David Benjamin0d56f882015-12-19 17:05:56 -0500291 ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
292 ssl->state = SSL3_ST_SW_FLUSH;
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_FLUSH:
David Benjamin0d56f882015-12-19 17:05:56 -0500296 if (BIO_flush(ssl->wbio) <= 0) {
David Benjamin4c5ddb82016-03-11 22:56:19 -0500297 ssl->rwstate = SSL_WRITING;
Adam Langley24819752014-12-15 18:42:07 -0800298 ret = -1;
299 goto end;
300 }
David Benjamin0d56f882015-12-19 17:05:56 -0500301 ssl->state = ssl->s3->tmp.next_state;
Adam Langley24819752014-12-15 18:42:07 -0800302 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langley24819752014-12-15 18:42:07 -0800304 case SSL3_ST_SR_CERT_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500305 if (ssl->s3->tmp.cert_request) {
306 ret = ssl3_get_client_certificate(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800307 if (ret <= 0) {
308 goto end;
309 }
310 }
David Benjamin0d56f882015-12-19 17:05:56 -0500311 ssl->state = SSL3_ST_SR_KEY_EXCH_A;
Adam Langley24819752014-12-15 18:42:07 -0800312 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700313
Adam Langley24819752014-12-15 18:42:07 -0800314 case SSL3_ST_SR_KEY_EXCH_A:
315 case SSL3_ST_SR_KEY_EXCH_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500316 ret = ssl3_get_client_key_exchange(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800317 if (ret <= 0) {
318 goto end;
319 }
David Benjamin0d56f882015-12-19 17:05:56 -0500320 ssl->state = SSL3_ST_SR_CERT_VRFY_A;
Adam Langley24819752014-12-15 18:42:07 -0800321 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700322
Adam Langley24819752014-12-15 18:42:07 -0800323 case SSL3_ST_SR_CERT_VRFY_A:
David Benjamin0d56f882015-12-19 17:05:56 -0500324 ret = ssl3_get_cert_verify(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800325 if (ret <= 0) {
326 goto end;
327 }
David Benjamin0d56f882015-12-19 17:05:56 -0500328 ssl->state = SSL3_ST_SR_CHANGE;
Adam Langley24819752014-12-15 18:42:07 -0800329 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700330
David Benjamina41280d2015-11-26 02:16:49 -0500331 case SSL3_ST_SR_CHANGE:
David Benjamin0d56f882015-12-19 17:05:56 -0500332 ret = ssl->method->ssl_read_change_cipher_spec(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500333 if (ret <= 0) {
334 goto end;
335 }
336
David Benjamin57997da2015-12-25 15:48:39 -0500337 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) {
David Benjamina41280d2015-11-26 02:16:49 -0500338 ret = -1;
339 goto end;
340 }
341
David Benjamin0d56f882015-12-19 17:05:56 -0500342 ssl->state = SSL3_ST_SR_FINISHED_A;
David Benjamina41280d2015-11-26 02:16:49 -0500343 break;
344
Adam Langley24819752014-12-15 18:42:07 -0800345 case SSL3_ST_SR_FINISHED_A:
David Benjamin1e6d6df2016-05-13 18:28:17 -0400346 ret = ssl3_get_finished(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800347 if (ret <= 0) {
348 goto end;
349 }
David Benjamin0d56f882015-12-19 17:05:56 -0500350 dtls1_stop_timer(ssl);
351 if (ssl->hit) {
352 ssl->state = SSL_ST_OK;
353 } else if (ssl->tlsext_ticket_expected) {
354 ssl->state = SSL3_ST_SW_SESSION_TICKET_A;
Adam Langley24819752014-12-15 18:42:07 -0800355 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500356 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800357 }
Adam Langley24819752014-12-15 18:42:07 -0800358 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley24819752014-12-15 18:42:07 -0800360 case SSL3_ST_SW_SESSION_TICKET_A:
361 case SSL3_ST_SW_SESSION_TICKET_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500362 ret = ssl3_send_new_session_ticket(ssl);
Adam Langley24819752014-12-15 18:42:07 -0800363 if (ret <= 0) {
364 goto end;
365 }
David Benjamin0d56f882015-12-19 17:05:56 -0500366 ssl->state = SSL3_ST_SW_CHANGE_A;
Adam Langley24819752014-12-15 18:42:07 -0800367 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langley24819752014-12-15 18:42:07 -0800369 case SSL3_ST_SW_CHANGE_A:
370 case SSL3_ST_SW_CHANGE_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500371 ret = dtls1_send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A,
Adam Langley24819752014-12-15 18:42:07 -0800372 SSL3_ST_SW_CHANGE_B);
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley24819752014-12-15 18:42:07 -0800374 if (ret <= 0) {
375 goto end;
376 }
Adam Langley95c29f32014-06-20 12:00:00 -0700377
David Benjamin0d56f882015-12-19 17:05:56 -0500378 ssl->state = SSL3_ST_SW_FINISHED_A;
Adam Langley95c29f32014-06-20 12:00:00 -0700379
David Benjamin57997da2015-12-25 15:48:39 -0500380 if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
Adam Langley24819752014-12-15 18:42:07 -0800381 ret = -1;
382 goto end;
383 }
Adam Langley24819752014-12-15 18:42:07 -0800384 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley24819752014-12-15 18:42:07 -0800386 case SSL3_ST_SW_FINISHED_A:
387 case SSL3_ST_SW_FINISHED_B:
David Benjamin0d56f882015-12-19 17:05:56 -0500388 ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A,
David Benjaminbaa12162015-12-29 19:13:58 -0500389 SSL3_ST_SW_FINISHED_B);
Adam Langley24819752014-12-15 18:42:07 -0800390 if (ret <= 0) {
391 goto end;
392 }
David Benjamin0d56f882015-12-19 17:05:56 -0500393 ssl->state = SSL3_ST_SW_FLUSH;
394 if (ssl->hit) {
395 ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE;
Adam Langley24819752014-12-15 18:42:07 -0800396 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500397 ssl->s3->tmp.next_state = SSL_ST_OK;
Adam Langley24819752014-12-15 18:42:07 -0800398 }
Adam Langley24819752014-12-15 18:42:07 -0800399 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700400
Adam Langley24819752014-12-15 18:42:07 -0800401 case SSL_ST_OK:
David Benjamin0d56f882015-12-19 17:05:56 -0500402 ssl3_cleanup_key_block(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langley24819752014-12-15 18:42:07 -0800404 /* remove buffering on output */
David Benjamin0d56f882015-12-19 17:05:56 -0500405 ssl_free_wbio_buffer(ssl);
Adam Langley95c29f32014-06-20 12:00:00 -0700406
David Benjaminfce37b02016-05-15 13:51:35 -0400407 /* |init_buf| cannot be released because post-handshake retransmit
408 * relies on that buffer being available as scratch space.
409 *
410 * TODO(davidben): Fix this. */
David Benjamin0d56f882015-12-19 17:05:56 -0500411 ssl->init_num = 0;
412 ssl->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
David Benjamin0d56f882015-12-19 17:05:56 -0500414 ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700415
David Benjamind23d5a52015-05-15 21:48:48 -0400416 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500417 cb(ssl, SSL_CB_HANDSHAKE_DONE, 1);
Adam Langley24819752014-12-15 18:42:07 -0800418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
Adam Langley24819752014-12-15 18:42:07 -0800420 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langley24819752014-12-15 18:42:07 -0800422 /* done handshaking, next message is client hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500423 ssl->d1->handshake_read_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800424 /* next message is server hello */
David Benjamin0d56f882015-12-19 17:05:56 -0500425 ssl->d1->handshake_write_seq = 0;
426 ssl->d1->next_handshake_write_seq = 0;
Adam Langley24819752014-12-15 18:42:07 -0800427 goto end;
428
429 default:
David Benjamin3570d732015-06-29 00:28:17 -0400430 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE);
Adam Langley24819752014-12-15 18:42:07 -0800431 ret = -1;
432 goto end;
433 }
434
David Benjamin0d56f882015-12-19 17:05:56 -0500435 if (!ssl->s3->tmp.reuse_message && !skip) {
436 if (cb != NULL && ssl->state != state) {
437 new_state = ssl->state;
438 ssl->state = state;
439 cb(ssl, SSL_CB_ACCEPT_LOOP, 1);
440 ssl->state = new_state;
Adam Langley24819752014-12-15 18:42:07 -0800441 }
442 }
443 skip = 0;
444 }
445
Adam Langley95c29f32014-06-20 12:00:00 -0700446end:
David Benjamin2755a3e2015-04-22 16:17:58 -0400447 BUF_MEM_free(buf);
Adam Langley24819752014-12-15 18:42:07 -0800448 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500449 cb(ssl, SSL_CB_ACCEPT_EXIT, ret);
Adam Langley24819752014-12-15 18:42:07 -0800450 }
451 return ret;
452}