2016-11-17 18:00:17 +00:00
|
|
|
/*
|
2018-03-20 13:00:17 +00:00
|
|
|
* Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2016-11-17 18:00:17 +00:00
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (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
|
|
|
|
*/
|
|
|
|
|
2019-09-27 21:58:06 +00:00
|
|
|
#include "../ssl_local.h"
|
|
|
|
#include "record_local.h"
|
2017-08-02 13:46:31 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2016-11-17 18:00:17 +00:00
|
|
|
|
|
|
|
/*-
|
2017-12-04 16:54:59 +00:00
|
|
|
* tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
|
|
|
|
* internal errors, but not otherwise.
|
2016-11-17 18:00:17 +00:00
|
|
|
*
|
|
|
|
* Returns:
|
|
|
|
* 0: (in non-constant time) if the record is publically invalid (i.e. too
|
|
|
|
* short etc).
|
|
|
|
* 1: if the record encryption was successful.
|
|
|
|
* -1: if the record's AEAD-authenticator is invalid or, if sending,
|
|
|
|
* an internal error occurred.
|
|
|
|
*/
|
2017-04-13 14:20:04 +00:00
|
|
|
int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
|
2016-11-17 18:00:17 +00:00
|
|
|
{
|
|
|
|
EVP_CIPHER_CTX *ctx;
|
2018-02-26 12:26:14 +00:00
|
|
|
unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
|
|
|
|
size_t ivlen, taglen, offset, loop, hdrlen;
|
2016-11-17 18:00:17 +00:00
|
|
|
unsigned char *staticiv;
|
|
|
|
unsigned char *seq;
|
|
|
|
int lenu, lenf;
|
|
|
|
SSL3_RECORD *rec = &recs[0];
|
2017-02-08 09:33:44 +00:00
|
|
|
uint32_t alg_enc;
|
2018-02-26 12:26:14 +00:00
|
|
|
WPACKET wpkt;
|
2016-11-17 18:00:17 +00:00
|
|
|
|
|
|
|
if (n_recs != 1) {
|
|
|
|
/* Should not happen */
|
|
|
|
/* TODO(TLS1.3): Support pipelining */
|
2017-12-04 16:54:59 +00:00
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2016-11-17 18:00:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-13 14:20:04 +00:00
|
|
|
if (sending) {
|
2016-11-17 18:00:17 +00:00
|
|
|
ctx = s->enc_write_ctx;
|
|
|
|
staticiv = s->write_iv;
|
|
|
|
seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
|
|
|
|
} else {
|
|
|
|
ctx = s->enc_read_ctx;
|
|
|
|
staticiv = s->read_iv;
|
|
|
|
seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
|
|
|
|
}
|
|
|
|
|
2018-08-07 11:40:08 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
2016-11-17 18:00:17 +00:00
|
|
|
memmove(rec->data, rec->input, rec->length);
|
|
|
|
rec->input = rec->data;
|
|
|
|
return 1;
|
|
|
|
}
|
2017-02-08 09:33:44 +00:00
|
|
|
|
2016-11-17 18:00:17 +00:00
|
|
|
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
|
|
|
|
|
2017-03-09 15:03:07 +00:00
|
|
|
if (s->early_data_state == SSL_EARLY_DATA_WRITING
|
|
|
|
|| s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
|
2017-08-31 13:32:51 +00:00
|
|
|
if (s->session != NULL && s->session->ext.max_early_data > 0) {
|
2017-07-05 19:53:03 +00:00
|
|
|
alg_enc = s->session->cipher->algorithm_enc;
|
2017-08-31 13:32:51 +00:00
|
|
|
} else {
|
|
|
|
if (!ossl_assert(s->psksession != NULL
|
2017-12-04 16:54:59 +00:00
|
|
|
&& s->psksession->ext.max_early_data > 0)) {
|
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2017-08-31 13:32:51 +00:00
|
|
|
return -1;
|
2017-12-04 16:54:59 +00:00
|
|
|
}
|
2017-07-05 19:53:03 +00:00
|
|
|
alg_enc = s->psksession->cipher->algorithm_enc;
|
2017-08-31 13:32:51 +00:00
|
|
|
}
|
2017-02-21 09:22:22 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* To get here we must have selected a ciphersuite - otherwise ctx would
|
|
|
|
* be NULL
|
|
|
|
*/
|
2017-12-04 16:54:59 +00:00
|
|
|
if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
|
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2017-02-21 09:22:22 +00:00
|
|
|
return -1;
|
2017-12-04 16:54:59 +00:00
|
|
|
}
|
2017-02-21 09:22:22 +00:00
|
|
|
alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
|
|
|
|
}
|
2017-02-08 09:33:44 +00:00
|
|
|
|
2017-02-06 19:25:34 +00:00
|
|
|
if (alg_enc & SSL_AESCCM) {
|
|
|
|
if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
|
2017-02-03 02:44:15 +00:00
|
|
|
taglen = EVP_CCM8_TLS_TAG_LEN;
|
|
|
|
else
|
|
|
|
taglen = EVP_CCM_TLS_TAG_LEN;
|
2017-04-13 14:20:04 +00:00
|
|
|
if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
|
2017-12-04 16:54:59 +00:00
|
|
|
NULL) <= 0) {
|
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2017-02-03 02:44:15 +00:00
|
|
|
return -1;
|
2017-12-04 16:54:59 +00:00
|
|
|
}
|
2017-02-06 19:25:34 +00:00
|
|
|
} else if (alg_enc & SSL_AESGCM) {
|
2017-02-03 02:44:15 +00:00
|
|
|
taglen = EVP_GCM_TLS_TAG_LEN;
|
2017-02-06 19:25:34 +00:00
|
|
|
} else if (alg_enc & SSL_CHACHA20) {
|
|
|
|
taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
|
|
|
|
} else {
|
2017-12-04 16:54:59 +00:00
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2017-02-06 19:25:34 +00:00
|
|
|
return -1;
|
2017-02-03 02:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 14:20:04 +00:00
|
|
|
if (!sending) {
|
2016-11-17 18:00:17 +00:00
|
|
|
/*
|
|
|
|
* Take off tag. There must be at least one byte of content type as
|
|
|
|
* well as the tag
|
|
|
|
*/
|
2017-02-03 02:44:15 +00:00
|
|
|
if (rec->length < taglen + 1)
|
2016-11-17 18:00:17 +00:00
|
|
|
return 0;
|
2017-02-03 02:44:15 +00:00
|
|
|
rec->length -= taglen;
|
2016-11-17 18:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up IV */
|
|
|
|
if (ivlen < SEQ_NUM_SIZE) {
|
|
|
|
/* Should not happen */
|
2017-12-04 16:54:59 +00:00
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2016-11-17 18:00:17 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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];
|
|
|
|
|
2016-12-14 17:27:15 +00:00
|
|
|
/* Increment the sequence counter */
|
|
|
|
for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
|
|
|
|
++seq[loop - 1];
|
|
|
|
if (seq[loop - 1] != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (loop == 0) {
|
|
|
|
/* Sequence has wrapped */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-11-29 23:27:27 +00:00
|
|
|
/* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
|
2017-04-13 14:20:04 +00:00
|
|
|
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
|
|
|
|
|| (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
|
2017-02-03 02:44:15 +00:00
|
|
|
taglen,
|
2018-02-26 12:26:14 +00:00
|
|
|
rec->data + rec->length) <= 0)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 + taglen)
|
|
|
|
|| !WPACKET_get_total_written(&wpkt, &hdrlen)
|
|
|
|
|| hdrlen != SSL3_RT_HEADER_LENGTH
|
|
|
|
|| !WPACKET_finish(&wpkt)) {
|
|
|
|
WPACKET_cleanup(&wpkt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For CCM we must explicitly set the total plaintext length before we add
|
|
|
|
* any AAD.
|
|
|
|
*/
|
|
|
|
if (((alg_enc & SSL_AESCCM) != 0
|
|
|
|
&& EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
|
|
|
|
(unsigned int)rec->length) <= 0)
|
|
|
|
|| EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
|
|
|
|
sizeof(recheader)) <= 0
|
2017-02-03 02:44:15 +00:00
|
|
|
|| EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
|
|
|
|
(unsigned int)rec->length) <= 0
|
2016-11-17 18:00:17 +00:00
|
|
|
|| EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
|
|
|
|
|| (size_t)(lenu + lenf) != rec->length) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-13 14:20:04 +00:00
|
|
|
if (sending) {
|
2016-11-17 18:00:17 +00:00
|
|
|
/* Add the tag */
|
2017-02-03 02:44:15 +00:00
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
|
2017-12-04 16:54:59 +00:00
|
|
|
rec->data + rec->length) <= 0) {
|
|
|
|
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
|
|
|
|
ERR_R_INTERNAL_ERROR);
|
2016-11-17 18:00:17 +00:00
|
|
|
return -1;
|
2017-12-04 16:54:59 +00:00
|
|
|
}
|
2017-02-03 02:44:15 +00:00
|
|
|
rec->length += taglen;
|
2016-11-17 18:00:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|