2 * Copyright 2005-2018 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) {
24 SSLerr(SSL_F_DTLS_RECORD_LAYER_NEW, ERR_R_MALLOC_FAILURE);
30 d->unprocessed_rcds.q = pqueue_new();
31 d->processed_rcds.q = pqueue_new();
32 d->buffered_app_data.q = pqueue_new();
34 if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
35 || d->buffered_app_data.q == NULL) {
36 pqueue_free(d->unprocessed_rcds.q);
37 pqueue_free(d->processed_rcds.q);
38 pqueue_free(d->buffered_app_data.q);
47 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
49 DTLS_RECORD_LAYER_clear(rl);
50 pqueue_free(rl->d->unprocessed_rcds.q);
51 pqueue_free(rl->d->processed_rcds.q);
52 pqueue_free(rl->d->buffered_app_data.q);
57 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
61 DTLS1_RECORD_DATA *rdata;
62 pqueue *unprocessed_rcds;
63 pqueue *processed_rcds;
64 pqueue *buffered_app_data;
68 while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
69 rdata = (DTLS1_RECORD_DATA *)item->data;
70 OPENSSL_free(rdata->rbuf.buf);
71 OPENSSL_free(item->data);
75 while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
76 rdata = (DTLS1_RECORD_DATA *)item->data;
77 OPENSSL_free(rdata->rbuf.buf);
78 OPENSSL_free(item->data);
82 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
83 rdata = (DTLS1_RECORD_DATA *)item->data;
84 OPENSSL_free(rdata->rbuf.buf);
85 OPENSSL_free(item->data);
89 unprocessed_rcds = d->unprocessed_rcds.q;
90 processed_rcds = d->processed_rcds.q;
91 buffered_app_data = d->buffered_app_data.q;
92 memset(d, 0, sizeof(*d));
93 d->unprocessed_rcds.q = unprocessed_rcds;
94 d->processed_rcds.q = processed_rcds;
95 d->buffered_app_data.q = buffered_app_data;
98 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
100 if (e == rl->d->w_epoch - 1) {
101 memcpy(rl->d->curr_write_sequence,
102 rl->write_sequence, sizeof(rl->write_sequence));
103 memcpy(rl->write_sequence,
104 rl->d->last_write_sequence, sizeof(rl->write_sequence));
105 } else if (e == rl->d->w_epoch + 1) {
106 memcpy(rl->d->last_write_sequence,
107 rl->write_sequence, sizeof(unsigned char[8]));
108 memcpy(rl->write_sequence,
109 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
114 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
116 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
119 /* copy buffered record into SSL structure */
120 static int dtls1_copy_record(SSL *s, pitem *item)
122 DTLS1_RECORD_DATA *rdata;
124 rdata = (DTLS1_RECORD_DATA *)item->data;
126 SSL3_BUFFER_release(&s->rlayer.rbuf);
128 s->rlayer.packet = rdata->packet;
129 s->rlayer.packet_length = rdata->packet_length;
130 memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
131 memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
133 /* Set proper sequence number for mac calculation */
134 memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
139 int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
141 DTLS1_RECORD_DATA *rdata;
144 /* Limit the size of the queue to prevent DOS attacks */
145 if (pqueue_size(queue->q) >= 100)
148 rdata = OPENSSL_malloc(sizeof(*rdata));
149 item = pitem_new(priority, rdata);
150 if (rdata == NULL || item == NULL) {
153 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
154 ERR_R_INTERNAL_ERROR);
158 rdata->packet = s->rlayer.packet;
159 rdata->packet_length = s->rlayer.packet_length;
160 memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
161 memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
165 #ifndef OPENSSL_NO_SCTP
166 /* Store bio_dgram_sctp_rcvinfo struct */
167 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
168 (SSL_get_state(s) == TLS_ST_SR_FINISHED
169 || SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
170 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
171 sizeof(rdata->recordinfo), &rdata->recordinfo);
175 s->rlayer.packet = NULL;
176 s->rlayer.packet_length = 0;
177 memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
178 memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
180 if (!ssl3_setup_buffers(s)) {
181 /* SSLfatal() already called */
182 OPENSSL_free(rdata->rbuf.buf);
188 /* insert should not fail, since duplicates are dropped */
189 if (pqueue_insert(queue->q, item) == NULL) {
190 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_BUFFER_RECORD,
191 ERR_R_INTERNAL_ERROR);
192 OPENSSL_free(rdata->rbuf.buf);
201 int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
205 item = pqueue_pop(queue->q);
207 dtls1_copy_record(s, item);
209 OPENSSL_free(item->data);
219 * retrieve a buffered record that belongs to the new epoch, i.e., not
222 #define dtls1_get_unprocessed_record(s) \
223 dtls1_retrieve_buffered_record((s), \
224 &((s)->rlayer.d->unprocessed_rcds))
226 int dtls1_process_buffered_records(SSL *s)
231 DTLS1_BITMAP *bitmap;
232 unsigned int is_next_epoch;
235 item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
237 /* Check if epoch is current. */
238 if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
239 return 1; /* Nothing to do. */
241 rr = RECORD_LAYER_get_rrec(&s->rlayer);
243 rb = RECORD_LAYER_get_rbuf(&s->rlayer);
245 if (SSL3_BUFFER_get_left(rb) > 0) {
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).
255 /* Process all the records. */
256 while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
257 dtls1_get_unprocessed_record(s);
258 bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);
259 if (bitmap == NULL) {
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
265 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
266 SSL_F_DTLS1_PROCESS_BUFFERED_RECORDS,
267 ERR_R_INTERNAL_ERROR);
270 #ifndef OPENSSL_NO_SCTP
271 /* Only do replay check if no SCTP bio */
272 if (!BIO_dgram_is_sctp(SSL_get_rbio(s)))
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.
281 replayok = dtls1_record_replay_check(s, bitmap);
284 if (!replayok || !dtls1_process_record(s, bitmap)) {
285 if (ossl_statem_in_error(s)) {
286 /* dtls1_process_record called SSLfatal() */
289 /* dump this record */
291 RECORD_LAYER_reset_packet_length(&s->rlayer);
295 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
296 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) < 0) {
297 /* SSLfatal() already called */
304 * sync epoch numbers once all the unprocessed records have been
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;
314 * Return up to 'len' payload bytes received in 'type' records.
315 * 'type' is one of the following:
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)
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).
324 * This function must handle any surprises the peer may have for us, such as
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.
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
334 * 2 bytes needed (AlertLevel, AlertDescription)
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
342 int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
343 size_t len, int peek, size_t *readbytes)
348 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
350 if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
351 /* Not initialized yet */
352 if (!ssl3_setup_buffers(s)) {
353 /* SSLfatal() already called */
358 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
359 (type != SSL3_RT_HANDSHAKE)) ||
360 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
361 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
362 ERR_R_INTERNAL_ERROR);
366 if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))
368 /* type == SSL3_RT_APPLICATION_DATA */
369 i = s->handshake_func(s);
370 /* SSLfatal() already called if appropriate */
378 s->rwstate = SSL_NOTHING;
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.
389 * We are not handshaking and have no data yet, so process data buffered
390 * during the last handshake in advance, if any.
392 if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
394 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
396 #ifndef OPENSSL_NO_SCTP
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);
405 dtls1_copy_record(s, item);
407 OPENSSL_free(item->data);
412 /* Check for timeout */
413 if (dtls1_handle_timeout(s) > 0) {
415 } else if (ossl_statem_in_error(s)) {
416 /* dtls1_handle_timeout() has failed with a fatal error */
420 /* get new packet if necessary */
421 if ((SSL3_RECORD_get_length(rr) == 0)
422 || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
423 RECORD_LAYER_set_numrpipes(&s->rlayer, 0);
424 iret = dtls1_get_record(s);
426 iret = dtls1_read_failed(s, iret);
428 * Anything other than a timeout is an error. SSLfatal() already
429 * called if appropriate.
436 RECORD_LAYER_set_numrpipes(&s->rlayer, 1);
440 * Reset the count of consecutive warning alerts if we've got a non-empty
441 * record that isn't an alert.
443 if (SSL3_RECORD_get_type(rr) != SSL3_RT_ALERT
444 && SSL3_RECORD_get_length(rr) != 0)
445 s->rlayer.alert_count = 0;
447 /* we now have a packet which can be read and processed */
449 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
450 * reset by ssl3_get_finished */
451 && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
453 * We now have application data between CCS and Finished. Most likely
454 * the packets were reordered on their way, so buffer the application
455 * data for later processing rather than dropping the connection.
457 if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
458 SSL3_RECORD_get_seq_num(rr)) < 0) {
459 /* SSLfatal() already called */
462 SSL3_RECORD_set_length(rr, 0);
467 * If the other end has shut down, throw anything we read away (even in
470 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
471 SSL3_RECORD_set_length(rr, 0);
472 s->rwstate = SSL_NOTHING;
476 if (type == SSL3_RECORD_get_type(rr)
477 || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
478 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
480 * SSL3_RT_APPLICATION_DATA or
481 * SSL3_RT_HANDSHAKE or
482 * SSL3_RT_CHANGE_CIPHER_SPEC
485 * make sure that we are not getting application data when we are
486 * doing a handshake for the first time
488 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
489 (s->enc_read_ctx == NULL)) {
490 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
491 SSL_R_APP_DATA_IN_HANDSHAKE);
495 if (recvd_type != NULL)
496 *recvd_type = SSL3_RECORD_get_type(rr);
501 if (len > SSL3_RECORD_get_length(rr))
502 n = SSL3_RECORD_get_length(rr);
506 memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
508 SSL3_RECORD_sub_length(rr, n);
509 SSL3_RECORD_add_off(rr, n);
510 if (SSL3_RECORD_get_length(rr) == 0) {
511 s->rlayer.rstate = SSL_ST_READ_HEADER;
512 SSL3_RECORD_set_off(rr, 0);
515 #ifndef OPENSSL_NO_SCTP
517 * We might had to delay a close_notify alert because of reordered
518 * app data. If there was an alert and there is no message to read
519 * anymore, finally set shutdown.
521 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
522 s->d1->shutdown_received
523 && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
524 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
533 * If we get here, then type != rr->type; if we have a handshake message,
534 * then it was unexpected (Hello Request or Client Hello).
537 if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
538 unsigned int alert_level, alert_descr;
539 unsigned char *alert_bytes = SSL3_RECORD_get_data(rr)
540 + SSL3_RECORD_get_off(rr);
543 if (!PACKET_buf_init(&alert, alert_bytes, SSL3_RECORD_get_length(rr))
544 || !PACKET_get_1(&alert, &alert_level)
545 || !PACKET_get_1(&alert, &alert_descr)
546 || PACKET_remaining(&alert) != 0) {
547 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
548 SSL_R_INVALID_ALERT);
553 s->msg_callback(0, s->version, SSL3_RT_ALERT, alert_bytes, 2, s,
554 s->msg_callback_arg);
556 if (s->info_callback != NULL)
557 cb = s->info_callback;
558 else if (s->ctx->info_callback != NULL)
559 cb = s->ctx->info_callback;
562 j = (alert_level << 8) | alert_descr;
563 cb(s, SSL_CB_READ_ALERT, j);
566 if (alert_level == SSL3_AL_WARNING) {
567 s->s3->warn_alert = alert_descr;
569 s->rlayer.alert_count++;
570 if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
571 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
572 SSL_R_TOO_MANY_WARN_ALERTS);
576 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
577 #ifndef OPENSSL_NO_SCTP
579 * With SCTP and streams the socket may deliver app data
580 * after a close_notify alert. We have to check this first so
581 * that nothing gets discarded.
583 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
584 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
585 s->d1->shutdown_received = 1;
586 s->rwstate = SSL_READING;
587 BIO_clear_retry_flags(SSL_get_rbio(s));
588 BIO_set_retry_read(SSL_get_rbio(s));
592 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
595 } else if (alert_level == SSL3_AL_FATAL) {
598 s->rwstate = SSL_NOTHING;
599 s->s3->fatal_alert = alert_descr;
600 SSLfatal(s, SSL_AD_NO_ALERT, SSL_F_DTLS1_READ_BYTES,
601 SSL_AD_REASON_OFFSET + alert_descr);
602 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
603 ERR_add_error_data(2, "SSL alert number ", tmp);
604 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
605 SSL_CTX_remove_session(s->session_ctx, s->session);
608 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_DTLS1_READ_BYTES,
609 SSL_R_UNKNOWN_ALERT_TYPE);
616 if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
618 s->rwstate = SSL_NOTHING;
619 SSL3_RECORD_set_length(rr, 0);
623 if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
625 * We can't process a CCS now, because previous handshake messages
626 * are still missing, so just drop it.
628 SSL3_RECORD_set_length(rr, 0);
633 * Unexpected handshake message (Client Hello, or protocol violation)
635 if ((SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) &&
636 !ossl_statem_get_in_handshake(s)) {
637 struct hm_header_st msg_hdr;
640 * This may just be a stale retransmit. Also sanity check that we have
641 * at least enough record bytes for a message header
643 if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch
644 || SSL3_RECORD_get_length(rr) < DTLS1_HM_HEADER_LENGTH) {
645 SSL3_RECORD_set_length(rr, 0);
649 dtls1_get_message_header(rr->data, &msg_hdr);
652 * If we are server, we may have a repeated FINISHED of the client
653 * here, then retransmit our CCS and FINISHED.
655 if (msg_hdr.type == SSL3_MT_FINISHED) {
656 if (dtls1_check_timeout_num(s) < 0) {
657 /* SSLfatal) already called */
661 if (dtls1_retransmit_buffered_messages(s) <= 0) {
662 /* Fail if we encountered a fatal error */
663 if (ossl_statem_in_error(s))
666 SSL3_RECORD_set_length(rr, 0);
671 * To get here we must be trying to read app data but found handshake
672 * data. But if we're trying to read app data, and we're not in init
673 * (which is tested for at the top of this function) then init must be
676 if (!ossl_assert(SSL_is_init_finished(s))) {
677 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_READ_BYTES,
678 ERR_R_INTERNAL_ERROR);
682 /* We found handshake data, so we're going back into init */
683 ossl_statem_set_in_init(s, 1);
685 i = s->handshake_func(s);
686 /* SSLfatal() called if appropriate */
692 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
693 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
694 /* no read-ahead left? */
697 * In the case where we try to read application data, but we
698 * trigger an SSL handshake, we return -1 with the retry
699 * option set. Otherwise renegotiation may cause nasty
700 * problems in the blocking world
702 s->rwstate = SSL_READING;
703 bio = SSL_get_rbio(s);
704 BIO_clear_retry_flags(bio);
705 BIO_set_retry_read(bio);
712 switch (SSL3_RECORD_get_type(rr)) {
714 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
715 SSL_R_UNEXPECTED_RECORD);
717 case SSL3_RT_CHANGE_CIPHER_SPEC:
719 case SSL3_RT_HANDSHAKE:
721 * we already handled all of these, with the possible exception of
722 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
723 * that should not happen when type != rr->type
725 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
726 ERR_R_INTERNAL_ERROR);
728 case SSL3_RT_APPLICATION_DATA:
730 * At this point, we were expecting handshake data, but have
731 * application data. If the library was running inside ssl3_read()
732 * (i.e. in_read_app_data is set) and it makes sense to read
733 * application data at this point (session renegotiation not yet
734 * started), we will indulge it.
736 if (s->s3->in_read_app_data &&
737 (s->s3->total_renegotiations != 0) &&
738 ossl_statem_app_data_allowed(s)) {
739 s->s3->in_read_app_data = 2;
742 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_DTLS1_READ_BYTES,
743 SSL_R_UNEXPECTED_RECORD);
751 * Call this to write data in records of type 'type' It will return <= 0 if
752 * not all data has been sent or non-blocking IO.
754 int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
759 if (!ossl_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH)) {
760 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DTLS1_WRITE_BYTES,
761 ERR_R_INTERNAL_ERROR);
764 s->rwstate = SSL_NOTHING;
765 i = do_dtls1_write(s, type, buf, len, 0, written);
769 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
770 size_t len, int create_empty_fragment, size_t *written)
772 unsigned char *p, *pseq;
773 int i, mac_size, clear = 0;
774 size_t prefix_len = 0;
780 wb = &s->rlayer.wbuf[0];
783 * first check if there is a SSL3_BUFFER still being written out. This
784 * will happen with non blocking IO
786 if (!ossl_assert(SSL3_BUFFER_get_left(wb) == 0)) {
787 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
788 ERR_R_INTERNAL_ERROR);
792 /* If we have an alert to send, lets send it */
793 if (s->s3->alert_dispatch) {
794 i = s->method->ssl_dispatch_alert(s);
797 /* if it went, fall through and send more stuff */
800 if (len == 0 && !create_empty_fragment)
803 if (len > ssl_get_max_send_fragment(s)) {
804 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
805 SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
811 if ((sess == NULL) ||
812 (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
818 mac_size = EVP_MD_CTX_size(s->write_hash);
820 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
821 SSL_R_EXCEEDS_MAX_FRAGMENT_SIZE);
826 p = SSL3_BUFFER_get_buf(wb) + prefix_len;
828 /* write the header */
830 *(p++) = type & 0xff;
831 SSL3_RECORD_set_type(&wr, type);
833 * Special case: for hello verify request, client version 1.0 and we
834 * haven't decided which version to use yet send back using version 1.0
835 * header: otherwise some clients will ignore it.
837 if (s->method->version == DTLS_ANY_VERSION &&
838 s->max_proto_version != DTLS1_BAD_VER) {
839 *(p++) = DTLS1_VERSION >> 8;
840 *(p++) = DTLS1_VERSION & 0xff;
842 *(p++) = s->version >> 8;
843 *(p++) = s->version & 0xff;
846 /* field where we are to write out packet epoch, seq num and len */
850 /* Explicit IV length, block ciphers appropriate version flag */
851 if (s->enc_write_ctx) {
852 int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
853 if (mode == EVP_CIPH_CBC_MODE) {
854 eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
858 /* Need explicit part of IV for GCM mode */
859 else if (mode == EVP_CIPH_GCM_MODE)
860 eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
861 else if (mode == EVP_CIPH_CCM_MODE)
862 eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
868 /* lets setup the record stuff. */
869 SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
870 SSL3_RECORD_set_length(&wr, len);
871 SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
874 * we now 'read' from wr.input, wr.length bytes into wr.data
877 /* first we compress */
878 if (s->compress != NULL) {
879 if (!ssl3_do_compress(s, &wr)) {
880 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
881 SSL_R_COMPRESSION_FAILURE);
885 memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
886 SSL3_RECORD_get_length(&wr));
887 SSL3_RECORD_reset_input(&wr);
891 * we should still have the output to wr.data and the input from
892 * wr.input. Length should be wr.length. wr.data still points in the
896 if (!SSL_WRITE_ETM(s) && mac_size != 0) {
897 if (!s->method->ssl3_enc->mac(s, &wr,
898 &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
900 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
901 ERR_R_INTERNAL_ERROR);
904 SSL3_RECORD_add_length(&wr, mac_size);
907 /* this is true regardless of mac size */
908 SSL3_RECORD_set_data(&wr, p);
909 SSL3_RECORD_reset_input(&wr);
912 SSL3_RECORD_add_length(&wr, eivlen);
914 if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1) {
915 if (!ossl_statem_in_error(s)) {
916 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
917 ERR_R_INTERNAL_ERROR);
922 if (SSL_WRITE_ETM(s) && mac_size != 0) {
923 if (!s->method->ssl3_enc->mac(s, &wr,
924 &(p[SSL3_RECORD_get_length(&wr)]), 1)) {
925 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DO_DTLS1_WRITE,
926 ERR_R_INTERNAL_ERROR);
929 SSL3_RECORD_add_length(&wr, mac_size);
932 /* record length after mac and block padding */
934 /* there's only one epoch between handshake and app data */
936 s2n(s->rlayer.d->w_epoch, pseq);
938 memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
940 s2n(SSL3_RECORD_get_length(&wr), pseq);
943 s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
944 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
947 * we should now have wr.data pointing to the encrypted data, which is
950 SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
951 SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
953 ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
955 if (create_empty_fragment) {
957 * we are in a recursive call; just return the length, don't write
960 *written = wr.length;
964 /* now let's set up wb */
965 SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
966 SSL3_BUFFER_set_offset(wb, 0);
969 * memorize arguments so that ssl3_write_pending can detect bad write
972 s->rlayer.wpend_tot = len;
973 s->rlayer.wpend_buf = buf;
974 s->rlayer.wpend_type = type;
975 s->rlayer.wpend_ret = len;
977 /* we now just need to write the buffer. Calls SSLfatal() as required. */
978 return ssl3_write_pending(s, type, buf, len, written);
981 DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
982 unsigned int *is_next_epoch)
987 /* In current epoch, accept HM, CCS, DATA, & ALERT */
988 if (rr->epoch == s->rlayer.d->r_epoch)
989 return &s->rlayer.d->bitmap;
992 * Only HM and ALERT messages can be from the next epoch and only if we
993 * have already processed all of the unprocessed records from the last
996 else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
997 s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
998 (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
1000 return &s->rlayer.d->next_bitmap;
1006 void dtls1_reset_seq_numbers(SSL *s, int rw)
1009 unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
1011 if (rw & SSL3_CC_READ) {
1012 seq = s->rlayer.read_sequence;
1013 s->rlayer.d->r_epoch++;
1014 memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
1015 sizeof(s->rlayer.d->bitmap));
1016 memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
1019 * We must not use any buffered messages received from the previous
1022 dtls1_clear_received_buffer(s);
1024 seq = s->rlayer.write_sequence;
1025 memcpy(s->rlayer.d->last_write_sequence, seq,
1026 sizeof(s->rlayer.write_sequence));
1027 s->rlayer.d->w_epoch++;
1030 memset(seq, 0, seq_bytes);