blob: 389795899b5c93a320023590d7baad42621ca85a [file] [log] [blame]
David Benjaminb8d28cf2015-07-28 21:34:45 -04001/* 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 Benjamin4119d422015-12-25 15:34:23 -0500112#include <string.h>
David Benjaminb8d28cf2015-07-28 21:34:45 -0400113
114#include <openssl/bytestring.h>
115#include <openssl/err.h>
David Benjamin728f3542016-06-02 15:42:01 -0400116#include <openssl/mem.h>
David Benjaminb8d28cf2015-07-28 21:34:45 -0400117
118#include "internal.h"
David Benjamin1a01e1f2016-06-08 18:31:24 -0400119#include "../crypto/internal.h"
David Benjaminb8d28cf2015-07-28 21:34:45 -0400120
121
David Benjamin4cf369b2015-08-22 01:35:43 -0400122/* kMaxEmptyRecords is the number of consecutive, empty records that will be
123 * processed. Without this limit an attacker could send empty records at a
124 * faster rate than we can process and cause record processing to loop
125 * forever. */
126static const uint8_t kMaxEmptyRecords = 32;
127
David Benjamin728f3542016-06-02 15:42:01 -0400128/* kMaxWarningAlerts is the number of consecutive warning alerts that will be
129 * processed. */
130static const uint8_t kMaxWarningAlerts = 4;
131
David Benjamind9f06712015-12-06 16:07:47 -0500132/* ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
133 * state needs record-splitting and zero otherwise. */
134static int ssl_needs_record_splitting(const SSL *ssl) {
David Benjamina1e9cab2015-12-30 00:08:49 -0500135 return ssl->s3->aead_write_ctx != NULL &&
136 ssl3_protocol_version(ssl) < TLS1_1_VERSION &&
David Benjamind9f06712015-12-06 16:07:47 -0500137 (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
David Benjamin79978df2015-12-25 15:56:49 -0500138 SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher);
David Benjamind9f06712015-12-06 16:07:47 -0500139}
140
David Benjamin1db21562015-12-25 15:11:39 -0500141int ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
142 size_t i;
143 for (i = seq_len - 1; i < seq_len; i--) {
144 ++seq[i];
145 if (seq[i] != 0) {
146 return 1;
147 }
148 }
149 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
150 return 0;
151}
152
David Benjaminb8d28cf2015-07-28 21:34:45 -0400153size_t ssl_record_prefix_len(const SSL *ssl) {
154 if (SSL_IS_DTLS(ssl)) {
155 return DTLS1_RT_HEADER_LENGTH +
David Benjamin79978df2015-12-25 15:56:49 -0500156 SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400157 } else {
158 return SSL3_RT_HEADER_LENGTH +
David Benjamin79978df2015-12-25 15:56:49 -0500159 SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_read_ctx);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400160 }
161}
162
David Benjamin1a01e1f2016-06-08 18:31:24 -0400163size_t ssl_seal_align_prefix_len(const SSL *ssl) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400164 if (SSL_IS_DTLS(ssl)) {
165 return DTLS1_RT_HEADER_LENGTH +
David Benjamin79978df2015-12-25 15:56:49 -0500166 SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400167 } else {
168 size_t ret = SSL3_RT_HEADER_LENGTH +
David Benjamin79978df2015-12-25 15:56:49 -0500169 SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx);
David Benjamind9f06712015-12-06 16:07:47 -0500170 if (ssl_needs_record_splitting(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400171 ret += SSL3_RT_HEADER_LENGTH;
David Benjamin79978df2015-12-25 15:56:49 -0500172 ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400173 }
174 return ret;
175 }
176}
177
178size_t ssl_max_seal_overhead(const SSL *ssl) {
Steven Valdez66af3b02016-06-01 14:07:09 -0400179 size_t ret = SSL_AEAD_CTX_max_overhead(ssl->s3->aead_write_ctx);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400180 if (SSL_IS_DTLS(ssl)) {
Steven Valdez66af3b02016-06-01 14:07:09 -0400181 ret += DTLS1_RT_HEADER_LENGTH;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400182 } else {
Steven Valdez66af3b02016-06-01 14:07:09 -0400183 ret += SSL3_RT_HEADER_LENGTH;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400184 }
Steven Valdez66af3b02016-06-01 14:07:09 -0400185 /* TLS 1.3 needs an extra byte for the encrypted record type. */
186 if (ssl->s3->have_version &&
187 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
188 ret += 1;
189 }
190 if (!SSL_IS_DTLS(ssl) && ssl_needs_record_splitting(ssl)) {
191 ret *= 2;
192 }
193 return ret;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400194}
195
David Benjamina7810c12016-06-06 18:54:51 -0400196enum ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
197 size_t *out_consumed, uint8_t *out_alert,
198 uint8_t *in, size_t in_len) {
David Benjamin728f3542016-06-02 15:42:01 -0400199 *out_consumed = 0;
200
David Benjaminb8d28cf2015-07-28 21:34:45 -0400201 CBS cbs;
202 CBS_init(&cbs, in, in_len);
203
204 /* Decode the record header. */
205 uint8_t type;
206 uint16_t version, ciphertext_len;
207 if (!CBS_get_u8(&cbs, &type) ||
208 !CBS_get_u16(&cbs, &version) ||
209 !CBS_get_u16(&cbs, &ciphertext_len)) {
210 *out_consumed = SSL3_RT_HEADER_LENGTH;
211 return ssl_open_record_partial;
212 }
213
Steven Valdez66af3b02016-06-01 14:07:09 -0400214 /* Check that the major version in the record matches. As of TLS 1.3, the
215 * minor version is no longer checked. */
216 if ((version >> 8) != SSL3_VERSION_MAJOR) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400217 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
218 *out_alert = SSL_AD_PROTOCOL_VERSION;
219 return ssl_open_record_error;
220 }
221
222 /* Check the ciphertext length. */
David Benjamin03f00052015-11-18 20:41:11 -0500223 if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400224 OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
225 *out_alert = SSL_AD_RECORD_OVERFLOW;
226 return ssl_open_record_error;
227 }
228
229 /* Extract the body. */
230 CBS body;
231 if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
232 *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
233 return ssl_open_record_partial;
234 }
235
David Benjamin4e9cc712016-06-01 20:16:03 -0400236 ssl_do_msg_callback(ssl, 0 /* read */, 0, SSL3_RT_HEADER, in,
237 SSL3_RT_HEADER_LENGTH);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400238
David Benjamina7810c12016-06-06 18:54:51 -0400239 /* Decrypt the body in-place. */
240 if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, type, version,
241 ssl->s3->read_sequence, (uint8_t *)CBS_data(&body),
David Benjaminb8d28cf2015-07-28 21:34:45 -0400242 CBS_len(&body))) {
243 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
244 *out_alert = SSL_AD_BAD_RECORD_MAC;
245 return ssl_open_record_error;
246 }
David Benjamin728f3542016-06-02 15:42:01 -0400247 *out_consumed = in_len - CBS_len(&cbs);
248
David Benjamin1db21562015-12-25 15:11:39 -0500249 if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400250 *out_alert = SSL_AD_INTERNAL_ERROR;
251 return ssl_open_record_error;
252 }
253
Steven Valdez66af3b02016-06-01 14:07:09 -0400254 /* TLS 1.3 hides the record type inside the encrypted data. */
255 if (ssl->s3->have_version &&
256 ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
257 ssl->s3->aead_read_ctx != NULL) {
David Benjaminc9ae27c2016-06-24 22:56:37 -0400258 /* The outer record type is always application_data. */
259 if (type != SSL3_RT_APPLICATION_DATA) {
260 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE);
261 *out_alert = SSL_AD_DECODE_ERROR;
262 return ssl_open_record_error;
263 }
264
David Benjamina7810c12016-06-06 18:54:51 -0400265 do {
266 if (!CBS_get_last_u8(out, &type)) {
267 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
268 *out_alert = SSL_AD_DECRYPT_ERROR;
269 return ssl_open_record_error;
270 }
271 } while (type == 0);
Steven Valdez66af3b02016-06-01 14:07:09 -0400272 }
273
David Benjaminb8d28cf2015-07-28 21:34:45 -0400274 /* Check the plaintext length. */
David Benjamina7810c12016-06-06 18:54:51 -0400275 if (CBS_len(out) > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400276 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
277 *out_alert = SSL_AD_RECORD_OVERFLOW;
278 return ssl_open_record_error;
279 }
280
David Benjamin4cf369b2015-08-22 01:35:43 -0400281 /* Limit the number of consecutive empty records. */
David Benjamina7810c12016-06-06 18:54:51 -0400282 if (CBS_len(out) == 0) {
David Benjamin4cf369b2015-08-22 01:35:43 -0400283 ssl->s3->empty_record_count++;
284 if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
285 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
286 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
287 return ssl_open_record_error;
288 }
289 /* Apart from the limit, empty records are returned up to the caller. This
290 * allows the caller to reject records of the wrong type. */
291 } else {
292 ssl->s3->empty_record_count = 0;
293 }
294
David Benjamin728f3542016-06-02 15:42:01 -0400295 if (type == SSL3_RT_ALERT) {
David Benjamina7810c12016-06-06 18:54:51 -0400296 return ssl_process_alert(ssl, out_alert, CBS_data(out), CBS_len(out));
David Benjamin728f3542016-06-02 15:42:01 -0400297 }
298
299 ssl->s3->warning_alert_count = 0;
300
David Benjaminb8d28cf2015-07-28 21:34:45 -0400301 *out_type = type;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400302 return ssl_open_record_success;
303}
304
305static int do_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
306 size_t max_out, uint8_t type, const uint8_t *in,
307 size_t in_len) {
308 if (max_out < SSL3_RT_HEADER_LENGTH) {
309 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
310 return 0;
311 }
David Benjamin1a01e1f2016-06-08 18:31:24 -0400312
313 /* Either |in| and |out| don't alias or |in| aligns with the
314 * ciphertext. |tls_seal_record| forbids aliasing, but TLS 1.3 aliases them
315 * internally. */
316 assert(!buffers_alias(in, in_len, out, max_out) ||
317 in ==
318 out + SSL3_RT_HEADER_LENGTH +
319 SSL_AEAD_CTX_explicit_nonce_len(ssl->s3->aead_write_ctx));
David Benjaminb8d28cf2015-07-28 21:34:45 -0400320
321 out[0] = type;
322
Steven Valdez66af3b02016-06-01 14:07:09 -0400323 /* The TLS record-layer version number is meaningless and, starting in
324 * TLS 1.3, is frozen at TLS 1.0. But for historical reasons, SSL 3.0
325 * ClientHellos should use SSL 3.0 and pre-TLS-1.3 expects the version
326 * to change after version negotiation. */
327 uint16_t wire_version = TLS1_VERSION;
328 if (ssl->version == SSL3_VERSION ||
329 (ssl->s3->have_version && ssl3_protocol_version(ssl) < TLS1_3_VERSION)) {
330 wire_version = ssl->version;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400331 }
332 out[1] = wire_version >> 8;
333 out[2] = wire_version & 0xff;
334
335 size_t ciphertext_len;
David Benjamin79978df2015-12-25 15:56:49 -0500336 if (!SSL_AEAD_CTX_seal(ssl->s3->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400337 &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
338 type, wire_version, ssl->s3->write_sequence, in,
339 in_len) ||
David Benjamin1db21562015-12-25 15:11:39 -0500340 !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400341 return 0;
342 }
343
344 if (ciphertext_len >= 1 << 16) {
345 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
346 return 0;
347 }
348 out[3] = ciphertext_len >> 8;
349 out[4] = ciphertext_len & 0xff;
350
351 *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
352
David Benjamin4e9cc712016-06-01 20:16:03 -0400353 ssl_do_msg_callback(ssl, 1 /* write */, 0, SSL3_RT_HEADER, out,
354 SSL3_RT_HEADER_LENGTH);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400355 return 1;
356}
357
358int tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
359 uint8_t type, const uint8_t *in, size_t in_len) {
David Benjamin1a01e1f2016-06-08 18:31:24 -0400360 if (buffers_alias(in, in_len, out, max_out)) {
361 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
362 return 0;
363 }
364
David Benjaminb8d28cf2015-07-28 21:34:45 -0400365 size_t frag_len = 0;
Steven Valdez66af3b02016-06-01 14:07:09 -0400366
367 /* TLS 1.3 hides the actual record type inside the encrypted data. */
368 if (ssl->s3->have_version &&
369 ssl3_protocol_version(ssl) >= TLS1_3_VERSION &&
David Benjamin34fce882016-06-16 17:03:03 -0400370 ssl->s3->aead_write_ctx != NULL) {
Steven Valdez66af3b02016-06-01 14:07:09 -0400371 size_t padding = SSL3_RT_HEADER_LENGTH + 1;
372
373 if (in_len > in_len + padding || max_out < in_len + padding) {
374 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
375 return 0;
376 }
377
378 memmove(out + SSL3_RT_HEADER_LENGTH, in, in_len);
379 out[SSL3_RT_HEADER_LENGTH + in_len] = type;
380 in = out + SSL3_RT_HEADER_LENGTH;
381 type = SSL3_RT_APPLICATION_DATA;
382 in_len++;
383 }
384
David Benjamind9f06712015-12-06 16:07:47 -0500385 if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
386 ssl_needs_record_splitting(ssl)) {
David Benjamin1a01e1f2016-06-08 18:31:24 -0400387 if (!do_seal_record(ssl, out, &frag_len, max_out, type, in, 1)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400388 return 0;
389 }
390 in++;
391 in_len--;
392 out += frag_len;
393 max_out -= frag_len;
394
David Benjaminbf82aed2016-03-01 22:57:40 -0500395#if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
David Benjamin79978df2015-12-25 15:56:49 -0500396 assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
397 ssl->s3->aead_write_ctx->cipher) ==
David Benjaminb8d28cf2015-07-28 21:34:45 -0400398 frag_len);
David Benjaminbf82aed2016-03-01 22:57:40 -0500399#endif
David Benjaminb8d28cf2015-07-28 21:34:45 -0400400 }
401
402 if (!do_seal_record(ssl, out, out_len, max_out, type, in, in_len)) {
403 return 0;
404 }
405 *out_len += frag_len;
406 return 1;
407}
David Benjamin4119d422015-12-25 15:34:23 -0500408
David Benjamin728f3542016-06-02 15:42:01 -0400409enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
410 const uint8_t *in, size_t in_len) {
411 /* Alerts records may not contain fragmented or multiple alerts. */
412 if (in_len != 2) {
413 *out_alert = SSL_AD_DECODE_ERROR;
414 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
415 return ssl_open_record_error;
416 }
417
418 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_ALERT, in, in_len);
419
420 const uint8_t alert_level = in[0];
421 const uint8_t alert_descr = in[1];
422
423 uint16_t alert = (alert_level << 8) | alert_descr;
424 ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
425
426 if (alert_level == SSL3_AL_WARNING) {
427 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
428 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
429 return ssl_open_record_close_notify;
430 }
431
432 ssl->s3->warning_alert_count++;
433 if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
434 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
435 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
436 return ssl_open_record_error;
437 }
438 return ssl_open_record_discard;
439 }
440
441 if (alert_level == SSL3_AL_FATAL) {
442 ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
443 SSL_CTX_remove_session(ssl->ctx, ssl->session);
444
445 char tmp[16];
446 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
447 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
448 ERR_add_error_data(2, "SSL alert number ", tmp);
449 return ssl_open_record_fatal_alert;
450 }
451
452 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
453 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
454 return ssl_open_record_error;
455}