2 * Copyright 2005-2017 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "../ssl_locl.h"
13 #include <openssl/evp.h>
14 #include <openssl/buffer.h>
15 #include "record_locl.h"
16 #include "../packet_locl.h"
17 #include "internal/cryptlib.h"
19 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
23 if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
28 d->unprocessed_rcds.q = pqueue_new();
29 d->processed_rcds.q = pqueue_new();
30 d->buffered_app_data.q = pqueue_new();
32 if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
33 || d->buffered_app_data.q == NULL) {
34 pqueue_free(d->unprocessed_rcds.q);
35 pqueue_free(d->processed_rcds.q);
36 pqueue_free(d->buffered_app_data.q);
45 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
47 DTLS_RECORD_LAYER_clear(rl);
48 pqueue_free(rl->d->unprocessed_rcds.q);
49 pqueue_free(rl->d->processed_rcds.q);
50 pqueue_free(rl->d->buffered_app_data.q);
55 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
59 DTLS1_RECORD_DATA *rdata;
60 pqueue *unprocessed_rcds;
61 pqueue *processed_rcds;
62 pqueue *buffered_app_data;
66 while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
67 rdata = (DTLS1_RECORD_DATA *)item->data;
68 OPENSSL_free(rdata->rbuf.buf);
69 OPENSSL_free(item->data);
73 while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
74 rdata = (DTLS1_RECORD_DATA *)item->data;
75 OPENSSL_free(rdata->rbuf.buf);
76 OPENSSL_free(item->data);
80 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
81 rdata = (DTLS1_RECORD_DATA *)item->data;
82 OPENSSL_free(rdata->rbuf.buf);
83 OPENSSL_free(item->data);
87 unprocessed_rcds = d->unprocessed_rcds.q;
88 processed_rcds = d->processed_rcds.q;
89 buffered_app_data = d->buffered_app_data.q;
90 memset(d, 0, sizeof(*d));
91 d->unprocessed_rcds.q = unprocessed_rcds;
92 d->processed_rcds.q = processed_rcds;
93 d->buffered_app_data.q = buffered_app_data;
96 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
98 if (e == rl->d->w_epoch - 1) {
99 memcpy(rl->d->curr_write_sequence,
100 rl->write_sequence, sizeof(rl->write_sequence));
101 memcpy(rl->write_sequence,
102 rl->d->last_write_sequence, sizeof(rl->write_sequence));
103 } else if (e == rl->d->w_epoch + 1) {
104 memcpy(rl->d->last_write_sequence,
105 rl->write_sequence, sizeof(unsigned char[8]));
106 memcpy(rl->write_sequence,
107 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
112 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
114 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
117 /* copy buffered record into SSL structure */
118 static int dtls1_copy_record(SSL *s, pitem *item)
120 DTLS1_RECORD_DATA *rdata;
122 rdata = (DTLS1_RECORD_DATA *)item->data;
124 SSL3_BUFFER_release(&s->rlayer.rbuf);
126 s->rlayer.packet = rdata->packet;
127 s->rlayer.packet_length = rdata->packet_length;
128 memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
129 memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
131 /* Set proper sequence number for mac calculation */
132 memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
137 int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
139 DTLS1_RECORD_DATA *rdata;
142 /* Limit the size of the queue to prevent DOS attacks */
143 if (pqueue_size(queue->q) >= 100)
146 rdata = OPENSSL_malloc(sizeof(*rdata));
147 item = pitem_new(priority, rdata);
148 if (rdata == NULL || item == NULL) {
151 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
152 ERR_R_INTERNAL_ERROR);
156 rdata->packet = s->rlayer.packet;
157 rdata->packet_length = s->rlayer.packet_length;
158 memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
159 memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
163 #ifndef OPENSSL_NO_SCTP
164 /* Store bio_dgram_sctp_rcvinfo struct */
165 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
166 (SSL_get_state(s) == TLS_ST_SR_FINISHED
167 || SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
168 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
169 sizeof(rdata->recordinfo), &rdata->recordinfo);
173 s->rlayer.packet = NULL;
174 s->rlayer.packet_length = 0;
175 memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
176 memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
178 if (!ssl3_setup_buffers(s)) {
179 /* SSLfatal() already called */
180 OPENSSL_free(rdata->rbuf.buf);
186 /* insert should not fail, since duplicates are dropped */
187 if (pqueue_insert(queue->q, item) == NULL) {
188 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
189 ERR_R_INTERNAL_ERROR);
190 OPENSSL_free(rdata->rbuf.buf);
199 int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
203 item = pqueue_pop(queue->q);
205 dtls1_copy_record(s, item);
207 OPENSSL_free(item->data);
217 * retrieve a buffered record that belongs to the new epoch, i.e., not
220 #define dtls1_get_unprocessed_record(s) \
221 dtls1_retrieve_buffered_record((s), \
222 &((s)->rlayer.d->unprocessed_rcds))
224 int dtls1_process_buffered_records(SSL *s)
229 DTLS1_BITMAP *bitmap;
230 unsigned int is_next_epoch;
233 item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
235 /* Check if epoch is current. */
236 if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
237 return 1; /* Nothing to do. */
239 rr = RECORD_LAYER_get_rrec(&s->rlayer);
241 rb = RECORD_LAYER_get_rbuf(&s->rlayer);
243 if (SSL3_BUFFER_get_left(rb) > 0) {
245 * We've still got data from the current packet to read. There could
246 * be a record from the new epoch in it - so don't overwrite it
247 * with the unprocessed records yet (we'll do it when we've
248 * finished reading the current packet).
253 /* Process all the records. */
254 while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
255 dtls1_get_unprocessed_record(s);
256 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
257 if (bitmap == NULL) {
259 * Should not happen. This will only ever be NULL when the
260 * current record is from a different epoch. But that cannot
261 * be the case because we already checked the epoch above
263 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
264 SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
265 ERR_R_INTERNAL_ERROR);
268 #ifndef OPENSSL_NO_SCTP
269 /* Only do replay check if no SCTP bio */
270 if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))
274 * Check whether this is a repeat, or aged record. We did this
275 * check once already when we first received the record - but
276 * we might have updated the window since then due to
277 * records we subsequently processed.
279 replayok = dtls1_record_replay_check(s, bitmap);
282 if (!replayok || !dtls1_process_record(s, bitmap)) {
283 if (ossl_statem_in_error(s)) {
284 /* dtls1_process_record called SSLfatal() */
287 /* dump this record */
289 RECORD_LAYER_reset_packet_length(&s->rlayer);
293 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
294 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) {
295 /* SSLfatal() already called */
302 * sync epoch numbers once all the unprocessed records have been
305 s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
306 s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
312 * Return up to 'len' payload bytes received in 'type' records.
313 * 'type' is one of the following:
315 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
316 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
317 * - 0 (during a shutdown, no data has to be returned)
319 * If we don't have stored data to work from, read a SSL/TLS record first
320 * (possibly multiple records if we still don't have anything to return).
322 * This function must handle any surprises the peer may have for us, such as
323 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
324 * messages are treated as if they were handshake messages *if* the |recd_type|
325 * argument is non NULL.
326 * Also if record payloads contain fragments too small to process, we store
327 * them until there is enough for the respective protocol (the record protocol
328 * may use arbitrary fragmentation and even interleaving):
329 * Change cipher spec protocol
330 * just 1 byte needed, no need for keeping anything stored
332 * 2 bytes needed (AlertLevel, AlertDescription)
334 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
335 * to detect unexpected Client Hello and Hello Request messages
336 * here, anything else is handled by higher layers
337 * Application data protocol
338 * none of our business
340 int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
341 size_t len, int peek, size_t *readbytes)
346 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
348 if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
349 /* Not initialized yet */
350 if (!ssl3_setup_buffers(s)) {
351 /* SSLfatal() already called */
356 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
357 (type != SSL3_RT_HANDSHAKE)) ||
358 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
359 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
363 if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))
365 /* type == SSL3_RT_APPLICATION_DATA */
366 i = s->handshake_func(s);
367 /* SSLfatal() already called if appropriate */
375 s->rwstate = SSL_NOTHING;
378 * s->s3->rrec.type - is the type of record
379 * s->s3->rrec.data, - data
380 * s->s3->rrec.off, - offset into 'data' for next read
381 * s->s3->rrec.length, - number of bytes.
386 * We are not handshaking and have no data yet, so process data buffered
387 * during the last handshake in advance, if any.
389 if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
391 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
393 #ifndef OPENSSL_NO_SCTP
394 /* Restore bio_dgram_sctp_rcvinfo struct */
395 if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
396 DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
397 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
398 sizeof(rdata->recordinfo), &rdata->recordinfo);
402 dtls1_copy_record(s, item);
404 OPENSSL_free(item->data);
409 /* Check for timeout */
410 if (dtls1_handle_timeout(s) > 0) {
412 } else if (ossl_statem_in_error(s)) {
413 /* dtls1_handle_timeout() has failed with a fatal error */
417 /* get new packet if necessary */
418 if ((SSL3_RECORD_get_length(rr) == 0)
419 || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
420 iret = dtls1_get_record(s);
422 iret = dtls1_read_failed(s, iret);
424 * Anything other than a timeout is an error. SSLfatal() already
425 * called if appropriate.
435 * Reset the count of consecutive warning alerts if we've got a non-empty
436 * record that isn't an alert.
438 if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
439 && SSL3_RECORD_get_length(rr) != 0)
440 s->rlayer.alert_count = 0;
442 /* we now have a packet which can be read and processed */
444 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
445 * reset by ssl3_get_finished */
446 && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
448 * We now have application data between CCS and Finished. Most likely
449 * the packets were reordered on their way, so buffer the application
450 * data for later processing rather than dropping the connection.
452 if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
453 SSL3_RECORD_get_seq_num(rr)) < 0) {
454 /* SSLfatal() already called */
457 SSL3_RECORD_set_length(rr, 0);
462 * If the other end has shut down, throw anything we read away (even in
465 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
466 SSL3_RECORD_set_length(rr, 0);
467 s->rwstate = SSL_NOTHING;
471 if (type == SSL3_RECORD_get_type(rr)
472 || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
473 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
475 * SSL3_RT_APPLICATION_DATA or
476 * SSL3_RT_HANDSHAKE or
477 * SSL3_RT_CHANGE_CIPHER_SPEC
480 * make sure that we are not getting application data when we are
481 * doing a handshake for the first time
483 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
484 (s->enc_read_ctx == NULL)) {
485 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
486 SSL_R_APP_DATA_IN_HANDSHAKE);
490 if (recvd_type != NULL)
491 *recvd_type = SSL3_RECORD_get_type(rr);
496 if (len > SSL3_RECORD_get_length(rr))
497 n = SSL3_RECORD_get_length(rr);
501 memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
503 SSL3_RECORD_sub_length(rr, n);
504 SSL3_RECORD_add_off(rr, n);
505 if (SSL3_RECORD_get_length(rr) == 0) {
506 s->rlayer.rstate = SSL_ST_READ_HEADER;
507 SSL3_RECORD_set_off(rr, 0);
510 #ifndef OPENSSL_NO_SCTP
512 * We might had to delay a close_notify alert because of reordered
513 * app data. If there was an alert and there is no message to read
514 * anymore, finally set shutdown.
516 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
517 s->d1->shutdown_received
518 && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
519 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
528 * If we get here, then type != rr->type; if we have a handshake message,
529 * then it was unexpected (Hello Request or Client Hello).
532 if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
533 unsigned int alert_level, alert_descr;
534 unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
535 + SSL3_RECORD_get_off(rr);
538 if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
539 || !PACKET_get_1(&alert, &alert_level)
540 || !PACKET_get_1(&alert, &alert_descr)
541 || PACKET_remaining(&alert) != 0) {
542 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
543 SSL_R_INVALID_ALERT);
548 s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
549 s->msg_callback_arg);
551 if (s->info_callback != NULL)
552 cb = s->info_callback;
553 else if (s->ctx->info_callback != NULL)
554 cb = s->ctx->info_callback;
557 j = (alert_level << 8) | alert_descr;
558 cb(s, SSL_CB_READ_ALERT, j);
561 if (alert_level == SSL3_AL_WARNING) {
562 s->s3->warn_alert = alert_descr;
564 s->rlayer.alert_count++;
565 if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
566 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
567 SSL_R_TOO_MANY_WARN_ALERTS);
571 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
572 #ifndef OPENSSL_NO_SCTP
574 * With SCTP and streams the socket may deliver app data
575 * after a close_notify alert. We have to check this first so
576 * that nothing gets discarded.
578 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
579 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
580 s->d1->shutdown_received = 1;
581 s->rwstate = SSL_READING;
582 BIO_clear_retry_flags(SSL_get_rbio(s));
583 BIO_set_retry_read(SSL_get_rbio(s));
587 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
590 } else if (alert_level == SSL3_AL_FATAL) {
593 s->rwstate = SSL_NOTHING;
594 s->s3->fatal_alert = alert_descr;
595 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_READ_BYTES,
596 SSL_AD_REASON_OFFSET + alert_descr);
597 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
598 ERR_add_error_data(2, "SSL alert number ", tmp);
599 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
600 SSL_CTX_remove_session(s->session_ctx, s->session);
603 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_READ_BYTES,
604 SSL_R_UNKNOWN_ALERT_TYPE);
611 if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
613 s->rwstate = SSL_NOTHING;
614 SSL3_RECORD_set_length(rr, 0);
618 if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
620 * We can't process a CCS now, because previous handshake messages
621 * are still missing, so just drop it.
623 SSL3_RECORD_set_length(rr, 0);
628 * Unexpected handshake message (Client Hello, or protocol violation)
630 if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) &&
631 !ossl_statem_get_in_handshake(s)) {
632 struct hm_header_st msg_hdr;
635 * This may just be a stale retransmit. Also sanity check that we have
636 * at least enough record bytes for a message header
638 if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch
639 || SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) {
640 SSL3_RECORD_set_length(rr, 0);
644 dtls1_get_message_header(rr->data, &msg_hdr);
647 * If we are server, we may have a repeated FINISHED of the client
648 * here, then retransmit our CCS and FINISHED.
650 if (msg_hdr.type == SSL3_MT_FINISHED) {
651 if (dtls1_check_timeout_num(s) < 0) {
652 /* SSLfatal) already called */
656 if (dtls1_retransmit_buffered_messages(s) <= 0) {
657 /* Fail if we encountered a fatal error */
658 if (ossl_statem_in_error(s))
661 SSL3_RECORD_set_length(rr, 0);
666 * To get here we must be trying to read app data but found handshake
667 * data. But if we're trying to read app data, and we're not in init
668 * (which is tested for at the top of this function) then init must be
671 if (!ossl_assert(SSL_is_init_finished(s))) {
672 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
673 ERR_R_INTERNAL_ERROR);
677 /* We found handshake data, so we're going back into init */
678 ossl_statem_set_in_init(s, 1);
680 i = s->handshake_func(s);
681 /* SSLfatal() called if appropriate */
687 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
688 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
689 /* no read-ahead left? */
692 * In the case where we try to read application data, but we
693 * trigger an SSL handshake, we return -1 with the retry
694 * option set. Otherwise renegotiation may cause nasty
695 * problems in the blocking world
697 s->rwstate = SSL_READING;
698 bio = SSL_get_rbio(s);
699 BIO_clear_retry_flags(bio);
700 BIO_set_retry_read(bio);
707 switch (SSL3_RECORD_get_type(rr)) {
709 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
710 SSL_R_UNEXPECTED_RECORD);
712 case SSL3_RT_CHANGE_CIPHER_SPEC:
714 case SSL3_RT_HANDSHAKE:
716 * we already handled all of these, with the possible exception of
717 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
718 * that should not happen when type != rr->type
720 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
721 ERR_R_INTERNAL_ERROR);
723 case SSL3_RT_APPLICATION_DATA:
725 * At this point, we were expecting handshake data, but have
726 * application data. If the library was running inside ssl3_read()
727 * (i.e. in_read_app_data is set) and it makes sense to read
728 * application data at this point (session renegotiation not yet
729 * started), we will indulge it.
731 if (s->s3->in_read_app_data &&
732 (s->s3->total_renegotiations != 0) &&
733 ossl_statem_app_data_allowed(s)) {
734 s->s3->in_read_app_data = 2;
737 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
738 SSL_R_UNEXPECTED_RECORD);
746 * Call this to write data in records of type 'type' It will return <= 0 if
747 * not all data has been sent or non-blocking IO.
749 int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
754 if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
755 SSLerr(SSL_F_DTLS1_WRITE_BYTES, ERR_R_INTERNAL_ERROR);
758 s->rwstate = SSL_NOTHING;
759 i = do_dtls1_write(s, type, buf, len, 0, written);
763 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
764 size_t len, int create_empty_fragment, size_t *written)
766 unsigned char *p, *pseq;
767 int i, mac_size, clear = 0;
768 size_t prefix_len = 0;
774 wb = &s->rlayer.wbuf[0];
777 * first check if there is a SSL3_BUFFER still being written out. This
778 * will happen with non blocking IO
780 if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
781 SSLerr(SSL_F_DO_DTLS1_WRITE, ERR_R_INTERNAL_ERROR);
785 /* If we have an alert to send, lets send it */
786 if (s->s3->alert_dispatch) {
787 i = s->method->ssl_dispatch_alert(s);
790 /* if it went, fall through and send more stuff */
793 if (len == 0 && !create_empty_fragment)
796 if (len > ssl_get_max_send_fragment(s)) {
797 SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
803 if ((sess == NULL) ||
804 (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
810 mac_size = EVP_MD_CTX_size(s->write_hash);
815 p = SSL3_BUFFER_get_buf(wb) + prefix_len;
817 /* write the header */
819 *(p++) = type & 0xff;
820 SSL3_RECORD_set_type(&wr, type);
822 * Special case: for hello verify request, client version 1.0 and we
823 * haven't decided which version to use yet send back using version 1.0
824 * header: otherwise some clients will ignore it.
826 if (s->method->version == DTLS_ANY_VERSION &&
827 s->max_proto_version != DTLS1_BAD_VER) {
828 *(p++) = DTLS1_VERSION >> 8;
829 *(p++) = DTLS1_VERSION & 0xff;
831 *(p++) = s->version >> 8;
832 *(p++) = s->version & 0xff;
835 /* field where we are to write out packet epoch, seq num and len */
839 /* Explicit IV length, block ciphers appropriate version flag */
840 if (s->enc_write_ctx) {
841 int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
842 if (mode == EVP_CIPH_CBC_MODE) {
843 eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
847 /* Need explicit part of IV for GCM mode */
848 else if (mode == EVP_CIPH_GCM_MODE)
849 eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
850 else if (mode == EVP_CIPH_CCM_MODE)
851 eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
857 /* lets setup the record stuff. */
858 SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
859 SSL3_RECORD_set_length(&wr, len);
860 SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
863 * we now 'read' from wr.input, wr.length bytes into wr.data
866 /* first we compress */
867 if (s->compress != NULL) {
868 if (!ssl3_do_compress(s, &wr)) {
869 SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
873 memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
874 SSL3_RECORD_get_length(&wr));
875 SSL3_RECORD_reset_input(&wr);
879 * we should still have the output to wr.data and the input from
880 * wr.input. Length should be wr.length. wr.data still points in the
884 if (!SSL_WRITE_ETM(s) && mac_size != 0) {
885 if (!s->method->ssl3_enc->mac(s, &wr,
886 &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
889 SSL3_RECORD_add_length(&wr, mac_size);
892 /* this is true regardless of mac size */
893 SSL3_RECORD_set_data(&wr, p);
894 SSL3_RECORD_reset_input(&wr);
897 SSL3_RECORD_add_length(&wr, eivlen);
899 if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)
902 if (SSL_WRITE_ETM(s) && mac_size != 0) {
903 if (!s->method->ssl3_enc->mac(s, &wr,
904 &(p[SSL3_RECORD_get_length(&wr)]), 1))
906 SSL3_RECORD_add_length(&wr, mac_size);
909 /* record length after mac and block padding */
911 /* there's only one epoch between handshake and app data */
913 s2n(s->rlayer.d->w_epoch, pseq);
915 memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
917 s2n(SSL3_RECORD_get_length(&wr), pseq);
920 s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
921 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
924 * we should now have wr.data pointing to the encrypted data, which is
927 SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
928 SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
930 ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
932 if (create_empty_fragment) {
934 * we are in a recursive call; just return the length, don't write
937 *written = wr.length;
941 /* now let's set up wb */
942 SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
943 SSL3_BUFFER_set_offset(wb, 0);
946 * memorize arguments so that ssl3_write_pending can detect bad write
949 s->rlayer.wpend_tot = len;
950 s->rlayer.wpend_buf = buf;
951 s->rlayer.wpend_type = type;
952 s->rlayer.wpend_ret = len;
954 /* we now just need to write the buffer. Calls SSLfatal() as required. */
955 return ssl3_write_pending(s, type, buf, len, written);
960 DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
961 unsigned int *is_next_epoch)
966 /* In current epoch, accept HM, CCS, DATA, & ALERT */
967 if (rr->epoch == s->rlayer.d->r_epoch)
968 return &s->rlayer.d->bitmap;
971 * Only HM and ALERT messages can be from the next epoch and only if we
972 * have already processed all of the unprocessed records from the last
975 else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
976 s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
977 (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
979 return &s->rlayer.d->next_bitmap;
985 void dtls1_reset_seq_numbers(SSL *s, int rw)
988 unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
990 if (rw & SSL3_CC_READ) {
991 seq = s->rlayer.read_sequence;
992 s->rlayer.d->r_epoch++;
993 memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
994 sizeof(s->rlayer.d->bitmap));
995 memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
998 * We must not use any buffered messages received from the previous
1001 dtls1_clear_received_buffer(s);
1003 seq = s->rlayer.write_sequence;
1004 memcpy(s->rlayer.d->last_write_sequence, seq,
1005 sizeof(s->rlayer.write_sequence));
1006 s->rlayer.d->w_epoch++;
1009 memset(seq, 0, seq_bytes);