blob: 6f07b937cb5d02cf25b57725602b56aad65e725c [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) {
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 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 Valdezd4514532018-08-13 10:07:45 -040060 TLS1_3_VERSION,
Steven Valdez861f3842018-03-27 13:15:26 -040061 TLS1_3_DRAFT28_VERSION,
David Benjaminfec83fc2018-06-27 13:42:03 -040062 TLS1_3_DRAFT23_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040063 TLS1_2_VERSION,
64 TLS1_1_VERSION,
65 TLS1_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040066};
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:
Steven Valdezd4514532018-08-13 10:07:45 -0400106 case TLS1_3_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400107 return "TLSv1.3";
108
109 case TLS1_2_VERSION:
110 return "TLSv1.2";
111
112 case TLS1_1_VERSION:
113 return "TLSv1.1";
114
115 case TLS1_VERSION:
116 return "TLSv1";
117
David Benjamina4bafd32017-10-03 15:06:29 -0400118 case DTLS1_VERSION:
119 return "DTLSv1";
120
121 case DTLS1_2_VERSION:
122 return "DTLSv1.2";
123
124 default:
125 return "unknown";
126 }
127}
128
129static uint16_t wire_version_to_api(uint16_t version) {
130 switch (version) {
131 // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
Steven Valdez74666da2018-01-09 06:18:36 -0500132 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -0400133 case TLS1_3_DRAFT28_VERSION:
Steven Valdezd4514532018-08-13 10:07:45 -0400134 case TLS1_3_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400135 return TLS1_3_VERSION;
136 default:
137 return version;
138 }
139}
140
141// api_version_to_wire maps |version| to some representative wire version. In
142// particular, it picks an arbitrary TLS 1.3 representative. This should only be
143// used in context where that does not matter.
144static bool api_version_to_wire(uint16_t *out, uint16_t version) {
Steven Valdez861f3842018-03-27 13:15:26 -0400145 if (version == TLS1_3_DRAFT23_VERSION ||
146 version == TLS1_3_DRAFT28_VERSION) {
David Benjamined9aed12017-09-27 19:24:09 -0400147 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400148 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400149
David Benjamina4bafd32017-10-03 15:06:29 -0400150 // Check it is a real protocol version.
151 uint16_t unused;
152 if (!ssl_protocol_version_from_wire(&unused, version)) {
153 return false;
154 }
155
156 *out = version;
157 return true;
158}
159
160static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
161 uint16_t version) {
162 if (!api_version_to_wire(&version, version) ||
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700163 !ssl_method_supports_version(method, version) ||
David Benjamin353577c2017-06-29 15:54:58 -0400164 !ssl_protocol_version_from_wire(out, version)) {
165 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamined9aed12017-09-27 19:24:09 -0400166 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400167 }
168
David Benjamined9aed12017-09-27 19:24:09 -0400169 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400170}
171
David Benjamined9aed12017-09-27 19:24:09 -0400172static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
173 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400174 // Zero is interpreted as the default minimum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400175 if (version == 0) {
David Benjaminc59b9aa2018-07-16 21:34:03 -0400176 // TLS 1.0 does not exist in DTLS.
Steven Valdez8f36c512017-06-20 10:55:02 -0400177 *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400178 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400179 }
180
181 return set_version_bound(method, out, version);
182}
183
David Benjamined9aed12017-09-27 19:24:09 -0400184static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
185 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400186 // Zero is interpreted as the default maximum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400187 if (version == 0) {
188 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400189 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400190 }
191
192 return set_version_bound(method, out, version);
193}
194
Steven Valdez8f36c512017-06-20 10:55:02 -0400195const struct {
196 uint16_t version;
197 uint32_t flag;
198} kProtocolVersions[] = {
Steven Valdez8f36c512017-06-20 10:55:02 -0400199 {TLS1_VERSION, SSL_OP_NO_TLSv1},
200 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
201 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
202 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
203};
204
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700205bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400206 uint16_t *out_max_version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
208 // DTLS 1.0 should be mapped to TLS 1.1.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700209 uint32_t options = hs->ssl->options;
210 if (SSL_is_dtls(hs->ssl)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400211 options &= ~SSL_OP_NO_TLSv1_1;
212 if (options & SSL_OP_NO_DTLSv1) {
213 options |= SSL_OP_NO_TLSv1_1;
214 }
215 }
216
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700217 uint16_t min_version = hs->config->conf_min_version;
218 uint16_t max_version = hs->config->conf_max_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400219
David Benjaminc11ea9422017-08-29 16:33:21 -0400220 // OpenSSL's API for controlling versions entails blacklisting individual
221 // protocols. This has two problems. First, on the client, the protocol can
222 // only express a contiguous range of versions. Second, a library consumer
223 // trying to set a maximum version cannot disable protocol versions that get
224 // added in a future version of the library.
225 //
226 // To account for both of these, OpenSSL interprets the client-side bitmask
227 // as a min/max range by picking the lowest contiguous non-empty range of
228 // enabled protocols. Note that this means it is impossible to set a maximum
229 // version of the higest supported TLS version in a future-proof way.
David Benjamined9aed12017-09-27 19:24:09 -0400230 bool any_enabled = false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400231 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400232 // Only look at the versions already enabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400233 if (min_version > kProtocolVersions[i].version) {
234 continue;
235 }
236 if (max_version < kProtocolVersions[i].version) {
237 break;
238 }
239
240 if (!(options & kProtocolVersions[i].flag)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400241 // The minimum version is the first enabled version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400242 if (!any_enabled) {
David Benjamined9aed12017-09-27 19:24:09 -0400243 any_enabled = true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400244 min_version = kProtocolVersions[i].version;
245 }
246 continue;
247 }
248
David Benjaminc11ea9422017-08-29 16:33:21 -0400249 // If there is a disabled version after the first enabled one, all versions
250 // after it are implicitly disabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400251 if (any_enabled) {
252 max_version = kProtocolVersions[i-1].version;
253 break;
254 }
255 }
256
257 if (!any_enabled) {
258 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
David Benjamined9aed12017-09-27 19:24:09 -0400259 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400260 }
261
262 *out_min_version = min_version;
263 *out_max_version = max_version;
David Benjamined9aed12017-09-27 19:24:09 -0400264 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400265}
266
David Benjamind9cbb532017-07-07 13:17:19 -0400267static uint16_t ssl_version(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400268 // In early data, we report the predicted version.
David Benjamind9cbb532017-07-07 13:17:19 -0400269 if (SSL_in_early_data(ssl) && !ssl->server) {
270 return ssl->s3->hs->early_session->ssl_version;
271 }
272 return ssl->version;
273}
274
David Benjamind1e3ce12017-10-06 18:31:15 -0400275uint16_t ssl_protocol_version(const SSL *ssl) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400276 assert(ssl->s3->have_version);
277 uint16_t version;
278 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400279 // |ssl->version| will always be set to a valid version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400280 assert(0);
281 return 0;
282 }
283
284 return version;
285}
286
David Benjamined9aed12017-09-27 19:24:09 -0400287bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
Steven Valdez520e1222017-06-13 12:45:25 -0400288 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400289 uint16_t protocol_version;
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700290 if (!ssl_method_supports_version(ssl->method, version) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400291 !ssl_protocol_version_from_wire(&protocol_version, version) ||
292 hs->min_version > protocol_version ||
293 protocol_version > hs->max_version) {
294 return false;
Steven Valdez520e1222017-06-13 12:45:25 -0400295 }
296
Steven Valdez56c4ed92018-05-10 11:04:53 -0400297 // If the TLS 1.3 variant is set to |tls13_default|, all variants are enabled,
298 // otherwise only the matching version is enabled.
299 if (protocol_version == TLS1_3_VERSION) {
300 switch (ssl->tls13_variant) {
301 case tls13_draft23:
302 return version == TLS1_3_DRAFT23_VERSION;
303 case tls13_draft28:
304 return version == TLS1_3_DRAFT28_VERSION;
Steven Valdezd4514532018-08-13 10:07:45 -0400305 case tls13_rfc:
306 return version == TLS1_3_VERSION;
Steven Valdez56c4ed92018-05-10 11:04:53 -0400307 case tls13_default:
308 return true;
309 }
Steven Valdezcd8470f2017-10-11 12:29:36 -0400310 }
311
Steven Valdez56c4ed92018-05-10 11:04:53 -0400312 return true;
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) {
Steven Valdezd4514532018-08-13 10:07:45 -0400360 return version == TLS1_3_DRAFT28_VERSION || version == TLS1_3_VERSION;
Steven Valdez861f3842018-03-27 13:15:26 -0400361}
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}