QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/tls_client_handshaker.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
dschinazi | 35e749e | 2019-04-09 09:36:04 -0700 | [diff] [blame] | 7 | #include <cstring> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
bnc | 1c9cb6b | 2021-01-23 05:51:24 -0800 | [diff] [blame] | 10 | #include "absl/strings/str_cat.h" |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 11 | #include "absl/strings/string_view.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "third_party/boringssl/src/include/openssl/ssl.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 13 | #include "quic/core/crypto/quic_crypto_client_config.h" |
| 14 | #include "quic/core/crypto/quic_encrypter.h" |
| 15 | #include "quic/core/crypto/transport_parameters.h" |
| 16 | #include "quic/core/quic_session.h" |
| 17 | #include "quic/core/quic_types.h" |
| 18 | #include "quic/platform/api/quic_flags.h" |
| 19 | #include "quic/platform/api/quic_hostname_utils.h" |
bnc | 9de6abe | 2021-04-28 06:24:19 -0700 | [diff] [blame] | 20 | #include "common/quiche_text_utils.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | |
| 22 | namespace quic { |
| 23 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 24 | TlsClientHandshaker::TlsClientHandshaker( |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 25 | const QuicServerId& server_id, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 26 | QuicCryptoStream* stream, |
| 27 | QuicSession* session, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 28 | std::unique_ptr<ProofVerifyContext> verify_context, |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 29 | QuicCryptoClientConfig* crypto_config, |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 30 | QuicCryptoClientStream::ProofHandler* proof_handler, |
| 31 | bool has_application_state) |
nharper | 80d3b17 | 2020-01-17 11:43:39 -0800 | [diff] [blame] | 32 | : TlsHandshaker(stream, session), |
nharper | f579b5e | 2020-01-21 14:11:18 -0800 | [diff] [blame] | 33 | session_(session), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 34 | server_id_(server_id), |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 35 | proof_verifier_(crypto_config->proof_verifier()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 36 | verify_context_(std::move(verify_context)), |
nharper | 40bdf53 | 2019-10-03 11:16:22 -0700 | [diff] [blame] | 37 | proof_handler_(proof_handler), |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 38 | session_cache_(crypto_config->session_cache()), |
| 39 | user_agent_id_(crypto_config->user_agent_id()), |
dschinazi | aaaf1a4 | 2020-04-16 11:44:31 -0700 | [diff] [blame] | 40 | pre_shared_key_(crypto_config->pre_shared_key()), |
nharper | 6ebe83b | 2019-06-13 17:43:52 -0700 | [diff] [blame] | 41 | crypto_negotiated_params_(new QuicCryptoNegotiatedParameters), |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 42 | has_application_state_(has_application_state), |
fayang | 133b868 | 2020-12-08 05:50:33 -0800 | [diff] [blame] | 43 | crypto_config_(crypto_config), |
wub | 2672991 | 2021-06-09 12:04:34 -0700 | [diff] [blame] | 44 | tls_connection_(crypto_config->ssl_ctx(), this, session->GetSSLConfig()) { |
fayang | 5e0ef2b | 2021-05-12 14:16:28 -0700 | [diff] [blame] | 45 | std::string token = |
| 46 | crypto_config->LookupOrCreate(server_id)->source_address_token(); |
| 47 | if (!token.empty()) { |
| 48 | session->SetSourceAddressTokenToSend(token); |
fayang | 133b868 | 2020-12-08 05:50:33 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 51 | |
nharper | e62aab7 | 2020-11-04 16:32:09 -0800 | [diff] [blame] | 52 | TlsClientHandshaker::~TlsClientHandshaker() {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 54 | bool TlsClientHandshaker::CryptoConnect() { |
dschinazi | aaaf1a4 | 2020-04-16 11:44:31 -0700 | [diff] [blame] | 55 | if (!pre_shared_key_.empty()) { |
| 56 | // TODO(b/154162689) add PSK support to QUIC+TLS. |
| 57 | std::string error_details = |
| 58 | "QUIC client pre-shared keys not yet supported with TLS"; |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 59 | QUIC_BUG(quic_bug_10576_1) << error_details; |
dschinazi | aaaf1a4 | 2020-04-16 11:44:31 -0700 | [diff] [blame] | 60 | CloseConnection(QUIC_HANDSHAKE_FAILED, error_details); |
| 61 | return false; |
| 62 | } |
| 63 | |
dschinazi | 869e434 | 2021-02-01 15:41:46 -0800 | [diff] [blame] | 64 | // Make sure we use the right TLS extension codepoint. |
| 65 | int use_legacy_extension = 0; |
| 66 | if (session()->version().UsesLegacyTlsExtension()) { |
| 67 | use_legacy_extension = 1; |
| 68 | } |
| 69 | SSL_set_quic_use_legacy_codepoint(ssl(), use_legacy_extension); |
| 70 | |
nharper | 0f51d2e | 2019-12-11 17:52:05 -0800 | [diff] [blame] | 71 | // Set the SNI to send, if any. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 72 | SSL_set_connect_state(ssl()); |
nharper | e0f979c | 2020-05-05 17:31:55 -0700 | [diff] [blame] | 73 | if (QUIC_DLOG_INFO_IS_ON() && |
| 74 | !QuicHostnameUtils::IsValidSNI(server_id_.host())) { |
| 75 | QUIC_DLOG(INFO) << "Client configured with invalid hostname \"" |
| 76 | << server_id_.host() << "\", not sending as SNI"; |
| 77 | } |
nharper | 0f51d2e | 2019-12-11 17:52:05 -0800 | [diff] [blame] | 78 | if (!server_id_.host().empty() && |
nharper | e0f979c | 2020-05-05 17:31:55 -0700 | [diff] [blame] | 79 | (QuicHostnameUtils::IsValidSNI(server_id_.host()) || |
| 80 | allow_invalid_sni_for_tests_) && |
nharper | 0f51d2e | 2019-12-11 17:52:05 -0800 | [diff] [blame] | 81 | SSL_set_tlsext_host_name(ssl(), server_id_.host().c_str()) != 1) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 85 | if (!SetAlpn()) { |
| 86 | CloseConnection(QUIC_HANDSHAKE_FAILED, "Client failed to set ALPN"); |
dschinazi | 35e749e | 2019-04-09 09:36:04 -0700 | [diff] [blame] | 87 | return false; |
| 88 | } |
dschinazi | 35e749e | 2019-04-09 09:36:04 -0700 | [diff] [blame] | 89 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | // Set the Transport Parameters to send in the ClientHello |
| 91 | if (!SetTransportParameters()) { |
| 92 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 93 | "Client failed to set Transport Parameters"); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 94 | return false; |
| 95 | } |
| 96 | |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 97 | // Set a session to resume, if there is one. |
| 98 | if (session_cache_) { |
nharper | 786a4ab | 2020-07-10 11:56:01 -0700 | [diff] [blame] | 99 | cached_state_ = session_cache_->Lookup(server_id_, SSL_get_SSL_CTX(ssl())); |
nharper | d25cd65 | 2020-05-20 13:10:26 -0700 | [diff] [blame] | 100 | } |
nharper | 786a4ab | 2020-07-10 11:56:01 -0700 | [diff] [blame] | 101 | if (cached_state_) { |
| 102 | SSL_set_session(ssl(), cached_state_->tls_session.get()); |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 103 | } |
| 104 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 105 | // Start the handshake. |
| 106 | AdvanceHandshake(); |
| 107 | return session()->connection()->connected(); |
| 108 | } |
| 109 | |
renjietang | 1a391de | 2020-05-12 10:30:13 -0700 | [diff] [blame] | 110 | bool TlsClientHandshaker::PrepareZeroRttConfig( |
| 111 | QuicResumptionState* cached_state) { |
| 112 | std::string error_details; |
renjietang | 5846898 | 2020-08-18 17:05:29 -0700 | [diff] [blame] | 113 | if (!cached_state->transport_params || |
| 114 | handshaker_delegate()->ProcessTransportParameters( |
dschinazi | 2c4d833 | 2020-05-27 17:23:32 -0700 | [diff] [blame] | 115 | *(cached_state->transport_params), |
renjietang | 1a391de | 2020-05-12 10:30:13 -0700 | [diff] [blame] | 116 | /*is_resumption = */ true, &error_details) != QUIC_NO_ERROR) { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 117 | QUIC_BUG(quic_bug_10576_2) |
QUICHE team | b244e1e | 2021-03-08 15:53:46 -0800 | [diff] [blame] | 118 | << "Unable to parse cached transport parameters."; |
renjietang | 1a391de | 2020-05-12 10:30:13 -0700 | [diff] [blame] | 119 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 120 | "Client failed to parse cached Transport Parameters."); |
| 121 | return false; |
| 122 | } |
renjietang | 8957c6e | 2020-08-12 16:08:21 -0700 | [diff] [blame] | 123 | |
| 124 | session()->connection()->OnTransportParametersResumed( |
| 125 | *(cached_state->transport_params)); |
renjietang | 1a391de | 2020-05-12 10:30:13 -0700 | [diff] [blame] | 126 | session()->OnConfigNegotiated(); |
| 127 | |
| 128 | if (has_application_state_) { |
renjietang | 5846898 | 2020-08-18 17:05:29 -0700 | [diff] [blame] | 129 | if (!cached_state->application_state || |
| 130 | !session()->ResumeApplicationState( |
| 131 | cached_state->application_state.get())) { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 132 | QUIC_BUG(quic_bug_10576_3) << "Unable to parse cached application state."; |
renjietang | 1a391de | 2020-05-12 10:30:13 -0700 | [diff] [blame] | 133 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 134 | "Client failed to parse cached application state."); |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 141 | static bool IsValidAlpn(const std::string& alpn_string) { |
| 142 | return alpn_string.length() <= std::numeric_limits<uint8_t>::max(); |
| 143 | } |
| 144 | |
| 145 | bool TlsClientHandshaker::SetAlpn() { |
| 146 | std::vector<std::string> alpns = session()->GetAlpnsToOffer(); |
| 147 | if (alpns.empty()) { |
| 148 | if (allow_empty_alpn_for_tests_) { |
| 149 | return true; |
| 150 | } |
| 151 | |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 152 | QUIC_BUG(quic_bug_10576_4) << "ALPN missing"; |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 153 | return false; |
| 154 | } |
| 155 | if (!std::all_of(alpns.begin(), alpns.end(), IsValidAlpn)) { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 156 | QUIC_BUG(quic_bug_10576_5) << "ALPN too long"; |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 157 | return false; |
| 158 | } |
| 159 | |
| 160 | // SSL_set_alpn_protos expects a sequence of one-byte-length-prefixed |
| 161 | // strings. |
| 162 | uint8_t alpn[1024]; |
| 163 | QuicDataWriter alpn_writer(sizeof(alpn), reinterpret_cast<char*>(alpn)); |
| 164 | bool success = true; |
| 165 | for (const std::string& alpn_string : alpns) { |
| 166 | success = success && alpn_writer.WriteUInt8(alpn_string.size()) && |
| 167 | alpn_writer.WriteStringPiece(alpn_string); |
| 168 | } |
| 169 | success = |
| 170 | success && (SSL_set_alpn_protos(ssl(), alpn, alpn_writer.length()) == 0); |
| 171 | if (!success) { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 172 | QUIC_BUG(quic_bug_10576_6) |
QUICHE team | b244e1e | 2021-03-08 15:53:46 -0800 | [diff] [blame] | 173 | << "Failed to set ALPN: " |
| 174 | << quiche::QuicheTextUtils::HexDump( |
| 175 | absl::string_view(alpn_writer.data(), alpn_writer.length())); |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
bnc | 1c9cb6b | 2021-01-23 05:51:24 -0800 | [diff] [blame] | 178 | |
bnc | d939efc | 2021-03-08 05:47:44 -0800 | [diff] [blame] | 179 | // Enable ALPS only for versions that use HTTP/3 frames. |
bnc | 126f68e | 2021-04-19 15:48:44 -0700 | [diff] [blame] | 180 | for (const std::string& alpn_string : alpns) { |
| 181 | ParsedQuicVersion version = ParseQuicVersionString(alpn_string); |
| 182 | if (!version.IsKnown() || !version.UsesHttp3()) { |
| 183 | continue; |
| 184 | } |
| 185 | if (SSL_add_application_settings( |
| 186 | ssl(), reinterpret_cast<const uint8_t*>(alpn_string.data()), |
| 187 | alpn_string.size(), nullptr, /* settings_len = */ 0) != 1) { |
| 188 | QUIC_BUG(quic_bug_10576_7) << "Failed to enable ALPS."; |
| 189 | return false; |
bnc | 1c9cb6b | 2021-01-23 05:51:24 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
vasilvv | 4724c9c | 2019-08-29 11:52:11 -0700 | [diff] [blame] | 193 | QUIC_DLOG(INFO) << "Client using ALPN: '" << alpns[0] << "'"; |
| 194 | return true; |
| 195 | } |
| 196 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 197 | bool TlsClientHandshaker::SetTransportParameters() { |
| 198 | TransportParameters params; |
| 199 | params.perspective = Perspective::IS_CLIENT; |
| 200 | params.version = |
| 201 | CreateQuicVersionLabel(session()->supported_versions().front()); |
| 202 | |
dschinazi | 2c4d833 | 2020-05-27 17:23:32 -0700 | [diff] [blame] | 203 | if (!handshaker_delegate()->FillTransportParameters(¶ms)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 204 | return false; |
| 205 | } |
dschinazi | 83c586a | 2020-07-07 11:50:45 -0700 | [diff] [blame] | 206 | if (!user_agent_id_.empty()) { |
| 207 | params.user_agent_id = user_agent_id_; |
dschinazi | 7e51f6b | 2020-05-13 17:36:44 -0700 | [diff] [blame] | 208 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 209 | |
dschinazi | 631f160 | 2020-05-19 10:10:22 -0700 | [diff] [blame] | 210 | // Notify QuicConnectionDebugVisitor. |
| 211 | session()->connection()->OnTransportParametersSent(params); |
| 212 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 213 | std::vector<uint8_t> param_bytes; |
dschinazi | 6c84c14 | 2019-07-31 09:11:49 -0700 | [diff] [blame] | 214 | return SerializeTransportParameters(session()->connection()->version(), |
| 215 | params, ¶m_bytes) && |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 216 | SSL_set_quic_transport_params(ssl(), param_bytes.data(), |
| 217 | param_bytes.size()) == 1; |
| 218 | } |
| 219 | |
| 220 | bool TlsClientHandshaker::ProcessTransportParameters( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 221 | std::string* error_details) { |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 222 | received_transport_params_ = std::make_unique<TransportParameters>(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 223 | const uint8_t* param_bytes; |
| 224 | size_t param_bytes_len; |
| 225 | SSL_get_peer_quic_transport_params(ssl(), ¶m_bytes, ¶m_bytes_len); |
dschinazi | 7b8f0c7 | 2020-03-02 13:17:57 -0800 | [diff] [blame] | 226 | if (param_bytes_len == 0) { |
| 227 | *error_details = "Server's transport parameters are missing"; |
| 228 | return false; |
| 229 | } |
| 230 | std::string parse_error_details; |
| 231 | if (!ParseTransportParameters( |
| 232 | session()->connection()->version(), Perspective::IS_SERVER, |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 233 | param_bytes, param_bytes_len, received_transport_params_.get(), |
| 234 | &parse_error_details)) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 235 | QUICHE_DCHECK(!parse_error_details.empty()); |
dschinazi | 7b8f0c7 | 2020-03-02 13:17:57 -0800 | [diff] [blame] | 236 | *error_details = |
| 237 | "Unable to parse server's transport parameters: " + parse_error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
dschinazi | 631f160 | 2020-05-19 10:10:22 -0700 | [diff] [blame] | 241 | // Notify QuicConnectionDebugVisitor. |
| 242 | session()->connection()->OnTransportParametersReceived( |
| 243 | *received_transport_params_); |
| 244 | |
dschinazi | 6cf4d2a | 2019-04-30 16:20:23 -0700 | [diff] [blame] | 245 | // When interoperating with non-Google implementations that do not send |
| 246 | // the version extension, set it to what we expect. |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 247 | if (received_transport_params_->version == 0) { |
| 248 | received_transport_params_->version = |
| 249 | CreateQuicVersionLabel(session()->connection()->version()); |
dschinazi | 6cf4d2a | 2019-04-30 16:20:23 -0700 | [diff] [blame] | 250 | } |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 251 | if (received_transport_params_->supported_versions.empty()) { |
| 252 | received_transport_params_->supported_versions.push_back( |
| 253 | received_transport_params_->version); |
dschinazi | 6cf4d2a | 2019-04-30 16:20:23 -0700 | [diff] [blame] | 254 | } |
| 255 | |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 256 | if (received_transport_params_->version != |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 257 | CreateQuicVersionLabel(session()->connection()->version())) { |
| 258 | *error_details = "Version mismatch detected"; |
| 259 | return false; |
| 260 | } |
| 261 | if (CryptoUtils::ValidateServerHelloVersions( |
renjietang | cc12f43 | 2020-04-09 16:44:22 -0700 | [diff] [blame] | 262 | received_transport_params_->supported_versions, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 263 | session()->connection()->server_supported_versions(), |
| 264 | error_details) != QUIC_NO_ERROR || |
dschinazi | 2c4d833 | 2020-05-27 17:23:32 -0700 | [diff] [blame] | 265 | handshaker_delegate()->ProcessTransportParameters( |
| 266 | *received_transport_params_, /* is_resumption = */ false, |
renjietang | c31106b | 2020-05-06 10:44:17 -0700 | [diff] [blame] | 267 | error_details) != QUIC_NO_ERROR) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 268 | QUICHE_DCHECK(!error_details->empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
| 272 | session()->OnConfigNegotiated(); |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 273 | if (is_connection_closed()) { |
renjietang | 92346b6 | 2020-05-11 13:15:24 -0700 | [diff] [blame] | 274 | *error_details = |
| 275 | "Session closed the connection when parsing negotiated config."; |
| 276 | return false; |
| 277 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 278 | return true; |
| 279 | } |
| 280 | |
| 281 | int TlsClientHandshaker::num_sent_client_hellos() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
nharper | 0270396 | 2019-11-07 12:23:13 -0800 | [diff] [blame] | 285 | bool TlsClientHandshaker::IsResumption() const { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 286 | QUIC_BUG_IF(quic_bug_12736_1, !one_rtt_keys_available()); |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 287 | return SSL_session_reused(ssl()) == 1; |
nharper | 0270396 | 2019-11-07 12:23:13 -0800 | [diff] [blame] | 288 | } |
| 289 | |
nharper | 4084fc9 | 2020-02-10 14:43:35 -0800 | [diff] [blame] | 290 | bool TlsClientHandshaker::EarlyDataAccepted() const { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 291 | QUIC_BUG_IF(quic_bug_12736_2, !one_rtt_keys_available()); |
nharper | 4084fc9 | 2020-02-10 14:43:35 -0800 | [diff] [blame] | 292 | return SSL_early_data_accepted(ssl()) == 1; |
| 293 | } |
| 294 | |
nharper | 26e3e88 | 2020-09-09 12:30:55 -0700 | [diff] [blame] | 295 | ssl_early_data_reason_t TlsClientHandshaker::EarlyDataReason() const { |
nharper | 6a6bd31 | 2020-09-17 13:20:53 -0700 | [diff] [blame] | 296 | return TlsHandshaker::EarlyDataReason(); |
nharper | 26e3e88 | 2020-09-09 12:30:55 -0700 | [diff] [blame] | 297 | } |
| 298 | |
nharper | 4084fc9 | 2020-02-10 14:43:35 -0800 | [diff] [blame] | 299 | bool TlsClientHandshaker::ReceivedInchoateReject() const { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 300 | QUIC_BUG_IF(quic_bug_12736_3, !one_rtt_keys_available()); |
nharper | 4084fc9 | 2020-02-10 14:43:35 -0800 | [diff] [blame] | 301 | // REJ messages are a QUIC crypto feature, so TLS always returns false. |
| 302 | return false; |
| 303 | } |
| 304 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 305 | int TlsClientHandshaker::num_scup_messages_received() const { |
| 306 | // SCUP messages aren't sent or received when using the TLS handshake. |
| 307 | return 0; |
| 308 | } |
| 309 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 310 | std::string TlsClientHandshaker::chlo_hash() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 311 | return ""; |
| 312 | } |
| 313 | |
| 314 | bool TlsClientHandshaker::encryption_established() const { |
| 315 | return encryption_established_; |
| 316 | } |
| 317 | |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 318 | bool TlsClientHandshaker::one_rtt_keys_available() const { |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 319 | return state_ >= HANDSHAKE_COMPLETE; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | const QuicCryptoNegotiatedParameters& |
| 323 | TlsClientHandshaker::crypto_negotiated_params() const { |
| 324 | return *crypto_negotiated_params_; |
| 325 | } |
| 326 | |
| 327 | CryptoMessageParser* TlsClientHandshaker::crypto_message_parser() { |
| 328 | return TlsHandshaker::crypto_message_parser(); |
| 329 | } |
| 330 | |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 331 | HandshakeState TlsClientHandshaker::GetHandshakeState() const { |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 332 | return state_; |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 333 | } |
| 334 | |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 335 | size_t TlsClientHandshaker::BufferSizeLimitForLevel( |
| 336 | EncryptionLevel level) const { |
| 337 | return TlsHandshaker::BufferSizeLimitForLevel(level); |
| 338 | } |
| 339 | |
mattm | 072a7e3 | 2020-10-09 16:16:56 -0700 | [diff] [blame] | 340 | bool TlsClientHandshaker::KeyUpdateSupportedLocally() const { |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | std::unique_ptr<QuicDecrypter> |
| 345 | TlsClientHandshaker::AdvanceKeysAndCreateCurrentOneRttDecrypter() { |
| 346 | return TlsHandshaker::AdvanceKeysAndCreateCurrentOneRttDecrypter(); |
| 347 | } |
| 348 | |
| 349 | std::unique_ptr<QuicEncrypter> |
| 350 | TlsClientHandshaker::CreateCurrentOneRttEncrypter() { |
| 351 | return TlsHandshaker::CreateCurrentOneRttEncrypter(); |
| 352 | } |
| 353 | |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 354 | void TlsClientHandshaker::OnOneRttPacketAcknowledged() { |
| 355 | OnHandshakeConfirmed(); |
| 356 | } |
| 357 | |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 358 | void TlsClientHandshaker::OnHandshakePacketSent() { |
| 359 | if (initial_keys_dropped_) { |
| 360 | return; |
| 361 | } |
fayang | bfc15ea | 2020-05-13 12:48:32 -0700 | [diff] [blame] | 362 | initial_keys_dropped_ = true; |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 363 | handshaker_delegate()->DiscardOldEncryptionKey(ENCRYPTION_INITIAL); |
| 364 | handshaker_delegate()->DiscardOldDecryptionKey(ENCRYPTION_INITIAL); |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 365 | } |
| 366 | |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 367 | void TlsClientHandshaker::OnConnectionClosed(QuicErrorCode error, |
| 368 | ConnectionCloseSource source) { |
| 369 | TlsHandshaker::OnConnectionClosed(error, source); |
fayang | a6a85a8 | 2020-05-04 08:58:53 -0700 | [diff] [blame] | 370 | } |
| 371 | |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 372 | void TlsClientHandshaker::OnHandshakeDoneReceived() { |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 373 | if (!one_rtt_keys_available()) { |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 374 | CloseConnection(QUIC_HANDSHAKE_FAILED, |
| 375 | "Unexpected handshake done received"); |
| 376 | return; |
| 377 | } |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 378 | OnHandshakeConfirmed(); |
| 379 | } |
| 380 | |
fayang | 133b868 | 2020-12-08 05:50:33 -0800 | [diff] [blame] | 381 | void TlsClientHandshaker::OnNewTokenReceived(absl::string_view token) { |
| 382 | if (token.empty()) { |
| 383 | return; |
| 384 | } |
| 385 | QuicCryptoClientConfig::CachedState* cached = |
| 386 | crypto_config_->LookupOrCreate(server_id_); |
| 387 | cached->set_source_address_token(token); |
| 388 | } |
| 389 | |
fayang | a45ee8a | 2020-03-20 08:56:11 -0700 | [diff] [blame] | 390 | void TlsClientHandshaker::SetWriteSecret( |
| 391 | EncryptionLevel level, |
| 392 | const SSL_CIPHER* cipher, |
| 393 | const std::vector<uint8_t>& write_secret) { |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 394 | if (is_connection_closed()) { |
fayang | a6a85a8 | 2020-05-04 08:58:53 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
renjietang | ce663db | 2020-05-28 13:50:15 -0700 | [diff] [blame] | 397 | if (level == ENCRYPTION_FORWARD_SECURE || level == ENCRYPTION_ZERO_RTT) { |
fayang | a45ee8a | 2020-03-20 08:56:11 -0700 | [diff] [blame] | 398 | encryption_established_ = true; |
| 399 | } |
| 400 | TlsHandshaker::SetWriteSecret(level, cipher, write_secret); |
fayang | bb003dd | 2020-10-27 15:25:26 -0700 | [diff] [blame] | 401 | if (level == ENCRYPTION_FORWARD_SECURE) { |
fayang | 9261a8a | 2020-09-22 07:29:46 -0700 | [diff] [blame] | 402 | handshaker_delegate()->DiscardOldEncryptionKey(ENCRYPTION_ZERO_RTT); |
| 403 | } |
fayang | a45ee8a | 2020-03-20 08:56:11 -0700 | [diff] [blame] | 404 | } |
| 405 | |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 406 | void TlsClientHandshaker::OnHandshakeConfirmed() { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 407 | QUICHE_DCHECK(one_rtt_keys_available()); |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 408 | if (state_ >= HANDSHAKE_CONFIRMED) { |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 409 | return; |
| 410 | } |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 411 | state_ = HANDSHAKE_CONFIRMED; |
renjietang | bd33b62 | 2020-02-12 16:52:30 -0800 | [diff] [blame] | 412 | handshaker_delegate()->DiscardOldEncryptionKey(ENCRYPTION_HANDSHAKE); |
| 413 | handshaker_delegate()->DiscardOldDecryptionKey(ENCRYPTION_HANDSHAKE); |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 414 | } |
| 415 | |
nharper | e62aab7 | 2020-11-04 16:32:09 -0800 | [diff] [blame] | 416 | QuicAsyncStatus TlsClientHandshaker::VerifyCertChain( |
| 417 | const std::vector<std::string>& certs, |
| 418 | std::string* error_details, |
| 419 | std::unique_ptr<ProofVerifyDetails>* details, |
nharper | 54fc9ab | 2020-11-12 11:07:39 -0800 | [diff] [blame] | 420 | uint8_t* out_alert, |
nharper | e62aab7 | 2020-11-04 16:32:09 -0800 | [diff] [blame] | 421 | std::unique_ptr<ProofVerifierCallback> callback) { |
| 422 | const uint8_t* ocsp_response_raw; |
| 423 | size_t ocsp_response_len; |
| 424 | SSL_get0_ocsp_response(ssl(), &ocsp_response_raw, &ocsp_response_len); |
| 425 | std::string ocsp_response(reinterpret_cast<const char*>(ocsp_response_raw), |
| 426 | ocsp_response_len); |
| 427 | const uint8_t* sct_list_raw; |
| 428 | size_t sct_list_len; |
| 429 | SSL_get0_signed_cert_timestamp_list(ssl(), &sct_list_raw, &sct_list_len); |
| 430 | std::string sct_list(reinterpret_cast<const char*>(sct_list_raw), |
| 431 | sct_list_len); |
| 432 | |
| 433 | return proof_verifier_->VerifyCertChain( |
| 434 | server_id_.host(), server_id_.port(), certs, ocsp_response, sct_list, |
nharper | 54fc9ab | 2020-11-12 11:07:39 -0800 | [diff] [blame] | 435 | verify_context_.get(), error_details, details, out_alert, |
| 436 | std::move(callback)); |
nharper | e62aab7 | 2020-11-04 16:32:09 -0800 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void TlsClientHandshaker::OnProofVerifyDetailsAvailable( |
| 440 | const ProofVerifyDetails& verify_details) { |
| 441 | proof_handler_->OnProofVerifyDetailsAvailable(verify_details); |
| 442 | } |
| 443 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 444 | void TlsClientHandshaker::FinishHandshake() { |
wub | 48b1681 | 2021-04-30 10:22:22 -0700 | [diff] [blame] | 445 | FillNegotiatedParams(); |
nharper | 786a4ab | 2020-07-10 11:56:01 -0700 | [diff] [blame] | 446 | |
wub | 48b1681 | 2021-04-30 10:22:22 -0700 | [diff] [blame] | 447 | if (retry_handshake_on_early_data_) { |
| 448 | QUICHE_CHECK(!SSL_in_early_data(ssl())); |
| 449 | } else { |
| 450 | if (SSL_in_early_data(ssl())) { |
| 451 | // SSL_do_handshake returns after sending the ClientHello if the session |
| 452 | // is 0-RTT-capable, which means that FinishHandshake will get called |
| 453 | // twice - the first time after sending the ClientHello, and the second |
| 454 | // time after the handshake is complete. If we're in the first time |
| 455 | // FinishHandshake is called, we can't do any end-of-handshake processing. |
| 456 | |
| 457 | // If we're attempting a 0-RTT handshake, then we need to let the |
| 458 | // transport and application know what state to apply to early data. |
| 459 | PrepareZeroRttConfig(cached_state_.get()); |
| 460 | return; |
| 461 | } |
nharper | d25cd65 | 2020-05-20 13:10:26 -0700 | [diff] [blame] | 462 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 463 | QUIC_LOG(INFO) << "Client: handshake finished"; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 464 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 465 | std::string error_details; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 466 | if (!ProcessTransportParameters(&error_details)) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 467 | QUICHE_DCHECK(!error_details.empty()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 468 | CloseConnection(QUIC_HANDSHAKE_FAILED, error_details); |
| 469 | return; |
| 470 | } |
| 471 | |
dschinazi | 35e749e | 2019-04-09 09:36:04 -0700 | [diff] [blame] | 472 | const uint8_t* alpn_data = nullptr; |
| 473 | unsigned alpn_length = 0; |
| 474 | SSL_get0_alpn_selected(ssl(), &alpn_data, &alpn_length); |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 475 | |
| 476 | if (alpn_length == 0) { |
| 477 | QUIC_DLOG(ERROR) << "Client: server did not select ALPN"; |
| 478 | // TODO(b/130164908) this should send no_application_protocol |
| 479 | // instead of QUIC_HANDSHAKE_FAILED. |
| 480 | CloseConnection(QUIC_HANDSHAKE_FAILED, "Server did not select ALPN"); |
| 481 | return; |
dschinazi | 35e749e | 2019-04-09 09:36:04 -0700 | [diff] [blame] | 482 | } |
| 483 | |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 484 | std::string received_alpn_string(reinterpret_cast<const char*>(alpn_data), |
| 485 | alpn_length); |
vasilvv | ad7424f | 2019-08-30 00:27:14 -0700 | [diff] [blame] | 486 | std::vector<std::string> offered_alpns = session()->GetAlpnsToOffer(); |
| 487 | if (std::find(offered_alpns.begin(), offered_alpns.end(), |
| 488 | received_alpn_string) == offered_alpns.end()) { |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 489 | QUIC_LOG(ERROR) << "Client: received mismatched ALPN '" |
vasilvv | ad7424f | 2019-08-30 00:27:14 -0700 | [diff] [blame] | 490 | << received_alpn_string; |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 491 | // TODO(b/130164908) this should send no_application_protocol |
| 492 | // instead of QUIC_HANDSHAKE_FAILED. |
| 493 | CloseConnection(QUIC_HANDSHAKE_FAILED, "Client received mismatched ALPN"); |
| 494 | return; |
| 495 | } |
vasilvv | ad7424f | 2019-08-30 00:27:14 -0700 | [diff] [blame] | 496 | session()->OnAlpnSelected(received_alpn_string); |
dschinazi | 9145364 | 2019-08-01 11:12:15 -0700 | [diff] [blame] | 497 | QUIC_DLOG(INFO) << "Client: server selected ALPN: '" << received_alpn_string |
| 498 | << "'"; |
bnc | 1c9cb6b | 2021-01-23 05:51:24 -0800 | [diff] [blame] | 499 | |
| 500 | // Parse ALPS extension. |
bnc | 126f68e | 2021-04-19 15:48:44 -0700 | [diff] [blame] | 501 | const uint8_t* alps_data; |
| 502 | size_t alps_length; |
| 503 | SSL_get0_peer_application_settings(ssl(), &alps_data, &alps_length); |
| 504 | if (alps_length > 0) { |
| 505 | auto error = session()->OnAlpsData(alps_data, alps_length); |
| 506 | if (error) { |
| 507 | // Calling CloseConnection() is safe even in case OnAlpsData() has |
| 508 | // already closed the connection. |
| 509 | CloseConnection( |
| 510 | QUIC_HANDSHAKE_FAILED, |
| 511 | absl::StrCat("Error processing ALPS data: ", error.value())); |
| 512 | return; |
bnc | 1c9cb6b | 2021-01-23 05:51:24 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 516 | state_ = HANDSHAKE_COMPLETE; |
renjietang | c50cc4a | 2020-08-17 18:33:30 -0700 | [diff] [blame] | 517 | handshaker_delegate()->OnTlsHandshakeComplete(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 518 | } |
| 519 | |
wub | 48b1681 | 2021-04-30 10:22:22 -0700 | [diff] [blame] | 520 | void TlsClientHandshaker::OnEnterEarlyData() { |
| 521 | QUICHE_DCHECK(retry_handshake_on_early_data_); |
| 522 | QUICHE_DCHECK(SSL_in_early_data(ssl())); |
| 523 | |
| 524 | // TODO(wub): It might be unnecessary to FillNegotiatedParams() at this time, |
| 525 | // because we fill it again when handshake completes. |
| 526 | FillNegotiatedParams(); |
| 527 | |
| 528 | // If we're attempting a 0-RTT handshake, then we need to let the transport |
| 529 | // and application know what state to apply to early data. |
| 530 | PrepareZeroRttConfig(cached_state_.get()); |
| 531 | } |
| 532 | |
| 533 | void TlsClientHandshaker::FillNegotiatedParams() { |
| 534 | const SSL_CIPHER* cipher = SSL_get_current_cipher(ssl()); |
| 535 | if (cipher) { |
| 536 | crypto_negotiated_params_->cipher_suite = |
| 537 | SSL_CIPHER_get_protocol_id(cipher); |
| 538 | } |
| 539 | crypto_negotiated_params_->key_exchange_group = SSL_get_curve_id(ssl()); |
| 540 | crypto_negotiated_params_->peer_signature_algorithm = |
| 541 | SSL_get_peer_signature_algorithm(ssl()); |
| 542 | } |
| 543 | |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 544 | void TlsClientHandshaker::ProcessPostHandshakeMessage() { |
| 545 | int rv = SSL_process_quic_post_handshake(ssl()); |
| 546 | if (rv != 1) { |
| 547 | CloseConnection(QUIC_HANDSHAKE_FAILED, "Unexpected post-handshake data"); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | bool TlsClientHandshaker::ShouldCloseConnectionOnUnexpectedError( |
| 552 | int ssl_error) { |
| 553 | if (ssl_error != SSL_ERROR_EARLY_DATA_REJECTED) { |
| 554 | return true; |
| 555 | } |
| 556 | HandleZeroRttReject(); |
| 557 | return false; |
| 558 | } |
| 559 | |
nharper | d25cd65 | 2020-05-20 13:10:26 -0700 | [diff] [blame] | 560 | void TlsClientHandshaker::HandleZeroRttReject() { |
| 561 | QUIC_LOG(INFO) << "0-RTT handshake attempted but was rejected by the server"; |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 562 | QUICHE_DCHECK(session_cache_); |
renjietang | 37d4ae1 | 2020-06-18 11:18:31 -0700 | [diff] [blame] | 563 | // Disable encrytion to block outgoing data until 1-RTT keys are available. |
| 564 | encryption_established_ = false; |
renjietang | f71e806 | 2020-09-14 12:01:15 -0700 | [diff] [blame] | 565 | handshaker_delegate()->OnZeroRttRejected(EarlyDataReason()); |
nharper | d25cd65 | 2020-05-20 13:10:26 -0700 | [diff] [blame] | 566 | SSL_reset_early_data_reject(ssl()); |
renjietang | 8aff71f | 2020-05-21 17:24:29 -0700 | [diff] [blame] | 567 | session_cache_->ClearEarlyData(server_id_); |
nharper | d25cd65 | 2020-05-20 13:10:26 -0700 | [diff] [blame] | 568 | AdvanceHandshake(); |
| 569 | } |
| 570 | |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 571 | void TlsClientHandshaker::InsertSession(bssl::UniquePtr<SSL_SESSION> session) { |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 572 | if (!received_transport_params_) { |
QUICHE team | 460f270 | 2021-03-16 15:08:15 -0700 | [diff] [blame] | 573 | QUIC_BUG(quic_bug_10576_8) << "Transport parameters isn't received"; |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 574 | return; |
| 575 | } |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 576 | if (session_cache_ == nullptr) { |
| 577 | QUIC_DVLOG(1) << "No session cache, not inserting a session"; |
| 578 | return; |
| 579 | } |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 580 | if (has_application_state_ && !received_application_state_) { |
| 581 | // Application state is not received yet. cache the sessions. |
| 582 | if (cached_tls_sessions_[0] != nullptr) { |
| 583 | cached_tls_sessions_[1] = std::move(cached_tls_sessions_[0]); |
| 584 | } |
| 585 | cached_tls_sessions_[0] = std::move(session); |
| 586 | return; |
| 587 | } |
| 588 | session_cache_->Insert(server_id_, std::move(session), |
renjietang | b5c6935 | 2020-04-22 17:56:04 -0700 | [diff] [blame] | 589 | *received_transport_params_, |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 590 | received_application_state_.get()); |
nharper | df7a77b | 2019-11-11 13:12:45 -0800 | [diff] [blame] | 591 | } |
| 592 | |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 593 | void TlsClientHandshaker::WriteMessage(EncryptionLevel level, |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 594 | absl::string_view data) { |
nharper | 0ab1c66 | 2020-11-02 17:05:32 -0800 | [diff] [blame] | 595 | if (level == ENCRYPTION_HANDSHAKE && state_ < HANDSHAKE_PROCESSED) { |
| 596 | state_ = HANDSHAKE_PROCESSED; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 597 | } |
| 598 | TlsHandshaker::WriteMessage(level, data); |
| 599 | } |
| 600 | |
nharper | ac52a86 | 2020-06-08 12:41:06 -0700 | [diff] [blame] | 601 | void TlsClientHandshaker::SetServerApplicationStateForResumption( |
renjietang | f21e385 | 2020-04-13 15:45:39 -0700 | [diff] [blame] | 602 | std::unique_ptr<ApplicationState> application_state) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 603 | QUICHE_DCHECK(one_rtt_keys_available()); |
renjietang | f21e385 | 2020-04-13 15:45:39 -0700 | [diff] [blame] | 604 | received_application_state_ = std::move(application_state); |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 605 | // At least one tls session is cached before application state is received. So |
| 606 | // insert now. |
| 607 | if (session_cache_ != nullptr && cached_tls_sessions_[0] != nullptr) { |
| 608 | if (cached_tls_sessions_[1] != nullptr) { |
| 609 | // Insert the older session first. |
| 610 | session_cache_->Insert(server_id_, std::move(cached_tls_sessions_[1]), |
renjietang | b5c6935 | 2020-04-22 17:56:04 -0700 | [diff] [blame] | 611 | *received_transport_params_, |
renjietang | bcc066a | 2020-04-21 18:05:57 -0700 | [diff] [blame] | 612 | received_application_state_.get()); |
| 613 | } |
| 614 | session_cache_->Insert(server_id_, std::move(cached_tls_sessions_[0]), |
renjietang | b5c6935 | 2020-04-22 17:56:04 -0700 | [diff] [blame] | 615 | *received_transport_params_, |
renjietang | 28e3a76 | 2020-04-16 12:16:17 -0700 | [diff] [blame] | 616 | received_application_state_.get()); |
| 617 | } |
renjietang | f21e385 | 2020-04-13 15:45:39 -0700 | [diff] [blame] | 618 | } |
| 619 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 620 | } // namespace quic |