blob: 2ef44a1b3e0dbed2de62f6f80d560c10822f88df [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 Langleyfcf25832014-12-18 17:42:32 -0800204 assert(n <= (int)(rb->len - rb->offset));
Adam Langley95c29f32014-06-20 12:00:00 -0700205
Adam Langleyfcf25832014-12-18 17:42:32 -0800206 if (!s->read_ahead) {
207 /* ignore max parameter */
208 max = n;
209 } else {
210 if (max < n) {
211 max = n;
212 }
213 if (max > (int)(rb->len - rb->offset)) {
214 max = rb->len - rb->offset;
215 }
216 }
Adam Langley95c29f32014-06-20 12:00:00 -0700217
Adam Langleyfcf25832014-12-18 17:42:32 -0800218 while (left < n) {
219 /* Now we have len+left bytes at the front of s->s3->rbuf.buf and need to
220 * read in more until we have len+n (up to len+max if possible). */
221 ERR_clear_system_error();
222 if (s->rbio != NULL) {
223 s->rwstate = SSL_READING;
224 i = BIO_read(s->rbio, pkt + len + left, max - left);
225 } else {
226 OPENSSL_PUT_ERROR(SSL, ssl3_read_n, SSL_R_READ_BIO_NOT_SET);
227 i = -1;
228 }
Adam Langley95c29f32014-06-20 12:00:00 -0700229
Adam Langleyfcf25832014-12-18 17:42:32 -0800230 if (i <= 0) {
231 rb->left = left;
232 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s) &&
233 len + left == 0) {
234 ssl3_release_read_buffer(s);
235 }
236 return i;
237 }
238 left += i;
239 /* reads should *never* span multiple packets for DTLS because the
240 * underlying transport protocol is message oriented as opposed to byte
241 * oriented as in the TLS case. */
242 if (SSL_IS_DTLS(s) && n > left) {
243 n = left; /* makes the while condition false */
244 }
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
Adam Langleyfcf25832014-12-18 17:42:32 -0800247 /* done reading, now the book-keeping */
248 rb->offset += n;
249 rb->left = left - n;
250 s->packet_length += n;
251 s->rwstate = SSL_NOTHING;
252
253 return n;
254}
Adam Langley95c29f32014-06-20 12:00:00 -0700255
Adam Langley48105fa2014-06-20 12:00:00 -0700256/* MAX_EMPTY_RECORDS defines the number of consecutive, empty records that will
257 * be processed per call to ssl3_get_record. Without this limit an attacker
258 * could send empty records at a faster rate than we can process and cause
259 * ssl3_get_record to loop forever. */
260#define MAX_EMPTY_RECORDS 32
261
Adam Langleyfcf25832014-12-18 17:42:32 -0800262/* Call this to get a new input record. It will return <= 0 if more data is
263 * needed, normally due to an error or non-blocking IO. When it finishes, one
264 * packet has been decoded and can be found in
Adam Langley95c29f32014-06-20 12:00:00 -0700265 * ssl->s3->rrec.type - is the type of record
Adam Langleyfcf25832014-12-18 17:42:32 -0800266 * ssl->s3->rrec.data - data
267 * ssl->s3->rrec.length - number of bytes */
Adam Langley95c29f32014-06-20 12:00:00 -0700268/* used only by ssl3_read_bytes */
Adam Langleyfcf25832014-12-18 17:42:32 -0800269static int ssl3_get_record(SSL *s) {
270 int ssl_major, ssl_minor, al;
David Benjamin1e52eca2015-01-22 15:33:51 -0500271 int n, i, ret = -1;
Adam Langleyfcf25832014-12-18 17:42:32 -0800272 SSL3_RECORD *rr;
Adam Langleyfcf25832014-12-18 17:42:32 -0800273 uint8_t *p;
Adam Langleyfcf25832014-12-18 17:42:32 -0800274 short version;
Adam Langleyfcf25832014-12-18 17:42:32 -0800275 size_t extra;
276 unsigned empty_record_count = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700277
Adam Langleyfcf25832014-12-18 17:42:32 -0800278 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langleyfcf25832014-12-18 17:42:32 -0800280 if (s->options & SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER) {
281 extra = SSL3_RT_MAX_EXTRA;
282 } else {
283 extra = 0;
284 }
285
286 if (extra && !s->s3->init_extra) {
287 /* An application error: SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER set after
288 * ssl3_setup_buffers() was done */
289 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, ERR_R_INTERNAL_ERROR);
290 return -1;
291 }
Adam Langley95c29f32014-06-20 12:00:00 -0700292
293again:
Adam Langleyfcf25832014-12-18 17:42:32 -0800294 /* check if we have the header */
295 if (s->rstate != SSL_ST_READ_BODY ||
296 s->packet_length < SSL3_RT_HEADER_LENGTH) {
297 n = ssl3_read_n(s, SSL3_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
298 if (n <= 0) {
299 return n; /* error or non-blocking */
300 }
301 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langleyfcf25832014-12-18 17:42:32 -0800303 p = s->packet;
304 if (s->msg_callback) {
305 s->msg_callback(0, 0, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
306 }
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langleyfcf25832014-12-18 17:42:32 -0800308 /* Pull apart the header into the SSL3_RECORD */
309 rr->type = *(p++);
310 ssl_major = *(p++);
311 ssl_minor = *(p++);
312 version = (ssl_major << 8) | ssl_minor;
313 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700314
Adam Langleyfcf25832014-12-18 17:42:32 -0800315 if (s->s3->have_version && version != s->version) {
316 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
317 if ((s->version & 0xFF00) == (version & 0xFF00)) {
318 /* Send back error using their minor version number. */
319 s->version = (unsigned short)version;
320 }
321 al = SSL_AD_PROTOCOL_VERSION;
322 goto f_err;
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langleyfcf25832014-12-18 17:42:32 -0800325 if ((version >> 8) != SSL3_VERSION_MAJOR) {
326 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_WRONG_VERSION_NUMBER);
327 goto err;
328 }
Adam Langley95c29f32014-06-20 12:00:00 -0700329
Adam Langleyfcf25832014-12-18 17:42:32 -0800330 if (rr->length > s->s3->rbuf.len - SSL3_RT_HEADER_LENGTH) {
331 al = SSL_AD_RECORD_OVERFLOW;
332 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_PACKET_LENGTH_TOO_LONG);
333 goto f_err;
334 }
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langleyfcf25832014-12-18 17:42:32 -0800336 /* now s->rstate == SSL_ST_READ_BODY */
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langleyfcf25832014-12-18 17:42:32 -0800339 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700340
Adam Langleyfcf25832014-12-18 17:42:32 -0800341 if (rr->length > s->packet_length - SSL3_RT_HEADER_LENGTH) {
342 /* now s->packet_length == SSL3_RT_HEADER_LENGTH */
343 i = rr->length;
344 n = ssl3_read_n(s, i, i, 1);
345 if (n <= 0) {
346 /* Error or non-blocking IO. Now |n| == |rr->length|, and
347 * |s->packet_length| == |SSL3_RT_HEADER_LENGTH| + |rr->length|. */
348 return n;
349 }
350 }
Adam Langley95c29f32014-06-20 12:00:00 -0700351
Adam Langleyfcf25832014-12-18 17:42:32 -0800352 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700353
Adam Langleyfcf25832014-12-18 17:42:32 -0800354 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and
355 * we have that many bytes in s->packet. */
356 rr->input = &s->packet[SSL3_RT_HEADER_LENGTH];
Adam Langley95c29f32014-06-20 12:00:00 -0700357
Adam Langleyfcf25832014-12-18 17:42:32 -0800358 /* ok, we can now read from |s->packet| data into |rr|. |rr->input| points at
359 * |rr->length| bytes, which need to be copied into |rr->data| by decryption.
360 * When the data is 'copied' into the |rr->data| buffer, |rr->input| will be
361 * pointed at the new buffer. */
Adam Langley95c29f32014-06-20 12:00:00 -0700362
Adam Langleyfcf25832014-12-18 17:42:32 -0800363 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ]
364 * rr->length bytes of encrypted compressed stuff. */
Adam Langley95c29f32014-06-20 12:00:00 -0700365
Adam Langleyfcf25832014-12-18 17:42:32 -0800366 /* check is not needed I believe */
367 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH + extra) {
368 al = SSL_AD_RECORD_OVERFLOW;
369 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
370 goto f_err;
371 }
Adam Langley95c29f32014-06-20 12:00:00 -0700372
Adam Langleyfcf25832014-12-18 17:42:32 -0800373 /* decrypt in place in 'rr->input' */
374 rr->data = rr->input;
Adam Langley95c29f32014-06-20 12:00:00 -0700375
David Benjamin1e52eca2015-01-22 15:33:51 -0500376 if (!s->enc_method->enc(s, 0)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800377 al = SSL_AD_BAD_RECORD_MAC;
378 OPENSSL_PUT_ERROR(SSL, ssl3_get_record,
379 SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
380 goto f_err;
381 }
Adam Langley95c29f32014-06-20 12:00:00 -0700382
Adam Langleyfcf25832014-12-18 17:42:32 -0800383 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH + extra) {
384 al = SSL_AD_RECORD_OVERFLOW;
385 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_DATA_LENGTH_TOO_LONG);
386 goto f_err;
387 }
Adam Langley95c29f32014-06-20 12:00:00 -0700388
Adam Langleyfcf25832014-12-18 17:42:32 -0800389 rr->off = 0;
390 /* So at this point the following is true:
391 * ssl->s3->rrec.type is the type of record;
392 * ssl->s3->rrec.length is the number of bytes in the record;
393 * ssl->s3->rrec.off is the offset to first valid byte;
394 * ssl->s3->rrec.data is where to take bytes from (increment after use). */
Adam Langley95c29f32014-06-20 12:00:00 -0700395
Adam Langleyfcf25832014-12-18 17:42:32 -0800396 /* we have pulled in a full packet so zero things */
397 s->packet_length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700398
Adam Langleyfcf25832014-12-18 17:42:32 -0800399 /* just read a 0 length packet */
400 if (rr->length == 0) {
401 empty_record_count++;
402 if (empty_record_count > MAX_EMPTY_RECORDS) {
403 al = SSL_AD_UNEXPECTED_MESSAGE;
404 OPENSSL_PUT_ERROR(SSL, ssl3_get_record, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
405 goto f_err;
406 }
407 goto again;
408 }
Adam Langley95c29f32014-06-20 12:00:00 -0700409
Adam Langleyfcf25832014-12-18 17:42:32 -0800410 return 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700411
412f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800413 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -0700414err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800415 return ret;
416}
Adam Langley95c29f32014-06-20 12:00:00 -0700417
Adam Langleyfcf25832014-12-18 17:42:32 -0800418/* Call this to write data in records of type |type|. It will return <= 0 if
419 * not all data has been sent or non-blocking IO. */
420int ssl3_write_bytes(SSL *s, int type, const void *buf_, int len) {
421 const uint8_t *buf = buf_;
422 unsigned int tot, n, nw;
423 int i;
Adam Langley95c29f32014-06-20 12:00:00 -0700424
Adam Langleyfcf25832014-12-18 17:42:32 -0800425 s->rwstate = SSL_NOTHING;
426 assert(s->s3->wnum <= INT_MAX);
427 tot = s->s3->wnum;
428 s->s3->wnum = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700429
David Benjamined7c4752015-02-16 19:16:46 -0500430 if (!s->in_handshake && SSL_in_init(s) && !SSL_in_false_start(s)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800431 i = s->handshake_func(s);
432 if (i < 0) {
433 return i;
434 }
435 if (i == 0) {
436 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
437 return -1;
438 }
439 }
Adam Langley95c29f32014-06-20 12:00:00 -0700440
Adam Langleyfcf25832014-12-18 17:42:32 -0800441 /* Ensure that if we end up with a smaller value of data to write out than
442 * the the original len from a write which didn't complete for non-blocking
443 * I/O and also somehow ended up avoiding the check for this in
444 * ssl3_write_pending/SSL_R_BAD_WRITE_RETRY as it must never be possible to
445 * end up with (len-tot) as a large number that will then promptly send
446 * beyond the end of the users buffer ... so we trap and report the error in
447 * a way the user will notice. */
448 if (len < 0 || (size_t)len < tot) {
449 OPENSSL_PUT_ERROR(SSL, ssl3_write_bytes, SSL_R_BAD_LENGTH);
450 return -1;
451 }
Adam Langley9611cfc2014-06-20 12:00:00 -0700452
Adam Langleyfcf25832014-12-18 17:42:32 -0800453 n = (len - tot);
454 for (;;) {
455 /* max contains the maximum number of bytes that we can put into a
456 * record. */
457 unsigned max = s->max_send_fragment;
458 /* fragment is true if do_ssl3_write should send the first byte in its own
459 * record in order to randomise a CBC IV. */
460 int fragment = 0;
Adam Langleyd493d522014-06-20 12:00:00 -0700461
Adam Langleyfcf25832014-12-18 17:42:32 -0800462 if (n > 1 && s->s3->need_record_splitting &&
463 type == SSL3_RT_APPLICATION_DATA && !s->s3->record_split_done) {
464 fragment = 1;
465 /* record_split_done records that the splitting has been done in case we
466 * hit an SSL_WANT_WRITE condition. In that case, we don't need to do the
467 * split again. */
468 s->s3->record_split_done = 1;
469 }
Adam Langleyd493d522014-06-20 12:00:00 -0700470
Adam Langleyfcf25832014-12-18 17:42:32 -0800471 if (n > max) {
472 nw = max;
473 } else {
474 nw = n;
475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langleyfcf25832014-12-18 17:42:32 -0800477 i = do_ssl3_write(s, type, &(buf[tot]), nw, fragment, 0);
478 if (i <= 0) {
479 s->s3->wnum = tot;
480 s->s3->record_split_done = 0;
481 return i;
482 }
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langleyfcf25832014-12-18 17:42:32 -0800484 if (i == (int)n || (type == SSL3_RT_APPLICATION_DATA &&
485 (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
486 /* next chunk of data should get another prepended, one-byte fragment in
487 * ciphersuites with known-IV weakness. */
488 s->s3->record_split_done = 0;
489 return tot + i;
490 }
Adam Langley95c29f32014-06-20 12:00:00 -0700491
Adam Langleyfcf25832014-12-18 17:42:32 -0800492 n -= i;
493 tot += i;
494 }
495}
Adam Langley95c29f32014-06-20 12:00:00 -0700496
Adam Langleyd493d522014-06-20 12:00:00 -0700497/* do_ssl3_write writes an SSL record of the given type. If |fragment| is 1
498 * then it splits the record into a one byte record and a record with the rest
499 * of the data in order to randomise a CBC IV. If |is_fragment| is true then
500 * this call resulted from do_ssl3_write calling itself in order to create that
501 * one byte fragment. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800502static int do_ssl3_write(SSL *s, int type, const uint8_t *buf, unsigned int len,
503 char fragment, char is_fragment) {
504 uint8_t *p, *plen;
David Benjaminb8a56f12014-12-23 11:41:02 -0500505 int i;
Adam Langleyfcf25832014-12-18 17:42:32 -0800506 int prefix_len = 0;
507 int eivlen = 0;
508 long align = 0;
509 SSL3_RECORD *wr;
510 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700511
Adam Langleyfcf25832014-12-18 17:42:32 -0800512 /* first check if there is a SSL3_BUFFER still being written out. This will
513 * happen with non blocking IO */
514 if (wb->left != 0) {
515 return ssl3_write_pending(s, type, buf, len);
516 }
Adam Langley95c29f32014-06-20 12:00:00 -0700517
Adam Langleyfcf25832014-12-18 17:42:32 -0800518 /* If we have an alert to send, lets send it */
519 if (s->s3->alert_dispatch) {
520 i = s->method->ssl_dispatch_alert(s);
521 if (i <= 0) {
522 return i;
523 }
524 /* if it went, fall through and send more stuff */
525 }
Adam Langley95c29f32014-06-20 12:00:00 -0700526
Adam Langleyfcf25832014-12-18 17:42:32 -0800527 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
528 return -1;
529 }
Adam Langleyc6c8ae82014-06-20 12:00:00 -0700530
Adam Langleyfcf25832014-12-18 17:42:32 -0800531 if (len == 0) {
532 return 0;
533 }
Adam Langley95c29f32014-06-20 12:00:00 -0700534
Adam Langleyfcf25832014-12-18 17:42:32 -0800535 wr = &s->s3->wrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700536
Adam Langleyfcf25832014-12-18 17:42:32 -0800537 if (fragment) {
538 /* countermeasure against known-IV weakness in CBC ciphersuites (see
539 * http://www.openssl.org/~bodo/tls-cbc.txt) */
540 prefix_len = do_ssl3_write(s, type, buf, 1 /* length */, 0 /* fragment */,
541 1 /* is_fragment */);
542 if (prefix_len <= 0) {
543 goto err;
544 }
Adam Langley95c29f32014-06-20 12:00:00 -0700545
Adam Langleyfcf25832014-12-18 17:42:32 -0800546 if (prefix_len >
547 (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {
548 /* insufficient space */
549 OPENSSL_PUT_ERROR(SSL, do_ssl3_write, ERR_R_INTERNAL_ERROR);
550 goto err;
551 }
552 }
Adam Langley95c29f32014-06-20 12:00:00 -0700553
Adam Langleyfcf25832014-12-18 17:42:32 -0800554 if (is_fragment) {
555 /* The extra fragment would be couple of cipher blocks, and that will be a
556 * multiple of SSL3_ALIGN_PAYLOAD. So, if we want to align the real
557 * payload, we can just pretend that we have two headers and a byte. */
558 align = (long)wb->buf + 2 * SSL3_RT_HEADER_LENGTH + 1;
559 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
560 p = wb->buf + align;
561 wb->offset = align;
562 } else if (prefix_len) {
563 p = wb->buf + wb->offset + prefix_len;
564 } else {
565 align = (long)wb->buf + SSL3_RT_HEADER_LENGTH;
566 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
567 p = wb->buf + align;
568 wb->offset = align;
569 }
Adam Langley95c29f32014-06-20 12:00:00 -0700570
Adam Langleyfcf25832014-12-18 17:42:32 -0800571 /* write the header */
Adam Langley95c29f32014-06-20 12:00:00 -0700572
Adam Langleyfcf25832014-12-18 17:42:32 -0800573 *(p++) = type & 0xff;
574 wr->type = type;
Adam Langley95c29f32014-06-20 12:00:00 -0700575
Adam Langleyfcf25832014-12-18 17:42:32 -0800576 /* Some servers hang if initial ClientHello is larger than 256 bytes and
577 * record version number > TLS 1.0. */
578 if (!s->s3->have_version && s->version > SSL3_VERSION) {
579 *(p++) = TLS1_VERSION >> 8;
580 *(p++) = TLS1_VERSION & 0xff;
581 } else {
582 *(p++) = s->version >> 8;
583 *(p++) = s->version & 0xff;
584 }
Adam Langley95c29f32014-06-20 12:00:00 -0700585
Adam Langleyfcf25832014-12-18 17:42:32 -0800586 /* field where we are to write out packet length */
587 plen = p;
588 p += 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700589
David Benjaminb8a56f12014-12-23 11:41:02 -0500590 /* Leave room for the variable nonce for AEADs which specify it explicitly. */
591 if (s->aead_write_ctx != NULL &&
592 s->aead_write_ctx->variable_nonce_included_in_record) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800593 eivlen = s->aead_write_ctx->variable_nonce_len;
594 }
Adam Langley95c29f32014-06-20 12:00:00 -0700595
Adam Langleyfcf25832014-12-18 17:42:32 -0800596 /* lets setup the record stuff. */
597 wr->data = p + eivlen;
598 wr->length = (int)(len - (fragment != 0));
599 wr->input = (uint8_t *)buf + (fragment != 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700600
Adam Langleyfcf25832014-12-18 17:42:32 -0800601 /* we now 'read' from wr->input, wr->length bytes into wr->data */
Adam Langley95c29f32014-06-20 12:00:00 -0700602
Adam Langleyfcf25832014-12-18 17:42:32 -0800603 memcpy(wr->data, wr->input, wr->length);
604 wr->input = wr->data;
Adam Langley95c29f32014-06-20 12:00:00 -0700605
Adam Langleyfcf25832014-12-18 17:42:32 -0800606 /* we should still have the output to wr->data and the input from wr->input.
607 * Length should be wr->length. wr->data still points in the wb->buf */
Adam Langley95c29f32014-06-20 12:00:00 -0700608
Adam Langleyfcf25832014-12-18 17:42:32 -0800609 wr->input = p;
610 wr->data = p;
611 wr->length += eivlen;
Adam Langley95c29f32014-06-20 12:00:00 -0700612
David Benjamin1e52eca2015-01-22 15:33:51 -0500613 if (!s->enc_method->enc(s, 1)) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800614 goto err;
615 }
Adam Langley95c29f32014-06-20 12:00:00 -0700616
Adam Langleyfcf25832014-12-18 17:42:32 -0800617 /* record length after mac and block padding */
618 s2n(wr->length, plen);
Adam Langley95c29f32014-06-20 12:00:00 -0700619
Adam Langleyfcf25832014-12-18 17:42:32 -0800620 if (s->msg_callback) {
621 s->msg_callback(1, 0, SSL3_RT_HEADER, plen - 5, 5, s, s->msg_callback_arg);
622 }
Adam Langley95c29f32014-06-20 12:00:00 -0700623
Adam Langleyfcf25832014-12-18 17:42:32 -0800624 /* we should now have wr->data pointing to the encrypted data, which is
625 * wr->length long. */
626 wr->type = type; /* not needed but helps for debugging */
627 wr->length += SSL3_RT_HEADER_LENGTH;
Adam Langley95c29f32014-06-20 12:00:00 -0700628
Adam Langleyfcf25832014-12-18 17:42:32 -0800629 if (is_fragment) {
630 /* we are in a recursive call; just return the length, don't write out
631 * anything. */
632 return wr->length;
633 }
Adam Langley95c29f32014-06-20 12:00:00 -0700634
Adam Langleyfcf25832014-12-18 17:42:32 -0800635 /* now let's set up wb */
636 wb->left = prefix_len + wr->length;
Adam Langley95c29f32014-06-20 12:00:00 -0700637
Adam Langleyfcf25832014-12-18 17:42:32 -0800638 /* memorize arguments so that ssl3_write_pending can detect bad write retries
639 * later */
640 s->s3->wpend_tot = len;
641 s->s3->wpend_buf = buf;
642 s->s3->wpend_type = type;
643 s->s3->wpend_ret = len;
644
645 /* we now just need to write the buffer */
646 return ssl3_write_pending(s, type, buf, len);
647
Adam Langley95c29f32014-06-20 12:00:00 -0700648err:
Adam Langleyfcf25832014-12-18 17:42:32 -0800649 return -1;
650}
Adam Langley95c29f32014-06-20 12:00:00 -0700651
652/* if s->s3->wbuf.left != 0, we need to call this */
Adam Langleyfcf25832014-12-18 17:42:32 -0800653int ssl3_write_pending(SSL *s, int type, const uint8_t *buf, unsigned int len) {
654 int i;
655 SSL3_BUFFER *wb = &(s->s3->wbuf);
Adam Langley95c29f32014-06-20 12:00:00 -0700656
Adam Langleyfcf25832014-12-18 17:42:32 -0800657 if (s->s3->wpend_tot > (int)len ||
658 (s->s3->wpend_buf != buf &&
659 !(s->mode & SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER)) ||
660 s->s3->wpend_type != type) {
661 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BAD_WRITE_RETRY);
662 return -1;
663 }
Adam Langley95c29f32014-06-20 12:00:00 -0700664
Adam Langleyfcf25832014-12-18 17:42:32 -0800665 for (;;) {
666 ERR_clear_system_error();
667 if (s->wbio != NULL) {
668 s->rwstate = SSL_WRITING;
669 i = BIO_write(s->wbio, (char *)&(wb->buf[wb->offset]),
670 (unsigned int)wb->left);
671 } else {
672 OPENSSL_PUT_ERROR(SSL, ssl3_write_pending, SSL_R_BIO_NOT_SET);
673 i = -1;
674 }
675 if (i == wb->left) {
676 wb->left = 0;
677 wb->offset += i;
678 if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_IS_DTLS(s)) {
679 ssl3_release_write_buffer(s);
680 }
681 s->rwstate = SSL_NOTHING;
682 return s->s3->wpend_ret;
683 } else if (i <= 0) {
684 if (SSL_IS_DTLS(s)) {
685 /* For DTLS, just drop it. That's kind of the whole
686 point in using a datagram service */
687 wb->left = 0;
688 }
689 return i;
690 }
691 wb->offset += i;
692 wb->left -= i;
693 }
694}
Adam Langley95c29f32014-06-20 12:00:00 -0700695
David Benjamin86271ee2014-07-21 16:14:03 -0400696/* ssl3_expect_change_cipher_spec informs the record layer that a
697 * ChangeCipherSpec record is required at this point. If a Handshake record is
698 * received before ChangeCipherSpec, the connection will fail. Moreover, if
699 * there are unprocessed handshake bytes, the handshake will also fail and the
700 * function returns zero. Otherwise, the function returns one. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800701int ssl3_expect_change_cipher_spec(SSL *s) {
702 if (s->s3->handshake_fragment_len > 0 || s->s3->tmp.reuse_message) {
703 OPENSSL_PUT_ERROR(SSL, ssl3_expect_change_cipher_spec,
704 SSL_R_UNPROCESSED_HANDSHAKE_DATA);
705 return 0;
706 }
707
708 s->s3->flags |= SSL3_FLAGS_EXPECT_CCS;
709 return 1;
710}
David Benjamin86271ee2014-07-21 16:14:03 -0400711
Adam Langley95c29f32014-06-20 12:00:00 -0700712/* Return up to 'len' payload bytes received in 'type' records.
713 * 'type' is one of the following:
714 *
715 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
716 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
717 * - 0 (during a shutdown, no data has to be returned)
718 *
719 * If we don't have stored data to work from, read a SSL/TLS record first
720 * (possibly multiple records if we still don't have anything to return).
721 *
722 * This function must handle any surprises the peer may have for us, such as
723 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
724 * a surprise, but handled as if it were), or renegotiation requests.
725 * Also if record payloads contain fragments too small to process, we store
726 * them until there is enough for the respective protocol (the record protocol
727 * may use arbitrary fragmentation and even interleaving):
728 * Change cipher spec protocol
729 * just 1 byte needed, no need for keeping anything stored
730 * Alert protocol
731 * 2 bytes needed (AlertLevel, AlertDescription)
732 * Handshake protocol
733 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
734 * to detect unexpected Client Hello and Hello Request messages
735 * here, anything else is handled by higher layers
736 * Application data protocol
737 * none of our business
738 */
Adam Langleyfcf25832014-12-18 17:42:32 -0800739int ssl3_read_bytes(SSL *s, int type, uint8_t *buf, int len, int peek) {
David Benjamin86058a22015-02-22 13:07:21 -0500740 int al, i, ret;
Adam Langleyfcf25832014-12-18 17:42:32 -0800741 unsigned int n;
742 SSL3_RECORD *rr;
743 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700744
Adam Langleyfcf25832014-12-18 17:42:32 -0800745 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
746 (peek && type != SSL3_RT_APPLICATION_DATA)) {
747 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
748 return -1;
749 }
Adam Langley95c29f32014-06-20 12:00:00 -0700750
Adam Langleyfcf25832014-12-18 17:42:32 -0800751 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
752 /* (partially) satisfy request from storage */
753 uint8_t *src = s->s3->handshake_fragment;
754 uint8_t *dst = buf;
755 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700756
Adam Langleyfcf25832014-12-18 17:42:32 -0800757 /* peek == 0 */
758 n = 0;
759 while (len > 0 && s->s3->handshake_fragment_len > 0) {
760 *dst++ = *src++;
761 len--;
762 s->s3->handshake_fragment_len--;
763 n++;
764 }
765 /* move any remaining fragment bytes: */
766 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
767 s->s3->handshake_fragment[k] = *src++;
768 }
769 return n;
770 }
Adam Langley95c29f32014-06-20 12:00:00 -0700771
Adam Langleyfcf25832014-12-18 17:42:32 -0800772 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700773
David Benjamin931ab342015-02-08 19:46:57 -0500774 /* This may require multiple iterations. False Start will cause
775 * |s->handshake_func| to signal success one step early, but the handshake
776 * must be completely finished before other modes are accepted.
777 *
778 * TODO(davidben): Move this check up to a higher level. */
779 while (!s->in_handshake && SSL_in_init(s)) {
780 assert(type == SSL3_RT_APPLICATION_DATA);
Adam Langleyfcf25832014-12-18 17:42:32 -0800781 i = s->handshake_func(s);
782 if (i < 0) {
783 return i;
784 }
785 if (i == 0) {
786 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
787 return -1;
788 }
789 }
790
David Benjamine0e7d0d2015-02-08 19:33:25 -0500791 if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) {
792 /* TODO(davidben): Is this redundant with the calls in the handshake? */
793 return -1;
794 }
795
Adam Langley95c29f32014-06-20 12:00:00 -0700796start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800797 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700798
Adam Langleyfcf25832014-12-18 17:42:32 -0800799 /* s->s3->rrec.type - is the type of record
800 * s->s3->rrec.data - data
801 * s->s3->rrec.off - offset into 'data' for next read
802 * s->s3->rrec.length - number of bytes. */
803 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700804
Adam Langleyfcf25832014-12-18 17:42:32 -0800805 /* get new packet if necessary */
806 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
807 ret = ssl3_get_record(s);
808 if (ret <= 0) {
809 return ret;
810 }
811 }
Adam Langley95c29f32014-06-20 12:00:00 -0700812
Adam Langleyfcf25832014-12-18 17:42:32 -0800813 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700814
Adam Langleyfcf25832014-12-18 17:42:32 -0800815 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
816 * reset by ssl3_get_finished */
817 && rr->type != SSL3_RT_HANDSHAKE) {
818 al = SSL_AD_UNEXPECTED_MESSAGE;
819 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
820 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
821 goto f_err;
822 }
Adam Langley95c29f32014-06-20 12:00:00 -0700823
Adam Langleyfcf25832014-12-18 17:42:32 -0800824 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
825 * Handshake record. */
826 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
827 al = SSL_AD_UNEXPECTED_MESSAGE;
828 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
829 goto f_err;
830 }
David Benjamin86271ee2014-07-21 16:14:03 -0400831
Adam Langleyfcf25832014-12-18 17:42:32 -0800832 /* If the other end has shut down, throw anything we read away (even in
833 * 'peek' mode) */
834 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
835 rr->length = 0;
836 s->rwstate = SSL_NOTHING;
837 return 0;
838 }
Adam Langley95c29f32014-06-20 12:00:00 -0700839
Adam Langleyfcf25832014-12-18 17:42:32 -0800840 if (type == rr->type) {
841 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
842 /* make sure that we are not getting application data when we are doing a
843 * handshake for the first time */
844 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500845 s->aead_read_ctx == NULL) {
846 /* TODO(davidben): Is this check redundant with the handshake_func
847 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 al = SSL_AD_UNEXPECTED_MESSAGE;
849 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
850 goto f_err;
851 }
Adam Langley95c29f32014-06-20 12:00:00 -0700852
Adam Langleyfcf25832014-12-18 17:42:32 -0800853 if (len <= 0) {
854 return len;
855 }
Adam Langley95c29f32014-06-20 12:00:00 -0700856
Adam Langleyfcf25832014-12-18 17:42:32 -0800857 if ((unsigned int)len > rr->length) {
858 n = rr->length;
859 } else {
860 n = (unsigned int)len;
861 }
Adam Langley95c29f32014-06-20 12:00:00 -0700862
Adam Langleyfcf25832014-12-18 17:42:32 -0800863 memcpy(buf, &(rr->data[rr->off]), n);
864 if (!peek) {
865 rr->length -= n;
866 rr->off += n;
867 if (rr->length == 0) {
868 s->rstate = SSL_ST_READ_HEADER;
869 rr->off = 0;
870 if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) {
871 ssl3_release_read_buffer(s);
872 }
873 }
874 }
875
876 return n;
877 }
Adam Langley95c29f32014-06-20 12:00:00 -0700878
879
Adam Langleyfcf25832014-12-18 17:42:32 -0800880 /* If we get here, then type != rr->type; if we have a handshake message,
881 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700882
Adam Langleyfcf25832014-12-18 17:42:32 -0800883 /* In case of record types for which we have 'fragment' storage, fill that so
884 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400885
Adam Langleyfcf25832014-12-18 17:42:32 -0800886 if (rr->type == SSL3_RT_HANDSHAKE) {
887 const size_t size = sizeof(s->s3->handshake_fragment);
888 const size_t avail = size - s->s3->handshake_fragment_len;
889 const size_t todo = (rr->length < avail) ? rr->length : avail;
890 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
891 &rr->data[rr->off], todo);
892 rr->off += todo;
893 rr->length -= todo;
894 s->s3->handshake_fragment_len += todo;
895 if (s->s3->handshake_fragment_len < size) {
896 goto start; /* fragment was too small */
897 }
Adam Langleyfcf25832014-12-18 17:42:32 -0800898 }
Adam Langley95c29f32014-06-20 12:00:00 -0700899
Adam Langleyfcf25832014-12-18 17:42:32 -0800900 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
901 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700902
Adam Langleyfcf25832014-12-18 17:42:32 -0800903 /* If we are a client, check for an incoming 'Hello Request': */
904 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
905 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
906 s->session != NULL && s->session->cipher != NULL) {
907 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 if (s->s3->handshake_fragment[1] != 0 ||
910 s->s3->handshake_fragment[2] != 0 ||
911 s->s3->handshake_fragment[3] != 0) {
912 al = SSL_AD_DECODE_ERROR;
913 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
914 goto f_err;
915 }
Adam Langley95c29f32014-06-20 12:00:00 -0700916
Adam Langleyfcf25832014-12-18 17:42:32 -0800917 if (s->msg_callback) {
918 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
919 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
920 }
Adam Langley95c29f32014-06-20 12:00:00 -0700921
Adam Langleyfcf25832014-12-18 17:42:32 -0800922 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
923 ssl3_renegotiate(s);
924 if (ssl3_renegotiate_check(s)) {
925 i = s->handshake_func(s);
926 if (i < 0) {
927 return i;
928 }
929 if (i == 0) {
930 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
931 return -1;
932 }
933 }
934 }
935 /* we either finished a handshake or ignored the request, now try again to
936 * obtain the (application) data we were asked for */
937 goto start;
938 }
David Benjaminb4188f02014-11-01 03:43:48 -0400939
David Benjamin86058a22015-02-22 13:07:21 -0500940 /* If an alert record, process one alert out of the record. Note that we allow
941 * a single record to contain multiple alerts. */
Adam Langleyfcf25832014-12-18 17:42:32 -0800942 if (rr->type == SSL3_RT_ALERT) {
David Benjamin86058a22015-02-22 13:07:21 -0500943 /* Alerts may not be fragmented. */
944 if (rr->length < 2) {
945 al = SSL_AD_DECODE_ERROR;
946 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
947 goto f_err;
948 }
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (s->msg_callback) {
David Benjamin86058a22015-02-22 13:07:21 -0500951 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langleyfcf25832014-12-18 17:42:32 -0800952 s->msg_callback_arg);
953 }
David Benjamin86058a22015-02-22 13:07:21 -0500954 const uint8_t alert_level = rr->data[rr->off++];
955 const uint8_t alert_descr = rr->data[rr->off++];
956 rr->length -= 2;
Adam Langley95c29f32014-06-20 12:00:00 -0700957
Adam Langleyfcf25832014-12-18 17:42:32 -0800958 if (s->info_callback != NULL) {
959 cb = s->info_callback;
960 } else if (s->ctx->info_callback != NULL) {
961 cb = s->ctx->info_callback;
962 }
Adam Langley95c29f32014-06-20 12:00:00 -0700963
Adam Langleyfcf25832014-12-18 17:42:32 -0800964 if (cb != NULL) {
David Benjamin86058a22015-02-22 13:07:21 -0500965 uint16_t alert = (alert_level << 8) | alert_descr;
966 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langleyfcf25832014-12-18 17:42:32 -0800967 }
Adam Langley95c29f32014-06-20 12:00:00 -0700968
David Benjamin86058a22015-02-22 13:07:21 -0500969 if (alert_level == SSL3_AL_WARNING) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800970 s->s3->warn_alert = alert_descr;
971 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
972 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
973 return 0;
974 }
Adam Langley95c29f32014-06-20 12:00:00 -0700975
Adam Langleyfcf25832014-12-18 17:42:32 -0800976 /* This is a warning but we receive it if we requested renegotiation and
977 * the peer denied it. Terminate with a fatal alert because if
978 * application tried to renegotiatie it presumably had a good reason and
979 * expects it to succeed.
980 *
981 * In future we might have a renegotiation where we don't care if the
982 * peer refused it where we carry on. */
983 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
984 al = SSL_AD_HANDSHAKE_FAILURE;
985 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
986 goto f_err;
987 }
David Benjamin86058a22015-02-22 13:07:21 -0500988 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langleyfcf25832014-12-18 17:42:32 -0800989 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700990
Adam Langleyfcf25832014-12-18 17:42:32 -0800991 s->rwstate = SSL_NOTHING;
992 s->s3->fatal_alert = alert_descr;
993 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
994 SSL_AD_REASON_OFFSET + alert_descr);
995 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
996 ERR_add_error_data(2, "SSL alert number ", tmp);
997 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
998 SSL_CTX_remove_session(s->ctx, s->session);
999 return 0;
1000 } else {
1001 al = SSL_AD_ILLEGAL_PARAMETER;
1002 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
1003 goto f_err;
1004 }
Adam Langley95c29f32014-06-20 12:00:00 -07001005
Adam Langleyfcf25832014-12-18 17:42:32 -08001006 goto start;
1007 }
Adam Langley95c29f32014-06-20 12:00:00 -07001008
Adam Langleyfcf25832014-12-18 17:42:32 -08001009 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1010 /* but we have not received a shutdown */
1011 s->rwstate = SSL_NOTHING;
1012 rr->length = 0;
1013 return 0;
1014 }
Adam Langley95c29f32014-06-20 12:00:00 -07001015
Adam Langleyfcf25832014-12-18 17:42:32 -08001016 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1017 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1018 * record payload has to look like */
1019 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1020 al = SSL_AD_ILLEGAL_PARAMETER;
1021 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1022 goto f_err;
1023 }
Adam Langley95c29f32014-06-20 12:00:00 -07001024
Adam Langleyfcf25832014-12-18 17:42:32 -08001025 /* Check we have a cipher to change to */
1026 if (s->s3->tmp.new_cipher == NULL) {
1027 al = SSL_AD_UNEXPECTED_MESSAGE;
1028 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1029 goto f_err;
1030 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001031
Adam Langleyfcf25832014-12-18 17:42:32 -08001032 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1033 al = SSL_AD_UNEXPECTED_MESSAGE;
1034 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1035 goto f_err;
1036 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001037
Adam Langleyfcf25832014-12-18 17:42:32 -08001038 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001039
Adam Langleyfcf25832014-12-18 17:42:32 -08001040 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001041
Adam Langleyfcf25832014-12-18 17:42:32 -08001042 if (s->msg_callback) {
1043 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1044 s->msg_callback_arg);
1045 }
Adam Langley95c29f32014-06-20 12:00:00 -07001046
Adam Langleyfcf25832014-12-18 17:42:32 -08001047 s->s3->change_cipher_spec = 1;
1048 if (!ssl3_do_change_cipher_spec(s)) {
1049 goto err;
1050 } else {
1051 goto start;
1052 }
1053 }
Adam Langley95c29f32014-06-20 12:00:00 -07001054
Adam Langleyfcf25832014-12-18 17:42:32 -08001055 /* Unexpected handshake message (Client Hello, or protocol violation) */
1056 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1057 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1058 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1059 s->renegotiate = 1;
1060 s->new_session = 1;
1061 }
1062 i = s->handshake_func(s);
1063 if (i < 0) {
1064 return i;
1065 }
1066 if (i == 0) {
1067 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1068 return -1;
1069 }
Adam Langley95c29f32014-06-20 12:00:00 -07001070
Adam Langleyfcf25832014-12-18 17:42:32 -08001071 goto start;
1072 }
1073
David Benjamine820df92015-02-08 16:06:54 -05001074 /* We already handled these. */
1075 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1076 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001077
David Benjamine820df92015-02-08 16:06:54 -05001078 al = SSL_AD_UNEXPECTED_MESSAGE;
1079 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001080
1081f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001082 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001083err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001084 return -1;
1085}
Adam Langley95c29f32014-06-20 12:00:00 -07001086
Adam Langleyfcf25832014-12-18 17:42:32 -08001087int ssl3_do_change_cipher_spec(SSL *s) {
1088 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001089
Adam Langleyfcf25832014-12-18 17:42:32 -08001090 if (s->state & SSL_ST_ACCEPT) {
1091 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1092 } else {
1093 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1094 }
Adam Langley95c29f32014-06-20 12:00:00 -07001095
Adam Langleyfcf25832014-12-18 17:42:32 -08001096 if (s->s3->tmp.key_block == NULL) {
1097 if (s->session == NULL || s->session->master_key_length == 0) {
1098 /* might happen if dtls1_read_bytes() calls this */
1099 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1100 SSL_R_CCS_RECEIVED_EARLY);
1101 return 0;
1102 }
Adam Langley95c29f32014-06-20 12:00:00 -07001103
Adam Langleyfcf25832014-12-18 17:42:32 -08001104 s->session->cipher = s->s3->tmp.new_cipher;
1105 if (!s->enc_method->setup_key_block(s)) {
1106 return 0;
1107 }
1108 }
Adam Langley95c29f32014-06-20 12:00:00 -07001109
Adam Langleyfcf25832014-12-18 17:42:32 -08001110 if (!s->enc_method->change_cipher_state(s, i)) {
1111 return 0;
1112 }
Adam Langley95c29f32014-06-20 12:00:00 -07001113
Adam Langleyfcf25832014-12-18 17:42:32 -08001114 return 1;
1115}
Adam Langley95c29f32014-06-20 12:00:00 -07001116
Adam Langleyfcf25832014-12-18 17:42:32 -08001117int ssl3_send_alert(SSL *s, int level, int desc) {
1118 /* Map tls/ssl alert value to correct one */
1119 desc = s->enc_method->alert_value(desc);
1120 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1121 /* SSL 3.0 does not have protocol_version alerts */
1122 desc = SSL_AD_HANDSHAKE_FAILURE;
1123 }
1124 if (desc < 0) {
1125 return -1;
1126 }
Adam Langley95c29f32014-06-20 12:00:00 -07001127
Adam Langleyfcf25832014-12-18 17:42:32 -08001128 /* If a fatal one, remove from cache */
1129 if (level == 2 && s->session != NULL) {
1130 SSL_CTX_remove_session(s->ctx, s->session);
1131 }
Adam Langley95c29f32014-06-20 12:00:00 -07001132
Adam Langleyfcf25832014-12-18 17:42:32 -08001133 s->s3->alert_dispatch = 1;
1134 s->s3->send_alert[0] = level;
1135 s->s3->send_alert[1] = desc;
1136 if (s->s3->wbuf.left == 0) {
1137 /* data is still being written out. */
1138 return s->method->ssl_dispatch_alert(s);
1139 }
Adam Langley95c29f32014-06-20 12:00:00 -07001140
Adam Langleyfcf25832014-12-18 17:42:32 -08001141 /* else data is still being written out, we will get written some time in the
1142 * future */
1143 return -1;
1144}
Adam Langley95c29f32014-06-20 12:00:00 -07001145
Adam Langleyfcf25832014-12-18 17:42:32 -08001146int ssl3_dispatch_alert(SSL *s) {
1147 int i, j;
1148 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001149
Adam Langleyfcf25832014-12-18 17:42:32 -08001150 s->s3->alert_dispatch = 0;
1151 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0, 0);
1152 if (i <= 0) {
1153 s->s3->alert_dispatch = 1;
1154 } else {
1155 /* Alert sent to BIO. If it is important, flush it now. If the message
1156 * does not get sent due to non-blocking IO, we will not worry too much. */
1157 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1158 BIO_flush(s->wbio);
1159 }
Adam Langley95c29f32014-06-20 12:00:00 -07001160
Adam Langleyfcf25832014-12-18 17:42:32 -08001161 if (s->msg_callback) {
1162 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1163 s->msg_callback_arg);
1164 }
1165
1166 if (s->info_callback != NULL) {
1167 cb = s->info_callback;
1168 } else if (s->ctx->info_callback != NULL) {
1169 cb = s->ctx->info_callback;
1170 }
1171
1172 if (cb != NULL) {
1173 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1174 cb(s, SSL_CB_WRITE_ALERT, j);
1175 }
1176 }
1177
1178 return i;
1179}