blob: a09efa19e29c978a47fa042f05a87f747d4bbf84 [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
David Benjamin86e95b82017-07-18 16:34:25 -040026namespace bssl {
27
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) {
30 case SSL3_VERSION:
31 case TLS1_VERSION:
32 case TLS1_1_VERSION:
33 case TLS1_2_VERSION:
34 *out = version;
David Benjamined9aed12017-09-27 19:24:09 -040035 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040036
Steven Valdez74666da2018-01-09 06:18:36 -050037 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -040038 case TLS1_3_DRAFT28_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -040039 *out = TLS1_3_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040040 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040041
42 case DTLS1_VERSION:
David Benjaminc11ea9422017-08-29 16:33:21 -040043 // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
Steven Valdez8f36c512017-06-20 10:55:02 -040044 *out = TLS1_1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040045 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040046
47 case DTLS1_2_VERSION:
48 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040049 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040050
51 default:
David Benjamined9aed12017-09-27 19:24:09 -040052 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040053 }
54}
55
David Benjaminc11ea9422017-08-29 16:33:21 -040056// The follow arrays are the supported versions for TLS and DTLS, in order of
57// decreasing preference.
Steven Valdez8f36c512017-06-20 10:55:02 -040058
59static const uint16_t kTLSVersions[] = {
Steven Valdez74666da2018-01-09 06:18:36 -050060 TLS1_3_DRAFT23_VERSION,
Steven Valdez861f3842018-03-27 13:15:26 -040061 TLS1_3_DRAFT28_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040062 TLS1_2_VERSION,
63 TLS1_1_VERSION,
64 TLS1_VERSION,
65 SSL3_VERSION,
66};
67
68static const uint16_t kDTLSVersions[] = {
69 DTLS1_2_VERSION,
70 DTLS1_VERSION,
71};
72
73static void get_method_versions(const SSL_PROTOCOL_METHOD *method,
74 const uint16_t **out, size_t *out_num) {
75 if (method->is_dtls) {
76 *out = kDTLSVersions;
77 *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions);
78 } else {
79 *out = kTLSVersions;
80 *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions);
81 }
82}
83
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -070084bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
85 uint16_t version) {
Steven Valdez8f36c512017-06-20 10:55:02 -040086 const uint16_t *versions;
87 size_t num_versions;
88 get_method_versions(method, &versions, &num_versions);
89 for (size_t i = 0; i < num_versions; i++) {
90 if (versions[i] == version) {
David Benjamined9aed12017-09-27 19:24:09 -040091 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040092 }
93 }
David Benjamined9aed12017-09-27 19:24:09 -040094 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040095}
96
David Benjamina4bafd32017-10-03 15:06:29 -040097// The following functions map between API versions and wire versions. The
98// public API works on wire versions, except that TLS 1.3 draft versions all
99// appear as TLS 1.3. This will get collapsed back down when TLS 1.3 is
100// finalized.
101
102static const char *ssl_version_to_string(uint16_t version) {
103 switch (version) {
Steven Valdez74666da2018-01-09 06:18:36 -0500104 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -0400105 case TLS1_3_DRAFT28_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400106 return "TLSv1.3";
107
108 case TLS1_2_VERSION:
109 return "TLSv1.2";
110
111 case TLS1_1_VERSION:
112 return "TLSv1.1";
113
114 case TLS1_VERSION:
115 return "TLSv1";
116
117 case SSL3_VERSION:
118 return "SSLv3";
119
120 case DTLS1_VERSION:
121 return "DTLSv1";
122
123 case DTLS1_2_VERSION:
124 return "DTLSv1.2";
125
126 default:
127 return "unknown";
128 }
129}
130
131static uint16_t wire_version_to_api(uint16_t version) {
132 switch (version) {
133 // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
Steven Valdez74666da2018-01-09 06:18:36 -0500134 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -0400135 case TLS1_3_DRAFT28_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400136 return TLS1_3_VERSION;
137 default:
138 return version;
139 }
140}
141
142// api_version_to_wire maps |version| to some representative wire version. In
143// particular, it picks an arbitrary TLS 1.3 representative. This should only be
144// used in context where that does not matter.
145static bool api_version_to_wire(uint16_t *out, uint16_t version) {
Steven Valdez861f3842018-03-27 13:15:26 -0400146 if (version == TLS1_3_DRAFT23_VERSION ||
147 version == TLS1_3_DRAFT28_VERSION) {
David Benjamined9aed12017-09-27 19:24:09 -0400148 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400149 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400150 if (version == TLS1_3_VERSION) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500151 version = TLS1_3_DRAFT23_VERSION;
Steven Valdez8f36c512017-06-20 10:55:02 -0400152 }
153
David Benjamina4bafd32017-10-03 15:06:29 -0400154 // Check it is a real protocol version.
155 uint16_t unused;
156 if (!ssl_protocol_version_from_wire(&unused, version)) {
157 return false;
158 }
159
160 *out = version;
161 return true;
162}
163
164static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
165 uint16_t version) {
166 if (!api_version_to_wire(&version, version) ||
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700167 !ssl_method_supports_version(method, version) ||
David Benjamin353577c2017-06-29 15:54:58 -0400168 !ssl_protocol_version_from_wire(out, version)) {
169 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamined9aed12017-09-27 19:24:09 -0400170 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400171 }
172
David Benjamined9aed12017-09-27 19:24:09 -0400173 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400174}
175
David Benjamined9aed12017-09-27 19:24:09 -0400176static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
177 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400178 // Zero is interpreted as the default minimum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400179 if (version == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400180 // SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS.
Steven Valdez8f36c512017-06-20 10:55:02 -0400181 *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400182 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400183 }
184
185 return set_version_bound(method, out, version);
186}
187
David Benjamined9aed12017-09-27 19:24:09 -0400188static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
189 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400190 // Zero is interpreted as the default maximum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400191 if (version == 0) {
192 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400193 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400194 }
195
196 return set_version_bound(method, out, version);
197}
198
Steven Valdez8f36c512017-06-20 10:55:02 -0400199const struct {
200 uint16_t version;
201 uint32_t flag;
202} kProtocolVersions[] = {
203 {SSL3_VERSION, SSL_OP_NO_SSLv3},
204 {TLS1_VERSION, SSL_OP_NO_TLSv1},
205 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
206 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
207 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
208};
209
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700210bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400211 uint16_t *out_max_version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400212 // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
213 // DTLS 1.0 should be mapped to TLS 1.1.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700214 uint32_t options = hs->ssl->options;
215 if (SSL_is_dtls(hs->ssl)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400216 options &= ~SSL_OP_NO_TLSv1_1;
217 if (options & SSL_OP_NO_DTLSv1) {
218 options |= SSL_OP_NO_TLSv1_1;
219 }
220 }
221
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700222 uint16_t min_version = hs->config->conf_min_version;
223 uint16_t max_version = hs->config->conf_max_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400224
David Benjaminc11ea9422017-08-29 16:33:21 -0400225 // OpenSSL's API for controlling versions entails blacklisting individual
226 // protocols. This has two problems. First, on the client, the protocol can
227 // only express a contiguous range of versions. Second, a library consumer
228 // trying to set a maximum version cannot disable protocol versions that get
229 // added in a future version of the library.
230 //
231 // To account for both of these, OpenSSL interprets the client-side bitmask
232 // as a min/max range by picking the lowest contiguous non-empty range of
233 // enabled protocols. Note that this means it is impossible to set a maximum
234 // version of the higest supported TLS version in a future-proof way.
David Benjamined9aed12017-09-27 19:24:09 -0400235 bool any_enabled = false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400236 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400237 // Only look at the versions already enabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400238 if (min_version > kProtocolVersions[i].version) {
239 continue;
240 }
241 if (max_version < kProtocolVersions[i].version) {
242 break;
243 }
244
245 if (!(options & kProtocolVersions[i].flag)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400246 // The minimum version is the first enabled version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400247 if (!any_enabled) {
David Benjamined9aed12017-09-27 19:24:09 -0400248 any_enabled = true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400249 min_version = kProtocolVersions[i].version;
250 }
251 continue;
252 }
253
David Benjaminc11ea9422017-08-29 16:33:21 -0400254 // If there is a disabled version after the first enabled one, all versions
255 // after it are implicitly disabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400256 if (any_enabled) {
257 max_version = kProtocolVersions[i-1].version;
258 break;
259 }
260 }
261
262 if (!any_enabled) {
263 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
David Benjamined9aed12017-09-27 19:24:09 -0400264 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400265 }
266
267 *out_min_version = min_version;
268 *out_max_version = max_version;
David Benjamined9aed12017-09-27 19:24:09 -0400269 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400270}
271
David Benjamind9cbb532017-07-07 13:17:19 -0400272static uint16_t ssl_version(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400273 // In early data, we report the predicted version.
David Benjamind9cbb532017-07-07 13:17:19 -0400274 if (SSL_in_early_data(ssl) && !ssl->server) {
275 return ssl->s3->hs->early_session->ssl_version;
276 }
277 return ssl->version;
278}
279
David Benjamind1e3ce12017-10-06 18:31:15 -0400280uint16_t ssl_protocol_version(const SSL *ssl) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400281 assert(ssl->s3->have_version);
282 uint16_t version;
283 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400284 // |ssl->version| will always be set to a valid version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400285 assert(0);
286 return 0;
287 }
288
289 return version;
290}
291
David Benjamined9aed12017-09-27 19:24:09 -0400292bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
Steven Valdez520e1222017-06-13 12:45:25 -0400293 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400294 uint16_t protocol_version;
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700295 if (!ssl_method_supports_version(ssl->method, version) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400296 !ssl_protocol_version_from_wire(&protocol_version, version) ||
297 hs->min_version > protocol_version ||
298 protocol_version > hs->max_version) {
299 return false;
Steven Valdez520e1222017-06-13 12:45:25 -0400300 }
301
Steven Valdez7e5dd252018-01-22 15:20:31 -0500302 // This logic is part of the TLS 1.3 variants mechanism used in TLS 1.3
Steven Valdez861f3842018-03-27 13:15:26 -0400303 // experimentation. TLS 1.3 variants must match the enabled |tls13_variant|.
Steven Valdezcd8470f2017-10-11 12:29:36 -0400304 if (protocol_version != TLS1_3_VERSION ||
Steven Valdez861f3842018-03-27 13:15:26 -0400305 (ssl->tls13_variant == tls13_draft28 &&
306 version == TLS1_3_DRAFT28_VERSION) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400307 (ssl->tls13_variant == tls13_default &&
Steven Valdez74666da2018-01-09 06:18:36 -0500308 version == TLS1_3_DRAFT23_VERSION)) {
Steven Valdezcd8470f2017-10-11 12:29:36 -0400309 return true;
310 }
311
Steven Valdezcd8470f2017-10-11 12:29:36 -0400312 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400313}
314
David Benjamined9aed12017-09-27 19:24:09 -0400315bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400316 const uint16_t *versions;
317 size_t num_versions;
318 get_method_versions(hs->ssl->method, &versions, &num_versions);
319 for (size_t i = 0; i < num_versions; i++) {
320 if (ssl_supports_version(hs, versions[i]) &&
321 !CBB_add_u16(cbb, versions[i])) {
David Benjamined9aed12017-09-27 19:24:09 -0400322 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400323 }
324 }
David Benjamined9aed12017-09-27 19:24:09 -0400325 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400326}
327
David Benjamined9aed12017-09-27 19:24:09 -0400328bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
329 uint16_t *out_version, const CBS *peer_versions) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400330 const uint16_t *versions;
331 size_t num_versions;
332 get_method_versions(hs->ssl->method, &versions, &num_versions);
333 for (size_t i = 0; i < num_versions; i++) {
334 if (!ssl_supports_version(hs, versions[i])) {
335 continue;
336 }
337
338 CBS copy = *peer_versions;
339 while (CBS_len(&copy) != 0) {
340 uint16_t version;
341 if (!CBS_get_u16(&copy, &version)) {
342 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
343 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamined9aed12017-09-27 19:24:09 -0400344 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400345 }
346
347 if (version == versions[i]) {
348 *out_version = version;
David Benjamined9aed12017-09-27 19:24:09 -0400349 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400350 }
351 }
352 }
353
354 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
355 *out_alert = SSL_AD_PROTOCOL_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400356 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400357}
David Benjamin86e95b82017-07-18 16:34:25 -0400358
Steven Valdez861f3842018-03-27 13:15:26 -0400359bool ssl_is_draft28(uint16_t version) {
360 return version == TLS1_3_DRAFT28_VERSION;
361}
362
David Benjamin86e95b82017-07-18 16:34:25 -0400363} // namespace bssl
364
365using namespace bssl;
366
367int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
368 return set_min_version(ctx->method, &ctx->conf_min_version, version);
369}
370
371int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
372 return set_max_version(ctx->method, &ctx->conf_max_version, version);
373}
374
375int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700376 if (!ssl->config) {
377 return 0;
378 }
379 return set_min_version(ssl->method, &ssl->config->conf_min_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400380}
381
382int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700383 if (!ssl->config) {
384 return 0;
385 }
386 return set_max_version(ssl->method, &ssl->config->conf_max_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400387}
388
389int SSL_version(const SSL *ssl) {
David Benjamina4bafd32017-10-03 15:06:29 -0400390 return wire_version_to_api(ssl_version(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -0400391}
392
393const char *SSL_get_version(const SSL *ssl) {
394 return ssl_version_to_string(ssl_version(ssl));
395}
396
397const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
398 return ssl_version_to_string(session->ssl_version);
399}
David Benjamina4bafd32017-10-03 15:06:29 -0400400
401uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
402 return wire_version_to_api(session->ssl_version);
403}
404
405int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
406 // This picks a representative TLS 1.3 version, but this API should only be
407 // used on unit test sessions anyway.
408 return api_version_to_wire(&session->ssl_version, version);
409}