2 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "../ssl_local.h"
13 #include <openssl/evp.h>
14 #include <openssl/buffer.h>
15 #include "record_local.h"
16 #include "internal/packet.h"
17 #include "internal/cryptlib.h"
19 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
23 if ((d = OPENSSL_malloc(sizeof(*d))) == NULL) {
24 ERR_raise(ERR_LIB_SSL, ERR_R_MALLOC_FAILURE);
30 d->buffered_app_data.q = pqueue_new();
32 if (d->buffered_app_data.q == NULL) {
41 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
46 DTLS_RECORD_LAYER_clear(rl);
47 pqueue_free(rl->d->buffered_app_data.q);
52 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
57 pqueue *buffered_app_data;
61 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
62 rec = (TLS_RECORD *)item->data;
63 if (rl->s->options & SSL_OP_CLEANSE_PLAINTEXT)
64 OPENSSL_cleanse(rec->data, rec->length);
65 OPENSSL_free(rec->data);
66 OPENSSL_free(item->data);
70 buffered_app_data = d->buffered_app_data.q;
71 memset(d, 0, sizeof(*d));
72 d->buffered_app_data.q = buffered_app_data;
75 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
77 if (e == rl->d->w_epoch - 1) {
78 memcpy(rl->d->curr_write_sequence,
79 rl->write_sequence, sizeof(rl->write_sequence));
80 memcpy(rl->write_sequence,
81 rl->d->last_write_sequence, sizeof(rl->write_sequence));
82 } else if (e == rl->d->w_epoch + 1) {
83 memcpy(rl->d->last_write_sequence,
84 rl->write_sequence, sizeof(unsigned char[8]));
85 memcpy(rl->write_sequence,
86 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
91 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
93 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
96 int dtls_buffer_record(SSL_CONNECTION *s, TLS_RECORD *rec)
100 record_pqueue *queue = &(s->rlayer.d->buffered_app_data);
102 /* Limit the size of the queue to prevent DOS attacks */
103 if (pqueue_size(queue->q) >= 100)
106 /* We don't buffer partially read records */
107 if (!ossl_assert(rec->off == 0))
110 rdata = OPENSSL_malloc(sizeof(*rdata));
111 item = pitem_new(rec->seq_num, rdata);
112 if (rdata == NULL || item == NULL) {
115 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
121 * We will release the record from the record layer soon, so we take a copy
122 * now. Copying data isn't good - but this should be infrequent so we
125 rdata->data = OPENSSL_memdup(rec->data, rec->length);
126 if (rdata->data == NULL) {
129 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
133 * We use a NULL rechandle to indicate that the data field has been
136 rdata->rechandle = NULL;
140 #ifndef OPENSSL_NO_SCTP
141 /* Store bio_dgram_sctp_rcvinfo struct */
142 if (BIO_dgram_is_sctp(SSL_get_rbio(ssl)) &&
143 (SSL_get_state(ssl) == TLS_ST_SR_FINISHED
144 || SSL_get_state(ssl) == TLS_ST_CR_FINISHED)) {
145 BIO_ctrl(SSL_get_rbio(ssl), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
146 sizeof(rdata->recordinfo), &rdata->recordinfo);
150 if (pqueue_insert(queue->q, item) == NULL) {
151 /* Must be a duplicate so ignore it */
152 OPENSSL_free(rdata->data);
160 /* Unbuffer a previously buffered TLS_RECORD structure if any */
161 static void dtls_unbuffer_record(SSL_CONNECTION *s)
166 /* If we already have records to handle then do nothing */
167 if (s->rlayer.curr_rec < s->rlayer.num_recs)
170 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
172 rdata = (TLS_RECORD *)item->data;
174 s->rlayer.tlsrecs[0] = *rdata;
175 s->rlayer.num_recs = 1;
176 s->rlayer.curr_rec = 0;
178 #ifndef OPENSSL_NO_SCTP
179 /* Restore bio_dgram_sctp_rcvinfo struct */
180 if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
181 DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
182 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
183 sizeof(rdata->recordinfo), &rdata->recordinfo);
187 /* Set proper sequence number for mac calculation */
188 memcpy(&(s->rlayer.read_sequence[2]), &(rdata->seq_num[2]), 6);
190 OPENSSL_free(item->data);
196 * Return up to 'len' payload bytes received in 'type' records.
197 * 'type' is one of the following:
199 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
200 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
201 * - 0 (during a shutdown, no data has to be returned)
203 * If we don't have stored data to work from, read a SSL/TLS record first
204 * (possibly multiple records if we still don't have anything to return).
206 * This function must handle any surprises the peer may have for us, such as
207 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
208 * messages are treated as if they were handshake messages *if* the |recd_type|
209 * argument is non NULL.
210 * Also if record payloads contain fragments too small to process, we store
211 * them until there is enough for the respective protocol (the record protocol
212 * may use arbitrary fragmentation and even interleaving):
213 * Change cipher spec protocol
214 * just 1 byte needed, no need for keeping anything stored
216 * 2 bytes needed (AlertLevel, AlertDescription)
218 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
219 * to detect unexpected Client Hello and Hello Request messages
220 * here, anything else is handled by higher layers
221 * Application data protocol
222 * none of our business
224 int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
225 size_t len, int peek, size_t *readbytes)
230 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
231 SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
236 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
237 (type != SSL3_RT_HANDSHAKE)) ||
238 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
239 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
243 if (!ossl_statem_get_in_handshake(sc) && SSL_in_init(s)) {
244 /* type == SSL3_RT_APPLICATION_DATA */
245 i = sc->handshake_func(s);
246 /* SSLfatal() already called if appropriate */
254 sc->rwstate = SSL_NOTHING;
257 * We are not handshaking and have no data yet, so process data buffered
258 * during the last handshake in advance, if any.
260 if (SSL_is_init_finished(s))
261 dtls_unbuffer_record(sc);
263 /* Check for timeout */
264 if (dtls1_handle_timeout(sc) > 0) {
266 } else if (ossl_statem_in_error(sc)) {
267 /* dtls1_handle_timeout() has failed with a fatal error */
271 /* get new packet if necessary */
272 if (sc->rlayer.curr_rec >= sc->rlayer.num_recs) {
273 sc->rlayer.curr_rec = sc->rlayer.num_recs = 0;
275 rr = &sc->rlayer.tlsrecs[sc->rlayer.num_recs];
277 ret = HANDLE_RLAYER_RETURN(sc,
278 sc->rlayer.rrlmethod->read_record(sc->rlayer.rrl,
280 &rr->version, &rr->type,
281 &rr->data, &rr->length,
282 &rr->epoch, rr->seq_num));
284 ret = dtls1_read_failed(sc, ret);
286 * Anything other than a timeout is an error. SSLfatal() already
287 * called if appropriate.
295 sc->rlayer.num_recs++;
296 } while (sc->rlayer.rrlmethod->processed_read_pending(sc->rlayer.rrl)
297 && sc->rlayer.num_recs < SSL_MAX_PIPELINES);
299 rr = &sc->rlayer.tlsrecs[sc->rlayer.curr_rec];
302 * Reset the count of consecutive warning alerts if we've got a non-empty
303 * record that isn't an alert.
305 if (rr->type != SSL3_RT_ALERT && rr->length != 0)
306 sc->rlayer.alert_count = 0;
308 /* we now have a packet which can be read and processed */
310 if (sc->s3.change_cipher_spec /* set when we receive ChangeCipherSpec,
311 * reset by ssl3_get_finished */
312 && (rr->type != SSL3_RT_HANDSHAKE)) {
314 * We now have application data between CCS and Finished. Most likely
315 * the packets were reordered on their way, so buffer the application
316 * data for later processing rather than dropping the connection.
318 if (dtls_buffer_record(sc, rr) < 0) {
319 /* SSLfatal() already called */
322 ssl_release_record(sc, rr);
327 * If the other end has shut down, throw anything we read away (even in
330 if (sc->shutdown & SSL_RECEIVED_SHUTDOWN) {
331 ssl_release_record(sc, rr);
332 sc->rwstate = SSL_NOTHING;
337 || (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC
338 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
340 * SSL3_RT_APPLICATION_DATA or
341 * SSL3_RT_HANDSHAKE or
342 * SSL3_RT_CHANGE_CIPHER_SPEC
345 * make sure that we are not getting application data when we are
346 * doing a handshake for the first time
348 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
349 (sc->enc_read_ctx == NULL)) {
350 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
351 SSL_R_APP_DATA_IN_HANDSHAKE);
355 if (recvd_type != NULL)
356 *recvd_type = rr->type;
360 * Release a zero length record. This ensures multiple calls to
361 * SSL_read() with a zero length buffer will eventually cause
362 * SSL_pending() to report data as being available.
365 ssl_release_record(sc, rr);
369 if (len > rr->length)
374 memcpy(buf, &(rr->data[rr->off]), n);
377 ssl_release_record(sc, rr);
379 if (sc->options & SSL_OP_CLEANSE_PLAINTEXT)
380 OPENSSL_cleanse(&(rr->data[rr->off]), n);
384 ssl_release_record(sc, rr);
386 #ifndef OPENSSL_NO_SCTP
388 * We might had to delay a close_notify alert because of reordered
389 * app data. If there was an alert and there is no message to read
390 * anymore, finally set shutdown.
392 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
393 sc->d1->shutdown_received
394 && BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) <= 0) {
395 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
404 * If we get here, then type != rr->type; if we have a handshake message,
405 * then it was unexpected (Hello Request or Client Hello).
408 if (rr->type == SSL3_RT_ALERT) {
409 unsigned int alert_level, alert_descr;
410 unsigned char *alert_bytes = rr->data + rr->off;
413 if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
414 || !PACKET_get_1(&alert, &alert_level)
415 || !PACKET_get_1(&alert, &alert_descr)
416 || PACKET_remaining(&alert) != 0) {
417 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_INVALID_ALERT);
421 if (sc->msg_callback)
422 sc->msg_callback(0, sc->version, SSL3_RT_ALERT, alert_bytes, 2, s,
423 sc->msg_callback_arg);
425 if (sc->info_callback != NULL)
426 cb = sc->info_callback;
427 else if (s->ctx->info_callback != NULL)
428 cb = s->ctx->info_callback;
431 j = (alert_level << 8) | alert_descr;
432 cb(s, SSL_CB_READ_ALERT, j);
435 if (alert_level == SSL3_AL_WARNING) {
436 sc->s3.warn_alert = alert_descr;
437 ssl_release_record(sc, rr);
439 sc->rlayer.alert_count++;
440 if (sc->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
441 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE,
442 SSL_R_TOO_MANY_WARN_ALERTS);
446 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
447 #ifndef OPENSSL_NO_SCTP
449 * With SCTP and streams the socket may deliver app data
450 * after a close_notify alert. We have to check this first so
451 * that nothing gets discarded.
453 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
454 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s)) > 0) {
455 sc->d1->shutdown_received = 1;
456 sc->rwstate = SSL_READING;
457 BIO_clear_retry_flags(SSL_get_rbio(s));
458 BIO_set_retry_read(SSL_get_rbio(s));
462 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
465 } else if (alert_level == SSL3_AL_FATAL) {
466 sc->rwstate = SSL_NOTHING;
467 sc->s3.fatal_alert = alert_descr;
468 SSLfatal_data(sc, SSL_AD_NO_ALERT,
469 SSL_AD_REASON_OFFSET + alert_descr,
470 "SSL alert number %d", alert_descr);
471 sc->shutdown |= SSL_RECEIVED_SHUTDOWN;
472 ssl_release_record(sc, rr);
473 SSL_CTX_remove_session(sc->session_ctx, sc->session);
476 SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_ALERT_TYPE);
483 if (sc->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
485 sc->rwstate = SSL_NOTHING;
486 ssl_release_record(sc, rr);
490 if (rr->type == SSL3_RT_CHANGE_CIPHER_SPEC) {
492 * We can't process a CCS now, because previous handshake messages
493 * are still missing, so just drop it.
495 ssl_release_record(sc, rr);
500 * Unexpected handshake message (Client Hello, or protocol violation)
502 if (rr->type == SSL3_RT_HANDSHAKE && !ossl_statem_get_in_handshake(sc)) {
503 struct hm_header_st msg_hdr;
506 * This may just be a stale retransmit. Also sanity check that we have
507 * at least enough record bytes for a message header
509 if (rr->epoch != sc->rlayer.d->r_epoch
510 || rr->length < DTLS1_HM_HEADER_LENGTH) {
511 ssl_release_record(sc, rr);
515 dtls1_get_message_header(rr->data, &msg_hdr);
518 * If we are server, we may have a repeated FINISHED of the client
519 * here, then retransmit our CCS and FINISHED.
521 if (msg_hdr.type == SSL3_MT_FINISHED) {
522 if (dtls1_check_timeout_num(sc) < 0) {
523 /* SSLfatal) already called */
527 if (dtls1_retransmit_buffered_messages(sc) <= 0) {
528 /* Fail if we encountered a fatal error */
529 if (ossl_statem_in_error(sc))
532 ssl_release_record(sc, rr);
533 if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
534 if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
535 /* no read-ahead left? */
538 sc->rwstate = SSL_READING;
539 bio = SSL_get_rbio(s);
540 BIO_clear_retry_flags(bio);
541 BIO_set_retry_read(bio);
549 * To get here we must be trying to read app data but found handshake
550 * data. But if we're trying to read app data, and we're not in init
551 * (which is tested for at the top of this function) then init must be
554 if (!ossl_assert(SSL_is_init_finished(s))) {
555 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
559 /* We found handshake data, so we're going back into init */
560 ossl_statem_set_in_init(sc, 1);
562 i = sc->handshake_func(s);
563 /* SSLfatal() called if appropriate */
569 if (!(sc->mode & SSL_MODE_AUTO_RETRY)) {
570 if (!sc->rlayer.rrlmethod->unprocessed_read_pending(sc->rlayer.rrl)) {
571 /* no read-ahead left? */
574 * In the case where we try to read application data, but we
575 * trigger an SSL handshake, we return -1 with the retry
576 * option set. Otherwise renegotiation may cause nasty
577 * problems in the blocking world
579 sc->rwstate = SSL_READING;
580 bio = SSL_get_rbio(s);
581 BIO_clear_retry_flags(bio);
582 BIO_set_retry_read(bio);
591 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
593 case SSL3_RT_CHANGE_CIPHER_SPEC:
595 case SSL3_RT_HANDSHAKE:
597 * we already handled all of these, with the possible exception of
598 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
599 * that should not happen when type != rr->type
601 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, ERR_R_INTERNAL_ERROR);
603 case SSL3_RT_APPLICATION_DATA:
605 * At this point, we were expecting handshake data, but have
606 * application data. If the library was running inside ssl3_read()
607 * (i.e. in_read_app_data is set) and it makes sense to read
608 * application data at this point (session renegotiation not yet
609 * started), we will indulge it.
611 if (sc->s3.in_read_app_data &&
612 (sc->s3.total_renegotiations != 0) &&
613 ossl_statem_app_data_allowed(sc)) {
614 sc->s3.in_read_app_data = 2;
617 SSLfatal(sc, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
625 * Call this to write data in records of type 'type' It will return <= 0 if
626 * not all data has been sent or non-blocking IO.
628 int dtls1_write_bytes(SSL_CONNECTION *s, int type, const void *buf,
629 size_t len, size_t *written)
633 if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
634 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
637 s->rwstate = SSL_NOTHING;
638 i = do_dtls1_write(s, type, buf, len, 0, written);
642 int do_dtls1_write(SSL_CONNECTION *sc, int type, const unsigned char *buf,
643 size_t len, int create_empty_fragment, size_t *written)
645 unsigned char *p, *pseq;
646 int i, mac_size, clear = 0;
647 size_t prefix_len = 0;
652 SSL *s = SSL_CONNECTION_GET_SSL(sc);
654 wb = &sc->rlayer.wbuf[0];
657 * DTLS writes whole datagrams, so there can't be anything left in
660 if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
661 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
665 /* If we have an alert to send, lets send it */
666 if (sc->s3.alert_dispatch) {
667 i = s->method->ssl_dispatch_alert(s);
670 /* if it went, fall through and send more stuff */
673 if (len == 0 && !create_empty_fragment)
676 if (len > ssl_get_max_send_fragment(sc)) {
677 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
684 || (sc->enc_write_ctx == NULL)
685 || (EVP_MD_CTX_get0_md(sc->write_hash) == NULL))
691 mac_size = EVP_MD_CTX_get_size(sc->write_hash);
693 SSLfatal(sc, SSL_AD_INTERNAL_ERROR,
694 SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
699 p = SSL3_BUFFER_get_buf(wb) + prefix_len;
701 /* write the header */
703 *(p++) = type & 0xff;
704 SSL3_RECORD_set_type(&wr, type);
706 * Special case: for hello verify request, client version 1.0 and we
707 * haven't decided which version to use yet send back using version 1.0
708 * header: otherwise some clients will ignore it.
710 if (s->method->version == DTLS_ANY_VERSION &&
711 sc->max_proto_version != DTLS1_BAD_VER) {
712 *(p++) = DTLS1_VERSION >> 8;
713 *(p++) = DTLS1_VERSION & 0xff;
715 *(p++) = sc->version >> 8;
716 *(p++) = sc->version & 0xff;
719 /* field where we are to write out packet epoch, seq num and len */
723 /* Explicit IV length, block ciphers appropriate version flag */
724 if (sc->enc_write_ctx) {
725 int mode = EVP_CIPHER_CTX_get_mode(sc->enc_write_ctx);
726 if (mode == EVP_CIPH_CBC_MODE) {
727 eivlen = EVP_CIPHER_CTX_get_iv_length(sc->enc_write_ctx);
729 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
735 /* Need explicit part of IV for GCM mode */
736 else if (mode == EVP_CIPH_GCM_MODE)
737 eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
738 else if (mode == EVP_CIPH_CCM_MODE)
739 eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
745 /* lets setup the record stuff. */
746 SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
747 SSL3_RECORD_set_length(&wr, len);
748 SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
751 * we now 'read' from wr.input, wr.length bytes into wr.data
754 /* first we compress */
755 if (sc->compress != NULL) {
756 if (!ssl3_do_compress(sc, &wr)) {
757 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, SSL_R_COMPRESSION_FAILURE);
761 memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
762 SSL3_RECORD_get_length(&wr));
763 SSL3_RECORD_reset_input(&wr);
767 * we should still have the output to wr.data and the input from
768 * wr.input. Length should be wr.length. wr.data still points in the
772 if (!SSL_WRITE_ETM(sc) && mac_size != 0) {
773 if (!s->method->ssl3_enc->mac(sc, &wr,
774 &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
776 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
779 SSL3_RECORD_add_length(&wr, mac_size);
782 /* this is true regardless of mac size */
783 SSL3_RECORD_set_data(&wr, p);
784 SSL3_RECORD_reset_input(&wr);
787 SSL3_RECORD_add_length(&wr, eivlen);
789 if (s->method->ssl3_enc->enc(sc, &wr, 1, 1, NULL, mac_size) < 1) {
790 if (!ossl_statem_in_error(sc)) {
791 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
796 if (SSL_WRITE_ETM(sc) && mac_size != 0) {
797 if (!s->method->ssl3_enc->mac(sc, &wr,
798 &(p[SSL3_RECORD_get_length(&wr)]), 1)) {
799 SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
802 SSL3_RECORD_add_length(&wr, mac_size);
805 /* record length after mac and block padding */
807 /* there's only one epoch between handshake and app data */
809 s2n(sc->rlayer.d->w_epoch, pseq);
811 memcpy(pseq, &(sc->rlayer.write_sequence[2]), 6);
813 s2n(SSL3_RECORD_get_length(&wr), pseq);
815 if (sc->msg_callback)
816 sc->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
817 DTLS1_RT_HEADER_LENGTH, s, sc->msg_callback_arg);
820 * we should now have wr.data pointing to the encrypted data, which is
823 SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
824 SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
826 ssl3_record_sequence_update(&(sc->rlayer.write_sequence[0]));
828 if (create_empty_fragment) {
830 * we are in a recursive call; just return the length, don't write
833 *written = wr.length;
837 /* now let's set up wb */
838 SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
839 SSL3_BUFFER_set_offset(wb, 0);
842 * memorize arguments so that ssl3_write_pending can detect bad write
845 sc->rlayer.wpend_tot = len;
846 sc->rlayer.wpend_buf = buf;
847 sc->rlayer.wpend_type = type;
848 sc->rlayer.wpend_ret = len;
850 /* we now just need to write the buffer. Calls SSLfatal() as required. */
851 return ssl3_write_pending(sc, type, buf, len, written);
854 void dtls1_reset_seq_numbers(SSL_CONNECTION *s, int rw)
857 unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
859 if (rw & SSL3_CC_READ) {
860 seq = s->rlayer.read_sequence;
861 s->rlayer.d->r_epoch++;
864 * We must not use any buffered messages received from the previous
867 dtls1_clear_received_buffer(s);
869 seq = s->rlayer.write_sequence;
870 memcpy(s->rlayer.d->last_write_sequence, seq,
871 sizeof(s->rlayer.write_sequence));
872 s->rlayer.d->w_epoch++;
875 memset(seq, 0, seq_bytes);