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