blob: b22b005f61f75b7dd12ef619ba81e36effa6c728 [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
Adam Langleyfcf25832014-12-18 17:42:32 -0800430 if (SSL_in_init(s) && !s->in_handshake) {
431 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) {
740 int al, i, j, ret;
741 unsigned int n;
742 SSL3_RECORD *rr;
743 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
744 uint8_t alert_buffer[2];
Adam Langley95c29f32014-06-20 12:00:00 -0700745
Adam Langleyfcf25832014-12-18 17:42:32 -0800746 if (s->s3->rbuf.buf == NULL && !ssl3_setup_read_buffer(s)) {
747 return -1;
748 }
Adam Langley95c29f32014-06-20 12:00:00 -0700749
Adam Langleyfcf25832014-12-18 17:42:32 -0800750 if ((type && type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
751 (peek && type != SSL3_RT_APPLICATION_DATA)) {
752 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, ERR_R_INTERNAL_ERROR);
753 return -1;
754 }
Adam Langley95c29f32014-06-20 12:00:00 -0700755
Adam Langleyfcf25832014-12-18 17:42:32 -0800756 if (type == SSL3_RT_HANDSHAKE && s->s3->handshake_fragment_len > 0) {
757 /* (partially) satisfy request from storage */
758 uint8_t *src = s->s3->handshake_fragment;
759 uint8_t *dst = buf;
760 unsigned int k;
Adam Langley95c29f32014-06-20 12:00:00 -0700761
Adam Langleyfcf25832014-12-18 17:42:32 -0800762 /* peek == 0 */
763 n = 0;
764 while (len > 0 && s->s3->handshake_fragment_len > 0) {
765 *dst++ = *src++;
766 len--;
767 s->s3->handshake_fragment_len--;
768 n++;
769 }
770 /* move any remaining fragment bytes: */
771 for (k = 0; k < s->s3->handshake_fragment_len; k++) {
772 s->s3->handshake_fragment[k] = *src++;
773 }
774 return n;
775 }
Adam Langley95c29f32014-06-20 12:00:00 -0700776
Adam Langleyfcf25832014-12-18 17:42:32 -0800777 /* Now s->s3->handshake_fragment_len == 0 if type == SSL3_RT_HANDSHAKE. */
Adam Langley95c29f32014-06-20 12:00:00 -0700778
Adam Langleyfcf25832014-12-18 17:42:32 -0800779 if (!s->in_handshake && SSL_in_init(s)) {
780 /* type == SSL3_RT_APPLICATION_DATA */
781 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
Adam Langley95c29f32014-06-20 12:00:00 -0700791start:
Adam Langleyfcf25832014-12-18 17:42:32 -0800792 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700793
Adam Langleyfcf25832014-12-18 17:42:32 -0800794 /* s->s3->rrec.type - is the type of record
795 * s->s3->rrec.data - data
796 * s->s3->rrec.off - offset into 'data' for next read
797 * s->s3->rrec.length - number of bytes. */
798 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700799
Adam Langleyfcf25832014-12-18 17:42:32 -0800800 /* get new packet if necessary */
801 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
802 ret = ssl3_get_record(s);
803 if (ret <= 0) {
804 return ret;
805 }
806 }
Adam Langley95c29f32014-06-20 12:00:00 -0700807
Adam Langleyfcf25832014-12-18 17:42:32 -0800808 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700809
Adam Langleyfcf25832014-12-18 17:42:32 -0800810 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
811 * reset by ssl3_get_finished */
812 && rr->type != SSL3_RT_HANDSHAKE) {
813 al = SSL_AD_UNEXPECTED_MESSAGE;
814 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
815 SSL_R_DATA_BETWEEN_CCS_AND_FINISHED);
816 goto f_err;
817 }
Adam Langley95c29f32014-06-20 12:00:00 -0700818
Adam Langleyfcf25832014-12-18 17:42:32 -0800819 /* If we are expecting a ChangeCipherSpec, it is illegal to receive a
820 * Handshake record. */
821 if (rr->type == SSL3_RT_HANDSHAKE && (s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
822 al = SSL_AD_UNEXPECTED_MESSAGE;
823 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_HANDSHAKE_RECORD_BEFORE_CCS);
824 goto f_err;
825 }
David Benjamin86271ee2014-07-21 16:14:03 -0400826
Adam Langleyfcf25832014-12-18 17:42:32 -0800827 /* If the other end has shut down, throw anything we read away (even in
828 * 'peek' mode) */
829 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
830 rr->length = 0;
831 s->rwstate = SSL_NOTHING;
832 return 0;
833 }
Adam Langley95c29f32014-06-20 12:00:00 -0700834
Adam Langleyfcf25832014-12-18 17:42:32 -0800835 if (type == rr->type) {
836 /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
837 /* make sure that we are not getting application data when we are doing a
838 * handshake for the first time */
839 if (SSL_in_init(s) && type == SSL3_RT_APPLICATION_DATA &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500840 s->aead_read_ctx == NULL) {
841 /* TODO(davidben): Is this check redundant with the handshake_func
842 * check? */
Adam Langleyfcf25832014-12-18 17:42:32 -0800843 al = SSL_AD_UNEXPECTED_MESSAGE;
844 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
845 goto f_err;
846 }
Adam Langley95c29f32014-06-20 12:00:00 -0700847
Adam Langleyfcf25832014-12-18 17:42:32 -0800848 if (len <= 0) {
849 return len;
850 }
Adam Langley95c29f32014-06-20 12:00:00 -0700851
Adam Langleyfcf25832014-12-18 17:42:32 -0800852 if ((unsigned int)len > rr->length) {
853 n = rr->length;
854 } else {
855 n = (unsigned int)len;
856 }
Adam Langley95c29f32014-06-20 12:00:00 -0700857
Adam Langleyfcf25832014-12-18 17:42:32 -0800858 memcpy(buf, &(rr->data[rr->off]), n);
859 if (!peek) {
860 rr->length -= n;
861 rr->off += n;
862 if (rr->length == 0) {
863 s->rstate = SSL_ST_READ_HEADER;
864 rr->off = 0;
865 if (s->mode & SSL_MODE_RELEASE_BUFFERS && s->s3->rbuf.left == 0) {
866 ssl3_release_read_buffer(s);
867 }
868 }
869 }
870
871 return n;
872 }
Adam Langley95c29f32014-06-20 12:00:00 -0700873
874
Adam Langleyfcf25832014-12-18 17:42:32 -0800875 /* If we get here, then type != rr->type; if we have a handshake message,
876 * then it was unexpected (Hello Request or Client Hello). */
Adam Langley95c29f32014-06-20 12:00:00 -0700877
Adam Langleyfcf25832014-12-18 17:42:32 -0800878 /* In case of record types for which we have 'fragment' storage, fill that so
879 * that we can process the data at a fixed place. */
Alex Chernyakhovsky4cd8c432014-11-01 19:39:08 -0400880
Adam Langleyfcf25832014-12-18 17:42:32 -0800881 if (rr->type == SSL3_RT_HANDSHAKE) {
882 const size_t size = sizeof(s->s3->handshake_fragment);
883 const size_t avail = size - s->s3->handshake_fragment_len;
884 const size_t todo = (rr->length < avail) ? rr->length : avail;
885 memcpy(s->s3->handshake_fragment + s->s3->handshake_fragment_len,
886 &rr->data[rr->off], todo);
887 rr->off += todo;
888 rr->length -= todo;
889 s->s3->handshake_fragment_len += todo;
890 if (s->s3->handshake_fragment_len < size) {
891 goto start; /* fragment was too small */
892 }
893 } else if (rr->type == SSL3_RT_ALERT) {
894 /* Note that this will still allow multiple alerts to be processed in the
895 * same record */
896 if (rr->length < sizeof(alert_buffer)) {
897 al = SSL_AD_DECODE_ERROR;
898 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_ALERT);
899 goto f_err;
900 }
901 memcpy(alert_buffer, &rr->data[rr->off], sizeof(alert_buffer));
902 rr->off += sizeof(alert_buffer);
903 rr->length -= sizeof(alert_buffer);
904 }
Adam Langley95c29f32014-06-20 12:00:00 -0700905
Adam Langleyfcf25832014-12-18 17:42:32 -0800906 /* s->s3->handshake_fragment_len == 4 iff rr->type == SSL3_RT_HANDSHAKE;
907 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.) */
Adam Langley95c29f32014-06-20 12:00:00 -0700908
Adam Langleyfcf25832014-12-18 17:42:32 -0800909 /* If we are a client, check for an incoming 'Hello Request': */
910 if (!s->server && s->s3->handshake_fragment_len >= 4 &&
911 s->s3->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST &&
912 s->session != NULL && s->session->cipher != NULL) {
913 s->s3->handshake_fragment_len = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700914
Adam Langleyfcf25832014-12-18 17:42:32 -0800915 if (s->s3->handshake_fragment[1] != 0 ||
916 s->s3->handshake_fragment[2] != 0 ||
917 s->s3->handshake_fragment[3] != 0) {
918 al = SSL_AD_DECODE_ERROR;
919 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_HELLO_REQUEST);
920 goto f_err;
921 }
Adam Langley95c29f32014-06-20 12:00:00 -0700922
Adam Langleyfcf25832014-12-18 17:42:32 -0800923 if (s->msg_callback) {
924 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
925 s->s3->handshake_fragment, 4, s, s->msg_callback_arg);
926 }
Adam Langley95c29f32014-06-20 12:00:00 -0700927
Adam Langleyfcf25832014-12-18 17:42:32 -0800928 if (SSL_is_init_finished(s) && !s->s3->renegotiate) {
929 ssl3_renegotiate(s);
930 if (ssl3_renegotiate_check(s)) {
931 i = s->handshake_func(s);
932 if (i < 0) {
933 return i;
934 }
935 if (i == 0) {
936 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
937 return -1;
938 }
939 }
940 }
941 /* we either finished a handshake or ignored the request, now try again to
942 * obtain the (application) data we were asked for */
943 goto start;
944 }
David Benjaminb4188f02014-11-01 03:43:48 -0400945
Adam Langleyfcf25832014-12-18 17:42:32 -0800946 if (rr->type == SSL3_RT_ALERT) {
947 const uint8_t alert_level = alert_buffer[0];
948 const uint8_t alert_descr = alert_buffer[1];
Adam Langley95c29f32014-06-20 12:00:00 -0700949
Adam Langleyfcf25832014-12-18 17:42:32 -0800950 if (s->msg_callback) {
951 s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_buffer, 2, s,
952 s->msg_callback_arg);
953 }
Adam Langley95c29f32014-06-20 12:00:00 -0700954
Adam Langleyfcf25832014-12-18 17:42:32 -0800955 if (s->info_callback != NULL) {
956 cb = s->info_callback;
957 } else if (s->ctx->info_callback != NULL) {
958 cb = s->ctx->info_callback;
959 }
Adam Langley95c29f32014-06-20 12:00:00 -0700960
Adam Langleyfcf25832014-12-18 17:42:32 -0800961 if (cb != NULL) {
962 j = (alert_level << 8) | alert_descr;
963 cb(s, SSL_CB_READ_ALERT, j);
964 }
Adam Langley95c29f32014-06-20 12:00:00 -0700965
Adam Langleyfcf25832014-12-18 17:42:32 -0800966 if (alert_level == 1) {
967 /* warning */
968 s->s3->warn_alert = alert_descr;
969 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
970 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
971 return 0;
972 }
Adam Langley95c29f32014-06-20 12:00:00 -0700973
Adam Langleyfcf25832014-12-18 17:42:32 -0800974 /* This is a warning but we receive it if we requested renegotiation and
975 * the peer denied it. Terminate with a fatal alert because if
976 * application tried to renegotiatie it presumably had a good reason and
977 * expects it to succeed.
978 *
979 * In future we might have a renegotiation where we don't care if the
980 * peer refused it where we carry on. */
981 else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
982 al = SSL_AD_HANDSHAKE_FAILURE;
983 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_NO_RENEGOTIATION);
984 goto f_err;
985 }
986 } else if (alert_level == 2) {
987 /* fatal */
988 char tmp[16];
Adam Langley95c29f32014-06-20 12:00:00 -0700989
Adam Langleyfcf25832014-12-18 17:42:32 -0800990 s->rwstate = SSL_NOTHING;
991 s->s3->fatal_alert = alert_descr;
992 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes,
993 SSL_AD_REASON_OFFSET + alert_descr);
994 BIO_snprintf(tmp, sizeof(tmp), "%d", alert_descr);
995 ERR_add_error_data(2, "SSL alert number ", tmp);
996 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
997 SSL_CTX_remove_session(s->ctx, s->session);
998 return 0;
999 } else {
1000 al = SSL_AD_ILLEGAL_PARAMETER;
1001 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
1002 goto f_err;
1003 }
Adam Langley95c29f32014-06-20 12:00:00 -07001004
Adam Langleyfcf25832014-12-18 17:42:32 -08001005 goto start;
1006 }
Adam Langley95c29f32014-06-20 12:00:00 -07001007
Adam Langleyfcf25832014-12-18 17:42:32 -08001008 if (s->shutdown & SSL_SENT_SHUTDOWN) {
1009 /* but we have not received a shutdown */
1010 s->rwstate = SSL_NOTHING;
1011 rr->length = 0;
1012 return 0;
1013 }
Adam Langley95c29f32014-06-20 12:00:00 -07001014
Adam Langleyfcf25832014-12-18 17:42:32 -08001015 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1016 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
1017 * record payload has to look like */
1018 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
1019 al = SSL_AD_ILLEGAL_PARAMETER;
1020 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
1021 goto f_err;
1022 }
Adam Langley95c29f32014-06-20 12:00:00 -07001023
Adam Langleyfcf25832014-12-18 17:42:32 -08001024 /* Check we have a cipher to change to */
1025 if (s->s3->tmp.new_cipher == NULL) {
1026 al = SSL_AD_UNEXPECTED_MESSAGE;
1027 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1028 goto f_err;
1029 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001030
Adam Langleyfcf25832014-12-18 17:42:32 -08001031 if (!(s->s3->flags & SSL3_FLAGS_EXPECT_CCS)) {
1032 al = SSL_AD_UNEXPECTED_MESSAGE;
1033 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_CCS_RECEIVED_EARLY);
1034 goto f_err;
1035 }
Adam Langleyce7f9ca2014-06-20 12:00:00 -07001036
Adam Langleyfcf25832014-12-18 17:42:32 -08001037 s->s3->flags &= ~SSL3_FLAGS_EXPECT_CCS;
Adam Langley95c29f32014-06-20 12:00:00 -07001038
Adam Langleyfcf25832014-12-18 17:42:32 -08001039 rr->length = 0;
Adam Langley95c29f32014-06-20 12:00:00 -07001040
Adam Langleyfcf25832014-12-18 17:42:32 -08001041 if (s->msg_callback) {
1042 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
1043 s->msg_callback_arg);
1044 }
Adam Langley95c29f32014-06-20 12:00:00 -07001045
Adam Langleyfcf25832014-12-18 17:42:32 -08001046 s->s3->change_cipher_spec = 1;
1047 if (!ssl3_do_change_cipher_spec(s)) {
1048 goto err;
1049 } else {
1050 goto start;
1051 }
1052 }
Adam Langley95c29f32014-06-20 12:00:00 -07001053
Adam Langleyfcf25832014-12-18 17:42:32 -08001054 /* Unexpected handshake message (Client Hello, or protocol violation) */
1055 if (s->s3->handshake_fragment_len >= 4 && !s->in_handshake) {
1056 if ((s->state & SSL_ST_MASK) == SSL_ST_OK) {
1057 s->state = s->server ? SSL_ST_ACCEPT : SSL_ST_CONNECT;
1058 s->renegotiate = 1;
1059 s->new_session = 1;
1060 }
1061 i = s->handshake_func(s);
1062 if (i < 0) {
1063 return i;
1064 }
1065 if (i == 0) {
1066 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
1067 return -1;
1068 }
Adam Langley95c29f32014-06-20 12:00:00 -07001069
Adam Langleyfcf25832014-12-18 17:42:32 -08001070 goto start;
1071 }
1072
David Benjamine820df92015-02-08 16:06:54 -05001073 /* We already handled these. */
1074 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT &&
1075 rr->type != SSL3_RT_HANDSHAKE);
David Benjaminddb9f152015-02-03 15:44:39 -05001076
David Benjamine820df92015-02-08 16:06:54 -05001077 al = SSL_AD_UNEXPECTED_MESSAGE;
1078 OPENSSL_PUT_ERROR(SSL, ssl3_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley95c29f32014-06-20 12:00:00 -07001079
1080f_err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001081 ssl3_send_alert(s, SSL3_AL_FATAL, al);
Adam Langley95c29f32014-06-20 12:00:00 -07001082err:
Adam Langleyfcf25832014-12-18 17:42:32 -08001083 return -1;
1084}
Adam Langley95c29f32014-06-20 12:00:00 -07001085
Adam Langleyfcf25832014-12-18 17:42:32 -08001086int ssl3_do_change_cipher_spec(SSL *s) {
1087 int i;
Adam Langley95c29f32014-06-20 12:00:00 -07001088
Adam Langleyfcf25832014-12-18 17:42:32 -08001089 if (s->state & SSL_ST_ACCEPT) {
1090 i = SSL3_CHANGE_CIPHER_SERVER_READ;
1091 } else {
1092 i = SSL3_CHANGE_CIPHER_CLIENT_READ;
1093 }
Adam Langley95c29f32014-06-20 12:00:00 -07001094
Adam Langleyfcf25832014-12-18 17:42:32 -08001095 if (s->s3->tmp.key_block == NULL) {
1096 if (s->session == NULL || s->session->master_key_length == 0) {
1097 /* might happen if dtls1_read_bytes() calls this */
1098 OPENSSL_PUT_ERROR(SSL, ssl3_do_change_cipher_spec,
1099 SSL_R_CCS_RECEIVED_EARLY);
1100 return 0;
1101 }
Adam Langley95c29f32014-06-20 12:00:00 -07001102
Adam Langleyfcf25832014-12-18 17:42:32 -08001103 s->session->cipher = s->s3->tmp.new_cipher;
1104 if (!s->enc_method->setup_key_block(s)) {
1105 return 0;
1106 }
1107 }
Adam Langley95c29f32014-06-20 12:00:00 -07001108
Adam Langleyfcf25832014-12-18 17:42:32 -08001109 if (!s->enc_method->change_cipher_state(s, i)) {
1110 return 0;
1111 }
Adam Langley95c29f32014-06-20 12:00:00 -07001112
Adam Langleyfcf25832014-12-18 17:42:32 -08001113 return 1;
1114}
Adam Langley95c29f32014-06-20 12:00:00 -07001115
Adam Langleyfcf25832014-12-18 17:42:32 -08001116int ssl3_send_alert(SSL *s, int level, int desc) {
1117 /* Map tls/ssl alert value to correct one */
1118 desc = s->enc_method->alert_value(desc);
1119 if (s->version == SSL3_VERSION && desc == SSL_AD_PROTOCOL_VERSION) {
1120 /* SSL 3.0 does not have protocol_version alerts */
1121 desc = SSL_AD_HANDSHAKE_FAILURE;
1122 }
1123 if (desc < 0) {
1124 return -1;
1125 }
Adam Langley95c29f32014-06-20 12:00:00 -07001126
Adam Langleyfcf25832014-12-18 17:42:32 -08001127 /* If a fatal one, remove from cache */
1128 if (level == 2 && s->session != NULL) {
1129 SSL_CTX_remove_session(s->ctx, s->session);
1130 }
Adam Langley95c29f32014-06-20 12:00:00 -07001131
Adam Langleyfcf25832014-12-18 17:42:32 -08001132 s->s3->alert_dispatch = 1;
1133 s->s3->send_alert[0] = level;
1134 s->s3->send_alert[1] = desc;
1135 if (s->s3->wbuf.left == 0) {
1136 /* data is still being written out. */
1137 return s->method->ssl_dispatch_alert(s);
1138 }
Adam Langley95c29f32014-06-20 12:00:00 -07001139
Adam Langleyfcf25832014-12-18 17:42:32 -08001140 /* else data is still being written out, we will get written some time in the
1141 * future */
1142 return -1;
1143}
Adam Langley95c29f32014-06-20 12:00:00 -07001144
Adam Langleyfcf25832014-12-18 17:42:32 -08001145int ssl3_dispatch_alert(SSL *s) {
1146 int i, j;
1147 void (*cb)(const SSL *ssl, int type, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -07001148
Adam Langleyfcf25832014-12-18 17:42:32 -08001149 s->s3->alert_dispatch = 0;
1150 i = do_ssl3_write(s, SSL3_RT_ALERT, &s->s3->send_alert[0], 2, 0, 0);
1151 if (i <= 0) {
1152 s->s3->alert_dispatch = 1;
1153 } else {
1154 /* Alert sent to BIO. If it is important, flush it now. If the message
1155 * does not get sent due to non-blocking IO, we will not worry too much. */
1156 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
1157 BIO_flush(s->wbio);
1158 }
Adam Langley95c29f32014-06-20 12:00:00 -07001159
Adam Langleyfcf25832014-12-18 17:42:32 -08001160 if (s->msg_callback) {
1161 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
1162 s->msg_callback_arg);
1163 }
1164
1165 if (s->info_callback != NULL) {
1166 cb = s->info_callback;
1167 } else if (s->ctx->info_callback != NULL) {
1168 cb = s->ctx->info_callback;
1169 }
1170
1171 if (cb != NULL) {
1172 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
1173 cb(s, SSL_CB_WRITE_ALERT, j);
1174 }
1175 }
1176
1177 return i;
1178}