Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DTLS implementation written by Nagendra Modadugu |
| 3 | * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. |
| 4 | */ |
| 5 | /* ==================================================================== |
| 6 | * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 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 |
| 18 | * distribution. |
| 19 | * |
| 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/)" |
| 24 | * |
| 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. |
| 29 | * |
| 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. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 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 | * ==================================================================== |
| 52 | * |
| 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). */ |
| 56 | |
| 57 | #include <openssl/base.h> |
| 58 | |
| 59 | #include <stdio.h> |
| 60 | |
| 61 | #if defined(OPENSSL_WINDOWS) |
| 62 | #include <sys/timeb.h> |
| 63 | #else |
| 64 | #include <sys/socket.h> |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 65 | #include <sys/time.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #include <openssl/err.h> |
| 69 | #include <openssl/mem.h> |
| 70 | #include <openssl/obj.h> |
| 71 | |
| 72 | #include "ssl_locl.h" |
| 73 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 74 | static void get_current_time(OPENSSL_timeval *t); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 75 | static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 76 | static void dtls1_set_handshake_header(SSL *s, int type, unsigned long len); |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 77 | static int dtls1_handshake_write(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 79 | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 80 | tls1_enc, |
| 81 | tls1_mac, |
| 82 | tls1_setup_key_block, |
| 83 | tls1_generate_master_secret, |
| 84 | tls1_change_cipher_state, |
| 85 | tls1_final_finish_mac, |
| 86 | TLS1_FINISH_MAC_LENGTH, |
| 87 | tls1_cert_verify_mac, |
| 88 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 89 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 90 | tls1_alert_code, |
| 91 | tls1_export_keying_material, |
| 92 | SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV, |
| 93 | DTLS1_HM_HEADER_LENGTH, |
| 94 | dtls1_set_handshake_header, |
| 95 | dtls1_handshake_write, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 96 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 97 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 98 | const SSL3_ENC_METHOD DTLSv1_2_enc_data = { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 99 | tls1_enc, |
| 100 | tls1_mac, |
| 101 | tls1_setup_key_block, |
| 102 | tls1_generate_master_secret, |
| 103 | tls1_change_cipher_state, |
| 104 | tls1_final_finish_mac, |
| 105 | TLS1_FINISH_MAC_LENGTH, |
| 106 | tls1_cert_verify_mac, |
| 107 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 108 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 109 | tls1_alert_code, |
| 110 | tls1_export_keying_material, |
| 111 | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | |
| 112 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
| 113 | DTLS1_HM_HEADER_LENGTH, |
| 114 | dtls1_set_handshake_header, |
| 115 | dtls1_handshake_write, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 116 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 117 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 118 | int dtls1_new(SSL *s) { |
| 119 | DTLS1_STATE *d1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 121 | if (!ssl3_new(s)) { |
| 122 | return 0; |
| 123 | } |
| 124 | d1 = OPENSSL_malloc(sizeof *d1); |
| 125 | if (d1 == NULL) { |
| 126 | ssl3_free(s); |
| 127 | return 0; |
| 128 | } |
| 129 | memset(d1, 0, sizeof *d1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 131 | d1->unprocessed_rcds.q = pqueue_new(); |
| 132 | d1->processed_rcds.q = pqueue_new(); |
| 133 | d1->buffered_messages = pqueue_new(); |
| 134 | d1->sent_messages = pqueue_new(); |
| 135 | d1->buffered_app_data.q = pqueue_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 136 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 137 | if (s->server) { |
| 138 | d1->cookie_len = sizeof(s->d1->cookie); |
| 139 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 141 | if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || |
| 142 | !d1->buffered_messages || !d1->sent_messages || |
| 143 | !d1->buffered_app_data.q) { |
| 144 | if (d1->unprocessed_rcds.q) { |
| 145 | pqueue_free(d1->unprocessed_rcds.q); |
| 146 | } |
| 147 | if (d1->processed_rcds.q) { |
| 148 | pqueue_free(d1->processed_rcds.q); |
| 149 | } |
| 150 | if (d1->buffered_messages) { |
| 151 | pqueue_free(d1->buffered_messages); |
| 152 | } |
| 153 | if (d1->sent_messages) { |
| 154 | pqueue_free(d1->sent_messages); |
| 155 | } |
| 156 | if (d1->buffered_app_data.q) { |
| 157 | pqueue_free(d1->buffered_app_data.q); |
| 158 | } |
| 159 | OPENSSL_free(d1); |
| 160 | ssl3_free(s); |
| 161 | return 0; |
| 162 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 164 | s->d1 = d1; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame^] | 165 | |
| 166 | /* Set the version to the highest version for DTLS. This controls the initial |
| 167 | * state of |s->enc_method| and what the API reports as the version prior to |
| 168 | * negotiation. |
| 169 | * |
| 170 | * TODO(davidben): This is fragile and confusing. */ |
| 171 | s->version = DTLS1_2_VERSION; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 172 | return 1; |
| 173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 174 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 175 | static void dtls1_clear_queues(SSL *s) { |
| 176 | pitem *item = NULL; |
| 177 | hm_fragment *frag = NULL; |
| 178 | DTLS1_RECORD_DATA *rdata; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 180 | while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { |
| 181 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 182 | if (rdata->rbuf.buf) { |
| 183 | OPENSSL_free(rdata->rbuf.buf); |
| 184 | } |
| 185 | OPENSSL_free(item->data); |
| 186 | pitem_free(item); |
| 187 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 189 | while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { |
| 190 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 191 | if (rdata->rbuf.buf) { |
| 192 | OPENSSL_free(rdata->rbuf.buf); |
| 193 | } |
| 194 | OPENSSL_free(item->data); |
| 195 | pitem_free(item); |
| 196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 198 | while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { |
| 199 | frag = (hm_fragment *)item->data; |
| 200 | dtls1_hm_fragment_free(frag); |
| 201 | pitem_free(item); |
| 202 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 204 | while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { |
| 205 | frag = (hm_fragment *)item->data; |
| 206 | dtls1_hm_fragment_free(frag); |
| 207 | pitem_free(item); |
| 208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 210 | while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { |
| 211 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 212 | if (rdata->rbuf.buf) { |
| 213 | OPENSSL_free(rdata->rbuf.buf); |
| 214 | } |
| 215 | OPENSSL_free(item->data); |
| 216 | pitem_free(item); |
| 217 | } |
| 218 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 220 | void dtls1_free(SSL *s) { |
| 221 | ssl3_free(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame^] | 223 | if (s == NULL || s->d1 == NULL) { |
| 224 | return; |
| 225 | } |
| 226 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 227 | dtls1_clear_queues(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 228 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 229 | pqueue_free(s->d1->unprocessed_rcds.q); |
| 230 | pqueue_free(s->d1->processed_rcds.q); |
| 231 | pqueue_free(s->d1->buffered_messages); |
| 232 | pqueue_free(s->d1->sent_messages); |
| 233 | pqueue_free(s->d1->buffered_app_data.q); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 235 | OPENSSL_free(s->d1); |
| 236 | s->d1 = NULL; |
| 237 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 239 | long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) { |
| 240 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 242 | switch (cmd) { |
| 243 | case DTLS_CTRL_GET_TIMEOUT: |
| 244 | if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) { |
| 245 | ret = 1; |
| 246 | } |
| 247 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 248 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 249 | case DTLS_CTRL_HANDLE_TIMEOUT: |
| 250 | ret = dtls1_handle_timeout(s); |
| 251 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 252 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 253 | default: |
| 254 | ret = ssl3_ctrl(s, cmd, larg, parg); |
| 255 | break; |
| 256 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 257 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | /* As it's impossible to use stream ciphers in "datagram" mode, this |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | * simple filter is designed to disengage them in DTLS. Unfortunately |
| 263 | * there is no universal way to identify stream SSL_CIPHER, so we have |
| 264 | * to explicitly list their SSL_* codes. Currently RC4 is the only one |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 265 | * available, but if new ones emerge, they will have to be added... */ |
| 266 | const SSL_CIPHER *dtls1_get_cipher(unsigned int u) { |
| 267 | const SSL_CIPHER *ciph = ssl3_get_cipher(u); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 268 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 269 | if (ciph != NULL) { |
| 270 | if (ciph->algorithm_enc == SSL_RC4) { |
| 271 | return NULL; |
| 272 | } |
| 273 | /* TODO(davidben): EVP_AEAD does not work in DTLS yet. */ |
| 274 | if (ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD || |
| 275 | ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) { |
| 276 | return NULL; |
| 277 | } |
| 278 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 279 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 280 | return ciph; |
| 281 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 282 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 283 | void dtls1_start_timer(SSL *s) { |
| 284 | /* If timer is not set, initialize duration with 1 second */ |
| 285 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 286 | s->d1->timeout_duration = 1; |
| 287 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 288 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 289 | /* Set timeout to current time */ |
| 290 | get_current_time(&s->d1->next_timeout); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 292 | /* Add duration to current time */ |
| 293 | s->d1->next_timeout.tv_sec += s->d1->timeout_duration; |
| 294 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 295 | &s->d1->next_timeout); |
| 296 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 298 | static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) { |
| 299 | OPENSSL_timeval timenow; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 300 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 301 | /* If no timeout is set, just return NULL */ |
| 302 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 303 | return NULL; |
| 304 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 306 | /* Get current time */ |
| 307 | get_current_time(&timenow); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 309 | /* If timer already expired, set remaining time to 0 */ |
| 310 | if (s->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 311 | (s->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 312 | s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
| 313 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 314 | return timeleft; |
| 315 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 316 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 317 | /* Calculate time left until timer expires */ |
| 318 | memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval)); |
| 319 | timeleft->tv_sec -= timenow.tv_sec; |
| 320 | timeleft->tv_usec -= timenow.tv_usec; |
| 321 | if (timeleft->tv_usec < 0) { |
| 322 | timeleft->tv_sec--; |
| 323 | timeleft->tv_usec += 1000000; |
| 324 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 326 | /* If remaining time is less than 15 ms, set it to 0 to prevent issues |
| 327 | * because of small devergences with socket timeouts. */ |
| 328 | if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { |
| 329 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 330 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 332 | return timeleft; |
| 333 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 335 | int dtls1_is_timer_expired(SSL *s) { |
| 336 | OPENSSL_timeval timeleft; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 337 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 338 | /* Get time left until timeout, return false if no timer running */ |
| 339 | if (dtls1_get_timeout(s, &timeleft) == NULL) { |
| 340 | return 0; |
| 341 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 342 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 343 | /* Return false if timer is not expired yet */ |
| 344 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 345 | return 0; |
| 346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 348 | /* Timer expired, so return true */ |
| 349 | return 1; |
| 350 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 352 | void dtls1_double_timeout(SSL *s) { |
| 353 | s->d1->timeout_duration *= 2; |
| 354 | if (s->d1->timeout_duration > 60) { |
| 355 | s->d1->timeout_duration = 60; |
| 356 | } |
| 357 | dtls1_start_timer(s); |
| 358 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 359 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 360 | void dtls1_stop_timer(SSL *s) { |
| 361 | /* Reset everything */ |
| 362 | memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st)); |
| 363 | memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval)); |
| 364 | s->d1->timeout_duration = 1; |
| 365 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 366 | &s->d1->next_timeout); |
| 367 | /* Clear retransmission buffer */ |
| 368 | dtls1_clear_record_buffer(s); |
| 369 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 370 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 371 | int dtls1_check_timeout_num(SSL *s) { |
| 372 | s->d1->timeout.num_alerts++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 373 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 374 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
| 375 | if (s->d1->timeout.num_alerts > 2) { |
| 376 | s->d1->mtu = |
| 377 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
| 378 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 380 | if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { |
| 381 | /* fail the connection, enough alerts have been sent */ |
| 382 | OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED); |
| 383 | return -1; |
| 384 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 386 | return 0; |
| 387 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 388 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 389 | int dtls1_handle_timeout(SSL *s) { |
| 390 | /* if no timer is expired, don't do anything */ |
| 391 | if (!dtls1_is_timer_expired(s)) { |
| 392 | return 0; |
| 393 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 394 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 395 | dtls1_double_timeout(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 396 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 397 | if (dtls1_check_timeout_num(s) < 0) { |
| 398 | return -1; |
| 399 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 401 | s->d1->timeout.read_timeouts++; |
| 402 | if (s->d1->timeout.read_timeouts > DTLS1_TMO_READ_COUNT) { |
| 403 | s->d1->timeout.read_timeouts = 1; |
| 404 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 406 | dtls1_start_timer(s); |
| 407 | return dtls1_retransmit_buffered_messages(s); |
| 408 | } |
| 409 | |
| 410 | static void get_current_time(OPENSSL_timeval *t) { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 411 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 412 | struct _timeb time; |
| 413 | _ftime(&time); |
| 414 | t->tv_sec = time.time; |
| 415 | t->tv_usec = time.millitm * 1000; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | #else |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 417 | gettimeofday(t, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 418 | #endif |
| 419 | } |
| 420 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 421 | static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) { |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 422 | uint8_t *message = (uint8_t *)s->init_buf->data; |
| 423 | const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
| 424 | uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH]; |
| 425 | uint8_t *p = serialised_header; |
| 426 | |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 427 | s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
| 428 | s->d1->next_handshake_write_seq++; |
| 429 | |
| 430 | dtls1_set_message_header(s, htype, len, s->d1->handshake_write_seq, 0, len); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 431 | s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH; |
| 432 | s->init_off = 0; |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 433 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 434 | /* Buffer the message to handle re-xmits */ |
| 435 | dtls1_buffer_message(s, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 437 | /* Add the new message to the handshake hash. Serialize the message |
| 438 | * header as if it were a single fragment. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 439 | *p++ = msg_hdr->type; |
| 440 | l2n3(msg_hdr->msg_len, p); |
| 441 | s2n(msg_hdr->seq, p); |
| 442 | l2n3(0, p); |
| 443 | l2n3(msg_hdr->msg_len, p); |
| 444 | ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)); |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 445 | ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len); |
| 446 | } |
| 447 | |
| 448 | static int dtls1_handshake_write(SSL *s) { |
| 449 | return dtls1_do_write(s, SSL3_RT_HANDSHAKE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 450 | } |