| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <openssl/ssl.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/nid.h> |
| 24 |
| 25 | #include "../crypto/internal.h" |
| 26 | #include "internal.h" |
| 27 |
| 28 |
| 29 | BSSL_NAMESPACE_BEGIN |
| 30 |
| 31 | DTLS1_STATE::DTLS1_STATE() |
| 32 | : has_change_cipher_spec(false), |
| 33 | outgoing_messages_complete(false), |
| 34 | flight_has_reply(false), |
| 35 | handshake_write_overflow(false), |
| 36 | handshake_read_overflow(false), |
| 37 | sending_flight(false), |
| 38 | sending_ack(false), |
| 39 | queued_key_update(QueuedKeyUpdate::kNone) {} |
| 40 |
| 41 | DTLS1_STATE::~DTLS1_STATE() {} |
| 42 |
| 43 | bool DTLS1_STATE::Init() { |
| 44 | // Set up the initial epochs. |
| 45 | read_epoch.aead = SSLAEADContext::CreateNullCipher(); |
| 46 | write_epoch.aead = SSLAEADContext::CreateNullCipher(); |
| 47 | if (read_epoch.aead == nullptr || write_epoch.aead == nullptr) { |
| 48 | return false; |
| 49 | } |
| 50 |
| 51 | return true; |
| 52 | } |
| 53 |
| 54 | bool dtls1_new(SSL *ssl) { |
| 55 | if (!tls_new(ssl)) { |
| 56 | return false; |
| 57 | } |
| 58 | UniquePtr<DTLS1_STATE> d1 = MakeUnique<DTLS1_STATE>(); |
| 59 | if (!d1 || !d1->Init()) { |
| 60 | tls_free(ssl); |
| 61 | return false; |
| 62 | } |
| 63 |
| 64 | ssl->d1 = d1.release(); |
| 65 | return true; |
| 66 | } |
| 67 |
| 68 | void dtls1_free(SSL *ssl) { |
| 69 | tls_free(ssl); |
| 70 |
| 71 | if (ssl == NULL) { |
| 72 | return; |
| 73 | } |
| 74 |
| 75 | Delete(ssl->d1); |
| 76 | ssl->d1 = NULL; |
| 77 | } |
| 78 |
| 79 | void DTLSTimer::StartMicroseconds(OPENSSL_timeval now, uint64_t microseconds) { |
| 80 | uint64_t seconds = microseconds / 1000000; |
| 81 | microseconds %= 1000000; |
| 82 |
| 83 | now.tv_usec += microseconds; |
| 84 | if (now.tv_usec >= 1000000) { |
| 85 | now.tv_usec -= 1000000; |
| 86 | seconds++; |
| 87 | } |
| 88 |
| 89 | if (now.tv_sec > UINT64_MAX - seconds) { |
| 90 | Stop(); |
| 91 | return; |
| 92 | } |
| 93 | now.tv_sec += seconds; |
| 94 | expire_time_ = now; |
| 95 | } |
| 96 |
| 97 | void DTLSTimer::Stop() { expire_time_ = {0, 0}; } |
| 98 |
| 99 | bool DTLSTimer::IsExpired(OPENSSL_timeval now) const { |
| 100 | return MicrosecondsRemaining(now) == 0; |
| 101 | } |
| 102 |
| 103 | bool DTLSTimer::IsSet() const { |
| 104 | return expire_time_.tv_sec != 0 || expire_time_.tv_usec != 0; |
| 105 | } |
| 106 |
| 107 | uint64_t DTLSTimer::MicrosecondsRemaining(OPENSSL_timeval now) const { |
| 108 | if (!IsSet()) { |
| 109 | return kNever; |
| 110 | } |
| 111 |
| 112 | if (now.tv_sec > expire_time_.tv_sec || |
| 113 | (now.tv_sec == expire_time_.tv_sec && |
| 114 | now.tv_usec >= expire_time_.tv_usec)) { |
| 115 | return 0; |
| 116 | } |
| 117 |
| 118 | uint64_t sec = expire_time_.tv_sec - now.tv_sec; |
| 119 | uint32_t usec; |
| 120 | if (expire_time_.tv_usec >= now.tv_usec) { |
| 121 | usec = expire_time_.tv_usec - now.tv_usec; |
| 122 | } else { |
| 123 | sec--; |
| 124 | usec = expire_time_.tv_usec + 1000000 - now.tv_usec; |
| 125 | } |
| 126 |
| 127 | // If remaining time is less than 15 ms, return 0 to prevent issues because of |
| 128 | // small divergences with socket timeouts. |
| 129 | if (sec == 0 && usec < 15000) { |
| 130 | return 0; |
| 131 | } |
| 132 |
| 133 | if (sec > UINT64_MAX / 1000000) { |
| 134 | return kNever; |
| 135 | } |
| 136 | sec *= 1000000; |
| 137 | if (sec > UINT64_MAX - usec) { |
| 138 | return kNever; |
| 139 | } |
| 140 | return sec + usec; |
| 141 | } |
| 142 |
| 143 | void dtls1_stop_timer(SSL *ssl) { |
| 144 | ssl->d1->num_timeouts = 0; |
| 145 | ssl->d1->retransmit_timer.Stop(); |
| 146 | ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms; |
| 147 | } |
| 148 |
| 149 | BSSL_NAMESPACE_END |
| 150 |
| 151 | using namespace bssl; |
| 152 |
| 153 | void DTLSv1_set_initial_timeout_duration(SSL *ssl, uint32_t duration_ms) { |
| 154 | ssl->initial_timeout_duration_ms = duration_ms; |
| 155 | } |
| 156 |
| 157 | int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) { |
| 158 | if (!SSL_is_dtls(ssl)) { |
| 159 | return 0; |
| 160 | } |
| 161 |
| 162 | OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get()); |
| 163 | uint64_t remaining_usec = |
| 164 | ssl->d1->retransmit_timer.MicrosecondsRemaining(now); |
| 165 | remaining_usec = |
| 166 | std::min(remaining_usec, ssl->d1->ack_timer.MicrosecondsRemaining(now)); |
| 167 | if (remaining_usec == DTLSTimer::kNever) { |
| 168 | return 0; // No timeout is set. |
| 169 | } |
| 170 |
| 171 | uint64_t remaining_sec = remaining_usec / 1000000; |
| 172 | remaining_usec %= 1000000; |
| 173 |
| 174 | // |timeval| uses |time_t|, which may be 32-bit. |
| 175 | const auto kTvSecMax = std::numeric_limits<decltype(out->tv_sec)>::max(); |
| 176 | if (remaining_sec > static_cast<uint64_t>(kTvSecMax)) { |
| 177 | out->tv_sec = kTvSecMax; // Saturate the output. |
| 178 | out->tv_usec = 999999; |
| 179 | } else { |
| 180 | out->tv_sec = static_cast<decltype(out->tv_sec)>(remaining_sec); |
| 181 | } |
| 182 | out->tv_usec = remaining_usec; |
| 183 | return 1; |
| 184 | } |
| 185 |
| 186 | int DTLSv1_handle_timeout(SSL *ssl) { |
| 187 | ssl_reset_error_state(ssl); |
| 188 |
| 189 | if (!SSL_is_dtls(ssl)) { |
| 190 | OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 191 | return -1; |
| 192 | } |
| 193 |
| 194 | if (!ssl->d1->ack_timer.IsSet() && !ssl->d1->retransmit_timer.IsSet()) { |
| 195 | // No timers are running. Don't bother querying the clock. |
| 196 | return 0; |
| 197 | } |
| 198 |
| 199 | OPENSSL_timeval now = ssl_ctx_get_current_time(ssl->ctx.get()); |
| 200 | bool any_timer_expired = false; |
| 201 | if (ssl->d1->ack_timer.IsExpired(now)) { |
| 202 | any_timer_expired = true; |
| 203 | ssl->d1->sending_ack = true; |
| 204 | ssl->d1->ack_timer.Stop(); |
| 205 | } |
| 206 |
| 207 | if (ssl->d1->retransmit_timer.IsExpired(now)) { |
| 208 | any_timer_expired = true; |
| 209 | ssl->d1->sending_flight = true; |
| 210 | ssl->d1->retransmit_timer.Stop(); |
| 211 |
| 212 | ssl->d1->num_timeouts++; |
| 213 | // Reduce MTU after 2 unsuccessful retransmissions. |
| 214 | if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS && |
| 215 | !(SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) { |
| 216 | long mtu = BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, |
| 217 | nullptr); |
| 218 | if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) { |
| 219 | ssl->d1->mtu = (unsigned)mtu; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 |
| 224 | if (!any_timer_expired) { |
| 225 | return 0; |
| 226 | } |
| 227 |
| 228 | return dtls1_flush(ssl); |
| 229 | } |
| 230 |