blob: f7184b0d249d3a9b4d720dd1bcef335a2a5be41d [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
David Benjamin86e95b82017-07-18 16:34:25 -040015#define BORINGSSL_INTERNAL_CXX_TYPES
16
Steven Valdez8f36c512017-06-20 10:55:02 -040017#include <openssl/ssl.h>
18
19#include <assert.h>
20
21#include <openssl/bytestring.h>
22#include <openssl/err.h>
23
24#include "internal.h"
25#include "../crypto/internal.h"
26
27
David Benjamin86e95b82017-07-18 16:34:25 -040028namespace bssl {
29
Steven Valdez8f36c512017-06-20 10:55:02 -040030int ssl_protocol_version_from_wire(uint16_t *out, uint16_t version) {
31 switch (version) {
32 case SSL3_VERSION:
33 case TLS1_VERSION:
34 case TLS1_1_VERSION:
35 case TLS1_2_VERSION:
36 *out = version;
37 return 1;
38
39 case TLS1_3_DRAFT_VERSION:
Steven Valdez520e1222017-06-13 12:45:25 -040040 case TLS1_3_EXPERIMENT_VERSION:
Steven Valdezdbe01582017-07-14 10:39:28 -040041 case TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -040042 *out = TLS1_3_VERSION;
43 return 1;
44
45 case DTLS1_VERSION:
46 /* DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0. */
47 *out = TLS1_1_VERSION;
48 return 1;
49
50 case DTLS1_2_VERSION:
51 *out = TLS1_2_VERSION;
52 return 1;
53
54 default:
55 return 0;
56 }
57}
58
59/* The follow arrays are the supported versions for TLS and DTLS, in order of
60 * decreasing preference. */
61
62static const uint16_t kTLSVersions[] = {
Steven Valdez520e1222017-06-13 12:45:25 -040063 TLS1_3_EXPERIMENT_VERSION,
Steven Valdezdbe01582017-07-14 10:39:28 -040064 TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION,
65 TLS1_3_DRAFT_VERSION,
Steven Valdez8f36c512017-06-20 10:55:02 -040066 TLS1_2_VERSION,
67 TLS1_1_VERSION,
68 TLS1_VERSION,
69 SSL3_VERSION,
70};
71
72static const uint16_t kDTLSVersions[] = {
73 DTLS1_2_VERSION,
74 DTLS1_VERSION,
75};
76
77static void get_method_versions(const SSL_PROTOCOL_METHOD *method,
78 const uint16_t **out, size_t *out_num) {
79 if (method->is_dtls) {
80 *out = kDTLSVersions;
81 *out_num = OPENSSL_ARRAY_SIZE(kDTLSVersions);
82 } else {
83 *out = kTLSVersions;
84 *out_num = OPENSSL_ARRAY_SIZE(kTLSVersions);
85 }
86}
87
88static int method_supports_version(const SSL_PROTOCOL_METHOD *method,
89 uint16_t version) {
90 const uint16_t *versions;
91 size_t num_versions;
92 get_method_versions(method, &versions, &num_versions);
93 for (size_t i = 0; i < num_versions; i++) {
94 if (versions[i] == version) {
95 return 1;
96 }
97 }
98 return 0;
99}
100
101static int set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
102 uint16_t version) {
103 /* The public API uses wire versions, except we use |TLS1_3_VERSION|
104 * everywhere to refer to any draft TLS 1.3 versions. In this direction, we
105 * map it to some representative TLS 1.3 draft version. */
Steven Valdez520e1222017-06-13 12:45:25 -0400106 if (version == TLS1_3_DRAFT_VERSION ||
Steven Valdezdbe01582017-07-14 10:39:28 -0400107 version == TLS1_3_EXPERIMENT_VERSION ||
108 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) {
David Benjamin353577c2017-06-29 15:54:58 -0400109 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
110 return 0;
111 }
Steven Valdez8f36c512017-06-20 10:55:02 -0400112 if (version == TLS1_3_VERSION) {
113 version = TLS1_3_DRAFT_VERSION;
114 }
115
David Benjamin353577c2017-06-29 15:54:58 -0400116 if (!method_supports_version(method, version) ||
117 !ssl_protocol_version_from_wire(out, version)) {
118 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION);
119 return 0;
120 }
121
122 return 1;
Steven Valdez8f36c512017-06-20 10:55:02 -0400123}
124
125static int set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
126 uint16_t version) {
127 /* Zero is interpreted as the default minimum version. */
128 if (version == 0) {
129 /* SSL 3.0 is disabled by default and TLS 1.0 does not exist in DTLS. */
130 *out = method->is_dtls ? TLS1_1_VERSION : TLS1_VERSION;
131 return 1;
132 }
133
134 return set_version_bound(method, out, version);
135}
136
137static int set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out,
138 uint16_t version) {
139 /* Zero is interpreted as the default maximum version. */
140 if (version == 0) {
141 *out = TLS1_2_VERSION;
142 return 1;
143 }
144
145 return set_version_bound(method, out, version);
146}
147
Steven Valdez8f36c512017-06-20 10:55:02 -0400148const struct {
149 uint16_t version;
150 uint32_t flag;
151} kProtocolVersions[] = {
152 {SSL3_VERSION, SSL_OP_NO_SSLv3},
153 {TLS1_VERSION, SSL_OP_NO_TLSv1},
154 {TLS1_1_VERSION, SSL_OP_NO_TLSv1_1},
155 {TLS1_2_VERSION, SSL_OP_NO_TLSv1_2},
156 {TLS1_3_VERSION, SSL_OP_NO_TLSv1_3},
157};
158
159int ssl_get_version_range(const SSL *ssl, uint16_t *out_min_version,
160 uint16_t *out_max_version) {
161 /* For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but
162 * DTLS 1.0 should be mapped to TLS 1.1. */
163 uint32_t options = ssl->options;
164 if (SSL_is_dtls(ssl)) {
165 options &= ~SSL_OP_NO_TLSv1_1;
166 if (options & SSL_OP_NO_DTLSv1) {
167 options |= SSL_OP_NO_TLSv1_1;
168 }
169 }
170
171 uint16_t min_version = ssl->conf_min_version;
172 uint16_t max_version = ssl->conf_max_version;
173
174 /* OpenSSL's API for controlling versions entails blacklisting individual
175 * protocols. This has two problems. First, on the client, the protocol can
176 * only express a contiguous range of versions. Second, a library consumer
177 * trying to set a maximum version cannot disable protocol versions that get
178 * added in a future version of the library.
179 *
180 * To account for both of these, OpenSSL interprets the client-side bitmask
181 * as a min/max range by picking the lowest contiguous non-empty range of
182 * enabled protocols. Note that this means it is impossible to set a maximum
183 * version of the higest supported TLS version in a future-proof way. */
184 int any_enabled = 0;
185 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) {
186 /* Only look at the versions already enabled. */
187 if (min_version > kProtocolVersions[i].version) {
188 continue;
189 }
190 if (max_version < kProtocolVersions[i].version) {
191 break;
192 }
193
194 if (!(options & kProtocolVersions[i].flag)) {
195 /* The minimum version is the first enabled version. */
196 if (!any_enabled) {
197 any_enabled = 1;
198 min_version = kProtocolVersions[i].version;
199 }
200 continue;
201 }
202
203 /* If there is a disabled version after the first enabled one, all versions
204 * after it are implicitly disabled. */
205 if (any_enabled) {
206 max_version = kProtocolVersions[i-1].version;
207 break;
208 }
209 }
210
211 if (!any_enabled) {
212 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED);
213 return 0;
214 }
215
216 *out_min_version = min_version;
217 *out_max_version = max_version;
218 return 1;
219}
220
David Benjamind9cbb532017-07-07 13:17:19 -0400221static uint16_t ssl_version(const SSL *ssl) {
222 /* In early data, we report the predicted version. */
223 if (SSL_in_early_data(ssl) && !ssl->server) {
224 return ssl->s3->hs->early_session->ssl_version;
225 }
226 return ssl->version;
227}
228
David Benjamin86e95b82017-07-18 16:34:25 -0400229static const char *ssl_version_to_string(uint16_t version) {
Steven Valdez8f36c512017-06-20 10:55:02 -0400230 switch (version) {
231 /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
232 case TLS1_3_DRAFT_VERSION:
Steven Valdez520e1222017-06-13 12:45:25 -0400233 case TLS1_3_EXPERIMENT_VERSION:
Steven Valdezdbe01582017-07-14 10:39:28 -0400234 case TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION:
Steven Valdez8f36c512017-06-20 10:55:02 -0400235 return "TLSv1.3";
236
237 case TLS1_2_VERSION:
238 return "TLSv1.2";
239
240 case TLS1_1_VERSION:
241 return "TLSv1.1";
242
243 case TLS1_VERSION:
244 return "TLSv1";
245
246 case SSL3_VERSION:
247 return "SSLv3";
248
249 case DTLS1_VERSION:
250 return "DTLSv1";
251
252 case DTLS1_2_VERSION:
253 return "DTLSv1.2";
254
255 default:
256 return "unknown";
257 }
258}
259
Steven Valdez8f36c512017-06-20 10:55:02 -0400260uint16_t ssl3_protocol_version(const SSL *ssl) {
261 assert(ssl->s3->have_version);
262 uint16_t version;
263 if (!ssl_protocol_version_from_wire(&version, ssl->version)) {
264 /* |ssl->version| will always be set to a valid version. */
265 assert(0);
266 return 0;
267 }
268
269 return version;
270}
271
272int ssl_supports_version(SSL_HANDSHAKE *hs, uint16_t version) {
Steven Valdez520e1222017-06-13 12:45:25 -0400273 SSL *const ssl = hs->ssl;
274 /* As a client, only allow the configured TLS 1.3 variant. As a server,
275 * support all TLS 1.3 variants as long as tls13_variant is set to a
276 * non-default value. */
277 if (ssl->server) {
Steven Valdez52586f92017-07-11 15:08:32 -0400278 if (ssl->tls13_variant == tls13_default &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400279 (version == TLS1_3_EXPERIMENT_VERSION ||
280 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION)) {
Steven Valdez520e1222017-06-13 12:45:25 -0400281 return 0;
282 }
283 } else {
Steven Valdez52586f92017-07-11 15:08:32 -0400284 if ((ssl->tls13_variant != tls13_experiment &&
Steven Valdez0e4a4482017-07-17 11:12:34 -0400285 ssl->tls13_variant != tls13_no_session_id_experiment &&
Steven Valdez520e1222017-06-13 12:45:25 -0400286 version == TLS1_3_EXPERIMENT_VERSION) ||
Steven Valdezdbe01582017-07-14 10:39:28 -0400287 (ssl->tls13_variant != tls13_record_type_experiment &&
288 version == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) ||
Steven Valdez52586f92017-07-11 15:08:32 -0400289 (ssl->tls13_variant != tls13_default &&
Steven Valdez520e1222017-06-13 12:45:25 -0400290 version == TLS1_3_DRAFT_VERSION)) {
291 return 0;
292 }
293 }
294
Steven Valdez8f36c512017-06-20 10:55:02 -0400295 uint16_t protocol_version;
Steven Valdez520e1222017-06-13 12:45:25 -0400296 return method_supports_version(ssl->method, version) &&
Steven Valdez8f36c512017-06-20 10:55:02 -0400297 ssl_protocol_version_from_wire(&protocol_version, version) &&
298 hs->min_version <= protocol_version &&
299 protocol_version <= hs->max_version;
300}
301
302int ssl_add_supported_versions(SSL_HANDSHAKE *hs, CBB *cbb) {
303 const uint16_t *versions;
304 size_t num_versions;
305 get_method_versions(hs->ssl->method, &versions, &num_versions);
306 for (size_t i = 0; i < num_versions; i++) {
307 if (ssl_supports_version(hs, versions[i]) &&
308 !CBB_add_u16(cbb, versions[i])) {
309 return 0;
310 }
311 }
312 return 1;
313}
314
315int ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert,
316 uint16_t *out_version, const CBS *peer_versions) {
317 const uint16_t *versions;
318 size_t num_versions;
319 get_method_versions(hs->ssl->method, &versions, &num_versions);
320 for (size_t i = 0; i < num_versions; i++) {
321 if (!ssl_supports_version(hs, versions[i])) {
322 continue;
323 }
324
325 CBS copy = *peer_versions;
326 while (CBS_len(&copy) != 0) {
327 uint16_t version;
328 if (!CBS_get_u16(&copy, &version)) {
329 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
330 *out_alert = SSL_AD_DECODE_ERROR;
331 return 0;
332 }
333
334 if (version == versions[i]) {
335 *out_version = version;
336 return 1;
337 }
338 }
339 }
340
341 OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL);
342 *out_alert = SSL_AD_PROTOCOL_VERSION;
343 return 0;
344}
David Benjamin86e95b82017-07-18 16:34:25 -0400345
346} // namespace bssl
347
348using namespace bssl;
349
350int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) {
351 return set_min_version(ctx->method, &ctx->conf_min_version, version);
352}
353
354int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) {
355 return set_max_version(ctx->method, &ctx->conf_max_version, version);
356}
357
358int SSL_set_min_proto_version(SSL *ssl, uint16_t version) {
359 return set_min_version(ssl->method, &ssl->conf_min_version, version);
360}
361
362int SSL_set_max_proto_version(SSL *ssl, uint16_t version) {
363 return set_max_version(ssl->method, &ssl->conf_max_version, version);
364}
365
366int SSL_version(const SSL *ssl) {
367 uint16_t ret = ssl_version(ssl);
368 /* Report TLS 1.3 draft version as TLS 1.3 in the public API. */
369 if (ret == TLS1_3_DRAFT_VERSION || ret == TLS1_3_EXPERIMENT_VERSION ||
370 ret == TLS1_3_RECORD_TYPE_EXPERIMENT_VERSION) {
371 return TLS1_3_VERSION;
372 }
373 return ret;
374}
375
376const char *SSL_get_version(const SSL *ssl) {
377 return ssl_version_to_string(ssl_version(ssl));
378}
379
380const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
381 return ssl_version_to_string(session->ssl_version);
382}