blob: c4f8f0ca8d0759a7d8f0548107b0e83b06e55709 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* DTLS implementation written by Nagendra Modadugu
2 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. */
3/* ====================================================================
4 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this
19 * software must display the following acknowledgment:
20 * "This product includes software developed by the OpenSSL Project
21 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 *
23 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24 * endorse or promote products derived from this software without
25 * prior written permission. For written permission, please contact
26 * openssl-core@openssl.org.
27 *
28 * 5. Products derived from this software may not be called "OpenSSL"
29 * nor may "OpenSSL" appear in their names without prior written
30 * permission of the OpenSSL Project.
31 *
32 * 6. Redistributions of any form whatsoever must retain the following
33 * acknowledgment:
34 * "This product includes software developed by the OpenSSL Project
35 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48 * OF THE POSSIBILITY OF SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This product includes cryptographic software written by Eric Young
52 * (eay@cryptsoft.com). This product includes software written by Tim
53 * Hudson (tjh@cryptsoft.com).
54 *
55 */
56/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
57 * All rights reserved.
58 *
59 * This package is an SSL implementation written
60 * by Eric Young (eay@cryptsoft.com).
61 * The implementation was written so as to conform with Netscapes SSL.
62 *
63 * This library is free for commercial and non-commercial use as long as
64 * the following conditions are aheared to. The following conditions
65 * apply to all code found in this distribution, be it the RC4, RSA,
66 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
67 * included with this distribution is covered by the same copyright terms
68 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
69 *
70 * Copyright remains Eric Young's, and as such any Copyright notices in
71 * the code are not to be removed.
72 * If this package is used in a product, Eric Young should be given attribution
73 * as the author of the parts of the library used.
74 * This can be in the form of a textual message at program startup or
75 * in documentation (online or textual) provided with the package.
76 *
77 * Redistribution and use in source and binary forms, with or without
78 * modification, are permitted provided that the following conditions
79 * are met:
80 * 1. Redistributions of source code must retain the copyright
81 * notice, this list of conditions and the following disclaimer.
82 * 2. Redistributions in binary form must reproduce the above copyright
83 * notice, this list of conditions and the following disclaimer in the
84 * documentation and/or other materials provided with the distribution.
85 * 3. All advertising materials mentioning features or use of this software
86 * must display the following acknowledgement:
87 * "This product includes cryptographic software written by
88 * Eric Young (eay@cryptsoft.com)"
89 * The word 'cryptographic' can be left out if the rouines from the library
90 * being used are not cryptographic related :-).
91 * 4. If you include any Windows specific code (or a derivative thereof) from
92 * the apps directory (application code) you must include an acknowledgement:
93 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
94 *
95 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
96 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
98 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
101 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
103 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
104 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105 * SUCH DAMAGE.
106 *
107 * The licence and distribution terms for any publically available version or
108 * derivative of this code cannot be changed. i.e. this code cannot simply be
109 * copied and put under another distribution licence
110 * [including the GNU Public Licence.] */
111
112#include <stdio.h>
113#include <errno.h>
114#include <assert.h>
115
116#include <openssl/buf.h>
117#include <openssl/mem.h>
118#include <openssl/evp.h>
119#include <openssl/err.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800123
124
Adam Langley95c29f32014-06-20 12:00:00 -0700125/* mod 128 saturating subtract of two 64-bit values in big-endian order */
Adam Langley71d8a082014-12-13 16:28:18 -0800126static int satsub64be(const uint8_t *v1, const uint8_t *v2) {
127 int ret, sat, brw, i;
Adam Langley95c29f32014-06-20 12:00:00 -0700128
Adam Langley71d8a082014-12-13 16:28:18 -0800129 if (sizeof(long) == 8) {
130 do {
131 const union {
132 long one;
133 char little;
134 } is_endian = {1};
135 long l;
Adam Langley95c29f32014-06-20 12:00:00 -0700136
Adam Langley71d8a082014-12-13 16:28:18 -0800137 if (is_endian.little) {
138 break;
139 }
140 /* not reached on little-endians */
141 /* following test is redundant, because input is
142 * always aligned, but I take no chances... */
143 if (((size_t)v1 | (size_t)v2) & 0x7) {
144 break;
145 }
Adam Langley95c29f32014-06-20 12:00:00 -0700146
Adam Langley71d8a082014-12-13 16:28:18 -0800147 l = *((long *)v1);
148 l -= *((long *)v2);
149 if (l > 128) {
150 return 128;
151 } else if (l < -128) {
152 return -128;
153 } else {
154 return (int)l;
155 }
156 } while (0);
157 }
Adam Langley95c29f32014-06-20 12:00:00 -0700158
Adam Langley71d8a082014-12-13 16:28:18 -0800159 ret = (int)v1[7] - (int)v2[7];
160 sat = 0;
161 brw = ret >> 8; /* brw is either 0 or -1 */
162 if (ret & 0x80) {
163 for (i = 6; i >= 0; i--) {
164 brw += (int)v1[i] - (int)v2[i];
165 sat |= ~brw;
166 brw >>= 8;
167 }
168 } else {
169 for (i = 6; i >= 0; i--) {
170 brw += (int)v1[i] - (int)v2[i];
171 sat |= brw;
172 brw >>= 8;
173 }
174 }
175 brw <<= 8; /* brw is either 0 or -256 */
Adam Langley95c29f32014-06-20 12:00:00 -0700176
Adam Langley71d8a082014-12-13 16:28:18 -0800177 if (sat & 0xff) {
178 return brw | 0x80;
179 } else {
180 return brw + (ret & 0xFF);
181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182}
183
Adam Langley95c29f32014-06-20 12:00:00 -0700184static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap);
185static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap);
Adam Langley95c29f32014-06-20 12:00:00 -0700186static int dtls1_process_record(SSL *s);
Adam Langley71d8a082014-12-13 16:28:18 -0800187static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
188 unsigned int len);
Adam Langley95c29f32014-06-20 12:00:00 -0700189
Adam Langley71d8a082014-12-13 16:28:18 -0800190static int dtls1_process_record(SSL *s) {
David Benjaminb8a56f12014-12-23 11:41:02 -0500191 int al;
Adam Langley71d8a082014-12-13 16:28:18 -0800192 SSL3_RECORD *rr;
Adam Langley71d8a082014-12-13 16:28:18 -0800193
194 rr = &(s->s3->rrec);
Adam Langley71d8a082014-12-13 16:28:18 -0800195
196 /* At this point, s->packet_length == SSL3_RT_HEADER_LNGTH + rr->length, and
197 * we have that many bytes in s->packet. */
198 rr->input = &(s->packet[DTLS1_RT_HEADER_LENGTH]);
199
200 /* ok, we can now read from 's->packet' data into 'rr' rr->input points at
201 * rr->length bytes, which need to be copied into rr->data by either the
202 * decryption or by the decompression When the data is 'copied' into the
203 * rr->data buffer, rr->input will be pointed at the new buffer */
204
205 /* We now have - encrypted [ MAC [ compressed [ plain ] ] ] rr->length bytes
206 * of encrypted compressed stuff. */
207
208 /* check is not needed I believe */
209 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
210 al = SSL_AD_RECORD_OVERFLOW;
211 OPENSSL_PUT_ERROR(SSL, dtls1_process_record,
212 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
213 goto f_err;
214 }
215
216 /* decrypt in place in 'rr->input' */
217 rr->data = rr->input;
218
David Benjamin1e52eca2015-01-22 15:33:51 -0500219 if (!s->enc_method->enc(s, 0)) {
David Benjamin5fa3eba2015-01-22 16:35:40 -0500220 /* Bad packets are silently dropped in DTLS. Clear the error queue of any
221 * errors decryption may have added. */
222 ERR_clear_error();
Adam Langley71d8a082014-12-13 16:28:18 -0800223 rr->length = 0;
224 s->packet_length = 0;
225 goto err;
226 }
Adam Langley71d8a082014-12-13 16:28:18 -0800227
228 if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {
229 al = SSL_AD_RECORD_OVERFLOW;
230 OPENSSL_PUT_ERROR(SSL, dtls1_process_record, SSL_R_DATA_LENGTH_TOO_LONG);
231 goto f_err;
232 }
233
234 rr->off = 0;
235 /* So at this point the following is true
236 * ssl->s3->rrec.type is the type of record
237 * ssl->s3->rrec.length == number of bytes in record
238 * ssl->s3->rrec.off == offset to first valid byte
239 * ssl->s3->rrec.data == where to take bytes from, increment
240 * after use :-). */
241
242 /* we have pulled in a full packet so zero things */
243 s->packet_length = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800244 return 1;
245
246f_err:
247 ssl3_send_alert(s, SSL3_AL_FATAL, al);
248
249err:
250 return 0;
251}
Adam Langley95c29f32014-06-20 12:00:00 -0700252
253/* Call this to get a new input record.
254 * It will return <= 0 if more data is needed, normally due to an error
255 * or non-blocking IO.
256 * When it finishes, one packet has been decoded and can be found in
257 * ssl->s3->rrec.type - is the type of record
Adam Langley71d8a082014-12-13 16:28:18 -0800258 * ssl->s3->rrec.data, - data
Adam Langley95c29f32014-06-20 12:00:00 -0700259 * ssl->s3->rrec.length, - number of bytes
Adam Langley71d8a082014-12-13 16:28:18 -0800260 *
261 * used only by dtls1_read_bytes */
262int dtls1_get_record(SSL *s) {
263 int ssl_major, ssl_minor;
264 int i, n;
265 SSL3_RECORD *rr;
266 unsigned char *p = NULL;
267 unsigned short version;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley71d8a082014-12-13 16:28:18 -0800269 rr = &(s->s3->rrec);
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 /* get something from the wire */
Adam Langley95c29f32014-06-20 12:00:00 -0700272again:
Adam Langley71d8a082014-12-13 16:28:18 -0800273 /* check if we have the header */
274 if ((s->rstate != SSL_ST_READ_BODY) ||
275 (s->packet_length < DTLS1_RT_HEADER_LENGTH)) {
276 n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, s->s3->rbuf.len, 0);
277 /* read timeout is handled by dtls1_read_bytes */
278 if (n <= 0) {
279 return n; /* error or non-blocking */
280 }
Adam Langley95c29f32014-06-20 12:00:00 -0700281
Adam Langley71d8a082014-12-13 16:28:18 -0800282 /* this packet contained a partial record, dump it */
283 if (s->packet_length != DTLS1_RT_HEADER_LENGTH) {
284 s->packet_length = 0;
285 goto again;
286 }
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley71d8a082014-12-13 16:28:18 -0800288 s->rstate = SSL_ST_READ_BODY;
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley71d8a082014-12-13 16:28:18 -0800290 p = s->packet;
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley71d8a082014-12-13 16:28:18 -0800292 if (s->msg_callback) {
293 s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH, s,
294 s->msg_callback_arg);
295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
Adam Langley71d8a082014-12-13 16:28:18 -0800297 /* Pull apart the header into the DTLS1_RECORD */
298 rr->type = *(p++);
299 ssl_major = *(p++);
300 ssl_minor = *(p++);
301 version = (ssl_major << 8) | ssl_minor;
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langley71d8a082014-12-13 16:28:18 -0800303 /* sequence number is 64 bits, with top 2 bytes = epoch */
304 n2s(p, rr->epoch);
Adam Langley95c29f32014-06-20 12:00:00 -0700305
Adam Langley71d8a082014-12-13 16:28:18 -0800306 memcpy(&(s->s3->read_sequence[2]), p, 6);
307 p += 6;
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 n2s(p, rr->length);
Adam Langley95c29f32014-06-20 12:00:00 -0700310
Adam Langley71d8a082014-12-13 16:28:18 -0800311 /* Lets check version */
312 if (s->s3->have_version) {
313 if (version != s->version) {
314 /* The record's version doesn't match, so silently drop it.
315 *
316 * TODO(davidben): This doesn't work. The DTLS record layer is not
317 * packet-based, so the remainder of the packet isn't dropped and we
318 * get a framing error. It's also unclear what it means to silently
319 * drop a record in a packet containing two records. */
320 rr->length = 0;
321 s->packet_length = 0;
322 goto again;
323 }
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
Adam Langley71d8a082014-12-13 16:28:18 -0800326 if ((version & 0xff00) != (s->version & 0xff00)) {
327 /* wrong version, silently discard record */
328 rr->length = 0;
329 s->packet_length = 0;
330 goto again;
331 }
Adam Langley95c29f32014-06-20 12:00:00 -0700332
Adam Langley71d8a082014-12-13 16:28:18 -0800333 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
334 /* record too long, silently discard it */
335 rr->length = 0;
336 s->packet_length = 0;
337 goto again;
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley71d8a082014-12-13 16:28:18 -0800340 /* now s->rstate == SSL_ST_READ_BODY */
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
Adam Langley71d8a082014-12-13 16:28:18 -0800343 /* s->rstate == SSL_ST_READ_BODY, get and decode the data */
Adam Langley95c29f32014-06-20 12:00:00 -0700344
Adam Langley71d8a082014-12-13 16:28:18 -0800345 if (rr->length > s->packet_length - DTLS1_RT_HEADER_LENGTH) {
346 /* now s->packet_length == DTLS1_RT_HEADER_LENGTH */
347 i = rr->length;
348 n = ssl3_read_n(s, i, i, 1);
349 if (n <= 0) {
350 return n; /* error or non-blocking io */
351 }
Adam Langley95c29f32014-06-20 12:00:00 -0700352
Adam Langley71d8a082014-12-13 16:28:18 -0800353 /* this packet contained a partial record, dump it */
354 if (n != i) {
355 rr->length = 0;
356 s->packet_length = 0;
357 goto again;
358 }
Adam Langley95c29f32014-06-20 12:00:00 -0700359
Adam Langley71d8a082014-12-13 16:28:18 -0800360 /* now n == rr->length,
361 * and s->packet_length == DTLS1_RT_HEADER_LENGTH + rr->length */
362 }
363 s->rstate = SSL_ST_READ_HEADER; /* set state for later operations */
Adam Langley95c29f32014-06-20 12:00:00 -0700364
David Benjamin0afbcc02015-04-05 04:06:20 -0400365 if (rr->epoch != s->d1->r_epoch) {
366 /* This record is from the wrong epoch. If it is the next epoch, it could be
367 * buffered. For simplicity, drop it and expect retransmit to handle it
368 * later; DTLS is supposed to handle packet loss. */
Adam Langley71d8a082014-12-13 16:28:18 -0800369 rr->length = 0;
David Benjamin0afbcc02015-04-05 04:06:20 -0400370 s->packet_length = 0;
371 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800372 }
Adam Langley95c29f32014-06-20 12:00:00 -0700373
Adam Langley71d8a082014-12-13 16:28:18 -0800374 /* Check whether this is a repeat, or aged record. */
David Benjamin0afbcc02015-04-05 04:06:20 -0400375 if (!dtls1_record_replay_check(s, &s->d1->bitmap)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800376 rr->length = 0;
377 s->packet_length = 0; /* dump this record */
378 goto again; /* get another record */
379 }
Adam Langley95c29f32014-06-20 12:00:00 -0700380
Adam Langley71d8a082014-12-13 16:28:18 -0800381 /* just read a 0 length packet */
382 if (rr->length == 0) {
383 goto again;
384 }
Adam Langley95c29f32014-06-20 12:00:00 -0700385
Adam Langley71d8a082014-12-13 16:28:18 -0800386 if (!dtls1_process_record(s)) {
387 rr->length = 0;
388 s->packet_length = 0; /* dump this record */
389 goto again; /* get another record */
390 }
David Benjamin0afbcc02015-04-05 04:06:20 -0400391 dtls1_record_bitmap_update(s, &s->d1->bitmap); /* Mark receipt of record. */
Adam Langley95c29f32014-06-20 12:00:00 -0700392
Adam Langley71d8a082014-12-13 16:28:18 -0800393 return 1;
394}
Adam Langley95c29f32014-06-20 12:00:00 -0700395
396/* Return up to 'len' payload bytes received in 'type' records.
397 * 'type' is one of the following:
398 *
399 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
400 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
401 * - 0 (during a shutdown, no data has to be returned)
402 *
403 * If we don't have stored data to work from, read a SSL/TLS record first
404 * (possibly multiple records if we still don't have anything to return).
405 *
406 * This function must handle any surprises the peer may have for us, such as
407 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
408 * a surprise, but handled as if it were), or renegotiation requests.
409 * Also if record payloads contain fragments too small to process, we store
410 * them until there is enough for the respective protocol (the record protocol
411 * may use arbitrary fragmentation and even interleaving):
412 * Change cipher spec protocol
413 * just 1 byte needed, no need for keeping anything stored
414 * Alert protocol
415 * 2 bytes needed (AlertLevel, AlertDescription)
416 * Handshake protocol
417 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
418 * to detect unexpected Client Hello and Hello Request messages
419 * here, anything else is handled by higher layers
420 * Application data protocol
421 * none of our business
422 */
Adam Langley71d8a082014-12-13 16:28:18 -0800423int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500424 int al, i, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800425 unsigned int n;
426 SSL3_RECORD *rr;
427 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700428
Adam Langley71d8a082014-12-13 16:28:18 -0800429 /* XXX: check what the second '&& type' is about */
430 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
431 (type != SSL3_RT_HANDSHAKE) && type) ||
432 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
433 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, ERR_R_INTERNAL_ERROR);
434 return -1;
435 }
Adam Langley95c29f32014-06-20 12:00:00 -0700436
Adam Langley71d8a082014-12-13 16:28:18 -0800437 if (!s->in_handshake && SSL_in_init(s)) {
438 /* type == SSL3_RT_APPLICATION_DATA */
439 i = s->handshake_func(s);
440 if (i < 0) {
441 return i;
442 }
443 if (i == 0) {
444 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_SSL_HANDSHAKE_FAILURE);
445 return -1;
446 }
447 }
Adam Langley95c29f32014-06-20 12:00:00 -0700448
David Benjamine0e7d0d2015-02-08 19:33:25 -0500449 if (s->s3->rbuf.buf == NULL && !ssl3_setup_buffers(s)) {
450 /* TODO(davidben): Is this redundant with the calls in the handshake? */
451 return -1;
452 }
453
Adam Langley95c29f32014-06-20 12:00:00 -0700454start:
Adam Langley71d8a082014-12-13 16:28:18 -0800455 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700456
Adam Langley71d8a082014-12-13 16:28:18 -0800457 /* s->s3->rrec.type - is the type of record
458 * s->s3->rrec.data - data
459 * s->s3->rrec.off - offset into 'data' for next read
460 * s->s3->rrec.length - number of bytes. */
461 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langley71d8a082014-12-13 16:28:18 -0800463 /* Check for timeout */
464 if (dtls1_handle_timeout(s) > 0) {
465 goto start;
466 }
Adam Langley95c29f32014-06-20 12:00:00 -0700467
Adam Langley71d8a082014-12-13 16:28:18 -0800468 /* get new packet if necessary */
469 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
470 ret = dtls1_get_record(s);
471 if (ret <= 0) {
472 ret = dtls1_read_failed(s, ret);
473 /* anything other than a timeout is an error */
474 if (ret <= 0) {
475 return ret;
476 } else {
477 goto start;
478 }
479 }
480 }
Adam Langley95c29f32014-06-20 12:00:00 -0700481
Adam Langley71d8a082014-12-13 16:28:18 -0800482 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700483
Adam Langley71d8a082014-12-13 16:28:18 -0800484 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
485 * ssl3_get_finished. */
David Benjamindc3da932015-03-12 15:09:02 -0400486 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
487 rr->type != SSL3_RT_ALERT) {
488 /* We now have an unexpected record between CCS and Finished. Most likely
David Benjamin4417d052015-04-05 04:17:25 -0400489 * the packets were reordered on their way. DTLS is unreliable, so drop the
490 * packet and expect the peer to retransmit. */
Adam Langley71d8a082014-12-13 16:28:18 -0800491 rr->length = 0;
492 goto start;
493 }
Adam Langley95c29f32014-06-20 12:00:00 -0700494
Adam Langley71d8a082014-12-13 16:28:18 -0800495 /* If the other end has shut down, throw anything we read away (even in
496 * 'peek' mode) */
497 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
498 rr->length = 0;
499 s->rwstate = SSL_NOTHING;
500 return 0;
501 }
Adam Langley95c29f32014-06-20 12:00:00 -0700502
503
Adam Langley71d8a082014-12-13 16:28:18 -0800504 if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
505 /* make sure that we are not getting application data when we
506 * are doing a handshake for the first time */
507 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500508 (s->aead_read_ctx == NULL)) {
509 /* TODO(davidben): Is this check redundant with the handshake_func
510 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800511 al = SSL_AD_UNEXPECTED_MESSAGE;
512 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
513 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700514 }
515
Adam Langley71d8a082014-12-13 16:28:18 -0800516 if (len <= 0) {
517 return len;
518 }
Adam Langley95c29f32014-06-20 12:00:00 -0700519
Adam Langley71d8a082014-12-13 16:28:18 -0800520 if ((unsigned int)len > rr->length) {
521 n = rr->length;
522 } else {
523 n = (unsigned int)len;
524 }
Adam Langley95c29f32014-06-20 12:00:00 -0700525
Adam Langley71d8a082014-12-13 16:28:18 -0800526 memcpy(buf, &(rr->data[rr->off]), n);
527 if (!peek) {
528 rr->length -= n;
529 rr->off += n;
530 if (rr->length == 0) {
531 s->rstate = SSL_ST_READ_HEADER;
532 rr->off = 0;
533 }
534 }
Adam Langley95c29f32014-06-20 12:00:00 -0700535
Adam Langley71d8a082014-12-13 16:28:18 -0800536 return n;
537 }
Adam Langley95c29f32014-06-20 12:00:00 -0700538
David Benjamin0ea8dda2015-01-31 20:33:40 -0500539 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700540
David Benjamin0ea8dda2015-01-31 20:33:40 -0500541 /* If an alert record, process one alert out of the record. Note that we allow
542 * a single record to contain multiple alerts. */
543 if (rr->type == SSL3_RT_ALERT) {
544 /* Alerts may not be fragmented. */
545 if (rr->length < 2) {
546 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700547 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500548 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800549 }
Adam Langley71d8a082014-12-13 16:28:18 -0800550
Adam Langley71d8a082014-12-13 16:28:18 -0800551 if (s->msg_callback) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500552 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800553 s->msg_callback_arg);
554 }
David Benjamin86058a22015-02-22 13:07:21 -0500555 const uint8_t alert_level = rr->data[rr->off++];
556 const uint8_t alert_descr = rr->data[rr->off++];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500557 rr->length -= 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800558
559 if (s->info_callback != NULL) {
560 cb = s->info_callback;
561 } else if (s->ctx->info_callback != NULL) {
562 cb = s->ctx->info_callback;
563 }
564
565 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500566 uint16_t alert = (alert_level << 8) | alert_descr;
567 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800568 }
569
David Benjamin86058a22015-02-22 13:07:21 -0500570 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800571 s->s3->warn_alert = alert_descr;
572 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
573 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
574 return 0;
575 }
David Benjamin86058a22015-02-22 13:07:21 -0500576 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800577 char tmp[16];
578
579 s->rwstate = SSL_NOTHING;
580 s->s3->fatal_alert = alert_descr;
581 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes,
582 SSL_AD_REASON_OFFSET + alert_descr);
583 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
584 ERR_add_error_data(2, "SSL alert number ", tmp);
585 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
586 SSL_CTX_remove_session(s->ctx, s->session);
587 return 0;
588 } else {
589 al = SSL_AD_ILLEGAL_PARAMETER;
590 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
591 goto f_err;
592 }
593
594 goto start;
595 }
596
597 if (s->shutdown & SSL_SENT_SHUTDOWN) {
598 /* but we have not received a shutdown */
599 s->rwstate = SSL_NOTHING;
600 rr->length = 0;
601 return 0;
602 }
603
604 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
David Benjaminf7f0f3a2015-03-02 21:09:31 -0500605 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
606 * record payload has to look like */
607 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800608 al = SSL_AD_ILLEGAL_PARAMETER;
609 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
610 goto f_err;
611 }
612
613 rr->length = 0;
614
615 if (s->msg_callback) {
616 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
617 s->msg_callback_arg);
618 }
619
620 /* We can't process a CCS now, because previous handshake
621 * messages are still missing, so just drop it.
622 */
623 if (!s->d1->change_cipher_spec_ok) {
624 goto start;
625 }
626
627 s->d1->change_cipher_spec_ok = 0;
628
629 s->s3->change_cipher_spec = 1;
630 if (!ssl3_do_change_cipher_spec(s)) {
631 goto err;
632 }
633
634 /* do this whenever CCS is processed */
635 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
636
637 goto start;
638 }
639
David Benjamin0ea8dda2015-01-31 20:33:40 -0500640 /* Unexpected handshake message. It may be a retransmitted Finished (the only
641 * post-CCS message). Otherwise, it's a pre-CCS handshake message from an
642 * unsupported renegotiation attempt. */
643 if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) {
644 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
645 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700646 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500647 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800648 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500649 struct hm_header_st msg_hdr;
650 dtls1_get_message_header(&rr->data[rr->off], &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800651
David Benjamin7eaab4c2015-03-02 19:01:16 -0500652 /* Ignore a stray Finished from the previous handshake. */
Adam Langley71d8a082014-12-13 16:28:18 -0800653 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500654 if (msg_hdr.frag_off == 0) {
655 /* Retransmit our last flight of messages. If the peer sends the second
656 * Finished, they may not have received ours. Only do this for the
657 * first fragment, in case the Finished was fragmented. */
658 if (dtls1_check_timeout_num(s) < 0) {
659 return -1;
660 }
661
662 dtls1_retransmit_buffered_messages(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800663 }
664
Adam Langley71d8a082014-12-13 16:28:18 -0800665 rr->length = 0;
666 goto start;
667 }
Adam Langley71d8a082014-12-13 16:28:18 -0800668 }
669
David Benjaminddb9f152015-02-03 15:44:39 -0500670 /* We already handled these. */
671 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT);
Adam Langley71d8a082014-12-13 16:28:18 -0800672
David Benjaminddb9f152015-02-03 15:44:39 -0500673 al = SSL_AD_UNEXPECTED_MESSAGE;
674 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800675
676f_err:
677 ssl3_send_alert(s, SSL3_AL_FATAL, al);
678err:
679 return -1;
680}
681
682int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) {
683 int i;
684
685 if (SSL_in_init(s) && !s->in_handshake) {
686 i = s->handshake_func(s);
687 if (i < 0) {
688 return i;
689 }
690 if (i == 0) {
691 OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes,
692 SSL_R_SSL_HANDSHAKE_FAILURE);
693 return -1;
694 }
695 }
696
697 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
698 OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes,
699 SSL_R_DTLS_MESSAGE_TOO_BIG);
700 return -1;
701 }
702
703 i = dtls1_write_bytes(s, type, buf_, len);
704 return i;
705}
706
Adam Langley71d8a082014-12-13 16:28:18 -0800707/* Call this to write data in records of type 'type' It will return <= 0 if not
708 * all data has been sent or non-blocking IO. */
709int dtls1_write_bytes(SSL *s, int type, const void *buf, int len) {
710 int i;
711
712 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
713 s->rwstate = SSL_NOTHING;
714 i = do_dtls1_write(s, type, buf, len);
715 return i;
716}
717
718static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
719 unsigned int len) {
720 uint8_t *p, *pseq;
David Benjaminb8a56f12014-12-23 11:41:02 -0500721 int i;
Adam Langley71d8a082014-12-13 16:28:18 -0800722 int prefix_len = 0;
723 int eivlen = 0;
724 SSL3_RECORD *wr;
725 SSL3_BUFFER *wb;
Adam Langley71d8a082014-12-13 16:28:18 -0800726
David Benjamin883e49f2015-04-05 12:19:14 -0400727 /* ssl3_write_pending drops the write if |BIO_write| fails in DTLS, so there
728 * is never pending data. */
729 assert(s->s3->wbuf.left == 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800730
731 /* If we have an alert to send, lets send it */
732 if (s->s3->alert_dispatch) {
733 i = s->method->ssl_dispatch_alert(s);
734 if (i <= 0) {
735 return i;
736 }
737 /* if it went, fall through and send more stuff */
738 }
739
740 if (len == 0) {
741 return 0;
742 }
743
744 wr = &(s->s3->wrec);
745 wb = &(s->s3->wbuf);
Adam Langley71d8a082014-12-13 16:28:18 -0800746
747 p = wb->buf + prefix_len;
748
749 /* write the header */
750
751 *(p++) = type & 0xff;
752 wr->type = type;
753 /* Special case: for hello verify request, client version 1.0 and
754 * we haven't decided which version to use yet send back using
755 * version 1.0 header: otherwise some clients will ignore it.
756 */
757 if (!s->s3->have_version) {
758 *(p++) = DTLS1_VERSION >> 8;
759 *(p++) = DTLS1_VERSION & 0xff;
760 } else {
761 *(p++) = s->version >> 8;
762 *(p++) = s->version & 0xff;
763 }
764
765 /* field where we are to write out packet epoch, seq num and len */
766 pseq = p;
767 p += 10;
768
David Benjaminb8a56f12014-12-23 11:41:02 -0500769 /* Leave room for the variable nonce for AEADs which specify it explicitly. */
770 if (s->aead_write_ctx != NULL &&
771 s->aead_write_ctx->variable_nonce_included_in_record) {
David Benjamine95d20d2014-12-23 11:16:01 -0500772 eivlen = s->aead_write_ctx->variable_nonce_len;
Adam Langley71d8a082014-12-13 16:28:18 -0800773 }
774
David Benjamina58c5782015-04-04 18:19:16 -0400775 /* Assemble the input for |s->enc_method->enc|. The input is the plaintext
776 * with |eivlen| bytes of space prepended for the explicit nonce. */
Adam Langley71d8a082014-12-13 16:28:18 -0800777 wr->input = p;
David Benjamina58c5782015-04-04 18:19:16 -0400778 wr->length = eivlen + len;
779 memcpy(p + eivlen, buf, len);
780
781 /* Encrypt in-place, so the output also goes into |p|. */
Adam Langley71d8a082014-12-13 16:28:18 -0800782 wr->data = p;
Adam Langley71d8a082014-12-13 16:28:18 -0800783
David Benjamin1e52eca2015-01-22 15:33:51 -0500784 if (!s->enc_method->enc(s, 1)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800785 goto err;
786 }
787
788 /* there's only one epoch between handshake and app data */
789 s2n(s->d1->w_epoch, pseq);
790
791 memcpy(pseq, &(s->s3->write_sequence[2]), 6);
792 pseq += 6;
793 s2n(wr->length, pseq);
794
795 if (s->msg_callback) {
796 s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
797 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
798 }
799
800 /* we should now have wr->data pointing to the encrypted data, which is
801 * wr->length long */
802 wr->type = type; /* not needed but helps for debugging */
803 wr->length += DTLS1_RT_HEADER_LENGTH;
804
David Benjamind81e73d2015-04-05 00:21:39 -0400805 if (!ssl3_record_sequence_update(&s->s3->write_sequence[2], 6)) {
806 goto err;
807 }
Adam Langley71d8a082014-12-13 16:28:18 -0800808
809 /* now let's set up wb */
810 wb->left = prefix_len + wr->length;
811 wb->offset = 0;
812
813 /* memorize arguments so that ssl3_write_pending can detect bad write retries
814 * later */
815 s->s3->wpend_tot = len;
816 s->s3->wpend_buf = buf;
817 s->s3->wpend_type = type;
818 s->s3->wpend_ret = len;
819
820 /* we now just need to write the buffer */
821 return ssl3_write_pending(s, type, buf, len);
822
823err:
824 return -1;
825}
826
827static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap) {
828 int cmp;
829 unsigned int shift;
830 const uint8_t *seq = s->s3->read_sequence;
831
832 cmp = satsub64be(seq, bitmap->max_seq_num);
833 if (cmp > 0) {
834 memcpy(s->s3->rrec.seq_num, seq, 8);
835 return 1; /* this record in new */
836 }
837 shift = -cmp;
838 if (shift >= sizeof(bitmap->map) * 8) {
839 return 0; /* stale, outside the window */
840 } else if (bitmap->map & (((uint64_t)1) << shift)) {
841 return 0; /* record previously received */
842 }
843
844 memcpy(s->s3->rrec.seq_num, seq, 8);
845 return 1;
846}
847
848static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) {
849 int cmp;
850 unsigned int shift;
851 const uint8_t *seq = s->s3->read_sequence;
852
853 cmp = satsub64be(seq, bitmap->max_seq_num);
854 if (cmp > 0) {
855 shift = cmp;
856 if (shift < sizeof(bitmap->map) * 8) {
857 bitmap->map <<= shift, bitmap->map |= 1UL;
858 } else {
859 bitmap->map = 1UL;
860 }
861 memcpy(bitmap->max_seq_num, seq, 8);
862 } else {
863 shift = -cmp;
864 if (shift < sizeof(bitmap->map) * 8) {
865 bitmap->map |= ((uint64_t)1) << shift;
866 }
867 }
868}
869
870int dtls1_dispatch_alert(SSL *s) {
871 int i, j;
872 void (*cb)(const SSL *ssl, int type, int val) = NULL;
873 uint8_t buf[DTLS1_AL_HEADER_LENGTH];
874 uint8_t *ptr = &buf[0];
875
876 s->s3->alert_dispatch = 0;
877
878 memset(buf, 0x00, sizeof(buf));
879 *ptr++ = s->s3->send_alert[0];
880 *ptr++ = s->s3->send_alert[1];
881
882 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf));
883 if (i <= 0) {
884 s->s3->alert_dispatch = 1;
885 } else {
886 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
887 (void)BIO_flush(s->wbio);
888 }
889
890 if (s->msg_callback) {
891 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
892 s->msg_callback_arg);
893 }
894
895 if (s->info_callback != NULL) {
896 cb = s->info_callback;
897 } else if (s->ctx->info_callback != NULL) {
898 cb = s->ctx->info_callback;
899 }
900
901 if (cb != NULL) {
902 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
903 cb(s, SSL_CB_WRITE_ALERT, j);
904 }
905 }
906
907 return i;
908}
909
Adam Langley71d8a082014-12-13 16:28:18 -0800910void dtls1_reset_seq_numbers(SSL *s, int rw) {
911 uint8_t *seq;
912 unsigned int seq_bytes = sizeof(s->s3->read_sequence);
913
914 if (rw & SSL3_CC_READ) {
915 seq = s->s3->read_sequence;
916 s->d1->r_epoch++;
David Benjamin0afbcc02015-04-05 04:06:20 -0400917 memset(&s->d1->bitmap, 0, sizeof(DTLS1_BITMAP));
Adam Langley71d8a082014-12-13 16:28:18 -0800918 } else {
919 seq = s->s3->write_sequence;
920 memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence));
921 s->d1->w_epoch++;
922 }
923
924 memset(seq, 0x00, seq_bytes);
925}