]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/record/rec_layer_d1.c
Fix DTLS buffered message DoS attack
[thirdparty/openssl.git] / ssl / record / rec_layer_d1.c
CommitLineData
0f113f3e 1/*
846e33c7 2 * Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
0f113f3e 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
36d16f8e
BL
8 */
9
10#include <stdio.h>
11#include <errno.h>
12#define USE_SOCKETS
999005e4 13#include "../ssl_locl.h"
36d16f8e
BL
14#include <openssl/evp.h>
15#include <openssl/buffer.h>
1711f8de 16#include "record_locl.h"
40f37188
MC
17
18int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
19{
20 DTLS_RECORD_LAYER *d;
0485d540 21
b4faea50 22 if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
40f37188 23 return (0);
40f37188
MC
24
25 rl->d = d;
5fb6f80c 26
cb2ce7ab
MC
27 d->unprocessed_rcds.q = pqueue_new();
28 d->processed_rcds.q = pqueue_new();
24a1e2f2 29 d->buffered_app_data.q = pqueue_new();
cb2ce7ab 30
a71edf3b
MC
31 if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
32 || d->buffered_app_data.q == NULL) {
25aaa98a
RS
33 pqueue_free(d->unprocessed_rcds.q);
34 pqueue_free(d->processed_rcds.q);
35 pqueue_free(d->buffered_app_data.q);
cb2ce7ab
MC
36 OPENSSL_free(d);
37 rl->d = NULL;
38 return (0);
39 }
40f37188
MC
40
41 return 1;
42}
43
44void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
45{
cb2ce7ab
MC
46 DTLS_RECORD_LAYER_clear(rl);
47 pqueue_free(rl->d->unprocessed_rcds.q);
48 pqueue_free(rl->d->processed_rcds.q);
24a1e2f2 49 pqueue_free(rl->d->buffered_app_data.q);
40f37188
MC
50 OPENSSL_free(rl->d);
51 rl->d = NULL;
52}
53
54void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
55{
56 DTLS_RECORD_LAYER *d;
cb2ce7ab
MC
57 pitem *item = NULL;
58 DTLS1_RECORD_DATA *rdata;
cf2cede4
RS
59 pqueue *unprocessed_rcds;
60 pqueue *processed_rcds;
61 pqueue *buffered_app_data;
cb2ce7ab 62
40f37188 63 d = rl->d;
0485d540 64
cb2ce7ab
MC
65 while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
66 rdata = (DTLS1_RECORD_DATA *)item->data;
b548a1f1 67 OPENSSL_free(rdata->rbuf.buf);
cb2ce7ab
MC
68 OPENSSL_free(item->data);
69 pitem_free(item);
70 }
71
72 while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
73 rdata = (DTLS1_RECORD_DATA *)item->data;
b548a1f1 74 OPENSSL_free(rdata->rbuf.buf);
cb2ce7ab
MC
75 OPENSSL_free(item->data);
76 pitem_free(item);
77 }
78
24a1e2f2
MC
79 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
80 rdata = (DTLS1_RECORD_DATA *)item->data;
b548a1f1 81 OPENSSL_free(rdata->rbuf.buf);
24a1e2f2
MC
82 OPENSSL_free(item->data);
83 pitem_free(item);
84 }
85
cb2ce7ab
MC
86 unprocessed_rcds = d->unprocessed_rcds.q;
87 processed_rcds = d->processed_rcds.q;
24a1e2f2 88 buffered_app_data = d->buffered_app_data.q;
b4faea50 89 memset(d, 0, sizeof(*d));
cb2ce7ab
MC
90 d->unprocessed_rcds.q = unprocessed_rcds;
91 d->processed_rcds.q = processed_rcds;
24a1e2f2 92 d->buffered_app_data.q = buffered_app_data;
40f37188
MC
93}
94
3bb8f87d
MC
95void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
96{
97 if (e == rl->d->w_epoch - 1) {
98 memcpy(rl->d->curr_write_sequence,
a230b26e 99 rl->write_sequence, sizeof(rl->write_sequence));
3bb8f87d 100 memcpy(rl->write_sequence,
a230b26e 101 rl->d->last_write_sequence, sizeof(rl->write_sequence));
3bb8f87d
MC
102 } else if (e == rl->d->w_epoch + 1) {
103 memcpy(rl->d->last_write_sequence,
a230b26e 104 rl->write_sequence, sizeof(unsigned char[8]));
3bb8f87d 105 memcpy(rl->write_sequence,
a230b26e 106 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
3bb8f87d
MC
107 }
108 rl->d->w_epoch = e;
109}
110
44cc35d3
MC
111void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl)
112{
113 memcpy(rl->write_sequence, rl->read_sequence, sizeof(rl->write_sequence));
114}
115
e3d0dae7
MC
116void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
117{
118 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
119}
120
0f113f3e 121static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
a773b52a 122 int len);
36d16f8e 123
36d16f8e 124/* copy buffered record into SSL structure */
0f113f3e
MC
125static int dtls1_copy_record(SSL *s, pitem *item)
126{
36d16f8e
BL
127 DTLS1_RECORD_DATA *rdata;
128
129 rdata = (DTLS1_RECORD_DATA *)item->data;
0f113f3e 130
88c23039 131 SSL3_BUFFER_release(&s->rlayer.rbuf);
0f113f3e 132
7a7048af
MC
133 s->rlayer.packet = rdata->packet;
134 s->rlayer.packet_length = rdata->packet_length;
88c23039
MC
135 memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
136 memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
36d16f8e 137
0f113f3e 138 /* Set proper sequence number for mac calculation */
de07f311 139 memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
0f113f3e
MC
140
141 return (1);
142}
36d16f8e 143
6f7ae319 144int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
0f113f3e
MC
145{
146 DTLS1_RECORD_DATA *rdata;
147 pitem *item;
148
149 /* Limit the size of the queue to prevent DOS attacks */
150 if (pqueue_size(queue->q) >= 100)
151 return 0;
152
b4faea50 153 rdata = OPENSSL_malloc(sizeof(*rdata));
0f113f3e
MC
154 item = pitem_new(priority, rdata);
155 if (rdata == NULL || item == NULL) {
b548a1f1 156 OPENSSL_free(rdata);
25aaa98a 157 pitem_free(item);
0f113f3e 158 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
06c6a2b4 159 return -1;
0f113f3e
MC
160 }
161
7a7048af
MC
162 rdata->packet = s->rlayer.packet;
163 rdata->packet_length = s->rlayer.packet_length;
88c23039
MC
164 memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
165 memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
0f113f3e
MC
166
167 item->data = rdata;
36d16f8e 168
7e159e01 169#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
170 /* Store bio_dgram_sctp_rcvinfo struct */
171 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
5998e290
MC
172 (SSL_get_state(s) == TLS_ST_SR_FINISHED
173 || SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
0f113f3e
MC
174 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
175 sizeof(rdata->recordinfo), &rdata->recordinfo);
176 }
7e159e01
DSH
177#endif
178
7a7048af
MC
179 s->rlayer.packet = NULL;
180 s->rlayer.packet_length = 0;
16f8d4eb
RS
181 memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
182 memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
0f113f3e
MC
183
184 if (!ssl3_setup_buffers(s)) {
185 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
b548a1f1 186 OPENSSL_free(rdata->rbuf.buf);
0f113f3e
MC
187 OPENSSL_free(rdata);
188 pitem_free(item);
189 return (-1);
190 }
36d16f8e 191
0f113f3e
MC
192 /* insert should not fail, since duplicates are dropped */
193 if (pqueue_insert(queue->q, item) == NULL) {
194 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
b548a1f1 195 OPENSSL_free(rdata->rbuf.buf);
0f113f3e
MC
196 OPENSSL_free(rdata);
197 pitem_free(item);
198 return (-1);
199 }
36d16f8e 200
0f113f3e
MC
201 return (1);
202}
203
fe589e61 204int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
0f113f3e 205{
36d16f8e
BL
206 pitem *item;
207
208 item = pqueue_pop(queue->q);
0f113f3e 209 if (item) {
36d16f8e
BL
210 dtls1_copy_record(s, item);
211
212 OPENSSL_free(item->data);
0f113f3e 213 pitem_free(item);
36d16f8e 214
0f113f3e 215 return (1);
36d16f8e
BL
216 }
217
0f113f3e
MC
218 return (0);
219}
36d16f8e 220
0f113f3e
MC
221/*
222 * retrieve a buffered record that belongs to the new epoch, i.e., not
223 * processed yet
224 */
36d16f8e
BL
225#define dtls1_get_unprocessed_record(s) \
226 dtls1_retrieve_buffered_record((s), \
cb2ce7ab 227 &((s)->rlayer.d->unprocessed_rcds))
36d16f8e 228
fe589e61 229int dtls1_process_buffered_records(SSL *s)
0f113f3e 230{
36d16f8e 231 pitem *item;
738ad946 232 SSL3_BUFFER *rb;
1fb9fdc3
MC
233 SSL3_RECORD *rr;
234 DTLS1_BITMAP *bitmap;
235 unsigned int is_next_epoch;
236 int replayok = 1;
0f113f3e 237
cb2ce7ab 238 item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
0f113f3e 239 if (item) {
36d16f8e 240 /* Check if epoch is current. */
cb2ce7ab 241 if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
1fb9fdc3
MC
242 return 1; /* Nothing to do. */
243
244 rr = RECORD_LAYER_get_rrec(&s->rlayer);
0f113f3e 245
738ad946
MC
246 rb = RECORD_LAYER_get_rbuf(&s->rlayer);
247
248 if (SSL3_BUFFER_get_left(rb) > 0) {
249 /*
250 * We've still got data from the current packet to read. There could
251 * be a record from the new epoch in it - so don't overwrite it
252 * with the unprocessed records yet (we'll do it when we've
253 * finished reading the current packet).
254 */
255 return 1;
256 }
257
36d16f8e 258 /* Process all the records. */
cb2ce7ab 259 while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
36d16f8e 260 dtls1_get_unprocessed_record(s);
1fb9fdc3
MC
261 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
262 if (bitmap == NULL) {
263 /*
264 * Should not happen. This will only ever be NULL when the
265 * current record is from a different epoch. But that cannot
266 * be the case because we already checked the epoch above
267 */
268 SSLerr(SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
269 ERR_R_INTERNAL_ERROR);
270 return 0;
271 }
272#ifndef OPENSSL_NO_SCTP
273 /* Only do replay check if no SCTP bio */
274 if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))
275#endif
276 {
277 /*
278 * Check whether this is a repeat, or aged record. We did this
279 * check once already when we first received the record - but
280 * we might have updated the window since then due to
281 * records we subsequently processed.
282 */
283 replayok = dtls1_record_replay_check(s, bitmap);
284 }
285
286 if (!replayok || !dtls1_process_record(s, bitmap)) {
287 /* dump this record */
288 rr->length = 0;
289 RECORD_LAYER_reset_packet_length(&s->rlayer);
290 continue;
291 }
292
cb2ce7ab 293 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
1fb9fdc3
MC
294 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0)
295 return 0;
36d16f8e 296 }
0f113f3e 297 }
36d16f8e 298
0f113f3e
MC
299 /*
300 * sync epoch numbers once all the unprocessed records have been
301 * processed
302 */
cb2ce7ab
MC
303 s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
304 s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
36d16f8e 305
1fb9fdc3 306 return 1;
0f113f3e 307}
36d16f8e 308
1d97c843
TH
309/*-
310 * Return up to 'len' payload bytes received in 'type' records.
36d16f8e
BL
311 * 'type' is one of the following:
312 *
313 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
314 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
315 * - 0 (during a shutdown, no data has to be returned)
316 *
317 * If we don't have stored data to work from, read a SSL/TLS record first
318 * (possibly multiple records if we still don't have anything to return).
319 *
320 * This function must handle any surprises the peer may have for us, such as
c69f2adf
MC
321 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
322 * messages are treated as if they were handshake messages *if* the |recd_type|
323 * argument is non NULL.
36d16f8e
BL
324 * Also if record payloads contain fragments too small to process, we store
325 * them until there is enough for the respective protocol (the record protocol
326 * may use arbitrary fragmentation and even interleaving):
327 * Change cipher spec protocol
328 * just 1 byte needed, no need for keeping anything stored
329 * Alert protocol
330 * 2 bytes needed (AlertLevel, AlertDescription)
331 * Handshake protocol
332 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
333 * to detect unexpected Client Hello and Hello Request messages
334 * here, anything else is handled by higher layers
335 * Application data protocol
336 * none of our business
337 */
657da85e
MC
338int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
339 int len, int peek)
0f113f3e
MC
340{
341 int al, i, j, ret;
342 unsigned int n;
343 SSL3_RECORD *rr;
344 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
345
88c23039 346 if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
28d59af8 347 /* Not initialized yet */
0f113f3e
MC
348 if (!ssl3_setup_buffers(s))
349 return (-1);
28d59af8 350 }
0f113f3e
MC
351
352 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
353 (type != SSL3_RT_HANDSHAKE)) ||
354 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
355 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
356 return -1;
357 }
358
359 /*
360 * check whether there's a handshake message (client hello?) waiting
361 */
a773b52a 362 if ((ret = have_handshake_fragment(s, type, buf, len)))
0f113f3e
MC
363 return ret;
364
365 /*
c661ac16
MC
366 * Now s->rlayer.d->handshake_fragment_len == 0 if
367 * type == SSL3_RT_HANDSHAKE.
0f113f3e 368 */
36d16f8e 369
7e159e01 370#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
371 /*
372 * Continue handshake if it had to be interrupted to read app data with
373 * SCTP.
374 */
024f543c 375 if ((!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) ||
8723588e 376 (BIO_dgram_is_sctp(SSL_get_rbio(s))
fe3a3291 377 && ossl_statem_in_sctp_read_sock(s)
0f113f3e 378 && s->s3->in_read_app_data != 2))
7e159e01 379#else
024f543c 380 if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))
7e159e01 381#endif
0f113f3e
MC
382 {
383 /* type == SSL3_RT_APPLICATION_DATA */
384 i = s->handshake_func(s);
385 if (i < 0)
386 return (i);
387 if (i == 0) {
388 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
389 return (-1);
390 }
391 }
392
393 start:
394 s->rwstate = SSL_NOTHING;
395
50e735f9
MC
396 /*-
397 * s->s3->rrec.type - is the type of record
398 * s->s3->rrec.data, - data
399 * s->s3->rrec.off, - offset into 'data' for next read
400 * s->s3->rrec.length, - number of bytes.
401 */
94777c9c 402 rr = s->rlayer.rrec;
0f113f3e
MC
403
404 /*
405 * We are not handshaking and have no data yet, so process data buffered
406 * during the last handshake in advance, if any.
407 */
49ae7423 408 if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
0f113f3e 409 pitem *item;
24a1e2f2 410 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
0f113f3e 411 if (item) {
7e159e01 412#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
413 /* Restore bio_dgram_sctp_rcvinfo struct */
414 if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
415 DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
416 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
417 sizeof(rdata->recordinfo), &rdata->recordinfo);
418 }
7e159e01
DSH
419#endif
420
0f113f3e
MC
421 dtls1_copy_record(s, item);
422
423 OPENSSL_free(item->data);
424 pitem_free(item);
425 }
426 }
427
428 /* Check for timeout */
429 if (dtls1_handle_timeout(s) > 0)
430 goto start;
431
432 /* get new packet if necessary */
747e1639 433 if ((SSL3_RECORD_get_length(rr) == 0)
a230b26e 434 || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
0f113f3e
MC
435 ret = dtls1_get_record(s);
436 if (ret <= 0) {
437 ret = dtls1_read_failed(s, ret);
438 /* anything other than a timeout is an error */
439 if (ret <= 0)
440 return (ret);
441 else
442 goto start;
443 }
444 }
445
0f113f3e
MC
446 /* we now have a packet which can be read and processed */
447
448 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
449 * reset by ssl3_get_finished */
747e1639 450 && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
0f113f3e
MC
451 /*
452 * We now have application data between CCS and Finished. Most likely
453 * the packets were reordered on their way, so buffer the application
454 * data for later processing rather than dropping the connection.
455 */
24a1e2f2 456 if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
a230b26e 457 SSL3_RECORD_get_seq_num(rr)) < 0) {
0f113f3e
MC
458 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
459 return -1;
460 }
747e1639 461 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
462 goto start;
463 }
464
465 /*
466 * If the other end has shut down, throw anything we read away (even in
467 * 'peek' mode)
468 */
469 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
747e1639 470 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
471 s->rwstate = SSL_NOTHING;
472 return (0);
473 }
474
c69f2adf 475 if (type == SSL3_RECORD_get_type(rr)
a230b26e
EK
476 || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
477 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
c69f2adf
MC
478 /*
479 * SSL3_RT_APPLICATION_DATA or
480 * SSL3_RT_HANDSHAKE or
481 * SSL3_RT_CHANGE_CIPHER_SPEC
482 */
0f113f3e
MC
483 /*
484 * make sure that we are not getting application data when we are
485 * doing a handshake for the first time
486 */
487 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
488 (s->enc_read_ctx == NULL)) {
489 al = SSL_AD_UNEXPECTED_MESSAGE;
490 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
491 goto f_err;
492 }
7e159e01 493
c69f2adf
MC
494 if (recvd_type != NULL)
495 *recvd_type = SSL3_RECORD_get_type(rr);
496
0f113f3e
MC
497 if (len <= 0)
498 return (len);
499
747e1639
MC
500 if ((unsigned int)len > SSL3_RECORD_get_length(rr))
501 n = SSL3_RECORD_get_length(rr);
0f113f3e
MC
502 else
503 n = (unsigned int)len;
504
747e1639 505 memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
0f113f3e 506 if (!peek) {
753be41d 507 SSL3_RECORD_sub_length(rr, n);
747e1639
MC
508 SSL3_RECORD_add_off(rr, n);
509 if (SSL3_RECORD_get_length(rr) == 0) {
295c3f41 510 s->rlayer.rstate = SSL_ST_READ_HEADER;
747e1639 511 SSL3_RECORD_set_off(rr, 0);
0f113f3e
MC
512 }
513 }
7e159e01 514#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
515 /*
516 * We were about to renegotiate but had to read belated application
517 * data first, so retry.
518 */
519 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
747e1639 520 SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&
fe3a3291 521 ossl_statem_in_sctp_read_sock(s)) {
0f113f3e
MC
522 s->rwstate = SSL_READING;
523 BIO_clear_retry_flags(SSL_get_rbio(s));
524 BIO_set_retry_read(SSL_get_rbio(s));
525 }
526
527 /*
528 * We might had to delay a close_notify alert because of reordered
529 * app data. If there was an alert and there is no message to read
530 * anymore, finally set shutdown.
531 */
532 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
533 s->d1->shutdown_received
534 && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
535 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
536 return (0);
537 }
538#endif
539 return (n);
540 }
541
542 /*
543 * If we get here, then type != rr->type; if we have a handshake message,
544 * then it was unexpected (Hello Request or Client Hello).
545 */
546
547 /*
548 * In case of record types for which we have 'fragment' storage, fill
549 * that so that we can process the data at a fixed place.
550 */
551 {
552 unsigned int k, dest_maxlen = 0;
553 unsigned char *dest = NULL;
554 unsigned int *dest_len = NULL;
555
747e1639 556 if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
c661ac16
MC
557 dest_maxlen = sizeof s->rlayer.d->handshake_fragment;
558 dest = s->rlayer.d->handshake_fragment;
559 dest_len = &s->rlayer.d->handshake_fragment_len;
747e1639 560 } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
c661ac16
MC
561 dest_maxlen = sizeof(s->rlayer.d->alert_fragment);
562 dest = s->rlayer.d->alert_fragment;
563 dest_len = &s->rlayer.d->alert_fragment_len;
0f113f3e 564 }
4817504d 565#ifndef OPENSSL_NO_HEARTBEATS
22e3dcb7 566 else if (SSL3_RECORD_get_type(rr) == DTLS1_RT_HEARTBEAT) {
69f68237 567 /* We allow a 0 return */
61986d32 568 if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
a230b26e 569 SSL3_RECORD_get_length(rr)) < 0) {
69f68237
MC
570 return -1;
571 }
0f113f3e 572 /* Exit and notify application to read again */
747e1639 573 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
574 s->rwstate = SSL_READING;
575 BIO_clear_retry_flags(SSL_get_rbio(s));
576 BIO_set_retry_read(SSL_get_rbio(s));
577 return (-1);
578 }
4817504d 579#endif
0f113f3e 580 /* else it's a CCS message, or application data or wrong */
747e1639 581 else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {
0f113f3e
MC
582 /*
583 * Application data while renegotiating is allowed. Try again
584 * reading.
585 */
a230b26e 586 if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {
0f113f3e
MC
587 BIO *bio;
588 s->s3->in_read_app_data = 2;
589 bio = SSL_get_rbio(s);
590 s->rwstate = SSL_READING;
591 BIO_clear_retry_flags(bio);
592 BIO_set_retry_read(bio);
593 return (-1);
594 }
595
596 /* Not certain if this is the right error handling */
597 al = SSL_AD_UNEXPECTED_MESSAGE;
598 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
599 goto f_err;
600 }
601
602 if (dest_maxlen > 0) {
603 /*
8483a003 604 * XDTLS: In a pathological case, the Client Hello may be
0f113f3e
MC
605 * fragmented--don't always expect dest_maxlen bytes
606 */
a230b26e 607 if (SSL3_RECORD_get_length(rr) < dest_maxlen) {
d4938995 608#ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
0f113f3e
MC
609 /*
610 * for normal alerts rr->length is 2, while
611 * dest_maxlen is 7 if we were to handle this
612 * non-existing alert...
613 */
a230b26e 614 FIX ME;
d4938995 615#endif
747e1639
MC
616 s->rlayer.rstate = SSL_ST_READ_HEADER;
617 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
618 goto start;
619 }
620
621 /* now move 'n' bytes: */
622 for (k = 0; k < dest_maxlen; k++) {
747e1639
MC
623 dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
624 SSL3_RECORD_add_off(rr, 1);
625 SSL3_RECORD_add_length(rr, -1);
0f113f3e
MC
626 }
627 *dest_len = dest_maxlen;
628 }
629 }
630
35a1cc90 631 /*-
c661ac16
MC
632 * s->rlayer.d->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
633 * s->rlayer.d->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
35a1cc90
MC
634 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
635 */
0f113f3e
MC
636
637 /* If we are a client, check for an incoming 'Hello Request': */
638 if ((!s->server) &&
c661ac16
MC
639 (s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
640 (s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
0f113f3e 641 (s->session != NULL) && (s->session->cipher != NULL)) {
c661ac16 642 s->rlayer.d->handshake_fragment_len = 0;
0f113f3e 643
c661ac16
MC
644 if ((s->rlayer.d->handshake_fragment[1] != 0) ||
645 (s->rlayer.d->handshake_fragment[2] != 0) ||
646 (s->rlayer.d->handshake_fragment[3] != 0)) {
0f113f3e
MC
647 al = SSL_AD_DECODE_ERROR;
648 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
4dc1aa04 649 goto f_err;
0f113f3e
MC
650 }
651
652 /*
653 * no need to check sequence number on HELLO REQUEST messages
654 */
655
656 if (s->msg_callback)
657 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
c661ac16 658 s->rlayer.d->handshake_fragment, 4, s,
0f113f3e
MC
659 s->msg_callback_arg);
660
661 if (SSL_is_init_finished(s) &&
662 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
663 !s->s3->renegotiate) {
664 s->d1->handshake_read_seq++;
665 s->new_session = 1;
666 ssl3_renegotiate(s);
667 if (ssl3_renegotiate_check(s)) {
668 i = s->handshake_func(s);
669 if (i < 0)
670 return (i);
671 if (i == 0) {
a230b26e 672 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
0f113f3e
MC
673 return (-1);
674 }
675
676 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
88c23039 677 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
28d59af8 678 /* no read-ahead left? */
0f113f3e
MC
679 BIO *bio;
680 /*
681 * In the case where we try to read application data,
682 * but we trigger an SSL handshake, we return -1 with
683 * the retry option set. Otherwise renegotiation may
684 * cause nasty problems in the blocking world
685 */
686 s->rwstate = SSL_READING;
687 bio = SSL_get_rbio(s);
688 BIO_clear_retry_flags(bio);
689 BIO_set_retry_read(bio);
690 return (-1);
691 }
692 }
693 }
694 }
695 /*
696 * we either finished a handshake or ignored the request, now try
697 * again to obtain the (application) data we were asked for
698 */
699 goto start;
700 }
701
c661ac16
MC
702 if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
703 int alert_level = s->rlayer.d->alert_fragment[0];
704 int alert_descr = s->rlayer.d->alert_fragment[1];
0f113f3e 705
c661ac16 706 s->rlayer.d->alert_fragment_len = 0;
0f113f3e
MC
707
708 if (s->msg_callback)
709 s->msg_callback(0, s->version, SSL3_RT_ALERT,
c661ac16
MC
710 s->rlayer.d->alert_fragment, 2, s,
711 s->msg_callback_arg);
0f113f3e
MC
712
713 if (s->info_callback != NULL)
714 cb = s->info_callback;
715 else if (s->ctx->info_callback != NULL)
716 cb = s->ctx->info_callback;
717
718 if (cb != NULL) {
719 j = (alert_level << 8) | alert_descr;
720 cb(s, SSL_CB_READ_ALERT, j);
721 }
722
fd865cad 723 if (alert_level == SSL3_AL_WARNING) {
0f113f3e
MC
724 s->s3->warn_alert = alert_descr;
725 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
7e159e01 726#ifndef OPENSSL_NO_SCTP
0f113f3e
MC
727 /*
728 * With SCTP and streams the socket may deliver app data
729 * after a close_notify alert. We have to check this first so
730 * that nothing gets discarded.
731 */
732 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
733 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
734 s->d1->shutdown_received = 1;
735 s->rwstate = SSL_READING;
736 BIO_clear_retry_flags(SSL_get_rbio(s));
737 BIO_set_retry_read(SSL_get_rbio(s));
738 return -1;
739 }
7e159e01 740#endif
0f113f3e
MC
741 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
742 return (0);
743 }
36d16f8e
BL
744#if 0
745 /* XXX: this is a possible improvement in the future */
0f113f3e
MC
746 /* now check if it's a missing record */
747 if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
748 unsigned short seq;
749 unsigned int frag_off;
c661ac16 750 unsigned char *p = &(s->rlayer.d->alert_fragment[2]);
0f113f3e
MC
751
752 n2s(p, seq);
753 n2l3(p, frag_off);
754
755 dtls1_retransmit_message(s,
756 dtls1_get_queue_priority
757 (frag->msg_header.seq, 0), frag_off,
758 &found);
759 if (!found && SSL_in_init(s)) {
760 /*
761 * fprintf( stderr,"in init = %d\n", SSL_in_init(s));
762 */
763 /*
764 * requested a message not yet sent, send an alert
765 * ourselves
766 */
767 ssl3_send_alert(s, SSL3_AL_WARNING,
768 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
769 }
770 }
36d16f8e 771#endif
fd865cad 772 } else if (alert_level == SSL3_AL_FATAL) {
0f113f3e
MC
773 char tmp[16];
774
775 s->rwstate = SSL_NOTHING;
776 s->s3->fatal_alert = alert_descr;
a230b26e 777 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
0f113f3e
MC
778 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
779 ERR_add_error_data(2, "SSL alert number ", tmp);
780 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
e2bb9b9b 781 SSL_CTX_remove_session(s->session_ctx, s->session);
0f113f3e
MC
782 return (0);
783 } else {
784 al = SSL_AD_ILLEGAL_PARAMETER;
785 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
786 goto f_err;
787 }
788
789 goto start;
790 }
791
792 if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
793 * shutdown */
794 s->rwstate = SSL_NOTHING;
747e1639 795 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
796 return (0);
797 }
798
747e1639 799 if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
0f113f3e
MC
800 /*
801 * We can't process a CCS now, because previous handshake messages
802 * are still missing, so just drop it.
803 */
c69f2adf 804 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
805 goto start;
806 }
807
808 /*
809 * Unexpected handshake message (Client Hello, or protocol violation)
810 */
c661ac16 811 if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
024f543c 812 !ossl_statem_get_in_handshake(s)) {
0f113f3e
MC
813 struct hm_header_st msg_hdr;
814
815 /* this may just be a stale retransmit */
816 dtls1_get_message_header(rr->data, &msg_hdr);
747e1639
MC
817 if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {
818 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
819 goto start;
820 }
821
822 /*
823 * If we are server, we may have a repeated FINISHED of the client
824 * here, then retransmit our CCS and FINISHED.
825 */
826 if (msg_hdr.type == SSL3_MT_FINISHED) {
827 if (dtls1_check_timeout_num(s) < 0)
828 return -1;
829
17dd65e6 830 dtls1_retransmit_buffered_messages(s);
747e1639 831 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
832 goto start;
833 }
834
49ae7423 835 if (SSL_is_init_finished(s) &&
0f113f3e 836 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
fe3a3291 837 ossl_statem_set_in_init(s, 1);
0f113f3e
MC
838 s->renegotiate = 1;
839 s->new_session = 1;
840 }
841 i = s->handshake_func(s);
842 if (i < 0)
843 return (i);
844 if (i == 0) {
845 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
846 return (-1);
847 }
848
849 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
88c23039 850 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
28d59af8 851 /* no read-ahead left? */
0f113f3e
MC
852 BIO *bio;
853 /*
854 * In the case where we try to read application data, but we
855 * trigger an SSL handshake, we return -1 with the retry
856 * option set. Otherwise renegotiation may cause nasty
857 * problems in the blocking world
858 */
859 s->rwstate = SSL_READING;
860 bio = SSL_get_rbio(s);
861 BIO_clear_retry_flags(bio);
862 BIO_set_retry_read(bio);
863 return (-1);
864 }
865 }
866 goto start;
867 }
868
747e1639 869 switch (SSL3_RECORD_get_type(rr)) {
0f113f3e 870 default:
0f113f3e
MC
871 /* TLS just ignores unknown message types */
872 if (s->version == TLS1_VERSION) {
747e1639 873 SSL3_RECORD_set_length(rr, 0);
0f113f3e
MC
874 goto start;
875 }
0f113f3e
MC
876 al = SSL_AD_UNEXPECTED_MESSAGE;
877 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
878 goto f_err;
879 case SSL3_RT_CHANGE_CIPHER_SPEC:
880 case SSL3_RT_ALERT:
881 case SSL3_RT_HANDSHAKE:
882 /*
883 * we already handled all of these, with the possible exception of
024f543c
MC
884 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
885 * that should not happen when type != rr->type
0f113f3e
MC
886 */
887 al = SSL_AD_UNEXPECTED_MESSAGE;
888 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
889 goto f_err;
890 case SSL3_RT_APPLICATION_DATA:
891 /*
892 * At this point, we were expecting handshake data, but have
893 * application data. If the library was running inside ssl3_read()
894 * (i.e. in_read_app_data is set) and it makes sense to read
895 * application data at this point (session renegotiation not yet
896 * started), we will indulge it.
897 */
898 if (s->s3->in_read_app_data &&
899 (s->s3->total_renegotiations != 0) &&
fe3a3291 900 ossl_statem_app_data_allowed(s)) {
0f113f3e
MC
901 s->s3->in_read_app_data = 2;
902 return (-1);
903 } else {
904 al = SSL_AD_UNEXPECTED_MESSAGE;
905 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
906 goto f_err;
907 }
908 }
909 /* not reached */
910
911 f_err:
912 ssl3_send_alert(s, SSL3_AL_FATAL, al);
0f113f3e
MC
913 return (-1);
914}
915
0f113f3e
MC
916 /*
917 * this only happens when a client hello is received and a handshake
918 * is started.
919 */
6f7ae319 920static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
a773b52a 921 int len)
0f113f3e
MC
922{
923
c661ac16 924 if ((type == SSL3_RT_HANDSHAKE)
a230b26e 925 && (s->rlayer.d->handshake_fragment_len > 0))
0f113f3e
MC
926 /* (partially) satisfy request from storage */
927 {
c661ac16 928 unsigned char *src = s->rlayer.d->handshake_fragment;
0f113f3e
MC
929 unsigned char *dst = buf;
930 unsigned int k, n;
931
932 /* peek == 0 */
933 n = 0;
c661ac16 934 while ((len > 0) && (s->rlayer.d->handshake_fragment_len > 0)) {
0f113f3e
MC
935 *dst++ = *src++;
936 len--;
c661ac16 937 s->rlayer.d->handshake_fragment_len--;
0f113f3e
MC
938 n++;
939 }
940 /* move any remaining fragment bytes: */
c661ac16
MC
941 for (k = 0; k < s->rlayer.d->handshake_fragment_len; k++)
942 s->rlayer.d->handshake_fragment[k] = *src++;
0f113f3e
MC
943 return n;
944 }
945
946 return 0;
947}
948
949/*
950 * Call this to write data in records of type 'type' It will return <= 0 if
951 * not all data has been sent or non-blocking IO.
36d16f8e 952 */
480b9e5d 953int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
0f113f3e
MC
954{
955 int i;
956
957 OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
958 s->rwstate = SSL_NOTHING;
959 i = do_dtls1_write(s, type, buf, len, 0);
960 return i;
961}
962
963int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
964 unsigned int len, int create_empty_fragment)
965{
966 unsigned char *p, *pseq;
967 int i, mac_size, clear = 0;
968 int prefix_len = 0;
969 int eivlen;
f482740f 970 SSL3_RECORD wr;
0f113f3e
MC
971 SSL3_BUFFER *wb;
972 SSL_SESSION *sess;
973
d102d9df 974 wb = &s->rlayer.wbuf[0];
db9a32e7 975
0f113f3e
MC
976 /*
977 * first check if there is a SSL3_BUFFER still being written out. This
978 * will happen with non blocking IO
979 */
db9a32e7 980 if (SSL3_BUFFER_get_left(wb) != 0) {
0f113f3e
MC
981 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
982 return (ssl3_write_pending(s, type, buf, len));
983 }
984
985 /* If we have an alert to send, lets send it */
986 if (s->s3->alert_dispatch) {
987 i = s->method->ssl_dispatch_alert(s);
988 if (i <= 0)
989 return (i);
990 /* if it went, fall through and send more stuff */
991 }
992
993 if (len == 0 && !create_empty_fragment)
994 return 0;
995
0f113f3e
MC
996 sess = s->session;
997
998 if ((sess == NULL) ||
999 (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
1000 clear = 1;
1001
1002 if (clear)
1003 mac_size = 0;
1004 else {
1005 mac_size = EVP_MD_CTX_size(s->write_hash);
1006 if (mac_size < 0)
1007 goto err;
1008 }
1009
747e1639 1010 p = SSL3_BUFFER_get_buf(wb) + prefix_len;
0f113f3e
MC
1011
1012 /* write the header */
1013
1014 *(p++) = type & 0xff;
f482740f 1015 SSL3_RECORD_set_type(&wr, type);
0f113f3e
MC
1016 /*
1017 * Special case: for hello verify request, client version 1.0 and we
1018 * haven't decided which version to use yet send back using version 1.0
1019 * header: otherwise some clients will ignore it.
1020 */
032924c4 1021 if (s->method->version == DTLS_ANY_VERSION &&
a230b26e 1022 s->max_proto_version != DTLS1_BAD_VER) {
0f113f3e
MC
1023 *(p++) = DTLS1_VERSION >> 8;
1024 *(p++) = DTLS1_VERSION & 0xff;
1025 } else {
1026 *(p++) = s->version >> 8;
1027 *(p++) = s->version & 0xff;
1028 }
1029
1030 /* field where we are to write out packet epoch, seq num and len */
1031 pseq = p;
1032 p += 10;
1033
1034 /* Explicit IV length, block ciphers appropriate version flag */
1035 if (s->enc_write_ctx) {
1036 int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
1037 if (mode == EVP_CIPH_CBC_MODE) {
1038 eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
1039 if (eivlen <= 1)
1040 eivlen = 0;
1041 }
1042 /* Need explicit part of IV for GCM mode */
1043 else if (mode == EVP_CIPH_GCM_MODE)
1044 eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
e75c5a79
DSH
1045 else if (mode == EVP_CIPH_CCM_MODE)
1046 eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
0f113f3e
MC
1047 else
1048 eivlen = 0;
1049 } else
1050 eivlen = 0;
1051
1052 /* lets setup the record stuff. */
f482740f
MC
1053 SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
1054 SSL3_RECORD_set_length(&wr, (int)len);
1055 SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
0f113f3e
MC
1056
1057 /*
f482740f 1058 * we now 'read' from wr.input, wr.length bytes into wr.data
0f113f3e
MC
1059 */
1060
1061 /* first we compress */
1062 if (s->compress != NULL) {
f482740f 1063 if (!ssl3_do_compress(s, &wr)) {
0f113f3e
MC
1064 SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
1065 goto err;
1066 }
1067 } else {
f482740f
MC
1068 memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
1069 SSL3_RECORD_get_length(&wr));
1070 SSL3_RECORD_reset_input(&wr);
0f113f3e 1071 }
36d16f8e 1072
0f113f3e 1073 /*
f482740f
MC
1074 * we should still have the output to wr.data and the input from
1075 * wr.input. Length should be wr.length. wr.data still points in the
0f113f3e
MC
1076 * wb->buf
1077 */
36d16f8e 1078
0f113f3e 1079 if (mac_size != 0) {
f482740f 1080 if (s->method->ssl3_enc->mac(s, &wr,
a230b26e
EK
1081 &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
1082 1) < 0)
0f113f3e 1083 goto err;
f482740f 1084 SSL3_RECORD_add_length(&wr, mac_size);
0f113f3e 1085 }
36d16f8e 1086
0f113f3e 1087 /* this is true regardless of mac size */
f482740f
MC
1088 SSL3_RECORD_set_data(&wr, p);
1089 SSL3_RECORD_reset_input(&wr);
36d16f8e 1090
0f113f3e 1091 if (eivlen)
f482740f 1092 SSL3_RECORD_add_length(&wr, eivlen);
36d16f8e 1093
f482740f 1094 if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)
0f113f3e 1095 goto err;
36d16f8e 1096
0f113f3e
MC
1097 /* record length after mac and block padding */
1098 /*
1099 * if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
1100 * SSL_in_init(s)))
1101 */
36d16f8e 1102
0f113f3e 1103 /* there's only one epoch between handshake and app data */
36d16f8e 1104
78a39fe7 1105 s2n(s->rlayer.d->w_epoch, pseq);
36d16f8e 1106
0f113f3e
MC
1107 /* XDTLS: ?? */
1108 /*
1109 * else s2n(s->d1->handshake_epoch, pseq);
1110 */
36d16f8e 1111
de07f311 1112 memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
0f113f3e 1113 pseq += 6;
f482740f 1114 s2n(SSL3_RECORD_get_length(&wr), pseq);
36d16f8e 1115
0f113f3e
MC
1116 if (s->msg_callback)
1117 s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
1118 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
36d16f8e 1119
0f113f3e 1120 /*
f482740f 1121 * we should now have wr.data pointing to the encrypted data, which is
0f113f3e
MC
1122 * wr->length long
1123 */
f482740f
MC
1124 SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
1125 SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
36d16f8e 1126
de07f311 1127 ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
36d16f8e 1128
0f113f3e
MC
1129 if (create_empty_fragment) {
1130 /*
1131 * we are in a recursive call; just return the length, don't write
1132 * out anything here
1133 */
f482740f 1134 return wr.length;
0f113f3e 1135 }
36d16f8e 1136
0f113f3e 1137 /* now let's set up wb */
f482740f 1138 SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
747e1639 1139 SSL3_BUFFER_set_offset(wb, 0);
0f113f3e
MC
1140
1141 /*
1142 * memorize arguments so that ssl3_write_pending can detect bad write
1143 * retries later
1144 */
f8caa3c8
MC
1145 s->rlayer.wpend_tot = len;
1146 s->rlayer.wpend_buf = buf;
1147 s->rlayer.wpend_type = type;
1148 s->rlayer.wpend_ret = len;
0f113f3e
MC
1149
1150 /* we now just need to write the buffer */
1151 return ssl3_write_pending(s, type, buf, len);
1152 err:
1153 return -1;
1154}
36d16f8e 1155
fe589e61 1156DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
a230b26e 1157 unsigned int *is_next_epoch)
0f113f3e
MC
1158{
1159
36d16f8e
BL
1160 *is_next_epoch = 0;
1161
1162 /* In current epoch, accept HM, CCS, DATA, & ALERT */
78a39fe7 1163 if (rr->epoch == s->rlayer.d->r_epoch)
91f93f69 1164 return &s->rlayer.d->bitmap;
36d16f8e 1165
738ad946
MC
1166 /*
1167 * Only HM and ALERT messages can be from the next epoch and only if we
1168 * have already processed all of the unprocessed records from the last
1169 * epoch
1170 */
78a39fe7 1171 else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
738ad946 1172 s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
a230b26e 1173 (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
36d16f8e 1174 *is_next_epoch = 1;
91f93f69 1175 return &s->rlayer.d->next_bitmap;
0f113f3e 1176 }
36d16f8e
BL
1177
1178 return NULL;
0f113f3e 1179}
36d16f8e 1180
0f113f3e
MC
1181void dtls1_reset_seq_numbers(SSL *s, int rw)
1182{
1183 unsigned char *seq;
de07f311 1184 unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
0f113f3e
MC
1185
1186 if (rw & SSL3_CC_READ) {
de07f311 1187 seq = s->rlayer.read_sequence;
78a39fe7 1188 s->rlayer.d->r_epoch++;
16f8d4eb
RS
1189 memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
1190 sizeof(s->rlayer.d->bitmap));
a230b26e 1191 memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
0f113f3e 1192 } else {
de07f311 1193 seq = s->rlayer.write_sequence;
3bb8f87d 1194 memcpy(s->rlayer.d->last_write_sequence, seq,
de07f311 1195 sizeof(s->rlayer.write_sequence));
78a39fe7 1196 s->rlayer.d->w_epoch++;
0f113f3e
MC
1197 }
1198
16f8d4eb 1199 memset(seq, 0, seq_bytes);
0f113f3e 1200}