tls_record.cc
Go to the documentation of this file.
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to. The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  * notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  * notice, this list of conditions and the following disclaimer in the
29  * documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  * must display the following acknowledgement:
32  * "This product includes cryptographic software written by
33  * Eric Young (eay@cryptsoft.com)"
34  * The word 'cryptographic' can be left out if the rouines from the library
35  * being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  * the apps directory (application code) you must include an acknowledgement:
38  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed. i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  * software must display the following acknowledgment:
74  * "This product includes software developed by the OpenSSL Project
75  * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  * endorse or promote products derived from this software without
79  * prior written permission. For written permission, please contact
80  * openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  * nor may "OpenSSL" appear in their names without prior written
84  * permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  * acknowledgment:
88  * "This product includes software developed by the OpenSSL Project
89  * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com). This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com). */
108 
109 #include <openssl/ssl.h>
110 
111 #include <assert.h>
112 #include <string.h>
113 
114 #include <openssl/bytestring.h>
115 #include <openssl/err.h>
116 #include <openssl/mem.h>
117 
118 #include "internal.h"
119 #include "../crypto/internal.h"
120 
121 
123 
124 // kMaxEmptyRecords is the number of consecutive, empty records that will be
125 // processed. Without this limit an attacker could send empty records at a
126 // faster rate than we can process and cause record processing to loop
127 // forever.
128 static const uint8_t kMaxEmptyRecords = 32;
129 
130 // kMaxEarlyDataSkipped is the maximum number of rejected early data bytes that
131 // will be skipped. Without this limit an attacker could send records at a
132 // faster rate than we can process and cause trial decryption to loop forever.
133 // This value should be slightly above kMaxEarlyDataAccepted, which is measured
134 // in plaintext.
135 static const size_t kMaxEarlyDataSkipped = 16384;
136 
137 // kMaxWarningAlerts is the number of consecutive warning alerts that will be
138 // processed.
139 static const uint8_t kMaxWarningAlerts = 4;
140 
141 // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
142 // state needs record-splitting and zero otherwise.
143 static bool ssl_needs_record_splitting(const SSL *ssl) {
144 #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
145  return !ssl->s3->aead_write_ctx->is_null_cipher() &&
146  ssl->s3->aead_write_ctx->ProtocolVersion() < TLS1_1_VERSION &&
147  (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
148  SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher());
149 #else
150  return false;
151 #endif
152 }
153 
154 bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
155  for (size_t i = seq_len - 1; i < seq_len; i--) {
156  ++seq[i];
157  if (seq[i] != 0) {
158  return true;
159  }
160  }
162  return false;
163 }
164 
165 size_t ssl_record_prefix_len(const SSL *ssl) {
166  size_t header_len;
167  if (SSL_is_dtls(ssl)) {
168  header_len = DTLS1_RT_HEADER_LENGTH;
169  } else {
170  header_len = SSL3_RT_HEADER_LENGTH;
171  }
172 
173  return header_len + ssl->s3->aead_read_ctx->ExplicitNonceLen();
174 }
175 
176 size_t ssl_seal_align_prefix_len(const SSL *ssl) {
177  if (SSL_is_dtls(ssl)) {
178  return DTLS1_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen();
179  }
180 
181  size_t ret =
182  SSL3_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen();
183  if (ssl_needs_record_splitting(ssl)) {
185  ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher());
186  }
187  return ret;
188 }
189 
191  size_t consumed) {
192  ssl->s3->early_data_skipped += consumed;
193  if (ssl->s3->early_data_skipped < consumed) {
194  ssl->s3->early_data_skipped = kMaxEarlyDataSkipped + 1;
195  }
196 
197  if (ssl->s3->early_data_skipped > kMaxEarlyDataSkipped) {
199  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
200  return ssl_open_record_error;
201  }
202 
204 }
205 
207  Span<uint8_t> *out, size_t *out_consumed,
208  uint8_t *out_alert, Span<uint8_t> in) {
209  *out_consumed = 0;
210  if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
212  }
213 
214  // If there is an unprocessed handshake message or we are already buffering
215  // too much, stop before decrypting another handshake record.
216  if (!tls_can_accept_handshake_data(ssl, out_alert)) {
217  return ssl_open_record_error;
218  }
219 
220  CBS cbs = CBS(in);
221 
222  // Decode the record header.
223  uint8_t type;
224  uint16_t version, ciphertext_len;
225  if (!CBS_get_u8(&cbs, &type) ||
226  !CBS_get_u16(&cbs, &version) ||
227  !CBS_get_u16(&cbs, &ciphertext_len)) {
228  *out_consumed = SSL3_RT_HEADER_LENGTH;
230  }
231 
232  bool version_ok;
233  if (ssl->s3->aead_read_ctx->is_null_cipher()) {
234  // Only check the first byte. Enforcing beyond that can prevent decoding
235  // version negotiation failure alerts.
236  version_ok = (version >> 8) == SSL3_VERSION_MAJOR;
237  } else {
238  version_ok = version == ssl->s3->aead_read_ctx->RecordVersion();
239  }
240 
241  if (!version_ok) {
243  *out_alert = SSL_AD_PROTOCOL_VERSION;
244  return ssl_open_record_error;
245  }
246 
247  // Check the ciphertext length.
248  if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
250  *out_alert = SSL_AD_RECORD_OVERFLOW;
251  return ssl_open_record_error;
252  }
253 
254  // Extract the body.
255  CBS body;
256  if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
257  *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
259  }
260 
262  ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header);
263 
264  *out_consumed = in.size() - CBS_len(&cbs);
265 
266  if (ssl->s3->have_version &&
268  SSL_in_init(ssl) &&
270  ciphertext_len == 1 &&
271  CBS_data(&body)[0] == 1) {
272  ssl->s3->empty_record_count++;
273  if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
275  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
276  return ssl_open_record_error;
277  }
279  }
280 
281  // Skip early data received when expecting a second ClientHello if we rejected
282  // 0RTT.
283  if (ssl->s3->skip_early_data &&
284  ssl->s3->aead_read_ctx->is_null_cipher() &&
286  return skip_early_data(ssl, out_alert, *out_consumed);
287  }
288 
289  // Decrypt the body in-place.
290  if (!ssl->s3->aead_read_ctx->Open(
291  out, type, version, ssl->s3->read_sequence, header,
292  MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) {
293  if (ssl->s3->skip_early_data && !ssl->s3->aead_read_ctx->is_null_cipher()) {
294  ERR_clear_error();
295  return skip_early_data(ssl, out_alert, *out_consumed);
296  }
297 
299  *out_alert = SSL_AD_BAD_RECORD_MAC;
300  return ssl_open_record_error;
301  }
302 
303  ssl->s3->skip_early_data = false;
304 
305  if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
306  *out_alert = SSL_AD_INTERNAL_ERROR;
307  return ssl_open_record_error;
308  }
309 
310  // TLS 1.3 hides the record type inside the encrypted data.
311  bool has_padding =
312  !ssl->s3->aead_read_ctx->is_null_cipher() &&
313  ssl->s3->aead_read_ctx->ProtocolVersion() >= TLS1_3_VERSION;
314 
315  // If there is padding, the plaintext limit includes the padding, but includes
316  // extra room for the inner content type.
317  size_t plaintext_limit =
319  if (out->size() > plaintext_limit) {
321  *out_alert = SSL_AD_RECORD_OVERFLOW;
322  return ssl_open_record_error;
323  }
324 
325  if (has_padding) {
326  // The outer record type is always application_data.
329  *out_alert = SSL_AD_DECODE_ERROR;
330  return ssl_open_record_error;
331  }
332 
333  do {
334  if (out->empty()) {
336  *out_alert = SSL_AD_DECRYPT_ERROR;
337  return ssl_open_record_error;
338  }
339  type = out->back();
340  *out = out->subspan(0, out->size() - 1);
341  } while (type == 0);
342  }
343 
344  // Limit the number of consecutive empty records.
345  if (out->empty()) {
346  ssl->s3->empty_record_count++;
347  if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
349  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
350  return ssl_open_record_error;
351  }
352  // Apart from the limit, empty records are returned up to the caller. This
353  // allows the caller to reject records of the wrong type.
354  } else {
355  ssl->s3->empty_record_count = 0;
356  }
357 
358  if (type == SSL3_RT_ALERT) {
359  return ssl_process_alert(ssl, out_alert, *out);
360  }
361 
362  // Handshake messages may not interleave with any other record type.
363  if (type != SSL3_RT_HANDSHAKE &&
366  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
367  return ssl_open_record_error;
368  }
369 
370  ssl->s3->warning_alert_count = 0;
371 
372  *out_type = type;
374 }
375 
376 static bool do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
377  uint8_t *out_suffix, uint8_t type, const uint8_t *in,
378  const size_t in_len) {
379  SSLAEADContext *aead = ssl->s3->aead_write_ctx.get();
380  uint8_t *extra_in = NULL;
381  size_t extra_in_len = 0;
382  if (!aead->is_null_cipher() &&
383  aead->ProtocolVersion() >= TLS1_3_VERSION) {
384  // TLS 1.3 hides the actual record type inside the encrypted data.
385  extra_in = &type;
386  extra_in_len = 1;
387  }
388 
389  size_t suffix_len, ciphertext_len;
390  if (!aead->SuffixLen(&suffix_len, in_len, extra_in_len) ||
391  !aead->CiphertextLen(&ciphertext_len, in_len, extra_in_len)) {
393  return false;
394  }
395 
396  assert(in == out || !buffers_alias(in, in_len, out, in_len));
397  assert(!buffers_alias(in, in_len, out_prefix, ssl_record_prefix_len(ssl)));
398  assert(!buffers_alias(in, in_len, out_suffix, suffix_len));
399 
400  if (extra_in_len) {
401  out_prefix[0] = SSL3_RT_APPLICATION_DATA;
402  } else {
403  out_prefix[0] = type;
404  }
405 
406  uint16_t record_version = aead->RecordVersion();
407 
408  out_prefix[1] = record_version >> 8;
409  out_prefix[2] = record_version & 0xff;
410  out_prefix[3] = ciphertext_len >> 8;
411  out_prefix[4] = ciphertext_len & 0xff;
413 
414  if (!aead->SealScatter(out_prefix + SSL3_RT_HEADER_LENGTH, out, out_suffix,
415  out_prefix[0], record_version, ssl->s3->write_sequence,
416  header, in, in_len, extra_in, extra_in_len) ||
417  !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
418  return false;
419  }
420 
421  ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header);
422  return true;
423 }
424 
425 static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type,
426  size_t in_len) {
427  size_t ret = SSL3_RT_HEADER_LENGTH;
428  if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
430  // In the case of record splitting, the 1-byte record (of the 1/n-1 split)
431  // will be placed in the prefix, as will four of the five bytes of the
432  // record header for the main record. The final byte will replace the first
433  // byte of the plaintext that was used in the small record.
434  ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher());
435  ret += SSL3_RT_HEADER_LENGTH - 1;
436  } else {
437  ret += ssl->s3->aead_write_ctx->ExplicitNonceLen();
438  }
439  return ret;
440 }
441 
442 static bool tls_seal_scatter_suffix_len(const SSL *ssl, size_t *out_suffix_len,
443  uint8_t type, size_t in_len) {
444  size_t extra_in_len = 0;
445  if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
446  ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) {
447  // TLS 1.3 adds an extra byte for encrypted record type.
448  extra_in_len = 1;
449  }
450  // clang-format off
452  in_len > 1 &&
454  // With record splitting enabled, the first byte gets sealed into a separate
455  // record which is written into the prefix.
456  in_len -= 1;
457  }
458  // clang-format on
459  return ssl->s3->aead_write_ctx->SuffixLen(out_suffix_len, in_len, extra_in_len);
460 }
461 
462 // tls_seal_scatter_record seals a new record of type |type| and body |in| and
463 // splits it between |out_prefix|, |out|, and |out_suffix|. Exactly
464 // |tls_seal_scatter_prefix_len| bytes are written to |out_prefix|, |in_len|
465 // bytes to |out|, and |tls_seal_scatter_suffix_len| bytes to |out_suffix|. It
466 // returns one on success and zero on error. If enabled,
467 // |tls_seal_scatter_record| implements TLS 1.0 CBC 1/n-1 record splitting and
468 // may write two records concatenated.
469 static bool tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
470  uint8_t *out_suffix, uint8_t type,
471  const uint8_t *in, size_t in_len) {
472  if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
474  assert(ssl->s3->aead_write_ctx->ExplicitNonceLen() == 0);
475  const size_t prefix_len = SSL3_RT_HEADER_LENGTH;
476 
477  // Write the 1-byte fragment into |out_prefix|.
478  uint8_t *split_body = out_prefix + prefix_len;
479  uint8_t *split_suffix = split_body + 1;
480 
481  if (!do_seal_record(ssl, out_prefix, split_body, split_suffix, type, in,
482  1)) {
483  return false;
484  }
485 
486  size_t split_record_suffix_len;
487  if (!ssl->s3->aead_write_ctx->SuffixLen(&split_record_suffix_len, 1, 0)) {
488  assert(false);
489  return false;
490  }
491  const size_t split_record_len = prefix_len + 1 + split_record_suffix_len;
493  ssl->s3->aead_write_ctx->cipher()) ==
494  split_record_len);
495 
496  // Write the n-1-byte fragment. The header gets split between |out_prefix|
497  // (header[:-1]) and |out| (header[-1:]).
498  uint8_t tmp_prefix[SSL3_RT_HEADER_LENGTH];
499  if (!do_seal_record(ssl, tmp_prefix, out + 1, out_suffix, type, in + 1,
500  in_len - 1)) {
501  return false;
502  }
503  assert(tls_seal_scatter_prefix_len(ssl, type, in_len) ==
504  split_record_len + SSL3_RT_HEADER_LENGTH - 1);
505  OPENSSL_memcpy(out_prefix + split_record_len, tmp_prefix,
507  OPENSSL_memcpy(out, tmp_prefix + SSL3_RT_HEADER_LENGTH - 1, 1);
508  return true;
509  }
510 
511  return do_seal_record(ssl, out_prefix, out, out_suffix, type, in, in_len);
512 }
513 
514 bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
515  size_t max_out_len, uint8_t type, const uint8_t *in,
516  size_t in_len) {
517  if (buffers_alias(in, in_len, out, max_out_len)) {
519  return false;
520  }
521 
522  const size_t prefix_len = tls_seal_scatter_prefix_len(ssl, type, in_len);
523  size_t suffix_len;
524  if (!tls_seal_scatter_suffix_len(ssl, &suffix_len, type, in_len)) {
525  return false;
526  }
527  if (in_len + prefix_len < in_len ||
528  prefix_len + in_len + suffix_len < prefix_len + in_len) {
530  return false;
531  }
532  if (max_out_len < in_len + prefix_len + suffix_len) {
534  return false;
535  }
536 
537  uint8_t *prefix = out;
538  uint8_t *body = out + prefix_len;
539  uint8_t *suffix = body + in_len;
540  if (!tls_seal_scatter_record(ssl, prefix, body, suffix, type, in, in_len)) {
541  return false;
542  }
543 
544  *out_len = prefix_len + in_len + suffix_len;
545  return true;
546 }
547 
550  // Alerts records may not contain fragmented or multiple alerts.
551  if (in.size() != 2) {
552  *out_alert = SSL_AD_DECODE_ERROR;
554  return ssl_open_record_error;
555  }
556 
557  ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_ALERT, in);
558 
559  const uint8_t alert_level = in[0];
560  const uint8_t alert_descr = in[1];
561 
562  uint16_t alert = (alert_level << 8) | alert_descr;
564 
565  if (alert_level == SSL3_AL_WARNING) {
566  if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
567  ssl->s3->read_shutdown = ssl_shutdown_close_notify;
569  }
570 
571  // Warning alerts do not exist in TLS 1.3, but RFC 8446 section 6.1
572  // continues to define user_canceled as a signal to cancel the handshake,
573  // without specifying how to handle it. JDK11 misuses it to signal
574  // full-duplex connection close after the handshake. As a workaround, skip
575  // user_canceled as in TLS 1.2. This matches NSS and OpenSSL.
576  if (ssl->s3->have_version &&
578  alert_descr != SSL_AD_USER_CANCELLED) {
579  *out_alert = SSL_AD_DECODE_ERROR;
581  return ssl_open_record_error;
582  }
583 
584  ssl->s3->warning_alert_count++;
585  if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
586  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
588  return ssl_open_record_error;
589  }
591  }
592 
593  if (alert_level == SSL3_AL_FATAL) {
595  ERR_add_error_dataf("SSL alert number %d", alert_descr);
596  *out_alert = 0; // No alert to send back to the peer.
597  return ssl_open_record_error;
598  }
599 
600  *out_alert = SSL_AD_ILLEGAL_PARAMETER;
602  return ssl_open_record_error;
603 }
604 
605 OpenRecordResult OpenRecord(SSL *ssl, Span<uint8_t> *out,
606  size_t *out_record_len, uint8_t *out_alert,
607  const Span<uint8_t> in) {
608  // This API is a work in progress and currently only works for TLS 1.2 servers
609  // and below.
610  if (SSL_in_init(ssl) ||
611  SSL_is_dtls(ssl) ||
613  assert(false);
614  *out_alert = SSL_AD_INTERNAL_ERROR;
615  return OpenRecordResult::kError;
616  }
617 
619  uint8_t type = 0;
621  ssl, &type, &plaintext, out_record_len, out_alert, in);
622 
623  switch (result) {
626  *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
627  return OpenRecordResult::kError;
628  }
629  *out = plaintext;
630  return OpenRecordResult::kOK;
632  return OpenRecordResult::kDiscard;
634  return OpenRecordResult::kIncompleteRecord;
636  return OpenRecordResult::kAlertCloseNotify;
638  return OpenRecordResult::kError;
639  }
640  assert(false);
641  return OpenRecordResult::kError;
642 }
643 
644 size_t SealRecordPrefixLen(const SSL *ssl, const size_t record_len) {
645  return tls_seal_scatter_prefix_len(ssl, SSL3_RT_APPLICATION_DATA, record_len);
646 }
647 
648 size_t SealRecordSuffixLen(const SSL *ssl, const size_t plaintext_len) {
649  assert(plaintext_len <= SSL3_RT_MAX_PLAIN_LENGTH);
650  size_t suffix_len;
652  plaintext_len)) {
653  assert(false);
655  return 0;
656  }
657  assert(suffix_len <= SSL3_RT_MAX_ENCRYPTED_OVERHEAD);
658  return suffix_len;
659 }
660 
661 bool SealRecord(SSL *ssl, const Span<uint8_t> out_prefix,
662  const Span<uint8_t> out, Span<uint8_t> out_suffix,
663  const Span<const uint8_t> in) {
664  // This API is a work in progress and currently only works for TLS 1.2 servers
665  // and below.
666  if (SSL_in_init(ssl) ||
667  SSL_is_dtls(ssl) ||
669  assert(false);
671  return false;
672  }
673 
674  if (out_prefix.size() != SealRecordPrefixLen(ssl, in.size()) ||
675  out.size() != in.size() ||
676  out_suffix.size() != SealRecordSuffixLen(ssl, in.size())) {
678  return false;
679  }
680  return tls_seal_scatter_record(ssl, out_prefix.data(), out.data(),
681  out_suffix.data(), SSL3_RT_APPLICATION_DATA,
682  in.data(), in.size());
683 }
684 
686 
687 using namespace bssl;
688 
689 size_t SSL_max_seal_overhead(const SSL *ssl) {
690  if (SSL_is_dtls(ssl)) {
692  }
693 
694  size_t ret = SSL3_RT_HEADER_LENGTH;
695  ret += ssl->s3->aead_write_ctx->MaxOverhead();
696  // TLS 1.3 needs an extra byte for the encrypted record type.
697  if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
698  ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) {
699  ret += 1;
700  }
701  if (ssl_needs_record_splitting(ssl)) {
702  ret *= 2;
703  }
704  return ret;
705 }
SSL3_VERSION_MAJOR
#define SSL3_VERSION_MAJOR
Definition: ssl.h:647
SSL3_AL_WARNING
#define SSL3_AL_WARNING
Definition: ssl3.h:279
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
tls_open_record
ssl_open_record_t 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)
Definition: tls_record.cc:206
SSL_AD_UNEXPECTED_MESSAGE
#define SSL_AD_UNEXPECTED_MESSAGE
Definition: ssl.h:3795
SSL_AD_USER_CANCELLED
#define SSL_AD_USER_CANCELLED
Definition: ssl.h:3817
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
ssl_open_record_partial
@ ssl_open_record_partial
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:936
CBS_get_u16
#define CBS_get_u16
Definition: boringssl_prefix_symbols.h:1073
Span::size
size_t size() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:133
cbs_st
Definition: bytestring.h:39
SSLAEADContext
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:764
tls_seal_record
bool 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)
Definition: tls_record.cc:514
SSL_AD_REASON_OFFSET
#define SSL_AD_REASON_OFFSET
Definition: ssl.h:3791
tls_seal_scatter_suffix_len
static bool tls_seal_scatter_suffix_len(const SSL *ssl, size_t *out_suffix_len, uint8_t type, size_t in_len)
Definition: tls_record.cc:442
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
SSL_R_WRONG_VERSION_NUMBER
#define SSL_R_WRONG_VERSION_NUMBER
Definition: ssl.h:5514
do_seal_record
static bool 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)
Definition: tls_record.cc:376
SSL3_RT_ALERT
#define SSL3_RT_ALERT
Definition: ssl3.h:272
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
tls_seal_scatter_record
static bool 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)
Definition: tls_record.cc:469
SSL_CIPHER_is_block_cipher
#define SSL_CIPHER_is_block_cipher
Definition: boringssl_prefix_symbols.h:60
string.h
SSL_AD_INTERNAL_ERROR
#define SSL_AD_INTERNAL_ERROR
Definition: ssl.h:3815
tls_can_accept_handshake_data
bool tls_can_accept_handshake_data(const SSL *ssl, uint8_t *out_alert)
Definition: s3_both.cc:519
tls_seal_scatter_prefix_len
static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type, size_t in_len)
Definition: tls_record.cc:425
SealRecord
bool SealRecord(SSL *ssl, const Span< uint8_t > out_prefix, const Span< uint8_t > out, Span< uint8_t > out_suffix, const Span< const uint8_t > in)
Definition: tls_record.cc:661
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
SSL_R_TOO_MANY_EMPTY_FRAGMENTS
#define SSL_R_TOO_MANY_EMPTY_FRAGMENTS
Definition: ssl.h:5486
ssl_process_alert
enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert, Span< const uint8_t > in)
Definition: tls_record.cc:548
bssl
Definition: hpke_test.cc:37
SSL_R_OUTPUT_ALIASES_INPUT
#define SSL_R_OUTPUT_ALIASES_INPUT
Definition: ssl.h:5456
SSLAEADContext::RecordVersion
uint16_t RecordVersion() const
Definition: ssl_aead_ctx.cc:169
version
Definition: version.py:1
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
SSL_AD_ILLEGAL_PARAMETER
#define SSL_AD_ILLEGAL_PARAMETER
Definition: ssl.h:3807
kMaxEmptyRecords
static const BSSL_NAMESPACE_BEGIN uint8_t kMaxEmptyRecords
Definition: tls_record.cc:128
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ssl_st::mode
uint32_t mode
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3764
SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
#define SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
Definition: ssl.h:5407
kMaxEarlyDataSkipped
static const size_t kMaxEarlyDataSkipped
Definition: tls_record.cc:135
ssl_do_msg_callback
void ssl_do_msg_callback(const SSL *ssl, int is_write, int content_type, Span< const uint8_t > in)
Definition: ssl_lib.cc:329
bytestring.h
SSL3_RT_MAX_ENCRYPTED_LENGTH
#define SSL3_RT_MAX_ENCRYPTED_LENGTH
Definition: ssl3.h:263
skip_early_data
static ssl_open_record_t skip_early_data(SSL *ssl, uint8_t *out_alert, size_t consumed)
Definition: tls_record.cc:190
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
TLS1_1_VERSION
#define TLS1_1_VERSION
Definition: ssl.h:651
SSL3_RT_MAX_PLAIN_LENGTH
#define SSL3_RT_MAX_PLAIN_LENGTH
Definition: ssl3.h:235
DTLS1_RT_HEADER_LENGTH
#define DTLS1_RT_HEADER_LENGTH
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2833
SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA
#define SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA
Definition: ssl.h:5537
ssl_open_record_discard
@ ssl_open_record_discard
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:935
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
ERR_add_error_dataf
#define ERR_add_error_dataf
Definition: boringssl_prefix_symbols.h:1412
CBS
struct cbs_st CBS
Definition: base.h:388
conf.version
string version
Definition: doc/python/sphinx/conf.py:36
SSL_R_UNEXPECTED_RECORD
#define SSL_R_UNEXPECTED_RECORD
Definition: ssl.h:5492
SSL_is_dtls
#define SSL_is_dtls
Definition: boringssl_prefix_symbols.h:402
ssl_record_prefix_len
size_t ssl_record_prefix_len(const SSL *ssl)
Definition: tls_record.cc:165
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
BSSL_NAMESPACE_END
#define BSSL_NAMESPACE_END
Definition: base.h:480
header
struct absl::base_internal::@2940::AllocList::Header header
CBS_get_u8
#define CBS_get_u8
Definition: boringssl_prefix_symbols.h:1082
err.h
SSL_AD_BAD_RECORD_MAC
#define SSL_AD_BAD_RECORD_MAC
Definition: ssl.h:3796
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
ssl_protocol_version
uint16_t ssl_protocol_version(const SSL *ssl)
Definition: ssl_versions.cc:251
ssl_seal_align_prefix_len
size_t ssl_seal_align_prefix_len(const SSL *ssl)
Definition: tls_record.cc:176
SSL_AD_PROTOCOL_VERSION
#define SSL_AD_PROTOCOL_VERSION
Definition: ssl.h:3813
SSLAEADContext::ProtocolVersion
uint16_t ProtocolVersion() const
Definition: ssl_aead_ctx.cc:160
SSL_R_INVALID_OUTER_RECORD_TYPE
#define SSL_R_INVALID_OUTER_RECORD_TYPE
Definition: ssl.h:5518
OpenRecord
OpenRecordResult OpenRecord(SSL *ssl, Span< uint8_t > *out, size_t *out_record_len, uint8_t *out_alert, const Span< uint8_t > in)
Definition: tls_record.cc:605
Span< uint8_t >
ERR_R_OVERFLOW
#define ERR_R_OVERFLOW
Definition: err.h:375
SSL3_AL_FATAL
#define SSL3_AL_FATAL
Definition: ssl3.h:280
ssl.h
SSL_AD_CLOSE_NOTIFY
#define SSL_AD_CLOSE_NOTIFY
Definition: ssl.h:3794
SSL_R_UNKNOWN_ALERT_TYPE
#define SSL_R_UNKNOWN_ALERT_TYPE
Definition: ssl.h:5494
SSL_max_seal_overhead
size_t SSL_max_seal_overhead(const SSL *ssl)
Definition: tls_record.cc:689
SSL_R_TOO_MANY_WARNING_ALERTS
#define SSL_R_TOO_MANY_WARNING_ALERTS
Definition: ssl.h:5487
SSL3_RT_HANDSHAKE
#define SSL3_RT_HANDSHAKE
Definition: ssl3.h:273
ssl_open_record_t
ssl_open_record_t
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:933
SSL_CB_READ_ALERT
#define SSL_CB_READ_ALERT
Definition: ssl.h:4285
ssl_open_record_close_notify
@ ssl_open_record_close_notify
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:937
ssl_record_sequence_update
bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len)
Definition: tls_record.cc:154
SSLAEADContext::SuffixLen
bool SuffixLen(size_t *out_suffix_len, size_t in_len, size_t extra_in_len) const
Definition: ssl_aead_ctx.cc:189
kMaxWarningAlerts
static const uint8_t kMaxWarningAlerts
Definition: tls_record.cc:139
TLS1_3_VERSION
#define TLS1_3_VERSION
Definition: ssl.h:653
SealRecordSuffixLen
size_t SealRecordSuffixLen(const SSL *ssl, const size_t plaintext_len)
Definition: tls_record.cc:648
SSL_MODE_CBC_RECORD_SPLITTING
#define SSL_MODE_CBC_RECORD_SPLITTING
Definition: ssl.h:793
TLS1_2_VERSION
#define TLS1_2_VERSION
Definition: ssl.h:652
BSSL_NAMESPACE_BEGIN
Definition: trust_token_test.cc:45
ssl_st::s3
bssl::SSL3_STATE * s3
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3730
SSL3_RT_CHANGE_CIPHER_SPEC
#define SSL3_RT_CHANGE_CIPHER_SPEC
Definition: ssl3.h:271
SSL_in_init
#define SSL_in_init
Definition: boringssl_prefix_symbols.h:401
suffix
unsigned char suffix[65536]
Definition: bloaty/third_party/zlib/examples/gun.c:164
ssl_open_record_success
@ ssl_open_record_success
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:934
SSL_AD_RECORD_OVERFLOW
#define SSL_AD_RECORD_OVERFLOW
Definition: ssl.h:3798
SSL_AD_DECODE_ERROR
#define SSL_AD_DECODE_ERROR
Definition: ssl.h:3810
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
SSLAEADContext::is_null_cipher
bool is_null_cipher() const
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:810
ssl_open_record_error
@ ssl_open_record_error
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:938
dtls1_use_current_epoch
@ dtls1_use_current_epoch
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:1004
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
SSLAEADContext::SealScatter
bool SealScatter(uint8_t *out_prefix, uint8_t *out, uint8_t *out_suffix, uint8_t type, uint16_t record_version, const uint8_t seqnum[8], Span< const uint8_t > header, const uint8_t *in, size_t in_len, const uint8_t *extra_in, size_t extra_in_len)
Definition: ssl_aead_ctx.cc:314
ERR_clear_error
#define ERR_clear_error
Definition: boringssl_prefix_symbols.h:1413
SSL_R_ENCRYPTED_LENGTH_TOO_LONG
#define SSL_R_ENCRYPTED_LENGTH_TOO_LONG
Definition: ssl.h:5414
CBS_get_bytes
#define CBS_get_bytes
Definition: boringssl_prefix_symbols.h:1067
tls_has_unprocessed_handshake_data
bool tls_has_unprocessed_handshake_data(const SSL *ssl)
Definition: s3_both.cc:539
SSL_R_BUFFER_TOO_SMALL
#define SSL_R_BUFFER_TOO_SMALL
Definition: ssl.h:5389
ssl_shutdown_close_notify
@ ssl_shutdown_close_notify
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:2605
plaintext
const char * plaintext
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil_unittest.cc:85
SSL3_RT_MAX_ENCRYPTED_OVERHEAD
#define SSL3_RT_MAX_ENCRYPTED_OVERHEAD
Definition: ssl3.h:246
Span::data
T * data() const
Definition: boringssl-with-bazel/src/include/openssl/span.h:132
mem.h
ssl_do_info_callback
void ssl_do_info_callback(const SSL *ssl, int type, int value)
Definition: ssl_lib.cc:316
buffers_alias
static int buffers_alias(const uint8_t *a, size_t a_len, const uint8_t *b, size_t b_len)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:216
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
SealRecordPrefixLen
size_t SealRecordPrefixLen(const SSL *ssl, const size_t record_len)
Definition: tls_record.cc:644
SSL_R_DATA_LENGTH_TOO_LONG
#define SSL_R_DATA_LENGTH_TOO_LONG
Definition: ssl.h:5404
SSL3_RT_HEADER
#define SSL3_RT_HEADER
Definition: ssl3.h:277
ssl_needs_record_splitting
static bool ssl_needs_record_splitting(const SSL *ssl)
Definition: tls_record.cc:143
SSL3_RT_APPLICATION_DATA
#define SSL3_RT_APPLICATION_DATA
Definition: ssl3.h:274
SSL_R_RECORD_TOO_LARGE
#define SSL_R_RECORD_TOO_LARGE
Definition: ssl.h:5467
dtls_max_seal_overhead
size_t dtls_max_seal_overhead(const SSL *ssl, enum dtls1_use_epoch_t use_epoch)
Definition: dtls_record.cc:284
SSL_AD_DECRYPT_ERROR
#define SSL_AD_DECRYPT_ERROR
Definition: ssl.h:3811
absl::MakeSpan
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
Definition: abseil-cpp/absl/types/span.h:661
SSL3_RT_HEADER_LENGTH
#define SSL3_RT_HEADER_LENGTH
Definition: ssl3.h:206
SSL_R_BAD_ALERT
#define SSL_R_BAD_ALERT
Definition: ssl.h:5370
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
SSLAEADContext::CiphertextLen
bool CiphertextLen(size_t *out_len, size_t in_len, size_t extra_in_len) const
Definition: ssl_aead_ctx.cc:199
ssl_cipher_get_record_split_len
size_t ssl_cipher_get_record_split_len(const SSL_CIPHER *cipher)
Definition: ssl_cipher.cc:1305


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:40