| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ | 5 #ifndef NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ |
| 6 #define NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ | 6 #define NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 14 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 15 #include "net/quic/crypto/crypto_protocol.h" | 15 #include "net/quic/crypto/crypto_protocol.h" |
| 16 #include "net/quic/quic_protocol.h" | 16 #include "net/quic/quic_protocol.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 class ChannelIDSigner; |
| 20 class CommonCertSets; | 21 class CommonCertSets; |
| 21 class KeyExchange; | 22 class KeyExchange; |
| 22 class ProofVerifier; | 23 class ProofVerifier; |
| 23 class QuicDecrypter; | 24 class QuicDecrypter; |
| 24 class QuicEncrypter; | 25 class QuicEncrypter; |
| 25 class QuicRandom; | 26 class QuicRandom; |
| 26 | 27 |
| 27 // An intermediate format of a handshake message that's convenient for a | 28 // An intermediate format of a handshake message that's convenient for a |
| 28 // CryptoFramer to serialize from or parse into. | 29 // CryptoFramer to serialize from or parse into. |
| 29 class NET_EXPORT_PRIVATE CryptoHandshakeMessage { | 30 class NET_EXPORT_PRIVATE CryptoHandshakeMessage { |
| 30 public: | 31 public: |
| 31 CryptoHandshakeMessage(); | 32 CryptoHandshakeMessage(); |
| 32 CryptoHandshakeMessage(const CryptoHandshakeMessage& other); | 33 CryptoHandshakeMessage(const CryptoHandshakeMessage& other); |
| 33 ~CryptoHandshakeMessage(); | 34 ~CryptoHandshakeMessage(); |
| 34 | 35 |
| 35 CryptoHandshakeMessage& operator=(const CryptoHandshakeMessage& other); | 36 CryptoHandshakeMessage& operator=(const CryptoHandshakeMessage& other); |
| 36 | 37 |
| 37 // Clears state. | 38 // Clears state. |
| 38 void Clear(); | 39 void Clear(); |
| 39 | 40 |
| 40 // GetSerialized returns the serialized form of this message and caches the | 41 // GetSerialized returns the serialized form of this message and caches the |
| 41 // result. Subsequently altering the message does not invalidate the cache. | 42 // result. Subsequently altering the message does not invalidate the cache. |
| 42 const QuicData& GetSerialized() const; | 43 const QuicData& GetSerialized() const; |
| 43 | 44 |
| 45 // MarkDirty invalidates the cache created by |GetSerialized|. |
| 46 void MarkDirty(); |
| 47 |
| 44 // SetValue sets an element with the given tag to the raw, memory contents of | 48 // SetValue sets an element with the given tag to the raw, memory contents of |
| 45 // |v|. | 49 // |v|. |
| 46 template<class T> void SetValue(QuicTag tag, const T& v) { | 50 template<class T> void SetValue(QuicTag tag, const T& v) { |
| 47 tag_value_map_[tag] = | 51 tag_value_map_[tag] = |
| 48 std::string(reinterpret_cast<const char*>(&v), sizeof(v)); | 52 std::string(reinterpret_cast<const char*>(&v), sizeof(v)); |
| 49 } | 53 } |
| 50 | 54 |
| 51 // SetVector sets an element with the given tag to the raw contents of an | 55 // SetVector sets an element with the given tag to the raw contents of an |
| 52 // array of elements in |v|. | 56 // array of elements in |v|. |
| 53 template<class T> void SetVector(QuicTag tag, const std::vector<T>& v) { | 57 template<class T> void SetVector(QuicTag tag, const std::vector<T>& v) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 68 | 72 |
| 69 void Insert(QuicTagValueMap::const_iterator begin, | 73 void Insert(QuicTagValueMap::const_iterator begin, |
| 70 QuicTagValueMap::const_iterator end); | 74 QuicTagValueMap::const_iterator end); |
| 71 | 75 |
| 72 // SetTaglist sets an element with the given tag to contain a list of tags, | 76 // SetTaglist sets an element with the given tag to contain a list of tags, |
| 73 // passed as varargs. The argument list must be terminated with a 0 element. | 77 // passed as varargs. The argument list must be terminated with a 0 element. |
| 74 void SetTaglist(QuicTag tag, ...); | 78 void SetTaglist(QuicTag tag, ...); |
| 75 | 79 |
| 76 void SetStringPiece(QuicTag tag, base::StringPiece value); | 80 void SetStringPiece(QuicTag tag, base::StringPiece value); |
| 77 | 81 |
| 82 // Erase removes a tag/value, if present, from the message. |
| 83 void Erase(QuicTag tag); |
| 84 |
| 78 // GetTaglist finds an element with the given tag containing zero or more | 85 // GetTaglist finds an element with the given tag containing zero or more |
| 79 // tags. If such a tag doesn't exist, it returns false. Otherwise it sets | 86 // tags. If such a tag doesn't exist, it returns false. Otherwise it sets |
| 80 // |out_tags| and |out_len| to point to the array of tags and returns true. | 87 // |out_tags| and |out_len| to point to the array of tags and returns true. |
| 81 // The array points into the CryptoHandshakeMessage and is valid only for as | 88 // The array points into the CryptoHandshakeMessage and is valid only for as |
| 82 // long as the CryptoHandshakeMessage exists and is not modified. | 89 // long as the CryptoHandshakeMessage exists and is not modified. |
| 83 QuicErrorCode GetTaglist(QuicTag tag, const QuicTag** out_tags, | 90 QuicErrorCode GetTaglist(QuicTag tag, const QuicTag** out_tags, |
| 84 size_t* out_len) const; | 91 size_t* out_len) const; |
| 85 | 92 |
| 86 bool GetStringPiece(QuicTag tag, base::StringPiece* out) const; | 93 bool GetStringPiece(QuicTag tag, base::StringPiece* out) const; |
| 87 | 94 |
| 88 // GetNthValue24 interprets the value with the given tag to be a series of | 95 // GetNthValue24 interprets the value with the given tag to be a series of |
| 89 // 24-bit, length prefixed values and it returns the subvalue with the given | 96 // 24-bit, length prefixed values and it returns the subvalue with the given |
| 90 // index. | 97 // index. |
| 91 QuicErrorCode GetNthValue24(QuicTag tag, | 98 QuicErrorCode GetNthValue24(QuicTag tag, |
| 92 unsigned index, | 99 unsigned index, |
| 93 base::StringPiece* out) const; | 100 base::StringPiece* out) const; |
| 94 bool GetString(QuicTag tag, std::string* out) const; | |
| 95 QuicErrorCode GetUint16(QuicTag tag, uint16* out) const; | 101 QuicErrorCode GetUint16(QuicTag tag, uint16* out) const; |
| 96 QuicErrorCode GetUint32(QuicTag tag, uint32* out) const; | 102 QuicErrorCode GetUint32(QuicTag tag, uint32* out) const; |
| 97 QuicErrorCode GetUint64(QuicTag tag, uint64* out) const; | 103 QuicErrorCode GetUint64(QuicTag tag, uint64* out) const; |
| 98 | 104 |
| 99 // size returns 4 (message tag) + 2 (uint16, number of entries) + | 105 // size returns 4 (message tag) + 2 (uint16, number of entries) + |
| 100 // (4 (tag) + 4 (end offset))*tag_value_map_.size() + ∑ value sizes. | 106 // (4 (tag) + 4 (end offset))*tag_value_map_.size() + ∑ value sizes. |
| 101 size_t size() const; | 107 size_t size() const; |
| 102 | 108 |
| 103 // set_minimum_size sets the minimum number of bytes that the message should | 109 // set_minimum_size sets the minimum number of bytes that the message should |
| 104 // consume. The CryptoFramer will add a PAD tag as needed when serializing in | 110 // consume. The CryptoFramer will add a PAD tag as needed when serializing in |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // client hello and server config. This is only populated in the client | 173 // client hello and server config. This is only populated in the client |
| 168 // because only the client needs to derive the forward secure keys at a later | 174 // because only the client needs to derive the forward secure keys at a later |
| 169 // time from the initial keys. | 175 // time from the initial keys. |
| 170 std::string hkdf_input_suffix; | 176 std::string hkdf_input_suffix; |
| 171 // cached_certs contains the cached certificates that a client used when | 177 // cached_certs contains the cached certificates that a client used when |
| 172 // sending a client hello. | 178 // sending a client hello. |
| 173 std::vector<std::string> cached_certs; | 179 std::vector<std::string> cached_certs; |
| 174 // client_key_exchange is used by clients to store the ephemeral KeyExchange | 180 // client_key_exchange is used by clients to store the ephemeral KeyExchange |
| 175 // for the connection. | 181 // for the connection. |
| 176 scoped_ptr<KeyExchange> client_key_exchange; | 182 scoped_ptr<KeyExchange> client_key_exchange; |
| 183 // channel_id is set by servers to a ChannelID key when the client correctly |
| 184 // proves possession of the corresponding private key. It consists of 32 |
| 185 // bytes of x coordinate, followed by 32 bytes of y coordinate. Both values |
| 186 // are big-endian and the pair is a P-256 public key. |
| 187 std::string channel_id; |
| 177 }; | 188 }; |
| 178 | 189 |
| 179 // QuicCryptoConfig contains common configuration between clients and servers. | 190 // QuicCryptoConfig contains common configuration between clients and servers. |
| 180 class NET_EXPORT_PRIVATE QuicCryptoConfig { | 191 class NET_EXPORT_PRIVATE QuicCryptoConfig { |
| 181 public: | 192 public: |
| 182 enum { | 193 enum { |
| 183 // CONFIG_VERSION is the one (and, for the moment, only) version number that | 194 // CONFIG_VERSION is the one (and, for the moment, only) version number that |
| 184 // we implement. | 195 // we implement. |
| 185 CONFIG_VERSION = 0, | 196 CONFIG_VERSION = 0, |
| 186 }; | 197 }; |
| 187 | 198 |
| 188 // kInitialLabel is a constant that is used when deriving the initial | 199 // kInitialLabel is a constant that is used when deriving the initial |
| 189 // (non-forward secure) keys for the connection in order to tie the resulting | 200 // (non-forward secure) keys for the connection in order to tie the resulting |
| 190 // key to this protocol. | 201 // key to this protocol. |
| 191 static const char kInitialLabel[]; | 202 static const char kInitialLabel[]; |
| 192 | 203 |
| 204 // kCETVLabel is a constant that is used when deriving the keys for the |
| 205 // encrypted tag/value block in the client hello. |
| 206 static const char kCETVLabel[]; |
| 207 |
| 193 // kForwardSecureLabel is a constant that is used when deriving the forward | 208 // kForwardSecureLabel is a constant that is used when deriving the forward |
| 194 // secure keys for the connection in order to tie the resulting key to this | 209 // secure keys for the connection in order to tie the resulting key to this |
| 195 // protocol. | 210 // protocol. |
| 196 static const char kForwardSecureLabel[]; | 211 static const char kForwardSecureLabel[]; |
| 197 | 212 |
| 198 QuicCryptoConfig(); | 213 QuicCryptoConfig(); |
| 199 ~QuicCryptoConfig(); | 214 ~QuicCryptoConfig(); |
| 200 | 215 |
| 201 // Protocol version | 216 // Protocol version |
| 202 uint16 version; | 217 uint16 version; |
| 203 // Key exchange methods. The following two members' values correspond by | 218 // Key exchange methods. The following two members' values correspond by |
| 204 // index. | 219 // index. |
| 205 QuicTagVector kexs; | 220 QuicTagVector kexs; |
| 206 // Authenticated encryption with associated data (AEAD) algorithms. | 221 // Authenticated encryption with associated data (AEAD) algorithms. |
| 207 QuicTagVector aead; | 222 QuicTagVector aead; |
| 208 | 223 |
| 209 scoped_ptr<CommonCertSets> common_cert_sets; | 224 const CommonCertSets* common_cert_sets; |
| 210 | 225 |
| 211 private: | 226 private: |
| 212 DISALLOW_COPY_AND_ASSIGN(QuicCryptoConfig); | 227 DISALLOW_COPY_AND_ASSIGN(QuicCryptoConfig); |
| 213 }; | 228 }; |
| 214 | 229 |
| 215 // QuicCryptoClientConfig contains crypto-related configuration settings for a | 230 // QuicCryptoClientConfig contains crypto-related configuration settings for a |
| 216 // client. Note that this object isn't thread-safe. It's designed to be used on | 231 // client. Note that this object isn't thread-safe. It's designed to be used on |
| 217 // a single thread at a time. | 232 // a single thread at a time. |
| 218 class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig { | 233 class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig { |
| 219 public: | 234 public: |
| 220 // A CachedState contains the information that the client needs in order to | 235 // A CachedState contains the information that the client needs in order to |
| 221 // perform a 0-RTT handshake with a server. This information can be reused | 236 // perform a 0-RTT handshake with a server. This information can be reused |
| 222 // over several connections to the same server. | 237 // over several connections to the same server. |
| 223 class CachedState { | 238 class CachedState { |
| 224 public: | 239 public: |
| 225 CachedState(); | 240 CachedState(); |
| 226 ~CachedState(); | 241 ~CachedState(); |
| 227 | 242 |
| 228 // is_complete returns true if this object contains enough information to | 243 // IsComplete returns true if this object contains enough information to |
| 229 // perform a handshake with the server. | 244 // perform a handshake with the server. |now| is used to judge whether any |
| 230 bool is_complete() const; | 245 // cached server config has expired. |
| 246 bool IsComplete(QuicWallTime now) const; |
| 231 | 247 |
| 232 // GetServerConfig returns the parsed contents of |server_config|, or NULL | 248 // GetServerConfig returns the parsed contents of |server_config|, or NULL |
| 233 // if |server_config| is empty. The return value is owned by this object | 249 // if |server_config| is empty. The return value is owned by this object |
| 234 // and is destroyed when this object is. | 250 // and is destroyed when this object is. |
| 235 const CryptoHandshakeMessage* GetServerConfig() const; | 251 const CryptoHandshakeMessage* GetServerConfig() const; |
| 236 | 252 |
| 237 // SetServerConfig checks that |scfg| parses correctly and stores it in | 253 // SetServerConfig checks that |server_config| parses correctly and stores |
| 238 // |server_config|. It returns true if the parsing succeeds and false | 254 // it in |server_config_|. |now| is used to judge whether |server_config| |
| 239 // otherwise. | 255 // has expired. |
| 240 bool SetServerConfig(base::StringPiece scfg); | 256 QuicErrorCode SetServerConfig(base::StringPiece server_config, |
| 257 QuicWallTime now, |
| 258 std::string* error_details); |
| 259 |
| 260 // InvalidateServerConfig clears the cached server config (if any). |
| 261 void InvalidateServerConfig(); |
| 241 | 262 |
| 242 // SetProof stores a certificate chain and signature. | 263 // SetProof stores a certificate chain and signature. |
| 243 void SetProof(const std::vector<std::string>& certs, | 264 void SetProof(const std::vector<std::string>& certs, |
| 244 base::StringPiece signature); | 265 base::StringPiece signature); |
| 245 | 266 |
| 246 // SetProofValid records that the certificate chain and signature have been | 267 // SetProofValid records that the certificate chain and signature have been |
| 247 // validated and that it's safe to assume that the server is legitimate. | 268 // validated and that it's safe to assume that the server is legitimate. |
| 248 // (Note: this does not check the chain or signature.) | 269 // (Note: this does not check the chain or signature.) |
| 249 void SetProofValid(); | 270 void SetProofValid(); |
| 250 | 271 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 // to store the cached certs that were sent as hints to the server in | 307 // to store the cached certs that were sent as hints to the server in |
| 287 // |out_params->cached_certs|. | 308 // |out_params->cached_certs|. |
| 288 void FillInchoateClientHello(const std::string& server_hostname, | 309 void FillInchoateClientHello(const std::string& server_hostname, |
| 289 const CachedState* cached, | 310 const CachedState* cached, |
| 290 QuicCryptoNegotiatedParameters* out_params, | 311 QuicCryptoNegotiatedParameters* out_params, |
| 291 CryptoHandshakeMessage* out) const; | 312 CryptoHandshakeMessage* out) const; |
| 292 | 313 |
| 293 // FillClientHello sets |out| to be a CHLO message based on the configuration | 314 // FillClientHello sets |out| to be a CHLO message based on the configuration |
| 294 // of this object. This object must have cached enough information about | 315 // of this object. This object must have cached enough information about |
| 295 // |server_hostname| in order to perform a handshake. This can be checked | 316 // |server_hostname| in order to perform a handshake. This can be checked |
| 296 // with the |is_complete| member of |CachedState|. | 317 // with the |IsComplete| member of |CachedState|. |
| 297 // | 318 // |
| 298 // |clock| and |rand| are used to generate the nonce and |out_params| is | 319 // |clock| and |rand| are used to generate the nonce and |out_params| is |
| 299 // filled with the results of the handshake that the server is expected to | 320 // filled with the results of the handshake that the server is expected to |
| 300 // accept. | 321 // accept. |
| 301 QuicErrorCode FillClientHello(const std::string& server_hostname, | 322 QuicErrorCode FillClientHello(const std::string& server_hostname, |
| 302 QuicGuid guid, | 323 QuicGuid guid, |
| 303 const CachedState* cached, | 324 const CachedState* cached, |
| 304 QuicWallTime now, | 325 QuicWallTime now, |
| 305 QuicRandom* rand, | 326 QuicRandom* rand, |
| 306 QuicCryptoNegotiatedParameters* out_params, | 327 QuicCryptoNegotiatedParameters* out_params, |
| 307 CryptoHandshakeMessage* out, | 328 CryptoHandshakeMessage* out, |
| 308 std::string* error_details) const; | 329 std::string* error_details) const; |
| 309 | 330 |
| 310 // ProcessRejection processes a REJ message from a server and updates the | 331 // ProcessRejection processes a REJ message from a server and updates the |
| 311 // cached information about that server. After this, |is_complete| may return | 332 // cached information about that server. After this, |IsComplete| may return |
| 312 // true for that server's CachedState. If the rejection message contains | 333 // true for that server's CachedState. If the rejection message contains |
| 313 // state about a future handshake (i.e. an nonce value from the server), then | 334 // state about a future handshake (i.e. an nonce value from the server), then |
| 314 // it will be saved in |out_params|. | 335 // it will be saved in |out_params|. |now| is used to judge whether the |
| 336 // server config in the rejection message has expired. |
| 315 QuicErrorCode ProcessRejection(CachedState* cached, | 337 QuicErrorCode ProcessRejection(CachedState* cached, |
| 316 const CryptoHandshakeMessage& rej, | 338 const CryptoHandshakeMessage& rej, |
| 339 QuicWallTime now, |
| 317 QuicCryptoNegotiatedParameters* out_params, | 340 QuicCryptoNegotiatedParameters* out_params, |
| 318 std::string* error_details); | 341 std::string* error_details); |
| 319 | 342 |
| 320 // ProcessServerHello processes the message in |server_hello|, writes the | 343 // ProcessServerHello processes the message in |server_hello|, writes the |
| 321 // negotiated parameters to |out_params| and returns QUIC_NO_ERROR. If | 344 // negotiated parameters to |out_params| and returns QUIC_NO_ERROR. If |
| 322 // |server_hello| is unacceptable then it puts an error message in | 345 // |server_hello| is unacceptable then it puts an error message in |
| 323 // |error_details| and returns an error code. | 346 // |error_details| and returns an error code. |
| 324 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, | 347 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, |
| 325 QuicGuid guid, | 348 QuicGuid guid, |
| 326 QuicCryptoNegotiatedParameters* out_params, | 349 QuicCryptoNegotiatedParameters* out_params, |
| 327 std::string* error_details); | 350 std::string* error_details); |
| 328 | 351 |
| 329 const ProofVerifier* proof_verifier() const; | 352 const ProofVerifier* proof_verifier() const; |
| 330 | 353 |
| 331 // SetProofVerifier takes ownership of a |ProofVerifier| that clients are | 354 // SetProofVerifier takes ownership of a |ProofVerifier| that clients are |
| 332 // free to use in order to verify certificate chains from servers. If a | 355 // free to use in order to verify certificate chains from servers. If a |
| 333 // ProofVerifier is set then the client will request a certificate chain from | 356 // ProofVerifier is set then the client will request a certificate chain from |
| 334 // the server. | 357 // the server. |
| 335 void SetProofVerifier(ProofVerifier* verifier); | 358 void SetProofVerifier(ProofVerifier* verifier); |
| 336 | 359 |
| 360 // SetChannelIDSigner sets a ChannelIDSigner that will be called when the |
| 361 // server supports channel IDs to sign a message proving possession of the |
| 362 // given ChannelID. This object takes ownership of |signer|. |
| 363 void SetChannelIDSigner(ChannelIDSigner* signer); |
| 364 |
| 337 private: | 365 private: |
| 338 // cached_states_ maps from the server hostname to the cached information | 366 // cached_states_ maps from the server hostname to the cached information |
| 339 // about that server. | 367 // about that server. |
| 340 std::map<std::string, CachedState*> cached_states_; | 368 std::map<std::string, CachedState*> cached_states_; |
| 341 | 369 |
| 342 scoped_ptr<ProofVerifier> proof_verifier_; | 370 scoped_ptr<ProofVerifier> proof_verifier_; |
| 371 scoped_ptr<ChannelIDSigner> channel_id_signer_; |
| 343 | 372 |
| 344 DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientConfig); | 373 DISALLOW_COPY_AND_ASSIGN(QuicCryptoClientConfig); |
| 345 }; | 374 }; |
| 346 | 375 |
| 347 } // namespace net | 376 } // namespace net |
| 348 | 377 |
| 349 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ | 378 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ |
| OLD | NEW |