1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_
#define NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_
#include <map>
#include <string>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_piece.h"
#include "net/base/net_export.h"
#include "net/quic/crypto/crypto_handshake.h"
#include "net/quic/quic_protocol.h"
namespace net {
class ChannelIDSigner;
class CryptoHandshakeMessage;
class ProofVerifier;
class ProofVerifyDetails;
class QuicRandom;
class QuicSessionKey;
// QuicCryptoClientConfig contains crypto-related configuration settings for a
// client. Note that this object isn't thread-safe. It's designed to be used on
// a single thread at a time.
class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig {
public:
// A CachedState contains the information that the client needs in order to
// perform a 0-RTT handshake with a server. This information can be reused
// over several connections to the same server.
class NET_EXPORT_PRIVATE CachedState {
public:
CachedState();
~CachedState();
// IsComplete returns true if this object contains enough information to
// perform a handshake with the server. |now| is used to judge whether any
// cached server config has expired.
bool IsComplete(QuicWallTime now) const;
// IsEmpty returns true if |server_config_| is empty.
bool IsEmpty() const;
// GetServerConfig returns the parsed contents of |server_config|, or NULL
// if |server_config| is empty. The return value is owned by this object
// and is destroyed when this object is.
const CryptoHandshakeMessage* GetServerConfig() const;
// SetServerConfig checks that |server_config| parses correctly and stores
// it in |server_config_|. |now| is used to judge whether |server_config|
// has expired.
QuicErrorCode SetServerConfig(base::StringPiece server_config,
QuicWallTime now,
std::string* error_details);
// InvalidateServerConfig clears the cached server config (if any).
void InvalidateServerConfig();
// SetProof stores a certificate chain and signature.
void SetProof(const std::vector<std::string>& certs,
base::StringPiece signature);
// Clears the certificate chain and signature and invalidates the proof.
void ClearProof();
// SetProofValid records that the certificate chain and signature have been
// validated and that it's safe to assume that the server is legitimate.
// (Note: this does not check the chain or signature.)
void SetProofValid();
// If the server config or the proof has changed then it needs to be
// revalidated. Helper function to keep server_config_valid_ and
// generation_counter_ in sync.
void SetProofInvalid();
const std::string& server_config() const;
const std::string& source_address_token() const;
const std::vector<std::string>& certs() const;
const std::string& signature() const;
bool proof_valid() const;
uint64 generation_counter() const;
const ProofVerifyDetails* proof_verify_details() const;
void set_source_address_token(base::StringPiece token);
// SetProofVerifyDetails takes ownership of |details|.
void SetProofVerifyDetails(ProofVerifyDetails* details);
// Copy the |server_config_|, |source_address_token_|, |certs_| and
// |server_config_sig_| from the |other|. The remaining fields,
// |generation_counter_|, |proof_verify_details_|, and |scfg_| remain
// unchanged.
void InitializeFrom(const CachedState& other);
// Initializes this cached state based on the arguments provided.
// Returns false if there is a problem parsing the server config.
bool Initialize(base::StringPiece server_config,
base::StringPiece source_address_token,
const std::vector<std::string>& certs,
base::StringPiece signature,
QuicWallTime now);
private:
std::string server_config_; // A serialized handshake message.
std::string source_address_token_; // An opaque proof of IP ownership.
std::vector<std::string> certs_; // A list of certificates in leaf-first
// order.
std::string server_config_sig_; // A signature of |server_config_|.
bool server_config_valid_; // True if |server_config_| is correctly
// signed and |certs_| has been
// validated.
// Generation counter associated with the |server_config_|, |certs_| and
// |server_config_sig_| combination. It is incremented whenever we set
// server_config_valid_ to false.
uint64 generation_counter_;
scoped_ptr<ProofVerifyDetails> proof_verify_details_;
// scfg contains the cached, parsed value of |server_config|.
mutable scoped_ptr<CryptoHandshakeMessage> scfg_;
DISALLOW_COPY_AND_ASSIGN(CachedState);
};
QuicCryptoClientConfig();
~QuicCryptoClientConfig();
// Sets the members to reasonable, default values.
void SetDefaults();
// LookupOrCreate returns a CachedState for the given |server_key|. If no such
// CachedState currently exists, it will be created and cached.
CachedState* LookupOrCreate(const QuicSessionKey& server_key);
// FillInchoateClientHello sets |out| to be a CHLO message that elicits a
// source-address token or SCFG from a server. If |cached| is non-NULL, the
// source-address token will be taken from it. |out_params| is used in order
// to store the cached certs that were sent as hints to the server in
// |out_params->cached_certs|. |preferred_version| is the version of the
// QUIC protocol that this client chose to use initially. This allows the
// server to detect downgrade attacks.
void FillInchoateClientHello(const QuicSessionKey& server_key,
const QuicVersion preferred_version,
const CachedState* cached,
QuicCryptoNegotiatedParameters* out_params,
CryptoHandshakeMessage* out) const;
// FillClientHello sets |out| to be a CHLO message based on the configuration
// of this object. This object must have cached enough information about
// the server's hostname in order to perform a handshake. This can be checked
// with the |IsComplete| member of |CachedState|.
//
// |initial_flow_control_window_bytes| is the size of the initial flow
// control window this client will use for new streams.
//
// |clock| and |rand| are used to generate the nonce and |out_params| is
// filled with the results of the handshake that the server is expected to
// accept. |preferred_version| is the version of the QUIC protocol that this
// client chose to use initially. This allows the server to detect downgrade
// attacks.
QuicErrorCode FillClientHello(const QuicSessionKey& server_key,
QuicConnectionId connection_id,
const QuicVersion preferred_version,
uint32 initial_flow_control_window_bytes,
const CachedState* cached,
QuicWallTime now,
QuicRandom* rand,
QuicCryptoNegotiatedParameters* out_params,
CryptoHandshakeMessage* out,
std::string* error_details) const;
// ProcessRejection processes a REJ message from a server and updates the
// cached information about that server. After this, |IsComplete| may return
// true for that server's CachedState. If the rejection message contains
// state about a future handshake (i.e. an nonce value from the server), then
// it will be saved in |out_params|. |now| is used to judge whether the
// server config in the rejection message has expired.
QuicErrorCode ProcessRejection(const CryptoHandshakeMessage& rej,
QuicWallTime now,
CachedState* cached,
QuicCryptoNegotiatedParameters* out_params,
std::string* error_details);
// ProcessServerHello processes the message in |server_hello|, updates the
// cached information about that server, writes the negotiated parameters to
// |out_params| and returns QUIC_NO_ERROR. If |server_hello| is unacceptable
// then it puts an error message in |error_details| and returns an error
// code. |negotiated_versions| contains the list of version, if any, that were
// present in a version negotiation packet previously recevied from the
// server. The contents of this list will be compared against the list of
// versions provided in the VER tag of the server hello.
QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello,
QuicConnectionId connection_id,
const QuicVersionVector& negotiated_versions,
CachedState* cached,
QuicCryptoNegotiatedParameters* out_params,
std::string* error_details);
ProofVerifier* proof_verifier() const;
// SetProofVerifier takes ownership of a |ProofVerifier| that clients are
// free to use in order to verify certificate chains from servers. If a
// ProofVerifier is set then the client will request a certificate chain from
// the server.
void SetProofVerifier(ProofVerifier* verifier);
ChannelIDSigner* channel_id_signer() const;
// SetChannelIDSigner sets a ChannelIDSigner that will be called when the
// server supports channel IDs to sign a message proving possession of the
// given ChannelID. This object takes ownership of |signer|.
void SetChannelIDSigner(ChannelIDSigner* signer);
// Initialize the CachedState from |canonical_crypto_config| for the
// |canonical_server_key| as the initial CachedState for |server_key|. We will
// copy config data only if |canonical_crypto_config| has valid proof.
void InitializeFrom(const QuicSessionKey& server_key,
const QuicSessionKey& canonical_server_key,
QuicCryptoClientConfig* canonical_crypto_config);
// Adds |suffix| as a domain suffix for which the server's crypto config
// is expected to be shared among servers with the domain suffix. If a server
// matches this suffix, then the server config from another server with the
// suffix will be used to initialize the cached state for this server.
void AddCanonicalSuffix(const std::string& suffix);
// Prefers AES-GCM (kAESG) over other AEAD algorithms. Call this method if
// the CPU has hardware acceleration for AES-GCM. This method can only be
// called after SetDefaults().
void PreferAesGcm();
// Disables the use of ECDSA for proof verification.
// Call this method on platforms that do not support ECDSA.
// TODO(rch): remove this method when we drop support for Windows XP.
void DisableEcdsa();
private:
typedef std::map<QuicSessionKey, CachedState*> CachedStateMap;
// If the suffix of the hostname in |server_key| is in |canoncial_suffixes_|,
// then populate |cached| with the canonical cached state from
// |canonical_server_map_| for that suffix.
void PopulateFromCanonicalConfig(const QuicSessionKey& server_key,
CachedState* cached);
// cached_states_ maps from the server_key to the cached information about
// that server.
CachedStateMap cached_states_;
// Contains a map of servers which could share the same server config. Map
// from a canonical host suffix/port/scheme to a representative server with
// the canonical suffix, which has a plausible set of initial certificates
// (or at least server public key).
std::map<QuicSessionKey, QuicSessionKey> canonical_server_map_;
// Contains list of suffixes (for exmaple ".c.youtube.com",
// ".googlevideo.com") of canoncial hostnames.
std::vector<std::string> canoncial_suffixes_;
scoped_ptr<ProofVerifier> proof_verifier_;
scoped_ptr<ChannelIDSigner> channel_id_signer_;
// True if ECDSA should be disabled.
bool disable_ecdsa_;
DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientConfig);
};
} // namespace net
#endif // NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_
|