blob: bacbfe6e5723522e271b23c3cce4f4cf1b8df278 [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 Benjamin2ee94aa2015-04-07 22:38:30 -0400121#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700122
Adam Langleyfcf25832014-12-18 17:42:32 -0800123
David Benjamin0d56f882015-12-19 17:05:56 -0500124static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
Adam Langley95c29f32014-06-20 12:00:00 -0700125
David Benjamin24f346d2015-06-06 03:28:08 -0400126/* kMaxWarningAlerts is the number of consecutive warning alerts that will be
127 * processed. */
128static const uint8_t kMaxWarningAlerts = 4;
129
David Benjaminb8d28cf2015-07-28 21:34:45 -0400130/* ssl3_get_record reads a new input record. On success, it places it in
131 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
132 * more data is needed. */
133static int ssl3_get_record(SSL *ssl) {
134 int ret;
Adam Langley95c29f32014-06-20 12:00:00 -0700135again:
David Benjaminfa214e42016-05-10 17:03:10 -0400136 switch (ssl->s3->recv_shutdown) {
137 case ssl_shutdown_none:
138 break;
139 case ssl_shutdown_fatal_alert:
140 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
141 return -1;
142 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500143 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500144 }
145
David Benjaminb8d28cf2015-07-28 21:34:45 -0400146 /* Ensure the buffer is large enough to decrypt in-place. */
147 ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
148 if (ret <= 0) {
149 return ret;
150 }
151 assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjaminb8d28cf2015-07-28 21:34:45 -0400153 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
154 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
155 uint8_t type, alert;
156 size_t len, consumed;
157 switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
158 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
159 case ssl_open_record_success:
160 ssl_read_buffer_consume(ssl, consumed);
David Benjamin6a08da22015-05-08 22:58:12 -0400161
David Benjaminb8d28cf2015-07-28 21:34:45 -0400162 if (len > 0xffff) {
163 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
164 return -1;
165 }
Adam Langley95c29f32014-06-20 12:00:00 -0700166
David Benjaminb8d28cf2015-07-28 21:34:45 -0400167 SSL3_RECORD *rr = &ssl->s3->rrec;
168 rr->type = type;
169 rr->length = (uint16_t)len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400170 rr->data = out;
171 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700172
David Benjaminb8d28cf2015-07-28 21:34:45 -0400173 case ssl_open_record_partial:
174 ret = ssl_read_buffer_extend_to(ssl, consumed);
175 if (ret <= 0) {
176 return ret;
177 }
178 goto again;
Adam Langley95c29f32014-06-20 12:00:00 -0700179
David Benjaminb8d28cf2015-07-28 21:34:45 -0400180 case ssl_open_record_discard:
181 ssl_read_buffer_consume(ssl, consumed);
182 goto again;
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 Benjaminc933a472015-05-30 16:14:58 -0400194int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
David Benjamind7ac1432016-03-10 00:41:25 -0500195 assert(!SSL_in_init(ssl) || SSL_in_false_start(ssl));
196
David Benjaminc933a472015-05-30 16:14:58 -0400197 return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
198}
199
Adam Langleyfcf25832014-12-18 17:42:32 -0800200/* Call this to write data in records of type |type|. It will return <= 0 if
201 * not all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500202int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800203 const uint8_t *buf = buf_;
David Benjamina8571592016-03-10 01:41:55 -0500204 unsigned tot, n, nw;
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamin0d56f882015-12-19 17:05:56 -0500206 assert(ssl->s3->wnum <= INT_MAX);
207 tot = ssl->s3->wnum;
208 ssl->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700209
Adam Langleyfcf25832014-12-18 17:42:32 -0800210 /* Ensure that if we end up with a smaller value of data to write out than
211 * the the original len from a write which didn't complete for non-blocking
212 * I/O and also somehow ended up avoiding the check for this in
213 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
214 * end up with (len-tot) as a large number that will then promptly send
215 * beyond the end of the users buffer ... so we trap and report the error in
216 * a way the user will notice. */
217 if (len < 0 || (size_t)len < tot) {
David Benjamin3570d732015-06-29 00:28:17 -0400218 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 return -1;
220 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700221
Adam Langleyfcf25832014-12-18 17:42:32 -0800222 n = (len - tot);
223 for (;;) {
224 /* max contains the maximum number of bytes that we can put into a
225 * record. */
David Benjamin0d56f882015-12-19 17:05:56 -0500226 unsigned max = ssl->max_send_fragment;
Adam Langleyfcf25832014-12-18 17:42:32 -0800227 if (n > max) {
228 nw = max;
229 } else {
230 nw = n;
231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamina8571592016-03-10 01:41:55 -0500233 int ret = do_ssl3_write(ssl, type, &buf[tot], nw);
234 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500235 ssl->s3->wnum = tot;
David Benjamina8571592016-03-10 01:41:55 -0500236 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
David Benjamina8571592016-03-10 01:41:55 -0500239 if (ret == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
240 (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
241 return tot + ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 }
Adam Langley95c29f32014-06-20 12:00:00 -0700243
David Benjamina8571592016-03-10 01:41:55 -0500244 n -= ret;
245 tot += ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 }
247}
Adam Langley95c29f32014-06-20 12:00:00 -0700248
David Benjamin0d56f882015-12-19 17:05:56 -0500249static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
David Benjamin2e0901b2015-11-02 17:50:10 -0500250 unsigned int len) {
David Benjamin0d56f882015-12-19 17:05:56 -0500251 if (ssl->s3->wpend_tot > (int)len ||
252 (ssl->s3->wpend_buf != buf &&
253 !(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
254 ssl->s3->wpend_type != type) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500255 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
256 return -1;
257 }
258
David Benjamin0d56f882015-12-19 17:05:56 -0500259 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500260 if (ret <= 0) {
261 return ret;
262 }
David Benjamin0d56f882015-12-19 17:05:56 -0500263 return ssl->s3->wpend_ret;
David Benjamin2e0901b2015-11-02 17:50:10 -0500264}
265
David Benjaminb8d28cf2015-07-28 21:34:45 -0400266/* do_ssl3_write writes an SSL record of the given type. */
David Benjamin0d56f882015-12-19 17:05:56 -0500267static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400268 /* If there is still data from the previous record, flush it. */
David Benjamin0d56f882015-12-19 17:05:56 -0500269 if (ssl_write_buffer_is_pending(ssl)) {
270 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500274 if (ssl->s3->alert_dispatch) {
275 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin9faafda2015-04-04 19:23:10 -0400276 if (ret <= 0) {
277 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 }
279 /* if it went, fall through and send more stuff */
280 }
Adam Langley95c29f32014-06-20 12:00:00 -0700281
David Benjaminb8d28cf2015-07-28 21:34:45 -0400282 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
283 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800284 return -1;
285 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700286
Adam Langleyfcf25832014-12-18 17:42:32 -0800287 if (len == 0) {
288 return 0;
289 }
Adam Langley95c29f32014-06-20 12:00:00 -0700290
David Benjamin0d56f882015-12-19 17:05:56 -0500291 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400292 if (max_out < len) {
293 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
David Benjamin9faafda2015-04-04 19:23:10 -0400294 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400296 uint8_t *out;
297 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500298 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
299 !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400300 return -1;
301 }
David Benjamin0d56f882015-12-19 17:05:56 -0500302 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 /* memorize arguments so that ssl3_write_pending can detect bad write retries
305 * later */
David Benjamin0d56f882015-12-19 17:05:56 -0500306 ssl->s3->wpend_tot = len;
307 ssl->s3->wpend_buf = buf;
308 ssl->s3->wpend_type = type;
309 ssl->s3->wpend_ret = len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800310
311 /* we now just need to write the buffer */
David Benjamin0d56f882015-12-19 17:05:56 -0500312 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800313}
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamina6022772015-05-30 16:22:10 -0400315int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500316 assert(!SSL_in_init(ssl));
David Benjamina6022772015-05-30 16:22:10 -0400317 return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
318}
319
David Benjamina41280d2015-11-26 02:16:49 -0500320int ssl3_read_change_cipher_spec(SSL *ssl) {
321 uint8_t byte;
322 int ret = ssl3_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1 /* len */,
323 0 /* no peek */);
324 if (ret <= 0) {
325 return ret;
326 }
327 assert(ret == 1);
328
329 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
330 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
331 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
332 return -1;
333 }
334
335 if (ssl->msg_callback != NULL) {
336 ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
337 ssl, ssl->msg_callback_arg);
338 }
339
340 return 1;
341}
342
David Benjamina6022772015-05-30 16:22:10 -0400343void ssl3_read_close_notify(SSL *ssl) {
344 ssl3_read_bytes(ssl, 0, NULL, 0, 0);
345}
346
David Benjamin1d5ef3b2015-10-12 19:54:18 -0400347static int ssl3_can_renegotiate(SSL *ssl) {
348 switch (ssl->renegotiate_mode) {
349 case ssl_renegotiate_never:
350 return 0;
351 case ssl_renegotiate_once:
352 return ssl->s3->total_renegotiations == 0;
353 case ssl_renegotiate_freely:
354 return 1;
Adam Langley27a0d082015-11-03 13:34:10 -0800355 case ssl_renegotiate_ignore:
356 return 1;
David Benjamin1d5ef3b2015-10-12 19:54:18 -0400357 }
358
359 assert(0);
360 return 0;
361}
362
Adam Langley95c29f32014-06-20 12:00:00 -0700363/* Return up to 'len' payload bytes received in 'type' records.
364 * 'type' is one of the following:
365 *
366 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
David Benjamina41280d2015-11-26 02:16:49 -0500367 * - SSL3_RT_CHANGE_CIPHER_SPEC (when ssl3_read_change_cipher_spec calls us)
368 * - SSL3_RT_APPLICATION_DATA (when ssl3_read_app_data calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700369 * - 0 (during a shutdown, no data has to be returned)
370 *
371 * If we don't have stored data to work from, read a SSL/TLS record first
372 * (possibly multiple records if we still don't have anything to return).
373 *
374 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500375 * Alert records (e.g. close_notify) or renegotiation requests. */
David Benjamin0d56f882015-12-19 17:05:56 -0500376int ssl3_read_bytes(SSL *ssl, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500377 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800378 unsigned int n;
379 SSL3_RECORD *rr;
David Benjamin82170242015-10-17 22:51:17 -0400380 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
David Benjamina41280d2015-11-26 02:16:49 -0500382 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
383 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800384 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400385 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 return -1;
387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langley95c29f32014-06-20 12:00:00 -0700389start:
David Benjamin0d56f882015-12-19 17:05:56 -0500390 /* ssl->s3->rrec.type - is the type of record
391 * ssl->s3->rrec.data - data
392 * ssl->s3->rrec.off - offset into 'data' for next read
393 * ssl->s3->rrec.length - number of bytes. */
394 rr = &ssl->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400397 if (rr->length == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500398 ret = ssl3_get_record(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 if (ret <= 0) {
400 return ret;
401 }
402 }
Adam Langley95c29f32014-06-20 12:00:00 -0700403
Adam Langleyfcf25832014-12-18 17:42:32 -0800404 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700405
David Benjaminee0b02a2015-08-22 11:35:40 -0400406 if (type != 0 && type == rr->type) {
David Benjamin0d56f882015-12-19 17:05:56 -0500407 ssl->s3->warning_alert_count = 0;
David Benjamin24f346d2015-06-06 03:28:08 -0400408
David Benjamina41280d2015-11-26 02:16:49 -0500409 /* Make sure that we are not getting application data when we are doing a
410 * handshake for the first time. */
David Benjamin0d56f882015-12-19 17:05:56 -0500411 if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA &&
David Benjamin79978df2015-12-25 15:56:49 -0500412 ssl->s3->aead_read_ctx == NULL) {
David Benjaminb8a56f12014-12-23 11:41:02 -0500413 /* TODO(davidben): Is this check redundant with the handshake_func
414 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400416 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 goto f_err;
418 }
Adam Langley95c29f32014-06-20 12:00:00 -0700419
David Benjamin4cf369b2015-08-22 01:35:43 -0400420 /* Discard empty records. */
421 if (rr->length == 0) {
422 goto start;
423 }
424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 if (len <= 0) {
426 return len;
427 }
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langleyfcf25832014-12-18 17:42:32 -0800429 if ((unsigned int)len > rr->length) {
430 n = rr->length;
431 } else {
432 n = (unsigned int)len;
433 }
Adam Langley95c29f32014-06-20 12:00:00 -0700434
David Benjamin7fc01002015-12-06 15:48:22 -0500435 memcpy(buf, rr->data, n);
Adam Langleyfcf25832014-12-18 17:42:32 -0800436 if (!peek) {
437 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500438 rr->data += n;
Adam Langleyfcf25832014-12-18 17:42:32 -0800439 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400440 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500441 ssl_read_buffer_discard(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800442 }
443 }
444
445 return n;
446 }
Adam Langley95c29f32014-06-20 12:00:00 -0700447
David Benjamin44d3eed2015-05-21 01:29:55 -0400448 /* Process unexpected records. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400449
David Benjamina41280d2015-11-26 02:16:49 -0500450 if (type == SSL3_RT_APPLICATION_DATA && rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400451 /* If peer renegotiations are disabled, all out-of-order handshake records
David Benjamin44d3eed2015-05-21 01:29:55 -0400452 * are fatal. Renegotiations as a server are never supported. */
David Benjamin0d56f882015-12-19 17:05:56 -0500453 if (ssl->server || !ssl3_can_renegotiate(ssl)) {
David Benjaminb16346b2015-04-08 19:16:58 -0400454 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400455 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjaminb16346b2015-04-08 19:16:58 -0400456 goto f_err;
457 }
458
David Benjamin8fd5c232015-11-26 01:39:53 -0500459 /* This must be a HelloRequest, possibly fragmented over multiple records.
460 * Consume data from the handshake protocol until it is complete. */
461 static const uint8_t kHelloRequest[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
David Benjamin0d56f882015-12-19 17:05:56 -0500462 while (ssl->s3->hello_request_len < sizeof(kHelloRequest)) {
David Benjamin8fd5c232015-11-26 01:39:53 -0500463 if (rr->length == 0) {
464 /* Get a new record. */
465 goto start;
466 }
David Benjamin0d56f882015-12-19 17:05:56 -0500467 if (rr->data[0] != kHelloRequest[ssl->s3->hello_request_len]) {
David Benjamin8fd5c232015-11-26 01:39:53 -0500468 al = SSL_AD_DECODE_ERROR;
469 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
470 goto f_err;
471 }
David Benjamin7fc01002015-12-06 15:48:22 -0500472 rr->data++;
David Benjamin8fd5c232015-11-26 01:39:53 -0500473 rr->length--;
David Benjamin0d56f882015-12-19 17:05:56 -0500474 ssl->s3->hello_request_len++;
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 }
David Benjamin0d56f882015-12-19 17:05:56 -0500476 ssl->s3->hello_request_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjamin0d56f882015-12-19 17:05:56 -0500478 if (ssl->msg_callback) {
479 ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, kHelloRequest,
480 sizeof(kHelloRequest), ssl, ssl->msg_callback_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 }
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin0d56f882015-12-19 17:05:56 -0500483 if (!SSL_is_init_finished(ssl) || !ssl->s3->initial_handshake_complete) {
David Benjamincd90f3a2015-05-21 01:37:55 -0400484 /* This cannot happen. If a handshake is in progress, |type| must be
485 * |SSL3_RT_HANDSHAKE|. */
486 assert(0);
David Benjamin3570d732015-06-29 00:28:17 -0400487 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamincd90f3a2015-05-21 01:37:55 -0400488 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 }
David Benjamincd90f3a2015-05-21 01:37:55 -0400490
David Benjamin0d56f882015-12-19 17:05:56 -0500491 if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
Adam Langley27a0d082015-11-03 13:34:10 -0800492 goto start;
493 }
494
David Benjamincd90f3a2015-05-21 01:37:55 -0400495 /* Renegotiation is only supported at quiescent points in the application
496 * protocol, namely in HTTPS, just before reading the HTTP response. Require
497 * the record-layer be idle and avoid complexities of sending a handshake
498 * record while an application_data record is being written. */
David Benjamin0d56f882015-12-19 17:05:56 -0500499 if (ssl_write_buffer_is_pending(ssl)) {
David Benjamincd90f3a2015-05-21 01:37:55 -0400500 al = SSL_AD_NO_RENEGOTIATION;
David Benjamin3570d732015-06-29 00:28:17 -0400501 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
David Benjamincd90f3a2015-05-21 01:37:55 -0400502 goto f_err;
503 }
504
505 /* Begin a new handshake. */
David Benjamin0d56f882015-12-19 17:05:56 -0500506 ssl->s3->total_renegotiations++;
507 ssl->state = SSL_ST_CONNECT;
508 i = ssl->handshake_func(ssl);
David Benjamincd90f3a2015-05-21 01:37:55 -0400509 if (i < 0) {
510 return i;
511 }
512 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400513 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
David Benjamincd90f3a2015-05-21 01:37:55 -0400514 return -1;
515 }
516
517 /* The handshake completed synchronously. Continue reading records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 goto start;
519 }
David Benjaminb4188f02014-11-01 03:43:48 -0400520
David Benjamin0d3a8c62016-03-11 22:25:18 -0500521 /* If an alert record, process the alert. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800522 if (rr->type == SSL3_RT_ALERT) {
David Benjamin0d3a8c62016-03-11 22:25:18 -0500523 /* Alerts records may not contain fragmented or multiple alerts. */
524 if (rr->length != 2) {
David Benjamin86058a22015-02-22 13:07:21 -0500525 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400526 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin86058a22015-02-22 13:07:21 -0500527 goto f_err;
528 }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
David Benjamin0d56f882015-12-19 17:05:56 -0500530 if (ssl->msg_callback) {
531 ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
532 ssl->msg_callback_arg);
Adam Langleyfcf25832014-12-18 17:42:32 -0800533 }
David Benjamin7fc01002015-12-06 15:48:22 -0500534 const uint8_t alert_level = rr->data[0];
535 const uint8_t alert_descr = rr->data[1];
David Benjamin86058a22015-02-22 13:07:21 -0500536 rr->length -= 2;
David Benjamin7fc01002015-12-06 15:48:22 -0500537 rr->data += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin0d56f882015-12-19 17:05:56 -0500539 if (ssl->info_callback != NULL) {
540 cb = ssl->info_callback;
541 } else if (ssl->ctx->info_callback != NULL) {
542 cb = ssl->ctx->info_callback;
Adam Langleyfcf25832014-12-18 17:42:32 -0800543 }
Adam Langley95c29f32014-06-20 12:00:00 -0700544
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500546 uint16_t alert = (alert_level << 8) | alert_descr;
David Benjamin0d56f882015-12-19 17:05:56 -0500547 cb(ssl, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800548 }
Adam Langley95c29f32014-06-20 12:00:00 -0700549
David Benjamin86058a22015-02-22 13:07:21 -0500550 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800551 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
David Benjaminfa214e42016-05-10 17:03:10 -0400552 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 return 0;
554 }
Adam Langley95c29f32014-06-20 12:00:00 -0700555
David Benjamin0d56f882015-12-19 17:05:56 -0500556 ssl->s3->warning_alert_count++;
557 if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
David Benjamin24f346d2015-06-06 03:28:08 -0400558 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400559 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
David Benjamin24f346d2015-06-06 03:28:08 -0400560 goto f_err;
561 }
David Benjamin86058a22015-02-22 13:07:21 -0500562 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700564
David Benjamin3570d732015-06-29 00:28:17 -0400565 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langleyfcf25832014-12-18 17:42:32 -0800566 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
567 ERR_add_error_data(2, "SSL alert number ", tmp);
David Benjaminfa214e42016-05-10 17:03:10 -0400568 ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
David Benjamin0d56f882015-12-19 17:05:56 -0500569 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 return 0;
571 } else {
572 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400573 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 goto f_err;
575 }
Adam Langley95c29f32014-06-20 12:00:00 -0700576
Adam Langleyfcf25832014-12-18 17:42:32 -0800577 goto start;
578 }
Adam Langley95c29f32014-06-20 12:00:00 -0700579
David Benjaminfa214e42016-05-10 17:03:10 -0400580 if (ssl->s3->send_shutdown == ssl_shutdown_close_notify) {
David Benjaminaa9361b2015-08-29 22:33:16 -0400581 /* close_notify has been sent, so discard all records other than alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800582 rr->length = 0;
David Benjaminaa9361b2015-08-29 22:33:16 -0400583 goto start;
Adam Langleyfcf25832014-12-18 17:42:32 -0800584 }
Adam Langley95c29f32014-06-20 12:00:00 -0700585
David Benjamine820df92015-02-08 16:06:54 -0500586 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400587 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -0700588
589f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500590 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700591err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 return -1;
593}
Adam Langley95c29f32014-06-20 12:00:00 -0700594
David Benjamin0d56f882015-12-19 17:05:56 -0500595int ssl3_send_alert(SSL *ssl, int level, int desc) {
David Benjaminfa214e42016-05-10 17:03:10 -0400596 /* It is illegal to send an alert when we've already sent a closing one. */
597 if (ssl->s3->send_shutdown != ssl_shutdown_none) {
598 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
599 return -1;
600 }
601
602 if (level == SSL3_AL_FATAL) {
603 if (ssl->session != NULL) {
604 SSL_CTX_remove_session(ssl->ctx, ssl->session);
605 }
606 ssl->s3->send_shutdown = ssl_shutdown_fatal_alert;
607 } else if (level == SSL3_AL_WARNING && desc == SSL_AD_CLOSE_NOTIFY) {
608 ssl->s3->send_shutdown = ssl_shutdown_close_notify;
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 }
Adam Langley95c29f32014-06-20 12:00:00 -0700610
David Benjamin0d56f882015-12-19 17:05:56 -0500611 ssl->s3->alert_dispatch = 1;
612 ssl->s3->send_alert[0] = level;
613 ssl->s3->send_alert[1] = desc;
614 if (!ssl_write_buffer_is_pending(ssl)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400615 /* Nothing is being written out, so the alert may be dispatched
616 * immediately. */
David Benjamin0d56f882015-12-19 17:05:56 -0500617 return ssl->method->ssl_dispatch_alert(ssl);
Adam Langleyfcf25832014-12-18 17:42:32 -0800618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
David Benjaminfa214e42016-05-10 17:03:10 -0400620 /* The alert will be dispatched later. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800621 return -1;
622}
Adam Langley95c29f32014-06-20 12:00:00 -0700623
David Benjamin0d56f882015-12-19 17:05:56 -0500624int ssl3_dispatch_alert(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500625 ssl->s3->alert_dispatch = 0;
David Benjamina8571592016-03-10 01:41:55 -0500626 int ret = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
627 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500628 ssl->s3->alert_dispatch = 1;
David Benjamina8571592016-03-10 01:41:55 -0500629 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800630 }
631
David Benjamina8571592016-03-10 01:41:55 -0500632 /* If the alert is fatal, flush the BIO now. */
633 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
634 BIO_flush(ssl->wbio);
635 }
636
637 if (ssl->msg_callback != NULL) {
638 ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_ALERT,
639 ssl->s3->send_alert, 2, ssl, ssl->msg_callback_arg);
640 }
641
642 void (*cb)(const SSL *ssl, int type, int value) = NULL;
643 if (ssl->info_callback != NULL) {
644 cb = ssl->info_callback;
645 } else if (ssl->ctx->info_callback != NULL) {
646 cb = ssl->ctx->info_callback;
647 }
648
649 if (cb != NULL) {
650 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
651 cb(ssl, SSL_CB_WRITE_ALERT, alert);
652 }
653
654 return 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800655}