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 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 57 | #include <openssl/ssl.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 59 | #include <limits.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 60 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 62 | #include <openssl/err.h> |
| 63 | #include <openssl/mem.h> |
| 64 | #include <openssl/obj.h> |
| 65 | |
| 66 | #include "internal.h" |
| 67 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | #if defined(OPENSSL_WINDOWS) |
| 69 | #include <sys/timeb.h> |
| 70 | #else |
| 71 | #include <sys/socket.h> |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 72 | #include <sys/time.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 75 | |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 76 | /* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire |
| 77 | * before starting to decrease the MTU. */ |
| 78 | #define DTLS1_MTU_TIMEOUTS 2 |
| 79 | |
| 80 | /* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire |
| 81 | * before failing the DTLS handshake. */ |
| 82 | #define DTLS1_MAX_TIMEOUTS 12 |
| 83 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 84 | static void get_current_time(const SSL *ssl, struct timeval *out_clock); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 85 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 86 | int dtls1_new(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 87 | DTLS1_STATE *d1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 89 | if (!ssl3_new(ssl)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | d1 = OPENSSL_malloc(sizeof *d1); |
| 93 | if (d1 == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 94 | ssl3_free(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | memset(d1, 0, sizeof *d1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 99 | d1->buffered_messages = pqueue_new(); |
| 100 | d1->sent_messages = pqueue_new(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | |
David Benjamin | 4417d05 | 2015-04-05 04:17:25 -0400 | [diff] [blame] | 102 | if (!d1->buffered_messages || !d1->sent_messages) { |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 103 | pqueue_free(d1->buffered_messages); |
| 104 | pqueue_free(d1->sent_messages); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 105 | OPENSSL_free(d1); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 106 | ssl3_free(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 109 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 110 | ssl->d1 = d1; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 111 | |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 112 | /* Set the version to the highest supported version. |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 113 | * |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 114 | * TODO(davidben): Move this field into |s3|, have it store the normalized |
| 115 | * protocol version, and implement this pre-negotiation quirk in |SSL_version| |
| 116 | * at the API boundary rather than in internal state. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 117 | ssl->version = DTLS1_2_VERSION; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 118 | return 1; |
| 119 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 120 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 121 | static void dtls1_clear_queues(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 122 | pitem *item = NULL; |
| 123 | hm_fragment *frag = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 125 | while ((item = pqueue_pop(ssl->d1->buffered_messages)) != NULL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 126 | frag = (hm_fragment *)item->data; |
| 127 | dtls1_hm_fragment_free(frag); |
| 128 | pitem_free(item); |
| 129 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 131 | while ((item = pqueue_pop(ssl->d1->sent_messages)) != NULL) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 132 | frag = (hm_fragment *)item->data; |
| 133 | dtls1_hm_fragment_free(frag); |
| 134 | pitem_free(item); |
| 135 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 136 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 138 | void dtls1_free(SSL *ssl) { |
| 139 | ssl3_free(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 141 | if (ssl == NULL || ssl->d1 == NULL) { |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 145 | dtls1_clear_queues(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 146 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 147 | pqueue_free(ssl->d1->buffered_messages); |
| 148 | pqueue_free(ssl->d1->sent_messages); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 149 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 150 | OPENSSL_free(ssl->d1); |
| 151 | ssl->d1 = NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 152 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 153 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 154 | int dtls1_supports_cipher(const SSL_CIPHER *cipher) { |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 155 | /* DTLS does not support stream ciphers. The NULL cipher is rejected because |
| 156 | * it's not needed. */ |
| 157 | return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 158 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 160 | void dtls1_start_timer(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 161 | /* If timer is not set, initialize duration with 1 second */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 162 | if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
| 163 | ssl->d1->timeout_duration = 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 164 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 166 | /* Set timeout to current time */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 167 | get_current_time(ssl, &ssl->d1->next_timeout); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 169 | /* Add duration to current time */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 170 | ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration; |
| 171 | BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 172 | &ssl->d1->next_timeout); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 174 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 175 | int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) { |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 176 | if (!SSL_IS_DTLS(ssl)) { |
| 177 | return 0; |
| 178 | } |
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 | /* If no timeout is set, just return NULL */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 181 | if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
| 182 | return 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 183 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 185 | /* Get current time */ |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 186 | struct timeval timenow; |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 187 | get_current_time(ssl, &timenow); |
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 | /* If timer already expired, set remaining time to 0 */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 190 | if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 191 | (ssl->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 192 | ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 193 | memset(out, 0, sizeof(struct timeval)); |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 194 | return 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 195 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 196 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 197 | /* Calculate time left until timer expires */ |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 198 | memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval)); |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 199 | out->tv_sec -= timenow.tv_sec; |
| 200 | out->tv_usec -= timenow.tv_usec; |
| 201 | if (out->tv_usec < 0) { |
| 202 | out->tv_sec--; |
| 203 | out->tv_usec += 1000000; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 204 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 206 | /* If remaining time is less than 15 ms, set it to 0 to prevent issues |
| 207 | * because of small devergences with socket timeouts. */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 208 | if (out->tv_sec == 0 && out->tv_usec < 15000) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 209 | memset(out, 0, sizeof(struct timeval)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 210 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 212 | return 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 213 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 215 | int dtls1_is_timer_expired(SSL *ssl) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 216 | struct timeval timeleft; |
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 | /* Get time left until timeout, return false if no timer running */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 219 | if (!DTLSv1_get_timeout(ssl, &timeleft)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 220 | return 0; |
| 221 | } |
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 | /* Return false if timer is not expired yet */ |
| 224 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 225 | return 0; |
| 226 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 227 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 228 | /* Timer expired, so return true */ |
| 229 | return 1; |
| 230 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 231 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 232 | void dtls1_double_timeout(SSL *ssl) { |
| 233 | ssl->d1->timeout_duration *= 2; |
| 234 | if (ssl->d1->timeout_duration > 60) { |
| 235 | ssl->d1->timeout_duration = 60; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 236 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 237 | dtls1_start_timer(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 238 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 240 | void dtls1_stop_timer(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 241 | /* Reset everything */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 242 | ssl->d1->num_timeouts = 0; |
| 243 | memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval)); |
| 244 | ssl->d1->timeout_duration = 1; |
| 245 | BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
| 246 | &ssl->d1->next_timeout); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 247 | /* Clear retransmission buffer */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 248 | dtls1_clear_record_buffer(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 249 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 251 | int dtls1_check_timeout_num(SSL *ssl) { |
| 252 | ssl->d1->num_timeouts++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 253 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 254 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 255 | if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS && |
| 256 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 257 | long mtu = BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 258 | NULL); |
| 259 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 260 | ssl->d1->mtu = (unsigned)mtu; |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 261 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 262 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 264 | if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 265 | /* fail the connection, enough alerts have been sent */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 266 | OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 267 | return -1; |
| 268 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 270 | return 0; |
| 271 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 272 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 273 | int DTLSv1_handle_timeout(SSL *ssl) { |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 274 | /* Functions which use SSL_get_error must clear the error queue on entry. */ |
| 275 | ERR_clear_error(); |
| 276 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 277 | if (!SSL_IS_DTLS(ssl)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 278 | return -1; |
| 279 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 281 | /* if no timer is expired, don't do anything */ |
| 282 | if (!dtls1_is_timer_expired(ssl)) { |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | dtls1_double_timeout(ssl); |
| 287 | |
| 288 | if (dtls1_check_timeout_num(ssl) < 0) { |
| 289 | return -1; |
| 290 | } |
| 291 | |
| 292 | dtls1_start_timer(ssl); |
| 293 | return dtls1_retransmit_buffered_messages(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 294 | } |
| 295 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 296 | static void get_current_time(const SSL *ssl, struct timeval *out_clock) { |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 297 | if (ssl->ctx->current_time_cb != NULL) { |
| 298 | ssl->ctx->current_time_cb(ssl, out_clock); |
| 299 | return; |
| 300 | } |
| 301 | |
Adam Langley | ded9358 | 2014-07-31 15:23:51 -0700 | [diff] [blame] | 302 | #if defined(OPENSSL_WINDOWS) |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 303 | struct _timeb time; |
| 304 | _ftime(&time); |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 305 | out_clock->tv_sec = time.time; |
| 306 | out_clock->tv_usec = time.millitm * 1000; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 307 | #else |
David Benjamin | 377fc31 | 2015-01-26 00:22:12 -0500 | [diff] [blame] | 308 | gettimeofday(out_clock, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 309 | #endif |
| 310 | } |
| 311 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 312 | int dtls1_set_handshake_header(SSL *ssl, int htype, unsigned long len) { |
| 313 | uint8_t *message = (uint8_t *)ssl->init_buf->data; |
| 314 | const struct hm_header_st *msg_hdr = &ssl->d1->w_msg_hdr; |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 315 | uint8_t serialised_header[DTLS1_HM_HEADER_LENGTH]; |
| 316 | uint8_t *p = serialised_header; |
| 317 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 318 | ssl->d1->handshake_write_seq = ssl->d1->next_handshake_write_seq; |
| 319 | ssl->d1->next_handshake_write_seq++; |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 320 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 321 | dtls1_set_message_header(ssl, htype, len, ssl->d1->handshake_write_seq, 0, |
| 322 | len); |
| 323 | ssl->init_num = (int)len + DTLS1_HM_HEADER_LENGTH; |
| 324 | ssl->init_off = 0; |
David Benjamin | 16d031a | 2014-12-14 18:52:44 -0500 | [diff] [blame] | 325 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 326 | /* Buffer the message to handle re-xmits */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 327 | dtls1_buffer_message(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 329 | /* Add the new message to the handshake hash. Serialize the message |
| 330 | * header as if it were a single fragment. */ |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 331 | *p++ = msg_hdr->type; |
| 332 | l2n3(msg_hdr->msg_len, p); |
| 333 | s2n(msg_hdr->seq, p); |
| 334 | l2n3(0, p); |
| 335 | l2n3(msg_hdr->msg_len, p); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 336 | return ssl3_update_handshake_hash(ssl, serialised_header, |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 337 | sizeof(serialised_header)) && |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 338 | ssl3_update_handshake_hash(ssl, message + DTLS1_HM_HEADER_LENGTH, len); |
David Benjamin | e4824e8 | 2014-12-14 19:44:34 -0500 | [diff] [blame] | 339 | } |
| 340 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 341 | int dtls1_handshake_write(SSL *ssl) { |
| 342 | return dtls1_do_handshake_write(ssl, dtls1_use_current_epoch); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 343 | } |