blob: dc799fb09a8b56e1d3c57f5c16f651399f6444f5 [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"
QUICHE teama6ef0a62019-03-07 20:34:33 -050015
16namespace quic {
17
rch85240a12019-12-23 11:51:59 -080018// An implementation of QuicCryptoClientStream::HandshakerInterface which uses
QUICHE teama6ef0a62019-03-07 20:34:33 -050019// QUIC crypto as the crypto handshake protocol.
20class QUIC_EXPORT_PRIVATE QuicCryptoClientHandshaker
rch85240a12019-12-23 11:51:59 -080021 : public QuicCryptoClientStream::HandshakerInterface,
QUICHE teama6ef0a62019-03-07 20:34:33 -050022 public QuicCryptoHandshaker {
23 public:
24 QuicCryptoClientHandshaker(
25 const QuicServerId& server_id,
26 QuicCryptoClientStream* stream,
27 QuicSession* session,
28 std::unique_ptr<ProofVerifyContext> verify_context,
29 QuicCryptoClientConfig* crypto_config,
30 QuicCryptoClientStream::ProofHandler* proof_handler);
31 QuicCryptoClientHandshaker(const QuicCryptoClientHandshaker&) = delete;
32 QuicCryptoClientHandshaker& operator=(const QuicCryptoClientHandshaker&) =
33 delete;
34
35 ~QuicCryptoClientHandshaker() override;
36
rch85240a12019-12-23 11:51:59 -080037 // From QuicCryptoClientStream::HandshakerInterface
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 bool CryptoConnect() override;
39 int num_sent_client_hellos() const override;
nharper02703962019-11-07 12:23:13 -080040 bool IsResumption() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050041 int num_scup_messages_received() const override;
vasilvvc48c8712019-03-11 13:38:16 -070042 std::string chlo_hash() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050043 bool encryption_established() const override;
fayang685367a2020-01-14 10:40:15 -080044 bool one_rtt_keys_available() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050045 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
46 const override;
47 CryptoMessageParser* crypto_message_parser() override;
fayang9a863cf2020-01-16 14:12:11 -080048 HandshakeState GetHandshakeState() const override;
nharper486a8a92019-08-28 16:25:10 -070049 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
fayang01062942020-01-22 07:23:23 -080050 void OnHandshakeDoneReceived() override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050051
52 // From QuicCryptoHandshaker
53 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
54
55 protected:
56 // Returns the QuicSession that this stream belongs to.
57 QuicSession* session() const { return session_; }
58
59 // Send either InchoateClientHello or ClientHello message to the server.
60 void DoSendCHLO(QuicCryptoClientConfig::CachedState* cached);
61
62 private:
QUICHE teama6ef0a62019-03-07 20:34:33 -050063 // ProofVerifierCallbackImpl is passed as the callback method to VerifyProof.
64 // The ProofVerifier calls this class with the result of proof verification
65 // when verification is performed asynchronously.
dschinazif25169a2019-10-23 08:12:18 -070066 class QUIC_EXPORT_PRIVATE ProofVerifierCallbackImpl
67 : public ProofVerifierCallback {
QUICHE teama6ef0a62019-03-07 20:34:33 -050068 public:
69 explicit ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent);
70 ~ProofVerifierCallbackImpl() override;
71
72 // ProofVerifierCallback interface.
73 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070074 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050075 std::unique_ptr<ProofVerifyDetails>* details) override;
76
77 // Cancel causes any future callbacks to be ignored. It must be called on
78 // the same thread as the callback will be made on.
79 void Cancel();
80
81 private:
82 QuicCryptoClientHandshaker* parent_;
83 };
84
85 enum State {
86 STATE_IDLE,
87 STATE_INITIALIZE,
88 STATE_SEND_CHLO,
89 STATE_RECV_REJ,
90 STATE_VERIFY_PROOF,
91 STATE_VERIFY_PROOF_COMPLETE,
QUICHE teama6ef0a62019-03-07 20:34:33 -050092 STATE_RECV_SHLO,
93 STATE_INITIALIZE_SCUP,
94 STATE_NONE,
95 };
96
97 // Handles new server config and optional source-address token provided by the
98 // server during a connection.
99 void HandleServerConfigUpdateMessage(
100 const CryptoHandshakeMessage& server_config_update);
101
102 // DoHandshakeLoop performs a step of the handshake state machine. Note that
103 // |in| may be nullptr if the call did not result from a received message.
104 void DoHandshakeLoop(const CryptoHandshakeMessage* in);
105
106 // Start the handshake process.
107 void DoInitialize(QuicCryptoClientConfig::CachedState* cached);
108
109 // Process REJ message from the server.
110 void DoReceiveREJ(const CryptoHandshakeMessage* in,
111 QuicCryptoClientConfig::CachedState* cached);
112
113 // Start the proof verification process. Returns the QuicAsyncStatus returned
114 // by the ProofVerifier's VerifyProof.
115 QuicAsyncStatus DoVerifyProof(QuicCryptoClientConfig::CachedState* cached);
116
117 // If proof is valid then it sets the proof as valid (which persists the
118 // server config). If not, it closes the connection.
119 void DoVerifyProofComplete(QuicCryptoClientConfig::CachedState* cached);
120
QUICHE teama6ef0a62019-03-07 20:34:33 -0500121 // Process SHLO message from the server.
122 void DoReceiveSHLO(const CryptoHandshakeMessage* in,
123 QuicCryptoClientConfig::CachedState* cached);
124
125 // Start the proof verification if |server_id_| is https and |cached| has
126 // signature.
127 void DoInitializeServerConfigUpdate(
128 QuicCryptoClientConfig::CachedState* cached);
129
130 // Called to set the proof of |cached| valid. Also invokes the session's
131 // OnProofValid() method.
132 void SetCachedProofValid(QuicCryptoClientConfig::CachedState* cached);
133
QUICHE teama6ef0a62019-03-07 20:34:33 -0500134 QuicCryptoClientStream* stream_;
135
136 QuicSession* session_;
fayangd58736d2019-11-27 13:35:31 -0800137 HandshakerDelegateInterface* delegate_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500138
139 State next_state_;
140 // num_client_hellos_ contains the number of client hello messages that this
141 // connection has sent.
142 int num_client_hellos_;
143
144 QuicCryptoClientConfig* const crypto_config_;
145
146 // SHA-256 hash of the most recently sent CHLO.
vasilvvc48c8712019-03-11 13:38:16 -0700147 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500148
149 // Server's (hostname, port, is_https, privacy_mode) tuple.
150 const QuicServerId server_id_;
151
152 // Generation counter from QuicCryptoClientConfig's CachedState.
153 uint64_t generation_counter_;
154
QUICHE teama6ef0a62019-03-07 20:34:33 -0500155 // verify_context_ contains the context object that we pass to asynchronous
156 // proof verifications.
157 std::unique_ptr<ProofVerifyContext> verify_context_;
158
159 // proof_verify_callback_ contains the callback object that we passed to an
160 // asynchronous proof verification. The ProofVerifier owns this object.
161 ProofVerifierCallbackImpl* proof_verify_callback_;
162 // proof_handler_ contains the callback object used by a quic client
163 // for proof verification. It is not owned by this class.
164 QuicCryptoClientStream::ProofHandler* proof_handler_;
165
166 // These members are used to store the result of an asynchronous proof
167 // verification. These members must not be used after
168 // STATE_VERIFY_PROOF_COMPLETE.
169 bool verify_ok_;
vasilvvc48c8712019-03-11 13:38:16 -0700170 std::string verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171 std::unique_ptr<ProofVerifyDetails> verify_details_;
172
QUICHE teama6ef0a62019-03-07 20:34:33 -0500173 QuicTime proof_verify_start_time_;
174
175 int num_scup_messages_received_;
176
177 bool encryption_established_;
fayang685367a2020-01-14 10:40:15 -0800178 bool one_rtt_keys_available_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500179 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
180 crypto_negotiated_params_;
181};
182
183} // namespace quic
184
185#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_