blob: e784e556ddfb2cb7cdbadc90bee0e61ca545b1f8 [file] [log] [blame]
David Benjaminb8d28cf2015-07-28 21:34:45 -04001/* 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 <openssl/ssl.h>
113
114#include <assert.h>
115#include <string.h>
116
117#include <openssl/bytestring.h>
118#include <openssl/err.h>
119
120#include "internal.h"
David Benjamin95d7a492016-06-09 16:38:00 -0400121#include "../crypto/internal.h"
David Benjaminb8d28cf2015-07-28 21:34:45 -0400122
123
124/* to_u64_be treats |in| as a 8-byte big-endian integer and returns the value as
125 * a |uint64_t|. */
126static uint64_t to_u64_be(const uint8_t in[8]) {
127 uint64_t ret = 0;
128 unsigned i;
129 for (i = 0; i < 8; i++) {
130 ret <<= 8;
131 ret |= in[i];
132 }
133 return ret;
134}
135
136/* dtls1_bitmap_should_discard returns one if |seq_num| has been seen in |bitmap|
137 * or is stale. Otherwise it returns zero. */
138static int dtls1_bitmap_should_discard(DTLS1_BITMAP *bitmap,
139 const uint8_t seq_num[8]) {
140 const unsigned kWindowSize = sizeof(bitmap->map) * 8;
141
142 uint64_t seq_num_u = to_u64_be(seq_num);
143 if (seq_num_u > bitmap->max_seq_num) {
144 return 0;
145 }
146 uint64_t idx = bitmap->max_seq_num - seq_num_u;
147 return idx >= kWindowSize || (bitmap->map & (((uint64_t)1) << idx));
148}
149
150/* dtls1_bitmap_record updates |bitmap| to record receipt of sequence number
151 * |seq_num|. It slides the window forward if needed. It is an error to call
152 * this function on a stale sequence number. */
153static void dtls1_bitmap_record(DTLS1_BITMAP *bitmap,
154 const uint8_t seq_num[8]) {
155 const unsigned kWindowSize = sizeof(bitmap->map) * 8;
156
157 uint64_t seq_num_u = to_u64_be(seq_num);
158 /* Shift the window if necessary. */
159 if (seq_num_u > bitmap->max_seq_num) {
160 uint64_t shift = seq_num_u - bitmap->max_seq_num;
161 if (shift >= kWindowSize) {
162 bitmap->map = 0;
163 } else {
164 bitmap->map <<= shift;
165 }
166 bitmap->max_seq_num = seq_num_u;
167 }
168
169 uint64_t idx = bitmap->max_seq_num - seq_num_u;
170 if (idx < kWindowSize) {
171 bitmap->map |= ((uint64_t)1) << idx;
172 }
173}
174
David Benjamina7810c12016-06-06 18:54:51 -0400175enum ssl_open_record_t dtls_open_record(SSL *ssl, uint8_t *out_type, CBS *out,
176 size_t *out_consumed,
177 uint8_t *out_alert, uint8_t *in,
178 size_t in_len) {
David Benjamin728f3542016-06-02 15:42:01 -0400179 *out_consumed = 0;
180
David Benjaminb8d28cf2015-07-28 21:34:45 -0400181 CBS cbs;
182 CBS_init(&cbs, in, in_len);
183
184 /* Decode the record. */
185 uint8_t type;
186 uint16_t version;
187 uint8_t sequence[8];
188 CBS body;
189 if (!CBS_get_u8(&cbs, &type) ||
190 !CBS_get_u16(&cbs, &version) ||
191 !CBS_copy_bytes(&cbs, sequence, 8) ||
192 !CBS_get_u16_length_prefixed(&cbs, &body) ||
193 (ssl->s3->have_version && version != ssl->version) ||
194 (version >> 8) != DTLS1_VERSION_MAJOR ||
195 CBS_len(&body) > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
196 /* The record header was incomplete or malformed. Drop the entire packet. */
197 *out_consumed = in_len;
198 return ssl_open_record_discard;
199 }
200
David Benjamin4e9cc712016-06-01 20:16:03 -0400201 ssl_do_msg_callback(ssl, 0 /* read */, 0, SSL3_RT_HEADER, in,
202 DTLS1_RT_HEADER_LENGTH);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400203
204 uint16_t epoch = (((uint16_t)sequence[0]) << 8) | sequence[1];
205 if (epoch != ssl->d1->r_epoch ||
206 dtls1_bitmap_should_discard(&ssl->d1->bitmap, sequence)) {
207 /* Drop this record. It's from the wrong epoch or is a replay. Note that if
208 * |epoch| is the next epoch, the record could be buffered for later. For
209 * simplicity, drop it and expect retransmit to handle it later; DTLS must
210 * handle packet loss anyway. */
211 *out_consumed = in_len - CBS_len(&cbs);
212 return ssl_open_record_discard;
213 }
214
David Benjamina7810c12016-06-06 18:54:51 -0400215 /* Decrypt the body in-place. */
216 if (!SSL_AEAD_CTX_open(ssl->s3->aead_read_ctx, out, type, version, sequence,
217 (uint8_t *)CBS_data(&body), CBS_len(&body))) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400218 /* Bad packets are silently dropped in DTLS. See section 4.2.1 of RFC 6347.
219 * Clear the error queue of any errors decryption may have added. Drop the
220 * entire packet as it must not have come from the peer.
221 *
222 * TODO(davidben): This doesn't distinguish malloc failures from encryption
223 * failures. */
224 ERR_clear_error();
225 *out_consumed = in_len - CBS_len(&cbs);
226 return ssl_open_record_discard;
227 }
David Benjamin728f3542016-06-02 15:42:01 -0400228 *out_consumed = in_len - CBS_len(&cbs);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400229
230 /* Check the plaintext length. */
David Benjamina7810c12016-06-06 18:54:51 -0400231 if (CBS_len(out) > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400232 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
233 *out_alert = SSL_AD_RECORD_OVERFLOW;
234 return ssl_open_record_error;
235 }
236
237 dtls1_bitmap_record(&ssl->d1->bitmap, sequence);
238
David Benjamin4cf369b2015-08-22 01:35:43 -0400239 /* TODO(davidben): Limit the number of empty records as in TLS? This is only
240 * useful if we also limit discarded packets. */
241
David Benjamin728f3542016-06-02 15:42:01 -0400242 if (type == SSL3_RT_ALERT) {
David Benjamina7810c12016-06-06 18:54:51 -0400243 return ssl_process_alert(ssl, out_alert, CBS_data(out), CBS_len(out));
David Benjamin728f3542016-06-02 15:42:01 -0400244 }
245
246 ssl->s3->warning_alert_count = 0;
247
David Benjaminb8d28cf2015-07-28 21:34:45 -0400248 *out_type = type;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400249 return ssl_open_record_success;
250}
251
252int dtls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
253 uint8_t type, const uint8_t *in, size_t in_len,
254 enum dtls1_use_epoch_t use_epoch) {
David Benjamin95d7a492016-06-09 16:38:00 -0400255 if (buffers_alias(in, in_len, out, max_out)) {
256 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
257 return 0;
258 }
259
David Benjaminb8d28cf2015-07-28 21:34:45 -0400260 /* Determine the parameters for the current epoch. */
261 uint16_t epoch = ssl->d1->w_epoch;
David Benjamin79978df2015-12-25 15:56:49 -0500262 SSL_AEAD_CTX *aead = ssl->s3->aead_write_ctx;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400263 uint8_t *seq = ssl->s3->write_sequence;
264 if (use_epoch == dtls1_use_previous_epoch) {
265 /* DTLS renegotiation is unsupported, so only epochs 0 (NULL cipher) and 1
266 * (negotiated cipher) exist. */
267 assert(ssl->d1->w_epoch == 1);
268 epoch = ssl->d1->w_epoch - 1;
269 aead = NULL;
270 seq = ssl->d1->last_write_sequence;
271 }
272
273 if (max_out < DTLS1_RT_HEADER_LENGTH) {
274 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
275 return 0;
276 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400277
278 out[0] = type;
279
280 uint16_t wire_version = ssl->s3->have_version ? ssl->version : DTLS1_VERSION;
281 out[1] = wire_version >> 8;
282 out[2] = wire_version & 0xff;
283
284 out[3] = epoch >> 8;
285 out[4] = epoch & 0xff;
286 memcpy(&out[5], &seq[2], 6);
287
288 size_t ciphertext_len;
289 if (!SSL_AEAD_CTX_seal(aead, out + DTLS1_RT_HEADER_LENGTH, &ciphertext_len,
290 max_out - DTLS1_RT_HEADER_LENGTH, type, wire_version,
291 &out[3] /* seq */, in, in_len) ||
David Benjamin1db21562015-12-25 15:11:39 -0500292 !ssl_record_sequence_update(&seq[2], 6)) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400293 return 0;
294 }
295
296 if (ciphertext_len >= 1 << 16) {
297 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
298 return 0;
299 }
300 out[11] = ciphertext_len >> 8;
301 out[12] = ciphertext_len & 0xff;
302
303 *out_len = DTLS1_RT_HEADER_LENGTH + ciphertext_len;
304
David Benjamin4e9cc712016-06-01 20:16:03 -0400305 ssl_do_msg_callback(ssl, 1 /* write */, 0, SSL3_RT_HEADER, out,
306 DTLS1_RT_HEADER_LENGTH);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400307
308 return 1;
309}