/src/boringssl/ssl/ssl_aead_ctx.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2015, Google Inc. |
2 | | * |
3 | | * Permission to use, copy, modify, and/or distribute this software for any |
4 | | * purpose with or without fee is hereby granted, provided that the above |
5 | | * copyright notice and this permission notice appear in all copies. |
6 | | * |
7 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
8 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
10 | | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
14 | | |
15 | | #include <openssl/ssl.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include <openssl/aead.h> |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/rand.h> |
23 | | |
24 | | #include "../crypto/internal.h" |
25 | | #include "internal.h" |
26 | | |
27 | | |
28 | | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
29 | 7.08M | #define FUZZER_MODE true |
30 | | #else |
31 | | #define FUZZER_MODE false |
32 | | #endif |
33 | | |
34 | | BSSL_NAMESPACE_BEGIN |
35 | | |
36 | | SSLAEADContext::SSLAEADContext(uint16_t version_arg, bool is_dtls_arg, |
37 | | const SSL_CIPHER *cipher_arg) |
38 | | : cipher_(cipher_arg), |
39 | | version_(version_arg), |
40 | | is_dtls_(is_dtls_arg), |
41 | | variable_nonce_included_in_record_(false), |
42 | | random_variable_nonce_(false), |
43 | | xor_fixed_nonce_(false), |
44 | | omit_length_in_ad_(false), |
45 | 270k | ad_is_header_(false) { |
46 | 270k | OPENSSL_memset(fixed_nonce_, 0, sizeof(fixed_nonce_)); |
47 | 270k | } |
48 | | |
49 | 270k | SSLAEADContext::~SSLAEADContext() {} |
50 | | |
51 | 144k | UniquePtr<SSLAEADContext> SSLAEADContext::CreateNullCipher(bool is_dtls) { |
52 | 144k | return MakeUnique<SSLAEADContext>(0 /* version */, is_dtls, |
53 | 144k | nullptr /* cipher */); |
54 | 144k | } |
55 | | |
56 | | UniquePtr<SSLAEADContext> SSLAEADContext::Create( |
57 | | enum evp_aead_direction_t direction, uint16_t version, bool is_dtls, |
58 | | const SSL_CIPHER *cipher, Span<const uint8_t> enc_key, |
59 | 125k | Span<const uint8_t> mac_key, Span<const uint8_t> fixed_iv) { |
60 | 125k | const EVP_AEAD *aead; |
61 | 125k | uint16_t protocol_version; |
62 | 125k | size_t expected_mac_key_len, expected_fixed_iv_len; |
63 | 125k | if (!ssl_protocol_version_from_wire(&protocol_version, version) || |
64 | 125k | !ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len, |
65 | 125k | &expected_fixed_iv_len, cipher, protocol_version, |
66 | 125k | is_dtls) || |
67 | | // Ensure the caller returned correct key sizes. |
68 | 125k | expected_fixed_iv_len != fixed_iv.size() || |
69 | 125k | expected_mac_key_len != mac_key.size()) { |
70 | 8 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
71 | 8 | return nullptr; |
72 | 8 | } |
73 | | |
74 | 125k | uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH]; |
75 | 125k | if (!mac_key.empty()) { |
76 | | // This is a "stateful" AEAD (for compatibility with pre-AEAD cipher |
77 | | // suites). |
78 | 85.6k | if (mac_key.size() + enc_key.size() + fixed_iv.size() > |
79 | 85.6k | sizeof(merged_key)) { |
80 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
81 | 0 | return nullptr; |
82 | 0 | } |
83 | 85.6k | OPENSSL_memcpy(merged_key, mac_key.data(), mac_key.size()); |
84 | 85.6k | OPENSSL_memcpy(merged_key + mac_key.size(), enc_key.data(), enc_key.size()); |
85 | 85.6k | OPENSSL_memcpy(merged_key + mac_key.size() + enc_key.size(), |
86 | 85.6k | fixed_iv.data(), fixed_iv.size()); |
87 | 85.6k | enc_key = MakeConstSpan(merged_key, |
88 | 85.6k | enc_key.size() + mac_key.size() + fixed_iv.size()); |
89 | 85.6k | } |
90 | | |
91 | 125k | UniquePtr<SSLAEADContext> aead_ctx = |
92 | 125k | MakeUnique<SSLAEADContext>(version, is_dtls, cipher); |
93 | 125k | if (!aead_ctx) { |
94 | 0 | return nullptr; |
95 | 0 | } |
96 | | |
97 | 125k | assert(aead_ctx->ProtocolVersion() == protocol_version); |
98 | | |
99 | 125k | if (!EVP_AEAD_CTX_init_with_direction( |
100 | 125k | aead_ctx->ctx_.get(), aead, enc_key.data(), enc_key.size(), |
101 | 125k | EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) { |
102 | 0 | return nullptr; |
103 | 0 | } |
104 | | |
105 | 125k | assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH); |
106 | 0 | static_assert(EVP_AEAD_MAX_NONCE_LENGTH < 256, |
107 | 125k | "variable_nonce_len doesn't fit in uint8_t"); |
108 | 125k | aead_ctx->variable_nonce_len_ = (uint8_t)EVP_AEAD_nonce_length(aead); |
109 | 125k | if (mac_key.empty()) { |
110 | 39.8k | assert(fixed_iv.size() <= sizeof(aead_ctx->fixed_nonce_)); |
111 | 0 | OPENSSL_memcpy(aead_ctx->fixed_nonce_, fixed_iv.data(), fixed_iv.size()); |
112 | 39.8k | aead_ctx->fixed_nonce_len_ = fixed_iv.size(); |
113 | | |
114 | 39.8k | if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) { |
115 | | // The fixed nonce into the actual nonce (the sequence number). |
116 | 4.91k | aead_ctx->xor_fixed_nonce_ = true; |
117 | 4.91k | aead_ctx->variable_nonce_len_ = 8; |
118 | 34.9k | } else { |
119 | | // The fixed IV is prepended to the nonce. |
120 | 34.9k | assert(fixed_iv.size() <= aead_ctx->variable_nonce_len_); |
121 | 0 | aead_ctx->variable_nonce_len_ -= fixed_iv.size(); |
122 | 34.9k | } |
123 | | |
124 | | // AES-GCM uses an explicit nonce. |
125 | 39.8k | if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) { |
126 | 34.9k | aead_ctx->variable_nonce_included_in_record_ = true; |
127 | 34.9k | } |
128 | | |
129 | | // The TLS 1.3 construction XORs the fixed nonce into the sequence number |
130 | | // and omits the additional data. |
131 | 39.8k | if (protocol_version >= TLS1_3_VERSION) { |
132 | 8.60k | aead_ctx->xor_fixed_nonce_ = true; |
133 | 8.60k | aead_ctx->variable_nonce_len_ = 8; |
134 | 8.60k | aead_ctx->variable_nonce_included_in_record_ = false; |
135 | 8.60k | aead_ctx->ad_is_header_ = true; |
136 | 8.60k | assert(fixed_iv.size() >= aead_ctx->variable_nonce_len_); |
137 | 8.60k | } |
138 | 85.6k | } else { |
139 | 85.6k | assert(protocol_version < TLS1_3_VERSION); |
140 | 0 | aead_ctx->variable_nonce_included_in_record_ = true; |
141 | 85.6k | aead_ctx->random_variable_nonce_ = true; |
142 | 85.6k | aead_ctx->omit_length_in_ad_ = true; |
143 | 85.6k | } |
144 | | |
145 | 0 | return aead_ctx; |
146 | 125k | } |
147 | | |
148 | | UniquePtr<SSLAEADContext> SSLAEADContext::CreatePlaceholderForQUIC( |
149 | 0 | uint16_t version, const SSL_CIPHER *cipher) { |
150 | 0 | return MakeUnique<SSLAEADContext>(version, false, cipher); |
151 | 0 | } |
152 | | |
153 | 14.0k | void SSLAEADContext::SetVersionIfNullCipher(uint16_t version) { |
154 | 14.0k | if (is_null_cipher()) { |
155 | 14.0k | version_ = version; |
156 | 14.0k | } |
157 | 14.0k | } |
158 | | |
159 | 2.61M | uint16_t SSLAEADContext::ProtocolVersion() const { |
160 | 2.61M | uint16_t protocol_version; |
161 | 2.61M | if(!ssl_protocol_version_from_wire(&protocol_version, version_)) { |
162 | 0 | assert(false); |
163 | 0 | return 0; |
164 | 0 | } |
165 | 2.61M | return protocol_version; |
166 | 2.61M | } |
167 | | |
168 | 1.29M | uint16_t SSLAEADContext::RecordVersion() const { |
169 | 1.29M | if (version_ == 0) { |
170 | 13.0k | assert(is_null_cipher()); |
171 | 13.0k | return is_dtls_ ? DTLS1_VERSION : TLS1_VERSION; |
172 | 13.0k | } |
173 | | |
174 | 1.28M | if (ProtocolVersion() <= TLS1_2_VERSION) { |
175 | 1.24M | return version_; |
176 | 1.24M | } |
177 | | |
178 | 38.0k | return TLS1_2_VERSION; |
179 | 1.28M | } |
180 | | |
181 | 2.59M | size_t SSLAEADContext::ExplicitNonceLen() const { |
182 | 2.59M | if (!FUZZER_MODE && variable_nonce_included_in_record_) { |
183 | 0 | return variable_nonce_len_; |
184 | 0 | } |
185 | 2.59M | return 0; |
186 | 2.59M | } |
187 | | |
188 | | bool SSLAEADContext::SuffixLen(size_t *out_suffix_len, const size_t in_len, |
189 | 1.10M | const size_t extra_in_len) const { |
190 | 1.10M | if (is_null_cipher() || FUZZER_MODE) { |
191 | 1.10M | *out_suffix_len = extra_in_len; |
192 | 1.10M | return true; |
193 | 1.10M | } |
194 | 0 | return !!EVP_AEAD_CTX_tag_len(ctx_.get(), out_suffix_len, in_len, |
195 | 0 | extra_in_len); |
196 | 1.10M | } |
197 | | |
198 | | bool SSLAEADContext::CiphertextLen(size_t *out_len, const size_t in_len, |
199 | 280k | const size_t extra_in_len) const { |
200 | 280k | size_t len; |
201 | 280k | if (!SuffixLen(&len, in_len, extra_in_len)) { |
202 | 0 | return false; |
203 | 0 | } |
204 | 280k | len += ExplicitNonceLen(); |
205 | 280k | len += in_len; |
206 | 280k | if (len < in_len || len >= 0xffff) { |
207 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
208 | 0 | return false; |
209 | 0 | } |
210 | 280k | *out_len = len; |
211 | 280k | return true; |
212 | 280k | } |
213 | | |
214 | 280k | size_t SSLAEADContext::MaxOverhead() const { |
215 | 280k | return ExplicitNonceLen() + |
216 | 280k | (is_null_cipher() || FUZZER_MODE |
217 | 280k | ? 0 |
218 | 280k | : EVP_AEAD_max_overhead(EVP_AEAD_CTX_aead(ctx_.get()))); |
219 | 280k | } |
220 | | |
221 | | Span<const uint8_t> SSLAEADContext::GetAdditionalData( |
222 | | uint8_t storage[13], uint8_t type, uint16_t record_version, uint64_t seqnum, |
223 | 0 | size_t plaintext_len, Span<const uint8_t> header) { |
224 | 0 | if (ad_is_header_) { |
225 | 0 | return header; |
226 | 0 | } |
227 | | |
228 | 0 | CRYPTO_store_u64_be(storage, seqnum); |
229 | 0 | size_t len = 8; |
230 | 0 | storage[len++] = type; |
231 | 0 | storage[len++] = static_cast<uint8_t>((record_version >> 8)); |
232 | 0 | storage[len++] = static_cast<uint8_t>(record_version); |
233 | 0 | if (!omit_length_in_ad_) { |
234 | 0 | storage[len++] = static_cast<uint8_t>((plaintext_len >> 8)); |
235 | 0 | storage[len++] = static_cast<uint8_t>(plaintext_len); |
236 | 0 | } |
237 | 0 | return MakeConstSpan(storage, len); |
238 | 0 | } |
239 | | |
240 | | bool SSLAEADContext::Open(Span<uint8_t> *out, uint8_t type, |
241 | | uint16_t record_version, uint64_t seqnum, |
242 | 599k | Span<const uint8_t> header, Span<uint8_t> in) { |
243 | 599k | if (is_null_cipher() || FUZZER_MODE) { |
244 | | // Handle the initial NULL cipher. |
245 | 599k | *out = in; |
246 | 599k | return true; |
247 | 599k | } |
248 | | |
249 | | // TLS 1.2 AEADs include the length in the AD and are assumed to have fixed |
250 | | // overhead. Otherwise the parameter is unused. |
251 | 0 | size_t plaintext_len = 0; |
252 | 0 | if (!omit_length_in_ad_) { |
253 | 0 | size_t overhead = MaxOverhead(); |
254 | 0 | if (in.size() < overhead) { |
255 | | // Publicly invalid. |
256 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
257 | 0 | return false; |
258 | 0 | } |
259 | 0 | plaintext_len = in.size() - overhead; |
260 | 0 | } |
261 | | |
262 | 0 | uint8_t ad_storage[13]; |
263 | 0 | Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version, |
264 | 0 | seqnum, plaintext_len, header); |
265 | | |
266 | | // Assemble the nonce. |
267 | 0 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
268 | 0 | size_t nonce_len = 0; |
269 | | |
270 | | // Prepend the fixed nonce, or left-pad with zeros if XORing. |
271 | 0 | if (xor_fixed_nonce_) { |
272 | 0 | nonce_len = fixed_nonce_len_ - variable_nonce_len_; |
273 | 0 | OPENSSL_memset(nonce, 0, nonce_len); |
274 | 0 | } else { |
275 | 0 | OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_); |
276 | 0 | nonce_len += fixed_nonce_len_; |
277 | 0 | } |
278 | | |
279 | | // Add the variable nonce. |
280 | 0 | if (variable_nonce_included_in_record_) { |
281 | 0 | if (in.size() < variable_nonce_len_) { |
282 | | // Publicly invalid. |
283 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
284 | 0 | return false; |
285 | 0 | } |
286 | 0 | OPENSSL_memcpy(nonce + nonce_len, in.data(), variable_nonce_len_); |
287 | 0 | in = in.subspan(variable_nonce_len_); |
288 | 0 | } else { |
289 | 0 | assert(variable_nonce_len_ == 8); |
290 | 0 | CRYPTO_store_u64_be(nonce + nonce_len, seqnum); |
291 | 0 | } |
292 | 0 | nonce_len += variable_nonce_len_; |
293 | | |
294 | | // XOR the fixed nonce, if necessary. |
295 | 0 | if (xor_fixed_nonce_) { |
296 | 0 | assert(nonce_len == fixed_nonce_len_); |
297 | 0 | for (size_t i = 0; i < fixed_nonce_len_; i++) { |
298 | 0 | nonce[i] ^= fixed_nonce_[i]; |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | // Decrypt in-place. |
303 | 0 | size_t len; |
304 | 0 | if (!EVP_AEAD_CTX_open(ctx_.get(), in.data(), &len, in.size(), nonce, |
305 | 0 | nonce_len, in.data(), in.size(), ad.data(), |
306 | 0 | ad.size())) { |
307 | 0 | return false; |
308 | 0 | } |
309 | 0 | *out = in.subspan(0, len); |
310 | 0 | return true; |
311 | 0 | } |
312 | | |
313 | | bool SSLAEADContext::SealScatter(uint8_t *out_prefix, uint8_t *out, |
314 | | uint8_t *out_suffix, uint8_t type, |
315 | | uint16_t record_version, uint64_t seqnum, |
316 | | Span<const uint8_t> header, const uint8_t *in, |
317 | | size_t in_len, const uint8_t *extra_in, |
318 | 280k | size_t extra_in_len) { |
319 | 280k | const size_t prefix_len = ExplicitNonceLen(); |
320 | 280k | size_t suffix_len; |
321 | 280k | if (!SuffixLen(&suffix_len, in_len, extra_in_len)) { |
322 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
323 | 0 | return false; |
324 | 0 | } |
325 | 280k | if ((in != out && buffers_alias(in, in_len, out, in_len)) || |
326 | 280k | buffers_alias(in, in_len, out_prefix, prefix_len) || |
327 | 280k | buffers_alias(in, in_len, out_suffix, suffix_len)) { |
328 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
329 | 0 | return false; |
330 | 0 | } |
331 | | |
332 | 280k | if (is_null_cipher() || FUZZER_MODE) { |
333 | | // Handle the initial NULL cipher. |
334 | 280k | OPENSSL_memmove(out, in, in_len); |
335 | 280k | OPENSSL_memmove(out_suffix, extra_in, extra_in_len); |
336 | 280k | return true; |
337 | 280k | } |
338 | | |
339 | 0 | uint8_t ad_storage[13]; |
340 | 0 | Span<const uint8_t> ad = GetAdditionalData(ad_storage, type, record_version, |
341 | 0 | seqnum, in_len, header); |
342 | | |
343 | | // Assemble the nonce. |
344 | 0 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
345 | 0 | size_t nonce_len = 0; |
346 | | |
347 | | // Prepend the fixed nonce, or left-pad with zeros if XORing. |
348 | 0 | if (xor_fixed_nonce_) { |
349 | 0 | nonce_len = fixed_nonce_len_ - variable_nonce_len_; |
350 | 0 | OPENSSL_memset(nonce, 0, nonce_len); |
351 | 0 | } else { |
352 | 0 | OPENSSL_memcpy(nonce, fixed_nonce_, fixed_nonce_len_); |
353 | 0 | nonce_len += fixed_nonce_len_; |
354 | 0 | } |
355 | | |
356 | | // Select the variable nonce. |
357 | 0 | if (random_variable_nonce_) { |
358 | 0 | assert(variable_nonce_included_in_record_); |
359 | 0 | if (!RAND_bytes(nonce + nonce_len, variable_nonce_len_)) { |
360 | 0 | return false; |
361 | 0 | } |
362 | 0 | } else { |
363 | | // When sending we use the sequence number as the variable part of the |
364 | | // nonce. |
365 | 0 | assert(variable_nonce_len_ == 8); |
366 | 0 | CRYPTO_store_u64_be(nonce + nonce_len, seqnum); |
367 | 0 | } |
368 | 0 | nonce_len += variable_nonce_len_; |
369 | | |
370 | | // Emit the variable nonce if included in the record. |
371 | 0 | if (variable_nonce_included_in_record_) { |
372 | 0 | assert(!xor_fixed_nonce_); |
373 | 0 | if (buffers_alias(in, in_len, out_prefix, variable_nonce_len_)) { |
374 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
375 | 0 | return false; |
376 | 0 | } |
377 | 0 | OPENSSL_memcpy(out_prefix, nonce + fixed_nonce_len_, |
378 | 0 | variable_nonce_len_); |
379 | 0 | } |
380 | | |
381 | | // XOR the fixed nonce, if necessary. |
382 | 0 | if (xor_fixed_nonce_) { |
383 | 0 | assert(nonce_len == fixed_nonce_len_); |
384 | 0 | for (size_t i = 0; i < fixed_nonce_len_; i++) { |
385 | 0 | nonce[i] ^= fixed_nonce_[i]; |
386 | 0 | } |
387 | 0 | } |
388 | | |
389 | 0 | size_t written_suffix_len; |
390 | 0 | bool result = !!EVP_AEAD_CTX_seal_scatter( |
391 | 0 | ctx_.get(), out, out_suffix, &written_suffix_len, suffix_len, nonce, |
392 | 0 | nonce_len, in, in_len, extra_in, extra_in_len, ad.data(), ad.size()); |
393 | 0 | assert(!result || written_suffix_len == suffix_len); |
394 | 0 | return result; |
395 | 0 | } |
396 | | |
397 | | bool SSLAEADContext::Seal(uint8_t *out, size_t *out_len, size_t max_out_len, |
398 | | uint8_t type, uint16_t record_version, |
399 | | uint64_t seqnum, Span<const uint8_t> header, |
400 | 20.9k | const uint8_t *in, size_t in_len) { |
401 | 20.9k | const size_t prefix_len = ExplicitNonceLen(); |
402 | 20.9k | size_t suffix_len; |
403 | 20.9k | if (!SuffixLen(&suffix_len, in_len, 0)) { |
404 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
405 | 0 | return false; |
406 | 0 | } |
407 | 20.9k | if (in_len + prefix_len < in_len || |
408 | 20.9k | in_len + prefix_len + suffix_len < in_len + prefix_len) { |
409 | 0 | OPENSSL_PUT_ERROR(CIPHER, SSL_R_RECORD_TOO_LARGE); |
410 | 0 | return false; |
411 | 0 | } |
412 | 20.9k | if (in_len + prefix_len + suffix_len > max_out_len) { |
413 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
414 | 0 | return false; |
415 | 0 | } |
416 | | |
417 | 20.9k | if (!SealScatter(out, out + prefix_len, out + prefix_len + in_len, type, |
418 | 20.9k | record_version, seqnum, header, in, in_len, 0, 0)) { |
419 | 0 | return false; |
420 | 0 | } |
421 | 20.9k | *out_len = prefix_len + in_len + suffix_len; |
422 | 20.9k | return true; |
423 | 20.9k | } |
424 | | |
425 | 0 | bool SSLAEADContext::GetIV(const uint8_t **out_iv, size_t *out_iv_len) const { |
426 | 0 | return !is_null_cipher() && |
427 | 0 | EVP_AEAD_CTX_get_iv(ctx_.get(), out_iv, out_iv_len); |
428 | 0 | } |
429 | | |
430 | | BSSL_NAMESPACE_END |