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