blob: c42d0009e7fd8a83ea870529b0be76f679b8093f [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 Benjaminb1f5bca2015-05-08 22:54:02 -0400132 * |s->packet| and |s->packet_length|. (If |s->read_ahead| is set and |extend|
133 * is 0, additional bytes may be read into |rbuf|, up to the size of the
134 * buffer.) */
Adam Langleyfcf25832014-12-18 17:42:32 -0800135 int i, len, left;
David Benjaminef4962f2015-04-06 20:31:44 -0400136 uintptr_t align = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800137 uint8_t *pkt;
138 SSL3_BUFFER *rb;
Adam Langley95c29f32014-06-20 12:00:00 -0700139
Adam Langleyfcf25832014-12-18 17:42:32 -0800140 if (n <= 0) {
141 return n;
142 }
Adam Langley95c29f32014-06-20 12:00:00 -0700143
Adam Langleyfcf25832014-12-18 17:42:32 -0800144 rb = &s->s3->rbuf;
145 if (rb->buf == NULL && !ssl3_setup_read_buffer(s)) {
146 return -1;
147 }
Adam Langley95c29f32014-06-20 12:00:00 -0700148
Adam Langleyfcf25832014-12-18 17:42:32 -0800149 left = rb->left;
Adam Langley95c29f32014-06-20 12:00:00 -0700150
David Benjaminef4962f2015-04-06 20:31:44 -0400151 align = (uintptr_t)rb->buf + SSL3_RT_HEADER_LENGTH;
152 align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
Adam Langley95c29f32014-06-20 12:00:00 -0700153
Adam Langleyfcf25832014-12-18 17:42:32 -0800154 if (!extend) {
155 /* start with empty packet ... */
156 if (left == 0) {
157 rb->offset = align;
158 } else if (align != 0 && left >= SSL3_RT_HEADER_LENGTH) {
159 /* check if next packet length is large enough to justify payload
160 * alignment... */
161 pkt = rb->buf + rb->offset;
162 if (pkt[0] == SSL3_RT_APPLICATION_DATA && (pkt[3] << 8 | pkt[4]) >= 128) {
163 /* Note that even if packet is corrupted and its length field is
164 * insane, we can only be led to wrong decision about whether memmove
165 * will occur or not. Header values has no effect on memmove arguments
166 * and therefore no buffer overrun can be triggered. */
167 memmove(rb->buf + align, pkt, left);
168 rb->offset = align;
169 }
170 }
171 s->packet = rb->buf + rb->offset;
172 s->packet_length = 0;
173 /* ... now we can act as if 'extend' was set */
174 }
Adam Langley95c29f32014-06-20 12:00:00 -0700175
Adam Langleyfcf25832014-12-18 17:42:32 -0800176 /* For DTLS/UDP reads should not span multiple packets because the read
177 * operation returns the whole packet at once (as long as it fits into the
178 * buffer). */
179 if (SSL_IS_DTLS(s) && left > 0 && n > left) {
180 n = left;
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
Adam Langleyfcf25832014-12-18 17:42:32 -0800183 /* if there is enough in the buffer from a previous read, take some */
184 if (left >= n) {
185 s->packet_length += n;
186 rb->left = left - n;
187 rb->offset += n;
188 return n;
189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
Adam Langleyfcf25832014-12-18 17:42:32 -0800191 /* else we need to read more data */
Adam Langley95c29f32014-06-20 12:00:00 -0700192
Adam Langleyfcf25832014-12-18 17:42:32 -0800193 len = s->packet_length;
194 pkt = rb->buf + align;
195 /* Move any available bytes to front of buffer: |len| bytes already pointed
196 * to by |packet|, |left| extra ones at the end. */
197 if (s->packet != pkt) {
198 /* len > 0 */
199 memmove(pkt, s->packet, len + left);
200 s->packet = pkt;
201 rb->offset = len + align;
202 }
Adam Langley95c29f32014-06-20 12:00:00 -0700203
Adam Langleyc4482d62015-03-09 17:49:31 -0700204 if (n > (int)(rb->len - rb->offset)) {
205 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, ERR_R_INTERNAL_ERROR);
206 return -1;
207 }
Adam Langley95c29f32014-06-20 12:00:00 -0700208
David Benjaminb1f5bca2015-05-08 22:54:02 -0400209 int max = n;
210 if (s->read_ahead && !extend) {
211 max = rb->len - rb->offset;
Adam Langleyfcf25832014-12-18 17:42:32 -0800212 }
Adam Langley95c29f32014-06-20 12:00:00 -0700213
Adam Langleyfcf25832014-12-18 17:42:32 -0800214 while (left < n) {
215 /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
216 * read in more until we have len+n (up to len+max if possible). */
217 ERR_clear_system_error();
218 if (s->rbio != NULL) {
219 s->rwstate = SSL_READING;
220 i = BIO_read(s->rbio, pkt + len + left, max - left);
221 } else {
222 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
223 i = -1;
224 }
Adam Langley95c29f32014-06-20 12:00:00 -0700225
Adam Langleyfcf25832014-12-18 17:42:32 -0800226 if (i <= 0) {
227 rb->left = left;
David Benjamin9417b762015-05-08 22:30:06 -0400228 if (len + left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800229 ssl3_release_read_buffer(s);
230 }
231 return i;
232 }
233 left += i;
234 /* reads should *never* span multiple packets for DTLS because the
235 * underlying transport protocol is message oriented as opposed to byte
236 * oriented as in the TLS case. */
237 if (SSL_IS_DTLS(s) && n > left) {
238 n = left; /* makes the while condition false */
239 }
240 }
Adam Langley95c29f32014-06-20 12:00:00 -0700241
Adam Langleyfcf25832014-12-18 17:42:32 -0800242 /* done reading, now the book-keeping */
243 rb->offset += n;
244 rb->left = left - n;
245 s->packet_length += n;
246 s->rwstate = SSL_NOTHING;
247
248 return n;
249}
Adam Langley95c29f32014-06-20 12:00:00 -0700250
Adam Langley48105fa2014-06-20 12:00:00 -0700251/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
252 * be processed per call to ssl3_get_record. Without this limit an attacker
253 * could send empty records at a faster rate than we can process and cause
254 * ssl3_get_record to loop forever. */
255#define MAX_EMPTY_RECORDS 32
256
Adam Langleyfcf25832014-12-18 17:42:32 -0800257/* Call this to get a new input record. It will return <= 0 if more data is
258 * needed, normally due to an error or non-blocking IO. When it finishes, one
259 * packet has been decoded and can be found in
Adam Langley95c29f32014-06-20 12:00:00 -0700260 * ssl->s3->rrec.type - is the type of record
Adam Langleyfcf25832014-12-18 17:42:32 -0800261 * ssl->s3->rrec.data - data
262 * ssl->s3->rrec.length - number of bytes */
Adam Langley95c29f32014-06-20 12:00:00 -0700263/* used only by ssl3_read_bytes */
Adam Langleyfcf25832014-12-18 17:42:32 -0800264static int ssl3_get_record(SSL *s) {
265 int ssl_major, ssl_minor, al;
David Benjamin1e52eca2015-01-22 15:33:51 -0500266 int n, i, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800267 SSL3_RECORD *rr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800268 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 short version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800270 size_t extra;
271 unsigned empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700274
Adam Langley95c29f32014-06-20 12:00:00 -0700275again:
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 /* check if we have the header */
277 if (s->rstate != SSL_ST_READ_BODY ||
278 s->packet_length < SSL3_RT_HEADER_LENGTH) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400279 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 if (n <= 0) {
281 return n; /* error or non-blocking */
282 }
283 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700284
David Benjamin6a08da22015-05-08 22:58:12 -0400285 /* Some bytes were read, so the read buffer must be existant and
286 * |s->s3->init_extra| is defined. */
287 assert(s->s3->rbuf.buf != NULL);
288 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
289
Adam Langleyfcf25832014-12-18 17:42:32 -0800290 p = s->packet;
291 if (s->msg_callback) {
292 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
293 }
Adam Langley95c29f32014-06-20 12:00:00 -0700294
Adam Langleyfcf25832014-12-18 17:42:32 -0800295 /* Pull apart the header into the SSL3_RECORD */
296 rr->type = *(p++);
297 ssl_major = *(p++);
298 ssl_minor = *(p++);
299 version = (ssl_major << 8) | ssl_minor;
300 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700301
Adam Langleyfcf25832014-12-18 17:42:32 -0800302 if (s->s3->have_version && version != s->version) {
303 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
Adam Langleyfcf25832014-12-18 17:42:32 -0800304 al = SSL_AD_PROTOCOL_VERSION;
305 goto f_err;
306 }
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 if ((version >> 8) != SSL3_VERSION_MAJOR) {
309 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
310 goto err;
311 }
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjamin4a3f0732015-04-05 02:21:33 -0400313 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800314 al = SSL_AD_RECORD_OVERFLOW;
David Benjamin4a3f0732015-04-05 02:21:33 -0400315 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800316 goto f_err;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langleyfcf25832014-12-18 17:42:32 -0800319 /* now s->rstate == SSL_ST_READ_BODY */
David Benjamin6a08da22015-05-08 22:58:12 -0400320 } else {
321 /* |packet_length| is non-zero and |s->rstate| is |SSL_ST_READ_BODY|. The
322 * read buffer must be existant and |s->s3->init_extra| is defined. */
323 assert(s->s3->rbuf.buf != NULL);
324 extra = s->s3->init_extra ? SSL3_RT_MAX_EXTRA : 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 }
Adam Langley95c29f32014-06-20 12:00:00 -0700326
Adam Langleyfcf25832014-12-18 17:42:32 -0800327 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langleyfcf25832014-12-18 17:42:32 -0800329 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
330 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
331 i = rr->length;
David Benjaminb1f5bca2015-05-08 22:54:02 -0400332 n = ssl3_read_n(s, i, 1);
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (n <= 0) {
334 /* Error or non-blocking IO. Now |n| == |rr->length|, and
335 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
336 return n;
337 }
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and
343 * we have that many bytes in s->packet. */
344 rr->input = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700345
Adam Langleyfcf25832014-12-18 17:42:32 -0800346 /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at
347 * |rr->length| bytes, which need to be copied into |rr->data| by decryption.
348 * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be
349 * pointed at the new buffer. */
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
352 * rr->length bytes of encrypted compressed stuff. */
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 /* decrypt in place in 'rr->input' */
355 rr->data = rr->input;
Adam Langley95c29f32014-06-20 12:00:00 -0700356
David Benjamin1e52eca2015-01-22 15:33:51 -0500357 if (!s->enc_method->enc(s, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 al = SSL_AD_BAD_RECORD_MAC;
359 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
360 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
361 goto f_err;
362 }
Adam Langley95c29f32014-06-20 12:00:00 -0700363
Adam Langleyfcf25832014-12-18 17:42:32 -0800364 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
365 al = SSL_AD_RECORD_OVERFLOW;
366 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
367 goto f_err;
368 }
Adam Langley95c29f32014-06-20 12:00:00 -0700369
Adam Langleyfcf25832014-12-18 17:42:32 -0800370 rr->off = 0;
371 /* So at this point the following is true:
372 * ssl->s3->rrec.type is the type of record;
373 * ssl->s3->rrec.length is the number of bytes in the record;
374 * ssl->s3->rrec.off is the offset to first valid byte;
375 * ssl->s3->rrec.data is where to take bytes from (increment after use). */
Adam Langley95c29f32014-06-20 12:00:00 -0700376
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 /* we have pulled in a full packet so zero things */
378 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700379
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 /* just read a 0 length packet */
381 if (rr->length == 0) {
382 empty_record_count++;
383 if (empty_record_count > MAX_EMPTY_RECORDS) {
384 al = SSL_AD_UNEXPECTED_MESSAGE;
385 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
386 goto f_err;
387 }
388 goto again;
389 }
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700392
393f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800394 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700395err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 return ret;
397}
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399/* Call this to write data in records of type |type|. It will return <= 0 if
400 * not all data has been sent or non-blocking IO. */
401int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
402 const uint8_t *buf = buf_;
403 unsigned int tot, n, nw;
404 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700405
Adam Langleyfcf25832014-12-18 17:42:32 -0800406 s->rwstate = SSL_NOTHING;
407 assert(s->s3->wnum <= INT_MAX);
408 tot = s->s3->wnum;
409 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700410
David Benjamined7c4752015-02-16 19:16:46 -0500411 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800412 i = s->handshake_func(s);
413 if (i < 0) {
414 return i;
415 }
416 if (i == 0) {
417 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
418 return -1;
419 }
420 }
Adam Langley95c29f32014-06-20 12:00:00 -0700421
Adam Langleyfcf25832014-12-18 17:42:32 -0800422 /* Ensure that if we end up with a smaller value of data to write out than
423 * the the original len from a write which didn't complete for non-blocking
424 * I/O and also somehow ended up avoiding the check for this in
425 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
426 * end up with (len-tot) as a large number that will then promptly send
427 * beyond the end of the users buffer ... so we trap and report the error in
428 * a way the user will notice. */
429 if (len < 0 || (size_t)len < tot) {
430 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
431 return -1;
432 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700433
David Benjamin9faafda2015-04-04 19:23:10 -0400434 int record_split_done = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800435 n = (len - tot);
436 for (;;) {
437 /* max contains the maximum number of bytes that we can put into a
438 * record. */
439 unsigned max = s->max_send_fragment;
440 /* fragment is true if do_ssl3_write should send the first byte in its own
441 * record in order to randomise a CBC IV. */
442 int fragment = 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400443 if (!record_split_done && s->s3->need_record_splitting &&
444 type == SSL3_RT_APPLICATION_DATA) {
445 /* Only the the first record per write call needs to be split. The
446 * remaining plaintext was determined before the IV was randomized. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800447 fragment = 1;
David Benjamin9faafda2015-04-04 19:23:10 -0400448 record_split_done = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800449 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800450 if (n > max) {
451 nw = max;
452 } else {
453 nw = n;
454 }
Adam Langley95c29f32014-06-20 12:00:00 -0700455
David Benjamin9faafda2015-04-04 19:23:10 -0400456 i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 if (i <= 0) {
458 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800459 return i;
460 }
Adam Langley95c29f32014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
463 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800464 return tot + i;
465 }
Adam Langley95c29f32014-06-20 12:00:00 -0700466
Adam Langleyfcf25832014-12-18 17:42:32 -0800467 n -= i;
468 tot += i;
469 }
470}
Adam Langley95c29f32014-06-20 12:00:00 -0700471
David Benjamin9faafda2015-04-04 19:23:10 -0400472/* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
473 * writes it to |out|. At most |max_out| bytes will be written. It returns one
474 * on success and zero on error. On success, |s->s3->wrec| is updated to include
475 * the new record. */
476static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
477 size_t max_out, uint8_t type, const uint8_t *in,
478 size_t in_len) {
479 if (max_out < SSL3_RT_HEADER_LENGTH) {
480 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
481 return 0;
482 }
483
484 out[0] = type;
485
486 /* Some servers hang if initial ClientHello is larger than 256 bytes and
487 * record version number > TLS 1.0. */
488 if (!s->s3->have_version && s->version > SSL3_VERSION) {
489 out[1] = TLS1_VERSION >> 8;
490 out[2] = TLS1_VERSION & 0xff;
491 } else {
492 out[1] = s->version >> 8;
493 out[2] = s->version & 0xff;
494 }
495
496 size_t explicit_nonce_len = 0;
497 if (s->aead_write_ctx != NULL &&
498 s->aead_write_ctx->variable_nonce_included_in_record) {
499 explicit_nonce_len = s->aead_write_ctx->variable_nonce_len;
500 }
501 size_t max_overhead = 0;
502 if (s->aead_write_ctx != NULL) {
503 max_overhead = s->aead_write_ctx->tag_len;
504 }
505
506 /* Assemble the input for |s->enc_method->enc|. The input is the plaintext
507 * with |explicit_nonce_len| bytes of space prepended for the explicit
508 * nonce. The input is copied into |out| and then encrypted in-place to take
509 * advantage of alignment.
510 *
511 * TODO(davidben): |tls1_enc| should accept its inputs and outputs directly
512 * rather than looking up in |wrec| and friends. The |max_overhead| bounds
513 * check would also be unnecessary if |max_out| were passed down. */
514 SSL3_RECORD *wr = &s->s3->wrec;
515 size_t plaintext_len = in_len + explicit_nonce_len;
516 if (plaintext_len < in_len || plaintext_len > INT_MAX ||
517 plaintext_len + max_overhead < plaintext_len) {
518 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
519 return 0;
520 }
521 if (max_out - SSL3_RT_HEADER_LENGTH < plaintext_len + max_overhead) {
522 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
523 return 0;
524 }
525 wr->type = type;
526 wr->input = out + SSL3_RT_HEADER_LENGTH;
527 wr->data = wr->input;
528 wr->length = plaintext_len;
529 memcpy(wr->input + explicit_nonce_len, in, in_len);
530
531 if (!s->enc_method->enc(s, 1)) {
532 return 0;
533 }
534
535 /* |wr->length| has now been set to the ciphertext length. */
536 if (wr->length >= 1 << 16) {
537 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
538 return 0;
539 }
540 out[3] = wr->length >> 8;
541 out[4] = wr->length & 0xff;
542 *out_len = SSL3_RT_HEADER_LENGTH + (size_t)wr->length;
543
544 if (s->msg_callback) {
545 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
546 s, s->msg_callback_arg);
547 }
548
549 return 1;
550}
551
Adam Langleyd493d522014-06-20 12:00:00 -0700552/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
553 * then it splits the record into a one byte record and a record with the rest
David Benjamin9faafda2015-04-04 19:23:10 -0400554 * of the data in order to randomise a CBC IV. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800555static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400556 char fragment) {
557 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley95c29f32014-06-20 12:00:00 -0700558
Adam Langleyfcf25832014-12-18 17:42:32 -0800559 /* first check if there is a SSL3_BUFFER still being written out. This will
560 * happen with non blocking IO */
561 if (wb->left != 0) {
562 return ssl3_write_pending(s, type, buf, len);
563 }
Adam Langley95c29f32014-06-20 12:00:00 -0700564
Adam Langleyfcf25832014-12-18 17:42:32 -0800565 /* If we have an alert to send, lets send it */
566 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400567 int ret = s->method->ssl_dispatch_alert(s);
568 if (ret <= 0) {
569 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 }
571 /* if it went, fall through and send more stuff */
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
575 return -1;
576 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700577
Adam Langleyfcf25832014-12-18 17:42:32 -0800578 if (len == 0) {
579 return 0;
580 }
David Benjamin9faafda2015-04-04 19:23:10 -0400581 if (len == 1) {
582 /* No sense in fragmenting a one-byte record. */
583 fragment = 0;
584 }
Adam Langley95c29f32014-06-20 12:00:00 -0700585
David Benjamin9faafda2015-04-04 19:23:10 -0400586 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
587 uintptr_t align;
Adam Langleyfcf25832014-12-18 17:42:32 -0800588 if (fragment) {
David Benjamin9faafda2015-04-04 19:23:10 -0400589 /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
590 * multiple of the block size which we may assume is aligned. Thus we only
591 * need to account for a second copy of the record header. */
592 align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 } else {
David Benjamin9faafda2015-04-04 19:23:10 -0400594 align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
595 }
David Benjaminef4962f2015-04-06 20:31:44 -0400596 align = (0 - align) & (SSL3_ALIGN_PAYLOAD - 1);
David Benjamin9faafda2015-04-04 19:23:10 -0400597 uint8_t *out = wb->buf + align;
598 wb->offset = align;
599 size_t max_out = wb->len - wb->offset;
600
601 const uint8_t *orig_buf = buf;
602 unsigned int orig_len = len;
603 size_t fragment_len = 0;
604 if (fragment) {
605 /* Write the first byte in its own record as a countermeasure against
606 * known-IV weaknesses in CBC ciphersuites. (See
607 * http://www.openssl.org/~bodo/tls-cbc.txt.) */
608 if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
609 return -1;
610 }
611 out += fragment_len;
612 max_out -= fragment_len;
613 buf++;
614 len--;
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 }
Adam Langley95c29f32014-06-20 12:00:00 -0700616
David Benjamin9faafda2015-04-04 19:23:10 -0400617 assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
618 == 0);
619 size_t ciphertext_len;
620 if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
621 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800622 }
David Benjamin9faafda2015-04-04 19:23:10 -0400623 ciphertext_len += fragment_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700624
Adam Langleyfcf25832014-12-18 17:42:32 -0800625 /* now let's set up wb */
David Benjamin9faafda2015-04-04 19:23:10 -0400626 wb->left = ciphertext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700627
Adam Langleyfcf25832014-12-18 17:42:32 -0800628 /* memorize arguments so that ssl3_write_pending can detect bad write retries
629 * later */
David Benjamin9faafda2015-04-04 19:23:10 -0400630 s->s3->wpend_tot = orig_len;
631 s->s3->wpend_buf = orig_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 s->s3->wpend_type = type;
David Benjamin9faafda2015-04-04 19:23:10 -0400633 s->s3->wpend_ret = orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800634
635 /* we now just need to write the buffer */
David Benjamin9faafda2015-04-04 19:23:10 -0400636 return ssl3_write_pending(s, type, orig_buf, orig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800637}
Adam Langley95c29f32014-06-20 12:00:00 -0700638
639/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800640int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
641 int i;
642 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700643
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 if (s->s3->wpend_tot > (int)len ||
645 (s->s3->wpend_buf != buf &&
646 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
647 s->s3->wpend_type != type) {
648 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
649 return -1;
650 }
Adam Langley95c29f32014-06-20 12:00:00 -0700651
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 for (;;) {
653 ERR_clear_system_error();
654 if (s->wbio != NULL) {
655 s->rwstate = SSL_WRITING;
656 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
657 (unsigned int)wb->left);
658 } else {
659 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
660 i = -1;
661 }
662 if (i == wb->left) {
663 wb->left = 0;
664 wb->offset += i;
David Benjamin9417b762015-05-08 22:30:06 -0400665 ssl3_release_write_buffer(s);
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 s->rwstate = SSL_NOTHING;
667 return s->s3->wpend_ret;
668 } else if (i <= 0) {
669 if (SSL_IS_DTLS(s)) {
David Benjaminaebefed2015-05-08 22:20:04 -0400670 /* For DTLS, just drop it. That's kind of the whole point in
671 * using a datagram service */
Adam Langleyfcf25832014-12-18 17:42:32 -0800672 wb->left = 0;
673 }
674 return i;
675 }
David Benjamin883e49f2015-04-05 12:19:14 -0400676 /* TODO(davidben): This codepath is used in DTLS, but the write
677 * payload may not split across packets. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800678 wb->offset += i;
679 wb->left -= i;
680 }
681}
Adam Langley95c29f32014-06-20 12:00:00 -0700682
David Benjamin86271ee2014-07-21 16:14:03 -0400683/* ssl3_expect_change_cipher_spec informs the record layer that a
684 * ChangeCipherSpec record is required at this point. If a Handshake record is
685 * received before ChangeCipherSpec, the connection will fail. Moreover, if
686 * there are unprocessed handshake bytes, the handshake will also fail and the
687 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800688int ssl3_expect_change_cipher_spec(SSL *s) {
689 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
690 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
691 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
692 return 0;
693 }
694
695 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
696 return 1;
697}
David Benjamin86271ee2014-07-21 16:14:03 -0400698
Adam Langley95c29f32014-06-20 12:00:00 -0700699/* Return up to 'len' payload bytes received in 'type' records.
700 * 'type' is one of the following:
701 *
702 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
703 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
704 * - 0 (during a shutdown, no data has to be returned)
705 *
706 * If we don't have stored data to work from, read a SSL/TLS record first
707 * (possibly multiple records if we still don't have anything to return).
708 *
709 * This function must handle any surprises the peer may have for us, such as
710 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
711 * a surprise, but handled as if it were), or renegotiation requests.
712 * Also if record payloads contain fragments too small to process, we store
713 * them until there is enough for the respective protocol (the record protocol
714 * may use arbitrary fragmentation and even interleaving):
715 * Change cipher spec protocol
716 * just 1 byte needed, no need for keeping anything stored
717 * Alert protocol
718 * 2 bytes needed (AlertLevel, AlertDescription)
719 * Handshake protocol
720 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
721 * to detect unexpected Client Hello and Hello Request messages
722 * here, anything else is handled by higher layers
723 * Application data protocol
724 * none of our business
725 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800726int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500727 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800728 unsigned int n;
729 SSL3_RECORD *rr;
730 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700731
Adam Langleyfcf25832014-12-18 17:42:32 -0800732 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
733 (peek && type != SSL3_RT_APPLICATION_DATA)) {
734 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
735 return -1;
736 }
Adam Langley95c29f32014-06-20 12:00:00 -0700737
Adam Langleyfcf25832014-12-18 17:42:32 -0800738 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
739 /* (partially) satisfy request from storage */
740 uint8_t *src = s->s3->handshake_fragment;
741 uint8_t *dst = buf;
742 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700743
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 /* peek == 0 */
745 n = 0;
746 while (len > 0 && s->s3->handshake_fragment_len > 0) {
747 *dst++ = *src++;
748 len--;
749 s->s3->handshake_fragment_len--;
750 n++;
751 }
752 /* move any remaining fragment bytes: */
753 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
754 s->s3->handshake_fragment[k] = *src++;
755 }
756 return n;
757 }
Adam Langley95c29f32014-06-20 12:00:00 -0700758
Adam Langleyfcf25832014-12-18 17:42:32 -0800759 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700760
David Benjamin931ab342015-02-08 19:46:57 -0500761 /* This may require multiple iterations. False Start will cause
762 * |s->handshake_func| to signal success one step early, but the handshake
763 * must be completely finished before other modes are accepted.
764 *
765 * TODO(davidben): Move this check up to a higher level. */
766 while (!s->in_handshake && SSL_in_init(s)) {
767 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800768 i = s->handshake_func(s);
769 if (i < 0) {
770 return i;
771 }
772 if (i == 0) {
773 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
774 return -1;
775 }
776 }
777
Adam Langley95c29f32014-06-20 12:00:00 -0700778start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 /* s->s3->rrec.type - is the type of record
782 * s->s3->rrec.data - data
783 * s->s3->rrec.off - offset into 'data' for next read
784 * s->s3->rrec.length - number of bytes. */
785 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700786
Adam Langleyfcf25832014-12-18 17:42:32 -0800787 /* get new packet if necessary */
788 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
789 ret = ssl3_get_record(s);
790 if (ret <= 0) {
791 return ret;
792 }
793 }
Adam Langley95c29f32014-06-20 12:00:00 -0700794
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700796
David Benjamindc3da932015-03-12 15:09:02 -0400797 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
798 * ssl3_get_finished. */
799 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
800 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800801 al = SSL_AD_UNEXPECTED_MESSAGE;
802 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
803 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
804 goto f_err;
805 }
Adam Langley95c29f32014-06-20 12:00:00 -0700806
Adam Langleyfcf25832014-12-18 17:42:32 -0800807 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
808 * Handshake record. */
809 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
810 al = SSL_AD_UNEXPECTED_MESSAGE;
811 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
812 goto f_err;
813 }
David Benjamin86271ee2014-07-21 16:14:03 -0400814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 /* If the other end has shut down, throw anything we read away (even in
816 * 'peek' mode) */
817 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
818 rr->length = 0;
819 s->rwstate = SSL_NOTHING;
820 return 0;
821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 if (type == rr->type) {
824 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
825 /* make sure that we are not getting application data when we are doing a
826 * handshake for the first time */
827 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500828 s->aead_read_ctx == NULL) {
829 /* TODO(davidben): Is this check redundant with the handshake_func
830 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 al = SSL_AD_UNEXPECTED_MESSAGE;
832 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
833 goto f_err;
834 }
Adam Langley95c29f32014-06-20 12:00:00 -0700835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 if (len <= 0) {
837 return len;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 if ((unsigned int)len > rr->length) {
841 n = rr->length;
842 } else {
843 n = (unsigned int)len;
844 }
Adam Langley95c29f32014-06-20 12:00:00 -0700845
Adam Langleyfcf25832014-12-18 17:42:32 -0800846 memcpy(buf, &(rr->data[rr->off]), n);
847 if (!peek) {
848 rr->length -= n;
849 rr->off += n;
850 if (rr->length == 0) {
851 s->rstate = SSL_ST_READ_HEADER;
852 rr->off = 0;
David Benjaminaebefed2015-05-08 22:20:04 -0400853 if (s->s3->rbuf.left == 0) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800854 ssl3_release_read_buffer(s);
855 }
856 }
857 }
858
859 return n;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 /* If we get here, then type != rr->type; if we have a handshake message,
864 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700865
Adam Langleyfcf25832014-12-18 17:42:32 -0800866 /* In case of record types for which we have 'fragment' storage, fill that so
867 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400868
Adam Langleyfcf25832014-12-18 17:42:32 -0800869 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjaminb16346b2015-04-08 19:16:58 -0400870 /* If peer renegotiations are disabled, all out-of-order handshake records
871 * are fatal. */
872 if (s->reject_peer_renegotiations) {
873 al = SSL_AD_NO_RENEGOTIATION;
874 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
875 goto f_err;
876 }
877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 const size_t size = sizeof(s->s3->handshake_fragment);
879 const size_t avail = size - s->s3->handshake_fragment_len;
880 const size_t todo = (rr->length < avail) ? rr->length : avail;
881 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
882 &rr->data[rr->off], todo);
883 rr->off += todo;
884 rr->length -= todo;
885 s->s3->handshake_fragment_len += todo;
886 if (s->s3->handshake_fragment_len < size) {
887 goto start; /* fragment was too small */
888 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800889 }
Adam Langley95c29f32014-06-20 12:00:00 -0700890
Adam Langleyfcf25832014-12-18 17:42:32 -0800891 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
892 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700893
Adam Langleyfcf25832014-12-18 17:42:32 -0800894 /* If we are a client, check for an incoming 'Hello Request': */
895 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
896 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
897 s->session != NULL && s->session->cipher != NULL) {
898 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 if (s->s3->handshake_fragment[1] != 0 ||
901 s->s3->handshake_fragment[2] != 0 ||
902 s->s3->handshake_fragment[3] != 0) {
903 al = SSL_AD_DECODE_ERROR;
904 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
905 goto f_err;
906 }
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 if (s->msg_callback) {
909 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
910 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
911 }
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
914 ssl3_renegotiate(s);
915 if (ssl3_renegotiate_check(s)) {
916 i = s->handshake_func(s);
917 if (i < 0) {
918 return i;
919 }
920 if (i == 0) {
921 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
922 return -1;
923 }
924 }
925 }
926 /* we either finished a handshake or ignored the request, now try again to
927 * obtain the (application) data we were asked for */
928 goto start;
929 }
David Benjaminb4188f02014-11-01 03:43:48 -0400930
David Benjamin86058a22015-02-22 13:07:21 -0500931 /* If an alert record, process one alert out of the record. Note that we allow
932 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800933 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500934 /* Alerts may not be fragmented. */
935 if (rr->length < 2) {
936 al = SSL_AD_DECODE_ERROR;
937 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
938 goto f_err;
939 }
Adam Langley95c29f32014-06-20 12:00:00 -0700940
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500942 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800943 s->msg_callback_arg);
944 }
David Benjamin86058a22015-02-22 13:07:21 -0500945 const uint8_t alert_level = rr->data[rr->off++];
946 const uint8_t alert_descr = rr->data[rr->off++];
947 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 if (s->info_callback != NULL) {
950 cb = s->info_callback;
951 } else if (s->ctx->info_callback != NULL) {
952 cb = s->ctx->info_callback;
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500956 uint16_t alert = (alert_level << 8) | alert_descr;
957 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
David Benjamin86058a22015-02-22 13:07:21 -0500960 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 s->s3->warn_alert = alert_descr;
962 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
963 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
964 return 0;
965 }
Adam Langley95c29f32014-06-20 12:00:00 -0700966
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 /* This is a warning but we receive it if we requested renegotiation and
968 * the peer denied it. Terminate with a fatal alert because if
969 * application tried to renegotiatie it presumably had a good reason and
970 * expects it to succeed.
971 *
972 * In future we might have a renegotiation where we don't care if the
973 * peer refused it where we carry on. */
974 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
975 al = SSL_AD_HANDSHAKE_FAILURE;
976 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
977 goto f_err;
978 }
David Benjamin86058a22015-02-22 13:07:21 -0500979 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700981
Adam Langleyfcf25832014-12-18 17:42:32 -0800982 s->rwstate = SSL_NOTHING;
983 s->s3->fatal_alert = alert_descr;
984 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
985 SSL_AD_REASON_OFFSET + alert_descr);
986 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
987 ERR_add_error_data(2, "SSL alert number ", tmp);
988 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
989 SSL_CTX_remove_session(s->ctx, s->session);
990 return 0;
991 } else {
992 al = SSL_AD_ILLEGAL_PARAMETER;
993 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
994 goto f_err;
995 }
Adam Langley95c29f32014-06-20 12:00:00 -0700996
Adam Langleyfcf25832014-12-18 17:42:32 -0800997 goto start;
998 }
Adam Langley95c29f32014-06-20 12:00:00 -0700999
Adam Langleyfcf25832014-12-18 17:42:32 -08001000 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1001 /* but we have not received a shutdown */
1002 s->rwstate = SSL_NOTHING;
1003 rr->length = 0;
1004 return 0;
1005 }
Adam Langley95c29f32014-06-20 12:00:00 -07001006
Adam Langleyfcf25832014-12-18 17:42:32 -08001007 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1008 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1009 * record payload has to look like */
1010 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1011 al = SSL_AD_ILLEGAL_PARAMETER;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1013 goto f_err;
1014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 /* Check we have a cipher to change to */
1017 if (s->s3->tmp.new_cipher == NULL) {
1018 al = SSL_AD_UNEXPECTED_MESSAGE;
1019 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1020 goto f_err;
1021 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001022
Adam Langleyfcf25832014-12-18 17:42:32 -08001023 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1024 al = SSL_AD_UNEXPECTED_MESSAGE;
1025 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1026 goto f_err;
1027 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001032
Adam Langleyfcf25832014-12-18 17:42:32 -08001033 if (s->msg_callback) {
1034 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1035 s->msg_callback_arg);
1036 }
Adam Langley95c29f32014-06-20 12:00:00 -07001037
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 s->s3->change_cipher_spec = 1;
1039 if (!ssl3_do_change_cipher_spec(s)) {
1040 goto err;
1041 } else {
1042 goto start;
1043 }
1044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 /* Unexpected handshake message (Client Hello, or protocol violation) */
1047 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1048 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1049 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1050 s->renegotiate = 1;
1051 s->new_session = 1;
1052 }
1053 i = s->handshake_func(s);
1054 if (i < 0) {
1055 return i;
1056 }
1057 if (i == 0) {
1058 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1059 return -1;
1060 }
Adam Langley95c29f32014-06-20 12:00:00 -07001061
Adam Langleyfcf25832014-12-18 17:42:32 -08001062 goto start;
1063 }
1064
David Benjamine820df92015-02-08 16:06:54 -05001065 /* We already handled these. */
1066 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1067 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001068
David Benjamine820df92015-02-08 16:06:54 -05001069 al = SSL_AD_UNEXPECTED_MESSAGE;
1070 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001071
1072f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001073 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001074err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 return -1;
1076}
Adam Langley95c29f32014-06-20 12:00:00 -07001077
Adam Langleyfcf25832014-12-18 17:42:32 -08001078int ssl3_do_change_cipher_spec(SSL *s) {
1079 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001080
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 if (s->state & SSL_ST_ACCEPT) {
1082 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1083 } else {
1084 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1085 }
Adam Langley95c29f32014-06-20 12:00:00 -07001086
Adam Langleyfcf25832014-12-18 17:42:32 -08001087 if (s->s3->tmp.key_block == NULL) {
1088 if (s->session == NULL || s->session->master_key_length == 0) {
1089 /* might happen if dtls1_read_bytes() calls this */
1090 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1091 SSL_R_CCS_RECEIVED_EARLY);
1092 return 0;
1093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 s->session->cipher = s->s3->tmp.new_cipher;
1096 if (!s->enc_method->setup_key_block(s)) {
1097 return 0;
1098 }
1099 }
Adam Langley95c29f32014-06-20 12:00:00 -07001100
Adam Langleyfcf25832014-12-18 17:42:32 -08001101 if (!s->enc_method->change_cipher_state(s, i)) {
1102 return 0;
1103 }
Adam Langley95c29f32014-06-20 12:00:00 -07001104
Adam Langleyfcf25832014-12-18 17:42:32 -08001105 return 1;
1106}
Adam Langley95c29f32014-06-20 12:00:00 -07001107
Adam Langleyfcf25832014-12-18 17:42:32 -08001108int ssl3_send_alert(SSL *s, int level, int desc) {
1109 /* Map tls/ssl alert value to correct one */
1110 desc = s->enc_method->alert_value(desc);
1111 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1112 /* SSL 3.0 does not have protocol_version alerts */
1113 desc = SSL_AD_HANDSHAKE_FAILURE;
1114 }
1115 if (desc < 0) {
1116 return -1;
1117 }
Adam Langley95c29f32014-06-20 12:00:00 -07001118
Adam Langleyfcf25832014-12-18 17:42:32 -08001119 /* If a fatal one, remove from cache */
1120 if (level == 2 && s->session != NULL) {
1121 SSL_CTX_remove_session(s->ctx, s->session);
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 s->s3->alert_dispatch = 1;
1125 s->s3->send_alert[0] = level;
1126 s->s3->send_alert[1] = desc;
1127 if (s->s3->wbuf.left == 0) {
1128 /* data is still being written out. */
1129 return s->method->ssl_dispatch_alert(s);
1130 }
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 /* else data is still being written out, we will get written some time in the
1133 * future */
1134 return -1;
1135}
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137int ssl3_dispatch_alert(SSL *s) {
1138 int i, j;
1139 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 s->s3->alert_dispatch = 0;
David Benjamin9faafda2015-04-04 19:23:10 -04001142 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 if (i <= 0) {
1144 s->s3->alert_dispatch = 1;
1145 } else {
1146 /* Alert sent to BIO. If it is important, flush it now. If the message
1147 * does not get sent due to non-blocking IO, we will not worry too much. */
1148 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1149 BIO_flush(s->wbio);
1150 }
Adam Langley95c29f32014-06-20 12:00:00 -07001151
Adam Langleyfcf25832014-12-18 17:42:32 -08001152 if (s->msg_callback) {
1153 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1154 s->msg_callback_arg);
1155 }
1156
1157 if (s->info_callback != NULL) {
1158 cb = s->info_callback;
1159 } else if (s->ctx->info_callback != NULL) {
1160 cb = s->ctx->info_callback;
1161 }
1162
1163 if (cb != NULL) {
1164 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1165 cb(s, SSL_CB_WRITE_ALERT, j);
1166 }
1167 }
1168
1169 return i;
1170}