blob: da186a85e7a56cc974e78c5ce6ddf7dccf46945e [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
14#include "base/macros.h"
15#include "third_party/boringssl/src/include/openssl/evp.h"
16#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake.h"
17#include "net/third_party/quiche/src/quic/core/crypto/crypto_handshake_message.h"
18#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
19#include "net/third_party/quiche/src/quic/core/crypto/quic_crypter.h"
20#include "net/third_party/quiche/src/quic/core/quic_packets.h"
21#include "net/third_party/quiche/src/quic/core/quic_time.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050023#include "net/third_party/quiche/src/quic/platform/api/quic_string_piece.h"
24
25namespace quic {
26
27class QuicRandom;
28
29class QUIC_EXPORT_PRIVATE CryptoUtils {
30 public:
31 CryptoUtils() = delete;
32
33 // Diversification is a utility class that's used to act like a union type.
34 // Values can be created by calling the functions like |NoDiversification|,
35 // below.
36 class Diversification {
37 public:
38 enum Mode {
39 NEVER, // Key diversification will never be used. Forward secure
40 // crypters will always use this mode.
41
42 PENDING, // Key diversification will happen when a nonce is later
43 // received. This should only be used by clients initial
44 // decrypters which are waiting on the divesification nonce
45 // from the server.
46
47 NOW, // Key diversification will happen immediate based on the nonce.
48 // This should only be used by servers initial encrypters.
49 };
50
51 Diversification(const Diversification& diversification) = default;
52
53 static Diversification Never() { return Diversification(NEVER, nullptr); }
54 static Diversification Pending() {
55 return Diversification(PENDING, nullptr);
56 }
57 static Diversification Now(DiversificationNonce* nonce) {
58 return Diversification(NOW, nonce);
59 }
60
61 Mode mode() const { return mode_; }
62 DiversificationNonce* nonce() const {
63 DCHECK_EQ(mode_, NOW);
64 return nonce_;
65 }
66
67 private:
68 Diversification(Mode mode, DiversificationNonce* nonce)
69 : mode_(mode), nonce_(nonce) {}
70
71 Mode mode_;
72 DiversificationNonce* nonce_;
73 };
74
75 // SetKeyAndIV derives the key and IV from the given packet protection secret
76 // |pp_secret| and sets those fields on the given QuicCrypter |*crypter|.
77 // This follows the derivation described in section 7.3 of RFC 8446, except
78 // with the label prefix in HKDF-Expand-Label changed from "tls13 " to "quic "
79 // as described in draft-ietf-quic-tls-14, section 5.1.
80 static void SetKeyAndIV(const EVP_MD* prf,
81 const std::vector<uint8_t>& pp_secret,
82 QuicCrypter* crypter);
83
84 // QUIC encrypts TLS handshake messages with a version-specific key (to
85 // prevent network observers that are not aware of that QUIC version from
86 // making decisions based on the TLS handshake). This packet protection secret
87 // is derived from the connection ID in the client's Initial packet.
88 //
89 // This function takes that |connection_id| and creates the encrypter and
90 // decrypter (put in |*crypters|) to use for this packet protection, as well
91 // as setting the key and IV on those crypters.
92 static void CreateTlsInitialCrypters(Perspective perspective,
93 QuicTransportVersion version,
94 QuicConnectionId connection_id,
95 CrypterPair* crypters);
96
97 // Generates the connection nonce. The nonce is formed as:
98 // <4 bytes> current time
99 // <8 bytes> |orbit| (or random if |orbit| is empty)
100 // <20 bytes> random
101 static void GenerateNonce(QuicWallTime now,
102 QuicRandom* random_generator,
103 QuicStringPiece orbit,
vasilvvc48c8712019-03-11 13:38:16 -0700104 std::string* nonce);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500105
106 // DeriveKeys populates |crypters->encrypter|, |crypters->decrypter|, and
107 // |subkey_secret| (optional -- may be null) given the contents of
108 // |premaster_secret|, |client_nonce|, |server_nonce| and |hkdf_input|. |aead|
109 // determines which cipher will be used. |perspective| controls whether the
110 // server's keys are assigned to |encrypter| or |decrypter|. |server_nonce| is
111 // optional and, if non-empty, is mixed into the key derivation.
112 // |subkey_secret| will have the same length as |premaster_secret|.
113 //
114 // If |pre_shared_key| is non-empty, it is incorporated into the key
115 // derivation parameters. If it is empty, the key derivation is unaltered.
116 //
117 // If the mode of |diversification| is NEVER, the the crypters will be
118 // configured to never perform key diversification. If the mode is
119 // NOW (which is only for servers, then the encrypter will be keyed via a
120 // two-step process that uses the nonce from |diversification|.
121 // If the mode is PENDING (which is only for servres), then the
122 // decrypter will only be keyed to a preliminary state: a call to
123 // |SetDiversificationNonce| with a diversification nonce will be needed to
124 // complete keying.
125 static bool DeriveKeys(QuicStringPiece premaster_secret,
126 QuicTag aead,
127 QuicStringPiece client_nonce,
128 QuicStringPiece server_nonce,
129 QuicStringPiece pre_shared_key,
vasilvvc48c8712019-03-11 13:38:16 -0700130 const std::string& hkdf_input,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500131 Perspective perspective,
132 Diversification diversification,
133 CrypterPair* crypters,
vasilvvc48c8712019-03-11 13:38:16 -0700134 std::string* subkey_secret);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500135
136 // Performs key extraction to derive a new secret of |result_len| bytes
137 // dependent on |subkey_secret|, |label|, and |context|. Returns false if the
138 // parameters are invalid (e.g. |label| contains null bytes); returns true on
139 // success.
140 static bool ExportKeyingMaterial(QuicStringPiece subkey_secret,
141 QuicStringPiece label,
142 QuicStringPiece context,
143 size_t result_len,
vasilvvc48c8712019-03-11 13:38:16 -0700144 std::string* result);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500145
146 // Computes the FNV-1a hash of the provided DER-encoded cert for use in the
147 // XLCT tag.
148 static uint64_t ComputeLeafCertHash(QuicStringPiece cert);
149
150 // Validates that |server_hello| is actually an SHLO message and that it is
151 // not part of a downgrade attack.
152 //
153 // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error
154 // code and sets |error_details|.
155 static QuicErrorCode ValidateServerHello(
156 const CryptoHandshakeMessage& server_hello,
157 const ParsedQuicVersionVector& negotiated_versions,
vasilvvc48c8712019-03-11 13:38:16 -0700158 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500159
160 // Validates that the |server_versions| received do not indicate that the
161 // ServerHello is part of a downgrade attack. |negotiated_versions| must
162 // contain the list of versions received in the server's version negotiation
163 // packet (or be empty if no such packet was received).
164 //
165 // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error
166 // code and sets |error_details|.
167 static QuicErrorCode ValidateServerHelloVersions(
168 const QuicVersionLabelVector& server_versions,
169 const ParsedQuicVersionVector& negotiated_versions,
vasilvvc48c8712019-03-11 13:38:16 -0700170 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500171
172 // Validates that |client_hello| is actually a CHLO and that this is not part
173 // of a downgrade attack.
174 // This includes verifiying versions and detecting downgrade attacks.
175 //
176 // Returns QUIC_NO_ERROR if this is the case or returns the appropriate error
177 // code and sets |error_details|.
178 static QuicErrorCode ValidateClientHello(
179 const CryptoHandshakeMessage& client_hello,
180 ParsedQuicVersion version,
181 const ParsedQuicVersionVector& supported_versions,
vasilvvc48c8712019-03-11 13:38:16 -0700182 std::string* error_details);
QUICHE teama6ef0a62019-03-07 20:34:33 -0500183
184 // Validates that the |client_version| received does not indicate that a
185 // downgrade attack has occurred. |connection_version| is the version of the
186 // QuicConnection, and |supported_versions| is all versions that that
187 // QuicConnection supports.
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 ValidateClientHelloVersion(
192 QuicVersionLabel client_version,
193 ParsedQuicVersion connection_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 // Returns the name of the HandshakeFailureReason as a char*
198 static const char* HandshakeFailureReasonToString(
199 HandshakeFailureReason reason);
200
201 // Writes a hash of the serialized |message| into |output|.
202 static void HashHandshakeMessage(const CryptoHandshakeMessage& message,
vasilvvc48c8712019-03-11 13:38:16 -0700203 std::string* output,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500204 Perspective perspective);
205
206 private:
207 // Implements the HKDF-Expand-Label function as defined in section 7.1 of RFC
208 // 8446, except that it uses "quic " as the prefix instead of "tls13 ", as
209 // specified by draft-ietf-quic-tls-14. The HKDF-Expand-Label function takes 4
210 // explicit arguments (Secret, Label, Context, and Length), as well as
211 // implicit PRF which is the hash function negotiated by TLS. Its use in QUIC
212 // (as needed by the QUIC stack, instead of as used internally by the TLS
213 // stack) is only for deriving initial secrets for obfuscation and for
214 // calculating packet protection keys and IVs from the corresponding packet
215 // protection secret. Neither of these uses need a Context (a zero-length
216 // context is provided), so this argument is omitted here.
217 //
218 // The implicit PRF is explicitly passed into HkdfExpandLabel as |prf|; the
219 // Secret, Label, and Length are passed in as |secret|, |label|, and
220 // |out_len|, respectively. The resulting expanded secret is returned.
221 static std::vector<uint8_t> HkdfExpandLabel(
222 const EVP_MD* prf,
223 const std::vector<uint8_t>& secret,
vasilvvc48c8712019-03-11 13:38:16 -0700224 const std::string& label,
QUICHE teama6ef0a62019-03-07 20:34:33 -0500225 size_t out_len);
226};
227
228} // namespace quic
229
230#endif // QUICHE_QUIC_CORE_CRYPTO_CRYPTO_UTILS_H_