#include "record_local.h"
#include "internal/packet.h"
-#if defined(OPENSSL_SMALL_FOOTPRINT) || \
- !( defined(AES_ASM) && ( \
- defined(__x86_64) || defined(__x86_64__) || \
- defined(_M_AMD64) || defined(_M_X64) ) \
- )
-# undef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
-# define EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK 0
-#endif
-
void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s)
{
rl->s = s;
rl->wpend_ret = 0;
rl->wpend_buf = NULL;
- ssl3_release_write_buffer(rl->s);
-
- RECORD_LAYER_reset_write_sequence(rl);
-
if (rl->rrlmethod != NULL)
rl->rrlmethod->free(rl->rrl); /* Ignore return value */
if (rl->wrlmethod != NULL)
DTLS_RECORD_LAYER_clear(rl);
}
-void RECORD_LAYER_release(RECORD_LAYER *rl)
-{
- if (rl->numwpipes > 0)
- ssl3_release_write_buffer(rl->s);
-}
-
/* Checks if we have unprocessed read ahead data pending */
int RECORD_LAYER_read_pending(const RECORD_LAYER *rl)
{
int RECORD_LAYER_write_pending(const RECORD_LAYER *rl)
{
- return (rl->numwpipes > 0)
- && SSL3_BUFFER_get_left(&rl->wbuf[rl->numwpipes - 1]) != 0;
+ return rl->wpend_tot > 0;
}
-void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl)
+static uint32_t ossl_get_max_early_data(SSL_CONNECTION *s)
{
- memset(rl->write_sequence, 0, sizeof(rl->write_sequence));
+ uint32_t max_early_data;
+ SSL_SESSION *sess = s->session;
+
+ /*
+ * If we are a client then we always use the max_early_data from the
+ * session/psksession. Otherwise we go with the lowest out of the max early
+ * data set in the session and the configured max_early_data.
+ */
+ if (!s->server && sess->ext.max_early_data == 0) {
+ if (!ossl_assert(s->psksession != NULL
+ && s->psksession->ext.max_early_data > 0)) {
+ SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
+ return 0;
+ }
+ sess = s->psksession;
+ }
+
+ if (!s->server)
+ max_early_data = sess->ext.max_early_data;
+ else if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED)
+ max_early_data = s->recv_max_early_data;
+ else
+ max_early_data = s->recv_max_early_data < sess->ext.max_early_data
+ ? s->recv_max_early_data : sess->ext.max_early_data;
+
+ return max_early_data;
+}
+
+static int ossl_early_data_count_ok(SSL_CONNECTION *s, size_t length,
+ size_t overhead, int send)
+{
+ uint32_t max_early_data;
+
+ max_early_data = ossl_get_max_early_data(s);
+
+ if (max_early_data == 0) {
+ SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
+ SSL_R_TOO_MUCH_EARLY_DATA);
+ return 0;
+ }
+
+ /* If we are dealing with ciphertext we need to allow for the overhead */
+ max_early_data += overhead;
+
+ if (s->early_data_count + length > max_early_data) {
+ SSLfatal(s, send ? SSL_AD_INTERNAL_ERROR : SSL_AD_UNEXPECTED_MESSAGE,
+ SSL_R_TOO_MUCH_EARLY_DATA);
+ return 0;
+ }
+ s->early_data_count += length;
+
+ return 1;
}
size_t ssl3_pending(const SSL *s)
const unsigned char *buf = buf_;
size_t tot;
size_t n, max_send_fragment, split_send_fragment, maxpipes;
- /* TODO(RECLAYER): Re-enable multiblock code */
-#if 0 && !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
- size_t nw;
-#endif
- SSL3_BUFFER *wb;
int i;
SSL_CONNECTION *s = SSL_CONNECTION_FROM_SSL_ONLY(ssl);
OSSL_RECORD_TEMPLATE tmpls[SSL_MAX_PIPELINES];
+ unsigned int recversion;
if (s == NULL)
return -1;
- wb = &s->rlayer.wbuf[0];
s->rwstate = SSL_NOTHING;
tot = s->rlayer.wnum;
/*
* report the error in a way the user will notice
*/
if ((len < s->rlayer.wnum)
- || ((wb->left != 0) && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
+ || ((s->rlayer.wpend_tot != 0)
+ && (len < (s->rlayer.wnum + s->rlayer.wpend_tot)))) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH);
return -1;
}
* into init unless we have writes pending - in which case we should finish
* doing that first.
*/
- if (wb->left == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
- || s->ext.extra_tickets_expected > 0))
+ if (s->rlayer.wpend_tot == 0 && (s->key_update != SSL_KEY_UPDATE_NONE
+ || s->ext.extra_tickets_expected > 0))
ossl_statem_set_in_init(s, 1);
/*
return i;
} else if (i > 0) {
/* Retry needed */
- i = s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl);
+ i = HANDLE_RLAYER_WRITE_RETURN(s,
+ s->rlayer.wrlmethod->retry_write_records(s->rlayer.wrl));
if (i <= 0)
return i;
tot += s->rlayer.wpend_tot;
s->rlayer.wpend_ret = len;
}
-/* TODO(RECLAYER): Re-enable multiblock code */
-#if 0 && !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
- /*
- * Depending on platform multi-block can deliver several *times*
- * better performance. Downside is that it has to allocate
- * jumbo buffer to accommodate up to 8 records, but the
- * compromise is considered worthy.
- */
- if (type == SSL3_RT_APPLICATION_DATA
- && len >= 4 * (max_send_fragment = ssl_get_max_send_fragment(s))
- && s->compress == NULL
- && s->msg_callback == NULL
- && !SSL_WRITE_ETM(s)
- && SSL_USE_EXPLICIT_IV(s)
- && !BIO_get_ktls_send(s->wbio)
- && (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
- & EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK) != 0) {
- unsigned char aad[13];
- EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
- size_t packlen;
- int packleni;
-
- /* minimize address aliasing conflicts */
- if ((max_send_fragment & 0xfff) == 0)
- max_send_fragment -= 512;
-
- if (tot == 0 || wb->buf == NULL) { /* allocate jumbo buffer */
- ssl3_release_write_buffer(s);
-
- packlen = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
- EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE,
- (int)max_send_fragment, NULL);
-
- if (len >= 8 * max_send_fragment)
- packlen *= 8;
- else
- packlen *= 4;
-
- if (!ssl3_setup_write_buffer(s, 1, packlen)) {
- /* SSLfatal() already called */
- return -1;
- }
- } else if (tot == len) { /* done? */
- /* free jumbo buffer */
- ssl3_release_write_buffer(s);
- *written = tot;
- return 1;
- }
-
- n = (len - tot);
- for (;;) {
- if (n < 4 * max_send_fragment) {
- /* free jumbo buffer */
- ssl3_release_write_buffer(s);
- break;
- }
-
- if (s->s3.alert_dispatch) {
- i = ssl->method->ssl_dispatch_alert(ssl);
- if (i <= 0) {
- /* SSLfatal() already called if appropriate */
- s->rlayer.wnum = tot;
- return i;
- }
- }
-
- if (n >= 8 * max_send_fragment)
- nw = max_send_fragment * (mb_param.interleave = 8);
- else
- nw = max_send_fragment * (mb_param.interleave = 4);
-
- memcpy(aad, s->rlayer.write_sequence, 8);
- aad[8] = type;
- aad[9] = (unsigned char)(s->version >> 8);
- aad[10] = (unsigned char)(s->version);
- aad[11] = 0;
- aad[12] = 0;
- mb_param.out = NULL;
- mb_param.inp = aad;
- mb_param.len = nw;
-
- packleni = EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
- EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
- sizeof(mb_param), &mb_param);
- packlen = (size_t)packleni;
- if (packleni <= 0 || packlen > wb->len) { /* never happens */
- /* free jumbo buffer */
- ssl3_release_write_buffer(s);
- break;
- }
-
- mb_param.out = wb->buf;
- mb_param.inp = &buf[tot];
- mb_param.len = nw;
-
- if (EVP_CIPHER_CTX_ctrl(s->enc_write_ctx,
- EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
- sizeof(mb_param), &mb_param) <= 0)
- return -1;
-
- s->rlayer.write_sequence[7] += mb_param.interleave;
- if (s->rlayer.write_sequence[7] < mb_param.interleave) {
- int j = 6;
- while (j >= 0 && (++s->rlayer.write_sequence[j--]) == 0) ;
- }
-
- wb->offset = 0;
- wb->left = packlen;
-
- s->rlayer.wpend_tot = nw;
- s->rlayer.wpend_buf = &buf[tot];
- s->rlayer.wpend_type = type;
- s->rlayer.wpend_ret = nw;
-
- i = ssl3_write_pending(s, type, &buf[tot], nw, &tmpwrit);
- if (i <= 0) {
- /* SSLfatal() already called if appropriate */
- if (i < 0 && (!s->wbio || !BIO_should_retry(s->wbio))) {
- /* free jumbo buffer */
- ssl3_release_write_buffer(s);
- }
- s->rlayer.wnum = tot;
- return i;
- }
- if (tmpwrit == n) {
- /* free jumbo buffer */
- ssl3_release_write_buffer(s);
- *written = tot + tmpwrit;
- return 1;
- }
- n -= tmpwrit;
- tot += tmpwrit;
- }
- } else
-#endif /* !defined(OPENSSL_NO_MULTIBLOCK) && EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK */
if (tot == len) { /* done? */
- if (s->mode & SSL_MODE_RELEASE_BUFFERS && !SSL_CONNECTION_IS_DTLS(s))
- ssl3_release_write_buffer(s);
-
*written = tot;
return 1;
}
/* If we have an alert to send, lets send it */
- if (s->s3.alert_dispatch) {
+ if (s->s3.alert_dispatch > 0) {
i = ssl->method->ssl_dispatch_alert(ssl);
if (i <= 0) {
/* SSLfatal() already called if appropriate */
max_send_fragment = ssl_get_max_send_fragment(s);
split_send_fragment = ssl_get_split_send_fragment(s);
- /*
- * TODO(RECLAYER): This comment is now out-of-date and probably needs to
- * move somewhere else
- *
- * If max_pipelines is 0 then this means "undefined" and we default to
- * 1 pipeline. Similarly if the cipher does not support pipelined
- * processing then we also only use 1 pipeline, or if we're not using
- * explicit IVs
- */
- maxpipes = s->max_pipelines;
- if (maxpipes > SSL_MAX_PIPELINES) {
- /*
- * We should have prevented this when we set max_pipelines so we
- * shouldn't get here
- */
- SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
- return -1;
- }
- /* If no explicit maxpipes configuration - default to 1 */
- /* TODO(RECLAYER): Should we ask the record layer how many pipes it supports? */
- if (maxpipes <= 0)
- maxpipes = 1;
-#if 0
- /* TODO(RECLAYER): FIX ME */
- if (maxpipes == 0
- || s->enc_write_ctx == NULL
- || (EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(s->enc_write_ctx))
- & EVP_CIPH_FLAG_PIPELINE) == 0
- || !SSL_USE_EXPLICIT_IV(s))
- maxpipes = 1;
-#endif
+
if (max_send_fragment == 0
|| split_send_fragment == 0
|| split_send_fragment > max_send_fragment) {
return -1;
}
+ /*
+ * Some servers hang if initial client hello is larger than 256 bytes
+ * and record version number > TLS 1.0
+ */
+ recversion = (s->version == TLS1_3_VERSION) ? TLS1_2_VERSION : s->version;
+ if (SSL_get_state(ssl) == TLS_ST_CW_CLNT_HELLO
+ && !s->renegotiate
+ && TLS1_get_version(ssl) > TLS1_VERSION
+ && s->hello_retry_request == SSL_HRR_NONE)
+ recversion = TLS1_VERSION;
+
for (;;) {
size_t tmppipelen, remain;
- size_t numpipes, j, lensofar = 0;
+ size_t j, lensofar = 0;
- if (n == 0)
- numpipes = 1;
- else
- numpipes = ((n - 1) / split_send_fragment) + 1;
- if (numpipes > maxpipes)
- numpipes = maxpipes;
+ /*
+ * Ask the record layer how it would like to split the amount of data
+ * that we have, and how many of those records it would like in one go.
+ */
+ maxpipes = s->rlayer.wrlmethod->get_max_records(s->rlayer.wrl, type, n,
+ max_send_fragment,
+ &split_send_fragment);
+ /*
+ * If max_pipelines is 0 then this means "undefined" and we default to
+ * whatever the record layer wants to do. Otherwise we use the smallest
+ * value from the number requested by the record layer, and max number
+ * configured by the user.
+ */
+ if (s->max_pipelines > 0 && maxpipes > s->max_pipelines)
+ maxpipes = s->max_pipelines;
+
+ if (maxpipes > SSL_MAX_PIPELINES)
+ maxpipes = SSL_MAX_PIPELINES;
+
+ if (split_send_fragment > max_send_fragment) {
+ SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
+ return -1;
+ }
- if (n / numpipes >= max_send_fragment) {
+ if (n / maxpipes >= split_send_fragment) {
/*
* We have enough data to completely fill all available
* pipelines
*/
- for (j = 0; j < numpipes; j++) {
+ for (j = 0; j < maxpipes; j++) {
tmpls[j].type = type;
- tmpls[j].buf = &(buf[tot]) + (j * max_send_fragment);
- tmpls[j].buflen = max_send_fragment;
+ tmpls[j].version = recversion;
+ tmpls[j].buf = &(buf[tot]) + (j * split_send_fragment);
+ tmpls[j].buflen = split_send_fragment;
}
/* Remember how much data we are going to be sending */
- s->rlayer.wpend_tot = numpipes * max_send_fragment;
+ s->rlayer.wpend_tot = maxpipes * split_send_fragment;
} else {
/* We can partially fill all available pipelines */
- tmppipelen = n / numpipes;
- remain = n % numpipes;
+ tmppipelen = n / maxpipes;
+ remain = n % maxpipes;
/*
* If there is a remainder we add an extra byte to the first few
* pipelines
*/
if (remain > 0)
tmppipelen++;
- for (j = 0; j < numpipes; j++) {
+ for (j = 0; j < maxpipes; j++) {
tmpls[j].type = type;
+ tmpls[j].version = recversion;
tmpls[j].buf = &(buf[tot]) + lensofar;
tmpls[j].buflen = tmppipelen;
lensofar += tmppipelen;
s->rlayer.wpend_tot = n;
}
- i = s->rlayer.wrlmethod->write_records(s->rlayer.wrl, tmpls, numpipes);
+ i = HANDLE_RLAYER_WRITE_RETURN(s,
+ s->rlayer.wrlmethod->write_records(s->rlayer.wrl, tmpls, maxpipes));
if (i <= 0) {
/* SSLfatal() already called if appropriate */
s->rlayer.wnum = tot;
return i;
}
- if (s->rlayer.wpend_tot == n ||
- (type == SSL3_RT_APPLICATION_DATA &&
- (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE))) {
- if (s->rlayer.wpend_tot == n
- && (s->mode & SSL_MODE_RELEASE_BUFFERS) != 0
- && !SSL_CONNECTION_IS_DTLS(s))
- ssl3_release_write_buffer(s);
-
+ if (s->rlayer.wpend_tot == n
+ || (type == SSL3_RT_APPLICATION_DATA
+ && (s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE) != 0)) {
*written = tot + s->rlayer.wpend_tot;
s->rlayer.wpend_tot = 0;
return 1;
}
}
-int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int ret, char *file,
- int line)
+int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
+ char *file, int line)
{
SSL *ssl = SSL_CONNECTION_GET_SSL(s);
if (ret == OSSL_RECORD_RETURN_RETRY) {
- s->rwstate = SSL_READING;
+ s->rwstate = writing ? SSL_WRITING : SSL_READING;
ret = -1;
} else {
s->rwstate = SSL_NOTHING;
if (ret == OSSL_RECORD_RETURN_EOF) {
- if (s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) {
+ if (writing) {
+ /*
+ * This shouldn't happen with a writing operation. We treat it
+ * as fatal.
+ */
+ ERR_new();
+ ERR_set_debug(file, line, 0);
+ ossl_statem_fatal(s, SSL_AD_INTERNAL_ERROR,
+ ERR_R_INTERNAL_ERROR, NULL);
+ ret = OSSL_RECORD_RETURN_FATAL;
+ } else if ((s->options & SSL_OP_IGNORE_UNEXPECTED_EOF) != 0) {
SSL_set_shutdown(ssl, SSL_RECEIVED_SHUTDOWN);
s->s3.warn_alert = SSL_AD_CLOSE_NOTIFY;
} else {
do {
rr = &s->rlayer.tlsrecs[s->rlayer.num_recs];
- ret = HANDLE_RLAYER_RETURN(s,
+ ret = HANDLE_RLAYER_READ_RETURN(s,
s->rlayer.rrlmethod->read_record(s->rlayer.rrl,
&rr->rechandle,
&rr->version, &rr->type,
* doing a handshake for the first time
*/
if (SSL_in_init(ssl) && type == SSL3_RT_APPLICATION_DATA
- && s->enc_read_ctx == NULL) {
+ && SSL_IS_FIRST_HANDSHAKE(s)) {
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_APP_DATA_IN_HANDSHAKE);
return -1;
}
}
}
-void ssl3_record_sequence_update(unsigned char *seq)
-{
- int i;
-
- for (i = 7; i >= 0; i--) {
- ++seq[i];
- if (seq[i] != 0)
- break;
- }
-}
-
/*
* Returns true if the current rrec was sent in SSLv2 backwards compatible
* format and false otherwise.
return ssl_security(s, op, bits, nid, other);
}
+static OSSL_FUNC_rlayer_padding_fn rlayer_padding_wrapper;
+static size_t rlayer_padding_wrapper(void *cbarg, int type, size_t len)
+{
+ SSL_CONNECTION *s = cbarg;
+ SSL *ssl = SSL_CONNECTION_GET_SSL(s);
+
+ return s->rlayer.record_padding_cb(ssl, type, len,
+ s->rlayer.record_padding_arg);
+}
+
static const OSSL_DISPATCH rlayer_dispatch[] = {
{ OSSL_FUNC_RLAYER_SKIP_EARLY_DATA, (void (*)(void))ossl_statem_skip_early_data },
{ 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 }
};
static const OSSL_RECORD_METHOD *ssl_select_next_record_layer(SSL_CONNECTION *s,
+ int direction,
int level)
{
#endif
/* Default to the current OSSL_RECORD_METHOD */
- return s->rlayer.rrlmethod;
+ return direction == OSSL_RECORD_DIRECTION_READ ? s->rlayer.rrlmethod
+ : s->rlayer.wrlmethod;
}
static int ssl_post_record_layer_select(SSL_CONNECTION *s, int direction)
{
OSSL_PARAM options[5], *opts = options;
OSSL_PARAM settings[6], *set = settings;
- const OSSL_RECORD_METHOD *origmeth = s->rlayer.rrlmethod;
const OSSL_RECORD_METHOD **thismethod;
- OSSL_RECORD_LAYER **thisrl;
+ OSSL_RECORD_LAYER **thisrl, *newrl = NULL;
BIO *thisbio;
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
const OSSL_RECORD_METHOD *meth;
int use_etm, stream_mac = 0, tlstree = 0;
- unsigned int maxfrag = SSL3_RT_MAX_PLAIN_LENGTH;
+ unsigned int maxfrag = (direction == OSSL_RECORD_DIRECTION_WRITE)
+ ? ssl_get_max_send_fragment(s)
+ : SSL3_RT_MAX_PLAIN_LENGTH;
int use_early_data = 0;
uint32_t max_early_data;
+ COMP_METHOD *compm = (comp == NULL) ? NULL : comp->method;
- meth = ssl_select_next_record_layer(s, level);
+ meth = ssl_select_next_record_layer(s, direction, level);
if (direction == OSSL_RECORD_DIRECTION_READ) {
thismethod = &s->rlayer.rrlmethod;
thisbio = s->wbio;
}
- if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) {
- ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
- return 0;
- }
-
- *thisrl = NULL;
- if (meth != NULL)
- *thismethod = meth;
+ if (meth == NULL)
+ meth = *thismethod;
- if (!ossl_assert(*thismethod != NULL)) {
+ if (!ossl_assert(meth != NULL)) {
ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
return 0;
}
&s->rlayer.default_read_buf_len);
*opts++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_READ_AHEAD,
&s->rlayer.read_ahead);
+ } else {
+ *opts++ = OSSL_PARAM_construct_size_t(OSSL_LIBSSL_RECORD_LAYER_PARAM_BLOCK_PADDING,
+ &s->rlayer.block_padding);
}
*opts = OSSL_PARAM_construct_end();
*set++ = OSSL_PARAM_construct_int(OSSL_LIBSSL_RECORD_LAYER_PARAM_TLSTREE,
&tlstree);
- if (s->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
+ /*
+ * We only need to do this for the read side. The write side should already
+ * have the correct value due to the ssl_get_max_send_fragment() call above
+ */
+ if (direction == OSSL_RECORD_DIRECTION_READ
+ && s->session != NULL
+ && USE_MAX_FRAGMENT_LENGTH_EXT(s->session))
maxfrag = GET_MAX_FRAGMENT_LENGTH(s->session);
+
if (maxfrag != SSL3_RT_MAX_PLAIN_LENGTH)
*set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_FRAG_LEN,
&maxfrag);
* using the early keys. A server also needs to worry about rejected early
* data that might arrive when the handshake keys are in force.
*/
- /* TODO(RECLAYER): Check this when doing the "write" record layer */
if (s->server && direction == OSSL_RECORD_DIRECTION_READ) {
use_early_data = (level == OSSL_RECORD_PROTECTION_LEVEL_EARLY
|| level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE);
max_early_data = ossl_get_max_early_data(s);
if (max_early_data != 0)
- *set++ = OSSL_PARAM_construct_uint(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA,
- &max_early_data);
+ *set++ = OSSL_PARAM_construct_uint32(OSSL_LIBSSL_RECORD_LAYER_PARAM_MAX_EARLY_DATA,
+ &max_early_data);
}
*set = OSSL_PARAM_construct_end();
int rlret;
BIO *prev = NULL;
BIO *next = NULL;
- unsigned int epoch = 0;;
-
+ unsigned int epoch = 0;
+ OSSL_DISPATCH rlayer_dispatch_tmp[OSSL_NELEM(rlayer_dispatch)];
+ size_t i, j;
if (direction == OSSL_RECORD_DIRECTION_READ) {
prev = s->rlayer.rrlnext;
return 0;
}
s->rlayer.rrlnext = next;
+ } 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 */
}
- rlret = (*thismethod)->new_record_layer(sctx->libctx,
- sctx->propq,
- version, s->server,
- direction, level, epoch,
- key, keylen, iv, ivlen,
- mackey, mackeylen, ciph,
- taglen, mactype, md, comp,
- prev, thisbio,
- next, NULL,
- NULL, settings, options,
- rlayer_dispatch, s,
- thisrl);
+ /*
+ * Create a copy of the dispatch array, missing out wrappers for
+ * callbacks that we don't need.
+ */
+ for (i = 0, j = 0; i < OSSL_NELEM(rlayer_dispatch); i++) {
+ switch (rlayer_dispatch[i].function_id) {
+ case OSSL_FUNC_RLAYER_MSG_CALLBACK:
+ if (s->msg_callback == NULL)
+ continue;
+ break;
+ case OSSL_FUNC_RLAYER_PADDING:
+ if (s->rlayer.record_padding_cb == NULL)
+ continue;
+ break;
+ default:
+ break;
+ }
+ rlayer_dispatch_tmp[j++] = rlayer_dispatch[i];
+ }
+
+ rlret = meth->new_record_layer(sctx->libctx, sctx->propq, version,
+ s->server, direction, level, epoch,
+ key, keylen, iv, ivlen, mackey,
+ mackeylen, ciph, taglen, mactype, md,
+ compm, prev, thisbio, next, NULL, NULL,
+ settings, options, rlayer_dispatch_tmp,
+ s, &newrl);
BIO_free(prev);
switch (rlret) {
case OSSL_RECORD_RETURN_FATAL:
return 0;
case OSSL_RECORD_RETURN_NON_FATAL_ERR:
- if (*thismethod != origmeth && origmeth != NULL) {
+ if (*thismethod != meth && *thismethod != NULL) {
/*
* We tried a new record layer method, but it didn't work out,
* so we fallback to the original method and try again
*/
- *thismethod = origmeth;
+ meth = *thismethod;
continue;
}
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_RECORD_LAYER);
break;
}
+ /*
+ * Free the old record layer if we have one except in the case of DTLS when
+ * writing and there are still buffered sent messages in our queue. In that
+ * case the record layer is still referenced by those buffered messages for
+ * potential retransmit. Only when those buffered messages get freed do we
+ * free the record layer object (see dtls1_hm_fragment_free)
+ */
+ if (!SSL_CONNECTION_IS_DTLS(s)
+ || direction == OSSL_RECORD_DIRECTION_READ
+ || pqueue_peek(s->d1->sent_messages) == NULL) {
+ if (*thismethod != NULL && !(*thismethod)->free(*thisrl)) {
+ SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
+ return 0;
+ }
+ }
+
+ *thisrl = newrl;
+ *thismethod = meth;
+
return ssl_post_record_layer_select(s, direction);
}
+
+int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers)
+{
+ if (!ossl_assert(s->rlayer.rrlmethod != NULL)
+ || !ossl_assert(s->rlayer.wrlmethod != NULL))
+ return 0;
+ s->rlayer.rrlmethod->set_protocol_version(s->rlayer.rrl, s->version);
+ s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, s->version);
+
+ return 1;
+}