blob: d33ebfe32fe7fdf7113bd3711da1e218bd79f481 [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
18// An implementation of QuicCryptoClientStream::HandshakerDelegate which uses
19// QUIC crypto as the crypto handshake protocol.
20class QUIC_EXPORT_PRIVATE QuicCryptoClientHandshaker
21 : public QuicCryptoClientStream::HandshakerDelegate,
22 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
37 // From QuicCryptoClientStream::HandshakerDelegate
38 bool CryptoConnect() override;
39 int num_sent_client_hellos() const override;
40 int num_scup_messages_received() const override;
vasilvvc48c8712019-03-11 13:38:16 -070041 std::string chlo_hash() const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050042 bool encryption_established() const override;
43 bool handshake_confirmed() const override;
44 const QuicCryptoNegotiatedParameters& crypto_negotiated_params()
45 const override;
46 CryptoMessageParser* crypto_message_parser() override;
nharper486a8a92019-08-28 16:25:10 -070047 size_t BufferSizeLimitForLevel(EncryptionLevel level) const override;
QUICHE teama6ef0a62019-03-07 20:34:33 -050048
49 // From QuicCryptoHandshaker
50 void OnHandshakeMessage(const CryptoHandshakeMessage& message) override;
51
52 protected:
53 // Returns the QuicSession that this stream belongs to.
54 QuicSession* session() const { return session_; }
55
56 // Send either InchoateClientHello or ClientHello message to the server.
57 void DoSendCHLO(QuicCryptoClientConfig::CachedState* cached);
58
59 private:
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 // ProofVerifierCallbackImpl is passed as the callback method to VerifyProof.
61 // The ProofVerifier calls this class with the result of proof verification
62 // when verification is performed asynchronously.
63 class ProofVerifierCallbackImpl : public ProofVerifierCallback {
64 public:
65 explicit ProofVerifierCallbackImpl(QuicCryptoClientHandshaker* parent);
66 ~ProofVerifierCallbackImpl() override;
67
68 // ProofVerifierCallback interface.
69 void Run(bool ok,
vasilvvc48c8712019-03-11 13:38:16 -070070 const std::string& error_details,
QUICHE teama6ef0a62019-03-07 20:34:33 -050071 std::unique_ptr<ProofVerifyDetails>* details) 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 enum State {
82 STATE_IDLE,
83 STATE_INITIALIZE,
84 STATE_SEND_CHLO,
85 STATE_RECV_REJ,
86 STATE_VERIFY_PROOF,
87 STATE_VERIFY_PROOF_COMPLETE,
QUICHE teama6ef0a62019-03-07 20:34:33 -050088 STATE_RECV_SHLO,
89 STATE_INITIALIZE_SCUP,
90 STATE_NONE,
91 };
92
93 // Handles new server config and optional source-address token provided by the
94 // server during a connection.
95 void HandleServerConfigUpdateMessage(
96 const CryptoHandshakeMessage& server_config_update);
97
98 // DoHandshakeLoop performs a step of the handshake state machine. Note that
99 // |in| may be nullptr if the call did not result from a received message.
100 void DoHandshakeLoop(const CryptoHandshakeMessage* in);
101
102 // Start the handshake process.
103 void DoInitialize(QuicCryptoClientConfig::CachedState* cached);
104
105 // Process REJ message from the server.
106 void DoReceiveREJ(const CryptoHandshakeMessage* in,
107 QuicCryptoClientConfig::CachedState* cached);
108
109 // Start the proof verification process. Returns the QuicAsyncStatus returned
110 // by the ProofVerifier's VerifyProof.
111 QuicAsyncStatus DoVerifyProof(QuicCryptoClientConfig::CachedState* cached);
112
113 // If proof is valid then it sets the proof as valid (which persists the
114 // server config). If not, it closes the connection.
115 void DoVerifyProofComplete(QuicCryptoClientConfig::CachedState* cached);
116
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117 // Process SHLO message from the server.
118 void DoReceiveSHLO(const CryptoHandshakeMessage* in,
119 QuicCryptoClientConfig::CachedState* cached);
120
121 // Start the proof verification if |server_id_| is https and |cached| has
122 // signature.
123 void DoInitializeServerConfigUpdate(
124 QuicCryptoClientConfig::CachedState* cached);
125
126 // Called to set the proof of |cached| valid. Also invokes the session's
127 // OnProofValid() method.
128 void SetCachedProofValid(QuicCryptoClientConfig::CachedState* cached);
129
QUICHE teama6ef0a62019-03-07 20:34:33 -0500130 QuicCryptoClientStream* stream_;
131
132 QuicSession* session_;
133
134 State next_state_;
135 // num_client_hellos_ contains the number of client hello messages that this
136 // connection has sent.
137 int num_client_hellos_;
138
139 QuicCryptoClientConfig* const crypto_config_;
140
141 // SHA-256 hash of the most recently sent CHLO.
vasilvvc48c8712019-03-11 13:38:16 -0700142 std::string chlo_hash_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500143
144 // Server's (hostname, port, is_https, privacy_mode) tuple.
145 const QuicServerId server_id_;
146
147 // Generation counter from QuicCryptoClientConfig's CachedState.
148 uint64_t generation_counter_;
149
QUICHE teama6ef0a62019-03-07 20:34:33 -0500150 // verify_context_ contains the context object that we pass to asynchronous
151 // proof verifications.
152 std::unique_ptr<ProofVerifyContext> verify_context_;
153
154 // proof_verify_callback_ contains the callback object that we passed to an
155 // asynchronous proof verification. The ProofVerifier owns this object.
156 ProofVerifierCallbackImpl* proof_verify_callback_;
157 // proof_handler_ contains the callback object used by a quic client
158 // for proof verification. It is not owned by this class.
159 QuicCryptoClientStream::ProofHandler* proof_handler_;
160
161 // These members are used to store the result of an asynchronous proof
162 // verification. These members must not be used after
163 // STATE_VERIFY_PROOF_COMPLETE.
164 bool verify_ok_;
vasilvvc48c8712019-03-11 13:38:16 -0700165 std::string verify_error_details_;
QUICHE teama6ef0a62019-03-07 20:34:33 -0500166 std::unique_ptr<ProofVerifyDetails> verify_details_;
167
QUICHE teama6ef0a62019-03-07 20:34:33 -0500168 QuicTime proof_verify_start_time_;
169
170 int num_scup_messages_received_;
171
172 bool encryption_established_;
173 bool handshake_confirmed_;
174 QuicReferenceCountedPointer<QuicCryptoNegotiatedParameters>
175 crypto_negotiated_params_;
176};
177
178} // namespace quic
179
180#endif // QUICHE_QUIC_CORE_QUIC_CRYPTO_CLIENT_HANDSHAKER_H_