blob: dcd26c284c1104fa2ec286d477be4aa5437eecfe [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);
Adam Langleyfcf25832014-12-18 17:42:32 -0800320 al = SSL_AD_PROTOCOL_VERSION;
321 goto f_err;
322 }
Adam Langley95c29f32014-06-20 12:00:00 -0700323
Adam Langleyfcf25832014-12-18 17:42:32 -0800324 if ((version >> 8) != SSL3_VERSION_MAJOR) {
325 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
326 goto err;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
David Benjamin4a3f0732015-04-05 02:21:33 -0400329 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 al = SSL_AD_RECORD_OVERFLOW;
David Benjamin4a3f0732015-04-05 02:21:33 -0400331 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
Adam Langleyfcf25832014-12-18 17:42:32 -0800332 goto f_err;
333 }
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langleyfcf25832014-12-18 17:42:32 -0800335 /* now s->rstate == SSL_ST_READ_BODY */
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
Adam Langleyfcf25832014-12-18 17:42:32 -0800338 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langleyfcf25832014-12-18 17:42:32 -0800340 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
341 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
342 i = rr->length;
343 n = ssl3_read_n(s, i, i, 1);
344 if (n <= 0) {
345 /* Error or non-blocking IO. Now |n| == |rr->length|, and
346 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
347 return n;
348 }
349 }
Adam Langley95c29f32014-06-20 12:00:00 -0700350
Adam Langleyfcf25832014-12-18 17:42:32 -0800351 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langleyfcf25832014-12-18 17:42:32 -0800353 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and
354 * we have that many bytes in s->packet. */
355 rr->input = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700356
Adam Langleyfcf25832014-12-18 17:42:32 -0800357 /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at
358 * |rr->length| bytes, which need to be copied into |rr->data| by decryption.
359 * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be
360 * pointed at the new buffer. */
Adam Langley95c29f32014-06-20 12:00:00 -0700361
Adam Langleyfcf25832014-12-18 17:42:32 -0800362 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
363 * rr->length bytes of encrypted compressed stuff. */
Adam Langley95c29f32014-06-20 12:00:00 -0700364
Adam Langleyfcf25832014-12-18 17:42:32 -0800365 /* decrypt in place in 'rr->input' */
366 rr->data = rr->input;
Adam Langley95c29f32014-06-20 12:00:00 -0700367
David Benjamin1e52eca2015-01-22 15:33:51 -0500368 if (!s->enc_method->enc(s, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800369 al = SSL_AD_BAD_RECORD_MAC;
370 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
371 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
372 goto f_err;
373 }
Adam Langley95c29f32014-06-20 12:00:00 -0700374
Adam Langleyfcf25832014-12-18 17:42:32 -0800375 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
376 al = SSL_AD_RECORD_OVERFLOW;
377 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
378 goto f_err;
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langleyfcf25832014-12-18 17:42:32 -0800381 rr->off = 0;
382 /* So at this point the following is true:
383 * ssl->s3->rrec.type is the type of record;
384 * ssl->s3->rrec.length is the number of bytes in the record;
385 * ssl->s3->rrec.off is the offset to first valid byte;
386 * ssl->s3->rrec.data is where to take bytes from (increment after use). */
Adam Langley95c29f32014-06-20 12:00:00 -0700387
Adam Langleyfcf25832014-12-18 17:42:32 -0800388 /* we have pulled in a full packet so zero things */
389 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700390
Adam Langleyfcf25832014-12-18 17:42:32 -0800391 /* just read a 0 length packet */
392 if (rr->length == 0) {
393 empty_record_count++;
394 if (empty_record_count > MAX_EMPTY_RECORDS) {
395 al = SSL_AD_UNEXPECTED_MESSAGE;
396 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
397 goto f_err;
398 }
399 goto again;
400 }
Adam Langley95c29f32014-06-20 12:00:00 -0700401
Adam Langleyfcf25832014-12-18 17:42:32 -0800402 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700403
404f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800405 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700406err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800407 return ret;
408}
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410/* Call this to write data in records of type |type|. It will return <= 0 if
411 * not all data has been sent or non-blocking IO. */
412int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
413 const uint8_t *buf = buf_;
414 unsigned int tot, n, nw;
415 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700416
Adam Langleyfcf25832014-12-18 17:42:32 -0800417 s->rwstate = SSL_NOTHING;
418 assert(s->s3->wnum <= INT_MAX);
419 tot = s->s3->wnum;
420 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700421
David Benjamined7c4752015-02-16 19:16:46 -0500422 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800423 i = s->handshake_func(s);
424 if (i < 0) {
425 return i;
426 }
427 if (i == 0) {
428 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
429 return -1;
430 }
431 }
Adam Langley95c29f32014-06-20 12:00:00 -0700432
Adam Langleyfcf25832014-12-18 17:42:32 -0800433 /* Ensure that if we end up with a smaller value of data to write out than
434 * the the original len from a write which didn't complete for non-blocking
435 * I/O and also somehow ended up avoiding the check for this in
436 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
437 * end up with (len-tot) as a large number that will then promptly send
438 * beyond the end of the users buffer ... so we trap and report the error in
439 * a way the user will notice. */
440 if (len < 0 || (size_t)len < tot) {
441 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
442 return -1;
443 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700444
David Benjamin9faafda2015-04-04 19:23:10 -0400445 int record_split_done = 0;
Adam Langleyfcf25832014-12-18 17:42:32 -0800446 n = (len - tot);
447 for (;;) {
448 /* max contains the maximum number of bytes that we can put into a
449 * record. */
450 unsigned max = s->max_send_fragment;
451 /* fragment is true if do_ssl3_write should send the first byte in its own
452 * record in order to randomise a CBC IV. */
453 int fragment = 0;
David Benjamin9faafda2015-04-04 19:23:10 -0400454 if (!record_split_done && s->s3->need_record_splitting &&
455 type == SSL3_RT_APPLICATION_DATA) {
456 /* Only the the first record per write call needs to be split. The
457 * remaining plaintext was determined before the IV was randomized. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800458 fragment = 1;
David Benjamin9faafda2015-04-04 19:23:10 -0400459 record_split_done = 1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800460 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800461 if (n > max) {
462 nw = max;
463 } else {
464 nw = n;
465 }
Adam Langley95c29f32014-06-20 12:00:00 -0700466
David Benjamin9faafda2015-04-04 19:23:10 -0400467 i = do_ssl3_write(s, type, &buf[tot], nw, fragment);
Adam Langleyfcf25832014-12-18 17:42:32 -0800468 if (i <= 0) {
469 s->s3->wnum = tot;
Adam Langleyfcf25832014-12-18 17:42:32 -0800470 return i;
471 }
Adam Langley95c29f32014-06-20 12:00:00 -0700472
Adam Langleyfcf25832014-12-18 17:42:32 -0800473 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
474 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800475 return tot + i;
476 }
Adam Langley95c29f32014-06-20 12:00:00 -0700477
Adam Langleyfcf25832014-12-18 17:42:32 -0800478 n -= i;
479 tot += i;
480 }
481}
Adam Langley95c29f32014-06-20 12:00:00 -0700482
David Benjamin9faafda2015-04-04 19:23:10 -0400483/* ssl3_seal_record seals a new record of type |type| and plaintext |in| and
484 * writes it to |out|. At most |max_out| bytes will be written. It returns one
485 * on success and zero on error. On success, |s->s3->wrec| is updated to include
486 * the new record. */
487static int ssl3_seal_record(SSL *s, uint8_t *out, size_t *out_len,
488 size_t max_out, uint8_t type, const uint8_t *in,
489 size_t in_len) {
490 if (max_out < SSL3_RT_HEADER_LENGTH) {
491 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
492 return 0;
493 }
494
495 out[0] = type;
496
497 /* Some servers hang if initial ClientHello is larger than 256 bytes and
498 * record version number > TLS 1.0. */
499 if (!s->s3->have_version && s->version > SSL3_VERSION) {
500 out[1] = TLS1_VERSION >> 8;
501 out[2] = TLS1_VERSION & 0xff;
502 } else {
503 out[1] = s->version >> 8;
504 out[2] = s->version & 0xff;
505 }
506
507 size_t explicit_nonce_len = 0;
508 if (s->aead_write_ctx != NULL &&
509 s->aead_write_ctx->variable_nonce_included_in_record) {
510 explicit_nonce_len = s->aead_write_ctx->variable_nonce_len;
511 }
512 size_t max_overhead = 0;
513 if (s->aead_write_ctx != NULL) {
514 max_overhead = s->aead_write_ctx->tag_len;
515 }
516
517 /* Assemble the input for |s->enc_method->enc|. The input is the plaintext
518 * with |explicit_nonce_len| bytes of space prepended for the explicit
519 * nonce. The input is copied into |out| and then encrypted in-place to take
520 * advantage of alignment.
521 *
522 * TODO(davidben): |tls1_enc| should accept its inputs and outputs directly
523 * rather than looking up in |wrec| and friends. The |max_overhead| bounds
524 * check would also be unnecessary if |max_out| were passed down. */
525 SSL3_RECORD *wr = &s->s3->wrec;
526 size_t plaintext_len = in_len + explicit_nonce_len;
527 if (plaintext_len < in_len || plaintext_len > INT_MAX ||
528 plaintext_len + max_overhead < plaintext_len) {
529 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
530 return 0;
531 }
532 if (max_out - SSL3_RT_HEADER_LENGTH < plaintext_len + max_overhead) {
533 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, SSL_R_BUFFER_TOO_SMALL);
534 return 0;
535 }
536 wr->type = type;
537 wr->input = out + SSL3_RT_HEADER_LENGTH;
538 wr->data = wr->input;
539 wr->length = plaintext_len;
540 memcpy(wr->input + explicit_nonce_len, in, in_len);
541
542 if (!s->enc_method->enc(s, 1)) {
543 return 0;
544 }
545
546 /* |wr->length| has now been set to the ciphertext length. */
547 if (wr->length >= 1 << 16) {
548 OPENSSL_PUT_ERROR(SSL, ssl3_seal_record, ERR_R_OVERFLOW);
549 return 0;
550 }
551 out[3] = wr->length >> 8;
552 out[4] = wr->length & 0xff;
553 *out_len = SSL3_RT_HEADER_LENGTH + (size_t)wr->length;
554
555 if (s->msg_callback) {
556 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out, SSL3_RT_HEADER_LENGTH,
557 s, s->msg_callback_arg);
558 }
559
560 return 1;
561}
562
Adam Langleyd493d522014-06-20 12:00:00 -0700563/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
564 * then it splits the record into a one byte record and a record with the rest
David Benjamin9faafda2015-04-04 19:23:10 -0400565 * of the data in order to randomise a CBC IV. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800566static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
David Benjamin9faafda2015-04-04 19:23:10 -0400567 char fragment) {
568 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley95c29f32014-06-20 12:00:00 -0700569
Adam Langleyfcf25832014-12-18 17:42:32 -0800570 /* first check if there is a SSL3_BUFFER still being written out. This will
571 * happen with non blocking IO */
572 if (wb->left != 0) {
573 return ssl3_write_pending(s, type, buf, len);
574 }
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 /* If we have an alert to send, lets send it */
577 if (s->s3->alert_dispatch) {
David Benjamin9faafda2015-04-04 19:23:10 -0400578 int ret = s->method->ssl_dispatch_alert(s);
579 if (ret <= 0) {
580 return ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800581 }
582 /* if it went, fall through and send more stuff */
583 }
Adam Langley95c29f32014-06-20 12:00:00 -0700584
Adam Langleyfcf25832014-12-18 17:42:32 -0800585 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
586 return -1;
587 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 if (len == 0) {
590 return 0;
591 }
David Benjamin9faafda2015-04-04 19:23:10 -0400592 if (len == 1) {
593 /* No sense in fragmenting a one-byte record. */
594 fragment = 0;
595 }
Adam Langley95c29f32014-06-20 12:00:00 -0700596
David Benjamin9faafda2015-04-04 19:23:10 -0400597 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
598 uintptr_t align;
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 if (fragment) {
David Benjamin9faafda2015-04-04 19:23:10 -0400600 /* Only CBC-mode ciphers require fragmenting. CBC-mode ciphertext is a
601 * multiple of the block size which we may assume is aligned. Thus we only
602 * need to account for a second copy of the record header. */
603 align = (uintptr_t)wb->buf + 2 * SSL3_RT_HEADER_LENGTH;
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 } else {
David Benjamin9faafda2015-04-04 19:23:10 -0400605 align = (uintptr_t)wb->buf + SSL3_RT_HEADER_LENGTH;
606 }
607 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
608 uint8_t *out = wb->buf + align;
609 wb->offset = align;
610 size_t max_out = wb->len - wb->offset;
611
612 const uint8_t *orig_buf = buf;
613 unsigned int orig_len = len;
614 size_t fragment_len = 0;
615 if (fragment) {
616 /* Write the first byte in its own record as a countermeasure against
617 * known-IV weaknesses in CBC ciphersuites. (See
618 * http://www.openssl.org/~bodo/tls-cbc.txt.) */
619 if (!ssl3_seal_record(s, out, &fragment_len, max_out, type, buf, 1)) {
620 return -1;
621 }
622 out += fragment_len;
623 max_out -= fragment_len;
624 buf++;
625 len--;
Adam Langleyfcf25832014-12-18 17:42:32 -0800626 }
Adam Langley95c29f32014-06-20 12:00:00 -0700627
David Benjamin9faafda2015-04-04 19:23:10 -0400628 assert((((uintptr_t)out + SSL3_RT_HEADER_LENGTH) & (SSL3_ALIGN_PAYLOAD - 1))
629 == 0);
630 size_t ciphertext_len;
631 if (!ssl3_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
632 return -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800633 }
David Benjamin9faafda2015-04-04 19:23:10 -0400634 ciphertext_len += fragment_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700635
Adam Langleyfcf25832014-12-18 17:42:32 -0800636 /* now let's set up wb */
David Benjamin9faafda2015-04-04 19:23:10 -0400637 wb->left = ciphertext_len;
Adam Langley95c29f32014-06-20 12:00:00 -0700638
Adam Langleyfcf25832014-12-18 17:42:32 -0800639 /* memorize arguments so that ssl3_write_pending can detect bad write retries
640 * later */
David Benjamin9faafda2015-04-04 19:23:10 -0400641 s->s3->wpend_tot = orig_len;
642 s->s3->wpend_buf = orig_buf;
Adam Langleyfcf25832014-12-18 17:42:32 -0800643 s->s3->wpend_type = type;
David Benjamin9faafda2015-04-04 19:23:10 -0400644 s->s3->wpend_ret = orig_len;
Adam Langleyfcf25832014-12-18 17:42:32 -0800645
646 /* we now just need to write the buffer */
David Benjamin9faafda2015-04-04 19:23:10 -0400647 return ssl3_write_pending(s, type, orig_buf, orig_len);
Adam Langleyfcf25832014-12-18 17:42:32 -0800648}
Adam Langley95c29f32014-06-20 12:00:00 -0700649
650/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800651int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
652 int i;
653 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700654
Adam Langleyfcf25832014-12-18 17:42:32 -0800655 if (s->s3->wpend_tot > (int)len ||
656 (s->s3->wpend_buf != buf &&
657 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
658 s->s3->wpend_type != type) {
659 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
660 return -1;
661 }
Adam Langley95c29f32014-06-20 12:00:00 -0700662
Adam Langleyfcf25832014-12-18 17:42:32 -0800663 for (;;) {
664 ERR_clear_system_error();
665 if (s->wbio != NULL) {
666 s->rwstate = SSL_WRITING;
667 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
668 (unsigned int)wb->left);
669 } else {
670 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
671 i = -1;
672 }
673 if (i == wb->left) {
674 wb->left = 0;
675 wb->offset += i;
676 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) {
677 ssl3_release_write_buffer(s);
678 }
679 s->rwstate = SSL_NOTHING;
680 return s->s3->wpend_ret;
681 } else if (i <= 0) {
682 if (SSL_IS_DTLS(s)) {
683 /* For DTLS, just drop it. That's kind of the whole
684 point in using a datagram service */
685 wb->left = 0;
686 }
687 return i;
688 }
689 wb->offset += i;
690 wb->left -= i;
691 }
692}
Adam Langley95c29f32014-06-20 12:00:00 -0700693
David Benjamin86271ee2014-07-21 16:14:03 -0400694/* ssl3_expect_change_cipher_spec informs the record layer that a
695 * ChangeCipherSpec record is required at this point. If a Handshake record is
696 * received before ChangeCipherSpec, the connection will fail. Moreover, if
697 * there are unprocessed handshake bytes, the handshake will also fail and the
698 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800699int ssl3_expect_change_cipher_spec(SSL *s) {
700 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
701 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
702 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
703 return 0;
704 }
705
706 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
707 return 1;
708}
David Benjamin86271ee2014-07-21 16:14:03 -0400709
Adam Langley95c29f32014-06-20 12:00:00 -0700710/* Return up to 'len' payload bytes received in 'type' records.
711 * 'type' is one of the following:
712 *
713 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
714 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
715 * - 0 (during a shutdown, no data has to be returned)
716 *
717 * If we don't have stored data to work from, read a SSL/TLS record first
718 * (possibly multiple records if we still don't have anything to return).
719 *
720 * This function must handle any surprises the peer may have for us, such as
721 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
722 * a surprise, but handled as if it were), or renegotiation requests.
723 * Also if record payloads contain fragments too small to process, we store
724 * them until there is enough for the respective protocol (the record protocol
725 * may use arbitrary fragmentation and even interleaving):
726 * Change cipher spec protocol
727 * just 1 byte needed, no need for keeping anything stored
728 * Alert protocol
729 * 2 bytes needed (AlertLevel, AlertDescription)
730 * Handshake protocol
731 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
732 * to detect unexpected Client Hello and Hello Request messages
733 * here, anything else is handled by higher layers
734 * Application data protocol
735 * none of our business
736 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800737int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500738 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800739 unsigned int n;
740 SSL3_RECORD *rr;
741 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700742
Adam Langleyfcf25832014-12-18 17:42:32 -0800743 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
744 (peek && type != SSL3_RT_APPLICATION_DATA)) {
745 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
746 return -1;
747 }
Adam Langley95c29f32014-06-20 12:00:00 -0700748
Adam Langleyfcf25832014-12-18 17:42:32 -0800749 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
750 /* (partially) satisfy request from storage */
751 uint8_t *src = s->s3->handshake_fragment;
752 uint8_t *dst = buf;
753 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700754
Adam Langleyfcf25832014-12-18 17:42:32 -0800755 /* peek == 0 */
756 n = 0;
757 while (len > 0 && s->s3->handshake_fragment_len > 0) {
758 *dst++ = *src++;
759 len--;
760 s->s3->handshake_fragment_len--;
761 n++;
762 }
763 /* move any remaining fragment bytes: */
764 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
765 s->s3->handshake_fragment[k] = *src++;
766 }
767 return n;
768 }
Adam Langley95c29f32014-06-20 12:00:00 -0700769
Adam Langleyfcf25832014-12-18 17:42:32 -0800770 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700771
David Benjamin931ab342015-02-08 19:46:57 -0500772 /* This may require multiple iterations. False Start will cause
773 * |s->handshake_func| to signal success one step early, but the handshake
774 * must be completely finished before other modes are accepted.
775 *
776 * TODO(davidben): Move this check up to a higher level. */
777 while (!s->in_handshake && SSL_in_init(s)) {
778 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 i = s->handshake_func(s);
780 if (i < 0) {
781 return i;
782 }
783 if (i == 0) {
784 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
785 return -1;
786 }
787 }
788
David Benjamine0e7d0d2015-02-08 19:33:25 -0500789 if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) {
790 /* TODO(davidben): Is this redundant with the calls in the handshake? */
791 return -1;
792 }
793
Adam Langley95c29f32014-06-20 12:00:00 -0700794start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800795 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700796
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 /* s->s3->rrec.type - is the type of record
798 * s->s3->rrec.data - data
799 * s->s3->rrec.off - offset into 'data' for next read
800 * s->s3->rrec.length - number of bytes. */
801 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700802
Adam Langleyfcf25832014-12-18 17:42:32 -0800803 /* get new packet if necessary */
804 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
805 ret = ssl3_get_record(s);
806 if (ret <= 0) {
807 return ret;
808 }
809 }
Adam Langley95c29f32014-06-20 12:00:00 -0700810
Adam Langleyfcf25832014-12-18 17:42:32 -0800811 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700812
David Benjamindc3da932015-03-12 15:09:02 -0400813 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
814 * ssl3_get_finished. */
815 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
816 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800817 al = SSL_AD_UNEXPECTED_MESSAGE;
818 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
819 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
820 goto f_err;
821 }
Adam Langley95c29f32014-06-20 12:00:00 -0700822
Adam Langleyfcf25832014-12-18 17:42:32 -0800823 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
824 * Handshake record. */
825 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
826 al = SSL_AD_UNEXPECTED_MESSAGE;
827 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
828 goto f_err;
829 }
David Benjamin86271ee2014-07-21 16:14:03 -0400830
Adam Langleyfcf25832014-12-18 17:42:32 -0800831 /* If the other end has shut down, throw anything we read away (even in
832 * 'peek' mode) */
833 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
834 rr->length = 0;
835 s->rwstate = SSL_NOTHING;
836 return 0;
837 }
Adam Langley95c29f32014-06-20 12:00:00 -0700838
Adam Langleyfcf25832014-12-18 17:42:32 -0800839 if (type == rr->type) {
840 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
841 /* make sure that we are not getting application data when we are doing a
842 * handshake for the first time */
843 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500844 s->aead_read_ctx == NULL) {
845 /* TODO(davidben): Is this check redundant with the handshake_func
846 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800847 al = SSL_AD_UNEXPECTED_MESSAGE;
848 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
849 goto f_err;
850 }
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langleyfcf25832014-12-18 17:42:32 -0800852 if (len <= 0) {
853 return len;
854 }
Adam Langley95c29f32014-06-20 12:00:00 -0700855
Adam Langleyfcf25832014-12-18 17:42:32 -0800856 if ((unsigned int)len > rr->length) {
857 n = rr->length;
858 } else {
859 n = (unsigned int)len;
860 }
Adam Langley95c29f32014-06-20 12:00:00 -0700861
Adam Langleyfcf25832014-12-18 17:42:32 -0800862 memcpy(buf, &(rr->data[rr->off]), n);
863 if (!peek) {
864 rr->length -= n;
865 rr->off += n;
866 if (rr->length == 0) {
867 s->rstate = SSL_ST_READ_HEADER;
868 rr->off = 0;
869 if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) {
870 ssl3_release_read_buffer(s);
871 }
872 }
873 }
874
875 return n;
876 }
Adam Langley95c29f32014-06-20 12:00:00 -0700877
878
Adam Langleyfcf25832014-12-18 17:42:32 -0800879 /* If we get here, then type != rr->type; if we have a handshake message,
880 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700881
Adam Langleyfcf25832014-12-18 17:42:32 -0800882 /* In case of record types for which we have 'fragment' storage, fill that so
883 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400884
Adam Langleyfcf25832014-12-18 17:42:32 -0800885 if (rr->type == SSL3_RT_HANDSHAKE) {
886 const size_t size = sizeof(s->s3->handshake_fragment);
887 const size_t avail = size - s->s3->handshake_fragment_len;
888 const size_t todo = (rr->length < avail) ? rr->length : avail;
889 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
890 &rr->data[rr->off], todo);
891 rr->off += todo;
892 rr->length -= todo;
893 s->s3->handshake_fragment_len += todo;
894 if (s->s3->handshake_fragment_len < size) {
895 goto start; /* fragment was too small */
896 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800897 }
Adam Langley95c29f32014-06-20 12:00:00 -0700898
Adam Langleyfcf25832014-12-18 17:42:32 -0800899 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
900 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700901
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 /* If we are a client, check for an incoming 'Hello Request': */
903 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
904 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
905 s->session != NULL && s->session->cipher != NULL) {
906 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700907
Adam Langleyfcf25832014-12-18 17:42:32 -0800908 if (s->s3->handshake_fragment[1] != 0 ||
909 s->s3->handshake_fragment[2] != 0 ||
910 s->s3->handshake_fragment[3] != 0) {
911 al = SSL_AD_DECODE_ERROR;
912 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
913 goto f_err;
914 }
Adam Langley95c29f32014-06-20 12:00:00 -0700915
Adam Langleyfcf25832014-12-18 17:42:32 -0800916 if (s->msg_callback) {
917 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
918 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
922 ssl3_renegotiate(s);
923 if (ssl3_renegotiate_check(s)) {
924 i = s->handshake_func(s);
925 if (i < 0) {
926 return i;
927 }
928 if (i == 0) {
929 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
930 return -1;
931 }
932 }
933 }
934 /* we either finished a handshake or ignored the request, now try again to
935 * obtain the (application) data we were asked for */
936 goto start;
937 }
David Benjaminb4188f02014-11-01 03:43:48 -0400938
David Benjamin86058a22015-02-22 13:07:21 -0500939 /* If an alert record, process one alert out of the record. Note that we allow
940 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800941 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500942 /* Alerts may not be fragmented. */
943 if (rr->length < 2) {
944 al = SSL_AD_DECODE_ERROR;
945 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
946 goto f_err;
947 }
Adam Langley95c29f32014-06-20 12:00:00 -0700948
Adam Langleyfcf25832014-12-18 17:42:32 -0800949 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500950 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800951 s->msg_callback_arg);
952 }
David Benjamin86058a22015-02-22 13:07:21 -0500953 const uint8_t alert_level = rr->data[rr->off++];
954 const uint8_t alert_descr = rr->data[rr->off++];
955 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700956
Adam Langleyfcf25832014-12-18 17:42:32 -0800957 if (s->info_callback != NULL) {
958 cb = s->info_callback;
959 } else if (s->ctx->info_callback != NULL) {
960 cb = s->ctx->info_callback;
961 }
Adam Langley95c29f32014-06-20 12:00:00 -0700962
Adam Langleyfcf25832014-12-18 17:42:32 -0800963 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500964 uint16_t alert = (alert_level << 8) | alert_descr;
965 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
David Benjamin86058a22015-02-22 13:07:21 -0500968 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800969 s->s3->warn_alert = alert_descr;
970 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
971 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
972 return 0;
973 }
Adam Langley95c29f32014-06-20 12:00:00 -0700974
Adam Langleyfcf25832014-12-18 17:42:32 -0800975 /* This is a warning but we receive it if we requested renegotiation and
976 * the peer denied it. Terminate with a fatal alert because if
977 * application tried to renegotiatie it presumably had a good reason and
978 * expects it to succeed.
979 *
980 * In future we might have a renegotiation where we don't care if the
981 * peer refused it where we carry on. */
982 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
983 al = SSL_AD_HANDSHAKE_FAILURE;
984 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
985 goto f_err;
986 }
David Benjamin86058a22015-02-22 13:07:21 -0500987 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800988 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 s->rwstate = SSL_NOTHING;
991 s->s3->fatal_alert = alert_descr;
992 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
993 SSL_AD_REASON_OFFSET + alert_descr);
994 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
995 ERR_add_error_data(2, "SSL alert number ", tmp);
996 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
997 SSL_CTX_remove_session(s->ctx, s->session);
998 return 0;
999 } else {
1000 al = SSL_AD_ILLEGAL_PARAMETER;
1001 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
1002 goto f_err;
1003 }
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 goto start;
1006 }
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1009 /* but we have not received a shutdown */
1010 s->rwstate = SSL_NOTHING;
1011 rr->length = 0;
1012 return 0;
1013 }
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1016 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1017 * record payload has to look like */
1018 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1019 al = SSL_AD_ILLEGAL_PARAMETER;
1020 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1021 goto f_err;
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 /* Check we have a cipher to change to */
1025 if (s->s3->tmp.new_cipher == NULL) {
1026 al = SSL_AD_UNEXPECTED_MESSAGE;
1027 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1028 goto f_err;
1029 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1032 al = SSL_AD_UNEXPECTED_MESSAGE;
1033 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1034 goto f_err;
1035 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (s->msg_callback) {
1042 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1043 s->msg_callback_arg);
1044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 s->s3->change_cipher_spec = 1;
1047 if (!ssl3_do_change_cipher_spec(s)) {
1048 goto err;
1049 } else {
1050 goto start;
1051 }
1052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 /* Unexpected handshake message (Client Hello, or protocol violation) */
1055 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1056 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1057 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1058 s->renegotiate = 1;
1059 s->new_session = 1;
1060 }
1061 i = s->handshake_func(s);
1062 if (i < 0) {
1063 return i;
1064 }
1065 if (i == 0) {
1066 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1067 return -1;
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 goto start;
1071 }
1072
David Benjamine820df92015-02-08 16:06:54 -05001073 /* We already handled these. */
1074 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1075 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001076
David Benjamine820df92015-02-08 16:06:54 -05001077 al = SSL_AD_UNEXPECTED_MESSAGE;
1078 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001079
1080f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001082err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 return -1;
1084}
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086int ssl3_do_change_cipher_spec(SSL *s) {
1087 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 if (s->state & SSL_ST_ACCEPT) {
1090 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1091 } else {
1092 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 if (s->s3->tmp.key_block == NULL) {
1096 if (s->session == NULL || s->session->master_key_length == 0) {
1097 /* might happen if dtls1_read_bytes() calls this */
1098 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1099 SSL_R_CCS_RECEIVED_EARLY);
1100 return 0;
1101 }
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 s->session->cipher = s->s3->tmp.new_cipher;
1104 if (!s->enc_method->setup_key_block(s)) {
1105 return 0;
1106 }
1107 }
Adam Langley95c29f32014-06-20 12:00:00 -07001108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 if (!s->enc_method->change_cipher_state(s, i)) {
1110 return 0;
1111 }
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 return 1;
1114}
Adam Langley95c29f32014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116int ssl3_send_alert(SSL *s, int level, int desc) {
1117 /* Map tls/ssl alert value to correct one */
1118 desc = s->enc_method->alert_value(desc);
1119 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1120 /* SSL 3.0 does not have protocol_version alerts */
1121 desc = SSL_AD_HANDSHAKE_FAILURE;
1122 }
1123 if (desc < 0) {
1124 return -1;
1125 }
Adam Langley95c29f32014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 /* If a fatal one, remove from cache */
1128 if (level == 2 && s->session != NULL) {
1129 SSL_CTX_remove_session(s->ctx, s->session);
1130 }
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 s->s3->alert_dispatch = 1;
1133 s->s3->send_alert[0] = level;
1134 s->s3->send_alert[1] = desc;
1135 if (s->s3->wbuf.left == 0) {
1136 /* data is still being written out. */
1137 return s->method->ssl_dispatch_alert(s);
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 /* else data is still being written out, we will get written some time in the
1141 * future */
1142 return -1;
1143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145int ssl3_dispatch_alert(SSL *s) {
1146 int i, j;
1147 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 s->s3->alert_dispatch = 0;
David Benjamin9faafda2015-04-04 19:23:10 -04001150 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0);
Adam Langleyfcf25832014-12-18 17:42:32 -08001151 if (i <= 0) {
1152 s->s3->alert_dispatch = 1;
1153 } else {
1154 /* Alert sent to BIO. If it is important, flush it now. If the message
1155 * does not get sent due to non-blocking IO, we will not worry too much. */
1156 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1157 BIO_flush(s->wbio);
1158 }
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (s->msg_callback) {
1161 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1162 s->msg_callback_arg);
1163 }
1164
1165 if (s->info_callback != NULL) {
1166 cb = s->info_callback;
1167 } else if (s->ctx->info_callback != NULL) {
1168 cb = s->ctx->info_callback;
1169 }
1170
1171 if (cb != NULL) {
1172 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1173 cb(s, SSL_CB_WRITE_ALERT, j);
1174 }
1175 }
1176
1177 return i;
1178}