/src/boringssl/ssl/tls13_enc.cc
Line | Count | Source |
1 | | // Copyright 2016 The BoringSSL Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <openssl/ssl.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include <algorithm> |
21 | | #include <string_view> |
22 | | #include <utility> |
23 | | |
24 | | #include <openssl/aead.h> |
25 | | #include <openssl/aes.h> |
26 | | #include <openssl/bytestring.h> |
27 | | #include <openssl/chacha.h> |
28 | | #include <openssl/digest.h> |
29 | | #include <openssl/hkdf.h> |
30 | | #include <openssl/hmac.h> |
31 | | #include <openssl/mem.h> |
32 | | |
33 | | #include "../crypto/bytestring/internal.h" |
34 | | #include "../crypto/fipsmodule/tls/internal.h" |
35 | | #include "../crypto/internal.h" |
36 | | #include "internal.h" |
37 | | |
38 | | |
39 | | BSSL_NAMESPACE_BEGIN |
40 | | |
41 | | static bool init_key_schedule(SSL_HANDSHAKE *hs, SSLTranscript *transcript, |
42 | 6.47k | uint16_t version, const SSL_CIPHER *cipher) { |
43 | 6.47k | if (!transcript->InitHash(version, cipher)) { |
44 | 0 | return false; |
45 | 0 | } |
46 | | |
47 | | // Initialize the secret to the zero key. |
48 | 6.47k | hs->secret.clear(); |
49 | 6.47k | hs->secret.Resize(transcript->DigestLen()); |
50 | 6.47k | return true; |
51 | 6.47k | } |
52 | | |
53 | | static bool hkdf_extract_to_secret(SSL_HANDSHAKE *hs, |
54 | | const SSLTranscript &transcript, |
55 | 16.2k | Span<const uint8_t> in) { |
56 | 16.2k | size_t len; |
57 | 16.2k | if (!HKDF_extract(hs->secret.data(), &len, transcript.Digest(), in.data(), |
58 | 16.2k | in.size(), hs->secret.data(), hs->secret.size())) { |
59 | 0 | return false; |
60 | 0 | } |
61 | 16.2k | assert(len == hs->secret.size()); |
62 | 16.2k | return true; |
63 | 16.2k | } |
64 | | |
65 | 6.40k | bool tls13_init_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> psk) { |
66 | 6.40k | if (!init_key_schedule(hs, &hs->transcript, ssl_protocol_version(hs->ssl), |
67 | 6.40k | hs->new_cipher)) { |
68 | 0 | return false; |
69 | 0 | } |
70 | | |
71 | | // Handback includes the whole handshake transcript, so we cannot free the |
72 | | // transcript buffer in the handback case. |
73 | 6.40k | if (!hs->handback) { |
74 | 6.40k | hs->transcript.FreeBuffer(); |
75 | 6.40k | } |
76 | 6.40k | return hkdf_extract_to_secret(hs, hs->transcript, psk); |
77 | 6.40k | } |
78 | | |
79 | | bool tls13_init_early_key_schedule(SSL_HANDSHAKE *hs, |
80 | 64 | const SSL_SESSION *session) { |
81 | 64 | assert(!hs->ssl->server); |
82 | | // When offering ECH, early data is associated with ClientHelloInner, not |
83 | | // ClientHelloOuter. |
84 | 64 | SSLTranscript *transcript = |
85 | 64 | hs->selected_ech_config ? &hs->inner_transcript : &hs->transcript; |
86 | 64 | return init_key_schedule(hs, transcript, |
87 | 64 | ssl_session_protocol_version(session), |
88 | 64 | session->cipher) && |
89 | 64 | hkdf_extract_to_secret(hs, *transcript, session->secret); |
90 | 64 | } |
91 | | |
92 | | static bool hkdf_expand_label_with_prefix(Span<uint8_t> out, |
93 | | const EVP_MD *digest, |
94 | | Span<const uint8_t> secret, |
95 | | std::string_view label_prefix, |
96 | | std::string_view label, |
97 | 195k | Span<const uint8_t> hash) { |
98 | | // This is a copy of CRYPTO_tls13_hkdf_expand_label, but modified to take an |
99 | | // arbitrary prefix for the label instead of using the hardcoded "tls13 " |
100 | | // prefix. |
101 | 195k | CBB cbb, child; |
102 | 195k | uint8_t *hkdf_label = nullptr; |
103 | 195k | size_t hkdf_label_len; |
104 | 195k | CBB_zero(&cbb); |
105 | 195k | if (!CBB_init(&cbb, |
106 | 195k | 2 + 1 + label_prefix.size() + label.size() + 1 + hash.size()) || |
107 | 195k | !CBB_add_u16(&cbb, out.size()) || |
108 | 195k | !CBB_add_u8_length_prefixed(&cbb, &child) || |
109 | 195k | !CBB_add_bytes(&child, |
110 | 195k | reinterpret_cast<const uint8_t *>(label_prefix.data()), |
111 | 195k | label_prefix.size()) || |
112 | 195k | !CBB_add_bytes(&child, reinterpret_cast<const uint8_t *>(label.data()), |
113 | 195k | label.size()) || |
114 | 195k | !CBB_add_u8_length_prefixed(&cbb, &child) || |
115 | 195k | !CBB_add_bytes(&child, hash.data(), hash.size()) || |
116 | 195k | !CBB_finish(&cbb, &hkdf_label, &hkdf_label_len)) { |
117 | 0 | CBB_cleanup(&cbb); |
118 | 0 | return false; |
119 | 0 | } |
120 | | |
121 | 195k | const int ret = HKDF_expand(out.data(), out.size(), digest, secret.data(), |
122 | 195k | secret.size(), hkdf_label, hkdf_label_len); |
123 | 195k | OPENSSL_free(hkdf_label); |
124 | 195k | return ret == 1; |
125 | 195k | } |
126 | | |
127 | | static bool hkdf_expand_label(Span<uint8_t> out, const EVP_MD *digest, |
128 | | Span<const uint8_t> secret, |
129 | | std::string_view label, Span<const uint8_t> hash, |
130 | 247k | bool is_dtls) { |
131 | 247k | if (is_dtls) { |
132 | 195k | return hkdf_expand_label_with_prefix(out, digest, secret, "dtls13", label, |
133 | 195k | hash); |
134 | 195k | } |
135 | 52.1k | return CRYPTO_tls13_hkdf_expand_label( |
136 | 52.1k | out.data(), out.size(), digest, secret.data(), secret.size(), |
137 | 52.1k | reinterpret_cast<const uint8_t *>(label.data()), label.size(), |
138 | 52.1k | hash.data(), hash.size()) == 1; |
139 | 247k | } |
140 | | |
141 | | static const char kTLS13LabelDerived[] = "derived"; |
142 | | |
143 | 9.80k | bool tls13_advance_key_schedule(SSL_HANDSHAKE *hs, Span<const uint8_t> in) { |
144 | 9.80k | uint8_t derive_context[EVP_MAX_MD_SIZE]; |
145 | 9.80k | unsigned derive_context_len; |
146 | 9.80k | return EVP_Digest(nullptr, 0, derive_context, &derive_context_len, |
147 | 9.80k | hs->transcript.Digest(), nullptr) && |
148 | 9.80k | hkdf_expand_label(Span(hs->secret), hs->transcript.Digest(), |
149 | 9.80k | hs->secret, kTLS13LabelDerived, |
150 | 9.80k | Span(derive_context, derive_context_len), |
151 | 9.80k | SSL_is_dtls(hs->ssl)) && |
152 | 9.80k | hkdf_extract_to_secret(hs, hs->transcript, in); |
153 | 9.80k | } |
154 | | |
155 | | // derive_secret_with_transcript derives a secret of length |
156 | | // `transcript.DigestLen()` and writes the result in `out` with the given label, |
157 | | // the current base secret, and the state of `transcript`. It returns true on |
158 | | // success and false on error. |
159 | | static bool derive_secret_with_transcript( |
160 | | const SSL_HANDSHAKE *hs, InplaceVector<uint8_t, SSL_MAX_MD_SIZE> *out, |
161 | 27.2k | const SSLTranscript &transcript, std::string_view label) { |
162 | 27.2k | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
163 | 27.2k | size_t context_hash_len; |
164 | 27.2k | if (!transcript.GetHash(context_hash, &context_hash_len)) { |
165 | 0 | return false; |
166 | 0 | } |
167 | | |
168 | 27.2k | out->ResizeForOverwrite(transcript.DigestLen()); |
169 | 27.2k | return hkdf_expand_label(Span(*out), transcript.Digest(), hs->secret, label, |
170 | 27.2k | Span(context_hash, context_hash_len), |
171 | 27.2k | SSL_is_dtls(hs->ssl)); |
172 | 27.2k | } |
173 | | |
174 | | static bool derive_secret(SSL_HANDSHAKE *hs, |
175 | | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> *out, |
176 | 27.1k | std::string_view label) { |
177 | 27.1k | return derive_secret_with_transcript(hs, out, hs->transcript, label); |
178 | 27.1k | } |
179 | | |
180 | | bool tls13_set_traffic_key(SSLImpl *ssl, enum ssl_encryption_level_t level, |
181 | | enum evp_aead_direction_t direction, |
182 | | const SSL_SESSION *session, |
183 | 55.6k | Span<const uint8_t> traffic_secret) { |
184 | 55.6k | uint16_t version = ssl_session_protocol_version(session); |
185 | 55.6k | const EVP_MD *digest = ssl_session_get_digest(session); |
186 | 55.6k | bool is_dtls = SSL_is_dtls(ssl); |
187 | 55.6k | UniquePtr<SSLAEADContext> traffic_aead; |
188 | 55.6k | if (SSL_is_quic(ssl)) { |
189 | | // Install a placeholder SSLAEADContext so that SSL accessors work. The |
190 | | // encryption itself will be handled by the SSL_QUIC_METHOD. |
191 | 0 | traffic_aead = SSLAEADContext::CreatePlaceholderForQUIC(session->cipher); |
192 | 55.6k | } else { |
193 | | // Look up cipher suite properties. |
194 | 55.6k | const EVP_AEAD *aead; |
195 | 55.6k | size_t discard; |
196 | 55.6k | if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher, |
197 | 55.6k | version)) { |
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | | // Derive the key and IV. |
202 | 55.6k | uint8_t key_buf[EVP_AEAD_MAX_KEY_LENGTH], iv_buf[EVP_AEAD_MAX_NONCE_LENGTH]; |
203 | 55.6k | auto key = Span(key_buf).first(EVP_AEAD_key_length(aead)); |
204 | 55.6k | auto iv = Span(iv_buf).first(EVP_AEAD_nonce_length(aead)); |
205 | 55.6k | if (!hkdf_expand_label(key, digest, traffic_secret, "key", {}, is_dtls) || |
206 | 55.6k | !hkdf_expand_label(iv, digest, traffic_secret, "iv", {}, is_dtls)) { |
207 | 0 | return false; |
208 | 0 | } |
209 | | |
210 | 55.6k | traffic_aead = SSLAEADContext::Create(direction, session->ssl_version, |
211 | 55.6k | session->cipher, key, {}, iv); |
212 | 55.6k | } |
213 | | |
214 | 55.6k | if (!traffic_aead) { |
215 | 3 | return false; |
216 | 3 | } |
217 | | |
218 | 55.6k | if (direction == evp_aead_open) { |
219 | 45.6k | if (!ssl->method->set_read_state(ssl, level, std::move(traffic_aead), |
220 | 45.6k | traffic_secret)) { |
221 | 25 | return false; |
222 | 25 | } |
223 | 45.6k | ssl->s3->read_traffic_secret.CopyFrom(traffic_secret); |
224 | 45.6k | } else { |
225 | 10.0k | if (!ssl->method->set_write_state(ssl, level, std::move(traffic_aead), |
226 | 10.0k | traffic_secret)) { |
227 | 0 | return false; |
228 | 0 | } |
229 | 10.0k | ssl->s3->write_traffic_secret.CopyFrom(traffic_secret); |
230 | 10.0k | } |
231 | | |
232 | 55.6k | return true; |
233 | 55.6k | } |
234 | | |
235 | | namespace { |
236 | | |
237 | | class AESRecordNumberEncrypter : public RecordNumberEncrypter { |
238 | | public: |
239 | 0 | bool SetKey(Span<const uint8_t> key) override { |
240 | 0 | return AES_set_encrypt_key(key.data(), key.size() * 8, &key_) == 0; |
241 | 0 | } |
242 | | |
243 | 0 | bool GenerateMask(Span<uint8_t> out, Span<const uint8_t> sample) override { |
244 | 0 | if (sample.size() < AES_BLOCK_SIZE || out.size() > AES_BLOCK_SIZE) { |
245 | 0 | return false; |
246 | 0 | } |
247 | 0 | uint8_t mask[AES_BLOCK_SIZE]; |
248 | 0 | AES_encrypt(sample.data(), mask, &key_); |
249 | 0 | OPENSSL_memcpy(out.data(), mask, out.size()); |
250 | 0 | return true; |
251 | 0 | } |
252 | | |
253 | | private: |
254 | | AES_KEY key_; |
255 | | }; |
256 | | |
257 | | class AES128RecordNumberEncrypter : public AESRecordNumberEncrypter { |
258 | | public: |
259 | 0 | size_t KeySize() override { return 16; } |
260 | | }; |
261 | | |
262 | | class AES256RecordNumberEncrypter : public AESRecordNumberEncrypter { |
263 | | public: |
264 | 0 | size_t KeySize() override { return 32; } |
265 | | }; |
266 | | |
267 | | class ChaChaRecordNumberEncrypter : public RecordNumberEncrypter { |
268 | | public: |
269 | 0 | size_t KeySize() override { return kKeySize; } |
270 | | |
271 | 0 | bool SetKey(Span<const uint8_t> key) override { |
272 | 0 | if (key.size() != kKeySize) { |
273 | 0 | return false; |
274 | 0 | } |
275 | 0 | OPENSSL_memcpy(key_, key.data(), key.size()); |
276 | 0 | return true; |
277 | 0 | } |
278 | | |
279 | 0 | bool GenerateMask(Span<uint8_t> out, Span<const uint8_t> sample) override { |
280 | | // RFC 9147 section 4.2.3 uses the first 4 bytes of the sample as the |
281 | | // counter and the next 12 bytes as the nonce. If we have less than 4+12=16 |
282 | | // bytes in the sample, then we'll read past the end of the `sample` buffer. |
283 | | // The counter is interpreted as little-endian per RFC 8439. |
284 | 0 | if (sample.size() < 16) { |
285 | 0 | return false; |
286 | 0 | } |
287 | 0 | uint32_t counter = CRYPTO_load_u32_le(sample.data()); |
288 | 0 | auto nonce = sample.subspan<4>(); |
289 | 0 | OPENSSL_memset(out.data(), 0, out.size()); |
290 | 0 | CRYPTO_chacha_20(out.data(), out.data(), out.size(), key_, nonce.data(), |
291 | 0 | counter); |
292 | 0 | return true; |
293 | 0 | } |
294 | | |
295 | | private: |
296 | | static constexpr size_t kKeySize = 32; |
297 | | uint8_t key_[kKeySize]; |
298 | | }; |
299 | | |
300 | | class NullRecordNumberEncrypter : public RecordNumberEncrypter { |
301 | | public: |
302 | 43.7k | size_t KeySize() override { return 0; } |
303 | 43.7k | bool SetKey(Span<const uint8_t> key) override { return true; } |
304 | 104k | bool GenerateMask(Span<uint8_t> out, Span<const uint8_t> sample) override { |
305 | 104k | OPENSSL_memset(out.data(), 0, out.size()); |
306 | 104k | return true; |
307 | 104k | } |
308 | | }; |
309 | | |
310 | | } // namespace |
311 | | |
312 | | UniquePtr<RecordNumberEncrypter> RecordNumberEncrypter::Create( |
313 | 43.7k | const SSL_CIPHER *cipher, Span<const uint8_t> traffic_secret) { |
314 | 43.7k | const EVP_MD *digest = ssl_get_handshake_digest(TLS1_3_VERSION, cipher); |
315 | 43.7k | UniquePtr<RecordNumberEncrypter> ret; |
316 | 43.7k | if (CRYPTO_fuzzer_mode_enabled()) { |
317 | 43.7k | ret = MakeUnique<NullRecordNumberEncrypter>(); |
318 | 43.7k | } else if (cipher->algorithm_enc == SSL_AES128GCM) { |
319 | 0 | ret = MakeUnique<AES128RecordNumberEncrypter>(); |
320 | 0 | } else if (cipher->algorithm_enc == SSL_AES256GCM) { |
321 | 0 | ret = MakeUnique<AES256RecordNumberEncrypter>(); |
322 | 0 | } else if (cipher->algorithm_enc == SSL_CHACHA20POLY1305) { |
323 | 0 | ret = MakeUnique<ChaChaRecordNumberEncrypter>(); |
324 | 0 | } else { |
325 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
326 | 0 | } |
327 | 43.7k | if (ret == nullptr) { |
328 | 0 | return nullptr; |
329 | 0 | } |
330 | | |
331 | 43.7k | uint8_t rne_key_buf[RecordNumberEncrypter::kMaxKeySize]; |
332 | 43.7k | auto rne_key = Span(rne_key_buf).first(ret->KeySize()); |
333 | 43.7k | if (!hkdf_expand_label(rne_key, digest, traffic_secret, "sn", {}, |
334 | 43.7k | /*is_dtls=*/true) || |
335 | 43.7k | !ret->SetKey(rne_key)) { |
336 | 0 | return nullptr; |
337 | 0 | } |
338 | 43.7k | return ret; |
339 | 43.7k | } |
340 | | |
341 | | static const char kTLS13LabelExporter[] = "exp master"; |
342 | | |
343 | | static const char kTLS13LabelClientEarlyTraffic[] = "c e traffic"; |
344 | | static const char kTLS13LabelClientHandshakeTraffic[] = "c hs traffic"; |
345 | | static const char kTLS13LabelServerHandshakeTraffic[] = "s hs traffic"; |
346 | | static const char kTLS13LabelClientApplicationTraffic[] = "c ap traffic"; |
347 | | static const char kTLS13LabelServerApplicationTraffic[] = "s ap traffic"; |
348 | | |
349 | 146 | bool tls13_derive_early_secret(SSL_HANDSHAKE *hs) { |
350 | 146 | SSLImpl *const ssl = hs->ssl; |
351 | | // When offering ECH on the client, early data is associated with |
352 | | // ClientHelloInner, not ClientHelloOuter. |
353 | 146 | const SSLTranscript &transcript = (!ssl->server && hs->selected_ech_config) |
354 | 146 | ? hs->inner_transcript |
355 | 146 | : hs->transcript; |
356 | 146 | if (!derive_secret_with_transcript(hs, &hs->early_traffic_secret, transcript, |
357 | 146 | kTLS13LabelClientEarlyTraffic) || |
358 | 146 | !ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET", |
359 | 146 | hs->early_traffic_secret)) { |
360 | 0 | return false; |
361 | 0 | } |
362 | 146 | return true; |
363 | 146 | } |
364 | | |
365 | 5.55k | bool tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) { |
366 | 5.55k | SSLImpl *const ssl = hs->ssl; |
367 | 5.55k | if (!derive_secret(hs, &hs->client_handshake_secret, |
368 | 5.55k | kTLS13LabelClientHandshakeTraffic) || |
369 | 5.55k | !ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET", |
370 | 5.55k | hs->client_handshake_secret) || |
371 | 5.55k | !derive_secret(hs, &hs->server_handshake_secret, |
372 | 5.55k | kTLS13LabelServerHandshakeTraffic) || |
373 | 5.55k | !ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET", |
374 | 5.55k | hs->server_handshake_secret)) { |
375 | 0 | return false; |
376 | 0 | } |
377 | | |
378 | 5.55k | return true; |
379 | 5.55k | } |
380 | | |
381 | 4.24k | bool tls13_derive_application_secrets(SSL_HANDSHAKE *hs) { |
382 | 4.24k | SSLImpl *const ssl = hs->ssl; |
383 | 4.24k | if (!derive_secret(hs, &hs->client_traffic_secret_0, |
384 | 4.24k | kTLS13LabelClientApplicationTraffic) || |
385 | 4.24k | !ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0", |
386 | 4.24k | hs->client_traffic_secret_0) || |
387 | 4.24k | !derive_secret(hs, &hs->server_traffic_secret_0, |
388 | 4.24k | kTLS13LabelServerApplicationTraffic) || |
389 | 4.24k | !ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0", |
390 | 4.24k | hs->server_traffic_secret_0) || |
391 | 4.24k | !derive_secret(hs, &ssl->s3->exporter_secret, kTLS13LabelExporter) || |
392 | 4.24k | !ssl_log_secret(ssl, "EXPORTER_SECRET", ssl->s3->exporter_secret)) { |
393 | 0 | return false; |
394 | 0 | } |
395 | | |
396 | 4.24k | return true; |
397 | 4.24k | } |
398 | | |
399 | | static const char kTLS13LabelApplicationTraffic[] = "traffic upd"; |
400 | | |
401 | | bool tls13_rotate_traffic_key(SSLImpl *ssl, |
402 | 37.0k | enum evp_aead_direction_t direction) { |
403 | 37.0k | InplaceVector<uint8_t, SSL_MAX_MD_SIZE> secret( |
404 | 37.0k | direction == evp_aead_open ? ssl->s3->read_traffic_secret |
405 | 37.0k | : ssl->s3->write_traffic_secret); |
406 | | |
407 | 37.0k | const SSL_SESSION *session = SSL_get_session(ssl); |
408 | 37.0k | const EVP_MD *digest = ssl_session_get_digest(session); |
409 | 37.0k | return hkdf_expand_label(Span(secret), digest, secret, |
410 | 37.0k | kTLS13LabelApplicationTraffic, {}, |
411 | 37.0k | SSL_is_dtls(ssl)) && |
412 | 37.0k | tls13_set_traffic_key(ssl, ssl_encryption_application, direction, |
413 | 37.0k | session, Span(secret)); |
414 | 37.0k | } |
415 | | |
416 | | static const char kTLS13LabelResumption[] = "res master"; |
417 | | |
418 | 3.30k | bool tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) { |
419 | 3.30k | return derive_secret(hs, &hs->new_session->secret, kTLS13LabelResumption); |
420 | 3.30k | } |
421 | | |
422 | | static const char kTLS13LabelFinished[] = "finished"; |
423 | | |
424 | | // tls13_verify_data sets `out` to be the HMAC of `context` using a derived |
425 | | // Finished key for both Finished messages and the PSK binder. `out` must have |
426 | | // space available for `EVP_MAX_MD_SIZE` bytes. |
427 | | static bool tls13_verify_data(uint8_t *out, size_t *out_len, |
428 | | const EVP_MD *digest, Span<const uint8_t> secret, |
429 | 8.25k | Span<const uint8_t> context, bool is_dtls) { |
430 | 8.25k | uint8_t key_buf[EVP_MAX_MD_SIZE]; |
431 | 8.25k | auto key = Span(key_buf, EVP_MD_size(digest)); |
432 | 8.25k | unsigned len; |
433 | 8.25k | if (!hkdf_expand_label(key, digest, secret, kTLS13LabelFinished, {}, |
434 | 8.25k | is_dtls) || |
435 | 8.25k | HMAC(digest, key.data(), key.size(), context.data(), context.size(), out, |
436 | 8.25k | &len) == nullptr) { |
437 | 0 | return false; |
438 | 0 | } |
439 | 8.25k | *out_len = len; |
440 | 8.25k | return true; |
441 | 8.25k | } |
442 | | |
443 | | bool tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len, |
444 | 7.55k | bool is_server) { |
445 | 7.55k | Span<const uint8_t> traffic_secret = |
446 | 7.55k | is_server ? hs->server_handshake_secret : hs->client_handshake_secret; |
447 | | |
448 | 7.55k | uint8_t context_hash[EVP_MAX_MD_SIZE]; |
449 | 7.55k | size_t context_hash_len; |
450 | 7.55k | if (!hs->transcript.GetHash(context_hash, &context_hash_len) || |
451 | 7.55k | !tls13_verify_data(out, out_len, hs->transcript.Digest(), traffic_secret, |
452 | 7.55k | Span(context_hash, context_hash_len), |
453 | 7.55k | SSL_is_dtls(hs->ssl))) { |
454 | 0 | return false; |
455 | 0 | } |
456 | 7.55k | return true; |
457 | 7.55k | } |
458 | | |
459 | | static const char kTLS13LabelResumptionPSK[] = "resumption"; |
460 | | |
461 | | bool tls13_derive_session_psk(SSL_SESSION *session, Span<const uint8_t> nonce, |
462 | 9.44k | bool is_dtls) { |
463 | 9.44k | const EVP_MD *digest = ssl_session_get_digest(session); |
464 | | // The session initially stores the resumption_master_secret, which we |
465 | | // override with the PSK. |
466 | 9.44k | assert(session->secret.size() == EVP_MD_size(digest)); |
467 | 9.44k | return hkdf_expand_label(Span(session->secret), digest, session->secret, |
468 | 9.44k | kTLS13LabelResumptionPSK, nonce, is_dtls); |
469 | 9.44k | } |
470 | | |
471 | | static const char kTLS13LabelExportKeying[] = "exporter"; |
472 | | |
473 | | bool tls13_export_keying_material(const SSLImpl *ssl, Span<uint8_t> out, |
474 | | Span<const uint8_t> secret, |
475 | | std::string_view label, |
476 | 0 | Span<const uint8_t> context) { |
477 | 0 | if (secret.empty()) { |
478 | 0 | assert(0); |
479 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
480 | 0 | return false; |
481 | 0 | } |
482 | | |
483 | 0 | const EVP_MD *digest = ssl_session_get_digest(SSL_get_session(ssl)); |
484 | |
|
485 | 0 | uint8_t hash_buf[EVP_MAX_MD_SIZE]; |
486 | 0 | uint8_t export_context_buf[EVP_MAX_MD_SIZE]; |
487 | 0 | unsigned hash_len; |
488 | 0 | unsigned export_context_len; |
489 | 0 | if (!EVP_Digest(context.data(), context.size(), hash_buf, &hash_len, digest, |
490 | 0 | nullptr) || |
491 | 0 | !EVP_Digest(nullptr, 0, export_context_buf, &export_context_len, digest, |
492 | 0 | nullptr)) { |
493 | 0 | return false; |
494 | 0 | } |
495 | | |
496 | 0 | auto hash = Span(hash_buf, hash_len); |
497 | 0 | auto export_context = Span(export_context_buf, export_context_len); |
498 | 0 | uint8_t derived_secret_buf[EVP_MAX_MD_SIZE]; |
499 | 0 | auto derived_secret = Span(derived_secret_buf, EVP_MD_size(digest)); |
500 | 0 | return hkdf_expand_label(derived_secret, digest, secret, label, |
501 | 0 | export_context, SSL_is_dtls(ssl)) && |
502 | 0 | hkdf_expand_label(out, digest, derived_secret, kTLS13LabelExportKeying, |
503 | 0 | hash, SSL_is_dtls(ssl)); |
504 | 0 | } |
505 | | |
506 | 736 | const EVP_MD *ssl_pre_shared_key_hash(const SSLPreSharedKey &psk) { |
507 | 736 | if (const auto *imported = std::get_if<SSLImportedPSK>(&psk); |
508 | 736 | imported != nullptr) { |
509 | 0 | return imported->md; |
510 | 0 | } |
511 | 736 | return ssl_session_get_digest(std::get<UniquePtr<SSL_SESSION>>(psk).get()); |
512 | 736 | } |
513 | | |
514 | 706 | Span<const uint8_t> ssl_pre_shared_key_identity(const SSLPreSharedKey &psk) { |
515 | 706 | if (const auto *imported = std::get_if<SSLImportedPSK>(&psk); |
516 | 706 | imported != nullptr) { |
517 | 0 | return imported->imported_identity; |
518 | 0 | } |
519 | 706 | return std::get<UniquePtr<SSL_SESSION>>(psk)->ticket; |
520 | 706 | } |
521 | | |
522 | 248 | Span<const uint8_t> ssl_pre_shared_key_secret(const SSLPreSharedKey &psk) { |
523 | 248 | if (const auto *imported = std::get_if<SSLImportedPSK>(&psk); |
524 | 248 | imported != nullptr) { |
525 | 0 | return imported->ipskx; |
526 | 0 | } |
527 | 248 | return std::get<UniquePtr<SSL_SESSION>>(psk)->secret; |
528 | 248 | } |
529 | | |
530 | | bool tls13_psk_binder(const SSL_HANDSHAKE *hs, Span<uint8_t> out, |
531 | | size_t *out_len, const SSLPreSharedKey &psk, |
532 | | const SSLTranscript &transcript, |
533 | 696 | Span<const uint8_t> client_hello, size_t binders_len) { |
534 | 696 | const EVP_MD *digest; |
535 | 696 | Span<const uint8_t> secret; |
536 | 696 | std::string_view label; |
537 | 696 | if (const auto *imported = std::get_if<SSLImportedPSK>(&psk); |
538 | 696 | imported != nullptr) { |
539 | 0 | digest = imported->md; |
540 | 0 | secret = imported->ipskx; |
541 | 0 | label = "imp binder"; |
542 | 696 | } else { |
543 | 696 | const SSL_SESSION *session = std::get<UniquePtr<SSL_SESSION>>(psk).get(); |
544 | 696 | digest = ssl_session_get_digest(session); |
545 | 696 | secret = session->secret; |
546 | 696 | label = "res binder"; |
547 | 696 | } |
548 | | |
549 | | // Compute the binder key. |
550 | | // |
551 | | // TODO(davidben): Ideally we wouldn't recompute early secret and the binder |
552 | | // key each time. |
553 | 696 | uint8_t binder_context[EVP_MAX_MD_SIZE]; |
554 | 696 | unsigned binder_context_len; |
555 | 696 | uint8_t early_secret[EVP_MAX_MD_SIZE] = {0}; |
556 | 696 | size_t early_secret_len; |
557 | 696 | uint8_t binder_key_buf[EVP_MAX_MD_SIZE] = {0}; |
558 | 696 | auto binder_key = Span(binder_key_buf, EVP_MD_size(digest)); |
559 | 696 | if (!EVP_Digest(nullptr, 0, binder_context, &binder_context_len, digest, |
560 | 696 | nullptr) || |
561 | 696 | !HKDF_extract(early_secret, &early_secret_len, digest, secret.data(), |
562 | 696 | secret.size(), nullptr, 0) || |
563 | 696 | !hkdf_expand_label( |
564 | 696 | binder_key, digest, Span(early_secret, early_secret_len), label, |
565 | 696 | Span(binder_context, binder_context_len), SSL_is_dtls(hs->ssl))) { |
566 | 0 | return false; |
567 | 0 | } |
568 | | |
569 | | // Hash the transcript and truncated ClientHello. As part of this, construct |
570 | | // the expected ClientHello header. |
571 | 696 | if (client_hello.size() < binders_len || client_hello.size() > 0xffffff) { |
572 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
573 | 0 | return false; |
574 | 0 | } |
575 | 696 | uint8_t header[4] = { |
576 | 696 | SSL3_MT_CLIENT_HELLO, |
577 | 696 | static_cast<uint8_t>(client_hello.size() >> 16), |
578 | 696 | static_cast<uint8_t>(client_hello.size() >> 8), |
579 | 696 | static_cast<uint8_t>(client_hello.size()), |
580 | 696 | }; |
581 | 696 | auto truncated = client_hello.subspan(0, client_hello.size() - binders_len); |
582 | 696 | uint8_t context[EVP_MAX_MD_SIZE]; |
583 | 696 | unsigned context_len; |
584 | 696 | ScopedEVP_MD_CTX ctx; |
585 | 696 | if (!transcript.CopyToHashContext(ctx.get(), digest) || |
586 | 696 | !EVP_DigestUpdate(ctx.get(), header, sizeof(header)) || |
587 | 696 | !EVP_DigestUpdate(ctx.get(), truncated.data(), truncated.size()) || |
588 | 696 | !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) { |
589 | 0 | return false; |
590 | 0 | } |
591 | | |
592 | 696 | BSSL_CHECK(out.size() >= EVP_MD_size(digest)); |
593 | 696 | if (!tls13_verify_data(out.data(), out_len, digest, binder_key, |
594 | 696 | Span(context, context_len), SSL_is_dtls(hs->ssl))) { |
595 | 0 | return false; |
596 | 0 | } |
597 | | |
598 | 696 | assert(*out_len == EVP_MD_size(digest)); |
599 | 696 | return true; |
600 | 696 | } |
601 | | |
602 | 0 | static std::optional<uint16_t> hkdf_md_to_kdf_id(const EVP_MD *hkdf_md) { |
603 | | // See Section 10 of RFC 9258. |
604 | 0 | switch (EVP_MD_nid(hkdf_md)) { |
605 | 0 | case NID_sha256: |
606 | 0 | return 0x0001; // HKDF_SHA256 |
607 | 0 | case NID_sha384: |
608 | 0 | return 0x0002; // HKDF_SHA384 |
609 | 0 | default: |
610 | 0 | return std::nullopt; |
611 | 0 | } |
612 | 0 | } |
613 | | |
614 | | std::optional<SSLImportedPSK> tls13_derive_imported_psk(const SSL_HANDSHAKE *hs, |
615 | | SSLCredential *cred, |
616 | | uint16_t protocol, |
617 | 0 | const EVP_MD *hkdf_md) { |
618 | 0 | assert(cred->type == SSLCredentialType::kPreSharedKey); |
619 | | |
620 | 0 | std::optional<uint16_t> target_kdf = hkdf_md_to_kdf_id(hkdf_md); |
621 | 0 | if (!target_kdf.has_value()) { |
622 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
623 | 0 | return std::nullopt; |
624 | 0 | } |
625 | | |
626 | 0 | SSLImportedPSK ret; |
627 | 0 | ret.credential = UpRef(cred); |
628 | 0 | ret.protocol = protocol; |
629 | 0 | ret.md = hkdf_md; |
630 | | |
631 | | // See Section 5.1 of RFC 9258. |
632 | 0 | ScopedCBB imported_id; |
633 | 0 | CBB external_identity, context; |
634 | 0 | if (!CBB_init(imported_id.get(), 2 + cred->epsk_id.size() + 2 + |
635 | 0 | cred->epsk_context.size() + 2 + 2) || |
636 | 0 | !CBB_add_u16_length_prefixed(imported_id.get(), &external_identity) || |
637 | 0 | !CBB_add_bytes(&external_identity, cred->epsk_id.data(), |
638 | 0 | cred->epsk_id.size()) || |
639 | 0 | !CBB_add_u16_length_prefixed(imported_id.get(), &context) || |
640 | 0 | !CBB_add_bytes(&context, cred->epsk_context.data(), |
641 | 0 | cred->epsk_context.size()) || |
642 | 0 | !CBB_add_u16(imported_id.get(), protocol) || |
643 | 0 | !CBB_add_u16(imported_id.get(), *target_kdf) || |
644 | 0 | !CBBFinishArray(imported_id.get(), &ret.imported_identity)) { |
645 | 0 | return std::nullopt; |
646 | 0 | } |
647 | | |
648 | 0 | ScopedEVP_MD_CTX imported_id_ctx; |
649 | 0 | InplaceVector<uint8_t, EVP_MAX_MD_SIZE> imported_id_hash; |
650 | 0 | imported_id_hash.ResizeForOverwrite(EVP_MD_size(cred->epsk_md)); |
651 | 0 | unsigned imported_id_hash_len; |
652 | 0 | if (!EVP_Digest(ret.imported_identity.data(), ret.imported_identity.size(), |
653 | 0 | imported_id_hash.data(), &imported_id_hash_len, cred->epsk_md, |
654 | 0 | nullptr)) { |
655 | 0 | return std::nullopt; |
656 | 0 | } |
657 | 0 | assert(imported_id_hash.size() == imported_id_hash_len); |
658 | | |
659 | 0 | ret.ipskx.ResizeForOverwrite(EVP_MD_size(hkdf_md)); |
660 | 0 | if (!hkdf_expand_label(Span(ret.ipskx), cred->epsk_md, cred->epskx, |
661 | 0 | "derived psk", imported_id_hash, |
662 | 0 | SSL_is_dtls(hs->ssl))) { |
663 | 0 | return std::nullopt; |
664 | 0 | } |
665 | | |
666 | 0 | return ret; |
667 | 0 | } |
668 | | |
669 | | bool tls13_compare_imported_psk_identity(Span<const uint8_t> id, |
670 | | const SSLCredential *cred, |
671 | | uint16_t protocol, |
672 | 0 | const EVP_MD *hkdf_md) { |
673 | 0 | assert(cred->type == SSLCredentialType::kPreSharedKey); |
674 | 0 | std::optional<uint16_t> target_kdf = hkdf_md_to_kdf_id(hkdf_md); |
675 | 0 | if (!target_kdf.has_value()) { |
676 | 0 | return false; |
677 | 0 | } |
678 | | |
679 | | // See Section 5.1 of RFC 9258. |
680 | 0 | CBS cbs = id, external_identity, context; |
681 | 0 | uint16_t found_protocol, found_kdf; |
682 | 0 | return CBS_get_u16_length_prefixed(&cbs, &external_identity) && |
683 | 0 | external_identity == Span(cred->epsk_id) && |
684 | 0 | CBS_get_u16_length_prefixed(&cbs, &context) && |
685 | 0 | context == Span(cred->epsk_context) && |
686 | 0 | CBS_get_u16(&cbs, &found_protocol) && found_protocol == protocol && |
687 | 0 | CBS_get_u16(&cbs, &found_kdf) && found_kdf == *target_kdf && |
688 | 0 | CBS_len(&cbs) == 0; |
689 | 0 | } |
690 | | |
691 | 2.05k | size_t ssl_ech_confirmation_signal_hello_offset(const SSLImpl *ssl) { |
692 | 2.05k | static_assert(ECH_CONFIRMATION_SIGNAL_LEN < SSL3_RANDOM_SIZE, |
693 | 2.05k | "the confirmation signal is a suffix of the random"); |
694 | 2.05k | const size_t header_len = |
695 | 2.05k | SSL_is_dtls(ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
696 | 2.05k | return header_len + 2 /* version */ + SSL3_RANDOM_SIZE - |
697 | 2.05k | ECH_CONFIRMATION_SIGNAL_LEN; |
698 | 2.05k | } |
699 | | |
700 | | bool ssl_ech_accept_confirmation( |
701 | | const SSL_HANDSHAKE *hs, Span<uint8_t, ECH_CONFIRMATION_SIGNAL_LEN> out, |
702 | | Span<const uint8_t, SSL3_RANDOM_SIZE> client_random, |
703 | | const SSLTranscript &transcript, bool is_hrr, Span<const uint8_t> msg, |
704 | 191 | size_t offset) { |
705 | | // See RFC 9849, sections 7.2 and 7.2.1. |
706 | 191 | static const uint8_t kZeros[EVP_MAX_MD_SIZE] = {0}; |
707 | | |
708 | | // We hash `msg`, with bytes from `offset` zeroed. |
709 | 191 | if (msg.size() < offset + ECH_CONFIRMATION_SIGNAL_LEN) { |
710 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
711 | 0 | return false; |
712 | 0 | } |
713 | | |
714 | | // We represent DTLS messages with the longer DTLS 1.2 header, but DTLS 1.3 |
715 | | // removes the extra fields from the transcript. |
716 | | // |
717 | | // Size bound implied by ECH_CONFIRMATION_SIGNAL_LEN >= SSL3_HM_HEADER_LENGTH. |
718 | 191 | auto header = msg.first<SSL3_HM_HEADER_LENGTH>(); |
719 | 191 | size_t full_header_len = |
720 | 191 | SSL_is_dtls(hs->ssl) ? DTLS1_HM_HEADER_LENGTH : SSL3_HM_HEADER_LENGTH; |
721 | 191 | auto before_zeros = msg.subspan(full_header_len, offset - full_header_len); |
722 | 191 | auto after_zeros = msg.subspan(offset + ECH_CONFIRMATION_SIGNAL_LEN); |
723 | | |
724 | 191 | uint8_t context[EVP_MAX_MD_SIZE]; |
725 | 191 | unsigned context_len; |
726 | 191 | ScopedEVP_MD_CTX ctx; |
727 | 191 | if (!transcript.CopyToHashContext(ctx.get(), transcript.Digest()) || |
728 | 191 | !EVP_DigestUpdate(ctx.get(), header.data(), header.size()) || |
729 | 191 | !EVP_DigestUpdate(ctx.get(), before_zeros.data(), before_zeros.size()) || |
730 | 191 | !EVP_DigestUpdate(ctx.get(), kZeros, ECH_CONFIRMATION_SIGNAL_LEN) || |
731 | 191 | !EVP_DigestUpdate(ctx.get(), after_zeros.data(), after_zeros.size()) || |
732 | 191 | !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) { |
733 | 0 | return false; |
734 | 0 | } |
735 | | |
736 | 191 | uint8_t secret[EVP_MAX_MD_SIZE]; |
737 | 191 | size_t secret_len; |
738 | 191 | if (!HKDF_extract(secret, &secret_len, transcript.Digest(), |
739 | 191 | client_random.data(), client_random.size(), kZeros, |
740 | 191 | transcript.DigestLen())) { |
741 | 0 | return false; |
742 | 0 | } |
743 | | |
744 | 191 | return hkdf_expand_label( |
745 | 191 | out, transcript.Digest(), Span(secret, secret_len), |
746 | 191 | is_hrr ? "hrr ech accept confirmation" : "ech accept confirmation", |
747 | 191 | Span(context, context_len), SSL_is_dtls(hs->ssl)); |
748 | 191 | } |
749 | | |
750 | | BSSL_NAMESPACE_END |