2022-05-09 19:00:54 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2022 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
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include "../../ssl_local.h"
|
|
|
|
#include "../record_local.h"
|
|
|
|
#include "recmethod_local.h"
|
|
|
|
|
|
|
|
static int tls13_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
|
|
|
|
unsigned char *key, size_t keylen,
|
|
|
|
unsigned char *iv, size_t ivlen,
|
|
|
|
unsigned char *mackey, size_t mackeylen,
|
|
|
|
const EVP_CIPHER *ciph,
|
|
|
|
size_t taglen,
|
|
|
|
int mactype,
|
|
|
|
const EVP_MD *md,
|
2022-09-15 23:03:02 +08:00
|
|
|
COMP_METHOD *comp)
|
2022-05-09 19:00:54 +08:00
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX *ciph_ctx;
|
|
|
|
int mode;
|
2022-09-17 00:34:40 +08:00
|
|
|
int enc = (rl->direction == OSSL_RECORD_DIRECTION_WRITE) ? 1 : 0;
|
2022-05-09 19:00:54 +08:00
|
|
|
|
|
|
|
if (ivlen > sizeof(rl->iv)) {
|
2022-05-12 23:35:52 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return OSSL_RECORD_RETURN_FATAL;
|
2022-05-09 19:00:54 +08:00
|
|
|
}
|
|
|
|
memcpy(rl->iv, iv, ivlen);
|
|
|
|
|
2022-05-26 00:19:33 +08:00
|
|
|
ciph_ctx = rl->enc_ctx = EVP_CIPHER_CTX_new();
|
2022-05-09 19:00:54 +08:00
|
|
|
if (ciph_ctx == NULL) {
|
2022-05-12 23:35:52 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return OSSL_RECORD_RETURN_FATAL;
|
2022-05-09 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mode = EVP_CIPHER_get_mode(ciph);
|
|
|
|
|
2022-09-17 00:34:40 +08:00
|
|
|
if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, enc) <= 0
|
2022-07-27 21:20:23 +08:00
|
|
|
|| EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen,
|
|
|
|
NULL) <= 0
|
2022-05-09 19:00:54 +08:00
|
|
|
|| (mode == EVP_CIPH_CCM_MODE
|
2022-07-27 21:20:23 +08:00
|
|
|
&& EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
|
|
|
|
NULL) <= 0)
|
2022-09-17 00:34:40 +08:00
|
|
|
|| EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, enc) <= 0) {
|
2022-05-12 23:35:52 +08:00
|
|
|
ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
|
|
|
|
return OSSL_RECORD_RETURN_FATAL;
|
2022-05-09 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
2022-05-12 23:35:52 +08:00
|
|
|
return OSSL_RECORD_RETURN_SUCCESS;
|
2022-05-09 19:00:54 +08:00
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int tls13_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
|
|
|
|
size_t n_recs, int sending, SSL_MAC_BUF *mac,
|
|
|
|
size_t macsize)
|
2022-05-09 19:00:54 +08:00
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX *ctx;
|
|
|
|
unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
|
|
|
|
size_t ivlen, offset, loop, hdrlen;
|
|
|
|
unsigned char *staticiv;
|
2022-05-24 23:00:50 +08:00
|
|
|
unsigned char *seq = rl->sequence;
|
2022-05-09 19:00:54 +08:00
|
|
|
int lenu, lenf;
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD *rec = &recs[0];
|
2022-05-09 19:00:54 +08:00
|
|
|
WPACKET wpkt;
|
|
|
|
const EVP_CIPHER *cipher;
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
if (n_recs != 1) {
|
|
|
|
/* Should not happen */
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-26 00:19:33 +08:00
|
|
|
ctx = rl->enc_ctx;
|
|
|
|
staticiv = rl->iv;
|
2022-05-09 19:00:54 +08:00
|
|
|
|
|
|
|
cipher = EVP_CIPHER_CTX_get0_cipher(ctx);
|
|
|
|
if (cipher == NULL) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mode = EVP_CIPHER_get_mode(cipher);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're sending an alert and ctx != NULL then we must be forcing
|
|
|
|
* plaintext alerts. If we're reading and ctx != NULL then we allow
|
|
|
|
* plaintext alerts at certain points in the handshake. If we've got this
|
|
|
|
* far then we have already validated that a plaintext alert is ok here.
|
|
|
|
*/
|
|
|
|
if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
|
|
|
|
memmove(rec->data, rec->input, rec->length);
|
|
|
|
rec->input = rec->data;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
|
|
|
|
|
|
|
|
if (!sending) {
|
|
|
|
/*
|
|
|
|
* Take off tag. There must be at least one byte of content type as
|
|
|
|
* well as the tag
|
|
|
|
*/
|
|
|
|
if (rec->length < rl->taglen + 1)
|
|
|
|
return 0;
|
|
|
|
rec->length -= rl->taglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up IV */
|
|
|
|
if (ivlen < SEQ_NUM_SIZE) {
|
|
|
|
/* Should not happen */
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
offset = ivlen - SEQ_NUM_SIZE;
|
|
|
|
memcpy(iv, staticiv, offset);
|
|
|
|
for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
|
|
|
|
iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
|
|
|
|
|
2022-10-17 19:28:07 +08:00
|
|
|
if (!tls_increment_sequence_ctr(rl)) {
|
|
|
|
/* RLAYERfatal already called */
|
2022-05-09 19:00:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
|
|
|
|
|| (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
|
|
|
|
rl->taglen,
|
|
|
|
rec->data + rec->length) <= 0)) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the AAD */
|
|
|
|
if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
|
|
|
|
|| !WPACKET_put_bytes_u8(&wpkt, rec->type)
|
|
|
|
|| !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
|
|
|
|
|| !WPACKET_put_bytes_u16(&wpkt, rec->length + rl->taglen)
|
|
|
|
|| !WPACKET_get_total_written(&wpkt, &hdrlen)
|
|
|
|
|| hdrlen != SSL3_RT_HEADER_LENGTH
|
|
|
|
|| !WPACKET_finish(&wpkt)) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
WPACKET_cleanup(&wpkt);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For CCM we must explicitly set the total plaintext length before we add
|
|
|
|
* any AAD.
|
|
|
|
*/
|
|
|
|
if ((mode == EVP_CIPH_CCM_MODE
|
|
|
|
&& EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
|
|
|
|
(unsigned int)rec->length) <= 0)
|
|
|
|
|| EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
|
|
|
|
sizeof(recheader)) <= 0
|
|
|
|
|| EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
|
|
|
|
(unsigned int)rec->length) <= 0
|
|
|
|
|| EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
|
|
|
|
|| (size_t)(lenu + lenf) != rec->length) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (sending) {
|
|
|
|
/* Add the tag */
|
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, rl->taglen,
|
|
|
|
rec->data + rec->length) <= 0) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rec->length += rl->taglen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int tls13_validate_record_header(OSSL_RECORD_LAYER *rl,
|
|
|
|
TLS_RL_RECORD *rec)
|
2022-05-13 00:21:25 +08:00
|
|
|
{
|
|
|
|
if (rec->type != SSL3_RT_APPLICATION_DATA
|
|
|
|
&& (rec->type != SSL3_RT_CHANGE_CIPHER_SPEC
|
|
|
|
|| !rl->is_first_handshake)
|
|
|
|
&& (rec->type != SSL3_RT_ALERT || !rl->allow_plain_alerts)) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_BAD_RECORD_TYPE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rec->rec_version != TLS1_2_VERSION) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_DECODE_ERROR, SSL_R_WRONG_VERSION_NUMBER);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rec->length > SSL3_RT_MAX_TLS13_ENCRYPTED_LENGTH) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW,
|
|
|
|
SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-02 23:27:09 +08:00
|
|
|
static int tls13_post_process_record(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec)
|
2022-05-13 00:21:25 +08:00
|
|
|
{
|
|
|
|
/* Skip this if we've received a plaintext alert */
|
|
|
|
if (rec->type != SSL3_RT_ALERT) {
|
|
|
|
size_t end;
|
|
|
|
|
|
|
|
if (rec->length == 0
|
|
|
|
|| rec->type != SSL3_RT_APPLICATION_DATA) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_UNEXPECTED_MESSAGE,
|
|
|
|
SSL_R_BAD_RECORD_TYPE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip trailing padding */
|
2022-07-27 21:20:23 +08:00
|
|
|
for (end = rec->length - 1; end > 0 && rec->data[end] == 0; end--)
|
2022-05-13 00:21:25 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
rec->length = end;
|
|
|
|
rec->type = rec->data[end];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rec->length > SSL3_RT_MAX_PLAIN_LENGTH) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_RECORD_OVERFLOW, SSL_R_DATA_LENGTH_TOO_LONG);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-26 00:30:33 +08:00
|
|
|
if (!tls13_common_post_process_record(rl, rec)) {
|
2022-05-13 00:21:25 +08:00
|
|
|
/* RLAYERfatal already called */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-08-24 16:34:53 +08:00
|
|
|
static uint8_t tls13_get_record_type(OSSL_RECORD_LAYER *rl,
|
|
|
|
OSSL_RECORD_TEMPLATE *template)
|
2022-09-27 00:07:02 +08:00
|
|
|
{
|
|
|
|
if (rl->allow_plain_alerts && template->type == SSL3_RT_ALERT)
|
2023-08-24 16:34:53 +08:00
|
|
|
return SSL3_RT_ALERT;
|
2022-09-27 00:07:02 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Aside from the above case we always use the application data record type
|
|
|
|
* when encrypting in TLSv1.3. The "inner" record type encodes the "real"
|
|
|
|
* record type from the template.
|
|
|
|
*/
|
|
|
|
return SSL3_RT_APPLICATION_DATA;
|
|
|
|
}
|
|
|
|
|
2022-09-27 22:32:22 +08:00
|
|
|
static int tls13_add_record_padding(OSSL_RECORD_LAYER *rl,
|
|
|
|
OSSL_RECORD_TEMPLATE *thistempl,
|
|
|
|
WPACKET *thispkt,
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD *thiswr)
|
2022-09-27 22:32:22 +08:00
|
|
|
{
|
|
|
|
size_t rlen;
|
|
|
|
|
|
|
|
/* Nothing to be done in the case of a plaintext alert */
|
|
|
|
if (rl->allow_plain_alerts && thistempl->type != SSL3_RT_ALERT)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!WPACKET_put_bytes_u8(thispkt, thistempl->type)) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD_add_length(thiswr, 1);
|
2022-09-27 22:32:22 +08:00
|
|
|
|
|
|
|
/* Add TLS1.3 padding */
|
2022-11-02 23:27:09 +08:00
|
|
|
rlen = TLS_RL_RECORD_get_length(thiswr);
|
2022-09-27 22:32:22 +08:00
|
|
|
if (rlen < rl->max_frag_len) {
|
|
|
|
size_t padding = 0;
|
|
|
|
size_t max_padding = rl->max_frag_len - rlen;
|
|
|
|
|
|
|
|
if (rl->padding != NULL) {
|
|
|
|
padding = rl->padding(rl->cbarg, thistempl->type, rlen);
|
|
|
|
} else if (rl->block_padding > 0) {
|
|
|
|
size_t mask = rl->block_padding - 1;
|
|
|
|
size_t remainder;
|
|
|
|
|
|
|
|
/* optimize for power of 2 */
|
|
|
|
if ((rl->block_padding & mask) == 0)
|
|
|
|
remainder = rlen & mask;
|
|
|
|
else
|
|
|
|
remainder = rlen % rl->block_padding;
|
|
|
|
/* don't want to add a block of padding if we don't have to */
|
|
|
|
if (remainder == 0)
|
|
|
|
padding = 0;
|
|
|
|
else
|
|
|
|
padding = rl->block_padding - remainder;
|
|
|
|
}
|
|
|
|
if (padding > 0) {
|
|
|
|
/* do not allow the record to exceed max plaintext length */
|
|
|
|
if (padding > max_padding)
|
|
|
|
padding = max_padding;
|
|
|
|
if (!WPACKET_memset(thispkt, 0, padding)) {
|
|
|
|
RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-11-02 23:27:09 +08:00
|
|
|
TLS_RL_RECORD_add_length(thiswr, padding);
|
2022-09-27 22:32:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-05-09 19:00:54 +08:00
|
|
|
struct record_functions_st tls_1_3_funcs = {
|
|
|
|
tls13_set_crypto_state,
|
|
|
|
tls13_cipher,
|
2022-05-13 00:21:25 +08:00
|
|
|
NULL,
|
|
|
|
tls_default_set_protocol_version,
|
2022-09-12 22:50:26 +08:00
|
|
|
tls_default_read_n,
|
|
|
|
tls_get_more_records,
|
2022-05-13 00:21:25 +08:00
|
|
|
tls13_validate_record_header,
|
2022-09-12 22:50:26 +08:00
|
|
|
tls13_post_process_record,
|
|
|
|
tls_get_max_records_default,
|
2022-09-26 23:35:30 +08:00
|
|
|
tls_write_records_default,
|
|
|
|
tls_allocate_write_buffers_default,
|
2022-09-27 00:07:02 +08:00
|
|
|
tls_initialise_write_packets_default,
|
2022-09-27 00:44:11 +08:00
|
|
|
tls13_get_record_type,
|
2022-09-27 22:32:22 +08:00
|
|
|
tls_prepare_record_header_default,
|
2022-09-27 23:43:23 +08:00
|
|
|
tls13_add_record_padding,
|
2022-10-04 22:59:06 +08:00
|
|
|
tls_prepare_for_encryption_default,
|
2022-10-04 23:32:02 +08:00
|
|
|
tls_post_encryption_processing_default,
|
|
|
|
NULL
|
2022-05-09 19:00:54 +08:00
|
|
|
};
|