Move record padding out of tls_common.c
[openssl.git] / ssl / record / ssl3_record_tls13.c
1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <assert.h>
11 #include "../ssl_local.h"
12 #include "record_local.h"
13 #include "internal/cryptlib.h"
14
15 /*-
16  * tls13_enc encrypts/decrypts |n_recs| in |recs|. Calls SSLfatal on internal
17  * error, but not otherwise. It is the responsibility of the caller to report
18  * a bad_record_mac.
19  *
20  * Returns:
21  *    0: On failure
22  *    1: if the record encryption/decryption was successful.
23  */
24 int tls13_enc(SSL_CONNECTION *s, SSL3_RECORD *recs, size_t n_recs, int sending,
25               ossl_unused SSL_MAC_BUF *mac, ossl_unused size_t macsize)
26 {
27     EVP_CIPHER_CTX *ctx;
28     unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
29     size_t taglen, offset, loop, hdrlen;
30     int ivlen;
31     unsigned char *staticiv;
32     unsigned char *seq;
33     int lenu, lenf;
34     SSL3_RECORD *rec = &recs[0];
35     uint32_t alg_enc;
36     WPACKET wpkt;
37
38     if (n_recs != 1) {
39         /* Should not happen */
40         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
41         return 0;
42     }
43
44     assert(sending);
45     ctx = s->enc_write_ctx;
46     staticiv = s->write_iv;
47     seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
48
49     /*
50      * If we're sending an alert and ctx != NULL then we must be forcing
51      * plaintext alerts. If we're reading and ctx != NULL then we allow
52      * plaintext alerts at certain points in the handshake. If we've got this
53      * far then we have already validated that a plaintext alert is ok here.
54      */
55     if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
56         memmove(rec->data, rec->input, rec->length);
57         rec->input = rec->data;
58         return 1;
59     }
60
61     ivlen = EVP_CIPHER_CTX_get_iv_length(ctx);
62     if (ivlen < 0) {
63         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
64         return 0;
65     }
66
67     if (s->early_data_state == SSL_EARLY_DATA_WRITING
68             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
69         if (s->session != NULL && s->session->ext.max_early_data > 0) {
70             alg_enc = s->session->cipher->algorithm_enc;
71         } else {
72             if (!ossl_assert(s->psksession != NULL
73                              && s->psksession->ext.max_early_data > 0)) {
74                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
75                 return 0;
76             }
77             alg_enc = s->psksession->cipher->algorithm_enc;
78         }
79     } else {
80         /*
81          * To get here we must have selected a ciphersuite - otherwise ctx would
82          * be NULL
83          */
84         if (!ossl_assert(s->s3.tmp.new_cipher != NULL)) {
85             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
86             return 0;
87         }
88         alg_enc = s->s3.tmp.new_cipher->algorithm_enc;
89     }
90
91     if (alg_enc & SSL_AESCCM) {
92         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
93             taglen = EVP_CCM8_TLS_TAG_LEN;
94          else
95             taglen = EVP_CCM_TLS_TAG_LEN;
96          if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,  NULL) <= 0) {
97             SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
98             return 0;
99         }
100     } else if (alg_enc & SSL_AESGCM) {
101         taglen = EVP_GCM_TLS_TAG_LEN;
102     } else if (alg_enc & SSL_CHACHA20) {
103         taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
104     } else {
105         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
106         return 0;
107     }
108
109     /* Set up IV */
110     if (ivlen < SEQ_NUM_SIZE) {
111         /* Should not happen */
112         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
113         return 0;
114     }
115     offset = ivlen - SEQ_NUM_SIZE;
116     memcpy(iv, staticiv, offset);
117     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
118         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
119
120     /* Increment the sequence counter */
121     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
122         ++seq[loop - 1];
123         if (seq[loop - 1] != 0)
124             break;
125     }
126     if (loop == 0) {
127         /* Sequence has wrapped */
128         return 0;
129     }
130
131     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0) {
132         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
133         return 0;
134     }
135
136     /* Set up the AAD */
137     if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
138             || !WPACKET_put_bytes_u8(&wpkt, rec->type)
139             || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
140             || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
141             || !WPACKET_get_total_written(&wpkt, &hdrlen)
142             || hdrlen != SSL3_RT_HEADER_LENGTH
143             || !WPACKET_finish(&wpkt)) {
144         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
145         WPACKET_cleanup(&wpkt);
146         return 0;
147     }
148
149     /*
150      * For CCM we must explicitly set the total plaintext length before we add
151      * any AAD.
152      */
153     if (((alg_enc & SSL_AESCCM) != 0
154                  && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
155                                      (unsigned int)rec->length) <= 0)
156             || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
157                                 sizeof(recheader)) <= 0
158             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
159                                 (unsigned int)rec->length) <= 0
160             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
161             || (size_t)(lenu + lenf) != rec->length) {
162         return 0;
163     }
164
165     /* Add the tag */
166     if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
167                             rec->data + rec->length) <= 0) {
168         SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
169         return 0;
170     }
171     rec->length += taglen;
172
173     return 1;
174 }