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