blob: 4c1133c1378593ebe59327f97637186ff3be749c [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* 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
Kenny Rootb8494592015-09-25 02:29:14 +0000109#include <openssl/ssl.h>
110
Adam Langleyd9e397b2015-01-22 14:27:53 -0800111#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800112#include <limits.h>
113#include <stdio.h>
Adam Langleye9ada862015-05-11 17:20:37 -0700114#include <string.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -0800115
116#include <openssl/buf.h>
117#include <openssl/err.h>
118#include <openssl/evp.h>
119#include <openssl/mem.h>
120#include <openssl/rand.h>
121
Adam Langleye9ada862015-05-11 17:20:37 -0700122#include "internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123
124
Adam Langley4139edb2016-01-13 15:00:54 -0800125static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126
Kenny Rootb8494592015-09-25 02:29:14 +0000127/* kMaxWarningAlerts is the number of consecutive warning alerts that will be
128 * processed. */
129static const uint8_t kMaxWarningAlerts = 4;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130
Kenny Rootb8494592015-09-25 02:29:14 +0000131/* ssl3_get_record reads a new input record. On success, it places it in
132 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
133 * more data is needed. */
134static int ssl3_get_record(SSL *ssl) {
135 int ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136again:
Kenny Rootb8494592015-09-25 02:29:14 +0000137 /* Ensure the buffer is large enough to decrypt in-place. */
138 ret = ssl_read_buffer_extend_to(ssl, ssl_record_prefix_len(ssl));
139 if (ret <= 0) {
140 return ret;
141 }
142 assert(ssl_read_buffer_len(ssl) >= ssl_record_prefix_len(ssl));
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143
Kenny Rootb8494592015-09-25 02:29:14 +0000144 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
145 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
146 uint8_t type, alert;
147 size_t len, consumed;
148 switch (tls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
149 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
150 case ssl_open_record_success:
151 ssl_read_buffer_consume(ssl, consumed);
Adam Langleye9ada862015-05-11 17:20:37 -0700152
Kenny Rootb8494592015-09-25 02:29:14 +0000153 if (len > 0xffff) {
154 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
155 return -1;
156 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157
Kenny Rootb8494592015-09-25 02:29:14 +0000158 SSL3_RECORD *rr = &ssl->s3->rrec;
159 rr->type = type;
160 rr->length = (uint16_t)len;
Kenny Rootb8494592015-09-25 02:29:14 +0000161 rr->data = out;
162 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800163
Kenny Rootb8494592015-09-25 02:29:14 +0000164 case ssl_open_record_partial:
165 ret = ssl_read_buffer_extend_to(ssl, consumed);
166 if (ret <= 0) {
167 return ret;
168 }
169 goto again;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170
Kenny Rootb8494592015-09-25 02:29:14 +0000171 case ssl_open_record_discard:
172 ssl_read_buffer_consume(ssl, consumed);
173 goto again;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800174
Kenny Rootb8494592015-09-25 02:29:14 +0000175 case ssl_open_record_error:
176 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
177 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 }
179
Kenny Rootb8494592015-09-25 02:29:14 +0000180 assert(0);
181 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
182 return -1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800183}
184
Adam Langleyf4e42722015-06-04 17:45:09 -0700185int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
186 return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
187}
188
Adam Langleyd9e397b2015-01-22 14:27:53 -0800189/* Call this to write data in records of type |type|. It will return <= 0 if
190 * not all data has been sent or non-blocking IO. */
Adam Langley4139edb2016-01-13 15:00:54 -0800191int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, int len) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192 const uint8_t *buf = buf_;
193 unsigned int tot, n, nw;
194 int i;
195
Adam Langley4139edb2016-01-13 15:00:54 -0800196 ssl->rwstate = SSL_NOTHING;
197 assert(ssl->s3->wnum <= INT_MAX);
198 tot = ssl->s3->wnum;
199 ssl->s3->wnum = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800200
Adam Langley4139edb2016-01-13 15:00:54 -0800201 if (!ssl->in_handshake && SSL_in_init(ssl) && !SSL_in_false_start(ssl)) {
202 i = ssl->handshake_func(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203 if (i < 0) {
204 return i;
205 }
206 if (i == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000207 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800208 return -1;
209 }
210 }
211
212 /* Ensure that if we end up with a smaller value of data to write out than
213 * the the original len from a write which didn't complete for non-blocking
214 * I/O and also somehow ended up avoiding the check for this in
215 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
216 * end up with (len-tot) as a large number that will then promptly send
217 * beyond the end of the users buffer ... so we trap and report the error in
218 * a way the user will notice. */
219 if (len < 0 || (size_t)len < tot) {
Kenny Rootb8494592015-09-25 02:29:14 +0000220 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_LENGTH);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800221 return -1;
222 }
223
224 n = (len - tot);
225 for (;;) {
226 /* max contains the maximum number of bytes that we can put into a
227 * record. */
Adam Langley4139edb2016-01-13 15:00:54 -0800228 unsigned max = ssl->max_send_fragment;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 if (n > max) {
230 nw = max;
231 } else {
232 nw = n;
233 }
234
Adam Langley4139edb2016-01-13 15:00:54 -0800235 i = do_ssl3_write(ssl, type, &buf[tot], nw);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800236 if (i <= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800237 ssl->s3->wnum = tot;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800238 return i;
239 }
240
241 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
Adam Langley4139edb2016-01-13 15:00:54 -0800242 (ssl->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 return tot + i;
244 }
245
246 n -= i;
247 tot += i;
248 }
249}
250
Adam Langley4139edb2016-01-13 15:00:54 -0800251static int ssl3_write_pending(SSL *ssl, int type, const uint8_t *buf,
Adam Langleyfad63272015-11-12 12:15:39 -0800252 unsigned int len) {
Adam Langley4139edb2016-01-13 15:00:54 -0800253 if (ssl->s3->wpend_tot > (int)len ||
254 (ssl->s3->wpend_buf != buf &&
255 !(ssl->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
256 ssl->s3->wpend_type != type) {
Adam Langleyfad63272015-11-12 12:15:39 -0800257 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_WRITE_RETRY);
258 return -1;
259 }
260
Adam Langley4139edb2016-01-13 15:00:54 -0800261 int ret = ssl_write_buffer_flush(ssl);
Adam Langleyfad63272015-11-12 12:15:39 -0800262 if (ret <= 0) {
263 return ret;
264 }
Adam Langley4139edb2016-01-13 15:00:54 -0800265 return ssl->s3->wpend_ret;
Adam Langleyfad63272015-11-12 12:15:39 -0800266}
267
Kenny Rootb8494592015-09-25 02:29:14 +0000268/* do_ssl3_write writes an SSL record of the given type. */
Adam Langley4139edb2016-01-13 15:00:54 -0800269static int do_ssl3_write(SSL *ssl, int type, const uint8_t *buf, unsigned len) {
Kenny Rootb8494592015-09-25 02:29:14 +0000270 /* If there is still data from the previous record, flush it. */
Adam Langley4139edb2016-01-13 15:00:54 -0800271 if (ssl_write_buffer_is_pending(ssl)) {
272 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800273 }
274
275 /* If we have an alert to send, lets send it */
Adam Langley4139edb2016-01-13 15:00:54 -0800276 if (ssl->s3->alert_dispatch) {
277 int ret = ssl->method->ssl_dispatch_alert(ssl);
Adam Langleye9ada862015-05-11 17:20:37 -0700278 if (ret <= 0) {
279 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800280 }
281 /* if it went, fall through and send more stuff */
282 }
283
Kenny Rootb8494592015-09-25 02:29:14 +0000284 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
285 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800286 return -1;
287 }
288
289 if (len == 0) {
290 return 0;
291 }
292
Adam Langley4139edb2016-01-13 15:00:54 -0800293 size_t max_out = len + ssl_max_seal_overhead(ssl);
Kenny Rootb8494592015-09-25 02:29:14 +0000294 if (max_out < len) {
295 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
Adam Langley1e4884f2015-09-24 10:57:52 -0700296 return -1;
297 }
Kenny Rootb8494592015-09-25 02:29:14 +0000298 uint8_t *out;
299 size_t ciphertext_len;
Adam Langley4139edb2016-01-13 15:00:54 -0800300 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
301 !tls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000302 return -1;
303 }
Adam Langley4139edb2016-01-13 15:00:54 -0800304 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800305
306 /* memorize arguments so that ssl3_write_pending can detect bad write retries
307 * later */
Adam Langley4139edb2016-01-13 15:00:54 -0800308 ssl->s3->wpend_tot = len;
309 ssl->s3->wpend_buf = buf;
310 ssl->s3->wpend_type = type;
311 ssl->s3->wpend_ret = len;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800312
313 /* we now just need to write the buffer */
Adam Langley4139edb2016-01-13 15:00:54 -0800314 return ssl3_write_pending(ssl, type, buf, len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800315}
316
Adam Langleyf4e42722015-06-04 17:45:09 -0700317int ssl3_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
318 return ssl3_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
319}
320
Adam Langley4139edb2016-01-13 15:00:54 -0800321int ssl3_read_change_cipher_spec(SSL *ssl) {
322 uint8_t byte;
323 int ret = ssl3_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1 /* len */,
324 0 /* no peek */);
325 if (ret <= 0) {
326 return ret;
327 }
328 assert(ret == 1);
329
330 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
331 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
332 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
333 return -1;
334 }
335
336 if (ssl->msg_callback != NULL) {
337 ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
338 ssl, ssl->msg_callback_arg);
339 }
340
341 return 1;
342}
343
Adam Langleyf4e42722015-06-04 17:45:09 -0700344void ssl3_read_close_notify(SSL *ssl) {
345 ssl3_read_bytes(ssl, 0, NULL, 0, 0);
346}
347
Kenny Roote99801b2015-11-06 15:31:15 -0800348static int ssl3_can_renegotiate(SSL *ssl) {
349 switch (ssl->renegotiate_mode) {
350 case ssl_renegotiate_never:
351 return 0;
352 case ssl_renegotiate_once:
353 return ssl->s3->total_renegotiations == 0;
354 case ssl_renegotiate_freely:
355 return 1;
Adam Langleyfad63272015-11-12 12:15:39 -0800356 case ssl_renegotiate_ignore:
357 return 1;
Kenny Roote99801b2015-11-06 15:31:15 -0800358 }
359
360 assert(0);
361 return 0;
362}
363
Adam Langleyd9e397b2015-01-22 14:27:53 -0800364/* Return up to 'len' payload bytes received in 'type' records.
365 * 'type' is one of the following:
366 *
367 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
Adam Langley4139edb2016-01-13 15:00:54 -0800368 * - SSL3_RT_CHANGE_CIPHER_SPEC (when ssl3_read_change_cipher_spec calls us)
369 * - SSL3_RT_APPLICATION_DATA (when ssl3_read_app_data calls us)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370 * - 0 (during a shutdown, no data has to be returned)
371 *
372 * If we don't have stored data to work from, read a SSL/TLS record first
373 * (possibly multiple records if we still don't have anything to return).
374 *
375 * This function must handle any surprises the peer may have for us, such as
Adam Langley4139edb2016-01-13 15:00:54 -0800376 * Alert records (e.g. close_notify) or renegotiation requests. */
377int ssl3_read_bytes(SSL *ssl, int type, uint8_t *buf, int len, int peek) {
Adam Langleye9ada862015-05-11 17:20:37 -0700378 int al, i, ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800379 unsigned int n;
380 SSL3_RECORD *rr;
Kenny Roote99801b2015-11-06 15:31:15 -0800381 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800382
Adam Langley4139edb2016-01-13 15:00:54 -0800383 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
384 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
Adam Langleyd9e397b2015-01-22 14:27:53 -0800385 (peek && type != SSL3_RT_APPLICATION_DATA)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000386 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800387 return -1;
388 }
389
Adam Langleye9ada862015-05-11 17:20:37 -0700390 /* This may require multiple iterations. False Start will cause
Adam Langley4139edb2016-01-13 15:00:54 -0800391 * |ssl->handshake_func| to signal success one step early, but the handshake
Adam Langleye9ada862015-05-11 17:20:37 -0700392 * must be completely finished before other modes are accepted.
393 *
394 * TODO(davidben): Move this check up to a higher level. */
Adam Langley4139edb2016-01-13 15:00:54 -0800395 while (!ssl->in_handshake && SSL_in_init(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700396 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langley4139edb2016-01-13 15:00:54 -0800397 i = ssl->handshake_func(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800398 if (i < 0) {
399 return i;
400 }
401 if (i == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000402 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 return -1;
404 }
405 }
406
407start:
Adam Langley4139edb2016-01-13 15:00:54 -0800408 ssl->rwstate = SSL_NOTHING;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409
Adam Langley4139edb2016-01-13 15:00:54 -0800410 /* ssl->s3->rrec.type - is the type of record
411 * ssl->s3->rrec.data - data
412 * ssl->s3->rrec.off - offset into 'data' for next read
413 * ssl->s3->rrec.length - number of bytes. */
414 rr = &ssl->s3->rrec;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800415
416 /* get new packet if necessary */
Kenny Rootb8494592015-09-25 02:29:14 +0000417 if (rr->length == 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800418 ret = ssl3_get_record(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800419 if (ret <= 0) {
420 return ret;
421 }
422 }
423
424 /* we now have a packet which can be read and processed */
425
Adam Langleyd9e397b2015-01-22 14:27:53 -0800426 /* If the other end has shut down, throw anything we read away (even in
427 * 'peek' mode) */
Adam Langley4139edb2016-01-13 15:00:54 -0800428 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800429 rr->length = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800430 ssl->rwstate = SSL_NOTHING;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800431 return 0;
432 }
433
Kenny Rootb8494592015-09-25 02:29:14 +0000434 if (type != 0 && type == rr->type) {
Adam Langley4139edb2016-01-13 15:00:54 -0800435 ssl->s3->warning_alert_count = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000436
Adam Langley4139edb2016-01-13 15:00:54 -0800437 /* Make sure that we are not getting application data when we are doing a
438 * handshake for the first time. */
439 if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA &&
440 ssl->aead_read_ctx == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800441 /* TODO(davidben): Is this check redundant with the handshake_func
442 * check? */
443 al = SSL_AD_UNEXPECTED_MESSAGE;
Kenny Rootb8494592015-09-25 02:29:14 +0000444 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800445 goto f_err;
446 }
447
Kenny Rootb8494592015-09-25 02:29:14 +0000448 /* Discard empty records. */
449 if (rr->length == 0) {
450 goto start;
451 }
452
Adam Langleyd9e397b2015-01-22 14:27:53 -0800453 if (len <= 0) {
454 return len;
455 }
456
457 if ((unsigned int)len > rr->length) {
458 n = rr->length;
459 } else {
460 n = (unsigned int)len;
461 }
462
Adam Langley4139edb2016-01-13 15:00:54 -0800463 memcpy(buf, rr->data, n);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800464 if (!peek) {
465 rr->length -= n;
Adam Langley4139edb2016-01-13 15:00:54 -0800466 rr->data += n;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800467 if (rr->length == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000468 /* The record has been consumed, so we may now clear the buffer. */
Adam Langley4139edb2016-01-13 15:00:54 -0800469 ssl_read_buffer_discard(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800470 }
471 }
472
473 return n;
474 }
475
Adam Langleyf4e42722015-06-04 17:45:09 -0700476 /* Process unexpected records. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800477
Adam Langley4139edb2016-01-13 15:00:54 -0800478 if (type == SSL3_RT_APPLICATION_DATA && rr->type == SSL3_RT_HANDSHAKE) {
Adam Langleye9ada862015-05-11 17:20:37 -0700479 /* If peer renegotiations are disabled, all out-of-order handshake records
Adam Langleyf4e42722015-06-04 17:45:09 -0700480 * are fatal. Renegotiations as a server are never supported. */
Adam Langley4139edb2016-01-13 15:00:54 -0800481 if (ssl->server || !ssl3_can_renegotiate(ssl)) {
Adam Langleye9ada862015-05-11 17:20:37 -0700482 al = SSL_AD_NO_RENEGOTIATION;
Kenny Rootb8494592015-09-25 02:29:14 +0000483 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
Adam Langleye9ada862015-05-11 17:20:37 -0700484 goto f_err;
485 }
486
Adam Langley4139edb2016-01-13 15:00:54 -0800487 /* This must be a HelloRequest, possibly fragmented over multiple records.
488 * Consume data from the handshake protocol until it is complete. */
489 static const uint8_t kHelloRequest[] = {SSL3_MT_HELLO_REQUEST, 0, 0, 0};
490 while (ssl->s3->hello_request_len < sizeof(kHelloRequest)) {
491 if (rr->length == 0) {
492 /* Get a new record. */
493 goto start;
494 }
495 if (rr->data[0] != kHelloRequest[ssl->s3->hello_request_len]) {
496 al = SSL_AD_DECODE_ERROR;
497 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
498 goto f_err;
499 }
500 rr->data++;
501 rr->length--;
502 ssl->s3->hello_request_len++;
503 }
504 ssl->s3->hello_request_len = 0;
505
506 if (ssl->msg_callback) {
507 ssl->msg_callback(0, ssl->version, SSL3_RT_HANDSHAKE, kHelloRequest,
508 sizeof(kHelloRequest), ssl, ssl->msg_callback_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800509 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800510
Adam Langley4139edb2016-01-13 15:00:54 -0800511 if (!SSL_is_init_finished(ssl) || !ssl->s3->initial_handshake_complete) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700512 /* This cannot happen. If a handshake is in progress, |type| must be
513 * |SSL3_RT_HANDSHAKE|. */
514 assert(0);
Kenny Rootb8494592015-09-25 02:29:14 +0000515 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langleyf4e42722015-06-04 17:45:09 -0700516 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517 }
Adam Langleyf4e42722015-06-04 17:45:09 -0700518
Adam Langley4139edb2016-01-13 15:00:54 -0800519 if (ssl->renegotiate_mode == ssl_renegotiate_ignore) {
Adam Langleyfad63272015-11-12 12:15:39 -0800520 goto start;
521 }
522
Adam Langleyf4e42722015-06-04 17:45:09 -0700523 /* Renegotiation is only supported at quiescent points in the application
524 * protocol, namely in HTTPS, just before reading the HTTP response. Require
525 * the record-layer be idle and avoid complexities of sending a handshake
526 * record while an application_data record is being written. */
Adam Langley4139edb2016-01-13 15:00:54 -0800527 if (ssl_write_buffer_is_pending(ssl)) {
Adam Langleyf4e42722015-06-04 17:45:09 -0700528 al = SSL_AD_NO_RENEGOTIATION;
Kenny Rootb8494592015-09-25 02:29:14 +0000529 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
Adam Langleyf4e42722015-06-04 17:45:09 -0700530 goto f_err;
531 }
532
533 /* Begin a new handshake. */
Adam Langley4139edb2016-01-13 15:00:54 -0800534 ssl->s3->total_renegotiations++;
535 ssl->state = SSL_ST_CONNECT;
536 i = ssl->handshake_func(ssl);
Adam Langleyf4e42722015-06-04 17:45:09 -0700537 if (i < 0) {
538 return i;
539 }
540 if (i == 0) {
Kenny Rootb8494592015-09-25 02:29:14 +0000541 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langleyf4e42722015-06-04 17:45:09 -0700542 return -1;
543 }
544
545 /* The handshake completed synchronously. Continue reading records. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800546 goto start;
547 }
548
Adam Langleye9ada862015-05-11 17:20:37 -0700549 /* If an alert record, process one alert out of the record. Note that we allow
550 * a single record to contain multiple alerts. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800551 if (rr->type == SSL3_RT_ALERT) {
Adam Langleye9ada862015-05-11 17:20:37 -0700552 /* Alerts may not be fragmented. */
553 if (rr->length < 2) {
554 al = SSL_AD_DECODE_ERROR;
Kenny Rootb8494592015-09-25 02:29:14 +0000555 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
Adam Langleye9ada862015-05-11 17:20:37 -0700556 goto f_err;
557 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800558
Adam Langley4139edb2016-01-13 15:00:54 -0800559 if (ssl->msg_callback) {
560 ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
561 ssl->msg_callback_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800562 }
Adam Langley4139edb2016-01-13 15:00:54 -0800563 const uint8_t alert_level = rr->data[0];
564 const uint8_t alert_descr = rr->data[1];
Adam Langleye9ada862015-05-11 17:20:37 -0700565 rr->length -= 2;
Adam Langley4139edb2016-01-13 15:00:54 -0800566 rr->data += 2;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800567
Adam Langley4139edb2016-01-13 15:00:54 -0800568 if (ssl->info_callback != NULL) {
569 cb = ssl->info_callback;
570 } else if (ssl->ctx->info_callback != NULL) {
571 cb = ssl->ctx->info_callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800572 }
573
574 if (cb != NULL) {
Adam Langleye9ada862015-05-11 17:20:37 -0700575 uint16_t alert = (alert_level << 8) | alert_descr;
Adam Langley4139edb2016-01-13 15:00:54 -0800576 cb(ssl, SSL_CB_READ_ALERT, alert);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800577 }
578
Adam Langleye9ada862015-05-11 17:20:37 -0700579 if (alert_level == SSL3_AL_WARNING) {
Adam Langley4139edb2016-01-13 15:00:54 -0800580 ssl->s3->warn_alert = alert_descr;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800581 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
Adam Langley4139edb2016-01-13 15:00:54 -0800582 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800583 return 0;
584 }
585
586 /* This is a warning but we receive it if we requested renegotiation and
587 * the peer denied it. Terminate with a fatal alert because if
588 * application tried to renegotiatie it presumably had a good reason and
589 * expects it to succeed.
590 *
591 * In future we might have a renegotiation where we don't care if the
592 * peer refused it where we carry on. */
593 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
594 al = SSL_AD_HANDSHAKE_FAILURE;
Kenny Rootb8494592015-09-25 02:29:14 +0000595 OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
596 goto f_err;
597 }
598
Adam Langley4139edb2016-01-13 15:00:54 -0800599 ssl->s3->warning_alert_count++;
600 if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
Kenny Rootb8494592015-09-25 02:29:14 +0000601 al = SSL_AD_UNEXPECTED_MESSAGE;
602 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800603 goto f_err;
604 }
Adam Langleye9ada862015-05-11 17:20:37 -0700605 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800606 char tmp[16];
607
Adam Langley4139edb2016-01-13 15:00:54 -0800608 ssl->rwstate = SSL_NOTHING;
609 ssl->s3->fatal_alert = alert_descr;
Kenny Rootb8494592015-09-25 02:29:14 +0000610 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800611 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
612 ERR_add_error_data(2, "SSL alert number ", tmp);
Adam Langley4139edb2016-01-13 15:00:54 -0800613 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
614 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800615 return 0;
616 } else {
617 al = SSL_AD_ILLEGAL_PARAMETER;
Kenny Rootb8494592015-09-25 02:29:14 +0000618 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800619 goto f_err;
620 }
621
622 goto start;
623 }
624
Adam Langley4139edb2016-01-13 15:00:54 -0800625 if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
Kenny Rootb8494592015-09-25 02:29:14 +0000626 /* close_notify has been sent, so discard all records other than alerts. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800627 rr->length = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000628 goto start;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800629 }
630
Adam Langleye9ada862015-05-11 17:20:37 -0700631 al = SSL_AD_UNEXPECTED_MESSAGE;
Kenny Rootb8494592015-09-25 02:29:14 +0000632 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800633
634f_err:
Adam Langley4139edb2016-01-13 15:00:54 -0800635 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800636err:
637 return -1;
638}
639
Adam Langley4139edb2016-01-13 15:00:54 -0800640int ssl3_do_change_cipher_spec(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800641 int i;
642
Adam Langley4139edb2016-01-13 15:00:54 -0800643 if (ssl->state & SSL_ST_ACCEPT) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800644 i = SSL3_CHANGE_CIPHER_SERVER_READ;
645 } else {
646 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
647 }
648
Adam Langley4139edb2016-01-13 15:00:54 -0800649 if (ssl->s3->tmp.key_block == NULL) {
650 if (ssl->session == NULL || ssl->session->master_key_length == 0) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800651 /* might happen if dtls1_read_bytes() calls this */
Kenny Rootb8494592015-09-25 02:29:14 +0000652 OPENSSL_PUT_ERROR(SSL, SSL_R_CCS_RECEIVED_EARLY);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800653 return 0;
654 }
655
Adam Langley4139edb2016-01-13 15:00:54 -0800656 ssl->session->cipher = ssl->s3->tmp.new_cipher;
657 if (!ssl->enc_method->setup_key_block(ssl)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800658 return 0;
659 }
660 }
661
Adam Langley4139edb2016-01-13 15:00:54 -0800662 if (!ssl->enc_method->change_cipher_state(ssl, i)) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800663 return 0;
664 }
665
666 return 1;
667}
668
Adam Langley4139edb2016-01-13 15:00:54 -0800669int ssl3_send_alert(SSL *ssl, int level, int desc) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800670 /* Map tls/ssl alert value to correct one */
Adam Langley4139edb2016-01-13 15:00:54 -0800671 desc = ssl->enc_method->alert_value(desc);
672 if (ssl->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800673 /* SSL 3.0 does not have protocol_version alerts */
674 desc = SSL_AD_HANDSHAKE_FAILURE;
675 }
676 if (desc < 0) {
677 return -1;
678 }
679
680 /* If a fatal one, remove from cache */
Adam Langley4139edb2016-01-13 15:00:54 -0800681 if (level == 2 && ssl->session != NULL) {
682 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800683 }
684
Adam Langley4139edb2016-01-13 15:00:54 -0800685 ssl->s3->alert_dispatch = 1;
686 ssl->s3->send_alert[0] = level;
687 ssl->s3->send_alert[1] = desc;
688 if (!ssl_write_buffer_is_pending(ssl)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000689 /* Nothing is being written out, so the alert may be dispatched
690 * immediately. */
Adam Langley4139edb2016-01-13 15:00:54 -0800691 return ssl->method->ssl_dispatch_alert(ssl);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800692 }
693
694 /* else data is still being written out, we will get written some time in the
695 * future */
696 return -1;
697}
698
Adam Langley4139edb2016-01-13 15:00:54 -0800699int ssl3_dispatch_alert(SSL *ssl) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800700 int i, j;
Kenny Roote99801b2015-11-06 15:31:15 -0800701 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800702
Adam Langley4139edb2016-01-13 15:00:54 -0800703 ssl->s3->alert_dispatch = 0;
704 i = do_ssl3_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800705 if (i <= 0) {
Adam Langley4139edb2016-01-13 15:00:54 -0800706 ssl->s3->alert_dispatch = 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800707 } else {
708 /* Alert sent to BIO. If it is important, flush it now. If the message
709 * does not get sent due to non-blocking IO, we will not worry too much. */
Adam Langley4139edb2016-01-13 15:00:54 -0800710 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
711 BIO_flush(ssl->wbio);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800712 }
713
Adam Langley4139edb2016-01-13 15:00:54 -0800714 if (ssl->msg_callback) {
715 ssl->msg_callback(1, ssl->version, SSL3_RT_ALERT, ssl->s3->send_alert, 2,
716 ssl, ssl->msg_callback_arg);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800717 }
718
Adam Langley4139edb2016-01-13 15:00:54 -0800719 if (ssl->info_callback != NULL) {
720 cb = ssl->info_callback;
721 } else if (ssl->ctx->info_callback != NULL) {
722 cb = ssl->ctx->info_callback;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800723 }
724
725 if (cb != NULL) {
Adam Langley4139edb2016-01-13 15:00:54 -0800726 j = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
727 cb(ssl, SSL_CB_WRITE_ALERT, j);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800728 }
729 }
730
731 return i;
732}