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 // Some helpers for quic crypto | 5 // Some helpers for quic crypto |
6 | 6 |
7 #ifndef NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ | 7 #ifndef NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ |
8 #define NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ | 8 #define NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ |
9 | 9 |
10 #include <stddef.h> | 10 #include <stddef.h> |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "net/quic/quic_time.h" | 22 #include "net/quic/quic_time.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
26 class QuicTime; | 26 class QuicTime; |
27 class QuicRandom; | 27 class QuicRandom; |
28 struct QuicCryptoNegotiatedParameters; | 28 struct QuicCryptoNegotiatedParameters; |
29 | 29 |
30 class NET_EXPORT_PRIVATE CryptoUtils { | 30 class NET_EXPORT_PRIVATE CryptoUtils { |
31 public: | 31 public: |
| 32 // Diversification is a utility class that's used to act like a union type. |
| 33 // Values can be created by calling the functions like |NoDiversification|, |
| 34 // below. |
| 35 class Diversification { |
| 36 public: |
| 37 enum Mode { |
| 38 NEVER, // Key diversification will never be used. Forward secure |
| 39 // crypters will always use this mode. |
| 40 |
| 41 PENDING, // Key diversification will happen when a nonce is later |
| 42 // received. This should only be used by clients initial |
| 43 // decrypters which are waiting on the divesification nonce |
| 44 // from the server. |
| 45 |
| 46 NOW, // Key diversification will happen immediate based on the nonce. |
| 47 // This should only be used by servers initial encrypters. |
| 48 }; |
| 49 |
| 50 Diversification(const Diversification& diversification) = default; |
| 51 |
| 52 static Diversification Never() { return Diversification(NEVER, nullptr); } |
| 53 static Diversification Pending() { |
| 54 return Diversification(PENDING, nullptr); |
| 55 } |
| 56 static Diversification Now(DiversificationNonce* nonce) { |
| 57 return Diversification(NOW, nonce); |
| 58 } |
| 59 |
| 60 Mode mode() const { return mode_; } |
| 61 DiversificationNonce* nonce() const { |
| 62 DCHECK_EQ(mode_, NOW); |
| 63 return nonce_; |
| 64 } |
| 65 |
| 66 private: |
| 67 Diversification(Mode mode, DiversificationNonce* nonce) |
| 68 : mode_(mode), nonce_(nonce) {} |
| 69 |
| 70 Mode mode_; |
| 71 DiversificationNonce* nonce_; |
| 72 }; |
| 73 |
32 // Generates the connection nonce. The nonce is formed as: | 74 // Generates the connection nonce. The nonce is formed as: |
33 // <4 bytes> current time | 75 // <4 bytes> current time |
34 // <8 bytes> |orbit| (or random if |orbit| is empty) | 76 // <8 bytes> |orbit| (or random if |orbit| is empty) |
35 // <20 bytes> random | 77 // <20 bytes> random |
36 static void GenerateNonce(QuicWallTime now, | 78 static void GenerateNonce(QuicWallTime now, |
37 QuicRandom* random_generator, | 79 QuicRandom* random_generator, |
38 base::StringPiece orbit, | 80 base::StringPiece orbit, |
39 std::string* nonce); | 81 std::string* nonce); |
40 | 82 |
41 // Returns true if the sni is valid, false otherwise. | 83 // Returns true if the sni is valid, false otherwise. |
42 // (1) disallow IP addresses; | 84 // (1) disallow IP addresses; |
43 // (2) check that the hostname contains valid characters only; and | 85 // (2) check that the hostname contains valid characters only; and |
44 // (3) contains at least one dot. | 86 // (3) contains at least one dot. |
45 static bool IsValidSNI(base::StringPiece sni); | 87 static bool IsValidSNI(base::StringPiece sni); |
46 | 88 |
47 // Convert hostname to lowercase and remove the trailing '.'. | 89 // Convert hostname to lowercase and remove the trailing '.'. |
48 // Returns |hostname|. NormalizeHostname() doesn't support IP address | 90 // Returns |hostname|. NormalizeHostname() doesn't support IP address |
49 // literals. IsValidSNI() should be called before calling NormalizeHostname(). | 91 // literals. IsValidSNI() should be called before calling NormalizeHostname(). |
50 static std::string NormalizeHostname(const char* hostname); | 92 static std::string NormalizeHostname(const char* hostname); |
51 | 93 |
52 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and | 94 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and |
53 // |subkey_secret| (optional -- may be null) given the contents of | 95 // |subkey_secret| (optional -- may be null) given the contents of |
54 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead| | 96 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead| |
55 // determines which cipher will be used. |perspective| controls whether the | 97 // determines which cipher will be used. |perspective| controls whether the |
56 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is | 98 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is |
57 // optional and, if non-empty, is mixed into the key derivation. | 99 // optional and, if non-empty, is mixed into the key derivation. |
58 // |subkey_secret| will have the same length as |premaster_secret|. | 100 // |subkey_secret| will have the same length as |premaster_secret|. |
| 101 // |
| 102 // If the mode of |diversification| is NEVER, the the crypters will be |
| 103 // configured to never perform key diversification. If the mode is |
| 104 // NOW (which is only for servers, then the encrypter will be keyed via a |
| 105 // two-step process that uses the nonce from |diversification|. |
| 106 // If the mode is PENDING (which is only for servres), then the |
| 107 // decrypter will only be keyed to a preliminary state: a call to |
| 108 // |SetDiversificationNonce| with a diversification nonce will be needed to |
| 109 // complete keying. |
59 static bool DeriveKeys(base::StringPiece premaster_secret, | 110 static bool DeriveKeys(base::StringPiece premaster_secret, |
60 QuicTag aead, | 111 QuicTag aead, |
61 base::StringPiece client_nonce, | 112 base::StringPiece client_nonce, |
62 base::StringPiece server_nonce, | 113 base::StringPiece server_nonce, |
63 const std::string& hkdf_input, | 114 const std::string& hkdf_input, |
64 Perspective perspective, | 115 Perspective perspective, |
| 116 Diversification diversification, |
65 CrypterPair* crypters, | 117 CrypterPair* crypters, |
66 std::string* subkey_secret); | 118 std::string* subkey_secret); |
67 | 119 |
68 // Performs key extraction to derive a new secret of |result_len| bytes | 120 // Performs key extraction to derive a new secret of |result_len| bytes |
69 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the | 121 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the |
70 // parameters are invalid (e.g. |label| contains null bytes); returns true on | 122 // parameters are invalid (e.g. |label| contains null bytes); returns true on |
71 // success. | 123 // success. |
72 static bool ExportKeyingMaterial(base::StringPiece subkey_secret, | 124 static bool ExportKeyingMaterial(base::StringPiece subkey_secret, |
73 base::StringPiece label, | 125 base::StringPiece label, |
74 base::StringPiece context, | 126 base::StringPiece context, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 static void HashHandshakeMessage(const CryptoHandshakeMessage& message, | 161 static void HashHandshakeMessage(const CryptoHandshakeMessage& message, |
110 std::string* output); | 162 std::string* output); |
111 | 163 |
112 private: | 164 private: |
113 DISALLOW_COPY_AND_ASSIGN(CryptoUtils); | 165 DISALLOW_COPY_AND_ASSIGN(CryptoUtils); |
114 }; | 166 }; |
115 | 167 |
116 } // namespace net | 168 } // namespace net |
117 | 169 |
118 #endif // NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ | 170 #endif // NET_QUIC_CRYPTO_CRYPTO_UTILS_H_ |
OLD | NEW |