ssl_versions.cc
Go to the documentation of this file.
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/ssl.h>
16 
17 #include <assert.h>
18 
19 #include <openssl/bytestring.h>
20 #include <openssl/err.h>
21 
22 #include "internal.h"
23 #include "../crypto/internal.h"
24 
25 
27 
29  switch (version) {
30  case TLS1_VERSION:
31  case TLS1_1_VERSION:
32  case TLS1_2_VERSION:
33  case TLS1_3_VERSION:
34  *out = version;
35  return true;
36 
37  case DTLS1_VERSION:
38  // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
40  return true;
41 
42  case DTLS1_2_VERSION:
44  return true;
45 
46  default:
47  return false;
48  }
49 }
50 
51 // The follow arrays are the supported versions for TLS and DTLS, in order of
52 // decreasing preference.
53 
54 static const uint16_t kTLSVersions[] = {
59 };
60 
61 static const uint16_t kDTLSVersions[] = {
64 };
65 
67  const SSL_PROTOCOL_METHOD *method) {
68  return method->is_dtls ? Span<const uint16_t>(kDTLSVersions)
70 }
71 
73  uint16_t version) {
74  for (uint16_t supported : get_method_versions(method)) {
75  if (supported == version) {
76  return true;
77  }
78  }
79  return false;
80 }
81 
82 // The following functions map between API versions and wire versions. The
83 // public API works on wire versions.
84 
85 static const char *ssl_version_to_string(uint16_t version) {
86  switch (version) {
87  case TLS1_3_VERSION:
88  return "TLSv1.3";
89 
90  case TLS1_2_VERSION:
91  return "TLSv1.2";
92 
93  case TLS1_1_VERSION:
94  return "TLSv1.1";
95 
96  case TLS1_VERSION:
97  return "TLSv1";
98 
99  case DTLS1_VERSION:
100  return "DTLSv1";
101 
102  case DTLS1_2_VERSION:
103  return "DTLSv1.2";
104 
105  default:
106  return "unknown";
107  }
108 }
109 
111  return version;
112 }
113 
114 // api_version_to_wire maps |version| to some representative wire version.
116  // Check it is a real protocol version.
117  uint16_t unused;
118  if (!ssl_protocol_version_from_wire(&unused, version)) {
119  return false;
120  }
121 
122  *out = version;
123  return true;
124 }
125 
127  uint16_t version) {
131  return false;
132  }
133 
134  *out = version;
135  return true;
136 }
137 
139  uint16_t version) {
140  // Zero is interpreted as the default minimum version.
141  if (version == 0) {
142  *out = method->is_dtls ? DTLS1_VERSION : TLS1_VERSION;
143  return true;
144  }
145 
147 }
148 
150  uint16_t version) {
151  // Zero is interpreted as the default maximum version.
152  if (version == 0) {
153  *out = method->is_dtls ? DTLS1_2_VERSION : TLS1_3_VERSION;
154  return true;
155  }
156 
158 }
159 
160 const struct {
163 } kProtocolVersions[] = {
168 };
169 
170 bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
171  uint16_t *out_max_version) {
172  // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
173  // DTLS 1.0 should be mapped to TLS 1.1.
174  uint32_t options = hs->ssl->options;
175  if (SSL_is_dtls(hs->ssl)) {
177  if (options & SSL_OP_NO_DTLSv1) {
179  }
180  }
181 
182  uint16_t min_version, max_version;
183  if (!ssl_protocol_version_from_wire(&min_version,
184  hs->config->conf_min_version) ||
185  !ssl_protocol_version_from_wire(&max_version,
186  hs->config->conf_max_version)) {
188  return false;
189  }
190 
191  // QUIC requires TLS 1.3.
192  if (hs->ssl->quic_method && min_version < TLS1_3_VERSION) {
193  min_version = TLS1_3_VERSION;
194  }
195 
196  // The |SSL_OP_NO_*| flags disable individual protocols. This has two
197  // problems. First, prior to TLS 1.3, the protocol can only express a
198  // contiguous range of versions. Second, a library consumer trying to set a
199  // maximum version cannot disable protocol versions that get added in a future
200  // version of the library.
201  //
202  // To account for both of these, OpenSSL interprets the client-side bitmask
203  // as a min/max range by picking the lowest contiguous non-empty range of
204  // enabled protocols. Note that this means it is impossible to set a maximum
205  // version of the higest supported TLS version in a future-proof way.
206  bool any_enabled = false;
207  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
208  // Only look at the versions already enabled.
209  if (min_version > kProtocolVersions[i].version) {
210  continue;
211  }
212  if (max_version < kProtocolVersions[i].version) {
213  break;
214  }
215 
216  if (!(options & kProtocolVersions[i].flag)) {
217  // The minimum version is the first enabled version.
218  if (!any_enabled) {
219  any_enabled = true;
220  min_version = kProtocolVersions[i].version;
221  }
222  continue;
223  }
224 
225  // If there is a disabled version after the first enabled one, all versions
226  // after it are implicitly disabled.
227  if (any_enabled) {
228  max_version = kProtocolVersions[i-1].version;
229  break;
230  }
231  }
232 
233  if (!any_enabled) {
235  return false;
236  }
237 
238  *out_min_version = min_version;
239  *out_max_version = max_version;
240  return true;
241 }
242 
243 static uint16_t ssl_version(const SSL *ssl) {
244  // In early data, we report the predicted version.
245  if (SSL_in_early_data(ssl) && !ssl->server) {
246  return ssl->s3->hs->early_session->ssl_version;
247  }
248  return ssl->version;
249 }
250 
252  assert(ssl->s3->have_version);
255  // |ssl->version| will always be set to a valid version.
256  assert(0);
257  return 0;
258  }
259 
260  return version;
261 }
262 
264  const SSL *const ssl = hs->ssl;
265  uint16_t protocol_version;
267  !ssl_protocol_version_from_wire(&protocol_version, version) ||
268  hs->min_version > protocol_version ||
269  protocol_version > hs->max_version) {
270  return false;
271  }
272 
273  return true;
274 }
275 
277  uint16_t extra_min_version) {
279  uint16_t protocol_version;
280  if (ssl_supports_version(hs, version) &&
281  ssl_protocol_version_from_wire(&protocol_version, version) &&
282  protocol_version >= extra_min_version && //
283  !CBB_add_u16(cbb, version)) {
284  return false;
285  }
286  }
287  return true;
288 }
289 
291  uint16_t *out_version, const CBS *peer_versions) {
293  if (!ssl_supports_version(hs, version)) {
294  continue;
295  }
296 
297  // JDK 11, prior to 11.0.2, has a buggy TLS 1.3 implementation which fails
298  // to send SNI when offering 1.3 sessions. Disable TLS 1.3 for such
299  // clients. We apply this logic here rather than |ssl_supports_version| so
300  // the downgrade signal continues to query the true capabilities. (The
301  // workaround is a limitation of the peer's capabilities rather than our
302  // own.)
303  //
304  // See https://bugs.openjdk.java.net/browse/JDK-8211806.
306  continue;
307  }
308 
309  CBS copy = *peer_versions;
310  while (CBS_len(&copy) != 0) {
311  uint16_t peer_version;
312  if (!CBS_get_u16(&copy, &peer_version)) {
314  *out_alert = SSL_AD_DECODE_ERROR;
315  return false;
316  }
317 
318  if (peer_version == version) {
319  *out_version = version;
320  return true;
321  }
322  }
323  }
324 
326  *out_alert = SSL_AD_PROTOCOL_VERSION;
327  return false;
328 }
329 
331 
332 using namespace bssl;
333 
335  return set_min_version(ctx->method, &ctx->conf_min_version, version);
336 }
337 
339  return set_max_version(ctx->method, &ctx->conf_max_version, version);
340 }
341 
343  return ctx->conf_min_version;
344 }
345 
347  return ctx->conf_max_version;
348 }
349 
351  if (!ssl->config) {
352  return 0;
353  }
354  return set_min_version(ssl->method, &ssl->config->conf_min_version, version);
355 }
356 
358  if (!ssl->config) {
359  return 0;
360  }
361  return set_max_version(ssl->method, &ssl->config->conf_max_version, version);
362 }
363 
365  if (!ssl->config) {
366  return 0;
367  }
368  return ssl->config->conf_min_version;
369 }
370 
372  if (!ssl->config) {
373  return 0;
374  }
375  return ssl->config->conf_max_version;
376 }
377 
378 int SSL_version(const SSL *ssl) {
379  return wire_version_to_api(ssl_version(ssl));
380 }
381 
382 const char *SSL_get_version(const SSL *ssl) {
383  return ssl_version_to_string(ssl_version(ssl));
384 }
385 
386 const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
387  return ssl_version_to_string(session->ssl_version);
388 }
389 
391  return wire_version_to_api(session->ssl_version);
392 }
393 
395  // This picks a representative TLS 1.3 version, but this API should only be
396  // used on unit test sessions anyway.
397  return api_version_to_wire(&session->ssl_version, version);
398 }
399 
401  return version == 0;
402 }
ssl_st::server
bool server
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3777
flag
uint32_t flag
Definition: ssl_versions.cc:162
SSL_set_max_proto_version
int SSL_set_max_proto_version(SSL *ssl, uint16_t version)
Definition: ssl_versions.cc:357
SSL_get_min_proto_version
uint16_t SSL_get_min_proto_version(const SSL *ssl)
Definition: ssl_versions.cc:364
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
SSL_OP_NO_TLSv1_2
#define SSL_OP_NO_TLSv1_2
Definition: ssl.h:720
CBS_get_u16
#define CBS_get_u16
Definition: boringssl_prefix_symbols.h:1073
ssl_negotiate_version
bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, uint16_t *out_version, const CBS *peer_versions)
Definition: ssl_versions.cc:290
cbs_st
Definition: bytestring.h:39
ctx
Definition: benchmark-async.c:30
SSL_OP_NO_TLSv1_1
#define SSL_OP_NO_TLSv1_1
Definition: ssl.h:721
api_version_to_wire
static bool api_version_to_wire(uint16_t *out, uint16_t version)
Definition: ssl_versions.cc:115
SSL_SESSION_get_protocol_version
uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session)
Definition: ssl_versions.cc:390
DTLS1_2_VERSION
#define DTLS1_2_VERSION
Definition: ssl.h:656
ssl_version
static uint16_t ssl_version(const SSL *ssl)
Definition: ssl_versions.cc:243
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
ssl_protocol_version_from_wire
BSSL_NAMESPACE_BEGIN bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version)
Definition: ssl_versions.cc:28
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
options
double_dict options[]
Definition: capstone_test.c:55
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:179
ssl_st::config
bssl::UniquePtr< bssl::SSL_CONFIG > config
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3712
SSL_HANDSHAKE::ssl
SSL * ssl
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1726
SSL_CTX_set_min_proto_version
int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version)
Definition: ssl_versions.cc:334
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
version
uint16_t version
Definition: ssl_versions.cc:161
bssl
Definition: hpke_test.cc:37
version
Definition: version.py:1
SSL_HANDSHAKE::min_version
uint16_t min_version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1745
SSL_R_UNSUPPORTED_PROTOCOL
#define SSL_R_UNSUPPORTED_PROTOCOL
Definition: ssl.h:5507
set_min_version
static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out, uint16_t version)
Definition: ssl_versions.cc:138
kTLSVersions
static const uint16_t kTLSVersions[]
Definition: ssl_versions.cc:54
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
kDTLSVersions
static const uint16_t kDTLSVersions[]
Definition: ssl_versions.cc:61
ssl_ctx_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3404
ssl_st::quic_method
const SSL_QUIC_METHOD * quic_method
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3769
SSL_get_version
const char * SSL_get_version(const SSL *ssl)
Definition: ssl_versions.cc:382
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
bytestring.h
SSL_CTX_set_record_protocol_version
int SSL_CTX_set_record_protocol_version(SSL_CTX *ctx, int version)
Definition: ssl_versions.cc:400
ssl_add_supported_versions
bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb, uint16_t extra_min_version)
Definition: ssl_versions.cc:276
CBB_add_u16
#define CBB_add_u16
Definition: boringssl_prefix_symbols.h:1027
ssl_session_st::ssl_version
uint16_t ssl_version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3795
ssl_protocol_version
uint16_t ssl_protocol_version(const SSL *ssl)
Definition: ssl_versions.cc:251
TLS1_1_VERSION
#define TLS1_1_VERSION
Definition: ssl.h:651
SSL_HANDSHAKE
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1720
get_method_versions
static Span< const uint16_t > get_method_versions(const SSL_PROTOCOL_METHOD *method)
Definition: ssl_versions.cc:66
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
set_max_version
static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out, uint16_t version)
Definition: ssl_versions.cc:149
SSL_R_NO_SUPPORTED_VERSIONS_ENABLED
#define SSL_R_NO_SUPPORTED_VERSIONS_ENABLED
Definition: ssl.h:5547
SSL_SESSION_get_version
const char * SSL_SESSION_get_version(const SSL_SESSION *session)
Definition: ssl_versions.cc:386
SSL_is_dtls
#define SSL_is_dtls
Definition: boringssl_prefix_symbols.h:402
SSL_R_UNKNOWN_SSL_VERSION
#define SSL_R_UNKNOWN_SSL_VERSION
Definition: ssl.h:5501
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
err.h
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
SSL_CONFIG::conf_min_version
uint16_t conf_min_version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2977
SSL_AD_PROTOCOL_VERSION
#define SSL_AD_PROTOCOL_VERSION
Definition: ssl.h:3813
SSL_PROTOCOL_METHOD
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2427
SSL_OP_NO_TLSv1_3
#define SSL_OP_NO_TLSv1_3
Definition: ssl.h:722
SSL_HANDSHAKE::max_version
uint16_t max_version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1749
SSL_set_min_proto_version
int SSL_set_min_proto_version(SSL *ssl, uint16_t version)
Definition: ssl_versions.cc:350
Span
Definition: boringssl-with-bazel/src/include/openssl/span.h:32
SSL_get_max_proto_version
uint16_t SSL_get_max_proto_version(const SSL *ssl)
Definition: ssl_versions.cc:371
ssl.h
ssl_get_version_range
bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version, uint16_t *out_max_version)
Definition: ssl_versions.cc:170
SSL_SESSION_set_protocol_version
int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version)
Definition: ssl_versions.cc:394
TLS1_3_VERSION
#define TLS1_3_VERSION
Definition: ssl.h:653
ssl_session_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3787
ssl_version_to_string
static const char * ssl_version_to_string(uint16_t version)
Definition: ssl_versions.cc:85
SSL_CTX_set_max_proto_version
int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version)
Definition: ssl_versions.cc:338
set_version_bound
static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out, uint16_t version)
Definition: ssl_versions.cc:126
TLS1_2_VERSION
#define TLS1_2_VERSION
Definition: ssl.h:652
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
ssl_st::s3
bssl::SSL3_STATE * s3
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3730
kProtocolVersions
const struct @377 kProtocolVersions[]
SSL_version
int SSL_version(const SSL *ssl)
Definition: ssl_versions.cc:378
SSL_HANDSHAKE::config
SSL_CONFIG * config
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1729
SSL_CTX_get_min_proto_version
uint16_t SSL_CTX_get_min_proto_version(const SSL_CTX *ctx)
Definition: ssl_versions.cc:342
TLS1_VERSION
#define TLS1_VERSION
Definition: ssl.h:650
SSL_AD_DECODE_ERROR
#define SSL_AD_DECODE_ERROR
Definition: ssl.h:3810
SSL_CTX_get_max_proto_version
uint16_t SSL_CTX_get_max_proto_version(const SSL_CTX *ctx)
Definition: ssl_versions.cc:346
SSL_CONFIG::conf_max_version
uint16_t conf_max_version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2972
SSL_OP_NO_DTLSv1
#define SSL_OP_NO_DTLSv1
Definition: ssl.h:723
ssl_st::method
const bssl::SSL_PROTOCOL_METHOD * method
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3706
ssl_supports_version
bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version)
Definition: ssl_versions.cc:263
ssl_st::options
uint32_t options
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3763
SSL_R_DECODE_ERROR
#define SSL_R_DECODE_ERROR
Definition: ssl.h:5405
SSL_in_early_data
#define SSL_in_early_data
Definition: boringssl_prefix_symbols.h:399
DTLS1_VERSION
#define DTLS1_VERSION
Definition: ssl.h:655
method
NSString * method
Definition: ProtoMethod.h:28
wire_version_to_api
static uint16_t wire_version_to_api(uint16_t version)
Definition: ssl_versions.cc:110
ssl_method_supports_version
bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, uint16_t version)
Definition: ssl_versions.cc:72
ssl_st::version
uint16_t version
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3715
SSL_OP_NO_TLSv1
#define SSL_OP_NO_TLSv1
Definition: ssl.h:719
SSL_HANDSHAKE::apply_jdk11_workaround
bool apply_jdk11_workaround
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2033
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
cbb_st
Definition: bytestring.h:375


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:21