blob: e80e773bb107709d768bb436de831d4161dd8ffb [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
Adam Langley95c29f32014-06-20 12:00:00 -0700112#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <stdio.h>
114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
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;
David Benjamin31a07792015-03-03 14:20:26 -0500192 SSL3_RECORD *rr = &s->s3->rrec;
Adam Langley71d8a082014-12-13 16:28:18 -0800193
194 /* check is not needed I believe */
195 if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
196 al = SSL_AD_RECORD_OVERFLOW;
197 OPENSSL_PUT_ERROR(SSL, dtls1_process_record,
198 SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
199 goto f_err;
200 }
201
David Benjamin31a07792015-03-03 14:20:26 -0500202 /* |rr->data| points to |rr->length| bytes of ciphertext in |s->packet|. */
203 rr->data = &s->packet[DTLS1_RT_HEADER_LENGTH];
Adam Langley71d8a082014-12-13 16:28:18 -0800204
David Benjamin31a07792015-03-03 14:20:26 -0500205 uint8_t seq[8];
206 seq[0] = rr->epoch >> 8;
207 seq[1] = rr->epoch & 0xff;
208 memcpy(&seq[2], &rr->seq_num[2], 6);
209
210 /* Decrypt the packet in-place. Note it is important that |SSL_AEAD_CTX_open|
211 * not write beyond |rr->length|. There may be another record in the packet.
212 *
213 * TODO(davidben): This assumes |s->version| is the same as the record-layer
214 * version which isn't always true, but it only differs with the NULL cipher
215 * which ignores the parameter. */
216 size_t plaintext_len;
217 if (!SSL_AEAD_CTX_open(s->aead_read_ctx, rr->data, &plaintext_len, rr->length,
218 rr->type, s->version, seq, rr->data, rr->length)) {
David Benjamin5fa3eba2015-01-22 16:35:40 -0500219 /* Bad packets are silently dropped in DTLS. Clear the error queue of any
220 * errors decryption may have added. */
221 ERR_clear_error();
Adam Langley71d8a082014-12-13 16:28:18 -0800222 rr->length = 0;
223 s->packet_length = 0;
224 goto err;
225 }
Adam Langley71d8a082014-12-13 16:28:18 -0800226
David Benjamin31a07792015-03-03 14:20:26 -0500227 if (plaintext_len > SSL3_RT_MAX_PLAIN_LENGTH) {
Adam Langley71d8a082014-12-13 16:28:18 -0800228 al = SSL_AD_RECORD_OVERFLOW;
229 OPENSSL_PUT_ERROR(SSL, dtls1_process_record, SSL_R_DATA_LENGTH_TOO_LONG);
230 goto f_err;
231 }
David Benjamin31a07792015-03-03 14:20:26 -0500232 assert(plaintext_len < (1u << 16));
233 rr->length = plaintext_len;
Adam Langley71d8a082014-12-13 16:28:18 -0800234
235 rr->off = 0;
236 /* So at this point the following is true
237 * ssl->s3->rrec.type is the type of record
238 * ssl->s3->rrec.length == number of bytes in record
239 * ssl->s3->rrec.off == offset to first valid byte
David Benjamin31a07792015-03-03 14:20:26 -0500240 * ssl->s3->rrec.data == the first byte of the record body. */
Adam Langley71d8a082014-12-13 16:28:18 -0800241
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) {
David Benjamin31a07792015-03-03 14:20:26 -0500263 uint8_t ssl_major, ssl_minor;
Adam Langley71d8a082014-12-13 16:28:18 -0800264 int i, n;
265 SSL3_RECORD *rr;
David Benjamin31a07792015-03-03 14:20:26 -0500266 uint8_t *p = NULL;
267 uint16_t 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)) {
David Benjaminb1f5bca2015-05-08 22:54:02 -0400276 n = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH, 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800277 /* 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++);
David Benjamin31a07792015-03-03 14:20:26 -0500301 version = (((uint16_t)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;
David Benjaminb1f5bca2015-05-08 22:54:02 -0400348 n = ssl3_read_n(s, i, 1);
Adam Langley71d8a082014-12-13 16:28:18 -0800349 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
449start:
Adam Langley71d8a082014-12-13 16:28:18 -0800450 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700451
Adam Langley71d8a082014-12-13 16:28:18 -0800452 /* s->s3->rrec.type - is the type of record
453 * s->s3->rrec.data - data
454 * s->s3->rrec.off - offset into 'data' for next read
455 * s->s3->rrec.length - number of bytes. */
456 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700457
Adam Langley71d8a082014-12-13 16:28:18 -0800458 /* Check for timeout */
David Benjamin8c249802015-05-05 09:44:18 -0400459 if (DTLSv1_handle_timeout(s) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800460 goto start;
461 }
Adam Langley95c29f32014-06-20 12:00:00 -0700462
Adam Langley71d8a082014-12-13 16:28:18 -0800463 /* get new packet if necessary */
464 if (rr->length == 0 || s->rstate == SSL_ST_READ_BODY) {
465 ret = dtls1_get_record(s);
466 if (ret <= 0) {
467 ret = dtls1_read_failed(s, ret);
468 /* anything other than a timeout is an error */
469 if (ret <= 0) {
470 return ret;
471 } else {
472 goto start;
473 }
474 }
475 }
Adam Langley95c29f32014-06-20 12:00:00 -0700476
Adam Langley71d8a082014-12-13 16:28:18 -0800477 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700478
Adam Langley71d8a082014-12-13 16:28:18 -0800479 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
480 * ssl3_get_finished. */
David Benjamindc3da932015-03-12 15:09:02 -0400481 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
482 rr->type != SSL3_RT_ALERT) {
483 /* We now have an unexpected record between CCS and Finished. Most likely
David Benjamin4417d052015-04-05 04:17:25 -0400484 * the packets were reordered on their way. DTLS is unreliable, so drop the
485 * packet and expect the peer to retransmit. */
Adam Langley71d8a082014-12-13 16:28:18 -0800486 rr->length = 0;
487 goto start;
488 }
Adam Langley95c29f32014-06-20 12:00:00 -0700489
Adam Langley71d8a082014-12-13 16:28:18 -0800490 /* If the other end has shut down, throw anything we read away (even in
491 * 'peek' mode) */
492 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
493 rr->length = 0;
494 s->rwstate = SSL_NOTHING;
495 return 0;
496 }
Adam Langley95c29f32014-06-20 12:00:00 -0700497
498
Adam Langley71d8a082014-12-13 16:28:18 -0800499 if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
500 /* make sure that we are not getting application data when we
501 * are doing a handshake for the first time */
502 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500503 (s->aead_read_ctx == NULL)) {
504 /* TODO(davidben): Is this check redundant with the handshake_func
505 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800506 al = SSL_AD_UNEXPECTED_MESSAGE;
507 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_APP_DATA_IN_HANDSHAKE);
508 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700509 }
510
Adam Langley71d8a082014-12-13 16:28:18 -0800511 if (len <= 0) {
512 return len;
513 }
Adam Langley95c29f32014-06-20 12:00:00 -0700514
Adam Langley71d8a082014-12-13 16:28:18 -0800515 if ((unsigned int)len > rr->length) {
516 n = rr->length;
517 } else {
518 n = (unsigned int)len;
519 }
Adam Langley95c29f32014-06-20 12:00:00 -0700520
Adam Langley71d8a082014-12-13 16:28:18 -0800521 memcpy(buf, &(rr->data[rr->off]), n);
522 if (!peek) {
523 rr->length -= n;
524 rr->off += n;
525 if (rr->length == 0) {
526 s->rstate = SSL_ST_READ_HEADER;
527 rr->off = 0;
528 }
529 }
Adam Langley95c29f32014-06-20 12:00:00 -0700530
Adam Langley71d8a082014-12-13 16:28:18 -0800531 return n;
532 }
Adam Langley95c29f32014-06-20 12:00:00 -0700533
David Benjamin0ea8dda2015-01-31 20:33:40 -0500534 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700535
David Benjamin0ea8dda2015-01-31 20:33:40 -0500536 /* If an alert record, process one alert out of the record. Note that we allow
537 * a single record to contain multiple alerts. */
538 if (rr->type == SSL3_RT_ALERT) {
539 /* Alerts may not be fragmented. */
540 if (rr->length < 2) {
541 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700542 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500543 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800544 }
Adam Langley71d8a082014-12-13 16:28:18 -0800545
Adam Langley71d8a082014-12-13 16:28:18 -0800546 if (s->msg_callback) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500547 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800548 s->msg_callback_arg);
549 }
David Benjamin86058a22015-02-22 13:07:21 -0500550 const uint8_t alert_level = rr->data[rr->off++];
551 const uint8_t alert_descr = rr->data[rr->off++];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500552 rr->length -= 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800553
554 if (s->info_callback != NULL) {
555 cb = s->info_callback;
556 } else if (s->ctx->info_callback != NULL) {
557 cb = s->ctx->info_callback;
558 }
559
560 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500561 uint16_t alert = (alert_level << 8) | alert_descr;
562 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800563 }
564
David Benjamin86058a22015-02-22 13:07:21 -0500565 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800566 s->s3->warn_alert = alert_descr;
567 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
568 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
569 return 0;
570 }
David Benjamin86058a22015-02-22 13:07:21 -0500571 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800572 char tmp[16];
573
574 s->rwstate = SSL_NOTHING;
575 s->s3->fatal_alert = alert_descr;
576 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes,
577 SSL_AD_REASON_OFFSET + alert_descr);
578 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
579 ERR_add_error_data(2, "SSL alert number ", tmp);
580 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
581 SSL_CTX_remove_session(s->ctx, s->session);
582 return 0;
583 } else {
584 al = SSL_AD_ILLEGAL_PARAMETER;
585 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNKNOWN_ALERT_TYPE);
586 goto f_err;
587 }
588
589 goto start;
590 }
591
592 if (s->shutdown & SSL_SENT_SHUTDOWN) {
593 /* but we have not received a shutdown */
594 s->rwstate = SSL_NOTHING;
595 rr->length = 0;
596 return 0;
597 }
598
599 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
David Benjaminf7f0f3a2015-03-02 21:09:31 -0500600 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
601 * record payload has to look like */
602 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800603 al = SSL_AD_ILLEGAL_PARAMETER;
604 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_CHANGE_CIPHER_SPEC);
605 goto f_err;
606 }
607
608 rr->length = 0;
609
610 if (s->msg_callback) {
611 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
612 s->msg_callback_arg);
613 }
614
615 /* We can't process a CCS now, because previous handshake
616 * messages are still missing, so just drop it.
617 */
618 if (!s->d1->change_cipher_spec_ok) {
619 goto start;
620 }
621
622 s->d1->change_cipher_spec_ok = 0;
623
624 s->s3->change_cipher_spec = 1;
625 if (!ssl3_do_change_cipher_spec(s)) {
626 goto err;
627 }
628
629 /* do this whenever CCS is processed */
630 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
631
632 goto start;
633 }
634
David Benjamin0ea8dda2015-01-31 20:33:40 -0500635 /* Unexpected handshake message. It may be a retransmitted Finished (the only
636 * post-CCS message). Otherwise, it's a pre-CCS handshake message from an
637 * unsupported renegotiation attempt. */
638 if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) {
639 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
640 al = SSL_AD_DECODE_ERROR;
Adam Langley5edc4e22015-03-09 13:58:39 -0700641 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500642 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800643 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500644 struct hm_header_st msg_hdr;
645 dtls1_get_message_header(&rr->data[rr->off], &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800646
David Benjamin7eaab4c2015-03-02 19:01:16 -0500647 /* Ignore a stray Finished from the previous handshake. */
Adam Langley71d8a082014-12-13 16:28:18 -0800648 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500649 if (msg_hdr.frag_off == 0) {
650 /* Retransmit our last flight of messages. If the peer sends the second
651 * Finished, they may not have received ours. Only do this for the
652 * first fragment, in case the Finished was fragmented. */
653 if (dtls1_check_timeout_num(s) < 0) {
654 return -1;
655 }
656
657 dtls1_retransmit_buffered_messages(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800658 }
659
Adam Langley71d8a082014-12-13 16:28:18 -0800660 rr->length = 0;
661 goto start;
662 }
Adam Langley71d8a082014-12-13 16:28:18 -0800663 }
664
David Benjaminddb9f152015-02-03 15:44:39 -0500665 /* We already handled these. */
666 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT);
Adam Langley71d8a082014-12-13 16:28:18 -0800667
David Benjaminddb9f152015-02-03 15:44:39 -0500668 al = SSL_AD_UNEXPECTED_MESSAGE;
669 OPENSSL_PUT_ERROR(SSL, dtls1_read_bytes, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800670
671f_err:
672 ssl3_send_alert(s, SSL3_AL_FATAL, al);
673err:
674 return -1;
675}
676
677int dtls1_write_app_data_bytes(SSL *s, int type, const void *buf_, int len) {
678 int i;
679
680 if (SSL_in_init(s) && !s->in_handshake) {
681 i = s->handshake_func(s);
682 if (i < 0) {
683 return i;
684 }
685 if (i == 0) {
686 OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes,
687 SSL_R_SSL_HANDSHAKE_FAILURE);
688 return -1;
689 }
690 }
691
692 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
693 OPENSSL_PUT_ERROR(SSL, dtls1_write_app_data_bytes,
694 SSL_R_DTLS_MESSAGE_TOO_BIG);
695 return -1;
696 }
697
698 i = dtls1_write_bytes(s, type, buf_, len);
699 return i;
700}
701
Adam Langley71d8a082014-12-13 16:28:18 -0800702/* Call this to write data in records of type 'type' It will return <= 0 if not
703 * all data has been sent or non-blocking IO. */
704int dtls1_write_bytes(SSL *s, int type, const void *buf, int len) {
705 int i;
706
707 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
708 s->rwstate = SSL_NOTHING;
709 i = do_dtls1_write(s, type, buf, len);
710 return i;
711}
712
David Benjamin31a07792015-03-03 14:20:26 -0500713/* dtls1_seal_record seals a new record of type |type| and plaintext |in| and
714 * writes it to |out|. At most |max_out| bytes will be written. It returns one
715 * on success and zero on error. On success, it updates the write sequence
716 * number. */
717static int dtls1_seal_record(SSL *s, uint8_t *out, size_t *out_len,
718 size_t max_out, uint8_t type, const uint8_t *in,
719 size_t in_len) {
720 if (max_out < DTLS1_RT_HEADER_LENGTH) {
721 OPENSSL_PUT_ERROR(SSL, dtls1_seal_record, SSL_R_BUFFER_TOO_SMALL);
722 return 0;
723 }
724
725 out[0] = type;
726
727 uint16_t wire_version = s->s3->have_version ? s->version : DTLS1_VERSION;
728 out[1] = wire_version >> 8;
729 out[2] = wire_version & 0xff;
730
731 out[3] = s->d1->w_epoch >> 8;
732 out[4] = s->d1->w_epoch & 0xff;
733 memcpy(&out[5], &s->s3->write_sequence[2], 6);
734
735 size_t ciphertext_len;
736 if (!SSL_AEAD_CTX_seal(s->aead_write_ctx, out + DTLS1_RT_HEADER_LENGTH,
737 &ciphertext_len, max_out - DTLS1_RT_HEADER_LENGTH,
738 type, wire_version, &out[3] /* seq */, in, in_len) ||
739 !ssl3_record_sequence_update(&s->s3->write_sequence[2], 6)) {
740 return 0;
741 }
742
743 if (ciphertext_len >= 1 << 16) {
744 OPENSSL_PUT_ERROR(SSL, dtls1_seal_record, ERR_R_OVERFLOW);
745 return 0;
746 }
747 out[11] = ciphertext_len >> 8;
748 out[12] = ciphertext_len & 0xff;
749
750 *out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len;
751
752 if (s->msg_callback) {
753 s->msg_callback(1 /* write */, 0, SSL3_RT_HEADER, out,
754 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
755 }
756
757 return 1;
758}
759
Adam Langley71d8a082014-12-13 16:28:18 -0800760static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
761 unsigned int len) {
David Benjamin31a07792015-03-03 14:20:26 -0500762 SSL3_BUFFER *wb = &s->s3->wbuf;
Adam Langley71d8a082014-12-13 16:28:18 -0800763
David Benjamin883e49f2015-04-05 12:19:14 -0400764 /* ssl3_write_pending drops the write if |BIO_write| fails in DTLS, so there
765 * is never pending data. */
766 assert(s->s3->wbuf.left == 0);
Adam Langley71d8a082014-12-13 16:28:18 -0800767
768 /* If we have an alert to send, lets send it */
769 if (s->s3->alert_dispatch) {
David Benjamin31a07792015-03-03 14:20:26 -0500770 int ret = s->method->ssl_dispatch_alert(s);
771 if (ret <= 0) {
772 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800773 }
774 /* if it went, fall through and send more stuff */
775 }
776
David Benjamin31a07792015-03-03 14:20:26 -0500777 if (wb->buf == NULL && !ssl3_setup_write_buffer(s)) {
778 return -1;
779 }
780
Adam Langley71d8a082014-12-13 16:28:18 -0800781 if (len == 0) {
782 return 0;
783 }
784
David Benjamin31a07792015-03-03 14:20:26 -0500785 /* Align the output so the ciphertext is aligned to |SSL3_ALIGN_PAYLOAD|. */
786 uintptr_t align = (uintptr_t)wb->buf + DTLS1_RT_HEADER_LENGTH;
787 align = (-align) & (SSL3_ALIGN_PAYLOAD - 1);
788 uint8_t *out = wb->buf + align;
789 wb->offset = align;
790 size_t max_out = wb->len - wb->offset;
Adam Langley71d8a082014-12-13 16:28:18 -0800791
David Benjamin31a07792015-03-03 14:20:26 -0500792 size_t ciphertext_len;
793 if (!dtls1_seal_record(s, out, &ciphertext_len, max_out, type, buf, len)) {
David Benjamin9417b762015-05-08 22:30:06 -0400794 return -1;
795 }
Adam Langley71d8a082014-12-13 16:28:18 -0800796
797 /* now let's set up wb */
David Benjamin31a07792015-03-03 14:20:26 -0500798 wb->left = ciphertext_len;
Adam Langley71d8a082014-12-13 16:28:18 -0800799
800 /* memorize arguments so that ssl3_write_pending can detect bad write retries
801 * later */
802 s->s3->wpend_tot = len;
803 s->s3->wpend_buf = buf;
804 s->s3->wpend_type = type;
805 s->s3->wpend_ret = len;
806
807 /* we now just need to write the buffer */
808 return ssl3_write_pending(s, type, buf, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800809}
810
811static int dtls1_record_replay_check(SSL *s, DTLS1_BITMAP *bitmap) {
812 int cmp;
813 unsigned int shift;
814 const uint8_t *seq = s->s3->read_sequence;
815
816 cmp = satsub64be(seq, bitmap->max_seq_num);
817 if (cmp > 0) {
818 memcpy(s->s3->rrec.seq_num, seq, 8);
819 return 1; /* this record in new */
820 }
821 shift = -cmp;
822 if (shift >= sizeof(bitmap->map) * 8) {
823 return 0; /* stale, outside the window */
824 } else if (bitmap->map & (((uint64_t)1) << shift)) {
825 return 0; /* record previously received */
826 }
827
828 memcpy(s->s3->rrec.seq_num, seq, 8);
829 return 1;
830}
831
832static void dtls1_record_bitmap_update(SSL *s, DTLS1_BITMAP *bitmap) {
833 int cmp;
834 unsigned int shift;
835 const uint8_t *seq = s->s3->read_sequence;
836
837 cmp = satsub64be(seq, bitmap->max_seq_num);
838 if (cmp > 0) {
839 shift = cmp;
840 if (shift < sizeof(bitmap->map) * 8) {
841 bitmap->map <<= shift, bitmap->map |= 1UL;
842 } else {
843 bitmap->map = 1UL;
844 }
845 memcpy(bitmap->max_seq_num, seq, 8);
846 } else {
847 shift = -cmp;
848 if (shift < sizeof(bitmap->map) * 8) {
849 bitmap->map |= ((uint64_t)1) << shift;
850 }
851 }
852}
853
854int dtls1_dispatch_alert(SSL *s) {
855 int i, j;
856 void (*cb)(const SSL *ssl, int type, int val) = NULL;
857 uint8_t buf[DTLS1_AL_HEADER_LENGTH];
858 uint8_t *ptr = &buf[0];
859
860 s->s3->alert_dispatch = 0;
861
862 memset(buf, 0x00, sizeof(buf));
863 *ptr++ = s->s3->send_alert[0];
864 *ptr++ = s->s3->send_alert[1];
865
866 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf));
867 if (i <= 0) {
868 s->s3->alert_dispatch = 1;
869 } else {
870 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
871 (void)BIO_flush(s->wbio);
872 }
873
874 if (s->msg_callback) {
875 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
876 s->msg_callback_arg);
877 }
878
879 if (s->info_callback != NULL) {
880 cb = s->info_callback;
881 } else if (s->ctx->info_callback != NULL) {
882 cb = s->ctx->info_callback;
883 }
884
885 if (cb != NULL) {
886 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
887 cb(s, SSL_CB_WRITE_ALERT, j);
888 }
889 }
890
891 return i;
892}
893
Adam Langley71d8a082014-12-13 16:28:18 -0800894void dtls1_reset_seq_numbers(SSL *s, int rw) {
895 uint8_t *seq;
896 unsigned int seq_bytes = sizeof(s->s3->read_sequence);
897
898 if (rw & SSL3_CC_READ) {
899 seq = s->s3->read_sequence;
900 s->d1->r_epoch++;
David Benjamin0afbcc02015-04-05 04:06:20 -0400901 memset(&s->d1->bitmap, 0, sizeof(DTLS1_BITMAP));
Adam Langley71d8a082014-12-13 16:28:18 -0800902 } else {
903 seq = s->s3->write_sequence;
904 memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence));
905 s->d1->w_epoch++;
906 }
907
908 memset(seq, 0x00, seq_bytes);
909}