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 | b5eb195 | 2016-06-17 18:44:38 -0400 | [diff] [blame] | 59 | #include <assert.h> |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 60 | #include <limits.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 61 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 63 | #include <openssl/err.h> |
| 64 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 65 | #include <openssl/nid.h> |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 66 | |
| 67 | #include "internal.h" |
| 68 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 69 | |
David Benjamin | e33b9b0 | 2015-01-26 01:27:22 -0500 | [diff] [blame] | 70 | /* DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire |
| 71 | * before starting to decrease the MTU. */ |
| 72 | #define DTLS1_MTU_TIMEOUTS 2 |
| 73 | |
| 74 | /* DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire |
| 75 | * before failing the DTLS handshake. */ |
| 76 | #define DTLS1_MAX_TIMEOUTS 12 |
| 77 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 78 | int dtls1_new(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 79 | DTLS1_STATE *d1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 80 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 81 | if (!ssl3_new(ssl)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | d1 = OPENSSL_malloc(sizeof *d1); |
| 85 | if (d1 == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 86 | ssl3_free(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | memset(d1, 0, sizeof *d1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 91 | ssl->d1 = d1; |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 92 | |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 93 | /* Set the version to the highest supported version. |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 94 | * |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 95 | * TODO(davidben): Move this field into |s3|, have it store the normalized |
| 96 | * protocol version, and implement this pre-negotiation quirk in |SSL_version| |
| 97 | * at the API boundary rather than in internal state. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 98 | ssl->version = DTLS1_2_VERSION; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 99 | return 1; |
| 100 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 101 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 102 | void dtls1_free(SSL *ssl) { |
| 103 | ssl3_free(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 105 | if (ssl == NULL || ssl->d1 == NULL) { |
David Benjamin | 62fd162 | 2015-01-11 13:30:01 -0500 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
David Benjamin | ec847ce | 2016-06-17 19:30:47 -0400 | [diff] [blame] | 109 | dtls_clear_incoming_messages(ssl); |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 110 | dtls_clear_outgoing_messages(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 111 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 112 | OPENSSL_free(ssl->d1); |
| 113 | ssl->d1 = NULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 114 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
David Benjamin | a1c90a5 | 2015-05-30 17:03:14 -0400 | [diff] [blame] | 116 | int dtls1_supports_cipher(const SSL_CIPHER *cipher) { |
Matt Braithwaite | af09675 | 2015-09-02 19:48:16 -0700 | [diff] [blame] | 117 | /* DTLS does not support stream ciphers. The NULL cipher is rejected because |
| 118 | * it's not needed. */ |
| 119 | return cipher->algorithm_enc != SSL_RC4 && cipher->algorithm_enc != SSL_eNULL; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 120 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 121 | |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 122 | void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) { |
| 123 | ssl->initial_timeout_duration_ms = duration_ms; |
| 124 | } |
| 125 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 126 | void dtls1_start_timer(SSL *ssl) { |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 127 | /* If timer is not set, initialize duration (by default, 1 second) */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 128 | if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 129 | ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 130 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 131 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 132 | /* Set timeout to current time */ |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame^] | 133 | ssl_get_current_time(ssl, &ssl->d1->next_timeout); |
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 | /* Add duration to current time */ |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 136 | ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000; |
| 137 | ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000; |
| 138 | if (ssl->d1->next_timeout.tv_usec >= 1000000) { |
| 139 | ssl->d1->next_timeout.tv_sec++; |
| 140 | ssl->d1->next_timeout.tv_usec -= 1000000; |
| 141 | } |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 142 | BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 143 | &ssl->d1->next_timeout); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 144 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 145 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 146 | int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 147 | if (!SSL_is_dtls(ssl)) { |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 148 | return 0; |
| 149 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 151 | /* If no timeout is set, just return NULL */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 152 | if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
| 153 | return 0; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 154 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 155 | |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 156 | struct timeval timenow; |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame^] | 157 | ssl_get_current_time(ssl, &timenow); |
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 | /* If timer already expired, set remaining time to 0 */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 160 | if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec || |
| 161 | (ssl->d1->next_timeout.tv_sec == timenow.tv_sec && |
| 162 | ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 163 | memset(out, 0, sizeof(struct timeval)); |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 164 | return 1; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 165 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 166 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 167 | /* Calculate time left until timer expires */ |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 168 | memcpy(out, &ssl->d1->next_timeout, sizeof(struct timeval)); |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 169 | out->tv_sec -= timenow.tv_sec; |
| 170 | out->tv_usec -= timenow.tv_usec; |
| 171 | if (out->tv_usec < 0) { |
| 172 | out->tv_sec--; |
| 173 | out->tv_usec += 1000000; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 174 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 176 | /* If remaining time is less than 15 ms, set it to 0 to prevent issues |
| 177 | * because of small devergences with socket timeouts. */ |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 178 | if (out->tv_sec == 0 && out->tv_usec < 15000) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 179 | memset(out, 0, sizeof(struct timeval)); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 180 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 181 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 182 | return 1; |
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 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 185 | int dtls1_is_timer_expired(SSL *ssl) { |
David Benjamin | 4d2e7ce | 2015-05-08 13:29:45 -0400 | [diff] [blame] | 186 | struct timeval timeleft; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 188 | /* Get time left until timeout, return false if no timer running */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 189 | if (!DTLSv1_get_timeout(ssl, &timeleft)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 190 | return 0; |
| 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 | /* Return false if timer is not expired yet */ |
| 194 | if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 195 | return 0; |
| 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 | /* Timer expired, so return true */ |
| 199 | return 1; |
| 200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 201 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 202 | void dtls1_double_timeout(SSL *ssl) { |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 203 | ssl->d1->timeout_duration_ms *= 2; |
| 204 | if (ssl->d1->timeout_duration_ms > 60000) { |
| 205 | ssl->d1->timeout_duration_ms = 60000; |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 206 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 207 | dtls1_start_timer(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 208 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 210 | void dtls1_stop_timer(SSL *ssl) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 211 | /* Reset everything */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 212 | ssl->d1->num_timeouts = 0; |
| 213 | memset(&ssl->d1->next_timeout, 0, sizeof(struct timeval)); |
Taylor Brandstetter | 376a0fe | 2016-05-10 19:30:28 -0700 | [diff] [blame] | 214 | ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms; |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 215 | BIO_ctrl(ssl->rbio, BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 216 | &ssl->d1->next_timeout); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 217 | /* Clear retransmission buffer */ |
David Benjamin | 29a83c5 | 2016-06-17 19:12:54 -0400 | [diff] [blame] | 218 | dtls_clear_outgoing_messages(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 221 | int dtls1_check_timeout_num(SSL *ssl) { |
| 222 | ssl->d1->num_timeouts++; |
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 | /* Reduce MTU after 2 unsuccessful retransmissions */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 225 | if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS && |
| 226 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
David Benjamin | 2f87112 | 2016-05-20 14:27:17 -0400 | [diff] [blame] | 227 | long mtu = BIO_ctrl(ssl->wbio, BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, NULL); |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 228 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 229 | ssl->d1->mtu = (unsigned)mtu; |
David Benjamin | 80cee91 | 2015-01-11 19:36:58 -0500 | [diff] [blame] | 230 | } |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 231 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 232 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 233 | if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 234 | /* fail the connection, enough alerts have been sent */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 235 | OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 236 | return -1; |
| 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 | return 0; |
| 240 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 241 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 242 | int DTLSv1_handle_timeout(SSL *ssl) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 243 | ssl->rwstate = SSL_NOTHING; |
David Benjamin | 15c1488 | 2016-03-14 14:25:46 -0400 | [diff] [blame] | 244 | /* Functions which use SSL_get_error must clear the error queue on entry. */ |
| 245 | ERR_clear_error(); |
| 246 | |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 247 | if (!SSL_is_dtls(ssl)) { |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 248 | return -1; |
| 249 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 250 | |
David Benjamin | 8c24980 | 2015-05-05 09:44:18 -0400 | [diff] [blame] | 251 | /* if no timer is expired, don't do anything */ |
| 252 | if (!dtls1_is_timer_expired(ssl)) { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | dtls1_double_timeout(ssl); |
| 257 | |
| 258 | if (dtls1_check_timeout_num(ssl) < 0) { |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | dtls1_start_timer(ssl); |
David Benjamin | aad50db | 2016-06-23 17:49:12 -0400 | [diff] [blame] | 263 | return dtls1_retransmit_outgoing_messages(ssl); |
Adam Langley | 71d8a08 | 2014-12-13 16:28:18 -0800 | [diff] [blame] | 264 | } |
| 265 | |
David Benjamin | aa7734b | 2016-06-07 16:40:46 -0400 | [diff] [blame] | 266 | void dtls1_expect_flight(SSL *ssl) { |
| 267 | dtls1_start_timer(ssl); |
| 268 | } |
| 269 | |
| 270 | void dtls1_received_flight(SSL *ssl) { |
| 271 | dtls1_stop_timer(ssl); |
| 272 | } |