1 /* ssl/d1_lib.c */ 2 /* 3 * DTLS implementation written by Nagendra Modadugu 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 5 */ 6 /* ==================================================================== 7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. All advertising materials mentioning features or use of this 22 * software must display the following acknowledgment: 23 * "This product includes software developed by the OpenSSL Project 24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 25 * 26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 27 * endorse or promote products derived from this software without 28 * prior written permission. For written permission, please contact 29 * openssl-core@OpenSSL.org. 30 * 31 * 5. Products derived from this software may not be called "OpenSSL" 32 * nor may "OpenSSL" appear in their names without prior written 33 * permission of the OpenSSL Project. 34 * 35 * 6. Redistributions of any form whatsoever must retain the following 36 * acknowledgment: 37 * "This product includes software developed by the OpenSSL Project 38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 51 * OF THE POSSIBILITY OF SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This product includes cryptographic software written by Eric Young 55 * (eay@cryptsoft.com). This product includes software written by Tim 56 * Hudson (tjh@cryptsoft.com). 57 * 58 */ 59 60 #include <sys/param.h> 61 #include <sys/socket.h> 62 63 #include <netinet/in.h> 64 65 #include <stdio.h> 66 #include <openssl/objects.h> 67 #include "ssl_locl.h" 68 69 const char dtls1_version_str[]="DTLSv1" OPENSSL_VERSION_PTEXT; 70 int dtls1_listen(SSL *s, struct sockaddr *client); 71 72 SSL3_ENC_METHOD DTLSv1_enc_data = { 73 dtls1_enc, 74 tls1_mac, 75 tls1_setup_key_block, 76 tls1_generate_master_secret, 77 tls1_change_cipher_state, 78 tls1_final_finish_mac, 79 TLS1_FINISH_MAC_LENGTH, 80 tls1_cert_verify_mac, 81 TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE, 82 TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE, 83 tls1_alert_code, 84 tls1_export_keying_material, 85 }; 86 87 long 88 dtls1_default_timeout(void) 89 { 90 /* 2 hours, the 24 hours mentioned in the DTLSv1 spec 91 * is way too long for http, the cache would over fill */ 92 return (60*60*2); 93 } 94 95 int 96 dtls1_new(SSL *s) 97 { 98 DTLS1_STATE *d1; 99 100 if (!ssl3_new(s)) 101 return (0); 102 if ((d1 = calloc(1, sizeof *d1)) == NULL) 103 return (0); 104 105 /* d1->handshake_epoch=0; */ 106 107 d1->unprocessed_rcds.q = pqueue_new(); 108 d1->processed_rcds.q = pqueue_new(); 109 d1->buffered_messages = pqueue_new(); 110 d1->sent_messages = pqueue_new(); 111 d1->buffered_app_data.q = pqueue_new(); 112 113 if (s->server) { 114 d1->cookie_len = sizeof(s->d1->cookie); 115 } 116 117 if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || 118 !d1->buffered_messages || !d1->sent_messages || 119 !d1->buffered_app_data.q) { 120 if (d1->unprocessed_rcds.q) 121 pqueue_free(d1->unprocessed_rcds.q); 122 if (d1->processed_rcds.q) 123 pqueue_free(d1->processed_rcds.q); 124 if (d1->buffered_messages) 125 pqueue_free(d1->buffered_messages); 126 if (d1->sent_messages) 127 pqueue_free(d1->sent_messages); 128 if (d1->buffered_app_data.q) 129 pqueue_free(d1->buffered_app_data.q); 130 free(d1); 131 return (0); 132 } 133 134 s->d1 = d1; 135 s->method->ssl_clear(s); 136 return (1); 137 } 138 139 static void 140 dtls1_clear_queues(SSL *s) 141 { 142 pitem *item = NULL; 143 hm_fragment *frag = NULL; 144 DTLS1_RECORD_DATA *rdata; 145 146 while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { 147 rdata = (DTLS1_RECORD_DATA *) item->data; 148 if (rdata->rbuf.buf) { 149 free(rdata->rbuf.buf); 150 } 151 free(item->data); 152 pitem_free(item); 153 } 154 155 while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { 156 rdata = (DTLS1_RECORD_DATA *) item->data; 157 if (rdata->rbuf.buf) { 158 free(rdata->rbuf.buf); 159 } 160 free(item->data); 161 pitem_free(item); 162 } 163 164 while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { 165 frag = (hm_fragment *)item->data; 166 free(frag->fragment); 167 free(frag); 168 pitem_free(item); 169 } 170 171 while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { 172 frag = (hm_fragment *)item->data; 173 free(frag->fragment); 174 free(frag); 175 pitem_free(item); 176 } 177 178 while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { 179 frag = (hm_fragment *)item->data; 180 free(frag->fragment); 181 free(frag); 182 pitem_free(item); 183 } 184 } 185 186 void 187 dtls1_free(SSL *s) 188 { 189 ssl3_free(s); 190 191 dtls1_clear_queues(s); 192 193 pqueue_free(s->d1->unprocessed_rcds.q); 194 pqueue_free(s->d1->processed_rcds.q); 195 pqueue_free(s->d1->buffered_messages); 196 pqueue_free(s->d1->sent_messages); 197 pqueue_free(s->d1->buffered_app_data.q); 198 199 free(s->d1); 200 s->d1 = NULL; 201 } 202 203 void 204 dtls1_clear(SSL *s) 205 { 206 pqueue unprocessed_rcds; 207 pqueue processed_rcds; 208 pqueue buffered_messages; 209 pqueue sent_messages; 210 pqueue buffered_app_data; 211 unsigned int mtu; 212 213 if (s->d1) { 214 unprocessed_rcds = s->d1->unprocessed_rcds.q; 215 processed_rcds = s->d1->processed_rcds.q; 216 buffered_messages = s->d1->buffered_messages; 217 sent_messages = s->d1->sent_messages; 218 buffered_app_data = s->d1->buffered_app_data.q; 219 mtu = s->d1->mtu; 220 221 dtls1_clear_queues(s); 222 223 memset(s->d1, 0, sizeof(*(s->d1))); 224 225 if (s->server) { 226 s->d1->cookie_len = sizeof(s->d1->cookie); 227 } 228 229 if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) { 230 s->d1->mtu = mtu; 231 } 232 233 s->d1->unprocessed_rcds.q = unprocessed_rcds; 234 s->d1->processed_rcds.q = processed_rcds; 235 s->d1->buffered_messages = buffered_messages; 236 s->d1->sent_messages = sent_messages; 237 s->d1->buffered_app_data.q = buffered_app_data; 238 } 239 240 ssl3_clear(s); 241 if (s->options & SSL_OP_CISCO_ANYCONNECT) 242 s->version = DTLS1_BAD_VER; 243 else 244 s->version = DTLS1_VERSION; 245 } 246 247 long 248 dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) 249 { 250 int ret = 0; 251 252 switch (cmd) { 253 case DTLS_CTRL_GET_TIMEOUT: 254 if (dtls1_get_timeout(s, (struct timeval*) parg) != NULL) { 255 ret = 1; 256 } 257 break; 258 case DTLS_CTRL_HANDLE_TIMEOUT: 259 ret = dtls1_handle_timeout(s); 260 break; 261 case DTLS_CTRL_LISTEN: 262 ret = dtls1_listen(s, parg); 263 break; 264 265 default: 266 ret = ssl3_ctrl(s, cmd, larg, parg); 267 break; 268 } 269 return (ret); 270 } 271 272 /* 273 * As it's impossible to use stream ciphers in "datagram" mode, this 274 * simple filter is designed to disengage them in DTLS. Unfortunately 275 * there is no universal way to identify stream SSL_CIPHER, so we have 276 * to explicitly list their SSL_* codes. Currently RC4 is the only one 277 * available, but if new ones emerge, they will have to be added... 278 */ 279 const SSL_CIPHER * 280 dtls1_get_cipher(unsigned int u) 281 { 282 const SSL_CIPHER *ciph = ssl3_get_cipher(u); 283 284 if (ciph != NULL) { 285 if (ciph->algorithm_enc == SSL_RC4) 286 return NULL; 287 } 288 289 return ciph; 290 } 291 292 void 293 dtls1_start_timer(SSL *s) 294 { 295 #ifndef OPENSSL_NO_SCTP 296 /* Disable timer for SCTP */ 297 if (BIO_dgram_is_sctp(SSL_get_wbio(s))) { 298 memset(&(s->d1->next_timeout), 0, sizeof(struct timeval)); 299 return; 300 } 301 #endif 302 303 /* If timer is not set, initialize duration with 1 second */ 304 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { 305 s->d1->timeout_duration = 1; 306 } 307 308 /* Set timeout to current time */ 309 gettimeofday(&(s->d1->next_timeout), NULL); 310 311 /* Add duration to current time */ 312 s->d1->next_timeout.tv_sec += s->d1->timeout_duration; 313 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &(s->d1->next_timeout)); 314 } 315 316 struct timeval* 317 dtls1_get_timeout(SSL *s, struct timeval* timeleft) { 318 struct timeval timenow; 319 320 /* If no timeout is set, just return NULL */ 321 if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { 322 return NULL; 323 } 324 325 /* Get current time */ 326 gettimeofday(&timenow, NULL); 327 328 /* If timer already expired, set remaining time to 0 */ 329 if (s->d1->next_timeout.tv_sec < timenow.tv_sec || 330 (s->d1->next_timeout.tv_sec == timenow.tv_sec && 331 s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { 332 memset(timeleft, 0, sizeof(struct timeval)); 333 return timeleft; 334 } 335 336 /* Calculate time left until timer expires */ 337 memcpy(timeleft, &(s->d1->next_timeout), sizeof(struct timeval)); 338 timeleft->tv_sec -= timenow.tv_sec; 339 timeleft->tv_usec -= timenow.tv_usec; 340 if (timeleft->tv_usec < 0) { 341 timeleft->tv_sec--; 342 timeleft->tv_usec += 1000000; 343 } 344 345 /* If remaining time is less than 15 ms, set it to 0 346 * to prevent issues because of small devergences with 347 * socket timeouts. 348 */ 349 if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { 350 memset(timeleft, 0, sizeof(struct timeval)); 351 } 352 353 354 return timeleft; 355 } 356 357 int 358 dtls1_is_timer_expired(SSL *s) 359 { 360 struct timeval timeleft; 361 362 /* Get time left until timeout, return false if no timer running */ 363 if (dtls1_get_timeout(s, &timeleft) == NULL) { 364 return 0; 365 } 366 367 /* Return false if timer is not expired yet */ 368 if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { 369 return 0; 370 } 371 372 /* Timer expired, so return true */ 373 return 1; 374 } 375 376 void 377 dtls1_double_timeout(SSL *s) 378 { 379 s->d1->timeout_duration *= 2; 380 if (s->d1->timeout_duration > 60) 381 s->d1->timeout_duration = 60; 382 dtls1_start_timer(s); 383 } 384 385 void 386 dtls1_stop_timer(SSL *s) 387 { 388 /* Reset everything */ 389 memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st)); 390 memset(&(s->d1->next_timeout), 0, sizeof(struct timeval)); 391 s->d1->timeout_duration = 1; 392 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &(s->d1->next_timeout)); 393 /* Clear retransmission buffer */ 394 dtls1_clear_record_buffer(s); 395 } 396 397 int 398 dtls1_check_timeout_num(SSL *s) 399 { 400 s->d1->timeout.num_alerts++; 401 402 /* Reduce MTU after 2 unsuccessful retransmissions */ 403 if (s->d1->timeout.num_alerts > 2) { 404 s->d1->mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); 405 406 } 407 408 if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { 409 /* fail the connection, enough alerts have been sent */ 410 SSLerr(SSL_F_DTLS1_CHECK_TIMEOUT_NUM, SSL_R_READ_TIMEOUT_EXPIRED); 411 return -1; 412 } 413 414 return 0; 415 } 416 417 int 418 dtls1_handle_timeout(SSL *s) 419 { 420 /* if no timer is expired, don't do anything */ 421 if (!dtls1_is_timer_expired(s)) { 422 return 0; 423 } 424 425 dtls1_double_timeout(s); 426 427 if (dtls1_check_timeout_num(s) < 0) 428 return -1; 429 430 s->d1->timeout.read_timeouts++; 431 if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { 432 s->d1->timeout.read_timeouts = 1; 433 } 434 435 dtls1_start_timer(s); 436 return dtls1_retransmit_buffered_messages(s); 437 } 438 439 int 440 dtls1_listen(SSL *s, struct sockaddr *client) 441 { 442 int ret; 443 444 SSL_set_options(s, SSL_OP_COOKIE_EXCHANGE); 445 s->d1->listen = 1; 446 447 ret = SSL_accept(s); 448 if (ret <= 0) 449 return ret; 450 451 (void)BIO_dgram_get_peer(SSL_get_rbio(s), client); 452 return 1; 453 } 454