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_CLIENT_HANDSHAKER_H_ |
| 6 | #define QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h" |
| 11 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_client_config.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_server_id.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
renjietang | f21e385 | 2020-04-13 15:45:39 -0700 | [diff] [blame] | 15 | #include "net/third_party/quiche/src/common/platform/api/quiche_logging.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 16 | |
| 17 | namespace quic { |
| 18 | |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 19 | // An implementation of QuicCryptoClientStream::HandshakerInterface which uses |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | // QUIC crypto as the crypto handshake protocol. |
| 21 | class QUIC_EXPORT_PRIVATE QuicCryptoClientHandshaker |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 22 | : public QuicCryptoClientStream::HandshakerInterface, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | public QuicCryptoHandshaker { |
| 24 | public: |
| 25 | QuicCryptoClientHandshaker( |
| 26 | const QuicServerId& server_id, |
| 27 | QuicCryptoClientStream* stream, |
| 28 | QuicSession* session, |
| 29 | std::unique_ptr<ProofVerifyContext> verify_context, |
| 30 | QuicCryptoClientConfig* crypto_config, |
| 31 | QuicCryptoClientStream::ProofHandler* proof_handler); |
| 32 | QuicCryptoClientHandshaker(const QuicCryptoClientHandshaker&) = delete; |
| 33 | QuicCryptoClientHandshaker& operator=(const QuicCryptoClientHandshaker&) = |
| 34 | delete; |
| 35 | |
| 36 | ~QuicCryptoClientHandshaker() override; |
| 37 | |
rch | 85240a1 | 2019-12-23 11:51:59 -0800 | [diff] [blame] | 38 | // From QuicCryptoClientStream::HandshakerInterface |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 39 | bool CryptoConnect() override; |
| 40 | int num_sent_client_hellos() const override; |
nharper | 0270396 | 2019-11-07 12:23:13 -0800 | [diff] [blame] | 41 | bool IsResumption() const override; |
nharper | 4084fc9 | 2020-02-10 14:43:35 -0800 | [diff] [blame] | 42 | bool EarlyDataAccepted() const override; |
| 43 | bool ReceivedInchoateReject() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | int num_scup_messages_received() const override; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 45 | std::string chlo_hash() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 46 | bool encryption_established() const override; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 47 | bool one_rtt_keys_available() const override; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 49 | const override; |
| 50 | CryptoMessageParser* crypto_message_parser() override; |
fayang | 9a863cf | 2020-01-16 14:12:11 -0800 | [diff] [blame] | 51 | HandshakeState GetHandshakeState() const override; |
nharper | 486a8a9 | 2019-08-28 16:25:10 -0700 | [diff] [blame] | 52 | size_t BufferSizeLimitForLevel(EncryptionLevel level) const override; |
fayang | 2f2915d | 2020-01-24 06:47:15 -0800 | [diff] [blame] | 53 | void OnOneRttPacketAcknowledged() override {} |
fayang | 44ae4e9 | 2020-04-28 13:09:42 -0700 | [diff] [blame] | 54 | void OnHandshakePacketSent() override {} |
fayang | a6a85a8 | 2020-05-04 08:58:53 -0700 | [diff] [blame] | 55 | void OnConnectionClosed(QuicErrorCode /*error*/, |
| 56 | ConnectionCloseSource /*source*/) override {} |
fayang | 0106294 | 2020-01-22 07:23:23 -0800 | [diff] [blame] | 57 | void OnHandshakeDoneReceived() override; |
renjietang | f21e385 | 2020-04-13 15:45:39 -0700 | [diff] [blame] | 58 | void OnApplicationState( |
| 59 | std::unique_ptr<ApplicationState> /*application_state*/) override { |
| 60 | QUICHE_NOTREACHED(); |
| 61 | } |
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 | // Returns the QuicSession that this stream belongs to. |
| 68 | QuicSession* session() const { return session_; } |
| 69 | |
| 70 | // Send either InchoateClientHello or ClientHello message to the server. |
| 71 | void DoSendCHLO(QuicCryptoClientConfig::CachedState* cached); |
| 72 | |
| 73 | private: |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 74 | // ProofVerifierCallbackImpl is passed as the callback method to VerifyProof. |
| 75 | // The ProofVerifier calls this class with the result of proof verification |
| 76 | // when verification is performed asynchronously. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 77 | class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl |
| 78 | : public ProofVerifierCallback { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 79 | public: |
| 80 | explicit ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent); |
| 81 | ~ProofVerifierCallbackImpl() override; |
| 82 | |
| 83 | // ProofVerifierCallback interface. |
| 84 | void Run(bool ok, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 85 | const std::string& error_details, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 86 | std::unique_ptr<ProofVerifyDetails>* details) override; |
| 87 | |
| 88 | // Cancel causes any future callbacks to be ignored. It must be called on |
| 89 | // the same thread as the callback will be made on. |
| 90 | void Cancel(); |
| 91 | |
| 92 | private: |
| 93 | QuicCryptoClientHandshaker* parent_; |
| 94 | }; |
| 95 | |
| 96 | enum State { |
| 97 | STATE_IDLE, |
| 98 | STATE_INITIALIZE, |
| 99 | STATE_SEND_CHLO, |
| 100 | STATE_RECV_REJ, |
| 101 | STATE_VERIFY_PROOF, |
| 102 | STATE_VERIFY_PROOF_COMPLETE, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 103 | STATE_RECV_SHLO, |
| 104 | STATE_INITIALIZE_SCUP, |
| 105 | STATE_NONE, |
| 106 | }; |
| 107 | |
| 108 | // Handles new server config and optional source-address token provided by the |
| 109 | // server during a connection. |
| 110 | void HandleServerConfigUpdateMessage( |
| 111 | const CryptoHandshakeMessage& server_config_update); |
| 112 | |
| 113 | // DoHandshakeLoop performs a step of the handshake state machine. Note that |
| 114 | // |in| may be nullptr if the call did not result from a received message. |
| 115 | void DoHandshakeLoop(const CryptoHandshakeMessage* in); |
| 116 | |
| 117 | // Start the handshake process. |
| 118 | void DoInitialize(QuicCryptoClientConfig::CachedState* cached); |
| 119 | |
| 120 | // Process REJ message from the server. |
| 121 | void DoReceiveREJ(const CryptoHandshakeMessage* in, |
| 122 | QuicCryptoClientConfig::CachedState* cached); |
| 123 | |
| 124 | // Start the proof verification process. Returns the QuicAsyncStatus returned |
| 125 | // by the ProofVerifier's VerifyProof. |
| 126 | QuicAsyncStatus DoVerifyProof(QuicCryptoClientConfig::CachedState* cached); |
| 127 | |
| 128 | // If proof is valid then it sets the proof as valid (which persists the |
| 129 | // server config). If not, it closes the connection. |
| 130 | void DoVerifyProofComplete(QuicCryptoClientConfig::CachedState* cached); |
| 131 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 132 | // Process SHLO message from the server. |
| 133 | void DoReceiveSHLO(const CryptoHandshakeMessage* in, |
| 134 | QuicCryptoClientConfig::CachedState* cached); |
| 135 | |
| 136 | // Start the proof verification if |server_id_| is https and |cached| has |
| 137 | // signature. |
| 138 | void DoInitializeServerConfigUpdate( |
| 139 | QuicCryptoClientConfig::CachedState* cached); |
| 140 | |
| 141 | // Called to set the proof of |cached| valid. Also invokes the session's |
| 142 | // OnProofValid() method. |
| 143 | void SetCachedProofValid(QuicCryptoClientConfig::CachedState* cached); |
| 144 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 145 | QuicCryptoClientStream* stream_; |
| 146 | |
| 147 | QuicSession* session_; |
fayang | d58736d | 2019-11-27 13:35:31 -0800 | [diff] [blame] | 148 | HandshakerDelegateInterface* delegate_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 149 | |
| 150 | State next_state_; |
| 151 | // num_client_hellos_ contains the number of client hello messages that this |
| 152 | // connection has sent. |
| 153 | int num_client_hellos_; |
| 154 | |
| 155 | QuicCryptoClientConfig* const crypto_config_; |
| 156 | |
| 157 | // SHA-256 hash of the most recently sent CHLO. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 158 | std::string chlo_hash_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 159 | |
| 160 | // Server's (hostname, port, is_https, privacy_mode) tuple. |
| 161 | const QuicServerId server_id_; |
| 162 | |
| 163 | // Generation counter from QuicCryptoClientConfig's CachedState. |
| 164 | uint64_t generation_counter_; |
| 165 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 166 | // verify_context_ contains the context object that we pass to asynchronous |
| 167 | // proof verifications. |
| 168 | std::unique_ptr<ProofVerifyContext> verify_context_; |
| 169 | |
| 170 | // proof_verify_callback_ contains the callback object that we passed to an |
| 171 | // asynchronous proof verification. The ProofVerifier owns this object. |
| 172 | ProofVerifierCallbackImpl* proof_verify_callback_; |
| 173 | // proof_handler_ contains the callback object used by a quic client |
| 174 | // for proof verification. It is not owned by this class. |
| 175 | QuicCryptoClientStream::ProofHandler* proof_handler_; |
| 176 | |
| 177 | // These members are used to store the result of an asynchronous proof |
| 178 | // verification. These members must not be used after |
| 179 | // STATE_VERIFY_PROOF_COMPLETE. |
| 180 | bool verify_ok_; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 181 | std::string verify_error_details_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 182 | std::unique_ptr<ProofVerifyDetails> verify_details_; |
| 183 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | QuicTime proof_verify_start_time_; |
| 185 | |
| 186 | int num_scup_messages_received_; |
| 187 | |
| 188 | bool encryption_established_; |
fayang | 685367a | 2020-01-14 10:40:15 -0800 | [diff] [blame] | 189 | bool one_rtt_keys_available_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 190 | QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters> |
| 191 | crypto_negotiated_params_; |
| 192 | }; |
| 193 | |
| 194 | } // namespace quic |
| 195 | |
| 196 | #endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_ |