2 * Copyright 2005-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include "internal/e_os.h"
12 #include <openssl/objects.h>
13 #include <openssl/rand.h>
14 #include "ssl_local.h"
15 #include "internal/time.h"
17 static int dtls1_handshake_write(SSL_CONNECTION *s);
18 static size_t dtls1_link_min_mtu(void);
20 /* XDTLS: figure out the right values */
21 static const size_t g_probable_mtu[] = { 1500, 512, 256 };
23 const SSL3_ENC_METHOD DTLSv1_enc_data = {
25 tls1_generate_master_secret,
26 tls1_change_cipher_state,
27 tls1_final_finish_mac,
28 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
29 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
31 tls1_export_keying_material,
32 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV,
33 dtls1_set_handshake_header,
34 dtls1_close_construct_packet,
38 const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
40 tls1_generate_master_secret,
41 tls1_change_cipher_state,
42 tls1_final_finish_mac,
43 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
44 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
46 tls1_export_keying_material,
47 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS
48 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
49 dtls1_set_handshake_header,
50 dtls1_close_construct_packet,
54 OSSL_TIME dtls1_default_timeout(void)
57 * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
58 * http, the cache would over fill
60 return ossl_seconds2time(60 * 60 * 2);
63 int dtls1_new(SSL *ssl)
66 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
71 if (!DTLS_RECORD_LAYER_new(&s->rlayer)) {
77 if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {
82 d1->buffered_messages = pqueue_new();
83 d1->sent_messages = pqueue_new();
86 d1->cookie_len = sizeof(s->d1->cookie);
92 if (d1->buffered_messages == NULL || d1->sent_messages == NULL) {
93 pqueue_free(d1->buffered_messages);
94 pqueue_free(d1->sent_messages);
102 if (!ssl->method->ssl_clear(ssl))
108 static void dtls1_clear_queues(SSL_CONNECTION *s)
110 dtls1_clear_received_buffer(s);
111 dtls1_clear_sent_buffer(s);
114 void dtls1_clear_received_buffer(SSL_CONNECTION *s)
117 hm_fragment *frag = NULL;
119 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
120 frag = (hm_fragment *)item->data;
121 dtls1_hm_fragment_free(frag);
126 void dtls1_clear_sent_buffer(SSL_CONNECTION *s)
129 hm_fragment *frag = NULL;
131 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
132 frag = (hm_fragment *)item->data;
134 if (frag->msg_header.is_ccs
135 && frag->msg_header.saved_retransmit_state.wrlmethod != NULL
136 && s->rlayer.wrl != frag->msg_header.saved_retransmit_state.wrl) {
138 * If we're freeing the CCS then we're done with the old wrl and it
141 frag->msg_header.saved_retransmit_state.wrlmethod->free(frag->msg_header.saved_retransmit_state.wrl);
144 dtls1_hm_fragment_free(frag);
150 void dtls1_free(SSL *ssl)
152 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
158 dtls1_clear_queues(s);
159 pqueue_free(s->d1->buffered_messages);
160 pqueue_free(s->d1->sent_messages);
163 DTLS_RECORD_LAYER_free(&s->rlayer);
171 int dtls1_clear(SSL *ssl)
173 pqueue *buffered_messages;
174 pqueue *sent_messages;
178 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
183 DTLS_RECORD_LAYER_clear(&s->rlayer);
186 DTLS_timer_cb timer_cb = s->d1->timer_cb;
188 buffered_messages = s->d1->buffered_messages;
189 sent_messages = s->d1->sent_messages;
191 link_mtu = s->d1->link_mtu;
193 dtls1_clear_queues(s);
195 memset(s->d1, 0, sizeof(*s->d1));
197 /* Restore the timer callback from previous state */
198 s->d1->timer_cb = timer_cb;
201 s->d1->cookie_len = sizeof(s->d1->cookie);
204 if (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU) {
206 s->d1->link_mtu = link_mtu;
209 s->d1->buffered_messages = buffered_messages;
210 s->d1->sent_messages = sent_messages;
213 if (!ssl3_clear(ssl))
216 if (ssl->method->version == DTLS_ANY_VERSION)
217 s->version = DTLS_MAX_VERSION_INTERNAL;
218 #ifndef OPENSSL_NO_DTLS1_METHOD
219 else if (s->options & SSL_OP_CISCO_ANYCONNECT)
220 s->client_version = s->version = DTLS1_BAD_VER;
223 s->version = ssl->method->version;
228 long dtls1_ctrl(SSL *ssl, int cmd, long larg, void *parg)
232 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
238 case DTLS_CTRL_GET_TIMEOUT:
239 if (dtls1_get_timeout(s, &t)) {
240 *(struct timeval *)parg = ossl_time_to_timeval(t);
244 case DTLS_CTRL_HANDLE_TIMEOUT:
245 ret = dtls1_handle_timeout(s);
247 case DTLS_CTRL_SET_LINK_MTU:
248 if (larg < (long)dtls1_link_min_mtu())
250 s->d1->link_mtu = larg;
252 case DTLS_CTRL_GET_LINK_MIN_MTU:
253 return (long)dtls1_link_min_mtu();
254 case SSL_CTRL_SET_MTU:
256 * We may not have a BIO set yet so can't call dtls1_min_mtu()
257 * We'll have to make do with dtls1_link_min_mtu() and max overhead
259 if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD)
264 ret = ssl3_ctrl(ssl, cmd, larg, parg);
270 static void dtls1_bio_set_next_timeout(BIO *bio, const DTLS1_STATE *d1)
272 struct timeval tv = ossl_time_to_timeval(d1->next_timeout);
274 BIO_ctrl(bio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &tv);
277 void dtls1_start_timer(SSL_CONNECTION *s)
280 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
282 #ifndef OPENSSL_NO_SCTP
283 /* Disable timer for SCTP */
284 if (BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
285 s->d1->next_timeout = ossl_time_zero();
291 * If timer is not set, initialize duration with 1 second or
292 * a user-specified value if the timer callback is installed.
294 if (ossl_time_is_zero(s->d1->next_timeout)) {
295 if (s->d1->timer_cb != NULL)
296 s->d1->timeout_duration_us = s->d1->timer_cb(ssl, 0);
298 s->d1->timeout_duration_us = 1000000;
301 /* Set timeout to current time plus duration */
302 duration = ossl_us2time(s->d1->timeout_duration_us);
303 s->d1->next_timeout = ossl_time_add(ossl_time_now(), duration);
305 /* set s->d1->next_timeout into ssl->rbio interface */
306 dtls1_bio_set_next_timeout(SSL_get_rbio(ssl), s->d1);
309 int dtls1_get_timeout(const SSL_CONNECTION *s, OSSL_TIME *timeleft)
313 /* If no timeout is set, just return NULL */
314 if (ossl_time_is_zero(s->d1->next_timeout))
317 /* Get current time */
318 timenow = ossl_time_now();
321 * If timer already expired or if remaining time is less than 15 ms,
322 * set it to 0 to prevent issues because of small divergences with
325 *timeleft = ossl_time_subtract(s->d1->next_timeout, timenow);
326 if (ossl_time_compare(*timeleft, ossl_ms2time(15)) <= 0)
327 *timeleft = ossl_time_zero();
331 int dtls1_is_timer_expired(SSL_CONNECTION *s)
335 /* Get time left until timeout, return false if no timer running */
336 if (!dtls1_get_timeout(s, &timeleft))
339 /* Return false if timer is not expired yet */
340 if (!ossl_time_is_zero(timeleft))
343 /* Timer expired, so return true */
347 static void dtls1_double_timeout(SSL_CONNECTION *s)
349 s->d1->timeout_duration_us *= 2;
350 if (s->d1->timeout_duration_us > 60000000)
351 s->d1->timeout_duration_us = 60000000;
354 void dtls1_stop_timer(SSL_CONNECTION *s)
356 /* Reset everything */
357 s->d1->timeout_num_alerts = 0;
358 s->d1->next_timeout = ossl_time_zero();
359 s->d1->timeout_duration_us = 1000000;
360 dtls1_bio_set_next_timeout(s->rbio, s->d1);
361 /* Clear retransmission buffer */
362 dtls1_clear_sent_buffer(s);
365 int dtls1_check_timeout_num(SSL_CONNECTION *s)
368 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
370 s->d1->timeout_num_alerts++;
372 /* Reduce MTU after 2 unsuccessful retransmissions */
373 if (s->d1->timeout_num_alerts > 2
374 && !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
376 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
377 if (mtu < s->d1->mtu)
381 if (s->d1->timeout_num_alerts > DTLS1_TMO_ALERT_COUNT) {
382 /* fail the connection, enough alerts have been sent */
383 SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_READ_TIMEOUT_EXPIRED);
390 int dtls1_handle_timeout(SSL_CONNECTION *s)
392 /* if no timer is expired, don't do anything */
393 if (!dtls1_is_timer_expired(s)) {
397 if (s->d1->timer_cb != NULL)
398 s->d1->timeout_duration_us = s->d1->timer_cb(SSL_CONNECTION_GET_SSL(s),
399 s->d1->timeout_duration_us);
401 dtls1_double_timeout(s);
403 if (dtls1_check_timeout_num(s) < 0) {
404 /* SSLfatal() already called */
408 dtls1_start_timer(s);
409 /* Calls SSLfatal() if required */
410 return dtls1_retransmit_buffered_messages(s);
413 #define LISTEN_SUCCESS 2
414 #define LISTEN_SEND_VERIFY_REQUEST 1
416 #ifndef OPENSSL_NO_SOCK
417 int DTLSv1_listen(SSL *ssl, BIO_ADDR *client)
419 int next, n, ret = 0;
420 unsigned char cookie[DTLS1_COOKIE_LENGTH];
421 unsigned char seq[SEQ_NUM_SIZE];
422 const unsigned char *data;
423 unsigned char *buf = NULL, *wbuf;
424 size_t fragoff, fraglen, msglen;
425 unsigned int rectype, versmajor, versminor, msgseq, msgtype, clientvers, cookielen;
427 BIO_ADDR *tmpclient = NULL;
428 PACKET pkt, msgpkt, msgpayload, session, cookiepkt;
429 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
434 if (s->handshake_func == NULL) {
435 /* Not properly initialized yet */
436 SSL_set_accept_state(ssl);
439 /* Ensure there is no state left over from a previous invocation */
445 rbio = SSL_get_rbio(ssl);
446 wbio = SSL_get_wbio(ssl);
448 if (!rbio || !wbio) {
449 ERR_raise(ERR_LIB_SSL, SSL_R_BIO_NOT_SET);
454 * Note: This check deliberately excludes DTLS1_BAD_VER because that version
455 * requires the MAC to be calculated *including* the first ClientHello
456 * (without the cookie). Since DTLSv1_listen is stateless that cannot be
457 * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via
460 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
461 ERR_raise(ERR_LIB_SSL, SSL_R_UNSUPPORTED_SSL_VERSION);
465 buf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH);
468 wbuf = OPENSSL_malloc(DTLS1_RT_HEADER_LENGTH + SSL3_RT_MAX_PLAIN_LENGTH);
478 n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH
479 + DTLS1_RT_HEADER_LENGTH);
481 if (BIO_should_retry(rbio)) {
482 /* Non-blocking IO */
489 if (!PACKET_buf_init(&pkt, buf, n)) {
490 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
496 * Parse the received record. If there are any problems with it we just
497 * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is
498 * resilient in the face of invalid records (e.g., invalid formatting,
499 * length, MAC, etc.). In general, invalid records SHOULD be silently
500 * discarded, thus preserving the association; however, an error MAY be
501 * logged for diagnostic purposes."
504 /* this packet contained a partial record, dump it */
505 if (n < DTLS1_RT_HEADER_LENGTH) {
506 ERR_raise(ERR_LIB_SSL, SSL_R_RECORD_TOO_SMALL);
510 /* Get the record header */
511 if (!PACKET_get_1(&pkt, &rectype)
512 || !PACKET_get_1(&pkt, &versmajor)
513 || !PACKET_get_1(&pkt, &versminor)) {
514 ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH);
519 s->msg_callback(0, (versmajor << 8) | versminor, SSL3_RT_HEADER, buf,
520 DTLS1_RT_HEADER_LENGTH, ssl, s->msg_callback_arg);
522 if (rectype != SSL3_RT_HANDSHAKE) {
523 ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE);
528 * Check record version number. We only check that the major version is
531 if (versmajor != DTLS1_VERSION_MAJOR) {
532 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
536 /* Save the sequence number: 64 bits, with top 2 bytes = epoch */
537 if (!PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)
538 || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {
539 ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH);
543 * We allow data remaining at the end of the packet because there could
544 * be a second record (but we ignore it)
547 /* This is an initial ClientHello so the epoch has to be 0 */
548 if (seq[0] != 0 || seq[1] != 0) {
549 ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE);
553 /* Get a pointer to the raw message for the later callback */
554 data = PACKET_data(&msgpkt);
556 /* Finished processing the record header, now process the message */
557 if (!PACKET_get_1(&msgpkt, &msgtype)
558 || !PACKET_get_net_3_len(&msgpkt, &msglen)
559 || !PACKET_get_net_2(&msgpkt, &msgseq)
560 || !PACKET_get_net_3_len(&msgpkt, &fragoff)
561 || !PACKET_get_net_3_len(&msgpkt, &fraglen)
562 || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)
563 || PACKET_remaining(&msgpkt) != 0) {
564 ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH);
568 if (msgtype != SSL3_MT_CLIENT_HELLO) {
569 ERR_raise(ERR_LIB_SSL, SSL_R_UNEXPECTED_MESSAGE);
573 /* Message sequence number can only be 0 or 1 */
575 ERR_raise(ERR_LIB_SSL, SSL_R_INVALID_SEQUENCE_NUMBER);
580 * We don't support fragment reassembly for ClientHellos whilst
581 * listening because that would require server side state (which is
582 * against the whole point of the ClientHello/HelloVerifyRequest
583 * mechanism). Instead we only look at the first ClientHello fragment
584 * and require that the cookie must be contained within it.
586 if (fragoff != 0 || fraglen > msglen) {
587 /* Non initial ClientHello fragment (or bad fragment) */
588 ERR_raise(ERR_LIB_SSL, SSL_R_FRAGMENTED_CLIENT_HELLO);
593 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,
594 fraglen + DTLS1_HM_HEADER_LENGTH, ssl,
595 s->msg_callback_arg);
597 if (!PACKET_get_net_2(&msgpayload, &clientvers)) {
598 ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH);
603 * Verify client version is supported
605 if (DTLS_VERSION_LT(clientvers, (unsigned int)ssl->method->version) &&
606 ssl->method->version != DTLS_ANY_VERSION) {
607 ERR_raise(ERR_LIB_SSL, SSL_R_WRONG_VERSION_NUMBER);
611 if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)
612 || !PACKET_get_length_prefixed_1(&msgpayload, &session)
613 || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {
615 * Could be malformed or the cookie does not fit within the initial
616 * ClientHello fragment. Either way we can't handle it.
618 ERR_raise(ERR_LIB_SSL, SSL_R_LENGTH_MISMATCH);
623 * Check if we have a cookie or not. If not we need to send a
624 * HelloVerifyRequest.
626 if (PACKET_remaining(&cookiepkt) == 0) {
627 next = LISTEN_SEND_VERIFY_REQUEST;
630 * We have a cookie, so lets check it.
632 if (ssl->ctx->app_verify_cookie_cb == NULL) {
633 ERR_raise(ERR_LIB_SSL, SSL_R_NO_VERIFY_COOKIE_CALLBACK);
638 if (ssl->ctx->app_verify_cookie_cb(ssl, PACKET_data(&cookiepkt),
639 (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {
641 * We treat invalid cookies in the same was as no cookie as
644 next = LISTEN_SEND_VERIFY_REQUEST;
646 /* Cookie verification succeeded */
647 next = LISTEN_SUCCESS;
651 if (next == LISTEN_SEND_VERIFY_REQUEST) {
653 unsigned int version;
657 * There was no cookie in the ClientHello so we need to send a
658 * HelloVerifyRequest. If this fails we do not worry about trying
659 * to resend, we just drop it.
662 /* Generate the cookie */
663 if (ssl->ctx->app_gen_cookie_cb == NULL ||
664 ssl->ctx->app_gen_cookie_cb(ssl, cookie, &cookielen) == 0 ||
666 ERR_raise(ERR_LIB_SSL, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
673 * Special case: for hello verify request, client version 1.0 and we
674 * haven't decided which version to use yet send back using version
675 * 1.0 header: otherwise some clients will ignore it.
677 version = (ssl->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION
680 /* Construct the record and message headers */
681 if (!WPACKET_init_static_len(&wpkt,
683 ssl_get_max_send_fragment(s)
684 + DTLS1_RT_HEADER_LENGTH,
686 || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)
687 || !WPACKET_put_bytes_u16(&wpkt, version)
689 * Record sequence number is always the same as in the
690 * received ClientHello
692 || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)
693 /* End of record, start sub packet for message */
694 || !WPACKET_start_sub_packet_u16(&wpkt)
696 || !WPACKET_put_bytes_u8(&wpkt,
697 DTLS1_MT_HELLO_VERIFY_REQUEST)
699 * Message length - doesn't follow normal TLS convention:
700 * the length isn't the last thing in the message header.
701 * We'll need to fill this in later when we know the
702 * length. Set it to zero for now
704 || !WPACKET_put_bytes_u24(&wpkt, 0)
706 * Message sequence number is always 0 for a
709 || !WPACKET_put_bytes_u16(&wpkt, 0)
711 * We never fragment a HelloVerifyRequest, so fragment
714 || !WPACKET_put_bytes_u24(&wpkt, 0)
716 * Fragment length is the same as message length, but
717 * this *is* the last thing in the message header so we
718 * can just start a sub-packet. No need to come back
719 * later for this one.
721 || !WPACKET_start_sub_packet_u24(&wpkt)
722 /* Create the actual HelloVerifyRequest body */
723 || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)
724 /* Close message body */
725 || !WPACKET_close(&wpkt)
726 /* Close record body */
727 || !WPACKET_close(&wpkt)
728 || !WPACKET_get_total_written(&wpkt, &wreclen)
729 || !WPACKET_finish(&wpkt)) {
730 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
731 WPACKET_cleanup(&wpkt);
738 * Fix up the message len in the message header. Its the same as the
739 * fragment len which has been filled in by WPACKET, so just copy
740 * that. Destination for the message len is after the record header
741 * plus one byte for the message content type. The source is the
742 * last 3 bytes of the message header
744 memcpy(&wbuf[DTLS1_RT_HEADER_LENGTH + 1],
745 &wbuf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],
749 s->msg_callback(1, 0, SSL3_RT_HEADER, buf,
750 DTLS1_RT_HEADER_LENGTH, ssl,
751 s->msg_callback_arg);
753 if ((tmpclient = BIO_ADDR_new()) == NULL) {
754 ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
759 * This is unnecessary if rbio and wbio are one and the same - but
760 * maybe they're not. We ignore errors here - some BIOs do not
763 if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {
764 (void)BIO_dgram_set_peer(wbio, tmpclient);
766 BIO_ADDR_free(tmpclient);
769 if (BIO_write(wbio, wbuf, wreclen) < (int)wreclen) {
770 if (BIO_should_retry(wbio)) {
772 * Non-blocking IO...but we're stateless, so we're just
773 * going to drop this packet.
781 if (BIO_flush(wbio) <= 0) {
782 if (BIO_should_retry(wbio)) {
784 * Non-blocking IO...but we're stateless, so we're just
785 * going to drop this packet.
793 } while (next != LISTEN_SUCCESS);
796 * Set expected sequence numbers to continue the handshake.
798 s->d1->handshake_read_seq = 1;
799 s->d1->handshake_write_seq = 1;
800 s->d1->next_handshake_write_seq = 1;
801 s->rlayer.wrlmethod->increment_sequence_ctr(s->rlayer.wrl);
804 * We are doing cookie exchange, so make sure we set that option in the
807 SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE);
810 * Tell the state machine that we've done the initial hello verify
813 ossl_statem_set_hello_verify_done(s);
816 * Some BIOs may not support this. If we fail we clear the client address
818 if (BIO_dgram_get_peer(rbio, client) <= 0)
819 BIO_ADDR_clear(client);
821 /* Buffer the record for use by the record layer */
822 if (BIO_write(s->rlayer.rrlnext, buf, n) != n) {
823 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
829 * Reset the record layer - but this time we can use the record we just
830 * buffered in s->rlayer.rrlnext
832 if (!ssl_set_new_record_layer(s,
834 OSSL_RECORD_DIRECTION_READ,
835 OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0,
836 NULL, 0, NULL, 0, NULL, 0, NULL, 0,
837 NID_undef, NULL, NULL, NULL)) {
838 /* SSLfatal already called */
845 BIO_ADDR_free(tmpclient);
852 static int dtls1_handshake_write(SSL_CONNECTION *s)
854 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
857 int dtls1_shutdown(SSL *s)
860 #ifndef OPENSSL_NO_SCTP
862 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL_ONLY(s);
867 wbio = SSL_get_wbio(s);
868 if (wbio != NULL && BIO_dgram_is_sctp(wbio) &&
869 !(sc->shutdown & SSL_SENT_SHUTDOWN)) {
870 ret = BIO_dgram_sctp_wait_for_dry(wbio);
875 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1,
879 ret = ssl3_shutdown(s);
880 #ifndef OPENSSL_NO_SCTP
881 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL);
886 int dtls1_query_mtu(SSL_CONNECTION *s)
888 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
890 if (s->d1->link_mtu) {
892 s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl));
896 /* AHA! Figure out the MTU, and stick to the right size */
897 if (s->d1->mtu < dtls1_min_mtu(s)) {
898 if (!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
900 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
903 * I've seen the kernel return bogus numbers when it doesn't know
904 * (initial write), so just make sure we have a reasonable number
906 if (s->d1->mtu < dtls1_min_mtu(s)) {
908 s->d1->mtu = dtls1_min_mtu(s);
909 BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SET_MTU,
910 (long)s->d1->mtu, NULL);
918 static size_t dtls1_link_min_mtu(void)
920 return (g_probable_mtu[(sizeof(g_probable_mtu) /
921 sizeof(g_probable_mtu[0])) - 1]);
924 size_t dtls1_min_mtu(SSL_CONNECTION *s)
926 SSL *ssl = SSL_CONNECTION_GET_SSL(s);
928 return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(ssl));
931 size_t DTLS_get_data_mtu(const SSL *ssl)
933 size_t mac_overhead, int_overhead, blocksize, ext_overhead;
934 const SSL_CIPHER *ciph = SSL_get_current_cipher(ssl);
936 const SSL_CONNECTION *s = SSL_CONNECTION_FROM_CONST_SSL_ONLY(ssl);
946 if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead,
947 &blocksize, &ext_overhead))
951 ext_overhead += mac_overhead;
953 int_overhead += mac_overhead;
955 /* Subtract external overhead (e.g. IV/nonce, separate MAC) */
956 if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu)
958 mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH;
960 /* Round encrypted payload down to cipher block size (for CBC etc.)
961 * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */
963 mtu -= (mtu % blocksize);
965 /* Subtract internal overhead (e.g. CBC padding len byte) */
966 if (int_overhead >= mtu)
973 void DTLS_set_timer_cb(SSL *ssl, DTLS_timer_cb cb)
975 SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
980 s->d1->timer_cb = cb;