| 1 |
// DTLS implementation written by Nagendra Modadugu ([email protected]) for the OpenSSL project 2005. |
| 2 |
// Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <openssl/ssl.h> |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/mem.h> |
| 13 |
#include <openssl/nid.h> |
| 14 |
|
| 15 |
#include "../crypto/internal.h" |
| 16 |
#include "internal.h" |
| 17 |
|
| 18 |
|
| 19 |
BSSL_NAMESPACE_BEGIN |
| 20 |
|
| 21 |
// DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire |
| 22 |
// before starting to decrease the MTU. |
| 23 |
#define DTLS1_MTU_TIMEOUTS 2 |
| 24 |
|
| 25 |
// DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire |
| 26 |
// before failing the DTLS handshake. |
| 27 |
#define DTLS1_MAX_TIMEOUTS 12 |
| 28 |
|
| 29 |
DTLS1_STATE::DTLS1_STATE() |
| 30 |
: has_change_cipher_spec(false), |
| 31 |
outgoing_messages_complete(false), |
| 32 |
flight_has_reply(false) {} |
| 33 |
|
| 34 |
DTLS1_STATE::~DTLS1_STATE() {} |
| 35 |
|
| 36 |
bool dtls1_new(SSL *ssl) { |
| 37 |
if (!tls_new(ssl)) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
UniquePtr<DTLS1_STATE> d1 = MakeUnique<DTLS1_STATE>(); |
| 41 |
if (!d1) { |
| 42 |
tls_free(ssl); |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
ssl->d1 = d1.release(); |
| 47 |
|
| 48 |
// Set the version to the highest supported version. |
| 49 |
// |
| 50 |
// TODO(davidben): Move this field into |s3|, have it store the normalized |
| 51 |
// protocol version, and implement this pre-negotiation quirk in |SSL_version| |
| 52 |
// at the API boundary rather than in internal state. |
| 53 |
ssl->version = DTLS1_2_VERSION; |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
void dtls1_free(SSL *ssl) { |
| 58 |
tls_free(ssl); |
| 59 |
|
| 60 |
if (ssl == NULL) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
Delete(ssl->d1); |
| 65 |
ssl->d1 = NULL; |
| 66 |
} |
| 67 |
|
| 68 |
void dtls1_start_timer(SSL *ssl) { |
| 69 |
// If timer is not set, initialize duration (by default, 1 second) |
| 70 |
if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
| 71 |
ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms; |
| 72 |
} |
| 73 |
|
| 74 |
// Set timeout to current time |
| 75 |
ssl_get_current_time(ssl, &ssl->d1->next_timeout); |
| 76 |
|
| 77 |
// Add duration to current time |
| 78 |
ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000; |
| 79 |
ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000; |
| 80 |
if (ssl->d1->next_timeout.tv_usec >= 1000000) { |
| 81 |
ssl->d1->next_timeout.tv_sec++; |
| 82 |
ssl->d1->next_timeout.tv_usec -= 1000000; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
bool dtls1_is_timer_expired(SSL *ssl) { |
| 87 |
struct timeval timeleft; |
| 88 |
|
| 89 |
// Get time left until timeout, return false if no timer running |
| 90 |
if (!DTLSv1_get_timeout(ssl, &timeleft)) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
// Return false if timer is not expired yet |
| 95 |
if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
// Timer expired, so return true |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
static void dtls1_double_timeout(SSL *ssl) { |
| 104 |
ssl->d1->timeout_duration_ms *= 2; |
| 105 |
if (ssl->d1->timeout_duration_ms > 60000) { |
| 106 |
ssl->d1->timeout_duration_ms = 60000; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void dtls1_stop_timer(SSL *ssl) { |
| 111 |
ssl->d1->num_timeouts = 0; |
| 112 |
OPENSSL_memset(&ssl->d1->next_timeout, 0, sizeof(ssl->d1->next_timeout)); |
| 113 |
ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms; |
| 114 |
} |
| 115 |
|
| 116 |
bool dtls1_check_timeout_num(SSL *ssl) { |
| 117 |
ssl->d1->num_timeouts++; |
| 118 |
|
| 119 |
// Reduce MTU after 2 unsuccessful retransmissions |
| 120 |
if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS && |
| 121 |
!(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 122 |
long mtu = |
| 123 |
BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, nullptr); |
| 124 |
if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 125 |
ssl->d1->mtu = (unsigned)mtu; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) { |
| 130 |
// fail the connection, enough alerts have been sent |
| 131 |
OPENSSL_PUT_ERROR(SSL, SSL_R_READ_TIMEOUT_EXPIRED); |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
BSSL_NAMESPACE_END |
| 139 |
|
| 140 |
using namespace bssl; |
| 141 |
|
| 142 |
void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) { |
| 143 |
ssl->initial_timeout_duration_ms = duration_ms; |
| 144 |
} |
| 145 |
|
| 146 |
int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) { |
| 147 |
if (!SSL_is_dtls(ssl)) { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
// If no timeout is set, just return 0. |
| 152 |
if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
struct OPENSSL_timeval timenow; |
| 157 |
ssl_get_current_time(ssl, &timenow); |
| 158 |
|
| 159 |
// If timer already expired, set remaining time to 0. |
| 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)) { |
| 163 |
OPENSSL_memset(out, 0, sizeof(*out)); |
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
// Calculate time left until timer expires. |
| 168 |
struct OPENSSL_timeval ret; |
| 169 |
OPENSSL_memcpy(&ret, &ssl->d1->next_timeout, sizeof(ret)); |
| 170 |
ret.tv_sec -= timenow.tv_sec; |
| 171 |
if (ret.tv_usec >= timenow.tv_usec) { |
| 172 |
ret.tv_usec -= timenow.tv_usec; |
| 173 |
} else { |
| 174 |
ret.tv_usec = 1000000 + ret.tv_usec - timenow.tv_usec; |
| 175 |
ret.tv_sec--; |
| 176 |
} |
| 177 |
|
| 178 |
// If remaining time is less than 15 ms, set it to 0 to prevent issues |
| 179 |
// because of small divergences with socket timeouts. |
| 180 |
if (ret.tv_sec == 0 && ret.tv_usec < 15000) { |
| 181 |
OPENSSL_memset(&ret, 0, sizeof(ret)); |
| 182 |
} |
| 183 |
|
| 184 |
// Clamp the result in case of overflow. |
| 185 |
if (ret.tv_sec > INT_MAX) { |
| 186 |
assert(0); |
| 187 |
out->tv_sec = INT_MAX; |
| 188 |
} else { |
| 189 |
out->tv_sec = ret.tv_sec; |
| 190 |
} |
| 191 |
|
| 192 |
out->tv_usec = ret.tv_usec; |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
int DTLSv1_handle_timeout(SSL *ssl) { |
| 197 |
ssl_reset_error_state(ssl); |
| 198 |
|
| 199 |
if (!SSL_is_dtls(ssl)) { |
| 200 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
// If no timer is expired, don't do anything. |
| 205 |
if (!dtls1_is_timer_expired(ssl)) { |
| 206 |
return 0; |
| 207 |
} |
| 208 |
|
| 209 |
if (!dtls1_check_timeout_num(ssl)) { |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
dtls1_double_timeout(ssl); |
| 214 |
dtls1_start_timer(ssl); |
| 215 |
return dtls1_retransmit_outgoing_messages(ssl); |
| 216 |
} |