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