blob: f2ade3a0fdcf4ec6981c07396cf1b3f2a260bb77 [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 <stdio.h>
116#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700117
118#include <openssl/buf.h>
119#include <openssl/mem.h>
120#include <openssl/evp.h>
121#include <openssl/err.h>
122#include <openssl/rand.h>
123
David Benjamin2ee94aa2015-04-07 22:38:30 -0400124#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800125
126
David Benjamin0d56f882015-12-19 17:05:56 -0500127static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400128 unsigned int len, enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -0700129
David Benjaminb8d28cf2015-07-28 21:34:45 -0400130/* dtls1_get_record reads a new input record. On success, it places it in
131 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
132 * more data is needed. */
133static int dtls1_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700134again:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400135 /* Read a new packet if there is no unconsumed one. */
136 if (ssl_read_buffer_len(ssl) == 0) {
137 int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
138 if (ret <= 0) {
139 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800140 }
Adam Langley71d8a082014-12-13 16:28:18 -0800141 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400142 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700143
David Benjaminb8d28cf2015-07-28 21:34:45 -0400144 /* Ensure the packet is large enough to decrypt in-place. */
145 if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
146 ssl_read_buffer_clear(ssl);
David Benjamin0afbcc02015-04-05 04:06:20 -0400147 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800148 }
Adam Langley95c29f32014-06-20 12:00:00 -0700149
David Benjaminb8d28cf2015-07-28 21:34:45 -0400150 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
151 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
152 uint8_t type, alert;
153 size_t len, consumed;
154 switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
155 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
156 case ssl_open_record_success:
157 ssl_read_buffer_consume(ssl, consumed);
158
David Benjaminb8d28cf2015-07-28 21:34:45 -0400159 if (len > 0xffff) {
160 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
161 return -1;
162 }
163
164 SSL3_RECORD *rr = &ssl->s3->rrec;
165 rr->type = type;
166 rr->length = (uint16_t)len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400167 rr->data = out;
168 return 1;
169
170 case ssl_open_record_discard:
171 ssl_read_buffer_consume(ssl, consumed);
172 goto again;
173
174 case ssl_open_record_error:
175 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
176 return -1;
177
178 case ssl_open_record_partial:
179 /* Impossible in DTLS. */
180 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800181 }
Adam Langley95c29f32014-06-20 12:00:00 -0700182
David Benjaminb8d28cf2015-07-28 21:34:45 -0400183 assert(0);
184 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
185 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800186}
Adam Langley95c29f32014-06-20 12:00:00 -0700187
David Benjamina6022772015-05-30 16:22:10 -0400188int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
189 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
190}
191
David Benjamina41280d2015-11-26 02:16:49 -0500192int dtls1_read_change_cipher_spec(SSL *ssl) {
193 uint8_t byte;
194 int ret = dtls1_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte,
195 1 /* len */, 0 /* no peek */);
196 if (ret <= 0) {
197 return ret;
198 }
199 assert(ret == 1);
200
201 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
202 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
203 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
204 return -1;
205 }
206
207 if (ssl->msg_callback != NULL) {
208 ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
209 ssl, ssl->msg_callback_arg);
210 }
211
212 return 1;
213}
214
David Benjamina6022772015-05-30 16:22:10 -0400215void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400216 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
217 * alerts also aren't delivered reliably, so we may even time out because the
218 * peer never received our close_notify. Report to the caller that the channel
219 * has fully shut down. */
220 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
David Benjamina6022772015-05-30 16:22:10 -0400221}
222
Adam Langley95c29f32014-06-20 12:00:00 -0700223/* Return up to 'len' payload bytes received in 'type' records.
224 * 'type' is one of the following:
225 *
David Benjamina41280d2015-11-26 02:16:49 -0500226 * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us)
227 * - SSL3_RT_CHANGE_CIPHER_SPEC (when dtls1_read_change_cipher_spec calls us)
228 * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700229 *
David Benjamina41280d2015-11-26 02:16:49 -0500230 * If we don't have stored data to work from, read a DTLS record first (possibly
231 * multiple records if we still don't have anything to return).
Adam Langley95c29f32014-06-20 12:00:00 -0700232 *
233 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500234 * Alert records (e.g. close_notify) and out of records. */
David Benjamin0d56f882015-12-19 17:05:56 -0500235int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500236 int al, i, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800237 unsigned int n;
238 SSL3_RECORD *rr;
David Benjamin82170242015-10-17 22:51:17 -0400239 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700240
David Benjamina41280d2015-11-26 02:16:49 -0500241 if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
242 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
David Benjamine9cb2ec2015-08-22 11:31:33 -0400243 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400244 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800245 return -1;
246 }
Adam Langley95c29f32014-06-20 12:00:00 -0700247
David Benjamin0d56f882015-12-19 17:05:56 -0500248 if (!ssl->in_handshake && SSL_in_init(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800249 /* type == SSL3_RT_APPLICATION_DATA */
David Benjamin0d56f882015-12-19 17:05:56 -0500250 i = ssl->handshake_func(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800251 if (i < 0) {
252 return i;
253 }
254 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400255 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800256 return -1;
257 }
258 }
Adam Langley95c29f32014-06-20 12:00:00 -0700259
260start:
David Benjamin0d56f882015-12-19 17:05:56 -0500261 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700262
David Benjamin0d56f882015-12-19 17:05:56 -0500263 /* ssl->s3->rrec.type - is the type of record
264 * ssl->s3->rrec.data - data
265 * ssl->s3->rrec.off - offset into 'data' for next read
266 * ssl->s3->rrec.length - number of bytes. */
267 rr = &ssl->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700268
Adam Langley71d8a082014-12-13 16:28:18 -0800269 /* Check for timeout */
David Benjamin0d56f882015-12-19 17:05:56 -0500270 if (DTLSv1_handle_timeout(ssl) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800271 goto start;
272 }
Adam Langley95c29f32014-06-20 12:00:00 -0700273
Adam Langley71d8a082014-12-13 16:28:18 -0800274 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400275 if (rr->length == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500276 ret = dtls1_get_record(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800277 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500278 ret = dtls1_read_failed(ssl, ret);
Adam Langley71d8a082014-12-13 16:28:18 -0800279 /* anything other than a timeout is an error */
280 if (ret <= 0) {
281 return ret;
282 } else {
283 goto start;
284 }
285 }
286 }
Adam Langley95c29f32014-06-20 12:00:00 -0700287
Adam Langley71d8a082014-12-13 16:28:18 -0800288 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700289
Adam Langley71d8a082014-12-13 16:28:18 -0800290 /* If the other end has shut down, throw anything we read away (even in
291 * 'peek' mode) */
David Benjamin0d56f882015-12-19 17:05:56 -0500292 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
Adam Langley71d8a082014-12-13 16:28:18 -0800293 rr->length = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500294 ssl->rwstate = SSL_NOTHING;
Adam Langley71d8a082014-12-13 16:28:18 -0800295 return 0;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
298
David Benjamina41280d2015-11-26 02:16:49 -0500299 if (type == rr->type) {
300 /* Make sure that we are not getting application data when we
301 * are doing a handshake for the first time. */
David Benjamin0d56f882015-12-19 17:05:56 -0500302 if (SSL_in_init(ssl) && (type == SSL3_RT_APPLICATION_DATA) &&
303 (ssl->aead_read_ctx == NULL)) {
David Benjaminb8a56f12014-12-23 11:41:02 -0500304 /* TODO(davidben): Is this check redundant with the handshake_func
305 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800306 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400307 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800308 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700309 }
310
David Benjamin4cf369b2015-08-22 01:35:43 -0400311 /* Discard empty records. */
312 if (rr->length == 0) {
313 goto start;
314 }
315
Adam Langley71d8a082014-12-13 16:28:18 -0800316 if (len <= 0) {
317 return len;
318 }
Adam Langley95c29f32014-06-20 12:00:00 -0700319
Adam Langley71d8a082014-12-13 16:28:18 -0800320 if ((unsigned int)len > rr->length) {
321 n = rr->length;
322 } else {
323 n = (unsigned int)len;
324 }
Adam Langley95c29f32014-06-20 12:00:00 -0700325
David Benjamin7fc01002015-12-06 15:48:22 -0500326 memcpy(buf, rr->data, n);
Adam Langley71d8a082014-12-13 16:28:18 -0800327 if (!peek) {
328 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500329 rr->data += n;
Adam Langley71d8a082014-12-13 16:28:18 -0800330 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400331 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500332 ssl_read_buffer_discard(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800333 }
334 }
Adam Langley95c29f32014-06-20 12:00:00 -0700335
Adam Langley71d8a082014-12-13 16:28:18 -0800336 return n;
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
David Benjamin0ea8dda2015-01-31 20:33:40 -0500339 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700340
David Benjamin0ea8dda2015-01-31 20:33:40 -0500341 /* If an alert record, process one alert out of the record. Note that we allow
342 * a single record to contain multiple alerts. */
343 if (rr->type == SSL3_RT_ALERT) {
344 /* Alerts may not be fragmented. */
345 if (rr->length < 2) {
346 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400347 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500348 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800349 }
Adam Langley71d8a082014-12-13 16:28:18 -0800350
David Benjamin0d56f882015-12-19 17:05:56 -0500351 if (ssl->msg_callback) {
352 ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
353 ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800354 }
David Benjamin7fc01002015-12-06 15:48:22 -0500355 const uint8_t alert_level = rr->data[0];
356 const uint8_t alert_descr = rr->data[1];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500357 rr->length -= 2;
David Benjamin7fc01002015-12-06 15:48:22 -0500358 rr->data += 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800359
David Benjamin0d56f882015-12-19 17:05:56 -0500360 if (ssl->info_callback != NULL) {
361 cb = ssl->info_callback;
362 } else if (ssl->ctx->info_callback != NULL) {
363 cb = ssl->ctx->info_callback;
Adam Langley71d8a082014-12-13 16:28:18 -0800364 }
365
366 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500367 uint16_t alert = (alert_level << 8) | alert_descr;
David Benjamin0d56f882015-12-19 17:05:56 -0500368 cb(ssl, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800369 }
370
David Benjamin86058a22015-02-22 13:07:21 -0500371 if (alert_level == SSL3_AL_WARNING) {
David Benjamin0d56f882015-12-19 17:05:56 -0500372 ssl->s3->warn_alert = alert_descr;
Adam Langley71d8a082014-12-13 16:28:18 -0800373 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
David Benjamin0d56f882015-12-19 17:05:56 -0500374 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
Adam Langley71d8a082014-12-13 16:28:18 -0800375 return 0;
376 }
David Benjamin86058a22015-02-22 13:07:21 -0500377 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800378 char tmp[16];
379
David Benjamin0d56f882015-12-19 17:05:56 -0500380 ssl->rwstate = SSL_NOTHING;
381 ssl->s3->fatal_alert = alert_descr;
David Benjamin3570d732015-06-29 00:28:17 -0400382 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800383 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
384 ERR_add_error_data(2, "SSL alert number ", tmp);
David Benjamin0d56f882015-12-19 17:05:56 -0500385 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
386 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langley71d8a082014-12-13 16:28:18 -0800387 return 0;
388 } else {
389 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400390 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800391 goto f_err;
392 }
393
394 goto start;
395 }
396
David Benjamina41280d2015-11-26 02:16:49 -0500397 /* Cross-epoch records are discarded, but we may receive out-of-order
398 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
399 * before the appropriate point in the handshake. Those must be silently
400 * discarded.
401 *
402 * However, only allow the out-of-order records in the correct epoch.
403 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
404 * the unencrypted epoch (we never renegotiate). Other cases fall through and
405 * fail with a fatal error. */
David Benjamin0d56f882015-12-19 17:05:56 -0500406 if ((rr->type == SSL3_RT_APPLICATION_DATA && ssl->aead_read_ctx != NULL) ||
407 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC && ssl->aead_read_ctx == NULL)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800408 rr->length = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800409 goto start;
410 }
411
David Benjamina41280d2015-11-26 02:16:49 -0500412 if (rr->type == SSL3_RT_HANDSHAKE) {
413 if (type != SSL3_RT_APPLICATION_DATA) {
414 /* Out-of-order handshake record while looking for ChangeCipherSpec. Drop
415 * it silently. */
416 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
417 rr->length = 0;
418 goto start;
419 }
420
421 /* Parse the first fragment header to determine if this is a pre-CCS or
422 * post-CCS handshake record. DTLS resets handshake message numbers on each
423 * handshake, so renegotiations and retransmissions are ambiguous. */
David Benjamin0ea8dda2015-01-31 20:33:40 -0500424 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
425 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400426 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500427 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800428 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500429 struct hm_header_st msg_hdr;
David Benjamin7fc01002015-12-06 15:48:22 -0500430 dtls1_get_message_header(rr->data, &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800431
Adam Langley71d8a082014-12-13 16:28:18 -0800432 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500433 if (msg_hdr.frag_off == 0) {
434 /* Retransmit our last flight of messages. If the peer sends the second
435 * Finished, they may not have received ours. Only do this for the
436 * first fragment, in case the Finished was fragmented. */
David Benjamin0d56f882015-12-19 17:05:56 -0500437 if (dtls1_check_timeout_num(ssl) < 0) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500438 return -1;
439 }
440
David Benjamin0d56f882015-12-19 17:05:56 -0500441 dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800442 }
443
Adam Langley71d8a082014-12-13 16:28:18 -0800444 rr->length = 0;
445 goto start;
446 }
Adam Langley71d8a082014-12-13 16:28:18 -0800447
David Benjamina41280d2015-11-26 02:16:49 -0500448 /* Otherwise, this is a pre-CCS handshake message from an unsupported
449 * renegotiation attempt. Fall through to the error path. */
450 }
Adam Langley71d8a082014-12-13 16:28:18 -0800451
David Benjaminddb9f152015-02-03 15:44:39 -0500452 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400453 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800454
455f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500456 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800457 return -1;
458}
459
David Benjamin0d56f882015-12-19 17:05:56 -0500460int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800461 int i;
462
David Benjamin0d56f882015-12-19 17:05:56 -0500463 if (SSL_in_init(ssl) && !ssl->in_handshake) {
464 i = ssl->handshake_func(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800465 if (i < 0) {
466 return i;
467 }
468 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400469 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800470 return -1;
471 }
472 }
473
474 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400475 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800476 return -1;
477 }
478
David Benjamin0d56f882015-12-19 17:05:56 -0500479 i = dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
David Benjaminc933a472015-05-30 16:14:58 -0400480 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800481 return i;
482}
483
Adam Langley71d8a082014-12-13 16:28:18 -0800484/* Call this to write data in records of type 'type' It will return <= 0 if not
485 * all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500486int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
David Benjamin3e3090d2015-04-05 12:48:30 -0400487 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800488 int i;
489
490 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjamin0d56f882015-12-19 17:05:56 -0500491 ssl->rwstate = SSL_NOTHING;
492 i = do_dtls1_write(ssl, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800493 return i;
494}
495
David Benjamin0d56f882015-12-19 17:05:56 -0500496static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400497 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500498 /* There should never be a pending write buffer in DTLS. One can't write half
499 * a datagram, so the write buffer is always dropped in
500 * |ssl_write_buffer_flush|. */
David Benjamin0d56f882015-12-19 17:05:56 -0500501 assert(!ssl_write_buffer_is_pending(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800502
503 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500504 if (ssl->s3->alert_dispatch) {
505 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin31a07792015-03-03 14:20:26 -0500506 if (ret <= 0) {
507 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800508 }
509 /* if it went, fall through and send more stuff */
510 }
511
David Benjaminb8d28cf2015-07-28 21:34:45 -0400512 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
513 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500514 return -1;
515 }
516
Adam Langley71d8a082014-12-13 16:28:18 -0800517 if (len == 0) {
518 return 0;
519 }
520
David Benjamin0d56f882015-12-19 17:05:56 -0500521 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400522 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500523 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500524 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
525 !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400526 use_epoch)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500527 ssl_write_buffer_clear(ssl);
David Benjamin9417b762015-05-08 22:30:06 -0400528 return -1;
529 }
David Benjamin0d56f882015-12-19 17:05:56 -0500530 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800531
David Benjamin0d56f882015-12-19 17:05:56 -0500532 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500533 if (ret <= 0) {
534 return ret;
535 }
536 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800537}
538
David Benjamin0d56f882015-12-19 17:05:56 -0500539int dtls1_dispatch_alert(SSL *ssl) {
Adam Langley71d8a082014-12-13 16:28:18 -0800540 int i, j;
David Benjamin82170242015-10-17 22:51:17 -0400541 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800542 uint8_t buf[DTLS1_AL_HEADER_LENGTH];
543 uint8_t *ptr = &buf[0];
544
David Benjamin0d56f882015-12-19 17:05:56 -0500545 ssl->s3->alert_dispatch = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800546
547 memset(buf, 0x00, sizeof(buf));
David Benjamin0d56f882015-12-19 17:05:56 -0500548 *ptr++ = ssl->s3->send_alert[0];
549 *ptr++ = ssl->s3->send_alert[1];
Adam Langley71d8a082014-12-13 16:28:18 -0800550
David Benjamin0d56f882015-12-19 17:05:56 -0500551 i = do_dtls1_write(ssl, SSL3_RT_ALERT, &buf[0], sizeof(buf),
David Benjamin3e3090d2015-04-05 12:48:30 -0400552 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800553 if (i <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500554 ssl->s3->alert_dispatch = 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800555 } else {
David Benjamin0d56f882015-12-19 17:05:56 -0500556 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
557 (void)BIO_flush(ssl->wbio);
Adam Langley71d8a082014-12-13 16:28:18 -0800558 }
559
David Benjamin0d56f882015-12-19 17:05:56 -0500560 if (ssl->msg_callback) {
561 ssl->msg_callback(1, ssl->version, SSL3_RT_ALERT, ssl->s3->send_alert, 2,
562 ssl, ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800563 }
564
David Benjamin0d56f882015-12-19 17:05:56 -0500565 if (ssl->info_callback != NULL) {
566 cb = ssl->info_callback;
567 } else if (ssl->ctx->info_callback != NULL) {
568 cb = ssl->ctx->info_callback;
Adam Langley71d8a082014-12-13 16:28:18 -0800569 }
570
571 if (cb != NULL) {
David Benjamin0d56f882015-12-19 17:05:56 -0500572 j = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
573 cb(ssl, SSL_CB_WRITE_ALERT, j);
Adam Langley71d8a082014-12-13 16:28:18 -0800574 }
575 }
576
577 return i;
578}