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