blob: 5ba93f248488bdced0417497c86da55c4b017dcf [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
9
QUICHE teama6ef0a62019-03-07 20:34:33 -050010#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"
renjietangf21e3852020-04-13 15:45:39 -070015#include "net/third_party/quiche/src/common/platform/api/quiche_logging.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016
17namespace quic {
18
rch85240a12019-12-23 11:51:59 -080019// An implementation of QuicCryptoClientStream::HandshakerInterface which uses
QUICHE teama6ef0a62019-03-07 20:34:33 -050020// QUIC crypto as the crypto handshake protocol.
21class QUIC_EXPORT_PRIVATE QuicCryptoClientHandshaker
rch85240a12019-12-23 11:51:59 -080022 : public QuicCryptoClientStream::HandshakerInterface,
QUICHE teama6ef0a62019-03-07 20:34:33 -050023 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
rch85240a12019-12-23 11:51:59 -080038 // From QuicCryptoClientStream::HandshakerInterface
QUICHE teama6ef0a62019-03-07 20:34:33 -050039 bool CryptoConnect() override;
40 int num_sent_client_hellos() const override;
nharper02703962019-11-07 12:23:13 -080041 bool IsResumption() const override;
nharper4084fc92020-02-10 14:43:35 -080042 bool EarlyDataAccepted() const override;
43 bool ReceivedInchoateReject() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050044 int num_scup_messages_received() const override;
vasilvvc48c8712019-03-11 13:38:16 -070045 std::string chlo_hash() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050046 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080047 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050048 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
49 const override;
50 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080051 HandshakeState GetHandshakeState() const override;
nharper486a8a92019-08-28 16:25:10 -070052 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
fayang2f2915d2020-01-24 06:47:15 -080053 void OnOneRttPacketAcknowledged() override {}
fayang44ae4e92020-04-28 13:09:42 -070054 void OnHandshakePacketSent() override {}
fayanga6a85a82020-05-04 08:58:53 -070055 void OnConnectionClosed(QuicErrorCode /*error*/,
56 ConnectionCloseSource /*source*/) override {}
fayang01062942020-01-22 07:23:23 -080057 void OnHandshakeDoneReceived() override;
renjietangf21e3852020-04-13 15:45:39 -070058 void OnApplicationState(
59 std::unique_ptr<ApplicationState> /*application_state*/) override {
60 QUICHE_NOTREACHED();
61 }
QUICHE teama6ef0a62019-03-07 20:34:33 -050062
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 teama6ef0a62019-03-07 20:34:33 -050074 // 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.
dschinazif25169a2019-10-23 08:12:18 -070077 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
78 : public ProofVerifierCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -050079 public:
80 explicit ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent);
81 ~ProofVerifierCallbackImpl() override;
82
83 // ProofVerifierCallback interface.
84 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070085 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050086 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 teama6ef0a62019-03-07 20:34:33 -0500103 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 teama6ef0a62019-03-07 20:34:33 -0500132 // 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 teama6ef0a62019-03-07 20:34:33 -0500145 QuicCryptoClientStream* stream_;
146
147 QuicSession* session_;
fayangd58736d2019-11-27 13:35:31 -0800148 HandshakerDelegateInterface* delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500149
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.
vasilvvc48c8712019-03-11 13:38:16 -0700158 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500159
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 teama6ef0a62019-03-07 20:34:33 -0500166 // 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_;
vasilvvc48c8712019-03-11 13:38:16 -0700181 std::string verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500182 std::unique_ptr<ProofVerifyDetails> verify_details_;
183
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184 QuicTime proof_verify_start_time_;
185
186 int num_scup_messages_received_;
187
188 bool encryption_established_;
fayang685367a2020-01-14 10:40:15 -0800189 bool one_rtt_keys_available_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500190 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
191 crypto_negotiated_params_;
192};
193
194} // namespace quic
195
196#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_