blob: ed6467661f5378290a3b9f3da75b3c11004023e7 [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
David Benjamin9e4e01e2015-09-15 01:48:04 -0400112#include <openssl/ssl.h>
113
Adam Langley95c29f32014-06-20 12:00:00 -0700114#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400115#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700116
117#include <openssl/buf.h>
118#include <openssl/mem.h>
119#include <openssl/evp.h>
120#include <openssl/err.h>
121#include <openssl/rand.h>
122
David Benjamin2ee94aa2015-04-07 22:38:30 -0400123#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800124
125
David Benjamin0d56f882015-12-19 17:05:56 -0500126static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400127 unsigned int len, enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -0700128
David Benjaminb8d28cf2015-07-28 21:34:45 -0400129/* dtls1_get_record reads a new input record. On success, it places it in
130 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
131 * more data is needed. */
132static int dtls1_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700133again:
David Benjaminfa214e42016-05-10 17:03:10 -0400134 switch (ssl->s3->recv_shutdown) {
135 case ssl_shutdown_none:
136 break;
137 case ssl_shutdown_fatal_alert:
138 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
139 return -1;
140 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500141 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500142 }
143
David Benjaminb8d28cf2015-07-28 21:34:45 -0400144 /* Read a new packet if there is no unconsumed one. */
145 if (ssl_read_buffer_len(ssl) == 0) {
146 int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
147 if (ret <= 0) {
148 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800149 }
Adam Langley71d8a082014-12-13 16:28:18 -0800150 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400151 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700152
David Benjaminb8d28cf2015-07-28 21:34:45 -0400153 /* Ensure the packet is large enough to decrypt in-place. */
154 if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
155 ssl_read_buffer_clear(ssl);
David Benjamin0afbcc02015-04-05 04:06:20 -0400156 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800157 }
Adam Langley95c29f32014-06-20 12:00:00 -0700158
David Benjaminb8d28cf2015-07-28 21:34:45 -0400159 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
160 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
161 uint8_t type, alert;
162 size_t len, consumed;
163 switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
164 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
165 case ssl_open_record_success:
166 ssl_read_buffer_consume(ssl, consumed);
167
David Benjaminb8d28cf2015-07-28 21:34:45 -0400168 if (len > 0xffff) {
169 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
170 return -1;
171 }
172
173 SSL3_RECORD *rr = &ssl->s3->rrec;
174 rr->type = type;
175 rr->length = (uint16_t)len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400176 rr->data = out;
177 return 1;
178
179 case ssl_open_record_discard:
180 ssl_read_buffer_consume(ssl, consumed);
181 goto again;
182
183 case ssl_open_record_error:
184 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
185 return -1;
186
187 case ssl_open_record_partial:
188 /* Impossible in DTLS. */
189 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800190 }
Adam Langley95c29f32014-06-20 12:00:00 -0700191
David Benjaminb8d28cf2015-07-28 21:34:45 -0400192 assert(0);
193 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
194 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800195}
Adam Langley95c29f32014-06-20 12:00:00 -0700196
David Benjamina6022772015-05-30 16:22:10 -0400197int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500198 assert(!SSL_in_init(ssl));
David Benjamina6022772015-05-30 16:22:10 -0400199 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
200}
201
David Benjamina41280d2015-11-26 02:16:49 -0500202int dtls1_read_change_cipher_spec(SSL *ssl) {
203 uint8_t byte;
204 int ret = dtls1_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte,
205 1 /* len */, 0 /* no peek */);
206 if (ret <= 0) {
207 return ret;
208 }
209 assert(ret == 1);
210
211 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
212 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
213 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
214 return -1;
215 }
216
217 if (ssl->msg_callback != NULL) {
218 ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
219 ssl, ssl->msg_callback_arg);
220 }
221
222 return 1;
223}
224
David Benjamina6022772015-05-30 16:22:10 -0400225void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400226 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
227 * alerts also aren't delivered reliably, so we may even time out because the
228 * peer never received our close_notify. Report to the caller that the channel
229 * has fully shut down. */
David Benjaminfa214e42016-05-10 17:03:10 -0400230 if (ssl->s3->recv_shutdown == ssl_shutdown_none) {
231 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
232 }
David Benjamina6022772015-05-30 16:22:10 -0400233}
234
Adam Langley95c29f32014-06-20 12:00:00 -0700235/* Return up to 'len' payload bytes received in 'type' records.
236 * 'type' is one of the following:
237 *
David Benjamina41280d2015-11-26 02:16:49 -0500238 * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us)
239 * - SSL3_RT_CHANGE_CIPHER_SPEC (when dtls1_read_change_cipher_spec calls us)
240 * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700241 *
David Benjamina41280d2015-11-26 02:16:49 -0500242 * If we don't have stored data to work from, read a DTLS record first (possibly
243 * multiple records if we still don't have anything to return).
Adam Langley95c29f32014-06-20 12:00:00 -0700244 *
245 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500246 * Alert records (e.g. close_notify) and out of records. */
David Benjamin0d56f882015-12-19 17:05:56 -0500247int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500248 int al, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800249 unsigned int n;
250 SSL3_RECORD *rr;
David Benjamin82170242015-10-17 22:51:17 -0400251 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700252
David Benjamina41280d2015-11-26 02:16:49 -0500253 if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
254 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
David Benjamine9cb2ec2015-08-22 11:31:33 -0400255 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400256 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800257 return -1;
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley95c29f32014-06-20 12:00:00 -0700260start:
David Benjamin0d56f882015-12-19 17:05:56 -0500261 /* ssl->s3->rrec.type - is the type of record
262 * ssl->s3->rrec.data - data
263 * ssl->s3->rrec.off - offset into 'data' for next read
264 * ssl->s3->rrec.length - number of bytes. */
265 rr = &ssl->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700266
Adam Langley71d8a082014-12-13 16:28:18 -0800267 /* Check for timeout */
David Benjamin0d56f882015-12-19 17:05:56 -0500268 if (DTLSv1_handle_timeout(ssl) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800269 goto start;
270 }
Adam Langley95c29f32014-06-20 12:00:00 -0700271
Adam Langley71d8a082014-12-13 16:28:18 -0800272 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400273 if (rr->length == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500274 ret = dtls1_get_record(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800275 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500276 ret = dtls1_read_failed(ssl, ret);
Adam Langley71d8a082014-12-13 16:28:18 -0800277 /* anything other than a timeout is an error */
278 if (ret <= 0) {
279 return ret;
280 } else {
281 goto start;
282 }
283 }
284 }
Adam Langley95c29f32014-06-20 12:00:00 -0700285
Adam Langley71d8a082014-12-13 16:28:18 -0800286 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700287
David Benjamina41280d2015-11-26 02:16:49 -0500288 if (type == rr->type) {
289 /* Make sure that we are not getting application data when we
290 * are doing a handshake for the first time. */
David Benjamin0d56f882015-12-19 17:05:56 -0500291 if (SSL_in_init(ssl) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjamin79978df2015-12-25 15:56:49 -0500292 (ssl->s3->aead_read_ctx == NULL)) {
David Benjaminb8a56f12014-12-23 11:41:02 -0500293 /* TODO(davidben): Is this check redundant with the handshake_func
294 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800295 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400296 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800297 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700298 }
299
David Benjamin4cf369b2015-08-22 01:35:43 -0400300 /* Discard empty records. */
301 if (rr->length == 0) {
302 goto start;
303 }
304
Adam Langley71d8a082014-12-13 16:28:18 -0800305 if (len <= 0) {
306 return len;
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
Adam Langley71d8a082014-12-13 16:28:18 -0800309 if ((unsigned int)len > rr->length) {
310 n = rr->length;
311 } else {
312 n = (unsigned int)len;
313 }
Adam Langley95c29f32014-06-20 12:00:00 -0700314
David Benjamin7fc01002015-12-06 15:48:22 -0500315 memcpy(buf, rr->data, n);
Adam Langley71d8a082014-12-13 16:28:18 -0800316 if (!peek) {
317 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500318 rr->data += n;
Adam Langley71d8a082014-12-13 16:28:18 -0800319 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400320 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500321 ssl_read_buffer_discard(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800322 }
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
Adam Langley71d8a082014-12-13 16:28:18 -0800325 return n;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
David Benjamin0ea8dda2015-01-31 20:33:40 -0500328 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700329
David Benjamin0d3a8c62016-03-11 22:25:18 -0500330 /* If an alert record, process the alert. */
David Benjamin0ea8dda2015-01-31 20:33:40 -0500331 if (rr->type == SSL3_RT_ALERT) {
David Benjamin0d3a8c62016-03-11 22:25:18 -0500332 /* Alerts records may not contain fragmented or multiple alerts. */
333 if (rr->length != 2) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500334 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400335 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500336 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800337 }
Adam Langley71d8a082014-12-13 16:28:18 -0800338
David Benjamin0d56f882015-12-19 17:05:56 -0500339 if (ssl->msg_callback) {
340 ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
341 ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800342 }
David Benjamin7fc01002015-12-06 15:48:22 -0500343 const uint8_t alert_level = rr->data[0];
344 const uint8_t alert_descr = rr->data[1];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500345 rr->length -= 2;
David Benjamin7fc01002015-12-06 15:48:22 -0500346 rr->data += 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800347
David Benjamin0d56f882015-12-19 17:05:56 -0500348 if (ssl->info_callback != NULL) {
349 cb = ssl->info_callback;
350 } else if (ssl->ctx->info_callback != NULL) {
351 cb = ssl->ctx->info_callback;
Adam Langley71d8a082014-12-13 16:28:18 -0800352 }
353
354 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500355 uint16_t alert = (alert_level << 8) | alert_descr;
David Benjamin0d56f882015-12-19 17:05:56 -0500356 cb(ssl, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800357 }
358
David Benjamin86058a22015-02-22 13:07:21 -0500359 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800360 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
David Benjaminfa214e42016-05-10 17:03:10 -0400361 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
Adam Langley71d8a082014-12-13 16:28:18 -0800362 return 0;
363 }
David Benjamin86058a22015-02-22 13:07:21 -0500364 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800365 char tmp[16];
366
David Benjamin3570d732015-06-29 00:28:17 -0400367 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800368 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
369 ERR_add_error_data(2, "SSL alert number ", tmp);
David Benjaminfa214e42016-05-10 17:03:10 -0400370 ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
David Benjamin0d56f882015-12-19 17:05:56 -0500371 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langley71d8a082014-12-13 16:28:18 -0800372 return 0;
373 } else {
374 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400375 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800376 goto f_err;
377 }
378
379 goto start;
380 }
381
David Benjamina41280d2015-11-26 02:16:49 -0500382 /* Cross-epoch records are discarded, but we may receive out-of-order
383 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
384 * before the appropriate point in the handshake. Those must be silently
385 * discarded.
386 *
387 * However, only allow the out-of-order records in the correct epoch.
388 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
389 * the unencrypted epoch (we never renegotiate). Other cases fall through and
390 * fail with a fatal error. */
David Benjamin79978df2015-12-25 15:56:49 -0500391 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
392 ssl->s3->aead_read_ctx != NULL) ||
393 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
394 ssl->s3->aead_read_ctx == NULL)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800395 rr->length = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800396 goto start;
397 }
398
David Benjamina41280d2015-11-26 02:16:49 -0500399 if (rr->type == SSL3_RT_HANDSHAKE) {
400 if (type != SSL3_RT_APPLICATION_DATA) {
401 /* Out-of-order handshake record while looking for ChangeCipherSpec. Drop
402 * it silently. */
403 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
404 rr->length = 0;
405 goto start;
406 }
407
408 /* Parse the first fragment header to determine if this is a pre-CCS or
409 * post-CCS handshake record. DTLS resets handshake message numbers on each
410 * handshake, so renegotiations and retransmissions are ambiguous. */
David Benjamin0ea8dda2015-01-31 20:33:40 -0500411 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
412 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400413 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500414 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800415 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500416 struct hm_header_st msg_hdr;
David Benjamin7fc01002015-12-06 15:48:22 -0500417 dtls1_get_message_header(rr->data, &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800418
Adam Langley71d8a082014-12-13 16:28:18 -0800419 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500420 if (msg_hdr.frag_off == 0) {
421 /* Retransmit our last flight of messages. If the peer sends the second
422 * Finished, they may not have received ours. Only do this for the
423 * first fragment, in case the Finished was fragmented. */
David Benjamin0d56f882015-12-19 17:05:56 -0500424 if (dtls1_check_timeout_num(ssl) < 0) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500425 return -1;
426 }
427
David Benjamin0d56f882015-12-19 17:05:56 -0500428 dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800429 }
430
Adam Langley71d8a082014-12-13 16:28:18 -0800431 rr->length = 0;
432 goto start;
433 }
Adam Langley71d8a082014-12-13 16:28:18 -0800434
David Benjamina41280d2015-11-26 02:16:49 -0500435 /* Otherwise, this is a pre-CCS handshake message from an unsupported
436 * renegotiation attempt. Fall through to the error path. */
437 }
Adam Langley71d8a082014-12-13 16:28:18 -0800438
David Benjaminddb9f152015-02-03 15:44:39 -0500439 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400440 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800441
442f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500443 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800444 return -1;
445}
446
David Benjamin0d56f882015-12-19 17:05:56 -0500447int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) {
David Benjamind7ac1432016-03-10 00:41:25 -0500448 assert(!SSL_in_init(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800449
450 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400451 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800452 return -1;
453 }
454
David Benjamind7ac1432016-03-10 00:41:25 -0500455 return dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
456 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800457}
458
Adam Langley71d8a082014-12-13 16:28:18 -0800459/* Call this to write data in records of type 'type' It will return <= 0 if not
460 * all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500461int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
David Benjamin3e3090d2015-04-05 12:48:30 -0400462 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800463 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjamind7ac1432016-03-10 00:41:25 -0500464 return do_dtls1_write(ssl, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800465}
466
David Benjamin0d56f882015-12-19 17:05:56 -0500467static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400468 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500469 /* There should never be a pending write buffer in DTLS. One can't write half
470 * a datagram, so the write buffer is always dropped in
471 * |ssl_write_buffer_flush|. */
David Benjamin0d56f882015-12-19 17:05:56 -0500472 assert(!ssl_write_buffer_is_pending(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800473
474 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500475 if (ssl->s3->alert_dispatch) {
476 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin31a07792015-03-03 14:20:26 -0500477 if (ret <= 0) {
478 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800479 }
480 /* if it went, fall through and send more stuff */
481 }
482
David Benjaminb8d28cf2015-07-28 21:34:45 -0400483 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
484 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500485 return -1;
486 }
487
Adam Langley71d8a082014-12-13 16:28:18 -0800488 if (len == 0) {
489 return 0;
490 }
491
David Benjamin0d56f882015-12-19 17:05:56 -0500492 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400493 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500494 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500495 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
496 !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400497 use_epoch)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500498 ssl_write_buffer_clear(ssl);
David Benjamin9417b762015-05-08 22:30:06 -0400499 return -1;
500 }
David Benjamin0d56f882015-12-19 17:05:56 -0500501 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800502
David Benjamin0d56f882015-12-19 17:05:56 -0500503 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500504 if (ret <= 0) {
505 return ret;
506 }
507 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800508}
509
David Benjamin0d56f882015-12-19 17:05:56 -0500510int dtls1_dispatch_alert(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500511 ssl->s3->alert_dispatch = 0;
David Benjamina8571592016-03-10 01:41:55 -0500512 int ret = do_dtls1_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2,
513 dtls1_use_current_epoch);
514 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500515 ssl->s3->alert_dispatch = 1;
David Benjamina8571592016-03-10 01:41:55 -0500516 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800517 }
518
David Benjamina8571592016-03-10 01:41:55 -0500519 /* If the alert is fatal, flush the BIO now. */
520 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
521 BIO_flush(ssl->wbio);
522 }
523
524 if (ssl->msg_callback != NULL) {
525 ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_ALERT,
526 ssl->s3->send_alert, 2, ssl, ssl->msg_callback_arg);
527 }
528
529 void (*cb)(const SSL *ssl, int type, int value) = NULL;
530 if (ssl->info_callback != NULL) {
531 cb = ssl->info_callback;
532 } else if (ssl->ctx->info_callback != NULL) {
533 cb = ssl->ctx->info_callback;
534 }
535
536 if (cb != NULL) {
537 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
538 cb(ssl, SSL_CB_WRITE_ALERT, alert);
539 }
540
541 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800542}