blob: 1bf71413e15c651e75cdd2cf5c3eff46e299c5b0 [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,
124 char fragment, char is_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
Adam Langleyfcf25832014-12-18 17:42:32 -0800456 n = (len - tot);
457 for (;;) {
458 /* max contains the maximum number of bytes that we can put into a
459 * record. */
460 unsigned max = s->max_send_fragment;
461 /* fragment is true if do_ssl3_write should send the first byte in its own
462 * record in order to randomise a CBC IV. */
463 int fragment = 0;
Adam Langleyd493d522014-06-20 12:00:00 -0700464
Adam Langleyfcf25832014-12-18 17:42:32 -0800465 if (n > 1 && s->s3->need_record_splitting &&
466 type == SSL3_RT_APPLICATION_DATA && !s->s3->record_split_done) {
467 fragment = 1;
468 /* record_split_done records that the splitting has been done in case we
469 * hit an SSL_WANT_WRITE condition. In that case, we don't need to do the
470 * split again. */
471 s->s3->record_split_done = 1;
472 }
Adam Langleyd493d522014-06-20 12:00:00 -0700473
Adam Langleyfcf25832014-12-18 17:42:32 -0800474 if (n > max) {
475 nw = max;
476 } else {
477 nw = n;
478 }
Adam Langley95c29f32014-06-20 12:00:00 -0700479
Adam Langleyfcf25832014-12-18 17:42:32 -0800480 i = do_ssl3_write(s, type, &(buf[tot]), nw, fragment, 0);
481 if (i <= 0) {
482 s->s3->wnum = tot;
483 s->s3->record_split_done = 0;
484 return i;
485 }
Adam Langley95c29f32014-06-20 12:00:00 -0700486
Adam Langleyfcf25832014-12-18 17:42:32 -0800487 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
488 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
489 /* next chunk of data should get another prepended, one-byte fragment in
490 * ciphersuites with known-IV weakness. */
491 s->s3->record_split_done = 0;
492 return tot + i;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langleyfcf25832014-12-18 17:42:32 -0800495 n -= i;
496 tot += i;
497 }
498}
Adam Langley95c29f32014-06-20 12:00:00 -0700499
Adam Langleyd493d522014-06-20 12:00:00 -0700500/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
501 * then it splits the record into a one byte record and a record with the rest
502 * of the data in order to randomise a CBC IV. If |is_fragment| is true then
503 * this call resulted from do_ssl3_write calling itself in order to create that
504 * one byte fragment. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800505static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
506 char fragment, char is_fragment) {
507 uint8_t *p, *plen;
David Benjaminb8a56f12014-12-23 11:41:02 -0500508 int i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800509 int prefix_len = 0;
510 int eivlen = 0;
511 long align = 0;
512 SSL3_RECORD *wr;
513 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langleyfcf25832014-12-18 17:42:32 -0800515 /* first check if there is a SSL3_BUFFER still being written out. This will
516 * happen with non blocking IO */
517 if (wb->left != 0) {
518 return ssl3_write_pending(s, type, buf, len);
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langleyfcf25832014-12-18 17:42:32 -0800521 /* If we have an alert to send, lets send it */
522 if (s->s3->alert_dispatch) {
523 i = s->method->ssl_dispatch_alert(s);
524 if (i <= 0) {
525 return i;
526 }
527 /* if it went, fall through and send more stuff */
528 }
Adam Langley95c29f32014-06-20 12:00:00 -0700529
Adam Langleyfcf25832014-12-18 17:42:32 -0800530 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
531 return -1;
532 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700533
Adam Langleyfcf25832014-12-18 17:42:32 -0800534 if (len == 0) {
535 return 0;
536 }
Adam Langley95c29f32014-06-20 12:00:00 -0700537
Adam Langleyfcf25832014-12-18 17:42:32 -0800538 wr = &s->s3->wrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700539
Adam Langleyfcf25832014-12-18 17:42:32 -0800540 if (fragment) {
541 /* countermeasure against known-IV weakness in CBC ciphersuites (see
542 * http://www.openssl.org/~bodo/tls-cbc.txt) */
543 prefix_len = do_ssl3_write(s, type, buf, 1 /* length */, 0 /* fragment */,
544 1 /* is_fragment */);
545 if (prefix_len <= 0) {
546 goto err;
547 }
Adam Langley95c29f32014-06-20 12:00:00 -0700548
Adam Langleyfcf25832014-12-18 17:42:32 -0800549 if (prefix_len >
550 (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
551 /* insufficient space */
552 OPENSSL_PUT_ERROR(SSL, do_ssl3_write, ERR_R_INTERNAL_ERROR);
553 goto err;
554 }
555 }
Adam Langley95c29f32014-06-20 12:00:00 -0700556
Adam Langleyfcf25832014-12-18 17:42:32 -0800557 if (is_fragment) {
558 /* The extra fragment would be couple of cipher blocks, and that will be a
559 * multiple of SSL3_ALIGN_PAYLOAD. So, if we want to align the real
560 * payload, we can just pretend that we have two headers and a byte. */
561 align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH + 1;
562 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
563 p = wb->buf + align;
564 wb->offset = align;
565 } else if (prefix_len) {
566 p = wb->buf + wb->offset + prefix_len;
567 } else {
568 align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
569 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
570 p = wb->buf + align;
571 wb->offset = align;
572 }
Adam Langley95c29f32014-06-20 12:00:00 -0700573
Adam Langleyfcf25832014-12-18 17:42:32 -0800574 /* write the header */
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 *(p++) = type & 0xff;
577 wr->type = type;
Adam Langley95c29f32014-06-20 12:00:00 -0700578
Adam Langleyfcf25832014-12-18 17:42:32 -0800579 /* Some servers hang if initial ClientHello is larger than 256 bytes and
580 * record version number > TLS 1.0. */
581 if (!s->s3->have_version && s->version > SSL3_VERSION) {
582 *(p++) = TLS1_VERSION >> 8;
583 *(p++) = TLS1_VERSION & 0xff;
584 } else {
585 *(p++) = s->version >> 8;
586 *(p++) = s->version & 0xff;
587 }
Adam Langley95c29f32014-06-20 12:00:00 -0700588
Adam Langleyfcf25832014-12-18 17:42:32 -0800589 /* field where we are to write out packet length */
590 plen = p;
591 p += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700592
David Benjaminb8a56f12014-12-23 11:41:02 -0500593 /* Leave room for the variable nonce for AEADs which specify it explicitly. */
594 if (s->aead_write_ctx != NULL &&
595 s->aead_write_ctx->variable_nonce_included_in_record) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 eivlen = s->aead_write_ctx->variable_nonce_len;
597 }
Adam Langley95c29f32014-06-20 12:00:00 -0700598
Adam Langleyfcf25832014-12-18 17:42:32 -0800599 /* lets setup the record stuff. */
600 wr->data = p + eivlen;
601 wr->length = (int)(len - (fragment != 0));
602 wr->input = (uint8_t *)buf + (fragment != 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700603
Adam Langleyfcf25832014-12-18 17:42:32 -0800604 /* we now 'read' from wr->input, wr->length bytes into wr->data */
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 memcpy(wr->data, wr->input, wr->length);
607 wr->input = wr->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 /* we should still have the output to wr->data and the input from wr->input.
610 * Length should be wr->length. wr->data still points in the wb->buf */
Adam Langley95c29f32014-06-20 12:00:00 -0700611
Adam Langleyfcf25832014-12-18 17:42:32 -0800612 wr->input = p;
613 wr->data = p;
614 wr->length += eivlen;
Adam Langley95c29f32014-06-20 12:00:00 -0700615
David Benjamin1e52eca2015-01-22 15:33:51 -0500616 if (!s->enc_method->enc(s, 1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 goto err;
618 }
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 /* record length after mac and block padding */
621 s2n(wr->length, plen);
Adam Langley95c29f32014-06-20 12:00:00 -0700622
Adam Langleyfcf25832014-12-18 17:42:32 -0800623 if (s->msg_callback) {
624 s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s, s->msg_callback_arg);
625 }
Adam Langley95c29f32014-06-20 12:00:00 -0700626
Adam Langleyfcf25832014-12-18 17:42:32 -0800627 /* we should now have wr->data pointing to the encrypted data, which is
628 * wr->length long. */
629 wr->type = type; /* not needed but helps for debugging */
630 wr->length += SSL3_RT_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700631
Adam Langleyfcf25832014-12-18 17:42:32 -0800632 if (is_fragment) {
633 /* we are in a recursive call; just return the length, don't write out
634 * anything. */
635 return wr->length;
636 }
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 /* now let's set up wb */
639 wb->left = prefix_len + wr->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700640
Adam Langleyfcf25832014-12-18 17:42:32 -0800641 /* memorize arguments so that ssl3_write_pending can detect bad write retries
642 * later */
643 s->s3->wpend_tot = len;
644 s->s3->wpend_buf = buf;
645 s->s3->wpend_type = type;
646 s->s3->wpend_ret = len;
647
648 /* we now just need to write the buffer */
649 return ssl3_write_pending(s, type, buf, len);
650
Adam Langley95c29f32014-06-20 12:00:00 -0700651err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800652 return -1;
653}
Adam Langley95c29f32014-06-20 12:00:00 -0700654
655/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800656int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
657 int i;
658 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700659
Adam Langleyfcf25832014-12-18 17:42:32 -0800660 if (s->s3->wpend_tot > (int)len ||
661 (s->s3->wpend_buf != buf &&
662 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
663 s->s3->wpend_type != type) {
664 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
665 return -1;
666 }
Adam Langley95c29f32014-06-20 12:00:00 -0700667
Adam Langleyfcf25832014-12-18 17:42:32 -0800668 for (;;) {
669 ERR_clear_system_error();
670 if (s->wbio != NULL) {
671 s->rwstate = SSL_WRITING;
672 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
673 (unsigned int)wb->left);
674 } else {
675 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
676 i = -1;
677 }
678 if (i == wb->left) {
679 wb->left = 0;
680 wb->offset += i;
681 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) {
682 ssl3_release_write_buffer(s);
683 }
684 s->rwstate = SSL_NOTHING;
685 return s->s3->wpend_ret;
686 } else if (i <= 0) {
687 if (SSL_IS_DTLS(s)) {
688 /* For DTLS, just drop it. That's kind of the whole
689 point in using a datagram service */
690 wb->left = 0;
691 }
692 return i;
693 }
694 wb->offset += i;
695 wb->left -= i;
696 }
697}
Adam Langley95c29f32014-06-20 12:00:00 -0700698
David Benjamin86271ee2014-07-21 16:14:03 -0400699/* ssl3_expect_change_cipher_spec informs the record layer that a
700 * ChangeCipherSpec record is required at this point. If a Handshake record is
701 * received before ChangeCipherSpec, the connection will fail. Moreover, if
702 * there are unprocessed handshake bytes, the handshake will also fail and the
703 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800704int ssl3_expect_change_cipher_spec(SSL *s) {
705 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
706 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
707 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
708 return 0;
709 }
710
711 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
712 return 1;
713}
David Benjamin86271ee2014-07-21 16:14:03 -0400714
Adam Langley95c29f32014-06-20 12:00:00 -0700715/* Return up to 'len' payload bytes received in 'type' records.
716 * 'type' is one of the following:
717 *
718 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
719 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
720 * - 0 (during a shutdown, no data has to be returned)
721 *
722 * If we don't have stored data to work from, read a SSL/TLS record first
723 * (possibly multiple records if we still don't have anything to return).
724 *
725 * This function must handle any surprises the peer may have for us, such as
726 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
727 * a surprise, but handled as if it were), or renegotiation requests.
728 * Also if record payloads contain fragments too small to process, we store
729 * them until there is enough for the respective protocol (the record protocol
730 * may use arbitrary fragmentation and even interleaving):
731 * Change cipher spec protocol
732 * just 1 byte needed, no need for keeping anything stored
733 * Alert protocol
734 * 2 bytes needed (AlertLevel, AlertDescription)
735 * Handshake protocol
736 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
737 * to detect unexpected Client Hello and Hello Request messages
738 * here, anything else is handled by higher layers
739 * Application data protocol
740 * none of our business
741 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800742int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500743 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800744 unsigned int n;
745 SSL3_RECORD *rr;
746 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700747
Adam Langleyfcf25832014-12-18 17:42:32 -0800748 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
749 (peek && type != SSL3_RT_APPLICATION_DATA)) {
750 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
751 return -1;
752 }
Adam Langley95c29f32014-06-20 12:00:00 -0700753
Adam Langleyfcf25832014-12-18 17:42:32 -0800754 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
755 /* (partially) satisfy request from storage */
756 uint8_t *src = s->s3->handshake_fragment;
757 uint8_t *dst = buf;
758 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700759
Adam Langleyfcf25832014-12-18 17:42:32 -0800760 /* peek == 0 */
761 n = 0;
762 while (len > 0 && s->s3->handshake_fragment_len > 0) {
763 *dst++ = *src++;
764 len--;
765 s->s3->handshake_fragment_len--;
766 n++;
767 }
768 /* move any remaining fragment bytes: */
769 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
770 s->s3->handshake_fragment[k] = *src++;
771 }
772 return n;
773 }
Adam Langley95c29f32014-06-20 12:00:00 -0700774
Adam Langleyfcf25832014-12-18 17:42:32 -0800775 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700776
David Benjamin931ab342015-02-08 19:46:57 -0500777 /* This may require multiple iterations. False Start will cause
778 * |s->handshake_func| to signal success one step early, but the handshake
779 * must be completely finished before other modes are accepted.
780 *
781 * TODO(davidben): Move this check up to a higher level. */
782 while (!s->in_handshake && SSL_in_init(s)) {
783 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800784 i = s->handshake_func(s);
785 if (i < 0) {
786 return i;
787 }
788 if (i == 0) {
789 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
790 return -1;
791 }
792 }
793
David Benjamine0e7d0d2015-02-08 19:33:25 -0500794 if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) {
795 /* TODO(davidben): Is this redundant with the calls in the handshake? */
796 return -1;
797 }
798
Adam Langley95c29f32014-06-20 12:00:00 -0700799start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700801
Adam Langleyfcf25832014-12-18 17:42:32 -0800802 /* s->s3->rrec.type - is the type of record
803 * s->s3->rrec.data - data
804 * s->s3->rrec.off - offset into 'data' for next read
805 * s->s3->rrec.length - number of bytes. */
806 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 /* get new packet if necessary */
809 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
810 ret = ssl3_get_record(s);
811 if (ret <= 0) {
812 return ret;
813 }
814 }
Adam Langley95c29f32014-06-20 12:00:00 -0700815
Adam Langleyfcf25832014-12-18 17:42:32 -0800816 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700817
David Benjamindc3da932015-03-12 15:09:02 -0400818 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
819 * ssl3_get_finished. */
820 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
821 rr->type != SSL3_RT_ALERT) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800822 al = SSL_AD_UNEXPECTED_MESSAGE;
823 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
824 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
825 goto f_err;
826 }
Adam Langley95c29f32014-06-20 12:00:00 -0700827
Adam Langleyfcf25832014-12-18 17:42:32 -0800828 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
829 * Handshake record. */
830 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
831 al = SSL_AD_UNEXPECTED_MESSAGE;
832 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
833 goto f_err;
834 }
David Benjamin86271ee2014-07-21 16:14:03 -0400835
Adam Langleyfcf25832014-12-18 17:42:32 -0800836 /* If the other end has shut down, throw anything we read away (even in
837 * 'peek' mode) */
838 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
839 rr->length = 0;
840 s->rwstate = SSL_NOTHING;
841 return 0;
842 }
Adam Langley95c29f32014-06-20 12:00:00 -0700843
Adam Langleyfcf25832014-12-18 17:42:32 -0800844 if (type == rr->type) {
845 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
846 /* make sure that we are not getting application data when we are doing a
847 * handshake for the first time */
848 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500849 s->aead_read_ctx == NULL) {
850 /* TODO(davidben): Is this check redundant with the handshake_func
851 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800852 al = SSL_AD_UNEXPECTED_MESSAGE;
853 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
854 goto f_err;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if (len <= 0) {
858 return len;
859 }
Adam Langley95c29f32014-06-20 12:00:00 -0700860
Adam Langleyfcf25832014-12-18 17:42:32 -0800861 if ((unsigned int)len > rr->length) {
862 n = rr->length;
863 } else {
864 n = (unsigned int)len;
865 }
Adam Langley95c29f32014-06-20 12:00:00 -0700866
Adam Langleyfcf25832014-12-18 17:42:32 -0800867 memcpy(buf, &(rr->data[rr->off]), n);
868 if (!peek) {
869 rr->length -= n;
870 rr->off += n;
871 if (rr->length == 0) {
872 s->rstate = SSL_ST_READ_HEADER;
873 rr->off = 0;
874 if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) {
875 ssl3_release_read_buffer(s);
876 }
877 }
878 }
879
880 return n;
881 }
Adam Langley95c29f32014-06-20 12:00:00 -0700882
883
Adam Langleyfcf25832014-12-18 17:42:32 -0800884 /* If we get here, then type != rr->type; if we have a handshake message,
885 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700886
Adam Langleyfcf25832014-12-18 17:42:32 -0800887 /* In case of record types for which we have 'fragment' storage, fill that so
888 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400889
Adam Langleyfcf25832014-12-18 17:42:32 -0800890 if (rr->type == SSL3_RT_HANDSHAKE) {
891 const size_t size = sizeof(s->s3->handshake_fragment);
892 const size_t avail = size - s->s3->handshake_fragment_len;
893 const size_t todo = (rr->length < avail) ? rr->length : avail;
894 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
895 &rr->data[rr->off], todo);
896 rr->off += todo;
897 rr->length -= todo;
898 s->s3->handshake_fragment_len += todo;
899 if (s->s3->handshake_fragment_len < size) {
900 goto start; /* fragment was too small */
901 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800902 }
Adam Langley95c29f32014-06-20 12:00:00 -0700903
Adam Langleyfcf25832014-12-18 17:42:32 -0800904 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
905 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700906
Adam Langleyfcf25832014-12-18 17:42:32 -0800907 /* If we are a client, check for an incoming 'Hello Request': */
908 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
909 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
910 s->session != NULL && s->session->cipher != NULL) {
911 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700912
Adam Langleyfcf25832014-12-18 17:42:32 -0800913 if (s->s3->handshake_fragment[1] != 0 ||
914 s->s3->handshake_fragment[2] != 0 ||
915 s->s3->handshake_fragment[3] != 0) {
916 al = SSL_AD_DECODE_ERROR;
917 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
918 goto f_err;
919 }
Adam Langley95c29f32014-06-20 12:00:00 -0700920
Adam Langleyfcf25832014-12-18 17:42:32 -0800921 if (s->msg_callback) {
922 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
923 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
924 }
Adam Langley95c29f32014-06-20 12:00:00 -0700925
Adam Langleyfcf25832014-12-18 17:42:32 -0800926 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
927 ssl3_renegotiate(s);
928 if (ssl3_renegotiate_check(s)) {
929 i = s->handshake_func(s);
930 if (i < 0) {
931 return i;
932 }
933 if (i == 0) {
934 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
935 return -1;
936 }
937 }
938 }
939 /* we either finished a handshake or ignored the request, now try again to
940 * obtain the (application) data we were asked for */
941 goto start;
942 }
David Benjaminb4188f02014-11-01 03:43:48 -0400943
David Benjamin86058a22015-02-22 13:07:21 -0500944 /* If an alert record, process one alert out of the record. Note that we allow
945 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500947 /* Alerts may not be fragmented. */
948 if (rr->length < 2) {
949 al = SSL_AD_DECODE_ERROR;
950 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
951 goto f_err;
952 }
Adam Langley95c29f32014-06-20 12:00:00 -0700953
Adam Langleyfcf25832014-12-18 17:42:32 -0800954 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500955 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800956 s->msg_callback_arg);
957 }
David Benjamin86058a22015-02-22 13:07:21 -0500958 const uint8_t alert_level = rr->data[rr->off++];
959 const uint8_t alert_descr = rr->data[rr->off++];
960 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700961
Adam Langleyfcf25832014-12-18 17:42:32 -0800962 if (s->info_callback != NULL) {
963 cb = s->info_callback;
964 } else if (s->ctx->info_callback != NULL) {
965 cb = s->ctx->info_callback;
966 }
Adam Langley95c29f32014-06-20 12:00:00 -0700967
Adam Langleyfcf25832014-12-18 17:42:32 -0800968 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500969 uint16_t alert = (alert_level << 8) | alert_descr;
970 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800971 }
Adam Langley95c29f32014-06-20 12:00:00 -0700972
David Benjamin86058a22015-02-22 13:07:21 -0500973 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 s->s3->warn_alert = alert_descr;
975 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
976 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
977 return 0;
978 }
Adam Langley95c29f32014-06-20 12:00:00 -0700979
Adam Langleyfcf25832014-12-18 17:42:32 -0800980 /* This is a warning but we receive it if we requested renegotiation and
981 * the peer denied it. Terminate with a fatal alert because if
982 * application tried to renegotiatie it presumably had a good reason and
983 * expects it to succeed.
984 *
985 * In future we might have a renegotiation where we don't care if the
986 * peer refused it where we carry on. */
987 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
988 al = SSL_AD_HANDSHAKE_FAILURE;
989 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
990 goto f_err;
991 }
David Benjamin86058a22015-02-22 13:07:21 -0500992 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800993 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700994
Adam Langleyfcf25832014-12-18 17:42:32 -0800995 s->rwstate = SSL_NOTHING;
996 s->s3->fatal_alert = alert_descr;
997 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
998 SSL_AD_REASON_OFFSET + alert_descr);
999 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
1000 ERR_add_error_data(2, "SSL alert number ", tmp);
1001 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
1002 SSL_CTX_remove_session(s->ctx, s->session);
1003 return 0;
1004 } else {
1005 al = SSL_AD_ILLEGAL_PARAMETER;
1006 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
1007 goto f_err;
1008 }
Adam Langley95c29f32014-06-20 12:00:00 -07001009
Adam Langleyfcf25832014-12-18 17:42:32 -08001010 goto start;
1011 }
Adam Langley95c29f32014-06-20 12:00:00 -07001012
Adam Langleyfcf25832014-12-18 17:42:32 -08001013 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1014 /* but we have not received a shutdown */
1015 s->rwstate = SSL_NOTHING;
1016 rr->length = 0;
1017 return 0;
1018 }
Adam Langley95c29f32014-06-20 12:00:00 -07001019
Adam Langleyfcf25832014-12-18 17:42:32 -08001020 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1021 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1022 * record payload has to look like */
1023 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1024 al = SSL_AD_ILLEGAL_PARAMETER;
1025 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1026 goto f_err;
1027 }
Adam Langley95c29f32014-06-20 12:00:00 -07001028
Adam Langleyfcf25832014-12-18 17:42:32 -08001029 /* Check we have a cipher to change to */
1030 if (s->s3->tmp.new_cipher == NULL) {
1031 al = SSL_AD_UNEXPECTED_MESSAGE;
1032 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1033 goto f_err;
1034 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001035
Adam Langleyfcf25832014-12-18 17:42:32 -08001036 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
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 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001043
Adam Langleyfcf25832014-12-18 17:42:32 -08001044 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 if (s->msg_callback) {
1047 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1048 s->msg_callback_arg);
1049 }
Adam Langley95c29f32014-06-20 12:00:00 -07001050
Adam Langleyfcf25832014-12-18 17:42:32 -08001051 s->s3->change_cipher_spec = 1;
1052 if (!ssl3_do_change_cipher_spec(s)) {
1053 goto err;
1054 } else {
1055 goto start;
1056 }
1057 }
Adam Langley95c29f32014-06-20 12:00:00 -07001058
Adam Langleyfcf25832014-12-18 17:42:32 -08001059 /* Unexpected handshake message (Client Hello, or protocol violation) */
1060 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1061 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1062 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1063 s->renegotiate = 1;
1064 s->new_session = 1;
1065 }
1066 i = s->handshake_func(s);
1067 if (i < 0) {
1068 return i;
1069 }
1070 if (i == 0) {
1071 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1072 return -1;
1073 }
Adam Langley95c29f32014-06-20 12:00:00 -07001074
Adam Langleyfcf25832014-12-18 17:42:32 -08001075 goto start;
1076 }
1077
David Benjamine820df92015-02-08 16:06:54 -05001078 /* We already handled these. */
1079 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1080 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001081
David Benjamine820df92015-02-08 16:06:54 -05001082 al = SSL_AD_UNEXPECTED_MESSAGE;
1083 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001084
1085f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001086 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001087err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001088 return -1;
1089}
Adam Langley95c29f32014-06-20 12:00:00 -07001090
Adam Langleyfcf25832014-12-18 17:42:32 -08001091int ssl3_do_change_cipher_spec(SSL *s) {
1092 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001093
Adam Langleyfcf25832014-12-18 17:42:32 -08001094 if (s->state & SSL_ST_ACCEPT) {
1095 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1096 } else {
1097 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1098 }
Adam Langley95c29f32014-06-20 12:00:00 -07001099
Adam Langleyfcf25832014-12-18 17:42:32 -08001100 if (s->s3->tmp.key_block == NULL) {
1101 if (s->session == NULL || s->session->master_key_length == 0) {
1102 /* might happen if dtls1_read_bytes() calls this */
1103 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1104 SSL_R_CCS_RECEIVED_EARLY);
1105 return 0;
1106 }
Adam Langley95c29f32014-06-20 12:00:00 -07001107
Adam Langleyfcf25832014-12-18 17:42:32 -08001108 s->session->cipher = s->s3->tmp.new_cipher;
1109 if (!s->enc_method->setup_key_block(s)) {
1110 return 0;
1111 }
1112 }
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 if (!s->enc_method->change_cipher_state(s, i)) {
1115 return 0;
1116 }
Adam Langley95c29f32014-06-20 12:00:00 -07001117
Adam Langleyfcf25832014-12-18 17:42:32 -08001118 return 1;
1119}
Adam Langley95c29f32014-06-20 12:00:00 -07001120
Adam Langleyfcf25832014-12-18 17:42:32 -08001121int ssl3_send_alert(SSL *s, int level, int desc) {
1122 /* Map tls/ssl alert value to correct one */
1123 desc = s->enc_method->alert_value(desc);
1124 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1125 /* SSL 3.0 does not have protocol_version alerts */
1126 desc = SSL_AD_HANDSHAKE_FAILURE;
1127 }
1128 if (desc < 0) {
1129 return -1;
1130 }
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 /* If a fatal one, remove from cache */
1133 if (level == 2 && s->session != NULL) {
1134 SSL_CTX_remove_session(s->ctx, s->session);
1135 }
Adam Langley95c29f32014-06-20 12:00:00 -07001136
Adam Langleyfcf25832014-12-18 17:42:32 -08001137 s->s3->alert_dispatch = 1;
1138 s->s3->send_alert[0] = level;
1139 s->s3->send_alert[1] = desc;
1140 if (s->s3->wbuf.left == 0) {
1141 /* data is still being written out. */
1142 return s->method->ssl_dispatch_alert(s);
1143 }
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145 /* else data is still being written out, we will get written some time in the
1146 * future */
1147 return -1;
1148}
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150int ssl3_dispatch_alert(SSL *s) {
1151 int i, j;
1152 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001153
Adam Langleyfcf25832014-12-18 17:42:32 -08001154 s->s3->alert_dispatch = 0;
1155 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0, 0);
1156 if (i <= 0) {
1157 s->s3->alert_dispatch = 1;
1158 } else {
1159 /* Alert sent to BIO. If it is important, flush it now. If the message
1160 * does not get sent due to non-blocking IO, we will not worry too much. */
1161 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1162 BIO_flush(s->wbio);
1163 }
Adam Langley95c29f32014-06-20 12:00:00 -07001164
Adam Langleyfcf25832014-12-18 17:42:32 -08001165 if (s->msg_callback) {
1166 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1167 s->msg_callback_arg);
1168 }
1169
1170 if (s->info_callback != NULL) {
1171 cb = s->info_callback;
1172 } else if (s->ctx->info_callback != NULL) {
1173 cb = s->ctx->info_callback;
1174 }
1175
1176 if (cb != NULL) {
1177 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1178 cb(s, SSL_CB_WRITE_ALERT, j);
1179 }
1180 }
1181
1182 return i;
1183}