blob: 6681490efcd01d8b9e6eeca0693849048e91a343 [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
Adam Langley95c29f32014-06-20 12:00:00 -0700112#include <assert.h>
David Benjaminf0ae1702015-04-07 23:05:04 -0400113#include <stdio.h>
114#include <string.h>
Adam Langley95c29f32014-06-20 12:00:00 -0700115
116#include <openssl/buf.h>
117#include <openssl/mem.h>
118#include <openssl/evp.h>
119#include <openssl/err.h>
120#include <openssl/rand.h>
121
David Benjamin2ee94aa2015-04-07 22:38:30 -0400122#include "internal.h"
Adam Langley71d8a082014-12-13 16:28:18 -0800123
124
Adam Langley71d8a082014-12-13 16:28:18 -0800125static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400126 unsigned int len, enum dtls1_use_epoch_t use_epoch);
Adam Langley95c29f32014-06-20 12:00:00 -0700127
David Benjaminb8d28cf2015-07-28 21:34:45 -0400128/* dtls1_get_record reads a new input record. On success, it places it in
129 * |ssl->s3->rrec| and returns one. Otherwise it returns <= 0 on error or if
130 * more data is needed. */
131static int dtls1_get_record(SSL *ssl) {
Adam Langley95c29f32014-06-20 12:00:00 -0700132again:
David Benjaminb8d28cf2015-07-28 21:34:45 -0400133 /* Read a new packet if there is no unconsumed one. */
134 if (ssl_read_buffer_len(ssl) == 0) {
135 int ret = ssl_read_buffer_extend_to(ssl, 0 /* unused */);
136 if (ret <= 0) {
137 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800138 }
Adam Langley71d8a082014-12-13 16:28:18 -0800139 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400140 assert(ssl_read_buffer_len(ssl) > 0);
Adam Langley95c29f32014-06-20 12:00:00 -0700141
David Benjaminb8d28cf2015-07-28 21:34:45 -0400142 /* Ensure the packet is large enough to decrypt in-place. */
143 if (ssl_read_buffer_len(ssl) < ssl_record_prefix_len(ssl)) {
144 ssl_read_buffer_clear(ssl);
David Benjamin0afbcc02015-04-05 04:06:20 -0400145 goto again;
Adam Langley71d8a082014-12-13 16:28:18 -0800146 }
Adam Langley95c29f32014-06-20 12:00:00 -0700147
David Benjaminb8d28cf2015-07-28 21:34:45 -0400148 uint8_t *out = ssl_read_buffer(ssl) + ssl_record_prefix_len(ssl);
149 size_t max_out = ssl_read_buffer_len(ssl) - ssl_record_prefix_len(ssl);
150 uint8_t type, alert;
151 size_t len, consumed;
152 switch (dtls_open_record(ssl, &type, out, &len, &consumed, &alert, max_out,
153 ssl_read_buffer(ssl), ssl_read_buffer_len(ssl))) {
154 case ssl_open_record_success:
155 ssl_read_buffer_consume(ssl, consumed);
156
157 /* Discard empty records.
158 * TODO(davidben): This logic should be moved to a higher level. See
159 * https://crbug.com/521840.
160 * TODO(davidben): Limit the number of empty records as in TLS? This is
161 * useful if we also limit discarded packets. */
162 if (len == 0) {
163 goto again;
164 }
165
166 if (len > 0xffff) {
167 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
168 return -1;
169 }
170
171 SSL3_RECORD *rr = &ssl->s3->rrec;
172 rr->type = type;
173 rr->length = (uint16_t)len;
174 rr->off = 0;
175 rr->data = out;
176 return 1;
177
178 case ssl_open_record_discard:
179 ssl_read_buffer_consume(ssl, consumed);
180 goto again;
181
182 case ssl_open_record_error:
183 ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
184 return -1;
185
186 case ssl_open_record_partial:
187 /* Impossible in DTLS. */
188 break;
Adam Langley71d8a082014-12-13 16:28:18 -0800189 }
Adam Langley95c29f32014-06-20 12:00:00 -0700190
David Benjaminb8d28cf2015-07-28 21:34:45 -0400191 assert(0);
192 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
193 return -1;
Adam Langley71d8a082014-12-13 16:28:18 -0800194}
Adam Langley95c29f32014-06-20 12:00:00 -0700195
David Benjamina6022772015-05-30 16:22:10 -0400196int dtls1_read_app_data(SSL *ssl, uint8_t *buf, int len, int peek) {
197 return dtls1_read_bytes(ssl, SSL3_RT_APPLICATION_DATA, buf, len, peek);
198}
199
200void dtls1_read_close_notify(SSL *ssl) {
201 dtls1_read_bytes(ssl, 0, NULL, 0, 0);
202}
203
Adam Langley95c29f32014-06-20 12:00:00 -0700204/* Return up to 'len' payload bytes received in 'type' records.
205 * 'type' is one of the following:
206 *
207 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
208 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
209 * - 0 (during a shutdown, no data has to be returned)
210 *
211 * If we don't have stored data to work from, read a SSL/TLS record first
212 * (possibly multiple records if we still don't have anything to return).
213 *
214 * This function must handle any surprises the peer may have for us, such as
215 * Alert records (e.g. close_notify), ChangeCipherSpec records (not really
216 * a surprise, but handled as if it were), or renegotiation requests.
217 * Also if record payloads contain fragments too small to process, we store
218 * them until there is enough for the respective protocol (the record protocol
219 * may use arbitrary fragmentation and even interleaving):
220 * Change cipher spec protocol
221 * just 1 byte needed, no need for keeping anything stored
222 * Alert protocol
223 * 2 bytes needed (AlertLevel, AlertDescription)
224 * Handshake protocol
225 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
226 * to detect unexpected Client Hello and Hello Request messages
227 * here, anything else is handled by higher layers
228 * Application data protocol
229 * none of our business
230 */
Adam Langley71d8a082014-12-13 16:28:18 -0800231int dtls1_read_bytes(SSL *s, int type, unsigned char *buf, int len, int peek) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500232 int al, i, ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800233 unsigned int n;
234 SSL3_RECORD *rr;
235 void (*cb)(const SSL *ssl, int type2, int val) = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700236
Adam Langley71d8a082014-12-13 16:28:18 -0800237 /* XXX: check what the second '&& type' is about */
238 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
239 (type != SSL3_RT_HANDSHAKE) && type) ||
240 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
David Benjamin3570d732015-06-29 00:28:17 -0400241 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Adam Langley71d8a082014-12-13 16:28:18 -0800242 return -1;
243 }
Adam Langley95c29f32014-06-20 12:00:00 -0700244
Adam Langley71d8a082014-12-13 16:28:18 -0800245 if (!s->in_handshake && SSL_in_init(s)) {
246 /* type == SSL3_RT_APPLICATION_DATA */
247 i = s->handshake_func(s);
248 if (i < 0) {
249 return i;
250 }
251 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400252 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800253 return -1;
254 }
255 }
Adam Langley95c29f32014-06-20 12:00:00 -0700256
257start:
Adam Langley71d8a082014-12-13 16:28:18 -0800258 s->rwstate = SSL_NOTHING;
Adam Langley95c29f32014-06-20 12:00:00 -0700259
Adam Langley71d8a082014-12-13 16:28:18 -0800260 /* s->s3->rrec.type - is the type of record
261 * s->s3->rrec.data - data
262 * s->s3->rrec.off - offset into 'data' for next read
263 * s->s3->rrec.length - number of bytes. */
264 rr = &s->s3->rrec;
Adam Langley95c29f32014-06-20 12:00:00 -0700265
Adam Langley71d8a082014-12-13 16:28:18 -0800266 /* Check for timeout */
David Benjamin8c249802015-05-05 09:44:18 -0400267 if (DTLSv1_handle_timeout(s) > 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800268 goto start;
269 }
Adam Langley95c29f32014-06-20 12:00:00 -0700270
Adam Langley71d8a082014-12-13 16:28:18 -0800271 /* get new packet if necessary */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400272 if (rr->length == 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800273 ret = dtls1_get_record(s);
274 if (ret <= 0) {
275 ret = dtls1_read_failed(s, ret);
276 /* anything other than a timeout is an error */
277 if (ret <= 0) {
278 return ret;
279 } else {
280 goto start;
281 }
282 }
283 }
Adam Langley95c29f32014-06-20 12:00:00 -0700284
Adam Langley71d8a082014-12-13 16:28:18 -0800285 /* we now have a packet which can be read and processed */
Adam Langley95c29f32014-06-20 12:00:00 -0700286
Adam Langley71d8a082014-12-13 16:28:18 -0800287 /* |change_cipher_spec is set when we receive a ChangeCipherSpec and reset by
288 * ssl3_get_finished. */
David Benjamindc3da932015-03-12 15:09:02 -0400289 if (s->s3->change_cipher_spec && rr->type != SSL3_RT_HANDSHAKE &&
290 rr->type != SSL3_RT_ALERT) {
291 /* We now have an unexpected record between CCS and Finished. Most likely
David Benjamin4417d052015-04-05 04:17:25 -0400292 * the packets were reordered on their way. DTLS is unreliable, so drop the
293 * packet and expect the peer to retransmit. */
Adam Langley71d8a082014-12-13 16:28:18 -0800294 rr->length = 0;
295 goto start;
296 }
Adam Langley95c29f32014-06-20 12:00:00 -0700297
Adam Langley71d8a082014-12-13 16:28:18 -0800298 /* If the other end has shut down, throw anything we read away (even in
299 * 'peek' mode) */
300 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
301 rr->length = 0;
302 s->rwstate = SSL_NOTHING;
303 return 0;
304 }
Adam Langley95c29f32014-06-20 12:00:00 -0700305
306
Adam Langley71d8a082014-12-13 16:28:18 -0800307 if (type == rr->type) { /* SSL3_RT_APPLICATION_DATA or SSL3_RT_HANDSHAKE */
308 /* make sure that we are not getting application data when we
309 * are doing a handshake for the first time */
310 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
David Benjaminb8a56f12014-12-23 11:41:02 -0500311 (s->aead_read_ctx == NULL)) {
312 /* TODO(davidben): Is this check redundant with the handshake_func
313 * check? */
Adam Langley71d8a082014-12-13 16:28:18 -0800314 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400315 OPENSSL_PUT_ERROR(SSL, SSL_R_APP_DATA_IN_HANDSHAKE);
Adam Langley71d8a082014-12-13 16:28:18 -0800316 goto f_err;
Adam Langley95c29f32014-06-20 12:00:00 -0700317 }
318
Adam Langley71d8a082014-12-13 16:28:18 -0800319 if (len <= 0) {
320 return len;
321 }
Adam Langley95c29f32014-06-20 12:00:00 -0700322
Adam Langley71d8a082014-12-13 16:28:18 -0800323 if ((unsigned int)len > rr->length) {
324 n = rr->length;
325 } else {
326 n = (unsigned int)len;
327 }
Adam Langley95c29f32014-06-20 12:00:00 -0700328
Adam Langley71d8a082014-12-13 16:28:18 -0800329 memcpy(buf, &(rr->data[rr->off]), n);
330 if (!peek) {
331 rr->length -= n;
332 rr->off += n;
333 if (rr->length == 0) {
Adam Langley71d8a082014-12-13 16:28:18 -0800334 rr->off = 0;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400335 /* The record has been consumed, so we may now clear the buffer. */
336 ssl_read_buffer_discard(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800337 }
338 }
Adam Langley95c29f32014-06-20 12:00:00 -0700339
Adam Langley71d8a082014-12-13 16:28:18 -0800340 return n;
341 }
Adam Langley95c29f32014-06-20 12:00:00 -0700342
David Benjamin0ea8dda2015-01-31 20:33:40 -0500343 /* If we get here, then type != rr->type. */
Adam Langley95c29f32014-06-20 12:00:00 -0700344
David Benjamin0ea8dda2015-01-31 20:33:40 -0500345 /* If an alert record, process one alert out of the record. Note that we allow
346 * a single record to contain multiple alerts. */
347 if (rr->type == SSL3_RT_ALERT) {
348 /* Alerts may not be fragmented. */
349 if (rr->length < 2) {
350 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400351 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500352 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800353 }
Adam Langley71d8a082014-12-13 16:28:18 -0800354
Adam Langley71d8a082014-12-13 16:28:18 -0800355 if (s->msg_callback) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500356 s->msg_callback(0, s->version, SSL3_RT_ALERT, &rr->data[rr->off], 2, s,
Adam Langley71d8a082014-12-13 16:28:18 -0800357 s->msg_callback_arg);
358 }
David Benjamin86058a22015-02-22 13:07:21 -0500359 const uint8_t alert_level = rr->data[rr->off++];
360 const uint8_t alert_descr = rr->data[rr->off++];
David Benjamin0ea8dda2015-01-31 20:33:40 -0500361 rr->length -= 2;
Adam Langley71d8a082014-12-13 16:28:18 -0800362
363 if (s->info_callback != NULL) {
364 cb = s->info_callback;
365 } else if (s->ctx->info_callback != NULL) {
366 cb = s->ctx->info_callback;
367 }
368
369 if (cb != NULL) {
David Benjamin0ea8dda2015-01-31 20:33:40 -0500370 uint16_t alert = (alert_level << 8) | alert_descr;
371 cb(s, SSL_CB_READ_ALERT, alert);
Adam Langley71d8a082014-12-13 16:28:18 -0800372 }
373
David Benjamin86058a22015-02-22 13:07:21 -0500374 if (alert_level == SSL3_AL_WARNING) {
Adam Langley71d8a082014-12-13 16:28:18 -0800375 s->s3->warn_alert = alert_descr;
376 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
377 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
378 return 0;
379 }
David Benjamin86058a22015-02-22 13:07:21 -0500380 } else if (alert_level == SSL3_AL_FATAL) {
Adam Langley71d8a082014-12-13 16:28:18 -0800381 char tmp[16];
382
383 s->rwstate = SSL_NOTHING;
384 s->s3->fatal_alert = alert_descr;
David Benjamin3570d732015-06-29 00:28:17 -0400385 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
Adam Langley71d8a082014-12-13 16:28:18 -0800386 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
387 ERR_add_error_data(2, "SSL alert number ", tmp);
388 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
389 SSL_CTX_remove_session(s->ctx, s->session);
390 return 0;
391 } else {
392 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400393 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
Adam Langley71d8a082014-12-13 16:28:18 -0800394 goto f_err;
395 }
396
397 goto start;
398 }
399
400 if (s->shutdown & SSL_SENT_SHUTDOWN) {
401 /* but we have not received a shutdown */
402 s->rwstate = SSL_NOTHING;
403 rr->length = 0;
404 return 0;
405 }
406
407 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
David Benjaminf7f0f3a2015-03-02 21:09:31 -0500408 /* 'Change Cipher Spec' is just a single byte, so we know exactly what the
409 * record payload has to look like */
410 if (rr->length != 1 || rr->off != 0 || rr->data[0] != SSL3_MT_CCS) {
Adam Langley71d8a082014-12-13 16:28:18 -0800411 al = SSL_AD_ILLEGAL_PARAMETER;
David Benjamin3570d732015-06-29 00:28:17 -0400412 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_CHANGE_CIPHER_SPEC);
Adam Langley71d8a082014-12-13 16:28:18 -0800413 goto f_err;
414 }
415
416 rr->length = 0;
417
418 if (s->msg_callback) {
419 s->msg_callback(0, s->version, SSL3_RT_CHANGE_CIPHER_SPEC, rr->data, 1, s,
420 s->msg_callback_arg);
421 }
422
423 /* We can't process a CCS now, because previous handshake
424 * messages are still missing, so just drop it.
425 */
426 if (!s->d1->change_cipher_spec_ok) {
427 goto start;
428 }
429
430 s->d1->change_cipher_spec_ok = 0;
431
432 s->s3->change_cipher_spec = 1;
433 if (!ssl3_do_change_cipher_spec(s)) {
434 goto err;
435 }
436
437 /* do this whenever CCS is processed */
438 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
439
440 goto start;
441 }
442
David Benjamin0ea8dda2015-01-31 20:33:40 -0500443 /* Unexpected handshake message. It may be a retransmitted Finished (the only
444 * post-CCS message). Otherwise, it's a pre-CCS handshake message from an
445 * unsupported renegotiation attempt. */
446 if (rr->type == SSL3_RT_HANDSHAKE && !s->in_handshake) {
447 if (rr->length < DTLS1_HM_HEADER_LENGTH) {
448 al = SSL_AD_DECODE_ERROR;
David Benjamin3570d732015-06-29 00:28:17 -0400449 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HANDSHAKE_RECORD);
David Benjamin0ea8dda2015-01-31 20:33:40 -0500450 goto f_err;
Adam Langley71d8a082014-12-13 16:28:18 -0800451 }
David Benjamin0ea8dda2015-01-31 20:33:40 -0500452 struct hm_header_st msg_hdr;
453 dtls1_get_message_header(&rr->data[rr->off], &msg_hdr);
Adam Langley71d8a082014-12-13 16:28:18 -0800454
David Benjamin7eaab4c2015-03-02 19:01:16 -0500455 /* Ignore a stray Finished from the previous handshake. */
Adam Langley71d8a082014-12-13 16:28:18 -0800456 if (msg_hdr.type == SSL3_MT_FINISHED) {
David Benjamin7eaab4c2015-03-02 19:01:16 -0500457 if (msg_hdr.frag_off == 0) {
458 /* Retransmit our last flight of messages. If the peer sends the second
459 * Finished, they may not have received ours. Only do this for the
460 * first fragment, in case the Finished was fragmented. */
461 if (dtls1_check_timeout_num(s) < 0) {
462 return -1;
463 }
464
465 dtls1_retransmit_buffered_messages(s);
Adam Langley71d8a082014-12-13 16:28:18 -0800466 }
467
Adam Langley71d8a082014-12-13 16:28:18 -0800468 rr->length = 0;
469 goto start;
470 }
Adam Langley71d8a082014-12-13 16:28:18 -0800471 }
472
David Benjaminddb9f152015-02-03 15:44:39 -0500473 /* We already handled these. */
474 assert(rr->type != SSL3_RT_CHANGE_CIPHER_SPEC && rr->type != SSL3_RT_ALERT);
Adam Langley71d8a082014-12-13 16:28:18 -0800475
David Benjaminddb9f152015-02-03 15:44:39 -0500476 al = SSL_AD_UNEXPECTED_MESSAGE;
David Benjamin3570d732015-06-29 00:28:17 -0400477 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
Adam Langley71d8a082014-12-13 16:28:18 -0800478
479f_err:
480 ssl3_send_alert(s, SSL3_AL_FATAL, al);
481err:
482 return -1;
483}
484
David Benjaminc933a472015-05-30 16:14:58 -0400485int dtls1_write_app_data(SSL *s, const void *buf_, int len) {
Adam Langley71d8a082014-12-13 16:28:18 -0800486 int i;
487
488 if (SSL_in_init(s) && !s->in_handshake) {
489 i = s->handshake_func(s);
490 if (i < 0) {
491 return i;
492 }
493 if (i == 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400494 OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
Adam Langley71d8a082014-12-13 16:28:18 -0800495 return -1;
496 }
497 }
498
499 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
David Benjamin3570d732015-06-29 00:28:17 -0400500 OPENSSL_PUT_ERROR(SSL, SSL_R_DTLS_MESSAGE_TOO_BIG);
Adam Langley71d8a082014-12-13 16:28:18 -0800501 return -1;
502 }
503
David Benjaminc933a472015-05-30 16:14:58 -0400504 i = dtls1_write_bytes(s, SSL3_RT_APPLICATION_DATA, buf_, len,
505 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800506 return i;
507}
508
Adam Langley71d8a082014-12-13 16:28:18 -0800509/* Call this to write data in records of type 'type' It will return <= 0 if not
510 * all data has been sent or non-blocking IO. */
David Benjamin3e3090d2015-04-05 12:48:30 -0400511int dtls1_write_bytes(SSL *s, int type, const void *buf, int len,
512 enum dtls1_use_epoch_t use_epoch) {
Adam Langley71d8a082014-12-13 16:28:18 -0800513 int i;
514
515 assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
516 s->rwstate = SSL_NOTHING;
David Benjamin3e3090d2015-04-05 12:48:30 -0400517 i = do_dtls1_write(s, type, buf, len, use_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800518 return i;
519}
520
521static int do_dtls1_write(SSL *s, int type, const uint8_t *buf,
David Benjamin3e3090d2015-04-05 12:48:30 -0400522 unsigned int len, enum dtls1_use_epoch_t use_epoch) {
David Benjamin883e49f2015-04-05 12:19:14 -0400523 /* ssl3_write_pending drops the write if |BIO_write| fails in DTLS, so there
524 * is never pending data. */
David Benjaminb8d28cf2015-07-28 21:34:45 -0400525 assert(!ssl_write_buffer_is_pending(s));
Adam Langley71d8a082014-12-13 16:28:18 -0800526
527 /* If we have an alert to send, lets send it */
528 if (s->s3->alert_dispatch) {
David Benjamin31a07792015-03-03 14:20:26 -0500529 int ret = s->method->ssl_dispatch_alert(s);
530 if (ret <= 0) {
531 return ret;
Adam Langley71d8a082014-12-13 16:28:18 -0800532 }
533 /* if it went, fall through and send more stuff */
534 }
535
David Benjaminb8d28cf2015-07-28 21:34:45 -0400536 if (len > SSL3_RT_MAX_PLAIN_LENGTH) {
537 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
David Benjamin31a07792015-03-03 14:20:26 -0500538 return -1;
539 }
540
Adam Langley71d8a082014-12-13 16:28:18 -0800541 if (len == 0) {
542 return 0;
543 }
544
David Benjaminb8d28cf2015-07-28 21:34:45 -0400545 size_t max_out = len + ssl_max_seal_overhead(s);
546 uint8_t *out;
David Benjamin31a07792015-03-03 14:20:26 -0500547 size_t ciphertext_len;
David Benjaminb8d28cf2015-07-28 21:34:45 -0400548 if (!ssl_write_buffer_init(s, &out, max_out) ||
549 !dtls_seal_record(s, out, &ciphertext_len, max_out, type, buf, len,
550 use_epoch)) {
David Benjamin9417b762015-05-08 22:30:06 -0400551 return -1;
552 }
David Benjaminb8d28cf2015-07-28 21:34:45 -0400553 ssl_write_buffer_set_len(s, ciphertext_len);
Adam Langley71d8a082014-12-13 16:28:18 -0800554
555 /* memorize arguments so that ssl3_write_pending can detect bad write retries
556 * later */
557 s->s3->wpend_tot = len;
558 s->s3->wpend_buf = buf;
559 s->s3->wpend_type = type;
560 s->s3->wpend_ret = len;
561
562 /* we now just need to write the buffer */
563 return ssl3_write_pending(s, type, buf, len);
Adam Langley71d8a082014-12-13 16:28:18 -0800564}
565
Adam Langley71d8a082014-12-13 16:28:18 -0800566int dtls1_dispatch_alert(SSL *s) {
567 int i, j;
568 void (*cb)(const SSL *ssl, int type, int val) = NULL;
569 uint8_t buf[DTLS1_AL_HEADER_LENGTH];
570 uint8_t *ptr = &buf[0];
571
572 s->s3->alert_dispatch = 0;
573
574 memset(buf, 0x00, sizeof(buf));
575 *ptr++ = s->s3->send_alert[0];
576 *ptr++ = s->s3->send_alert[1];
577
David Benjamin3e3090d2015-04-05 12:48:30 -0400578 i = do_dtls1_write(s, SSL3_RT_ALERT, &buf[0], sizeof(buf),
579 dtls1_use_current_epoch);
Adam Langley71d8a082014-12-13 16:28:18 -0800580 if (i <= 0) {
581 s->s3->alert_dispatch = 1;
582 } else {
583 if (s->s3->send_alert[0] == SSL3_AL_FATAL) {
584 (void)BIO_flush(s->wbio);
585 }
586
587 if (s->msg_callback) {
588 s->msg_callback(1, s->version, SSL3_RT_ALERT, s->s3->send_alert, 2, s,
589 s->msg_callback_arg);
590 }
591
592 if (s->info_callback != NULL) {
593 cb = s->info_callback;
594 } else if (s->ctx->info_callback != NULL) {
595 cb = s->ctx->info_callback;
596 }
597
598 if (cb != NULL) {
599 j = (s->s3->send_alert[0] << 8) | s->s3->send_alert[1];
600 cb(s, SSL_CB_WRITE_ALERT, j);
601 }
602 }
603
604 return i;
605}
606
Adam Langley71d8a082014-12-13 16:28:18 -0800607void dtls1_reset_seq_numbers(SSL *s, int rw) {
608 uint8_t *seq;
609 unsigned int seq_bytes = sizeof(s->s3->read_sequence);
610
611 if (rw & SSL3_CC_READ) {
612 seq = s->s3->read_sequence;
613 s->d1->r_epoch++;
David Benjamin0afbcc02015-04-05 04:06:20 -0400614 memset(&s->d1->bitmap, 0, sizeof(DTLS1_BITMAP));
Adam Langley71d8a082014-12-13 16:28:18 -0800615 } else {
616 seq = s->s3->write_sequence;
617 memcpy(s->d1->last_write_sequence, seq, sizeof(s->s3->write_sequence));
618 s->d1->w_epoch++;
619 }
620
621 memset(seq, 0x00, seq_bytes);
622}