blob: 6fd19c65726662af5c92b0fe3a249b2b383be541 [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
26int ssl_protocol_version_from_wire(uint16_t *out, uint16_t version) {
27 switch (version) {
28 case SSL3_VERSION:
29 case TLS1_VERSION:
30 case TLS1_1_VERSION:
31 case TLS1_2_VERSION:
32 *out = version;
33 return 1;
34
35 case TLS1_3_DRAFT_VERSION:
Steven Valdez520e1222017-06-13 12:45:25 -040036 case TLS1_3_EXPERIMENT_VERSION:
Steven Valdezdbe01582017-07-14 10:39:28 -040037 case TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -040038 *out = TLS1_3_VERSION;
39 return 1;
40
41 case DTLS1_VERSION:
42 /* DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0. */
43 *out = TLS1_1_VERSION;
44 return 1;
45
46 case DTLS1_2_VERSION:
47 *out = TLS1_2_VERSION;
48 return 1;
49
50 default:
51 return 0;
52 }
53}
54
55/* The follow arrays are the supported versions for TLS and DTLS, in order of
56 * decreasing preference. */
57
58static const uint16_t kTLSVersions[] = {
Steven Valdez520e1222017-06-13 12:45:25 -040059 TLS1_3_EXPERIMENT_VERSION,
Steven Valdezdbe01582017-07-14 10:39:28 -040060 TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION,
61 TLS1_3_DRAFT_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
84static int method_supports_version(const SSL_PROTOCOL_METHOD *method,
85 uint16_t version) {
86 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) {
91 return 1;
92 }
93 }
94 return 0;
95}
96
97static int set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
98 uint16_t version) {
99 /* The public API uses wire versions, except we use |TLS1_3_VERSION|
100 * everywhere to refer to any draft TLS 1.3 versions. In this direction, we
101 * map it to some representative TLS 1.3 draft version. */
Steven Valdez520e1222017-06-13 12:45:25 -0400102 if (version == TLS1_3_DRAFT_VERSION ||
Steven Valdezdbe01582017-07-14 10:39:28 -0400103 version == TLS1_3_EXPERIMENT_VERSION ||
104 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) {
David Benjamin353577c2017-06-29 15:54:58 -0400105 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
106 return 0;
107 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400108 if (version == TLS1_3_VERSION) {
109 version = TLS1_3_DRAFT_VERSION;
110 }
111
David Benjamin353577c2017-06-29 15:54:58 -0400112 if (!method_supports_version(method, version) ||
113 !ssl_protocol_version_from_wire(out, version)) {
114 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
115 return 0;
116 }
117
118 return 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400119}
120
121static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
122 uint16_t version) {
123 /* Zero is interpreted as the default minimum version. */
124 if (version == 0) {
125 /* SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS. */
126 *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
127 return 1;
128 }
129
130 return set_version_bound(method, out, version);
131}
132
133static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
134 uint16_t version) {
135 /* Zero is interpreted as the default maximum version. */
136 if (version == 0) {
137 *out = TLS1_2_VERSION;
138 return 1;
139 }
140
141 return set_version_bound(method, out, version);
142}
143
144int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
145 return set_min_version(ctx->method, &ctx->conf_min_version, version);
146}
147
148int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
149 return set_max_version(ctx->method, &ctx->conf_max_version, version);
150}
151
152int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
153 return set_min_version(ssl->method, &ssl->conf_min_version, version);
154}
155
156int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
157 return set_max_version(ssl->method, &ssl->conf_max_version, version);
158}
159
160const struct {
161 uint16_t version;
162 uint32_t flag;
163} kProtocolVersions[] = {
164 {SSL3_VERSION, SSL_OP_NO_SSLv3},
165 {TLS1_VERSION, SSL_OP_NO_TLSv1},
166 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
167 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
168 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
169};
170
171int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
172 uint16_t *out_max_version) {
173 /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
174 * DTLS 1.0 should be mapped to TLS 1.1. */
175 uint32_t options = ssl->options;
176 if (SSL_is_dtls(ssl)) {
177 options &= ~SSL_OP_NO_TLSv1_1;
178 if (options & SSL_OP_NO_DTLSv1) {
179 options |= SSL_OP_NO_TLSv1_1;
180 }
181 }
182
183 uint16_t min_version = ssl->conf_min_version;
184 uint16_t max_version = ssl->conf_max_version;
185
186 /* OpenSSL's API for controlling versions entails blacklisting individual
187 * protocols. This has two problems. First, on the client, the protocol can
188 * only express a contiguous range of versions. Second, a library consumer
189 * trying to set a maximum version cannot disable protocol versions that get
190 * added in a future version of the library.
191 *
192 * To account for both of these, OpenSSL interprets the client-side bitmask
193 * as a min/max range by picking the lowest contiguous non-empty range of
194 * enabled protocols. Note that this means it is impossible to set a maximum
195 * version of the higest supported TLS version in a future-proof way. */
196 int any_enabled = 0;
197 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
198 /* Only look at the versions already enabled. */
199 if (min_version > kProtocolVersions[i].version) {
200 continue;
201 }
202 if (max_version < kProtocolVersions[i].version) {
203 break;
204 }
205
206 if (!(options & kProtocolVersions[i].flag)) {
207 /* The minimum version is the first enabled version. */
208 if (!any_enabled) {
209 any_enabled = 1;
210 min_version = kProtocolVersions[i].version;
211 }
212 continue;
213 }
214
215 /* If there is a disabled version after the first enabled one, all versions
216 * after it are implicitly disabled. */
217 if (any_enabled) {
218 max_version = kProtocolVersions[i-1].version;
219 break;
220 }
221 }
222
223 if (!any_enabled) {
224 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
225 return 0;
226 }
227
228 *out_min_version = min_version;
229 *out_max_version = max_version;
230 return 1;
231}
232
David Benjamind9cbb532017-07-07 13:17:19 -0400233static uint16_t ssl_version(const SSL *ssl) {
234 /* In early data, we report the predicted version. */
235 if (SSL_in_early_data(ssl) && !ssl->server) {
236 return ssl->s3->hs->early_session->ssl_version;
237 }
238 return ssl->version;
239}
240
Steven Valdez8f36c512017-06-20 10:55:02 -0400241int SSL_version(const SSL *ssl) {
David Benjamind9cbb532017-07-07 13:17:19 -0400242 uint16_t ret = ssl_version(ssl);
Steven Valdez8f36c512017-06-20 10:55:02 -0400243 /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
Steven Valdezdbe01582017-07-14 10:39:28 -0400244 if (ret == TLS1_3_DRAFT_VERSION || ret == TLS1_3_EXPERIMENT_VERSION ||
245 ret == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400246 return TLS1_3_VERSION;
247 }
David Benjamind9cbb532017-07-07 13:17:19 -0400248 return ret;
Steven Valdez8f36c512017-06-20 10:55:02 -0400249}
250
251static const char *ssl_get_version(int version) {
252 switch (version) {
253 /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
254 case TLS1_3_DRAFT_VERSION:
Steven Valdez520e1222017-06-13 12:45:25 -0400255 case TLS1_3_EXPERIMENT_VERSION:
Steven Valdezdbe01582017-07-14 10:39:28 -0400256 case TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -0400257 return "TLSv1.3";
258
259 case TLS1_2_VERSION:
260 return "TLSv1.2";
261
262 case TLS1_1_VERSION:
263 return "TLSv1.1";
264
265 case TLS1_VERSION:
266 return "TLSv1";
267
268 case SSL3_VERSION:
269 return "SSLv3";
270
271 case DTLS1_VERSION:
272 return "DTLSv1";
273
274 case DTLS1_2_VERSION:
275 return "DTLSv1.2";
276
277 default:
278 return "unknown";
279 }
280}
281
282const char *SSL_get_version(const SSL *ssl) {
David Benjamind9cbb532017-07-07 13:17:19 -0400283 return ssl_get_version(ssl_version(ssl));
Steven Valdez8f36c512017-06-20 10:55:02 -0400284}
285
286const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
287 return ssl_get_version(session->ssl_version);
288}
289
290uint16_t ssl3_protocol_version(const SSL *ssl) {
291 assert(ssl->s3->have_version);
292 uint16_t version;
293 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
294 /* |ssl->version| will always be set to a valid version. */
295 assert(0);
296 return 0;
297 }
298
299 return version;
300}
301
302int ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
Steven Valdez520e1222017-06-13 12:45:25 -0400303 SSL *const ssl = hs->ssl;
304 /* As a client, only allow the configured TLS 1.3 variant. As a server,
305 * support all TLS 1.3 variants as long as tls13_variant is set to a
306 * non-default value. */
307 if (ssl->server) {
Steven Valdez52586f92017-07-11 15:08:32 -0400308 if (ssl->tls13_variant == tls13_default &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400309 (version == TLS1_3_EXPERIMENT_VERSION ||
310 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION)) {
Steven Valdez520e1222017-06-13 12:45:25 -0400311 return 0;
312 }
313 } else {
Steven Valdez52586f92017-07-11 15:08:32 -0400314 if ((ssl->tls13_variant != tls13_experiment &&
Steven Valdez0e4a4482017-07-17 11:12:34 -0400315 ssl->tls13_variant != tls13_no_session_id_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400316 version == TLS1_3_EXPERIMENT_VERSION) ||
Steven Valdezdbe01582017-07-14 10:39:28 -0400317 (ssl->tls13_variant != tls13_record_type_experiment &&
318 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) ||
Steven Valdez52586f92017-07-11 15:08:32 -0400319 (ssl->tls13_variant != tls13_default &&
Steven Valdez520e1222017-06-13 12:45:25 -0400320 version == TLS1_3_DRAFT_VERSION)) {
321 return 0;
322 }
323 }
324
Steven Valdez8f36c512017-06-20 10:55:02 -0400325 uint16_t protocol_version;
Steven Valdez520e1222017-06-13 12:45:25 -0400326 return method_supports_version(ssl->method, version) &&
Steven Valdez8f36c512017-06-20 10:55:02 -0400327 ssl_protocol_version_from_wire(&protocol_version, version) &&
328 hs->min_version <= protocol_version &&
329 protocol_version <= hs->max_version;
330}
331
332int ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
333 const uint16_t *versions;
334 size_t num_versions;
335 get_method_versions(hs->ssl->method, &versions, &num_versions);
336 for (size_t i = 0; i < num_versions; i++) {
337 if (ssl_supports_version(hs, versions[i]) &&
338 !CBB_add_u16(cbb, versions[i])) {
339 return 0;
340 }
341 }
342 return 1;
343}
344
345int ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
346 uint16_t *out_version, const CBS *peer_versions) {
347 const uint16_t *versions;
348 size_t num_versions;
349 get_method_versions(hs->ssl->method, &versions, &num_versions);
350 for (size_t i = 0; i < num_versions; i++) {
351 if (!ssl_supports_version(hs, versions[i])) {
352 continue;
353 }
354
355 CBS copy = *peer_versions;
356 while (CBS_len(&copy) != 0) {
357 uint16_t version;
358 if (!CBS_get_u16(&copy, &version)) {
359 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
360 *out_alert = SSL_AD_DECODE_ERROR;
361 return 0;
362 }
363
364 if (version == versions[i]) {
365 *out_version = version;
366 return 1;
367 }
368 }
369 }
370
371 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
372 *out_alert = SSL_AD_PROTOCOL_VERSION;
373 return 0;
374}