| 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 <assert.h> |
| 18 | #include <limits.h> |
| 19 | #include <string.h> |
| 20 |
| 21 | #include <utility> |
| 22 |
| 23 | #include <openssl/bytestring.h> |
| 24 | #include <openssl/digest.h> |
| 25 | #include <openssl/err.h> |
| 26 | #include <openssl/mem.h> |
| 27 | #include <openssl/sha.h> |
| 28 | #include <openssl/stack.h> |
| 29 |
| 30 | #include "../crypto/internal.h" |
| 31 | #include "internal.h" |
| 32 |
| 33 |
| 34 | BSSL_NAMESPACE_BEGIN |
| 35 |
| 36 | enum client_hs_state_t { |
| 37 | state_read_hello_retry_request = 0, |
| 38 | state_send_second_client_hello, |
| 39 | state_read_server_hello, |
| 40 | state_read_encrypted_extensions, |
| 41 | state_read_certificate_request, |
| 42 | state_read_server_certificate, |
| 43 | state_read_server_certificate_verify, |
| 44 | state_server_certificate_reverify, |
| 45 | state_read_server_finished, |
| 46 | state_send_end_of_early_data, |
| 47 | state_send_client_encrypted_extensions, |
| 48 | state_send_client_certificate, |
| 49 | state_send_client_certificate_verify, |
| 50 | state_complete_second_flight, |
| 51 | state_done, |
| 52 | }; |
| 53 |
| 54 | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
| 55 |
| 56 | // end_of_early_data closes the early data stream for |hs| and switches the |
| 57 | // encryption level to |level|. It returns true on success and false on error. |
| 58 | static bool close_early_data(SSL_HANDSHAKE *hs, ssl_encryption_level_t level) { |
| 59 | SSL *const ssl = hs->ssl; |
| 60 | assert(hs->in_early_data); |
| 61 |
| 62 | // Note |can_early_write| may already be false if |SSL_write| exceeded the |
| 63 | // early data write limit. |
| 64 | hs->can_early_write = false; |
| 65 |
| 66 | // 0-RTT write states on the client differ between TLS 1.3, DTLS 1.3, and |
| 67 | // QUIC. TLS 1.3 has one write encryption level at a time. 0-RTT write keys |
| 68 | // overwrite the null cipher and defer handshake write keys. While a |
| 69 | // HelloRetryRequest can cause us to rewind back to the null cipher, sequence |
| 70 | // numbers have no effect, so we can install a "new" null cipher. |
| 71 | // |
| 72 | // In QUIC and DTLS 1.3, 0-RTT write state cannot override or defer the normal |
| 73 | // write state. The two ClientHello sequence numbers must align, and handshake |
| 74 | // write keys must be installed early to ACK the EncryptedExtensions. |
| 75 | // |
| 76 | // TODO(crbug.com/381113363): We do not support 0-RTT in DTLS 1.3 and, in |
| 77 | // QUIC, the caller handles 0-RTT data, so we can skip installing 0-RTT keys |
| 78 | // and act as if there is one write level. Now that we're implementing |
| 79 | // DTLS 1.3, switch the abstraction to the DTLS/QUIC model where handshake |
| 80 | // keys write keys are installed immediately, but the TLS record layer |
| 81 | // internally waits to activate that epoch until the 0-RTT channel is closed. |
| 82 | if (!SSL_is_quic(ssl)) { |
| 83 | if (level == ssl_encryption_initial) { |
| 84 | bssl::UniquePtr<SSLAEADContext> null_ctx = |
| 85 | SSLAEADContext::CreateNullCipher(); |
| 86 | if (!null_ctx || // |
| 87 | !ssl->method->set_write_state(ssl, ssl_encryption_initial, |
| 88 | std::move(null_ctx), |
| 89 | /*traffic_secret=*/{})) { |
| 90 | return false; |
| 91 | } |
| 92 | } else { |
| 93 | assert(level == ssl_encryption_handshake); |
| 94 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
| 95 | hs->new_session.get(), |
| 96 | hs->client_handshake_secret)) { |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | } else { |
| 101 | assert(ssl->s3->quic_write_level == level); |
| 102 | } |
| 103 |
| 104 | return true; |
| 105 | } |
| 106 |
| 107 | static bool parse_server_hello_tls13(const SSL_HANDSHAKE *hs, |
| 108 | ParsedServerHello *out, uint8_t *out_alert, |
| 109 | const SSLMessage &msg) { |
| 110 | if (!ssl_parse_server_hello(out, out_alert, msg)) { |
| 111 | return false; |
| 112 | } |
| 113 | uint16_t expected_version = |
| 114 | SSL_is_dtls(hs->ssl) ? DTLS1_2_VERSION : TLS1_2_VERSION; |
| 115 | // DTLS 1.3 disables "compatibility mode" (RFC 8446, appendix D.4). When |
| 116 | // disabled, servers MUST NOT echo the legacy_session_id (RFC 9147, section |
| 117 | // 5). The client could have sent a session ID indicating its willingness to |
| 118 | // resume a DTLS 1.2 session, so just checking that the session IDs match is |
| 119 | // incorrect. |
| 120 | Span<const uint8_t> expected_session_id = |
| 121 | SSL_is_dtls(hs->ssl) ? Span<const uint8_t>() : Span(hs->session_id); |
| 122 |
| 123 | // RFC 8446 fixes some legacy values. Check them. |
| 124 | if (out->legacy_version != expected_version || // |
| 125 | out->compression_method != 0 || |
| 126 | Span<const uint8_t>(out->session_id) != expected_session_id || |
| 127 | CBS_len(&out->extensions) == 0) { |
| 128 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 129 | *out_alert = SSL_AD_DECODE_ERROR; |
| 130 | return false; |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 |
| 135 | static bool is_hello_retry_request(const ParsedServerHello &server_hello) { |
| 136 | return Span<const uint8_t>(server_hello.random) == kHelloRetryRequest; |
| 137 | } |
| 138 |
| 139 | static bool check_ech_confirmation(const SSL_HANDSHAKE *hs, bool *out_accepted, |
| 140 | uint8_t *out_alert, |
| 141 | const ParsedServerHello &server_hello) { |
| 142 | const bool is_hrr = is_hello_retry_request(server_hello); |
| 143 | size_t offset; |
| 144 | if (is_hrr) { |
| 145 | // We check for an unsolicited extension when parsing all of them. |
| 146 | SSLExtension ech(TLSEXT_TYPE_encrypted_client_hello); |
| 147 | if (!ssl_parse_extensions(&server_hello.extensions, out_alert, {&ech}, |
| 148 | /*ignore_unknown=*/true)) { |
| 149 | return false; |
| 150 | } |
| 151 | if (!ech.present) { |
| 152 | *out_accepted = false; |
| 153 | return true; |
| 154 | } |
| 155 | if (CBS_len(&ech.data) != ECH_CONFIRMATION_SIGNAL_LEN) { |
| 156 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 157 | *out_alert = SSL_AD_DECODE_ERROR; |
| 158 | return false; |
| 159 | } |
| 160 | offset = CBS_data(&ech.data) - CBS_data(&server_hello.raw); |
| 161 | } else { |
| 162 | offset = ssl_ech_confirmation_signal_hello_offset(hs->ssl); |
| 163 | } |
| 164 |
| 165 | if (!hs->selected_ech_config) { |
| 166 | *out_accepted = false; |
| 167 | return true; |
| 168 | } |
| 169 |
| 170 | uint8_t expected[ECH_CONFIRMATION_SIGNAL_LEN]; |
| 171 | if (!ssl_ech_accept_confirmation(hs, expected, hs->inner_client_random, |
| 172 | hs->inner_transcript, is_hrr, |
| 173 | server_hello.raw, offset)) { |
| 174 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 175 | return false; |
| 176 | } |
| 177 |
| 178 | *out_accepted = CRYPTO_memcmp(CBS_data(&server_hello.raw) + offset, expected, |
| 179 | sizeof(expected)) == 0; |
| 180 | return true; |
| 181 | } |
| 182 |
| 183 | static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) { |
| 184 | SSL *const ssl = hs->ssl; |
| 185 | assert(ssl->s3->version != 0); |
| 186 | SSLMessage msg; |
| 187 | if (!ssl->method->get_message(ssl, &msg)) { |
| 188 | return ssl_hs_read_message; |
| 189 | } |
| 190 |
| 191 | // Queue up a ChangeCipherSpec for whenever we next send something. This |
| 192 | // will be before the second ClientHello. If we offered early data, this was |
| 193 | // already done. |
| 194 | if (!hs->early_data_offered && // |
| 195 | !ssl->method->add_change_cipher_spec(ssl)) { |
| 196 | return ssl_hs_error; |
| 197 | } |
| 198 |
| 199 | ParsedServerHello server_hello; |
| 200 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 201 | if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) { |
| 202 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 203 | return ssl_hs_error; |
| 204 | } |
| 205 |
| 206 | // The cipher suite must be one we offered. We currently offer all supported |
| 207 | // TLS 1.3 ciphers unless policy controls limited it. So we check the version |
| 208 | // and that it's ok per policy. |
| 209 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(server_hello.cipher_suite); |
| 210 | if (cipher == nullptr || |
| 211 | SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) || |
| 212 | SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) || |
| 213 | !ssl_tls13_cipher_meets_policy(SSL_CIPHER_get_protocol_id(cipher), |
| 214 | ssl->config->compliance_policy)) { |
| 215 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 216 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 217 | return ssl_hs_error; |
| 218 | } |
| 219 |
| 220 | hs->new_cipher = cipher; |
| 221 |
| 222 | const bool is_hrr = is_hello_retry_request(server_hello); |
| 223 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
| 224 | (is_hrr && !hs->transcript.UpdateForHelloRetryRequest())) { |
| 225 | return ssl_hs_error; |
| 226 | } |
| 227 | if (hs->selected_ech_config) { |
| 228 | if (!hs->inner_transcript.InitHash(ssl_protocol_version(ssl), |
| 229 | hs->new_cipher) || |
| 230 | (is_hrr && !hs->inner_transcript.UpdateForHelloRetryRequest())) { |
| 231 | return ssl_hs_error; |
| 232 | } |
| 233 | } |
| 234 |
| 235 | // Determine which ClientHello the server is responding to. Run |
| 236 | // |check_ech_confirmation| unconditionally, so we validate the extension |
| 237 | // contents. |
| 238 | bool ech_accepted; |
| 239 | if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) { |
| 240 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 241 | return ssl_hs_error; |
| 242 | } |
| 243 | if (hs->selected_ech_config) { |
| 244 | ssl->s3->ech_status = ech_accepted ? ssl_ech_accepted : ssl_ech_rejected; |
| 245 | } |
| 246 |
| 247 | if (!is_hrr) { |
| 248 | hs->tls13_state = state_read_server_hello; |
| 249 | return ssl_hs_ok; |
| 250 | } |
| 251 |
| 252 | // The ECH extension, if present, was already parsed by |
| 253 | // |check_ech_confirmation|. |
| 254 | SSLExtension cookie(TLSEXT_TYPE_cookie), |
| 255 | key_share(TLSEXT_TYPE_key_share, !hs->key_share_bytes.empty()), |
| 256 | supported_versions(TLSEXT_TYPE_supported_versions), |
| 257 | ech_unused(TLSEXT_TYPE_encrypted_client_hello, |
| 258 | hs->selected_ech_config || hs->config->ech_grease_enabled); |
| 259 | if (!ssl_parse_extensions( |
| 260 | &server_hello.extensions, &alert, |
| 261 | {&cookie, &key_share, &supported_versions, &ech_unused}, |
| 262 | /*ignore_unknown=*/false)) { |
| 263 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 264 | return ssl_hs_error; |
| 265 | } |
| 266 |
| 267 | if (!cookie.present && !key_share.present) { |
| 268 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST); |
| 269 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 270 | return ssl_hs_error; |
| 271 | } |
| 272 | if (cookie.present) { |
| 273 | CBS cookie_value; |
| 274 | if (!CBS_get_u16_length_prefixed(&cookie.data, &cookie_value) || // |
| 275 | CBS_len(&cookie_value) == 0 || // |
| 276 | CBS_len(&cookie.data) != 0) { |
| 277 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 278 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 279 | return ssl_hs_error; |
| 280 | } |
| 281 |
| 282 | if (!hs->cookie.CopyFrom(cookie_value)) { |
| 283 | return ssl_hs_error; |
| 284 | } |
| 285 | } |
| 286 |
| 287 | if (key_share.present) { |
| 288 | // If offering PAKE, we won't send key_share extensions, in which case we |
| 289 | // would have rejected key_share from the peer. |
| 290 | assert(!hs->pake_prover); |
| 291 |
| 292 | uint16_t group_id; |
| 293 | if (!CBS_get_u16(&key_share.data, &group_id) || |
| 294 | CBS_len(&key_share.data) != 0) { |
| 295 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 296 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 297 | return ssl_hs_error; |
| 298 | } |
| 299 |
| 300 | // The group must be supported. |
| 301 | if (!tls1_check_group_id(hs, group_id)) { |
| 302 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 303 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 304 | return ssl_hs_error; |
| 305 | } |
| 306 |
| 307 | // Check that the HelloRetryRequest does not request a key share that was |
| 308 | // provided in the initial ClientHello. |
| 309 | if (hs->key_shares[0]->GroupID() == group_id || |
| 310 | (hs->key_shares[1] && hs->key_shares[1]->GroupID() == group_id)) { |
| 311 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 312 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
| 313 | return ssl_hs_error; |
| 314 | } |
| 315 |
| 316 | if (!ssl_setup_key_shares(hs, group_id)) { |
| 317 | return ssl_hs_error; |
| 318 | } |
| 319 | } |
| 320 |
| 321 | // Although we now know whether ClientHelloInner was used, we currently |
| 322 | // maintain both transcripts up to ServerHello. We could swap transcripts |
| 323 | // early, but then ClientHello construction and |check_ech_confirmation| |
| 324 | // become more complex. |
| 325 | if (!ssl_hash_message(hs, msg)) { |
| 326 | return ssl_hs_error; |
| 327 | } |
| 328 | if (ssl->s3->ech_status == ssl_ech_accepted && |
| 329 | !hs->inner_transcript.Update(msg.raw)) { |
| 330 | return ssl_hs_error; |
| 331 | } |
| 332 |
| 333 | // HelloRetryRequest should be the end of the flight. |
| 334 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 335 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 336 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 337 | return ssl_hs_error; |
| 338 | } |
| 339 |
| 340 | ssl->method->next_message(ssl); |
| 341 | ssl->s3->used_hello_retry_request = true; |
| 342 | hs->tls13_state = state_send_second_client_hello; |
| 343 | // 0-RTT is rejected if we receive a HelloRetryRequest. |
| 344 | if (hs->in_early_data) { |
| 345 | ssl->s3->early_data_reason = ssl_early_data_hello_retry_request; |
| 346 | if (!close_early_data(hs, ssl_encryption_initial)) { |
| 347 | return ssl_hs_error; |
| 348 | } |
| 349 | return ssl_hs_early_data_rejected; |
| 350 | } |
| 351 | return ssl_hs_ok; |
| 352 | } |
| 353 |
| 354 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) { |
| 355 | // Build the second ClientHelloInner, if applicable. The second ClientHello |
| 356 | // uses an empty string for |enc|. |
| 357 | if (hs->ssl->s3->ech_status == ssl_ech_accepted && |
| 358 | !ssl_encrypt_client_hello(hs, {})) { |
| 359 | return ssl_hs_error; |
| 360 | } |
| 361 |
| 362 | if (!ssl_add_client_hello(hs)) { |
| 363 | return ssl_hs_error; |
| 364 | } |
| 365 |
| 366 | ssl_done_writing_client_hello(hs); |
| 367 | hs->tls13_state = state_read_server_hello; |
| 368 | return ssl_hs_flush; |
| 369 | } |
| 370 |
| 371 | static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { |
| 372 | SSL *const ssl = hs->ssl; |
| 373 | SSLMessage msg; |
| 374 | if (!ssl->method->get_message(ssl, &msg)) { |
| 375 | return ssl_hs_read_message; |
| 376 | } |
| 377 | ParsedServerHello server_hello; |
| 378 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 379 | if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) { |
| 380 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 381 | return ssl_hs_error; |
| 382 | } |
| 383 |
| 384 | // Forbid a second HelloRetryRequest. |
| 385 | if (is_hello_retry_request(server_hello)) { |
| 386 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 387 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 388 | return ssl_hs_error; |
| 389 | } |
| 390 |
| 391 | // Check the cipher suite, in case this is after HelloRetryRequest. |
| 392 | if (SSL_CIPHER_get_protocol_id(hs->new_cipher) != server_hello.cipher_suite) { |
| 393 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
| 394 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 395 | return ssl_hs_error; |
| 396 | } |
| 397 |
| 398 | if (ssl->s3->ech_status == ssl_ech_accepted) { |
| 399 | if (ssl->s3->used_hello_retry_request) { |
| 400 | // HelloRetryRequest and ServerHello must accept ECH consistently. |
| 401 | bool ech_accepted; |
| 402 | if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) { |
| 403 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 404 | return ssl_hs_error; |
| 405 | } |
| 406 | if (!ech_accepted) { |
| 407 | OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_ECH_NEGOTIATION); |
| 408 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 409 | return ssl_hs_error; |
| 410 | } |
| 411 | } |
| 412 |
| 413 | hs->transcript = std::move(hs->inner_transcript); |
| 414 | hs->extensions.sent = hs->inner_extensions_sent; |
| 415 | // Report the inner random value through |SSL_get_client_random|. |
| 416 | OPENSSL_memcpy(ssl->s3->client_random, hs->inner_client_random, |
| 417 | SSL3_RANDOM_SIZE); |
| 418 | } |
| 419 |
| 420 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_hello.random), |
| 421 | SSL3_RANDOM_SIZE); |
| 422 |
| 423 | // When offering ECH, |ssl->session| is only offered in ClientHelloInner. |
| 424 | const bool pre_shared_key_allowed = |
| 425 | ssl->session != nullptr && |
| 426 | ssl_session_get_type(ssl->session.get()) == |
| 427 | SSLSessionType::kPreSharedKey && |
| 428 | ssl->s3->ech_status != ssl_ech_rejected; |
| 429 | SSLExtension key_share(TLSEXT_TYPE_key_share, hs->key_shares[0] != nullptr), |
| 430 | pake_share(TLSEXT_TYPE_pake, hs->pake_prover != nullptr), |
| 431 | pre_shared_key(TLSEXT_TYPE_pre_shared_key, pre_shared_key_allowed), |
| 432 | supported_versions(TLSEXT_TYPE_supported_versions); |
| 433 | if (!ssl_parse_extensions( |
| 434 | &server_hello.extensions, &alert, |
| 435 | {&key_share, &pre_shared_key, &supported_versions, &pake_share}, |
| 436 | /*ignore_unknown=*/false)) { |
| 437 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 438 | return ssl_hs_error; |
| 439 | } |
| 440 |
| 441 | // Recheck supported_versions, in case this is after HelloRetryRequest. |
| 442 | uint16_t version; |
| 443 | if (!supported_versions.present || // |
| 444 | !CBS_get_u16(&supported_versions.data, &version) || // |
| 445 | CBS_len(&supported_versions.data) != 0 || // |
| 446 | version != ssl->s3->version) { |
| 447 | OPENSSL_PUT_ERROR(SSL, SSL_R_SECOND_SERVERHELLO_VERSION_MISMATCH); |
| 448 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 449 | return ssl_hs_error; |
| 450 | } |
| 451 |
| 452 | // The combination of ServerHello extensions determines the kind of handshake |
| 453 | // that the server selected. Check for invalid combinations. |
| 454 |
| 455 | // pake replaces key_share and may not be used with pre_shared_key. |
| 456 | if (pake_share.present && (key_share.present || pre_shared_key.present)) { |
| 457 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 458 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION); |
| 459 | return ssl_hs_error; |
| 460 | } |
| 461 | // In PAKE mode, we require a PAKE handshake and do not support resumption. |
| 462 | if (hs->pake_prover != nullptr && !pake_share.present) { |
| 463 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION); |
| 464 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 465 | return ssl_hs_error; |
| 466 | } |
| 467 | // In non-PAKE modes, we require per-connection forward secrecy and do not |
| 468 | // support psk_ke. |
| 469 | if (hs->pake_prover == nullptr && !key_share.present) { |
| 470 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
| 471 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
| 472 | return ssl_hs_error; |
| 473 | } |
| 474 | // The above imples only one of three handshake forms will be allowed. The |
| 475 | // checks for unsolicited extensions ensure the server did not select |
| 476 | // something we cannot respond to. |
| 477 | assert( |
| 478 | // Full handshake |
| 479 | (key_share.present && !pake_share.present && !pre_shared_key.present) || |
| 480 | // PSK/resumption handshake |
| 481 | (key_share.present && !pake_share.present && pre_shared_key.present) || |
| 482 | // PAKE handshake |
| 483 | (!key_share.present && pake_share.present && !pre_shared_key.present)); |
| 484 |
| 485 | alert = SSL_AD_DECODE_ERROR; |
| 486 | if (pre_shared_key.present) { |
| 487 | if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert, |
| 488 | &pre_shared_key.data)) { |
| 489 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 490 | return ssl_hs_error; |
| 491 | } |
| 492 |
| 493 | if (ssl->session->ssl_version != ssl->s3->version) { |
| 494 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
| 495 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 496 | return ssl_hs_error; |
| 497 | } |
| 498 |
| 499 | if (ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) { |
| 500 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH); |
| 501 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 502 | return ssl_hs_error; |
| 503 | } |
| 504 |
| 505 | if (!ssl_session_is_context_valid(hs, ssl->session.get())) { |
| 506 | // This is actually a client application bug. |
| 507 | OPENSSL_PUT_ERROR(SSL, |
| 508 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 509 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 510 | return ssl_hs_error; |
| 511 | } |
| 512 |
| 513 | ssl->s3->session_reused = true; |
| 514 | hs->can_release_private_key = true; |
| 515 | // Only authentication information carries over in TLS 1.3. |
| 516 | hs->new_session = |
| 517 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
| 518 | if (!hs->new_session) { |
| 519 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 520 | return ssl_hs_error; |
| 521 | } |
| 522 | ssl_set_session(ssl, NULL); |
| 523 |
| 524 | // Resumption incorporates fresh key material, so refresh the timeout. |
| 525 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
| 526 | ssl->session_ctx->session_psk_dhe_timeout); |
| 527 | } else if (!ssl_get_new_session(hs)) { |
| 528 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 529 | return ssl_hs_error; |
| 530 | } |
| 531 |
| 532 | hs->new_session->cipher = hs->new_cipher; |
| 533 |
| 534 | // Set up the key schedule and incorporate the PSK into the running secret. |
| 535 | size_t hash_len = EVP_MD_size( |
| 536 | ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher)); |
| 537 | if (!tls13_init_key_schedule(hs, ssl->s3->session_reused |
| 538 | ? Span(hs->new_session->secret) |
| 539 | : Span(kZeroes, hash_len))) { |
| 540 | return ssl_hs_error; |
| 541 | } |
| 542 |
| 543 | // Resolve ECDHE or PAKE and incorporate it into the secret. |
| 544 | Array<uint8_t> shared_secret; |
| 545 | alert = SSL_AD_DECODE_ERROR; |
| 546 | if (key_share.present) { |
| 547 | if (!ssl_ext_key_share_parse_serverhello(hs, &shared_secret, &alert, |
| 548 | &key_share.data)) { |
| 549 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 550 | return ssl_hs_error; |
| 551 | } |
| 552 | } else if (pake_share.present) { |
| 553 | if (!ssl_ext_pake_parse_serverhello(hs, &shared_secret, &alert, |
| 554 | &pake_share.data)) { |
| 555 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 556 | return ssl_hs_error; |
| 557 | } |
| 558 | } else { |
| 559 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 560 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 561 | return ssl_hs_error; |
| 562 | } |
| 563 |
| 564 | if (!tls13_advance_key_schedule(hs, shared_secret) || // |
| 565 | !ssl_hash_message(hs, msg) || // |
| 566 | !tls13_derive_handshake_secrets(hs)) { |
| 567 | return ssl_hs_error; |
| 568 | } |
| 569 |
| 570 | // If currently sending early data over TCP, we defer installing client |
| 571 | // traffic keys to when the early data stream is closed. See |
| 572 | // |close_early_data|. Note if the server has already rejected 0-RTT via |
| 573 | // HelloRetryRequest, |in_early_data| is already false. |
| 574 | if (!hs->in_early_data || SSL_is_quic(ssl)) { |
| 575 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
| 576 | hs->new_session.get(), |
| 577 | hs->client_handshake_secret)) { |
| 578 | return ssl_hs_error; |
| 579 | } |
| 580 | } |
| 581 |
| 582 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
| 583 | hs->new_session.get(), |
| 584 | hs->server_handshake_secret)) { |
| 585 | return ssl_hs_error; |
| 586 | } |
| 587 |
| 588 | ssl->method->next_message(ssl); |
| 589 | hs->tls13_state = state_read_encrypted_extensions; |
| 590 | return ssl_hs_ok; |
| 591 | } |
| 592 |
| 593 | static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) { |
| 594 | SSL *const ssl = hs->ssl; |
| 595 | SSLMessage msg; |
| 596 | if (!ssl->method->get_message(ssl, &msg)) { |
| 597 | return ssl_hs_read_message; |
| 598 | } |
| 599 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
| 600 | return ssl_hs_error; |
| 601 | } |
| 602 |
| 603 | CBS body = msg.body, extensions; |
| 604 | if (!CBS_get_u16_length_prefixed(&body, &extensions) || // |
| 605 | CBS_len(&body) != 0) { |
| 606 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 607 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 608 | return ssl_hs_error; |
| 609 | } |
| 610 |
| 611 | if (!ssl_parse_serverhello_tlsext(hs, &extensions)) { |
| 612 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 613 | return ssl_hs_error; |
| 614 | } |
| 615 |
| 616 | if (ssl->s3->early_data_accepted) { |
| 617 | // The extension parser checks the server resumed the session. |
| 618 | assert(ssl->s3->session_reused); |
| 619 | // If offering ECH, the server may not accept early data with |
| 620 | // ClientHelloOuter. We do not offer sessions with ClientHelloOuter, so this |
| 621 | // this should be implied by checking |session_reused|. |
| 622 | assert(ssl->s3->ech_status != ssl_ech_rejected); |
| 623 |
| 624 | if (hs->early_session->cipher != hs->new_session->cipher) { |
| 625 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_MISMATCH_ON_EARLY_DATA); |
| 626 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 627 | return ssl_hs_error; |
| 628 | } |
| 629 | if (Span(hs->early_session->early_alpn) != ssl->s3->alpn_selected) { |
| 630 | OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA); |
| 631 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 632 | return ssl_hs_error; |
| 633 | } |
| 634 | // Channel ID is incompatible with 0-RTT. The ALPS extension should be |
| 635 | // negotiated implicitly. |
| 636 | if (hs->channel_id_negotiated || |
| 637 | hs->new_session->has_application_settings) { |
| 638 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA); |
| 639 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 640 | return ssl_hs_error; |
| 641 | } |
| 642 | hs->new_session->has_application_settings = |
| 643 | hs->early_session->has_application_settings; |
| 644 | if (!hs->new_session->local_application_settings.CopyFrom( |
| 645 | hs->early_session->local_application_settings) || |
| 646 | !hs->new_session->peer_application_settings.CopyFrom( |
| 647 | hs->early_session->peer_application_settings)) { |
| 648 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 649 | return ssl_hs_error; |
| 650 | } |
| 651 | } |
| 652 |
| 653 | // Store the negotiated ALPN in the session. |
| 654 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
| 655 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 656 | return ssl_hs_error; |
| 657 | } |
| 658 |
| 659 | if (!ssl_hash_message(hs, msg)) { |
| 660 | return ssl_hs_error; |
| 661 | } |
| 662 |
| 663 | ssl->method->next_message(ssl); |
| 664 | hs->tls13_state = state_read_certificate_request; |
| 665 | if (hs->in_early_data && !ssl->s3->early_data_accepted) { |
| 666 | if (!close_early_data(hs, ssl_encryption_handshake)) { |
| 667 | return ssl_hs_error; |
| 668 | } |
| 669 | return ssl_hs_early_data_rejected; |
| 670 | } |
| 671 | return ssl_hs_ok; |
| 672 | } |
| 673 |
| 674 | static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) { |
| 675 | SSL *const ssl = hs->ssl; |
| 676 | // CertificateRequest may only be sent in non-resumption handshakes. |
| 677 | if (ssl->s3->session_reused) { |
| 678 | if (ssl->ctx->reverify_on_resume && !ssl->s3->early_data_accepted) { |
| 679 | hs->tls13_state = state_server_certificate_reverify; |
| 680 | return ssl_hs_ok; |
| 681 | } |
| 682 | hs->tls13_state = state_read_server_finished; |
| 683 | return ssl_hs_ok; |
| 684 | } |
| 685 |
| 686 | if (hs->pake_prover) { |
| 687 | hs->tls13_state = state_read_server_finished; |
| 688 | return ssl_hs_ok; |
| 689 | } |
| 690 |
| 691 | SSLMessage msg; |
| 692 | if (!ssl->method->get_message(ssl, &msg)) { |
| 693 | return ssl_hs_read_message; |
| 694 | } |
| 695 |
| 696 | // CertificateRequest is optional. |
| 697 | if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) { |
| 698 | hs->tls13_state = state_read_server_certificate; |
| 699 | return ssl_hs_ok; |
| 700 | } |
| 701 |
| 702 | SSLExtension sigalgs(TLSEXT_TYPE_signature_algorithms), |
| 703 | ca(TLSEXT_TYPE_certificate_authorities); |
| 704 | CBS body = msg.body, context, extensions, supported_signature_algorithms; |
| 705 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 706 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
| 707 | // The request context is always empty during the handshake. |
| 708 | CBS_len(&context) != 0 || |
| 709 | !CBS_get_u16_length_prefixed(&body, &extensions) || // |
| 710 | CBS_len(&body) != 0 || |
| 711 | !ssl_parse_extensions(&extensions, &alert, {&sigalgs, &ca}, |
| 712 | /*ignore_unknown=*/true) || |
| 713 | !sigalgs.present || |
| 714 | !CBS_get_u16_length_prefixed(&sigalgs.data, |
| 715 | &supported_signature_algorithms) || |
| 716 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
| 717 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 718 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 719 | return ssl_hs_error; |
| 720 | } |
| 721 |
| 722 | if (ca.present) { |
| 723 | hs->ca_names = SSL_parse_CA_list(ssl, &alert, &ca.data); |
| 724 | if (!hs->ca_names) { |
| 725 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 726 | return ssl_hs_error; |
| 727 | } |
| 728 | } else { |
| 729 | hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null()); |
| 730 | if (!hs->ca_names) { |
| 731 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 732 | return ssl_hs_error; |
| 733 | } |
| 734 | } |
| 735 |
| 736 | hs->cert_request = true; |
| 737 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
| 738 |
| 739 | if (!ssl_hash_message(hs, msg)) { |
| 740 | return ssl_hs_error; |
| 741 | } |
| 742 |
| 743 | ssl->method->next_message(ssl); |
| 744 | hs->tls13_state = state_read_server_certificate; |
| 745 | return ssl_hs_ok; |
| 746 | } |
| 747 |
| 748 | static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) { |
| 749 | SSL *const ssl = hs->ssl; |
| 750 | SSLMessage msg; |
| 751 | if (!ssl->method->get_message(ssl, &msg)) { |
| 752 | return ssl_hs_read_message; |
| 753 | } |
| 754 |
| 755 | if (msg.type != SSL3_MT_COMPRESSED_CERTIFICATE && |
| 756 | !ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
| 757 | return ssl_hs_error; |
| 758 | } |
| 759 |
| 760 | if (!tls13_process_certificate(hs, msg, false /* certificate required */) || |
| 761 | !ssl_hash_message(hs, msg)) { |
| 762 | return ssl_hs_error; |
| 763 | } |
| 764 |
| 765 | ssl->method->next_message(ssl); |
| 766 | hs->tls13_state = state_read_server_certificate_verify; |
| 767 | return ssl_hs_ok; |
| 768 | } |
| 769 |
| 770 | static enum ssl_hs_wait_t do_read_server_certificate_verify(SSL_HANDSHAKE *hs) { |
| 771 | SSL *const ssl = hs->ssl; |
| 772 | SSLMessage msg; |
| 773 | if (!ssl->method->get_message(ssl, &msg)) { |
| 774 | return ssl_hs_read_message; |
| 775 | } |
| 776 | switch (ssl_verify_peer_cert(hs)) { |
| 777 | case ssl_verify_ok: |
| 778 | break; |
| 779 | case ssl_verify_invalid: |
| 780 | return ssl_hs_error; |
| 781 | case ssl_verify_retry: |
| 782 | hs->tls13_state = state_read_server_certificate_verify; |
| 783 | return ssl_hs_certificate_verify; |
| 784 | } |
| 785 |
| 786 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
| 787 | !tls13_process_certificate_verify(hs, msg) || |
| 788 | !ssl_hash_message(hs, msg)) { |
| 789 | return ssl_hs_error; |
| 790 | } |
| 791 |
| 792 | ssl->method->next_message(ssl); |
| 793 | hs->tls13_state = state_read_server_finished; |
| 794 | return ssl_hs_ok; |
| 795 | } |
| 796 |
| 797 | static enum ssl_hs_wait_t do_server_certificate_reverify(SSL_HANDSHAKE *hs) { |
| 798 | switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) { |
| 799 | case ssl_verify_ok: |
| 800 | break; |
| 801 | case ssl_verify_invalid: |
| 802 | return ssl_hs_error; |
| 803 | case ssl_verify_retry: |
| 804 | hs->tls13_state = state_server_certificate_reverify; |
| 805 | return ssl_hs_certificate_verify; |
| 806 | } |
| 807 | hs->tls13_state = state_read_server_finished; |
| 808 | return ssl_hs_ok; |
| 809 | } |
| 810 |
| 811 | static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { |
| 812 | SSL *const ssl = hs->ssl; |
| 813 | SSLMessage msg; |
| 814 | if (!ssl->method->get_message(ssl, &msg)) { |
| 815 | return ssl_hs_read_message; |
| 816 | } |
| 817 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
| 818 | !tls13_process_finished(hs, msg, false /* don't use saved value */) || |
| 819 | !ssl_hash_message(hs, msg) || |
| 820 | // Update the secret to the master secret and derive traffic keys. |
| 821 | !tls13_advance_key_schedule(hs, |
| 822 | Span(kZeroes, hs->transcript.DigestLen())) || |
| 823 | !tls13_derive_application_secrets(hs)) { |
| 824 | return ssl_hs_error; |
| 825 | } |
| 826 |
| 827 | // Finished should be the end of the flight. |
| 828 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
| 829 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 830 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
| 831 | return ssl_hs_error; |
| 832 | } |
| 833 |
| 834 | ssl->method->next_message(ssl); |
| 835 | hs->tls13_state = state_send_end_of_early_data; |
| 836 | return ssl_hs_ok; |
| 837 | } |
| 838 |
| 839 | static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) { |
| 840 | SSL *const ssl = hs->ssl; |
| 841 |
| 842 | if (ssl->s3->early_data_accepted) { |
| 843 | // DTLS and QUIC omit the EndOfEarlyData message. See RFC 9001, section 8.3, |
| 844 | // and RFC 9147, section 5.6. |
| 845 | if (!SSL_is_quic(ssl) && !SSL_is_dtls(ssl)) { |
| 846 | ScopedCBB cbb; |
| 847 | CBB body; |
| 848 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 849 | SSL3_MT_END_OF_EARLY_DATA) || |
| 850 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 851 | return ssl_hs_error; |
| 852 | } |
| 853 | } |
| 854 |
| 855 | if (!close_early_data(hs, ssl_encryption_handshake)) { |
| 856 | return ssl_hs_error; |
| 857 | } |
| 858 | } |
| 859 |
| 860 | hs->tls13_state = state_send_client_encrypted_extensions; |
| 861 | return ssl_hs_ok; |
| 862 | } |
| 863 |
| 864 | static enum ssl_hs_wait_t do_send_client_encrypted_extensions( |
| 865 | SSL_HANDSHAKE *hs) { |
| 866 | SSL *const ssl = hs->ssl; |
| 867 | // For now, only one extension uses client EncryptedExtensions. This function |
| 868 | // may be generalized if others use it in the future. |
| 869 | if (hs->new_session->has_application_settings && |
| 870 | !ssl->s3->early_data_accepted) { |
| 871 | ScopedCBB cbb; |
| 872 | CBB body, extensions, extension; |
| 873 | uint16_t extension_type = TLSEXT_TYPE_application_settings_old; |
| 874 | if (hs->config->alps_use_new_codepoint) { |
| 875 | extension_type = TLSEXT_TYPE_application_settings; |
| 876 | } |
| 877 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
| 878 | SSL3_MT_ENCRYPTED_EXTENSIONS) || |
| 879 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
| 880 | !CBB_add_u16(&extensions, extension_type) || |
| 881 | !CBB_add_u16_length_prefixed(&extensions, &extension) || |
| 882 | !CBB_add_bytes(&extension, |
| 883 | hs->new_session->local_application_settings.data(), |
| 884 | hs->new_session->local_application_settings.size()) || |
| 885 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 886 | return ssl_hs_error; |
| 887 | } |
| 888 | } |
| 889 |
| 890 | hs->tls13_state = state_send_client_certificate; |
| 891 | return ssl_hs_ok; |
| 892 | } |
| 893 |
| 894 | static bool check_credential(SSL_HANDSHAKE *hs, const SSL_CREDENTIAL *cred, |
| 895 | uint16_t *out_sigalg) { |
| 896 | if (cred->type != SSLCredentialType::kX509) { |
| 897 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 898 | return false; |
| 899 | } |
| 900 |
| 901 | // All currently supported credentials require a signature. |
| 902 | if (!tls1_choose_signature_algorithm(hs, cred, out_sigalg)) { |
| 903 | return false; |
| 904 | } |
| 905 | // Use this credential if it either matches a requested issuer, |
| 906 | // or does not require issuer matching. |
| 907 | return ssl_credential_matches_requested_issuers(hs, cred); |
| 908 | } |
| 909 |
| 910 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
| 911 | SSL *const ssl = hs->ssl; |
| 912 |
| 913 | // The peer didn't request a certificate. |
| 914 | if (!hs->cert_request) { |
| 915 | hs->tls13_state = state_complete_second_flight; |
| 916 | return ssl_hs_ok; |
| 917 | } |
| 918 |
| 919 | if (ssl->s3->ech_status == ssl_ech_rejected) { |
| 920 | // Do not send client certificates on ECH reject. We have not authenticated |
| 921 | // the server for the name that can learn the certificate. |
| 922 | SSL_certs_clear(ssl); |
| 923 | } else if (hs->config->cert->cert_cb != nullptr) { |
| 924 | // Call cert_cb to update the certificate. |
| 925 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
| 926 | if (rv == 0) { |
| 927 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 928 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 929 | return ssl_hs_error; |
| 930 | } |
| 931 | if (rv < 0) { |
| 932 | hs->tls13_state = state_send_client_certificate; |
| 933 | return ssl_hs_x509_lookup; |
| 934 | } |
| 935 | } |
| 936 |
| 937 | Array<SSL_CREDENTIAL *> creds; |
| 938 | if (!ssl_get_full_credential_list(hs, &creds)) { |
| 939 | return ssl_hs_error; |
| 940 | } |
| 941 |
| 942 | if (!creds.empty()) { |
| 943 | // Select the credential to use. |
| 944 | for (SSL_CREDENTIAL *cred : creds) { |
| 945 | ERR_clear_error(); |
| 946 | uint16_t sigalg; |
| 947 | if (check_credential(hs, cred, &sigalg)) { |
| 948 | hs->credential = UpRef(cred); |
| 949 | hs->signature_algorithm = sigalg; |
| 950 | break; |
| 951 | } |
| 952 | } |
| 953 | if (hs->credential == nullptr) { |
| 954 | // The error from the last attempt is in the error queue. |
| 955 | assert(ERR_peek_error() != 0); |
| 956 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 957 | return ssl_hs_error; |
| 958 | } |
| 959 | } |
| 960 |
| 961 | if (!tls13_add_certificate(hs)) { |
| 962 | return ssl_hs_error; |
| 963 | } |
| 964 |
| 965 | hs->tls13_state = state_send_client_certificate_verify; |
| 966 | return ssl_hs_ok; |
| 967 | } |
| 968 |
| 969 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) { |
| 970 | // Don't send CertificateVerify if there is no certificate. |
| 971 | if (hs->credential == nullptr) { |
| 972 | hs->tls13_state = state_complete_second_flight; |
| 973 | return ssl_hs_ok; |
| 974 | } |
| 975 |
| 976 | switch (tls13_add_certificate_verify(hs)) { |
| 977 | case ssl_private_key_success: |
| 978 | hs->tls13_state = state_complete_second_flight; |
| 979 | return ssl_hs_ok; |
| 980 |
| 981 | case ssl_private_key_retry: |
| 982 | hs->tls13_state = state_send_client_certificate_verify; |
| 983 | return ssl_hs_private_key_operation; |
| 984 |
| 985 | case ssl_private_key_failure: |
| 986 | return ssl_hs_error; |
| 987 | } |
| 988 |
| 989 | assert(0); |
| 990 | return ssl_hs_error; |
| 991 | } |
| 992 |
| 993 | static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { |
| 994 | SSL *const ssl = hs->ssl; |
| 995 | hs->can_release_private_key = true; |
| 996 |
| 997 | // Send a Channel ID assertion if necessary. |
| 998 | if (hs->channel_id_negotiated) { |
| 999 | ScopedCBB cbb; |
| 1000 | CBB body; |
| 1001 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
| 1002 | !tls1_write_channel_id(hs, &body) || |
| 1003 | !ssl_add_message_cbb(ssl, cbb.get())) { |
| 1004 | return ssl_hs_error; |
| 1005 | } |
| 1006 | } |
| 1007 |
| 1008 | // Send a Finished message. |
| 1009 | if (!tls13_add_finished(hs)) { |
| 1010 | return ssl_hs_error; |
| 1011 | } |
| 1012 |
| 1013 | // Derive the final keys and enable them. |
| 1014 | if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
| 1015 | hs->new_session.get(), |
| 1016 | hs->client_traffic_secret_0) || |
| 1017 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
| 1018 | hs->new_session.get(), |
| 1019 | hs->server_traffic_secret_0) || |
| 1020 | !tls13_derive_resumption_secret(hs)) { |
| 1021 | return ssl_hs_error; |
| 1022 | } |
| 1023 |
| 1024 | hs->tls13_state = state_done; |
| 1025 | return ssl_hs_flush; |
| 1026 | } |
| 1027 |
| 1028 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
| 1029 | while (hs->tls13_state != state_done) { |
| 1030 | enum ssl_hs_wait_t ret = ssl_hs_error; |
| 1031 | enum client_hs_state_t state = |
| 1032 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
| 1033 | switch (state) { |
| 1034 | case state_read_hello_retry_request: |
| 1035 | ret = do_read_hello_retry_request(hs); |
| 1036 | break; |
| 1037 | case state_send_second_client_hello: |
| 1038 | ret = do_send_second_client_hello(hs); |
| 1039 | break; |
| 1040 | case state_read_server_hello: |
| 1041 | ret = do_read_server_hello(hs); |
| 1042 | break; |
| 1043 | case state_read_encrypted_extensions: |
| 1044 | ret = do_read_encrypted_extensions(hs); |
| 1045 | break; |
| 1046 | case state_read_certificate_request: |
| 1047 | ret = do_read_certificate_request(hs); |
| 1048 | break; |
| 1049 | case state_read_server_certificate: |
| 1050 | ret = do_read_server_certificate(hs); |
| 1051 | break; |
| 1052 | case state_read_server_certificate_verify: |
| 1053 | ret = do_read_server_certificate_verify(hs); |
| 1054 | break; |
| 1055 | case state_server_certificate_reverify: |
| 1056 | ret = do_server_certificate_reverify(hs); |
| 1057 | break; |
| 1058 | case state_read_server_finished: |
| 1059 | ret = do_read_server_finished(hs); |
| 1060 | break; |
| 1061 | case state_send_end_of_early_data: |
| 1062 | ret = do_send_end_of_early_data(hs); |
| 1063 | break; |
| 1064 | case state_send_client_certificate: |
| 1065 | ret = do_send_client_certificate(hs); |
| 1066 | break; |
| 1067 | case state_send_client_encrypted_extensions: |
| 1068 | ret = do_send_client_encrypted_extensions(hs); |
| 1069 | break; |
| 1070 | case state_send_client_certificate_verify: |
| 1071 | ret = do_send_client_certificate_verify(hs); |
| 1072 | break; |
| 1073 | case state_complete_second_flight: |
| 1074 | ret = do_complete_second_flight(hs); |
| 1075 | break; |
| 1076 | case state_done: |
| 1077 | ret = ssl_hs_ok; |
| 1078 | break; |
| 1079 | } |
| 1080 |
| 1081 | if (hs->tls13_state != state) { |
| 1082 | ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1); |
| 1083 | } |
| 1084 |
| 1085 | if (ret != ssl_hs_ok) { |
| 1086 | return ret; |
| 1087 | } |
| 1088 | } |
| 1089 |
| 1090 | return ssl_hs_ok; |
| 1091 | } |
| 1092 |
| 1093 | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) { |
| 1094 | enum client_hs_state_t state = |
| 1095 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
| 1096 | switch (state) { |
| 1097 | case state_read_hello_retry_request: |
| 1098 | return "TLS 1.3 client read_hello_retry_request"; |
| 1099 | case state_send_second_client_hello: |
| 1100 | return "TLS 1.3 client send_second_client_hello"; |
| 1101 | case state_read_server_hello: |
| 1102 | return "TLS 1.3 client read_server_hello"; |
| 1103 | case state_read_encrypted_extensions: |
| 1104 | return "TLS 1.3 client read_encrypted_extensions"; |
| 1105 | case state_read_certificate_request: |
| 1106 | return "TLS 1.3 client read_certificate_request"; |
| 1107 | case state_read_server_certificate: |
| 1108 | return "TLS 1.3 client read_server_certificate"; |
| 1109 | case state_read_server_certificate_verify: |
| 1110 | return "TLS 1.3 client read_server_certificate_verify"; |
| 1111 | case state_server_certificate_reverify: |
| 1112 | return "TLS 1.3 client server_certificate_reverify"; |
| 1113 | case state_read_server_finished: |
| 1114 | return "TLS 1.3 client read_server_finished"; |
| 1115 | case state_send_end_of_early_data: |
| 1116 | return "TLS 1.3 client send_end_of_early_data"; |
| 1117 | case state_send_client_encrypted_extensions: |
| 1118 | return "TLS 1.3 client send_client_encrypted_extensions"; |
| 1119 | case state_send_client_certificate: |
| 1120 | return "TLS 1.3 client send_client_certificate"; |
| 1121 | case state_send_client_certificate_verify: |
| 1122 | return "TLS 1.3 client send_client_certificate_verify"; |
| 1123 | case state_complete_second_flight: |
| 1124 | return "TLS 1.3 client complete_second_flight"; |
| 1125 | case state_done: |
| 1126 | return "TLS 1.3 client done"; |
| 1127 | } |
| 1128 |
| 1129 | return "TLS 1.3 client unknown"; |
| 1130 | } |
| 1131 |
| 1132 | bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) { |
| 1133 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
| 1134 | // Ignore tickets on shutdown. Callers tend to indiscriminately call |
| 1135 | // |SSL_shutdown| before destroying an |SSL|, at which point calling the new |
| 1136 | // session callback may be confusing. |
| 1137 | return true; |
| 1138 | } |
| 1139 |
| 1140 | CBS body = msg.body; |
| 1141 | UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body); |
| 1142 | if (!session) { |
| 1143 | return false; |
| 1144 | } |
| 1145 |
| 1146 | if ((ssl->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) && |
| 1147 | ssl->session_ctx->new_session_cb != NULL && |
| 1148 | ssl->session_ctx->new_session_cb(ssl, session.get())) { |
| 1149 | // |new_session_cb|'s return value signals that it took ownership. |
| 1150 | session.release(); |
| 1151 | } |
| 1152 |
| 1153 | return true; |
| 1154 | } |
| 1155 |
| 1156 | UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, CBS *body) { |
| 1157 | UniquePtr<SSL_SESSION> session = SSL_SESSION_dup( |
| 1158 | ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
| 1159 | if (!session) { |
| 1160 | return nullptr; |
| 1161 | } |
| 1162 |
| 1163 | ssl_session_rebase_time(ssl, session.get()); |
| 1164 |
| 1165 | uint32_t server_timeout; |
| 1166 | CBS ticket_nonce, ticket, extensions; |
| 1167 | if (!CBS_get_u32(body, &server_timeout) || |
| 1168 | !CBS_get_u32(body, &session->ticket_age_add) || |
| 1169 | !CBS_get_u8_length_prefixed(body, &ticket_nonce) || |
| 1170 | !CBS_get_u16_length_prefixed(body, &ticket) || |
| 1171 | CBS_len(&ticket) == 0 || // |
| 1172 | !session->ticket.CopyFrom(ticket) || |
| 1173 | !CBS_get_u16_length_prefixed(body, &extensions) || // |
| 1174 | CBS_len(body) != 0) { |
| 1175 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1176 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1177 | return nullptr; |
| 1178 | } |
| 1179 |
| 1180 | // Cap the renewable lifetime by the server advertised value. This avoids |
| 1181 | // wasting bandwidth on 0-RTT when we know the server will reject it. |
| 1182 | if (session->timeout > server_timeout) { |
| 1183 | session->timeout = server_timeout; |
| 1184 | } |
| 1185 |
| 1186 | if (!tls13_derive_session_psk(session.get(), ticket_nonce, |
| 1187 | SSL_is_dtls(ssl))) { |
| 1188 | return nullptr; |
| 1189 | } |
| 1190 |
| 1191 | SSLExtension early_data(TLSEXT_TYPE_early_data); |
| 1192 | SSLExtension flags(TLSEXT_TYPE_tls_flags); |
| 1193 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 1194 | if (!ssl_parse_extensions(&extensions, &alert, {&early_data, &flags}, |
| 1195 | /*ignore_unknown=*/true)) { |
| 1196 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1197 | return nullptr; |
| 1198 | } |
| 1199 |
| 1200 | if (early_data.present) { |
| 1201 | if (!CBS_get_u32(&early_data.data, &session->ticket_max_early_data) || |
| 1202 | CBS_len(&early_data.data) != 0) { |
| 1203 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1204 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1205 | return nullptr; |
| 1206 | } |
| 1207 |
| 1208 | // QUIC does not use the max_early_data_size parameter and always sets it to |
| 1209 | // a fixed value. See RFC 9001, section 4.6.1. |
| 1210 | if (SSL_is_quic(ssl) && session->ticket_max_early_data != 0xffffffff) { |
| 1211 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 1212 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1213 | return nullptr; |
| 1214 | } |
| 1215 | } |
| 1216 |
| 1217 | if (flags.present) { |
| 1218 | SSLFlags parsed; |
| 1219 | if (!ssl_parse_flags_extension_request(&flags.data, &parsed, &alert)) { |
| 1220 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 1221 | return nullptr; |
| 1222 | } |
| 1223 | if (parsed & kSSLFlagResumptionAcrossNames) { |
| 1224 | session->is_resumable_across_names = true; |
| 1225 | } |
| 1226 | } |
| 1227 |
| 1228 | // Historically, OpenSSL filled in fake session IDs for ticket-based sessions. |
| 1229 | // Envoy's tests depend on this, although perhaps they shouldn't. |
| 1230 | session->session_id.ResizeForOverwrite(SHA256_DIGEST_LENGTH); |
| 1231 | SHA256(CBS_data(&ticket), CBS_len(&ticket), session->session_id.data()); |
| 1232 |
| 1233 | session->ticket_age_add_valid = true; |
| 1234 | session->not_resumable = false; |
| 1235 |
| 1236 | return session; |
| 1237 | } |
| 1238 |
| 1239 | BSSL_NAMESPACE_END |
| 1240 |