blob: a5a7cd9700e6e20ef9119f2cc950e8518f377b9d [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:
33 *out = version;
David Benjamined9aed12017-09-27 19:24:09 -040034 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040035
Steven Valdez74666da2018-01-09 06:18:36 -050036 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -040037 case TLS1_3_DRAFT28_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -040038 *out = TLS1_3_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040039 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040040
41 case DTLS1_VERSION:
David Benjaminc11ea9422017-08-29 16:33:21 -040042 // DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0.
Steven Valdez8f36c512017-06-20 10:55:02 -040043 *out = TLS1_1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040044 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040045
46 case DTLS1_2_VERSION:
47 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -040048 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040049
50 default:
David Benjamined9aed12017-09-27 19:24:09 -040051 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040052 }
53}
54
David Benjaminc11ea9422017-08-29 16:33:21 -040055// The follow arrays are the supported versions for TLS and DTLS, in order of
56// decreasing preference.
Steven Valdez8f36c512017-06-20 10:55:02 -040057
58static const uint16_t kTLSVersions[] = {
Steven Valdez861f3842018-03-27 13:15:26 -040059 TLS1_3_DRAFT28_VERSION,
David Benjaminfec83fc2018-06-27 13:42:03 -040060 TLS1_3_DRAFT23_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040061 TLS1_2_VERSION,
62 TLS1_1_VERSION,
63 TLS1_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040064};
65
66static const uint16_t kDTLSVersions[] = {
67 DTLS1_2_VERSION,
68 DTLS1_VERSION,
69};
70
71static void get_method_versions(const SSL_PROTOCOL_METHOD *method,
72 const uint16_t **out, size_t *out_num) {
73 if (method->is_dtls) {
74 *out = kDTLSVersions;
75 *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions);
76 } else {
77 *out = kTLSVersions;
78 *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions);
79 }
80}
81
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -070082bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method,
83 uint16_t version) {
Steven Valdez8f36c512017-06-20 10:55:02 -040084 const uint16_t *versions;
85 size_t num_versions;
86 get_method_versions(method, &versions, &num_versions);
87 for (size_t i = 0; i < num_versions; i++) {
88 if (versions[i] == version) {
David Benjamined9aed12017-09-27 19:24:09 -040089 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -040090 }
91 }
David Benjamined9aed12017-09-27 19:24:09 -040092 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -040093}
94
David Benjamina4bafd32017-10-03 15:06:29 -040095// The following functions map between API versions and wire versions. The
96// public API works on wire versions, except that TLS 1.3 draft versions all
97// appear as TLS 1.3. This will get collapsed back down when TLS 1.3 is
98// finalized.
99
100static const char *ssl_version_to_string(uint16_t version) {
101 switch (version) {
Steven Valdez74666da2018-01-09 06:18:36 -0500102 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -0400103 case TLS1_3_DRAFT28_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400104 return "TLSv1.3";
105
106 case TLS1_2_VERSION:
107 return "TLSv1.2";
108
109 case TLS1_1_VERSION:
110 return "TLSv1.1";
111
112 case TLS1_VERSION:
113 return "TLSv1";
114
David Benjamina4bafd32017-10-03 15:06:29 -0400115 case DTLS1_VERSION:
116 return "DTLSv1";
117
118 case DTLS1_2_VERSION:
119 return "DTLSv1.2";
120
121 default:
122 return "unknown";
123 }
124}
125
126static uint16_t wire_version_to_api(uint16_t version) {
127 switch (version) {
128 // Report TLS 1.3 draft versions as TLS 1.3 in the public API.
Steven Valdez74666da2018-01-09 06:18:36 -0500129 case TLS1_3_DRAFT23_VERSION:
Steven Valdez861f3842018-03-27 13:15:26 -0400130 case TLS1_3_DRAFT28_VERSION:
David Benjamina4bafd32017-10-03 15:06:29 -0400131 return TLS1_3_VERSION;
132 default:
133 return version;
134 }
135}
136
137// api_version_to_wire maps |version| to some representative wire version. In
138// particular, it picks an arbitrary TLS 1.3 representative. This should only be
139// used in context where that does not matter.
140static bool api_version_to_wire(uint16_t *out, uint16_t version) {
Steven Valdez861f3842018-03-27 13:15:26 -0400141 if (version == TLS1_3_DRAFT23_VERSION ||
142 version == TLS1_3_DRAFT28_VERSION) {
David Benjamined9aed12017-09-27 19:24:09 -0400143 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400144 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400145 if (version == TLS1_3_VERSION) {
Steven Valdez7e5dd252018-01-22 15:20:31 -0500146 version = TLS1_3_DRAFT23_VERSION;
Steven Valdez8f36c512017-06-20 10:55:02 -0400147 }
148
David Benjamina4bafd32017-10-03 15:06:29 -0400149 // Check it is a real protocol version.
150 uint16_t unused;
151 if (!ssl_protocol_version_from_wire(&unused, version)) {
152 return false;
153 }
154
155 *out = version;
156 return true;
157}
158
159static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
160 uint16_t version) {
161 if (!api_version_to_wire(&version, version) ||
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700162 !ssl_method_supports_version(method, version) ||
David Benjamin353577c2017-06-29 15:54:58 -0400163 !ssl_protocol_version_from_wire(out, version)) {
164 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
David Benjamined9aed12017-09-27 19:24:09 -0400165 return false;
David Benjamin353577c2017-06-29 15:54:58 -0400166 }
167
David Benjamined9aed12017-09-27 19:24:09 -0400168 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400169}
170
David Benjamined9aed12017-09-27 19:24:09 -0400171static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
172 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400173 // Zero is interpreted as the default minimum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400174 if (version == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400175 // SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS.
Steven Valdez8f36c512017-06-20 10:55:02 -0400176 *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400177 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400178 }
179
180 return set_version_bound(method, out, version);
181}
182
David Benjamined9aed12017-09-27 19:24:09 -0400183static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
184 uint16_t version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400185 // Zero is interpreted as the default maximum version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400186 if (version == 0) {
187 *out = TLS1_2_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400188 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400189 }
190
191 return set_version_bound(method, out, version);
192}
193
Steven Valdez8f36c512017-06-20 10:55:02 -0400194const struct {
195 uint16_t version;
196 uint32_t flag;
197} kProtocolVersions[] = {
Steven Valdez8f36c512017-06-20 10:55:02 -0400198 {TLS1_VERSION, SSL_OP_NO_TLSv1},
199 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
200 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
201 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
202};
203
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700204bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version,
David Benjamined9aed12017-09-27 19:24:09 -0400205 uint16_t *out_max_version) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400206 // For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
207 // DTLS 1.0 should be mapped to TLS 1.1.
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700208 uint32_t options = hs->ssl->options;
209 if (SSL_is_dtls(hs->ssl)) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400210 options &= ~SSL_OP_NO_TLSv1_1;
211 if (options & SSL_OP_NO_DTLSv1) {
212 options |= SSL_OP_NO_TLSv1_1;
213 }
214 }
215
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700216 uint16_t min_version = hs->config->conf_min_version;
217 uint16_t max_version = hs->config->conf_max_version;
Steven Valdez8f36c512017-06-20 10:55:02 -0400218
David Benjaminc11ea9422017-08-29 16:33:21 -0400219 // OpenSSL's API for controlling versions entails blacklisting individual
220 // protocols. This has two problems. First, on the client, the protocol can
221 // only express a contiguous range of versions. Second, a library consumer
222 // trying to set a maximum version cannot disable protocol versions that get
223 // added in a future version of the library.
224 //
225 // To account for both of these, OpenSSL interprets the client-side bitmask
226 // as a min/max range by picking the lowest contiguous non-empty range of
227 // enabled protocols. Note that this means it is impossible to set a maximum
228 // version of the higest supported TLS version in a future-proof way.
David Benjamined9aed12017-09-27 19:24:09 -0400229 bool any_enabled = false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400230 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400231 // Only look at the versions already enabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400232 if (min_version > kProtocolVersions[i].version) {
233 continue;
234 }
235 if (max_version < kProtocolVersions[i].version) {
236 break;
237 }
238
239 if (!(options & kProtocolVersions[i].flag)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400240 // The minimum version is the first enabled version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400241 if (!any_enabled) {
David Benjamined9aed12017-09-27 19:24:09 -0400242 any_enabled = true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400243 min_version = kProtocolVersions[i].version;
244 }
245 continue;
246 }
247
David Benjaminc11ea9422017-08-29 16:33:21 -0400248 // If there is a disabled version after the first enabled one, all versions
249 // after it are implicitly disabled.
Steven Valdez8f36c512017-06-20 10:55:02 -0400250 if (any_enabled) {
251 max_version = kProtocolVersions[i-1].version;
252 break;
253 }
254 }
255
256 if (!any_enabled) {
257 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
David Benjamined9aed12017-09-27 19:24:09 -0400258 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400259 }
260
261 *out_min_version = min_version;
262 *out_max_version = max_version;
David Benjamined9aed12017-09-27 19:24:09 -0400263 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400264}
265
David Benjamind9cbb532017-07-07 13:17:19 -0400266static uint16_t ssl_version(const SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400267 // In early data, we report the predicted version.
David Benjamind9cbb532017-07-07 13:17:19 -0400268 if (SSL_in_early_data(ssl) && !ssl->server) {
269 return ssl->s3->hs->early_session->ssl_version;
270 }
271 return ssl->version;
272}
273
David Benjamind1e3ce12017-10-06 18:31:15 -0400274uint16_t ssl_protocol_version(const SSL *ssl) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400275 assert(ssl->s3->have_version);
276 uint16_t version;
277 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400278 // |ssl->version| will always be set to a valid version.
Steven Valdez8f36c512017-06-20 10:55:02 -0400279 assert(0);
280 return 0;
281 }
282
283 return version;
284}
285
David Benjamined9aed12017-09-27 19:24:09 -0400286bool ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
Steven Valdez520e1222017-06-13 12:45:25 -0400287 SSL *const ssl = hs->ssl;
Steven Valdezcd8470f2017-10-11 12:29:36 -0400288 uint16_t protocol_version;
Matthew Braithwaite0e9e0ba2018-04-10 15:40:12 -0700289 if (!ssl_method_supports_version(ssl->method, version) ||
Steven Valdezcd8470f2017-10-11 12:29:36 -0400290 !ssl_protocol_version_from_wire(&protocol_version, version) ||
291 hs->min_version > protocol_version ||
292 protocol_version > hs->max_version) {
293 return false;
Steven Valdez520e1222017-06-13 12:45:25 -0400294 }
295
Steven Valdez56c4ed92018-05-10 11:04:53 -0400296 // If the TLS 1.3 variant is set to |tls13_default|, all variants are enabled,
297 // otherwise only the matching version is enabled.
298 if (protocol_version == TLS1_3_VERSION) {
299 switch (ssl->tls13_variant) {
300 case tls13_draft23:
301 return version == TLS1_3_DRAFT23_VERSION;
302 case tls13_draft28:
303 return version == TLS1_3_DRAFT28_VERSION;
304 case tls13_default:
305 return true;
306 }
Steven Valdezcd8470f2017-10-11 12:29:36 -0400307 }
308
Steven Valdez56c4ed92018-05-10 11:04:53 -0400309 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400310}
311
David Benjamined9aed12017-09-27 19:24:09 -0400312bool ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400313 const uint16_t *versions;
314 size_t num_versions;
315 get_method_versions(hs->ssl->method, &versions, &num_versions);
316 for (size_t i = 0; i < num_versions; i++) {
317 if (ssl_supports_version(hs, versions[i]) &&
318 !CBB_add_u16(cbb, versions[i])) {
David Benjamined9aed12017-09-27 19:24:09 -0400319 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400320 }
321 }
David Benjamined9aed12017-09-27 19:24:09 -0400322 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400323}
324
David Benjamined9aed12017-09-27 19:24:09 -0400325bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
326 uint16_t *out_version, const CBS *peer_versions) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400327 const uint16_t *versions;
328 size_t num_versions;
329 get_method_versions(hs->ssl->method, &versions, &num_versions);
330 for (size_t i = 0; i < num_versions; i++) {
331 if (!ssl_supports_version(hs, versions[i])) {
332 continue;
333 }
334
335 CBS copy = *peer_versions;
336 while (CBS_len(&copy) != 0) {
337 uint16_t version;
338 if (!CBS_get_u16(&copy, &version)) {
339 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
340 *out_alert = SSL_AD_DECODE_ERROR;
David Benjamined9aed12017-09-27 19:24:09 -0400341 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400342 }
343
344 if (version == versions[i]) {
345 *out_version = version;
David Benjamined9aed12017-09-27 19:24:09 -0400346 return true;
Steven Valdez8f36c512017-06-20 10:55:02 -0400347 }
348 }
349 }
350
351 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
352 *out_alert = SSL_AD_PROTOCOL_VERSION;
David Benjamined9aed12017-09-27 19:24:09 -0400353 return false;
Steven Valdez8f36c512017-06-20 10:55:02 -0400354}
David Benjamin86e95b82017-07-18 16:34:25 -0400355
Steven Valdez861f3842018-03-27 13:15:26 -0400356bool ssl_is_draft28(uint16_t version) {
357 return version == TLS1_3_DRAFT28_VERSION;
358}
359
David Benjamin86e95b82017-07-18 16:34:25 -0400360} // namespace bssl
361
362using namespace bssl;
363
364int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
365 return set_min_version(ctx->method, &ctx->conf_min_version, version);
366}
367
368int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
369 return set_max_version(ctx->method, &ctx->conf_max_version, version);
370}
371
372int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700373 if (!ssl->config) {
374 return 0;
375 }
376 return set_min_version(ssl->method, &ssl->config->conf_min_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400377}
378
379int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700380 if (!ssl->config) {
381 return 0;
382 }
383 return set_max_version(ssl->method, &ssl->config->conf_max_version, version);
David Benjamin86e95b82017-07-18 16:34:25 -0400384}
385
386int SSL_version(const SSL *ssl) {
David Benjamina4bafd32017-10-03 15:06:29 -0400387 return wire_version_to_api(ssl_version(ssl));
David Benjamin86e95b82017-07-18 16:34:25 -0400388}
389
390const char *SSL_get_version(const SSL *ssl) {
391 return ssl_version_to_string(ssl_version(ssl));
392}
393
394const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
395 return ssl_version_to_string(session->ssl_version);
396}
David Benjamina4bafd32017-10-03 15:06:29 -0400397
398uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) {
399 return wire_version_to_api(session->ssl_version);
400}
401
402int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) {
403 // This picks a representative TLS 1.3 version, but this API should only be
404 // used on unit test sessions anyway.
405 return api_version_to_wire(&session->ssl_version, version);
406}