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 | 11 |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/string_piece.h" |
11 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
12 #include "net/quic/crypto/crypto_protocol.h" | 15 #include "net/quic/crypto/crypto_protocol.h" |
13 #include "net/quic/crypto/crypto_utils.h" | 16 #include "net/quic/crypto/crypto_utils.h" |
14 | 17 |
15 namespace net { | 18 namespace net { |
16 | 19 |
17 class KeyExchange; | 20 class KeyExchange; |
| 21 class QuicDecrypter; |
| 22 class QuicEncrypter; |
18 class QuicRandom; | 23 class QuicRandom; |
19 class QuicClock; | 24 class QuicClock; |
20 | 25 |
| 26 // An intermediate format of a handshake message that's convenient for a |
| 27 // CryptoFramer to serialize from or parse into. |
| 28 struct NET_EXPORT_PRIVATE CryptoHandshakeMessage { |
| 29 CryptoHandshakeMessage(); |
| 30 CryptoHandshakeMessage(const CryptoHandshakeMessage& other); |
| 31 ~CryptoHandshakeMessage(); |
| 32 |
| 33 CryptoHandshakeMessage& operator=(const CryptoHandshakeMessage& other); |
| 34 |
| 35 // GetSerialized returns the serialized form of this message and caches the |
| 36 // result. Subsequently altering the message does not invalidate the cache. |
| 37 const QuicData& GetSerialized() const; |
| 38 |
| 39 // SetValue sets an element with the given tag to the raw, memory contents of |
| 40 // |v|. |
| 41 template<class T> void SetValue(CryptoTag tag, const T& v) { |
| 42 tag_value_map[tag] = std::string(reinterpret_cast<const char*>(&v), |
| 43 sizeof(v)); |
| 44 } |
| 45 |
| 46 // SetVector sets an element with the given tag to the raw contents of an |
| 47 // array of elements in |v|. |
| 48 template<class T> void SetVector(CryptoTag tag, const std::vector<T>& v) { |
| 49 if (v.empty()) { |
| 50 tag_value_map[tag] = std::string(); |
| 51 } else { |
| 52 tag_value_map[tag] = std::string(reinterpret_cast<const char*>(&v[0]), |
| 53 v.size() * sizeof(T)); |
| 54 } |
| 55 } |
| 56 |
| 57 // SetTaglist sets an element with the given tag to contain a list of tags, |
| 58 // passed as varargs. The argument list must be terminated with a 0 element. |
| 59 void SetTaglist(CryptoTag tag, ...); |
| 60 |
| 61 // GetTaglist finds an element with the given tag containing zero or more |
| 62 // tags. If such a tag doesn't exist, it returns false. Otherwise it sets |
| 63 // |out_tags| and |out_len| to point to the array of tags and returns true. |
| 64 // The array points into the CryptoHandshakeMessage and is valid only for as |
| 65 // long as the CryptoHandshakeMessage exists and is not modified. |
| 66 QuicErrorCode GetTaglist(CryptoTag tag, const CryptoTag** out_tags, |
| 67 size_t* out_len) const; |
| 68 |
| 69 bool GetStringPiece(CryptoTag tag, base::StringPiece* out) const; |
| 70 |
| 71 // GetNthValue16 interprets the value with the given tag to be a series of |
| 72 // 16-bit length prefixed values and it returns the subvalue with the given |
| 73 // index. |
| 74 QuicErrorCode GetNthValue16(CryptoTag tag, |
| 75 unsigned index, |
| 76 base::StringPiece* out) const; |
| 77 bool GetString(CryptoTag tag, std::string* out) const; |
| 78 QuicErrorCode GetUint16(CryptoTag tag, uint16* out) const; |
| 79 QuicErrorCode GetUint32(CryptoTag tag, uint32* out) const; |
| 80 |
| 81 CryptoTag tag; |
| 82 CryptoTagValueMap tag_value_map; |
| 83 |
| 84 private: |
| 85 // GetPOD is a utility function for extracting a plain-old-data value. If |
| 86 // |tag| exists in the message, and has a value of exactly |len| bytes then |
| 87 // it copies |len| bytes of data into |out|. Otherwise |len| bytes at |out| |
| 88 // are zeroed out. |
| 89 // |
| 90 // If used to copy integers then this assumes that the machine is |
| 91 // little-endian. |
| 92 QuicErrorCode GetPOD(CryptoTag tag, void* out, size_t len) const; |
| 93 |
| 94 // The serialized form of the handshake message. This member is constructed |
| 95 // lasily. |
| 96 mutable scoped_ptr<QuicData> serialized_; |
| 97 }; |
| 98 |
21 // TODO(rch): sync with server more rationally | 99 // TODO(rch): sync with server more rationally |
22 class NET_EXPORT_PRIVATE QuicServerConfigProtobuf { | 100 class NET_EXPORT_PRIVATE QuicServerConfigProtobuf { |
23 public: | 101 public: |
24 class NET_EXPORT_PRIVATE PrivateKey { | 102 class NET_EXPORT_PRIVATE PrivateKey { |
25 public: | 103 public: |
26 CryptoTag tag() const { | 104 CryptoTag tag() const { |
27 return tag_; | 105 return tag_; |
28 } | 106 } |
29 void set_tag(CryptoTag tag) { | 107 void set_tag(CryptoTag tag) { |
30 tag_ = tag; | 108 tag_ = tag; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // Parameters negotiated by the crypto handshake. | 152 // Parameters negotiated by the crypto handshake. |
75 struct NET_EXPORT_PRIVATE QuicCryptoNegotiatedParams { | 153 struct NET_EXPORT_PRIVATE QuicCryptoNegotiatedParams { |
76 // Initializes the members to 0 or empty values. | 154 // Initializes the members to 0 or empty values. |
77 QuicCryptoNegotiatedParams(); | 155 QuicCryptoNegotiatedParams(); |
78 ~QuicCryptoNegotiatedParams(); | 156 ~QuicCryptoNegotiatedParams(); |
79 | 157 |
80 uint16 version; | 158 uint16 version; |
81 CryptoTag key_exchange; | 159 CryptoTag key_exchange; |
82 CryptoTag aead; | 160 CryptoTag aead; |
83 std::string premaster_secret; | 161 std::string premaster_secret; |
| 162 scoped_ptr<QuicEncrypter> encrypter; |
| 163 scoped_ptr<QuicDecrypter> decrypter; |
84 }; | 164 }; |
85 | 165 |
86 // QuicCryptoConfig contains common configuration between clients and servers. | 166 // QuicCryptoConfig contains common configuration between clients and servers. |
87 class NET_EXPORT_PRIVATE QuicCryptoConfig { | 167 class NET_EXPORT_PRIVATE QuicCryptoConfig { |
88 public: | 168 public: |
89 QuicCryptoConfig(); | 169 QuicCryptoConfig(); |
90 ~QuicCryptoConfig(); | 170 ~QuicCryptoConfig(); |
91 | 171 |
92 // ProcessPeerHandshake performs common processing when receiving a peer's | 172 // ProcessPeerHandshake performs common processing when receiving a peer's |
93 // handshake message. | 173 // handshake message. |
94 bool ProcessPeerHandshake(const CryptoHandshakeMessage& peer_handshake, | 174 bool ProcessPeerHandshake(const CryptoHandshakeMessage& peer_handshake, |
95 CryptoUtils::Priority priority, | 175 CryptoUtils::Priority priority, |
96 QuicCryptoNegotiatedParams* out_params, | 176 QuicCryptoNegotiatedParams* out_params, |
97 std::string* error_details) const; | 177 std::string* error_details) const; |
98 | 178 |
99 // Protocol version | 179 // Protocol version |
100 uint16 version; | 180 uint16 version; |
101 // Key exchange methods. The following two members' values correspond by | 181 // Key exchange methods. The following two members' values correspond by |
102 // index. | 182 // index. |
103 CryptoTagVector kexs; | 183 CryptoTagVector kexs; |
104 std::vector<KeyExchange*> key_exchanges; | 184 std::vector<KeyExchange*> key_exchanges; |
105 // Authenticated encryption with associated data (AEAD) algorithms | 185 // Authenticated encryption with associated data (AEAD) algorithms. |
106 CryptoTagVector aead; | 186 CryptoTagVector aead; |
107 | 187 |
108 private: | 188 private: |
109 DISALLOW_COPY_AND_ASSIGN(QuicCryptoConfig); | 189 DISALLOW_COPY_AND_ASSIGN(QuicCryptoConfig); |
110 }; | 190 }; |
111 | 191 |
112 // QuicCryptoClientConfig contains crypto-related configuration settings for a | 192 // QuicCryptoClientConfig contains crypto-related configuration settings for a |
113 // client. | 193 // client. |
114 class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig { | 194 class NET_EXPORT_PRIVATE QuicCryptoClientConfig : public QuicCryptoConfig { |
115 public: | 195 public: |
| 196 QuicCryptoClientConfig(); |
| 197 |
116 // Sets the members to reasonable, default values. |rand| is used in order to | 198 // Sets the members to reasonable, default values. |rand| is used in order to |
117 // generate ephemeral ECDH keys. | 199 // generate ephemeral ECDH keys. |
118 void SetDefaults(QuicRandom* rand); | 200 void SetDefaults(QuicRandom* rand); |
119 | 201 |
120 // FillClientHello sets |out| to be a CHLO message based on the configuration | 202 // FillClientHello sets |out| to be a CHLO message based on the configuration |
121 // of this object. | 203 // of this object. |
122 void FillClientHello(const std::string& nonce, | 204 void FillClientHello(const std::string& nonce, |
123 const std::string& server_hostname, | 205 const std::string& server_hostname, |
124 CryptoHandshakeMessage* out); | 206 CryptoHandshakeMessage* out); |
125 | 207 |
126 // ProcessServerHello processes the tags in |server_hello|, writes the | 208 // ProcessServerHello processes the message in |server_hello|, writes the |
127 // negotiated parameters to |out_params| and returns QUIC_NO_ERROR. If | 209 // negotiated parameters to |out_params| and returns QUIC_NO_ERROR. If |
128 // |server_hello| is unacceptable then it puts an error message in | 210 // |server_hello| is unacceptable then it puts an error message in |
129 // |error_details| and returns an error code. | 211 // |error_details| and returns an error code. |
130 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, | 212 QuicErrorCode ProcessServerHello(const CryptoHandshakeMessage& server_hello, |
| 213 const std::string& nonce, |
131 QuicCryptoNegotiatedParams* out_params, | 214 QuicCryptoNegotiatedParams* out_params, |
132 std::string* error_details); | 215 std::string* error_details); |
| 216 |
| 217 // The |info| string for the HKDF function. |
| 218 std::string hkdf_info; |
133 }; | 219 }; |
134 | 220 |
135 // QuicCryptoServerConfig contains the crypto configuration of a QUIC server. | 221 // QuicCryptoServerConfig contains the crypto configuration of a QUIC server. |
136 // Unlike a client, a QUIC server can have multiple configurations active in | 222 // Unlike a client, a QUIC server can have multiple configurations active in |
137 // order to support clients resuming with a previous configuration. | 223 // order to support clients resuming with a previous configuration. |
138 // TODO(agl): when adding configurations at runtime is added, this object will | 224 // TODO(agl): when adding configurations at runtime is added, this object will |
139 // need to consider locking. | 225 // need to consider locking. |
140 class NET_EXPORT_PRIVATE QuicCryptoServerConfig { | 226 class NET_EXPORT_PRIVATE QuicCryptoServerConfig { |
141 public: | 227 public: |
142 QuicCryptoServerConfig(); | 228 QuicCryptoServerConfig(); |
(...skipping 21 matching lines...) Expand all Loading... |
164 // or reject the connection. If the connection is to be accepted, |out| is | 250 // or reject the connection. If the connection is to be accepted, |out| is |
165 // set to the contents of the ServerHello, |out_params| is completed and true | 251 // set to the contents of the ServerHello, |out_params| is completed and true |
166 // is returned. |nonce| is used as the server's nonce. Otherwise |out| is | 252 // is returned. |nonce| is used as the server's nonce. Otherwise |out| is |
167 // set to be a REJ message and false is returned. | 253 // set to be a REJ message and false is returned. |
168 bool ProcessClientHello(const CryptoHandshakeMessage& client_hello, | 254 bool ProcessClientHello(const CryptoHandshakeMessage& client_hello, |
169 const std::string& nonce, | 255 const std::string& nonce, |
170 CryptoHandshakeMessage* out, | 256 CryptoHandshakeMessage* out, |
171 QuicCryptoNegotiatedParams* out_params, | 257 QuicCryptoNegotiatedParams* out_params, |
172 std::string* error_details); | 258 std::string* error_details); |
173 | 259 |
| 260 // The |info| string for the HKDF function. |
| 261 std::string hkdf_info; |
| 262 |
174 private: | 263 private: |
175 // Config represents a server config: a collection of preferences and | 264 // Config represents a server config: a collection of preferences and |
176 // Diffie-Hellman public values. | 265 // Diffie-Hellman public values. |
177 struct Config : public QuicCryptoConfig { | 266 struct Config : public QuicCryptoConfig { |
178 Config(); | 267 Config(); |
179 ~Config(); | 268 ~Config(); |
180 | 269 |
181 // serialized contains the bytes of this server config, suitable for sending | 270 // serialized contains the bytes of this server config, suitable for sending |
182 // on the wire. | 271 // on the wire. |
183 std::string serialized; | 272 std::string serialized; |
184 | 273 |
185 // tag_value_map contains the raw key/value pairs for the config. | 274 // tag_value_map contains the raw key/value pairs for the config. |
186 CryptoTagValueMap tag_value_map; | 275 CryptoTagValueMap tag_value_map; |
187 }; | 276 }; |
188 | 277 |
189 std::map<ServerConfigID, Config*> configs_; | 278 std::map<ServerConfigID, Config*> configs_; |
190 | 279 |
191 std::string active_config_; | 280 std::string active_config_; |
192 }; | 281 }; |
193 | 282 |
194 } // namespace net | 283 } // namespace net |
195 | 284 |
196 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ | 285 #endif // NET_QUIC_CRYPTO_CRYPTO_HANDSHAKE_H_ |
OLD | NEW |