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