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