blob: 6c167f8f6947c789dfa35b1174df60414214983f [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
vasilvv872e7a32019-03-12 16:42:44 -070012#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050013
QUICHE teama6ef0a62019-03-07 20:34:33 -050014#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"
dschinazi278efae2020-01-28 17:03:09 -080019#include "net/third_party/quiche/src/quic/core/quic_connection_id.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050020#include "net/third_party/quiche/src/quic/core/quic_packets.h"
21#include "net/third_party/quiche/src/quic/core/quic_time.h"
dschinazi278efae2020-01-28 17:03:09 -080022#include "net/third_party/quiche/src/quic/core/quic_versions.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050023#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
dmcardle904ef182019-12-13 08:34:33 -080024#include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050025
26namespace quic {
27
28class QuicRandom;
29
30class 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.
dschinazif25169a2019-10-23 08:12:18 -070037 class QUIC_EXPORT_PRIVATE Diversification {
QUICHE teama6ef0a62019-03-07 20:34:33 -050038 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
nharperc8d9e402019-09-12 18:30:14 -070085 // 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 teama6ef0a62019-03-07 20:34:33 -050087 // 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
nharperc8d9e402019-09-12 18:30:14 -070092 // 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 teama6ef0a62019-03-07 20:34:33 -050097 QuicConnectionId connection_id,
98 CrypterPair* crypters);
99
dschinazi278efae2020-01-28 17:03:09 -0800100 // 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 teama6ef0a62019-03-07 20:34:33 -0500109 // 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,
dmcardle904ef182019-12-13 08:34:33 -0800115 quiche::QuicheStringPiece orbit,
vasilvvc48c8712019-03-11 13:38:16 -0700116 std::string* nonce);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500117
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.
nharperc1bbfe62019-09-27 16:48:40 -0700137 static bool DeriveKeys(const ParsedQuicVersion& version,
dmcardle904ef182019-12-13 08:34:33 -0800138 quiche::QuicheStringPiece premaster_secret,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500139 QuicTag aead,
dmcardle904ef182019-12-13 08:34:33 -0800140 quiche::QuicheStringPiece client_nonce,
141 quiche::QuicheStringPiece server_nonce,
142 quiche::QuicheStringPiece pre_shared_key,
vasilvvc48c8712019-03-11 13:38:16 -0700143 const std::string& hkdf_input,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500144 Perspective perspective,
145 Diversification diversification,
146 CrypterPair* crypters,
vasilvvc48c8712019-03-11 13:38:16 -0700147 std::string* subkey_secret);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500148
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.
dmcardle904ef182019-12-13 08:34:33 -0800153 static bool ExportKeyingMaterial(quiche::QuicheStringPiece subkey_secret,
154 quiche::QuicheStringPiece label,
155 quiche::QuicheStringPiece context,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500156 size_t result_len,
vasilvvc48c8712019-03-11 13:38:16 -0700157 std::string* result);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500158
159 // Computes the FNV-1a hash of the provided DER-encoded cert for use in the
160 // XLCT tag.
dmcardle904ef182019-12-13 08:34:33 -0800161 static uint64_t ComputeLeafCertHash(quiche::QuicheStringPiece cert);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500162
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,
vasilvvc48c8712019-03-11 13:38:16 -0700171 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500172
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,
vasilvvc48c8712019-03-11 13:38:16 -0700183 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500184
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,
vasilvvc48c8712019-03-11 13:38:16 -0700195 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500196
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,
vasilvvc48c8712019-03-11 13:38:16 -0700208 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500209
210 // Returns the name of the HandshakeFailureReason as a char*
211 static const char* HandshakeFailureReasonToString(
212 HandshakeFailureReason reason);
213
QUICHE team84910bd2019-03-15 07:03:40 -0700214 // Returns a hash of the serialized |message|.
rchd5d13c22019-03-18 14:31:09 -0700215 static std::string HashHandshakeMessage(const CryptoHandshakeMessage& message,
216 Perspective perspective);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500217};
218
219} // namespace quic
220
221#endif // QUICHE_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_