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