blob: c3f03bc60e945f0e71370646bf105d012bd7d3f4 [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
David Benjamin15aa8952016-05-10 20:51:34 -0400117#include <openssl/bio.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700118#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 Benjaminfa214e42016-05-10 17:03:10 -0400135 switch (ssl->s3->recv_shutdown) {
136 case ssl_shutdown_none:
137 break;
138 case ssl_shutdown_fatal_alert:
139 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
140 return -1;
141 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500142 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500143 }
144
David Benjaminb8d28cf2015-07-28 21:34:45 -0400145 /* Read a new packet if there is no unconsumed one. */
146 if (ssl_read_buffer_len(ssl) == 0) {
147 int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
David Benjamin15aa8952016-05-10 20:51:34 -0400148 if (ret < 0 && dtls1_is_timer_expired(ssl)) {
149 /* For blocking BIOs, retransmits must be handled internally. */
150 int timeout_ret = DTLSv1_handle_timeout(ssl);
151 if (timeout_ret <= 0) {
152 return timeout_ret;
153 }
154 goto again;
155 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400156 if (ret <= 0) {
157 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800158 }
Adam Langley71d8a082014-12-13 16:28:18 -0800159 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400160 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700161
David Benjaminb8d28cf2015-07-28 21:34:45 -0400162 /* Ensure the packet is large enough to decrypt in-place. */
163 if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
164 ssl_read_buffer_clear(ssl);
David Benjamin0afbcc02015-04-05 04:06:20 -0400165 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800166 }
Adam Langley95c29f32014-06-20 12:00:00 -0700167
David Benjaminb8d28cf2015-07-28 21:34:45 -0400168 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
169 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
170 uint8_t type, alert;
171 size_t len, consumed;
172 switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
173 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
174 case ssl_open_record_success:
175 ssl_read_buffer_consume(ssl, consumed);
176
David Benjaminb8d28cf2015-07-28 21:34:45 -0400177 if (len > 0xffff) {
178 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
179 return -1;
180 }
181
182 SSL3_RECORD *rr = &ssl->s3->rrec;
183 rr->type = type;
184 rr->length = (uint16_t)len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400185 rr->data = out;
186 return 1;
187
188 case ssl_open_record_discard:
189 ssl_read_buffer_consume(ssl, consumed);
190 goto again;
191
192 case ssl_open_record_error:
193 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
194 return -1;
195
196 case ssl_open_record_partial:
197 /* Impossible in DTLS. */
198 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800199 }
Adam Langley95c29f32014-06-20 12:00:00 -0700200
David Benjaminb8d28cf2015-07-28 21:34:45 -0400201 assert(0);
202 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
203 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800204}
Adam Langley95c29f32014-06-20 12:00:00 -0700205
David Benjamina6022772015-05-30 16:22:10 -0400206int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500207 assert(!SSL_in_init(ssl));
David Benjamina6022772015-05-30 16:22:10 -0400208 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
209}
210
David Benjamina41280d2015-11-26 02:16:49 -0500211int dtls1_read_change_cipher_spec(SSL *ssl) {
212 uint8_t byte;
213 int ret = dtls1_read_bytes(ssl, SSL3_RT_CHANGE_CIPHER_SPEC, &byte,
214 1 /* len */, 0 /* no peek */);
215 if (ret <= 0) {
216 return ret;
217 }
218 assert(ret == 1);
219
220 if (ssl->s3->rrec.length != 0 || byte != SSL3_MT_CCS) {
221 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
222 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
223 return -1;
224 }
225
David Benjamin4e9cc712016-06-01 20:16:03 -0400226 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version,
227 SSL3_RT_CHANGE_CIPHER_SPEC, &byte, 1);
David Benjamina41280d2015-11-26 02:16:49 -0500228 return 1;
229}
230
David Benjamina6022772015-05-30 16:22:10 -0400231void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400232 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
233 * alerts also aren't delivered reliably, so we may even time out because the
234 * peer never received our close_notify. Report to the caller that the channel
235 * has fully shut down. */
David Benjaminfa214e42016-05-10 17:03:10 -0400236 if (ssl->s3->recv_shutdown == ssl_shutdown_none) {
237 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
238 }
David Benjamina6022772015-05-30 16:22:10 -0400239}
240
Adam Langley95c29f32014-06-20 12:00:00 -0700241/* Return up to 'len' payload bytes received in 'type' records.
242 * 'type' is one of the following:
243 *
David Benjamina41280d2015-11-26 02:16:49 -0500244 * - SSL3_RT_HANDSHAKE (when dtls1_get_message calls us)
245 * - SSL3_RT_CHANGE_CIPHER_SPEC (when dtls1_read_change_cipher_spec calls us)
246 * - SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700247 *
David Benjamina41280d2015-11-26 02:16:49 -0500248 * If we don't have stored data to work from, read a DTLS record first (possibly
249 * multiple records if we still don't have anything to return).
Adam Langley95c29f32014-06-20 12:00:00 -0700250 *
251 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500252 * Alert records (e.g. close_notify) and out of records. */
David Benjamin0d56f882015-12-19 17:05:56 -0500253int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500254 int al, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800255 unsigned int n;
256 SSL3_RECORD *rr;
Adam Langley95c29f32014-06-20 12:00:00 -0700257
David Benjamina41280d2015-11-26 02:16:49 -0500258 if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE &&
259 type != SSL3_RT_CHANGE_CIPHER_SPEC) ||
David Benjamine9cb2ec2015-08-22 11:31:33 -0400260 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400261 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800262 return -1;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langley95c29f32014-06-20 12:00:00 -0700265start:
David Benjamin0d56f882015-12-19 17:05:56 -0500266 /* ssl->s3->rrec.type - is the type of record
267 * ssl->s3->rrec.data - data
268 * ssl->s3->rrec.off - offset into 'data' for next read
269 * ssl->s3->rrec.length - number of bytes. */
270 rr = &ssl->s3->rrec;
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 Benjamin15aa8952016-05-10 20:51:34 -0400276 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800277 }
278 }
Adam Langley95c29f32014-06-20 12:00:00 -0700279
Adam Langley71d8a082014-12-13 16:28:18 -0800280 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700281
David Benjamina41280d2015-11-26 02:16:49 -0500282 if (type == rr->type) {
David Benjamin4cf369b2015-08-22 01:35:43 -0400283 /* Discard empty records. */
284 if (rr->length == 0) {
285 goto start;
286 }
287
Adam Langley71d8a082014-12-13 16:28:18 -0800288 if (len <= 0) {
289 return len;
290 }
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley71d8a082014-12-13 16:28:18 -0800292 if ((unsigned int)len > rr->length) {
293 n = rr->length;
294 } else {
295 n = (unsigned int)len;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
David Benjamin7fc01002015-12-06 15:48:22 -0500298 memcpy(buf, rr->data, n);
Adam Langley71d8a082014-12-13 16:28:18 -0800299 if (!peek) {
300 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500301 rr->data += n;
Adam Langley71d8a082014-12-13 16:28:18 -0800302 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400303 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500304 ssl_read_buffer_discard(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800305 }
306 }
Adam Langley95c29f32014-06-20 12:00:00 -0700307
Adam Langley71d8a082014-12-13 16:28:18 -0800308 return n;
309 }
Adam Langley95c29f32014-06-20 12:00:00 -0700310
David Benjamin0ea8dda2015-01-31 20:33:40 -0500311 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700312
David Benjamin0d3a8c62016-03-11 22:25:18 -0500313 /* If an alert record, process the alert. */
David Benjamin0ea8dda2015-01-31 20:33:40 -0500314 if (rr->type == SSL3_RT_ALERT) {
David Benjamin0d3a8c62016-03-11 22:25:18 -0500315 /* Alerts records may not contain fragmented or multiple alerts. */
316 if (rr->length != 2) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500317 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400318 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500319 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800320 }
Adam Langley71d8a082014-12-13 16:28:18 -0800321
David Benjamin4e9cc712016-06-01 20:16:03 -0400322 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version, SSL3_RT_ALERT,
323 rr->data, 2);
324
David Benjamin7fc01002015-12-06 15:48:22 -0500325 const uint8_t alert_level = rr->data[0];
326 const uint8_t alert_descr = rr->data[1];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500327 rr->length -= 2;
David Benjamin7fc01002015-12-06 15:48:22 -0500328 rr->data += 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800329
David Benjamin4e9cc712016-06-01 20:16:03 -0400330 uint16_t alert = (alert_level << 8) | alert_descr;
331 ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800332
David Benjamin86058a22015-02-22 13:07:21 -0500333 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800334 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
David Benjaminfa214e42016-05-10 17:03:10 -0400335 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
Adam Langley71d8a082014-12-13 16:28:18 -0800336 return 0;
337 }
David Benjamin86058a22015-02-22 13:07:21 -0500338 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800339 char tmp[16];
340
David Benjamin3570d732015-06-29 00:28:17 -0400341 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800342 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
343 ERR_add_error_data(2, "SSL alert number ", tmp);
David Benjaminfa214e42016-05-10 17:03:10 -0400344 ssl->s3->recv_shutdown = ssl_shutdown_fatal_alert;
David Benjamin0d56f882015-12-19 17:05:56 -0500345 SSL_CTX_remove_session(ssl->ctx, ssl->session);
Adam Langley71d8a082014-12-13 16:28:18 -0800346 return 0;
347 } else {
348 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400349 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800350 goto f_err;
351 }
352
353 goto start;
354 }
355
David Benjamina41280d2015-11-26 02:16:49 -0500356 /* Cross-epoch records are discarded, but we may receive out-of-order
357 * application data between ChangeCipherSpec and Finished or a ChangeCipherSpec
358 * before the appropriate point in the handshake. Those must be silently
359 * discarded.
360 *
361 * However, only allow the out-of-order records in the correct epoch.
362 * Application data must come in the encrypted epoch, and ChangeCipherSpec in
363 * the unencrypted epoch (we never renegotiate). Other cases fall through and
364 * fail with a fatal error. */
David Benjamin79978df2015-12-25 15:56:49 -0500365 if ((rr->type == SSL3_RT_APPLICATION_DATA &&
366 ssl->s3->aead_read_ctx != NULL) ||
367 (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC &&
368 ssl->s3->aead_read_ctx == NULL)) {
Adam Langley71d8a082014-12-13 16:28:18 -0800369 rr->length = 0;
Adam Langley71d8a082014-12-13 16:28:18 -0800370 goto start;
371 }
372
David Benjamina41280d2015-11-26 02:16:49 -0500373 if (rr->type == SSL3_RT_HANDSHAKE) {
374 if (type != SSL3_RT_APPLICATION_DATA) {
375 /* Out-of-order handshake record while looking for ChangeCipherSpec. Drop
376 * it silently. */
377 assert(type == SSL3_RT_CHANGE_CIPHER_SPEC);
378 rr->length = 0;
379 goto start;
380 }
381
382 /* Parse the first fragment header to determine if this is a pre-CCS or
383 * post-CCS handshake record. DTLS resets handshake message numbers on each
384 * handshake, so renegotiations and retransmissions are ambiguous. */
David Benjamin0ea8dda2015-01-31 20:33:40 -0500385 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
386 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400387 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500388 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800389 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500390 struct hm_header_st msg_hdr;
David Benjamin7fc01002015-12-06 15:48:22 -0500391 dtls1_get_message_header(rr->data, &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800392
Adam Langley71d8a082014-12-13 16:28:18 -0800393 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500394 if (msg_hdr.frag_off == 0) {
395 /* Retransmit our last flight of messages. If the peer sends the second
396 * Finished, they may not have received ours. Only do this for the
397 * first fragment, in case the Finished was fragmented. */
David Benjamin0d56f882015-12-19 17:05:56 -0500398 if (dtls1_check_timeout_num(ssl) < 0) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500399 return -1;
400 }
401
David Benjamin0d56f882015-12-19 17:05:56 -0500402 dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800403 }
404
Adam Langley71d8a082014-12-13 16:28:18 -0800405 rr->length = 0;
406 goto start;
407 }
Adam Langley71d8a082014-12-13 16:28:18 -0800408
David Benjamina41280d2015-11-26 02:16:49 -0500409 /* Otherwise, this is a pre-CCS handshake message from an unsupported
410 * renegotiation attempt. Fall through to the error path. */
411 }
Adam Langley71d8a082014-12-13 16:28:18 -0800412
David Benjaminddb9f152015-02-03 15:44:39 -0500413 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400414 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800415
416f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500417 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800418 return -1;
419}
420
David Benjamin0d56f882015-12-19 17:05:56 -0500421int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) {
David Benjamind7ac1432016-03-10 00:41:25 -0500422 assert(!SSL_in_init(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800423
424 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400425 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800426 return -1;
427 }
428
David Benjamind7ac1432016-03-10 00:41:25 -0500429 return dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
430 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800431}
432
Adam Langley71d8a082014-12-13 16:28:18 -0800433/* Call this to write data in records of type 'type' It will return <= 0 if not
434 * all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500435int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
David Benjamin3e3090d2015-04-05 12:48:30 -0400436 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800437 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjamind7ac1432016-03-10 00:41:25 -0500438 return do_dtls1_write(ssl, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800439}
440
David Benjamin0d56f882015-12-19 17:05:56 -0500441static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400442 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500443 /* There should never be a pending write buffer in DTLS. One can't write half
444 * a datagram, so the write buffer is always dropped in
445 * |ssl_write_buffer_flush|. */
David Benjamin0d56f882015-12-19 17:05:56 -0500446 assert(!ssl_write_buffer_is_pending(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800447
448 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500449 if (ssl->s3->alert_dispatch) {
450 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin31a07792015-03-03 14:20:26 -0500451 if (ret <= 0) {
452 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800453 }
454 /* if it went, fall through and send more stuff */
455 }
456
David Benjaminb8d28cf2015-07-28 21:34:45 -0400457 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
458 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500459 return -1;
460 }
461
Adam Langley71d8a082014-12-13 16:28:18 -0800462 if (len == 0) {
463 return 0;
464 }
465
David Benjamin0d56f882015-12-19 17:05:56 -0500466 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400467 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500468 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500469 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
470 !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400471 use_epoch)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500472 ssl_write_buffer_clear(ssl);
David Benjamin9417b762015-05-08 22:30:06 -0400473 return -1;
474 }
David Benjamin0d56f882015-12-19 17:05:56 -0500475 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800476
David Benjamin0d56f882015-12-19 17:05:56 -0500477 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500478 if (ret <= 0) {
479 return ret;
480 }
481 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800482}
483
David Benjamin0d56f882015-12-19 17:05:56 -0500484int dtls1_dispatch_alert(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500485 ssl->s3->alert_dispatch = 0;
David Benjamina8571592016-03-10 01:41:55 -0500486 int ret = do_dtls1_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2,
487 dtls1_use_current_epoch);
488 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500489 ssl->s3->alert_dispatch = 1;
David Benjamina8571592016-03-10 01:41:55 -0500490 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800491 }
492
David Benjamina8571592016-03-10 01:41:55 -0500493 /* If the alert is fatal, flush the BIO now. */
494 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
495 BIO_flush(ssl->wbio);
496 }
497
David Benjamin4e9cc712016-06-01 20:16:03 -0400498 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_ALERT,
499 ssl->s3->send_alert, 2);
David Benjamina8571592016-03-10 01:41:55 -0500500
David Benjamin4e9cc712016-06-01 20:16:03 -0400501 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
502 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
David Benjamina8571592016-03-10 01:41:55 -0500503
504 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800505}