* https://www.openssl.org/source/license.html
*/
+#include <assert.h>
#include "../ssl_locl.h"
#include "record_locl.h"
{
EVP_CIPHER_CTX *ctx;
unsigned char iv[EVP_MAX_IV_LENGTH];
- size_t ivlen, offset, loop;
+ size_t ivlen, taglen, offset, loop;
unsigned char *staticiv;
unsigned char *seq;
int lenu, lenf;
SSL3_RECORD *rec = &recs[0];
+ uint32_t alg_enc;
if (n_recs != 1) {
/* Should not happen */
rec->input = rec->data;
return 1;
}
+
ivlen = EVP_CIPHER_CTX_iv_length(ctx);
+ if (s->early_data_state == SSL_EARLY_DATA_WRITING) {
+ alg_enc = s->session->cipher->algorithm_enc;
+ } else {
+ /*
+ * To get here we must have selected a ciphersuite - otherwise ctx would
+ * be NULL
+ */
+ assert(s->s3->tmp.new_cipher != NULL);
+ if (s->s3->tmp.new_cipher == NULL)
+ return -1;
+ alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
+ }
+
+ if (alg_enc & SSL_AESCCM) {
+ if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
+ taglen = EVP_CCM8_TLS_TAG_LEN;
+ else
+ taglen = EVP_CCM_TLS_TAG_LEN;
+ if (send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
+ NULL) <= 0)
+ return -1;
+ } else if (alg_enc & SSL_AESGCM) {
+ taglen = EVP_GCM_TLS_TAG_LEN;
+ } else if (alg_enc & SSL_CHACHA20) {
+ taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
+ } else {
+ return -1;
+ }
+
if (!send) {
/*
* Take off tag. There must be at least one byte of content type as
* well as the tag
*/
- /*
- * TODO(TLS1.3): We're going to need to figure out the tag len based on
- * the cipher. For now we just support GCM tags.
- * TODO(TLS1.3): When we've swapped over the record layer to TLSv1.3
- * then the length must be 1 + the tag len to account for the content
- * byte that we know must have been encrypted.
- */
- if (rec->length < EVP_GCM_TLS_TAG_LEN)
+ if (rec->length < taglen + 1)
return 0;
- rec->length -= EVP_GCM_TLS_TAG_LEN;
+ rec->length -= taglen;
}
/* Set up IV */
for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
- /* TODO(size_t): lenu/lenf should be a size_t but EVP can't support it */
+ /* 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;
+ }
+
+ /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, send) <= 0
- || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
- (unsigned int)rec->length) <= 0
|| (!send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
- EVP_GCM_TLS_TAG_LEN,
+ taglen,
rec->data + rec->length) <= 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 -1;
}
-
if (send) {
/* Add the tag */
- if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, EVP_GCM_TLS_TAG_LEN,
+ if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
rec->data + rec->length) <= 0)
return -1;
- rec->length += EVP_GCM_TLS_TAG_LEN;
+ rec->length += taglen;
}
return 1;