blob: 12cdeacf62596a82fb876ba7b177df83f75be19f [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
Adam Langley71d8a082014-12-13 16:28:18 -0800127static int do_dtls1_write(SSL *s, 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;
167 rr->off = 0;
168 rr->data = out;
169 return 1;
170
171 case ssl_open_record_discard:
172 ssl_read_buffer_consume(ssl, consumed);
173 goto again;
174
175 case ssl_open_record_error:
176 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
177 return -1;
178
179 case ssl_open_record_partial:
180 /* Impossible in DTLS. */
181 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800182 }
Adam Langley95c29f32014-06-20 12:00:00 -0700183
David Benjaminb8d28cf2015-07-28 21:34:45 -0400184 assert(0);
185 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
186 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800187}
Adam Langley95c29f32014-06-20 12:00:00 -0700188
David Benjamina6022772015-05-30 16:22:10 -0400189int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
190 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
191}
192
193void dtls1_read_close_notify(SSL *ssl) {
David Benjamine9cb2ec2015-08-22 11:31:33 -0400194 /* Bidirectional shutdown doesn't make sense for an unordered transport. DTLS
195 * alerts also aren't delivered reliably, so we may even time out because the
196 * peer never received our close_notify. Report to the caller that the channel
197 * has fully shut down. */
198 ssl->shutdown |= SSL_RECEIVED_SHUTDOWN;
David Benjamina6022772015-05-30 16:22:10 -0400199}
200
Adam Langley95c29f32014-06-20 12:00:00 -0700201/* Return up to 'len' payload bytes received in 'type' records.
202 * 'type' is one of the following:
203 *
204 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
205 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
Adam Langley95c29f32014-06-20 12:00:00 -0700206 *
207 * If we don't have stored data to work from, read a SSL/TLS record first
208 * (possibly multiple records if we still don't have anything to return).
209 *
210 * This function must handle any surprises the peer may have for us, such as
211 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
212 * a surprise, but handled as if it were), or renegotiation requests.
213 * Also if record payloads contain fragments too small to process, we store
214 * them until there is enough for the respective protocol (the record protocol
215 * may use arbitrary fragmentation and even interleaving):
216 * Change cipher spec protocol
217 * just 1 byte needed, no need for keeping anything stored
218 * Alert protocol
219 * 2 bytes needed (AlertLevel, AlertDescription)
220 * Handshake protocol
221 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
222 * to detect unexpected Client Hello and Hello Request messages
223 * here, anything else is handled by higher layers
224 * Application data protocol
225 * none of our business
226 */
Adam Langley71d8a082014-12-13 16:28:18 -0800227int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500228 int al, i, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800229 unsigned int n;
230 SSL3_RECORD *rr;
David Benjamin82170242015-10-17 22:51:17 -0400231 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700232
David Benjamine9cb2ec2015-08-22 11:31:33 -0400233 if ((type != SSL3_RT_APPLICATION_DATA && type != SSL3_RT_HANDSHAKE) ||
234 (peek && type != SSL3_RT_APPLICATION_DATA)) {
David Benjamin3570d732015-06-29 00:28:17 -0400235 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800236 return -1;
237 }
Adam Langley95c29f32014-06-20 12:00:00 -0700238
Adam Langley71d8a082014-12-13 16:28:18 -0800239 if (!s->in_handshake && SSL_in_init(s)) {
240 /* type == SSL3_RT_APPLICATION_DATA */
241 i = s->handshake_func(s);
242 if (i < 0) {
243 return i;
244 }
245 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400246 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800247 return -1;
248 }
249 }
Adam Langley95c29f32014-06-20 12:00:00 -0700250
251start:
Adam Langley71d8a082014-12-13 16:28:18 -0800252 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700253
Adam Langley71d8a082014-12-13 16:28:18 -0800254 /* s->s3->rrec.type - is the type of record
255 * s->s3->rrec.data - data
256 * s->s3->rrec.off - offset into 'data' for next read
257 * s->s3->rrec.length - number of bytes. */
258 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley71d8a082014-12-13 16:28:18 -0800260 /* Check for timeout */
David Benjamin8c249802015-05-05 09:44:18 -0400261 if (DTLSv1_handle_timeout(s) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800262 goto start;
263 }
Adam Langley95c29f32014-06-20 12:00:00 -0700264
Adam Langley71d8a082014-12-13 16:28:18 -0800265 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400266 if (rr->length == 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800267 ret = dtls1_get_record(s);
268 if (ret <= 0) {
269 ret = dtls1_read_failed(s, ret);
270 /* anything other than a timeout is an error */
271 if (ret <= 0) {
272 return ret;
273 } else {
274 goto start;
275 }
276 }
277 }
Adam Langley95c29f32014-06-20 12:00:00 -0700278
Adam Langley71d8a082014-12-13 16:28:18 -0800279 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700280
Adam Langley71d8a082014-12-13 16:28:18 -0800281 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
282 * ssl3_get_finished. */
David Benjamindc3da932015-03-12 15:09:02 -0400283 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
284 rr->type != SSL3_RT_ALERT) {
285 /* We now have an unexpected record between CCS and Finished. Most likely
David Benjamin4417d052015-04-05 04:17:25 -0400286 * the packets were reordered on their way. DTLS is unreliable, so drop the
287 * packet and expect the peer to retransmit. */
Adam Langley71d8a082014-12-13 16:28:18 -0800288 rr->length = 0;
289 goto start;
290 }
Adam Langley95c29f32014-06-20 12:00:00 -0700291
Adam Langley71d8a082014-12-13 16:28:18 -0800292 /* If the other end has shut down, throw anything we read away (even in
293 * 'peek' mode) */
294 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
295 rr->length = 0;
296 s->rwstate = SSL_NOTHING;
297 return 0;
298 }
Adam Langley95c29f32014-06-20 12:00:00 -0700299
300
Adam Langley71d8a082014-12-13 16:28:18 -0800301 if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
302 /* make sure that we are not getting application data when we
303 * are doing a handshake for the first time */
304 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500305 (s->aead_read_ctx == NULL)) {
306 /* TODO(davidben): Is this check redundant with the handshake_func
307 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800308 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400309 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800310 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700311 }
312
David Benjamin4cf369b2015-08-22 01:35:43 -0400313 /* Discard empty records. */
314 if (rr->length == 0) {
315 goto start;
316 }
317
Adam Langley71d8a082014-12-13 16:28:18 -0800318 if (len <= 0) {
319 return len;
320 }
Adam Langley95c29f32014-06-20 12:00:00 -0700321
Adam Langley71d8a082014-12-13 16:28:18 -0800322 if ((unsigned int)len > rr->length) {
323 n = rr->length;
324 } else {
325 n = (unsigned int)len;
326 }
Adam Langley95c29f32014-06-20 12:00:00 -0700327
Adam Langley71d8a082014-12-13 16:28:18 -0800328 memcpy(buf, &(rr->data[rr->off]), n);
329 if (!peek) {
330 rr->length -= n;
331 rr->off += n;
332 if (rr->length == 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800333 rr->off = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400334 /* The record has been consumed, so we may now clear the buffer. */
335 ssl_read_buffer_discard(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800336 }
337 }
Adam Langley95c29f32014-06-20 12:00:00 -0700338
Adam Langley71d8a082014-12-13 16:28:18 -0800339 return n;
340 }
Adam Langley95c29f32014-06-20 12:00:00 -0700341
David Benjamin0ea8dda2015-01-31 20:33:40 -0500342 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700343
David Benjamin0ea8dda2015-01-31 20:33:40 -0500344 /* If an alert record, process one alert out of the record. Note that we allow
345 * a single record to contain multiple alerts. */
346 if (rr->type == SSL3_RT_ALERT) {
347 /* Alerts may not be fragmented. */
348 if (rr->length < 2) {
349 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400350 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500351 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800352 }
Adam Langley71d8a082014-12-13 16:28:18 -0800353
Adam Langley71d8a082014-12-13 16:28:18 -0800354 if (s->msg_callback) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500355 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800356 s->msg_callback_arg);
357 }
David Benjamin86058a22015-02-22 13:07:21 -0500358 const uint8_t alert_level = rr->data[rr->off++];
359 const uint8_t alert_descr = rr->data[rr->off++];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500360 rr->length -= 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800361
362 if (s->info_callback != NULL) {
363 cb = s->info_callback;
364 } else if (s->ctx->info_callback != NULL) {
365 cb = s->ctx->info_callback;
366 }
367
368 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500369 uint16_t alert = (alert_level << 8) | alert_descr;
370 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800371 }
372
David Benjamin86058a22015-02-22 13:07:21 -0500373 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800374 s->s3->warn_alert = alert_descr;
375 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
376 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
377 return 0;
378 }
David Benjamin86058a22015-02-22 13:07:21 -0500379 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800380 char tmp[16];
381
382 s->rwstate = SSL_NOTHING;
383 s->s3->fatal_alert = alert_descr;
David Benjamin3570d732015-06-29 00:28:17 -0400384 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800385 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
386 ERR_add_error_data(2, "SSL alert number ", tmp);
387 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
388 SSL_CTX_remove_session(s->ctx, s->session);
389 return 0;
390 } else {
391 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400392 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800393 goto f_err;
394 }
395
396 goto start;
397 }
398
Adam Langley71d8a082014-12-13 16:28:18 -0800399 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
David Benjaminf7f0f3a2015-03-02 21:09:31 -0500400 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
401 * record payload has to look like */
402 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800403 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400404 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800405 goto f_err;
406 }
407
408 rr->length = 0;
409
410 if (s->msg_callback) {
411 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
412 s->msg_callback_arg);
413 }
414
415 /* We can't process a CCS now, because previous handshake
416 * messages are still missing, so just drop it.
417 */
418 if (!s->d1->change_cipher_spec_ok) {
419 goto start;
420 }
421
422 s->d1->change_cipher_spec_ok = 0;
423
424 s->s3->change_cipher_spec = 1;
425 if (!ssl3_do_change_cipher_spec(s)) {
426 goto err;
427 }
428
Adam Langley71d8a082014-12-13 16:28:18 -0800429 goto start;
430 }
431
David Benjamin0ea8dda2015-01-31 20:33:40 -0500432 /* Unexpected handshake message. It may be a retransmitted Finished (the only
433 * post-CCS message). Otherwise, it's a pre-CCS handshake message from an
434 * unsupported renegotiation attempt. */
435 if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) {
436 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
437 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400438 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500439 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800440 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500441 struct hm_header_st msg_hdr;
442 dtls1_get_message_header(&rr->data[rr->off], &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800443
David Benjamin7eaab4c2015-03-02 19:01:16 -0500444 /* Ignore a stray Finished from the previous handshake. */
Adam Langley71d8a082014-12-13 16:28:18 -0800445 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500446 if (msg_hdr.frag_off == 0) {
447 /* Retransmit our last flight of messages. If the peer sends the second
448 * Finished, they may not have received ours. Only do this for the
449 * first fragment, in case the Finished was fragmented. */
450 if (dtls1_check_timeout_num(s) < 0) {
451 return -1;
452 }
453
454 dtls1_retransmit_buffered_messages(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800455 }
456
Adam Langley71d8a082014-12-13 16:28:18 -0800457 rr->length = 0;
458 goto start;
459 }
Adam Langley71d8a082014-12-13 16:28:18 -0800460 }
461
David Benjaminddb9f152015-02-03 15:44:39 -0500462 /* We already handled these. */
463 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT);
Adam Langley71d8a082014-12-13 16:28:18 -0800464
David Benjaminddb9f152015-02-03 15:44:39 -0500465 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400466 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800467
468f_err:
469 ssl3_send_alert(s, SSL3_AL_FATAL, al);
470err:
471 return -1;
472}
473
David Benjaminc933a472015-05-30 16:14:58 -0400474int dtls1_write_app_data(SSL *s, const void *buf_, int len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800475 int i;
476
477 if (SSL_in_init(s) && !s->in_handshake) {
478 i = s->handshake_func(s);
479 if (i < 0) {
480 return i;
481 }
482 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400483 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800484 return -1;
485 }
486 }
487
488 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400489 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800490 return -1;
491 }
492
David Benjaminc933a472015-05-30 16:14:58 -0400493 i = dtls1_write_bytes(s, SSL3_RT_APPLICATION_DATA, buf_, len,
494 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800495 return i;
496}
497
Adam Langley71d8a082014-12-13 16:28:18 -0800498/* Call this to write data in records of type 'type' It will return <= 0 if not
499 * all data has been sent or non-blocking IO. */
David Benjamin3e3090d2015-04-05 12:48:30 -0400500int dtls1_write_bytes(SSL *s, int type, const void *buf, int len,
501 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800502 int i;
503
504 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
505 s->rwstate = SSL_NOTHING;
David Benjamin3e3090d2015-04-05 12:48:30 -0400506 i = do_dtls1_write(s, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800507 return i;
508}
509
510static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400511 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500512 /* There should never be a pending write buffer in DTLS. One can't write half
513 * a datagram, so the write buffer is always dropped in
514 * |ssl_write_buffer_flush|. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400515 assert(!ssl_write_buffer_is_pending(s));
Adam Langley71d8a082014-12-13 16:28:18 -0800516
517 /* If we have an alert to send, lets send it */
518 if (s->s3->alert_dispatch) {
David Benjamin31a07792015-03-03 14:20:26 -0500519 int ret = s->method->ssl_dispatch_alert(s);
520 if (ret <= 0) {
521 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800522 }
523 /* if it went, fall through and send more stuff */
524 }
525
David Benjaminb8d28cf2015-07-28 21:34:45 -0400526 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
527 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500528 return -1;
529 }
530
Adam Langley71d8a082014-12-13 16:28:18 -0800531 if (len == 0) {
532 return 0;
533 }
534
David Benjaminb8d28cf2015-07-28 21:34:45 -0400535 size_t max_out = len + ssl_max_seal_overhead(s);
536 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500537 size_t ciphertext_len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400538 if (!ssl_write_buffer_init(s, &out, max_out) ||
539 !dtls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len,
540 use_epoch)) {
David Benjamin2e0901b2015-11-02 17:50:10 -0500541 ssl_write_buffer_clear(s);
David Benjamin9417b762015-05-08 22:30:06 -0400542 return -1;
543 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400544 ssl_write_buffer_set_len(s, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800545
David Benjamin2e0901b2015-11-02 17:50:10 -0500546 int ret = ssl_write_buffer_flush(s);
547 if (ret <= 0) {
548 return ret;
549 }
550 return (int)len;
Adam Langley71d8a082014-12-13 16:28:18 -0800551}
552
Adam Langley71d8a082014-12-13 16:28:18 -0800553int dtls1_dispatch_alert(SSL *s) {
554 int i, j;
David Benjamin82170242015-10-17 22:51:17 -0400555 void (*cb)(const SSL *ssl, int type, int value) = NULL;
Adam Langley71d8a082014-12-13 16:28:18 -0800556 uint8_t buf[DTLS1_AL_HEADER_LENGTH];
557 uint8_t *ptr = &buf[0];
558
559 s->s3->alert_dispatch = 0;
560
561 memset(buf, 0x00, sizeof(buf));
562 *ptr++ = s->s3->send_alert[0];
563 *ptr++ = s->s3->send_alert[1];
564
David Benjamin3e3090d2015-04-05 12:48:30 -0400565 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf),
566 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800567 if (i <= 0) {
568 s->s3->alert_dispatch = 1;
569 } else {
570 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
571 (void)BIO_flush(s->wbio);
572 }
573
574 if (s->msg_callback) {
575 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
576 s->msg_callback_arg);
577 }
578
579 if (s->info_callback != NULL) {
580 cb = s->info_callback;
581 } else if (s->ctx->info_callback != NULL) {
582 cb = s->ctx->info_callback;
583 }
584
585 if (cb != NULL) {
586 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
587 cb(s, SSL_CB_WRITE_ALERT, j);
588 }
589 }
590
591 return i;
592}