Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/ssl_aead_ctx.cc

raw
1 // Copyright (c) 2015, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <openssl/ssl.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/aead.h>
10 #include <openssl/err.h>
11 #include <openssl/rand.h>
12
13 #include "../crypto/fipsmodule/cipher/internal.h"
14 #include "../crypto/internal.h"
15 #include "internal.h"
16
17
18 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
19 #define FUZZER_MODE true
20 #else
21 #define FUZZER_MODE false
22 #endif
23
24 BSSL_NAMESPACE_BEGIN
25
26 SSLAEADContext::SSLAEADContext(uint16_t version_arg, bool is_dtls_arg,
27 const SSL_CIPHER *cipher_arg)
28 : cipher_(cipher_arg),
29 version_(version_arg),
30 is_dtls_(is_dtls_arg),
31 variable_nonce_included_in_record_(false),
32 random_variable_nonce_(false),
33 xor_fixed_nonce_(false),
34 omit_length_in_ad_(false),
35 ad_is_header_(false) {
36 OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_));
37 }
38
39 SSLAEADContext::~SSLAEADContext() {}
40
41 UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) {
42 return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls,
43 nullptr /* cipher */);
44 }
45
46 UniquePtr<SSLAEADContext> SSLAEADContext::Create(
47 enum evp_aead_direction_t direction, uint16_t version, bool is_dtls,
48 const SSL_CIPHER *cipher, Span<const uint8_t> enc_key,
49 Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) {
50 const EVP_AEAD *aead;
51 uint16_t protocol_version;
52 size_t expected_mac_key_len, expected_fixed_iv_len;
53 if (!ssl_protocol_version_from_wire(&protocol_version, version) ||
54 !ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len,
55 &expected_fixed_iv_len, cipher, protocol_version,
56 is_dtls) ||
57 // Ensure the caller returned correct key sizes.
58 expected_fixed_iv_len != fixed_iv.size() ||
59 expected_mac_key_len != mac_key.size()) {
60 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
61 return nullptr;
62 }
63
64 uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH];
65 if (!mac_key.empty()) {
66 // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher
67 // suites).
68 if (mac_key.size() + enc_key.size() + fixed_iv.size() >
69 sizeof(merged_key)) {
70 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
71 return nullptr;
72 }
73 OPENSSL_memcpy(merged_key, mac_key.data(), mac_key.size());
74 OPENSSL_memcpy(merged_key + mac_key.size(), enc_key.data(), enc_key.size());
75 OPENSSL_memcpy(merged_key + mac_key.size() + enc_key.size(),
76 fixed_iv.data(), fixed_iv.size());
77 enc_key = MakeConstSpan(merged_key,
78 enc_key.size() + mac_key.size() + fixed_iv.size());
79 }
80
81 UniquePtr<SSLAEADContext> aead_ctx =
82 MakeUnique<SSLAEADContext>(version, is_dtls, cipher);
83 if (!aead_ctx) {
84 return nullptr;
85 }
86
87 assert(aead_ctx->ProtocolVersion() == protocol_version);
88
89 if (!EVP_AEAD_CTX_init_with_direction(
90 aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(),
91 EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) {
92 return nullptr;
93 }
94
95 assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH);
96 static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256,
97 "variable_nonce_len doesn't fit in uint8_t");
98 aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead);
99 if (mac_key.empty()) {
100 assert(fixed_iv.size() <= sizeof(aead_ctx->fixed_nonce_));
101 OPENSSL_memcpy(aead_ctx->fixed_nonce_, fixed_iv.data(), fixed_iv.size());
102 aead_ctx->fixed_nonce_len_ = fixed_iv.size();
103
104 if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) {
105 // The fixed nonce into the actual nonce (the sequence number).
106 aead_ctx->xor_fixed_nonce_ = true;
107 aead_ctx->variable_nonce_len_ = 8;
108 } else {
109 // The fixed IV is prepended to the nonce.
110 assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_);
111 aead_ctx->variable_nonce_len_ -= fixed_iv.size();
112 }
113
114 // AES-GCM uses an explicit nonce.
115 if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) {
116 aead_ctx->variable_nonce_included_in_record_ = true;
117 }
118
119 // The TLS 1.3 construction XORs the fixed nonce into the sequence number
120 // and omits the additional data.
121 if (protocol_version >= TLS1_3_VERSION) {
122 aead_ctx->xor_fixed_nonce_ = true;
123 aead_ctx->variable_nonce_len_ = 8;
124 aead_ctx->variable_nonce_included_in_record_ = false;
125 aead_ctx->ad_is_header_ = true;
126 assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_);
127 }
128 } else {
129 assert(protocol_version < TLS1_3_VERSION);
130 aead_ctx->variable_nonce_included_in_record_ = true;
131 aead_ctx->random_variable_nonce_ = true;
132 aead_ctx->omit_length_in_ad_ = true;
133 }
134
135 return aead_ctx;
136 }
137
138 UniquePtr<SSLAEADContext> SSLAEADContext::CreatePlaceholderForQUIC(
139 uint16_t version, const SSL_CIPHER *cipher) {
140 return MakeUnique<SSLAEADContext>(version, false, cipher);
141 }
142
143 void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) {
144 if (is_null_cipher()) {
145 version_ = version;
146 }
147 }
148
149 uint16_t SSLAEADContext::ProtocolVersion() const {
150 uint16_t protocol_version;
151 if (!ssl_protocol_version_from_wire(&protocol_version, version_)) {
152 assert(false);
153 return 0;
154 }
155 return protocol_version;
156 }
157
158 uint16_t SSLAEADContext::RecordVersion() const {
159 if (version_ == 0) {
160 assert(is_null_cipher());
161 return is_dtls_ ? DTLS1_VERSION : TLS1_VERSION;
162 }
163
164 if (ProtocolVersion() <= TLS1_2_VERSION) {
165 return version_;
166 }
167
168 return TLS1_2_VERSION;
169 }
170
171 size_t SSLAEADContext::ExplicitNonceLen() const {
172 if (!FUZZER_MODE && variable_nonce_included_in_record_) {
173 return variable_nonce_len_;
174 }
175 return 0;
176 }
177
178 bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len,
179 const size_t extra_in_len) const {
180 if (is_null_cipher() || FUZZER_MODE) {
181 *out_suffix_len = extra_in_len;
182 return true;
183 }
184 return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len,
185 extra_in_len);
186 }
187
188 bool SSLAEADContext::CiphertextLen(size_t *out_len, const size_t in_len,
189 const size_t extra_in_len) const {
190 size_t len;
191 if (!SuffixLen(&len, in_len, extra_in_len)) {
192 return false;
193 }
194 len += ExplicitNonceLen();
195 len += in_len;
196 if (len < in_len || len >= 0xffff) {
197 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
198 return false;
199 }
200 *out_len = len;
201 return true;
202 }
203
204 size_t SSLAEADContext::MaxOverhead() const {
205 return ExplicitNonceLen() +
206 (is_null_cipher() || FUZZER_MODE
207 ? 0
208 : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get())));
209 }
210
211 Span<const uint8_t> SSLAEADContext::GetAdditionalData(
212 uint8_t storage[13], uint8_t type, uint16_t record_version,
213 const uint8_t seqnum[8], size_t plaintext_len, Span<const uint8_t> header) {
214 if (ad_is_header_) {
215 return header;
216 }
217
218 OPENSSL_memcpy(storage, seqnum, 8);
219 size_t len = 8;
220 storage[len++] = type;
221 storage[len++] = static_cast<uint8_t>((record_version >> 8));
222 storage[len++] = static_cast<uint8_t>(record_version);
223 if (!omit_length_in_ad_) {
224 storage[len++] = static_cast<uint8_t>((plaintext_len >> 8));
225 storage[len++] = static_cast<uint8_t>(plaintext_len);
226 }
227 return MakeConstSpan(storage, len);
228 }
229
230 bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type,
231 uint16_t record_version, const uint8_t seqnum[8],
232 Span<const uint8_t> header, Span<uint8_t> in) {
233 if (is_null_cipher() || FUZZER_MODE) {
234 // Handle the initial NULL cipher.
235 *out = in;
236 return true;
237 }
238
239 // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed
240 // overhead. Otherwise the parameter is unused.
241 size_t plaintext_len = 0;
242 if (!omit_length_in_ad_) {
243 size_t overhead = MaxOverhead();
244 if (in.size() < overhead) {
245 // Publicly invalid.
246 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
247 return false;
248 }
249 plaintext_len = in.size() - overhead;
250 }
251
252 uint8_t ad_storage[13];
253 Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
254 seqnum, plaintext_len, header);
255
256 // Assemble the nonce.
257 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
258 size_t nonce_len = 0;
259
260 // Prepend the fixed nonce, or left-pad with zeros if XORing.
261 if (xor_fixed_nonce_) {
262 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
263 OPENSSL_memset(nonce, 0, nonce_len);
264 } else {
265 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
266 nonce_len += fixed_nonce_len_;
267 }
268
269 // Add the variable nonce.
270 if (variable_nonce_included_in_record_) {
271 if (in.size() < variable_nonce_len_) {
272 // Publicly invalid.
273 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH);
274 return false;
275 }
276 OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_);
277 in = in.subspan(variable_nonce_len_);
278 } else {
279 assert(variable_nonce_len_ == 8);
280 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
281 }
282 nonce_len += variable_nonce_len_;
283
284 // XOR the fixed nonce, if necessary.
285 if (xor_fixed_nonce_) {
286 assert(nonce_len == fixed_nonce_len_);
287 for (size_t i = 0; i < fixed_nonce_len_; i++) {
288 nonce[i] ^= fixed_nonce_[i];
289 }
290 }
291
292 // Decrypt in-place.
293 size_t len;
294 if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce,
295 nonce_len, in.data(), in.size(), ad.data(),
296 ad.size())) {
297 return false;
298 }
299 *out = in.subspan(0, len);
300 return true;
301 }
302
303 bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out,
304 uint8_t *out_suffix, uint8_t type,
305 uint16_t record_version,
306 const uint8_t seqnum[8],
307 Span<const uint8_t> header, const uint8_t *in,
308 size_t in_len, const uint8_t *extra_in,
309 size_t extra_in_len) {
310 const size_t prefix_len = ExplicitNonceLen();
311 size_t suffix_len;
312 if (!SuffixLen(&suffix_len, in_len, extra_in_len)) {
313 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
314 return false;
315 }
316 if ((in != out && buffers_alias(in, in_len, out, in_len)) ||
317 buffers_alias(in, in_len, out_prefix, prefix_len) ||
318 buffers_alias(in, in_len, out_suffix, suffix_len)) {
319 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
320 return false;
321 }
322
323 if (is_null_cipher() || FUZZER_MODE) {
324 // Handle the initial NULL cipher.
325 OPENSSL_memmove(out, in, in_len);
326 OPENSSL_memmove(out_suffix, extra_in, extra_in_len);
327 return true;
328 }
329
330 uint8_t ad_storage[13];
331 Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version,
332 seqnum, in_len, header);
333
334 // Assemble the nonce.
335 uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH];
336 size_t nonce_len = 0;
337
338 // Prepend the fixed nonce, or left-pad with zeros if XORing.
339 if (xor_fixed_nonce_) {
340 nonce_len = fixed_nonce_len_ - variable_nonce_len_;
341 OPENSSL_memset(nonce, 0, nonce_len);
342 } else {
343 OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_);
344 nonce_len += fixed_nonce_len_;
345 }
346
347 // Select the variable nonce.
348 if (random_variable_nonce_) {
349 assert(variable_nonce_included_in_record_);
350 if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) {
351 return false;
352 }
353 } else {
354 // When sending we use the sequence number as the variable part of the
355 // nonce.
356 assert(variable_nonce_len_ == 8);
357 OPENSSL_memcpy(nonce + nonce_len, seqnum, variable_nonce_len_);
358 }
359 nonce_len += variable_nonce_len_;
360
361 // Emit the variable nonce if included in the record.
362 if (variable_nonce_included_in_record_) {
363 assert(!xor_fixed_nonce_);
364 if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) {
365 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
366 return false;
367 }
368 OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_, variable_nonce_len_);
369 }
370
371 // XOR the fixed nonce, if necessary.
372 if (xor_fixed_nonce_) {
373 assert(nonce_len == fixed_nonce_len_);
374 for (size_t i = 0; i < fixed_nonce_len_; i++) {
375 nonce[i] ^= fixed_nonce_[i];
376 }
377 }
378
379 size_t written_suffix_len;
380 bool result = !!EVP_AEAD_CTX_seal_scatter(
381 ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce,
382 nonce_len, in, in_len, extra_in, extra_in_len, ad.data(), ad.size());
383 assert(!result || written_suffix_len == suffix_len);
384 return result;
385 }
386
387 bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len,
388 uint8_t type, uint16_t record_version,
389 const uint8_t seqnum[8], Span<const uint8_t> header,
390 const uint8_t *in, size_t in_len) {
391 const size_t prefix_len = ExplicitNonceLen();
392 size_t suffix_len;
393 if (!SuffixLen(&suffix_len, in_len, 0)) {
394 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
395 return false;
396 }
397 if (in_len + prefix_len < in_len ||
398 in_len + prefix_len + suffix_len < in_len + prefix_len) {
399 OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE);
400 return false;
401 }
402 if (in_len + prefix_len + suffix_len > max_out_len) {
403 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
404 return false;
405 }
406
407 if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type,
408 record_version, seqnum, header, in, in_len, 0, 0)) {
409 return false;
410 }
411 *out_len = prefix_len + in_len + suffix_len;
412 return true;
413 }
414
415 bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const {
416 return !is_null_cipher() &&
417 EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len);
418 }
419
420 #define SSLAEADCONTEXT_SERDE_VERSION 1
421
422 /*
423 * SSLAEADContextVersion ::= INTEGER {v1 (1)}
424 *
425 * SSLAEADContext ::= SEQUENCE {
426 * serializationVersion SSLAEADContextVersion,
427 * cipher INTEGER,
428 * cipherState EvpAeadCtxState
429 * }
430 */
431 int SSLAEADContext::SerializeState(CBB *cbb) const {
432 uint32_t cipher_id = SSL_CIPHER_get_id(cipher_);
433
434 CBB seq;
435
436 if (!CBB_add_asn1(cbb, &seq, CBS_ASN1_SEQUENCE) ||
437 !CBB_add_asn1_uint64(&seq, SSLAEADCONTEXT_SERDE_VERSION) ||
438 !CBB_add_asn1_uint64(&seq, cipher_id)) {
439 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
440 return 0;
441 }
442
443 if (!EVP_AEAD_CTX_serialize_state(ctx_.get(), &seq)) {
444 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
445 return 0;
446 }
447
448 return CBB_flush(cbb);
449 }
450
451 // See |SSLAEADContext::SerializeState| for a description of the serialization
452 // format.
453 int SSLAEADContext::DeserializeState(CBS *cbs) const {
454 CBS seq;
455
456 uint64_t serde_version;
457 uint64_t cipher_id;
458
459 if (!CBS_get_asn1(cbs, &seq, CBS_ASN1_SEQUENCE) ||
460 !CBS_get_asn1_uint64(&seq, &serde_version) ||
461 serde_version != SSLAEADCONTEXT_SERDE_VERSION ||
462 !CBS_get_asn1_uint64(&seq, &cipher_id) || cipher_id > UINT32_MAX ||
463 cipher_id != SSL_CIPHER_get_id(cipher_)) {
464 OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_AEAD_CONTEXT);
465 return 0;
466 }
467
468 if (!EVP_AEAD_CTX_deserialize_state(ctx_.get(), &seq)) {
469 OPENSSL_PUT_ERROR(SSL, SSL_R_SERIALIZATION_INVALID_SSL_AEAD_CONTEXT);
470 return 0;
471 }
472
473 return 1;
474 }
475
476 BSSL_NAMESPACE_END