| 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_CORE_CRYPTO_CRYPTO_UTILS_H_ | 7 #ifndef NET_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |
| 8 #define NET_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ | 8 #define NET_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |
| 9 | 9 |
| 10 #include <cstddef> | 10 #include <cstddef> |
| 11 #include <cstdint> | 11 #include <cstdint> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/strings/string_piece.h" | |
| 16 #include "net/quic/core/crypto/crypto_handshake.h" | 15 #include "net/quic/core/crypto/crypto_handshake.h" |
| 17 #include "net/quic/core/crypto/crypto_handshake_message.h" | 16 #include "net/quic/core/crypto/crypto_handshake_message.h" |
| 18 #include "net/quic/core/crypto/crypto_protocol.h" | 17 #include "net/quic/core/crypto/crypto_protocol.h" |
| 19 #include "net/quic/core/quic_packets.h" | 18 #include "net/quic/core/quic_packets.h" |
| 20 #include "net/quic/core/quic_time.h" | 19 #include "net/quic/core/quic_time.h" |
| 21 #include "net/quic/platform/api/quic_export.h" | 20 #include "net/quic/platform/api/quic_export.h" |
| 21 #include "net/quic/platform/api/quic_string_piece.h" |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 class QuicRandom; | 25 class QuicRandom; |
| 26 | 26 |
| 27 class QUIC_EXPORT_PRIVATE CryptoUtils { | 27 class QUIC_EXPORT_PRIVATE CryptoUtils { |
| 28 public: | 28 public: |
| 29 // Diversification is a utility class that's used to act like a union type. | 29 // Diversification is a utility class that's used to act like a union type. |
| 30 // Values can be created by calling the functions like |NoDiversification|, | 30 // Values can be created by calling the functions like |NoDiversification|, |
| 31 // below. | 31 // below. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 Mode mode_; | 67 Mode mode_; |
| 68 DiversificationNonce* nonce_; | 68 DiversificationNonce* nonce_; |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 // Generates the connection nonce. The nonce is formed as: | 71 // Generates the connection nonce. The nonce is formed as: |
| 72 // <4 bytes> current time | 72 // <4 bytes> current time |
| 73 // <8 bytes> |orbit| (or random if |orbit| is empty) | 73 // <8 bytes> |orbit| (or random if |orbit| is empty) |
| 74 // <20 bytes> random | 74 // <20 bytes> random |
| 75 static void GenerateNonce(QuicWallTime now, | 75 static void GenerateNonce(QuicWallTime now, |
| 76 QuicRandom* random_generator, | 76 QuicRandom* random_generator, |
| 77 base::StringPiece orbit, | 77 QuicStringPiece orbit, |
| 78 std::string* nonce); | 78 std::string* nonce); |
| 79 | 79 |
| 80 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and | 80 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and |
| 81 // |subkey_secret| (optional -- may be null) given the contents of | 81 // |subkey_secret| (optional -- may be null) given the contents of |
| 82 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead| | 82 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead| |
| 83 // determines which cipher will be used. |perspective| controls whether the | 83 // determines which cipher will be used. |perspective| controls whether the |
| 84 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is | 84 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is |
| 85 // optional and, if non-empty, is mixed into the key derivation. | 85 // optional and, if non-empty, is mixed into the key derivation. |
| 86 // |subkey_secret| will have the same length as |premaster_secret|. | 86 // |subkey_secret| will have the same length as |premaster_secret|. |
| 87 // | 87 // |
| 88 // If the mode of |diversification| is NEVER, the the crypters will be | 88 // If the mode of |diversification| is NEVER, the the crypters will be |
| 89 // configured to never perform key diversification. If the mode is | 89 // configured to never perform key diversification. If the mode is |
| 90 // NOW (which is only for servers, then the encrypter will be keyed via a | 90 // NOW (which is only for servers, then the encrypter will be keyed via a |
| 91 // two-step process that uses the nonce from |diversification|. | 91 // two-step process that uses the nonce from |diversification|. |
| 92 // If the mode is PENDING (which is only for servres), then the | 92 // If the mode is PENDING (which is only for servres), then the |
| 93 // decrypter will only be keyed to a preliminary state: a call to | 93 // decrypter will only be keyed to a preliminary state: a call to |
| 94 // |SetDiversificationNonce| with a diversification nonce will be needed to | 94 // |SetDiversificationNonce| with a diversification nonce will be needed to |
| 95 // complete keying. | 95 // complete keying. |
| 96 static bool DeriveKeys(base::StringPiece premaster_secret, | 96 static bool DeriveKeys(QuicStringPiece premaster_secret, |
| 97 QuicTag aead, | 97 QuicTag aead, |
| 98 base::StringPiece client_nonce, | 98 QuicStringPiece client_nonce, |
| 99 base::StringPiece server_nonce, | 99 QuicStringPiece server_nonce, |
| 100 const std::string& hkdf_input, | 100 const std::string& hkdf_input, |
| 101 Perspective perspective, | 101 Perspective perspective, |
| 102 Diversification diversification, | 102 Diversification diversification, |
| 103 CrypterPair* crypters, | 103 CrypterPair* crypters, |
| 104 std::string* subkey_secret); | 104 std::string* subkey_secret); |
| 105 | 105 |
| 106 // Performs key extraction to derive a new secret of |result_len| bytes | 106 // Performs key extraction to derive a new secret of |result_len| bytes |
| 107 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the | 107 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the |
| 108 // parameters are invalid (e.g. |label| contains null bytes); returns true on | 108 // parameters are invalid (e.g. |label| contains null bytes); returns true on |
| 109 // success. | 109 // success. |
| 110 static bool ExportKeyingMaterial(base::StringPiece subkey_secret, | 110 static bool ExportKeyingMaterial(QuicStringPiece subkey_secret, |
| 111 base::StringPiece label, | 111 QuicStringPiece label, |
| 112 base::StringPiece context, | 112 QuicStringPiece context, |
| 113 size_t result_len, | 113 size_t result_len, |
| 114 std::string* result); | 114 std::string* result); |
| 115 | 115 |
| 116 // Computes the FNV-1a hash of the provided DER-encoded cert for use in the | 116 // Computes the FNV-1a hash of the provided DER-encoded cert for use in the |
| 117 // XLCT tag. | 117 // XLCT tag. |
| 118 static uint64_t ComputeLeafCertHash(base::StringPiece cert); | 118 static uint64_t ComputeLeafCertHash(QuicStringPiece cert); |
| 119 | 119 |
| 120 // Validates that |server_hello| is actually an SHLO message and that it is | 120 // Validates that |server_hello| is actually an SHLO message and that it is |
| 121 // not part of a downgrade attack. | 121 // not part of a downgrade attack. |
| 122 // | 122 // |
| 123 // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error | 123 // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error |
| 124 // code and sets |error_details|. | 124 // code and sets |error_details|. |
| 125 static QuicErrorCode ValidateServerHello( | 125 static QuicErrorCode ValidateServerHello( |
| 126 const CryptoHandshakeMessage& server_hello, | 126 const CryptoHandshakeMessage& server_hello, |
| 127 const QuicVersionVector& negotiated_versions, | 127 const QuicVersionVector& negotiated_versions, |
| 128 std::string* error_details); | 128 std::string* error_details); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 147 static void HashHandshakeMessage(const CryptoHandshakeMessage& message, | 147 static void HashHandshakeMessage(const CryptoHandshakeMessage& message, |
| 148 std::string* output); | 148 std::string* output); |
| 149 | 149 |
| 150 private: | 150 private: |
| 151 DISALLOW_COPY_AND_ASSIGN(CryptoUtils); | 151 DISALLOW_COPY_AND_ASSIGN(CryptoUtils); |
| 152 }; | 152 }; |
| 153 | 153 |
| 154 } // namespace net | 154 } // namespace net |
| 155 | 155 |
| 156 #endif // NET_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ | 156 #endif // NET_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |
| OLD | NEW |