QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 5 | #ifndef QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_ |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 10 | #include "quic/core/proto/cached_network_parameters_proto.h" |
| 11 | #include "quic/core/proto/source_address_token_proto.h" |
| 12 | #include "quic/core/quic_crypto_handshaker.h" |
| 13 | #include "quic/core/quic_crypto_server_stream_base.h" |
| 14 | #include "quic/core/quic_session.h" |
| 15 | #include "quic/platform/api/quic_export.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | namespace test { |
| 20 | class QuicCryptoServerStreamPeer; |
| 21 | } // namespace test |
| 22 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 23 | class QUIC_EXPORT_PRIVATE QuicCryptoServerStream |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 24 | : public QuicCryptoServerStreamBase, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | public QuicCryptoHandshaker { |
| 26 | public: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 27 | QuicCryptoServerStream(const QuicCryptoServerStream&) = delete; |
| 28 | QuicCryptoServerStream& operator=(const QuicCryptoServerStream&) = delete; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 29 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 30 | ~QuicCryptoServerStream() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 32 | // From QuicCryptoServerStreamBase |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | void CancelOutstandingCallbacks() override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 34 | bool GetBase64SHA256ClientChannelID(std::string* output) const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | void SendServerConfigUpdate( |
| 36 | const CachedNetworkParameters* cached_network_params) override; |
fayang | 6098a0a | 2020-03-13 15:32:10 -0700 | [diff] [blame] | 37 | bool IsZeroRtt() const override; |
nharper | fd0e263 | 2020-06-02 11:19:05 -0700 | [diff] [blame] | 38 | bool IsResumption() const override; |
| 39 | bool ResumptionAttempted() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | int NumServerConfigUpdateMessagesSent() const override; |
| 41 | const CachedNetworkParameters* PreviousCachedNetworkParams() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 42 | void SetPreviousCachedNetworkParams( |
| 43 | CachedNetworkParameters cached_network_params) override; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 44 | void OnPacketDecrypted(EncryptionLevel level) override; |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 45 | void OnOneRttPacketAcknowledged() override {} |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 46 | void OnHandshakePacketSent() override {} |
fayang | a6a85a8 | 2020-05-04 08:58:53 -0700 | [diff] [blame] | 47 | void OnConnectionClosed(QuicErrorCode /*error*/, |
| 48 | ConnectionCloseSource /*source*/) override {} |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 49 | void OnHandshakeDoneReceived() override; |
fayang | 133b868 | 2020-12-08 05:50:33 -0800 | [diff] [blame] | 50 | void OnNewTokenReceived(absl::string_view token) override; |
| 51 | std::string GetAddressToken() const override; |
| 52 | bool ValidateAddressToken(absl::string_view token) const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 53 | bool ShouldSendExpectCTHeader() const override; |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 54 | const ProofSource::Details* ProofSourceDetails() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | |
| 56 | // From QuicCryptoStream |
nharper | 6a6bd31 | 2020-09-17 13:20:53 -0700 | [diff] [blame] | 57 | ssl_early_data_reason_t EarlyDataReason() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 58 | bool encryption_established() const override; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 59 | bool one_rtt_keys_available() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 61 | const override; |
| 62 | CryptoMessageParser* crypto_message_parser() override; |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 63 | HandshakeState GetHandshakeState() const override; |
nharper | ac52a86 | 2020-06-08 12:41:06 -0700 | [diff] [blame] | 64 | void SetServerApplicationStateForResumption( |
| 65 | std::unique_ptr<ApplicationState> state) override; |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 66 | size_t BufferSizeLimitForLevel(EncryptionLevel level) const override; |
mattm | 072a7e3 | 2020-10-09 16:16:56 -0700 | [diff] [blame] | 67 | bool KeyUpdateSupportedLocally() const override; |
| 68 | std::unique_ptr<QuicDecrypter> AdvanceKeysAndCreateCurrentOneRttDecrypter() |
| 69 | override; |
| 70 | std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter() override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 71 | |
| 72 | // From QuicCryptoHandshaker |
| 73 | void OnHandshakeMessage(const CryptoHandshakeMessage& message) override; |
| 74 | |
| 75 | protected: |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 76 | QUIC_EXPORT_PRIVATE friend std::unique_ptr<QuicCryptoServerStreamBase> |
| 77 | CreateCryptoServerStream(const QuicCryptoServerConfig* crypto_config, |
| 78 | QuicCompressedCertsCache* compressed_certs_cache, |
| 79 | QuicSession* session, |
| 80 | QuicCryptoServerStreamBase::Helper* helper); |
| 81 | |
| 82 | // |crypto_config| must outlive the stream. |
| 83 | // |session| must outlive the stream. |
| 84 | // |helper| must outlive the stream. |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 85 | QuicCryptoServerStream(const QuicCryptoServerConfig* crypto_config, |
| 86 | QuicCompressedCertsCache* compressed_certs_cache, |
| 87 | QuicSession* session, |
| 88 | QuicCryptoServerStreamBase::Helper* helper); |
nharper | bb7d1f3 | 2020-02-21 17:20:33 -0800 | [diff] [blame] | 89 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | virtual void ProcessClientHello( |
| 91 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 92 | result, |
| 93 | std::unique_ptr<ProofSource::Details> proof_source_details, |
| 94 | std::unique_ptr<ProcessClientHelloResultCallback> done_cb); |
| 95 | |
| 96 | // Hook that allows the server to set QuicConfig defaults just |
| 97 | // before going through the parameter negotiation step. |
| 98 | virtual void OverrideQuicConfigDefaults(QuicConfig* config); |
| 99 | |
| 100 | // Returns client address used to generate and validate source address token. |
| 101 | virtual const QuicSocketAddress GetClientAddress(); |
| 102 | |
| 103 | // Returns the QuicSession that this stream belongs to. |
| 104 | QuicSession* session() const { return session_; } |
| 105 | |
| 106 | void set_encryption_established(bool encryption_established) { |
| 107 | encryption_established_ = encryption_established; |
| 108 | } |
| 109 | |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 110 | void set_one_rtt_keys_available(bool one_rtt_keys_available) { |
| 111 | one_rtt_keys_available_ = one_rtt_keys_available; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | private: |
| 115 | friend class test::QuicCryptoServerStreamPeer; |
| 116 | |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 117 | class QUIC_EXPORT_PRIVATE ValidateCallback |
| 118 | : public ValidateClientHelloResultCallback { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 119 | public: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 120 | explicit ValidateCallback(QuicCryptoServerStream* parent); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 121 | ValidateCallback(const ValidateCallback&) = delete; |
| 122 | ValidateCallback& operator=(const ValidateCallback&) = delete; |
| 123 | // To allow the parent to detach itself from the callback before deletion. |
| 124 | void Cancel(); |
| 125 | |
| 126 | // From ValidateClientHelloResultCallback |
| 127 | void Run(QuicReferenceCountedPointer<Result> result, |
| 128 | std::unique_ptr<ProofSource::Details> details) override; |
| 129 | |
| 130 | private: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 131 | QuicCryptoServerStream* parent_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | class SendServerConfigUpdateCallback |
| 135 | : public BuildServerConfigUpdateMessageResultCallback { |
| 136 | public: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 137 | explicit SendServerConfigUpdateCallback(QuicCryptoServerStream* parent); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 138 | SendServerConfigUpdateCallback(const SendServerConfigUpdateCallback&) = |
| 139 | delete; |
| 140 | void operator=(const SendServerConfigUpdateCallback&) = delete; |
| 141 | |
| 142 | // To allow the parent to detach itself from the callback before deletion. |
| 143 | void Cancel(); |
| 144 | |
| 145 | // From BuildServerConfigUpdateMessageResultCallback |
| 146 | void Run(bool ok, const CryptoHandshakeMessage& message) override; |
| 147 | |
| 148 | private: |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 149 | QuicCryptoServerStream* parent_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // Invoked by ValidateCallback::RunImpl once initial validation of |
| 153 | // the client hello is complete. Finishes processing of the client |
| 154 | // hello message and handles handshake success/failure. |
| 155 | void FinishProcessingHandshakeMessage( |
| 156 | QuicReferenceCountedPointer<ValidateClientHelloResultCallback::Result> |
| 157 | result, |
| 158 | std::unique_ptr<ProofSource::Details> details); |
| 159 | |
| 160 | class ProcessClientHelloCallback; |
| 161 | friend class ProcessClientHelloCallback; |
| 162 | |
| 163 | // Portion of FinishProcessingHandshakeMessage which executes after |
| 164 | // ProcessClientHello has been called. |
| 165 | void FinishProcessingHandshakeMessageAfterProcessClientHello( |
| 166 | const ValidateClientHelloResultCallback::Result& result, |
| 167 | QuicErrorCode error, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 168 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 169 | std::unique_ptr<CryptoHandshakeMessage> reply, |
| 170 | std::unique_ptr<DiversificationNonce> diversification_nonce, |
| 171 | std::unique_ptr<ProofSource::Details> proof_source_details); |
| 172 | |
| 173 | // Invoked by SendServerConfigUpdateCallback::RunImpl once the proof has been |
| 174 | // received. |ok| indicates whether or not the proof was successfully |
| 175 | // acquired, and |message| holds the partially-constructed message from |
| 176 | // SendServerConfigUpdate. |
| 177 | void FinishSendServerConfigUpdate(bool ok, |
| 178 | const CryptoHandshakeMessage& message); |
| 179 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 180 | // Returns the QuicTransportVersion of the connection. |
| 181 | QuicTransportVersion transport_version() const { |
renjietang | d1d0085 | 2019-09-06 10:43:12 -0700 | [diff] [blame] | 182 | return session_->transport_version(); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 183 | } |
| 184 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 185 | QuicSession* session_; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 186 | HandshakerDelegateInterface* delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 187 | |
| 188 | // crypto_config_ contains crypto parameters for the handshake. |
| 189 | const QuicCryptoServerConfig* crypto_config_; |
| 190 | |
| 191 | // compressed_certs_cache_ contains a set of most recently compressed certs. |
| 192 | // Owned by QuicDispatcher. |
| 193 | QuicCompressedCertsCache* compressed_certs_cache_; |
| 194 | |
| 195 | // Server's certificate chain and signature of the server config, as provided |
| 196 | // by ProofSource::GetProof. |
| 197 | QuicReferenceCountedPointer<QuicSignedServerConfig> signed_config_; |
| 198 | |
| 199 | // Hash of the last received CHLO message which can be used for generating |
| 200 | // server config update messages. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 201 | std::string chlo_hash_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 202 | |
| 203 | // Pointer to the helper for this crypto stream. Must outlive this stream. |
nharper | 5f23a2d | 2020-02-20 10:44:09 -0800 | [diff] [blame] | 204 | QuicCryptoServerStreamBase::Helper* helper_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 205 | |
| 206 | // Number of handshake messages received by this stream. |
| 207 | uint8_t num_handshake_messages_; |
| 208 | |
| 209 | // Number of handshake messages received by this stream that contain |
| 210 | // server nonces (indicating that this is a non-zero-RTT handshake |
| 211 | // attempt). |
| 212 | uint8_t num_handshake_messages_with_server_nonces_; |
| 213 | |
| 214 | // Pointer to the active callback that will receive the result of |
| 215 | // BuildServerConfigUpdateMessage and forward it to |
| 216 | // FinishSendServerConfigUpdate. nullptr if no update message is currently |
| 217 | // being built. |
| 218 | SendServerConfigUpdateCallback* send_server_config_update_cb_; |
| 219 | |
| 220 | // Number of server config update (SCUP) messages sent by this stream. |
| 221 | int num_server_config_update_messages_sent_; |
| 222 | |
| 223 | // If the client provides CachedNetworkParameters in the STK in the CHLO, then |
| 224 | // store here, and send back in future STKs if we have no better bandwidth |
| 225 | // estimate to send. |
| 226 | std::unique_ptr<CachedNetworkParameters> previous_cached_network_params_; |
| 227 | |
| 228 | // Contains any source address tokens which were present in the CHLO. |
| 229 | SourceAddressTokens previous_source_address_tokens_; |
| 230 | |
wub | 4ca3d67 | 2019-06-12 08:27:17 -0700 | [diff] [blame] | 231 | // True if client attempts 0-rtt handshake (which can succeed or fail). |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 232 | bool zero_rtt_attempted_; |
| 233 | |
| 234 | // Size of the packet containing the most recently received CHLO. |
| 235 | QuicByteCount chlo_packet_size_; |
| 236 | |
| 237 | // Pointer to the active callback that will receive the result of the client |
| 238 | // hello validation request and forward it to FinishProcessingHandshakeMessage |
| 239 | // for processing. nullptr if no handshake message is being validated. Note |
| 240 | // that this field is mutually exclusive with process_client_hello_cb_. |
| 241 | ValidateCallback* validate_client_hello_cb_; |
| 242 | |
| 243 | // Pointer to the active callback which will receive the results of |
| 244 | // ProcessClientHello and forward it to |
| 245 | // FinishProcessingHandshakeMessageAfterProcessClientHello. Note that this |
| 246 | // field is mutually exclusive with validate_client_hello_cb_. |
| 247 | ProcessClientHelloCallback* process_client_hello_cb_; |
| 248 | |
nharper | 9b0a1af | 2020-08-07 17:11:29 -0700 | [diff] [blame] | 249 | // The ProofSource::Details from this connection. |
| 250 | std::unique_ptr<ProofSource::Details> proof_source_details_; |
| 251 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 252 | bool encryption_established_; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 253 | bool one_rtt_keys_available_; |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 254 | bool one_rtt_packet_decrypted_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 255 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> |
| 256 | crypto_negotiated_params_; |
| 257 | }; |
| 258 | |
| 259 | } // namespace quic |
| 260 | |
nharper | 21a3249 | 2020-02-28 12:53:17 -0800 | [diff] [blame] | 261 | #endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_SERVER_STREAM_H_ |