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); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 77 | static int dtls1_handshake_write( |
| 78 | SSL *s, enum should_add_to_finished_hash should_add_to_finished_hash); |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 79 | static void dtls1_add_to_finished_hash(SSL *s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 80 | |
David Benjamin | 338fcaf | 2014-12-11 01:20:52 -0500 | [diff] [blame] | 81 | const SSL3_ENC_METHOD DTLSv1_enc_data = { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 82 | tls1_enc, |
| 83 | tls1_mac, |
| 84 | tls1_setup_key_block, |
| 85 | tls1_generate_master_secret, |
| 86 | tls1_change_cipher_state, |
| 87 | tls1_final_finish_mac, |
| 88 | TLS1_FINISH_MAC_LENGTH, |
| 89 | tls1_cert_verify_mac, |
| 90 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 91 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 92 | tls1_alert_code, |
| 93 | tls1_export_keying_material, |
| 94 | SSL_ENC_FLAG_DTLS|SSL_ENC_FLAG_EXPLICIT_IV, |
| 95 | DTLS1_HM_HEADER_LENGTH, |
| 96 | dtls1_set_handshake_header, |
| 97 | dtls1_handshake_write, |
| 98 | dtls1_add_to_finished_hash, |
| 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, |
| 103 | tls1_mac, |
| 104 | tls1_setup_key_block, |
| 105 | tls1_generate_master_secret, |
| 106 | tls1_change_cipher_state, |
| 107 | tls1_final_finish_mac, |
| 108 | TLS1_FINISH_MAC_LENGTH, |
| 109 | tls1_cert_verify_mac, |
| 110 | TLS_MD_CLIENT_FINISH_CONST,TLS_MD_CLIENT_FINISH_CONST_SIZE, |
| 111 | TLS_MD_SERVER_FINISH_CONST,TLS_MD_SERVER_FINISH_CONST_SIZE, |
| 112 | tls1_alert_code, |
| 113 | tls1_export_keying_material, |
| 114 | SSL_ENC_FLAG_DTLS | SSL_ENC_FLAG_EXPLICIT_IV | SSL_ENC_FLAG_SIGALGS | |
| 115 | SSL_ENC_FLAG_SHA256_PRF | SSL_ENC_FLAG_TLS1_2_CIPHERS, |
| 116 | DTLS1_HM_HEADER_LENGTH, |
| 117 | dtls1_set_handshake_header, |
| 118 | dtls1_handshake_write, |
| 119 | dtls1_add_to_finished_hash, |
| 120 | }; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 122 | int dtls1_new(SSL *s) { |
| 123 | DTLS1_STATE *d1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 125 | if (!ssl3_new(s)) { |
| 126 | return 0; |
| 127 | } |
| 128 | d1 = OPENSSL_malloc(sizeof *d1); |
| 129 | if (d1 == NULL) { |
| 130 | ssl3_free(s); |
| 131 | return 0; |
| 132 | } |
| 133 | memset(d1, 0, sizeof *d1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 135 | d1->unprocessed_rcds.q = pqueue_new(); |
| 136 | d1->processed_rcds.q = pqueue_new(); |
| 137 | d1->buffered_messages = pqueue_new(); |
| 138 | d1->sent_messages = pqueue_new(); |
| 139 | d1->buffered_app_data.q = pqueue_new(); |
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 (s->server) { |
| 142 | d1->cookie_len = sizeof(s->d1->cookie); |
| 143 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 145 | if (!d1->unprocessed_rcds.q || !d1->processed_rcds.q || |
| 146 | !d1->buffered_messages || !d1->sent_messages || |
| 147 | !d1->buffered_app_data.q) { |
| 148 | if (d1->unprocessed_rcds.q) { |
| 149 | pqueue_free(d1->unprocessed_rcds.q); |
| 150 | } |
| 151 | if (d1->processed_rcds.q) { |
| 152 | pqueue_free(d1->processed_rcds.q); |
| 153 | } |
| 154 | if (d1->buffered_messages) { |
| 155 | pqueue_free(d1->buffered_messages); |
| 156 | } |
| 157 | if (d1->sent_messages) { |
| 158 | pqueue_free(d1->sent_messages); |
| 159 | } |
| 160 | if (d1->buffered_app_data.q) { |
| 161 | pqueue_free(d1->buffered_app_data.q); |
| 162 | } |
| 163 | OPENSSL_free(d1); |
| 164 | ssl3_free(s); |
| 165 | return 0; |
| 166 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 168 | s->d1 = d1; |
| 169 | s->method->ssl_clear(s); |
| 170 | return 1; |
| 171 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 173 | static void dtls1_clear_queues(SSL *s) { |
| 174 | pitem *item = NULL; |
| 175 | hm_fragment *frag = NULL; |
| 176 | DTLS1_RECORD_DATA *rdata; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 178 | while ((item = pqueue_pop(s->d1->unprocessed_rcds.q)) != NULL) { |
| 179 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 180 | if (rdata->rbuf.buf) { |
| 181 | OPENSSL_free(rdata->rbuf.buf); |
| 182 | } |
| 183 | OPENSSL_free(item->data); |
| 184 | pitem_free(item); |
| 185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 187 | while ((item = pqueue_pop(s->d1->processed_rcds.q)) != NULL) { |
| 188 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 189 | if (rdata->rbuf.buf) { |
| 190 | OPENSSL_free(rdata->rbuf.buf); |
| 191 | } |
| 192 | OPENSSL_free(item->data); |
| 193 | pitem_free(item); |
| 194 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 195 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 196 | while ((item = pqueue_pop(s->d1->buffered_messages)) != NULL) { |
| 197 | frag = (hm_fragment *)item->data; |
| 198 | dtls1_hm_fragment_free(frag); |
| 199 | pitem_free(item); |
| 200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 202 | while ((item = pqueue_pop(s->d1->sent_messages)) != NULL) { |
| 203 | frag = (hm_fragment *)item->data; |
| 204 | dtls1_hm_fragment_free(frag); |
| 205 | pitem_free(item); |
| 206 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 208 | while ((item = pqueue_pop(s->d1->buffered_app_data.q)) != NULL) { |
| 209 | rdata = (DTLS1_RECORD_DATA *)item->data; |
| 210 | if (rdata->rbuf.buf) { |
| 211 | OPENSSL_free(rdata->rbuf.buf); |
| 212 | } |
| 213 | OPENSSL_free(item->data); |
| 214 | pitem_free(item); |
| 215 | } |
| 216 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 218 | void dtls1_free(SSL *s) { |
| 219 | ssl3_free(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 221 | dtls1_clear_queues(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 222 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 223 | pqueue_free(s->d1->unprocessed_rcds.q); |
| 224 | pqueue_free(s->d1->processed_rcds.q); |
| 225 | pqueue_free(s->d1->buffered_messages); |
| 226 | pqueue_free(s->d1->sent_messages); |
| 227 | pqueue_free(s->d1->buffered_app_data.q); |
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 | OPENSSL_free(s->d1); |
| 230 | s->d1 = NULL; |
| 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 233 | void dtls1_clear(SSL *s) { |
| 234 | pqueue unprocessed_rcds; |
| 235 | pqueue processed_rcds; |
| 236 | pqueue buffered_messages; |
| 237 | pqueue sent_messages; |
| 238 | pqueue buffered_app_data; |
| 239 | unsigned int mtu; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 240 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 241 | if (s->d1) { |
| 242 | unprocessed_rcds = s->d1->unprocessed_rcds.q; |
| 243 | processed_rcds = s->d1->processed_rcds.q; |
| 244 | buffered_messages = s->d1->buffered_messages; |
| 245 | sent_messages = s->d1->sent_messages; |
| 246 | buffered_app_data = s->d1->buffered_app_data.q; |
| 247 | mtu = s->d1->mtu; |
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 | dtls1_clear_queues(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 251 | memset(s->d1, 0, sizeof(*(s->d1))); |
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 | if (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU) { |
| 254 | s->d1->mtu = mtu; |
| 255 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 256 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 257 | s->d1->unprocessed_rcds.q = unprocessed_rcds; |
| 258 | s->d1->processed_rcds.q = processed_rcds; |
| 259 | s->d1->buffered_messages = buffered_messages; |
| 260 | s->d1->sent_messages = sent_messages; |
| 261 | s->d1->buffered_app_data.q = buffered_app_data; |
| 262 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 264 | ssl3_clear(s); |
| 265 | s->version = DTLS1_2_VERSION; |
| 266 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 267 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 268 | long dtls1_ctrl(SSL *s, int cmd, long larg, void *parg) { |
| 269 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 270 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 271 | switch (cmd) { |
| 272 | case DTLS_CTRL_GET_TIMEOUT: |
| 273 | if (dtls1_get_timeout(s, (OPENSSL_timeval *)parg) != NULL) { |
| 274 | ret = 1; |
| 275 | } |
| 276 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 278 | case DTLS_CTRL_HANDLE_TIMEOUT: |
| 279 | ret = dtls1_handle_timeout(s); |
| 280 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 282 | default: |
| 283 | ret = ssl3_ctrl(s, cmd, larg, parg); |
| 284 | break; |
| 285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | /* As it's impossible to use stream ciphers in "datagram" mode, this |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | * simple filter is designed to disengage them in DTLS. Unfortunately |
| 292 | * there is no universal way to identify stream SSL_CIPHER, so we have |
| 293 | * to explicitly list their SSL_* codes. Currently RC4 is the only one |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 294 | * available, but if new ones emerge, they will have to be added... */ |
| 295 | const SSL_CIPHER *dtls1_get_cipher(unsigned int u) { |
| 296 | const SSL_CIPHER *ciph = ssl3_get_cipher(u); |
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 | if (ciph != NULL) { |
| 299 | if (ciph->algorithm_enc == SSL_RC4) { |
| 300 | return NULL; |
| 301 | } |
| 302 | /* TODO(davidben): EVP_AEAD does not work in DTLS yet. */ |
| 303 | if (ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD || |
| 304 | ciph->algorithm2 & SSL_CIPHER_ALGORITHM2_STATEFUL_AEAD) { |
| 305 | return NULL; |
| 306 | } |
| 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 | return ciph; |
| 310 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 311 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 312 | void dtls1_start_timer(SSL *s) { |
| 313 | /* If timer is not set, initialize duration with 1 second */ |
| 314 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 315 | s->d1->timeout_duration = 1; |
| 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 | /* Set timeout to current time */ |
| 319 | get_current_time(&s->d1->next_timeout); |
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 | /* Add duration to current time */ |
| 322 | s->d1->next_timeout.tv_sec += s->d1->timeout_duration; |
| 323 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 324 | &s->d1->next_timeout); |
| 325 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 327 | static OPENSSL_timeval *dtls1_get_timeout(SSL *s, OPENSSL_timeval *timeleft) { |
| 328 | OPENSSL_timeval timenow; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 330 | /* If no timeout is set, just return NULL */ |
| 331 | if (s->d1->next_timeout.tv_sec == 0 && s->d1->next_timeout.tv_usec == 0) { |
| 332 | return NULL; |
| 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 | /* Get current time */ |
| 336 | get_current_time(&timenow); |
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 | /* If timer already expired, set remaining time to 0 */ |
| 339 | if (s->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 340 | (s->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 341 | s->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
| 342 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 343 | return timeleft; |
| 344 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 346 | /* Calculate time left until timer expires */ |
| 347 | memcpy(timeleft, &s->d1->next_timeout, sizeof(OPENSSL_timeval)); |
| 348 | timeleft->tv_sec -= timenow.tv_sec; |
| 349 | timeleft->tv_usec -= timenow.tv_usec; |
| 350 | if (timeleft->tv_usec < 0) { |
| 351 | timeleft->tv_sec--; |
| 352 | timeleft->tv_usec += 1000000; |
| 353 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 355 | /* If remaining time is less than 15 ms, set it to 0 to prevent issues |
| 356 | * because of small devergences with socket timeouts. */ |
| 357 | if (timeleft->tv_sec == 0 && timeleft->tv_usec < 15000) { |
| 358 | memset(timeleft, 0, sizeof(OPENSSL_timeval)); |
| 359 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 360 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 361 | return timeleft; |
| 362 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 364 | int dtls1_is_timer_expired(SSL *s) { |
| 365 | OPENSSL_timeval timeleft; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 366 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 367 | /* Get time left until timeout, return false if no timer running */ |
| 368 | if (dtls1_get_timeout(s, &timeleft) == NULL) { |
| 369 | return 0; |
| 370 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 372 | /* Return false if timer is not expired yet */ |
| 373 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 374 | return 0; |
| 375 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 377 | /* Timer expired, so return true */ |
| 378 | return 1; |
| 379 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 380 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 381 | void dtls1_double_timeout(SSL *s) { |
| 382 | s->d1->timeout_duration *= 2; |
| 383 | if (s->d1->timeout_duration > 60) { |
| 384 | s->d1->timeout_duration = 60; |
| 385 | } |
| 386 | dtls1_start_timer(s); |
| 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 | void dtls1_stop_timer(SSL *s) { |
| 390 | /* Reset everything */ |
| 391 | memset(&(s->d1->timeout), 0, sizeof(struct dtls1_timeout_st)); |
| 392 | memset(&s->d1->next_timeout, 0, sizeof(OPENSSL_timeval)); |
| 393 | s->d1->timeout_duration = 1; |
| 394 | BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 395 | &s->d1->next_timeout); |
| 396 | /* Clear retransmission buffer */ |
| 397 | dtls1_clear_record_buffer(s); |
| 398 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 399 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 400 | int dtls1_check_timeout_num(SSL *s) { |
| 401 | s->d1->timeout.num_alerts++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 403 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
| 404 | if (s->d1->timeout.num_alerts > 2) { |
| 405 | s->d1->mtu = |
| 406 | BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
| 407 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 409 | if (s->d1->timeout.num_alerts > DTLS1_TMO_ALERT_COUNT) { |
| 410 | /* fail the connection, enough alerts have been sent */ |
| 411 | OPENSSL_PUT_ERROR(SSL, dtls1_check_timeout_num, SSL_R_READ_TIMEOUT_EXPIRED); |
| 412 | return -1; |
| 413 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 415 | return 0; |
| 416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 417 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 418 | int dtls1_handle_timeout(SSL *s) { |
| 419 | /* if no timer is expired, don't do anything */ |
| 420 | if (!dtls1_is_timer_expired(s)) { |
| 421 | return 0; |
| 422 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 424 | dtls1_double_timeout(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 426 | if (dtls1_check_timeout_num(s) < 0) { |
| 427 | return -1; |
| 428 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 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 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 435 | dtls1_start_timer(s); |
| 436 | return dtls1_retransmit_buffered_messages(s); |
| 437 | } |
| 438 | |
| 439 | static void get_current_time(OPENSSL_timeval *t) { |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 440 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 441 | struct _timeb time; |
| 442 | _ftime(&time); |
| 443 | t->tv_sec = time.time; |
| 444 | t->tv_usec = time.millitm * 1000; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 445 | #else |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 446 | gettimeofday(t, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 447 | #endif |
| 448 | } |
| 449 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 450 | static void dtls1_set_handshake_header(SSL *s, int htype, unsigned long len) { |
| 451 | uint8_t *p = (uint8_t *)s->init_buf->data; |
| 452 | dtls1_set_message_header(s, p, htype, len, 0, len); |
| 453 | s->init_num = (int)len + DTLS1_HM_HEADER_LENGTH; |
| 454 | s->init_off = 0; |
| 455 | /* Buffer the message to handle re-xmits */ |
| 456 | dtls1_buffer_message(s, 0); |
| 457 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 458 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 459 | static int dtls1_handshake_write( |
| 460 | SSL *s, enum should_add_to_finished_hash should_add_to_finished_hash) { |
| 461 | return dtls1_do_write(s, SSL3_RT_HANDSHAKE, should_add_to_finished_hash); |
| 462 | } |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 463 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 464 | static void dtls1_add_to_finished_hash(SSL *s) { |
| 465 | uint8_t *record = (uint8_t *)&s->init_buf->data[s->init_off]; |
| 466 | const struct hm_header_st *msg_hdr = &s->d1->w_msg_hdr; |
| 467 | uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH]; |
| 468 | uint8_t *p = serialised_header; |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 469 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame^] | 470 | /* Construct the message header as if it were a single fragment. */ |
| 471 | *p++ = msg_hdr->type; |
| 472 | l2n3(msg_hdr->msg_len, p); |
| 473 | s2n(msg_hdr->seq, p); |
| 474 | l2n3(0, p); |
| 475 | l2n3(msg_hdr->msg_len, p); |
| 476 | ssl3_finish_mac(s, serialised_header, sizeof(serialised_header)); |
| 477 | ssl3_finish_mac(s, record + DTLS1_HM_HEADER_LENGTH, |
| 478 | s->init_num - DTLS1_HM_HEADER_LENGTH); |
| 479 | } |