blob: 964f7c93ef319d8207a6f03a828f3a091d08cc89 [file] [log] [blame]
Steven Valdez8f36c512017-06-20 10:55:02 -04001/* 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
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070026BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040027
David Benjamined9aed12017-09-27 19:24:09 -040028bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version) {
Steven Valdez8f36c512017-06-20 10:55:02 -040029 switch (version) {
Steven Valdez8f36c512017-06-20 10:55:02 -040030 case TLS1_VERSION:
31 case TLS1_1_VERSION:
32 case TLS1_2_VERSION:
Steven Valdezd4514532018-08-13 10:07:45 -040033 case TLS1_3_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -040034 *out = version;
David Benjamined9aed12017-09-27 19:24:09 -040035 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040036
Steven Valdez8f36c512017-06-20 10:55:02 -040037 case DTLS1_VERSION:
David Benjaminc11ea9422017-08-29 16:33:21 -040038 // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
Steven Valdez8f36c512017-06-20 10:55:02 -040039 *out = TLS1_1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040040 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040041
42 case DTLS1_2_VERSION:
43 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040044 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040045
46 default:
David Benjamined9aed12017-09-27 19:24:09 -040047 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040048 }
49}
50
David Benjaminc11ea9422017-08-29 16:33:21 -040051// The follow arrays are the supported versions for TLS and DTLS, in order of
52// decreasing preference.
Steven Valdez8f36c512017-06-20 10:55:02 -040053
54static const uint16_t kTLSVersions[] = {
Steven Valdezd4514532018-08-13 10:07:45 -040055 TLS1_3_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040056 TLS1_2_VERSION,
57 TLS1_1_VERSION,
58 TLS1_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040059};
60
61static const uint16_t kDTLSVersions[] = {
62 DTLS1_2_VERSION,
63 DTLS1_VERSION,
64};
65
David Benjamin95dd54e2019-08-22 16:43:15 -040066static Span<const uint16_t> get_method_versions(
67 const SSL_PROTOCOL_METHOD *method) {
68 return method->is_dtls ? Span<const uint16_t>(kDTLSVersions)
69 : Span<const uint16_t>(kTLSVersions);
Steven Valdez8f36c512017-06-20 10:55:02 -040070}
71
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -070072bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
73 uint16_t version) {
David Benjamin95dd54e2019-08-22 16:43:15 -040074 for (uint16_t supported : get_method_versions(method)) {
75 if (supported == version) {
David Benjamined9aed12017-09-27 19:24:09 -040076 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040077 }
78 }
David Benjamined9aed12017-09-27 19:24:09 -040079 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040080}
81
David Benjamina4bafd32017-10-03 15:06:29 -040082// The following functions map between API versions and wire versions. The
Steven Valdezb84674b2018-08-28 10:14:07 -040083// public API works on wire versions.
David Benjamina4bafd32017-10-03 15:06:29 -040084
85static const char *ssl_version_to_string(uint16_t version) {
86 switch (version) {
Steven Valdezd4514532018-08-13 10:07:45 -040087 case TLS1_3_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -040088 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
David Benjamina4bafd32017-10-03 15:06:29 -040099 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
110static uint16_t wire_version_to_api(uint16_t version) {
Steven Valdezb84674b2018-08-28 10:14:07 -0400111 return version;
David Benjamina4bafd32017-10-03 15:06:29 -0400112}
113
Steven Valdezb84674b2018-08-28 10:14:07 -0400114// api_version_to_wire maps |version| to some representative wire version.
David Benjamina4bafd32017-10-03 15:06:29 -0400115static bool api_version_to_wire(uint16_t *out, uint16_t version) {
David Benjamina4bafd32017-10-03 15:06:29 -0400116 // 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
126static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
127 uint16_t version) {
128 if (!api_version_to_wire(&version, version) ||
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700129 !ssl_method_supports_version(method, version)) {
David Benjamin353577c2017-06-29 15:54:58 -0400130 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamined9aed12017-09-27 19:24:09 -0400131 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400132 }
133
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700134 *out = version;
David Benjamined9aed12017-09-27 19:24:09 -0400135 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400136}
137
David Benjamined9aed12017-09-27 19:24:09 -0400138static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
139 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400140 // Zero is interpreted as the default minimum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400141 if (version == 0) {
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700142 *out = method->is_dtls ? DTLS1_VERSION : TLS1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400143 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400144 }
145
146 return set_version_bound(method, out, version);
147}
148
David Benjamined9aed12017-09-27 19:24:09 -0400149static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
150 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400151 // Zero is interpreted as the default maximum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400152 if (version == 0) {
Matthew Braithwaite58d56f42019-11-04 13:20:01 -0800153 *out = method->is_dtls ? DTLS1_2_VERSION : TLS1_3_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400154 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400155 }
156
157 return set_version_bound(method, out, version);
158}
159
Steven Valdez8f36c512017-06-20 10:55:02 -0400160const struct {
161 uint16_t version;
162 uint32_t flag;
163} kProtocolVersions[] = {
Steven Valdez8f36c512017-06-20 10:55:02 -0400164 {TLS1_VERSION, SSL_OP_NO_TLSv1},
165 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
166 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
167 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
168};
169
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700170bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400171 uint16_t *out_max_version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400172 // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
173 // DTLS 1.0 should be mapped to TLS 1.1.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700174 uint32_t options = hs->ssl->options;
175 if (SSL_is_dtls(hs->ssl)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400176 options &= ~SSL_OP_NO_TLSv1_1;
177 if (options & SSL_OP_NO_DTLSv1) {
178 options |= SSL_OP_NO_TLSv1_1;
179 }
180 }
181
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700182 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)) {
187 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
188 return false;
189 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400190
Steven Valdezc8e0f902018-07-14 11:23:01 -0400191 // QUIC requires TLS 1.3.
Alessandro Ghedini3cbb0292018-12-13 13:53:57 +0000192 if (hs->ssl->quic_method && min_version < TLS1_3_VERSION) {
Steven Valdezc8e0f902018-07-14 11:23:01 -0400193 min_version = TLS1_3_VERSION;
194 }
195
David Benjamin72b095d2020-06-11 14:41:28 -0400196 // 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.
David Benjaminc11ea9422017-08-29 16:33:21 -0400201 //
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.
David Benjamined9aed12017-09-27 19:24:09 -0400206 bool any_enabled = false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400207 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400208 // Only look at the versions already enabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400209 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)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400217 // The minimum version is the first enabled version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400218 if (!any_enabled) {
David Benjamined9aed12017-09-27 19:24:09 -0400219 any_enabled = true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400220 min_version = kProtocolVersions[i].version;
221 }
222 continue;
223 }
224
David Benjaminc11ea9422017-08-29 16:33:21 -0400225 // If there is a disabled version after the first enabled one, all versions
226 // after it are implicitly disabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400227 if (any_enabled) {
228 max_version = kProtocolVersions[i-1].version;
229 break;
230 }
231 }
232
233 if (!any_enabled) {
234 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
David Benjamined9aed12017-09-27 19:24:09 -0400235 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400236 }
237
238 *out_min_version = min_version;
239 *out_max_version = max_version;
David Benjamined9aed12017-09-27 19:24:09 -0400240 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400241}
242
David Benjamind9cbb532017-07-07 13:17:19 -0400243static uint16_t ssl_version(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400244 // In early data, we report the predicted version.
David Benjamind9cbb532017-07-07 13:17:19 -0400245 if (SSL_in_early_data(ssl) && !ssl->server) {
246 return ssl->s3->hs->early_session->ssl_version;
247 }
248 return ssl->version;
249}
250
David Benjamind1e3ce12017-10-06 18:31:15 -0400251uint16_t ssl_protocol_version(const SSL *ssl) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400252 assert(ssl->s3->have_version);
253 uint16_t version;
254 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400255 // |ssl->version| will always be set to a valid version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400256 assert(0);
257 return 0;
258 }
259
260 return version;
261}
262
David Benjamin14e51ad2021-05-19 15:24:34 -0400263bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version) {
264 const SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400265 uint16_t protocol_version;
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700266 if (!ssl_method_supports_version(ssl->method, version) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400267 !ssl_protocol_version_from_wire(&protocol_version, version) ||
268 hs->min_version > protocol_version ||
269 protocol_version > hs->max_version) {
270 return false;
Steven Valdez520e1222017-06-13 12:45:25 -0400271 }
272
Steven Valdez56c4ed92018-05-10 11:04:53 -0400273 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400274}
275
David Benjamin83a49932021-05-20 15:57:09 -0400276bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb,
277 uint16_t extra_min_version) {
David Benjamin95dd54e2019-08-22 16:43:15 -0400278 for (uint16_t version : get_method_versions(hs->ssl->method)) {
David Benjamin83a49932021-05-20 15:57:09 -0400279 uint16_t protocol_version;
David Benjamin95dd54e2019-08-22 16:43:15 -0400280 if (ssl_supports_version(hs, version) &&
David Benjamin83a49932021-05-20 15:57:09 -0400281 ssl_protocol_version_from_wire(&protocol_version, version) &&
282 protocol_version >= extra_min_version && //
David Benjamin95dd54e2019-08-22 16:43:15 -0400283 !CBB_add_u16(cbb, version)) {
David Benjamined9aed12017-09-27 19:24:09 -0400284 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400285 }
286 }
David Benjamined9aed12017-09-27 19:24:09 -0400287 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400288}
289
David Benjamined9aed12017-09-27 19:24:09 -0400290bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
291 uint16_t *out_version, const CBS *peer_versions) {
David Benjamin95dd54e2019-08-22 16:43:15 -0400292 for (uint16_t version : get_method_versions(hs->ssl->method)) {
293 if (!ssl_supports_version(hs, version)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400294 continue;
295 }
296
David Benjamin6965d252018-11-19 15:49:56 -0600297 // 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.
David Benjamin95dd54e2019-08-22 16:43:15 -0400305 if (version == TLS1_3_VERSION && hs->apply_jdk11_workaround) {
David Benjamin6965d252018-11-19 15:49:56 -0600306 continue;
307 }
308
Steven Valdez8f36c512017-06-20 10:55:02 -0400309 CBS copy = *peer_versions;
310 while (CBS_len(&copy) != 0) {
David Benjamin95dd54e2019-08-22 16:43:15 -0400311 uint16_t peer_version;
312 if (!CBS_get_u16(&copy, &peer_version)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400313 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
314 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamined9aed12017-09-27 19:24:09 -0400315 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400316 }
317
David Benjamin95dd54e2019-08-22 16:43:15 -0400318 if (peer_version == version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400319 *out_version = version;
David Benjamined9aed12017-09-27 19:24:09 -0400320 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400321 }
322 }
323 }
324
325 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
326 *out_alert = SSL_AD_PROTOCOL_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400327 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400328}
David Benjamin86e95b82017-07-18 16:34:25 -0400329
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700330BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400331
332using namespace bssl;
333
334int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
335 return set_min_version(ctx->method, &ctx->conf_min_version, version);
336}
337
338int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
339 return set_max_version(ctx->method, &ctx->conf_max_version, version);
340}
341
Alessandro Ghedini12049fd2019-10-10 17:25:50 +0100342uint16_t SSL_CTX_get_min_proto_version(const SSL_CTX *ctx) {
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700343 return ctx->conf_min_version;
344}
345
Alessandro Ghedini12049fd2019-10-10 17:25:50 +0100346uint16_t SSL_CTX_get_max_proto_version(const SSL_CTX *ctx) {
Nitish Sakhawalkara4af5f82019-03-25 17:26:59 -0700347 return ctx->conf_max_version;
348}
349
David Benjamin86e95b82017-07-18 16:34:25 -0400350int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700351 if (!ssl->config) {
352 return 0;
353 }
354 return set_min_version(ssl->method, &ssl->config->conf_min_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400355}
356
357int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700358 if (!ssl->config) {
359 return 0;
360 }
361 return set_max_version(ssl->method, &ssl->config->conf_max_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400362}
363
Alessandro Ghedini12049fd2019-10-10 17:25:50 +0100364uint16_t SSL_get_min_proto_version(const SSL *ssl) {
365 if (!ssl->config) {
366 return 0;
367 }
368 return ssl->config->conf_min_version;
369}
370
371uint16_t SSL_get_max_proto_version(const SSL *ssl) {
372 if (!ssl->config) {
373 return 0;
374 }
375 return ssl->config->conf_max_version;
376}
377
David Benjamin86e95b82017-07-18 16:34:25 -0400378int SSL_version(const SSL *ssl) {
David Benjamina4bafd32017-10-03 15:06:29 -0400379 return wire_version_to_api(ssl_version(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -0400380}
381
382const char *SSL_get_version(const SSL *ssl) {
383 return ssl_version_to_string(ssl_version(ssl));
384}
385
386const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
387 return ssl_version_to_string(session->ssl_version);
388}
David Benjamina4bafd32017-10-03 15:06:29 -0400389
390uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
391 return wire_version_to_api(session->ssl_version);
392}
393
394int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
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}
Adam Langleyc2827d32021-10-12 13:18:18 -0700399
400int SSL_CTX_set_record_protocol_version(SSL_CTX *ctx, int version) {
401 return version == 0;
402}