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 | |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 59 | #include <limits.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 60 | #include <stdio.h> |
| 61 | |
| 62 | #if defined(OPENSSL_WINDOWS) |
| 63 | #include <sys/timeb.h> |
| 64 | #else |
| 65 | #include <sys/socket.h> |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 66 | #include <sys/time.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | #include <openssl/err.h> |
| 70 | #include <openssl/mem.h> |
| 71 | #include <openssl/obj.h> |
| 72 | |
| 73 | #include "ssl_locl.h" |
| 74 | |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 75 | /* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire |
| 76 | * before starting to decrease the MTU. */ |
| 77 | #define DTLS1_MTU_TIMEOUTS 2 |
| 78 | |
| 79 | /* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire |
| 80 | * before failing the DTLS handshake. */ |
| 81 | #define DTLS1_MAX_TIMEOUTS 12 |
| 82 | |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 83 | static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 84 | static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 86 | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 87 | tls1_enc, |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 88 | tls1_prf, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 89 | tls1_setup_key_block, |
| 90 | tls1_generate_master_secret, |
| 91 | tls1_change_cipher_state, |
| 92 | tls1_final_finish_mac, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 93 | tls1_cert_verify_mac, |
| 94 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 95 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 96 | tls1_alert_code, |
| 97 | tls1_export_keying_material, |
| 98 | SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 99 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 101 | const SSL3_ENC_METHOD DTLSv1_2_enc_data = { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 102 | tls1_enc, |
David Benjamin | 41ac979 | 2014-12-23 10:41:06 -0500 | [diff] [blame] | 103 | tls1_prf, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 104 | tls1_setup_key_block, |
| 105 | tls1_generate_master_secret, |
| 106 | tls1_change_cipher_state, |
| 107 | tls1_final_finish_mac, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 108 | tls1_cert_verify_mac, |
| 109 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 110 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 111 | tls1_alert_code, |
| 112 | tls1_export_keying_material, |
| 113 | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | |
| 114 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 115 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 117 | int dtls1_new(SSL *s) { |
| 118 | DTLS1_STATE *d1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 120 | if (!ssl3_new(s)) { |
| 121 | return 0; |
| 122 | } |
| 123 | d1 = OPENSSL_malloc(sizeof *d1); |
| 124 | if (d1 == NULL) { |
| 125 | ssl3_free(s); |
| 126 | return 0; |
| 127 | } |
| 128 | memset(d1, 0, sizeof *d1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 130 | d1->unprocessed_rcds.q = pqueue_new(); |
| 131 | d1->processed_rcds.q = pqueue_new(); |
| 132 | d1->buffered_messages = pqueue_new(); |
| 133 | d1->sent_messages = pqueue_new(); |
| 134 | d1->buffered_app_data.q = pqueue_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 135 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 136 | if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || |
| 137 | !d1->buffered_messages || !d1->sent_messages || |
| 138 | !d1->buffered_app_data.q) { |
| 139 | if (d1->unprocessed_rcds.q) { |
| 140 | pqueue_free(d1->unprocessed_rcds.q); |
| 141 | } |
| 142 | if (d1->processed_rcds.q) { |
| 143 | pqueue_free(d1->processed_rcds.q); |
| 144 | } |
| 145 | if (d1->buffered_messages) { |
| 146 | pqueue_free(d1->buffered_messages); |
| 147 | } |
| 148 | if (d1->sent_messages) { |
| 149 | pqueue_free(d1->sent_messages); |
| 150 | } |
| 151 | if (d1->buffered_app_data.q) { |
| 152 | pqueue_free(d1->buffered_app_data.q); |
| 153 | } |
| 154 | OPENSSL_free(d1); |
| 155 | ssl3_free(s); |
| 156 | return 0; |
| 157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 159 | s->d1 = d1; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 160 | |
| 161 | /* Set the version to the highest version for DTLS. This controls the initial |
| 162 | * state of |s->enc_method| and what the API reports as the version prior to |
| 163 | * negotiation. |
| 164 | * |
| 165 | * TODO(davidben): This is fragile and confusing. */ |
| 166 | s->version = DTLS1_2_VERSION; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 167 | return 1; |
| 168 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 170 | static void dtls1_clear_queues(SSL *s) { |
| 171 | pitem *item = NULL; |
| 172 | hm_fragment *frag = NULL; |
| 173 | DTLS1_RECORD_DATA *rdata; |
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 | while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { |
| 176 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 177 | if (rdata->rbuf.buf) { |
| 178 | OPENSSL_free(rdata->rbuf.buf); |
| 179 | } |
| 180 | OPENSSL_free(item->data); |
| 181 | pitem_free(item); |
| 182 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 183 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 184 | while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { |
| 185 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 186 | if (rdata->rbuf.buf) { |
| 187 | OPENSSL_free(rdata->rbuf.buf); |
| 188 | } |
| 189 | OPENSSL_free(item->data); |
| 190 | pitem_free(item); |
| 191 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 193 | while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { |
| 194 | frag = (hm_fragment *)item->data; |
| 195 | dtls1_hm_fragment_free(frag); |
| 196 | pitem_free(item); |
| 197 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 199 | while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { |
| 200 | frag = (hm_fragment *)item->data; |
| 201 | dtls1_hm_fragment_free(frag); |
| 202 | pitem_free(item); |
| 203 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 205 | while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { |
| 206 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 207 | if (rdata->rbuf.buf) { |
| 208 | OPENSSL_free(rdata->rbuf.buf); |
| 209 | } |
| 210 | OPENSSL_free(item->data); |
| 211 | pitem_free(item); |
| 212 | } |
| 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 215 | void dtls1_free(SSL *s) { |
| 216 | ssl3_free(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 218 | if (s == NULL || s->d1 == NULL) { |
| 219 | return; |
| 220 | } |
| 221 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 222 | dtls1_clear_queues(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 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); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 229 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 230 | OPENSSL_free(s->d1); |
| 231 | s->d1 = NULL; |
| 232 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 234 | long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) { |
| 235 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 236 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 237 | switch (cmd) { |
| 238 | case DTLS_CTRL_GET_TIMEOUT: |
| 239 | if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) { |
| 240 | ret = 1; |
| 241 | } |
| 242 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 244 | case DTLS_CTRL_HANDLE_TIMEOUT: |
| 245 | ret = dtls1_handle_timeout(s); |
| 246 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 247 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 248 | default: |
| 249 | ret = ssl3_ctrl(s, cmd, larg, parg); |
| 250 | break; |
| 251 | } |
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 | return ret; |
| 254 | } |
| 255 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 256 | const SSL_CIPHER *dtls1_get_cipher(unsigned int u) { |
| 257 | const SSL_CIPHER *ciph = ssl3_get_cipher(u); |
David Benjamin | e95d20d | 2014-12-23 11:16:01 -0500 | [diff] [blame] | 258 | /* DTLS does not support stream ciphers. */ |
| 259 | if (ciph == NULL || ciph->algorithm_enc == SSL_RC4) { |
| 260 | return NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 261 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 263 | return ciph; |
| 264 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 266 | void dtls1_start_timer(SSL *s) { |
| 267 | /* If timer is not set, initialize duration with 1 second */ |
| 268 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 269 | s->d1->timeout_duration = 1; |
| 270 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 271 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 272 | /* Set timeout to current time */ |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 273 | get_current_time(s, &s->d1->next_timeout); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 274 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 275 | /* Add duration to current time */ |
| 276 | s->d1->next_timeout.tv_sec += s->d1->timeout_duration; |
| 277 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 278 | &s->d1->next_timeout); |
| 279 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 281 | static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) { |
| 282 | OPENSSL_timeval timenow; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 283 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 284 | /* If no timeout is set, just return NULL */ |
| 285 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 286 | return NULL; |
| 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 | /* Get current time */ |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 290 | get_current_time(s, &timenow); |
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 | /* If timer already expired, set remaining time to 0 */ |
| 293 | if (s->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 294 | (s->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 295 | s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
| 296 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 297 | return timeleft; |
| 298 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 299 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 300 | /* Calculate time left until timer expires */ |
| 301 | memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval)); |
| 302 | timeleft->tv_sec -= timenow.tv_sec; |
| 303 | timeleft->tv_usec -= timenow.tv_usec; |
| 304 | if (timeleft->tv_usec < 0) { |
| 305 | timeleft->tv_sec--; |
| 306 | timeleft->tv_usec += 1000000; |
| 307 | } |
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 remaining time is less than 15 ms, set it to 0 to prevent issues |
| 310 | * because of small devergences with socket timeouts. */ |
| 311 | if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { |
| 312 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 313 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 315 | return timeleft; |
| 316 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 318 | int dtls1_is_timer_expired(SSL *s) { |
| 319 | OPENSSL_timeval timeleft; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 320 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 321 | /* Get time left until timeout, return false if no timer running */ |
| 322 | if (dtls1_get_timeout(s, &timeleft) == NULL) { |
| 323 | return 0; |
| 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 | /* Return false if timer is not expired yet */ |
| 327 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 328 | return 0; |
| 329 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 331 | /* Timer expired, so return true */ |
| 332 | return 1; |
| 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 | void dtls1_double_timeout(SSL *s) { |
| 336 | s->d1->timeout_duration *= 2; |
| 337 | if (s->d1->timeout_duration > 60) { |
| 338 | s->d1->timeout_duration = 60; |
| 339 | } |
| 340 | dtls1_start_timer(s); |
| 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 | void dtls1_stop_timer(SSL *s) { |
| 344 | /* Reset everything */ |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 345 | s->d1->num_timeouts = 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 346 | memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval)); |
| 347 | s->d1->timeout_duration = 1; |
| 348 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 349 | &s->d1->next_timeout); |
| 350 | /* Clear retransmission buffer */ |
| 351 | dtls1_clear_record_buffer(s); |
| 352 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 354 | int dtls1_check_timeout_num(SSL *s) { |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 355 | s->d1->num_timeouts++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 356 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 357 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 358 | if (s->d1->num_timeouts > DTLS1_MTU_TIMEOUTS && |
David Benjamin | 7f18b13 | 2015-01-11 17:36:21 -0500 | [diff] [blame] | 359 | !(SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 360 | long mtu = BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, |
| 361 | NULL); |
| 362 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 363 | s->d1->mtu = (unsigned)mtu; |
| 364 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 365 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 366 | |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 367 | if (s->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 368 | /* fail the connection, enough alerts have been sent */ |
| 369 | OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED); |
| 370 | return -1; |
| 371 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 372 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 373 | return 0; |
| 374 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 375 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 376 | int dtls1_handle_timeout(SSL *s) { |
| 377 | /* if no timer is expired, don't do anything */ |
| 378 | if (!dtls1_is_timer_expired(s)) { |
| 379 | return 0; |
| 380 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 382 | dtls1_double_timeout(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 383 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 384 | if (dtls1_check_timeout_num(s) < 0) { |
| 385 | return -1; |
| 386 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 387 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 388 | dtls1_start_timer(s); |
| 389 | return dtls1_retransmit_buffered_messages(s); |
| 390 | } |
| 391 | |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 392 | static void get_current_time(SSL *ssl, OPENSSL_timeval *out_clock) { |
| 393 | if (ssl->ctx->current_time_cb != NULL) { |
| 394 | ssl->ctx->current_time_cb(ssl, out_clock); |
| 395 | return; |
| 396 | } |
| 397 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 398 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 399 | struct _timeb time; |
| 400 | _ftime(&time); |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 401 | out_clock->tv_sec = time.time; |
| 402 | out_clock->tv_usec = time.millitm * 1000; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 403 | #else |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 404 | gettimeofday(out_clock, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | #endif |
| 406 | } |
| 407 | |
David Benjamin | 2fa83de | 2015-02-08 01:40:08 -0500 | [diff] [blame] | 408 | int dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) { |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 409 | uint8_t *message = (uint8_t *)s->init_buf->data; |
| 410 | const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
| 411 | uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH]; |
| 412 | uint8_t *p = serialised_header; |
| 413 | |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 414 | s->d1->handshake_write_seq = s->d1->next_handshake_write_seq; |
| 415 | s->d1->next_handshake_write_seq++; |
| 416 | |
| 417 | 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] | 418 | s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH; |
| 419 | s->init_off = 0; |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 420 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 421 | /* Buffer the message to handle re-xmits */ |
| 422 | dtls1_buffer_message(s, 0); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 424 | /* Add the new message to the handshake hash. Serialize the message |
| 425 | * header as if it were a single fragment. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 426 | *p++ = msg_hdr->type; |
| 427 | l2n3(msg_hdr->msg_len, p); |
| 428 | s2n(msg_hdr->seq, p); |
| 429 | l2n3(0, p); |
| 430 | l2n3(msg_hdr->msg_len, p); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 431 | return ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)) && |
| 432 | ssl3_finish_mac(s, message + DTLS1_HM_HEADER_LENGTH, len); |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 433 | } |
| 434 | |
David Benjamin | 2fa83de | 2015-02-08 01:40:08 -0500 | [diff] [blame] | 435 | int dtls1_handshake_write(SSL *s) { |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 436 | return dtls1_do_write(s, SSL3_RT_HANDSHAKE); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 437 | } |