| 1 |
// Copyright (c) 2018, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <openssl/bytestring.h> |
| 7 |
#include <openssl/err.h> |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
|
| 11 |
|
| 12 |
BSSL_NAMESPACE_BEGIN |
| 13 |
|
| 14 |
constexpr int kHandoffVersion = 0; |
| 15 |
constexpr int kHandbackVersion = 0; |
| 16 |
|
| 17 |
static const CBS_ASN1_TAG kHandoffTagALPS = CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 18 |
|
| 19 |
// early_data_t represents the state of early data in a more compact way than |
| 20 |
// the 3 bits used by the implementation. |
| 21 |
enum early_data_t { |
| 22 |
early_data_not_offered = 0, |
| 23 |
early_data_accepted = 1, |
| 24 |
early_data_rejected_hrr = 2, |
| 25 |
early_data_skipped = 3, |
| 26 |
|
| 27 |
early_data_max_value = early_data_skipped, |
| 28 |
}; |
| 29 |
|
| 30 |
// serialize_features adds a description of features supported by this binary to |
| 31 |
// |out|. Returns true on success and false on error. |
| 32 |
static bool serialize_features(CBB *out) { |
| 33 |
CBB ciphers; |
| 34 |
if (!CBB_add_asn1(out, &ciphers, CBS_ASN1_OCTETSTRING)) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
Span<const SSL_CIPHER> all_ciphers = AllCiphers(); |
| 38 |
for (const SSL_CIPHER& cipher : all_ciphers) { |
| 39 |
if (!CBB_add_u16(&ciphers, static_cast<uint16_t>(cipher.id))) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
} |
| 43 |
CBB groups; |
| 44 |
if (!CBB_add_asn1(out, &groups, CBS_ASN1_OCTETSTRING)) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
for (const NamedGroup& g : NamedGroups()) { |
| 48 |
if (!CBB_add_u16(&groups, g.group_id)) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
} |
| 52 |
// ALPS is a draft protocol and may change over time. The handoff structure |
| 53 |
// contains a [0] IMPLICIT OCTET STRING OPTIONAL, containing a list of u16 |
| 54 |
// ALPS versions that the binary supports. For now we name them by codepoint. |
| 55 |
// Once ALPS is finalized and past the support horizon, this field can be |
| 56 |
// removed. |
| 57 |
CBB alps; |
| 58 |
if (!CBB_add_asn1(out, &alps, kHandoffTagALPS) || |
| 59 |
!CBB_add_u16(&alps, TLSEXT_TYPE_application_settings_old) || |
| 60 |
!CBB_add_u16(&alps, TLSEXT_TYPE_application_settings)) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
return CBB_flush(out); |
| 64 |
} |
| 65 |
|
| 66 |
bool SSL_serialize_handoff(const SSL *ssl, CBB *out, |
| 67 |
SSL_CLIENT_HELLO *out_hello) { |
| 68 |
const SSL3_STATE *const s3 = ssl->s3; |
| 69 |
if (!ssl->server || |
| 70 |
s3->hs == nullptr || |
| 71 |
s3->rwstate != SSL_ERROR_HANDOFF) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
CBB seq; |
| 76 |
SSLMessage msg; |
| 77 |
Span<const uint8_t> transcript = s3->hs->transcript.buffer(); |
| 78 |
|
| 79 |
if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 80 |
!CBB_add_asn1_uint64(&seq, kHandoffVersion) || |
| 81 |
!CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) || |
| 82 |
!CBB_add_asn1_octet_string(&seq, |
| 83 |
reinterpret_cast<uint8_t *>(s3->hs_buf->data), |
| 84 |
s3->hs_buf->length) || |
| 85 |
!serialize_features(&seq) || |
| 86 |
!CBB_flush(out) || |
| 87 |
!ssl->method->get_message(ssl, &msg) || |
| 88 |
!ssl_client_hello_init(ssl, out_hello, msg.body)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
return true; |
| 93 |
} |
| 94 |
|
| 95 |
bool SSL_decline_handoff(SSL *ssl) { |
| 96 |
const SSL3_STATE *const s3 = ssl->s3; |
| 97 |
if (!ssl->server || |
| 98 |
s3->hs == nullptr || |
| 99 |
s3->rwstate != SSL_ERROR_HANDOFF) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
s3->hs->config->handoff = false; |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
// apply_remote_features reads a list of supported features from |in| and |
| 108 |
// (possibly) reconfigures |ssl| to disallow the negotation of features whose |
| 109 |
// support has not been indicated. (This prevents the the handshake from |
| 110 |
// committing to features that are not supported on the handoff/handback side.) |
| 111 |
static bool apply_remote_features(SSL *ssl, CBS *in) { |
| 112 |
CBS ciphers; |
| 113 |
if (!CBS_get_asn1(in, &ciphers, CBS_ASN1_OCTETSTRING)) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
bssl::UniquePtr<STACK_OF(SSL_CIPHER)> supported(sk_SSL_CIPHER_new_null()); |
| 117 |
if (!supported) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
while (CBS_len(&ciphers)) { |
| 121 |
uint16_t id; |
| 122 |
if (!CBS_get_u16(&ciphers, &id)) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
const SSL_CIPHER *cipher = SSL_get_cipher_by_value(id); |
| 126 |
if (!cipher) { |
| 127 |
continue; |
| 128 |
} |
| 129 |
if (!sk_SSL_CIPHER_push(supported.get(), cipher)) { |
| 130 |
return false; |
| 131 |
} |
| 132 |
} |
| 133 |
STACK_OF(SSL_CIPHER) *configured = |
| 134 |
ssl->config->cipher_list ? ssl->config->cipher_list->ciphers.get() |
| 135 |
: ssl->ctx->cipher_list->ciphers.get(); |
| 136 |
bssl::UniquePtr<STACK_OF(SSL_CIPHER)> unsupported(sk_SSL_CIPHER_new_null()); |
| 137 |
if (!unsupported) { |
| 138 |
return false; |
| 139 |
} |
| 140 |
for (const SSL_CIPHER *configured_cipher : configured) { |
| 141 |
if (sk_SSL_CIPHER_find_awslc(supported.get(), nullptr, configured_cipher)) { |
| 142 |
continue; |
| 143 |
} |
| 144 |
if (!sk_SSL_CIPHER_push(unsupported.get(), configured_cipher)) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
} |
| 148 |
if (sk_SSL_CIPHER_num(unsupported.get()) && !ssl->config->cipher_list) { |
| 149 |
ssl->config->cipher_list = bssl::MakeUnique<SSLCipherPreferenceList>(); |
| 150 |
if (!ssl->config->cipher_list || |
| 151 |
!ssl->config->cipher_list->Init(*ssl->ctx->cipher_list)) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
} |
| 155 |
for (const SSL_CIPHER *unsupported_cipher : unsupported.get()) { |
| 156 |
ssl->config->cipher_list->Remove(unsupported_cipher); |
| 157 |
} |
| 158 |
if (sk_SSL_CIPHER_num(SSL_get_ciphers(ssl)) == 0) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
CBS groups; |
| 163 |
if (!CBS_get_asn1(in, &groups, CBS_ASN1_OCTETSTRING)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
Array<uint16_t> supported_groups; |
| 167 |
if (!supported_groups.Init(CBS_len(&groups) / 2)) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
size_t idx = 0; |
| 171 |
while (CBS_len(&groups)) { |
| 172 |
uint16_t group; |
| 173 |
if (!CBS_get_u16(&groups, &group)) { |
| 174 |
return false; |
| 175 |
} |
| 176 |
supported_groups[idx++] = group; |
| 177 |
} |
| 178 |
Span<const uint16_t> configured_groups = |
| 179 |
tls1_get_grouplist(ssl->s3->hs.get()); |
| 180 |
Array<uint16_t> new_configured_groups; |
| 181 |
if (!new_configured_groups.Init(configured_groups.size())) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
idx = 0; |
| 185 |
for (uint16_t configured_group : configured_groups) { |
| 186 |
bool ok = false; |
| 187 |
for (uint16_t supported_group : supported_groups) { |
| 188 |
if (supported_group == configured_group) { |
| 189 |
ok = true; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
if (ok) { |
| 194 |
new_configured_groups[idx++] = configured_group; |
| 195 |
} |
| 196 |
} |
| 197 |
if (idx == 0) { |
| 198 |
return false; |
| 199 |
} |
| 200 |
new_configured_groups.Shrink(idx); |
| 201 |
ssl->config->supported_group_list = std::move(new_configured_groups); |
| 202 |
|
| 203 |
CBS alps; |
| 204 |
CBS_init(&alps, nullptr, 0); |
| 205 |
if (!CBS_get_optional_asn1(in, &alps, /*out_present=*/nullptr, |
| 206 |
kHandoffTagALPS)) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
bool supports_alps = false; |
| 210 |
while (CBS_len(&alps) != 0) { |
| 211 |
uint16_t id; |
| 212 |
if (!CBS_get_u16(&alps, &id)) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
// For now, we support two ALPS codepoints, so we need to extract both |
| 216 |
// codepoints, and then filter what the handshaker might try to send. |
| 217 |
if ((id == TLSEXT_TYPE_application_settings && |
| 218 |
ssl->config->alps_use_new_codepoint) || |
| 219 |
(id == TLSEXT_TYPE_application_settings_old && |
| 220 |
!ssl->config->alps_use_new_codepoint)) { |
| 221 |
supports_alps = true; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
if (!supports_alps) { |
| 226 |
ssl->config->alps_configs.clear(); |
| 227 |
} |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
// uses_disallowed_feature returns true iff |ssl| enables a feature that |
| 233 |
// disqualifies it for split handshakes. |
| 234 |
static bool uses_disallowed_feature(const SSL *ssl) { |
| 235 |
return ssl->method->is_dtls || (ssl->config->cert && ssl->config->cert->dc) || |
| 236 |
ssl->config->quic_transport_params.size() > 0 || ssl->ctx->ech_keys; |
| 237 |
} |
| 238 |
|
| 239 |
bool SSL_apply_handoff(SSL *ssl, Span<const uint8_t> handoff) { |
| 240 |
if (uses_disallowed_feature(ssl)) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
CBS seq, handoff_cbs(handoff); |
| 245 |
uint64_t handoff_version; |
| 246 |
if (!CBS_get_asn1(&handoff_cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 247 |
!CBS_get_asn1_uint64(&seq, &handoff_version) || |
| 248 |
handoff_version != kHandoffVersion) { |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
CBS transcript, hs_buf; |
| 253 |
if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) || |
| 254 |
!CBS_get_asn1(&seq, &hs_buf, CBS_ASN1_OCTETSTRING) || |
| 255 |
!apply_remote_features(ssl, &seq)) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
SSL_set_accept_state(ssl); |
| 260 |
|
| 261 |
SSL3_STATE *const s3 = ssl->s3; |
| 262 |
s3->v2_hello_done = true; |
| 263 |
s3->has_message = true; |
| 264 |
|
| 265 |
s3->hs_buf.reset(BUF_MEM_new()); |
| 266 |
if (!s3->hs_buf || |
| 267 |
!BUF_MEM_append(s3->hs_buf.get(), CBS_data(&hs_buf), CBS_len(&hs_buf))) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
if (CBS_len(&transcript) != 0) { |
| 272 |
s3->hs->transcript.Update(transcript); |
| 273 |
s3->is_v2_hello = true; |
| 274 |
} |
| 275 |
s3->hs->handback = true; |
| 276 |
|
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
bool SSL_serialize_handback(const SSL *ssl, CBB *out) { |
| 281 |
if (!ssl->server || uses_disallowed_feature(ssl)) { |
| 282 |
return false; |
| 283 |
} |
| 284 |
const SSL3_STATE *const s3 = ssl->s3; |
| 285 |
SSL_HANDSHAKE *const hs = s3->hs.get(); |
| 286 |
handback_t type; |
| 287 |
switch (hs->state) { |
| 288 |
case state12_read_change_cipher_spec: |
| 289 |
type = handback_after_session_resumption; |
| 290 |
break; |
| 291 |
case state12_read_client_certificate: |
| 292 |
type = handback_after_ecdhe; |
| 293 |
break; |
| 294 |
case state12_finish_server_handshake: |
| 295 |
type = handback_after_handshake; |
| 296 |
break; |
| 297 |
case state12_tls13: |
| 298 |
if (hs->tls13_state != state13_send_half_rtt_ticket) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
type = handback_tls13; |
| 302 |
break; |
| 303 |
default: |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
size_t hostname_len = 0; |
| 308 |
if (s3->hostname) { |
| 309 |
hostname_len = strlen(s3->hostname.get()); |
| 310 |
} |
| 311 |
|
| 312 |
Span<const uint8_t> transcript; |
| 313 |
if (type != handback_after_handshake) { |
| 314 |
transcript = s3->hs->transcript.buffer(); |
| 315 |
} |
| 316 |
size_t write_iv_len = 0; |
| 317 |
const uint8_t *write_iv = nullptr; |
| 318 |
if ((type == handback_after_session_resumption || |
| 319 |
type == handback_after_handshake) && |
| 320 |
ssl->version == TLS1_VERSION && |
| 321 |
SSL_CIPHER_is_block_cipher(s3->aead_write_ctx->cipher()) && |
| 322 |
!s3->aead_write_ctx->GetIV(&write_iv, &write_iv_len)) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
size_t read_iv_len = 0; |
| 326 |
const uint8_t *read_iv = nullptr; |
| 327 |
if (type == handback_after_handshake && |
| 328 |
ssl->version == TLS1_VERSION && |
| 329 |
SSL_CIPHER_is_block_cipher(s3->aead_read_ctx->cipher()) && |
| 330 |
!s3->aead_read_ctx->GetIV(&read_iv, &read_iv_len)) { |
| 331 |
return false; |
| 332 |
} |
| 333 |
|
| 334 |
// TODO(mab): make sure everything is serialized. |
| 335 |
CBB seq, key_share; |
| 336 |
const SSL_SESSION *session; |
| 337 |
if (type == handback_tls13) { |
| 338 |
session = hs->new_session.get(); |
| 339 |
} else { |
| 340 |
session = s3->session_reused ? ssl->session.get() : hs->new_session.get(); |
| 341 |
} |
| 342 |
static const uint8_t kUnusedChannelID[64] = {0}; |
| 343 |
if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 344 |
!CBB_add_asn1_uint64(&seq, kHandbackVersion) || |
| 345 |
!CBB_add_asn1_uint64(&seq, type) || |
| 346 |
!CBB_add_asn1_octet_string(&seq, s3->read_sequence, |
| 347 |
sizeof(s3->read_sequence)) || |
| 348 |
!CBB_add_asn1_octet_string(&seq, s3->write_sequence, |
| 349 |
sizeof(s3->write_sequence)) || |
| 350 |
!CBB_add_asn1_octet_string(&seq, s3->server_random, |
| 351 |
sizeof(s3->server_random)) || |
| 352 |
!CBB_add_asn1_octet_string(&seq, s3->client_random, |
| 353 |
sizeof(s3->client_random)) || |
| 354 |
!CBB_add_asn1_octet_string(&seq, read_iv, read_iv_len) || |
| 355 |
!CBB_add_asn1_octet_string(&seq, write_iv, write_iv_len) || |
| 356 |
!CBB_add_asn1_bool(&seq, s3->session_reused) || |
| 357 |
!CBB_add_asn1_bool(&seq, hs->channel_id_negotiated) || |
| 358 |
!ssl_session_serialize(session, &seq) || |
| 359 |
!CBB_add_asn1_octet_string(&seq, s3->next_proto_negotiated.data(), |
| 360 |
s3->next_proto_negotiated.size()) || |
| 361 |
!CBB_add_asn1_octet_string(&seq, s3->alpn_selected.data(), |
| 362 |
s3->alpn_selected.size()) || |
| 363 |
!CBB_add_asn1_octet_string( |
| 364 |
&seq, reinterpret_cast<uint8_t *>(s3->hostname.get()), |
| 365 |
hostname_len) || |
| 366 |
!CBB_add_asn1_octet_string(&seq, kUnusedChannelID, |
| 367 |
sizeof(kUnusedChannelID)) || |
| 368 |
// These two fields were historically |token_binding_negotiated| and |
| 369 |
// |negotiated_token_binding_param|. |
| 370 |
!CBB_add_asn1_bool(&seq, 0) || |
| 371 |
!CBB_add_asn1_uint64(&seq, 0) || |
| 372 |
!CBB_add_asn1_bool(&seq, s3->hs->next_proto_neg_seen) || |
| 373 |
!CBB_add_asn1_bool(&seq, s3->hs->cert_request) || |
| 374 |
!CBB_add_asn1_bool(&seq, s3->hs->extended_master_secret) || |
| 375 |
!CBB_add_asn1_bool(&seq, s3->hs->ticket_expected) || |
| 376 |
!CBB_add_asn1_uint64(&seq, SSL_CIPHER_get_id(s3->hs->new_cipher)) || |
| 377 |
!CBB_add_asn1_octet_string(&seq, transcript.data(), transcript.size()) || |
| 378 |
!CBB_add_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) { |
| 379 |
return false; |
| 380 |
} |
| 381 |
if (type == handback_after_ecdhe) { |
| 382 |
CBB private_key; |
| 383 |
if (!CBB_add_asn1_uint64(&key_share, s3->hs->key_shares[0]->GroupID()) || |
| 384 |
!CBB_add_asn1(&key_share, &private_key, CBS_ASN1_OCTETSTRING) || |
| 385 |
!s3->hs->key_shares[0]->SerializePrivateKey(&private_key) || |
| 386 |
!CBB_flush(&key_share)) { |
| 387 |
return false; |
| 388 |
} |
| 389 |
} |
| 390 |
if (type == handback_tls13) { |
| 391 |
early_data_t early_data; |
| 392 |
// Check early data invariants. |
| 393 |
if (ssl->enable_early_data == |
| 394 |
(s3->early_data_reason == ssl_early_data_disabled)) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
if (hs->early_data_offered) { |
| 398 |
if (s3->early_data_accepted && !s3->skip_early_data) { |
| 399 |
early_data = early_data_accepted; |
| 400 |
} else if (!s3->early_data_accepted && !s3->skip_early_data) { |
| 401 |
early_data = early_data_rejected_hrr; |
| 402 |
} else if (!s3->early_data_accepted && s3->skip_early_data) { |
| 403 |
early_data = early_data_skipped; |
| 404 |
} else { |
| 405 |
return false; |
| 406 |
} |
| 407 |
} else if (!s3->early_data_accepted && !s3->skip_early_data) { |
| 408 |
early_data = early_data_not_offered; |
| 409 |
} else { |
| 410 |
return false; |
| 411 |
} |
| 412 |
if (!CBB_add_asn1_octet_string(&seq, hs->client_traffic_secret_0().data(), |
| 413 |
hs->client_traffic_secret_0().size()) || |
| 414 |
!CBB_add_asn1_octet_string(&seq, hs->server_traffic_secret_0().data(), |
| 415 |
hs->server_traffic_secret_0().size()) || |
| 416 |
!CBB_add_asn1_octet_string(&seq, hs->client_handshake_secret().data(), |
| 417 |
hs->client_handshake_secret().size()) || |
| 418 |
!CBB_add_asn1_octet_string(&seq, hs->server_handshake_secret().data(), |
| 419 |
hs->server_handshake_secret().size()) || |
| 420 |
!CBB_add_asn1_octet_string(&seq, hs->secret().data(), |
| 421 |
hs->secret().size()) || |
| 422 |
!CBB_add_asn1_octet_string(&seq, s3->exporter_secret, |
| 423 |
s3->exporter_secret_len) || |
| 424 |
!CBB_add_asn1_bool(&seq, s3->used_hello_retry_request) || |
| 425 |
!CBB_add_asn1_bool(&seq, hs->accept_psk_mode) || |
| 426 |
!CBB_add_asn1_int64(&seq, s3->ticket_age_skew) || |
| 427 |
!CBB_add_asn1_uint64(&seq, s3->early_data_reason) || |
| 428 |
!CBB_add_asn1_uint64(&seq, early_data)) { |
| 429 |
return false; |
| 430 |
} |
| 431 |
if (early_data == early_data_accepted && |
| 432 |
!CBB_add_asn1_octet_string(&seq, hs->early_traffic_secret().data(), |
| 433 |
hs->early_traffic_secret().size())) { |
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
if (session->has_application_settings) { |
| 438 |
uint16_t alps_codepoint = TLSEXT_TYPE_application_settings_old; |
| 439 |
if (hs->config->alps_use_new_codepoint) { |
| 440 |
alps_codepoint = TLSEXT_TYPE_application_settings; |
| 441 |
} |
| 442 |
if (!CBB_add_asn1_uint64(&seq, alps_codepoint)) { |
| 443 |
return false; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
return CBB_flush(out); |
| 448 |
} |
| 449 |
|
| 450 |
static bool CopyExact(Span<uint8_t> out, const CBS *in) { |
| 451 |
if (CBS_len(in) != out.size()) { |
| 452 |
return false; |
| 453 |
} |
| 454 |
OPENSSL_memcpy(out.data(), CBS_data(in), out.size()); |
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
bool SSL_apply_handback(SSL *ssl, Span<const uint8_t> handback) { |
| 459 |
if (ssl->do_handshake != nullptr || |
| 460 |
ssl->method->is_dtls) { |
| 461 |
return false; |
| 462 |
} |
| 463 |
|
| 464 |
SSL3_STATE *const s3 = ssl->s3; |
| 465 |
uint64_t handback_version, unused_token_binding_param, cipher, type_u64, |
| 466 |
alps_codepoint; |
| 467 |
|
| 468 |
CBS seq, read_seq, write_seq, server_rand, client_rand, read_iv, write_iv, |
| 469 |
next_proto, alpn, hostname, unused_channel_id, transcript, key_share; |
| 470 |
int session_reused, channel_id_negotiated, cert_request, |
| 471 |
extended_master_secret, ticket_expected, unused_token_binding, |
| 472 |
next_proto_neg_seen; |
| 473 |
SSL_SESSION *session = nullptr; |
| 474 |
|
| 475 |
CBS handback_cbs(handback); |
| 476 |
if (!CBS_get_asn1(&handback_cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 477 |
!CBS_get_asn1_uint64(&seq, &handback_version) || |
| 478 |
handback_version != kHandbackVersion || |
| 479 |
!CBS_get_asn1_uint64(&seq, &type_u64) || |
| 480 |
type_u64 > handback_max_value) { |
| 481 |
return false; |
| 482 |
} |
| 483 |
|
| 484 |
handback_t type = static_cast<handback_t>(type_u64); |
| 485 |
if (!CBS_get_asn1(&seq, &read_seq, CBS_ASN1_OCTETSTRING) || |
| 486 |
CBS_len(&read_seq) != sizeof(s3->read_sequence) || |
| 487 |
!CBS_get_asn1(&seq, &write_seq, CBS_ASN1_OCTETSTRING) || |
| 488 |
CBS_len(&write_seq) != sizeof(s3->write_sequence) || |
| 489 |
!CBS_get_asn1(&seq, &server_rand, CBS_ASN1_OCTETSTRING) || |
| 490 |
CBS_len(&server_rand) != sizeof(s3->server_random) || |
| 491 |
!CBS_copy_bytes(&server_rand, s3->server_random, |
| 492 |
sizeof(s3->server_random)) || |
| 493 |
!CBS_get_asn1(&seq, &client_rand, CBS_ASN1_OCTETSTRING) || |
| 494 |
CBS_len(&client_rand) != sizeof(s3->client_random) || |
| 495 |
!CBS_copy_bytes(&client_rand, s3->client_random, |
| 496 |
sizeof(s3->client_random)) || |
| 497 |
!CBS_get_asn1(&seq, &read_iv, CBS_ASN1_OCTETSTRING) || |
| 498 |
!CBS_get_asn1(&seq, &write_iv, CBS_ASN1_OCTETSTRING) || |
| 499 |
!CBS_get_asn1_bool(&seq, &session_reused) || |
| 500 |
!CBS_get_asn1_bool(&seq, &channel_id_negotiated)) { |
| 501 |
return false; |
| 502 |
} |
| 503 |
|
| 504 |
s3->hs = ssl_handshake_new(ssl); |
| 505 |
if (!s3->hs) { |
| 506 |
return false; |
| 507 |
} |
| 508 |
SSL_HANDSHAKE *const hs = s3->hs.get(); |
| 509 |
if (!session_reused || type == handback_tls13) { |
| 510 |
hs->new_session = |
| 511 |
SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool); |
| 512 |
session = hs->new_session.get(); |
| 513 |
} else { |
| 514 |
ssl->session = |
| 515 |
SSL_SESSION_parse(&seq, ssl->ctx->x509_method, ssl->ctx->pool); |
| 516 |
session = ssl->session.get(); |
| 517 |
} |
| 518 |
|
| 519 |
if (!session || !CBS_get_asn1(&seq, &next_proto, CBS_ASN1_OCTETSTRING) || |
| 520 |
!CBS_get_asn1(&seq, &alpn, CBS_ASN1_OCTETSTRING) || |
| 521 |
!CBS_get_asn1(&seq, &hostname, CBS_ASN1_OCTETSTRING) || |
| 522 |
!CBS_get_asn1(&seq, &unused_channel_id, CBS_ASN1_OCTETSTRING) || |
| 523 |
!CBS_get_asn1_bool(&seq, &unused_token_binding) || |
| 524 |
!CBS_get_asn1_uint64(&seq, &unused_token_binding_param) || |
| 525 |
!CBS_get_asn1_bool(&seq, &next_proto_neg_seen) || |
| 526 |
!CBS_get_asn1_bool(&seq, &cert_request) || |
| 527 |
!CBS_get_asn1_bool(&seq, &extended_master_secret) || |
| 528 |
!CBS_get_asn1_bool(&seq, &ticket_expected) || |
| 529 |
!CBS_get_asn1_uint64(&seq, &cipher)) { |
| 530 |
return false; |
| 531 |
} |
| 532 |
if ((hs->new_cipher = |
| 533 |
SSL_get_cipher_by_value(static_cast<uint16_t>(cipher))) == nullptr) { |
| 534 |
return false; |
| 535 |
} |
| 536 |
if (!CBS_get_asn1(&seq, &transcript, CBS_ASN1_OCTETSTRING) || |
| 537 |
!CBS_get_asn1(&seq, &key_share, CBS_ASN1_SEQUENCE)) { |
| 538 |
return false; |
| 539 |
} |
| 540 |
CBS client_handshake_secret, server_handshake_secret, client_traffic_secret_0, |
| 541 |
server_traffic_secret_0, secret, exporter_secret, early_traffic_secret; |
| 542 |
if (type == handback_tls13) { |
| 543 |
int used_hello_retry_request, accept_psk_mode; |
| 544 |
uint64_t early_data, early_data_reason; |
| 545 |
int64_t ticket_age_skew; |
| 546 |
if (!CBS_get_asn1(&seq, &client_traffic_secret_0, CBS_ASN1_OCTETSTRING) || |
| 547 |
!CBS_get_asn1(&seq, &server_traffic_secret_0, CBS_ASN1_OCTETSTRING) || |
| 548 |
!CBS_get_asn1(&seq, &client_handshake_secret, CBS_ASN1_OCTETSTRING) || |
| 549 |
!CBS_get_asn1(&seq, &server_handshake_secret, CBS_ASN1_OCTETSTRING) || |
| 550 |
!CBS_get_asn1(&seq, &secret, CBS_ASN1_OCTETSTRING) || |
| 551 |
!CBS_get_asn1(&seq, &exporter_secret, CBS_ASN1_OCTETSTRING) || |
| 552 |
!CBS_get_asn1_bool(&seq, &used_hello_retry_request) || |
| 553 |
!CBS_get_asn1_bool(&seq, &accept_psk_mode) || |
| 554 |
!CBS_get_asn1_int64(&seq, &ticket_age_skew) || |
| 555 |
!CBS_get_asn1_uint64(&seq, &early_data_reason) || |
| 556 |
early_data_reason > ssl_early_data_reason_max_value || |
| 557 |
!CBS_get_asn1_uint64(&seq, &early_data) || |
| 558 |
early_data > early_data_max_value) { |
| 559 |
return false; |
| 560 |
} |
| 561 |
early_data_t early_data_type = static_cast<early_data_t>(early_data); |
| 562 |
if (early_data_type == early_data_accepted && |
| 563 |
!CBS_get_asn1(&seq, &early_traffic_secret, CBS_ASN1_OCTETSTRING)) { |
| 564 |
return false; |
| 565 |
} |
| 566 |
|
| 567 |
if (session->has_application_settings) { |
| 568 |
// Making it optional to keep compatibility with older handshakers. |
| 569 |
// Older handshakers won't send the field. |
| 570 |
if (CBS_len(&seq) == 0) { |
| 571 |
hs->config->alps_use_new_codepoint = false; |
| 572 |
} else { |
| 573 |
if (!CBS_get_asn1_uint64(&seq, &alps_codepoint)) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
if (alps_codepoint == TLSEXT_TYPE_application_settings) { |
| 578 |
hs->config->alps_use_new_codepoint = true; |
| 579 |
} else if (alps_codepoint == TLSEXT_TYPE_application_settings_old) { |
| 580 |
hs->config->alps_use_new_codepoint = false; |
| 581 |
} else { |
| 582 |
OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPS_CODEPOINT); |
| 583 |
return false; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (ticket_age_skew > std::numeric_limits<int32_t>::max() || |
| 589 |
ticket_age_skew < std::numeric_limits<int32_t>::min()) { |
| 590 |
return false; |
| 591 |
} |
| 592 |
s3->ticket_age_skew = static_cast<int32_t>(ticket_age_skew); |
| 593 |
s3->used_hello_retry_request = used_hello_retry_request; |
| 594 |
hs->accept_psk_mode = accept_psk_mode; |
| 595 |
|
| 596 |
s3->early_data_reason = |
| 597 |
static_cast<ssl_early_data_reason_t>(early_data_reason); |
| 598 |
ssl->enable_early_data = s3->early_data_reason != ssl_early_data_disabled; |
| 599 |
s3->skip_early_data = false; |
| 600 |
s3->early_data_accepted = false; |
| 601 |
hs->early_data_offered = false; |
| 602 |
switch (early_data_type) { |
| 603 |
case early_data_not_offered: |
| 604 |
break; |
| 605 |
case early_data_accepted: |
| 606 |
s3->early_data_accepted = true; |
| 607 |
hs->early_data_offered = true; |
| 608 |
hs->can_early_write = true; |
| 609 |
hs->can_early_read = true; |
| 610 |
hs->in_early_data = true; |
| 611 |
break; |
| 612 |
case early_data_rejected_hrr: |
| 613 |
hs->early_data_offered = true; |
| 614 |
break; |
| 615 |
case early_data_skipped: |
| 616 |
s3->skip_early_data = true; |
| 617 |
hs->early_data_offered = true; |
| 618 |
break; |
| 619 |
default: |
| 620 |
return false; |
| 621 |
} |
| 622 |
} else { |
| 623 |
s3->early_data_reason = ssl_early_data_protocol_version; |
| 624 |
} |
| 625 |
|
| 626 |
ssl->version = session->ssl_version; |
| 627 |
ssl->verify_result = session->verify_result; |
| 628 |
s3->have_version = true; |
| 629 |
if (!ssl_method_supports_version(ssl->method, ssl->version) || |
| 630 |
session->cipher != hs->new_cipher || |
| 631 |
ssl_protocol_version(ssl) < SSL_CIPHER_get_min_version(session->cipher) || |
| 632 |
SSL_CIPHER_get_max_version(session->cipher) < ssl_protocol_version(ssl)) { |
| 633 |
return false; |
| 634 |
} |
| 635 |
ssl->do_handshake = ssl_server_handshake; |
| 636 |
ssl->server = true; |
| 637 |
switch (type) { |
| 638 |
case handback_after_session_resumption: |
| 639 |
hs->state = state12_read_change_cipher_spec; |
| 640 |
if (!session_reused) { |
| 641 |
return false; |
| 642 |
} |
| 643 |
break; |
| 644 |
case handback_after_ecdhe: |
| 645 |
hs->state = state12_read_client_certificate; |
| 646 |
if (session_reused) { |
| 647 |
return false; |
| 648 |
} |
| 649 |
break; |
| 650 |
case handback_after_handshake: |
| 651 |
hs->state = state12_finish_server_handshake; |
| 652 |
break; |
| 653 |
case handback_tls13: |
| 654 |
hs->state = state12_tls13; |
| 655 |
hs->tls13_state = state13_send_half_rtt_ticket; |
| 656 |
break; |
| 657 |
default: |
| 658 |
return false; |
| 659 |
} |
| 660 |
s3->session_reused = session_reused; |
| 661 |
hs->channel_id_negotiated = channel_id_negotiated; |
| 662 |
if (!s3->next_proto_negotiated.CopyFrom(next_proto) || |
| 663 |
!s3->alpn_selected.CopyFrom(alpn)) { |
| 664 |
return false; |
| 665 |
} |
| 666 |
|
| 667 |
const size_t hostname_len = CBS_len(&hostname); |
| 668 |
if (hostname_len == 0) { |
| 669 |
s3->hostname.reset(); |
| 670 |
} else { |
| 671 |
char *hostname_str = nullptr; |
| 672 |
if (!CBS_strdup(&hostname, &hostname_str)) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
s3->hostname.reset(hostname_str); |
| 676 |
} |
| 677 |
|
| 678 |
hs->next_proto_neg_seen = next_proto_neg_seen; |
| 679 |
hs->wait = ssl_hs_flush; |
| 680 |
hs->extended_master_secret = extended_master_secret; |
| 681 |
hs->ticket_expected = ticket_expected; |
| 682 |
s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
| 683 |
hs->cert_request = cert_request; |
| 684 |
|
| 685 |
if (type != handback_after_handshake && |
| 686 |
(!hs->transcript.Init() || |
| 687 |
!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
| 688 |
!hs->transcript.Update(transcript))) { |
| 689 |
return false; |
| 690 |
} |
| 691 |
if (type == handback_tls13) { |
| 692 |
hs->ResizeSecrets(hs->transcript.DigestLen()); |
| 693 |
if (!CopyExact(hs->client_traffic_secret_0(), &client_traffic_secret_0) || |
| 694 |
!CopyExact(hs->server_traffic_secret_0(), &server_traffic_secret_0) || |
| 695 |
!CopyExact(hs->client_handshake_secret(), &client_handshake_secret) || |
| 696 |
!CopyExact(hs->server_handshake_secret(), &server_handshake_secret) || |
| 697 |
!CopyExact(hs->secret(), &secret) || |
| 698 |
!CopyExact({s3->exporter_secret, hs->transcript.DigestLen()}, |
| 699 |
&exporter_secret)) { |
| 700 |
return false; |
| 701 |
} |
| 702 |
s3->exporter_secret_len = CBS_len(&exporter_secret); |
| 703 |
|
| 704 |
if (s3->early_data_accepted && |
| 705 |
!CopyExact(hs->early_traffic_secret(), &early_traffic_secret)) { |
| 706 |
return false; |
| 707 |
} |
| 708 |
} |
| 709 |
Array<uint8_t> key_block; |
| 710 |
switch (type) { |
| 711 |
case handback_after_session_resumption: |
| 712 |
// The write keys are installed after server Finished, but the client |
| 713 |
// keys must wait for ChangeCipherSpec. |
| 714 |
if (!tls1_configure_aead(ssl, evp_aead_seal, &key_block, session, |
| 715 |
write_iv)) { |
| 716 |
return false; |
| 717 |
} |
| 718 |
break; |
| 719 |
case handback_after_ecdhe: |
| 720 |
// The premaster secret is not yet computed, so install no keys. |
| 721 |
break; |
| 722 |
case handback_after_handshake: |
| 723 |
// The handshake is complete, so both keys are installed. |
| 724 |
if (!tls1_configure_aead(ssl, evp_aead_seal, &key_block, session, |
| 725 |
write_iv) || |
| 726 |
!tls1_configure_aead(ssl, evp_aead_open, &key_block, session, |
| 727 |
read_iv)) { |
| 728 |
return false; |
| 729 |
} |
| 730 |
break; |
| 731 |
case handback_tls13: |
| 732 |
// After server Finished, the application write keys are installed, but |
| 733 |
// none of the read keys. The read keys are installed in the state machine |
| 734 |
// immediately after processing handback. |
| 735 |
if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
| 736 |
hs->new_session.get(), |
| 737 |
hs->server_traffic_secret_0())) { |
| 738 |
return false; |
| 739 |
} |
| 740 |
break; |
| 741 |
} |
| 742 |
if (!CopyExact({s3->read_sequence, sizeof(s3->read_sequence)}, &read_seq) || |
| 743 |
!CopyExact({s3->write_sequence, sizeof(s3->write_sequence)}, |
| 744 |
&write_seq)) { |
| 745 |
return false; |
| 746 |
} |
| 747 |
if (type == handback_after_ecdhe) { |
| 748 |
uint64_t group_id; |
| 749 |
CBS private_key; |
| 750 |
if (!CBS_get_asn1_uint64(&key_share, &group_id) || // |
| 751 |
group_id > 0xffff || |
| 752 |
!CBS_get_asn1(&key_share, &private_key, CBS_ASN1_OCTETSTRING)) { |
| 753 |
return false; |
| 754 |
} |
| 755 |
hs->key_shares[0] = SSLKeyShare::Create(group_id); |
| 756 |
if (!hs->key_shares[0] || |
| 757 |
!hs->key_shares[0]->DeserializePrivateKey(&private_key)) { |
| 758 |
return false; |
| 759 |
} |
| 760 |
} |
| 761 |
return true; // Trailing data allowed for extensibility. |
| 762 |
} |
| 763 |
|
| 764 |
BSSL_NAMESPACE_END |
| 765 |
|
| 766 |
using namespace bssl; |
| 767 |
|
| 768 |
int SSL_serialize_capabilities(const SSL *ssl, CBB *out) { |
| 769 |
CBB seq; |
| 770 |
if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 771 |
!serialize_features(&seq) || // |
| 772 |
!CBB_flush(out)) { |
| 773 |
return 0; |
| 774 |
} |
| 775 |
|
| 776 |
return 1; |
| 777 |
} |
| 778 |
|
| 779 |
int SSL_request_handshake_hints(SSL *ssl, const uint8_t *client_hello, |
| 780 |
size_t client_hello_len, |
| 781 |
const uint8_t *capabilities, |
| 782 |
size_t capabilities_len) { |
| 783 |
if (SSL_is_dtls(ssl)) { |
| 784 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 785 |
return 0; |
| 786 |
} |
| 787 |
|
| 788 |
CBS cbs, seq; |
| 789 |
CBS_init(&cbs, capabilities, capabilities_len); |
| 790 |
UniquePtr<SSL_HANDSHAKE_HINTS> hints = MakeUnique<SSL_HANDSHAKE_HINTS>(); |
| 791 |
if (hints == nullptr || |
| 792 |
!CBS_get_asn1(&cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 793 |
!apply_remote_features(ssl, &seq)) { |
| 794 |
return 0; |
| 795 |
} |
| 796 |
|
| 797 |
SSL3_STATE *const s3 = ssl->s3; |
| 798 |
s3->v2_hello_done = true; |
| 799 |
s3->has_message = true; |
| 800 |
|
| 801 |
Array<uint8_t> client_hello_msg; |
| 802 |
ScopedCBB client_hello_cbb; |
| 803 |
CBB client_hello_body; |
| 804 |
if (!ssl->method->init_message(ssl, client_hello_cbb.get(), |
| 805 |
&client_hello_body, SSL3_MT_CLIENT_HELLO) || |
| 806 |
!CBB_add_bytes(&client_hello_body, client_hello, client_hello_len) || |
| 807 |
!ssl->method->finish_message(ssl, client_hello_cbb.get(), |
| 808 |
&client_hello_msg)) { |
| 809 |
return 0; |
| 810 |
} |
| 811 |
|
| 812 |
s3->hs_buf.reset(BUF_MEM_new()); |
| 813 |
if (!s3->hs_buf || !BUF_MEM_append(s3->hs_buf.get(), client_hello_msg.data(), |
| 814 |
client_hello_msg.size())) { |
| 815 |
return 0; |
| 816 |
} |
| 817 |
|
| 818 |
s3->hs->hints_requested = true; |
| 819 |
s3->hs->hints = std::move(hints); |
| 820 |
return 1; |
| 821 |
} |
| 822 |
|
| 823 |
// |SSL_HANDSHAKE_HINTS| is serialized as the following ASN.1 structure. We use |
| 824 |
// implicit tagging to make it a little more compact. |
| 825 |
// |
| 826 |
// HandshakeHints ::= SEQUENCE { |
| 827 |
// serverRandomTLS13 [0] IMPLICIT OCTET STRING OPTIONAL, |
| 828 |
// keyShareHint [1] IMPLICIT KeyShareHint OPTIONAL, |
| 829 |
// signatureHint [2] IMPLICIT SignatureHint OPTIONAL, |
| 830 |
// -- At most one of decryptedPSKHint or ignorePSKHint may be present. It |
| 831 |
// -- corresponds to the first entry in pre_shared_keys. TLS 1.2 session |
| 832 |
// -- tickets use a separate hint, to ensure the caller does not apply the |
| 833 |
// -- hint to the wrong field. |
| 834 |
// decryptedPSKHint [3] IMPLICIT OCTET STRING OPTIONAL, |
| 835 |
// ignorePSKHint [4] IMPLICIT NULL OPTIONAL, |
| 836 |
// compressCertificateHint [5] IMPLICIT CompressCertificateHint OPTIONAL, |
| 837 |
// -- TLS 1.2 and 1.3 use different server random hints because one contains |
| 838 |
// -- a timestamp while the other doesn't. If the hint was generated |
| 839 |
// -- assuming TLS 1.3 but we actually negotiate TLS 1.2, mixing the two |
| 840 |
// -- will break this. |
| 841 |
// serverRandomTLS12 [6] IMPLICIT OCTET STRING OPTIONAL, |
| 842 |
// ecdheHint [7] IMPLICIT ECDHEHint OPTIONAL |
| 843 |
// -- At most one of decryptedTicketHint or ignoreTicketHint may be present. |
| 844 |
// -- renewTicketHint requires decryptedTicketHint. |
| 845 |
// decryptedTicketHint [8] IMPLICIT OCTET STRING OPTIONAL, |
| 846 |
// renewTicketHint [9] IMPLICIT NULL OPTIONAL, |
| 847 |
// ignoreTicketHint [10] IMPLICIT NULL OPTIONAL, |
| 848 |
// } |
| 849 |
// |
| 850 |
// KeyShareHint ::= SEQUENCE { |
| 851 |
// groupId INTEGER, |
| 852 |
// publicKey OCTET STRING, |
| 853 |
// secret OCTET STRING, |
| 854 |
// } |
| 855 |
// |
| 856 |
// SignatureHint ::= SEQUENCE { |
| 857 |
// algorithm INTEGER, |
| 858 |
// input OCTET STRING, |
| 859 |
// subjectPublicKeyInfo OCTET STRING, |
| 860 |
// signature OCTET STRING, |
| 861 |
// } |
| 862 |
// |
| 863 |
// CompressCertificateHint ::= SEQUENCE { |
| 864 |
// algorithm INTEGER, |
| 865 |
// input OCTET STRING, |
| 866 |
// compressed OCTET STRING, |
| 867 |
// } |
| 868 |
// |
| 869 |
// ECDHEHint ::= SEQUENCE { |
| 870 |
// groupId INTEGER, |
| 871 |
// publicKey OCTET STRING, |
| 872 |
// privateKey OCTET STRING, |
| 873 |
// } |
| 874 |
|
| 875 |
// HandshakeHints tags. |
| 876 |
static const CBS_ASN1_TAG kServerRandomTLS13Tag = |
| 877 |
CBS_ASN1_CONTEXT_SPECIFIC | 0; |
| 878 |
static const CBS_ASN1_TAG kKeyShareHintTag = |
| 879 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 1; |
| 880 |
static const CBS_ASN1_TAG kSignatureHintTag = |
| 881 |
CBS_ASN1_CONSTRUCTED | CBS_ASN1_CONTEXT_SPECIFIC | 2; |
| 882 |
static const CBS_ASN1_TAG kDecryptedPSKTag = CBS_ASN1_CONTEXT_SPECIFIC | 3; |
| 883 |
static const CBS_ASN1_TAG kIgnorePSKTag = CBS_ASN1_CONTEXT_SPECIFIC | 4; |
| 884 |
static const CBS_ASN1_TAG kCompressCertificateTag = |
| 885 |
CBS_ASN1_CONTEXT_SPECIFIC | 5; |
| 886 |
static const CBS_ASN1_TAG kServerRandomTLS12Tag = |
| 887 |
CBS_ASN1_CONTEXT_SPECIFIC | 6; |
| 888 |
static const CBS_ASN1_TAG kECDHEHintTag = CBS_ASN1_CONSTRUCTED | 7; |
| 889 |
static const CBS_ASN1_TAG kDecryptedTicketTag = CBS_ASN1_CONTEXT_SPECIFIC | 8; |
| 890 |
static const CBS_ASN1_TAG kRenewTicketTag = CBS_ASN1_CONTEXT_SPECIFIC | 9; |
| 891 |
static const CBS_ASN1_TAG kIgnoreTicketTag = CBS_ASN1_CONTEXT_SPECIFIC | 10; |
| 892 |
|
| 893 |
int SSL_serialize_handshake_hints(const SSL *ssl, CBB *out) { |
| 894 |
const SSL_HANDSHAKE *hs = ssl->s3->hs.get(); |
| 895 |
if (!ssl->server || !hs->hints_requested) { |
| 896 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 897 |
return 0; |
| 898 |
} |
| 899 |
|
| 900 |
const SSL_HANDSHAKE_HINTS *hints = hs->hints.get(); |
| 901 |
CBB seq, child; |
| 902 |
if (!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE)) { |
| 903 |
return 0; |
| 904 |
} |
| 905 |
|
| 906 |
if (!hints->server_random_tls13.empty()) { |
| 907 |
if (!CBB_add_asn1(&seq, &child, kServerRandomTLS13Tag) || |
| 908 |
!CBB_add_bytes(&child, hints->server_random_tls13.data(), |
| 909 |
hints->server_random_tls13.size())) { |
| 910 |
return 0; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
if (hints->key_share_group_id != 0 && !hints->key_share_public_key.empty() && |
| 915 |
!hints->key_share_secret.empty()) { |
| 916 |
if (!CBB_add_asn1(&seq, &child, kKeyShareHintTag) || |
| 917 |
!CBB_add_asn1_uint64(&child, hints->key_share_group_id) || |
| 918 |
!CBB_add_asn1_octet_string(&child, hints->key_share_public_key.data(), |
| 919 |
hints->key_share_public_key.size()) || |
| 920 |
!CBB_add_asn1_octet_string(&child, hints->key_share_secret.data(), |
| 921 |
hints->key_share_secret.size())) { |
| 922 |
return 0; |
| 923 |
} |
| 924 |
} |
| 925 |
|
| 926 |
if (hints->signature_algorithm != 0 && !hints->signature_input.empty() && |
| 927 |
!hints->signature.empty()) { |
| 928 |
if (!CBB_add_asn1(&seq, &child, kSignatureHintTag) || |
| 929 |
!CBB_add_asn1_uint64(&child, hints->signature_algorithm) || |
| 930 |
!CBB_add_asn1_octet_string(&child, hints->signature_input.data(), |
| 931 |
hints->signature_input.size()) || |
| 932 |
!CBB_add_asn1_octet_string(&child, hints->signature_spki.data(), |
| 933 |
hints->signature_spki.size()) || |
| 934 |
!CBB_add_asn1_octet_string(&child, hints->signature.data(), |
| 935 |
hints->signature.size())) { |
| 936 |
return 0; |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
if (!hints->decrypted_psk.empty()) { |
| 941 |
if (!CBB_add_asn1(&seq, &child, kDecryptedPSKTag) || |
| 942 |
!CBB_add_bytes(&child, hints->decrypted_psk.data(), |
| 943 |
hints->decrypted_psk.size())) { |
| 944 |
return 0; |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
if (hints->ignore_psk && // |
| 949 |
!CBB_add_asn1(&seq, &child, kIgnorePSKTag)) { |
| 950 |
return 0; |
| 951 |
} |
| 952 |
|
| 953 |
if (hints->cert_compression_alg_id != 0 && |
| 954 |
!hints->cert_compression_input.empty() && |
| 955 |
!hints->cert_compression_output.empty()) { |
| 956 |
if (!CBB_add_asn1(&seq, &child, kCompressCertificateTag) || |
| 957 |
!CBB_add_asn1_uint64(&child, hints->cert_compression_alg_id) || |
| 958 |
!CBB_add_asn1_octet_string(&child, hints->cert_compression_input.data(), |
| 959 |
hints->cert_compression_input.size()) || |
| 960 |
!CBB_add_asn1_octet_string(&child, |
| 961 |
hints->cert_compression_output.data(), |
| 962 |
hints->cert_compression_output.size())) { |
| 963 |
return 0; |
| 964 |
} |
| 965 |
} |
| 966 |
|
| 967 |
if (!hints->server_random_tls12.empty()) { |
| 968 |
if (!CBB_add_asn1(&seq, &child, kServerRandomTLS12Tag) || |
| 969 |
!CBB_add_bytes(&child, hints->server_random_tls12.data(), |
| 970 |
hints->server_random_tls12.size())) { |
| 971 |
return 0; |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
if (hints->ecdhe_group_id != 0 && !hints->ecdhe_public_key.empty() && |
| 976 |
!hints->ecdhe_private_key.empty()) { |
| 977 |
if (!CBB_add_asn1(&seq, &child, kECDHEHintTag) || |
| 978 |
!CBB_add_asn1_uint64(&child, hints->ecdhe_group_id) || |
| 979 |
!CBB_add_asn1_octet_string(&child, hints->ecdhe_public_key.data(), |
| 980 |
hints->ecdhe_public_key.size()) || |
| 981 |
!CBB_add_asn1_octet_string(&child, hints->ecdhe_private_key.data(), |
| 982 |
hints->ecdhe_private_key.size())) { |
| 983 |
return 0; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
if (!hints->decrypted_ticket.empty()) { |
| 989 |
if (!CBB_add_asn1(&seq, &child, kDecryptedTicketTag) || |
| 990 |
!CBB_add_bytes(&child, hints->decrypted_ticket.data(), |
| 991 |
hints->decrypted_ticket.size())) { |
| 992 |
return 0; |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
if (hints->renew_ticket && // |
| 997 |
!CBB_add_asn1(&seq, &child, kRenewTicketTag)) { |
| 998 |
return 0; |
| 999 |
} |
| 1000 |
|
| 1001 |
if (hints->ignore_ticket && // |
| 1002 |
!CBB_add_asn1(&seq, &child, kIgnoreTicketTag)) { |
| 1003 |
return 0; |
| 1004 |
} |
| 1005 |
|
| 1006 |
return CBB_flush(out); |
| 1007 |
} |
| 1008 |
|
| 1009 |
static bool get_optional_implicit_null(CBS *cbs, bool *out_present, |
| 1010 |
CBS_ASN1_TAG tag) { |
| 1011 |
CBS value; |
| 1012 |
int present; |
| 1013 |
if (!CBS_get_optional_asn1(cbs, &value, &present, tag) || |
| 1014 |
(present && CBS_len(&value) != 0)) { |
| 1015 |
return false; |
| 1016 |
} |
| 1017 |
*out_present = present; |
| 1018 |
return true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
int SSL_set_handshake_hints(SSL *ssl, const uint8_t *hints, size_t hints_len) { |
| 1022 |
if (SSL_is_dtls(ssl)) { |
| 1023 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 1024 |
return 0; |
| 1025 |
} |
| 1026 |
|
| 1027 |
UniquePtr<SSL_HANDSHAKE_HINTS> hints_obj = MakeUnique<SSL_HANDSHAKE_HINTS>(); |
| 1028 |
if (hints_obj == nullptr) { |
| 1029 |
return 0; |
| 1030 |
} |
| 1031 |
|
| 1032 |
CBS cbs, seq, server_random_tls13, key_share, signature_hint, psk, |
| 1033 |
cert_compression, server_random_tls12, ecdhe, ticket; |
| 1034 |
int has_server_random_tls13, has_key_share, has_signature_hint, has_psk, |
| 1035 |
has_cert_compression, has_server_random_tls12, has_ecdhe, has_ticket; |
| 1036 |
CBS_init(&cbs, hints, hints_len); |
| 1037 |
if (!CBS_get_asn1(&cbs, &seq, CBS_ASN1_SEQUENCE) || |
| 1038 |
!CBS_get_optional_asn1(&seq, &server_random_tls13, |
| 1039 |
&has_server_random_tls13, kServerRandomTLS13Tag) || |
| 1040 |
!CBS_get_optional_asn1(&seq, &key_share, &has_key_share, |
| 1041 |
kKeyShareHintTag) || |
| 1042 |
!CBS_get_optional_asn1(&seq, &signature_hint, &has_signature_hint, |
| 1043 |
kSignatureHintTag) || |
| 1044 |
!CBS_get_optional_asn1(&seq, &psk, &has_psk, kDecryptedPSKTag) || |
| 1045 |
!get_optional_implicit_null(&seq, &hints_obj->ignore_psk, |
| 1046 |
kIgnorePSKTag) || |
| 1047 |
!CBS_get_optional_asn1(&seq, &cert_compression, &has_cert_compression, |
| 1048 |
kCompressCertificateTag) || |
| 1049 |
!CBS_get_optional_asn1(&seq, &server_random_tls12, |
| 1050 |
&has_server_random_tls12, kServerRandomTLS12Tag) || |
| 1051 |
!CBS_get_optional_asn1(&seq, &ecdhe, &has_ecdhe, kECDHEHintTag) || |
| 1052 |
!CBS_get_optional_asn1(&seq, &ticket, &has_ticket, kDecryptedTicketTag) || |
| 1053 |
!get_optional_implicit_null(&seq, &hints_obj->renew_ticket, |
| 1054 |
kRenewTicketTag) || |
| 1055 |
!get_optional_implicit_null(&seq, &hints_obj->ignore_ticket, |
| 1056 |
kIgnoreTicketTag)) { |
| 1057 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1058 |
return 0; |
| 1059 |
} |
| 1060 |
|
| 1061 |
if (has_server_random_tls13 && |
| 1062 |
!hints_obj->server_random_tls13.CopyFrom(server_random_tls13)) { |
| 1063 |
return 0; |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (has_key_share) { |
| 1067 |
uint64_t group_id; |
| 1068 |
CBS public_key, secret; |
| 1069 |
if (!CBS_get_asn1_uint64(&key_share, &group_id) || // |
| 1070 |
group_id == 0 || group_id > 0xffff || |
| 1071 |
!CBS_get_asn1(&key_share, &public_key, CBS_ASN1_OCTETSTRING) || |
| 1072 |
!hints_obj->key_share_public_key.CopyFrom(public_key) || |
| 1073 |
!CBS_get_asn1(&key_share, &secret, CBS_ASN1_OCTETSTRING) || |
| 1074 |
!hints_obj->key_share_secret.CopyFrom(secret)) { |
| 1075 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
hints_obj->key_share_group_id = static_cast<uint16_t>(group_id); |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (has_signature_hint) { |
| 1082 |
uint64_t sig_alg; |
| 1083 |
CBS input, spki, signature; |
| 1084 |
if (!CBS_get_asn1_uint64(&signature_hint, &sig_alg) || // |
| 1085 |
sig_alg == 0 || sig_alg > 0xffff || |
| 1086 |
!CBS_get_asn1(&signature_hint, &input, CBS_ASN1_OCTETSTRING) || |
| 1087 |
!hints_obj->signature_input.CopyFrom(input) || |
| 1088 |
!CBS_get_asn1(&signature_hint, &spki, CBS_ASN1_OCTETSTRING) || |
| 1089 |
!hints_obj->signature_spki.CopyFrom(spki) || |
| 1090 |
!CBS_get_asn1(&signature_hint, &signature, CBS_ASN1_OCTETSTRING) || |
| 1091 |
!hints_obj->signature.CopyFrom(signature)) { |
| 1092 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1093 |
return 0; |
| 1094 |
} |
| 1095 |
hints_obj->signature_algorithm = static_cast<uint16_t>(sig_alg); |
| 1096 |
} |
| 1097 |
|
| 1098 |
if (has_psk && !hints_obj->decrypted_psk.CopyFrom(psk)) { |
| 1099 |
return 0; |
| 1100 |
} |
| 1101 |
if (has_psk && hints_obj->ignore_psk) { |
| 1102 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1103 |
return 0; |
| 1104 |
} |
| 1105 |
|
| 1106 |
if (has_cert_compression) { |
| 1107 |
uint64_t alg; |
| 1108 |
CBS input, output; |
| 1109 |
if (!CBS_get_asn1_uint64(&cert_compression, &alg) || // |
| 1110 |
alg == 0 || alg > 0xffff || |
| 1111 |
!CBS_get_asn1(&cert_compression, &input, CBS_ASN1_OCTETSTRING) || |
| 1112 |
!hints_obj->cert_compression_input.CopyFrom(input) || |
| 1113 |
!CBS_get_asn1(&cert_compression, &output, CBS_ASN1_OCTETSTRING) || |
| 1114 |
!hints_obj->cert_compression_output.CopyFrom(output)) { |
| 1115 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
hints_obj->cert_compression_alg_id = static_cast<uint16_t>(alg); |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (has_server_random_tls12 && |
| 1122 |
!hints_obj->server_random_tls12.CopyFrom(server_random_tls12)) { |
| 1123 |
return 0; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (has_ecdhe) { |
| 1127 |
uint64_t group_id; |
| 1128 |
CBS public_key, private_key; |
| 1129 |
if (!CBS_get_asn1_uint64(&ecdhe, &group_id) || // |
| 1130 |
group_id == 0 || group_id > 0xffff || |
| 1131 |
!CBS_get_asn1(&ecdhe, &public_key, CBS_ASN1_OCTETSTRING) || |
| 1132 |
!hints_obj->ecdhe_public_key.CopyFrom(public_key) || |
| 1133 |
!CBS_get_asn1(&ecdhe, &private_key, CBS_ASN1_OCTETSTRING) || |
| 1134 |
!hints_obj->ecdhe_private_key.CopyFrom(private_key)) { |
| 1135 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1136 |
return 0; |
| 1137 |
} |
| 1138 |
hints_obj->ecdhe_group_id = static_cast<uint16_t>(group_id); |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (has_ticket && !hints_obj->decrypted_ticket.CopyFrom(ticket)) { |
| 1142 |
return 0; |
| 1143 |
} |
| 1144 |
if (has_ticket && hints_obj->ignore_ticket) { |
| 1145 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1146 |
return 0; |
| 1147 |
} |
| 1148 |
if (!has_ticket && hints_obj->renew_ticket) { |
| 1149 |
OPENSSL_PUT_ERROR(SSL, SSL_R_COULD_NOT_PARSE_HINTS); |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
|
| 1153 |
ssl->s3->hs->hints = std::move(hints_obj); |
| 1154 |
return 1; |
| 1155 |
} |