David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 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> |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 112 | #include <string.h> |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 113 | |
| 114 | #include <openssl/bytestring.h> |
| 115 | #include <openssl/err.h> |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 116 | #include <openssl/mem.h> |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 117 | |
| 118 | #include "internal.h" |
David Benjamin | 1a01e1f | 2016-06-08 18:31:24 -0400 | [diff] [blame] | 119 | #include "../crypto/internal.h" |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 120 | |
| 121 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 122 | BSSL_NAMESPACE_BEGIN |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 123 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 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. |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 128 | static const uint8_t kMaxEmptyRecords = 32; |
| 129 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 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. |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 135 | static const size_t kMaxEarlyDataSkipped = 16384; |
| 136 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 137 | // kMaxWarningAlerts is the number of consecutive warning alerts that will be |
| 138 | // processed. |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 139 | static const uint8_t kMaxWarningAlerts = 4; |
| 140 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 141 | // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher |
| 142 | // state needs record-splitting and zero otherwise. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 143 | static bool ssl_needs_record_splitting(const SSL *ssl) { |
Adam Langley | 1430873 | 2017-07-14 16:51:39 -0700 | [diff] [blame] | 144 | #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 145 | return !ssl->s3->aead_write_ctx->is_null_cipher() && |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 146 | ssl->s3->aead_write_ctx->ProtocolVersion() < TLS1_1_VERSION && |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 147 | (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 && |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 148 | SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher()); |
Adam Langley | 1430873 | 2017-07-14 16:51:39 -0700 | [diff] [blame] | 149 | #else |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 150 | return false; |
Adam Langley | 1430873 | 2017-07-14 16:51:39 -0700 | [diff] [blame] | 151 | #endif |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 152 | } |
| 153 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 154 | bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 155 | for (size_t i = seq_len - 1; i < seq_len; i--) { |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 156 | ++seq[i]; |
| 157 | if (seq[i] != 0) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 158 | return true; |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 162 | return false; |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 163 | } |
| 164 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 165 | size_t ssl_record_prefix_len(const SSL *ssl) { |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 166 | size_t header_len; |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 167 | if (SSL_is_dtls(ssl)) { |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 168 | header_len = DTLS1_RT_HEADER_LENGTH; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 169 | } else { |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 170 | header_len = SSL3_RT_HEADER_LENGTH; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 171 | } |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 172 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 173 | return header_len + ssl->s3->aead_read_ctx->ExplicitNonceLen(); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 174 | } |
| 175 | |
David Benjamin | 1a01e1f | 2016-06-08 18:31:24 -0400 | [diff] [blame] | 176 | size_t ssl_seal_align_prefix_len(const SSL *ssl) { |
David Benjamin | ce079fd | 2016-08-02 16:22:34 -0400 | [diff] [blame] | 177 | if (SSL_is_dtls(ssl)) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 178 | return DTLS1_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen(); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 179 | } |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 180 | |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 181 | size_t ret = |
| 182 | SSL3_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen(); |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 183 | if (ssl_needs_record_splitting(ssl)) { |
Steven Valdez | 924a352 | 2017-03-02 16:05:03 -0500 | [diff] [blame] | 184 | ret += SSL3_RT_HEADER_LENGTH; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 185 | ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher()); |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 186 | } |
| 187 | return ret; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 188 | } |
| 189 | |
David Benjamin | 13761f2 | 2017-11-14 10:51:44 +0800 | [diff] [blame] | 190 | static ssl_open_record_t skip_early_data(SSL *ssl, uint8_t *out_alert, |
| 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) { |
| 198 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA); |
| 199 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 200 | return ssl_open_record_error; |
| 201 | } |
| 202 | |
| 203 | return ssl_open_record_discard; |
| 204 | } |
| 205 | |
| 206 | ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, |
| 207 | Span<uint8_t> *out, size_t *out_consumed, |
| 208 | uint8_t *out_alert, Span<uint8_t> in) { |
David Benjamin | f6632da | 2017-10-12 19:11:47 -0400 | [diff] [blame] | 209 | *out_consumed = 0; |
| 210 | if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) { |
| 211 | return ssl_open_record_close_notify; |
| 212 | } |
| 213 | |
David Benjamin | 33febf6 | 2017-10-07 16:52:50 -0400 | [diff] [blame] | 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 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 220 | CBS cbs = CBS(in); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 221 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 222 | // Decode the record header. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 223 | uint8_t type; |
| 224 | uint16_t version, ciphertext_len; |
Steven Valdez | 924a352 | 2017-03-02 16:05:03 -0500 | [diff] [blame] | 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; |
| 229 | return ssl_open_record_partial; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 230 | } |
| 231 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 232 | bool version_ok; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 233 | if (ssl->s3->aead_read_ctx->is_null_cipher()) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 234 | // Only check the first byte. Enforcing beyond that can prevent decoding |
| 235 | // version negotiation failure alerts. |
David Benjamin | e6f2221 | 2016-11-08 14:28:24 -0500 | [diff] [blame] | 236 | version_ok = (version >> 8) == SSL3_VERSION_MAJOR; |
David Benjamin | e6f2221 | 2016-11-08 14:28:24 -0500 | [diff] [blame] | 237 | } else { |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 238 | version_ok = version == ssl->s3->aead_read_ctx->RecordVersion(); |
David Benjamin | e6f2221 | 2016-11-08 14:28:24 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | if (!version_ok) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 242 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
| 243 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 244 | return ssl_open_record_error; |
| 245 | } |
| 246 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 247 | // Check the ciphertext length. |
David Benjamin | 03f0005 | 2015-11-18 20:41:11 -0500 | [diff] [blame] | 248 | if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 249 | OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); |
| 250 | *out_alert = SSL_AD_RECORD_OVERFLOW; |
| 251 | return ssl_open_record_error; |
| 252 | } |
| 253 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 254 | // Extract the body. |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 255 | CBS body; |
| 256 | if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) { |
Steven Valdez | 924a352 | 2017-03-02 16:05:03 -0500 | [diff] [blame] | 257 | *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 258 | return ssl_open_record_partial; |
| 259 | } |
| 260 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 261 | Span<const uint8_t> header = in.subspan(0, SSL3_RT_HEADER_LENGTH); |
| 262 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 263 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 264 | *out_consumed = in.size() - CBS_len(&cbs); |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 265 | |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 266 | if (ssl->s3->have_version && |
Steven Valdez | 64cc121 | 2017-12-04 11:15:37 -0500 | [diff] [blame] | 267 | ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
Steven Valdez | 964b237 | 2017-11-07 17:09:52 -0500 | [diff] [blame] | 268 | SSL_in_init(ssl) && |
| 269 | type == SSL3_RT_CHANGE_CIPHER_SPEC && |
| 270 | ciphertext_len == 1 && |
| 271 | CBS_data(&body)[0] == 1) { |
| 272 | ssl->s3->empty_record_count++; |
| 273 | if (ssl->s3->empty_record_count > kMaxEmptyRecords) { |
| 274 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 275 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 276 | return ssl_open_record_error; |
| 277 | } |
| 278 | return ssl_open_record_discard; |
| 279 | } |
| 280 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 281 | // Skip early data received when expecting a second ClientHello if we rejected |
| 282 | // 0RTT. |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 283 | if (ssl->s3->skip_early_data && |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 284 | ssl->s3->aead_read_ctx->is_null_cipher() && |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 285 | type == SSL3_RT_APPLICATION_DATA) { |
David Benjamin | 13761f2 | 2017-11-14 10:51:44 +0800 | [diff] [blame] | 286 | return skip_early_data(ssl, out_alert, *out_consumed); |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 287 | } |
| 288 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 289 | // Decrypt the body in-place. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 290 | if (!ssl->s3->aead_read_ctx->Open( |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 291 | out, type, version, ssl->s3->read_sequence, header, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 292 | MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 293 | if (ssl->s3->skip_early_data && !ssl->s3->aead_read_ctx->is_null_cipher()) { |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 294 | ERR_clear_error(); |
David Benjamin | 13761f2 | 2017-11-14 10:51:44 +0800 | [diff] [blame] | 295 | return skip_early_data(ssl, out_alert, *out_consumed); |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 296 | } |
| 297 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 298 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 299 | *out_alert = SSL_AD_BAD_RECORD_MAC; |
| 300 | return ssl_open_record_error; |
| 301 | } |
Steven Valdez | a4ee74d | 2016-11-29 13:36:45 -0500 | [diff] [blame] | 302 | |
David Benjamin | 046bc1f | 2017-08-31 15:06:42 -0400 | [diff] [blame] | 303 | ssl->s3->skip_early_data = false; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 304 | |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 305 | if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 306 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 307 | return ssl_open_record_error; |
| 308 | } |
| 309 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 310 | // TLS 1.3 hides the record type inside the encrypted data. |
David Benjamin | 13761f2 | 2017-11-14 10:51:44 +0800 | [diff] [blame] | 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 = |
| 318 | has_padding ? SSL3_RT_MAX_PLAIN_LENGTH + 1 : SSL3_RT_MAX_PLAIN_LENGTH; |
| 319 | if (out->size() > plaintext_limit) { |
| 320 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 321 | *out_alert = SSL_AD_RECORD_OVERFLOW; |
| 322 | return ssl_open_record_error; |
| 323 | } |
| 324 | |
| 325 | if (has_padding) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 326 | // The outer record type is always application_data. |
David Benjamin | c9ae27c | 2016-06-24 22:56:37 -0400 | [diff] [blame] | 327 | if (type != SSL3_RT_APPLICATION_DATA) { |
| 328 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE); |
| 329 | *out_alert = SSL_AD_DECODE_ERROR; |
| 330 | return ssl_open_record_error; |
| 331 | } |
| 332 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 333 | do { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 334 | if (out->empty()) { |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 335 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 336 | *out_alert = SSL_AD_DECRYPT_ERROR; |
| 337 | return ssl_open_record_error; |
| 338 | } |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 339 | type = out->back(); |
| 340 | *out = out->subspan(0, out->size() - 1); |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 341 | } while (type == 0); |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame] | 342 | } |
| 343 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 344 | // Limit the number of consecutive empty records. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 345 | if (out->empty()) { |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 346 | ssl->s3->empty_record_count++; |
| 347 | if (ssl->s3->empty_record_count > kMaxEmptyRecords) { |
| 348 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 349 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 350 | return ssl_open_record_error; |
| 351 | } |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 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. |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 354 | } else { |
| 355 | ssl->s3->empty_record_count = 0; |
| 356 | } |
| 357 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 358 | if (type == SSL3_RT_ALERT) { |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 359 | return ssl_process_alert(ssl, out_alert, *out); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 360 | } |
| 361 | |
David Benjamin | 33febf6 | 2017-10-07 16:52:50 -0400 | [diff] [blame] | 362 | // Handshake messages may not interleave with any other record type. |
| 363 | if (type != SSL3_RT_HANDSHAKE && |
| 364 | tls_has_unprocessed_handshake_data(ssl)) { |
| 365 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD); |
| 366 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 367 | return ssl_open_record_error; |
| 368 | } |
| 369 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 370 | ssl->s3->warning_alert_count = 0; |
| 371 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 372 | *out_type = type; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 373 | return ssl_open_record_success; |
| 374 | } |
| 375 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 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) { |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 379 | SSLAEADContext *aead = ssl->s3->aead_write_ctx.get(); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 380 | uint8_t *extra_in = NULL; |
| 381 | size_t extra_in_len = 0; |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 382 | if (!aead->is_null_cipher() && |
| 383 | aead->ProtocolVersion() >= TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 384 | // TLS 1.3 hides the actual record type inside the encrypted data. |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 385 | extra_in = &type; |
| 386 | extra_in_len = 1; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 387 | } |
| 388 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 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)) { |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 392 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 393 | return false; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 394 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 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) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 401 | out_prefix[0] = SSL3_RT_APPLICATION_DATA; |
| 402 | } else { |
| 403 | out_prefix[0] = type; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 404 | } |
David Benjamin | 1a01e1f | 2016-06-08 18:31:24 -0400 | [diff] [blame] | 405 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 406 | uint16_t record_version = aead->RecordVersion(); |
Steven Valdez | 1682126 | 2017-09-08 17:03:42 -0400 | [diff] [blame] | 407 | |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 408 | out_prefix[1] = record_version >> 8; |
| 409 | out_prefix[2] = record_version & 0xff; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 410 | out_prefix[3] = ciphertext_len >> 8; |
| 411 | out_prefix[4] = ciphertext_len & 0xff; |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 412 | Span<const uint8_t> header = MakeSpan(out_prefix, SSL3_RT_HEADER_LENGTH); |
David Benjamin | 6f600d6 | 2016-12-21 16:06:54 -0500 | [diff] [blame] | 413 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 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) || |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 417 | !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 418 | return false; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 419 | } |
| 420 | |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 421 | ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 422 | return true; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 425 | static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type, |
David Benjamin | e2ab21d | 2018-04-04 23:55:06 -0400 | [diff] [blame] | 426 | size_t in_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 427 | size_t ret = SSL3_RT_HEADER_LENGTH; |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 428 | if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 && |
| 429 | ssl_needs_record_splitting(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 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. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 434 | ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher()); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 435 | ret += SSL3_RT_HEADER_LENGTH - 1; |
| 436 | } else { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 437 | ret += ssl->s3->aead_write_ctx->ExplicitNonceLen(); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 438 | } |
| 439 | return ret; |
| 440 | } |
| 441 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 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() && |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 446 | ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 447 | // TLS 1.3 adds an extra byte for encrypted record type. |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 448 | extra_in_len = 1; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 449 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 450 | if (type == SSL3_RT_APPLICATION_DATA && // clang-format off |
| 451 | in_len > 1 && |
| 452 | ssl_needs_record_splitting(ssl)) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 453 | // With record splitting enabled, the first byte gets sealed into a separate |
| 454 | // record which is written into the prefix. |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 455 | in_len -= 1; |
| 456 | } |
| 457 | return ssl->s3->aead_write_ctx->SuffixLen(out_suffix_len, in_len, extra_in_len); |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 458 | } |
| 459 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 460 | // tls_seal_scatter_record seals a new record of type |type| and body |in| and |
| 461 | // splits it between |out_prefix|, |out|, and |out_suffix|. Exactly |
| 462 | // |tls_seal_scatter_prefix_len| bytes are written to |out_prefix|, |in_len| |
| 463 | // bytes to |out|, and |tls_seal_scatter_suffix_len| bytes to |out_suffix|. It |
| 464 | // returns one on success and zero on error. If enabled, |
| 465 | // |tls_seal_scatter_record| implements TLS 1.0 CBC 1/n-1 record splitting and |
| 466 | // may write two records concatenated. |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 467 | static bool tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out, |
David Benjamin | e8d0746 | 2017-10-12 18:09:20 -0400 | [diff] [blame] | 468 | uint8_t *out_suffix, uint8_t type, |
| 469 | const uint8_t *in, size_t in_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 470 | if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 && |
| 471 | ssl_needs_record_splitting(ssl)) { |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 472 | assert(ssl->s3->aead_write_ctx->ExplicitNonceLen() == 0); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 473 | const size_t prefix_len = SSL3_RT_HEADER_LENGTH; |
| 474 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 475 | // Write the 1-byte fragment into |out_prefix|. |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 476 | uint8_t *split_body = out_prefix + prefix_len; |
| 477 | uint8_t *split_suffix = split_body + 1; |
| 478 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 479 | if (!do_seal_record(ssl, out_prefix, split_body, split_suffix, type, in, |
| 480 | 1)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 481 | return false; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 482 | } |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 483 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 484 | size_t split_record_suffix_len; |
| 485 | if (!ssl->s3->aead_write_ctx->SuffixLen(&split_record_suffix_len, 1, 0)) { |
| 486 | assert(false); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 487 | return false; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 488 | } |
| 489 | const size_t split_record_len = prefix_len + 1 + split_record_suffix_len; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 490 | assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len( |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 491 | ssl->s3->aead_write_ctx->cipher()) == |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 492 | split_record_len); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 493 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 494 | // Write the n-1-byte fragment. The header gets split between |out_prefix| |
| 495 | // (header[:-1]) and |out| (header[-1:]). |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 496 | uint8_t tmp_prefix[SSL3_RT_HEADER_LENGTH]; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 497 | if (!do_seal_record(ssl, tmp_prefix, out + 1, out_suffix, type, in + 1, |
| 498 | in_len - 1)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 499 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 500 | } |
| 501 | assert(tls_seal_scatter_prefix_len(ssl, type, in_len) == |
| 502 | split_record_len + SSL3_RT_HEADER_LENGTH - 1); |
| 503 | OPENSSL_memcpy(out_prefix + split_record_len, tmp_prefix, |
| 504 | SSL3_RT_HEADER_LENGTH - 1); |
| 505 | OPENSSL_memcpy(out, tmp_prefix + SSL3_RT_HEADER_LENGTH - 1, 1); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 506 | return true; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 507 | } |
| 508 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 509 | return do_seal_record(ssl, out_prefix, out, out_suffix, type, in, in_len); |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 510 | } |
| 511 | |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 512 | bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, |
| 513 | size_t max_out_len, uint8_t type, const uint8_t *in, |
| 514 | size_t in_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 515 | if (buffers_alias(in, in_len, out, max_out_len)) { |
| 516 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 517 | return false; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 518 | } |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 519 | |
| 520 | const size_t prefix_len = tls_seal_scatter_prefix_len(ssl, type, in_len); |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 521 | size_t suffix_len; |
| 522 | if (!tls_seal_scatter_suffix_len(ssl, &suffix_len, type, in_len)) { |
| 523 | return false; |
| 524 | } |
| 525 | if (in_len + prefix_len < in_len || |
| 526 | prefix_len + in_len + suffix_len < prefix_len + in_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 527 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 528 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 529 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 530 | if (max_out_len < in_len + prefix_len + suffix_len) { |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 531 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 532 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | uint8_t *prefix = out; |
| 536 | uint8_t *body = out + prefix_len; |
| 537 | uint8_t *suffix = body + in_len; |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 538 | if (!tls_seal_scatter_record(ssl, prefix, body, suffix, type, in, in_len)) { |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 539 | return false; |
Martin Kreichgauer | 9f2bffb | 2017-06-30 05:29:50 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | *out_len = prefix_len + in_len + suffix_len; |
David Benjamin | 8525ff3 | 2018-09-05 18:44:15 -0500 | [diff] [blame] | 543 | return true; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 544 | } |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 545 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 546 | enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert, |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 547 | Span<const uint8_t> in) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 548 | // Alerts records may not contain fragmented or multiple alerts. |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 549 | if (in.size() != 2) { |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 550 | *out_alert = SSL_AD_DECODE_ERROR; |
| 551 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT); |
| 552 | return ssl_open_record_error; |
| 553 | } |
| 554 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 555 | ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_ALERT, in); |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 556 | |
| 557 | const uint8_t alert_level = in[0]; |
| 558 | const uint8_t alert_descr = in[1]; |
| 559 | |
| 560 | uint16_t alert = (alert_level << 8) | alert_descr; |
| 561 | ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert); |
| 562 | |
| 563 | if (alert_level == SSL3_AL_WARNING) { |
| 564 | if (alert_descr == SSL_AD_CLOSE_NOTIFY) { |
David Benjamin | 23c25d5 | 2017-10-06 16:02:47 -0400 | [diff] [blame] | 565 | ssl->s3->read_shutdown = ssl_shutdown_close_notify; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 566 | return ssl_open_record_close_notify; |
| 567 | } |
| 568 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 569 | // Warning alerts do not exist in TLS 1.3. |
David Benjamin | e8e84b9 | 2016-08-03 15:39:47 -0400 | [diff] [blame] | 570 | if (ssl->s3->have_version && |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 571 | ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
David Benjamin | e8e84b9 | 2016-08-03 15:39:47 -0400 | [diff] [blame] | 572 | *out_alert = SSL_AD_DECODE_ERROR; |
| 573 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT); |
| 574 | return ssl_open_record_error; |
| 575 | } |
| 576 | |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 577 | ssl->s3->warning_alert_count++; |
| 578 | if (ssl->s3->warning_alert_count > kMaxWarningAlerts) { |
| 579 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 580 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS); |
| 581 | return ssl_open_record_error; |
| 582 | } |
| 583 | return ssl_open_record_discard; |
| 584 | } |
| 585 | |
| 586 | if (alert_level == SSL3_AL_FATAL) { |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 587 | OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr); |
David Benjamin | 31aad2d | 2017-10-06 16:04:06 -0400 | [diff] [blame] | 588 | ERR_add_error_dataf("SSL alert number %d", alert_descr); |
David Benjamin | 751d1a1 | 2017-09-25 13:37:30 -0400 | [diff] [blame] | 589 | *out_alert = 0; // No alert to send back to the peer. |
| 590 | return ssl_open_record_error; |
David Benjamin | 728f354 | 2016-06-02 15:42:01 -0400 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 594 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE); |
| 595 | return ssl_open_record_error; |
| 596 | } |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 597 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 598 | OpenRecordResult OpenRecord(SSL *ssl, Span<uint8_t> *out, |
| 599 | size_t *out_record_len, uint8_t *out_alert, |
| 600 | const Span<uint8_t> in) { |
| 601 | // This API is a work in progress and currently only works for TLS 1.2 servers |
| 602 | // and below. |
| 603 | if (SSL_in_init(ssl) || |
| 604 | SSL_is_dtls(ssl) || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 605 | ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 606 | assert(false); |
| 607 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 608 | return OpenRecordResult::kError; |
| 609 | } |
| 610 | |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 611 | Span<uint8_t> plaintext; |
Adam Langley | 771df44 | 2017-10-11 16:15:34 -0700 | [diff] [blame] | 612 | uint8_t type = 0; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 613 | const ssl_open_record_t result = tls_open_record( |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 614 | ssl, &type, &plaintext, out_record_len, out_alert, in); |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 615 | |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 616 | switch (result) { |
| 617 | case ssl_open_record_success: |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 618 | if (type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_ALERT) { |
| 619 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 620 | return OpenRecordResult::kError; |
| 621 | } |
David Benjamin | c64d123 | 2017-10-04 18:14:28 -0400 | [diff] [blame] | 622 | *out = plaintext; |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 623 | return OpenRecordResult::kOK; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 624 | case ssl_open_record_discard: |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 625 | return OpenRecordResult::kDiscard; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 626 | case ssl_open_record_partial: |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 627 | return OpenRecordResult::kIncompleteRecord; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 628 | case ssl_open_record_close_notify: |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 629 | return OpenRecordResult::kAlertCloseNotify; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 630 | case ssl_open_record_error: |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 631 | return OpenRecordResult::kError; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 632 | } |
Martin Kreichgauer | 26ababb | 2017-08-03 12:02:34 -0700 | [diff] [blame] | 633 | assert(false); |
| 634 | return OpenRecordResult::kError; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 637 | size_t SealRecordPrefixLen(const SSL *ssl, const size_t record_len) { |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 638 | return tls_seal_scatter_prefix_len(ssl, SSL3_RT_APPLICATION_DATA, record_len); |
| 639 | } |
| 640 | |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 641 | size_t SealRecordSuffixLen(const SSL *ssl, const size_t plaintext_len) { |
| 642 | assert(plaintext_len <= SSL3_RT_MAX_PLAIN_LENGTH); |
| 643 | size_t suffix_len; |
| 644 | if (!tls_seal_scatter_suffix_len(ssl, &suffix_len, SSL3_RT_APPLICATION_DATA, |
| 645 | plaintext_len)) { |
| 646 | assert(false); |
| 647 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 648 | return 0; |
| 649 | } |
| 650 | assert(suffix_len <= SSL3_RT_MAX_ENCRYPTED_OVERHEAD); |
| 651 | return suffix_len; |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | bool SealRecord(SSL *ssl, const Span<uint8_t> out_prefix, |
| 655 | const Span<uint8_t> out, Span<uint8_t> out_suffix, |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 656 | const Span<const uint8_t> in) { |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 657 | // This API is a work in progress and currently only works for TLS 1.2 servers |
| 658 | // and below. |
| 659 | if (SSL_in_init(ssl) || |
| 660 | SSL_is_dtls(ssl) || |
David Benjamin | d1e3ce1 | 2017-10-06 18:31:15 -0400 | [diff] [blame] | 661 | ssl_protocol_version(ssl) > TLS1_2_VERSION) { |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 662 | assert(false); |
| 663 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | if (out_prefix.size() != SealRecordPrefixLen(ssl, in.size()) || |
| 668 | out.size() != in.size() || |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 669 | out_suffix.size() != SealRecordSuffixLen(ssl, in.size())) { |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 670 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 671 | return false; |
| 672 | } |
Martin Kreichgauer | abbf365 | 2017-07-21 16:27:54 -0700 | [diff] [blame] | 673 | return tls_seal_scatter_record(ssl, out_prefix.data(), out.data(), |
| 674 | out_suffix.data(), SSL3_RT_APPLICATION_DATA, |
| 675 | in.data(), in.size()); |
Martin Kreichgauer | 17c3057 | 2017-07-18 12:42:18 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 678 | BSSL_NAMESPACE_END |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 679 | |
| 680 | using namespace bssl; |
| 681 | |
| 682 | size_t SSL_max_seal_overhead(const SSL *ssl) { |
| 683 | if (SSL_is_dtls(ssl)) { |
| 684 | return dtls_max_seal_overhead(ssl, dtls1_use_current_epoch); |
| 685 | } |
| 686 | |
| 687 | size_t ret = SSL3_RT_HEADER_LENGTH; |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 688 | ret += ssl->s3->aead_write_ctx->MaxOverhead(); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 689 | // TLS 1.3 needs an extra byte for the encrypted record type. |
David Benjamin | cfc11c2 | 2017-07-18 22:45:18 -0400 | [diff] [blame] | 690 | if (!ssl->s3->aead_write_ctx->is_null_cipher() && |
Steven Valdez | c7d4d21 | 2017-09-11 13:53:08 -0400 | [diff] [blame] | 691 | ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) { |
David Benjamin | 86e95b8 | 2017-07-18 16:34:25 -0400 | [diff] [blame] | 692 | ret += 1; |
| 693 | } |
| 694 | if (ssl_needs_record_splitting(ssl)) { |
| 695 | ret *= 2; |
| 696 | } |
| 697 | return ret; |
| 698 | } |