2 * Copyright 2005-2016 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
13 #include "../ssl_locl.h"
14 #include <openssl/evp.h>
15 #include <openssl/buffer.h>
16 #include "record_locl.h"
18 int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl)
22 if ((d = OPENSSL_malloc(sizeof(*d))) == NULL)
27 d->unprocessed_rcds.q = pqueue_new();
28 d->processed_rcds.q = pqueue_new();
29 d->buffered_app_data.q = pqueue_new();
31 if (d->unprocessed_rcds.q == NULL || d->processed_rcds.q == NULL
32 || d->buffered_app_data.q == NULL) {
33 pqueue_free(d->unprocessed_rcds.q);
34 pqueue_free(d->processed_rcds.q);
35 pqueue_free(d->buffered_app_data.q);
44 void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl)
46 DTLS_RECORD_LAYER_clear(rl);
47 pqueue_free(rl->d->unprocessed_rcds.q);
48 pqueue_free(rl->d->processed_rcds.q);
49 pqueue_free(rl->d->buffered_app_data.q);
54 void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl)
58 DTLS1_RECORD_DATA *rdata;
59 pqueue *unprocessed_rcds;
60 pqueue *processed_rcds;
61 pqueue *buffered_app_data;
65 while ((item = pqueue_pop(d->unprocessed_rcds.q)) != NULL) {
66 rdata = (DTLS1_RECORD_DATA *)item->data;
67 OPENSSL_free(rdata->rbuf.buf);
68 OPENSSL_free(item->data);
72 while ((item = pqueue_pop(d->processed_rcds.q)) != NULL) {
73 rdata = (DTLS1_RECORD_DATA *)item->data;
74 OPENSSL_free(rdata->rbuf.buf);
75 OPENSSL_free(item->data);
79 while ((item = pqueue_pop(d->buffered_app_data.q)) != NULL) {
80 rdata = (DTLS1_RECORD_DATA *)item->data;
81 OPENSSL_free(rdata->rbuf.buf);
82 OPENSSL_free(item->data);
86 unprocessed_rcds = d->unprocessed_rcds.q;
87 processed_rcds = d->processed_rcds.q;
88 buffered_app_data = d->buffered_app_data.q;
89 memset(d, 0, sizeof(*d));
90 d->unprocessed_rcds.q = unprocessed_rcds;
91 d->processed_rcds.q = processed_rcds;
92 d->buffered_app_data.q = buffered_app_data;
95 void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e)
97 if (e == rl->d->w_epoch - 1) {
98 memcpy(rl->d->curr_write_sequence,
99 rl->write_sequence, sizeof(rl->write_sequence));
100 memcpy(rl->write_sequence,
101 rl->d->last_write_sequence, sizeof(rl->write_sequence));
102 } else if (e == rl->d->w_epoch + 1) {
103 memcpy(rl->d->last_write_sequence,
104 rl->write_sequence, sizeof(unsigned char[8]));
105 memcpy(rl->write_sequence,
106 rl->d->curr_write_sequence, sizeof(rl->write_sequence));
111 void DTLS_RECORD_LAYER_resync_write(RECORD_LAYER *rl)
113 memcpy(rl->write_sequence, rl->read_sequence, sizeof(rl->write_sequence));
116 void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq)
118 memcpy(rl->write_sequence, seq, SEQ_NUM_SIZE);
121 static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
124 /* copy buffered record into SSL structure */
125 static int dtls1_copy_record(SSL *s, pitem *item)
127 DTLS1_RECORD_DATA *rdata;
129 rdata = (DTLS1_RECORD_DATA *)item->data;
131 SSL3_BUFFER_release(&s->rlayer.rbuf);
133 s->rlayer.packet = rdata->packet;
134 s->rlayer.packet_length = rdata->packet_length;
135 memcpy(&s->rlayer.rbuf, &(rdata->rbuf), sizeof(SSL3_BUFFER));
136 memcpy(&s->rlayer.rrec, &(rdata->rrec), sizeof(SSL3_RECORD));
138 /* Set proper sequence number for mac calculation */
139 memcpy(&(s->rlayer.read_sequence[2]), &(rdata->packet[5]), 6);
144 int dtls1_buffer_record(SSL *s, record_pqueue *queue, unsigned char *priority)
146 DTLS1_RECORD_DATA *rdata;
149 /* Limit the size of the queue to prevent DOS attacks */
150 if (pqueue_size(queue->q) >= 100)
153 rdata = OPENSSL_malloc(sizeof(*rdata));
154 item = pitem_new(priority, rdata);
155 if (rdata == NULL || item == NULL) {
158 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
162 rdata->packet = s->rlayer.packet;
163 rdata->packet_length = s->rlayer.packet_length;
164 memcpy(&(rdata->rbuf), &s->rlayer.rbuf, sizeof(SSL3_BUFFER));
165 memcpy(&(rdata->rrec), &s->rlayer.rrec, sizeof(SSL3_RECORD));
169 #ifndef OPENSSL_NO_SCTP
170 /* Store bio_dgram_sctp_rcvinfo struct */
171 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
172 (SSL_get_state(s) == TLS_ST_SR_FINISHED
173 || SSL_get_state(s) == TLS_ST_CR_FINISHED)) {
174 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_GET_RCVINFO,
175 sizeof(rdata->recordinfo), &rdata->recordinfo);
179 s->rlayer.packet = NULL;
180 s->rlayer.packet_length = 0;
181 memset(&s->rlayer.rbuf, 0, sizeof(s->rlayer.rbuf));
182 memset(&s->rlayer.rrec, 0, sizeof(s->rlayer.rrec));
184 if (!ssl3_setup_buffers(s)) {
185 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
186 OPENSSL_free(rdata->rbuf.buf);
192 /* insert should not fail, since duplicates are dropped */
193 if (pqueue_insert(queue->q, item) == NULL) {
194 SSLerr(SSL_F_DTLS1_BUFFER_RECORD, ERR_R_INTERNAL_ERROR);
195 OPENSSL_free(rdata->rbuf.buf);
204 int dtls1_retrieve_buffered_record(SSL *s, record_pqueue *queue)
208 item = pqueue_pop(queue->q);
210 dtls1_copy_record(s, item);
212 OPENSSL_free(item->data);
222 * retrieve a buffered record that belongs to the new epoch, i.e., not
225 #define dtls1_get_unprocessed_record(s) \
226 dtls1_retrieve_buffered_record((s), \
227 &((s)->rlayer.d->unprocessed_rcds))
229 int dtls1_process_buffered_records(SSL *s)
234 item = pqueue_peek(s->rlayer.d->unprocessed_rcds.q);
236 /* Check if epoch is current. */
237 if (s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch)
238 return (1); /* Nothing to do. */
240 rb = RECORD_LAYER_get_rbuf(&s->rlayer);
242 if (SSL3_BUFFER_get_left(rb) > 0) {
244 * We've still got data from the current packet to read. There could
245 * be a record from the new epoch in it - so don't overwrite it
246 * with the unprocessed records yet (we'll do it when we've
247 * finished reading the current packet).
252 /* Process all the records. */
253 while (pqueue_peek(s->rlayer.d->unprocessed_rcds.q)) {
254 dtls1_get_unprocessed_record(s);
255 if (!dtls1_process_record(s))
257 if (dtls1_buffer_record(s, &(s->rlayer.d->processed_rcds),
258 SSL3_RECORD_get_seq_num(s->rlayer.rrec)) <
265 * sync epoch numbers once all the unprocessed records have been
268 s->rlayer.d->processed_rcds.epoch = s->rlayer.d->r_epoch;
269 s->rlayer.d->unprocessed_rcds.epoch = s->rlayer.d->r_epoch + 1;
275 * Return up to 'len' payload bytes received in 'type' records.
276 * 'type' is one of the following:
278 * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
279 * - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
280 * - 0 (during a shutdown, no data has to be returned)
282 * If we don't have stored data to work from, read a SSL/TLS record first
283 * (possibly multiple records if we still don't have anything to return).
285 * This function must handle any surprises the peer may have for us, such as
286 * Alert records (e.g. close_notify) or renegotiation requests. ChangeCipherSpec
287 * messages are treated as if they were handshake messages *if* the |recd_type|
288 * argument is non NULL.
289 * Also if record payloads contain fragments too small to process, we store
290 * them until there is enough for the respective protocol (the record protocol
291 * may use arbitrary fragmentation and even interleaving):
292 * Change cipher spec protocol
293 * just 1 byte needed, no need for keeping anything stored
295 * 2 bytes needed (AlertLevel, AlertDescription)
297 * 4 bytes needed (HandshakeType, uint24 length) -- we just have
298 * to detect unexpected Client Hello and Hello Request messages
299 * here, anything else is handled by higher layers
300 * Application data protocol
301 * none of our business
303 int dtls1_read_bytes(SSL *s, int type, int *recvd_type, unsigned char *buf,
309 void (*cb) (const SSL *ssl, int type2, int val) = NULL;
311 if (!SSL3_BUFFER_is_initialised(&s->rlayer.rbuf)) {
312 /* Not initialized yet */
313 if (!ssl3_setup_buffers(s))
317 if ((type && (type != SSL3_RT_APPLICATION_DATA) &&
318 (type != SSL3_RT_HANDSHAKE)) ||
319 (peek && (type != SSL3_RT_APPLICATION_DATA))) {
320 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
325 * check whether there's a handshake message (client hello?) waiting
327 if ((ret = have_handshake_fragment(s, type, buf, len)))
331 * Now s->rlayer.d->handshake_fragment_len == 0 if
332 * type == SSL3_RT_HANDSHAKE.
335 #ifndef OPENSSL_NO_SCTP
337 * Continue handshake if it had to be interrupted to read app data with
340 if ((!ossl_statem_get_in_handshake(s) && SSL_in_init(s)) ||
341 (BIO_dgram_is_sctp(SSL_get_rbio(s))
342 && ossl_statem_in_sctp_read_sock(s)
343 && s->s3->in_read_app_data != 2))
345 if (!ossl_statem_get_in_handshake(s) && SSL_in_init(s))
348 /* type == SSL3_RT_APPLICATION_DATA */
349 i = s->handshake_func(s);
353 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
359 s->rwstate = SSL_NOTHING;
362 * s->s3->rrec.type - is the type of record
363 * s->s3->rrec.data, - data
364 * s->s3->rrec.off, - offset into 'data' for next read
365 * s->s3->rrec.length, - number of bytes.
370 * We are not handshaking and have no data yet, so process data buffered
371 * during the last handshake in advance, if any.
373 if (SSL_is_init_finished(s) && SSL3_RECORD_get_length(rr) == 0) {
375 item = pqueue_pop(s->rlayer.d->buffered_app_data.q);
377 #ifndef OPENSSL_NO_SCTP
378 /* Restore bio_dgram_sctp_rcvinfo struct */
379 if (BIO_dgram_is_sctp(SSL_get_rbio(s))) {
380 DTLS1_RECORD_DATA *rdata = (DTLS1_RECORD_DATA *)item->data;
381 BIO_ctrl(SSL_get_rbio(s), BIO_CTRL_DGRAM_SCTP_SET_RCVINFO,
382 sizeof(rdata->recordinfo), &rdata->recordinfo);
386 dtls1_copy_record(s, item);
388 OPENSSL_free(item->data);
393 /* Check for timeout */
394 if (dtls1_handle_timeout(s) > 0)
397 /* get new packet if necessary */
398 if ((SSL3_RECORD_get_length(rr) == 0)
399 || (s->rlayer.rstate == SSL_ST_READ_BODY)) {
400 ret = dtls1_get_record(s);
402 ret = dtls1_read_failed(s, ret);
403 /* anything other than a timeout is an error */
411 /* we now have a packet which can be read and processed */
413 if (s->s3->change_cipher_spec /* set when we receive ChangeCipherSpec,
414 * reset by ssl3_get_finished */
415 && (SSL3_RECORD_get_type(rr) != SSL3_RT_HANDSHAKE)) {
417 * We now have application data between CCS and Finished. Most likely
418 * the packets were reordered on their way, so buffer the application
419 * data for later processing rather than dropping the connection.
421 if (dtls1_buffer_record(s, &(s->rlayer.d->buffered_app_data),
422 SSL3_RECORD_get_seq_num(rr)) < 0) {
423 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
426 SSL3_RECORD_set_length(rr, 0);
431 * If the other end has shut down, throw anything we read away (even in
434 if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
435 SSL3_RECORD_set_length(rr, 0);
436 s->rwstate = SSL_NOTHING;
440 if (type == SSL3_RECORD_get_type(rr)
441 || (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC
442 && type == SSL3_RT_HANDSHAKE && recvd_type != NULL)) {
444 * SSL3_RT_APPLICATION_DATA or
445 * SSL3_RT_HANDSHAKE or
446 * SSL3_RT_CHANGE_CIPHER_SPEC
449 * make sure that we are not getting application data when we are
450 * doing a handshake for the first time
452 if (SSL_in_init(s) && (type == SSL3_RT_APPLICATION_DATA) &&
453 (s->enc_read_ctx == NULL)) {
454 al = SSL_AD_UNEXPECTED_MESSAGE;
455 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_APP_DATA_IN_HANDSHAKE);
459 if (recvd_type != NULL)
460 *recvd_type = SSL3_RECORD_get_type(rr);
465 if ((unsigned int)len > SSL3_RECORD_get_length(rr))
466 n = SSL3_RECORD_get_length(rr);
468 n = (unsigned int)len;
470 memcpy(buf, &(SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)]), n);
472 SSL3_RECORD_sub_length(rr, n);
473 SSL3_RECORD_add_off(rr, n);
474 if (SSL3_RECORD_get_length(rr) == 0) {
475 s->rlayer.rstate = SSL_ST_READ_HEADER;
476 SSL3_RECORD_set_off(rr, 0);
479 #ifndef OPENSSL_NO_SCTP
481 * We were about to renegotiate but had to read belated application
482 * data first, so retry.
484 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
485 SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA &&
486 ossl_statem_in_sctp_read_sock(s)) {
487 s->rwstate = SSL_READING;
488 BIO_clear_retry_flags(SSL_get_rbio(s));
489 BIO_set_retry_read(SSL_get_rbio(s));
493 * We might had to delay a close_notify alert because of reordered
494 * app data. If there was an alert and there is no message to read
495 * anymore, finally set shutdown.
497 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
498 s->d1->shutdown_received
499 && !BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
500 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
508 * If we get here, then type != rr->type; if we have a handshake message,
509 * then it was unexpected (Hello Request or Client Hello).
513 * In case of record types for which we have 'fragment' storage, fill
514 * that so that we can process the data at a fixed place.
517 unsigned int k, dest_maxlen = 0;
518 unsigned char *dest = NULL;
519 unsigned int *dest_len = NULL;
521 if (SSL3_RECORD_get_type(rr) == SSL3_RT_HANDSHAKE) {
522 dest_maxlen = sizeof s->rlayer.d->handshake_fragment;
523 dest = s->rlayer.d->handshake_fragment;
524 dest_len = &s->rlayer.d->handshake_fragment_len;
525 } else if (SSL3_RECORD_get_type(rr) == SSL3_RT_ALERT) {
526 dest_maxlen = sizeof(s->rlayer.d->alert_fragment);
527 dest = s->rlayer.d->alert_fragment;
528 dest_len = &s->rlayer.d->alert_fragment_len;
530 #ifndef OPENSSL_NO_HEARTBEATS
531 else if (SSL3_RECORD_get_type(rr) == DTLS1_RT_HEARTBEAT) {
532 /* We allow a 0 return */
533 if (dtls1_process_heartbeat(s, SSL3_RECORD_get_data(rr),
534 SSL3_RECORD_get_length(rr)) < 0) {
537 /* Exit and notify application to read again */
538 SSL3_RECORD_set_length(rr, 0);
539 s->rwstate = SSL_READING;
540 BIO_clear_retry_flags(SSL_get_rbio(s));
541 BIO_set_retry_read(SSL_get_rbio(s));
545 /* else it's a CCS message, or application data or wrong */
546 else if (SSL3_RECORD_get_type(rr) != SSL3_RT_CHANGE_CIPHER_SPEC) {
548 * Application data while renegotiating is allowed. Try again
551 if (SSL3_RECORD_get_type(rr) == SSL3_RT_APPLICATION_DATA) {
553 s->s3->in_read_app_data = 2;
554 bio = SSL_get_rbio(s);
555 s->rwstate = SSL_READING;
556 BIO_clear_retry_flags(bio);
557 BIO_set_retry_read(bio);
561 /* Not certain if this is the right error handling */
562 al = SSL_AD_UNEXPECTED_MESSAGE;
563 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
567 if (dest_maxlen > 0) {
569 * XDTLS: In a pathological case, the Client Hello may be
570 * fragmented--don't always expect dest_maxlen bytes
572 if (SSL3_RECORD_get_length(rr) < dest_maxlen) {
573 #ifdef DTLS1_AD_MISSING_HANDSHAKE_MESSAGE
575 * for normal alerts rr->length is 2, while
576 * dest_maxlen is 7 if we were to handle this
577 * non-existing alert...
581 s->rlayer.rstate = SSL_ST_READ_HEADER;
582 SSL3_RECORD_set_length(rr, 0);
586 /* now move 'n' bytes: */
587 for (k = 0; k < dest_maxlen; k++) {
588 dest[k] = SSL3_RECORD_get_data(rr)[SSL3_RECORD_get_off(rr)];
589 SSL3_RECORD_add_off(rr, 1);
590 SSL3_RECORD_add_length(rr, -1);
592 *dest_len = dest_maxlen;
597 * s->rlayer.d->handshake_fragment_len == 12 iff rr->type == SSL3_RT_HANDSHAKE;
598 * s->rlayer.d->alert_fragment_len == 7 iff rr->type == SSL3_RT_ALERT.
599 * (Possibly rr is 'empty' now, i.e. rr->length may be 0.)
602 /* If we are a client, check for an incoming 'Hello Request': */
604 (s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
605 (s->rlayer.d->handshake_fragment[0] == SSL3_MT_HELLO_REQUEST) &&
606 (s->session != NULL) && (s->session->cipher != NULL)) {
607 s->rlayer.d->handshake_fragment_len = 0;
609 if ((s->rlayer.d->handshake_fragment[1] != 0) ||
610 (s->rlayer.d->handshake_fragment[2] != 0) ||
611 (s->rlayer.d->handshake_fragment[3] != 0)) {
612 al = SSL_AD_DECODE_ERROR;
613 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_BAD_HELLO_REQUEST);
618 * no need to check sequence number on HELLO REQUEST messages
622 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
623 s->rlayer.d->handshake_fragment, 4, s,
624 s->msg_callback_arg);
626 if (SSL_is_init_finished(s) &&
627 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS) &&
628 !s->s3->renegotiate) {
629 s->d1->handshake_read_seq++;
632 if (ssl3_renegotiate_check(s)) {
633 i = s->handshake_func(s);
637 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
641 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
642 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
643 /* no read-ahead left? */
646 * In the case where we try to read application data,
647 * but we trigger an SSL handshake, we return -1 with
648 * the retry option set. Otherwise renegotiation may
649 * cause nasty problems in the blocking world
651 s->rwstate = SSL_READING;
652 bio = SSL_get_rbio(s);
653 BIO_clear_retry_flags(bio);
654 BIO_set_retry_read(bio);
661 * we either finished a handshake or ignored the request, now try
662 * again to obtain the (application) data we were asked for
667 if (s->rlayer.d->alert_fragment_len >= DTLS1_AL_HEADER_LENGTH) {
668 int alert_level = s->rlayer.d->alert_fragment[0];
669 int alert_descr = s->rlayer.d->alert_fragment[1];
671 s->rlayer.d->alert_fragment_len = 0;
674 s->msg_callback(0, s->version, SSL3_RT_ALERT,
675 s->rlayer.d->alert_fragment, 2, s,
676 s->msg_callback_arg);
678 if (s->info_callback != NULL)
679 cb = s->info_callback;
680 else if (s->ctx->info_callback != NULL)
681 cb = s->ctx->info_callback;
684 j = (alert_level << 8) | alert_descr;
685 cb(s, SSL_CB_READ_ALERT, j);
688 if (alert_level == SSL3_AL_WARNING) {
689 s->s3->warn_alert = alert_descr;
690 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
691 #ifndef OPENSSL_NO_SCTP
693 * With SCTP and streams the socket may deliver app data
694 * after a close_notify alert. We have to check this first so
695 * that nothing gets discarded.
697 if (BIO_dgram_is_sctp(SSL_get_rbio(s)) &&
698 BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
699 s->d1->shutdown_received = 1;
700 s->rwstate = SSL_READING;
701 BIO_clear_retry_flags(SSL_get_rbio(s));
702 BIO_set_retry_read(SSL_get_rbio(s));
706 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
710 /* XXX: this is a possible improvement in the future */
711 /* now check if it's a missing record */
712 if (alert_descr == DTLS1_AD_MISSING_HANDSHAKE_MESSAGE) {
714 unsigned int frag_off;
715 unsigned char *p = &(s->rlayer.d->alert_fragment[2]);
720 dtls1_retransmit_message(s,
721 dtls1_get_queue_priority
722 (frag->msg_header.seq, 0), frag_off,
724 if (!found && SSL_in_init(s)) {
726 * fprintf( stderr,"in init = %d\n", SSL_in_init(s));
729 * requested a message not yet sent, send an alert
732 ssl3_send_alert(s, SSL3_AL_WARNING,
733 DTLS1_AD_MISSING_HANDSHAKE_MESSAGE);
737 } else if (alert_level == SSL3_AL_FATAL) {
740 s->rwstate = SSL_NOTHING;
741 s->s3->fatal_alert = alert_descr;
742 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_AD_REASON_OFFSET + alert_descr);
743 BIO_snprintf(tmp, sizeof tmp, "%d", alert_descr);
744 ERR_add_error_data(2, "SSL alert number ", tmp);
745 s->shutdown |= SSL_RECEIVED_SHUTDOWN;
746 SSL_CTX_remove_session(s->session_ctx, s->session);
749 al = SSL_AD_ILLEGAL_PARAMETER;
750 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNKNOWN_ALERT_TYPE);
757 if (s->shutdown & SSL_SENT_SHUTDOWN) { /* but we have not received a
759 s->rwstate = SSL_NOTHING;
760 SSL3_RECORD_set_length(rr, 0);
764 if (SSL3_RECORD_get_type(rr) == SSL3_RT_CHANGE_CIPHER_SPEC) {
766 * We can't process a CCS now, because previous handshake messages
767 * are still missing, so just drop it.
769 SSL3_RECORD_set_length(rr, 0);
774 * Unexpected handshake message (Client Hello, or protocol violation)
776 if ((s->rlayer.d->handshake_fragment_len >= DTLS1_HM_HEADER_LENGTH) &&
777 !ossl_statem_get_in_handshake(s)) {
778 struct hm_header_st msg_hdr;
780 /* this may just be a stale retransmit */
781 dtls1_get_message_header(rr->data, &msg_hdr);
782 if (SSL3_RECORD_get_epoch(rr) != s->rlayer.d->r_epoch) {
783 SSL3_RECORD_set_length(rr, 0);
788 * If we are server, we may have a repeated FINISHED of the client
789 * here, then retransmit our CCS and FINISHED.
791 if (msg_hdr.type == SSL3_MT_FINISHED) {
792 if (dtls1_check_timeout_num(s) < 0)
795 dtls1_retransmit_buffered_messages(s);
796 SSL3_RECORD_set_length(rr, 0);
800 if (SSL_is_init_finished(s) &&
801 !(s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)) {
802 ossl_statem_set_in_init(s, 1);
806 i = s->handshake_func(s);
810 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_SSL_HANDSHAKE_FAILURE);
814 if (!(s->mode & SSL_MODE_AUTO_RETRY)) {
815 if (SSL3_BUFFER_get_left(&s->rlayer.rbuf) == 0) {
816 /* no read-ahead left? */
819 * In the case where we try to read application data, but we
820 * trigger an SSL handshake, we return -1 with the retry
821 * option set. Otherwise renegotiation may cause nasty
822 * problems in the blocking world
824 s->rwstate = SSL_READING;
825 bio = SSL_get_rbio(s);
826 BIO_clear_retry_flags(bio);
827 BIO_set_retry_read(bio);
834 switch (SSL3_RECORD_get_type(rr)) {
836 /* TLS just ignores unknown message types */
837 if (s->version == TLS1_VERSION) {
838 SSL3_RECORD_set_length(rr, 0);
841 al = SSL_AD_UNEXPECTED_MESSAGE;
842 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
844 case SSL3_RT_CHANGE_CIPHER_SPEC:
846 case SSL3_RT_HANDSHAKE:
848 * we already handled all of these, with the possible exception of
849 * SSL3_RT_HANDSHAKE when ossl_statem_get_in_handshake(s) is true, but
850 * that should not happen when type != rr->type
852 al = SSL_AD_UNEXPECTED_MESSAGE;
853 SSLerr(SSL_F_DTLS1_READ_BYTES, ERR_R_INTERNAL_ERROR);
855 case SSL3_RT_APPLICATION_DATA:
857 * At this point, we were expecting handshake data, but have
858 * application data. If the library was running inside ssl3_read()
859 * (i.e. in_read_app_data is set) and it makes sense to read
860 * application data at this point (session renegotiation not yet
861 * started), we will indulge it.
863 if (s->s3->in_read_app_data &&
864 (s->s3->total_renegotiations != 0) &&
865 ossl_statem_app_data_allowed(s)) {
866 s->s3->in_read_app_data = 2;
869 al = SSL_AD_UNEXPECTED_MESSAGE;
870 SSLerr(SSL_F_DTLS1_READ_BYTES, SSL_R_UNEXPECTED_RECORD);
877 ssl3_send_alert(s, SSL3_AL_FATAL, al);
882 * this only happens when a client hello is received and a handshake
885 static int have_handshake_fragment(SSL *s, int type, unsigned char *buf,
889 if ((type == SSL3_RT_HANDSHAKE)
890 && (s->rlayer.d->handshake_fragment_len > 0))
891 /* (partially) satisfy request from storage */
893 unsigned char *src = s->rlayer.d->handshake_fragment;
894 unsigned char *dst = buf;
899 while ((len > 0) && (s->rlayer.d->handshake_fragment_len > 0)) {
902 s->rlayer.d->handshake_fragment_len--;
905 /* move any remaining fragment bytes: */
906 for (k = 0; k < s->rlayer.d->handshake_fragment_len; k++)
907 s->rlayer.d->handshake_fragment[k] = *src++;
915 * Call this to write data in records of type 'type' It will return <= 0 if
916 * not all data has been sent or non-blocking IO.
918 int dtls1_write_bytes(SSL *s, int type, const void *buf, int len)
922 OPENSSL_assert(len <= SSL3_RT_MAX_PLAIN_LENGTH);
923 s->rwstate = SSL_NOTHING;
924 i = do_dtls1_write(s, type, buf, len, 0);
928 int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
929 unsigned int len, int create_empty_fragment)
931 unsigned char *p, *pseq;
932 int i, mac_size, clear = 0;
939 wb = &s->rlayer.wbuf[0];
942 * first check if there is a SSL3_BUFFER still being written out. This
943 * will happen with non blocking IO
945 if (SSL3_BUFFER_get_left(wb) != 0) {
946 OPENSSL_assert(0); /* XDTLS: want to see if we ever get here */
947 return (ssl3_write_pending(s, type, buf, len));
950 /* If we have an alert to send, lets send it */
951 if (s->s3->alert_dispatch) {
952 i = s->method->ssl_dispatch_alert(s);
955 /* if it went, fall through and send more stuff */
958 if (len == 0 && !create_empty_fragment)
963 if ((sess == NULL) ||
964 (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL))
970 mac_size = EVP_MD_CTX_size(s->write_hash);
975 p = SSL3_BUFFER_get_buf(wb) + prefix_len;
977 /* write the header */
979 *(p++) = type & 0xff;
980 SSL3_RECORD_set_type(&wr, type);
982 * Special case: for hello verify request, client version 1.0 and we
983 * haven't decided which version to use yet send back using version 1.0
984 * header: otherwise some clients will ignore it.
986 if (s->method->version == DTLS_ANY_VERSION &&
987 s->max_proto_version != DTLS1_BAD_VER) {
988 *(p++) = DTLS1_VERSION >> 8;
989 *(p++) = DTLS1_VERSION & 0xff;
991 *(p++) = s->version >> 8;
992 *(p++) = s->version & 0xff;
995 /* field where we are to write out packet epoch, seq num and len */
999 /* Explicit IV length, block ciphers appropriate version flag */
1000 if (s->enc_write_ctx) {
1001 int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);
1002 if (mode == EVP_CIPH_CBC_MODE) {
1003 eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);
1007 /* Need explicit part of IV for GCM mode */
1008 else if (mode == EVP_CIPH_GCM_MODE)
1009 eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
1010 else if (mode == EVP_CIPH_CCM_MODE)
1011 eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
1017 /* lets setup the record stuff. */
1018 SSL3_RECORD_set_data(&wr, p + eivlen); /* make room for IV in case of CBC */
1019 SSL3_RECORD_set_length(&wr, (int)len);
1020 SSL3_RECORD_set_input(&wr, (unsigned char *)buf);
1023 * we now 'read' from wr.input, wr.length bytes into wr.data
1026 /* first we compress */
1027 if (s->compress != NULL) {
1028 if (!ssl3_do_compress(s, &wr)) {
1029 SSLerr(SSL_F_DO_DTLS1_WRITE, SSL_R_COMPRESSION_FAILURE);
1033 memcpy(SSL3_RECORD_get_data(&wr), SSL3_RECORD_get_input(&wr),
1034 SSL3_RECORD_get_length(&wr));
1035 SSL3_RECORD_reset_input(&wr);
1039 * we should still have the output to wr.data and the input from
1040 * wr.input. Length should be wr.length. wr.data still points in the
1044 if (mac_size != 0) {
1045 if (s->method->ssl3_enc->mac(s, &wr,
1046 &(p[SSL3_RECORD_get_length(&wr) + eivlen]),
1049 SSL3_RECORD_add_length(&wr, mac_size);
1052 /* this is true regardless of mac size */
1053 SSL3_RECORD_set_data(&wr, p);
1054 SSL3_RECORD_reset_input(&wr);
1057 SSL3_RECORD_add_length(&wr, eivlen);
1059 if (s->method->ssl3_enc->enc(s, &wr, 1, 1) < 1)
1062 /* record length after mac and block padding */
1064 * if (type == SSL3_RT_APPLICATION_DATA || (type == SSL3_RT_ALERT && !
1068 /* there's only one epoch between handshake and app data */
1070 s2n(s->rlayer.d->w_epoch, pseq);
1074 * else s2n(s->d1->handshake_epoch, pseq);
1077 memcpy(pseq, &(s->rlayer.write_sequence[2]), 6);
1079 s2n(SSL3_RECORD_get_length(&wr), pseq);
1081 if (s->msg_callback)
1082 s->msg_callback(1, 0, SSL3_RT_HEADER, pseq - DTLS1_RT_HEADER_LENGTH,
1083 DTLS1_RT_HEADER_LENGTH, s, s->msg_callback_arg);
1086 * we should now have wr.data pointing to the encrypted data, which is
1089 SSL3_RECORD_set_type(&wr, type); /* not needed but helps for debugging */
1090 SSL3_RECORD_add_length(&wr, DTLS1_RT_HEADER_LENGTH);
1092 ssl3_record_sequence_update(&(s->rlayer.write_sequence[0]));
1094 if (create_empty_fragment) {
1096 * we are in a recursive call; just return the length, don't write
1102 /* now let's set up wb */
1103 SSL3_BUFFER_set_left(wb, prefix_len + SSL3_RECORD_get_length(&wr));
1104 SSL3_BUFFER_set_offset(wb, 0);
1107 * memorize arguments so that ssl3_write_pending can detect bad write
1110 s->rlayer.wpend_tot = len;
1111 s->rlayer.wpend_buf = buf;
1112 s->rlayer.wpend_type = type;
1113 s->rlayer.wpend_ret = len;
1115 /* we now just need to write the buffer */
1116 return ssl3_write_pending(s, type, buf, len);
1121 DTLS1_BITMAP *dtls1_get_bitmap(SSL *s, SSL3_RECORD *rr,
1122 unsigned int *is_next_epoch)
1127 /* In current epoch, accept HM, CCS, DATA, & ALERT */
1128 if (rr->epoch == s->rlayer.d->r_epoch)
1129 return &s->rlayer.d->bitmap;
1132 * Only HM and ALERT messages can be from the next epoch and only if we
1133 * have already processed all of the unprocessed records from the last
1136 else if (rr->epoch == (unsigned long)(s->rlayer.d->r_epoch + 1) &&
1137 s->rlayer.d->unprocessed_rcds.epoch != s->rlayer.d->r_epoch &&
1138 (rr->type == SSL3_RT_HANDSHAKE || rr->type == SSL3_RT_ALERT)) {
1140 return &s->rlayer.d->next_bitmap;
1146 void dtls1_reset_seq_numbers(SSL *s, int rw)
1149 unsigned int seq_bytes = sizeof(s->rlayer.read_sequence);
1151 if (rw & SSL3_CC_READ) {
1152 seq = s->rlayer.read_sequence;
1153 s->rlayer.d->r_epoch++;
1154 memcpy(&s->rlayer.d->bitmap, &s->rlayer.d->next_bitmap,
1155 sizeof(s->rlayer.d->bitmap));
1156 memset(&s->rlayer.d->next_bitmap, 0, sizeof(s->rlayer.d->next_bitmap));
1158 seq = s->rlayer.write_sequence;
1159 memcpy(s->rlayer.d->last_write_sequence, seq,
1160 sizeof(s->rlayer.write_sequence));
1161 s->rlayer.d->w_epoch++;
1164 memset(seq, 0, seq_bytes);