/*
- * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
#include <stdio.h>
#include <limits.h>
#include <errno.h>
+#include <assert.h>
#include "../ssl_local.h"
+#include "../quic/quic_local.h"
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <openssl/rand.h>
rl->s = s;
}
-void RECORD_LAYER_clear(RECORD_LAYER *rl)
+int RECORD_LAYER_clear(RECORD_LAYER *rl)
{
+ int ret = 1;
+
+ /* Clear any buffered records we no longer need */
+ while (rl->curr_rec < rl->num_recs)
+ ret &= ssl_release_record(rl->s,
+ &(rl->tlsrecs[rl->curr_rec++]),
+ 0);
+
+
rl->wnum = 0;
memset(rl->handshake_fragment, 0, sizeof(rl->handshake_fragment));
rl->handshake_fragment_len = 0;
rl->wpend_tot = 0;
rl->wpend_type = 0;
- rl->wpend_ret = 0;
rl->wpend_buf = NULL;
+ rl->alert_count = 0;
+ rl->num_recs = 0;
+ rl->curr_rec = 0;
+
+ BIO_free(rl->rrlnext);
+ rl->rrlnext = NULL;
if (rl->rrlmethod != NULL)
rl->rrlmethod->free(rl->rrl); /* Ignore return value */
if (rl->d)
DTLS_RECORD_LAYER_clear(rl);
+
+ return ret;
+}
+
+int RECORD_LAYER_reset(RECORD_LAYER *rl)
+{
+ int ret;
+
+ ret = RECORD_LAYER_clear(rl);
+
+ /* We try and reset both record layers even if one fails */
+ ret &= ssl_set_new_record_layer(rl->s,
+ SSL_CONNECTION_IS_DTLS(rl->s)
+ ? DTLS_ANY_VERSION : TLS_ANY_VERSION,
+ OSSL_RECORD_DIRECTION_READ,
+ OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0,
+ NULL, 0, NULL, 0, NULL, 0, NULL, 0,
+ NID_undef, NULL, NULL, NULL);
+
+ ret &= ssl_set_new_record_layer(rl->s,
+ SSL_CONNECTION_IS_DTLS(rl->s)
+ ? DTLS_ANY_VERSION : TLS_ANY_VERSION,
+ OSSL_RECORD_DIRECTION_WRITE,
+ OSSL_RECORD_PROTECTION_LEVEL_NONE, NULL, 0,
+ NULL, 0, NULL, 0, NULL, 0, NULL, 0,
+ NID_undef, NULL, NULL, NULL);
+
+ /* SSLfatal already called in the event of failure */
+ return ret;
}
/* Checks if we have unprocessed read ahead data pending */
TLS_RECORD *rdata;
pitem *item, *iter;
- iter = pqueue_iterator(sc->rlayer.d->buffered_app_data.q);
+ iter = pqueue_iterator(sc->rlayer.d->buffered_app_data);
while ((item = pqueue_next(&iter)) != NULL) {
rdata = item->data;
num += rdata->length;
{
SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
- if (sc == NULL)
+ if (sc == NULL || IS_QUIC(s))
return;
sc->rlayer.default_read_buf_len = len;
}
return shrt;
}
-static int tls_write_check_pending(SSL_CONNECTION *s, int type,
+static int tls_write_check_pending(SSL_CONNECTION *s, uint8_t type,
const unsigned char *buf, size_t len)
{
if (s->rlayer.wpend_tot == 0)
* Call this to write data in records of type 'type' It will return <= 0 if
* not all data has been sent or non-blocking IO.
*/
-int ssl3_write_bytes(SSL *ssl, int type, const void *buf_, size_t len,
+int ssl3_write_bytes(SSL *ssl, uint8_t type, const void *buf_, size_t len,
size_t *written)
{
const unsigned char *buf = buf_;
/* Retry needed */
i = HANDLE_RLAYER_WRITE_RETURN(s,
s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl));
- if (i <= 0)
+ if (i <= 0) {
+ s->rlayer.wnum = tot;
return i;
+ }
tot += s->rlayer.wpend_tot;
s->rlayer.wpend_tot = 0;
} /* else no retry required */
s->rlayer.wpend_tot = 0;
s->rlayer.wpend_type = type;
s->rlayer.wpend_buf = buf;
- s->rlayer.wpend_ret = len;
}
if (tot == len) { /* done? */
i = ssl->method->ssl_dispatch_alert(ssl);
if (i <= 0) {
/* SSLfatal() already called if appropriate */
+ s->rlayer.wnum = tot;
return i;
}
/* if it went, fall through and send more stuff */
} else {
ERR_new();
ERR_set_debug(file, line, 0);
+ /*
+ * This reason code is part of the API and may be used by
+ * applications for control flow decisions.
+ */
ossl_statem_fatal(s, SSL_AD_DECODE_ERROR,
SSL_R_UNEXPECTED_EOF_WHILE_READING, NULL);
}
return ret;
}
-void ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr)
+int ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr, size_t length)
{
+ assert(rr->length >= length);
if (rr->rechandle != NULL) {
+ if (length == 0)
+ length = rr->length;
/* The record layer allocated the buffers for this record */
- s->rlayer.rrlmethod->release_record(s->rlayer.rrl, rr->rechandle);
- } else {
+ if (HANDLE_RLAYER_READ_RETURN(s,
+ s->rlayer.rrlmethod->release_record(s->rlayer.rrl,
+ rr->rechandle,
+ length)) <= 0) {
+ /* RLAYER_fatal already called */
+ return 0;
+ }
+
+ if (length == rr->length)
+ s->rlayer.curr_rec++;
+ } else if (length == 0 || length == rr->length) {
/* We allocated the buffers for this record (only happens with DTLS) */
- OPENSSL_free(rr->data);
+ OPENSSL_free(rr->allocdata);
+ rr->allocdata = NULL;
}
- s->rlayer.curr_rec++;
+ rr->length -= length;
+ if (rr->length > 0)
+ rr->off += length;
+ else
+ rr->off = 0;
+
+ return 1;
}
/*-
* Return up to 'len' payload bytes received in 'type' records.
* 'type' is one of the following:
*
- * - SSL3_RT_HANDSHAKE (when ssl3_get_message calls us)
+ * - SSL3_RT_HANDSHAKE (when tls_get_message_header and tls_get_message_body
+ * call us)
* - SSL3_RT_APPLICATION_DATA (when ssl3_read calls us)
* - 0 (during a shutdown, no data has to be returned)
*
* Application data protocol
* none of our business
*/
-int ssl3_read_bytes(SSL *ssl, int type, int *recvd_type, unsigned char *buf,
- size_t len, int peek, size_t *readbytes)
+int ssl3_read_bytes(SSL *ssl, uint8_t type, uint8_t *recvd_type,
+ unsigned char *buf, size_t len,
+ int peek, size_t *readbytes)
{
int i, j, ret;
size_t n, curr_rec, totalbytes;
* SSL_read() with a zero length buffer will eventually cause
* SSL_pending() to report data as being available.
*/
- if (rr->length == 0)
- ssl_release_record(s, rr);
+ if (rr->length == 0 && !ssl_release_record(s, rr, 0))
+ return -1;
return 0;
}
buf += n;
if (peek) {
/* Mark any zero length record as consumed CVE-2016-6305 */
- if (rr->length == 0)
- ssl_release_record(s, rr);
+ if (rr->length == 0 && !ssl_release_record(s, rr, 0))
+ return -1;
} else {
- if (s->options & SSL_OP_CLEANSE_PLAINTEXT)
- OPENSSL_cleanse(&(rr->data[rr->off]), n);
- rr->length -= n;
- rr->off += n;
- if (rr->length == 0)
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, n))
+ return -1;
}
if (rr->length == 0
|| (peek && n == rr->length)) {
if (rr->type == SSL3_RT_ALERT) {
unsigned int alert_level, alert_descr;
- unsigned char *alert_bytes = rr->data
- + rr->off;
+ const unsigned char *alert_bytes = rr->data + rr->off;
PACKET alert;
if (!PACKET_buf_init(&alert, alert_bytes, rr->length)
if ((!is_tls13 && alert_level == SSL3_AL_WARNING)
|| (is_tls13 && alert_descr == SSL_AD_USER_CANCELLED)) {
s->s3.warn_alert = alert_descr;
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, 0))
+ return -1;
s->rlayer.alert_count++;
if (s->rlayer.alert_count == MAX_WARN_ALERT_COUNT) {
SSL_AD_REASON_OFFSET + alert_descr,
"SSL alert number %d", alert_descr);
s->shutdown |= SSL_RECEIVED_SHUTDOWN;
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, 0))
+ return -1;
SSL_CTX_remove_session(s->session_ctx, s->session);
return 0;
} else if (alert_descr == SSL_AD_NO_RENEGOTIATION) {
* sent close_notify.
*/
if (!SSL_CONNECTION_IS_TLS13(s)) {
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, 0))
+ return -1;
if ((s->mode & SSL_MODE_AUTO_RETRY) != 0)
goto start;
* above.
* No alert sent because we already sent close_notify
*/
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, 0))
+ return -1;
SSLfatal(s, SSL_AD_NO_ALERT,
SSL_R_APPLICATION_DATA_AFTER_CLOSE_NOTIFY);
return -1;
n = rr->length; /* available bytes */
/* now move 'n' bytes: */
- memcpy(dest + *dest_len, rr->data + rr->off, n);
- rr->off += n;
- rr->length -= n;
- *dest_len += n;
- if (rr->length == 0)
- ssl_release_record(s, rr);
+ if (n > 0) {
+ memcpy(dest + *dest_len, rr->data + rr->off, n);
+ *dest_len += n;
+ }
+ /*
+ * We release the number of bytes consumed, or the whole record if it
+ * is zero length
+ */
+ if ((n > 0 || rr->length == 0) && !ssl_release_record(s, rr, n))
+ return -1;
if (*dest_len < dest_maxlen)
goto start; /* fragment was too small */
/* SSLfatal() already called */
return -1;
}
- ssl_release_record(s, rr);
+ if (!ssl_release_record(s, rr, 0))
+ return -1;
goto start;
} else {
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_RECORD);
{ OSSL_FUNC_RLAYER_MSG_CALLBACK, (void (*)(void))rlayer_msg_callback_wrapper },
{ OSSL_FUNC_RLAYER_SECURITY, (void (*)(void))rlayer_security_wrapper },
{ OSSL_FUNC_RLAYER_PADDING, (void (*)(void))rlayer_padding_wrapper },
- { 0, NULL }
+ OSSL_DISPATCH_END
};
void ossl_ssl_set_custom_record_layer(SSL_CONNECTION *s,
prev = s->rlayer.rrlnext;
if (SSL_CONNECTION_IS_DTLS(s)
&& level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
- epoch = DTLS_RECORD_LAYER_get_r_epoch(&s->rlayer) + 1; /* new epoch */
+ epoch = dtls1_get_epoch(s, SSL3_CC_READ); /* new epoch */
#ifndef OPENSSL_NO_DGRAM
if (SSL_CONNECTION_IS_DTLS(s))
next = BIO_new(BIO_s_mem());
if (next == NULL) {
- BIO_free(prev);
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
return 0;
}
} else {
if (SSL_CONNECTION_IS_DTLS(s)
&& level != OSSL_RECORD_PROTECTION_LEVEL_NONE)
- epoch = DTLS_RECORD_LAYER_get_w_epoch(&s->rlayer) + 1; /* new epoch */
+ epoch = dtls1_get_epoch(s, SSL3_CC_WRITE); /* new epoch */
}
/*