blob: b9eebf323932a49916670f1c23bdcb14ba93b8c6 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400109#include <openssl/ssl.h>
110
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <assert.h>
Adam Langley87750b42014-06-20 12:00:00 -0700112#include <limits.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700114
115#include <openssl/buf.h>
116#include <openssl/err.h>
117#include <openssl/evp.h>
118#include <openssl/mem.h>
119#include <openssl/rand.h>
120
David Benjamin17cf2cb2016-12-13 01:07:13 -0500121#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700123
Adam Langleyfcf25832014-12-18 17:42:32 -0800124
David Benjamin86e95b82017-07-18 16:34:25 -0400125namespace bssl {
126
David Benjamin0d56f882015-12-19 17:05:56 -0500127static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjaminc11ea9422017-08-29 16:33:21 -0400129// ssl3_get_record reads a new input record. On success, it places it in
130// |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
131// more data is needed.
David Benjaminb8d28cf2015-07-28 21:34:45 -0400132static int ssl3_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700133again:
David Benjaminfa214e42016-05-10 17:03:10 -0400134 switch (ssl->s3->recv_shutdown) {
135 case ssl_shutdown_none:
136 break;
137 case ssl_shutdown_fatal_alert:
138 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
139 return -1;
140 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500141 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500142 }
143
David Benjamina7810c12016-06-06 18:54:51 -0400144 CBS body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800145 uint8_t type, alert = SSL_AD_DECODE_ERROR;
David Benjamina7810c12016-06-06 18:54:51 -0400146 size_t consumed;
David Benjamin728f3542016-06-02 15:42:01 -0400147 enum ssl_open_record_t open_ret =
David Benjamina7810c12016-06-06 18:54:51 -0400148 tls_open_record(ssl, &type, &body, &consumed, &alert,
David Benjamin728f3542016-06-02 15:42:01 -0400149 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
150 if (open_ret != ssl_open_record_partial) {
151 ssl_read_buffer_consume(ssl, consumed);
152 }
153 switch (open_ret) {
David Benjamina7810c12016-06-06 18:54:51 -0400154 case ssl_open_record_partial: {
155 int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
David Benjamin728f3542016-06-02 15:42:01 -0400156 if (read_ret <= 0) {
157 return read_ret;
158 }
159 goto again;
David Benjamina7810c12016-06-06 18:54:51 -0400160 }
David Benjamin6a08da22015-05-08 22:58:12 -0400161
David Benjamine8703a32017-07-09 16:17:55 -0400162 case ssl_open_record_success: {
David Benjamina7810c12016-06-06 18:54:51 -0400163 if (CBS_len(&body) > 0xffff) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400164 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
165 return -1;
166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjaminb8d28cf2015-07-28 21:34:45 -0400168 SSL3_RECORD *rr = &ssl->s3->rrec;
169 rr->type = type;
David Benjamina7810c12016-06-06 18:54:51 -0400170 rr->length = (uint16_t)CBS_len(&body);
171 rr->data = (uint8_t *)CBS_data(&body);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400172 return 1;
David Benjamine8703a32017-07-09 16:17:55 -0400173 }
Adam Langley95c29f32014-06-20 12:00:00 -0700174
David Benjamin728f3542016-06-02 15:42:01 -0400175 case ssl_open_record_discard:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400176 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700177
David Benjamin728f3542016-06-02 15:42:01 -0400178 case ssl_open_record_close_notify:
179 return 0;
180
181 case ssl_open_record_fatal_alert:
182 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjaminb8d28cf2015-07-28 21:34:45 -0400184 case ssl_open_record_error:
185 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
186 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 }
Adam Langley95c29f32014-06-20 12:00:00 -0700188
David Benjaminb8d28cf2015-07-28 21:34:45 -0400189 assert(0);
190 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
191 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800192}
Adam Langley95c29f32014-06-20 12:00:00 -0700193
David Benjamin74795b32017-08-31 15:13:12 -0400194int ssl3_write_app_data(SSL *ssl, bool *out_needs_handshake, const uint8_t *buf,
Steven Valdeze831a812017-03-09 14:56:07 -0500195 int len) {
Steven Valdez681eb6a2016-12-19 13:19:29 -0500196 assert(ssl_can_write(ssl));
David Benjamincfc11c22017-07-18 22:45:18 -0400197 assert(!ssl->s3->aead_write_ctx->is_null_cipher());
David Benjamind7ac1432016-03-10 00:41:25 -0500198
David Benjamin74795b32017-08-31 15:13:12 -0400199 *out_needs_handshake = false;
Steven Valdeze831a812017-03-09 14:56:07 -0500200
David Benjamina8571592016-03-10 01:41:55 -0500201 unsigned tot, n, nw;
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamin0d56f882015-12-19 17:05:56 -0500203 assert(ssl->s3->wnum <= INT_MAX);
204 tot = ssl->s3->wnum;
205 ssl->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700206
David Benjaminc11ea9422017-08-29 16:33:21 -0400207 // Ensure that if we end up with a smaller value of data to write out than
208 // the the original len from a write which didn't complete for non-blocking
209 // I/O and also somehow ended up avoiding the check for this in
210 // ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
211 // end up with (len-tot) as a large number that will then promptly send
212 // beyond the end of the users buffer ... so we trap and report the error in
213 // a way the user will notice.
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 if (len < 0 || (size_t)len < tot) {
David Benjamin3570d732015-06-29 00:28:17 -0400215 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 return -1;
217 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700218
Steven Valdeze831a812017-03-09 14:56:07 -0500219 const int is_early_data_write =
220 !ssl->server && SSL_in_early_data(ssl) && ssl->s3->hs->can_early_write;
221
David Benjamin8671c472017-02-02 15:05:35 -0500222 n = len - tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800223 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400224 // max contains the maximum number of bytes that we can put into a record.
David Benjamin0d56f882015-12-19 17:05:56 -0500225 unsigned max = ssl->max_send_fragment;
Steven Valdeze831a812017-03-09 14:56:07 -0500226 if (is_early_data_write && max > ssl->session->ticket_max_early_data -
227 ssl->s3->hs->early_data_written) {
228 max = ssl->session->ticket_max_early_data - ssl->s3->hs->early_data_written;
229 if (max == 0) {
230 ssl->s3->wnum = tot;
David Benjaminfd45ee72017-08-31 14:49:09 -0400231 ssl->s3->hs->can_early_write = false;
David Benjamin74795b32017-08-31 15:13:12 -0400232 *out_needs_handshake = true;
Steven Valdeze831a812017-03-09 14:56:07 -0500233 return -1;
234 }
235 }
236
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 if (n > max) {
238 nw = max;
239 } else {
240 nw = n;
241 }
Adam Langley95c29f32014-06-20 12:00:00 -0700242
David Benjamin8671c472017-02-02 15:05:35 -0500243 int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &buf[tot], nw);
David Benjamina8571592016-03-10 01:41:55 -0500244 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500245 ssl->s3->wnum = tot;
David Benjamina8571592016-03-10 01:41:55 -0500246 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 }
Adam Langley95c29f32014-06-20 12:00:00 -0700248
Steven Valdeze831a812017-03-09 14:56:07 -0500249 if (is_early_data_write) {
250 ssl->s3->hs->early_data_written += ret;
251 }
252
David Benjamin8671c472017-02-02 15:05:35 -0500253 if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
David Benjamina8571592016-03-10 01:41:55 -0500254 return tot + ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
David Benjamina8571592016-03-10 01:41:55 -0500257 n -= ret;
258 tot += ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800259 }
260}
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamin0d56f882015-12-19 17:05:56 -0500262static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
David Benjamin2e0901b2015-11-02 17:50:10 -0500263 unsigned int len) {
David Benjamin0d56f882015-12-19 17:05:56 -0500264 if (ssl->s3->wpend_tot > (int)len ||
David Benjaminc6722cd2016-10-24 20:13:20 -0400265 (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
266 ssl->s3->wpend_buf != buf) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500267 ssl->s3->wpend_type != type) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500268 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
269 return -1;
270 }
271
David Benjamin0d56f882015-12-19 17:05:56 -0500272 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500273 if (ret <= 0) {
274 return ret;
275 }
David Benjamin046bc1f2017-08-31 15:06:42 -0400276 ssl->s3->wpend_pending = false;
David Benjamin0d56f882015-12-19 17:05:56 -0500277 return ssl->s3->wpend_ret;
David Benjamin2e0901b2015-11-02 17:50:10 -0500278}
279
David Benjaminc11ea9422017-08-29 16:33:21 -0400280// do_ssl3_write writes an SSL record of the given type.
David Benjamin0d56f882015-12-19 17:05:56 -0500281static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400282 // If there is still data from the previous record, flush it.
Steven Valdeze831a812017-03-09 14:56:07 -0500283 if (ssl->s3->wpend_pending) {
David Benjamin0d56f882015-12-19 17:05:56 -0500284 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjaminb8d28cf2015-07-28 21:34:45 -0400287 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
288 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800289 return -1;
290 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 if (len == 0) {
293 return 0;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
David Benjaminbbba9392017-04-06 12:54:12 -0400296 size_t flight_len = 0;
297 if (ssl->s3->pending_flight != NULL) {
298 flight_len =
299 ssl->s3->pending_flight->length - ssl->s3->pending_flight_offset;
300 }
301
David Benjaminda863602016-11-04 15:44:28 -0400302 size_t max_out = len + SSL_max_seal_overhead(ssl);
David Benjaminbbba9392017-04-06 12:54:12 -0400303 if (max_out < len || max_out + flight_len < max_out) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400304 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin9faafda2015-04-04 19:23:10 -0400305 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 }
David Benjaminbbba9392017-04-06 12:54:12 -0400307 max_out += flight_len;
308
David Benjaminb8d28cf2015-07-28 21:34:45 -0400309 uint8_t *out;
310 size_t ciphertext_len;
David Benjaminbbba9392017-04-06 12:54:12 -0400311 if (!ssl_write_buffer_init(ssl, &out, max_out)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400312 return -1;
313 }
David Benjaminbbba9392017-04-06 12:54:12 -0400314
David Benjaminc11ea9422017-08-29 16:33:21 -0400315 // Add any unflushed handshake data as a prefix. This may be a KeyUpdate
316 // acknowledgment or 0-RTT key change messages. |pending_flight| must be clear
317 // when data is added to |write_buffer| or it will be written in the wrong
318 // order.
David Benjaminbbba9392017-04-06 12:54:12 -0400319 if (ssl->s3->pending_flight != NULL) {
320 OPENSSL_memcpy(
321 out, ssl->s3->pending_flight->data + ssl->s3->pending_flight_offset,
322 flight_len);
323 BUF_MEM_free(ssl->s3->pending_flight);
324 ssl->s3->pending_flight = NULL;
325 ssl->s3->pending_flight_offset = 0;
326 }
327
328 if (!tls_seal_record(ssl, out + flight_len, &ciphertext_len,
329 max_out - flight_len, type, buf, len)) {
330 return -1;
331 }
332 ssl_write_buffer_set_len(ssl, flight_len + ciphertext_len);
333
David Benjaminc11ea9422017-08-29 16:33:21 -0400334 // Now that we've made progress on the connection, uncork KeyUpdate
335 // acknowledgments.
David Benjamin046bc1f2017-08-31 15:06:42 -0400336 ssl->s3->key_update_pending = false;
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjaminc11ea9422017-08-29 16:33:21 -0400338 // Memorize arguments so that ssl3_write_pending can detect bad write retries
339 // later.
David Benjamin0d56f882015-12-19 17:05:56 -0500340 ssl->s3->wpend_tot = len;
341 ssl->s3->wpend_buf = buf;
342 ssl->s3->wpend_type = type;
343 ssl->s3->wpend_ret = len;
David Benjamin046bc1f2017-08-31 15:06:42 -0400344 ssl->s3->wpend_pending = true;
Adam Langleyfcf25832014-12-18 17:42:32 -0800345
David Benjaminc11ea9422017-08-29 16:33:21 -0400346 // We now just need to write the buffer.
David Benjamin0d56f882015-12-19 17:05:56 -0500347 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800348}
Adam Langley95c29f32014-06-20 12:00:00 -0700349
David Benjamin163f29a2016-07-28 11:05:58 -0400350static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) {
351 SSL3_RECORD *rr = &ssl->s3->rrec;
352
353 if (len <= 0) {
354 return len;
355 }
356
357 if (len > (int)rr->length) {
358 len = (int)rr->length;
359 }
360
David Benjamin17cf2cb2016-12-13 01:07:13 -0500361 OPENSSL_memcpy(out, rr->data, len);
David Benjamin163f29a2016-07-28 11:05:58 -0400362 if (!peek) {
363 rr->length -= len;
364 rr->data += len;
365 if (rr->length == 0) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400366 // The record has been consumed, so we may now clear the buffer.
David Benjamin163f29a2016-07-28 11:05:58 -0400367 ssl_read_buffer_discard(ssl);
368 }
369 }
370 return len;
371}
372
David Benjamin74795b32017-08-31 15:13:12 -0400373int ssl3_read_app_data(SSL *ssl, bool *out_got_handshake, uint8_t *buf, int len,
David Benjamin163f29a2016-07-28 11:05:58 -0400374 int peek) {
Steven Valdez681eb6a2016-12-19 13:19:29 -0500375 assert(ssl_can_read(ssl));
David Benjamincfc11c22017-07-18 22:45:18 -0400376 assert(!ssl->s3->aead_read_ctx->is_null_cipher());
David Benjamin74795b32017-08-31 15:13:12 -0400377 *out_got_handshake = false;
David Benjamin4bea8502016-05-12 09:34:55 -0400378
David Benjamin163f29a2016-07-28 11:05:58 -0400379 SSL3_RECORD *rr = &ssl->s3->rrec;
380
381 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400382 // A previous iteration may have read a partial handshake message. Do not
383 // allow more app data in that case.
David Benjamin163f29a2016-07-28 11:05:58 -0400384 int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0;
385
David Benjaminc11ea9422017-08-29 16:33:21 -0400386 // Get new packet if necessary.
David Benjamin163f29a2016-07-28 11:05:58 -0400387 if (rr->length == 0 && !has_hs_data) {
388 int ret = ssl3_get_record(ssl);
389 if (ret <= 0) {
390 return ret;
391 }
392 }
393
394 if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400395 // If reading 0-RTT data, reject handshake data. 0-RTT data is terminated
396 // by an alert.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500397 if (SSL_in_init(ssl)) {
398 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
399 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
400 return -1;
401 }
402
David Benjaminc11ea9422017-08-29 16:33:21 -0400403 // Post-handshake data prior to TLS 1.3 is always renegotiation, which we
404 // never accept as a server. Otherwise |ssl3_get_message| will send
405 // |SSL_R_EXCESSIVE_MESSAGE_SIZE|.
David Benjamin163f29a2016-07-28 11:05:58 -0400406 if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
407 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
408 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
409 return -1;
410 }
411
David Benjaminc11ea9422017-08-29 16:33:21 -0400412 // Parse post-handshake handshake messages.
David Benjamin7934f082017-08-01 16:32:25 -0400413 int ret = ssl3_read_message(ssl);
David Benjamin163f29a2016-07-28 11:05:58 -0400414 if (ret <= 0) {
415 return ret;
416 }
David Benjamin74795b32017-08-31 15:13:12 -0400417 *out_got_handshake = true;
David Benjamin163f29a2016-07-28 11:05:58 -0400418 return -1;
419 }
420
Alessandro Ghedinide254b42017-04-17 19:12:33 +0100421 const int is_early_data_read = ssl->server &&
422 ssl->s3->hs != NULL &&
423 ssl->s3->hs->can_early_read &&
424 ssl3_protocol_version(ssl) >= TLS1_3_VERSION;
425
David Benjaminc11ea9422017-08-29 16:33:21 -0400426 // Handle the end_of_early_data alert.
Steven Valdez681eb6a2016-12-19 13:19:29 -0500427 if (rr->type == SSL3_RT_ALERT &&
David Benjaminbbfe6032017-03-26 14:55:42 -0500428 rr->length == 2 &&
429 rr->data[0] == SSL3_AL_WARNING &&
430 rr->data[1] == TLS1_AD_END_OF_EARLY_DATA &&
Alessandro Ghedinide254b42017-04-17 19:12:33 +0100431 is_early_data_read) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400432 // Consume the record.
David Benjaminbbfe6032017-03-26 14:55:42 -0500433 rr->length = 0;
434 ssl_read_buffer_discard(ssl);
David Benjaminc11ea9422017-08-29 16:33:21 -0400435 // Stop accepting early data.
David Benjaminfd45ee72017-08-31 14:49:09 -0400436 ssl->s3->hs->can_early_read = false;
David Benjamin74795b32017-08-31 15:13:12 -0400437 *out_got_handshake = true;
Steven Valdez681eb6a2016-12-19 13:19:29 -0500438 return -1;
439 }
440
David Benjamin163f29a2016-07-28 11:05:58 -0400441 if (rr->type != SSL3_RT_APPLICATION_DATA) {
442 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
443 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
444 return -1;
445 }
446
Alessandro Ghedinide254b42017-04-17 19:12:33 +0100447 if (is_early_data_read) {
448 if (rr->length > kMaxEarlyDataAccepted - ssl->s3->hs->early_data_read) {
449 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_READ_EARLY_DATA);
450 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_UNEXPECTED_MESSAGE);
451 return -1;
452 }
453
454 ssl->s3->hs->early_data_read += rr->length;
455 }
456
David Benjamin163f29a2016-07-28 11:05:58 -0400457 if (rr->length != 0) {
458 return consume_record(ssl, buf, len, peek);
459 }
460
David Benjaminc11ea9422017-08-29 16:33:21 -0400461 // Discard empty records and loop again.
David Benjamin163f29a2016-07-28 11:05:58 -0400462 }
David Benjamina6022772015-05-30 16:22:10 -0400463}
464
David Benjamina41280d2015-11-26 02:16:49 -0500465int ssl3_read_change_cipher_spec(SSL *ssl) {
David Benjamin585320c2016-05-10 20:46:16 -0400466 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamina41280d2015-11-26 02:16:49 -0500467
David Benjamin585320c2016-05-10 20:46:16 -0400468 if (rr->length == 0) {
469 int ret = ssl3_get_record(ssl);
470 if (ret <= 0) {
471 return ret;
472 }
473 }
474
475 if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
476 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
477 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
478 return -1;
479 }
480
481 if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
David Benjamina41280d2015-11-26 02:16:49 -0500482 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
483 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
484 return -1;
485 }
486
David Benjaminc0279992016-09-19 20:15:07 -0400487 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data,
488 rr->length);
David Benjamin585320c2016-05-10 20:46:16 -0400489
490 rr->length = 0;
491 ssl_read_buffer_discard(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500492 return 1;
493}
494
David Benjamina6022772015-05-30 16:22:10 -0400495void ssl3_read_close_notify(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400496 // Read records until an error or close_notify.
David Benjamin4aa40812016-06-01 19:40:23 -0400497 while (ssl3_get_record(ssl) > 0) {
498 ;
499 }
David Benjamina6022772015-05-30 16:22:10 -0400500}
501
David Benjamin163f29a2016-07-28 11:05:58 -0400502int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) {
503 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamin397c8e62016-07-08 14:14:36 -0700504
David Benjamin163f29a2016-07-28 11:05:58 -0400505 for (;;) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400506 // Get new packet if necessary.
David Benjamin4cf369b2015-08-22 01:35:43 -0400507 if (rr->length == 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400508 int ret = ssl3_get_record(ssl);
509 if (ret <= 0) {
510 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 }
512 }
513
David Benjaminc11ea9422017-08-29 16:33:21 -0400514 // WatchGuard's TLS 1.3 interference bug is very distinctive: they drop the
515 // ServerHello and send the remaining encrypted application data records
516 // as-is. This manifests as an application data record when we expect
517 // handshake. Report a dedicated error code for this case.
David Benjamin5aaaa982017-06-22 15:03:18 -0400518 if (!ssl->server && rr->type == SSL3_RT_APPLICATION_DATA &&
David Benjamincfc11c22017-07-18 22:45:18 -0400519 ssl->s3->aead_read_ctx->is_null_cipher()) {
David Benjamin5aaaa982017-06-22 15:03:18 -0400520 OPENSSL_PUT_ERROR(SSL, SSL_R_APPLICATION_DATA_INSTEAD_OF_HANDSHAKE);
521 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
522 return -1;
523 }
524
David Benjaminc11ea9422017-08-29 16:33:21 -0400525 // Accept server_plaintext_handshake records when the content type TLS 1.3
526 // variant is enabled.
Steven Valdezdbe01582017-07-14 10:39:28 -0400527 if (rr->type != SSL3_RT_HANDSHAKE &&
528 !(!ssl->server &&
529 ssl->tls13_variant == tls13_record_type_experiment &&
David Benjamincfc11c22017-07-18 22:45:18 -0400530 ssl->s3->aead_read_ctx->is_null_cipher() &&
Steven Valdezdbe01582017-07-14 10:39:28 -0400531 rr->type == SSL3_RT_PLAINTEXT_HANDSHAKE)) {
David Benjamin163f29a2016-07-28 11:05:58 -0400532 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
533 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamincd90f3a2015-05-21 01:37:55 -0400534 return -1;
535 }
536
David Benjamin163f29a2016-07-28 11:05:58 -0400537 if (rr->length != 0) {
538 return consume_record(ssl, buf, len, 0 /* consume data */);
539 }
540
David Benjaminc11ea9422017-08-29 16:33:21 -0400541 // Discard empty records and loop again.
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800543}
Adam Langley95c29f32014-06-20 12:00:00 -0700544
David Benjamin0d56f882015-12-19 17:05:56 -0500545int ssl3_send_alert(SSL *ssl, int level, int desc) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400546 // It is illegal to send an alert when we've already sent a closing one.
David Benjaminfa214e42016-05-10 17:03:10 -0400547 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
548 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
549 return -1;
550 }
551
David Benjamin0efa7592017-01-27 20:51:33 -0500552 if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
David Benjaminfa214e42016-05-10 17:03:10 -0400553 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
David Benjamin0efa7592017-01-27 20:51:33 -0500554 } else {
555 assert(level == SSL3_AL_FATAL);
556 ssl->s3->send_shutdown = ssl_shutdown_fatal_alert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 }
Adam Langley95c29f32014-06-20 12:00:00 -0700558
David Benjamin0d56f882015-12-19 17:05:56 -0500559 ssl->s3->alert_dispatch = 1;
560 ssl->s3->send_alert[0] = level;
561 ssl->s3->send_alert[1] = desc;
562 if (!ssl_write_buffer_is_pending(ssl)) {
David Benjaminc11ea9422017-08-29 16:33:21 -0400563 // Nothing is being written out, so the alert may be dispatched
564 // immediately.
David Benjaminf0ee9072016-06-15 17:44:37 -0400565 return ssl->method->dispatch_alert(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 }
Adam Langley95c29f32014-06-20 12:00:00 -0700567
David Benjaminc11ea9422017-08-29 16:33:21 -0400568 // The alert will be dispatched later.
Adam Langleyfcf25832014-12-18 17:42:32 -0800569 return -1;
570}
Adam Langley95c29f32014-06-20 12:00:00 -0700571
David Benjamin0d56f882015-12-19 17:05:56 -0500572int ssl3_dispatch_alert(SSL *ssl) {
David Benjamina8571592016-03-10 01:41:55 -0500573 int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
574 if (ret <= 0) {
David Benjamina8571592016-03-10 01:41:55 -0500575 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 }
David Benjamin0efa7592017-01-27 20:51:33 -0500577 ssl->s3->alert_dispatch = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800578
David Benjaminc11ea9422017-08-29 16:33:21 -0400579 // If the alert is fatal, flush the BIO now.
David Benjamina8571592016-03-10 01:41:55 -0500580 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
581 BIO_flush(ssl->wbio);
582 }
583
David Benjaminc0279992016-09-19 20:15:07 -0400584 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert,
585 2);
David Benjamina8571592016-03-10 01:41:55 -0500586
David Benjamin4e9cc712016-06-01 20:16:03 -0400587 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
588 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
David Benjamina8571592016-03-10 01:41:55 -0500589
590 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800591}
David Benjamin86e95b82017-07-18 16:34:25 -0400592
593} // namespace bssl