blob: 171b11d86487491661d4567d5ded213ccbc814ba [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
109#include <assert.h>
Adam Langley87750b42014-06-20 12:00:00 -0700110#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700111#include <stdio.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400112#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700113
114#include <openssl/buf.h>
115#include <openssl/err.h>
116#include <openssl/evp.h>
117#include <openssl/mem.h>
118#include <openssl/rand.h>
119
David Benjamin2ee94aa2015-04-07 22:38:30 -0400120#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -0700121
Adam Langleyfcf25832014-12-18 17:42:32 -0800122
123static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400124 char fragment);
Adam Langley95c29f32014-06-20 12:00:00 -0700125static int ssl3_get_record(SSL *s);
126
David Benjaminb1f5bca2015-05-08 22:54:02 -0400127int ssl3_read_n(SSL *s, int n, int extend) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800128 /* If |extend| is 0, obtain new n-byte packet;
129 * if |extend| is 1, increase packet by another n bytes.
130 *
131 * The packet will be in the sub-array of |s->s3->rbuf.buf| specified by
David Benjamin9a41d1b2015-05-16 01:30:09 -0400132 * |s->packet| and |s->packet_length|. (If DTLS and |extend| is 0, additional
133 * bytes will be read into |rbuf|, up to the size of the buffer.)
134 *
135 * TODO(davidben): |dtls1_get_record| and |ssl3_get_record| have very
136 * different needs. Separate the two record layers. In DTLS, |BIO_read| is
137 * called at most once, and only when |extend| is 0. In TLS, the buffer never
138 * contains more than one record. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800139 int i, len, left;
David Benjaminef4962f2015-04-06 20:31:44 -0400140 uintptr_t align = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800141 uint8_t *pkt;
142 SSL3_BUFFER *rb;
Adam Langley95c29f32014-06-20 12:00:00 -0700143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144 if (n <= 0) {
145 return n;
146 }
Adam Langley95c29f32014-06-20 12:00:00 -0700147
Adam Langleyfcf25832014-12-18 17:42:32 -0800148 rb = &s->s3->rbuf;
149 if (rb->buf == NULL && !ssl3_setup_read_buffer(s)) {
150 return -1;
151 }
Adam Langley95c29f32014-06-20 12:00:00 -0700152
Adam Langleyfcf25832014-12-18 17:42:32 -0800153 left = rb->left;
Adam Langley95c29f32014-06-20 12:00:00 -0700154
David Benjaminef4962f2015-04-06 20:31:44 -0400155 align = (uintptr_t)rb->buf + SSL3_RT_HEADER_LENGTH;
156 align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700157
Adam Langleyfcf25832014-12-18 17:42:32 -0800158 if (!extend) {
159 /* start with empty packet ... */
160 if (left == 0) {
161 rb->offset = align;
162 } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
163 /* check if next packet length is large enough to justify payload
164 * alignment... */
165 pkt = rb->buf + rb->offset;
166 if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) {
167 /* Note that even if packet is corrupted and its length field is
168 * insane, we can only be led to wrong decision about whether memmove
169 * will occur or not. Header values has no effect on memmove arguments
170 * and therefore no buffer overrun can be triggered. */
171 memmove(rb->buf + align, pkt, left);
172 rb->offset = align;
173 }
174 }
175 s->packet = rb->buf + rb->offset;
176 s->packet_length = 0;
177 /* ... now we can act as if 'extend' was set */
178 }
Adam Langley95c29f32014-06-20 12:00:00 -0700179
Adam Langleyfcf25832014-12-18 17:42:32 -0800180 /* For DTLS/UDP reads should not span multiple packets because the read
181 * operation returns the whole packet at once (as long as it fits into the
David Benjamin9a41d1b2015-05-16 01:30:09 -0400182 * buffer). Moreover, if |extend| is true, we must not read another packet,
183 * even if the entire packet was consumed. */
184 if (SSL_IS_DTLS(s) && ((left > 0 && n > left) || extend)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800185 n = left;
186 }
Adam Langley95c29f32014-06-20 12:00:00 -0700187
Adam Langleyfcf25832014-12-18 17:42:32 -0800188 /* if there is enough in the buffer from a previous read, take some */
189 if (left >= n) {
190 s->packet_length += n;
191 rb->left = left - n;
192 rb->offset += n;
193 return n;
194 }
Adam Langley95c29f32014-06-20 12:00:00 -0700195
Adam Langleyfcf25832014-12-18 17:42:32 -0800196 /* else we need to read more data */
Adam Langley95c29f32014-06-20 12:00:00 -0700197
Adam Langleyfcf25832014-12-18 17:42:32 -0800198 len = s->packet_length;
199 pkt = rb->buf + align;
200 /* Move any available bytes to front of buffer: |len| bytes already pointed
201 * to by |packet|, |left| extra ones at the end. */
202 if (s->packet != pkt) {
203 /* len > 0 */
204 memmove(pkt, s->packet, len + left);
205 s->packet = pkt;
206 rb->offset = len + align;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
Adam Langleyc4482d62015-03-09 17:49:31 -0700209 if (n > (int)(rb->len - rb->offset)) {
210 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR);
211 return -1;
212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
David Benjaminb1f5bca2015-05-08 22:54:02 -0400214 int max = n;
David Benjamin9a41d1b2015-05-16 01:30:09 -0400215 if (SSL_IS_DTLS(s) && !extend) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400216 max = rb->len - rb->offset;
Adam Langleyfcf25832014-12-18 17:42:32 -0800217 }
Adam Langley95c29f32014-06-20 12:00:00 -0700218
Adam Langleyfcf25832014-12-18 17:42:32 -0800219 while (left < n) {
220 /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
221 * read in more until we have len+n (up to len+max if possible). */
222 ERR_clear_system_error();
223 if (s->rbio != NULL) {
224 s->rwstate = SSL_READING;
225 i = BIO_read(s->rbio, pkt + len + left, max - left);
226 } else {
227 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
228 i = -1;
229 }
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Adam Langleyfcf25832014-12-18 17:42:32 -0800231 if (i <= 0) {
232 rb->left = left;
David Benjamin9417b762015-05-08 22:30:06 -0400233 if (len + left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800234 ssl3_release_read_buffer(s);
235 }
236 return i;
237 }
238 left += i;
239 /* reads should *never* span multiple packets for DTLS because the
240 * underlying transport protocol is message oriented as opposed to byte
241 * oriented as in the TLS case. */
242 if (SSL_IS_DTLS(s) && n > left) {
243 n = left; /* makes the while condition false */
244 }
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* done reading, now the book-keeping */
248 rb->offset += n;
249 rb->left = left - n;
250 s->packet_length += n;
251 s->rwstate = SSL_NOTHING;
252
253 return n;
254}
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langley48105fa2014-06-20 12:00:00 -0700256/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
257 * be processed per call to ssl3_get_record. Without this limit an attacker
258 * could send empty records at a faster rate than we can process and cause
259 * ssl3_get_record to loop forever. */
260#define MAX_EMPTY_RECORDS 32
261
Adam Langleyfcf25832014-12-18 17:42:32 -0800262/* Call this to get a new input record. It will return <= 0 if more data is
263 * needed, normally due to an error or non-blocking IO. When it finishes, one
264 * packet has been decoded and can be found in
Adam Langley95c29f32014-06-20 12:00:00 -0700265 * ssl->s3->rrec.type - is the type of record
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 * ssl->s3->rrec.data - data
267 * ssl->s3->rrec.length - number of bytes */
Adam Langley95c29f32014-06-20 12:00:00 -0700268/* used only by ssl3_read_bytes */
Adam Langleyfcf25832014-12-18 17:42:32 -0800269static int ssl3_get_record(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -0500270 uint8_t ssl_major, ssl_minor;
271 int al, n, i, ret = -1;
272 SSL3_RECORD *rr = &s->s3->rrec;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 uint8_t *p;
David Benjamin31a07792015-03-03 14:20:26 -0500274 uint16_t version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 size_t extra;
276 unsigned empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langley95c29f32014-06-20 12:00:00 -0700278again:
Adam Langleyfcf25832014-12-18 17:42:32 -0800279 /* check if we have the header */
280 if (s->rstate != SSL_ST_READ_BODY ||
281 s->packet_length < SSL3_RT_HEADER_LENGTH) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400282 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (n <= 0) {
284 return n; /* error or non-blocking */
285 }
286 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamin6a08da22015-05-08 22:58:12 -0400288 /* Some bytes were read, so the read buffer must be existant and
289 * |s->s3->init_extra| is defined. */
290 assert(s->s3->rbuf.buf != NULL);
291 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
292
Adam Langleyfcf25832014-12-18 17:42:32 -0800293 p = s->packet;
294 if (s->msg_callback) {
295 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langleyfcf25832014-12-18 17:42:32 -0800298 /* Pull apart the header into the SSL3_RECORD */
299 rr->type = *(p++);
300 ssl_major = *(p++);
301 ssl_minor = *(p++);
David Benjamin31a07792015-03-03 14:20:26 -0500302 version = (((uint16_t)ssl_major) << 8) | ssl_minor;
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700304
Adam Langleyfcf25832014-12-18 17:42:32 -0800305 if (s->s3->have_version && version != s->version) {
306 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800307 al = SSL_AD_PROTOCOL_VERSION;
308 goto f_err;
309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 if ((version >> 8) != SSL3_VERSION_MAJOR) {
312 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
313 goto err;
314 }
Adam Langley95c29f32014-06-20 12:00:00 -0700315
David Benjamin4a3f0732015-04-05 02:21:33 -0400316 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800317 al = SSL_AD_RECORD_OVERFLOW;
David Benjamin4a3f0732015-04-05 02:21:33 -0400318 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 goto f_err;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langleyfcf25832014-12-18 17:42:32 -0800322 /* now s->rstate == SSL_ST_READ_BODY */
David Benjamin6a08da22015-05-08 22:58:12 -0400323 } else {
324 /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The
325 * read buffer must be existant and |s->s3->init_extra| is defined. */
326 assert(s->s3->rbuf.buf != NULL);
327 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700331
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
333 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
334 i = rr->length;
David Benjaminb1f5bca2015-05-08 22:54:02 -0400335 n = ssl3_read_n(s, i, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 if (n <= 0) {
337 /* Error or non-blocking IO. Now |n| == |rr->length|, and
338 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
339 return n;
340 }
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langleyfcf25832014-12-18 17:42:32 -0800343 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin31a07792015-03-03 14:20:26 -0500345 /* |rr->data| points to |rr->length| bytes of ciphertext in |s->packet|. */
346 rr->data = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700347
David Benjamin31a07792015-03-03 14:20:26 -0500348 /* Decrypt the packet in-place.
349 *
350 * TODO(davidben): This assumes |s->version| is the same as the record-layer
351 * version which isn't always true, but it only differs with the NULL cipher
352 * which ignores the parameter. */
353 size_t plaintext_len;
354 if (!SSL_AEAD_CTX_open(s->aead_read_ctx, rr->data, &plaintext_len, rr->length,
355 rr->type, s->version, s->s3->read_sequence, rr->data,
356 rr->length)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 al = SSL_AD_BAD_RECORD_MAC;
358 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
359 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
360 goto f_err;
361 }
David Benjamin31a07792015-03-03 14:20:26 -0500362 if (!ssl3_record_sequence_update(s->s3->read_sequence, 8)) {
363 goto err;
364 }
365 if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 al = SSL_AD_RECORD_OVERFLOW;
367 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
368 goto f_err;
369 }
David Benjamin31a07792015-03-03 14:20:26 -0500370 assert(plaintext_len <= (1u << 16));
371 rr->length = plaintext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 rr->off = 0;
374 /* So at this point the following is true:
375 * ssl->s3->rrec.type is the type of record;
376 * ssl->s3->rrec.length is the number of bytes in the record;
377 * ssl->s3->rrec.off is the offset to first valid byte;
David Benjamin31a07792015-03-03 14:20:26 -0500378 * ssl->s3->rrec.data the first byte of the record body. */
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 /* we have pulled in a full packet so zero things */
381 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 /* just read a 0 length packet */
384 if (rr->length == 0) {
385 empty_record_count++;
386 if (empty_record_count > MAX_EMPTY_RECORDS) {
387 al = SSL_AD_UNEXPECTED_MESSAGE;
388 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
389 goto f_err;
390 }
391 goto again;
392 }
Adam Langley95c29f32014-06-20 12:00:00 -0700393
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700395
396f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800397 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700398err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 return ret;
400}
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402/* Call this to write data in records of type |type|. It will return <= 0 if
403 * not all data has been sent or non-blocking IO. */
404int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
405 const uint8_t *buf = buf_;
406 unsigned int tot, n, nw;
407 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700408
Adam Langleyfcf25832014-12-18 17:42:32 -0800409 s->rwstate = SSL_NOTHING;
410 assert(s->s3->wnum <= INT_MAX);
411 tot = s->s3->wnum;
412 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700413
David Benjamined7c4752015-02-16 19:16:46 -0500414 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 i = s->handshake_func(s);
416 if (i < 0) {
417 return i;
418 }
419 if (i == 0) {
420 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
421 return -1;
422 }
423 }
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 /* Ensure that if we end up with a smaller value of data to write out than
426 * the the original len from a write which didn't complete for non-blocking
427 * I/O and also somehow ended up avoiding the check for this in
428 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
429 * end up with (len-tot) as a large number that will then promptly send
430 * beyond the end of the users buffer ... so we trap and report the error in
431 * a way the user will notice. */
432 if (len < 0 || (size_t)len < tot) {
433 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
434 return -1;
435 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700436
David Benjamin9faafda2015-04-04 19:23:10 -0400437 int record_split_done = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800438 n = (len - tot);
439 for (;;) {
440 /* max contains the maximum number of bytes that we can put into a
441 * record. */
442 unsigned max = s->max_send_fragment;
443 /* fragment is true if do_ssl3_write should send the first byte in its own
444 * record in order to randomise a CBC IV. */
445 int fragment = 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400446 if (!record_split_done && s->s3->need_record_splitting &&
447 type == SSL3_RT_APPLICATION_DATA) {
448 /* Only the the first record per write call needs to be split. The
449 * remaining plaintext was determined before the IV was randomized. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 fragment = 1;
David Benjamin9faafda2015-04-04 19:23:10 -0400451 record_split_done = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800452 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 if (n > max) {
454 nw = max;
455 } else {
456 nw = n;
457 }
Adam Langley95c29f32014-06-20 12:00:00 -0700458
David Benjamin9faafda2015-04-04 19:23:10 -0400459 i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 if (i <= 0) {
461 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 return i;
463 }
Adam Langley95c29f32014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
466 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 return tot + i;
468 }
Adam Langley95c29f32014-06-20 12:00:00 -0700469
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 n -= i;
471 tot += i;
472 }
473}
Adam Langley95c29f32014-06-20 12:00:00 -0700474
David Benjamin9faafda2015-04-04 19:23:10 -0400475/* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
476 * writes it to |out|. At most |max_out| bytes will be written. It returns one
David Benjamin31a07792015-03-03 14:20:26 -0500477 * on success and zero on error. On success, it updates the write sequence
478 * number. */
David Benjamin9faafda2015-04-04 19:23:10 -0400479static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
480 size_t max_out, uint8_t type, const uint8_t *in,
481 size_t in_len) {
482 if (max_out < SSL3_RT_HEADER_LENGTH) {
483 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
484 return 0;
485 }
486
487 out[0] = type;
488
489 /* Some servers hang if initial ClientHello is larger than 256 bytes and
490 * record version number > TLS 1.0. */
David Benjamin31a07792015-03-03 14:20:26 -0500491 uint16_t wire_version = s->version;
David Benjamin9faafda2015-04-04 19:23:10 -0400492 if (!s->s3->have_version && s->version > SSL3_VERSION) {
David Benjamin31a07792015-03-03 14:20:26 -0500493 wire_version = TLS1_VERSION;
494 }
495 out[1] = wire_version >> 8;
496 out[2] = wire_version & 0xff;
497
498 size_t ciphertext_len;
499 if (!SSL_AEAD_CTX_seal(s->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
500 &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
501 type, wire_version, s->s3->write_sequence, in,
502 in_len) ||
503 !ssl3_record_sequence_update(s->s3->write_sequence, 8)) {
504 return 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400505 }
506
David Benjamin31a07792015-03-03 14:20:26 -0500507 if (ciphertext_len >= 1 << 16) {
David Benjamin9faafda2015-04-04 19:23:10 -0400508 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
509 return 0;
510 }
David Benjamin31a07792015-03-03 14:20:26 -0500511 out[3] = ciphertext_len >> 8;
512 out[4] = ciphertext_len & 0xff;
David Benjamin9faafda2015-04-04 19:23:10 -0400513
David Benjamin31a07792015-03-03 14:20:26 -0500514 *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
David Benjamin9faafda2015-04-04 19:23:10 -0400515
516 if (s->msg_callback) {
517 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
518 s, s->msg_callback_arg);
519 }
520
521 return 1;
522}
523
Adam Langleyd493d522014-06-20 12:00:00 -0700524/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
525 * then it splits the record into a one byte record and a record with the rest
David Benjamin9faafda2015-04-04 19:23:10 -0400526 * of the data in order to randomise a CBC IV. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800527static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400528 char fragment) {
529 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 /* first check if there is a SSL3_BUFFER still being written out. This will
532 * happen with non blocking IO */
533 if (wb->left != 0) {
534 return ssl3_write_pending(s, type, buf, len);
535 }
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 /* If we have an alert to send, lets send it */
538 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400539 int ret = s->method->ssl_dispatch_alert(s);
540 if (ret <= 0) {
541 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800542 }
543 /* if it went, fall through and send more stuff */
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
547 return -1;
548 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700549
Adam Langleyfcf25832014-12-18 17:42:32 -0800550 if (len == 0) {
551 return 0;
552 }
David Benjamin9faafda2015-04-04 19:23:10 -0400553 if (len == 1) {
554 /* No sense in fragmenting a one-byte record. */
555 fragment = 0;
556 }
Adam Langley95c29f32014-06-20 12:00:00 -0700557
David Benjamin9faafda2015-04-04 19:23:10 -0400558 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
559 uintptr_t align;
Adam Langleyfcf25832014-12-18 17:42:32 -0800560 if (fragment) {
David Benjamin9faafda2015-04-04 19:23:10 -0400561 /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
562 * multiple of the block size which we may assume is aligned. Thus we only
563 * need to account for a second copy of the record header. */
564 align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 } else {
David Benjamin9faafda2015-04-04 19:23:10 -0400566 align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
567 }
David Benjaminef4962f2015-04-06 20:31:44 -0400568 align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
David Benjamin9faafda2015-04-04 19:23:10 -0400569 uint8_t *out = wb->buf + align;
570 wb->offset = align;
571 size_t max_out = wb->len - wb->offset;
572
573 const uint8_t *orig_buf = buf;
574 unsigned int orig_len = len;
575 size_t fragment_len = 0;
576 if (fragment) {
577 /* Write the first byte in its own record as a countermeasure against
578 * known-IV weaknesses in CBC ciphersuites. (See
579 * http://www.openssl.org/~bodo/tls-cbc.txt.) */
580 if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
581 return -1;
582 }
583 out += fragment_len;
584 max_out -= fragment_len;
585 buf++;
586 len--;
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
David Benjamin9faafda2015-04-04 19:23:10 -0400589 assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
590 == 0);
591 size_t ciphertext_len;
592 if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
593 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800594 }
David Benjamin9faafda2015-04-04 19:23:10 -0400595 ciphertext_len += fragment_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700596
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 /* now let's set up wb */
David Benjamin9faafda2015-04-04 19:23:10 -0400598 wb->left = ciphertext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 /* memorize arguments so that ssl3_write_pending can detect bad write retries
601 * later */
David Benjamin9faafda2015-04-04 19:23:10 -0400602 s->s3->wpend_tot = orig_len;
603 s->s3->wpend_buf = orig_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 s->s3->wpend_type = type;
David Benjamin9faafda2015-04-04 19:23:10 -0400605 s->s3->wpend_ret = orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800606
607 /* we now just need to write the buffer */
David Benjamin9faafda2015-04-04 19:23:10 -0400608 return ssl3_write_pending(s, type, orig_buf, orig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800609}
Adam Langley95c29f32014-06-20 12:00:00 -0700610
611/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800612int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
613 int i;
614 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700615
Adam Langleyfcf25832014-12-18 17:42:32 -0800616 if (s->s3->wpend_tot > (int)len ||
617 (s->s3->wpend_buf != buf &&
618 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
619 s->s3->wpend_type != type) {
620 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
621 return -1;
622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 for (;;) {
625 ERR_clear_system_error();
626 if (s->wbio != NULL) {
627 s->rwstate = SSL_WRITING;
628 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
629 (unsigned int)wb->left);
630 } else {
631 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
632 i = -1;
633 }
634 if (i == wb->left) {
635 wb->left = 0;
636 wb->offset += i;
David Benjamin9417b762015-05-08 22:30:06 -0400637 ssl3_release_write_buffer(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 s->rwstate = SSL_NOTHING;
639 return s->s3->wpend_ret;
640 } else if (i <= 0) {
641 if (SSL_IS_DTLS(s)) {
David Benjaminaebefed2015-05-08 22:20:04 -0400642 /* For DTLS, just drop it. That's kind of the whole point in
643 * using a datagram service */
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 wb->left = 0;
645 }
646 return i;
647 }
David Benjamin883e49f2015-04-05 12:19:14 -0400648 /* TODO(davidben): This codepath is used in DTLS, but the write
649 * payload may not split across packets. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 wb->offset += i;
651 wb->left -= i;
652 }
653}
Adam Langley95c29f32014-06-20 12:00:00 -0700654
David Benjamin86271ee2014-07-21 16:14:03 -0400655/* ssl3_expect_change_cipher_spec informs the record layer that a
656 * ChangeCipherSpec record is required at this point. If a Handshake record is
657 * received before ChangeCipherSpec, the connection will fail. Moreover, if
658 * there are unprocessed handshake bytes, the handshake will also fail and the
659 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800660int ssl3_expect_change_cipher_spec(SSL *s) {
661 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
662 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
663 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
664 return 0;
665 }
666
667 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
668 return 1;
669}
David Benjamin86271ee2014-07-21 16:14:03 -0400670
Adam Langley95c29f32014-06-20 12:00:00 -0700671/* Return up to 'len' payload bytes received in 'type' records.
672 * 'type' is one of the following:
673 *
674 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
675 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
676 * - 0 (during a shutdown, no data has to be returned)
677 *
678 * If we don't have stored data to work from, read a SSL/TLS record first
679 * (possibly multiple records if we still don't have anything to return).
680 *
681 * This function must handle any surprises the peer may have for us, such as
682 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
683 * a surprise, but handled as if it were), or renegotiation requests.
684 * Also if record payloads contain fragments too small to process, we store
685 * them until there is enough for the respective protocol (the record protocol
686 * may use arbitrary fragmentation and even interleaving):
687 * Change cipher spec protocol
688 * just 1 byte needed, no need for keeping anything stored
689 * Alert protocol
690 * 2 bytes needed (AlertLevel, AlertDescription)
691 * Handshake protocol
692 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
693 * to detect unexpected Client Hello and Hello Request messages
694 * here, anything else is handled by higher layers
695 * Application data protocol
696 * none of our business
697 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800698int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500699 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800700 unsigned int n;
701 SSL3_RECORD *rr;
702 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700703
Adam Langleyfcf25832014-12-18 17:42:32 -0800704 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
705 (peek && type != SSL3_RT_APPLICATION_DATA)) {
706 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
707 return -1;
708 }
Adam Langley95c29f32014-06-20 12:00:00 -0700709
Adam Langleyfcf25832014-12-18 17:42:32 -0800710 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
711 /* (partially) satisfy request from storage */
712 uint8_t *src = s->s3->handshake_fragment;
713 uint8_t *dst = buf;
714 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700715
Adam Langleyfcf25832014-12-18 17:42:32 -0800716 /* peek == 0 */
717 n = 0;
718 while (len > 0 && s->s3->handshake_fragment_len > 0) {
719 *dst++ = *src++;
720 len--;
721 s->s3->handshake_fragment_len--;
722 n++;
723 }
724 /* move any remaining fragment bytes: */
725 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
726 s->s3->handshake_fragment[k] = *src++;
727 }
728 return n;
729 }
Adam Langley95c29f32014-06-20 12:00:00 -0700730
Adam Langleyfcf25832014-12-18 17:42:32 -0800731 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700732
David Benjamin931ab342015-02-08 19:46:57 -0500733 /* This may require multiple iterations. False Start will cause
734 * |s->handshake_func| to signal success one step early, but the handshake
735 * must be completely finished before other modes are accepted.
736 *
737 * TODO(davidben): Move this check up to a higher level. */
738 while (!s->in_handshake && SSL_in_init(s)) {
739 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800740 i = s->handshake_func(s);
741 if (i < 0) {
742 return i;
743 }
744 if (i == 0) {
745 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
746 return -1;
747 }
748 }
749
Adam Langley95c29f32014-06-20 12:00:00 -0700750start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700752
Adam Langleyfcf25832014-12-18 17:42:32 -0800753 /* s->s3->rrec.type - is the type of record
754 * s->s3->rrec.data - data
755 * s->s3->rrec.off - offset into 'data' for next read
756 * s->s3->rrec.length - number of bytes. */
757 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 /* get new packet if necessary */
760 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
761 ret = ssl3_get_record(s);
762 if (ret <= 0) {
763 return ret;
764 }
765 }
Adam Langley95c29f32014-06-20 12:00:00 -0700766
Adam Langleyfcf25832014-12-18 17:42:32 -0800767 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700768
David Benjamindc3da932015-03-12 15:09:02 -0400769 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
770 * ssl3_get_finished. */
771 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
772 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800773 al = SSL_AD_UNEXPECTED_MESSAGE;
774 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
775 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
776 goto f_err;
777 }
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
780 * Handshake record. */
781 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
782 al = SSL_AD_UNEXPECTED_MESSAGE;
783 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
784 goto f_err;
785 }
David Benjamin86271ee2014-07-21 16:14:03 -0400786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 /* If the other end has shut down, throw anything we read away (even in
788 * 'peek' mode) */
789 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
790 rr->length = 0;
791 s->rwstate = SSL_NOTHING;
792 return 0;
793 }
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 if (type == rr->type) {
796 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
797 /* make sure that we are not getting application data when we are doing a
798 * handshake for the first time */
799 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500800 s->aead_read_ctx == NULL) {
801 /* TODO(davidben): Is this check redundant with the handshake_func
802 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 al = SSL_AD_UNEXPECTED_MESSAGE;
804 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
805 goto f_err;
806 }
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 if (len <= 0) {
809 return len;
810 }
Adam Langley95c29f32014-06-20 12:00:00 -0700811
Adam Langleyfcf25832014-12-18 17:42:32 -0800812 if ((unsigned int)len > rr->length) {
813 n = rr->length;
814 } else {
815 n = (unsigned int)len;
816 }
Adam Langley95c29f32014-06-20 12:00:00 -0700817
Adam Langleyfcf25832014-12-18 17:42:32 -0800818 memcpy(buf, &(rr->data[rr->off]), n);
819 if (!peek) {
820 rr->length -= n;
821 rr->off += n;
822 if (rr->length == 0) {
823 s->rstate = SSL_ST_READ_HEADER;
824 rr->off = 0;
David Benjaminaebefed2015-05-08 22:20:04 -0400825 if (s->s3->rbuf.left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800826 ssl3_release_read_buffer(s);
827 }
828 }
829 }
830
831 return n;
832 }
Adam Langley95c29f32014-06-20 12:00:00 -0700833
David Benjamin44d3eed2015-05-21 01:29:55 -0400834 /* Process unexpected records. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400837 /* If peer renegotiations are disabled, all out-of-order handshake records
David Benjamin44d3eed2015-05-21 01:29:55 -0400838 * are fatal. Renegotiations as a server are never supported. */
839 if (!s->accept_peer_renegotiations || s->server) {
David Benjaminb16346b2015-04-08 19:16:58 -0400840 al = SSL_AD_NO_RENEGOTIATION;
841 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
842 goto f_err;
843 }
844
David Benjamin44d3eed2015-05-21 01:29:55 -0400845 /* HelloRequests may be fragmented across multiple records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 const size_t size = sizeof(s->s3->handshake_fragment);
847 const size_t avail = size - s->s3->handshake_fragment_len;
848 const size_t todo = (rr->length < avail) ? rr->length : avail;
849 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
850 &rr->data[rr->off], todo);
851 rr->off += todo;
852 rr->length -= todo;
853 s->s3->handshake_fragment_len += todo;
854 if (s->s3->handshake_fragment_len < size) {
855 goto start; /* fragment was too small */
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
David Benjamin44d3eed2015-05-21 01:29:55 -0400858 /* Parse out and consume a HelloRequest. */
859 if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST ||
860 s->s3->handshake_fragment[1] != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 s->s3->handshake_fragment[2] != 0 ||
862 s->s3->handshake_fragment[3] != 0) {
863 al = SSL_AD_DECODE_ERROR;
864 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
865 goto f_err;
866 }
David Benjamin44d3eed2015-05-21 01:29:55 -0400867 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (s->msg_callback) {
870 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
871 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
Adam Langleyfcf25832014-12-18 17:42:32 -0800874 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
875 ssl3_renegotiate(s);
876 if (ssl3_renegotiate_check(s)) {
877 i = s->handshake_func(s);
878 if (i < 0) {
879 return i;
880 }
881 if (i == 0) {
882 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
883 return -1;
884 }
885 }
886 }
887 /* we either finished a handshake or ignored the request, now try again to
888 * obtain the (application) data we were asked for */
889 goto start;
890 }
David Benjaminb4188f02014-11-01 03:43:48 -0400891
David Benjamin86058a22015-02-22 13:07:21 -0500892 /* If an alert record, process one alert out of the record. Note that we allow
893 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500895 /* Alerts may not be fragmented. */
896 if (rr->length < 2) {
897 al = SSL_AD_DECODE_ERROR;
898 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
899 goto f_err;
900 }
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500903 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 s->msg_callback_arg);
905 }
David Benjamin86058a22015-02-22 13:07:21 -0500906 const uint8_t alert_level = rr->data[rr->off++];
907 const uint8_t alert_descr = rr->data[rr->off++];
908 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 if (s->info_callback != NULL) {
911 cb = s->info_callback;
912 } else if (s->ctx->info_callback != NULL) {
913 cb = s->ctx->info_callback;
914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500917 uint16_t alert = (alert_level << 8) | alert_descr;
918 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
David Benjamin86058a22015-02-22 13:07:21 -0500921 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 s->s3->warn_alert = alert_descr;
923 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
924 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
925 return 0;
926 }
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 /* This is a warning but we receive it if we requested renegotiation and
929 * the peer denied it. Terminate with a fatal alert because if
930 * application tried to renegotiatie it presumably had a good reason and
931 * expects it to succeed.
932 *
933 * In future we might have a renegotiation where we don't care if the
934 * peer refused it where we carry on. */
935 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
936 al = SSL_AD_HANDSHAKE_FAILURE;
937 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
938 goto f_err;
939 }
David Benjamin86058a22015-02-22 13:07:21 -0500940 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700942
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 s->rwstate = SSL_NOTHING;
944 s->s3->fatal_alert = alert_descr;
945 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
946 SSL_AD_REASON_OFFSET + alert_descr);
947 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
948 ERR_add_error_data(2, "SSL alert number ", tmp);
949 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
950 SSL_CTX_remove_session(s->ctx, s->session);
951 return 0;
952 } else {
953 al = SSL_AD_ILLEGAL_PARAMETER;
954 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
955 goto f_err;
956 }
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 goto start;
959 }
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 if (s->shutdown & SSL_SENT_SHUTDOWN) {
962 /* but we have not received a shutdown */
963 s->rwstate = SSL_NOTHING;
964 rr->length = 0;
965 return 0;
966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
969 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
970 * record payload has to look like */
971 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
972 al = SSL_AD_ILLEGAL_PARAMETER;
973 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
974 goto f_err;
975 }
Adam Langley95c29f32014-06-20 12:00:00 -0700976
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 /* Check we have a cipher to change to */
978 if (s->s3->tmp.new_cipher == NULL) {
979 al = SSL_AD_UNEXPECTED_MESSAGE;
980 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
981 goto f_err;
982 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700983
Adam Langleyfcf25832014-12-18 17:42:32 -0800984 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
985 al = SSL_AD_UNEXPECTED_MESSAGE;
986 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
987 goto f_err;
988 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -0700991
Adam Langleyfcf25832014-12-18 17:42:32 -0800992 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700993
Adam Langleyfcf25832014-12-18 17:42:32 -0800994 if (s->msg_callback) {
995 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
996 s->msg_callback_arg);
997 }
Adam Langley95c29f32014-06-20 12:00:00 -0700998
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 s->s3->change_cipher_spec = 1;
1000 if (!ssl3_do_change_cipher_spec(s)) {
1001 goto err;
1002 } else {
1003 goto start;
1004 }
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
David Benjamine820df92015-02-08 16:06:54 -05001007 /* We already handled these. */
1008 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1009 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001010
David Benjamine820df92015-02-08 16:06:54 -05001011 al = SSL_AD_UNEXPECTED_MESSAGE;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001013
1014f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001016err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 return -1;
1018}
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020int ssl3_do_change_cipher_spec(SSL *s) {
1021 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 if (s->state & SSL_ST_ACCEPT) {
1024 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1025 } else {
1026 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1027 }
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 if (s->s3->tmp.key_block == NULL) {
1030 if (s->session == NULL || s->session->master_key_length == 0) {
1031 /* might happen if dtls1_read_bytes() calls this */
1032 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1033 SSL_R_CCS_RECEIVED_EARLY);
1034 return 0;
1035 }
Adam Langley95c29f32014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 s->session->cipher = s->s3->tmp.new_cipher;
1038 if (!s->enc_method->setup_key_block(s)) {
1039 return 0;
1040 }
1041 }
Adam Langley95c29f32014-06-20 12:00:00 -07001042
Adam Langleyfcf25832014-12-18 17:42:32 -08001043 if (!s->enc_method->change_cipher_state(s, i)) {
1044 return 0;
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 return 1;
1048}
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050int ssl3_send_alert(SSL *s, int level, int desc) {
1051 /* Map tls/ssl alert value to correct one */
1052 desc = s->enc_method->alert_value(desc);
1053 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1054 /* SSL 3.0 does not have protocol_version alerts */
1055 desc = SSL_AD_HANDSHAKE_FAILURE;
1056 }
1057 if (desc < 0) {
1058 return -1;
1059 }
Adam Langley95c29f32014-06-20 12:00:00 -07001060
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 /* If a fatal one, remove from cache */
1062 if (level == 2 && s->session != NULL) {
1063 SSL_CTX_remove_session(s->ctx, s->session);
1064 }
Adam Langley95c29f32014-06-20 12:00:00 -07001065
Adam Langleyfcf25832014-12-18 17:42:32 -08001066 s->s3->alert_dispatch = 1;
1067 s->s3->send_alert[0] = level;
1068 s->s3->send_alert[1] = desc;
1069 if (s->s3->wbuf.left == 0) {
1070 /* data is still being written out. */
1071 return s->method->ssl_dispatch_alert(s);
1072 }
Adam Langley95c29f32014-06-20 12:00:00 -07001073
Adam Langleyfcf25832014-12-18 17:42:32 -08001074 /* else data is still being written out, we will get written some time in the
1075 * future */
1076 return -1;
1077}
Adam Langley95c29f32014-06-20 12:00:00 -07001078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079int ssl3_dispatch_alert(SSL *s) {
1080 int i, j;
1081 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001082
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 s->s3->alert_dispatch = 0;
David Benjamin9faafda2015-04-04 19:23:10 -04001084 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001085 if (i <= 0) {
1086 s->s3->alert_dispatch = 1;
1087 } else {
1088 /* Alert sent to BIO. If it is important, flush it now. If the message
1089 * does not get sent due to non-blocking IO, we will not worry too much. */
1090 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1091 BIO_flush(s->wbio);
1092 }
Adam Langley95c29f32014-06-20 12:00:00 -07001093
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 if (s->msg_callback) {
1095 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1096 s->msg_callback_arg);
1097 }
1098
1099 if (s->info_callback != NULL) {
1100 cb = s->info_callback;
1101 } else if (s->ctx->info_callback != NULL) {
1102 cb = s->ctx->info_callback;
1103 }
1104
1105 if (cb != NULL) {
1106 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1107 cb(s, SSL_CB_WRITE_ALERT, j);
1108 }
1109 }
1110
1111 return i;
1112}