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