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_
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"
23 class ChannelIDSource
;
24 class CryptoHandshakeMessage
;
26 class ProofVerifyDetails
;
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
{
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
{
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.
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.
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|
76 ServerConfigState
SetServerConfig(base::StringPiece server_config
,
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.
90 // Clears the certificate chain and signature and invalidates the proof.
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.)
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
125 QuicConnectionId
GetNextServerDesignatedConnectionId();
127 // Adds the servernonce to the queue of server nonces.
128 void add_server_nonce(const std::string
& server_nonce
);
130 // If true, the crypto config contains at least one server nonce, and the
131 // client should use one of these nonces.
132 bool has_server_nonce() const;
134 // This function should only be called when has_server_nonce is true.
135 // Returns the next connection_id specified by the server and removes it
136 // from the queue of ids.
137 std::string
GetNextServerNonce();
139 // SetProofVerifyDetails takes ownership of |details|.
140 void SetProofVerifyDetails(ProofVerifyDetails
* details
);
142 // Copy the |server_config_|, |source_address_token_|, |certs_| and
143 // |server_config_sig_| from the |other|. The remaining fields,
144 // |generation_counter_|, |proof_verify_details_|, and |scfg_| remain
146 void InitializeFrom(const CachedState
& other
);
148 // Initializes this cached state based on the arguments provided.
149 // Returns false if there is a problem parsing the server config.
150 bool Initialize(base::StringPiece server_config
,
151 base::StringPiece source_address_token
,
152 const std::vector
<std::string
>& certs
,
153 base::StringPiece signature
,
157 std::string server_config_
; // A serialized handshake message.
158 std::string source_address_token_
; // An opaque proof of IP ownership.
159 std::vector
<std::string
> certs_
; // A list of certificates in leaf-first
161 std::string server_config_sig_
; // A signature of |server_config_|.
162 bool server_config_valid_
; // True if |server_config_| is correctly
163 // signed and |certs_| has been
165 // Generation counter associated with the |server_config_|, |certs_| and
166 // |server_config_sig_| combination. It is incremented whenever we set
167 // server_config_valid_ to false.
168 uint64 generation_counter_
;
170 scoped_ptr
<ProofVerifyDetails
> proof_verify_details_
;
172 // scfg contains the cached, parsed value of |server_config|.
173 mutable scoped_ptr
<CryptoHandshakeMessage
> scfg_
;
175 // TODO(jokulik): Consider using a hash-set as extra book-keeping to ensure
176 // that no connection-id is added twice. Also, consider keeping the server
177 // nonces and connection_ids together in one queue.
178 std::queue
<QuicConnectionId
> server_designated_connection_ids_
;
179 std::queue
<std::string
> server_nonces_
;
181 DISALLOW_COPY_AND_ASSIGN(CachedState
);
184 QuicCryptoClientConfig();
185 ~QuicCryptoClientConfig();
187 // LookupOrCreate returns a CachedState for the given |server_id|. If no such
188 // CachedState currently exists, it will be created and cached.
189 CachedState
* LookupOrCreate(const QuicServerId
& server_id
);
191 // Delete all CachedState objects from cached_states_.
192 void ClearCachedStates();
194 // FillInchoateClientHello sets |out| to be a CHLO message that elicits a
195 // source-address token or SCFG from a server. If |cached| is non-nullptr, the
196 // source-address token will be taken from it. |out_params| is used in order
197 // to store the cached certs that were sent as hints to the server in
198 // |out_params->cached_certs|. |preferred_version| is the version of the
199 // QUIC protocol that this client chose to use initially. This allows the
200 // server to detect downgrade attacks.
201 void FillInchoateClientHello(const QuicServerId
& server_id
,
202 const QuicVersion preferred_version
,
203 const CachedState
* cached
,
204 QuicCryptoNegotiatedParameters
* out_params
,
205 CryptoHandshakeMessage
* out
) const;
207 // FillClientHello sets |out| to be a CHLO message based on the configuration
208 // of this object. This object must have cached enough information about
209 // the server's hostname in order to perform a handshake. This can be checked
210 // with the |IsComplete| member of |CachedState|.
212 // |now| and |rand| are used to generate the nonce and |out_params| is
213 // filled with the results of the handshake that the server is expected to
214 // accept. |preferred_version| is the version of the QUIC protocol that this
215 // client chose to use initially. This allows the server to detect downgrade
218 // If |channel_id_key| is not null, it is used to sign a secret value derived
219 // from the client and server's keys, and the Channel ID public key and the
220 // signature are placed in the CETV value of the CHLO.
221 QuicErrorCode
FillClientHello(const QuicServerId
& server_id
,
222 QuicConnectionId connection_id
,
223 const QuicVersion preferred_version
,
224 const CachedState
* cached
,
227 const ChannelIDKey
* channel_id_key
,
228 QuicCryptoNegotiatedParameters
* out_params
,
229 CryptoHandshakeMessage
* out
,
230 std::string
* error_details
) const;
232 // ProcessRejection processes a REJ message from a server and updates the
233 // cached information about that server. After this, |IsComplete| may return
234 // true for that server's CachedState. If the rejection message contains state
235 // about a future handshake (i.e. an nonce value from the server), then it
236 // will be saved in |out_params|. |now| is used to judge whether the server
237 // config in the rejection message has expired. |is_https| is used to track
238 // reject reason for secure vs insecure QUIC.
239 QuicErrorCode
ProcessRejection(const CryptoHandshakeMessage
& rej
,
243 QuicCryptoNegotiatedParameters
* out_params
,
244 std::string
* error_details
);
246 // ProcessServerHello processes the message in |server_hello|, updates the
247 // cached information about that server, writes the negotiated parameters to
248 // |out_params| and returns QUIC_NO_ERROR. If |server_hello| is unacceptable
249 // then it puts an error message in |error_details| and returns an error
250 // code. |negotiated_versions| contains the list of version, if any, that were
251 // present in a version negotiation packet previously recevied from the
252 // server. The contents of this list will be compared against the list of
253 // versions provided in the VER tag of the server hello.
254 QuicErrorCode
ProcessServerHello(const CryptoHandshakeMessage
& server_hello
,
255 QuicConnectionId connection_id
,
256 const QuicVersionVector
& negotiated_versions
,
258 QuicCryptoNegotiatedParameters
* out_params
,
259 std::string
* error_details
);
261 // Processes the message in |server_update|, updating the cached source
262 // address token, and server config.
263 // If |server_update| is invalid then |error_details| will contain an error
264 // message, and an error code will be returned. If all has gone well
265 // QUIC_NO_ERROR is returned.
266 QuicErrorCode
ProcessServerConfigUpdate(
267 const CryptoHandshakeMessage
& server_update
,
270 QuicCryptoNegotiatedParameters
* out_params
,
271 std::string
* error_details
);
273 ProofVerifier
* proof_verifier() const;
275 // SetProofVerifier takes ownership of a |ProofVerifier| that clients are
276 // free to use in order to verify certificate chains from servers. If a
277 // ProofVerifier is set then the client will request a certificate chain from
279 void SetProofVerifier(ProofVerifier
* verifier
);
281 ChannelIDSource
* channel_id_source() const;
283 // SetChannelIDSource sets a ChannelIDSource that will be called, when the
284 // server supports channel IDs, to obtain a channel ID for signing a message
285 // proving possession of the channel ID. This object takes ownership of
287 void SetChannelIDSource(ChannelIDSource
* source
);
289 // Initialize the CachedState from |canonical_crypto_config| for the
290 // |canonical_server_id| as the initial CachedState for |server_id|. We will
291 // copy config data only if |canonical_crypto_config| has valid proof.
292 void InitializeFrom(const QuicServerId
& server_id
,
293 const QuicServerId
& canonical_server_id
,
294 QuicCryptoClientConfig
* canonical_crypto_config
);
296 // Adds |suffix| as a domain suffix for which the server's crypto config
297 // is expected to be shared among servers with the domain suffix. If a server
298 // matches this suffix, then the server config from another server with the
299 // suffix will be used to initialize the cached state for this server.
300 void AddCanonicalSuffix(const std::string
& suffix
);
302 // Prefers AES-GCM (kAESG) over other AEAD algorithms. Call this method if
303 // the CPU has hardware acceleration for AES-GCM. This method can only be
304 // called after SetDefaults().
307 // Disables the use of ECDSA for proof verification.
308 // Call this method on platforms that do not support ECDSA.
309 // TODO(rch): remove this method when we drop support for Windows XP.
312 // Saves the |user_agent_id| that will be passed in QUIC's CHLO message.
313 void set_user_agent_id(const std::string
& user_agent_id
) {
314 user_agent_id_
= user_agent_id
;
318 typedef std::map
<QuicServerId
, CachedState
*> CachedStateMap
;
320 // Sets the members to reasonable, default values.
323 // CacheNewServerConfig checks for SCFG, STK, PROF, and CRT tags in |message|,
324 // verifies them, and stores them in the cached state if they validate.
325 // This is used on receipt of a REJ from a server, or when a server sends
326 // updated server config during a connection.
327 QuicErrorCode
CacheNewServerConfig(
328 const CryptoHandshakeMessage
& message
,
330 const std::vector
<std::string
>& cached_certs
,
332 std::string
* error_details
);
334 // If the suffix of the hostname in |server_id| is in |canonical_suffixes_|,
335 // then populate |cached| with the canonical cached state from
336 // |canonical_server_map_| for that suffix. Returns true if |cached| is
337 // initialized with canonical cached state.
338 bool PopulateFromCanonicalConfig(const QuicServerId
& server_id
,
339 CachedState
* cached
);
341 // cached_states_ maps from the server_id to the cached information about
343 CachedStateMap cached_states_
;
345 // Contains a map of servers which could share the same server config. Map
346 // from a canonical host suffix/port/scheme to a representative server with
347 // the canonical suffix, which has a plausible set of initial certificates
348 // (or at least server public key).
349 std::map
<QuicServerId
, QuicServerId
> canonical_server_map_
;
351 // Contains list of suffixes (for exmaple ".c.youtube.com",
352 // ".googlevideo.com") of canonical hostnames.
353 std::vector
<std::string
> canonical_suffixes_
;
355 scoped_ptr
<ProofVerifier
> proof_verifier_
;
356 scoped_ptr
<ChannelIDSource
> channel_id_source_
;
358 // True if ECDSA should be disabled.
361 // The |user_agent_id_| passed in QUIC's CHLO message.
362 std::string user_agent_id_
;
364 DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientConfig
);
369 #endif // NET_QUIC_CRYPTO_QUIC_CRYPTO_CLIENT_CONFIG_H_