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