Port Chromium's blink_tests target to GN.
[chromium-blink-merge.git] / net / quic / crypto / quic_crypto_client_config.h
blob1fddafa07954d0bfdcfcc4819b8caebe30e5ec2c
1 // Copyright 2013 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.
5 #ifndef NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_
6 #define NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_
8 #include <map>
9 #include <queue>
10 #include <string>
11 #include <vector>
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_piece.h"
15 #include "net/base/net_export.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/quic_protocol.h"
18 #include "net/quic/quic_server_id.h"
20 namespace net {
22 class ChannelIDKey;
23 class ChannelIDSource;
24 class CryptoHandshakeMessage;
25 class ProofVerifier;
26 class ProofVerifyDetails;
27 class QuicRandom;
29 // QuicCryptoClientConfig contains crypto-related configuration settings for a
30 // client. Note that this object isn't thread-safe. It's designed to be used on
31 // a single thread at a time.
32 class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig {
33 public:
34 // A CachedState contains the information that the client needs in order to
35 // perform a 0-RTT handshake with a server. This information can be reused
36 // over several connections to the same server.
37 class NET_EXPORT_PRIVATE CachedState {
38 public:
39 // Enum to track if the server config is valid or not. If it is not valid,
40 // it specifies why it is invalid.
41 enum ServerConfigState {
42 // WARNING: Do not change the numerical values of any of server config
43 // state. Do not remove deprecated server config states - just comment
44 // them as deprecated.
45 SERVER_CONFIG_EMPTY = 0,
46 SERVER_CONFIG_INVALID = 1,
47 SERVER_CONFIG_CORRUPTED = 2,
48 SERVER_CONFIG_EXPIRED = 3,
49 SERVER_CONFIG_INVALID_EXPIRY = 4,
50 SERVER_CONFIG_VALID = 5,
51 // NOTE: Add new server config states only immediately above this line.
52 // Make sure to update the QuicServerConfigState enum in
53 // tools/metrics/histograms/histograms.xml accordingly.
54 SERVER_CONFIG_COUNT
57 CachedState();
58 ~CachedState();
60 // IsComplete returns true if this object contains enough information to
61 // perform a handshake with the server. |now| is used to judge whether any
62 // cached server config has expired.
63 bool IsComplete(QuicWallTime now) const;
65 // IsEmpty returns true if |server_config_| is empty.
66 bool IsEmpty() const;
68 // GetServerConfig returns the parsed contents of |server_config|, or
69 // nullptr if |server_config| is empty. The return value is owned by this
70 // object and is destroyed when this object is.
71 const CryptoHandshakeMessage* GetServerConfig() const;
73 // SetServerConfig checks that |server_config| parses correctly and stores
74 // it in |server_config_|. |now| is used to judge whether |server_config|
75 // has expired.
76 ServerConfigState SetServerConfig(base::StringPiece server_config,
77 QuicWallTime now,
78 std::string* error_details);
80 // InvalidateServerConfig clears the cached server config (if any).
81 void InvalidateServerConfig();
83 // SetProof stores a certificate chain and signature.
84 void SetProof(const std::vector<std::string>& certs,
85 base::StringPiece signature);
87 // Clears all the data.
88 void Clear();
90 // Clears the certificate chain and signature and invalidates the proof.
91 void ClearProof();
93 // SetProofValid records that the certificate chain and signature have been
94 // validated and that it's safe to assume that the server is legitimate.
95 // (Note: this does not check the chain or signature.)
96 void SetProofValid();
98 // If the server config or the proof has changed then it needs to be
99 // revalidated. Helper function to keep server_config_valid_ and
100 // generation_counter_ in sync.
101 void SetProofInvalid();
103 const std::string& server_config() const;
104 const std::string& source_address_token() const;
105 const std::vector<std::string>& certs() const;
106 const std::string& signature() const;
107 bool proof_valid() const;
108 uint64 generation_counter() const;
109 const ProofVerifyDetails* proof_verify_details() const;
111 void set_source_address_token(base::StringPiece token);
113 // Adds the connection ID to the queue of server-designated connection-ids.
114 void add_server_designated_connection_id(QuicConnectionId connection_id);
116 // If true, the crypto config contains at least one connection ID specified
117 // by the server, and the client should use one of these IDs when initiating
118 // the next connection.
119 bool has_server_designated_connection_id() const;
121 // This function should only be called when
122 // has_server_designated_connection_id is true. Returns the next
123 // connection_id specified by the server and removes it from the
124 // queue of ids.
125 QuicConnectionId GetNextServerDesignatedConnectionId();
127 // SetProofVerifyDetails takes ownership of |details|.
128 void SetProofVerifyDetails(ProofVerifyDetails* details);
130 // Copy the |server_config_|, |source_address_token_|, |certs_| and
131 // |server_config_sig_| from the |other|. The remaining fields,
132 // |generation_counter_|, |proof_verify_details_|, and |scfg_| remain
133 // unchanged.
134 void InitializeFrom(const CachedState& other);
136 // Initializes this cached state based on the arguments provided.
137 // Returns false if there is a problem parsing the server config.
138 bool Initialize(base::StringPiece server_config,
139 base::StringPiece source_address_token,
140 const std::vector<std::string>& certs,
141 base::StringPiece signature,
142 QuicWallTime now);
144 private:
145 std::string server_config_; // A serialized handshake message.
146 std::string source_address_token_; // An opaque proof of IP ownership.
147 std::vector<std::string> certs_; // A list of certificates in leaf-first
148 // order.
149 std::string server_config_sig_; // A signature of |server_config_|.
150 bool server_config_valid_; // True if |server_config_| is correctly
151 // signed and |certs_| has been
152 // validated.
153 // Generation counter associated with the |server_config_|, |certs_| and
154 // |server_config_sig_| combination. It is incremented whenever we set
155 // server_config_valid_ to false.
156 uint64 generation_counter_;
158 scoped_ptr<ProofVerifyDetails> proof_verify_details_;
160 // scfg contains the cached, parsed value of |server_config|.
161 mutable scoped_ptr<CryptoHandshakeMessage> scfg_;
163 // TODO(jokulik): Consider using a hash-set as extra book-keeping to ensure
164 // that no connection-id is added twice.
165 std::queue<QuicConnectionId> server_designated_connection_ids_;
167 DISALLOW_COPY_AND_ASSIGN(CachedState);
170 QuicCryptoClientConfig();
171 ~QuicCryptoClientConfig();
173 // LookupOrCreate returns a CachedState for the given |server_id|. If no such
174 // CachedState currently exists, it will be created and cached.
175 CachedState* LookupOrCreate(const QuicServerId& server_id);
177 // Delete all CachedState objects from cached_states_.
178 void ClearCachedStates();
180 // FillInchoateClientHello sets |out| to be a CHLO message that elicits a
181 // source-address token or SCFG from a server. If |cached| is non-nullptr, the
182 // source-address token will be taken from it. |out_params| is used in order
183 // to store the cached certs that were sent as hints to the server in
184 // |out_params->cached_certs|. |preferred_version| is the version of the
185 // QUIC protocol that this client chose to use initially. This allows the
186 // server to detect downgrade attacks.
187 void FillInchoateClientHello(const QuicServerId& server_id,
188 const QuicVersion preferred_version,
189 const CachedState* cached,
190 QuicCryptoNegotiatedParameters* out_params,
191 CryptoHandshakeMessage* out) const;
193 // FillClientHello sets |out| to be a CHLO message based on the configuration
194 // of this object. This object must have cached enough information about
195 // the server's hostname in order to perform a handshake. This can be checked
196 // with the |IsComplete| member of |CachedState|.
198 // |now| and |rand| are used to generate the nonce and |out_params| is
199 // filled with the results of the handshake that the server is expected to
200 // accept. |preferred_version| is the version of the QUIC protocol that this
201 // client chose to use initially. This allows the server to detect downgrade
202 // attacks.
204 // If |channel_id_key| is not null, it is used to sign a secret value derived
205 // from the client and server's keys, and the Channel ID public key and the
206 // signature are placed in the CETV value of the CHLO.
207 QuicErrorCode FillClientHello(const QuicServerId& server_id,
208 QuicConnectionId connection_id,
209 const QuicVersion preferred_version,
210 const CachedState* cached,
211 QuicWallTime now,
212 QuicRandom* rand,
213 const ChannelIDKey* channel_id_key,
214 QuicCryptoNegotiatedParameters* out_params,
215 CryptoHandshakeMessage* out,
216 std::string* error_details) const;
218 // ProcessRejection processes a REJ message from a server and updates the
219 // cached information about that server. After this, |IsComplete| may return
220 // true for that server's CachedState. If the rejection message contains state
221 // about a future handshake (i.e. an nonce value from the server), then it
222 // will be saved in |out_params|. |now| is used to judge whether the server
223 // config in the rejection message has expired. |is_https| is used to track
224 // reject reason for secure vs insecure QUIC. If the rejection message
225 // indicates that the reject is a stateless-reject, returns error code
226 // QUIC_CRYPTO_HANDSHAKE_RECEIVED_STATELESS_REJECT.
227 QuicErrorCode ProcessRejection(const CryptoHandshakeMessage& rej,
228 QuicWallTime now,
229 CachedState* cached,
230 bool is_https,
231 QuicCryptoNegotiatedParameters* out_params,
232 std::string* error_details);
234 // ProcessServerHello processes the message in |server_hello|, updates the
235 // cached information about that server, writes the negotiated parameters to
236 // |out_params| and returns QUIC_NO_ERROR. If |server_hello| is unacceptable
237 // then it puts an error message in |error_details| and returns an error
238 // code. |negotiated_versions| contains the list of version, if any, that were
239 // present in a version negotiation packet previously recevied from the
240 // server. The contents of this list will be compared against the list of
241 // versions provided in the VER tag of the server hello.
242 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello,
243 QuicConnectionId connection_id,
244 const QuicVersionVector& negotiated_versions,
245 CachedState* cached,
246 QuicCryptoNegotiatedParameters* out_params,
247 std::string* error_details);
249 // Processes the message in |server_update|, updating the cached source
250 // address token, and server config.
251 // If |server_update| is invalid then |error_details| will contain an error
252 // message, and an error code will be returned. If all has gone well
253 // QUIC_NO_ERROR is returned.
254 QuicErrorCode ProcessServerConfigUpdate(
255 const CryptoHandshakeMessage& server_update,
256 QuicWallTime now,
257 CachedState* cached,
258 QuicCryptoNegotiatedParameters* out_params,
259 std::string* error_details);
261 ProofVerifier* proof_verifier() const;
263 // SetProofVerifier takes ownership of a |ProofVerifier| that clients are
264 // free to use in order to verify certificate chains from servers. If a
265 // ProofVerifier is set then the client will request a certificate chain from
266 // the server.
267 void SetProofVerifier(ProofVerifier* verifier);
269 ChannelIDSource* channel_id_source() const;
271 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
272 // server supports channel IDs, to obtain a channel ID for signing a message
273 // proving possession of the channel ID. This object takes ownership of
274 // |source|.
275 void SetChannelIDSource(ChannelIDSource* source);
277 // Initialize the CachedState from |canonical_crypto_config| for the
278 // |canonical_server_id| as the initial CachedState for |server_id|. We will
279 // copy config data only if |canonical_crypto_config| has valid proof.
280 void InitializeFrom(const QuicServerId& server_id,
281 const QuicServerId& canonical_server_id,
282 QuicCryptoClientConfig* canonical_crypto_config);
284 // Adds |suffix| as a domain suffix for which the server's crypto config
285 // is expected to be shared among servers with the domain suffix. If a server
286 // matches this suffix, then the server config from another server with the
287 // suffix will be used to initialize the cached state for this server.
288 void AddCanonicalSuffix(const std::string& suffix);
290 // Prefers AES-GCM (kAESG) over other AEAD algorithms. Call this method if
291 // the CPU has hardware acceleration for AES-GCM. This method can only be
292 // called after SetDefaults().
293 void PreferAesGcm();
295 // Disables the use of ECDSA for proof verification.
296 // Call this method on platforms that do not support ECDSA.
297 // TODO(rch): remove this method when we drop support for Windows XP.
298 void DisableEcdsa();
300 // Saves the |user_agent_id| that will be passed in QUIC's CHLO message.
301 void set_user_agent_id(const std::string& user_agent_id) {
302 user_agent_id_ = user_agent_id;
305 private:
306 typedef std::map<QuicServerId, CachedState*> CachedStateMap;
308 // Sets the members to reasonable, default values.
309 void SetDefaults();
311 // CacheNewServerConfig checks for SCFG, STK, PROF, and CRT tags in |message|,
312 // verifies them, and stores them in the cached state if they validate.
313 // This is used on receipt of a REJ from a server, or when a server sends
314 // updated server config during a connection.
315 QuicErrorCode CacheNewServerConfig(
316 const CryptoHandshakeMessage& message,
317 QuicWallTime now,
318 const std::vector<std::string>& cached_certs,
319 CachedState* cached,
320 std::string* error_details);
322 // If the suffix of the hostname in |server_id| is in |canonical_suffixes_|,
323 // then populate |cached| with the canonical cached state from
324 // |canonical_server_map_| for that suffix. Returns true if |cached| is
325 // initialized with canonical cached state.
326 bool PopulateFromCanonicalConfig(const QuicServerId& server_id,
327 CachedState* cached);
329 // cached_states_ maps from the server_id to the cached information about
330 // that server.
331 CachedStateMap cached_states_;
333 // Contains a map of servers which could share the same server config. Map
334 // from a canonical host suffix/port/scheme to a representative server with
335 // the canonical suffix, which has a plausible set of initial certificates
336 // (or at least server public key).
337 std::map<QuicServerId, QuicServerId> canonical_server_map_;
339 // Contains list of suffixes (for exmaple ".c.youtube.com",
340 // ".googlevideo.com") of canonical hostnames.
341 std::vector<std::string> canonical_suffixes_;
343 scoped_ptr<ProofVerifier> proof_verifier_;
344 scoped_ptr<ChannelIDSource> channel_id_source_;
346 // True if ECDSA should be disabled.
347 bool disable_ecdsa_;
349 // The |user_agent_id_| passed in QUIC's CHLO message.
350 std::string user_agent_id_;
352 DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientConfig);
355 } // namespace net
357 #endif // NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_