160662d10Schristos /*
2*1dcdf01fSchristos  * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
360662d10Schristos  *
4*1dcdf01fSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*1dcdf01fSchristos  * this file except in compliance with the License.  You can obtain a copy
6*1dcdf01fSchristos  * in the file LICENSE in the source distribution or at
7*1dcdf01fSchristos  * https://www.openssl.org/source/license.html
860662d10Schristos  */
960662d10Schristos 
10*1dcdf01fSchristos #include "e_os.h"
1160662d10Schristos #include <stdio.h>
1260662d10Schristos #include <openssl/objects.h>
13*1dcdf01fSchristos #include <openssl/rand.h>
14*1dcdf01fSchristos #include "ssl_local.h"
1560662d10Schristos 
1660662d10Schristos static void get_current_time(struct timeval *t);
1760662d10Schristos static int dtls1_handshake_write(SSL *s);
18*1dcdf01fSchristos static size_t dtls1_link_min_mtu(void);
1960662d10Schristos 
20*1dcdf01fSchristos /* XDTLS:  figure out the right values */
21*1dcdf01fSchristos static const size_t g_probable_mtu[] = { 1500, 512, 256 };
22*1dcdf01fSchristos 
23*1dcdf01fSchristos const SSL3_ENC_METHOD DTLSv1_enc_data = {
2460662d10Schristos     tls1_enc,
2560662d10Schristos     tls1_mac,
2660662d10Schristos     tls1_setup_key_block,
2760662d10Schristos     tls1_generate_master_secret,
2860662d10Schristos     tls1_change_cipher_state,
2960662d10Schristos     tls1_final_finish_mac,
3060662d10Schristos     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
3160662d10Schristos     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
3260662d10Schristos     tls1_alert_code,
3360662d10Schristos     tls1_export_keying_material,
3460662d10Schristos     SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV,
3560662d10Schristos     dtls1_set_handshake_header,
36*1dcdf01fSchristos     dtls1_close_construct_packet,
3760662d10Schristos     dtls1_handshake_write
3860662d10Schristos };
3960662d10Schristos 
40*1dcdf01fSchristos const SSL3_ENC_METHOD DTLSv1_2_enc_data = {
4160662d10Schristos     tls1_enc,
4260662d10Schristos     tls1_mac,
4360662d10Schristos     tls1_setup_key_block,
4460662d10Schristos     tls1_generate_master_secret,
4560662d10Schristos     tls1_change_cipher_state,
4660662d10Schristos     tls1_final_finish_mac,
4760662d10Schristos     TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
4860662d10Schristos     TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
4960662d10Schristos     tls1_alert_code,
5060662d10Schristos     tls1_export_keying_material,
5160662d10Schristos     SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS
5260662d10Schristos         | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS,
5360662d10Schristos     dtls1_set_handshake_header,
54*1dcdf01fSchristos     dtls1_close_construct_packet,
5560662d10Schristos     dtls1_handshake_write
5660662d10Schristos };
5760662d10Schristos 
dtls1_default_timeout(void)5860662d10Schristos long dtls1_default_timeout(void)
5960662d10Schristos {
6060662d10Schristos     /*
6160662d10Schristos      * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
6260662d10Schristos      * http, the cache would over fill
6360662d10Schristos      */
6460662d10Schristos     return (60 * 60 * 2);
6560662d10Schristos }
6660662d10Schristos 
dtls1_new(SSL * s)6760662d10Schristos int dtls1_new(SSL *s)
6860662d10Schristos {
6960662d10Schristos     DTLS1_STATE *d1;
7060662d10Schristos 
71*1dcdf01fSchristos     if (!DTLS_RECORD_LAYER_new(&s->rlayer)) {
72*1dcdf01fSchristos         return 0;
73*1dcdf01fSchristos     }
74*1dcdf01fSchristos 
7560662d10Schristos     if (!ssl3_new(s))
76*1dcdf01fSchristos         return 0;
77*1dcdf01fSchristos     if ((d1 = OPENSSL_zalloc(sizeof(*d1))) == NULL) {
78*1dcdf01fSchristos         ssl3_free(s);
79*1dcdf01fSchristos         return 0;
80*1dcdf01fSchristos     }
8160662d10Schristos 
8260662d10Schristos     d1->buffered_messages = pqueue_new();
8360662d10Schristos     d1->sent_messages = pqueue_new();
8460662d10Schristos 
8560662d10Schristos     if (s->server) {
8660662d10Schristos         d1->cookie_len = sizeof(s->d1->cookie);
8760662d10Schristos     }
8860662d10Schristos 
8960662d10Schristos     d1->link_mtu = 0;
9060662d10Schristos     d1->mtu = 0;
9160662d10Schristos 
92*1dcdf01fSchristos     if (d1->buffered_messages == NULL || d1->sent_messages == NULL) {
9360662d10Schristos         pqueue_free(d1->buffered_messages);
9460662d10Schristos         pqueue_free(d1->sent_messages);
9560662d10Schristos         OPENSSL_free(d1);
96*1dcdf01fSchristos         ssl3_free(s);
97*1dcdf01fSchristos         return 0;
9860662d10Schristos     }
9960662d10Schristos 
10060662d10Schristos     s->d1 = d1;
101*1dcdf01fSchristos 
102*1dcdf01fSchristos     if (!s->method->ssl_clear(s))
103*1dcdf01fSchristos         return 0;
104*1dcdf01fSchristos 
105*1dcdf01fSchristos     return 1;
10660662d10Schristos }
10760662d10Schristos 
dtls1_clear_queues(SSL * s)10860662d10Schristos static void dtls1_clear_queues(SSL *s)
10960662d10Schristos {
11060662d10Schristos     dtls1_clear_received_buffer(s);
11160662d10Schristos     dtls1_clear_sent_buffer(s);
11260662d10Schristos }
11360662d10Schristos 
dtls1_clear_received_buffer(SSL * s)11460662d10Schristos void dtls1_clear_received_buffer(SSL *s)
11560662d10Schristos {
11660662d10Schristos     pitem *item = NULL;
11760662d10Schristos     hm_fragment *frag = NULL;
11860662d10Schristos 
11960662d10Schristos     while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
12060662d10Schristos         frag = (hm_fragment *)item->data;
12160662d10Schristos         dtls1_hm_fragment_free(frag);
12260662d10Schristos         pitem_free(item);
12360662d10Schristos     }
12460662d10Schristos }
12560662d10Schristos 
dtls1_clear_sent_buffer(SSL * s)12660662d10Schristos void dtls1_clear_sent_buffer(SSL *s)
12760662d10Schristos {
12860662d10Schristos     pitem *item = NULL;
12960662d10Schristos     hm_fragment *frag = NULL;
13060662d10Schristos 
13160662d10Schristos     while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
13260662d10Schristos         frag = (hm_fragment *)item->data;
13360662d10Schristos         dtls1_hm_fragment_free(frag);
13460662d10Schristos         pitem_free(item);
13560662d10Schristos     }
13660662d10Schristos }
13760662d10Schristos 
13860662d10Schristos 
dtls1_free(SSL * s)13960662d10Schristos void dtls1_free(SSL *s)
14060662d10Schristos {
141*1dcdf01fSchristos     DTLS_RECORD_LAYER_free(&s->rlayer);
142*1dcdf01fSchristos 
14360662d10Schristos     ssl3_free(s);
14460662d10Schristos 
145*1dcdf01fSchristos     if (s->d1 != NULL) {
14660662d10Schristos         dtls1_clear_queues(s);
14760662d10Schristos         pqueue_free(s->d1->buffered_messages);
14860662d10Schristos         pqueue_free(s->d1->sent_messages);
149*1dcdf01fSchristos     }
15060662d10Schristos 
15160662d10Schristos     OPENSSL_free(s->d1);
15260662d10Schristos     s->d1 = NULL;
15360662d10Schristos }
15460662d10Schristos 
dtls1_clear(SSL * s)155*1dcdf01fSchristos int dtls1_clear(SSL *s)
15660662d10Schristos {
157*1dcdf01fSchristos     pqueue *buffered_messages;
158*1dcdf01fSchristos     pqueue *sent_messages;
159*1dcdf01fSchristos     size_t mtu;
160*1dcdf01fSchristos     size_t link_mtu;
161*1dcdf01fSchristos 
162*1dcdf01fSchristos     DTLS_RECORD_LAYER_clear(&s->rlayer);
16360662d10Schristos 
16460662d10Schristos     if (s->d1) {
165*1dcdf01fSchristos         DTLS_timer_cb timer_cb = s->d1->timer_cb;
166*1dcdf01fSchristos 
16760662d10Schristos         buffered_messages = s->d1->buffered_messages;
16860662d10Schristos         sent_messages = s->d1->sent_messages;
16960662d10Schristos         mtu = s->d1->mtu;
17060662d10Schristos         link_mtu = s->d1->link_mtu;
17160662d10Schristos 
17260662d10Schristos         dtls1_clear_queues(s);
17360662d10Schristos 
174*1dcdf01fSchristos         memset(s->d1, 0, sizeof(*s->d1));
175*1dcdf01fSchristos 
176*1dcdf01fSchristos         /* Restore the timer callback from previous state */
177*1dcdf01fSchristos         s->d1->timer_cb = timer_cb;
17860662d10Schristos 
17960662d10Schristos         if (s->server) {
18060662d10Schristos             s->d1->cookie_len = sizeof(s->d1->cookie);
18160662d10Schristos         }
18260662d10Schristos 
18360662d10Schristos         if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
18460662d10Schristos             s->d1->mtu = mtu;
18560662d10Schristos             s->d1->link_mtu = link_mtu;
18660662d10Schristos         }
18760662d10Schristos 
18860662d10Schristos         s->d1->buffered_messages = buffered_messages;
18960662d10Schristos         s->d1->sent_messages = sent_messages;
19060662d10Schristos     }
19160662d10Schristos 
192*1dcdf01fSchristos     if (!ssl3_clear(s))
193*1dcdf01fSchristos         return 0;
194*1dcdf01fSchristos 
195*1dcdf01fSchristos     if (s->method->version == DTLS_ANY_VERSION)
196*1dcdf01fSchristos         s->version = DTLS_MAX_VERSION;
197*1dcdf01fSchristos #ifndef OPENSSL_NO_DTLS1_METHOD
198*1dcdf01fSchristos     else if (s->options & SSL_OP_CISCO_ANYCONNECT)
19960662d10Schristos         s->client_version = s->version = DTLS1_BAD_VER;
200*1dcdf01fSchristos #endif
20160662d10Schristos     else
20260662d10Schristos         s->version = s->method->version;
203*1dcdf01fSchristos 
204*1dcdf01fSchristos     return 1;
20560662d10Schristos }
20660662d10Schristos 
dtls1_ctrl(SSL * s,int cmd,long larg,void * parg)20760662d10Schristos long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
20860662d10Schristos {
20960662d10Schristos     int ret = 0;
21060662d10Schristos 
21160662d10Schristos     switch (cmd) {
21260662d10Schristos     case DTLS_CTRL_GET_TIMEOUT:
21360662d10Schristos         if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) {
21460662d10Schristos             ret = 1;
21560662d10Schristos         }
21660662d10Schristos         break;
21760662d10Schristos     case DTLS_CTRL_HANDLE_TIMEOUT:
21860662d10Schristos         ret = dtls1_handle_timeout(s);
21960662d10Schristos         break;
22060662d10Schristos     case DTLS_CTRL_SET_LINK_MTU:
22160662d10Schristos         if (larg < (long)dtls1_link_min_mtu())
22260662d10Schristos             return 0;
22360662d10Schristos         s->d1->link_mtu = larg;
22460662d10Schristos         return 1;
22560662d10Schristos     case DTLS_CTRL_GET_LINK_MIN_MTU:
22660662d10Schristos         return (long)dtls1_link_min_mtu();
22760662d10Schristos     case SSL_CTRL_SET_MTU:
22860662d10Schristos         /*
22960662d10Schristos          *  We may not have a BIO set yet so can't call dtls1_min_mtu()
23060662d10Schristos          *  We'll have to make do with dtls1_link_min_mtu() and max overhead
23160662d10Schristos          */
23260662d10Schristos         if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD)
23360662d10Schristos             return 0;
23460662d10Schristos         s->d1->mtu = larg;
23560662d10Schristos         return larg;
23660662d10Schristos     default:
23760662d10Schristos         ret = ssl3_ctrl(s, cmd, larg, parg);
23860662d10Schristos         break;
23960662d10Schristos     }
240*1dcdf01fSchristos     return ret;
24160662d10Schristos }
24260662d10Schristos 
dtls1_start_timer(SSL * s)24360662d10Schristos void dtls1_start_timer(SSL *s)
24460662d10Schristos {
245*1dcdf01fSchristos     unsigned int sec, usec;
246*1dcdf01fSchristos 
24760662d10Schristos #ifndef OPENSSL_NO_SCTP
24860662d10Schristos     /* Disable timer for SCTP */
24960662d10Schristos     if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
250*1dcdf01fSchristos         memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
25160662d10Schristos         return;
25260662d10Schristos     }
25360662d10Schristos #endif
25460662d10Schristos 
255*1dcdf01fSchristos     /*
256*1dcdf01fSchristos      * If timer is not set, initialize duration with 1 second or
257*1dcdf01fSchristos      * a user-specified value if the timer callback is installed.
258*1dcdf01fSchristos      */
25960662d10Schristos     if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
260*1dcdf01fSchristos 
261*1dcdf01fSchristos         if (s->d1->timer_cb != NULL)
262*1dcdf01fSchristos             s->d1->timeout_duration_us = s->d1->timer_cb(s, 0);
263*1dcdf01fSchristos         else
264*1dcdf01fSchristos             s->d1->timeout_duration_us = 1000000;
26560662d10Schristos     }
26660662d10Schristos 
26760662d10Schristos     /* Set timeout to current time */
26860662d10Schristos     get_current_time(&(s->d1->next_timeout));
26960662d10Schristos 
27060662d10Schristos     /* Add duration to current time */
271*1dcdf01fSchristos 
272*1dcdf01fSchristos     sec  = s->d1->timeout_duration_us / 1000000;
273*1dcdf01fSchristos     usec = s->d1->timeout_duration_us - (sec * 1000000);
274*1dcdf01fSchristos 
275*1dcdf01fSchristos     s->d1->next_timeout.tv_sec  += sec;
276*1dcdf01fSchristos     s->d1->next_timeout.tv_usec += usec;
277*1dcdf01fSchristos 
278*1dcdf01fSchristos     if (s->d1->next_timeout.tv_usec >= 1000000) {
279*1dcdf01fSchristos         s->d1->next_timeout.tv_sec++;
280*1dcdf01fSchristos         s->d1->next_timeout.tv_usec -= 1000000;
281*1dcdf01fSchristos     }
282*1dcdf01fSchristos 
28360662d10Schristos     BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
28460662d10Schristos              &(s->d1->next_timeout));
28560662d10Schristos }
28660662d10Schristos 
dtls1_get_timeout(SSL * s,struct timeval * timeleft)28760662d10Schristos struct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
28860662d10Schristos {
28960662d10Schristos     struct timeval timenow;
29060662d10Schristos 
29160662d10Schristos     /* If no timeout is set, just return NULL */
29260662d10Schristos     if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
29360662d10Schristos         return NULL;
29460662d10Schristos     }
29560662d10Schristos 
29660662d10Schristos     /* Get current time */
29760662d10Schristos     get_current_time(&timenow);
29860662d10Schristos 
29960662d10Schristos     /* If timer already expired, set remaining time to 0 */
30060662d10Schristos     if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
30160662d10Schristos         (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
30260662d10Schristos          s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
303*1dcdf01fSchristos         memset(timeleft, 0, sizeof(*timeleft));
30460662d10Schristos         return timeleft;
30560662d10Schristos     }
30660662d10Schristos 
30760662d10Schristos     /* Calculate time left until timer expires */
30860662d10Schristos     memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval));
30960662d10Schristos     timeleft->tv_sec -= timenow.tv_sec;
31060662d10Schristos     timeleft->tv_usec -= timenow.tv_usec;
31160662d10Schristos     if (timeleft->tv_usec < 0) {
31260662d10Schristos         timeleft->tv_sec--;
31360662d10Schristos         timeleft->tv_usec += 1000000;
31460662d10Schristos     }
31560662d10Schristos 
31660662d10Schristos     /*
31760662d10Schristos      * If remaining time is less than 15 ms, set it to 0 to prevent issues
318*1dcdf01fSchristos      * because of small divergences with socket timeouts.
31960662d10Schristos      */
32060662d10Schristos     if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
321*1dcdf01fSchristos         memset(timeleft, 0, sizeof(*timeleft));
32260662d10Schristos     }
32360662d10Schristos 
32460662d10Schristos     return timeleft;
32560662d10Schristos }
32660662d10Schristos 
dtls1_is_timer_expired(SSL * s)32760662d10Schristos int dtls1_is_timer_expired(SSL *s)
32860662d10Schristos {
32960662d10Schristos     struct timeval timeleft;
33060662d10Schristos 
33160662d10Schristos     /* Get time left until timeout, return false if no timer running */
33260662d10Schristos     if (dtls1_get_timeout(s, &timeleft) == NULL) {
33360662d10Schristos         return 0;
33460662d10Schristos     }
33560662d10Schristos 
33660662d10Schristos     /* Return false if timer is not expired yet */
33760662d10Schristos     if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
33860662d10Schristos         return 0;
33960662d10Schristos     }
34060662d10Schristos 
34160662d10Schristos     /* Timer expired, so return true */
34260662d10Schristos     return 1;
34360662d10Schristos }
34460662d10Schristos 
dtls1_double_timeout(SSL * s)345*1dcdf01fSchristos static void dtls1_double_timeout(SSL *s)
34660662d10Schristos {
347*1dcdf01fSchristos     s->d1->timeout_duration_us *= 2;
348*1dcdf01fSchristos     if (s->d1->timeout_duration_us > 60000000)
349*1dcdf01fSchristos         s->d1->timeout_duration_us = 60000000;
35060662d10Schristos }
35160662d10Schristos 
dtls1_stop_timer(SSL * s)35260662d10Schristos void dtls1_stop_timer(SSL *s)
35360662d10Schristos {
35460662d10Schristos     /* Reset everything */
355*1dcdf01fSchristos     memset(&s->d1->timeout, 0, sizeof(s->d1->timeout));
356*1dcdf01fSchristos     memset(&s->d1->next_timeout, 0, sizeof(s->d1->next_timeout));
357*1dcdf01fSchristos     s->d1->timeout_duration_us = 1000000;
35860662d10Schristos     BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
35960662d10Schristos              &(s->d1->next_timeout));
36060662d10Schristos     /* Clear retransmission buffer */
36160662d10Schristos     dtls1_clear_sent_buffer(s);
36260662d10Schristos }
36360662d10Schristos 
dtls1_check_timeout_num(SSL * s)36460662d10Schristos int dtls1_check_timeout_num(SSL *s)
36560662d10Schristos {
366*1dcdf01fSchristos     size_t mtu;
36760662d10Schristos 
36860662d10Schristos     s->d1->timeout.num_alerts++;
36960662d10Schristos 
37060662d10Schristos     /* Reduce MTU after 2 unsuccessful retransmissions */
37160662d10Schristos     if (s->d1->timeout.num_alerts > 2
37260662d10Schristos         && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
37360662d10Schristos         mtu =
374*1dcdf01fSchristos             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
37560662d10Schristos         if (mtu < s->d1->mtu)
37660662d10Schristos             s->d1->mtu = mtu;
37760662d10Schristos     }
37860662d10Schristos 
37960662d10Schristos     if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
38060662d10Schristos         /* fail the connection, enough alerts have been sent */
381*1dcdf01fSchristos         SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_CHECK_TIMEOUT_NUM,
382*1dcdf01fSchristos                  SSL_R_READ_TIMEOUT_EXPIRED);
38360662d10Schristos         return -1;
38460662d10Schristos     }
38560662d10Schristos 
38660662d10Schristos     return 0;
38760662d10Schristos }
38860662d10Schristos 
dtls1_handle_timeout(SSL * s)38960662d10Schristos int dtls1_handle_timeout(SSL *s)
39060662d10Schristos {
39160662d10Schristos     /* if no timer is expired, don't do anything */
39260662d10Schristos     if (!dtls1_is_timer_expired(s)) {
39360662d10Schristos         return 0;
39460662d10Schristos     }
39560662d10Schristos 
396*1dcdf01fSchristos     if (s->d1->timer_cb != NULL)
397*1dcdf01fSchristos         s->d1->timeout_duration_us = s->d1->timer_cb(s, s->d1->timeout_duration_us);
398*1dcdf01fSchristos     else
39960662d10Schristos         dtls1_double_timeout(s);
40060662d10Schristos 
401*1dcdf01fSchristos     if (dtls1_check_timeout_num(s) < 0) {
402*1dcdf01fSchristos         /* SSLfatal() already called */
40360662d10Schristos         return -1;
404*1dcdf01fSchristos     }
40560662d10Schristos 
40660662d10Schristos     s->d1->timeout.read_timeouts++;
40760662d10Schristos     if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
40860662d10Schristos         s->d1->timeout.read_timeouts = 1;
40960662d10Schristos     }
41060662d10Schristos 
41160662d10Schristos     dtls1_start_timer(s);
412*1dcdf01fSchristos     /* Calls SSLfatal() if required */
41360662d10Schristos     return dtls1_retransmit_buffered_messages(s);
41460662d10Schristos }
41560662d10Schristos 
get_current_time(struct timeval * t)41660662d10Schristos static void get_current_time(struct timeval *t)
41760662d10Schristos {
41860662d10Schristos #if defined(_WIN32)
41960662d10Schristos     SYSTEMTIME st;
42060662d10Schristos     union {
42160662d10Schristos         unsigned __int64 ul;
42260662d10Schristos         FILETIME ft;
42360662d10Schristos     } now;
42460662d10Schristos 
42560662d10Schristos     GetSystemTime(&st);
42660662d10Schristos     SystemTimeToFileTime(&st, &now.ft);
427*1dcdf01fSchristos     /* re-bias to 1/1/1970 */
42860662d10Schristos # ifdef  __MINGW32__
42960662d10Schristos     now.ul -= 116444736000000000ULL;
43060662d10Schristos # else
431*1dcdf01fSchristos     /* *INDENT-OFF* */
432*1dcdf01fSchristos     now.ul -= 116444736000000000UI64;
433*1dcdf01fSchristos     /* *INDENT-ON* */
43460662d10Schristos # endif
43560662d10Schristos     t->tv_sec = (long)(now.ul / 10000000);
43660662d10Schristos     t->tv_usec = ((int)(now.ul % 10000000)) / 10;
43760662d10Schristos #else
43860662d10Schristos     gettimeofday(t, NULL);
43960662d10Schristos #endif
44060662d10Schristos }
44160662d10Schristos 
442*1dcdf01fSchristos #define LISTEN_SUCCESS              2
443*1dcdf01fSchristos #define LISTEN_SEND_VERIFY_REQUEST  1
444*1dcdf01fSchristos 
445*1dcdf01fSchristos #ifndef OPENSSL_NO_SOCK
DTLSv1_listen(SSL * s,BIO_ADDR * client)446*1dcdf01fSchristos int DTLSv1_listen(SSL *s, BIO_ADDR *client)
44760662d10Schristos {
448*1dcdf01fSchristos     int next, n, ret = 0;
449*1dcdf01fSchristos     unsigned char cookie[DTLS1_COOKIE_LENGTH];
450*1dcdf01fSchristos     unsigned char seq[SEQ_NUM_SIZE];
451*1dcdf01fSchristos     const unsigned char *data;
452*1dcdf01fSchristos     unsigned char *buf, *wbuf;
453*1dcdf01fSchristos     size_t fragoff, fraglen, msglen, reclen, align = 0;
454*1dcdf01fSchristos     unsigned int rectype, versmajor, msgseq, msgtype, clientvers, cookielen;
455*1dcdf01fSchristos     BIO *rbio, *wbio;
456*1dcdf01fSchristos     BIO_ADDR *tmpclient = NULL;
457*1dcdf01fSchristos     PACKET pkt, msgpkt, msgpayload, session, cookiepkt;
458*1dcdf01fSchristos 
459*1dcdf01fSchristos     if (s->handshake_func == NULL) {
460*1dcdf01fSchristos         /* Not properly initialized yet */
461*1dcdf01fSchristos         SSL_set_accept_state(s);
462*1dcdf01fSchristos     }
46360662d10Schristos 
46460662d10Schristos     /* Ensure there is no state left over from a previous invocation */
465*1dcdf01fSchristos     if (!SSL_clear(s))
466*1dcdf01fSchristos         return -1;
46760662d10Schristos 
468*1dcdf01fSchristos     ERR_clear_error();
469*1dcdf01fSchristos 
470*1dcdf01fSchristos     rbio = SSL_get_rbio(s);
471*1dcdf01fSchristos     wbio = SSL_get_wbio(s);
472*1dcdf01fSchristos 
473*1dcdf01fSchristos     if (!rbio || !wbio) {
474*1dcdf01fSchristos         SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BIO_NOT_SET);
475*1dcdf01fSchristos         return -1;
476*1dcdf01fSchristos     }
477*1dcdf01fSchristos 
478*1dcdf01fSchristos     /*
479*1dcdf01fSchristos      * Note: This check deliberately excludes DTLS1_BAD_VER because that version
480*1dcdf01fSchristos      * requires the MAC to be calculated *including* the first ClientHello
481*1dcdf01fSchristos      * (without the cookie). Since DTLSv1_listen is stateless that cannot be
482*1dcdf01fSchristos      * supported. DTLS1_BAD_VER must use cookies in a stateful manner (e.g. via
483*1dcdf01fSchristos      * SSL_accept)
484*1dcdf01fSchristos      */
485*1dcdf01fSchristos     if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00)) {
486*1dcdf01fSchristos         SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNSUPPORTED_SSL_VERSION);
487*1dcdf01fSchristos         return -1;
488*1dcdf01fSchristos     }
489*1dcdf01fSchristos 
490*1dcdf01fSchristos     if (!ssl3_setup_buffers(s)) {
491*1dcdf01fSchristos         /* SSLerr already called */
492*1dcdf01fSchristos         return -1;
493*1dcdf01fSchristos     }
494*1dcdf01fSchristos     buf = RECORD_LAYER_get_rbuf(&s->rlayer)->buf;
495*1dcdf01fSchristos     wbuf = RECORD_LAYER_get_wbuf(&s->rlayer)[0].buf;
496*1dcdf01fSchristos #if defined(SSL3_ALIGN_PAYLOAD)
497*1dcdf01fSchristos # if SSL3_ALIGN_PAYLOAD != 0
498*1dcdf01fSchristos     /*
499*1dcdf01fSchristos      * Using SSL3_RT_HEADER_LENGTH here instead of DTLS1_RT_HEADER_LENGTH for
500*1dcdf01fSchristos      * consistency with ssl3_read_n. In practice it should make no difference
501*1dcdf01fSchristos      * for sensible values of SSL3_ALIGN_PAYLOAD because the difference between
502*1dcdf01fSchristos      * SSL3_RT_HEADER_LENGTH and DTLS1_RT_HEADER_LENGTH is exactly 8
503*1dcdf01fSchristos      */
504*1dcdf01fSchristos     align = (size_t)buf + SSL3_RT_HEADER_LENGTH;
505*1dcdf01fSchristos     align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);
506*1dcdf01fSchristos # endif
507*1dcdf01fSchristos #endif
508*1dcdf01fSchristos     buf += align;
509*1dcdf01fSchristos 
510*1dcdf01fSchristos     do {
511*1dcdf01fSchristos         /* Get a packet */
512*1dcdf01fSchristos 
513*1dcdf01fSchristos         clear_sys_error();
514*1dcdf01fSchristos         n = BIO_read(rbio, buf, SSL3_RT_MAX_PLAIN_LENGTH
515*1dcdf01fSchristos                                 + DTLS1_RT_HEADER_LENGTH);
516*1dcdf01fSchristos         if (n <= 0) {
517*1dcdf01fSchristos             if (BIO_should_retry(rbio)) {
518*1dcdf01fSchristos                 /* Non-blocking IO */
519*1dcdf01fSchristos                 goto end;
520*1dcdf01fSchristos             }
521*1dcdf01fSchristos             return -1;
522*1dcdf01fSchristos         }
523*1dcdf01fSchristos 
524*1dcdf01fSchristos         if (!PACKET_buf_init(&pkt, buf, n)) {
525*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
526*1dcdf01fSchristos             return -1;
527*1dcdf01fSchristos         }
528*1dcdf01fSchristos 
529*1dcdf01fSchristos         /*
530*1dcdf01fSchristos          * Parse the received record. If there are any problems with it we just
531*1dcdf01fSchristos          * dump it - with no alert. RFC6347 says this "Unlike TLS, DTLS is
532*1dcdf01fSchristos          * resilient in the face of invalid records (e.g., invalid formatting,
533*1dcdf01fSchristos          * length, MAC, etc.).  In general, invalid records SHOULD be silently
534*1dcdf01fSchristos          * discarded, thus preserving the association; however, an error MAY be
535*1dcdf01fSchristos          * logged for diagnostic purposes."
536*1dcdf01fSchristos          */
537*1dcdf01fSchristos 
538*1dcdf01fSchristos         /* this packet contained a partial record, dump it */
539*1dcdf01fSchristos         if (n < DTLS1_RT_HEADER_LENGTH) {
540*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_RECORD_TOO_SMALL);
541*1dcdf01fSchristos             goto end;
542*1dcdf01fSchristos         }
543*1dcdf01fSchristos 
544*1dcdf01fSchristos         if (s->msg_callback)
545*1dcdf01fSchristos             s->msg_callback(0, 0, SSL3_RT_HEADER, buf,
546*1dcdf01fSchristos                             DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
547*1dcdf01fSchristos 
548*1dcdf01fSchristos         /* Get the record header */
549*1dcdf01fSchristos         if (!PACKET_get_1(&pkt, &rectype)
550*1dcdf01fSchristos             || !PACKET_get_1(&pkt, &versmajor)) {
551*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
552*1dcdf01fSchristos             goto end;
553*1dcdf01fSchristos         }
554*1dcdf01fSchristos 
555*1dcdf01fSchristos         if (rectype != SSL3_RT_HANDSHAKE) {
556*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
557*1dcdf01fSchristos             goto end;
558*1dcdf01fSchristos         }
559*1dcdf01fSchristos 
560*1dcdf01fSchristos         /*
561*1dcdf01fSchristos          * Check record version number. We only check that the major version is
562*1dcdf01fSchristos          * the same.
563*1dcdf01fSchristos          */
564*1dcdf01fSchristos         if (versmajor != DTLS1_VERSION_MAJOR) {
565*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
566*1dcdf01fSchristos             goto end;
567*1dcdf01fSchristos         }
568*1dcdf01fSchristos 
569*1dcdf01fSchristos         if (!PACKET_forward(&pkt, 1)
570*1dcdf01fSchristos             /* Save the sequence number: 64 bits, with top 2 bytes = epoch */
571*1dcdf01fSchristos             || !PACKET_copy_bytes(&pkt, seq, SEQ_NUM_SIZE)
572*1dcdf01fSchristos             || !PACKET_get_length_prefixed_2(&pkt, &msgpkt)) {
573*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
574*1dcdf01fSchristos             goto end;
575*1dcdf01fSchristos         }
576*1dcdf01fSchristos         reclen = PACKET_remaining(&msgpkt);
577*1dcdf01fSchristos         /*
578*1dcdf01fSchristos          * We allow data remaining at the end of the packet because there could
579*1dcdf01fSchristos          * be a second record (but we ignore it)
580*1dcdf01fSchristos          */
581*1dcdf01fSchristos 
582*1dcdf01fSchristos         /* This is an initial ClientHello so the epoch has to be 0 */
583*1dcdf01fSchristos         if (seq[0] != 0 || seq[1] != 0) {
584*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
585*1dcdf01fSchristos             goto end;
586*1dcdf01fSchristos         }
587*1dcdf01fSchristos 
588*1dcdf01fSchristos         /* Get a pointer to the raw message for the later callback */
589*1dcdf01fSchristos         data = PACKET_data(&msgpkt);
590*1dcdf01fSchristos 
591*1dcdf01fSchristos         /* Finished processing the record header, now process the message */
592*1dcdf01fSchristos         if (!PACKET_get_1(&msgpkt, &msgtype)
593*1dcdf01fSchristos             || !PACKET_get_net_3_len(&msgpkt, &msglen)
594*1dcdf01fSchristos             || !PACKET_get_net_2(&msgpkt, &msgseq)
595*1dcdf01fSchristos             || !PACKET_get_net_3_len(&msgpkt, &fragoff)
596*1dcdf01fSchristos             || !PACKET_get_net_3_len(&msgpkt, &fraglen)
597*1dcdf01fSchristos             || !PACKET_get_sub_packet(&msgpkt, &msgpayload, fraglen)
598*1dcdf01fSchristos             || PACKET_remaining(&msgpkt) != 0) {
599*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
600*1dcdf01fSchristos             goto end;
601*1dcdf01fSchristos         }
602*1dcdf01fSchristos 
603*1dcdf01fSchristos         if (msgtype != SSL3_MT_CLIENT_HELLO) {
604*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_UNEXPECTED_MESSAGE);
605*1dcdf01fSchristos             goto end;
606*1dcdf01fSchristos         }
607*1dcdf01fSchristos 
608*1dcdf01fSchristos         /* Message sequence number can only be 0 or 1 */
609*1dcdf01fSchristos         if (msgseq > 2) {
610*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_INVALID_SEQUENCE_NUMBER);
611*1dcdf01fSchristos             goto end;
612*1dcdf01fSchristos         }
613*1dcdf01fSchristos 
614*1dcdf01fSchristos         /*
615*1dcdf01fSchristos          * We don't support fragment reassembly for ClientHellos whilst
616*1dcdf01fSchristos          * listening because that would require server side state (which is
617*1dcdf01fSchristos          * against the whole point of the ClientHello/HelloVerifyRequest
618*1dcdf01fSchristos          * mechanism). Instead we only look at the first ClientHello fragment
619*1dcdf01fSchristos          * and require that the cookie must be contained within it.
620*1dcdf01fSchristos          */
621*1dcdf01fSchristos         if (fragoff != 0 || fraglen > msglen) {
622*1dcdf01fSchristos             /* Non initial ClientHello fragment (or bad fragment) */
623*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_FRAGMENTED_CLIENT_HELLO);
624*1dcdf01fSchristos             goto end;
625*1dcdf01fSchristos         }
626*1dcdf01fSchristos 
627*1dcdf01fSchristos         if (s->msg_callback)
628*1dcdf01fSchristos             s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, data,
629*1dcdf01fSchristos                             fraglen + DTLS1_HM_HEADER_LENGTH, s,
630*1dcdf01fSchristos                             s->msg_callback_arg);
631*1dcdf01fSchristos 
632*1dcdf01fSchristos         if (!PACKET_get_net_2(&msgpayload, &clientvers)) {
633*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
634*1dcdf01fSchristos             goto end;
635*1dcdf01fSchristos         }
636*1dcdf01fSchristos 
637*1dcdf01fSchristos         /*
638*1dcdf01fSchristos          * Verify client version is supported
639*1dcdf01fSchristos          */
640*1dcdf01fSchristos         if (DTLS_VERSION_LT(clientvers, (unsigned int)s->method->version) &&
641*1dcdf01fSchristos             s->method->version != DTLS_ANY_VERSION) {
642*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_WRONG_VERSION_NUMBER);
643*1dcdf01fSchristos             goto end;
644*1dcdf01fSchristos         }
645*1dcdf01fSchristos 
646*1dcdf01fSchristos         if (!PACKET_forward(&msgpayload, SSL3_RANDOM_SIZE)
647*1dcdf01fSchristos             || !PACKET_get_length_prefixed_1(&msgpayload, &session)
648*1dcdf01fSchristos             || !PACKET_get_length_prefixed_1(&msgpayload, &cookiepkt)) {
649*1dcdf01fSchristos             /*
650*1dcdf01fSchristos              * Could be malformed or the cookie does not fit within the initial
651*1dcdf01fSchristos              * ClientHello fragment. Either way we can't handle it.
652*1dcdf01fSchristos              */
653*1dcdf01fSchristos             SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_LENGTH_MISMATCH);
654*1dcdf01fSchristos             goto end;
655*1dcdf01fSchristos         }
656*1dcdf01fSchristos 
657*1dcdf01fSchristos         /*
658*1dcdf01fSchristos          * Check if we have a cookie or not. If not we need to send a
659*1dcdf01fSchristos          * HelloVerifyRequest.
660*1dcdf01fSchristos          */
661*1dcdf01fSchristos         if (PACKET_remaining(&cookiepkt) == 0) {
662*1dcdf01fSchristos             next = LISTEN_SEND_VERIFY_REQUEST;
663*1dcdf01fSchristos         } else {
664*1dcdf01fSchristos             /*
665*1dcdf01fSchristos              * We have a cookie, so lets check it.
666*1dcdf01fSchristos              */
667*1dcdf01fSchristos             if (s->ctx->app_verify_cookie_cb == NULL) {
668*1dcdf01fSchristos                 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_NO_VERIFY_COOKIE_CALLBACK);
669*1dcdf01fSchristos                 /* This is fatal */
670*1dcdf01fSchristos                 return -1;
671*1dcdf01fSchristos             }
672*1dcdf01fSchristos             if (s->ctx->app_verify_cookie_cb(s, PACKET_data(&cookiepkt),
673*1dcdf01fSchristos                     (unsigned int)PACKET_remaining(&cookiepkt)) == 0) {
674*1dcdf01fSchristos                 /*
675*1dcdf01fSchristos                  * We treat invalid cookies in the same was as no cookie as
676*1dcdf01fSchristos                  * per RFC6347
677*1dcdf01fSchristos                  */
678*1dcdf01fSchristos                 next = LISTEN_SEND_VERIFY_REQUEST;
679*1dcdf01fSchristos             } else {
680*1dcdf01fSchristos                 /* Cookie verification succeeded */
681*1dcdf01fSchristos                 next = LISTEN_SUCCESS;
682*1dcdf01fSchristos             }
683*1dcdf01fSchristos         }
684*1dcdf01fSchristos 
685*1dcdf01fSchristos         if (next == LISTEN_SEND_VERIFY_REQUEST) {
686*1dcdf01fSchristos             WPACKET wpkt;
687*1dcdf01fSchristos             unsigned int version;
688*1dcdf01fSchristos             size_t wreclen;
689*1dcdf01fSchristos 
690*1dcdf01fSchristos             /*
691*1dcdf01fSchristos              * There was no cookie in the ClientHello so we need to send a
692*1dcdf01fSchristos              * HelloVerifyRequest. If this fails we do not worry about trying
693*1dcdf01fSchristos              * to resend, we just drop it.
694*1dcdf01fSchristos              */
695*1dcdf01fSchristos 
696*1dcdf01fSchristos             /* Generate the cookie */
697*1dcdf01fSchristos             if (s->ctx->app_gen_cookie_cb == NULL ||
698*1dcdf01fSchristos                 s->ctx->app_gen_cookie_cb(s, cookie, &cookielen) == 0 ||
699*1dcdf01fSchristos                 cookielen > 255) {
700*1dcdf01fSchristos                 SSLerr(SSL_F_DTLSV1_LISTEN, SSL_R_COOKIE_GEN_CALLBACK_FAILURE);
701*1dcdf01fSchristos                 /* This is fatal */
702*1dcdf01fSchristos                 return -1;
703*1dcdf01fSchristos             }
704*1dcdf01fSchristos 
705*1dcdf01fSchristos             /*
706*1dcdf01fSchristos              * Special case: for hello verify request, client version 1.0 and we
707*1dcdf01fSchristos              * haven't decided which version to use yet send back using version
708*1dcdf01fSchristos              * 1.0 header: otherwise some clients will ignore it.
709*1dcdf01fSchristos              */
710*1dcdf01fSchristos             version = (s->method->version == DTLS_ANY_VERSION) ? DTLS1_VERSION
711*1dcdf01fSchristos                                                                : s->version;
712*1dcdf01fSchristos 
713*1dcdf01fSchristos             /* Construct the record and message headers */
714*1dcdf01fSchristos             if (!WPACKET_init_static_len(&wpkt,
715*1dcdf01fSchristos                                          wbuf,
716*1dcdf01fSchristos                                          ssl_get_max_send_fragment(s)
717*1dcdf01fSchristos                                          + DTLS1_RT_HEADER_LENGTH,
718*1dcdf01fSchristos                                          0)
719*1dcdf01fSchristos                     || !WPACKET_put_bytes_u8(&wpkt, SSL3_RT_HANDSHAKE)
720*1dcdf01fSchristos                     || !WPACKET_put_bytes_u16(&wpkt, version)
721*1dcdf01fSchristos                        /*
722*1dcdf01fSchristos                         * Record sequence number is always the same as in the
723*1dcdf01fSchristos                         * received ClientHello
724*1dcdf01fSchristos                         */
725*1dcdf01fSchristos                     || !WPACKET_memcpy(&wpkt, seq, SEQ_NUM_SIZE)
726*1dcdf01fSchristos                        /* End of record, start sub packet for message */
727*1dcdf01fSchristos                     || !WPACKET_start_sub_packet_u16(&wpkt)
728*1dcdf01fSchristos                        /* Message type */
729*1dcdf01fSchristos                     || !WPACKET_put_bytes_u8(&wpkt,
730*1dcdf01fSchristos                                              DTLS1_MT_HELLO_VERIFY_REQUEST)
731*1dcdf01fSchristos                        /*
732*1dcdf01fSchristos                         * Message length - doesn't follow normal TLS convention:
733*1dcdf01fSchristos                         * the length isn't the last thing in the message header.
734*1dcdf01fSchristos                         * We'll need to fill this in later when we know the
735*1dcdf01fSchristos                         * length. Set it to zero for now
736*1dcdf01fSchristos                         */
737*1dcdf01fSchristos                     || !WPACKET_put_bytes_u24(&wpkt, 0)
738*1dcdf01fSchristos                        /*
739*1dcdf01fSchristos                         * Message sequence number is always 0 for a
740*1dcdf01fSchristos                         * HelloVerifyRequest
741*1dcdf01fSchristos                         */
742*1dcdf01fSchristos                     || !WPACKET_put_bytes_u16(&wpkt, 0)
743*1dcdf01fSchristos                        /*
744*1dcdf01fSchristos                         * We never fragment a HelloVerifyRequest, so fragment
745*1dcdf01fSchristos                         * offset is 0
746*1dcdf01fSchristos                         */
747*1dcdf01fSchristos                     || !WPACKET_put_bytes_u24(&wpkt, 0)
748*1dcdf01fSchristos                        /*
749*1dcdf01fSchristos                         * Fragment length is the same as message length, but
750*1dcdf01fSchristos                         * this *is* the last thing in the message header so we
751*1dcdf01fSchristos                         * can just start a sub-packet. No need to come back
752*1dcdf01fSchristos                         * later for this one.
753*1dcdf01fSchristos                         */
754*1dcdf01fSchristos                     || !WPACKET_start_sub_packet_u24(&wpkt)
755*1dcdf01fSchristos                        /* Create the actual HelloVerifyRequest body */
756*1dcdf01fSchristos                     || !dtls_raw_hello_verify_request(&wpkt, cookie, cookielen)
757*1dcdf01fSchristos                        /* Close message body */
758*1dcdf01fSchristos                     || !WPACKET_close(&wpkt)
759*1dcdf01fSchristos                        /* Close record body */
760*1dcdf01fSchristos                     || !WPACKET_close(&wpkt)
761*1dcdf01fSchristos                     || !WPACKET_get_total_written(&wpkt, &wreclen)
762*1dcdf01fSchristos                     || !WPACKET_finish(&wpkt)) {
763*1dcdf01fSchristos                 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_INTERNAL_ERROR);
764*1dcdf01fSchristos                 WPACKET_cleanup(&wpkt);
765*1dcdf01fSchristos                 /* This is fatal */
766*1dcdf01fSchristos                 return -1;
767*1dcdf01fSchristos             }
768*1dcdf01fSchristos 
769*1dcdf01fSchristos             /*
770*1dcdf01fSchristos              * Fix up the message len in the message header. Its the same as the
771*1dcdf01fSchristos              * fragment len which has been filled in by WPACKET, so just copy
772*1dcdf01fSchristos              * that. Destination for the message len is after the record header
773*1dcdf01fSchristos              * plus one byte for the message content type. The source is the
774*1dcdf01fSchristos              * last 3 bytes of the message header
775*1dcdf01fSchristos              */
776*1dcdf01fSchristos             memcpy(&wbuf[DTLS1_RT_HEADER_LENGTH + 1],
777*1dcdf01fSchristos                    &wbuf[DTLS1_RT_HEADER_LENGTH + DTLS1_HM_HEADER_LENGTH - 3],
778*1dcdf01fSchristos                    3);
779*1dcdf01fSchristos 
780*1dcdf01fSchristos             if (s->msg_callback)
781*1dcdf01fSchristos                 s->msg_callback(1, 0, SSL3_RT_HEADER, buf,
782*1dcdf01fSchristos                                 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
783*1dcdf01fSchristos 
784*1dcdf01fSchristos             if ((tmpclient = BIO_ADDR_new()) == NULL) {
785*1dcdf01fSchristos                 SSLerr(SSL_F_DTLSV1_LISTEN, ERR_R_MALLOC_FAILURE);
786*1dcdf01fSchristos                 goto end;
787*1dcdf01fSchristos             }
788*1dcdf01fSchristos 
789*1dcdf01fSchristos             /*
790*1dcdf01fSchristos              * This is unnecessary if rbio and wbio are one and the same - but
791*1dcdf01fSchristos              * maybe they're not. We ignore errors here - some BIOs do not
792*1dcdf01fSchristos              * support this.
793*1dcdf01fSchristos              */
794*1dcdf01fSchristos             if (BIO_dgram_get_peer(rbio, tmpclient) > 0) {
795*1dcdf01fSchristos                 (void)BIO_dgram_set_peer(wbio, tmpclient);
796*1dcdf01fSchristos             }
797*1dcdf01fSchristos             BIO_ADDR_free(tmpclient);
798*1dcdf01fSchristos             tmpclient = NULL;
799*1dcdf01fSchristos 
800*1dcdf01fSchristos             /* TODO(size_t): convert this call */
801*1dcdf01fSchristos             if (BIO_write(wbio, wbuf, wreclen) < (int)wreclen) {
802*1dcdf01fSchristos                 if (BIO_should_retry(wbio)) {
803*1dcdf01fSchristos                     /*
804*1dcdf01fSchristos                      * Non-blocking IO...but we're stateless, so we're just
805*1dcdf01fSchristos                      * going to drop this packet.
806*1dcdf01fSchristos                      */
807*1dcdf01fSchristos                     goto end;
808*1dcdf01fSchristos                 }
809*1dcdf01fSchristos                 return -1;
810*1dcdf01fSchristos             }
811*1dcdf01fSchristos 
812*1dcdf01fSchristos             if (BIO_flush(wbio) <= 0) {
813*1dcdf01fSchristos                 if (BIO_should_retry(wbio)) {
814*1dcdf01fSchristos                     /*
815*1dcdf01fSchristos                      * Non-blocking IO...but we're stateless, so we're just
816*1dcdf01fSchristos                      * going to drop this packet.
817*1dcdf01fSchristos                      */
818*1dcdf01fSchristos                     goto end;
819*1dcdf01fSchristos                 }
820*1dcdf01fSchristos                 return -1;
821*1dcdf01fSchristos             }
822*1dcdf01fSchristos         }
823*1dcdf01fSchristos     } while (next != LISTEN_SUCCESS);
824*1dcdf01fSchristos 
825*1dcdf01fSchristos     /*
826*1dcdf01fSchristos      * Set expected sequence numbers to continue the handshake.
827*1dcdf01fSchristos      */
828*1dcdf01fSchristos     s->d1->handshake_read_seq = 1;
829*1dcdf01fSchristos     s->d1->handshake_write_seq = 1;
830*1dcdf01fSchristos     s->d1->next_handshake_write_seq = 1;
831*1dcdf01fSchristos     DTLS_RECORD_LAYER_set_write_sequence(&s->rlayer, seq);
832*1dcdf01fSchristos 
833*1dcdf01fSchristos     /*
834*1dcdf01fSchristos      * We are doing cookie exchange, so make sure we set that option in the
835*1dcdf01fSchristos      * SSL object
836*1dcdf01fSchristos      */
83760662d10Schristos     SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
83860662d10Schristos 
839*1dcdf01fSchristos     /*
840*1dcdf01fSchristos      * Tell the state machine that we've done the initial hello verify
841*1dcdf01fSchristos      * exchange
842*1dcdf01fSchristos      */
843*1dcdf01fSchristos     ossl_statem_set_hello_verify_done(s);
844*1dcdf01fSchristos 
845*1dcdf01fSchristos     /*
846*1dcdf01fSchristos      * Some BIOs may not support this. If we fail we clear the client address
847*1dcdf01fSchristos      */
848*1dcdf01fSchristos     if (BIO_dgram_get_peer(rbio, client) <= 0)
849*1dcdf01fSchristos         BIO_ADDR_clear(client);
850*1dcdf01fSchristos 
851*1dcdf01fSchristos     /* Buffer the record in the processed_rcds queue */
852*1dcdf01fSchristos     if (!dtls_buffer_listen_record(s, reclen, seq, align))
853*1dcdf01fSchristos         return -1;
854*1dcdf01fSchristos 
855*1dcdf01fSchristos     ret = 1;
856*1dcdf01fSchristos  end:
857*1dcdf01fSchristos     BIO_ADDR_free(tmpclient);
85860662d10Schristos     return ret;
85960662d10Schristos }
860*1dcdf01fSchristos #endif
86160662d10Schristos 
dtls1_handshake_write(SSL * s)86260662d10Schristos static int dtls1_handshake_write(SSL *s)
86360662d10Schristos {
86460662d10Schristos     return dtls1_do_write(s, SSL3_RT_HANDSHAKE);
86560662d10Schristos }
866*1dcdf01fSchristos 
dtls1_shutdown(SSL * s)867*1dcdf01fSchristos int dtls1_shutdown(SSL *s)
868*1dcdf01fSchristos {
869*1dcdf01fSchristos     int ret;
870*1dcdf01fSchristos #ifndef OPENSSL_NO_SCTP
871*1dcdf01fSchristos     BIO *wbio;
872*1dcdf01fSchristos 
873*1dcdf01fSchristos     wbio = SSL_get_wbio(s);
874*1dcdf01fSchristos     if (wbio != NULL && BIO_dgram_is_sctp(wbio) &&
875*1dcdf01fSchristos         !(s->shutdown & SSL_SENT_SHUTDOWN)) {
876*1dcdf01fSchristos         ret = BIO_dgram_sctp_wait_for_dry(wbio);
877*1dcdf01fSchristos         if (ret < 0)
878*1dcdf01fSchristos             return -1;
879*1dcdf01fSchristos 
880*1dcdf01fSchristos         if (ret == 0)
881*1dcdf01fSchristos             BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 1,
882*1dcdf01fSchristos                      NULL);
883*1dcdf01fSchristos     }
884*1dcdf01fSchristos #endif
885*1dcdf01fSchristos     ret = ssl3_shutdown(s);
886*1dcdf01fSchristos #ifndef OPENSSL_NO_SCTP
887*1dcdf01fSchristos     BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN, 0, NULL);
888*1dcdf01fSchristos #endif
889*1dcdf01fSchristos     return ret;
890*1dcdf01fSchristos }
891*1dcdf01fSchristos 
dtls1_query_mtu(SSL * s)892*1dcdf01fSchristos int dtls1_query_mtu(SSL *s)
893*1dcdf01fSchristos {
894*1dcdf01fSchristos     if (s->d1->link_mtu) {
895*1dcdf01fSchristos         s->d1->mtu =
896*1dcdf01fSchristos             s->d1->link_mtu - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
897*1dcdf01fSchristos         s->d1->link_mtu = 0;
898*1dcdf01fSchristos     }
899*1dcdf01fSchristos 
900*1dcdf01fSchristos     /* AHA!  Figure out the MTU, and stick to the right size */
901*1dcdf01fSchristos     if (s->d1->mtu < dtls1_min_mtu(s)) {
902*1dcdf01fSchristos         if (!(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
903*1dcdf01fSchristos             s->d1->mtu =
904*1dcdf01fSchristos                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
905*1dcdf01fSchristos 
906*1dcdf01fSchristos             /*
907*1dcdf01fSchristos              * I've seen the kernel return bogus numbers when it doesn't know
908*1dcdf01fSchristos              * (initial write), so just make sure we have a reasonable number
909*1dcdf01fSchristos              */
910*1dcdf01fSchristos             if (s->d1->mtu < dtls1_min_mtu(s)) {
911*1dcdf01fSchristos                 /* Set to min mtu */
912*1dcdf01fSchristos                 s->d1->mtu = dtls1_min_mtu(s);
913*1dcdf01fSchristos                 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SET_MTU,
914*1dcdf01fSchristos                          (long)s->d1->mtu, NULL);
915*1dcdf01fSchristos             }
916*1dcdf01fSchristos         } else
917*1dcdf01fSchristos             return 0;
918*1dcdf01fSchristos     }
919*1dcdf01fSchristos     return 1;
920*1dcdf01fSchristos }
921*1dcdf01fSchristos 
dtls1_link_min_mtu(void)922*1dcdf01fSchristos static size_t dtls1_link_min_mtu(void)
923*1dcdf01fSchristos {
924*1dcdf01fSchristos     return (g_probable_mtu[(sizeof(g_probable_mtu) /
925*1dcdf01fSchristos                             sizeof(g_probable_mtu[0])) - 1]);
926*1dcdf01fSchristos }
927*1dcdf01fSchristos 
dtls1_min_mtu(SSL * s)928*1dcdf01fSchristos size_t dtls1_min_mtu(SSL *s)
929*1dcdf01fSchristos {
930*1dcdf01fSchristos     return dtls1_link_min_mtu() - BIO_dgram_get_mtu_overhead(SSL_get_wbio(s));
931*1dcdf01fSchristos }
932*1dcdf01fSchristos 
DTLS_get_data_mtu(const SSL * s)933*1dcdf01fSchristos size_t DTLS_get_data_mtu(const SSL *s)
934*1dcdf01fSchristos {
935*1dcdf01fSchristos     size_t mac_overhead, int_overhead, blocksize, ext_overhead;
936*1dcdf01fSchristos     const SSL_CIPHER *ciph = SSL_get_current_cipher(s);
937*1dcdf01fSchristos     size_t mtu = s->d1->mtu;
938*1dcdf01fSchristos 
939*1dcdf01fSchristos     if (ciph == NULL)
940*1dcdf01fSchristos         return 0;
941*1dcdf01fSchristos 
942*1dcdf01fSchristos     if (!ssl_cipher_get_overhead(ciph, &mac_overhead, &int_overhead,
943*1dcdf01fSchristos                                  &blocksize, &ext_overhead))
944*1dcdf01fSchristos         return 0;
945*1dcdf01fSchristos 
946*1dcdf01fSchristos     if (SSL_READ_ETM(s))
947*1dcdf01fSchristos         ext_overhead += mac_overhead;
948*1dcdf01fSchristos     else
949*1dcdf01fSchristos         int_overhead += mac_overhead;
950*1dcdf01fSchristos 
951*1dcdf01fSchristos     /* Subtract external overhead (e.g. IV/nonce, separate MAC) */
952*1dcdf01fSchristos     if (ext_overhead + DTLS1_RT_HEADER_LENGTH >= mtu)
953*1dcdf01fSchristos         return 0;
954*1dcdf01fSchristos     mtu -= ext_overhead + DTLS1_RT_HEADER_LENGTH;
955*1dcdf01fSchristos 
956*1dcdf01fSchristos     /* Round encrypted payload down to cipher block size (for CBC etc.)
957*1dcdf01fSchristos      * No check for overflow since 'mtu % blocksize' cannot exceed mtu. */
958*1dcdf01fSchristos     if (blocksize)
959*1dcdf01fSchristos         mtu -= (mtu % blocksize);
960*1dcdf01fSchristos 
961*1dcdf01fSchristos     /* Subtract internal overhead (e.g. CBC padding len byte) */
962*1dcdf01fSchristos     if (int_overhead >= mtu)
963*1dcdf01fSchristos         return 0;
964*1dcdf01fSchristos     mtu -= int_overhead;
965*1dcdf01fSchristos 
966*1dcdf01fSchristos     return mtu;
967*1dcdf01fSchristos }
968*1dcdf01fSchristos 
DTLS_set_timer_cb(SSL * s,DTLS_timer_cb cb)969*1dcdf01fSchristos void DTLS_set_timer_cb(SSL *s, DTLS_timer_cb cb)
970*1dcdf01fSchristos {
971*1dcdf01fSchristos     s->d1->timer_cb = cb;
972*1dcdf01fSchristos }
973