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> |
| 116 | |
| 117 | #include "internal.h" |
| 118 | |
| 119 | |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 120 | /* kMaxEmptyRecords is the number of consecutive, empty records that will be |
| 121 | * processed. Without this limit an attacker could send empty records at a |
| 122 | * faster rate than we can process and cause record processing to loop |
| 123 | * forever. */ |
| 124 | static const uint8_t kMaxEmptyRecords = 32; |
| 125 | |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 126 | /* ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher |
| 127 | * state needs record-splitting and zero otherwise. */ |
| 128 | static int ssl_needs_record_splitting(const SSL *ssl) { |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 129 | return ssl->s3->aead_write_ctx != NULL && |
| 130 | ssl3_protocol_version(ssl) < TLS1_1_VERSION && |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 131 | (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 && |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 132 | SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher); |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 133 | } |
| 134 | |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 135 | int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) { |
| 136 | size_t i; |
| 137 | for (i = seq_len - 1; i < seq_len; i--) { |
| 138 | ++seq[i]; |
| 139 | if (seq[i] != 0) { |
| 140 | return 1; |
| 141 | } |
| 142 | } |
| 143 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 144 | return 0; |
| 145 | } |
| 146 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 147 | size_t ssl_record_prefix_len(const SSL *ssl) { |
| 148 | if (SSL_IS_DTLS(ssl)) { |
| 149 | return DTLS1_RT_HEADER_LENGTH + |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 150 | SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 151 | } else { |
| 152 | return SSL3_RT_HEADER_LENGTH + |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 153 | SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | size_t ssl_seal_prefix_len(const SSL *ssl) { |
| 158 | if (SSL_IS_DTLS(ssl)) { |
| 159 | return DTLS1_RT_HEADER_LENGTH + |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 160 | SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 161 | } else { |
| 162 | size_t ret = SSL3_RT_HEADER_LENGTH + |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 163 | SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx); |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 164 | if (ssl_needs_record_splitting(ssl)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 165 | ret += SSL3_RT_HEADER_LENGTH; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 166 | ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 167 | } |
| 168 | return ret; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | size_t ssl_max_seal_overhead(const SSL *ssl) { |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 173 | size_t ret = SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 174 | if (SSL_IS_DTLS(ssl)) { |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 175 | ret += DTLS1_RT_HEADER_LENGTH; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 176 | } else { |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 177 | ret += SSL3_RT_HEADER_LENGTH; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 178 | } |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 179 | /* TLS 1.3 needs an extra byte for the encrypted record type. */ |
| 180 | if (ssl->s3->have_version && |
| 181 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 182 | ret += 1; |
| 183 | } |
| 184 | if (!SSL_IS_DTLS(ssl) && ssl_needs_record_splitting(ssl)) { |
| 185 | ret *= 2; |
| 186 | } |
| 187 | return ret; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | enum ssl_open_record_t tls_open_record( |
| 191 | SSL *ssl, uint8_t *out_type, uint8_t *out, size_t *out_len, |
| 192 | size_t *out_consumed, uint8_t *out_alert, size_t max_out, const uint8_t *in, |
| 193 | size_t in_len) { |
| 194 | CBS cbs; |
| 195 | CBS_init(&cbs, in, in_len); |
| 196 | |
| 197 | /* Decode the record header. */ |
| 198 | uint8_t type; |
| 199 | uint16_t version, ciphertext_len; |
| 200 | if (!CBS_get_u8(&cbs, &type) || |
| 201 | !CBS_get_u16(&cbs, &version) || |
| 202 | !CBS_get_u16(&cbs, &ciphertext_len)) { |
| 203 | *out_consumed = SSL3_RT_HEADER_LENGTH; |
| 204 | return ssl_open_record_partial; |
| 205 | } |
| 206 | |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 207 | /* Check that the major version in the record matches. As of TLS 1.3, the |
| 208 | * minor version is no longer checked. */ |
| 209 | if ((version >> 8) != SSL3_VERSION_MAJOR) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 210 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
| 211 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 212 | return ssl_open_record_error; |
| 213 | } |
| 214 | |
| 215 | /* Check the ciphertext length. */ |
David Benjamin | 03f0005 | 2015-11-18 20:41:11 -0500 | [diff] [blame] | 216 | if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 217 | OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG); |
| 218 | *out_alert = SSL_AD_RECORD_OVERFLOW; |
| 219 | return ssl_open_record_error; |
| 220 | } |
| 221 | |
| 222 | /* Extract the body. */ |
| 223 | CBS body; |
| 224 | if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) { |
| 225 | *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len; |
| 226 | return ssl_open_record_partial; |
| 227 | } |
| 228 | |
| 229 | if (ssl->msg_callback != NULL) { |
| 230 | ssl->msg_callback(0 /* read */, 0, SSL3_RT_HEADER, in, |
| 231 | SSL3_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg); |
| 232 | } |
| 233 | |
| 234 | /* Decrypt the body. */ |
| 235 | size_t plaintext_len; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 236 | if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, &plaintext_len, max_out, |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 237 | type, version, ssl->s3->read_sequence, CBS_data(&body), |
| 238 | CBS_len(&body))) { |
| 239 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 240 | *out_alert = SSL_AD_BAD_RECORD_MAC; |
| 241 | return ssl_open_record_error; |
| 242 | } |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 243 | if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 244 | *out_alert = SSL_AD_INTERNAL_ERROR; |
| 245 | return ssl_open_record_error; |
| 246 | } |
| 247 | |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 248 | /* TLS 1.3 hides the record type inside the encrypted data. */ |
| 249 | if (ssl->s3->have_version && |
| 250 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION && |
| 251 | ssl->s3->aead_read_ctx != NULL) { |
| 252 | while (plaintext_len != 0 && out[plaintext_len - 1] == 0) { |
| 253 | plaintext_len--; |
| 254 | } |
| 255 | |
| 256 | if (plaintext_len == 0) { |
| 257 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC); |
| 258 | *out_alert = SSL_AD_DECRYPT_ERROR; |
| 259 | return ssl_open_record_error; |
| 260 | } |
| 261 | type = out[plaintext_len - 1]; |
| 262 | plaintext_len--; |
| 263 | } |
| 264 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 265 | /* Check the plaintext length. */ |
David Benjamin | 03f0005 | 2015-11-18 20:41:11 -0500 | [diff] [blame] | 266 | if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 267 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
| 268 | *out_alert = SSL_AD_RECORD_OVERFLOW; |
| 269 | return ssl_open_record_error; |
| 270 | } |
| 271 | |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 272 | /* Limit the number of consecutive empty records. */ |
| 273 | if (plaintext_len == 0) { |
| 274 | ssl->s3->empty_record_count++; |
| 275 | if (ssl->s3->empty_record_count > kMaxEmptyRecords) { |
| 276 | OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS); |
| 277 | *out_alert = SSL_AD_UNEXPECTED_MESSAGE; |
| 278 | return ssl_open_record_error; |
| 279 | } |
| 280 | /* Apart from the limit, empty records are returned up to the caller. This |
| 281 | * allows the caller to reject records of the wrong type. */ |
| 282 | } else { |
| 283 | ssl->s3->empty_record_count = 0; |
| 284 | } |
| 285 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 286 | *out_type = type; |
| 287 | *out_len = plaintext_len; |
| 288 | *out_consumed = in_len - CBS_len(&cbs); |
| 289 | return ssl_open_record_success; |
| 290 | } |
| 291 | |
| 292 | static int do_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, |
| 293 | size_t max_out, uint8_t type, const uint8_t *in, |
| 294 | size_t in_len) { |
| 295 | if (max_out < SSL3_RT_HEADER_LENGTH) { |
| 296 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 297 | return 0; |
| 298 | } |
| 299 | /* Check the record header does not alias any part of the input. |
| 300 | * |SSL_AEAD_CTX_seal| will internally enforce other aliasing requirements. */ |
| 301 | if (in < out + SSL3_RT_HEADER_LENGTH && out < in + in_len) { |
| 302 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | out[0] = type; |
| 307 | |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 308 | /* The TLS record-layer version number is meaningless and, starting in |
| 309 | * TLS 1.3, is frozen at TLS 1.0. But for historical reasons, SSL 3.0 |
| 310 | * ClientHellos should use SSL 3.0 and pre-TLS-1.3 expects the version |
| 311 | * to change after version negotiation. */ |
| 312 | uint16_t wire_version = TLS1_VERSION; |
| 313 | if (ssl->version == SSL3_VERSION || |
| 314 | (ssl->s3->have_version && ssl3_protocol_version(ssl) < TLS1_3_VERSION)) { |
| 315 | wire_version = ssl->version; |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 316 | } |
| 317 | out[1] = wire_version >> 8; |
| 318 | out[2] = wire_version & 0xff; |
| 319 | |
| 320 | size_t ciphertext_len; |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 321 | if (!SSL_AEAD_CTX_seal(ssl->s3->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH, |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 322 | &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH, |
| 323 | type, wire_version, ssl->s3->write_sequence, in, |
| 324 | in_len) || |
David Benjamin | 1db2156 | 2015-12-25 15:11:39 -0500 | [diff] [blame] | 325 | !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | if (ciphertext_len >= 1 << 16) { |
| 330 | OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW); |
| 331 | return 0; |
| 332 | } |
| 333 | out[3] = ciphertext_len >> 8; |
| 334 | out[4] = ciphertext_len & 0xff; |
| 335 | |
| 336 | *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len; |
| 337 | |
| 338 | if (ssl->msg_callback) { |
| 339 | ssl->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, |
| 340 | SSL3_RT_HEADER_LENGTH, ssl, ssl->msg_callback_arg); |
| 341 | } |
| 342 | |
| 343 | return 1; |
| 344 | } |
| 345 | |
| 346 | int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, |
| 347 | uint8_t type, const uint8_t *in, size_t in_len) { |
| 348 | size_t frag_len = 0; |
Steven Valdez | 66af3b0 | 2016-06-01 14:07:09 -0400 | [diff] [blame^] | 349 | |
| 350 | /* TLS 1.3 hides the actual record type inside the encrypted data. */ |
| 351 | if (ssl->s3->have_version && |
| 352 | ssl3_protocol_version(ssl) >= TLS1_3_VERSION && |
| 353 | ssl->s3->aead_read_ctx != NULL) { |
| 354 | size_t padding = SSL3_RT_HEADER_LENGTH + 1; |
| 355 | |
| 356 | if (in_len > in_len + padding || max_out < in_len + padding) { |
| 357 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | memmove(out + SSL3_RT_HEADER_LENGTH, in, in_len); |
| 362 | out[SSL3_RT_HEADER_LENGTH + in_len] = type; |
| 363 | in = out + SSL3_RT_HEADER_LENGTH; |
| 364 | type = SSL3_RT_APPLICATION_DATA; |
| 365 | in_len++; |
| 366 | } |
| 367 | |
David Benjamin | d9f0671 | 2015-12-06 16:07:47 -0500 | [diff] [blame] | 368 | if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 && |
| 369 | ssl_needs_record_splitting(ssl)) { |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 370 | /* |do_seal_record| will notice if it clobbers |in[0]|, but not if it |
| 371 | * aliases the rest of |in|. */ |
| 372 | if (in + 1 <= out && out < in + in_len) { |
| 373 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
| 374 | return 0; |
| 375 | } |
| 376 | /* Ensure |do_seal_record| does not write beyond |in[0]|. */ |
| 377 | size_t frag_max_out = max_out; |
David Benjamin | a1fadd3 | 2015-08-28 18:11:00 -0400 | [diff] [blame] | 378 | if (out <= in + 1 && in + 1 < out + frag_max_out) { |
| 379 | frag_max_out = (size_t)(in + 1 - out); |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 380 | } |
| 381 | if (!do_seal_record(ssl, out, &frag_len, frag_max_out, type, in, 1)) { |
| 382 | return 0; |
| 383 | } |
| 384 | in++; |
| 385 | in_len--; |
| 386 | out += frag_len; |
| 387 | max_out -= frag_len; |
| 388 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 389 | #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 390 | assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len( |
| 391 | ssl->s3->aead_write_ctx->cipher) == |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 392 | frag_len); |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 393 | #endif |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | if (!do_seal_record(ssl, out, out_len, max_out, type, in, in_len)) { |
| 397 | return 0; |
| 398 | } |
| 399 | *out_len += frag_len; |
| 400 | return 1; |
| 401 | } |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 402 | |
| 403 | void ssl_set_read_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) { |
| 404 | if (SSL_IS_DTLS(ssl)) { |
| 405 | ssl->d1->r_epoch++; |
| 406 | memset(&ssl->d1->bitmap, 0, sizeof(ssl->d1->bitmap)); |
| 407 | } |
| 408 | memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence)); |
| 409 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 410 | SSL_AEAD_CTX_free(ssl->s3->aead_read_ctx); |
| 411 | ssl->s3->aead_read_ctx = aead_ctx; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | void ssl_set_write_state(SSL *ssl, SSL_AEAD_CTX *aead_ctx) { |
| 415 | if (SSL_IS_DTLS(ssl)) { |
| 416 | ssl->d1->w_epoch++; |
| 417 | memcpy(ssl->d1->last_write_sequence, ssl->s3->write_sequence, |
| 418 | sizeof(ssl->s3->write_sequence)); |
| 419 | } |
| 420 | memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence)); |
| 421 | |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 422 | SSL_AEAD_CTX_free(ssl->s3->aead_write_ctx); |
| 423 | ssl->s3->aead_write_ctx = aead_ctx; |
David Benjamin | 4119d42 | 2015-12-25 15:34:23 -0500 | [diff] [blame] | 424 | } |