blob: 4aa5d469dcb6dd18456a279b942bce511ae93214 [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/channel_id.h"
11#include "net/third_party/quiche/src/quic/core/crypto/proof_verifier.h"
12#include "net/third_party/quiche/src/quic/core/crypto/quic_crypto_client_config.h"
13#include "net/third_party/quiche/src/quic/core/quic_crypto_client_stream.h"
14#include "net/third_party/quiche/src/quic/core/quic_server_id.h"
15#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050016
17namespace quic {
18
19// An implementation of QuicCryptoClientStream::HandshakerDelegate which uses
20// QUIC crypto as the crypto handshake protocol.
21class QUIC_EXPORT_PRIVATE QuicCryptoClientHandshaker
22 : public QuicCryptoClientStream::HandshakerDelegate,
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
38 // From QuicCryptoClientStream::HandshakerDelegate
39 bool CryptoConnect() override;
40 int num_sent_client_hellos() const override;
41 int num_scup_messages_received() const override;
42 bool WasChannelIDSent() const override;
43 bool WasChannelIDSourceCallbackRun() const override;
vasilvvc48c8712019-03-11 13:38:16 -070044 std::string chlo_hash() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 bool encryption_established() const override;
46 bool handshake_confirmed() const override;
47 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
48 const override;
49 CryptoMessageParser* crypto_message_parser() override;
50
51 // From QuicCryptoHandshaker
52 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
53
54 protected:
55 // Returns the QuicSession that this stream belongs to.
56 QuicSession* session() const { return session_; }
57
58 // Send either InchoateClientHello or ClientHello message to the server.
59 void DoSendCHLO(QuicCryptoClientConfig::CachedState* cached);
60
61 private:
62 // ChannelIDSourceCallbackImpl is passed as the callback method to
63 // GetChannelIDKey. The ChannelIDSource calls this class with the result of
64 // channel ID lookup when lookup is performed asynchronously.
65 class ChannelIDSourceCallbackImpl : public ChannelIDSourceCallback {
66 public:
67 explicit ChannelIDSourceCallbackImpl(QuicCryptoClientHandshaker* parent);
68 ~ChannelIDSourceCallbackImpl() override;
69
70 // ChannelIDSourceCallback interface.
71 void Run(std::unique_ptr<ChannelIDKey>* channel_id_key) override;
72
73 // Cancel causes any future callbacks to be ignored. It must be called on
74 // the same thread as the callback will be made on.
75 void Cancel();
76
77 private:
78 QuicCryptoClientHandshaker* parent_;
79 };
80
81 // ProofVerifierCallbackImpl is passed as the callback method to VerifyProof.
82 // The ProofVerifier calls this class with the result of proof verification
83 // when verification is performed asynchronously.
84 class ProofVerifierCallbackImpl : public ProofVerifierCallback {
85 public:
86 explicit ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent);
87 ~ProofVerifierCallbackImpl() override;
88
89 // ProofVerifierCallback interface.
90 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070091 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050092 std::unique_ptr<ProofVerifyDetails>* details) override;
93
94 // Cancel causes any future callbacks to be ignored. It must be called on
95 // the same thread as the callback will be made on.
96 void Cancel();
97
98 private:
99 QuicCryptoClientHandshaker* parent_;
100 };
101
102 enum State {
103 STATE_IDLE,
104 STATE_INITIALIZE,
105 STATE_SEND_CHLO,
106 STATE_RECV_REJ,
107 STATE_VERIFY_PROOF,
108 STATE_VERIFY_PROOF_COMPLETE,
109 STATE_GET_CHANNEL_ID,
110 STATE_GET_CHANNEL_ID_COMPLETE,
111 STATE_RECV_SHLO,
112 STATE_INITIALIZE_SCUP,
113 STATE_NONE,
114 };
115
116 // Handles new server config and optional source-address token provided by the
117 // server during a connection.
118 void HandleServerConfigUpdateMessage(
119 const CryptoHandshakeMessage& server_config_update);
120
121 // DoHandshakeLoop performs a step of the handshake state machine. Note that
122 // |in| may be nullptr if the call did not result from a received message.
123 void DoHandshakeLoop(const CryptoHandshakeMessage* in);
124
125 // Start the handshake process.
126 void DoInitialize(QuicCryptoClientConfig::CachedState* cached);
127
128 // Process REJ message from the server.
129 void DoReceiveREJ(const CryptoHandshakeMessage* in,
130 QuicCryptoClientConfig::CachedState* cached);
131
132 // Start the proof verification process. Returns the QuicAsyncStatus returned
133 // by the ProofVerifier's VerifyProof.
134 QuicAsyncStatus DoVerifyProof(QuicCryptoClientConfig::CachedState* cached);
135
136 // If proof is valid then it sets the proof as valid (which persists the
137 // server config). If not, it closes the connection.
138 void DoVerifyProofComplete(QuicCryptoClientConfig::CachedState* cached);
139
140 // Start the look up of Channel ID process. Returns either QUIC_SUCCESS if
141 // RequiresChannelID returns false or QuicAsyncStatus returned by
142 // GetChannelIDKey.
143 QuicAsyncStatus DoGetChannelID(QuicCryptoClientConfig::CachedState* cached);
144
145 // If there is no channel ID, then close the connection otherwise transtion to
146 // STATE_SEND_CHLO state.
147 void DoGetChannelIDComplete();
148
149 // Process SHLO message from the server.
150 void DoReceiveSHLO(const CryptoHandshakeMessage* in,
151 QuicCryptoClientConfig::CachedState* cached);
152
153 // Start the proof verification if |server_id_| is https and |cached| has
154 // signature.
155 void DoInitializeServerConfigUpdate(
156 QuicCryptoClientConfig::CachedState* cached);
157
158 // Called to set the proof of |cached| valid. Also invokes the session's
159 // OnProofValid() method.
160 void SetCachedProofValid(QuicCryptoClientConfig::CachedState* cached);
161
162 // Returns true if the server crypto config in |cached| requires a ChannelID
163 // and the client config settings also allow sending a ChannelID.
164 bool RequiresChannelID(QuicCryptoClientConfig::CachedState* cached);
165
166 QuicCryptoClientStream* stream_;
167
168 QuicSession* session_;
169
170 State next_state_;
171 // num_client_hellos_ contains the number of client hello messages that this
172 // connection has sent.
173 int num_client_hellos_;
174
175 QuicCryptoClientConfig* const crypto_config_;
176
177 // SHA-256 hash of the most recently sent CHLO.
vasilvvc48c8712019-03-11 13:38:16 -0700178 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500179
180 // Server's (hostname, port, is_https, privacy_mode) tuple.
181 const QuicServerId server_id_;
182
183 // Generation counter from QuicCryptoClientConfig's CachedState.
184 uint64_t generation_counter_;
185
186 // True if a channel ID was sent.
187 bool channel_id_sent_;
188
189 // True if channel_id_source_callback_ was run.
190 bool channel_id_source_callback_run_;
191
192 // channel_id_source_callback_ contains the callback object that we passed
193 // to an asynchronous channel ID lookup. The ChannelIDSource owns this
194 // object.
195 ChannelIDSourceCallbackImpl* channel_id_source_callback_;
196
197 // These members are used to store the result of an asynchronous channel ID
198 // lookup. These members must not be used after
199 // STATE_GET_CHANNEL_ID_COMPLETE.
200 std::unique_ptr<ChannelIDKey> channel_id_key_;
201
202 // verify_context_ contains the context object that we pass to asynchronous
203 // proof verifications.
204 std::unique_ptr<ProofVerifyContext> verify_context_;
205
206 // proof_verify_callback_ contains the callback object that we passed to an
207 // asynchronous proof verification. The ProofVerifier owns this object.
208 ProofVerifierCallbackImpl* proof_verify_callback_;
209 // proof_handler_ contains the callback object used by a quic client
210 // for proof verification. It is not owned by this class.
211 QuicCryptoClientStream::ProofHandler* proof_handler_;
212
213 // These members are used to store the result of an asynchronous proof
214 // verification. These members must not be used after
215 // STATE_VERIFY_PROOF_COMPLETE.
216 bool verify_ok_;
vasilvvc48c8712019-03-11 13:38:16 -0700217 std::string verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500218 std::unique_ptr<ProofVerifyDetails> verify_details_;
219
220 // True if the server responded to a previous CHLO with a stateless
221 // reject. Used for book-keeping between the STATE_RECV_REJ,
222 // STATE_VERIFY_PROOF*, and subsequent STATE_SEND_CHLO state.
223 bool stateless_reject_received_;
224
225 QuicTime proof_verify_start_time_;
226
227 int num_scup_messages_received_;
228
229 bool encryption_established_;
230 bool handshake_confirmed_;
231 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
232 crypto_negotiated_params_;
233};
234
235} // namespace quic
236
237#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_