blob: c67a7aec713f6f76307dd7ca8561c50ed81c0c9c [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>
David Benjaminc6604172016-06-02 16:38:35 -0400119#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700120#include <openssl/mem.h>
121#include <openssl/evp.h>
122#include <openssl/err.h>
123#include <openssl/rand.h>
124
David Benjamin2ee94aa2015-04-07 22:38:30 -0400125#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800126
127
David Benjamin0d56f882015-12-19 17:05:56 -0500128static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400129 unsigned int len, enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -0700130
David Benjaminc6604172016-06-02 16:38:35 -0400131int dtls1_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700132again:
David Benjaminfa214e42016-05-10 17:03:10 -0400133 switch (ssl->s3->recv_shutdown) {
134 case ssl_shutdown_none:
135 break;
136 case ssl_shutdown_fatal_alert:
137 OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
138 return -1;
139 case ssl_shutdown_close_notify:
David Benjamin8f731352016-03-10 01:15:15 -0500140 return 0;
David Benjamin8f731352016-03-10 01:15:15 -0500141 }
142
David Benjaminb8d28cf2015-07-28 21:34:45 -0400143 /* Read a new packet if there is no unconsumed one. */
144 if (ssl_read_buffer_len(ssl) == 0) {
David Benjamin728f3542016-06-02 15:42:01 -0400145 int read_ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
146 if (read_ret < 0 && dtls1_is_timer_expired(ssl)) {
David Benjamin15aa8952016-05-10 20:51:34 -0400147 /* For blocking BIOs, retransmits must be handled internally. */
148 int timeout_ret = DTLSv1_handle_timeout(ssl);
149 if (timeout_ret <= 0) {
150 return timeout_ret;
151 }
152 goto again;
153 }
David Benjamin728f3542016-06-02 15:42:01 -0400154 if (read_ret <= 0) {
155 return read_ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800156 }
Adam Langley71d8a082014-12-13 16:28:18 -0800157 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400158 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700159
David Benjamina7810c12016-06-06 18:54:51 -0400160 CBS body;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400161 uint8_t type, alert;
David Benjamina7810c12016-06-06 18:54:51 -0400162 size_t consumed;
David Benjamin728f3542016-06-02 15:42:01 -0400163 enum ssl_open_record_t open_ret =
David Benjamina7810c12016-06-06 18:54:51 -0400164 dtls_open_record(ssl, &type, &body, &consumed, &alert,
David Benjamin728f3542016-06-02 15:42:01 -0400165 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl));
166 ssl_read_buffer_consume(ssl, consumed);
167 switch (open_ret) {
168 case ssl_open_record_partial:
169 /* Impossible in DTLS. */
170 break;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400171
David Benjamin728f3542016-06-02 15:42:01 -0400172 case ssl_open_record_success:
David Benjamina7810c12016-06-06 18:54:51 -0400173 if (CBS_len(&body) > 0xffff) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400174 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
175 return -1;
176 }
177
178 SSL3_RECORD *rr = &ssl->s3->rrec;
179 rr->type = type;
David Benjamina7810c12016-06-06 18:54:51 -0400180 rr->length = (uint16_t)CBS_len(&body);
181 rr->data = (uint8_t *)CBS_data(&body);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400182 return 1;
183
184 case ssl_open_record_discard:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400185 goto again;
186
David Benjamin728f3542016-06-02 15:42:01 -0400187 case ssl_open_record_close_notify:
188 return 0;
189
190 case ssl_open_record_fatal_alert:
191 return -1;
192
David Benjaminb8d28cf2015-07-28 21:34:45 -0400193 case ssl_open_record_error:
194 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
195 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800196 }
Adam Langley95c29f32014-06-20 12:00:00 -0700197
David Benjaminb8d28cf2015-07-28 21:34:45 -0400198 assert(0);
199 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
200 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800201}
Adam Langley95c29f32014-06-20 12:00:00 -0700202
David Benjamina6022772015-05-30 16:22:10 -0400203int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500204 assert(!SSL_in_init(ssl));
David Benjamina6022772015-05-30 16:22:10 -0400205 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
206}
207
David Benjamina41280d2015-11-26 02:16:49 -0500208int dtls1_read_change_cipher_spec(SSL *ssl) {
David Benjamin585320c2016-05-10 20:46:16 -0400209 SSL3_RECORD *rr = &ssl->s3->rrec;
David Benjamina41280d2015-11-26 02:16:49 -0500210
David Benjamin585320c2016-05-10 20:46:16 -0400211again:
212 if (rr->length == 0) {
213 int ret = dtls1_get_record(ssl);
214 if (ret <= 0) {
215 return ret;
216 }
217 }
218
219 /* Drop handshake records silently. The epochs match, so this must be a
220 * retransmit of a message we already received. */
221 if (rr->type == SSL3_RT_HANDSHAKE) {
222 rr->length = 0;
223 goto again;
224 }
225
226 /* Other record types are illegal in this epoch. Note all application data
227 * records come in the encrypted epoch. */
228 if (rr->type != SSL3_RT_CHANGE_CIPHER_SPEC) {
229 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
230 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
231 return -1;
232 }
233
234 if (rr->length != 1 || rr->data[0] != SSL3_MT_CCS) {
David Benjamina41280d2015-11-26 02:16:49 -0500235 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
236 ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
237 return -1;
238 }
239
David Benjamin4e9cc712016-06-01 20:16:03 -0400240 ssl_do_msg_callback(ssl, 0 /* read */, ssl->version,
David Benjamin585320c2016-05-10 20:46:16 -0400241 SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, rr->length);
242
243 rr->length = 0;
244 ssl_read_buffer_discard(ssl);
David Benjamina41280d2015-11-26 02:16:49 -0500245 return 1;
246}
247
David Benjamina6022772015-05-30 16:22:10 -0400248void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400249 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
250 * alerts also aren't delivered reliably, so we may even time out because the
251 * peer never received our close_notify. Report to the caller that the channel
252 * has fully shut down. */
David Benjaminfa214e42016-05-10 17:03:10 -0400253 if (ssl->s3->recv_shutdown == ssl_shutdown_none) {
254 ssl->s3->recv_shutdown = ssl_shutdown_close_notify;
255 }
David Benjamina6022772015-05-30 16:22:10 -0400256}
257
Adam Langley95c29f32014-06-20 12:00:00 -0700258/* Return up to 'len' payload bytes received in 'type' records.
David Benjaminc6604172016-06-02 16:38:35 -0400259 * 'type' must be SSL3_RT_APPLICATION_DATA (when dtls1_read_app_data calls us).
Adam Langley95c29f32014-06-20 12:00:00 -0700260 *
David Benjamina41280d2015-11-26 02:16:49 -0500261 * If we don't have stored data to work from, read a DTLS record first (possibly
262 * multiple records if we still don't have anything to return).
Adam Langley95c29f32014-06-20 12:00:00 -0700263 *
264 * This function must handle any surprises the peer may have for us, such as
David Benjamina41280d2015-11-26 02:16:49 -0500265 * Alert records (e.g. close_notify) and out of records. */
David Benjamin0d56f882015-12-19 17:05:56 -0500266int dtls1_read_bytes(SSL *ssl, int type, unsigned char *buf, int len, int peek) {
David Benjaminc79845c2016-03-10 01:28:00 -0500267 int al, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800268 unsigned int n;
269 SSL3_RECORD *rr;
Adam Langley95c29f32014-06-20 12:00:00 -0700270
David Benjaminc6604172016-06-02 16:38:35 -0400271 if (type != SSL3_RT_APPLICATION_DATA) {
David Benjamin3570d732015-06-29 00:28:17 -0400272 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800273 return -1;
274 }
Adam Langley95c29f32014-06-20 12:00:00 -0700275
Adam Langley95c29f32014-06-20 12:00:00 -0700276start:
David Benjamin0d56f882015-12-19 17:05:56 -0500277 /* ssl->s3->rrec.type - is the type of record
278 * ssl->s3->rrec.data - data
279 * ssl->s3->rrec.off - offset into 'data' for next read
280 * ssl->s3->rrec.length - number of bytes. */
281 rr = &ssl->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700282
Adam Langley71d8a082014-12-13 16:28:18 -0800283 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400284 if (rr->length == 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500285 ret = dtls1_get_record(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800286 if (ret <= 0) {
David Benjamin15aa8952016-05-10 20:51:34 -0400287 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800288 }
289 }
Adam Langley95c29f32014-06-20 12:00:00 -0700290
Adam Langley71d8a082014-12-13 16:28:18 -0800291 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700292
David Benjamina41280d2015-11-26 02:16:49 -0500293 if (type == rr->type) {
David Benjamin4cf369b2015-08-22 01:35:43 -0400294 /* Discard empty records. */
295 if (rr->length == 0) {
296 goto start;
297 }
298
Adam Langley71d8a082014-12-13 16:28:18 -0800299 if (len <= 0) {
300 return len;
301 }
Adam Langley95c29f32014-06-20 12:00:00 -0700302
Adam Langley71d8a082014-12-13 16:28:18 -0800303 if ((unsigned int)len > rr->length) {
304 n = rr->length;
305 } else {
306 n = (unsigned int)len;
307 }
Adam Langley95c29f32014-06-20 12:00:00 -0700308
David Benjamin7fc01002015-12-06 15:48:22 -0500309 memcpy(buf, rr->data, n);
Adam Langley71d8a082014-12-13 16:28:18 -0800310 if (!peek) {
311 rr->length -= n;
David Benjamin7fc01002015-12-06 15:48:22 -0500312 rr->data += n;
Adam Langley71d8a082014-12-13 16:28:18 -0800313 if (rr->length == 0) {
David Benjaminb8d28cf2015-07-28 21:34:45 -0400314 /* The record has been consumed, so we may now clear the buffer. */
David Benjamin0d56f882015-12-19 17:05:56 -0500315 ssl_read_buffer_discard(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800316 }
317 }
Adam Langley95c29f32014-06-20 12:00:00 -0700318
Adam Langley71d8a082014-12-13 16:28:18 -0800319 return n;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
David Benjamin0ea8dda2015-01-31 20:33:40 -0500322 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700323
David Benjamina41280d2015-11-26 02:16:49 -0500324 if (rr->type == SSL3_RT_HANDSHAKE) {
David Benjamina41280d2015-11-26 02:16:49 -0500325 /* Parse the first fragment header to determine if this is a pre-CCS or
326 * post-CCS handshake record. DTLS resets handshake message numbers on each
327 * handshake, so renegotiations and retransmissions are ambiguous. */
David Benjaminc6604172016-06-02 16:38:35 -0400328 CBS cbs, body;
329 struct hm_header_st msg_hdr;
330 CBS_init(&cbs, rr->data, rr->length);
331 if (!dtls1_parse_fragment(&cbs, &msg_hdr, &body)) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500332 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400333 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500334 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800335 }
336
Adam Langley71d8a082014-12-13 16:28:18 -0800337 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500338 if (msg_hdr.frag_off == 0) {
339 /* Retransmit our last flight of messages. If the peer sends the second
340 * Finished, they may not have received ours. Only do this for the
341 * first fragment, in case the Finished was fragmented. */
David Benjamin0d56f882015-12-19 17:05:56 -0500342 if (dtls1_check_timeout_num(ssl) < 0) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500343 return -1;
344 }
345
David Benjamin0d56f882015-12-19 17:05:56 -0500346 dtls1_retransmit_buffered_messages(ssl);
Adam Langley71d8a082014-12-13 16:28:18 -0800347 }
348
Adam Langley71d8a082014-12-13 16:28:18 -0800349 rr->length = 0;
350 goto start;
351 }
Adam Langley71d8a082014-12-13 16:28:18 -0800352
David Benjamina41280d2015-11-26 02:16:49 -0500353 /* Otherwise, this is a pre-CCS handshake message from an unsupported
354 * renegotiation attempt. Fall through to the error path. */
355 }
Adam Langley71d8a082014-12-13 16:28:18 -0800356
David Benjaminddb9f152015-02-03 15:44:39 -0500357 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400358 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800359
360f_err:
David Benjamin0d56f882015-12-19 17:05:56 -0500361 ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
Adam Langley71d8a082014-12-13 16:28:18 -0800362 return -1;
363}
364
David Benjamin0d56f882015-12-19 17:05:56 -0500365int dtls1_write_app_data(SSL *ssl, const void *buf_, int len) {
David Benjamind7ac1432016-03-10 00:41:25 -0500366 assert(!SSL_in_init(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800367
368 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400369 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800370 return -1;
371 }
372
David Benjamind7ac1432016-03-10 00:41:25 -0500373 return dtls1_write_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf_, len,
374 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800375}
376
Adam Langley71d8a082014-12-13 16:28:18 -0800377/* Call this to write data in records of type 'type' It will return <= 0 if not
378 * all data has been sent or non-blocking IO. */
David Benjamin0d56f882015-12-19 17:05:56 -0500379int dtls1_write_bytes(SSL *ssl, int type, const void *buf, int len,
David Benjamin3e3090d2015-04-05 12:48:30 -0400380 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800381 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
David Benjamind7ac1432016-03-10 00:41:25 -0500382 return do_dtls1_write(ssl, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800383}
384
David Benjamin0d56f882015-12-19 17:05:56 -0500385static int do_dtls1_write(SSL *ssl, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400386 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500387 /* There should never be a pending write buffer in DTLS. One can't write half
388 * a datagram, so the write buffer is always dropped in
389 * |ssl_write_buffer_flush|. */
David Benjamin0d56f882015-12-19 17:05:56 -0500390 assert(!ssl_write_buffer_is_pending(ssl));
Adam Langley71d8a082014-12-13 16:28:18 -0800391
392 /* If we have an alert to send, lets send it */
David Benjamin0d56f882015-12-19 17:05:56 -0500393 if (ssl->s3->alert_dispatch) {
394 int ret = ssl->method->ssl_dispatch_alert(ssl);
David Benjamin31a07792015-03-03 14:20:26 -0500395 if (ret <= 0) {
396 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800397 }
398 /* if it went, fall through and send more stuff */
399 }
400
David Benjaminb8d28cf2015-07-28 21:34:45 -0400401 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
402 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500403 return -1;
404 }
405
Adam Langley71d8a082014-12-13 16:28:18 -0800406 if (len == 0) {
407 return 0;
408 }
409
David Benjamin0d56f882015-12-19 17:05:56 -0500410 size_t max_out = len + ssl_max_seal_overhead(ssl);
David Benjaminb8d28cf2015-07-28 21:34:45 -0400411 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500412 size_t ciphertext_len;
David Benjamin0d56f882015-12-19 17:05:56 -0500413 if (!ssl_write_buffer_init(ssl, &out, max_out) ||
414 !dtls_seal_record(ssl, out, &ciphertext_len, max_out, type, buf, len,
David Benjaminb8d28cf2015-07-28 21:34:45 -0400415 use_epoch)) {
David Benjamin0d56f882015-12-19 17:05:56 -0500416 ssl_write_buffer_clear(ssl);
David Benjamin9417b762015-05-08 22:30:06 -0400417 return -1;
418 }
David Benjamin0d56f882015-12-19 17:05:56 -0500419 ssl_write_buffer_set_len(ssl, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800420
David Benjamin0d56f882015-12-19 17:05:56 -0500421 int ret = ssl_write_buffer_flush(ssl);
David Benjamin2e0901b2015-11-02 17:50:10 -0500422 if (ret <= 0) {
423 return ret;
424 }
425 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800426}
427
David Benjamin0d56f882015-12-19 17:05:56 -0500428int dtls1_dispatch_alert(SSL *ssl) {
David Benjamin0d56f882015-12-19 17:05:56 -0500429 ssl->s3->alert_dispatch = 0;
David Benjamina8571592016-03-10 01:41:55 -0500430 int ret = do_dtls1_write(ssl, SSL3_RT_ALERT, &ssl->s3->send_alert[0], 2,
431 dtls1_use_current_epoch);
432 if (ret <= 0) {
David Benjamin0d56f882015-12-19 17:05:56 -0500433 ssl->s3->alert_dispatch = 1;
David Benjamina8571592016-03-10 01:41:55 -0500434 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800435 }
436
David Benjamina8571592016-03-10 01:41:55 -0500437 /* If the alert is fatal, flush the BIO now. */
438 if (ssl->s3->send_alert[0] == SSL3_AL_FATAL) {
439 BIO_flush(ssl->wbio);
440 }
441
David Benjamin4e9cc712016-06-01 20:16:03 -0400442 ssl_do_msg_callback(ssl, 1 /* write */, ssl->version, SSL3_RT_ALERT,
443 ssl->s3->send_alert, 2);
David Benjamina8571592016-03-10 01:41:55 -0500444
David Benjamin4e9cc712016-06-01 20:16:03 -0400445 int alert = (ssl->s3->send_alert[0] << 8) | ssl->s3->send_alert[1];
446 ssl_do_info_callback(ssl, SSL_CB_WRITE_ALERT, alert);
David Benjamina8571592016-03-10 01:41:55 -0500447
448 return 1;
Adam Langley71d8a082014-12-13 16:28:18 -0800449}