blob: ad67902a2da11fa7f81eb5a566eb11aad5692a87 [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 Benjaminb8d28cf2015-07-28 21:34:45 -0400134 /* Read a new packet if there is no unconsumed one. */
135 if (ssl_read_buffer_len(ssl) == 0) {
136 int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
137 if (ret <= 0) {
138 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800139 }
Adam Langley71d8a082014-12-13 16:28:18 -0800140 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400141 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700142
David Benjaminb8d28cf2015-07-28 21:34:45 -0400143 /* Ensure the packet is large enough to decrypt in-place. */
144 if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
145 ssl_read_buffer_clear(ssl);
David Benjamin0afbcc02015-04-05 04:06:20 -0400146 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800147 }
Adam Langley95c29f32014-06-20 12:00:00 -0700148
David Benjaminb8d28cf2015-07-28 21:34:45 -0400149 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
150 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
151 uint8_t type, alert;
152 size_t len, consumed;
153 switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
154 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
155 case ssl_open_record_success:
156 ssl_read_buffer_consume(ssl, consumed);
157
David Benjaminb8d28cf2015-07-28 21:34:45 -0400158 if (len > 0xffff) {
159 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
160 return -1;
161 }
162
163 SSL3_RECORD *rr = &ssl->s3->rrec;
164 rr->type = type;
165 rr->length = (uint16_t)len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400166 rr->data = out;
167 return 1;
168
169 case ssl_open_record_discard:
170 ssl_read_buffer_consume(ssl, consumed);
171 goto again;
172
173 case ssl_open_record_error:
174 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
175 return -1;
176
177 case ssl_open_record_partial:
178 /* Impossible in DTLS. */
179 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800180 }
Adam Langley95c29f32014-06-20 12:00:00 -0700181
David Benjaminb8d28cf2015-07-28 21:34:45 -0400182 assert(0);
183 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
184 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800185}
Adam Langley95c29f32014-06-20 12:00:00 -0700186
David Benjamina6022772015-05-30 16:22:10 -0400187int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
188 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
189}
190
David Benjamina41280d2015-11-26 02:16:49 -0500191int dtls1_read_change_cipher_spec(SSL *ssl) {
192 uint8_t byte;
193 int ret = dtls1_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte,
194 1 /* len */, 0 /* no peek */);
195 if (ret <= 0) {
196 return ret;
197 }
198 assert(ret == 1);
199
200 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
201 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
202 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
203 return -1;
204 }
205
206 if (ssl->msg_callback != NULL) {
207 ssl->msg_callback(0, ssl->version, SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1,
208 ssl, ssl->msg_callback_arg);
209 }
210
211 return 1;
212}
213
David Benjamina6022772015-05-30 16:22:10 -0400214void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400215 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
216 * alerts also aren't delivered reliably, so we may even time out because the
217 * peer never received our close_notify. Report to the caller that the channel
218 * has fully shut down. */
219 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
David Benjamina6022772015-05-30 16:22:10 -0400220}
221
Adam Langley95c29f32014-06-20 12:00:00 -0700222/* Return up to 'len' payload bytes received in 'type' records.
223 * 'type' is one of the following:
224 *
David Benjamina41280d2015-11-26 02:16:49 -0500225 * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us)
226 * - SSL3_RT_CHANGE_CIPHER_SPEC (when dtls1_read_change_cipher_spec calls us)
227 * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700228 *
David Benjamina41280d2015-11-26 02:16:49 -0500229 * If we don't have stored data to work from, read a DTLS record first (possibly
230 * multiple records if we still don't have anything to return).
Adam Langley95c29f32014-06-20 12:00:00 -0700231 *
232 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500233 * Alert records (e.g. close_notify) and out of records. */
David Benjamin0d56f882015-12-19 17:05:56 -0500234int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500235 int al, i, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800236 unsigned int n;
237 SSL3_RECORD *rr;
David Benjamin82170242015-10-17 22:51:17 -0400238 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700239
David Benjamina41280d2015-11-26 02:16:49 -0500240 if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
241 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
David Benjamine9cb2ec2015-08-22 11:31:33 -0400242 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400243 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800244 return -1;
245 }
Adam Langley95c29f32014-06-20 12:00:00 -0700246
David Benjamin0d56f882015-12-19 17:05:56 -0500247 if (!ssl->in_handshake && SSL_in_init(ssl)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800248 /* type == SSL3_RT_APPLICATION_DATA */
David Benjamin0d56f882015-12-19 17:05:56 -0500249 i = ssl->handshake_func(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800250 if (i < 0) {
251 return i;
252 }
253 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400254 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800255 return -1;
256 }
257 }
Adam Langley95c29f32014-06-20 12:00:00 -0700258
259start:
David Benjamin0d56f882015-12-19 17:05:56 -0500260 ssl->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700261
David Benjamin0d56f882015-12-19 17:05:56 -0500262 /* ssl->s3->rrec.type - is the type of record
263 * ssl->s3->rrec.data - data
264 * ssl->s3->rrec.off - offset into 'data' for next read
265 * ssl->s3->rrec.length - number of bytes. */
266 rr = &ssl->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700267
Adam Langley71d8a082014-12-13 16:28:18 -0800268 /* Check for timeout */
David Benjamin0d56f882015-12-19 17:05:56 -0500269 if (DTLSv1_handle_timeout(ssl) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800270 goto start;
271 }
Adam Langley95c29f32014-06-20 12:00:00 -0700272
Adam Langley71d8a082014-12-13 16:28:18 -0800273 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400274 if (rr->length == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500275 ret = dtls1_get_record(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800276 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500277 ret = dtls1_read_failed(ssl, ret);
Adam Langley71d8a082014-12-13 16:28:18 -0800278 /* anything other than a timeout is an error */
279 if (ret <= 0) {
280 return ret;
281 } else {
282 goto start;
283 }
284 }
285 }
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langley71d8a082014-12-13 16:28:18 -0800287 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700288
Adam Langley71d8a082014-12-13 16:28:18 -0800289 /* If the other end has shut down, throw anything we read away (even in
290 * 'peek' mode) */
David Benjamin0d56f882015-12-19 17:05:56 -0500291 if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
Adam Langley71d8a082014-12-13 16:28:18 -0800292 rr->length = 0;
David Benjamin0d56f882015-12-19 17:05:56 -0500293 ssl->rwstate = SSL_NOTHING;
Adam Langley71d8a082014-12-13 16:28:18 -0800294 return 0;
295 }
Adam Langley95c29f32014-06-20 12:00:00 -0700296
297
David Benjamina41280d2015-11-26 02:16:49 -0500298 if (type == rr->type) {
299 /* Make sure that we are not getting application data when we
300 * are doing a handshake for the first time. */
David Benjamin0d56f882015-12-19 17:05:56 -0500301 if (SSL_in_init(ssl) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjamin79978df2015-12-25 15:56:49 -0500302 (ssl->s3->aead_read_ctx == NULL)) {
David Benjaminb8a56f12014-12-23 11:41:02 -0500303 /* TODO(davidben): Is this check redundant with the handshake_func
304 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800305 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400306 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800307 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700308 }
309
David Benjamin4cf369b2015-08-22 01:35:43 -0400310 /* Discard empty records. */
311 if (rr->length == 0) {
312 goto start;
313 }
314
Adam Langley71d8a082014-12-13 16:28:18 -0800315 if (len <= 0) {
316 return len;
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley71d8a082014-12-13 16:28:18 -0800319 if ((unsigned int)len > rr->length) {
320 n = rr->length;
321 } else {
322 n = (unsigned int)len;
323 }
Adam Langley95c29f32014-06-20 12:00:00 -0700324
David Benjamin7fc01002015-12-06 15:48:22 -0500325 memcpy(buf, rr->data, n);
Adam Langley71d8a082014-12-13 16:28:18 -0800326 if (!peek) {
327 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500328 rr->data += n;
Adam Langley71d8a082014-12-13 16:28:18 -0800329 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400330 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500331 ssl_read_buffer_discard(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800332 }
333 }
Adam Langley95c29f32014-06-20 12:00:00 -0700334
Adam Langley71d8a082014-12-13 16:28:18 -0800335 return n;
336 }
Adam Langley95c29f32014-06-20 12:00:00 -0700337
David Benjamin0ea8dda2015-01-31 20:33:40 -0500338 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700339
David Benjamin0ea8dda2015-01-31 20:33:40 -0500340 /* If an alert record, process one alert out of the record. Note that we allow
341 * a single record to contain multiple alerts. */
342 if (rr->type == SSL3_RT_ALERT) {
343 /* Alerts may not be fragmented. */
344 if (rr->length < 2) {
345 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400346 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500347 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800348 }
Adam Langley71d8a082014-12-13 16:28:18 -0800349
David Benjamin0d56f882015-12-19 17:05:56 -0500350 if (ssl->msg_callback) {
351 ssl->msg_callback(0, ssl->version, SSL3_RT_ALERT, rr->data, 2, ssl,
352 ssl->msg_callback_arg);
Adam Langley71d8a082014-12-13 16:28:18 -0800353 }
David Benjamin7fc01002015-12-06 15:48:22 -0500354 const uint8_t alert_level = rr->data[0];
355 const uint8_t alert_descr = rr->data[1];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500356 rr->length -= 2;
David Benjamin7fc01002015-12-06 15:48:22 -0500357 rr->data += 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800358
David Benjamin0d56f882015-12-19 17:05:56 -0500359 if (ssl->info_callback != NULL) {
360 cb = ssl->info_callback;
361 } else if (ssl->ctx->info_callback != NULL) {
362 cb = ssl->ctx->info_callback;
Adam Langley71d8a082014-12-13 16:28:18 -0800363 }
364
365 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500366 uint16_t alert = (alert_level << 8) | alert_descr;
David Benjamin0d56f882015-12-19 17:05:56 -0500367 cb(ssl, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800368 }
369
David Benjamin86058a22015-02-22 13:07:21 -0500370 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800371 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
David Benjamin282511d2016-03-10 00:23:55 -0500372 ssl->s3->clean_shutdown = 1;
David Benjamin0d56f882015-12-19 17:05:56 -0500373 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
Adam Langley71d8a082014-12-13 16:28:18 -0800374 return 0;
375 }
David Benjamin86058a22015-02-22 13:07:21 -0500376 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800377 char tmp[16];
378
David Benjamin0d56f882015-12-19 17:05:56 -0500379 ssl->rwstate = SSL_NOTHING;
David Benjamin3570d732015-06-29 00:28:17 -0400380 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800381 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
382 ERR_add_error_data(2, "SSL alert number ", tmp);
David Benjamin0d56f882015-12-19 17:05:56 -0500383 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
384 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langley71d8a082014-12-13 16:28:18 -0800385 return 0;
386 } else {
387 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400388 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800389 goto f_err;
390 }
391
392 goto start;
393 }
394
David Benjamina41280d2015-11-26 02:16:49 -0500395 /* Cross-epoch records are discarded, but we may receive out-of-order
396 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
397 * before the appropriate point in the handshake. Those must be silently
398 * discarded.
399 *
400 * However, only allow the out-of-order records in the correct epoch.
401 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
402 * the unencrypted epoch (we never renegotiate). Other cases fall through and
403 * fail with a fatal error. */
David Benjamin79978df2015-12-25 15:56:49 -0500404 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
405 ssl->s3->aead_read_ctx != NULL) ||
406 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
407 ssl->s3->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) {
David Benjamind7ac1432016-03-10 00:41:25 -0500461 assert(!SSL_in_init(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800462
463 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400464 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800465 return -1;
466 }
467
David Benjamind7ac1432016-03-10 00:41:25 -0500468 return dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
469 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800470}
471
Adam Langley71d8a082014-12-13 16:28:18 -0800472/* Call this to write data in records of type 'type' It will return <= 0 if not
473 * all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500474int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
David Benjamin3e3090d2015-04-05 12:48:30 -0400475 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800476 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjamin0d56f882015-12-19 17:05:56 -0500477 ssl->rwstate = SSL_NOTHING;
David Benjamind7ac1432016-03-10 00:41:25 -0500478 return do_dtls1_write(ssl, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800479}
480
David Benjamin0d56f882015-12-19 17:05:56 -0500481static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400482 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500483 /* There should never be a pending write buffer in DTLS. One can't write half
484 * a datagram, so the write buffer is always dropped in
485 * |ssl_write_buffer_flush|. */
David Benjamin0d56f882015-12-19 17:05:56 -0500486 assert(!ssl_write_buffer_is_pending(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800487
488 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500489 if (ssl->s3->alert_dispatch) {
490 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin31a07792015-03-03 14:20:26 -0500491 if (ret <= 0) {
492 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800493 }
494 /* if it went, fall through and send more stuff */
495 }
496
David Benjaminb8d28cf2015-07-28 21:34:45 -0400497 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
498 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500499 return -1;
500 }
501
Adam Langley71d8a082014-12-13 16:28:18 -0800502 if (len == 0) {
503 return 0;
504 }
505
David Benjamin0d56f882015-12-19 17:05:56 -0500506 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400507 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500508 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500509 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
510 !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400511 use_epoch)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500512 ssl_write_buffer_clear(ssl);
David Benjamin9417b762015-05-08 22:30:06 -0400513 return -1;
514 }
David Benjamin0d56f882015-12-19 17:05:56 -0500515 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800516
David Benjamin0d56f882015-12-19 17:05:56 -0500517 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500518 if (ret <= 0) {
519 return ret;
520 }
521 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800522}
523
David Benjamin0d56f882015-12-19 17:05:56 -0500524int dtls1_dispatch_alert(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500525 ssl->s3->alert_dispatch = 0;
David Benjamina8571592016-03-10 01:41:55 -0500526 int ret = do_dtls1_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2,
527 dtls1_use_current_epoch);
528 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500529 ssl->s3->alert_dispatch = 1;
David Benjamina8571592016-03-10 01:41:55 -0500530 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800531 }
532
David Benjamina8571592016-03-10 01:41:55 -0500533 /* If the alert is fatal, flush the BIO now. */
534 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
535 BIO_flush(ssl->wbio);
536 }
537
538 if (ssl->msg_callback != NULL) {
539 ssl->msg_callback(1 /* write */, ssl->version, SSL3_RT_ALERT,
540 ssl->s3->send_alert, 2, ssl, ssl->msg_callback_arg);
541 }
542
543 void (*cb)(const SSL *ssl, int type, int value) = NULL;
544 if (ssl->info_callback != NULL) {
545 cb = ssl->info_callback;
546 } else if (ssl->ctx->info_callback != NULL) {
547 cb = ssl->ctx->info_callback;
548 }
549
550 if (cb != NULL) {
551 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
552 cb(ssl, SSL_CB_WRITE_ALERT, alert);
553 }
554
555 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800556}