1 /* $OpenBSD: d1_srvr.c,v 1.88 2017/05/07 04:22:24 beck Exp $ */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * openssl-core@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60 * All rights reserved.
62 * This package is an SSL implementation written
63 * by Eric Young (eay@cryptsoft.com).
64 * The implementation was written so as to conform with Netscapes SSL.
66 * This library is free for commercial and non-commercial use as long as
67 * the following conditions are aheared to. The following conditions
68 * apply to all code found in this distribution, be it the RC4, RSA,
69 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70 * included with this distribution is covered by the same copyright terms
71 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
73 * Copyright remains Eric Young's, and as such any Copyright notices in
74 * the code are not to be removed.
75 * If this package is used in a product, Eric Young should be given attribution
76 * as the author of the parts of the library used.
77 * This can be in the form of a textual message at program startup or
78 * in documentation (online or textual) provided with the package.
80 * Redistribution and use in source and binary forms, with or without
81 * modification, are permitted provided that the following conditions
83 * 1. Redistributions of source code must retain the copyright
84 * notice, this list of conditions and the following disclaimer.
85 * 2. Redistributions in binary form must reproduce the above copyright
86 * notice, this list of conditions and the following disclaimer in the
87 * documentation and/or other materials provided with the distribution.
88 * 3. All advertising materials mentioning features or use of this software
89 * must display the following acknowledgement:
90 * "This product includes cryptographic software written by
91 * Eric Young (eay@cryptsoft.com)"
92 * The word 'cryptographic' can be left out if the rouines from the library
93 * being used are not cryptographic related :-).
94 * 4. If you include any Windows specific code (or a derivative thereof) from
95 * the apps directory (application code) you must include an acknowledgement:
96 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
98 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
110 * The licence and distribution terms for any publically available version or
111 * derivative of this code cannot be changed. i.e. this code cannot simply be
112 * copied and put under another distribution licence
113 * [including the GNU Public Licence.]
118 #include "ssl_locl.h"
120 #include <openssl/bn.h>
121 #include <openssl/buffer.h>
122 #include <openssl/dh.h>
123 #include <openssl/evp.h>
124 #include <openssl/md5.h>
125 #include <openssl/objects.h>
126 #include <openssl/x509.h>
128 static int dtls1_send_hello_verify_request(SSL
*s
);
130 static const SSL_METHOD_INTERNAL DTLSv1_server_method_internal_data
= {
131 .version
= DTLS1_VERSION
,
132 .min_version
= DTLS1_VERSION
,
133 .max_version
= DTLS1_VERSION
,
134 .ssl_new
= dtls1_new
,
135 .ssl_clear
= dtls1_clear
,
136 .ssl_free
= dtls1_free
,
137 .ssl_accept
= dtls1_accept
,
138 .ssl_connect
= ssl_undefined_function
,
139 .ssl_read
= ssl3_read
,
140 .ssl_peek
= ssl3_peek
,
141 .ssl_write
= ssl3_write
,
142 .ssl_shutdown
= dtls1_shutdown
,
143 .ssl_pending
= ssl3_pending
,
144 .get_ssl_method
= dtls1_get_server_method
,
145 .get_timeout
= dtls1_default_timeout
,
146 .ssl_version
= ssl_undefined_void_function
,
147 .ssl_renegotiate
= ssl3_renegotiate
,
148 .ssl_renegotiate_check
= ssl3_renegotiate_check
,
149 .ssl_get_message
= dtls1_get_message
,
150 .ssl_read_bytes
= dtls1_read_bytes
,
151 .ssl_write_bytes
= dtls1_write_app_data_bytes
,
152 .ssl3_enc
= &DTLSv1_enc_data
,
155 static const SSL_METHOD DTLSv1_server_method_data
= {
156 .ssl_dispatch_alert
= dtls1_dispatch_alert
,
157 .num_ciphers
= ssl3_num_ciphers
,
158 .get_cipher
= dtls1_get_cipher
,
159 .get_cipher_by_char
= ssl3_get_cipher_by_char
,
160 .put_cipher_by_char
= ssl3_put_cipher_by_char
,
161 .internal
= &DTLSv1_server_method_internal_data
,
165 DTLSv1_server_method(void)
167 return &DTLSv1_server_method_data
;
171 dtls1_get_server_method(int ver
)
173 if (ver
== DTLS1_VERSION
)
174 return (DTLSv1_server_method());
181 void (*cb
)(const SSL
*ssl
, int type
, int val
) = NULL
;
184 int new_state
, state
, skip
= 0;
190 if (s
->internal
->info_callback
!= NULL
)
191 cb
= s
->internal
->info_callback
;
192 else if (s
->ctx
->internal
->info_callback
!= NULL
)
193 cb
= s
->ctx
->internal
->info_callback
;
195 listen
= D1I(s
)->listen
;
197 /* init things to blank */
198 s
->internal
->in_handshake
++;
199 if (!SSL_in_init(s
) || SSL_in_before(s
))
202 D1I(s
)->listen
= listen
;
204 if (s
->cert
== NULL
) {
205 SSLerror(s
, SSL_R_NO_CERTIFICATE_SET
);
211 state
= S3I(s
)->hs
.state
;
213 switch (S3I(s
)->hs
.state
) {
214 case SSL_ST_RENEGOTIATE
:
215 s
->internal
->renegotiate
= 1;
216 /* S3I(s)->hs.state=SSL_ST_ACCEPT; */
220 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
221 case SSL_ST_OK
|SSL_ST_ACCEPT
:
225 cb(s
, SSL_CB_HANDSHAKE_START
, 1);
227 if ((s
->version
& 0xff00) != (DTLS1_VERSION
& 0xff00)) {
228 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
232 s
->internal
->type
= SSL_ST_ACCEPT
;
234 if (!ssl3_setup_init_buffer(s
)) {
238 if (!ssl3_setup_buffers(s
)) {
243 s
->internal
->init_num
= 0;
245 if (S3I(s
)->hs
.state
!= SSL_ST_RENEGOTIATE
) {
246 /* Ok, we now need to push on a buffering BIO so that
247 * the output is sent in a way that TCP likes :-)
248 * ...but not with SCTP :-)
250 if (!ssl_init_wbio_buffer(s
, 1)) {
255 if (!tls1_init_finished_mac(s
)) {
260 S3I(s
)->hs
.state
= SSL3_ST_SR_CLNT_HELLO_A
;
261 s
->ctx
->internal
->stats
.sess_accept
++;
263 /* S3I(s)->hs.state == SSL_ST_RENEGOTIATE,
264 * we will just send a HelloRequest */
265 s
->ctx
->internal
->stats
.sess_accept_renegotiate
++;
266 S3I(s
)->hs
.state
= SSL3_ST_SW_HELLO_REQ_A
;
271 case SSL3_ST_SW_HELLO_REQ_A
:
272 case SSL3_ST_SW_HELLO_REQ_B
:
274 s
->internal
->shutdown
= 0;
275 dtls1_clear_record_buffer(s
);
276 dtls1_start_timer(s
);
277 ret
= ssl3_send_hello_request(s
);
280 S3I(s
)->hs
.next_state
= SSL3_ST_SR_CLNT_HELLO_A
;
281 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
282 s
->internal
->init_num
= 0;
284 if (!tls1_init_finished_mac(s
)) {
290 case SSL3_ST_SW_HELLO_REQ_C
:
291 S3I(s
)->hs
.state
= SSL_ST_OK
;
294 case SSL3_ST_SR_CLNT_HELLO_A
:
295 case SSL3_ST_SR_CLNT_HELLO_B
:
296 case SSL3_ST_SR_CLNT_HELLO_C
:
298 s
->internal
->shutdown
= 0;
299 ret
= ssl3_get_client_hello(s
);
304 if (ret
== 1 && (SSL_get_options(s
) & SSL_OP_COOKIE_EXCHANGE
))
305 S3I(s
)->hs
.state
= DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A
;
307 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_HELLO_A
;
309 s
->internal
->init_num
= 0;
311 /* Reflect ClientHello sequence to remain stateless while listening */
313 memcpy(S3I(s
)->write_sequence
, S3I(s
)->read_sequence
, sizeof(S3I(s
)->write_sequence
));
316 /* If we're just listening, stop here */
317 if (listen
&& S3I(s
)->hs
.state
== SSL3_ST_SW_SRVR_HELLO_A
) {
320 /* Set expected sequence numbers
321 * to continue the handshake.
323 D1I(s
)->handshake_read_seq
= 2;
324 D1I(s
)->handshake_write_seq
= 1;
325 D1I(s
)->next_handshake_write_seq
= 1;
331 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A
:
332 case DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B
:
334 ret
= dtls1_send_hello_verify_request(s
);
337 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
338 S3I(s
)->hs
.next_state
= SSL3_ST_SR_CLNT_HELLO_A
;
340 /* HelloVerifyRequest resets Finished MAC */
341 if (!tls1_init_finished_mac(s
)) {
348 case SSL3_ST_SW_SRVR_HELLO_A
:
349 case SSL3_ST_SW_SRVR_HELLO_B
:
350 s
->internal
->renegotiate
= 2;
351 dtls1_start_timer(s
);
352 ret
= ssl3_send_server_hello(s
);
356 if (s
->internal
->hit
) {
357 if (s
->internal
->tlsext_ticket_expected
)
358 S3I(s
)->hs
.state
= SSL3_ST_SW_SESSION_TICKET_A
;
360 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
362 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_A
;
363 s
->internal
->init_num
= 0;
366 case SSL3_ST_SW_CERT_A
:
367 case SSL3_ST_SW_CERT_B
:
368 /* Check if it is anon DH. */
369 if (!(S3I(s
)->hs
.new_cipher
->algorithm_auth
&
371 dtls1_start_timer(s
);
372 ret
= ssl3_send_server_certificate(s
);
375 if (s
->internal
->tlsext_status_expected
)
376 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_STATUS_A
;
378 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
381 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
383 s
->internal
->init_num
= 0;
386 case SSL3_ST_SW_KEY_EXCH_A
:
387 case SSL3_ST_SW_KEY_EXCH_B
:
388 alg_k
= S3I(s
)->hs
.new_cipher
->algorithm_mkey
;
390 /* Only send if using a DH key exchange. */
391 if (alg_k
& (SSL_kDHE
|SSL_kECDHE
)) {
392 dtls1_start_timer(s
);
393 ret
= ssl3_send_server_key_exchange(s
);
399 S3I(s
)->hs
.state
= SSL3_ST_SW_CERT_REQ_A
;
400 s
->internal
->init_num
= 0;
403 case SSL3_ST_SW_CERT_REQ_A
:
404 case SSL3_ST_SW_CERT_REQ_B
:
406 * Determine whether or not we need to request a
409 * Do not request a certificate if:
411 * - We did not ask for it (SSL_VERIFY_PEER is unset).
413 * - SSL_VERIFY_CLIENT_ONCE is set and we are
416 * - We are using an anonymous ciphersuites
417 * (see section "Certificate request" in SSL 3 drafts
418 * and in RFC 2246) ... except when the application
419 * insists on verification (against the specs, but
420 * s3_clnt.c accepts this for SSL 3).
422 if (!(s
->verify_mode
& SSL_VERIFY_PEER
) ||
423 ((s
->session
->peer
!= NULL
) &&
424 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)) ||
425 ((S3I(s
)->hs
.new_cipher
->algorithm_auth
&
426 SSL_aNULL
) && !(s
->verify_mode
&
427 SSL_VERIFY_FAIL_IF_NO_PEER_CERT
))) {
428 /* no cert request */
430 S3I(s
)->tmp
.cert_request
= 0;
431 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_DONE_A
;
433 S3I(s
)->tmp
.cert_request
= 1;
434 dtls1_start_timer(s
);
435 ret
= ssl3_send_certificate_request(s
);
438 S3I(s
)->hs
.state
= SSL3_ST_SW_SRVR_DONE_A
;
439 s
->internal
->init_num
= 0;
443 case SSL3_ST_SW_SRVR_DONE_A
:
444 case SSL3_ST_SW_SRVR_DONE_B
:
445 dtls1_start_timer(s
);
446 ret
= ssl3_send_server_done(s
);
449 S3I(s
)->hs
.next_state
= SSL3_ST_SR_CERT_A
;
450 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
451 s
->internal
->init_num
= 0;
454 case SSL3_ST_SW_FLUSH
:
455 s
->internal
->rwstate
= SSL_WRITING
;
456 if (BIO_flush(s
->wbio
) <= 0) {
457 /* If the write error was fatal, stop trying */
458 if (!BIO_should_retry(s
->wbio
)) {
459 s
->internal
->rwstate
= SSL_NOTHING
;
460 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
466 s
->internal
->rwstate
= SSL_NOTHING
;
467 S3I(s
)->hs
.state
= S3I(s
)->hs
.next_state
;
470 case SSL3_ST_SR_CERT_A
:
471 case SSL3_ST_SR_CERT_B
:
472 if (S3I(s
)->tmp
.cert_request
) {
473 ret
= ssl3_get_client_certificate(s
);
477 s
->internal
->init_num
= 0;
478 S3I(s
)->hs
.state
= SSL3_ST_SR_KEY_EXCH_A
;
481 case SSL3_ST_SR_KEY_EXCH_A
:
482 case SSL3_ST_SR_KEY_EXCH_B
:
483 ret
= ssl3_get_client_key_exchange(s
);
487 S3I(s
)->hs
.state
= SSL3_ST_SR_CERT_VRFY_A
;
488 s
->internal
->init_num
= 0;
491 /* For the ECDH ciphersuites when
492 * the client sends its ECDH pub key in
493 * a certificate, the CertificateVerify
494 * message is not sent.
496 S3I(s
)->hs
.state
= SSL3_ST_SR_FINISHED_A
;
497 s
->internal
->init_num
= 0;
498 } else if (SSL_USE_SIGALGS(s
)) {
499 S3I(s
)->hs
.state
= SSL3_ST_SR_CERT_VRFY_A
;
500 s
->internal
->init_num
= 0;
501 if (!s
->session
->peer
)
505 * For sigalgs freeze the handshake buffer
506 * at this point and digest cached records.
508 if (!S3I(s
)->handshake_buffer
) {
509 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
513 s
->s3
->flags
|= TLS1_FLAGS_KEEP_HANDSHAKE
;
514 if (!tls1_digest_cached_records(s
)) {
519 S3I(s
)->hs
.state
= SSL3_ST_SR_CERT_VRFY_A
;
520 s
->internal
->init_num
= 0;
523 * We need to get hashes here so if there is
524 * a client cert, it can be verified.
526 if (S3I(s
)->handshake_buffer
) {
527 if (!tls1_digest_cached_records(s
)) {
532 if (!tls1_handshake_hash_value(s
,
533 S3I(s
)->tmp
.cert_verify_md
,
534 sizeof(S3I(s
)->tmp
.cert_verify_md
),
542 case SSL3_ST_SR_CERT_VRFY_A
:
543 case SSL3_ST_SR_CERT_VRFY_B
:
545 D1I(s
)->change_cipher_spec_ok
= 1;
546 /* we should decide if we expected this one */
547 ret
= ssl3_get_cert_verify(s
);
550 S3I(s
)->hs
.state
= SSL3_ST_SR_FINISHED_A
;
551 s
->internal
->init_num
= 0;
554 case SSL3_ST_SR_FINISHED_A
:
555 case SSL3_ST_SR_FINISHED_B
:
556 D1I(s
)->change_cipher_spec_ok
= 1;
557 ret
= ssl3_get_finished(s
, SSL3_ST_SR_FINISHED_A
,
558 SSL3_ST_SR_FINISHED_B
);
562 if (s
->internal
->hit
)
563 S3I(s
)->hs
.state
= SSL_ST_OK
;
564 else if (s
->internal
->tlsext_ticket_expected
)
565 S3I(s
)->hs
.state
= SSL3_ST_SW_SESSION_TICKET_A
;
567 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
568 s
->internal
->init_num
= 0;
571 case SSL3_ST_SW_SESSION_TICKET_A
:
572 case SSL3_ST_SW_SESSION_TICKET_B
:
573 ret
= ssl3_send_newsession_ticket(s
);
576 S3I(s
)->hs
.state
= SSL3_ST_SW_CHANGE_A
;
577 s
->internal
->init_num
= 0;
580 case SSL3_ST_SW_CERT_STATUS_A
:
581 case SSL3_ST_SW_CERT_STATUS_B
:
582 ret
= ssl3_send_cert_status(s
);
585 S3I(s
)->hs
.state
= SSL3_ST_SW_KEY_EXCH_A
;
586 s
->internal
->init_num
= 0;
590 case SSL3_ST_SW_CHANGE_A
:
591 case SSL3_ST_SW_CHANGE_B
:
593 s
->session
->cipher
= S3I(s
)->hs
.new_cipher
;
594 if (!tls1_setup_key_block(s
)) {
599 ret
= dtls1_send_change_cipher_spec(s
,
600 SSL3_ST_SW_CHANGE_A
, SSL3_ST_SW_CHANGE_B
);
606 S3I(s
)->hs
.state
= SSL3_ST_SW_FINISHED_A
;
607 s
->internal
->init_num
= 0;
609 if (!tls1_change_cipher_state(s
,
610 SSL3_CHANGE_CIPHER_SERVER_WRITE
)) {
615 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
618 case SSL3_ST_SW_FINISHED_A
:
619 case SSL3_ST_SW_FINISHED_B
:
620 ret
= ssl3_send_finished(s
,
621 SSL3_ST_SW_FINISHED_A
, SSL3_ST_SW_FINISHED_B
,
622 TLS_MD_SERVER_FINISH_CONST
,
623 TLS_MD_SERVER_FINISH_CONST_SIZE
);
626 S3I(s
)->hs
.state
= SSL3_ST_SW_FLUSH
;
627 if (s
->internal
->hit
) {
628 S3I(s
)->hs
.next_state
= SSL3_ST_SR_FINISHED_A
;
631 S3I(s
)->hs
.next_state
= SSL_ST_OK
;
633 s
->internal
->init_num
= 0;
637 /* clean a few things up */
638 tls1_cleanup_key_block(s
);
640 /* remove buffering on output */
641 ssl_free_wbio_buffer(s
);
643 s
->internal
->init_num
= 0;
645 if (s
->internal
->renegotiate
== 2) /* skipped if we just sent a HelloRequest */
647 s
->internal
->renegotiate
= 0;
648 s
->internal
->new_session
= 0;
650 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
652 s
->ctx
->internal
->stats
.sess_accept_good
++;
654 s
->internal
->handshake_func
= dtls1_accept
;
657 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
662 /* done handshaking, next message is client hello */
663 D1I(s
)->handshake_read_seq
= 0;
664 /* next message is server hello */
665 D1I(s
)->handshake_write_seq
= 0;
666 D1I(s
)->next_handshake_write_seq
= 0;
671 SSLerror(s
, SSL_R_UNKNOWN_STATE
);
677 if (!S3I(s
)->tmp
.reuse_message
&& !skip
) {
678 if (s
->internal
->debug
) {
679 if ((ret
= BIO_flush(s
->wbio
)) <= 0)
683 if ((cb
!= NULL
) && (S3I(s
)->hs
.state
!= state
)) {
684 new_state
= S3I(s
)->hs
.state
;
685 S3I(s
)->hs
.state
= state
;
686 cb(s
, SSL_CB_ACCEPT_LOOP
, 1);
687 S3I(s
)->hs
.state
= new_state
;
693 /* BIO_flush(s->wbio); */
695 s
->internal
->in_handshake
--;
698 cb(s
, SSL_CB_ACCEPT_EXIT
, ret
);
704 dtls1_send_hello_verify_request(SSL
*s
)
706 CBB cbb
, verify
, cookie
;
708 memset(&cbb
, 0, sizeof(cbb
));
710 if (S3I(s
)->hs
.state
== DTLS1_ST_SW_HELLO_VERIFY_REQUEST_A
) {
711 if (s
->ctx
->internal
->app_gen_cookie_cb
== NULL
||
712 s
->ctx
->internal
->app_gen_cookie_cb(s
, D1I(s
)->cookie
,
713 &(D1I(s
)->cookie_len
)) == 0) {
714 SSLerror(s
, ERR_R_INTERNAL_ERROR
);
718 if (!ssl3_handshake_msg_start_cbb(s
, &cbb
, &verify
,
719 DTLS1_MT_HELLO_VERIFY_REQUEST
))
721 if (!CBB_add_u16(&verify
, s
->version
))
723 if (!CBB_add_u8_length_prefixed(&verify
, &cookie
))
725 if (!CBB_add_bytes(&cookie
, D1I(s
)->cookie
, D1I(s
)->cookie_len
))
727 if (!ssl3_handshake_msg_finish_cbb(s
, &cbb
))
730 S3I(s
)->hs
.state
= DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B
;
733 /* S3I(s)->hs.state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
734 return (ssl3_handshake_write(s
));