2 * DTLS implementation written by Nagendra Modadugu
3 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5 /* ====================================================================
6 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com). */
57 #include <openssl/base.h>
61 #if defined(OPENSSL_WINDOWS)
62 #include <sys/timeb.h>
64 #include <sys/socket.h>
68 #include <openssl/err.h>
69 #include <openssl/mem.h>
70 #include <openssl/obj.h>
74 static void get_current_time(OPENSSL_timeval *t);
75 static OPENSSL_timeval* dtls1_get_timeout(SSL *s, OPENSSL_timeval* timeleft);
76 static void dtls1_set_handshake_header(SSL *s, int type, unsigned long len);
77 static int dtls1_handshake_write(SSL *s);
78 int dtls1_listen(SSL *s, struct sockaddr *client);
80 SSL3_ENC_METHOD DTLSv1_enc_data={
84 tls1_generate_master_secret,
85 tls1_change_cipher_state,
86 tls1_final_finish_mac,
87 TLS1_FINISH_MAC_LENGTH,
89 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
90 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
92 tls1_export_keying_material,
93 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV,
94 DTLS1_HM_HEADER_LENGTH,
95 dtls1_set_handshake_header,
99 SSL3_ENC_METHOD DTLSv1_2_enc_data={
102 tls1_setup_key_block,
103 tls1_generate_master_secret,
104 tls1_change_cipher_state,
105 tls1_final_finish_mac,
106 TLS1_FINISH_MAC_LENGTH,
107 tls1_cert_verify_mac,
108 TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE,
109 TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE,
111 tls1_export_keying_material,
112 SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV|SSL_ENC_FLAG_SIGALGS
113 |SSL_ENC_FLAG_SHA256_PRF|SSL_ENC_FLAG_TLS1_2_CIPHERS,
114 DTLS1_HM_HEADER_LENGTH,
115 dtls1_set_handshake_header,
116 dtls1_handshake_write
119 int dtls1_new(SSL *s)
123 if (!ssl3_new(s)) return(0);
124 if ((d1=OPENSSL_malloc(sizeof *d1)) == NULL)
129 memset(d1,0, sizeof *d1);
131 /* d1->handshake_epoch=0; */
133 d1->unprocessed_rcds.q=pqueue_new();
134 d1->processed_rcds.q=pqueue_new();
135 d1->buffered_messages = pqueue_new();
136 d1->sent_messages=pqueue_new();
137 d1->buffered_app_data.q=pqueue_new();
141 d1->cookie_len = sizeof(s->d1->cookie);
144 if( ! d1->unprocessed_rcds.q || ! d1->processed_rcds.q
145 || ! d1->buffered_messages || ! d1->sent_messages || ! d1->buffered_app_data.q)
147 if ( d1->unprocessed_rcds.q) pqueue_free(d1->unprocessed_rcds.q);
148 if ( d1->processed_rcds.q) pqueue_free(d1->processed_rcds.q);
149 if ( d1->buffered_messages) pqueue_free(d1->buffered_messages);
150 if ( d1->sent_messages) pqueue_free(d1->sent_messages);
151 if ( d1->buffered_app_data.q) pqueue_free(d1->buffered_app_data.q);
158 s->method->ssl_clear(s);
162 static void dtls1_clear_queues(SSL *s)
165 hm_fragment *frag = NULL;
166 DTLS1_RECORD_DATA *rdata;
168 while( (item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL)
170 rdata = (DTLS1_RECORD_DATA *) item->data;
173 OPENSSL_free(rdata->rbuf.buf);
175 OPENSSL_free(item->data);
179 while( (item = pqueue_pop(s->d1->processed_rcds.q)) != NULL)
181 rdata = (DTLS1_RECORD_DATA *) item->data;
184 OPENSSL_free(rdata->rbuf.buf);
186 OPENSSL_free(item->data);
190 while( (item = pqueue_pop(s->d1->buffered_messages)) != NULL)
192 frag = (hm_fragment *)item->data;
193 OPENSSL_free(frag->fragment);
198 while ( (item = pqueue_pop(s->d1->sent_messages)) != NULL)
200 frag = (hm_fragment *)item->data;
201 OPENSSL_free(frag->fragment);
206 while ( (item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL)
208 rdata = (DTLS1_RECORD_DATA *) item->data;
211 OPENSSL_free(rdata->rbuf.buf);
213 OPENSSL_free(item->data);
218 void dtls1_free(SSL *s)
222 dtls1_clear_queues(s);
224 pqueue_free(s->d1->unprocessed_rcds.q);
225 pqueue_free(s->d1->processed_rcds.q);
226 pqueue_free(s->d1->buffered_messages);
227 pqueue_free(s->d1->sent_messages);
228 pqueue_free(s->d1->buffered_app_data.q);
234 void dtls1_clear(SSL *s)
236 pqueue unprocessed_rcds;
237 pqueue processed_rcds;
238 pqueue buffered_messages;
239 pqueue sent_messages;
240 pqueue buffered_app_data;
245 unprocessed_rcds = s->d1->unprocessed_rcds.q;
246 processed_rcds = s->d1->processed_rcds.q;
247 buffered_messages = s->d1->buffered_messages;
248 sent_messages = s->d1->sent_messages;
249 buffered_app_data = s->d1->buffered_app_data.q;
252 dtls1_clear_queues(s);
254 memset(s->d1, 0, sizeof(*(s->d1)));
258 s->d1->cookie_len = sizeof(s->d1->cookie);
261 if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)
266 s->d1->unprocessed_rcds.q = unprocessed_rcds;
267 s->d1->processed_rcds.q = processed_rcds;
268 s->d1->buffered_messages = buffered_messages;
269 s->d1->sent_messages = sent_messages;
270 s->d1->buffered_app_data.q = buffered_app_data;
274 if (s->method->version == DTLS_ANY_VERSION)
275 s->version=DTLS1_2_VERSION;
277 s->version=s->method->version;
280 long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg)
286 case DTLS_CTRL_GET_TIMEOUT:
287 if (dtls1_get_timeout(s, (OPENSSL_timeval*) parg) != NULL)
292 case DTLS_CTRL_HANDLE_TIMEOUT:
293 ret = dtls1_handle_timeout(s);
295 case DTLS_CTRL_LISTEN:
296 ret = dtls1_listen(s, parg);
300 ret = ssl3_ctrl(s, cmd, larg, parg);
307 * As it's impossible to use stream ciphers in "datagram" mode, this
308 * simple filter is designed to disengage them in DTLS. Unfortunately
309 * there is no universal way to identify stream SSL_CIPHER, so we have
310 * to explicitly list their SSL_* codes. Currently RC4 is the only one
311 * available, but if new ones emerge, they will have to be added...
313 const SSL_CIPHER *dtls1_get_cipher(unsigned int u)
315 const SSL_CIPHER *ciph = ssl3_get_cipher(u);
319 if (ciph->algorithm_enc == SSL_RC4)
326 void dtls1_start_timer(SSL *s)
328 /* If timer is not set, initialize duration with 1 second */
329 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0)
331 s->d1->timeout_duration = 1;
334 /* Set timeout to current time */
335 get_current_time(&s->d1->next_timeout);
337 /* Add duration to current time */
338 s->d1->next_timeout.tv_sec += s->d1->timeout_duration;
339 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &s->d1->next_timeout);
342 static OPENSSL_timeval* dtls1_get_timeout(SSL *s, OPENSSL_timeval* timeleft)
344 OPENSSL_timeval timenow;
346 /* If no timeout is set, just return NULL */
347 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0)
352 /* Get current time */
353 get_current_time(&timenow);
355 /* If timer already expired, set remaining time to 0 */
356 if (s->d1->next_timeout.tv_sec < timenow.tv_sec ||
357 (s->d1->next_timeout.tv_sec == timenow.tv_sec &&
358 s->d1->next_timeout.tv_usec <= timenow.tv_usec))
360 memset(timeleft, 0, sizeof(OPENSSL_timeval));
364 /* Calculate time left until timer expires */
365 memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval));
366 timeleft->tv_sec -= timenow.tv_sec;
367 timeleft->tv_usec -= timenow.tv_usec;
368 if (timeleft->tv_usec < 0)
371 timeleft->tv_usec += 1000000;
374 /* If remaining time is less than 15 ms, set it to 0
375 * to prevent issues because of small devergences with
378 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000)
380 memset(timeleft, 0, sizeof(OPENSSL_timeval));
387 int dtls1_is_timer_expired(SSL *s)
389 OPENSSL_timeval timeleft;
391 /* Get time left until timeout, return false if no timer running */
392 if (dtls1_get_timeout(s, &timeleft) == NULL)
397 /* Return false if timer is not expired yet */
398 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0)
403 /* Timer expired, so return true */
407 void dtls1_double_timeout(SSL *s)
409 s->d1->timeout_duration *= 2;
410 if (s->d1->timeout_duration > 60)
411 s->d1->timeout_duration = 60;
412 dtls1_start_timer(s);
415 void dtls1_stop_timer(SSL *s)
417 /* Reset everything */
418 memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st));
419 memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval));
420 s->d1->timeout_duration = 1;
421 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &s->d1->next_timeout);
422 /* Clear retransmission buffer */
423 dtls1_clear_record_buffer(s);
426 int dtls1_check_timeout_num(SSL *s)
428 s->d1->timeout.num_alerts++;
430 /* Reduce MTU after 2 unsuccessful retransmissions */
431 if (s->d1->timeout.num_alerts > 2)
433 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL);
436 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT)
438 /* fail the connection, enough alerts have been sent */
439 OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED);
446 int dtls1_handle_timeout(SSL *s)
448 /* if no timer is expired, don't do anything */
449 if (!dtls1_is_timer_expired(s))
454 dtls1_double_timeout(s);
456 if (dtls1_check_timeout_num(s) < 0)
459 s->d1->timeout.read_timeouts++;
460 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT)
462 s->d1->timeout.read_timeouts = 1;
465 dtls1_start_timer(s);
466 return dtls1_retransmit_buffered_messages(s);
469 static void get_current_time(OPENSSL_timeval *t)
471 #if defined(OPENSSL_WINDOWS)
474 t->tv_sec = time.time;
475 t->tv_usec = time.millitm * 1000;
477 gettimeofday(t, NULL);
481 int dtls1_listen(SSL *s, struct sockaddr *client)
485 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE);
489 if (ret <= 0) return ret;
491 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_GET_PEER, 0, &client);
495 static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len)
497 unsigned char *p = (unsigned char *)s->init_buf->data;
498 dtls1_set_message_header(s, p, htype, len, 0, len);
499 s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH;
501 /* Buffer the message to handle re-xmits */
502 dtls1_buffer_message(s, 0);
505 static int dtls1_handshake_write(SSL *s)
507 return dtls1_do_write(s, SSL3_RT_HANDSHAKE);