d1_lib.c revision 280297
1160814Ssimon/* ssl/d1_lib.c */
2280297Sjkim/*
3160814Ssimon * DTLS implementation written by Nagendra Modadugu
4280297Sjkim * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    openssl-core@OpenSSL.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon
60160814Ssimon#include <stdio.h>
61205128Ssimon#define USE_SOCKETS
62160814Ssimon#include <openssl/objects.h>
63160814Ssimon#include "ssl_locl.h"
64160814Ssimon
65205128Ssimon#if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
66280297Sjkim# include <sys/timeb.h>
67205128Ssimon#endif
68205128Ssimon
69205128Ssimonstatic void get_current_time(struct timeval *t);
70280297Sjkimconst char dtls1_version_str[] = "DTLSv1" OPENSSL_VERSION_PTEXT;
71205128Ssimonint dtls1_listen(SSL *s, struct sockaddr *client);
72160814Ssimon
73280297SjkimSSL3_ENC_METHOD DTLSv1_enc_data = {
74160814Ssimon    dtls1_enc,
75280297Sjkim    tls1_mac,
76280297Sjkim    tls1_setup_key_block,
77280297Sjkim    tls1_generate_master_secret,
78280297Sjkim    tls1_change_cipher_state,
79280297Sjkim    tls1_final_finish_mac,
80280297Sjkim    TLS1_FINISH_MAC_LENGTH,
81280297Sjkim    tls1_cert_verify_mac,
82280297Sjkim    TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE,
83280297Sjkim    TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE,
84280297Sjkim    tls1_alert_code,
85280297Sjkim    tls1_export_keying_material,
86280297Sjkim};
87160814Ssimon
88160814Ssimonlong dtls1_default_timeout(void)
89280297Sjkim{
90280297Sjkim    /*
91280297Sjkim     * 2 hours, the 24 hours mentioned in the DTLSv1 spec is way too long for
92280297Sjkim     * http, the cache would over fill
93280297Sjkim     */
94280297Sjkim    return (60 * 60 * 2);
95280297Sjkim}
96160814Ssimon
97160814Ssimonint dtls1_new(SSL *s)
98280297Sjkim{
99280297Sjkim    DTLS1_STATE *d1;
100160814Ssimon
101280297Sjkim    if (!ssl3_new(s))
102280297Sjkim        return (0);
103280297Sjkim    if ((d1 = OPENSSL_malloc(sizeof *d1)) == NULL)
104280297Sjkim        return (0);
105280297Sjkim    memset(d1, 0, sizeof *d1);
106160814Ssimon
107280297Sjkim    /* d1->handshake_epoch=0; */
108160814Ssimon
109280297Sjkim    d1->unprocessed_rcds.q = pqueue_new();
110280297Sjkim    d1->processed_rcds.q = pqueue_new();
111280297Sjkim    d1->buffered_messages = pqueue_new();
112280297Sjkim    d1->sent_messages = pqueue_new();
113280297Sjkim    d1->buffered_app_data.q = pqueue_new();
114160814Ssimon
115280297Sjkim    if (s->server) {
116280297Sjkim        d1->cookie_len = sizeof(s->d1->cookie);
117280297Sjkim    }
118160814Ssimon
119280297Sjkim    d1->link_mtu = 0;
120280297Sjkim    d1->mtu = 0;
121276861Sjkim
122280297Sjkim    if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q
123280297Sjkim        || !d1->buffered_messages || !d1->sent_messages
124280297Sjkim        || !d1->buffered_app_data.q) {
125280297Sjkim        if (d1->unprocessed_rcds.q)
126280297Sjkim            pqueue_free(d1->unprocessed_rcds.q);
127280297Sjkim        if (d1->processed_rcds.q)
128280297Sjkim            pqueue_free(d1->processed_rcds.q);
129280297Sjkim        if (d1->buffered_messages)
130280297Sjkim            pqueue_free(d1->buffered_messages);
131280297Sjkim        if (d1->sent_messages)
132280297Sjkim            pqueue_free(d1->sent_messages);
133280297Sjkim        if (d1->buffered_app_data.q)
134280297Sjkim            pqueue_free(d1->buffered_app_data.q);
135280297Sjkim        OPENSSL_free(d1);
136280297Sjkim        return (0);
137280297Sjkim    }
138160814Ssimon
139280297Sjkim    s->d1 = d1;
140280297Sjkim    s->method->ssl_clear(s);
141280297Sjkim    return (1);
142280297Sjkim}
143160814Ssimon
144237657Sjkimstatic void dtls1_clear_queues(SSL *s)
145280297Sjkim{
146160814Ssimon    pitem *item = NULL;
147160814Ssimon    hm_fragment *frag = NULL;
148280297Sjkim    DTLS1_RECORD_DATA *rdata;
149160814Ssimon
150280297Sjkim    while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) {
151280297Sjkim        rdata = (DTLS1_RECORD_DATA *)item->data;
152280297Sjkim        if (rdata->rbuf.buf) {
153280297Sjkim            OPENSSL_free(rdata->rbuf.buf);
154280297Sjkim        }
155160814Ssimon        OPENSSL_free(item->data);
156160814Ssimon        pitem_free(item);
157280297Sjkim    }
158280297Sjkim
159280297Sjkim    while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) {
160280297Sjkim        rdata = (DTLS1_RECORD_DATA *)item->data;
161280297Sjkim        if (rdata->rbuf.buf) {
162280297Sjkim            OPENSSL_free(rdata->rbuf.buf);
163160814Ssimon        }
164160814Ssimon        OPENSSL_free(item->data);
165160814Ssimon        pitem_free(item);
166280297Sjkim    }
167160814Ssimon
168280297Sjkim    while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) {
169160814Ssimon        frag = (hm_fragment *)item->data;
170276861Sjkim        dtls1_hm_fragment_free(frag);
171160814Ssimon        pitem_free(item);
172280297Sjkim    }
173160814Ssimon
174280297Sjkim    while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) {
175160814Ssimon        frag = (hm_fragment *)item->data;
176276861Sjkim        dtls1_hm_fragment_free(frag);
177160814Ssimon        pitem_free(item);
178280297Sjkim    }
179280297Sjkim
180280297Sjkim    while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) {
181280297Sjkim        rdata = (DTLS1_RECORD_DATA *)item->data;
182280297Sjkim        if (rdata->rbuf.buf) {
183280297Sjkim            OPENSSL_free(rdata->rbuf.buf);
184160814Ssimon        }
185280297Sjkim        OPENSSL_free(item->data);
186280297Sjkim        pitem_free(item);
187280297Sjkim    }
188280297Sjkim}
189160814Ssimon
190237657Sjkimvoid dtls1_free(SSL *s)
191280297Sjkim{
192280297Sjkim    ssl3_free(s);
193237657Sjkim
194280297Sjkim    dtls1_clear_queues(s);
195237657Sjkim
196237657Sjkim    pqueue_free(s->d1->unprocessed_rcds.q);
197237657Sjkim    pqueue_free(s->d1->processed_rcds.q);
198237657Sjkim    pqueue_free(s->d1->buffered_messages);
199280297Sjkim    pqueue_free(s->d1->sent_messages);
200280297Sjkim    pqueue_free(s->d1->buffered_app_data.q);
201160814Ssimon
202280297Sjkim    OPENSSL_free(s->d1);
203280297Sjkim    s->d1 = NULL;
204280297Sjkim}
205160814Ssimon
206160814Ssimonvoid dtls1_clear(SSL *s)
207280297Sjkim{
208237657Sjkim    pqueue unprocessed_rcds;
209237657Sjkim    pqueue processed_rcds;
210237657Sjkim    pqueue buffered_messages;
211280297Sjkim    pqueue sent_messages;
212280297Sjkim    pqueue buffered_app_data;
213280297Sjkim    unsigned int mtu;
214280297Sjkim    unsigned int link_mtu;
215237657Sjkim
216280297Sjkim    if (s->d1) {
217280297Sjkim        unprocessed_rcds = s->d1->unprocessed_rcds.q;
218280297Sjkim        processed_rcds = s->d1->processed_rcds.q;
219280297Sjkim        buffered_messages = s->d1->buffered_messages;
220280297Sjkim        sent_messages = s->d1->sent_messages;
221280297Sjkim        buffered_app_data = s->d1->buffered_app_data.q;
222280297Sjkim        mtu = s->d1->mtu;
223280297Sjkim        link_mtu = s->d1->link_mtu;
224237657Sjkim
225280297Sjkim        dtls1_clear_queues(s);
226237657Sjkim
227280297Sjkim        memset(s->d1, 0, sizeof(*(s->d1)));
228237657Sjkim
229280297Sjkim        if (s->server) {
230280297Sjkim            s->d1->cookie_len = sizeof(s->d1->cookie);
231280297Sjkim        }
232237657Sjkim
233280297Sjkim        if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) {
234280297Sjkim            s->d1->mtu = mtu;
235280297Sjkim            s->d1->link_mtu = link_mtu;
236280297Sjkim        }
237237657Sjkim
238280297Sjkim        s->d1->unprocessed_rcds.q = unprocessed_rcds;
239280297Sjkim        s->d1->processed_rcds.q = processed_rcds;
240280297Sjkim        s->d1->buffered_messages = buffered_messages;
241280297Sjkim        s->d1->sent_messages = sent_messages;
242280297Sjkim        s->d1->buffered_app_data.q = buffered_app_data;
243280297Sjkim    }
244237657Sjkim
245280297Sjkim    ssl3_clear(s);
246280297Sjkim    if (s->options & SSL_OP_CISCO_ANYCONNECT)
247280297Sjkim        s->version = DTLS1_BAD_VER;
248280297Sjkim    else
249280297Sjkim        s->version = DTLS1_VERSION;
250280297Sjkim}
251194206Ssimon
252205128Ssimonlong dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
253280297Sjkim{
254280297Sjkim    int ret = 0;
255205128Ssimon
256280297Sjkim    switch (cmd) {
257280297Sjkim    case DTLS_CTRL_GET_TIMEOUT:
258280297Sjkim        if (dtls1_get_timeout(s, (struct timeval *)parg) != NULL) {
259280297Sjkim            ret = 1;
260280297Sjkim        }
261280297Sjkim        break;
262280297Sjkim    case DTLS_CTRL_HANDLE_TIMEOUT:
263280297Sjkim        ret = dtls1_handle_timeout(s);
264280297Sjkim        break;
265280297Sjkim    case DTLS_CTRL_LISTEN:
266280297Sjkim        ret = dtls1_listen(s, parg);
267280297Sjkim        break;
268280297Sjkim    case SSL_CTRL_CHECK_PROTO_VERSION:
269280297Sjkim        /*
270280297Sjkim         * For library-internal use; checks that the current protocol is the
271280297Sjkim         * highest enabled version (according to s->ctx->method, as version
272280297Sjkim         * negotiation may have changed s->method).
273280297Sjkim         */
274273144Sjkim#if DTLS_MAX_VERSION != DTLS1_VERSION
275280297Sjkim# error Code needs update for DTLS_method() support beyond DTLS1_VERSION.
276273144Sjkim#endif
277280297Sjkim        /*
278280297Sjkim         * Just one protocol version is supported so far; fail closed if the
279280297Sjkim         * version is not as expected.
280280297Sjkim         */
281280297Sjkim        return s->version == DTLS_MAX_VERSION;
282280297Sjkim    case DTLS_CTRL_SET_LINK_MTU:
283280297Sjkim        if (larg < (long)dtls1_link_min_mtu())
284280297Sjkim            return 0;
285280297Sjkim        s->d1->link_mtu = larg;
286280297Sjkim        return 1;
287280297Sjkim    case DTLS_CTRL_GET_LINK_MIN_MTU:
288280297Sjkim        return (long)dtls1_link_min_mtu();
289280297Sjkim    case SSL_CTRL_SET_MTU:
290280297Sjkim        /*
291280297Sjkim         *  We may not have a BIO set yet so can't call dtls1_min_mtu()
292280297Sjkim         *  We'll have to make do with dtls1_link_min_mtu() and max overhead
293280297Sjkim         */
294280297Sjkim        if (larg < (long)dtls1_link_min_mtu() - DTLS1_MAX_MTU_OVERHEAD)
295280297Sjkim            return 0;
296280297Sjkim        s->d1->mtu = larg;
297280297Sjkim        return larg;
298280297Sjkim    default:
299280297Sjkim        ret = ssl3_ctrl(s, cmd, larg, parg);
300280297Sjkim        break;
301280297Sjkim    }
302280297Sjkim    return (ret);
303280297Sjkim}
304205128Ssimon
305194206Ssimon/*
306194206Ssimon * As it's impossible to use stream ciphers in "datagram" mode, this
307194206Ssimon * simple filter is designed to disengage them in DTLS. Unfortunately
308194206Ssimon * there is no universal way to identify stream SSL_CIPHER, so we have
309194206Ssimon * to explicitly list their SSL_* codes. Currently RC4 is the only one
310194206Ssimon * available, but if new ones emerge, they will have to be added...
311194206Ssimon */
312238405Sjkimconst SSL_CIPHER *dtls1_get_cipher(unsigned int u)
313280297Sjkim{
314280297Sjkim    const SSL_CIPHER *ciph = ssl3_get_cipher(u);
315194206Ssimon
316280297Sjkim    if (ciph != NULL) {
317280297Sjkim        if (ciph->algorithm_enc == SSL_RC4)
318280297Sjkim            return NULL;
319280297Sjkim    }
320194206Ssimon
321280297Sjkim    return ciph;
322280297Sjkim}
323205128Ssimon
324205128Ssimonvoid dtls1_start_timer(SSL *s)
325280297Sjkim{
326238405Sjkim#ifndef OPENSSL_NO_SCTP
327280297Sjkim    /* Disable timer for SCTP */
328280297Sjkim    if (BIO_dgram_is_sctp(SSL_get_wbio(s))) {
329280297Sjkim        memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
330280297Sjkim        return;
331280297Sjkim    }
332238405Sjkim#endif
333238405Sjkim
334280297Sjkim    /* If timer is not set, initialize duration with 1 second */
335280297Sjkim    if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
336280297Sjkim        s->d1->timeout_duration = 1;
337280297Sjkim    }
338205128Ssimon
339280297Sjkim    /* Set timeout to current time */
340280297Sjkim    get_current_time(&(s->d1->next_timeout));
341205128Ssimon
342280297Sjkim    /* Add duration to current time */
343280297Sjkim    s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
344280297Sjkim    BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
345280297Sjkim             &(s->d1->next_timeout));
346280297Sjkim}
347205128Ssimon
348280297Sjkimstruct timeval *dtls1_get_timeout(SSL *s, struct timeval *timeleft)
349280297Sjkim{
350280297Sjkim    struct timeval timenow;
351205128Ssimon
352280297Sjkim    /* If no timeout is set, just return NULL */
353280297Sjkim    if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) {
354280297Sjkim        return NULL;
355280297Sjkim    }
356205128Ssimon
357280297Sjkim    /* Get current time */
358280297Sjkim    get_current_time(&timenow);
359205128Ssimon
360280297Sjkim    /* If timer already expired, set remaining time to 0 */
361280297Sjkim    if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
362280297Sjkim        (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
363280297Sjkim         s->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
364280297Sjkim        memset(timeleft, 0, sizeof(struct timeval));
365280297Sjkim        return timeleft;
366280297Sjkim    }
367205128Ssimon
368280297Sjkim    /* Calculate time left until timer expires */
369280297Sjkim    memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval));
370280297Sjkim    timeleft->tv_sec -= timenow.tv_sec;
371280297Sjkim    timeleft->tv_usec -= timenow.tv_usec;
372280297Sjkim    if (timeleft->tv_usec < 0) {
373280297Sjkim        timeleft->tv_sec--;
374280297Sjkim        timeleft->tv_usec += 1000000;
375280297Sjkim    }
376215697Ssimon
377280297Sjkim    /*
378280297Sjkim     * If remaining time is less than 15 ms, set it to 0 to prevent issues
379280297Sjkim     * because of small devergences with socket timeouts.
380280297Sjkim     */
381280297Sjkim    if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) {
382280297Sjkim        memset(timeleft, 0, sizeof(struct timeval));
383280297Sjkim    }
384205128Ssimon
385280297Sjkim    return timeleft;
386280297Sjkim}
387280297Sjkim
388205128Ssimonint dtls1_is_timer_expired(SSL *s)
389280297Sjkim{
390280297Sjkim    struct timeval timeleft;
391205128Ssimon
392280297Sjkim    /* Get time left until timeout, return false if no timer running */
393280297Sjkim    if (dtls1_get_timeout(s, &timeleft) == NULL) {
394280297Sjkim        return 0;
395280297Sjkim    }
396205128Ssimon
397280297Sjkim    /* Return false if timer is not expired yet */
398280297Sjkim    if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
399280297Sjkim        return 0;
400280297Sjkim    }
401205128Ssimon
402280297Sjkim    /* Timer expired, so return true */
403280297Sjkim    return 1;
404280297Sjkim}
405205128Ssimon
406205128Ssimonvoid dtls1_double_timeout(SSL *s)
407280297Sjkim{
408280297Sjkim    s->d1->timeout_duration *= 2;
409280297Sjkim    if (s->d1->timeout_duration > 60)
410280297Sjkim        s->d1->timeout_duration = 60;
411280297Sjkim    dtls1_start_timer(s);
412280297Sjkim}
413205128Ssimon
414205128Ssimonvoid dtls1_stop_timer(SSL *s)
415280297Sjkim{
416280297Sjkim    /* Reset everything */
417280297Sjkim    memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
418280297Sjkim    memset(&(s->d1->next_timeout), 0, sizeof(struct timeval));
419280297Sjkim    s->d1->timeout_duration = 1;
420280297Sjkim    BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0,
421280297Sjkim             &(s->d1->next_timeout));
422280297Sjkim    /* Clear retransmission buffer */
423280297Sjkim    dtls1_clear_record_buffer(s);
424280297Sjkim}
425205128Ssimon
426237657Sjkimint dtls1_check_timeout_num(SSL *s)
427280297Sjkim{
428280297Sjkim    unsigned int mtu;
429276861Sjkim
430280297Sjkim    s->d1->timeout.num_alerts++;
431205128Ssimon
432280297Sjkim    /* Reduce MTU after 2 unsuccessful retransmissions */
433280297Sjkim    if (s->d1->timeout.num_alerts > 2
434280297Sjkim        && !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
435280297Sjkim        mtu =
436280297Sjkim            BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0,
437280297Sjkim                     NULL);
438280297Sjkim        if (mtu < s->d1->mtu)
439280297Sjkim            s->d1->mtu = mtu;
440280297Sjkim    }
441205128Ssimon
442280297Sjkim    if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) {
443280297Sjkim        /* fail the connection, enough alerts have been sent */
444280297Sjkim        SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED);
445280297Sjkim        return -1;
446280297Sjkim    }
447237657Sjkim
448280297Sjkim    return 0;
449280297Sjkim}
450237657Sjkim
451237657Sjkimint dtls1_handle_timeout(SSL *s)
452280297Sjkim{
453280297Sjkim    /* if no timer is expired, don't do anything */
454280297Sjkim    if (!dtls1_is_timer_expired(s)) {
455280297Sjkim        return 0;
456280297Sjkim    }
457205128Ssimon
458280297Sjkim    dtls1_double_timeout(s);
459237657Sjkim
460280297Sjkim    if (dtls1_check_timeout_num(s) < 0)
461280297Sjkim        return -1;
462237657Sjkim
463280297Sjkim    s->d1->timeout.read_timeouts++;
464280297Sjkim    if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) {
465280297Sjkim        s->d1->timeout.read_timeouts = 1;
466280297Sjkim    }
467238405Sjkim#ifndef OPENSSL_NO_HEARTBEATS
468280297Sjkim    if (s->tlsext_hb_pending) {
469280297Sjkim        s->tlsext_hb_pending = 0;
470280297Sjkim        return dtls1_heartbeat(s);
471280297Sjkim    }
472238405Sjkim#endif
473238405Sjkim
474280297Sjkim    dtls1_start_timer(s);
475280297Sjkim    return dtls1_retransmit_buffered_messages(s);
476280297Sjkim}
477205128Ssimon
478205128Ssimonstatic void get_current_time(struct timeval *t)
479205128Ssimon{
480205128Ssimon#ifdef OPENSSL_SYS_WIN32
481280297Sjkim    struct _timeb tb;
482280297Sjkim    _ftime(&tb);
483280297Sjkim    t->tv_sec = (long)tb.time;
484280297Sjkim    t->tv_usec = (long)tb.millitm * 1000;
485205128Ssimon#elif defined(OPENSSL_SYS_VMS)
486280297Sjkim    struct timeb tb;
487280297Sjkim    ftime(&tb);
488280297Sjkim    t->tv_sec = (long)tb.time;
489280297Sjkim    t->tv_usec = (long)tb.millitm * 1000;
490205128Ssimon#else
491280297Sjkim    gettimeofday(t, NULL);
492205128Ssimon#endif
493205128Ssimon}
494205128Ssimon
495205128Ssimonint dtls1_listen(SSL *s, struct sockaddr *client)
496280297Sjkim{
497280297Sjkim    int ret;
498205128Ssimon
499280297Sjkim    SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
500280297Sjkim    s->d1->listen = 1;
501205128Ssimon
502280297Sjkim    ret = SSL_accept(s);
503280297Sjkim    if (ret <= 0)
504280297Sjkim        return ret;
505280297Sjkim
506280297Sjkim    (void)BIO_dgram_get_peer(SSL_get_rbio(s), client);
507280297Sjkim    return 1;
508280297Sjkim}
509