1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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 <openssl/bytestring.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 
24 #include "../crypto/internal.h"
25 #include "internal.h"
26 
27 
28 BSSL_NAMESPACE_BEGIN
29 
30 // kMaxEmptyRecords is the number of consecutive, empty records that will be
31 // processed. Without this limit an attacker could send empty records at a
32 // faster rate than we can process and cause record processing to loop
33 // forever.
34 static const uint8_t kMaxEmptyRecords = 32;
35 
36 // kMaxEarlyDataSkipped is the maximum number of rejected early data bytes that
37 // will be skipped. Without this limit an attacker could send records at a
38 // faster rate than we can process and cause trial decryption to loop forever.
39 // This value should be slightly above kMaxEarlyDataAccepted, which is measured
40 // in plaintext.
41 static const size_t kMaxEarlyDataSkipped = 16384;
42 
43 // kMaxWarningAlerts is the number of consecutive warning alerts that will be
44 // processed.
45 static const uint8_t kMaxWarningAlerts = 4;
46 
47 // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
48 // state needs record-splitting and zero otherwise.
ssl_needs_record_splitting(const SSL * ssl)49 bool ssl_needs_record_splitting(const SSL *ssl) {
50   return !CRYPTO_fuzzer_mode_enabled() &&
51          !ssl->s3->aead_write_ctx->is_null_cipher() &&
52          ssl_protocol_version(ssl) < TLS1_1_VERSION &&
53          (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
54          SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher());
55 }
56 
ssl_record_prefix_len(const SSL * ssl)57 size_t ssl_record_prefix_len(const SSL *ssl) {
58   assert(!SSL_is_dtls(ssl));
59   return SSL3_RT_HEADER_LENGTH + ssl->s3->aead_read_ctx->ExplicitNonceLen();
60 }
61 
skip_early_data(SSL * ssl,uint8_t * out_alert,size_t consumed)62 static ssl_open_record_t skip_early_data(SSL *ssl, uint8_t *out_alert,
63                                          size_t consumed) {
64   ssl->s3->early_data_skipped += consumed;
65   if (ssl->s3->early_data_skipped < consumed) {
66     ssl->s3->early_data_skipped = kMaxEarlyDataSkipped + 1;
67   }
68 
69   if (ssl->s3->early_data_skipped > kMaxEarlyDataSkipped) {
70     OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA);
71     *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
72     return ssl_open_record_error;
73   }
74 
75   return ssl_open_record_discard;
76 }
77 
tls_record_version(const SSL * ssl)78 static uint16_t tls_record_version(const SSL *ssl) {
79   if (ssl->s3->version == 0) {
80     // Before the version is determined, outgoing records use TLS 1.0 for
81     // historical compatibility requirements.
82     return TLS1_VERSION;
83   }
84 
85   // TLS 1.3 freezes the record version at TLS 1.2. Previous ones use the
86   // version itself.
87   return ssl_protocol_version(ssl) >= TLS1_3_VERSION ? TLS1_2_VERSION
88                                                      : ssl->s3->version;
89 }
90 
tls_open_record(SSL * ssl,uint8_t * out_type,Span<uint8_t> * out,size_t * out_consumed,uint8_t * out_alert,Span<uint8_t> in)91 ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
92                                   Span<uint8_t> *out, size_t *out_consumed,
93                                   uint8_t *out_alert, Span<uint8_t> in) {
94   *out_consumed = 0;
95   if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
96     return ssl_open_record_close_notify;
97   }
98 
99   // If there is an unprocessed handshake message or we are already buffering
100   // too much, stop before decrypting another handshake record.
101   if (!tls_can_accept_handshake_data(ssl, out_alert)) {
102     return ssl_open_record_error;
103   }
104 
105   CBS cbs = CBS(in);
106 
107   // Decode the record header.
108   uint8_t type;
109   uint16_t version, ciphertext_len;
110   if (!CBS_get_u8(&cbs, &type) ||      //
111       !CBS_get_u16(&cbs, &version) ||  //
112       !CBS_get_u16(&cbs, &ciphertext_len)) {
113     *out_consumed = SSL3_RT_HEADER_LENGTH;
114     return ssl_open_record_partial;
115   }
116 
117   bool version_ok;
118   if (ssl->s3->aead_read_ctx->is_null_cipher()) {
119     // Only check the first byte. Enforcing beyond that can prevent decoding
120     // version negotiation failure alerts.
121     version_ok = (version >> 8) == SSL3_VERSION_MAJOR;
122   } else {
123     version_ok = version == tls_record_version(ssl);
124   }
125 
126   if (!version_ok) {
127     OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
128     *out_alert = SSL_AD_PROTOCOL_VERSION;
129     return ssl_open_record_error;
130   }
131 
132   // Check the ciphertext length.
133   if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
134     OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
135     *out_alert = SSL_AD_RECORD_OVERFLOW;
136     return ssl_open_record_error;
137   }
138 
139   // Extract the body.
140   CBS body;
141   if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
142     *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
143     return ssl_open_record_partial;
144   }
145 
146   Span<const uint8_t> header = in.subspan(0, SSL3_RT_HEADER_LENGTH);
147   ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header);
148 
149   *out_consumed = in.size() - CBS_len(&cbs);
150 
151   // In TLS 1.3, during the handshake, skip ChangeCipherSpec records.
152   static const uint8_t kChangeCipherSpec[] = {SSL3_MT_CCS};
153   if (ssl_has_final_version(ssl) &&
154       ssl_protocol_version(ssl) >= TLS1_3_VERSION && SSL_in_init(ssl) &&
155       type == SSL3_RT_CHANGE_CIPHER_SPEC &&
156       Span<const uint8_t>(body) == kChangeCipherSpec) {
157     ssl->s3->empty_record_count++;
158     if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
159       OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
160       *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
161       return ssl_open_record_error;
162     }
163     return ssl_open_record_discard;
164   }
165 
166   // Skip early data received when expecting a second ClientHello if we rejected
167   // 0RTT.
168   if (ssl->s3->skip_early_data &&                  //
169       ssl->s3->aead_read_ctx->is_null_cipher() &&  //
170       type == SSL3_RT_APPLICATION_DATA) {
171     return skip_early_data(ssl, out_alert, *out_consumed);
172   }
173 
174   // Ensure the sequence number update does not overflow.
175   if (ssl->s3->read_sequence + 1 == 0) {
176     OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
177     *out_alert = SSL_AD_INTERNAL_ERROR;
178     return ssl_open_record_error;
179   }
180 
181   // Decrypt the body in-place.
182   if (!ssl->s3->aead_read_ctx->Open(
183           out, type, version, ssl->s3->read_sequence, header,
184           Span(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) {
185     if (ssl->s3->skip_early_data && !ssl->s3->aead_read_ctx->is_null_cipher()) {
186       ERR_clear_error();
187       return skip_early_data(ssl, out_alert, *out_consumed);
188     }
189 
190     OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
191     *out_alert = SSL_AD_BAD_RECORD_MAC;
192     return ssl_open_record_error;
193   }
194 
195   ssl->s3->skip_early_data = false;
196   ssl->s3->read_sequence++;
197 
198   // TLS 1.3 hides the record type inside the encrypted data.
199   bool has_padding = !ssl->s3->aead_read_ctx->is_null_cipher() &&
200                      ssl_protocol_version(ssl) >= TLS1_3_VERSION;
201 
202   // If there is padding, the plaintext limit includes the padding, but includes
203   // extra room for the inner content type.
204   size_t plaintext_limit =
205       has_padding ? SSL3_RT_MAX_PLAIN_LENGTH + 1 : SSL3_RT_MAX_PLAIN_LENGTH;
206   if (out->size() > plaintext_limit) {
207     OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
208     *out_alert = SSL_AD_RECORD_OVERFLOW;
209     return ssl_open_record_error;
210   }
211 
212   if (has_padding) {
213     // The outer record type is always application_data.
214     if (type != SSL3_RT_APPLICATION_DATA) {
215       OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE);
216       *out_alert = SSL_AD_DECODE_ERROR;
217       return ssl_open_record_error;
218     }
219 
220     do {
221       if (out->empty()) {
222         OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
223         *out_alert = SSL_AD_DECRYPT_ERROR;
224         return ssl_open_record_error;
225       }
226       type = out->back();
227       *out = out->subspan(0, out->size() - 1);
228     } while (type == 0);
229   }
230 
231   // Limit the number of consecutive empty records.
232   if (out->empty()) {
233     ssl->s3->empty_record_count++;
234     if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
235       OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
236       *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
237       return ssl_open_record_error;
238     }
239     // Apart from the limit, empty records are returned up to the caller. This
240     // allows the caller to reject records of the wrong type.
241   } else {
242     ssl->s3->empty_record_count = 0;
243   }
244 
245   if (type == SSL3_RT_ALERT) {
246     return ssl_process_alert(ssl, out_alert, *out);
247   }
248 
249   // Handshake messages may not interleave with any other record type.
250   if (type != SSL3_RT_HANDSHAKE &&  //
251       tls_has_unprocessed_handshake_data(ssl)) {
252     OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
253     *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
254     return ssl_open_record_error;
255   }
256 
257   ssl->s3->warning_alert_count = 0;
258 
259   *out_type = type;
260   return ssl_open_record_success;
261 }
262 
do_seal_record(SSL * ssl,uint8_t * out_prefix,uint8_t * out,uint8_t * out_suffix,uint8_t type,const uint8_t * in,const size_t in_len)263 static bool do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
264                            uint8_t *out_suffix, uint8_t type, const uint8_t *in,
265                            const size_t in_len) {
266   SSLAEADContext *aead = ssl->s3->aead_write_ctx.get();
267   uint8_t *extra_in = NULL;
268   size_t extra_in_len = 0;
269   if (!aead->is_null_cipher() && ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
270     // TLS 1.3 hides the actual record type inside the encrypted data.
271     extra_in = &type;
272     extra_in_len = 1;
273   }
274 
275   size_t suffix_len, ciphertext_len;
276   if (!aead->SuffixLen(&suffix_len, in_len, extra_in_len) ||
277       !aead->CiphertextLen(&ciphertext_len, in_len, extra_in_len)) {
278     OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
279     return false;
280   }
281 
282   assert(in == out || !buffers_alias(in, in_len, out, in_len));
283   assert(!buffers_alias(in, in_len, out_prefix, ssl_record_prefix_len(ssl)));
284   assert(!buffers_alias(in, in_len, out_suffix, suffix_len));
285 
286   if (extra_in_len) {
287     out_prefix[0] = SSL3_RT_APPLICATION_DATA;
288   } else {
289     out_prefix[0] = type;
290   }
291 
292   uint16_t record_version = tls_record_version(ssl);
293   out_prefix[1] = record_version >> 8;
294   out_prefix[2] = record_version & 0xff;
295   out_prefix[3] = ciphertext_len >> 8;
296   out_prefix[4] = ciphertext_len & 0xff;
297   Span<const uint8_t> header = Span(out_prefix, SSL3_RT_HEADER_LENGTH);
298 
299   // Ensure the sequence number update does not overflow.
300   if (ssl->s3->write_sequence + 1 == 0) {
301     OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
302     return false;
303   }
304 
305   if (!aead->SealScatter(out_prefix + SSL3_RT_HEADER_LENGTH, out, out_suffix,
306                          out_prefix[0], record_version, ssl->s3->write_sequence,
307                          header, in, in_len, extra_in, extra_in_len)) {
308     return false;
309   }
310 
311   ssl->s3->write_sequence++;
312   ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header);
313   return true;
314 }
315 
tls_seal_scatter_prefix_len(const SSL * ssl,uint8_t type,size_t in_len)316 static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type,
317                                           size_t in_len) {
318   size_t ret = SSL3_RT_HEADER_LENGTH;
319   if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
320       ssl_needs_record_splitting(ssl)) {
321     // In the case of record splitting, the 1-byte record (of the 1/n-1 split)
322     // will be placed in the prefix, as will four of the five bytes of the
323     // record header for the main record. The final byte will replace the first
324     // byte of the plaintext that was used in the small record.
325     ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher());
326     ret += SSL3_RT_HEADER_LENGTH - 1;
327   } else {
328     ret += ssl->s3->aead_write_ctx->ExplicitNonceLen();
329   }
330   return ret;
331 }
332 
tls_seal_scatter_suffix_len(const SSL * ssl,size_t * out_suffix_len,uint8_t type,size_t in_len)333 static bool tls_seal_scatter_suffix_len(const SSL *ssl, size_t *out_suffix_len,
334                                         uint8_t type, size_t in_len) {
335   size_t extra_in_len = 0;
336   if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
337       ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
338     // TLS 1.3 adds an extra byte for encrypted record type.
339     extra_in_len = 1;
340   }
341   // clang-format off
342   if (type == SSL3_RT_APPLICATION_DATA &&
343       in_len > 1 &&
344       ssl_needs_record_splitting(ssl)) {
345     // With record splitting enabled, the first byte gets sealed into a separate
346     // record which is written into the prefix.
347     in_len -= 1;
348   }
349   // clang-format on
350   return ssl->s3->aead_write_ctx->SuffixLen(out_suffix_len, in_len,
351                                             extra_in_len);
352 }
353 
354 // tls_seal_scatter_record seals a new record of type |type| and body |in| and
355 // splits it between |out_prefix|, |out|, and |out_suffix|. Exactly
356 // |tls_seal_scatter_prefix_len| bytes are written to |out_prefix|, |in_len|
357 // bytes to |out|, and |tls_seal_scatter_suffix_len| bytes to |out_suffix|. It
358 // returns one on success and zero on error. If enabled,
359 // |tls_seal_scatter_record| implements TLS 1.0 CBC 1/n-1 record splitting and
360 // may write two records concatenated.
tls_seal_scatter_record(SSL * ssl,uint8_t * out_prefix,uint8_t * out,uint8_t * out_suffix,uint8_t type,const uint8_t * in,size_t in_len)361 static bool tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
362                                     uint8_t *out_suffix, uint8_t type,
363                                     const uint8_t *in, size_t in_len) {
364   if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
365       ssl_needs_record_splitting(ssl)) {
366     assert(ssl->s3->aead_write_ctx->ExplicitNonceLen() == 0);
367     const size_t prefix_len = SSL3_RT_HEADER_LENGTH;
368 
369     // Write the 1-byte fragment into |out_prefix|.
370     uint8_t *split_body = out_prefix + prefix_len;
371     uint8_t *split_suffix = split_body + 1;
372 
373     if (!do_seal_record(ssl, out_prefix, split_body, split_suffix, type, in,
374                         1)) {
375       return false;
376     }
377 
378     size_t split_record_suffix_len;
379     if (!ssl->s3->aead_write_ctx->SuffixLen(&split_record_suffix_len, 1, 0)) {
380       assert(false);
381       return false;
382     }
383     const size_t split_record_len = prefix_len + 1 + split_record_suffix_len;
384     assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
385                                        ssl->s3->aead_write_ctx->cipher()) ==
386            split_record_len);
387 
388     // Write the n-1-byte fragment. The header gets split between |out_prefix|
389     // (header[:-1]) and |out| (header[-1:]).
390     uint8_t tmp_prefix[SSL3_RT_HEADER_LENGTH];
391     if (!do_seal_record(ssl, tmp_prefix, out + 1, out_suffix, type, in + 1,
392                         in_len - 1)) {
393       return false;
394     }
395     assert(tls_seal_scatter_prefix_len(ssl, type, in_len) ==
396            split_record_len + SSL3_RT_HEADER_LENGTH - 1);
397     OPENSSL_memcpy(out_prefix + split_record_len, tmp_prefix,
398                    SSL3_RT_HEADER_LENGTH - 1);
399     OPENSSL_memcpy(out, tmp_prefix + SSL3_RT_HEADER_LENGTH - 1, 1);
400     return true;
401   }
402 
403   return do_seal_record(ssl, out_prefix, out, out_suffix, type, in, in_len);
404 }
405 
tls_seal_record(SSL * ssl,uint8_t * out,size_t * out_len,size_t max_out_len,uint8_t type,const uint8_t * in,size_t in_len)406 bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
407                      size_t max_out_len, uint8_t type, const uint8_t *in,
408                      size_t in_len) {
409   if (buffers_alias(in, in_len, out, max_out_len)) {
410     OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
411     return false;
412   }
413 
414   const size_t prefix_len = tls_seal_scatter_prefix_len(ssl, type, in_len);
415   size_t suffix_len;
416   if (!tls_seal_scatter_suffix_len(ssl, &suffix_len, type, in_len)) {
417     return false;
418   }
419   if (in_len + prefix_len < in_len ||
420       prefix_len + in_len + suffix_len < prefix_len + in_len) {
421     OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
422     return false;
423   }
424   if (max_out_len < in_len + prefix_len + suffix_len) {
425     OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
426     return false;
427   }
428 
429   uint8_t *prefix = out;
430   uint8_t *body = out + prefix_len;
431   uint8_t *suffix = body + in_len;
432   if (!tls_seal_scatter_record(ssl, prefix, body, suffix, type, in, in_len)) {
433     return false;
434   }
435 
436   *out_len = prefix_len + in_len + suffix_len;
437   return true;
438 }
439 
ssl_process_alert(SSL * ssl,uint8_t * out_alert,Span<const uint8_t> in)440 enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
441                                          Span<const uint8_t> in) {
442   // Alerts records may not contain fragmented or multiple alerts.
443   if (in.size() != 2) {
444     *out_alert = SSL_AD_DECODE_ERROR;
445     OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
446     return ssl_open_record_error;
447   }
448 
449   ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_ALERT, in);
450 
451   const uint8_t alert_level = in[0];
452   const uint8_t alert_descr = in[1];
453 
454   uint16_t alert = (alert_level << 8) | alert_descr;
455   ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
456 
457   if (alert_level == SSL3_AL_WARNING) {
458     if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
459       ssl->s3->read_shutdown = ssl_shutdown_close_notify;
460       return ssl_open_record_close_notify;
461     }
462 
463     // Warning alerts do not exist in TLS 1.3, but RFC 8446 section 6.1
464     // continues to define user_canceled as a signal to cancel the handshake,
465     // without specifying how to handle it. JDK11 misuses it to signal
466     // full-duplex connection close after the handshake. As a workaround, skip
467     // user_canceled as in TLS 1.2. This matches NSS and OpenSSL.
468     if (ssl_has_final_version(ssl) &&
469         ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
470         alert_descr != SSL_AD_USER_CANCELLED) {
471       *out_alert = SSL_AD_DECODE_ERROR;
472       OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
473       return ssl_open_record_error;
474     }
475 
476     ssl->s3->warning_alert_count++;
477     if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
478       *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
479       OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
480       return ssl_open_record_error;
481     }
482     return ssl_open_record_discard;
483   }
484 
485   if (alert_level == SSL3_AL_FATAL) {
486     OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
487     ERR_add_error_dataf("SSL alert number %d", alert_descr);
488     *out_alert = 0;  // No alert to send back to the peer.
489     return ssl_open_record_error;
490   }
491 
492   *out_alert = SSL_AD_ILLEGAL_PARAMETER;
493   OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
494   return ssl_open_record_error;
495 }
496 
497 BSSL_NAMESPACE_END
498 
499 using namespace bssl;
500 
SSL_max_seal_overhead(const SSL * ssl)501 size_t SSL_max_seal_overhead(const SSL *ssl) {
502   if (SSL_is_dtls(ssl)) {
503     // TODO(crbug.com/381113363): Use the 0-RTT epoch if writing 0-RTT.
504     return dtls_max_seal_overhead(ssl, ssl->d1->write_epoch.epoch());
505   }
506 
507   size_t ret = SSL3_RT_HEADER_LENGTH;
508   ret += ssl->s3->aead_write_ctx->MaxOverhead();
509   // TLS 1.3 needs an extra byte for the encrypted record type.
510   if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
511       ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
512     ret += 1;
513   }
514   if (ssl_needs_record_splitting(ssl)) {
515     ret *= 2;
516   }
517   return ret;
518 }
519