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