blob: 442e5e185de8b29bd87dcfe38c34de6a85add94e [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 Benjamin0b145c22014-11-26 20:10:09 -0500115#include <assert.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116#include <stdio.h>
117
118#include <openssl/bn.h>
119#include <openssl/buf.h>
120#include <openssl/dh.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400121#include <openssl/err.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700122#include <openssl/evp.h>
123#include <openssl/md5.h>
124#include <openssl/obj.h>
125#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
Adam Langley24819752014-12-15 18:42:07 -0800131int dtls1_accept(SSL *s) {
132 BUF_MEM *buf = NULL;
133 void (*cb)(const SSL *ssl, int type, int val) = 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
Adam Langley24819752014-12-15 18:42:07 -0800138 assert(s->handshake_func == dtls1_accept);
139 assert(s->server);
140 assert(SSL_IS_DTLS(s));
David Benjaminbeb47022014-11-30 02:58:52 -0500141
Adam Langley24819752014-12-15 18:42:07 -0800142 ERR_clear_error();
143 ERR_clear_system_error();
Adam Langley95c29f32014-06-20 12:00:00 -0700144
Adam Langley24819752014-12-15 18:42:07 -0800145 if (s->info_callback != NULL) {
146 cb = s->info_callback;
147 } else if (s->ctx->info_callback != NULL) {
148 cb = s->ctx->info_callback;
149 }
Adam Langley95c29f32014-06-20 12:00:00 -0700150
Adam Langley24819752014-12-15 18:42:07 -0800151 s->in_handshake++;
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langley24819752014-12-15 18:42:07 -0800153 if (s->cert == NULL) {
154 OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_NO_CERTIFICATE_SET);
155 return -1;
156 }
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langley24819752014-12-15 18:42:07 -0800158 for (;;) {
159 state = s->state;
Adam Langley95c29f32014-06-20 12:00:00 -0700160
Adam Langley24819752014-12-15 18:42:07 -0800161 switch (s->state) {
162 case SSL_ST_RENEGOTIATE:
163 s->renegotiate = 1;
164 /* s->state=SSL_ST_ACCEPT; */
Adam Langley95c29f32014-06-20 12:00:00 -0700165
Adam Langley24819752014-12-15 18:42:07 -0800166 case SSL_ST_ACCEPT:
167 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
168 if (cb != NULL) {
169 cb(s, SSL_CB_HANDSHAKE_START, 1);
170 }
Adam Langley95c29f32014-06-20 12:00:00 -0700171
Adam Langley24819752014-12-15 18:42:07 -0800172 if (s->init_buf == NULL) {
173 buf = BUF_MEM_new();
174 if (buf == NULL || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
175 ret = -1;
176 goto end;
177 }
178 s->init_buf = buf;
179 buf = NULL;
180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
Adam Langley24819752014-12-15 18:42:07 -0800182 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
Adam Langley24819752014-12-15 18:42:07 -0800184 if (s->state != SSL_ST_RENEGOTIATE) {
185 if (!ssl_init_wbio_buffer(s, 1)) {
186 ret = -1;
187 goto end;
188 }
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley24819752014-12-15 18:42:07 -0800190 if (!ssl3_init_finished_mac(s)) {
191 OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR);
192 ret = -1;
193 goto end;
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langley24819752014-12-15 18:42:07 -0800196 s->state = SSL3_ST_SR_CLNT_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800197 } else {
198 /* s->state == SSL_ST_RENEGOTIATE, * we will just send a
199 * HelloRequest */
Adam Langley24819752014-12-15 18:42:07 -0800200 s->state = SSL3_ST_SW_HELLO_REQ_A;
201 }
Adam Langley69a01602014-11-17 17:26:55 -0800202
Adam Langley24819752014-12-15 18:42:07 -0800203 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700204
Adam Langley24819752014-12-15 18:42:07 -0800205 case SSL3_ST_SW_HELLO_REQ_A:
206 case SSL3_ST_SW_HELLO_REQ_B:
207 s->shutdown = 0;
208 dtls1_clear_record_buffer(s);
209 dtls1_start_timer(s);
210 ret = ssl3_send_hello_request(s);
211 if (ret <= 0) {
212 goto end;
213 }
214 s->s3->tmp.next_state = SSL3_ST_SR_CLNT_HELLO_A;
215 s->state = SSL3_ST_SW_FLUSH;
216 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langley24819752014-12-15 18:42:07 -0800218 if (!ssl3_init_finished_mac(s)) {
219 OPENSSL_PUT_ERROR(SSL, dtls1_accept, ERR_R_INTERNAL_ERROR);
220 ret = -1;
221 goto end;
222 }
223 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700224
Adam Langley24819752014-12-15 18:42:07 -0800225 case SSL3_ST_SW_HELLO_REQ_C:
226 s->state = SSL_ST_OK;
227 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700228
Adam Langley24819752014-12-15 18:42:07 -0800229 case SSL3_ST_SR_CLNT_HELLO_A:
230 case SSL3_ST_SR_CLNT_HELLO_B:
231 case SSL3_ST_SR_CLNT_HELLO_C:
232 case SSL3_ST_SR_CLNT_HELLO_D:
233 s->shutdown = 0;
234 ret = ssl3_get_client_hello(s);
235 if (ret <= 0) {
236 goto end;
237 }
238 dtls1_stop_timer(s);
David Benjamina54e2e82015-02-16 16:31:43 -0500239 s->state = SSL3_ST_SW_SRVR_HELLO_A;
Adam Langley24819752014-12-15 18:42:07 -0800240 s->init_num = 0;
241 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700242
Adam Langley24819752014-12-15 18:42:07 -0800243 case SSL3_ST_SW_SRVR_HELLO_A:
244 case SSL3_ST_SW_SRVR_HELLO_B:
245 s->renegotiate = 2;
246 dtls1_start_timer(s);
247 ret = ssl3_send_server_hello(s);
248 if (ret <= 0) {
249 goto end;
250 }
Adam Langley95c29f32014-06-20 12:00:00 -0700251
Adam Langley24819752014-12-15 18:42:07 -0800252 if (s->hit) {
253 if (s->tlsext_ticket_expected) {
254 s->state = SSL3_ST_SW_SESSION_TICKET_A;
255 } else {
256 s->state = SSL3_ST_SW_CHANGE_A;
257 }
258 } else {
259 s->state = SSL3_ST_SW_CERT_A;
260 }
261 s->init_num = 0;
262 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700263
Adam Langley24819752014-12-15 18:42:07 -0800264 case SSL3_ST_SW_CERT_A:
265 case SSL3_ST_SW_CERT_B:
David Benjamine95d20d2014-12-23 11:16:01 -0500266 if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) {
Adam Langley24819752014-12-15 18:42:07 -0800267 dtls1_start_timer(s);
268 ret = ssl3_send_server_certificate(s);
269 if (ret <= 0) {
270 goto end;
271 }
272 if (s->s3->tmp.certificate_status_expected) {
273 s->state = SSL3_ST_SW_CERT_STATUS_A;
274 } else {
275 s->state = SSL3_ST_SW_KEY_EXCH_A;
276 }
277 } else {
278 skip = 1;
279 s->state = SSL3_ST_SW_KEY_EXCH_A;
280 }
281 s->init_num = 0;
282 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700283
Adam Langley24819752014-12-15 18:42:07 -0800284 case SSL3_ST_SW_KEY_EXCH_A:
285 case SSL3_ST_SW_KEY_EXCH_B:
286 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley24819752014-12-15 18:42:07 -0800288 /* Send a ServerKeyExchange message if:
289 * - The key exchange is ephemeral or anonymous
290 * Diffie-Hellman.
291 * - There is a PSK identity hint.
292 *
293 * TODO(davidben): This logic is currently duplicated
294 * in s3_srvr.c. Fix this. In the meantime, keep them
295 * in sync. */
296 if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) ||
297 ((alg_a & SSL_aPSK) && s->psk_identity_hint)) {
298 dtls1_start_timer(s);
299 ret = ssl3_send_server_key_exchange(s);
300 if (ret <= 0) {
301 goto end;
302 }
303 } else {
304 skip = 1;
305 }
Adam Langley95c29f32014-06-20 12:00:00 -0700306
Adam Langley24819752014-12-15 18:42:07 -0800307 s->state = SSL3_ST_SW_CERT_REQ_A;
308 s->init_num = 0;
309 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langley24819752014-12-15 18:42:07 -0800311 case SSL3_ST_SW_CERT_REQ_A:
312 case SSL3_ST_SW_CERT_REQ_B:
313 if (/* don't request cert unless asked for it: */
314 !(s->verify_mode & SSL_VERIFY_PEER) ||
315 /* if SSL_VERIFY_CLIENT_ONCE is set,
316 * don't request cert during re-negotiation: */
317 ((s->session->peer != NULL) &&
318 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
Adam Langley24819752014-12-15 18:42:07 -0800319 /* With normal PSK Certificates and
320 * Certificate Requests are omitted */
321 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
322 /* no cert request */
323 skip = 1;
324 s->s3->tmp.cert_request = 0;
325 s->state = SSL3_ST_SW_SRVR_DONE_A;
326 } else {
327 s->s3->tmp.cert_request = 1;
328 dtls1_start_timer(s);
329 ret = ssl3_send_certificate_request(s);
330 if (ret <= 0) {
331 goto end;
332 }
Adam Langley24819752014-12-15 18:42:07 -0800333 s->state = SSL3_ST_SW_SRVR_DONE_A;
Adam Langley24819752014-12-15 18:42:07 -0800334 s->init_num = 0;
335 }
336 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langley24819752014-12-15 18:42:07 -0800338 case SSL3_ST_SW_SRVR_DONE_A:
339 case SSL3_ST_SW_SRVR_DONE_B:
340 dtls1_start_timer(s);
341 ret = ssl3_send_server_done(s);
342 if (ret <= 0) {
343 goto end;
344 }
345 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
346 s->state = SSL3_ST_SW_FLUSH;
347 s->init_num = 0;
348 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700349
Adam Langley24819752014-12-15 18:42:07 -0800350 case SSL3_ST_SW_FLUSH:
351 s->rwstate = SSL_WRITING;
352 if (BIO_flush(s->wbio) <= 0) {
Adam Langley24819752014-12-15 18:42:07 -0800353 ret = -1;
354 goto end;
355 }
356 s->rwstate = SSL_NOTHING;
357 s->state = s->s3->tmp.next_state;
358 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley24819752014-12-15 18:42:07 -0800360 case SSL3_ST_SR_CERT_A:
361 case SSL3_ST_SR_CERT_B:
362 if (s->s3->tmp.cert_request) {
363 ret = ssl3_get_client_certificate(s);
364 if (ret <= 0) {
365 goto end;
366 }
367 }
368 s->init_num = 0;
369 s->state = SSL3_ST_SR_KEY_EXCH_A;
370 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langley24819752014-12-15 18:42:07 -0800372 case SSL3_ST_SR_KEY_EXCH_A:
373 case SSL3_ST_SR_KEY_EXCH_B:
374 ret = ssl3_get_client_key_exchange(s);
375 if (ret <= 0) {
376 goto end;
377 }
378 s->state = SSL3_ST_SR_CERT_VRFY_A;
379 s->init_num = 0;
380 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langley24819752014-12-15 18:42:07 -0800382 case SSL3_ST_SR_CERT_VRFY_A:
383 case SSL3_ST_SR_CERT_VRFY_B:
Adam Langley24819752014-12-15 18:42:07 -0800384 ret = ssl3_get_cert_verify(s);
385 if (ret <= 0) {
386 goto end;
387 }
388 s->state = SSL3_ST_SR_FINISHED_A;
389 s->init_num = 0;
390 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langley24819752014-12-15 18:42:07 -0800392 case SSL3_ST_SR_FINISHED_A:
393 case SSL3_ST_SR_FINISHED_B:
394 s->d1->change_cipher_spec_ok = 1;
395 ret =
396 ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B);
397 if (ret <= 0) {
398 goto end;
399 }
400 dtls1_stop_timer(s);
401 if (s->hit) {
402 s->state = SSL_ST_OK;
403 } else if (s->tlsext_ticket_expected) {
404 s->state = SSL3_ST_SW_SESSION_TICKET_A;
405 } else {
406 s->state = SSL3_ST_SW_CHANGE_A;
407 }
408 s->init_num = 0;
409 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
Adam Langley24819752014-12-15 18:42:07 -0800411 case SSL3_ST_SW_SESSION_TICKET_A:
412 case SSL3_ST_SW_SESSION_TICKET_B:
413 ret = ssl3_send_new_session_ticket(s);
414 if (ret <= 0) {
415 goto end;
416 }
417 s->state = SSL3_ST_SW_CHANGE_A;
418 s->init_num = 0;
419 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langley24819752014-12-15 18:42:07 -0800421 case SSL3_ST_SW_CHANGE_A:
422 case SSL3_ST_SW_CHANGE_B:
423 s->session->cipher = s->s3->tmp.new_cipher;
424 if (!s->enc_method->setup_key_block(s)) {
425 ret = -1;
426 goto end;
427 }
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley24819752014-12-15 18:42:07 -0800429 ret = dtls1_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A,
430 SSL3_ST_SW_CHANGE_B);
Adam Langley95c29f32014-06-20 12:00:00 -0700431
Adam Langley24819752014-12-15 18:42:07 -0800432 if (ret <= 0) {
433 goto end;
434 }
Adam Langley95c29f32014-06-20 12:00:00 -0700435
Adam Langley24819752014-12-15 18:42:07 -0800436 s->state = SSL3_ST_SW_FINISHED_A;
437 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700438
Adam Langley24819752014-12-15 18:42:07 -0800439 if (!s->enc_method->change_cipher_state(
440 s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) {
441 ret = -1;
442 goto end;
443 }
Adam Langley95c29f32014-06-20 12:00:00 -0700444
Adam Langley24819752014-12-15 18:42:07 -0800445 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
446 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700447
Adam Langley24819752014-12-15 18:42:07 -0800448 case SSL3_ST_SW_FINISHED_A:
449 case SSL3_ST_SW_FINISHED_B:
450 ret =
451 ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B,
452 s->enc_method->server_finished_label,
453 s->enc_method->server_finished_label_len);
454 if (ret <= 0) {
455 goto end;
456 }
457 s->state = SSL3_ST_SW_FLUSH;
458 if (s->hit) {
459 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
460 } else {
461 s->s3->tmp.next_state = SSL_ST_OK;
462 }
463 s->init_num = 0;
464 break;
Adam Langley95c29f32014-06-20 12:00:00 -0700465
Adam Langley24819752014-12-15 18:42:07 -0800466 case SSL_ST_OK:
467 ssl3_cleanup_key_block(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700468
Adam Langley24819752014-12-15 18:42:07 -0800469 /* remove buffering on output */
470 ssl_free_wbio_buffer(s);
Adam Langley95c29f32014-06-20 12:00:00 -0700471
Adam Langley24819752014-12-15 18:42:07 -0800472 s->init_num = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700473
Adam Langley24819752014-12-15 18:42:07 -0800474 if (s->renegotiate == 2) {
475 /* skipped if we just sent a HelloRequest */
476 s->renegotiate = 0;
David Benjamine6df0542015-05-12 22:02:08 -0400477 s->s3->initial_handshake_complete = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langley24819752014-12-15 18:42:07 -0800479 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
Adam Langley95c29f32014-06-20 12:00:00 -0700480
Adam Langley24819752014-12-15 18:42:07 -0800481 if (cb != NULL) {
482 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
483 }
484 }
Adam Langley95c29f32014-06-20 12:00:00 -0700485
Adam Langley24819752014-12-15 18:42:07 -0800486 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700487
Adam Langley24819752014-12-15 18:42:07 -0800488 /* done handshaking, next message is client hello */
489 s->d1->handshake_read_seq = 0;
490 /* next message is server hello */
491 s->d1->handshake_write_seq = 0;
492 s->d1->next_handshake_write_seq = 0;
493 goto end;
494
495 default:
496 OPENSSL_PUT_ERROR(SSL, dtls1_accept, SSL_R_UNKNOWN_STATE);
497 ret = -1;
498 goto end;
499 }
500
501 if (!s->s3->tmp.reuse_message && !skip) {
502 if (cb != NULL && s->state != state) {
503 new_state = s->state;
504 s->state = state;
505 cb(s, SSL_CB_ACCEPT_LOOP, 1);
506 s->state = new_state;
507 }
508 }
509 skip = 0;
510 }
511
Adam Langley95c29f32014-06-20 12:00:00 -0700512end:
Adam Langley24819752014-12-15 18:42:07 -0800513 s->in_handshake--;
David Benjamin2755a3e2015-04-22 16:17:58 -0400514 BUF_MEM_free(buf);
Adam Langley24819752014-12-15 18:42:07 -0800515 if (cb != NULL) {
516 cb(s, SSL_CB_ACCEPT_EXIT, ret);
517 }
518 return ret;
519}