QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Some helpers for quic crypto |
| 6 | |
| 7 | #ifndef QUICHE_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |
| 8 | #define QUICHE_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |
| 9 | |
| 10 | #include <cstddef> |
| 11 | #include <cstdint> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 12 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 14 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 15 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h" |
| 16 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h" |
| 17 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 18 | #include "net/third_party/quiche/src/quic/core/crypto/quic_crypter.h" |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 19 | #include "net/third_party/quiche/src/quic/core/quic_connection_id.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 20 | #include "net/third_party/quiche/src/quic/core/quic_packets.h" |
| 21 | #include "net/third_party/quiche/src/quic/core/quic_time.h" |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 22 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | #include "net/third_party/quiche/src/quic/platform/api/quic_export.h" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 24 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | |
| 26 | namespace quic { |
| 27 | |
| 28 | class QuicRandom; |
| 29 | |
| 30 | class QUIC_EXPORT_PRIVATE CryptoUtils { |
| 31 | public: |
| 32 | CryptoUtils() = delete; |
| 33 | |
| 34 | // Diversification is a utility class that's used to act like a union type. |
| 35 | // Values can be created by calling the functions like |NoDiversification|, |
| 36 | // below. |
dschinazi | f25169a | 2019-10-23 08:12:18 -0700 | [diff] [blame] | 37 | class QUIC_EXPORT_PRIVATE Diversification { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 38 | public: |
| 39 | enum Mode { |
| 40 | NEVER, // Key diversification will never be used. Forward secure |
| 41 | // crypters will always use this mode. |
| 42 | |
| 43 | PENDING, // Key diversification will happen when a nonce is later |
| 44 | // received. This should only be used by clients initial |
| 45 | // decrypters which are waiting on the divesification nonce |
| 46 | // from the server. |
| 47 | |
| 48 | NOW, // Key diversification will happen immediate based on the nonce. |
| 49 | // This should only be used by servers initial encrypters. |
| 50 | }; |
| 51 | |
| 52 | Diversification(const Diversification& diversification) = default; |
| 53 | |
| 54 | static Diversification Never() { return Diversification(NEVER, nullptr); } |
| 55 | static Diversification Pending() { |
| 56 | return Diversification(PENDING, nullptr); |
| 57 | } |
| 58 | static Diversification Now(DiversificationNonce* nonce) { |
| 59 | return Diversification(NOW, nonce); |
| 60 | } |
| 61 | |
| 62 | Mode mode() const { return mode_; } |
| 63 | DiversificationNonce* nonce() const { |
| 64 | DCHECK_EQ(mode_, NOW); |
| 65 | return nonce_; |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | Diversification(Mode mode, DiversificationNonce* nonce) |
| 70 | : mode_(mode), nonce_(nonce) {} |
| 71 | |
| 72 | Mode mode_; |
| 73 | DiversificationNonce* nonce_; |
| 74 | }; |
| 75 | |
| 76 | // SetKeyAndIV derives the key and IV from the given packet protection secret |
| 77 | // |pp_secret| and sets those fields on the given QuicCrypter |*crypter|. |
| 78 | // This follows the derivation described in section 7.3 of RFC 8446, except |
| 79 | // with the label prefix in HKDF-Expand-Label changed from "tls13 " to "quic " |
| 80 | // as described in draft-ietf-quic-tls-14, section 5.1. |
| 81 | static void SetKeyAndIV(const EVP_MD* prf, |
| 82 | const std::vector<uint8_t>& pp_secret, |
| 83 | QuicCrypter* crypter); |
| 84 | |
nharper | c8d9e40 | 2019-09-12 18:30:14 -0700 | [diff] [blame] | 85 | // IETF QUIC encrypts ENCRYPTION_INITIAL messages with a version-specific key |
| 86 | // (to prevent network observers that are not aware of that QUIC version from |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 87 | // making decisions based on the TLS handshake). This packet protection secret |
| 88 | // is derived from the connection ID in the client's Initial packet. |
| 89 | // |
| 90 | // This function takes that |connection_id| and creates the encrypter and |
| 91 | // decrypter (put in |*crypters|) to use for this packet protection, as well |
nharper | c8d9e40 | 2019-09-12 18:30:14 -0700 | [diff] [blame] | 92 | // as setting the key and IV on those crypters. For older versions of QUIC |
| 93 | // that do not use the new IETF style ENCRYPTION_INITIAL obfuscators, this |
| 94 | // function puts a NullEncrypter and NullDecrypter in |*crypters|. |
| 95 | static void CreateInitialObfuscators(Perspective perspective, |
| 96 | ParsedQuicVersion version, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 97 | QuicConnectionId connection_id, |
| 98 | CrypterPair* crypters); |
| 99 | |
dschinazi | 278efae | 2020-01-28 17:03:09 -0800 | [diff] [blame] | 100 | // IETF QUIC Retry packets carry a retry integrity tag to detect packet |
| 101 | // corruption and make it harder for an attacker to spoof. This function |
| 102 | // checks whether a given retry packet is valid. |
| 103 | static bool ValidateRetryIntegrityTag( |
| 104 | ParsedQuicVersion version, |
| 105 | QuicConnectionId original_connection_id, |
| 106 | quiche::QuicheStringPiece retry_without_tag, |
| 107 | quiche::QuicheStringPiece integrity_tag); |
| 108 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 109 | // Generates the connection nonce. The nonce is formed as: |
| 110 | // <4 bytes> current time |
| 111 | // <8 bytes> |orbit| (or random if |orbit| is empty) |
| 112 | // <20 bytes> random |
| 113 | static void GenerateNonce(QuicWallTime now, |
| 114 | QuicRandom* random_generator, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 115 | quiche::QuicheStringPiece orbit, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 116 | std::string* nonce); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | |
| 118 | // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and |
| 119 | // |subkey_secret| (optional -- may be null) given the contents of |
| 120 | // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead| |
| 121 | // determines which cipher will be used. |perspective| controls whether the |
| 122 | // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is |
| 123 | // optional and, if non-empty, is mixed into the key derivation. |
| 124 | // |subkey_secret| will have the same length as |premaster_secret|. |
| 125 | // |
| 126 | // If |pre_shared_key| is non-empty, it is incorporated into the key |
| 127 | // derivation parameters. If it is empty, the key derivation is unaltered. |
| 128 | // |
| 129 | // If the mode of |diversification| is NEVER, the the crypters will be |
| 130 | // configured to never perform key diversification. If the mode is |
| 131 | // NOW (which is only for servers, then the encrypter will be keyed via a |
| 132 | // two-step process that uses the nonce from |diversification|. |
| 133 | // If the mode is PENDING (which is only for servres), then the |
| 134 | // decrypter will only be keyed to a preliminary state: a call to |
| 135 | // |SetDiversificationNonce| with a diversification nonce will be needed to |
| 136 | // complete keying. |
nharper | c1bbfe6 | 2019-09-27 16:48:40 -0700 | [diff] [blame] | 137 | static bool DeriveKeys(const ParsedQuicVersion& version, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 138 | quiche::QuicheStringPiece premaster_secret, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 139 | QuicTag aead, |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 140 | quiche::QuicheStringPiece client_nonce, |
| 141 | quiche::QuicheStringPiece server_nonce, |
| 142 | quiche::QuicheStringPiece pre_shared_key, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 143 | const std::string& hkdf_input, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | Perspective perspective, |
| 145 | Diversification diversification, |
| 146 | CrypterPair* crypters, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 147 | std::string* subkey_secret); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 148 | |
| 149 | // Performs key extraction to derive a new secret of |result_len| bytes |
| 150 | // dependent on |subkey_secret|, |label|, and |context|. Returns false if the |
| 151 | // parameters are invalid (e.g. |label| contains null bytes); returns true on |
| 152 | // success. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 153 | static bool ExportKeyingMaterial(quiche::QuicheStringPiece subkey_secret, |
| 154 | quiche::QuicheStringPiece label, |
| 155 | quiche::QuicheStringPiece context, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 156 | size_t result_len, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 157 | std::string* result); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 158 | |
| 159 | // Computes the FNV-1a hash of the provided DER-encoded cert for use in the |
| 160 | // XLCT tag. |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame] | 161 | static uint64_t ComputeLeafCertHash(quiche::QuicheStringPiece cert); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 162 | |
| 163 | // Validates that |server_hello| is actually an SHLO message and that it is |
| 164 | // not part of a downgrade attack. |
| 165 | // |
| 166 | // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error |
| 167 | // code and sets |error_details|. |
| 168 | static QuicErrorCode ValidateServerHello( |
| 169 | const CryptoHandshakeMessage& server_hello, |
| 170 | const ParsedQuicVersionVector& negotiated_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 171 | std::string* error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 172 | |
| 173 | // Validates that the |server_versions| received do not indicate that the |
| 174 | // ServerHello is part of a downgrade attack. |negotiated_versions| must |
| 175 | // contain the list of versions received in the server's version negotiation |
| 176 | // packet (or be empty if no such packet was received). |
| 177 | // |
| 178 | // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error |
| 179 | // code and sets |error_details|. |
| 180 | static QuicErrorCode ValidateServerHelloVersions( |
| 181 | const QuicVersionLabelVector& server_versions, |
| 182 | const ParsedQuicVersionVector& negotiated_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 183 | std::string* error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 184 | |
| 185 | // Validates that |client_hello| is actually a CHLO and that this is not part |
| 186 | // of a downgrade attack. |
| 187 | // This includes verifiying versions and detecting downgrade attacks. |
| 188 | // |
| 189 | // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error |
| 190 | // code and sets |error_details|. |
| 191 | static QuicErrorCode ValidateClientHello( |
| 192 | const CryptoHandshakeMessage& client_hello, |
| 193 | ParsedQuicVersion version, |
| 194 | const ParsedQuicVersionVector& supported_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 195 | std::string* error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 196 | |
| 197 | // Validates that the |client_version| received does not indicate that a |
| 198 | // downgrade attack has occurred. |connection_version| is the version of the |
| 199 | // QuicConnection, and |supported_versions| is all versions that that |
| 200 | // QuicConnection supports. |
| 201 | // |
| 202 | // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error |
| 203 | // code and sets |error_details|. |
| 204 | static QuicErrorCode ValidateClientHelloVersion( |
| 205 | QuicVersionLabel client_version, |
| 206 | ParsedQuicVersion connection_version, |
| 207 | const ParsedQuicVersionVector& supported_versions, |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 208 | std::string* error_details); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 209 | |
| 210 | // Returns the name of the HandshakeFailureReason as a char* |
| 211 | static const char* HandshakeFailureReasonToString( |
| 212 | HandshakeFailureReason reason); |
| 213 | |
QUICHE team | 84910bd | 2019-03-15 07:03:40 -0700 | [diff] [blame] | 214 | // Returns a hash of the serialized |message|. |
rch | d5d13c2 | 2019-03-18 14:31:09 -0700 | [diff] [blame] | 215 | static std::string HashHandshakeMessage(const CryptoHandshakeMessage& message, |
| 216 | Perspective perspective); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | } // namespace quic |
| 220 | |
| 221 | #endif // QUICHE_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_ |