| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
|
| 8 |
#include <algorithm> |
| 9 |
|
| 10 |
#include <openssl/bytestring.h> |
| 11 |
#include <openssl/err.h> |
| 12 |
#include <openssl/span.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
#include "../crypto/internal.h" |
| 16 |
|
| 17 |
|
| 18 |
BSSL_NAMESPACE_BEGIN |
| 19 |
|
| 20 |
bool ssl_protocol_version_from_wire(uint16_t *out, uint16_t version) { |
| 21 |
switch (version) { |
| 22 |
case TLS1_VERSION: |
| 23 |
case TLS1_1_VERSION: |
| 24 |
case TLS1_2_VERSION: |
| 25 |
case TLS1_3_VERSION: |
| 26 |
*out = version; |
| 27 |
return true; |
| 28 |
|
| 29 |
case DTLS1_VERSION: |
| 30 |
// DTLS 1.0 is analogous to TLS 1.1, not TLS 1.0. |
| 31 |
*out = TLS1_1_VERSION; |
| 32 |
return true; |
| 33 |
|
| 34 |
case DTLS1_2_VERSION: |
| 35 |
*out = TLS1_2_VERSION; |
| 36 |
return true; |
| 37 |
|
| 38 |
default: |
| 39 |
return false; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// The follow arrays are the supported versions for TLS and DTLS, in order of |
| 44 |
// decreasing preference. |
| 45 |
|
| 46 |
static const uint16_t kTLSVersions[] = { |
| 47 |
TLS1_3_VERSION, |
| 48 |
TLS1_2_VERSION, |
| 49 |
TLS1_1_VERSION, |
| 50 |
TLS1_VERSION, |
| 51 |
}; |
| 52 |
|
| 53 |
static const uint16_t kDTLSVersions[] = { |
| 54 |
DTLS1_2_VERSION, |
| 55 |
DTLS1_VERSION, |
| 56 |
}; |
| 57 |
|
| 58 |
static Span<const uint16_t> get_method_versions( |
| 59 |
const SSL_PROTOCOL_METHOD *method) { |
| 60 |
return method->is_dtls ? Span<const uint16_t>(kDTLSVersions) |
| 61 |
: Span<const uint16_t>(kTLSVersions); |
| 62 |
} |
| 63 |
|
| 64 |
bool ssl_method_supports_version(const SSL_PROTOCOL_METHOD *method, |
| 65 |
uint16_t version) { |
| 66 |
for (uint16_t supported : get_method_versions(method)) { |
| 67 |
if (supported == version) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
// The following functions map between API versions and wire versions. The |
| 75 |
// public API works on wire versions. |
| 76 |
|
| 77 |
static const char* kUnknownVersion = "unknown"; |
| 78 |
|
| 79 |
struct VersionInfo { |
| 80 |
uint16_t version; |
| 81 |
const char *name; |
| 82 |
}; |
| 83 |
|
| 84 |
static const VersionInfo kVersionNames[] = { |
| 85 |
{TLS1_3_VERSION, "TLSv1.3"}, |
| 86 |
{TLS1_2_VERSION, "TLSv1.2"}, |
| 87 |
{TLS1_1_VERSION, "TLSv1.1"}, |
| 88 |
{TLS1_VERSION, "TLSv1"}, |
| 89 |
{DTLS1_VERSION, "DTLSv1"}, |
| 90 |
{DTLS1_2_VERSION, "DTLSv1.2"}, |
| 91 |
}; |
| 92 |
|
| 93 |
static const char *ssl_version_to_string(uint16_t version) { |
| 94 |
for (const auto &v : kVersionNames) { |
| 95 |
if (v.version == version) { |
| 96 |
return v.name; |
| 97 |
} |
| 98 |
} |
| 99 |
return kUnknownVersion; |
| 100 |
} |
| 101 |
|
| 102 |
static uint16_t wire_version_to_api(uint16_t version) { |
| 103 |
return version; |
| 104 |
} |
| 105 |
|
| 106 |
// api_version_to_wire maps |version| to some representative wire version. |
| 107 |
static bool api_version_to_wire(uint16_t *out, uint16_t version) { |
| 108 |
// Check it is a real protocol version. |
| 109 |
uint16_t unused; |
| 110 |
if (!ssl_protocol_version_from_wire(&unused, version)) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
*out = version; |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static bool set_version_bound(const SSL_PROTOCOL_METHOD *method, uint16_t *out, |
| 119 |
uint16_t version) { |
| 120 |
if (!api_version_to_wire(&version, version) || |
| 121 |
!ssl_method_supports_version(method, version)) { |
| 122 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_SSL_VERSION); |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
*out = version; |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
static bool set_min_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out, |
| 131 |
uint16_t version) { |
| 132 |
// Zero is interpreted as the default minimum version. |
| 133 |
if (version == 0) { |
| 134 |
*out = method->is_dtls ? DTLS1_VERSION : TLS1_VERSION; |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
return set_version_bound(method, out, version); |
| 139 |
} |
| 140 |
|
| 141 |
static bool set_max_version(const SSL_PROTOCOL_METHOD *method, uint16_t *out, |
| 142 |
uint16_t version) { |
| 143 |
// Zero is interpreted as the default maximum version. |
| 144 |
if (version == 0) { |
| 145 |
*out = method->is_dtls ? DTLS1_2_VERSION : TLS1_3_VERSION; |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
return set_version_bound(method, out, version); |
| 150 |
} |
| 151 |
|
| 152 |
const struct { |
| 153 |
uint16_t version; |
| 154 |
uint32_t flag; |
| 155 |
} kProtocolVersions[] = { |
| 156 |
{TLS1_VERSION, SSL_OP_NO_TLSv1}, |
| 157 |
{TLS1_1_VERSION, SSL_OP_NO_TLSv1_1}, |
| 158 |
{TLS1_2_VERSION, SSL_OP_NO_TLSv1_2}, |
| 159 |
{TLS1_3_VERSION, SSL_OP_NO_TLSv1_3}, |
| 160 |
}; |
| 161 |
|
| 162 |
bool ssl_get_version_range(const SSL_HANDSHAKE *hs, uint16_t *out_min_version, |
| 163 |
uint16_t *out_max_version) { |
| 164 |
// For historical reasons, |SSL_OP_NO_DTLSv1| aliases |SSL_OP_NO_TLSv1|, but |
| 165 |
// DTLS 1.0 should be mapped to TLS 1.1. |
| 166 |
uint32_t options = hs->ssl->options; |
| 167 |
if (SSL_is_dtls(hs->ssl)) { |
| 168 |
options &= ~SSL_OP_NO_TLSv1_1; |
| 169 |
if (options & SSL_OP_NO_DTLSv1) { |
| 170 |
options |= SSL_OP_NO_TLSv1_1; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
uint16_t min_version, max_version; |
| 175 |
if (!ssl_protocol_version_from_wire(&min_version, |
| 176 |
hs->config->conf_min_version) || |
| 177 |
!ssl_protocol_version_from_wire(&max_version, |
| 178 |
hs->config->conf_max_version)) { |
| 179 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
// QUIC requires TLS 1.3. |
| 184 |
if (hs->ssl->quic_method && min_version < TLS1_3_VERSION) { |
| 185 |
min_version = TLS1_3_VERSION; |
| 186 |
} |
| 187 |
|
| 188 |
// The |SSL_OP_NO_*| flags disable individual protocols. This has two |
| 189 |
// problems. First, prior to TLS 1.3, the protocol can only express a |
| 190 |
// contiguous range of versions. Second, a library consumer trying to set a |
| 191 |
// maximum version cannot disable protocol versions that get added in a future |
| 192 |
// version of the library. |
| 193 |
// |
| 194 |
// To account for both of these, OpenSSL interprets the client-side bitmask |
| 195 |
// as a min/max range by picking the lowest contiguous non-empty range of |
| 196 |
// enabled protocols. Note that this means it is impossible to set a maximum |
| 197 |
// version of the higest supported TLS version in a future-proof way. |
| 198 |
bool any_enabled = false; |
| 199 |
for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kProtocolVersions); i++) { |
| 200 |
// Only look at the versions already enabled. |
| 201 |
if (min_version > kProtocolVersions[i].version) { |
| 202 |
continue; |
| 203 |
} |
| 204 |
if (max_version < kProtocolVersions[i].version) { |
| 205 |
break; |
| 206 |
} |
| 207 |
|
| 208 |
if (!(options & kProtocolVersions[i].flag)) { |
| 209 |
// The minimum version is the first enabled version. |
| 210 |
if (!any_enabled) { |
| 211 |
any_enabled = true; |
| 212 |
min_version = kProtocolVersions[i].version; |
| 213 |
} |
| 214 |
continue; |
| 215 |
} |
| 216 |
|
| 217 |
// If there is a disabled version after the first enabled one, all versions |
| 218 |
// after it are implicitly disabled. |
| 219 |
if (any_enabled) { |
| 220 |
max_version = kProtocolVersions[i-1].version; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
if (!any_enabled) { |
| 226 |
OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SUPPORTED_VERSIONS_ENABLED); |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
*out_min_version = min_version; |
| 231 |
*out_max_version = max_version; |
| 232 |
return true; |
| 233 |
} |
| 234 |
|
| 235 |
static uint16_t ssl_version(const SSL *ssl) { |
| 236 |
// In early data, we report the predicted version. |
| 237 |
if (SSL_in_early_data(ssl) && !ssl->server) { |
| 238 |
return ssl->s3->hs->early_session->ssl_version; |
| 239 |
} |
| 240 |
return ssl->version; |
| 241 |
} |
| 242 |
|
| 243 |
uint16_t ssl_protocol_version(const SSL *ssl) { |
| 244 |
assert(ssl->s3->have_version); |
| 245 |
uint16_t version; |
| 246 |
if (!ssl_protocol_version_from_wire(&version, ssl->version)) { |
| 247 |
// |ssl->version| will always be set to a valid version. |
| 248 |
assert(0); |
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
return version; |
| 253 |
} |
| 254 |
|
| 255 |
bool ssl_supports_version(const SSL_HANDSHAKE *hs, uint16_t version) { |
| 256 |
const SSL *const ssl = hs->ssl; |
| 257 |
uint16_t protocol_version; |
| 258 |
if (!ssl_method_supports_version(ssl->method, version) || |
| 259 |
!ssl_protocol_version_from_wire(&protocol_version, version) || |
| 260 |
hs->min_version > protocol_version || |
| 261 |
protocol_version > hs->max_version) { |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
bool ssl_add_supported_versions(const SSL_HANDSHAKE *hs, CBB *cbb, |
| 269 |
uint16_t extra_min_version) { |
| 270 |
for (uint16_t version : get_method_versions(hs->ssl->method)) { |
| 271 |
uint16_t protocol_version; |
| 272 |
if (ssl_supports_version(hs, version) && |
| 273 |
ssl_protocol_version_from_wire(&protocol_version, version) && |
| 274 |
protocol_version >= extra_min_version && // |
| 275 |
!CBB_add_u16(cbb, version)) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
} |
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
bool ssl_negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 283 |
uint16_t *out_version, const CBS *peer_versions) { |
| 284 |
for (uint16_t version : get_method_versions(hs->ssl->method)) { |
| 285 |
if (!ssl_supports_version(hs, version)) { |
| 286 |
continue; |
| 287 |
} |
| 288 |
|
| 289 |
// JDK 11, prior to 11.0.2, has a buggy TLS 1.3 implementation which fails |
| 290 |
// to send SNI when offering 1.3 sessions. Disable TLS 1.3 for such |
| 291 |
// clients. We apply this logic here rather than |ssl_supports_version| so |
| 292 |
// the downgrade signal continues to query the true capabilities. (The |
| 293 |
// workaround is a limitation of the peer's capabilities rather than our |
| 294 |
// own.) |
| 295 |
// |
| 296 |
// See https://bugs.openjdk.java.net/browse/JDK-8211806. |
| 297 |
if (version == TLS1_3_VERSION && hs->apply_jdk11_workaround) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
CBS copy = *peer_versions; |
| 302 |
while (CBS_len(©) != 0) { |
| 303 |
uint16_t peer_version; |
| 304 |
if (!CBS_get_u16(©, &peer_version)) { |
| 305 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 306 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
if (peer_version == version) { |
| 311 |
*out_version = version; |
| 312 |
return true; |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 318 |
*out_alert = SSL_AD_PROTOCOL_VERSION; |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
BSSL_NAMESPACE_END |
| 323 |
|
| 324 |
using namespace bssl; |
| 325 |
|
| 326 |
int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, uint16_t version) { |
| 327 |
ctx->conf_min_version_use_default = (version == 0); |
| 328 |
return set_min_version(ctx->method, &ctx->conf_min_version, version); |
| 329 |
} |
| 330 |
|
| 331 |
int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, uint16_t version) { |
| 332 |
ctx->conf_max_version_use_default = (version == 0); |
| 333 |
return set_max_version(ctx->method, &ctx->conf_max_version, version); |
| 334 |
} |
| 335 |
|
| 336 |
uint16_t SSL_CTX_get_min_proto_version(const SSL_CTX *ctx) { |
| 337 |
if (ctx->conf_min_version_use_default) { |
| 338 |
return 0; |
| 339 |
} |
| 340 |
return ctx->conf_min_version; |
| 341 |
} |
| 342 |
|
| 343 |
uint16_t SSL_CTX_get_max_proto_version(const SSL_CTX *ctx) { |
| 344 |
if (ctx->conf_max_version_use_default) { |
| 345 |
return 0; |
| 346 |
} |
| 347 |
return ctx->conf_max_version; |
| 348 |
} |
| 349 |
|
| 350 |
int SSL_set_min_proto_version(SSL *ssl, uint16_t version) { |
| 351 |
if (!ssl->config) { |
| 352 |
return 0; |
| 353 |
} |
| 354 |
ssl->config->conf_min_version_use_default = (version == 0); |
| 355 |
return set_min_version(ssl->method, &ssl->config->conf_min_version, version); |
| 356 |
} |
| 357 |
|
| 358 |
int SSL_set_max_proto_version(SSL *ssl, uint16_t version) { |
| 359 |
if (!ssl->config) { |
| 360 |
return 0; |
| 361 |
} |
| 362 |
ssl->config->conf_max_version_use_default = (version == 0); |
| 363 |
return set_max_version(ssl->method, &ssl->config->conf_max_version, version); |
| 364 |
} |
| 365 |
|
| 366 |
uint16_t SSL_get_min_proto_version(const SSL *ssl) { |
| 367 |
if (!ssl->config || ssl->config->conf_min_version_use_default) { |
| 368 |
return 0; |
| 369 |
} |
| 370 |
return ssl->config->conf_min_version; |
| 371 |
} |
| 372 |
|
| 373 |
uint16_t SSL_get_max_proto_version(const SSL *ssl) { |
| 374 |
if (!ssl->config || ssl->config->conf_max_version_use_default) { |
| 375 |
return 0; |
| 376 |
} |
| 377 |
return ssl->config->conf_max_version; |
| 378 |
} |
| 379 |
|
| 380 |
int SSL_version(const SSL *ssl) { |
| 381 |
return wire_version_to_api(ssl_version(ssl)); |
| 382 |
} |
| 383 |
|
| 384 |
const char *SSL_get_version(const SSL *ssl) { |
| 385 |
return ssl_version_to_string(ssl_version(ssl)); |
| 386 |
} |
| 387 |
|
| 388 |
size_t SSL_get_all_version_names(const char **out, size_t max_out) { |
| 389 |
return GetAllNames(out, max_out, MakeConstSpan(&kUnknownVersion, 1), |
| 390 |
&VersionInfo::name, MakeConstSpan(kVersionNames)); |
| 391 |
} |
| 392 |
|
| 393 |
const char *SSL_SESSION_get_version(const SSL_SESSION *session) { |
| 394 |
return ssl_version_to_string(session->ssl_version); |
| 395 |
} |
| 396 |
|
| 397 |
uint16_t SSL_SESSION_get_protocol_version(const SSL_SESSION *session) { |
| 398 |
return wire_version_to_api(session->ssl_version); |
| 399 |
} |
| 400 |
|
| 401 |
int SSL_SESSION_set_protocol_version(SSL_SESSION *session, uint16_t version) { |
| 402 |
// This picks a representative TLS 1.3 version, but this API should only be |
| 403 |
// used on unit test sessions anyway. |
| 404 |
return api_version_to_wire(&session->ssl_version, version); |
| 405 |
} |
| 406 |
|
| 407 |
int SSL_CTX_set_record_protocol_version(SSL_CTX *ctx, int version) { |
| 408 |
return version == 0; |
| 409 |
} |