Update copyright year
[openssl.git] / ssl / record / ssl3_record_tls13.c
1 /*
2  * Copyright 2016-2020 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 *s, SSL3_RECORD *recs, size_t n_recs, int sending,
24               SSL_MAC_BUF *mac, 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 ivlen, taglen, offset, loop, hdrlen;
29     unsigned char *staticiv;
30     unsigned char *seq;
31     int lenu, lenf;
32     SSL3_RECORD *rec = &recs[0];
33     uint32_t alg_enc;
34     WPACKET wpkt;
35
36     if (n_recs != 1) {
37         /* Should not happen */
38         /* TODO(TLS1.3): Support pipelining */
39         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
40                  ERR_R_INTERNAL_ERROR);
41         return 0;
42     }
43
44     if (sending) {
45         ctx = s->enc_write_ctx;
46         staticiv = s->write_iv;
47         seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
48     } else {
49         ctx = s->enc_read_ctx;
50         staticiv = s->read_iv;
51         seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
52     }
53
54     /*
55      * If we're sending an alert and ctx != NULL then we must be forcing
56      * plaintext alerts. If we're reading and ctx != NULL then we allow
57      * plaintext alerts at certain points in the handshake. If we've got this
58      * far then we have already validated that a plaintext alert is ok here.
59      */
60     if (ctx == NULL || rec->type == SSL3_RT_ALERT) {
61         memmove(rec->data, rec->input, rec->length);
62         rec->input = rec->data;
63         return 1;
64     }
65
66     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
67
68     if (s->early_data_state == SSL_EARLY_DATA_WRITING
69             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
70         if (s->session != NULL && s->session->ext.max_early_data > 0) {
71             alg_enc = s->session->cipher->algorithm_enc;
72         } else {
73             if (!ossl_assert(s->psksession != NULL
74                              && s->psksession->ext.max_early_data > 0)) {
75                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
76                          ERR_R_INTERNAL_ERROR);
77                 return 0;
78             }
79             alg_enc = s->psksession->cipher->algorithm_enc;
80         }
81     } else {
82         /*
83          * To get here we must have selected a ciphersuite - otherwise ctx would
84          * be NULL
85          */
86         if (!ossl_assert(s->s3.tmp.new_cipher != NULL)) {
87             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
88                      ERR_R_INTERNAL_ERROR);
89             return 0;
90         }
91         alg_enc = s->s3.tmp.new_cipher->algorithm_enc;
92     }
93
94     if (alg_enc & SSL_AESCCM) {
95         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
96             taglen = EVP_CCM8_TLS_TAG_LEN;
97          else
98             taglen = EVP_CCM_TLS_TAG_LEN;
99          if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
100                                          NULL) <= 0) {
101             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
102                      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, SSL_F_TLS13_ENC,
111                  ERR_R_INTERNAL_ERROR);
112         return 0;
113     }
114
115     if (!sending) {
116         /*
117          * Take off tag. There must be at least one byte of content type as
118          * well as the tag
119          */
120         if (rec->length < taglen + 1)
121             return 0;
122         rec->length -= taglen;
123     }
124
125     /* Set up IV */
126     if (ivlen < SEQ_NUM_SIZE) {
127         /* Should not happen */
128         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
129                  ERR_R_INTERNAL_ERROR);
130         return 0;
131     }
132     offset = ivlen - SEQ_NUM_SIZE;
133     memcpy(iv, staticiv, offset);
134     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
135         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
136
137     /* Increment the sequence counter */
138     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
139         ++seq[loop - 1];
140         if (seq[loop - 1] != 0)
141             break;
142     }
143     if (loop == 0) {
144         /* Sequence has wrapped */
145         return 0;
146     }
147
148     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
149     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
150             || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
151                                              taglen,
152                                              rec->data + rec->length) <= 0)) {
153         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
154                  ERR_R_INTERNAL_ERROR);
155         return 0;
156     }
157
158     /* Set up the AAD */
159     if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
160             || !WPACKET_put_bytes_u8(&wpkt, rec->type)
161             || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
162             || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
163             || !WPACKET_get_total_written(&wpkt, &hdrlen)
164             || hdrlen != SSL3_RT_HEADER_LENGTH
165             || !WPACKET_finish(&wpkt)) {
166         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
167                  ERR_R_INTERNAL_ERROR);
168         WPACKET_cleanup(&wpkt);
169         return 0;
170     }
171
172     /*
173      * For CCM we must explicitly set the total plaintext length before we add
174      * any AAD.
175      */
176     if (((alg_enc & SSL_AESCCM) != 0
177                  && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
178                                      (unsigned int)rec->length) <= 0)
179             || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
180                                 sizeof(recheader)) <= 0
181             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
182                                 (unsigned int)rec->length) <= 0
183             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
184             || (size_t)(lenu + lenf) != rec->length) {
185         return 0;
186     }
187     if (sending) {
188         /* Add the tag */
189         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
190                                 rec->data + rec->length) <= 0) {
191             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
192                      ERR_R_INTERNAL_ERROR);
193             return 0;
194         }
195         rec->length += taglen;
196     }
197
198     return 1;
199 }