| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 4 |
// |
| 5 |
// ECC cipher suite support in OpenSSL originally developed by |
| 6 |
// SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. |
| 7 |
// |
| 8 |
// SPDX-License-Identifier: Apache-2.0 |
| 9 |
|
| 10 |
#include <openssl/ssl.h> |
| 11 |
|
| 12 |
#include <assert.h> |
| 13 |
#include <limits.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include <tuple> |
| 17 |
|
| 18 |
#include <openssl/buf.h> |
| 19 |
#include <openssl/bytestring.h> |
| 20 |
#include <openssl/err.h> |
| 21 |
#include <openssl/evp.h> |
| 22 |
#include <openssl/mem.h> |
| 23 |
#include <openssl/md5.h> |
| 24 |
#include <openssl/nid.h> |
| 25 |
#include <openssl/rand.h> |
| 26 |
#include <openssl/sha.h> |
| 27 |
|
| 28 |
#include "../crypto/internal.h" |
| 29 |
#include "internal.h" |
| 30 |
|
| 31 |
|
| 32 |
BSSL_NAMESPACE_BEGIN |
| 33 |
|
| 34 |
static bool add_record_to_flight(SSL *ssl, uint8_t type, |
| 35 |
Span<const uint8_t> in) { |
| 36 |
// The caller should have flushed |pending_hs_data| first. |
| 37 |
assert(!ssl->s3->pending_hs_data); |
| 38 |
// We'll never add a flight while in the process of writing it out. |
| 39 |
assert(ssl->s3->pending_flight_offset == 0); |
| 40 |
|
| 41 |
if (ssl->s3->pending_flight == nullptr) { |
| 42 |
ssl->s3->pending_flight.reset(BUF_MEM_new()); |
| 43 |
if (ssl->s3->pending_flight == nullptr) { |
| 44 |
return false; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
size_t max_out = in.size() + SSL_max_seal_overhead(ssl); |
| 49 |
size_t new_cap = ssl->s3->pending_flight->length + max_out; |
| 50 |
if (max_out < in.size() || new_cap < max_out) { |
| 51 |
OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
size_t len; |
| 56 |
if (!BUF_MEM_reserve(ssl->s3->pending_flight.get(), new_cap) || |
| 57 |
!tls_seal_record(ssl, |
| 58 |
(uint8_t *)ssl->s3->pending_flight->data + |
| 59 |
ssl->s3->pending_flight->length, |
| 60 |
&len, max_out, type, in.data(), in.size())) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
ssl->s3->pending_flight->length += len; |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
bool tls_init_message(const SSL *ssl, CBB *cbb, CBB *body, uint8_t type) { |
| 69 |
// Pick a modest size hint to save most of the |realloc| calls. |
| 70 |
if (!CBB_init(cbb, 64) || |
| 71 |
!CBB_add_u8(cbb, type) || |
| 72 |
!CBB_add_u24_length_prefixed(cbb, body)) { |
| 73 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 74 |
CBB_cleanup(cbb); |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
bool tls_finish_message(const SSL *ssl, CBB *cbb, Array<uint8_t> *out_msg) { |
| 82 |
return CBBFinishArray(cbb, out_msg); |
| 83 |
} |
| 84 |
|
| 85 |
bool tls_add_message(SSL *ssl, Array<uint8_t> msg) { |
| 86 |
// Pack handshake data into the minimal number of records. This avoids |
| 87 |
// unnecessary encryption overhead, notably in TLS 1.3 where we send several |
| 88 |
// encrypted messages in a row. For now, we do not do this for the null |
| 89 |
// cipher. The benefit is smaller and there is a risk of breaking buggy |
| 90 |
// implementations. |
| 91 |
// |
| 92 |
// TODO(davidben): See if we can do this uniformly. |
| 93 |
Span<const uint8_t> rest = msg; |
| 94 |
if (ssl->quic_method == nullptr && |
| 95 |
ssl->s3->aead_write_ctx->is_null_cipher()) { |
| 96 |
while (!rest.empty()) { |
| 97 |
Span<const uint8_t> chunk = rest.subspan(0, ssl->max_send_fragment); |
| 98 |
rest = rest.subspan(chunk.size()); |
| 99 |
|
| 100 |
if (!add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, chunk)) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
} else { |
| 105 |
while (!rest.empty()) { |
| 106 |
// Flush if |pending_hs_data| is full. |
| 107 |
if (ssl->s3->pending_hs_data && |
| 108 |
ssl->s3->pending_hs_data->length >= ssl->max_send_fragment && |
| 109 |
!tls_flush_pending_hs_data(ssl)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
size_t pending_len = |
| 114 |
ssl->s3->pending_hs_data ? ssl->s3->pending_hs_data->length : 0; |
| 115 |
Span<const uint8_t> chunk = |
| 116 |
rest.subspan(0, ssl->max_send_fragment - pending_len); |
| 117 |
assert(!chunk.empty()); |
| 118 |
rest = rest.subspan(chunk.size()); |
| 119 |
|
| 120 |
if (!ssl->s3->pending_hs_data) { |
| 121 |
ssl->s3->pending_hs_data.reset(BUF_MEM_new()); |
| 122 |
} |
| 123 |
if (!ssl->s3->pending_hs_data || |
| 124 |
!BUF_MEM_append(ssl->s3->pending_hs_data.get(), chunk.data(), |
| 125 |
chunk.size())) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HANDSHAKE, msg); |
| 132 |
// TODO(svaldez): Move this up a layer to fix abstraction for SSLTranscript on |
| 133 |
// hs. |
| 134 |
if (ssl->s3->hs != NULL && |
| 135 |
!ssl->s3->hs->transcript.Update(msg)) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
bool tls_flush_pending_hs_data(SSL *ssl) { |
| 142 |
if (!ssl->s3->pending_hs_data || ssl->s3->pending_hs_data->length == 0) { |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
UniquePtr<BUF_MEM> pending_hs_data = std::move(ssl->s3->pending_hs_data); |
| 147 |
auto data = |
| 148 |
MakeConstSpan(reinterpret_cast<const uint8_t *>(pending_hs_data->data), |
| 149 |
pending_hs_data->length); |
| 150 |
if (ssl->quic_method) { |
| 151 |
if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) && |
| 152 |
!ssl->quic_method->add_handshake_data(ssl, ssl->s3->write_level, |
| 153 |
data.data(), data.size())) { |
| 154 |
OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 155 |
return false; |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
return add_record_to_flight(ssl, SSL3_RT_HANDSHAKE, data); |
| 161 |
} |
| 162 |
|
| 163 |
bool tls_add_change_cipher_spec(SSL *ssl) { |
| 164 |
static const uint8_t kChangeCipherSpec[1] = {SSL3_MT_CCS}; |
| 165 |
|
| 166 |
if (!tls_flush_pending_hs_data(ssl)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
if (!ssl->quic_method && |
| 171 |
!add_record_to_flight(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 172 |
kChangeCipherSpec)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_CHANGE_CIPHER_SPEC, |
| 177 |
kChangeCipherSpec); |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
int tls_flush_flight(SSL *ssl) { |
| 182 |
if (!tls_flush_pending_hs_data(ssl)) { |
| 183 |
return -1; |
| 184 |
} |
| 185 |
|
| 186 |
if (ssl->quic_method) { |
| 187 |
if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 188 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 189 |
return -1; |
| 190 |
} |
| 191 |
|
| 192 |
if (!ssl->quic_method->flush_flight(ssl)) { |
| 193 |
OPENSSL_PUT_ERROR(SSL, SSL_R_QUIC_INTERNAL_ERROR); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (ssl->s3->pending_flight == nullptr) { |
| 199 |
return 1; |
| 200 |
} |
| 201 |
|
| 202 |
if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 203 |
OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
static_assert(INT_MAX <= 0xffffffff, "int is larger than 32 bits"); |
| 208 |
if (ssl->s3->pending_flight->length > INT_MAX) { |
| 209 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 210 |
return -1; |
| 211 |
} |
| 212 |
|
| 213 |
// If there is pending data in the write buffer, it must be flushed out before |
| 214 |
// any new data in pending_flight. |
| 215 |
if (!ssl->s3->write_buffer.empty()) { |
| 216 |
int ret = ssl_write_buffer_flush(ssl); |
| 217 |
if (ret <= 0) { |
| 218 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 219 |
return ret; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (ssl->wbio == nullptr) { |
| 224 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BIO_NOT_SET); |
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
// Write the pending flight. |
| 229 |
while (ssl->s3->pending_flight_offset < ssl->s3->pending_flight->length) { |
| 230 |
int ret = BIO_write( |
| 231 |
ssl->wbio.get(), |
| 232 |
ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset, |
| 233 |
ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset); |
| 234 |
if (ret <= 0) { |
| 235 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 236 |
return ret; |
| 237 |
} |
| 238 |
|
| 239 |
ssl->s3->pending_flight_offset += ret; |
| 240 |
} |
| 241 |
|
| 242 |
if (BIO_flush(ssl->wbio.get()) <= 0) { |
| 243 |
ssl->s3->rwstate = SSL_ERROR_WANT_WRITE; |
| 244 |
return -1; |
| 245 |
} |
| 246 |
|
| 247 |
ssl->s3->pending_flight.reset(); |
| 248 |
ssl->s3->pending_flight_offset = 0; |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
static ssl_open_record_t read_v2_client_hello(SSL *ssl, size_t *out_consumed, |
| 253 |
Span<const uint8_t> in) { |
| 254 |
*out_consumed = 0; |
| 255 |
assert(in.size() >= SSL3_RT_HEADER_LENGTH); |
| 256 |
// Determine the length of the V2ClientHello. |
| 257 |
size_t msg_length = ((in[0] & 0x7f) << 8) | in[1]; |
| 258 |
if (msg_length > (1024 * 4)) { |
| 259 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
| 260 |
return ssl_open_record_error; |
| 261 |
} |
| 262 |
if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { |
| 263 |
// Reject lengths that are too short early. We have already read |
| 264 |
// |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an |
| 265 |
// (invalid) V2ClientHello which would be shorter than that. |
| 266 |
OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); |
| 267 |
return ssl_open_record_error; |
| 268 |
} |
| 269 |
|
| 270 |
// Ask for the remainder of the V2ClientHello. |
| 271 |
if (in.size() < 2 + msg_length) { |
| 272 |
*out_consumed = 2 + msg_length; |
| 273 |
return ssl_open_record_partial; |
| 274 |
} |
| 275 |
|
| 276 |
CBS v2_client_hello = CBS(ssl->s3->read_buffer.span().subspan(2, msg_length)); |
| 277 |
// The V2ClientHello without the length is incorporated into the handshake |
| 278 |
// hash. This is only ever called at the start of the handshake, so hs is |
| 279 |
// guaranteed to be non-NULL. |
| 280 |
if (!ssl->s3->hs->transcript.Update(v2_client_hello)) { |
| 281 |
return ssl_open_record_error; |
| 282 |
} |
| 283 |
|
| 284 |
ssl_do_msg_callback(ssl, 0 /* read */, 0 /* V2ClientHello */, |
| 285 |
v2_client_hello); |
| 286 |
|
| 287 |
uint8_t msg_type; |
| 288 |
uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 289 |
CBS cipher_specs, session_id, challenge; |
| 290 |
if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 291 |
!CBS_get_u16(&v2_client_hello, &version) || |
| 292 |
!CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 293 |
!CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 294 |
!CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 295 |
!CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 296 |
!CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 297 |
!CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 298 |
CBS_len(&v2_client_hello) != 0) { |
| 299 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 300 |
return ssl_open_record_error; |
| 301 |
} |
| 302 |
|
| 303 |
// msg_type has already been checked. |
| 304 |
assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 305 |
|
| 306 |
// The client_random is the V2ClientHello challenge. Truncate or left-pad with |
| 307 |
// zeros as needed. |
| 308 |
size_t rand_len = CBS_len(&challenge); |
| 309 |
if (rand_len > SSL3_RANDOM_SIZE) { |
| 310 |
rand_len = SSL3_RANDOM_SIZE; |
| 311 |
} |
| 312 |
uint8_t random[SSL3_RANDOM_SIZE]; |
| 313 |
OPENSSL_memset(random, 0, SSL3_RANDOM_SIZE); |
| 314 |
OPENSSL_memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 315 |
rand_len); |
| 316 |
|
| 317 |
// Write out an equivalent TLS ClientHello directly to the handshake buffer. |
| 318 |
size_t max_v3_client_hello = SSL3_HM_HEADER_LENGTH + 2 /* version */ + |
| 319 |
SSL3_RANDOM_SIZE + 1 /* session ID length */ + |
| 320 |
2 /* cipher list length */ + |
| 321 |
CBS_len(&cipher_specs) / 3 * 2 + |
| 322 |
1 /* compression length */ + 1 /* compression */; |
| 323 |
ScopedCBB client_hello; |
| 324 |
CBB hello_body, cipher_suites; |
| 325 |
if (!ssl->s3->hs_buf) { |
| 326 |
ssl->s3->hs_buf.reset(BUF_MEM_new()); |
| 327 |
} |
| 328 |
if (!ssl->s3->hs_buf || |
| 329 |
!BUF_MEM_reserve(ssl->s3->hs_buf.get(), max_v3_client_hello) || |
| 330 |
!CBB_init_fixed(client_hello.get(), (uint8_t *)ssl->s3->hs_buf->data, |
| 331 |
ssl->s3->hs_buf->max) || |
| 332 |
!CBB_add_u8(client_hello.get(), SSL3_MT_CLIENT_HELLO) || |
| 333 |
!CBB_add_u24_length_prefixed(client_hello.get(), &hello_body) || |
| 334 |
!CBB_add_u16(&hello_body, version) || |
| 335 |
!CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 336 |
// No session id. |
| 337 |
!CBB_add_u8(&hello_body, 0) || |
| 338 |
!CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 339 |
return ssl_open_record_error; |
| 340 |
} |
| 341 |
|
| 342 |
// Copy the cipher suites. |
| 343 |
while (CBS_len(&cipher_specs) > 0) { |
| 344 |
uint32_t cipher_spec; |
| 345 |
if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 346 |
OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 347 |
return ssl_open_record_error; |
| 348 |
} |
| 349 |
|
| 350 |
// Skip SSLv2 ciphers. |
| 351 |
if ((cipher_spec & 0xff0000) != 0) { |
| 352 |
continue; |
| 353 |
} |
| 354 |
if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 355 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 356 |
return ssl_open_record_error; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
// Add the null compression scheme and finish. |
| 361 |
if (!CBB_add_u8(&hello_body, 1) || |
| 362 |
!CBB_add_u8(&hello_body, 0) || |
| 363 |
!CBB_finish(client_hello.get(), NULL, &ssl->s3->hs_buf->length)) { |
| 364 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 365 |
return ssl_open_record_error; |
| 366 |
} |
| 367 |
|
| 368 |
*out_consumed = 2 + msg_length; |
| 369 |
ssl->s3->is_v2_hello = true; |
| 370 |
return ssl_open_record_success; |
| 371 |
} |
| 372 |
|
| 373 |
static bool parse_message(const SSL *ssl, SSLMessage *out, |
| 374 |
size_t *out_bytes_needed) { |
| 375 |
if (!ssl->s3->hs_buf) { |
| 376 |
*out_bytes_needed = 4; |
| 377 |
return false; |
| 378 |
} |
| 379 |
|
| 380 |
CBS cbs; |
| 381 |
uint32_t len; |
| 382 |
CBS_init(&cbs, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data), |
| 383 |
ssl->s3->hs_buf->length); |
| 384 |
if (!CBS_get_u8(&cbs, &out->type) || |
| 385 |
!CBS_get_u24(&cbs, &len)) { |
| 386 |
*out_bytes_needed = 4; |
| 387 |
return false; |
| 388 |
} |
| 389 |
|
| 390 |
if (!CBS_get_bytes(&cbs, &out->body, len)) { |
| 391 |
*out_bytes_needed = 4 + len; |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
CBS_init(&out->raw, reinterpret_cast<const uint8_t *>(ssl->s3->hs_buf->data), |
| 396 |
4 + len); |
| 397 |
out->is_v2_hello = ssl->s3->is_v2_hello; |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
bool tls_get_message(const SSL *ssl, SSLMessage *out) { |
| 402 |
size_t unused; |
| 403 |
if (!parse_message(ssl, out, &unused)) { |
| 404 |
return false; |
| 405 |
} |
| 406 |
if (!ssl->s3->has_message) { |
| 407 |
if (!out->is_v2_hello) { |
| 408 |
ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HANDSHAKE, out->raw); |
| 409 |
} |
| 410 |
ssl->s3->has_message = true; |
| 411 |
} |
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert) { |
| 416 |
// If there is a complete message, the caller must have consumed it first. |
| 417 |
SSLMessage msg; |
| 418 |
size_t bytes_needed; |
| 419 |
if (parse_message(ssl, &msg, &bytes_needed)) { |
| 420 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 421 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
// Enforce the limit so the peer cannot force us to buffer 16MB. |
| 426 |
if (bytes_needed > 4 + ssl_max_handshake_message_len(ssl)) { |
| 427 |
OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESSIVE_MESSAGE_SIZE); |
| 428 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
return true; |
| 433 |
} |
| 434 |
|
| 435 |
bool tls_has_unprocessed_handshake_data(const SSL *ssl) { |
| 436 |
size_t msg_len = 0; |
| 437 |
if (ssl->s3->has_message) { |
| 438 |
SSLMessage msg; |
| 439 |
size_t unused; |
| 440 |
if (parse_message(ssl, &msg, &unused)) { |
| 441 |
msg_len = CBS_len(&msg.raw); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
return ssl->s3->hs_buf && ssl->s3->hs_buf->length > msg_len; |
| 446 |
} |
| 447 |
|
| 448 |
bool tls_append_handshake_data(SSL *ssl, Span<const uint8_t> data) { |
| 449 |
// Re-create the handshake buffer if needed. |
| 450 |
if (!ssl->s3->hs_buf) { |
| 451 |
ssl->s3->hs_buf.reset(BUF_MEM_new()); |
| 452 |
} |
| 453 |
return ssl->s3->hs_buf && |
| 454 |
BUF_MEM_append(ssl->s3->hs_buf.get(), data.data(), data.size()); |
| 455 |
} |
| 456 |
|
| 457 |
ssl_open_record_t tls_open_handshake(SSL *ssl, size_t *out_consumed, |
| 458 |
uint8_t *out_alert, Span<uint8_t> in) { |
| 459 |
*out_consumed = 0; |
| 460 |
// Bypass the record layer for the first message to handle V2ClientHello. |
| 461 |
if (ssl->server && !ssl->s3->v2_hello_done) { |
| 462 |
// Ask for the first 5 bytes, the size of the TLS record header. This is |
| 463 |
// sufficient to detect a V2ClientHello and ensures that we never read |
| 464 |
// beyond the first record. |
| 465 |
if (in.size() < SSL3_RT_HEADER_LENGTH) { |
| 466 |
*out_consumed = SSL3_RT_HEADER_LENGTH; |
| 467 |
return ssl_open_record_partial; |
| 468 |
} |
| 469 |
|
| 470 |
// Some dedicated error codes for protocol mixups should the application |
| 471 |
// wish to interpret them differently. (These do not overlap with |
| 472 |
// ClientHello or V2ClientHello.) |
| 473 |
const char *str = reinterpret_cast<const char*>(in.data()); |
| 474 |
if (strncmp("GET ", str, 4) == 0 || |
| 475 |
strncmp("POST ", str, 5) == 0 || |
| 476 |
strncmp("HEAD ", str, 5) == 0 || |
| 477 |
strncmp("PUT ", str, 4) == 0) { |
| 478 |
OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); |
| 479 |
*out_alert = 0; |
| 480 |
return ssl_open_record_error; |
| 481 |
} |
| 482 |
if (strncmp("CONNE", str, 5) == 0) { |
| 483 |
OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); |
| 484 |
*out_alert = 0; |
| 485 |
return ssl_open_record_error; |
| 486 |
} |
| 487 |
|
| 488 |
// Check for a V2ClientHello. |
| 489 |
if ((in[0] & 0x80) != 0 && in[2] == SSL2_MT_CLIENT_HELLO && |
| 490 |
in[3] == SSL3_VERSION_MAJOR) { |
| 491 |
auto ret = read_v2_client_hello(ssl, out_consumed, in); |
| 492 |
if (ret == ssl_open_record_error) { |
| 493 |
*out_alert = 0; |
| 494 |
} else if (ret == ssl_open_record_success) { |
| 495 |
ssl->s3->v2_hello_done = true; |
| 496 |
} |
| 497 |
return ret; |
| 498 |
} |
| 499 |
|
| 500 |
ssl->s3->v2_hello_done = true; |
| 501 |
} |
| 502 |
|
| 503 |
uint8_t type; |
| 504 |
Span<uint8_t> body; |
| 505 |
auto ret = tls_open_record(ssl, &type, &body, out_consumed, out_alert, in); |
| 506 |
if (ret != ssl_open_record_success) { |
| 507 |
return ret; |
| 508 |
} |
| 509 |
|
| 510 |
// WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the |
| 511 |
// ServerHello and send the remaining encrypted application data records |
| 512 |
// as-is. This manifests as an application data record when we expect |
| 513 |
// handshake. Report a dedicated error code for this case. |
| 514 |
if (!ssl->server && type == SSL3_RT_APPLICATION_DATA && |
| 515 |
ssl->s3->aead_read_ctx->is_null_cipher()) { |
| 516 |
OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE); |
| 517 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 518 |
return ssl_open_record_error; |
| 519 |
} |
| 520 |
|
| 521 |
if (type != SSL3_RT_HANDSHAKE) { |
| 522 |
OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 523 |
*out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 524 |
return ssl_open_record_error; |
| 525 |
} |
| 526 |
|
| 527 |
// Append the entire handshake record to the buffer. |
| 528 |
if (!tls_append_handshake_data(ssl, body)) { |
| 529 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 530 |
return ssl_open_record_error; |
| 531 |
} |
| 532 |
|
| 533 |
return ssl_open_record_success; |
| 534 |
} |
| 535 |
|
| 536 |
void tls_next_message(SSL *ssl) { |
| 537 |
SSLMessage msg; |
| 538 |
if (!tls_get_message(ssl, &msg) || |
| 539 |
!ssl->s3->hs_buf || |
| 540 |
ssl->s3->hs_buf->length < CBS_len(&msg.raw)) { |
| 541 |
assert(0); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
OPENSSL_memmove(ssl->s3->hs_buf->data, |
| 546 |
ssl->s3->hs_buf->data + CBS_len(&msg.raw), |
| 547 |
ssl->s3->hs_buf->length - CBS_len(&msg.raw)); |
| 548 |
ssl->s3->hs_buf->length -= CBS_len(&msg.raw); |
| 549 |
ssl->s3->is_v2_hello = false; |
| 550 |
ssl->s3->has_message = false; |
| 551 |
|
| 552 |
// Post-handshake messages are rare, so release the buffer after every |
| 553 |
// message. During the handshake, |on_handshake_complete| will release it. |
| 554 |
if (!SSL_in_init(ssl) && ssl->s3->hs_buf->length == 0) { |
| 555 |
ssl->s3->hs_buf.reset(); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
// CipherScorer produces a "score" for each possible cipher suite offered by |
| 560 |
// the client. |
| 561 |
class CipherScorer { |
| 562 |
public: |
| 563 |
CipherScorer(bool has_aes_hw) : aes_is_fine_(has_aes_hw) {} |
| 564 |
|
| 565 |
typedef std::tuple<bool, bool> Score; |
| 566 |
|
| 567 |
// MinScore returns a |Score| that will compare less than the score of all |
| 568 |
// cipher suites. |
| 569 |
Score MinScore() const { |
| 570 |
return Score(false, false); |
| 571 |
} |
| 572 |
|
| 573 |
Score Evaluate(const SSL_CIPHER *a) const { |
| 574 |
return Score( |
| 575 |
// Something is always preferable to nothing. |
| 576 |
true, |
| 577 |
// Either AES is fine, or else ChaCha20 is preferred. |
| 578 |
aes_is_fine_ || a->algorithm_enc == SSL_CHACHA20POLY1305); |
| 579 |
} |
| 580 |
|
| 581 |
private: |
| 582 |
const bool aes_is_fine_; |
| 583 |
}; |
| 584 |
|
| 585 |
const SSL_CIPHER *ssl_choose_tls13_cipher( |
| 586 |
const STACK_OF(SSL_CIPHER) *client_cipher_suites, bool has_aes_hw, uint16_t version, |
| 587 |
const STACK_OF(SSL_CIPHER) *tls13_ciphers) { |
| 588 |
|
| 589 |
const SSL_CIPHER *best = nullptr; |
| 590 |
CipherScorer scorer(has_aes_hw); |
| 591 |
CipherScorer::Score best_score = scorer.MinScore(); |
| 592 |
|
| 593 |
for (size_t i = 0; i < sk_SSL_CIPHER_num(client_cipher_suites); i++) { |
| 594 |
const SSL_CIPHER *client_cipher = sk_SSL_CIPHER_value(client_cipher_suites, i); |
| 595 |
const SSL_CIPHER *candidate = nullptr; |
| 596 |
if (tls13_ciphers != nullptr) { |
| 597 |
// Limit to configured TLS 1.3 ciphers |
| 598 |
for (size_t j = 0; j < sk_SSL_CIPHER_num(tls13_ciphers); j++) { |
| 599 |
const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(tls13_ciphers, j); |
| 600 |
if (cipher != nullptr && cipher->id == client_cipher->id) { |
| 601 |
candidate = (SSL_CIPHER *)cipher; |
| 602 |
break; |
| 603 |
} |
| 604 |
} |
| 605 |
} |
| 606 |
if (candidate == nullptr || |
| 607 |
SSL_CIPHER_get_min_version(candidate) > version || |
| 608 |
SSL_CIPHER_get_max_version(candidate) < version) { |
| 609 |
continue; |
| 610 |
} |
| 611 |
|
| 612 |
const CipherScorer::Score candidate_score = scorer.Evaluate(candidate); |
| 613 |
// |candidate_score| must be larger to displace the current choice. That way |
| 614 |
// the client's order controls between ciphers with an equal score. |
| 615 |
if (candidate_score > best_score) { |
| 616 |
best = candidate; |
| 617 |
best_score = candidate_score; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return best; |
| 622 |
} |
| 623 |
|
| 624 |
BSSL_NAMESPACE_END |