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