Ensure that we write out alerts correctly after early_data
[openssl.git] / ssl / record / ssl3_record_tls13.c
1 /*
2  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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_locl.h"
11 #include "record_locl.h"
12 #include "internal/cryptlib.h"
13
14 /*-
15  * tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
16  * internal errors, but not otherwise.
17  *
18  * Returns:
19  *    0: (in non-constant time) if the record is publically invalid (i.e. too
20  *        short etc).
21  *    1: if the record encryption was successful.
22  *   -1: if the record's AEAD-authenticator is invalid or, if sending,
23  *       an internal error occurred.
24  */
25 int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
26 {
27     EVP_CIPHER_CTX *ctx;
28     unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
29     size_t ivlen, taglen, offset, loop, hdrlen;
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         /* TODO(TLS1.3): Support pipelining */
40         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
41                  ERR_R_INTERNAL_ERROR);
42         return -1;
43     }
44
45     if (sending) {
46         ctx = s->enc_write_ctx;
47         staticiv = s->write_iv;
48         seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
49     } else {
50         ctx = s->enc_read_ctx;
51         staticiv = s->read_iv;
52         seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
53     }
54
55     if (ctx == NULL
56             || (rec->type == SSL3_RT_ALERT
57                 && s->statem.enc_write_state
58                    == ENC_WRITE_STATE_WRITE_PLAIN_ALERTS)) {
59         memmove(rec->data, rec->input, rec->length);
60         rec->input = rec->data;
61         return 1;
62     }
63
64     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
65
66     if (s->early_data_state == SSL_EARLY_DATA_WRITING
67             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
68         if (s->session != NULL && s->session->ext.max_early_data > 0) {
69             alg_enc = s->session->cipher->algorithm_enc;
70         } else {
71             if (!ossl_assert(s->psksession != NULL
72                              && s->psksession->ext.max_early_data > 0)) {
73                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
74                          ERR_R_INTERNAL_ERROR);
75                 return -1;
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, SSL_F_TLS13_ENC,
86                      ERR_R_INTERNAL_ERROR);
87             return -1;
88         }
89         alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
90     }
91
92     if (alg_enc & SSL_AESCCM) {
93         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
94             taglen = EVP_CCM8_TLS_TAG_LEN;
95          else
96             taglen = EVP_CCM_TLS_TAG_LEN;
97          if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
98                                          NULL) <= 0) {
99             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
100                      ERR_R_INTERNAL_ERROR);
101             return -1;
102         }
103     } else if (alg_enc & SSL_AESGCM) {
104         taglen = EVP_GCM_TLS_TAG_LEN;
105     } else if (alg_enc & SSL_CHACHA20) {
106         taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
107     } else {
108         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
109                  ERR_R_INTERNAL_ERROR);
110         return -1;
111     }
112
113     if (!sending) {
114         /*
115          * Take off tag. There must be at least one byte of content type as
116          * well as the tag
117          */
118         if (rec->length < taglen + 1)
119             return 0;
120         rec->length -= taglen;
121     }
122
123     /* Set up IV */
124     if (ivlen < SEQ_NUM_SIZE) {
125         /* Should not happen */
126         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
127                  ERR_R_INTERNAL_ERROR);
128         return -1;
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 -1;
144     }
145
146     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
147     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
148             || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
149                                              taglen,
150                                              rec->data + rec->length) <= 0)) {
151         return -1;
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         WPACKET_cleanup(&wpkt);
163         return -1;
164     }
165
166     /*
167      * For CCM we must explicitly set the total plaintext length before we add
168      * any AAD.
169      */
170     if (((alg_enc & SSL_AESCCM) != 0
171                  && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
172                                      (unsigned int)rec->length) <= 0)
173             || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
174                                 sizeof(recheader)) <= 0
175             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
176                                 (unsigned int)rec->length) <= 0
177             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
178             || (size_t)(lenu + lenf) != rec->length) {
179         return -1;
180     }
181     if (sending) {
182         /* Add the tag */
183         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
184                                 rec->data + rec->length) <= 0) {
185             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
186                      ERR_R_INTERNAL_ERROR);
187             return -1;
188         }
189         rec->length += taglen;
190     }
191
192     return 1;
193 }