d1_lib.cc
Go to the documentation of this file.
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 
57 #include <openssl/ssl.h>
58 
59 #include <assert.h>
60 #include <limits.h>
61 #include <string.h>
62 
63 #include <openssl/err.h>
64 #include <openssl/mem.h>
65 #include <openssl/nid.h>
66 
67 #include "../crypto/internal.h"
68 #include "internal.h"
69 
70 
72 
73 // DTLS1_MTU_TIMEOUTS is the maximum number of timeouts to expire
74 // before starting to decrease the MTU.
75 #define DTLS1_MTU_TIMEOUTS 2
76 
77 // DTLS1_MAX_TIMEOUTS is the maximum number of timeouts to expire
78 // before failing the DTLS handshake.
79 #define DTLS1_MAX_TIMEOUTS 12
80 
82  : has_change_cipher_spec(false),
83  outgoing_messages_complete(false),
84  flight_has_reply(false) {}
85 
87 
88 bool dtls1_new(SSL *ssl) {
89  if (!tls_new(ssl)) {
90  return false;
91  }
92  UniquePtr<DTLS1_STATE> d1 = MakeUnique<DTLS1_STATE>();
93  if (!d1) {
94  tls_free(ssl);
95  return false;
96  }
97 
98  ssl->d1 = d1.release();
99 
100  // Set the version to the highest supported version.
101  //
102  // TODO(davidben): Move this field into |s3|, have it store the normalized
103  // protocol version, and implement this pre-negotiation quirk in |SSL_version|
104  // at the API boundary rather than in internal state.
105  ssl->version = DTLS1_2_VERSION;
106  return true;
107 }
108 
109 void dtls1_free(SSL *ssl) {
110  tls_free(ssl);
111 
112  if (ssl == NULL) {
113  return;
114  }
115 
116  Delete(ssl->d1);
117  ssl->d1 = NULL;
118 }
119 
120 void dtls1_start_timer(SSL *ssl) {
121  // If timer is not set, initialize duration (by default, 1 second)
122  if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
123  ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
124  }
125 
126  // Set timeout to current time
127  ssl_get_current_time(ssl, &ssl->d1->next_timeout);
128 
129  // Add duration to current time
130  ssl->d1->next_timeout.tv_sec += ssl->d1->timeout_duration_ms / 1000;
131  ssl->d1->next_timeout.tv_usec += (ssl->d1->timeout_duration_ms % 1000) * 1000;
132  if (ssl->d1->next_timeout.tv_usec >= 1000000) {
133  ssl->d1->next_timeout.tv_sec++;
134  ssl->d1->next_timeout.tv_usec -= 1000000;
135  }
136 }
137 
139  struct timeval timeleft;
140 
141  // Get time left until timeout, return false if no timer running
142  if (!DTLSv1_get_timeout(ssl, &timeleft)) {
143  return false;
144  }
145 
146  // Return false if timer is not expired yet
147  if (timeleft.tv_sec > 0 || timeleft.tv_usec > 0) {
148  return false;
149  }
150 
151  // Timer expired, so return true
152  return true;
153 }
154 
155 static void dtls1_double_timeout(SSL *ssl) {
156  ssl->d1->timeout_duration_ms *= 2;
157  if (ssl->d1->timeout_duration_ms > 60000) {
158  ssl->d1->timeout_duration_ms = 60000;
159  }
160 }
161 
162 void dtls1_stop_timer(SSL *ssl) {
163  ssl->d1->num_timeouts = 0;
164  OPENSSL_memset(&ssl->d1->next_timeout, 0, sizeof(ssl->d1->next_timeout));
165  ssl->d1->timeout_duration_ms = ssl->initial_timeout_duration_ms;
166 }
167 
169  ssl->d1->num_timeouts++;
170 
171  // Reduce MTU after 2 unsuccessful retransmissions
172  if (ssl->d1->num_timeouts > DTLS1_MTU_TIMEOUTS &&
174  long mtu =
175  BIO_ctrl(ssl->wbio.get(), BIO_CTRL_DGRAM_GET_FALLBACK_MTU, 0, nullptr);
176  if (mtu >= 0 && mtu <= (1 << 30) && (unsigned)mtu >= dtls1_min_mtu()) {
177  ssl->d1->mtu = (unsigned)mtu;
178  }
179  }
180 
181  if (ssl->d1->num_timeouts > DTLS1_MAX_TIMEOUTS) {
182  // fail the connection, enough alerts have been sent
184  return false;
185  }
186 
187  return true;
188 }
189 
191 
192 using namespace bssl;
193 
194 void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms) {
195  ssl->initial_timeout_duration_ms = duration_ms;
196 }
197 
198 int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out) {
199  if (!SSL_is_dtls(ssl)) {
200  return 0;
201  }
202 
203  // If no timeout is set, just return 0.
204  if (ssl->d1->next_timeout.tv_sec == 0 && ssl->d1->next_timeout.tv_usec == 0) {
205  return 0;
206  }
207 
208  struct OPENSSL_timeval timenow;
209  ssl_get_current_time(ssl, &timenow);
210 
211  // If timer already expired, set remaining time to 0.
212  if (ssl->d1->next_timeout.tv_sec < timenow.tv_sec ||
213  (ssl->d1->next_timeout.tv_sec == timenow.tv_sec &&
214  ssl->d1->next_timeout.tv_usec <= timenow.tv_usec)) {
215  OPENSSL_memset(out, 0, sizeof(*out));
216  return 1;
217  }
218 
219  // Calculate time left until timer expires.
220  struct OPENSSL_timeval ret;
221  OPENSSL_memcpy(&ret, &ssl->d1->next_timeout, sizeof(ret));
222  ret.tv_sec -= timenow.tv_sec;
223  if (ret.tv_usec >= timenow.tv_usec) {
224  ret.tv_usec -= timenow.tv_usec;
225  } else {
226  ret.tv_usec = 1000000 + ret.tv_usec - timenow.tv_usec;
227  ret.tv_sec--;
228  }
229 
230  // If remaining time is less than 15 ms, set it to 0 to prevent issues
231  // because of small divergences with socket timeouts.
232  if (ret.tv_sec == 0 && ret.tv_usec < 15000) {
233  OPENSSL_memset(&ret, 0, sizeof(ret));
234  }
235 
236  // Clamp the result in case of overflow.
237  if (ret.tv_sec > INT_MAX) {
238  assert(0);
239  out->tv_sec = INT_MAX;
240  } else {
241  out->tv_sec = ret.tv_sec;
242  }
243 
244  out->tv_usec = ret.tv_usec;
245  return 1;
246 }
247 
250 
251  if (!SSL_is_dtls(ssl)) {
253  return -1;
254  }
255 
256  // If no timer is expired, don't do anything.
257  if (!dtls1_is_timer_expired(ssl)) {
258  return 0;
259  }
260 
261  if (!dtls1_check_timeout_num(ssl)) {
262  return -1;
263  }
264 
266  dtls1_start_timer(ssl);
268 }
dtls1_start_timer
void dtls1_start_timer(SSL *ssl)
Definition: d1_lib.cc:120
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
dtls1_double_timeout
static void dtls1_double_timeout(SSL *ssl)
Definition: d1_lib.cc:155
tls_free
void tls_free(SSL *ssl)
Definition: s3_lib.cc:210
DTLS1_2_VERSION
#define DTLS1_2_VERSION
Definition: ssl.h:656
dtls1_check_timeout_num
bool dtls1_check_timeout_num(SSL *ssl)
Definition: d1_lib.cc:168
false
#define false
Definition: setup_once.h:323
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
string.h
DTLS1_MTU_TIMEOUTS
#define DTLS1_MTU_TIMEOUTS
Definition: d1_lib.cc:75
bssl
Definition: hpke_test.cc:37
ssl_get_current_time
void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock)
Definition: ssl_lib.cc:354
DTLSv1_handle_timeout
int DTLSv1_handle_timeout(SSL *ssl)
Definition: d1_lib.cc:248
DTLS1_STATE::DTLS1_STATE
DTLS1_STATE()
Definition: d1_lib.cc:81
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
OPENSSL_timeval::tv_sec
uint64_t tv_sec
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2874
dtls1_free
void dtls1_free(SSL *ssl)
Definition: d1_lib.cc:109
dtls1_is_timer_expired
bool dtls1_is_timer_expired(SSL *ssl)
Definition: d1_lib.cc:138
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
Definition: err.h:372
BIO_ctrl
#define BIO_ctrl
Definition: boringssl_prefix_symbols.h:779
OPENSSL_timeval
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2873
dtls1_retransmit_outgoing_messages
int dtls1_retransmit_outgoing_messages(SSL *ssl)
Definition: d1_both.cc:820
SSL_is_dtls
#define SSL_is_dtls
Definition: boringssl_prefix_symbols.h:402
ssl_st::wbio
bssl::UniquePtr< BIO > wbio
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3723
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
ssl_st::initial_timeout_duration_ms
unsigned initial_timeout_duration_ms
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3746
SSL_R_READ_TIMEOUT_EXPIRED
#define SSL_R_READ_TIMEOUT_EXPIRED
Definition: ssl.h:5465
err.h
Delete
void Delete(T *t)
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:208
ssl.h
OPENSSL_timeval::tv_usec
uint32_t tv_usec
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2875
ssl_reset_error_state
void ssl_reset_error_state(SSL *ssl)
Definition: ssl_lib.cc:201
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
nid.h
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
timeval::tv_usec
long tv_usec
Definition: setup_once.h:122
SSL_OP_NO_QUERY_MTU
#define SSL_OP_NO_QUERY_MTU
Definition: ssl.h:706
timeval
Definition: setup_once.h:113
ssl_st::d1
bssl::DTLS1_STATE * d1
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3731
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
BIO_CTRL_DGRAM_GET_FALLBACK_MTU
#define BIO_CTRL_DGRAM_GET_FALLBACK_MTU
Definition: bio.h:587
DTLSv1_get_timeout
int DTLSv1_get_timeout(const SSL *ssl, struct timeval *out)
Definition: d1_lib.cc:198
SSL_get_options
#define SSL_get_options
Definition: boringssl_prefix_symbols.h:354
tls_new
bool tls_new(SSL *ssl)
Definition: s3_lib.cc:186
dtls1_stop_timer
void dtls1_stop_timer(SSL *ssl)
Definition: d1_lib.cc:162
mem.h
DTLS1_STATE::~DTLS1_STATE
~DTLS1_STATE()
Definition: d1_lib.cc:86
DTLSv1_set_initial_timeout_duration
void DTLSv1_set_initial_timeout_duration(SSL *ssl, unsigned int duration_ms)
Definition: d1_lib.cc:194
dtls1_min_mtu
unsigned int dtls1_min_mtu(void)
Definition: d1_both.cc:831
ssl_st::version
uint16_t version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3715
DTLS1_MAX_TIMEOUTS
#define DTLS1_MAX_TIMEOUTS
Definition: d1_lib.cc:79
dtls1_new
bool dtls1_new(SSL *ssl)
Definition: d1_lib.cc:88


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:08