1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
/* Copyright (c) 2016, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <CNIOBoringSSL_ssl.h>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <utility>
#include <CNIOBoringSSL_aead.h>
#include <CNIOBoringSSL_bytestring.h>
#include <CNIOBoringSSL_digest.h>
#include <CNIOBoringSSL_hkdf.h>
#include <CNIOBoringSSL_hmac.h>
#include <CNIOBoringSSL_mem.h>
#include "../crypto/internal.h"
#include "internal.h"
BSSL_NAMESPACE_BEGIN
static bool init_key_schedule(SSL_HANDSHAKE *hs, SSLTranscript *transcript,
uint16_t version, const SSL_CIPHER *cipher) {
if (!transcript->InitHash(version, cipher)) {
return false;
}
// Initialize the secret to the zero key.
hs->ResizeSecrets(transcript->DigestLen());
OPENSSL_memset(hs->secret().data(), 0, hs->secret().size());
return true;
}
static bool hkdf_extract_to_secret(SSL_HANDSHAKE *hs,
const SSLTranscript &transcript,
Span<const uint8_t> in) {
size_t len;
if (!HKDF_extract(hs->secret().data(), &len, transcript.Digest(), in.data(),
in.size(), hs->secret().data(), hs->secret().size())) {
return false;
}
assert(len == hs->secret().size());
return true;
}
bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk) {
if (!init_key_schedule(hs, &hs->transcript, ssl_protocol_version(hs->ssl),
hs->new_cipher)) {
return false;
}
// Handback includes the whole handshake transcript, so we cannot free the
// transcript buffer in the handback case.
if (!hs->handback) {
hs->transcript.FreeBuffer();
}
return hkdf_extract_to_secret(hs, hs->transcript, psk);
}
bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs,
const SSL_SESSION *session) {
assert(!hs->ssl->server);
// When offering ECH, early data is associated with ClientHelloInner, not
// ClientHelloOuter.
SSLTranscript *transcript =
hs->selected_ech_config ? &hs->inner_transcript : &hs->transcript;
return init_key_schedule(hs, transcript,
ssl_session_protocol_version(session),
session->cipher) &&
hkdf_extract_to_secret(
hs, *transcript,
MakeConstSpan(session->secret, session->secret_length));
}
static Span<const char> label_to_span(const char *label) {
return MakeConstSpan(label, strlen(label));
}
static bool hkdf_expand_label(Span<uint8_t> out, const EVP_MD *digest,
Span<const uint8_t> secret,
Span<const char> label,
Span<const uint8_t> hash) {
Span<const char> protocol_label = label_to_span("tls13 ");
ScopedCBB cbb;
CBB child;
Array<uint8_t> hkdf_label;
if (!CBB_init(cbb.get(), 2 + 1 + protocol_label.size() + label.size() + 1 +
hash.size()) ||
!CBB_add_u16(cbb.get(), out.size()) ||
!CBB_add_u8_length_prefixed(cbb.get(), &child) ||
!CBB_add_bytes(&child,
reinterpret_cast<const uint8_t *>(protocol_label.data()),
protocol_label.size()) ||
!CBB_add_bytes(&child, reinterpret_cast<const uint8_t *>(label.data()),
label.size()) ||
!CBB_add_u8_length_prefixed(cbb.get(), &child) ||
!CBB_add_bytes(&child, hash.data(), hash.size()) ||
!CBBFinishArray(cbb.get(), &hkdf_label)) {
return false;
}
return HKDF_expand(out.data(), out.size(), digest, secret.data(),
secret.size(), hkdf_label.data(), hkdf_label.size());
}
static const char kTLS13LabelDerived[] = "derived";
bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in) {
uint8_t derive_context[EVP_MAX_MD_SIZE];
unsigned derive_context_len;
return EVP_Digest(nullptr, 0, derive_context, &derive_context_len,
hs->transcript.Digest(), nullptr) &&
hkdf_expand_label(hs->secret(), hs->transcript.Digest(), hs->secret(),
label_to_span(kTLS13LabelDerived),
MakeConstSpan(derive_context, derive_context_len)) &&
hkdf_extract_to_secret(hs, hs->transcript, in);
}
// derive_secret_with_transcript derives a secret of length |out.size()| and
// writes the result in |out| with the given label, the current base secret, and
// the state of |transcript|. It returns true on success and false on error.
static bool derive_secret_with_transcript(const SSL_HANDSHAKE *hs,
Span<uint8_t> out,
const SSLTranscript &transcript,
Span<const char> label) {
uint8_t context_hash[EVP_MAX_MD_SIZE];
size_t context_hash_len;
if (!transcript.GetHash(context_hash, &context_hash_len)) {
return false;
}
return hkdf_expand_label(out, transcript.Digest(), hs->secret(), label,
MakeConstSpan(context_hash, context_hash_len));
}
static bool derive_secret(SSL_HANDSHAKE *hs, Span<uint8_t> out,
Span<const char> label) {
return derive_secret_with_transcript(hs, out, hs->transcript, label);
}
bool tls13_set_traffic_key(SSL *ssl, enum ssl_encryption_level_t level,
enum evp_aead_direction_t direction,
const SSL_SESSION *session,
Span<const uint8_t> traffic_secret) {
uint16_t version = ssl_session_protocol_version(session);
UniquePtr<SSLAEADContext> traffic_aead;
Span<const uint8_t> secret_for_quic;
if (ssl->quic_method != nullptr) {
// Install a placeholder SSLAEADContext so that SSL accessors work. The
// encryption itself will be handled by the SSL_QUIC_METHOD.
traffic_aead =
SSLAEADContext::CreatePlaceholderForQUIC(version, session->cipher);
secret_for_quic = traffic_secret;
} else {
// Look up cipher suite properties.
const EVP_AEAD *aead;
size_t discard;
if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher,
version, SSL_is_dtls(ssl))) {
return false;
}
const EVP_MD *digest = ssl_session_get_digest(session);
// Derive the key.
size_t key_len = EVP_AEAD_key_length(aead);
uint8_t key_buf[EVP_AEAD_MAX_KEY_LENGTH];
auto key = MakeSpan(key_buf, key_len);
if (!hkdf_expand_label(key, digest, traffic_secret, label_to_span("key"),
{})) {
return false;
}
// Derive the IV.
size_t iv_len = EVP_AEAD_nonce_length(aead);
uint8_t iv_buf[EVP_AEAD_MAX_NONCE_LENGTH];
auto iv = MakeSpan(iv_buf, iv_len);
if (!hkdf_expand_label(iv, digest, traffic_secret, label_to_span("iv"),
{})) {
return false;
}
traffic_aead = SSLAEADContext::Create(direction, session->ssl_version,
SSL_is_dtls(ssl), session->cipher,
key, Span<const uint8_t>(), iv);
}
if (!traffic_aead) {
return false;
}
if (traffic_secret.size() >
OPENSSL_ARRAY_SIZE(ssl->s3->read_traffic_secret) ||
traffic_secret.size() >
OPENSSL_ARRAY_SIZE(ssl->s3->write_traffic_secret)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
if (direction == evp_aead_open) {
if (!ssl->method->set_read_state(ssl, level, std::move(traffic_aead),
secret_for_quic)) {
return false;
}
OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret.data(),
traffic_secret.size());
ssl->s3->read_traffic_secret_len = traffic_secret.size();
} else {
if (!ssl->method->set_write_state(ssl, level, std::move(traffic_aead),
secret_for_quic)) {
return false;
}
OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret.data(),
traffic_secret.size());
ssl->s3->write_traffic_secret_len = traffic_secret.size();
}
return true;
}
static const char kTLS13LabelExporter[] = "exp master";
static const char kTLS13LabelClientEarlyTraffic[] = "c e traffic";
static const char kTLS13LabelClientHandshakeTraffic[] = "c hs traffic";
static const char kTLS13LabelServerHandshakeTraffic[] = "s hs traffic";
static const char kTLS13LabelClientApplicationTraffic[] = "c ap traffic";
static const char kTLS13LabelServerApplicationTraffic[] = "s ap traffic";
bool tls13_derive_early_secret(SSL_HANDSHAKE *hs) {
SSL *const ssl = hs->ssl;
// When offering ECH on the client, early data is associated with
// ClientHelloInner, not ClientHelloOuter.
const SSLTranscript &transcript = (!ssl->server && hs->selected_ech_config)
? hs->inner_transcript
: hs->transcript;
if (!derive_secret_with_transcript(
hs, hs->early_traffic_secret(), transcript,
label_to_span(kTLS13LabelClientEarlyTraffic)) ||
!ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET",
hs->early_traffic_secret())) {
return false;
}
return true;
}
bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) {
SSL *const ssl = hs->ssl;
if (!derive_secret(hs, hs->client_handshake_secret(),
label_to_span(kTLS13LabelClientHandshakeTraffic)) ||
!ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
hs->client_handshake_secret()) ||
!derive_secret(hs, hs->server_handshake_secret(),
label_to_span(kTLS13LabelServerHandshakeTraffic)) ||
!ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
hs->server_handshake_secret())) {
return false;
}
return true;
}
bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs) {
SSL *const ssl = hs->ssl;
ssl->s3->exporter_secret_len = hs->transcript.DigestLen();
if (!derive_secret(hs, hs->client_traffic_secret_0(),
label_to_span(kTLS13LabelClientApplicationTraffic)) ||
!ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0",
hs->client_traffic_secret_0()) ||
!derive_secret(hs, hs->server_traffic_secret_0(),
label_to_span(kTLS13LabelServerApplicationTraffic)) ||
!ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0",
hs->server_traffic_secret_0()) ||
!derive_secret(
hs, MakeSpan(ssl->s3->exporter_secret, ssl->s3->exporter_secret_len),
label_to_span(kTLS13LabelExporter)) ||
!ssl_log_secret(ssl, "EXPORTER_SECRET",
MakeConstSpan(ssl->s3->exporter_secret,
ssl->s3->exporter_secret_len))) {
return false;
}
return true;
}
static const char kTLS13LabelApplicationTraffic[] = "traffic upd";
bool tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) {
Span<uint8_t> secret;
if (direction == evp_aead_open) {
secret = MakeSpan(ssl->s3->read_traffic_secret,
ssl->s3->read_traffic_secret_len);
} else {
secret = MakeSpan(ssl->s3->write_traffic_secret,
ssl->s3->write_traffic_secret_len);
}
const SSL_SESSION *session = SSL_get_session(ssl);
const EVP_MD *digest = ssl_session_get_digest(session);
return hkdf_expand_label(secret, digest, secret,
label_to_span(kTLS13LabelApplicationTraffic), {}) &&
tls13_set_traffic_key(ssl, ssl_encryption_application, direction,
session, secret);
}
static const char kTLS13LabelResumption[] = "res master";
bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) {
if (hs->transcript.DigestLen() > SSL_MAX_MASTER_KEY_LENGTH) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
hs->new_session->secret_length = hs->transcript.DigestLen();
return derive_secret(
hs, MakeSpan(hs->new_session->secret, hs->new_session->secret_length),
label_to_span(kTLS13LabelResumption));
}
static const char kTLS13LabelFinished[] = "finished";
// tls13_verify_data sets |out| to be the HMAC of |context| using a derived
// Finished key for both Finished messages and the PSK binder. |out| must have
// space available for |EVP_MAX_MD_SIZE| bytes.
static bool tls13_verify_data(uint8_t *out, size_t *out_len,
const EVP_MD *digest, uint16_t version,
Span<const uint8_t> secret,
Span<const uint8_t> context) {
uint8_t key_buf[EVP_MAX_MD_SIZE];
auto key = MakeSpan(key_buf, EVP_MD_size(digest));
unsigned len;
if (!hkdf_expand_label(key, digest, secret,
label_to_span(kTLS13LabelFinished), {}) ||
HMAC(digest, key.data(), key.size(), context.data(), context.size(), out,
&len) == nullptr) {
return false;
}
*out_len = len;
return true;
}
bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
bool is_server) {
Span<const uint8_t> traffic_secret =
is_server ? hs->server_handshake_secret() : hs->client_handshake_secret();
uint8_t context_hash[EVP_MAX_MD_SIZE];
size_t context_hash_len;
if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
!tls13_verify_data(out, out_len, hs->transcript.Digest(),
hs->ssl->version, traffic_secret,
MakeConstSpan(context_hash, context_hash_len))) {
return 0;
}
return 1;
}
static const char kTLS13LabelResumptionPSK[] = "resumption";
bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce) {
const EVP_MD *digest = ssl_session_get_digest(session);
// The session initially stores the resumption_master_secret, which we
// override with the PSK.
auto session_secret = MakeSpan(session->secret, session->secret_length);
return hkdf_expand_label(session_secret, digest, session_secret,
label_to_span(kTLS13LabelResumptionPSK), nonce);
}
static const char kTLS13LabelExportKeying[] = "exporter";
bool tls13_export_keying_material(SSL *ssl, Span<uint8_t> out,
Span<const uint8_t> secret,
Span<const char> label,
Span<const uint8_t> context) {
if (secret.empty()) {
assert(0);
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl));
uint8_t hash_buf[EVP_MAX_MD_SIZE];
uint8_t export_context_buf[EVP_MAX_MD_SIZE];
unsigned hash_len;
unsigned export_context_len;
if (!EVP_Digest(context.data(), context.size(), hash_buf, &hash_len, digest,
nullptr) ||
!EVP_Digest(nullptr, 0, export_context_buf, &export_context_len, digest,
nullptr)) {
return false;
}
auto hash = MakeConstSpan(hash_buf, hash_len);
auto export_context = MakeConstSpan(export_context_buf, export_context_len);
uint8_t derived_secret_buf[EVP_MAX_MD_SIZE];
auto derived_secret = MakeSpan(derived_secret_buf, EVP_MD_size(digest));
return hkdf_expand_label(derived_secret, digest, secret, label,
export_context) &&
hkdf_expand_label(out, digest, derived_secret,
label_to_span(kTLS13LabelExportKeying), hash);
}
static const char kTLS13LabelPSKBinder[] = "res binder";
static bool tls13_psk_binder(uint8_t *out, size_t *out_len,
const SSL_SESSION *session,
const SSLTranscript &transcript,
Span<const uint8_t> client_hello,
size_t binders_len) {
const EVP_MD *digest = ssl_session_get_digest(session);
// Compute the binder key.
//
// TODO(davidben): Ideally we wouldn't recompute early secret and the binder
// key each time.
uint8_t binder_context[EVP_MAX_MD_SIZE];
unsigned binder_context_len;
uint8_t early_secret[EVP_MAX_MD_SIZE] = {0};
size_t early_secret_len;
uint8_t binder_key_buf[EVP_MAX_MD_SIZE] = {0};
auto binder_key = MakeSpan(binder_key_buf, EVP_MD_size(digest));
if (!EVP_Digest(nullptr, 0, binder_context, &binder_context_len, digest,
nullptr) ||
!HKDF_extract(early_secret, &early_secret_len, digest, session->secret,
session->secret_length, nullptr, 0) ||
!hkdf_expand_label(binder_key, digest,
MakeConstSpan(early_secret, early_secret_len),
label_to_span(kTLS13LabelPSKBinder),
MakeConstSpan(binder_context, binder_context_len))) {
return false;
}
// Hash the transcript and truncated ClientHello.
if (client_hello.size() < binders_len) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
auto truncated = client_hello.subspan(0, client_hello.size() - binders_len);
uint8_t context[EVP_MAX_MD_SIZE];
unsigned context_len;
ScopedEVP_MD_CTX ctx;
if (!transcript.CopyToHashContext(ctx.get(), digest) ||
!EVP_DigestUpdate(ctx.get(), truncated.data(),
truncated.size()) ||
!EVP_DigestFinal_ex(ctx.get(), context, &context_len)) {
return false;
}
if (!tls13_verify_data(out, out_len, digest, session->ssl_version, binder_key,
MakeConstSpan(context, context_len))) {
return false;
}
assert(*out_len == EVP_MD_size(digest));
return true;
}
bool tls13_write_psk_binder(const SSL_HANDSHAKE *hs,
const SSLTranscript &transcript, Span<uint8_t> msg,
size_t *out_binder_len) {
const SSL *const ssl = hs->ssl;
const EVP_MD *digest = ssl_session_get_digest(ssl->session.get());
const size_t hash_len = EVP_MD_size(digest);
// We only offer one PSK, so the binders are a u16 and u8 length
// prefix, followed by the binder. The caller is assumed to have constructed
// |msg| with placeholder binders.
const size_t binders_len = 3 + hash_len;
uint8_t verify_data[EVP_MAX_MD_SIZE];
size_t verify_data_len;
if (!tls13_psk_binder(verify_data, &verify_data_len, ssl->session.get(),
transcript, msg, binders_len) ||
verify_data_len != hash_len) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
OPENSSL_memcpy(msg.data() + msg.size() - verify_data_len, verify_data,
verify_data_len);
if (out_binder_len != nullptr) {
*out_binder_len = verify_data_len;
}
return true;
}
bool tls13_verify_psk_binder(const SSL_HANDSHAKE *hs,
const SSL_SESSION *session, const SSLMessage &msg,
CBS *binders) {
uint8_t verify_data[EVP_MAX_MD_SIZE];
size_t verify_data_len;
CBS binder;
// The binders are computed over |msg| with |binders| and its u16 length
// prefix removed. The caller is assumed to have parsed |msg|, extracted
// |binders|, and verified the PSK extension is last.
if (!tls13_psk_binder(verify_data, &verify_data_len, session, hs->transcript,
msg.raw, 2 + CBS_len(binders)) ||
// We only consider the first PSK, so compare against the first binder.
!CBS_get_u8_length_prefixed(binders, &binder)) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
bool binder_ok =
CBS_len(&binder) == verify_data_len &&
CRYPTO_memcmp(CBS_data(&binder), verify_data, verify_data_len) == 0;
#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
binder_ok = true;
#endif
if (!binder_ok) {
OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
return false;
}
return true;
}
size_t ssl_ech_confirmation_signal_hello_offset(const SSL *ssl) {
static_assert(ECH_CONFIRMATION_SIGNAL_LEN < SSL3_RANDOM_SIZE,
"the confirmation signal is a suffix of the random");
const size_t header_len =
SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH;
return header_len + 2 /* version */ + SSL3_RANDOM_SIZE -
ECH_CONFIRMATION_SIGNAL_LEN;
}
bool ssl_ech_accept_confirmation(
const SSL_HANDSHAKE *hs, bssl::Span<uint8_t> out,
const SSLTranscript &transcript,
bssl::Span<const uint8_t> server_hello) {
// We hash |server_hello|, with the last |ECH_CONFIRMATION_SIGNAL_LEN| bytes
// of the random value zeroed.
static const uint8_t kZeroes[ECH_CONFIRMATION_SIGNAL_LEN] = {0};
const size_t offset = ssl_ech_confirmation_signal_hello_offset(hs->ssl);
if (server_hello.size() < offset + ECH_CONFIRMATION_SIGNAL_LEN) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
auto before_zeroes = server_hello.subspan(0, offset);
auto after_zeroes =
server_hello.subspan(offset + ECH_CONFIRMATION_SIGNAL_LEN);
uint8_t context_hash[EVP_MAX_MD_SIZE];
unsigned context_hash_len;
ScopedEVP_MD_CTX ctx;
if (!transcript.CopyToHashContext(ctx.get(), transcript.Digest()) ||
!EVP_DigestUpdate(ctx.get(), before_zeroes.data(),
before_zeroes.size()) ||
!EVP_DigestUpdate(ctx.get(), kZeroes, sizeof(kZeroes)) ||
!EVP_DigestUpdate(ctx.get(), after_zeroes.data(), after_zeroes.size()) ||
!EVP_DigestFinal_ex(ctx.get(), context_hash, &context_hash_len)) {
return false;
}
// Per draft-ietf-tls-esni-10, accept_confirmation is computed with
// Derive-Secret, which derives a secret of size Hash.length. That value is
// then truncated to the first 8 bytes. Note this differs from deriving an
// 8-byte secret because the target length is included in the derivation.
//
// TODO(https://crbug.com/boringssl/275): draft-11 will avoid this.
uint8_t accept_confirmation_buf[EVP_MAX_MD_SIZE];
bssl::Span<uint8_t> accept_confirmation =
MakeSpan(accept_confirmation_buf, transcript.DigestLen());
if (!hkdf_expand_label(accept_confirmation, transcript.Digest(),
hs->secret(), label_to_span("ech accept confirmation"),
MakeConstSpan(context_hash, context_hash_len))) {
return false;
}
static_assert(ECH_CONFIRMATION_SIGNAL_LEN < EVP_MAX_MD_SIZE,
"ECH confirmation signal too big");
if (out.size() != ECH_CONFIRMATION_SIGNAL_LEN) {
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
return false;
}
OPENSSL_memcpy(out.data(), accept_confirmation.data(), out.size());
return true;
}
BSSL_NAMESPACE_END
|