2 * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 #include <openssl/objects.h>
13 #include <openssl/rand.h>
16 static void get_current_time(struct timeval *t);
17 static int dtls1_handshake_write(SSL *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 = {
27 tls1_generate_master_secret,
28 tls1_change_cipher_state,
29 tls1_final_finish_mac,
30 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
31 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
33 tls1_export_keying_material,
34 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV,
35 dtls1_set_handshake_header,
36 dtls1_close_construct_packet,
40 const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
44 tls1_generate_master_secret,
45 tls1_change_cipher_state,
46 tls1_final_finish_mac,
47 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
48 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
50 tls1_export_keying_material,
51 SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS
52 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
53 dtls1_set_handshake_header,
54 dtls1_close_construct_packet,
58 long dtls1_default_timeout(void)
61 * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
62 * http, the cache would over fill
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 (!s->method->ssl_clear(s))
108 static void dtls1_clear_queues(SSL *s)
110 dtls1_clear_received_buffer(s);
111 dtls1_clear_sent_buffer(s);
114 void dtls1_clear_received_buffer(SSL *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 *s)
129 hm_fragment *frag = NULL;
131 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
132 frag = (hm_fragment *)item->data;
133 dtls1_hm_fragment_free(frag);
139 void dtls1_free(SSL *s)
141 DTLS_RECORD_LAYER_free(&s->rlayer);
145 dtls1_clear_queues(s);
147 pqueue_free(s->d1->buffered_messages);
148 pqueue_free(s->d1->sent_messages);
154 int dtls1_clear(SSL *s)
156 pqueue *buffered_messages;
157 pqueue *sent_messages;
161 DTLS_RECORD_LAYER_clear(&s->rlayer);
164 DTLS_timer_cb timer_cb = s->d1->timer_cb;
166 buffered_messages = s->d1->buffered_messages;
167 sent_messages = s->d1->sent_messages;
169 link_mtu = s->d1->link_mtu;
171 dtls1_clear_queues(s);
173 memset(s->d1, 0, sizeof(*s->d1));
175 /* Restore the timer callback from previous state */
176 s->d1->timer_cb = timer_cb;
179 s->d1->cookie_len = sizeof(s->d1->cookie);
182 if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
184 s->d1->link_mtu = link_mtu;
187 s->d1->buffered_messages = buffered_messages;
188 s->d1->sent_messages = sent_messages;
194 if (s->method->version == DTLS_ANY_VERSION)
195 s->version = DTLS_MAX_VERSION;
196 #ifndef OPENSSL_NO_DTLS1_METHOD
197 else if (s->options & SSL_OP_CISCO_ANYCONNECT)
198 s->client_version = s->version = DTLS1_BAD_VER;
201 s->version = s->method->version;
206 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
211 case DTLS_CTRL_GET_TIMEOUT:
212 if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) {
216 case DTLS_CTRL_HANDLE_TIMEOUT:
217 ret = dtls1_handle_timeout(s);
219 case DTLS_CTRL_SET_LINK_MTU:
220 if (larg < (long)dtls1_link_min_mtu())
222 s->d1->link_mtu = larg;
224 case DTLS_CTRL_GET_LINK_MIN_MTU:
225 return (long)dtls1_link_min_mtu();
226 case SSL_CTRL_SET_MTU:
228 * We may not have a BIO set yet so can't call dtls1_min_mtu()
229 * We'll have to make do with dtls1_link_min_mtu() and max overhead
231 if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD)
236 ret = ssl3_ctrl(s, cmd, larg, parg);
242 void dtls1_start_timer(SSL *s)
244 unsigned int sec, usec;
246 #ifndef OPENSSL_NO_SCTP
247 /* Disable timer for SCTP */
248 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
249 memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
255 * If timer is not set, initialize duration with 1 second or
256 * a user-specified value if the timer callback is installed.
258 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
260 if (s->d1->timer_cb != NULL)
261 s->d1->timeout_duration_us = s->d1->timer_cb(s, 0);
263 s->d1->timeout_duration_us = 1000000;
266 /* Set timeout to current time */
267 get_current_time(&(s->d1->next_timeout));
269 /* Add duration to current time */
271 sec = s->d1->timeout_duration_us / 1000000;
272 usec = s->d1->timeout_duration_us - (sec * 1000000);
274 s->d1->next_timeout.tv_sec += sec;
275 s->d1->next_timeout.tv_usec += usec;
277 if (s->d1->next_timeout.tv_usec >= 1000000) {
278 s->d1->next_timeout.tv_sec++;
279 s->d1->next_timeout.tv_usec -= 1000000;
282 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
283 &(s->d1->next_timeout));
286 struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
288 struct timeval timenow;
290 /* If no timeout is set, just return NULL */
291 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
295 /* Get current time */
296 get_current_time(&timenow);
298 /* If timer already expired, set remaining time to 0 */
299 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
300 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
301 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
302 memset(timeleft, 0, sizeof(*timeleft));
306 /* Calculate time left until timer expires */
307 memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval));
308 timeleft->tv_sec -= timenow.tv_sec;
309 timeleft->tv_usec -= timenow.tv_usec;
310 if (timeleft->tv_usec < 0) {
312 timeleft->tv_usec += 1000000;
316 * If remaining time is less than 15 ms, set it to 0 to prevent issues
317 * because of small divergences with socket timeouts.
319 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
320 memset(timeleft, 0, sizeof(*timeleft));
326 int dtls1_is_timer_expired(SSL *s)
328 struct timeval timeleft;
330 /* Get time left until timeout, return false if no timer running */
331 if (dtls1_get_timeout(s, &timeleft) == NULL) {
335 /* Return false if timer is not expired yet */
336 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
340 /* Timer expired, so return true */
344 void dtls1_double_timeout(SSL *s)
346 s->d1->timeout_duration_us *= 2;
347 if (s->d1->timeout_duration_us > 60000000)
348 s->d1->timeout_duration_us = 60000000;
349 dtls1_start_timer(s);
352 void dtls1_stop_timer(SSL *s)
354 /* Reset everything */
355 memset(&s->d1->timeout, 0, sizeof(s->d1->timeout));
356 memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
357 s->d1->timeout_duration_us = 1000000;
358 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
359 &(s->d1->next_timeout));
360 /* Clear retransmission buffer */
361 dtls1_clear_sent_buffer(s);
364 int dtls1_check_timeout_num(SSL *s)
368 s->d1->timeout.num_alerts++;
370 /* Reduce MTU after 2 unsuccessful retransmissions */
371 if (s->d1->timeout.num_alerts > 2
372 && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
374 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
375 if (mtu < s->d1->mtu)
379 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
380 /* fail the connection, enough alerts have been sent */
381 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_CHECK_TIMEOUT_NUM,
382 SSL_R_READ_TIMEOUT_EXPIRED);
389 int dtls1_handle_timeout(SSL *s)
391 /* if no timer is expired, don't do anything */
392 if (!dtls1_is_timer_expired(s)) {
396 if (s->d1->timer_cb != NULL)
397 s->d1->timeout_duration_us = s->d1->timer_cb(s, s->d1->timeout_duration_us);
399 dtls1_double_timeout(s);
401 if (dtls1_check_timeout_num(s) < 0) {
402 /* SSLfatal() already called */
406 s->d1->timeout.read_timeouts++;
407 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
408 s->d1->timeout.read_timeouts = 1;
411 dtls1_start_timer(s);
412 /* Calls SSLfatal() if required */
413 return dtls1_retransmit_buffered_messages(s);
416 static void get_current_time(struct timeval *t)
426 SystemTimeToFileTime(&st, &now.ft);
427 /* re-bias to 1/1/1970 */
429 now.ul -= 116444736000000000ULL;
432 now.ul -= 116444736000000000UI64;
435 t->tv_sec = (long)(now.ul / 10000000);
436 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
438 gettimeofday(t, NULL);
442 #define LISTEN_SUCCESS 2
443 #define LISTEN_SEND_VERIFY_REQUEST 1
445 #ifndef OPENSSL_NO_SOCK
446 int DTLSv1_listen(SSL *s, BIO_ADDR *client)
448 int next, n, ret = 0, clearpkt = 0;
449 unsigned char cookie[DTLS1_COOKIE_LENGTH];
450 unsigned char seq[SEQ_NUM_SIZE];
451 const unsigned char *data;
452 unsigned char *buf, *wbuf;
453 size_t fragoff, fraglen, msglen;
454 unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;
456 BIO_ADDR *tmpclient = NULL;
457 PACKET pkt, msgpkt, msgpayload, session, cookiepkt;
459 if (s->handshake_func == NULL) {
460 /* Not properly initialized yet */
461 SSL_set_accept_state(s);
464 /* Ensure there is no state left over from a previous invocation */
470 rbio = SSL_get_rbio(s);
471 wbio = SSL_get_wbio(s);
473 if (!rbio || !wbio) {
474 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);
479 * We only peek at incoming ClientHello's until we're sure we are going to
480 * to respond with a HelloVerifyRequest. If its a ClientHello with a valid
481 * cookie then we leave it in the BIO for accept to handle.
483 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);
486 * Note: This check deliberately excludes DTLS1_BAD_VER because that version
487 * requires the MAC to be calculated *including* the first ClientHello
488 * (without the cookie). Since DTLSv1_listen is stateless that cannot be
489 * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via
492 if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
493 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);
497 if (!ssl3_setup_buffers(s)) {
498 /* SSLerr already called */
501 buf = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
502 wbuf = RECORD_LAYER_get_wbuf(&s->rlayer)[0].buf;
508 n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH
509 + DTLS1_RT_HEADER_LENGTH);
512 if (BIO_should_retry(rbio)) {
513 /* Non-blocking IO */
519 /* If we hit any problems we need to clear this packet from the BIO */
522 if (!PACKET_buf_init(&pkt, buf, n)) {
523 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
528 * Parse the received record. If there are any problems with it we just
529 * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is
530 * resilient in the face of invalid records (e.g., invalid formatting,
531 * length, MAC, etc.). In general, invalid records SHOULD be silently
532 * discarded, thus preserving the association; however, an error MAY be
533 * logged for diagnostic purposes."
536 /* this packet contained a partial record, dump it */
537 if (n < DTLS1_RT_HEADER_LENGTH) {
538 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);
543 s->msg_callback(0, 0, SSL3_RT_HEADER, buf,
544 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
546 /* Get the record header */
547 if (!PACKET_get_1(&pkt, &rectype)
548 || !PACKET_get_1(&pkt, &versmajor)) {
549 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
553 if (rectype != SSL3_RT_HANDSHAKE) {
554 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
559 * Check record version number. We only check that the major version is
562 if (versmajor != DTLS1_VERSION_MAJOR) {
563 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
567 if (!PACKET_forward(&pkt, 1)
568 /* Save the sequence number: 64 bits, with top 2 bytes = epoch */
569 || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)
570 || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {
571 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
575 * We allow data remaining at the end of the packet because there could
576 * be a second record (but we ignore it)
579 /* This is an initial ClientHello so the epoch has to be 0 */
580 if (seq[0] != 0 || seq[1] != 0) {
581 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
585 /* Get a pointer to the raw message for the later callback */
586 data = PACKET_data(&msgpkt);
588 /* Finished processing the record header, now process the message */
589 if (!PACKET_get_1(&msgpkt, &msgtype)
590 || !PACKET_get_net_3_len(&msgpkt, &msglen)
591 || !PACKET_get_net_2(&msgpkt, &msgseq)
592 || !PACKET_get_net_3_len(&msgpkt, &fragoff)
593 || !PACKET_get_net_3_len(&msgpkt, &fraglen)
594 || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)
595 || PACKET_remaining(&msgpkt) != 0) {
596 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
600 if (msgtype != SSL3_MT_CLIENT_HELLO) {
601 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
605 /* Message sequence number can only be 0 or 1 */
607 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);
612 * We don't support fragment reassembly for ClientHellos whilst
613 * listening because that would require server side state (which is
614 * against the whole point of the ClientHello/HelloVerifyRequest
615 * mechanism). Instead we only look at the first ClientHello fragment
616 * and require that the cookie must be contained within it.
618 if (fragoff != 0 || fraglen > msglen) {
619 /* Non initial ClientHello fragment (or bad fragment) */
620 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);
625 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,
626 fraglen + DTLS1_HM_HEADER_LENGTH, s,
627 s->msg_callback_arg);
629 if (!PACKET_get_net_2(&msgpayload, &clientvers)) {
630 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
635 * Verify client version is supported
637 if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&
638 s->method->version != DTLS_ANY_VERSION) {
639 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);
643 if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)
644 || !PACKET_get_length_prefixed_1(&msgpayload, &session)
645 || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {
647 * Could be malformed or the cookie does not fit within the initial
648 * ClientHello fragment. Either way we can't handle it.
650 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
655 * Check if we have a cookie or not. If not we need to send a
656 * HelloVerifyRequest.
658 if (PACKET_remaining(&cookiepkt) == 0) {
659 next = LISTEN_SEND_VERIFY_REQUEST;
662 * We have a cookie, so lets check it.
664 if (s->ctx->app_verify_cookie_cb == NULL) {
665 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);
669 if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),
670 (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {
672 * We treat invalid cookies in the same was as no cookie as
675 next = LISTEN_SEND_VERIFY_REQUEST;
677 /* Cookie verification succeeded */
678 next = LISTEN_SUCCESS;
682 if (next == LISTEN_SEND_VERIFY_REQUEST) {
684 unsigned int version;
688 * There was no cookie in the ClientHello so we need to send a
689 * HelloVerifyRequest. If this fails we do not worry about trying
690 * to resend, we just drop it.
694 * Dump the read packet, we don't need it any more. Ignore return
697 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);
698 BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);
699 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 1, NULL);
701 /* Generate the cookie */
702 if (s->ctx->app_gen_cookie_cb == NULL ||
703 s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||
705 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
711 * Special case: for hello verify request, client version 1.0 and we
712 * haven't decided which version to use yet send back using version
713 * 1.0 header: otherwise some clients will ignore it.
715 version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION
718 /* Construct the record and message headers */
719 if (!WPACKET_init_static_len(&wpkt,
721 SSL3_RT_MAX_PLAIN_LENGTH
722 + DTLS1_RT_HEADER_LENGTH,
724 || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)
725 || !WPACKET_put_bytes_u16(&wpkt, version)
727 * Record sequence number is always the same as in the
728 * received ClientHello
730 || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)
731 /* End of record, start sub packet for message */
732 || !WPACKET_start_sub_packet_u16(&wpkt)
734 || !WPACKET_put_bytes_u8(&wpkt,
735 DTLS1_MT_HELLO_VERIFY_REQUEST)
737 * Message length - doesn't follow normal TLS convention:
738 * the length isn't the last thing in the message header.
739 * We'll need to fill this in later when we know the
740 * length. Set it to zero for now
742 || !WPACKET_put_bytes_u24(&wpkt, 0)
744 * Message sequence number is always 0 for a
747 || !WPACKET_put_bytes_u16(&wpkt, 0)
749 * We never fragment a HelloVerifyRequest, so fragment
752 || !WPACKET_put_bytes_u24(&wpkt, 0)
754 * Fragment length is the same as message length, but
755 * this *is* the last thing in the message header so we
756 * can just start a sub-packet. No need to come back
757 * later for this one.
759 || !WPACKET_start_sub_packet_u24(&wpkt)
760 /* Create the actual HelloVerifyRequest body */
761 || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)
762 /* Close message body */
763 || !WPACKET_close(&wpkt)
764 /* Close record body */
765 || !WPACKET_close(&wpkt)
766 || !WPACKET_get_total_written(&wpkt, &wreclen)
767 || !WPACKET_finish(&wpkt)) {
768 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
769 WPACKET_cleanup(&wpkt);
775 * Fix up the message len in the message header. Its the same as the
776 * fragment len which has been filled in by WPACKET, so just copy
777 * that. Destination for the message len is after the record header
778 * plus one byte for the message content type. The source is the
779 * last 3 bytes of the message header
781 memcpy(&wbuf[DTLS1_RT_HEADER_LENGTH + 1],
782 &wbuf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],
786 s->msg_callback(1, 0, SSL3_RT_HEADER, buf,
787 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
789 if ((tmpclient = BIO_ADDR_new()) == NULL) {
790 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);
795 * This is unnecessary if rbio and wbio are one and the same - but
796 * maybe they're not. We ignore errors here - some BIOs do not
799 if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {
800 (void)BIO_dgram_set_peer(wbio, tmpclient);
802 BIO_ADDR_free(tmpclient);
805 /* TODO(size_t): convert this call */
806 if (BIO_write(wbio, wbuf, wreclen) < (int)wreclen) {
807 if (BIO_should_retry(wbio)) {
809 * Non-blocking IO...but we're stateless, so we're just
810 * going to drop this packet.
817 if (BIO_flush(wbio) <= 0) {
818 if (BIO_should_retry(wbio)) {
820 * Non-blocking IO...but we're stateless, so we're just
821 * going to drop this packet.
828 } while (next != LISTEN_SUCCESS);
831 * Set expected sequence numbers to continue the handshake.
833 s->d1->handshake_read_seq = 1;
834 s->d1->handshake_write_seq = 1;
835 s->d1->next_handshake_write_seq = 1;
836 DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);
839 * We are doing cookie exchange, so make sure we set that option in the
842 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
845 * Tell the state machine that we've done the initial hello verify
848 ossl_statem_set_hello_verify_done(s);
851 * Some BIOs may not support this. If we fail we clear the client address
853 if (BIO_dgram_get_peer(rbio, client) <= 0)
854 BIO_ADDR_clear(client);
860 BIO_ADDR_free(tmpclient);
861 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_PEEK_MODE, 0, NULL);
863 /* Dump this packet. Ignore return value */
864 BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH);
870 static int dtls1_handshake_write(SSL *s)
872 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
875 int dtls1_shutdown(SSL *s)
878 #ifndef OPENSSL_NO_SCTP
881 wbio = SSL_get_wbio(s);
882 if (wbio != NULL && BIO_dgram_is_sctp(wbio) &&
883 !(s->shutdown & SSL_SENT_SHUTDOWN)) {
884 ret = BIO_dgram_sctp_wait_for_dry(wbio);
889 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1,
893 ret = ssl3_shutdown(s);
894 #ifndef OPENSSL_NO_SCTP
895 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL);
900 int dtls1_query_mtu(SSL *s)
902 if (s->d1->link_mtu) {
904 s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
908 /* AHA! Figure out the MTU, and stick to the right size */
909 if (s->d1->mtu < dtls1_min_mtu(s)) {
910 if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
912 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
915 * I've seen the kernel return bogus numbers when it doesn't know
916 * (initial write), so just make sure we have a reasonable number
918 if (s->d1->mtu < dtls1_min_mtu(s)) {
920 s->d1->mtu = dtls1_min_mtu(s);
921 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
922 (long)s->d1->mtu, NULL);
930 static size_t dtls1_link_min_mtu(void)
932 return (g_probable_mtu[(sizeof(g_probable_mtu) /
933 sizeof(g_probable_mtu[0])) - 1]);
936 size_t dtls1_min_mtu(SSL *s)
938 return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
941 size_t DTLS_get_data_mtu(const SSL *s)
943 size_t mac_overhead, int_overhead, blocksize, ext_overhead;
944 const SSL_CIPHER *ciph = SSL_get_current_cipher(s);
945 size_t mtu = s->d1->mtu;
950 if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead,
951 &blocksize, &ext_overhead))
955 ext_overhead += mac_overhead;
957 int_overhead += mac_overhead;
959 /* Subtract external overhead (e.g. IV/nonce, separate MAC) */
960 if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu)
962 mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH;
964 /* Round encrypted payload down to cipher block size (for CBC etc.)
965 * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */
967 mtu -= (mtu % blocksize);
969 /* Subtract internal overhead (e.g. CBC padding len byte) */
970 if (int_overhead >= mtu)
977 void DTLS_set_timer_cb(SSL *s, DTLS_timer_cb cb)
979 s->d1->timer_cb = cb;