blob: d898cf248d2d3a2790cb2557a3c643fa887c83e8 [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
David Benjaminbd15a8e2015-05-29 18:48:16 -0400180 /* In DTLS, if there is leftover data from the previous packet or |extend| is
181 * true, clamp to the previous read. DTLS records may not span packet
182 * boundaries. */
183 if (SSL_IS_DTLS(s) && n > left && (left > 0 || extend)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800184 n = left;
185 }
Adam Langley95c29f32014-06-20 12:00:00 -0700186
Adam Langleyfcf25832014-12-18 17:42:32 -0800187 /* if there is enough in the buffer from a previous read, take some */
188 if (left >= n) {
189 s->packet_length += n;
190 rb->left = left - n;
191 rb->offset += n;
192 return n;
193 }
Adam Langley95c29f32014-06-20 12:00:00 -0700194
Adam Langleyfcf25832014-12-18 17:42:32 -0800195 /* else we need to read more data */
Adam Langley95c29f32014-06-20 12:00:00 -0700196
Adam Langleyfcf25832014-12-18 17:42:32 -0800197 len = s->packet_length;
198 pkt = rb->buf + align;
199 /* Move any available bytes to front of buffer: |len| bytes already pointed
200 * to by |packet|, |left| extra ones at the end. */
201 if (s->packet != pkt) {
202 /* len > 0 */
203 memmove(pkt, s->packet, len + left);
204 s->packet = pkt;
205 rb->offset = len + align;
206 }
Adam Langley95c29f32014-06-20 12:00:00 -0700207
Adam Langleyc4482d62015-03-09 17:49:31 -0700208 if (n > (int)(rb->len - rb->offset)) {
209 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR);
210 return -1;
211 }
Adam Langley95c29f32014-06-20 12:00:00 -0700212
David Benjaminb1f5bca2015-05-08 22:54:02 -0400213 int max = n;
David Benjamin9a41d1b2015-05-16 01:30:09 -0400214 if (SSL_IS_DTLS(s) && !extend) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400215 max = rb->len - rb->offset;
Adam Langleyfcf25832014-12-18 17:42:32 -0800216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 while (left < n) {
219 /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
220 * read in more until we have len+n (up to len+max if possible). */
221 ERR_clear_system_error();
222 if (s->rbio != NULL) {
223 s->rwstate = SSL_READING;
224 i = BIO_read(s->rbio, pkt + len + left, max - left);
225 } else {
226 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
227 i = -1;
228 }
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 if (i <= 0) {
231 rb->left = left;
David Benjamin9417b762015-05-08 22:30:06 -0400232 if (len + left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 ssl3_release_read_buffer(s);
234 }
235 return i;
236 }
237 left += i;
238 /* reads should *never* span multiple packets for DTLS because the
239 * underlying transport protocol is message oriented as opposed to byte
240 * oriented as in the TLS case. */
241 if (SSL_IS_DTLS(s) && n > left) {
242 n = left; /* makes the while condition false */
243 }
244 }
Adam Langley95c29f32014-06-20 12:00:00 -0700245
Adam Langleyfcf25832014-12-18 17:42:32 -0800246 /* done reading, now the book-keeping */
247 rb->offset += n;
248 rb->left = left - n;
249 s->packet_length += n;
250 s->rwstate = SSL_NOTHING;
251
252 return n;
253}
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Adam Langley48105fa2014-06-20 12:00:00 -0700255/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
256 * be processed per call to ssl3_get_record. Without this limit an attacker
257 * could send empty records at a faster rate than we can process and cause
258 * ssl3_get_record to loop forever. */
259#define MAX_EMPTY_RECORDS 32
260
Adam Langleyfcf25832014-12-18 17:42:32 -0800261/* Call this to get a new input record. It will return <= 0 if more data is
262 * needed, normally due to an error or non-blocking IO. When it finishes, one
263 * packet has been decoded and can be found in
Adam Langley95c29f32014-06-20 12:00:00 -0700264 * ssl->s3->rrec.type - is the type of record
Adam Langleyfcf25832014-12-18 17:42:32 -0800265 * ssl->s3->rrec.data - data
266 * ssl->s3->rrec.length - number of bytes */
Adam Langley95c29f32014-06-20 12:00:00 -0700267/* used only by ssl3_read_bytes */
Adam Langleyfcf25832014-12-18 17:42:32 -0800268static int ssl3_get_record(SSL *s) {
David Benjamin31a07792015-03-03 14:20:26 -0500269 uint8_t ssl_major, ssl_minor;
270 int al, n, i, ret = -1;
271 SSL3_RECORD *rr = &s->s3->rrec;
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 uint8_t *p;
David Benjamin31a07792015-03-03 14:20:26 -0500273 uint16_t version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 size_t extra;
275 unsigned empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700276
Adam Langley95c29f32014-06-20 12:00:00 -0700277again:
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 /* check if we have the header */
279 if (s->rstate != SSL_ST_READ_BODY ||
280 s->packet_length < SSL3_RT_HEADER_LENGTH) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400281 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800282 if (n <= 0) {
283 return n; /* error or non-blocking */
284 }
285 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700286
David Benjamin6a08da22015-05-08 22:58:12 -0400287 /* Some bytes were read, so the read buffer must be existant and
288 * |s->s3->init_extra| is defined. */
289 assert(s->s3->rbuf.buf != NULL);
290 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
291
Adam Langleyfcf25832014-12-18 17:42:32 -0800292 p = s->packet;
293 if (s->msg_callback) {
294 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 /* Pull apart the header into the SSL3_RECORD */
298 rr->type = *(p++);
299 ssl_major = *(p++);
300 ssl_minor = *(p++);
David Benjamin31a07792015-03-03 14:20:26 -0500301 version = (((uint16_t)ssl_major) << 8) | ssl_minor;
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700303
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 if (s->s3->have_version && version != s->version) {
305 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 al = SSL_AD_PROTOCOL_VERSION;
307 goto f_err;
308 }
Adam Langley95c29f32014-06-20 12:00:00 -0700309
Adam Langleyfcf25832014-12-18 17:42:32 -0800310 if ((version >> 8) != SSL3_VERSION_MAJOR) {
311 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
312 goto err;
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamin4a3f0732015-04-05 02:21:33 -0400315 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 al = SSL_AD_RECORD_OVERFLOW;
David Benjamin4a3f0732015-04-05 02:21:33 -0400317 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 goto f_err;
319 }
Adam Langley95c29f32014-06-20 12:00:00 -0700320
Adam Langleyfcf25832014-12-18 17:42:32 -0800321 /* now s->rstate == SSL_ST_READ_BODY */
David Benjamin6a08da22015-05-08 22:58:12 -0400322 } else {
323 /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The
324 * read buffer must be existant and |s->s3->init_extra| is defined. */
325 assert(s->s3->rbuf.buf != NULL);
326 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700330
Adam Langleyfcf25832014-12-18 17:42:32 -0800331 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
332 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
333 i = rr->length;
David Benjaminb1f5bca2015-05-08 22:54:02 -0400334 n = ssl3_read_n(s, i, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 if (n <= 0) {
336 /* Error or non-blocking IO. Now |n| == |rr->length|, and
337 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
338 return n;
339 }
340 }
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700343
David Benjamin31a07792015-03-03 14:20:26 -0500344 /* |rr->data| points to |rr->length| bytes of ciphertext in |s->packet|. */
345 rr->data = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700346
David Benjamin31a07792015-03-03 14:20:26 -0500347 /* Decrypt the packet in-place.
348 *
349 * TODO(davidben): This assumes |s->version| is the same as the record-layer
350 * version which isn't always true, but it only differs with the NULL cipher
351 * which ignores the parameter. */
352 size_t plaintext_len;
353 if (!SSL_AEAD_CTX_open(s->aead_read_ctx, rr->data, &plaintext_len, rr->length,
354 rr->type, s->version, s->s3->read_sequence, rr->data,
355 rr->length)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800356 al = SSL_AD_BAD_RECORD_MAC;
357 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
358 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
359 goto f_err;
360 }
David Benjamin31a07792015-03-03 14:20:26 -0500361 if (!ssl3_record_sequence_update(s->s3->read_sequence, 8)) {
362 goto err;
363 }
364 if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 al = SSL_AD_RECORD_OVERFLOW;
366 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
367 goto f_err;
368 }
David Benjamin31a07792015-03-03 14:20:26 -0500369 assert(plaintext_len <= (1u << 16));
370 rr->length = plaintext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700371
Adam Langleyfcf25832014-12-18 17:42:32 -0800372 rr->off = 0;
373 /* So at this point the following is true:
374 * ssl->s3->rrec.type is the type of record;
375 * ssl->s3->rrec.length is the number of bytes in the record;
376 * ssl->s3->rrec.off is the offset to first valid byte;
David Benjamin31a07792015-03-03 14:20:26 -0500377 * ssl->s3->rrec.data the first byte of the record body. */
Adam Langley95c29f32014-06-20 12:00:00 -0700378
Adam Langleyfcf25832014-12-18 17:42:32 -0800379 /* we have pulled in a full packet so zero things */
380 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700381
Adam Langleyfcf25832014-12-18 17:42:32 -0800382 /* just read a 0 length packet */
383 if (rr->length == 0) {
384 empty_record_count++;
385 if (empty_record_count > MAX_EMPTY_RECORDS) {
386 al = SSL_AD_UNEXPECTED_MESSAGE;
387 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
388 goto f_err;
389 }
390 goto again;
391 }
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langleyfcf25832014-12-18 17:42:32 -0800393 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700394
395f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700397err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800398 return ret;
399}
Adam Langley95c29f32014-06-20 12:00:00 -0700400
David Benjaminc933a472015-05-30 16:14:58 -0400401int ssl3_write_app_data(SSL *ssl, const void *buf, int len) {
402 return ssl3_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len);
403}
404
Adam Langleyfcf25832014-12-18 17:42:32 -0800405/* Call this to write data in records of type |type|. It will return <= 0 if
406 * not all data has been sent or non-blocking IO. */
407int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
408 const uint8_t *buf = buf_;
409 unsigned int tot, n, nw;
410 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 s->rwstate = SSL_NOTHING;
413 assert(s->s3->wnum <= INT_MAX);
414 tot = s->s3->wnum;
415 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700416
David Benjamined7c4752015-02-16 19:16:46 -0500417 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 i = s->handshake_func(s);
419 if (i < 0) {
420 return i;
421 }
422 if (i == 0) {
423 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
424 return -1;
425 }
426 }
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 /* Ensure that if we end up with a smaller value of data to write out than
429 * the the original len from a write which didn't complete for non-blocking
430 * I/O and also somehow ended up avoiding the check for this in
431 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
432 * end up with (len-tot) as a large number that will then promptly send
433 * beyond the end of the users buffer ... so we trap and report the error in
434 * a way the user will notice. */
435 if (len < 0 || (size_t)len < tot) {
436 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
437 return -1;
438 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700439
David Benjamin9faafda2015-04-04 19:23:10 -0400440 int record_split_done = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 n = (len - tot);
442 for (;;) {
443 /* max contains the maximum number of bytes that we can put into a
444 * record. */
445 unsigned max = s->max_send_fragment;
446 /* fragment is true if do_ssl3_write should send the first byte in its own
447 * record in order to randomise a CBC IV. */
448 int fragment = 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400449 if (!record_split_done && s->s3->need_record_splitting &&
450 type == SSL3_RT_APPLICATION_DATA) {
451 /* Only the the first record per write call needs to be split. The
452 * remaining plaintext was determined before the IV was randomized. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 fragment = 1;
David Benjamin9faafda2015-04-04 19:23:10 -0400454 record_split_done = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800455 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 if (n > max) {
457 nw = max;
458 } else {
459 nw = n;
460 }
Adam Langley95c29f32014-06-20 12:00:00 -0700461
David Benjamin9faafda2015-04-04 19:23:10 -0400462 i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
Adam Langleyfcf25832014-12-18 17:42:32 -0800463 if (i <= 0) {
464 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 return i;
466 }
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
469 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 return tot + i;
471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 n -= i;
474 tot += i;
475 }
476}
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjamin9faafda2015-04-04 19:23:10 -0400478/* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
479 * writes it to |out|. At most |max_out| bytes will be written. It returns one
David Benjamin31a07792015-03-03 14:20:26 -0500480 * on success and zero on error. On success, it updates the write sequence
481 * number. */
David Benjamin9faafda2015-04-04 19:23:10 -0400482static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
483 size_t max_out, uint8_t type, const uint8_t *in,
484 size_t in_len) {
485 if (max_out < SSL3_RT_HEADER_LENGTH) {
486 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
487 return 0;
488 }
489
490 out[0] = type;
491
492 /* Some servers hang if initial ClientHello is larger than 256 bytes and
493 * record version number > TLS 1.0. */
David Benjamin31a07792015-03-03 14:20:26 -0500494 uint16_t wire_version = s->version;
David Benjamin9faafda2015-04-04 19:23:10 -0400495 if (!s->s3->have_version && s->version > SSL3_VERSION) {
David Benjamin31a07792015-03-03 14:20:26 -0500496 wire_version = TLS1_VERSION;
497 }
498 out[1] = wire_version >> 8;
499 out[2] = wire_version & 0xff;
500
501 size_t ciphertext_len;
502 if (!SSL_AEAD_CTX_seal(s->aead_write_ctx, out + SSL3_RT_HEADER_LENGTH,
503 &ciphertext_len, max_out - SSL3_RT_HEADER_LENGTH,
504 type, wire_version, s->s3->write_sequence, in,
505 in_len) ||
506 !ssl3_record_sequence_update(s->s3->write_sequence, 8)) {
507 return 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400508 }
509
David Benjamin31a07792015-03-03 14:20:26 -0500510 if (ciphertext_len >= 1 << 16) {
David Benjamin9faafda2015-04-04 19:23:10 -0400511 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
512 return 0;
513 }
David Benjamin31a07792015-03-03 14:20:26 -0500514 out[3] = ciphertext_len >> 8;
515 out[4] = ciphertext_len & 0xff;
David Benjamin9faafda2015-04-04 19:23:10 -0400516
David Benjamin31a07792015-03-03 14:20:26 -0500517 *out_len = SSL3_RT_HEADER_LENGTH + ciphertext_len;
David Benjamin9faafda2015-04-04 19:23:10 -0400518
519 if (s->msg_callback) {
520 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
521 s, s->msg_callback_arg);
522 }
523
524 return 1;
525}
526
Adam Langleyd493d522014-06-20 12:00:00 -0700527/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
528 * then it splits the record into a one byte record and a record with the rest
David Benjamin9faafda2015-04-04 19:23:10 -0400529 * of the data in order to randomise a CBC IV. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800530static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400531 char fragment) {
532 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley95c29f32014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 /* first check if there is a SSL3_BUFFER still being written out. This will
535 * happen with non blocking IO */
536 if (wb->left != 0) {
537 return ssl3_write_pending(s, type, buf, len);
538 }
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 /* If we have an alert to send, lets send it */
541 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400542 int ret = s->method->ssl_dispatch_alert(s);
543 if (ret <= 0) {
544 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800545 }
546 /* if it went, fall through and send more stuff */
547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
550 return -1;
551 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700552
Adam Langleyfcf25832014-12-18 17:42:32 -0800553 if (len == 0) {
554 return 0;
555 }
David Benjamin9faafda2015-04-04 19:23:10 -0400556 if (len == 1) {
557 /* No sense in fragmenting a one-byte record. */
558 fragment = 0;
559 }
Adam Langley95c29f32014-06-20 12:00:00 -0700560
David Benjamin9faafda2015-04-04 19:23:10 -0400561 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
562 uintptr_t align;
Adam Langleyfcf25832014-12-18 17:42:32 -0800563 if (fragment) {
David Benjamin9faafda2015-04-04 19:23:10 -0400564 /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
565 * multiple of the block size which we may assume is aligned. Thus we only
566 * need to account for a second copy of the record header. */
567 align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800568 } else {
David Benjamin9faafda2015-04-04 19:23:10 -0400569 align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
570 }
David Benjaminef4962f2015-04-06 20:31:44 -0400571 align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
David Benjamin9faafda2015-04-04 19:23:10 -0400572 uint8_t *out = wb->buf + align;
573 wb->offset = align;
574 size_t max_out = wb->len - wb->offset;
575
576 const uint8_t *orig_buf = buf;
577 unsigned int orig_len = len;
578 size_t fragment_len = 0;
579 if (fragment) {
580 /* Write the first byte in its own record as a countermeasure against
581 * known-IV weaknesses in CBC ciphersuites. (See
582 * http://www.openssl.org/~bodo/tls-cbc.txt.) */
583 if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
584 return -1;
585 }
586 out += fragment_len;
587 max_out -= fragment_len;
588 buf++;
589 len--;
Adam Langleyfcf25832014-12-18 17:42:32 -0800590 }
Adam Langley95c29f32014-06-20 12:00:00 -0700591
David Benjamin9faafda2015-04-04 19:23:10 -0400592 assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
593 == 0);
594 size_t ciphertext_len;
595 if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
596 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800597 }
David Benjamin9faafda2015-04-04 19:23:10 -0400598 ciphertext_len += fragment_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 /* now let's set up wb */
David Benjamin9faafda2015-04-04 19:23:10 -0400601 wb->left = ciphertext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 /* memorize arguments so that ssl3_write_pending can detect bad write retries
604 * later */
David Benjamin9faafda2015-04-04 19:23:10 -0400605 s->s3->wpend_tot = orig_len;
606 s->s3->wpend_buf = orig_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800607 s->s3->wpend_type = type;
David Benjamin9faafda2015-04-04 19:23:10 -0400608 s->s3->wpend_ret = orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800609
610 /* we now just need to write the buffer */
David Benjamin9faafda2015-04-04 19:23:10 -0400611 return ssl3_write_pending(s, type, orig_buf, orig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800612}
Adam Langley95c29f32014-06-20 12:00:00 -0700613
614/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800615int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
616 int i;
617 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700618
Adam Langleyfcf25832014-12-18 17:42:32 -0800619 if (s->s3->wpend_tot > (int)len ||
620 (s->s3->wpend_buf != buf &&
621 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
622 s->s3->wpend_type != type) {
623 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
624 return -1;
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 for (;;) {
628 ERR_clear_system_error();
629 if (s->wbio != NULL) {
630 s->rwstate = SSL_WRITING;
631 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
632 (unsigned int)wb->left);
633 } else {
634 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
635 i = -1;
636 }
637 if (i == wb->left) {
638 wb->left = 0;
639 wb->offset += i;
David Benjamin9417b762015-05-08 22:30:06 -0400640 ssl3_release_write_buffer(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 s->rwstate = SSL_NOTHING;
642 return s->s3->wpend_ret;
643 } else if (i <= 0) {
644 if (SSL_IS_DTLS(s)) {
David Benjaminaebefed2015-05-08 22:20:04 -0400645 /* For DTLS, just drop it. That's kind of the whole point in
646 * using a datagram service */
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 wb->left = 0;
648 }
649 return i;
650 }
David Benjamin883e49f2015-04-05 12:19:14 -0400651 /* TODO(davidben): This codepath is used in DTLS, but the write
652 * payload may not split across packets. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800653 wb->offset += i;
654 wb->left -= i;
655 }
656}
Adam Langley95c29f32014-06-20 12:00:00 -0700657
David Benjamin86271ee2014-07-21 16:14:03 -0400658/* ssl3_expect_change_cipher_spec informs the record layer that a
659 * ChangeCipherSpec record is required at this point. If a Handshake record is
660 * received before ChangeCipherSpec, the connection will fail. Moreover, if
661 * there are unprocessed handshake bytes, the handshake will also fail and the
662 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800663int ssl3_expect_change_cipher_spec(SSL *s) {
664 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
665 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
666 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
667 return 0;
668 }
669
670 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
671 return 1;
672}
David Benjamin86271ee2014-07-21 16:14:03 -0400673
Adam Langley95c29f32014-06-20 12:00:00 -0700674/* Return up to 'len' payload bytes received in 'type' records.
675 * 'type' is one of the following:
676 *
677 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
678 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
679 * - 0 (during a shutdown, no data has to be returned)
680 *
681 * If we don't have stored data to work from, read a SSL/TLS record first
682 * (possibly multiple records if we still don't have anything to return).
683 *
684 * This function must handle any surprises the peer may have for us, such as
685 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
686 * a surprise, but handled as if it were), or renegotiation requests.
687 * Also if record payloads contain fragments too small to process, we store
688 * them until there is enough for the respective protocol (the record protocol
689 * may use arbitrary fragmentation and even interleaving):
690 * Change cipher spec protocol
691 * just 1 byte needed, no need for keeping anything stored
692 * Alert protocol
693 * 2 bytes needed (AlertLevel, AlertDescription)
694 * Handshake protocol
695 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
696 * to detect unexpected Client Hello and Hello Request messages
697 * here, anything else is handled by higher layers
698 * Application data protocol
699 * none of our business
700 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800701int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500702 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800703 unsigned int n;
704 SSL3_RECORD *rr;
705 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700706
Adam Langleyfcf25832014-12-18 17:42:32 -0800707 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
708 (peek && type != SSL3_RT_APPLICATION_DATA)) {
709 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
710 return -1;
711 }
Adam Langley95c29f32014-06-20 12:00:00 -0700712
Adam Langleyfcf25832014-12-18 17:42:32 -0800713 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
714 /* (partially) satisfy request from storage */
715 uint8_t *src = s->s3->handshake_fragment;
716 uint8_t *dst = buf;
717 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700718
Adam Langleyfcf25832014-12-18 17:42:32 -0800719 /* peek == 0 */
720 n = 0;
721 while (len > 0 && s->s3->handshake_fragment_len > 0) {
722 *dst++ = *src++;
723 len--;
724 s->s3->handshake_fragment_len--;
725 n++;
726 }
727 /* move any remaining fragment bytes: */
728 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
729 s->s3->handshake_fragment[k] = *src++;
730 }
731 return n;
732 }
Adam Langley95c29f32014-06-20 12:00:00 -0700733
Adam Langleyfcf25832014-12-18 17:42:32 -0800734 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700735
David Benjamin931ab342015-02-08 19:46:57 -0500736 /* This may require multiple iterations. False Start will cause
737 * |s->handshake_func| to signal success one step early, but the handshake
738 * must be completely finished before other modes are accepted.
739 *
740 * TODO(davidben): Move this check up to a higher level. */
741 while (!s->in_handshake && SSL_in_init(s)) {
742 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 i = s->handshake_func(s);
744 if (i < 0) {
745 return i;
746 }
747 if (i == 0) {
748 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
749 return -1;
750 }
751 }
752
Adam Langley95c29f32014-06-20 12:00:00 -0700753start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 /* s->s3->rrec.type - is the type of record
757 * s->s3->rrec.data - data
758 * s->s3->rrec.off - offset into 'data' for next read
759 * s->s3->rrec.length - number of bytes. */
760 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 /* get new packet if necessary */
763 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
764 ret = ssl3_get_record(s);
765 if (ret <= 0) {
766 return ret;
767 }
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700771
David Benjamindc3da932015-03-12 15:09:02 -0400772 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
773 * ssl3_get_finished. */
774 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
775 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800776 al = SSL_AD_UNEXPECTED_MESSAGE;
777 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
778 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
779 goto f_err;
780 }
Adam Langley95c29f32014-06-20 12:00:00 -0700781
Adam Langleyfcf25832014-12-18 17:42:32 -0800782 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
783 * Handshake record. */
784 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
785 al = SSL_AD_UNEXPECTED_MESSAGE;
786 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
787 goto f_err;
788 }
David Benjamin86271ee2014-07-21 16:14:03 -0400789
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 /* If the other end has shut down, throw anything we read away (even in
791 * 'peek' mode) */
792 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
793 rr->length = 0;
794 s->rwstate = SSL_NOTHING;
795 return 0;
796 }
Adam Langley95c29f32014-06-20 12:00:00 -0700797
Adam Langleyfcf25832014-12-18 17:42:32 -0800798 if (type == rr->type) {
799 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
800 /* make sure that we are not getting application data when we are doing a
801 * handshake for the first time */
802 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500803 s->aead_read_ctx == NULL) {
804 /* TODO(davidben): Is this check redundant with the handshake_func
805 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 al = SSL_AD_UNEXPECTED_MESSAGE;
807 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
808 goto f_err;
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 if (len <= 0) {
812 return len;
813 }
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 if ((unsigned int)len > rr->length) {
816 n = rr->length;
817 } else {
818 n = (unsigned int)len;
819 }
Adam Langley95c29f32014-06-20 12:00:00 -0700820
Adam Langleyfcf25832014-12-18 17:42:32 -0800821 memcpy(buf, &(rr->data[rr->off]), n);
822 if (!peek) {
823 rr->length -= n;
824 rr->off += n;
825 if (rr->length == 0) {
826 s->rstate = SSL_ST_READ_HEADER;
827 rr->off = 0;
David Benjaminaebefed2015-05-08 22:20:04 -0400828 if (s->s3->rbuf.left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800829 ssl3_release_read_buffer(s);
830 }
831 }
832 }
833
834 return n;
835 }
Adam Langley95c29f32014-06-20 12:00:00 -0700836
David Benjamin44d3eed2015-05-21 01:29:55 -0400837 /* Process unexpected records. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400840 /* If peer renegotiations are disabled, all out-of-order handshake records
David Benjamin44d3eed2015-05-21 01:29:55 -0400841 * are fatal. Renegotiations as a server are never supported. */
842 if (!s->accept_peer_renegotiations || s->server) {
David Benjaminb16346b2015-04-08 19:16:58 -0400843 al = SSL_AD_NO_RENEGOTIATION;
844 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
845 goto f_err;
846 }
847
David Benjamin44d3eed2015-05-21 01:29:55 -0400848 /* HelloRequests may be fragmented across multiple records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800849 const size_t size = sizeof(s->s3->handshake_fragment);
850 const size_t avail = size - s->s3->handshake_fragment_len;
851 const size_t todo = (rr->length < avail) ? rr->length : avail;
852 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
853 &rr->data[rr->off], todo);
854 rr->off += todo;
855 rr->length -= todo;
856 s->s3->handshake_fragment_len += todo;
857 if (s->s3->handshake_fragment_len < size) {
858 goto start; /* fragment was too small */
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
David Benjamin44d3eed2015-05-21 01:29:55 -0400861 /* Parse out and consume a HelloRequest. */
862 if (s->s3->handshake_fragment[0] != SSL3_MT_HELLO_REQUEST ||
863 s->s3->handshake_fragment[1] != 0 ||
Adam Langleyfcf25832014-12-18 17:42:32 -0800864 s->s3->handshake_fragment[2] != 0 ||
865 s->s3->handshake_fragment[3] != 0) {
866 al = SSL_AD_DECODE_ERROR;
867 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
868 goto f_err;
869 }
David Benjamin44d3eed2015-05-21 01:29:55 -0400870 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700871
Adam Langleyfcf25832014-12-18 17:42:32 -0800872 if (s->msg_callback) {
873 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
874 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
875 }
Adam Langley95c29f32014-06-20 12:00:00 -0700876
David Benjamincd90f3a2015-05-21 01:37:55 -0400877 if (!SSL_is_init_finished(s) || !s->s3->initial_handshake_complete) {
878 /* This cannot happen. If a handshake is in progress, |type| must be
879 * |SSL3_RT_HANDSHAKE|. */
880 assert(0);
881 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
882 goto err;
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 }
David Benjamincd90f3a2015-05-21 01:37:55 -0400884
885 /* Renegotiation is only supported at quiescent points in the application
886 * protocol, namely in HTTPS, just before reading the HTTP response. Require
887 * the record-layer be idle and avoid complexities of sending a handshake
888 * record while an application_data record is being written. */
889 if (s->s3->wbuf.left != 0 || s->s3->rbuf.left != 0) {
890 al = SSL_AD_NO_RENEGOTIATION;
891 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
892 goto f_err;
893 }
894
895 /* Begin a new handshake. */
896 s->state = SSL_ST_CONNECT;
897 i = s->handshake_func(s);
898 if (i < 0) {
899 return i;
900 }
901 if (i == 0) {
902 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
903 return -1;
904 }
905
906 /* The handshake completed synchronously. Continue reading records. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 goto start;
908 }
David Benjaminb4188f02014-11-01 03:43:48 -0400909
David Benjamin86058a22015-02-22 13:07:21 -0500910 /* If an alert record, process one alert out of the record. Note that we allow
911 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800912 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500913 /* Alerts may not be fragmented. */
914 if (rr->length < 2) {
915 al = SSL_AD_DECODE_ERROR;
916 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
917 goto f_err;
918 }
Adam Langley95c29f32014-06-20 12:00:00 -0700919
Adam Langleyfcf25832014-12-18 17:42:32 -0800920 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500921 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 s->msg_callback_arg);
923 }
David Benjamin86058a22015-02-22 13:07:21 -0500924 const uint8_t alert_level = rr->data[rr->off++];
925 const uint8_t alert_descr = rr->data[rr->off++];
926 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 if (s->info_callback != NULL) {
929 cb = s->info_callback;
930 } else if (s->ctx->info_callback != NULL) {
931 cb = s->ctx->info_callback;
932 }
Adam Langley95c29f32014-06-20 12:00:00 -0700933
Adam Langleyfcf25832014-12-18 17:42:32 -0800934 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500935 uint16_t alert = (alert_level << 8) | alert_descr;
936 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800937 }
Adam Langley95c29f32014-06-20 12:00:00 -0700938
David Benjamin86058a22015-02-22 13:07:21 -0500939 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800940 s->s3->warn_alert = alert_descr;
941 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
942 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
943 return 0;
944 }
Adam Langley95c29f32014-06-20 12:00:00 -0700945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 /* This is a warning but we receive it if we requested renegotiation and
947 * the peer denied it. Terminate with a fatal alert because if
948 * application tried to renegotiatie it presumably had a good reason and
949 * expects it to succeed.
950 *
951 * In future we might have a renegotiation where we don't care if the
952 * peer refused it where we carry on. */
953 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
954 al = SSL_AD_HANDSHAKE_FAILURE;
955 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
956 goto f_err;
957 }
David Benjamin86058a22015-02-22 13:07:21 -0500958 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800959 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 s->rwstate = SSL_NOTHING;
962 s->s3->fatal_alert = alert_descr;
963 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
964 SSL_AD_REASON_OFFSET + alert_descr);
965 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
966 ERR_add_error_data(2, "SSL alert number ", tmp);
967 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
968 SSL_CTX_remove_session(s->ctx, s->session);
969 return 0;
970 } else {
971 al = SSL_AD_ILLEGAL_PARAMETER;
972 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
973 goto f_err;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 goto start;
977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
Adam Langleyfcf25832014-12-18 17:42:32 -0800979 if (s->shutdown & SSL_SENT_SHUTDOWN) {
980 /* but we have not received a shutdown */
981 s->rwstate = SSL_NOTHING;
982 rr->length = 0;
983 return 0;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
987 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
988 * record payload has to look like */
989 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
990 al = SSL_AD_ILLEGAL_PARAMETER;
991 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
992 goto f_err;
993 }
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 /* Check we have a cipher to change to */
996 if (s->s3->tmp.new_cipher == NULL) {
997 al = SSL_AD_UNEXPECTED_MESSAGE;
998 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
999 goto f_err;
1000 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001001
Adam Langleyfcf25832014-12-18 17:42:32 -08001002 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1003 al = SSL_AD_UNEXPECTED_MESSAGE;
1004 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1005 goto f_err;
1006 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001011
Adam Langleyfcf25832014-12-18 17:42:32 -08001012 if (s->msg_callback) {
1013 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1014 s->msg_callback_arg);
1015 }
Adam Langley95c29f32014-06-20 12:00:00 -07001016
Adam Langleyfcf25832014-12-18 17:42:32 -08001017 s->s3->change_cipher_spec = 1;
1018 if (!ssl3_do_change_cipher_spec(s)) {
1019 goto err;
1020 } else {
1021 goto start;
1022 }
1023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
David Benjamine820df92015-02-08 16:06:54 -05001025 /* We already handled these. */
1026 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1027 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001028
David Benjamine820df92015-02-08 16:06:54 -05001029 al = SSL_AD_UNEXPECTED_MESSAGE;
1030 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001031
1032f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001034err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 return -1;
1036}
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langleyfcf25832014-12-18 17:42:32 -08001038int ssl3_do_change_cipher_spec(SSL *s) {
1039 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (s->state & SSL_ST_ACCEPT) {
1042 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1043 } else {
1044 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 if (s->s3->tmp.key_block == NULL) {
1048 if (s->session == NULL || s->session->master_key_length == 0) {
1049 /* might happen if dtls1_read_bytes() calls this */
1050 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1051 SSL_R_CCS_RECEIVED_EARLY);
1052 return 0;
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 s->session->cipher = s->s3->tmp.new_cipher;
1056 if (!s->enc_method->setup_key_block(s)) {
1057 return 0;
1058 }
1059 }
Adam Langley95c29f32014-06-20 12:00:00 -07001060
Adam Langleyfcf25832014-12-18 17:42:32 -08001061 if (!s->enc_method->change_cipher_state(s, i)) {
1062 return 0;
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 return 1;
1066}
Adam Langley95c29f32014-06-20 12:00:00 -07001067
Adam Langleyfcf25832014-12-18 17:42:32 -08001068int ssl3_send_alert(SSL *s, int level, int desc) {
1069 /* Map tls/ssl alert value to correct one */
1070 desc = s->enc_method->alert_value(desc);
1071 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1072 /* SSL 3.0 does not have protocol_version alerts */
1073 desc = SSL_AD_HANDSHAKE_FAILURE;
1074 }
1075 if (desc < 0) {
1076 return -1;
1077 }
Adam Langley95c29f32014-06-20 12:00:00 -07001078
Adam Langleyfcf25832014-12-18 17:42:32 -08001079 /* If a fatal one, remove from cache */
1080 if (level == 2 && s->session != NULL) {
1081 SSL_CTX_remove_session(s->ctx, s->session);
1082 }
Adam Langley95c29f32014-06-20 12:00:00 -07001083
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 s->s3->alert_dispatch = 1;
1085 s->s3->send_alert[0] = level;
1086 s->s3->send_alert[1] = desc;
1087 if (s->s3->wbuf.left == 0) {
1088 /* data is still being written out. */
1089 return s->method->ssl_dispatch_alert(s);
1090 }
Adam Langley95c29f32014-06-20 12:00:00 -07001091
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 /* else data is still being written out, we will get written some time in the
1093 * future */
1094 return -1;
1095}
Adam Langley95c29f32014-06-20 12:00:00 -07001096
Adam Langleyfcf25832014-12-18 17:42:32 -08001097int ssl3_dispatch_alert(SSL *s) {
1098 int i, j;
1099 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 s->s3->alert_dispatch = 0;
David Benjamin9faafda2015-04-04 19:23:10 -04001102 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 if (i <= 0) {
1104 s->s3->alert_dispatch = 1;
1105 } else {
1106 /* Alert sent to BIO. If it is important, flush it now. If the message
1107 * does not get sent due to non-blocking IO, we will not worry too much. */
1108 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1109 BIO_flush(s->wbio);
1110 }
Adam Langley95c29f32014-06-20 12:00:00 -07001111
Adam Langleyfcf25832014-12-18 17:42:32 -08001112 if (s->msg_callback) {
1113 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1114 s->msg_callback_arg);
1115 }
1116
1117 if (s->info_callback != NULL) {
1118 cb = s->info_callback;
1119 } else if (s->ctx->info_callback != NULL) {
1120 cb = s->ctx->info_callback;
1121 }
1122
1123 if (cb != NULL) {
1124 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1125 cb(s, SSL_CB_WRITE_ALERT, j);
1126 }
1127 }
1128
1129 return i;
1130}