blob: 42fffb1d6b0d07e6ed65fda101c10871bcee55f9 [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 Benjamin0d56f882015-12-19 17:05:56 -0500125static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
Adam Langley95c29f32014-06-20 12:00:00 -0700126
David Benjaminb8d28cf2015-07-28 21:34:45 -0400127/* ssl3_get_record reads a new input record. On success, it places it in
128 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
129 * more data is needed. */
130static int ssl3_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700131again:
David Benjaminfa214e42016-05-10 17:03:10 -0400132 switch (ssl->s3->recv_shutdown) {
133 case ssl_shutdown_none:
134 break;
135 case ssl_shutdown_fatal_alert:
136 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
137 return -1;
138 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500139 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500140 }
141
David Benjamina7810c12016-06-06 18:54:51 -0400142 CBS body;
Adam Langleyc68e5b92017-02-08 13:33:15 -0800143 uint8_t type, alert = SSL_AD_DECODE_ERROR;
David Benjamina7810c12016-06-06 18:54:51 -0400144 size_t consumed;
David Benjamin728f3542016-06-02 15:42:01 -0400145 enum ssl_open_record_t open_ret =
David Benjamina7810c12016-06-06 18:54:51 -0400146 tls_open_record(ssl, &type, &body, &consumed, &alert,
David Benjamin728f3542016-06-02 15:42:01 -0400147 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
148 if (open_ret != ssl_open_record_partial) {
149 ssl_read_buffer_consume(ssl, consumed);
150 }
151 switch (open_ret) {
David Benjamina7810c12016-06-06 18:54:51 -0400152 case ssl_open_record_partial: {
153 int read_ret = ssl_read_buffer_extend_to(ssl, consumed);
David Benjamin728f3542016-06-02 15:42:01 -0400154 if (read_ret <= 0) {
155 return read_ret;
156 }
157 goto again;
David Benjamina7810c12016-06-06 18:54:51 -0400158 }
David Benjamin6a08da22015-05-08 22:58:12 -0400159
David Benjamin728f3542016-06-02 15:42:01 -0400160 case ssl_open_record_success:
David Benjamina7810c12016-06-06 18:54:51 -0400161 if (CBS_len(&body) > 0xffff) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400162 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
163 return -1;
164 }
Adam Langley95c29f32014-06-20 12:00:00 -0700165
David Benjaminb8d28cf2015-07-28 21:34:45 -0400166 SSL3_RECORD *rr = &ssl->s3->rrec;
167 rr->type = type;
David Benjamina7810c12016-06-06 18:54:51 -0400168 rr->length = (uint16_t)CBS_len(&body);
169 rr->data = (uint8_t *)CBS_data(&body);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400170 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700171
David Benjamin728f3542016-06-02 15:42:01 -0400172 case ssl_open_record_discard:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400173 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700174
David Benjamin728f3542016-06-02 15:42:01 -0400175 case ssl_open_record_close_notify:
176 return 0;
177
178 case ssl_open_record_fatal_alert:
179 return -1;
Adam Langley95c29f32014-06-20 12:00:00 -0700180
David Benjaminb8d28cf2015-07-28 21:34:45 -0400181 case ssl_open_record_error:
182 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
183 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 }
Adam Langley95c29f32014-06-20 12:00:00 -0700185
David Benjaminb8d28cf2015-07-28 21:34:45 -0400186 assert(0);
187 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
188 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800189}
Adam Langley95c29f32014-06-20 12:00:00 -0700190
David Benjamin8671c472017-02-02 15:05:35 -0500191int ssl3_write_app_data(SSL *ssl, const uint8_t *buf, int len) {
Steven Valdez681eb6a2016-12-19 13:19:29 -0500192 assert(ssl_can_write(ssl));
David Benjamind7ac1432016-03-10 00:41:25 -0500193
David Benjamina8571592016-03-10 01:41:55 -0500194 unsigned tot, n, nw;
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamin0d56f882015-12-19 17:05:56 -0500196 assert(ssl->s3->wnum <= INT_MAX);
197 tot = ssl->s3->wnum;
198 ssl->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200 /* Ensure that if we end up with a smaller value of data to write out than
201 * the the original len from a write which didn't complete for non-blocking
202 * I/O and also somehow ended up avoiding the check for this in
203 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
204 * end up with (len-tot) as a large number that will then promptly send
205 * beyond the end of the users buffer ... so we trap and report the error in
206 * a way the user will notice. */
207 if (len < 0 || (size_t)len < tot) {
David Benjamin3570d732015-06-29 00:28:17 -0400208 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 return -1;
210 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700211
David Benjamin8671c472017-02-02 15:05:35 -0500212 n = len - tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800213 for (;;) {
214 /* max contains the maximum number of bytes that we can put into a
215 * record. */
David Benjamin0d56f882015-12-19 17:05:56 -0500216 unsigned max = ssl->max_send_fragment;
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 if (n > max) {
218 nw = max;
219 } else {
220 nw = n;
221 }
Adam Langley95c29f32014-06-20 12:00:00 -0700222
David Benjamin8671c472017-02-02 15:05:35 -0500223 int ret = do_ssl3_write(ssl, SSL3_RT_APPLICATION_DATA, &buf[tot], nw);
David Benjamina8571592016-03-10 01:41:55 -0500224 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500225 ssl->s3->wnum = tot;
David Benjamina8571592016-03-10 01:41:55 -0500226 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 }
Adam Langley95c29f32014-06-20 12:00:00 -0700228
David Benjamin8671c472017-02-02 15:05:35 -0500229 if (ret == (int)n || (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE)) {
David Benjamina8571592016-03-10 01:41:55 -0500230 return tot + ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamina8571592016-03-10 01:41:55 -0500233 n -= ret;
234 tot += ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800235 }
236}
Adam Langley95c29f32014-06-20 12:00:00 -0700237
David Benjamin0d56f882015-12-19 17:05:56 -0500238static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
David Benjamin2e0901b2015-11-02 17:50:10 -0500239 unsigned int len) {
David Benjamin0d56f882015-12-19 17:05:56 -0500240 if (ssl->s3->wpend_tot > (int)len ||
David Benjaminc6722cd2016-10-24 20:13:20 -0400241 (!(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER) &&
242 ssl->s3->wpend_buf != buf) ||
David Benjamin0d56f882015-12-19 17:05:56 -0500243 ssl->s3->wpend_type != type) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500244 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
245 return -1;
246 }
247
David Benjamin0d56f882015-12-19 17:05:56 -0500248 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500249 if (ret <= 0) {
250 return ret;
251 }
David Benjamin0d56f882015-12-19 17:05:56 -0500252 return ssl->s3->wpend_ret;
David Benjamin2e0901b2015-11-02 17:50:10 -0500253}
254
David Benjaminb8d28cf2015-07-28 21:34:45 -0400255/* do_ssl3_write writes an SSL record of the given type. */
David Benjamin0d56f882015-12-19 17:05:56 -0500256static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400257 /* If there is still data from the previous record, flush it. */
David Benjamin0d56f882015-12-19 17:05:56 -0500258 if (ssl_write_buffer_is_pending(ssl)) {
259 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800260 }
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamindaf207a2017-01-03 18:37:41 -0500262 /* The handshake flight buffer is mutually exclusive with application data.
263 *
264 * TODO(davidben): This will not be true when closure alerts use this. */
265 if (ssl->s3->pending_flight != NULL) {
266 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
267 return -1;
268 }
269
David Benjaminb8d28cf2015-07-28 21:34:45 -0400270 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
271 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 return -1;
273 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700274
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 if (len == 0) {
276 return 0;
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
David Benjaminda8636082016-11-04 15:44:28 -0400279 size_t max_out = len + SSL_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400280 if (max_out < len) {
281 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin9faafda2015-04-04 19:23:10 -0400282 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400284 uint8_t *out;
285 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500286 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
287 !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400288 return -1;
289 }
David Benjamin0d56f882015-12-19 17:05:56 -0500290 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 /* memorize arguments so that ssl3_write_pending can detect bad write retries
293 * later */
David Benjamin0d56f882015-12-19 17:05:56 -0500294 ssl->s3->wpend_tot = len;
295 ssl->s3->wpend_buf = buf;
296 ssl->s3->wpend_type = type;
297 ssl->s3->wpend_ret = len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800298
299 /* we now just need to write the buffer */
David Benjamin0d56f882015-12-19 17:05:56 -0500300 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800301}
Adam Langley95c29f32014-06-20 12:00:00 -0700302
David Benjamin163f29a2016-07-28 11:05:58 -0400303static int consume_record(SSL *ssl, uint8_t *out, int len, int peek) {
304 SSL3_RECORD *rr = &ssl->s3->rrec;
305
306 if (len <= 0) {
307 return len;
308 }
309
310 if (len > (int)rr->length) {
311 len = (int)rr->length;
312 }
313
David Benjamin17cf2cb2016-12-13 01:07:13 -0500314 OPENSSL_memcpy(out, rr->data, len);
David Benjamin163f29a2016-07-28 11:05:58 -0400315 if (!peek) {
316 rr->length -= len;
317 rr->data += len;
318 if (rr->length == 0) {
319 /* The record has been consumed, so we may now clear the buffer. */
320 ssl_read_buffer_discard(ssl);
321 }
322 }
323 return len;
324}
325
326int ssl3_read_app_data(SSL *ssl, int *out_got_handshake, uint8_t *buf, int len,
327 int peek) {
Steven Valdez681eb6a2016-12-19 13:19:29 -0500328 assert(ssl_can_read(ssl));
David Benjamin163f29a2016-07-28 11:05:58 -0400329 *out_got_handshake = 0;
David Benjamin4bea8502016-05-12 09:34:55 -0400330
Steven Valdez681eb6a2016-12-19 13:19:29 -0500331 ssl->method->release_current_message(ssl, 0 /* don't free buffer */);
332
David Benjamin163f29a2016-07-28 11:05:58 -0400333 SSL3_RECORD *rr = &ssl->s3->rrec;
334
335 for (;;) {
336 /* A previous iteration may have read a partial handshake message. Do not
337 * allow more app data in that case. */
338 int has_hs_data = ssl->init_buf != NULL && ssl->init_buf->length > 0;
339
340 /* Get new packet if necessary. */
341 if (rr->length == 0 && !has_hs_data) {
342 int ret = ssl3_get_record(ssl);
343 if (ret <= 0) {
344 return ret;
345 }
346 }
347
348 if (has_hs_data || rr->type == SSL3_RT_HANDSHAKE) {
Steven Valdez681eb6a2016-12-19 13:19:29 -0500349 /* If reading 0-RTT data, reject handshake data. 0-RTT data is terminated
350 * by an alert. */
351 if (SSL_in_init(ssl)) {
352 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
353 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
354 return -1;
355 }
356
David Benjamin163f29a2016-07-28 11:05:58 -0400357 /* Post-handshake data prior to TLS 1.3 is always renegotiation, which we
358 * never accept as a server. Otherwise |ssl3_get_message| will send
359 * |SSL_R_EXCESSIVE_MESSAGE_SIZE|. */
360 if (ssl->server && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
361 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
362 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
363 return -1;
364 }
365
366 /* Parse post-handshake handshake messages. */
David Benjaminf71036e2017-01-21 14:49:39 -0500367 int ret = ssl3_get_message(ssl);
David Benjamin163f29a2016-07-28 11:05:58 -0400368 if (ret <= 0) {
369 return ret;
370 }
371 *out_got_handshake = 1;
372 return -1;
373 }
374
Steven Valdez681eb6a2016-12-19 13:19:29 -0500375 if (rr->type == SSL3_RT_ALERT &&
376 ssl->server &&
377 ssl->s3->hs != NULL &&
378 ssl->s3->hs->can_early_read &&
379 ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
380 int ret = ssl3_read_end_of_early_data(ssl);
381 if (ret <= 0) {
382 return ret;
383 }
384 ssl->s3->hs->can_early_read = 0;
385 *out_got_handshake = 1;
386 return -1;
387 }
388
David Benjamin163f29a2016-07-28 11:05:58 -0400389 if (rr->type != SSL3_RT_APPLICATION_DATA) {
390 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
391 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
392 return -1;
393 }
394
395 if (rr->length != 0) {
396 return consume_record(ssl, buf, len, peek);
397 }
398
399 /* Discard empty records and loop again. */
400 }
David Benjamina6022772015-05-30 16:22:10 -0400401}
402
David Benjamina41280d2015-11-26 02:16:49 -0500403int ssl3_read_change_cipher_spec(SSL *ssl) {
David Benjamin585320c2016-05-10 20:46:16 -0400404 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamina41280d2015-11-26 02:16:49 -0500405
David Benjamin585320c2016-05-10 20:46:16 -0400406 if (rr->length == 0) {
407 int ret = ssl3_get_record(ssl);
408 if (ret <= 0) {
409 return ret;
410 }
411 }
412
413 if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
414 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
415 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
416 return -1;
417 }
418
419 if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
David Benjamina41280d2015-11-26 02:16:49 -0500420 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
421 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
422 return -1;
423 }
424
David Benjaminc0279992016-09-19 20:15:07 -0400425 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data,
426 rr->length);
David Benjamin585320c2016-05-10 20:46:16 -0400427
428 rr->length = 0;
429 ssl_read_buffer_discard(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500430 return 1;
431}
432
Steven Valdez2d850622017-01-11 11:34:52 -0500433int ssl3_read_end_of_early_data(SSL *ssl) {
434 SSL3_RECORD *rr = &ssl->s3->rrec;
435
436 if (rr->length == 0) {
437 int ret = ssl3_get_record(ssl);
438 if (ret <= 0) {
439 return ret;
440 }
441 }
442
443 if (rr->type != SSL3_RT_ALERT ||
444 rr->length != 2 ||
445 rr->data[0] != SSL3_AL_WARNING ||
446 rr->data[1] != TLS1_AD_END_OF_EARLY_DATA) {
447 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
448 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
449 return -1;
450 }
451
452 rr->length = 0;
453 ssl_read_buffer_discard(ssl);
454 return 1;
455}
456
David Benjamina6022772015-05-30 16:22:10 -0400457void ssl3_read_close_notify(SSL *ssl) {
David Benjamin4aa40812016-06-01 19:40:23 -0400458 /* Read records until an error or close_notify. */
459 while (ssl3_get_record(ssl) > 0) {
460 ;
461 }
David Benjamina6022772015-05-30 16:22:10 -0400462}
463
David Benjamin163f29a2016-07-28 11:05:58 -0400464int ssl3_read_handshake_bytes(SSL *ssl, uint8_t *buf, int len) {
465 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamin397c8e62016-07-08 14:14:36 -0700466
David Benjamin163f29a2016-07-28 11:05:58 -0400467 for (;;) {
468 /* Get new packet if necessary. */
David Benjamin4cf369b2015-08-22 01:35:43 -0400469 if (rr->length == 0) {
David Benjamin163f29a2016-07-28 11:05:58 -0400470 int ret = ssl3_get_record(ssl);
471 if (ret <= 0) {
472 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 }
474 }
475
David Benjamin163f29a2016-07-28 11:05:58 -0400476 if (rr->type != SSL3_RT_HANDSHAKE) {
477 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
478 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamincd90f3a2015-05-21 01:37:55 -0400479 return -1;
480 }
481
David Benjamin163f29a2016-07-28 11:05:58 -0400482 if (rr->length != 0) {
483 return consume_record(ssl, buf, len, 0 /* consume data */);
484 }
485
486 /* Discard empty records and loop again. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800488}
Adam Langley95c29f32014-06-20 12:00:00 -0700489
David Benjamin0d56f882015-12-19 17:05:56 -0500490int ssl3_send_alert(SSL *ssl, int level, int desc) {
David Benjaminfa214e42016-05-10 17:03:10 -0400491 /* It is illegal to send an alert when we've already sent a closing one. */
492 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
493 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
494 return -1;
495 }
496
David Benjamin0efa7592017-01-27 20:51:33 -0500497 if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
David Benjaminfa214e42016-05-10 17:03:10 -0400498 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
David Benjamin0efa7592017-01-27 20:51:33 -0500499 } else {
500 assert(level == SSL3_AL_FATAL);
501 ssl->s3->send_shutdown = ssl_shutdown_fatal_alert;
Adam Langleyfcf25832014-12-18 17:42:32 -0800502 }
Adam Langley95c29f32014-06-20 12:00:00 -0700503
David Benjamin0d56f882015-12-19 17:05:56 -0500504 ssl->s3->alert_dispatch = 1;
505 ssl->s3->send_alert[0] = level;
506 ssl->s3->send_alert[1] = desc;
507 if (!ssl_write_buffer_is_pending(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400508 /* Nothing is being written out, so the alert may be dispatched
509 * immediately. */
David Benjaminf0ee9072016-06-15 17:44:37 -0400510 return ssl->method->dispatch_alert(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800511 }
Adam Langley95c29f32014-06-20 12:00:00 -0700512
David Benjaminfa214e42016-05-10 17:03:10 -0400513 /* The alert will be dispatched later. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800514 return -1;
515}
Adam Langley95c29f32014-06-20 12:00:00 -0700516
David Benjamin0d56f882015-12-19 17:05:56 -0500517int ssl3_dispatch_alert(SSL *ssl) {
David Benjamina8571592016-03-10 01:41:55 -0500518 int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
519 if (ret <= 0) {
David Benjamina8571592016-03-10 01:41:55 -0500520 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 }
David Benjamin0efa7592017-01-27 20:51:33 -0500522 ssl->s3->alert_dispatch = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800523
David Benjamina8571592016-03-10 01:41:55 -0500524 /* If the alert is fatal, flush the BIO now. */
525 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
526 BIO_flush(ssl->wbio);
527 }
528
David Benjaminc0279992016-09-19 20:15:07 -0400529 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_ALERT, ssl->s3->send_alert,
530 2);
David Benjamina8571592016-03-10 01:41:55 -0500531
David Benjamin4e9cc712016-06-01 20:16:03 -0400532 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
533 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
David Benjamina8571592016-03-10 01:41:55 -0500534
535 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800536}