blob: 2d888e23131028353e9665d02fd0c748b7263694 [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>
110#include <errno.h>
Adam Langley87750b42014-06-20 12:00:00 -0700111#include <limits.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700112#include <stdio.h>
113
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
120#include "ssl_locl.h"
121
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
Adam Langleyfcf25832014-12-18 17:42:32 -0800127int ssl3_read_n(SSL *s, int n, int max, int extend) {
128 /* 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
132 * |s->packet| and |s->packet_length|. (If |s->read_ahead| is set, |max|
133 * bytes may be stored in |rbuf| (plus |s->packet_length| bytes if |extend|
134 * is one.) */
135 int i, len, left;
136 long align = 0;
137 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
Adam Langleyfcf25832014-12-18 17:42:32 -0800151 align = (long)rb->buf + SSL3_RT_HEADER_LENGTH;
152 align = (-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
Adam Langleyfcf25832014-12-18 17:42:32 -0800209 if (!s->read_ahead) {
210 /* ignore max parameter */
211 max = n;
212 } else {
213 if (max < n) {
214 max = n;
215 }
216 if (max > (int)(rb->len - rb->offset)) {
217 max = rb->len - rb->offset;
218 }
219 }
Adam Langley95c29f32014-06-20 12:00:00 -0700220
Adam Langleyfcf25832014-12-18 17:42:32 -0800221 while (left < n) {
222 /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
223 * read in more until we have len+n (up to len+max if possible). */
224 ERR_clear_system_error();
225 if (s->rbio != NULL) {
226 s->rwstate = SSL_READING;
227 i = BIO_read(s->rbio, pkt + len + left, max - left);
228 } else {
229 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
230 i = -1;
231 }
Adam Langley95c29f32014-06-20 12:00:00 -0700232
Adam Langleyfcf25832014-12-18 17:42:32 -0800233 if (i <= 0) {
234 rb->left = left;
235 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s) &&
236 len + left == 0) {
237 ssl3_release_read_buffer(s);
238 }
239 return i;
240 }
241 left += i;
242 /* reads should *never* span multiple packets for DTLS because the
243 * underlying transport protocol is message oriented as opposed to byte
244 * oriented as in the TLS case. */
245 if (SSL_IS_DTLS(s) && n > left) {
246 n = left; /* makes the while condition false */
247 }
248 }
Adam Langley95c29f32014-06-20 12:00:00 -0700249
Adam Langleyfcf25832014-12-18 17:42:32 -0800250 /* done reading, now the book-keeping */
251 rb->offset += n;
252 rb->left = left - n;
253 s->packet_length += n;
254 s->rwstate = SSL_NOTHING;
255
256 return n;
257}
Adam Langley95c29f32014-06-20 12:00:00 -0700258
Adam Langley48105fa2014-06-20 12:00:00 -0700259/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
260 * be processed per call to ssl3_get_record. Without this limit an attacker
261 * could send empty records at a faster rate than we can process and cause
262 * ssl3_get_record to loop forever. */
263#define MAX_EMPTY_RECORDS 32
264
Adam Langleyfcf25832014-12-18 17:42:32 -0800265/* Call this to get a new input record. It will return <= 0 if more data is
266 * needed, normally due to an error or non-blocking IO. When it finishes, one
267 * packet has been decoded and can be found in
Adam Langley95c29f32014-06-20 12:00:00 -0700268 * ssl->s3->rrec.type - is the type of record
Adam Langleyfcf25832014-12-18 17:42:32 -0800269 * ssl->s3->rrec.data - data
270 * ssl->s3->rrec.length - number of bytes */
Adam Langley95c29f32014-06-20 12:00:00 -0700271/* used only by ssl3_read_bytes */
Adam Langleyfcf25832014-12-18 17:42:32 -0800272static int ssl3_get_record(SSL *s) {
273 int ssl_major, ssl_minor, al;
David Benjamin1e52eca2015-01-22 15:33:51 -0500274 int n, i, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 SSL3_RECORD *rr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800276 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800277 short version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 size_t extra;
279 unsigned empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langleyfcf25832014-12-18 17:42:32 -0800281 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langleyfcf25832014-12-18 17:42:32 -0800283 if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) {
284 extra = SSL3_RT_MAX_EXTRA;
285 } else {
286 extra = 0;
287 }
288
289 if (extra && !s->s3->init_extra) {
290 /* An application error: SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER set after
291 * ssl3_setup_buffers() was done */
292 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, ERR_R_INTERNAL_ERROR);
293 return -1;
294 }
Adam Langley95c29f32014-06-20 12:00:00 -0700295
296again:
Adam Langleyfcf25832014-12-18 17:42:32 -0800297 /* check if we have the header */
298 if (s->rstate != SSL_ST_READ_BODY ||
299 s->packet_length < SSL3_RT_HEADER_LENGTH) {
300 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
301 if (n <= 0) {
302 return n; /* error or non-blocking */
303 }
304 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langleyfcf25832014-12-18 17:42:32 -0800306 p = s->packet;
307 if (s->msg_callback) {
308 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langleyfcf25832014-12-18 17:42:32 -0800311 /* Pull apart the header into the SSL3_RECORD */
312 rr->type = *(p++);
313 ssl_major = *(p++);
314 ssl_minor = *(p++);
315 version = (ssl_major << 8) | ssl_minor;
316 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700317
Adam Langleyfcf25832014-12-18 17:42:32 -0800318 if (s->s3->have_version && version != s->version) {
319 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
320 if ((s->version & 0xFF00) == (version & 0xFF00)) {
321 /* Send back error using their minor version number. */
322 s->version = (unsigned short)version;
323 }
324 al = SSL_AD_PROTOCOL_VERSION;
325 goto f_err;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langleyfcf25832014-12-18 17:42:32 -0800328 if ((version >> 8) != SSL3_VERSION_MAJOR) {
329 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
330 goto err;
331 }
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langleyfcf25832014-12-18 17:42:32 -0800333 if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) {
334 al = SSL_AD_RECORD_OVERFLOW;
335 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_PACKET_LENGTH_TOO_LONG);
336 goto f_err;
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* now s->rstate == SSL_ST_READ_BODY */
340 }
Adam Langley95c29f32014-06-20 12:00:00 -0700341
Adam Langleyfcf25832014-12-18 17:42:32 -0800342 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700343
Adam Langleyfcf25832014-12-18 17:42:32 -0800344 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
345 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
346 i = rr->length;
347 n = ssl3_read_n(s, i, i, 1);
348 if (n <= 0) {
349 /* Error or non-blocking IO. Now |n| == |rr->length|, and
350 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
351 return n;
352 }
353 }
Adam Langley95c29f32014-06-20 12:00:00 -0700354
Adam Langleyfcf25832014-12-18 17:42:32 -0800355 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and
358 * we have that many bytes in s->packet. */
359 rr->input = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700360
Adam Langleyfcf25832014-12-18 17:42:32 -0800361 /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at
362 * |rr->length| bytes, which need to be copied into |rr->data| by decryption.
363 * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be
364 * pointed at the new buffer. */
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
367 * rr->length bytes of encrypted compressed stuff. */
Adam Langley95c29f32014-06-20 12:00:00 -0700368
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 /* check is not needed I believe */
370 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
371 al = SSL_AD_RECORD_OVERFLOW;
372 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
373 goto f_err;
374 }
Adam Langley95c29f32014-06-20 12:00:00 -0700375
Adam Langleyfcf25832014-12-18 17:42:32 -0800376 /* decrypt in place in 'rr->input' */
377 rr->data = rr->input;
Adam Langley95c29f32014-06-20 12:00:00 -0700378
David Benjamin1e52eca2015-01-22 15:33:51 -0500379 if (!s->enc_method->enc(s, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800380 al = SSL_AD_BAD_RECORD_MAC;
381 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
382 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
383 goto f_err;
384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langleyfcf25832014-12-18 17:42:32 -0800386 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
387 al = SSL_AD_RECORD_OVERFLOW;
388 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
389 goto f_err;
390 }
Adam Langley95c29f32014-06-20 12:00:00 -0700391
Adam Langleyfcf25832014-12-18 17:42:32 -0800392 rr->off = 0;
393 /* So at this point the following is true:
394 * ssl->s3->rrec.type is the type of record;
395 * ssl->s3->rrec.length is the number of bytes in the record;
396 * ssl->s3->rrec.off is the offset to first valid byte;
397 * ssl->s3->rrec.data is where to take bytes from (increment after use). */
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 /* we have pulled in a full packet so zero things */
400 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 /* just read a 0 length packet */
403 if (rr->length == 0) {
404 empty_record_count++;
405 if (empty_record_count > MAX_EMPTY_RECORDS) {
406 al = SSL_AD_UNEXPECTED_MESSAGE;
407 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
408 goto f_err;
409 }
410 goto again;
411 }
Adam Langley95c29f32014-06-20 12:00:00 -0700412
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700414
415f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800416 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700417err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800418 return ret;
419}
Adam Langley95c29f32014-06-20 12:00:00 -0700420
Adam Langleyfcf25832014-12-18 17:42:32 -0800421/* Call this to write data in records of type |type|. It will return <= 0 if
422 * not all data has been sent or non-blocking IO. */
423int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
424 const uint8_t *buf = buf_;
425 unsigned int tot, n, nw;
426 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700427
Adam Langleyfcf25832014-12-18 17:42:32 -0800428 s->rwstate = SSL_NOTHING;
429 assert(s->s3->wnum <= INT_MAX);
430 tot = s->s3->wnum;
431 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700432
David Benjamined7c4752015-02-16 19:16:46 -0500433 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800434 i = s->handshake_func(s);
435 if (i < 0) {
436 return i;
437 }
438 if (i == 0) {
439 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
440 return -1;
441 }
442 }
Adam Langley95c29f32014-06-20 12:00:00 -0700443
Adam Langleyfcf25832014-12-18 17:42:32 -0800444 /* Ensure that if we end up with a smaller value of data to write out than
445 * the the original len from a write which didn't complete for non-blocking
446 * I/O and also somehow ended up avoiding the check for this in
447 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
448 * end up with (len-tot) as a large number that will then promptly send
449 * beyond the end of the users buffer ... so we trap and report the error in
450 * a way the user will notice. */
451 if (len < 0 || (size_t)len < tot) {
452 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
453 return -1;
454 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700455
David Benjamin9faafda2015-04-04 19:23:10 -0400456 int record_split_done = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800457 n = (len - tot);
458 for (;;) {
459 /* max contains the maximum number of bytes that we can put into a
460 * record. */
461 unsigned max = s->max_send_fragment;
462 /* fragment is true if do_ssl3_write should send the first byte in its own
463 * record in order to randomise a CBC IV. */
464 int fragment = 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400465 if (!record_split_done && s->s3->need_record_splitting &&
466 type == SSL3_RT_APPLICATION_DATA) {
467 /* Only the the first record per write call needs to be split. The
468 * remaining plaintext was determined before the IV was randomized. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800469 fragment = 1;
David Benjamin9faafda2015-04-04 19:23:10 -0400470 record_split_done = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800472 if (n > max) {
473 nw = max;
474 } else {
475 nw = n;
476 }
Adam Langley95c29f32014-06-20 12:00:00 -0700477
David Benjamin9faafda2015-04-04 19:23:10 -0400478 i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
Adam Langleyfcf25832014-12-18 17:42:32 -0800479 if (i <= 0) {
480 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800481 return i;
482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
485 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800486 return tot + i;
487 }
Adam Langley95c29f32014-06-20 12:00:00 -0700488
Adam Langleyfcf25832014-12-18 17:42:32 -0800489 n -= i;
490 tot += i;
491 }
492}
Adam Langley95c29f32014-06-20 12:00:00 -0700493
David Benjamin9faafda2015-04-04 19:23:10 -0400494/* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
495 * writes it to |out|. At most |max_out| bytes will be written. It returns one
496 * on success and zero on error. On success, |s->s3->wrec| is updated to include
497 * the new record. */
498static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
499 size_t max_out, uint8_t type, const uint8_t *in,
500 size_t in_len) {
501 if (max_out < SSL3_RT_HEADER_LENGTH) {
502 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
503 return 0;
504 }
505
506 out[0] = type;
507
508 /* Some servers hang if initial ClientHello is larger than 256 bytes and
509 * record version number > TLS 1.0. */
510 if (!s->s3->have_version && s->version > SSL3_VERSION) {
511 out[1] = TLS1_VERSION >> 8;
512 out[2] = TLS1_VERSION & 0xff;
513 } else {
514 out[1] = s->version >> 8;
515 out[2] = s->version & 0xff;
516 }
517
518 size_t explicit_nonce_len = 0;
519 if (s->aead_write_ctx != NULL &&
520 s->aead_write_ctx->variable_nonce_included_in_record) {
521 explicit_nonce_len = s->aead_write_ctx->variable_nonce_len;
522 }
523 size_t max_overhead = 0;
524 if (s->aead_write_ctx != NULL) {
525 max_overhead = s->aead_write_ctx->tag_len;
526 }
527
528 /* Assemble the input for |s->enc_method->enc|. The input is the plaintext
529 * with |explicit_nonce_len| bytes of space prepended for the explicit
530 * nonce. The input is copied into |out| and then encrypted in-place to take
531 * advantage of alignment.
532 *
533 * TODO(davidben): |tls1_enc| should accept its inputs and outputs directly
534 * rather than looking up in |wrec| and friends. The |max_overhead| bounds
535 * check would also be unnecessary if |max_out| were passed down. */
536 SSL3_RECORD *wr = &s->s3->wrec;
537 size_t plaintext_len = in_len + explicit_nonce_len;
538 if (plaintext_len < in_len || plaintext_len > INT_MAX ||
539 plaintext_len + max_overhead < plaintext_len) {
540 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
541 return 0;
542 }
543 if (max_out - SSL3_RT_HEADER_LENGTH < plaintext_len + max_overhead) {
544 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
545 return 0;
546 }
547 wr->type = type;
548 wr->input = out + SSL3_RT_HEADER_LENGTH;
549 wr->data = wr->input;
550 wr->length = plaintext_len;
551 memcpy(wr->input + explicit_nonce_len, in, in_len);
552
553 if (!s->enc_method->enc(s, 1)) {
554 return 0;
555 }
556
557 /* |wr->length| has now been set to the ciphertext length. */
558 if (wr->length >= 1 << 16) {
559 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
560 return 0;
561 }
562 out[3] = wr->length >> 8;
563 out[4] = wr->length & 0xff;
564 *out_len = SSL3_RT_HEADER_LENGTH + (size_t)wr->length;
565
566 if (s->msg_callback) {
567 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
568 s, s->msg_callback_arg);
569 }
570
571 return 1;
572}
573
Adam Langleyd493d522014-06-20 12:00:00 -0700574/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
575 * then it splits the record into a one byte record and a record with the rest
David Benjamin9faafda2015-04-04 19:23:10 -0400576 * of the data in order to randomise a CBC IV. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800577static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400578 char fragment) {
579 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley95c29f32014-06-20 12:00:00 -0700580
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 /* first check if there is a SSL3_BUFFER still being written out. This will
582 * happen with non blocking IO */
583 if (wb->left != 0) {
584 return ssl3_write_pending(s, type, buf, len);
585 }
Adam Langley95c29f32014-06-20 12:00:00 -0700586
Adam Langleyfcf25832014-12-18 17:42:32 -0800587 /* If we have an alert to send, lets send it */
588 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400589 int ret = s->method->ssl_dispatch_alert(s);
590 if (ret <= 0) {
591 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800592 }
593 /* if it went, fall through and send more stuff */
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
597 return -1;
598 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700599
Adam Langleyfcf25832014-12-18 17:42:32 -0800600 if (len == 0) {
601 return 0;
602 }
David Benjamin9faafda2015-04-04 19:23:10 -0400603 if (len == 1) {
604 /* No sense in fragmenting a one-byte record. */
605 fragment = 0;
606 }
Adam Langley95c29f32014-06-20 12:00:00 -0700607
David Benjamin9faafda2015-04-04 19:23:10 -0400608 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
609 uintptr_t align;
Adam Langleyfcf25832014-12-18 17:42:32 -0800610 if (fragment) {
David Benjamin9faafda2015-04-04 19:23:10 -0400611 /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
612 * multiple of the block size which we may assume is aligned. Thus we only
613 * need to account for a second copy of the record header. */
614 align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800615 } else {
David Benjamin9faafda2015-04-04 19:23:10 -0400616 align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
617 }
618 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
619 uint8_t *out = wb->buf + align;
620 wb->offset = align;
621 size_t max_out = wb->len - wb->offset;
622
623 const uint8_t *orig_buf = buf;
624 unsigned int orig_len = len;
625 size_t fragment_len = 0;
626 if (fragment) {
627 /* Write the first byte in its own record as a countermeasure against
628 * known-IV weaknesses in CBC ciphersuites. (See
629 * http://www.openssl.org/~bodo/tls-cbc.txt.) */
630 if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
631 return -1;
632 }
633 out += fragment_len;
634 max_out -= fragment_len;
635 buf++;
636 len--;
Adam Langleyfcf25832014-12-18 17:42:32 -0800637 }
Adam Langley95c29f32014-06-20 12:00:00 -0700638
David Benjamin9faafda2015-04-04 19:23:10 -0400639 assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
640 == 0);
641 size_t ciphertext_len;
642 if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
643 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800644 }
David Benjamin9faafda2015-04-04 19:23:10 -0400645 ciphertext_len += fragment_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700646
Adam Langleyfcf25832014-12-18 17:42:32 -0800647 /* now let's set up wb */
David Benjamin9faafda2015-04-04 19:23:10 -0400648 wb->left = ciphertext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700649
Adam Langleyfcf25832014-12-18 17:42:32 -0800650 /* memorize arguments so that ssl3_write_pending can detect bad write retries
651 * later */
David Benjamin9faafda2015-04-04 19:23:10 -0400652 s->s3->wpend_tot = orig_len;
653 s->s3->wpend_buf = orig_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800654 s->s3->wpend_type = type;
David Benjamin9faafda2015-04-04 19:23:10 -0400655 s->s3->wpend_ret = orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800656
657 /* we now just need to write the buffer */
David Benjamin9faafda2015-04-04 19:23:10 -0400658 return ssl3_write_pending(s, type, orig_buf, orig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800659}
Adam Langley95c29f32014-06-20 12:00:00 -0700660
661/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800662int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
663 int i;
664 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700665
Adam Langleyfcf25832014-12-18 17:42:32 -0800666 if (s->s3->wpend_tot > (int)len ||
667 (s->s3->wpend_buf != buf &&
668 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
669 s->s3->wpend_type != type) {
670 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
671 return -1;
672 }
Adam Langley95c29f32014-06-20 12:00:00 -0700673
Adam Langleyfcf25832014-12-18 17:42:32 -0800674 for (;;) {
675 ERR_clear_system_error();
676 if (s->wbio != NULL) {
677 s->rwstate = SSL_WRITING;
678 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
679 (unsigned int)wb->left);
680 } else {
681 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
682 i = -1;
683 }
684 if (i == wb->left) {
685 wb->left = 0;
686 wb->offset += i;
687 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) {
688 ssl3_release_write_buffer(s);
689 }
690 s->rwstate = SSL_NOTHING;
691 return s->s3->wpend_ret;
692 } else if (i <= 0) {
693 if (SSL_IS_DTLS(s)) {
694 /* For DTLS, just drop it. That's kind of the whole
695 point in using a datagram service */
696 wb->left = 0;
697 }
698 return i;
699 }
700 wb->offset += i;
701 wb->left -= i;
702 }
703}
Adam Langley95c29f32014-06-20 12:00:00 -0700704
David Benjamin86271ee2014-07-21 16:14:03 -0400705/* ssl3_expect_change_cipher_spec informs the record layer that a
706 * ChangeCipherSpec record is required at this point. If a Handshake record is
707 * received before ChangeCipherSpec, the connection will fail. Moreover, if
708 * there are unprocessed handshake bytes, the handshake will also fail and the
709 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800710int ssl3_expect_change_cipher_spec(SSL *s) {
711 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
712 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
713 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
714 return 0;
715 }
716
717 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
718 return 1;
719}
David Benjamin86271ee2014-07-21 16:14:03 -0400720
Adam Langley95c29f32014-06-20 12:00:00 -0700721/* Return up to 'len' payload bytes received in 'type' records.
722 * 'type' is one of the following:
723 *
724 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
725 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
726 * - 0 (during a shutdown, no data has to be returned)
727 *
728 * If we don't have stored data to work from, read a SSL/TLS record first
729 * (possibly multiple records if we still don't have anything to return).
730 *
731 * This function must handle any surprises the peer may have for us, such as
732 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
733 * a surprise, but handled as if it were), or renegotiation requests.
734 * Also if record payloads contain fragments too small to process, we store
735 * them until there is enough for the respective protocol (the record protocol
736 * may use arbitrary fragmentation and even interleaving):
737 * Change cipher spec protocol
738 * just 1 byte needed, no need for keeping anything stored
739 * Alert protocol
740 * 2 bytes needed (AlertLevel, AlertDescription)
741 * Handshake protocol
742 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
743 * to detect unexpected Client Hello and Hello Request messages
744 * here, anything else is handled by higher layers
745 * Application data protocol
746 * none of our business
747 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800748int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500749 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 unsigned int n;
751 SSL3_RECORD *rr;
752 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
755 (peek && type != SSL3_RT_APPLICATION_DATA)) {
756 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
757 return -1;
758 }
Adam Langley95c29f32014-06-20 12:00:00 -0700759
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
761 /* (partially) satisfy request from storage */
762 uint8_t *src = s->s3->handshake_fragment;
763 uint8_t *dst = buf;
764 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700765
Adam Langleyfcf25832014-12-18 17:42:32 -0800766 /* peek == 0 */
767 n = 0;
768 while (len > 0 && s->s3->handshake_fragment_len > 0) {
769 *dst++ = *src++;
770 len--;
771 s->s3->handshake_fragment_len--;
772 n++;
773 }
774 /* move any remaining fragment bytes: */
775 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
776 s->s3->handshake_fragment[k] = *src++;
777 }
778 return n;
779 }
Adam Langley95c29f32014-06-20 12:00:00 -0700780
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700782
David Benjamin931ab342015-02-08 19:46:57 -0500783 /* This may require multiple iterations. False Start will cause
784 * |s->handshake_func| to signal success one step early, but the handshake
785 * must be completely finished before other modes are accepted.
786 *
787 * TODO(davidben): Move this check up to a higher level. */
788 while (!s->in_handshake && SSL_in_init(s)) {
789 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800790 i = s->handshake_func(s);
791 if (i < 0) {
792 return i;
793 }
794 if (i == 0) {
795 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
796 return -1;
797 }
798 }
799
David Benjamine0e7d0d2015-02-08 19:33:25 -0500800 if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) {
801 /* TODO(davidben): Is this redundant with the calls in the handshake? */
802 return -1;
803 }
804
Adam Langley95c29f32014-06-20 12:00:00 -0700805start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800806 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 /* s->s3->rrec.type - is the type of record
809 * s->s3->rrec.data - data
810 * s->s3->rrec.off - offset into 'data' for next read
811 * s->s3->rrec.length - number of bytes. */
812 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700813
Adam Langleyfcf25832014-12-18 17:42:32 -0800814 /* get new packet if necessary */
815 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
816 ret = ssl3_get_record(s);
817 if (ret <= 0) {
818 return ret;
819 }
820 }
Adam Langley95c29f32014-06-20 12:00:00 -0700821
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700823
David Benjamindc3da932015-03-12 15:09:02 -0400824 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
825 * ssl3_get_finished. */
826 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
827 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 al = SSL_AD_UNEXPECTED_MESSAGE;
829 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
830 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
831 goto f_err;
832 }
Adam Langley95c29f32014-06-20 12:00:00 -0700833
Adam Langleyfcf25832014-12-18 17:42:32 -0800834 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
835 * Handshake record. */
836 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
837 al = SSL_AD_UNEXPECTED_MESSAGE;
838 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
839 goto f_err;
840 }
David Benjamin86271ee2014-07-21 16:14:03 -0400841
Adam Langleyfcf25832014-12-18 17:42:32 -0800842 /* If the other end has shut down, throw anything we read away (even in
843 * 'peek' mode) */
844 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
845 rr->length = 0;
846 s->rwstate = SSL_NOTHING;
847 return 0;
848 }
Adam Langley95c29f32014-06-20 12:00:00 -0700849
Adam Langleyfcf25832014-12-18 17:42:32 -0800850 if (type == rr->type) {
851 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
852 /* make sure that we are not getting application data when we are doing a
853 * handshake for the first time */
854 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500855 s->aead_read_ctx == NULL) {
856 /* TODO(davidben): Is this check redundant with the handshake_func
857 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 al = SSL_AD_UNEXPECTED_MESSAGE;
859 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
860 goto f_err;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 if (len <= 0) {
864 return len;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 if ((unsigned int)len > rr->length) {
868 n = rr->length;
869 } else {
870 n = (unsigned int)len;
871 }
Adam Langley95c29f32014-06-20 12:00:00 -0700872
Adam Langleyfcf25832014-12-18 17:42:32 -0800873 memcpy(buf, &(rr->data[rr->off]), n);
874 if (!peek) {
875 rr->length -= n;
876 rr->off += n;
877 if (rr->length == 0) {
878 s->rstate = SSL_ST_READ_HEADER;
879 rr->off = 0;
880 if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) {
881 ssl3_release_read_buffer(s);
882 }
883 }
884 }
885
886 return n;
887 }
Adam Langley95c29f32014-06-20 12:00:00 -0700888
889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 /* If we get here, then type != rr->type; if we have a handshake message,
891 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700892
Adam Langleyfcf25832014-12-18 17:42:32 -0800893 /* In case of record types for which we have 'fragment' storage, fill that so
894 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400895
Adam Langleyfcf25832014-12-18 17:42:32 -0800896 if (rr->type == SSL3_RT_HANDSHAKE) {
897 const size_t size = sizeof(s->s3->handshake_fragment);
898 const size_t avail = size - s->s3->handshake_fragment_len;
899 const size_t todo = (rr->length < avail) ? rr->length : avail;
900 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
901 &rr->data[rr->off], todo);
902 rr->off += todo;
903 rr->length -= todo;
904 s->s3->handshake_fragment_len += todo;
905 if (s->s3->handshake_fragment_len < size) {
906 goto start; /* fragment was too small */
907 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 }
Adam Langley95c29f32014-06-20 12:00:00 -0700909
Adam Langleyfcf25832014-12-18 17:42:32 -0800910 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
911 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 /* If we are a client, check for an incoming 'Hello Request': */
914 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
915 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
916 s->session != NULL && s->session->cipher != NULL) {
917 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700918
Adam Langleyfcf25832014-12-18 17:42:32 -0800919 if (s->s3->handshake_fragment[1] != 0 ||
920 s->s3->handshake_fragment[2] != 0 ||
921 s->s3->handshake_fragment[3] != 0) {
922 al = SSL_AD_DECODE_ERROR;
923 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
924 goto f_err;
925 }
Adam Langley95c29f32014-06-20 12:00:00 -0700926
Adam Langleyfcf25832014-12-18 17:42:32 -0800927 if (s->msg_callback) {
928 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
929 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
930 }
Adam Langley95c29f32014-06-20 12:00:00 -0700931
Adam Langleyfcf25832014-12-18 17:42:32 -0800932 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
933 ssl3_renegotiate(s);
934 if (ssl3_renegotiate_check(s)) {
935 i = s->handshake_func(s);
936 if (i < 0) {
937 return i;
938 }
939 if (i == 0) {
940 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
941 return -1;
942 }
943 }
944 }
945 /* we either finished a handshake or ignored the request, now try again to
946 * obtain the (application) data we were asked for */
947 goto start;
948 }
David Benjaminb4188f02014-11-01 03:43:48 -0400949
David Benjamin86058a22015-02-22 13:07:21 -0500950 /* If an alert record, process one alert out of the record. Note that we allow
951 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500953 /* Alerts may not be fragmented. */
954 if (rr->length < 2) {
955 al = SSL_AD_DECODE_ERROR;
956 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
957 goto f_err;
958 }
Adam Langley95c29f32014-06-20 12:00:00 -0700959
Adam Langleyfcf25832014-12-18 17:42:32 -0800960 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500961 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 s->msg_callback_arg);
963 }
David Benjamin86058a22015-02-22 13:07:21 -0500964 const uint8_t alert_level = rr->data[rr->off++];
965 const uint8_t alert_descr = rr->data[rr->off++];
966 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 if (s->info_callback != NULL) {
969 cb = s->info_callback;
970 } else if (s->ctx->info_callback != NULL) {
971 cb = s->ctx->info_callback;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500975 uint16_t alert = (alert_level << 8) | alert_descr;
976 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800977 }
Adam Langley95c29f32014-06-20 12:00:00 -0700978
David Benjamin86058a22015-02-22 13:07:21 -0500979 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 s->s3->warn_alert = alert_descr;
981 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
982 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
983 return 0;
984 }
Adam Langley95c29f32014-06-20 12:00:00 -0700985
Adam Langleyfcf25832014-12-18 17:42:32 -0800986 /* This is a warning but we receive it if we requested renegotiation and
987 * the peer denied it. Terminate with a fatal alert because if
988 * application tried to renegotiatie it presumably had a good reason and
989 * expects it to succeed.
990 *
991 * In future we might have a renegotiation where we don't care if the
992 * peer refused it where we carry on. */
993 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
994 al = SSL_AD_HANDSHAKE_FAILURE;
995 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
996 goto f_err;
997 }
David Benjamin86058a22015-02-22 13:07:21 -0500998 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800999 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -07001000
Adam Langleyfcf25832014-12-18 17:42:32 -08001001 s->rwstate = SSL_NOTHING;
1002 s->s3->fatal_alert = alert_descr;
1003 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
1004 SSL_AD_REASON_OFFSET + alert_descr);
1005 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
1006 ERR_add_error_data(2, "SSL alert number ", tmp);
1007 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1008 SSL_CTX_remove_session(s->ctx, s->session);
1009 return 0;
1010 } else {
1011 al = SSL_AD_ILLEGAL_PARAMETER;
1012 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
1013 goto f_err;
1014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 goto start;
1017 }
Adam Langley95c29f32014-06-20 12:00:00 -07001018
Adam Langleyfcf25832014-12-18 17:42:32 -08001019 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1020 /* but we have not received a shutdown */
1021 s->rwstate = SSL_NOTHING;
1022 rr->length = 0;
1023 return 0;
1024 }
Adam Langley95c29f32014-06-20 12:00:00 -07001025
Adam Langleyfcf25832014-12-18 17:42:32 -08001026 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1027 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1028 * record payload has to look like */
1029 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1030 al = SSL_AD_ILLEGAL_PARAMETER;
1031 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1032 goto f_err;
1033 }
Adam Langley95c29f32014-06-20 12:00:00 -07001034
Adam Langleyfcf25832014-12-18 17:42:32 -08001035 /* Check we have a cipher to change to */
1036 if (s->s3->tmp.new_cipher == NULL) {
1037 al = SSL_AD_UNEXPECTED_MESSAGE;
1038 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1039 goto f_err;
1040 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1043 al = SSL_AD_UNEXPECTED_MESSAGE;
1044 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1045 goto f_err;
1046 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001047
Adam Langleyfcf25832014-12-18 17:42:32 -08001048 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001049
Adam Langleyfcf25832014-12-18 17:42:32 -08001050 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001051
Adam Langleyfcf25832014-12-18 17:42:32 -08001052 if (s->msg_callback) {
1053 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1054 s->msg_callback_arg);
1055 }
Adam Langley95c29f32014-06-20 12:00:00 -07001056
Adam Langleyfcf25832014-12-18 17:42:32 -08001057 s->s3->change_cipher_spec = 1;
1058 if (!ssl3_do_change_cipher_spec(s)) {
1059 goto err;
1060 } else {
1061 goto start;
1062 }
1063 }
Adam Langley95c29f32014-06-20 12:00:00 -07001064
Adam Langleyfcf25832014-12-18 17:42:32 -08001065 /* Unexpected handshake message (Client Hello, or protocol violation) */
1066 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1067 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1068 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1069 s->renegotiate = 1;
1070 s->new_session = 1;
1071 }
1072 i = s->handshake_func(s);
1073 if (i < 0) {
1074 return i;
1075 }
1076 if (i == 0) {
1077 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1078 return -1;
1079 }
Adam Langley95c29f32014-06-20 12:00:00 -07001080
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 goto start;
1082 }
1083
David Benjamine820df92015-02-08 16:06:54 -05001084 /* We already handled these. */
1085 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1086 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001087
David Benjamine820df92015-02-08 16:06:54 -05001088 al = SSL_AD_UNEXPECTED_MESSAGE;
1089 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001090
1091f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001092 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001093err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 return -1;
1095}
Adam Langley95c29f32014-06-20 12:00:00 -07001096
Adam Langleyfcf25832014-12-18 17:42:32 -08001097int ssl3_do_change_cipher_spec(SSL *s) {
1098 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 if (s->state & SSL_ST_ACCEPT) {
1101 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1102 } else {
1103 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1104 }
Adam Langley95c29f32014-06-20 12:00:00 -07001105
Adam Langleyfcf25832014-12-18 17:42:32 -08001106 if (s->s3->tmp.key_block == NULL) {
1107 if (s->session == NULL || s->session->master_key_length == 0) {
1108 /* might happen if dtls1_read_bytes() calls this */
1109 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1110 SSL_R_CCS_RECEIVED_EARLY);
1111 return 0;
1112 }
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 s->session->cipher = s->s3->tmp.new_cipher;
1115 if (!s->enc_method->setup_key_block(s)) {
1116 return 0;
1117 }
1118 }
Adam Langley95c29f32014-06-20 12:00:00 -07001119
Adam Langleyfcf25832014-12-18 17:42:32 -08001120 if (!s->enc_method->change_cipher_state(s, i)) {
1121 return 0;
1122 }
Adam Langley95c29f32014-06-20 12:00:00 -07001123
Adam Langleyfcf25832014-12-18 17:42:32 -08001124 return 1;
1125}
Adam Langley95c29f32014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127int ssl3_send_alert(SSL *s, int level, int desc) {
1128 /* Map tls/ssl alert value to correct one */
1129 desc = s->enc_method->alert_value(desc);
1130 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1131 /* SSL 3.0 does not have protocol_version alerts */
1132 desc = SSL_AD_HANDSHAKE_FAILURE;
1133 }
1134 if (desc < 0) {
1135 return -1;
1136 }
Adam Langley95c29f32014-06-20 12:00:00 -07001137
Adam Langleyfcf25832014-12-18 17:42:32 -08001138 /* If a fatal one, remove from cache */
1139 if (level == 2 && s->session != NULL) {
1140 SSL_CTX_remove_session(s->ctx, s->session);
1141 }
Adam Langley95c29f32014-06-20 12:00:00 -07001142
Adam Langleyfcf25832014-12-18 17:42:32 -08001143 s->s3->alert_dispatch = 1;
1144 s->s3->send_alert[0] = level;
1145 s->s3->send_alert[1] = desc;
1146 if (s->s3->wbuf.left == 0) {
1147 /* data is still being written out. */
1148 return s->method->ssl_dispatch_alert(s);
1149 }
Adam Langley95c29f32014-06-20 12:00:00 -07001150
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 /* else data is still being written out, we will get written some time in the
1152 * future */
1153 return -1;
1154}
Adam Langley95c29f32014-06-20 12:00:00 -07001155
Adam Langleyfcf25832014-12-18 17:42:32 -08001156int ssl3_dispatch_alert(SSL *s) {
1157 int i, j;
1158 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 s->s3->alert_dispatch = 0;
David Benjamin9faafda2015-04-04 19:23:10 -04001161 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001162 if (i <= 0) {
1163 s->s3->alert_dispatch = 1;
1164 } else {
1165 /* Alert sent to BIO. If it is important, flush it now. If the message
1166 * does not get sent due to non-blocking IO, we will not worry too much. */
1167 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1168 BIO_flush(s->wbio);
1169 }
Adam Langley95c29f32014-06-20 12:00:00 -07001170
Adam Langleyfcf25832014-12-18 17:42:32 -08001171 if (s->msg_callback) {
1172 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1173 s->msg_callback_arg);
1174 }
1175
1176 if (s->info_callback != NULL) {
1177 cb = s->info_callback;
1178 } else if (s->ctx->info_callback != NULL) {
1179 cb = s->ctx->info_callback;
1180 }
1181
1182 if (cb != NULL) {
1183 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1184 cb(s, SSL_CB_WRITE_ALERT, j);
1185 }
1186 }
1187
1188 return i;
1189}