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