Convert the remaining functions in the record layer to use SSLfatal()
[openssl.git] / ssl / record / ssl3_record_tls13.c
1 /*
2  * Copyright 2016 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];
29     size_t ivlen, taglen, offset, loop;
30     unsigned char *staticiv;
31     unsigned char *seq;
32     int lenu, lenf;
33     SSL3_RECORD *rec = &recs[0];
34     uint32_t alg_enc;
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 -1;
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     if (ctx == NULL) {
55         memmove(rec->data, rec->input, rec->length);
56         rec->input = rec->data;
57         return 1;
58     }
59
60     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
61
62     if (s->early_data_state == SSL_EARLY_DATA_WRITING
63             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
64         if (s->session != NULL && s->session->ext.max_early_data > 0) {
65             alg_enc = s->session->cipher->algorithm_enc;
66         } else {
67             if (!ossl_assert(s->psksession != NULL
68                              && s->psksession->ext.max_early_data > 0)) {
69                 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
70                          ERR_R_INTERNAL_ERROR);
71                 return -1;
72             }
73             alg_enc = s->psksession->cipher->algorithm_enc;
74         }
75     } else {
76         /*
77          * To get here we must have selected a ciphersuite - otherwise ctx would
78          * be NULL
79          */
80         if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
81             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
82                      ERR_R_INTERNAL_ERROR);
83             return -1;
84         }
85         alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
86     }
87
88     if (alg_enc & SSL_AESCCM) {
89         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
90             taglen = EVP_CCM8_TLS_TAG_LEN;
91          else
92             taglen = EVP_CCM_TLS_TAG_LEN;
93          if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
94                                          NULL) <= 0) {
95             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
96                      ERR_R_INTERNAL_ERROR);
97             return -1;
98         }
99     } else if (alg_enc & SSL_AESGCM) {
100         taglen = EVP_GCM_TLS_TAG_LEN;
101     } else if (alg_enc & SSL_CHACHA20) {
102         taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
103     } else {
104         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
105                  ERR_R_INTERNAL_ERROR);
106         return -1;
107     }
108
109     if (!sending) {
110         /*
111          * Take off tag. There must be at least one byte of content type as
112          * well as the tag
113          */
114         if (rec->length < taglen + 1)
115             return 0;
116         rec->length -= taglen;
117     }
118
119     /* Set up IV */
120     if (ivlen < SEQ_NUM_SIZE) {
121         /* Should not happen */
122         SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
123                  ERR_R_INTERNAL_ERROR);
124         return -1;
125     }
126     offset = ivlen - SEQ_NUM_SIZE;
127     memcpy(iv, staticiv, offset);
128     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
129         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
130
131     /* Increment the sequence counter */
132     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
133         ++seq[loop - 1];
134         if (seq[loop - 1] != 0)
135             break;
136     }
137     if (loop == 0) {
138         /* Sequence has wrapped */
139         return -1;
140     }
141
142     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
143     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
144             || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
145                                              taglen,
146                                              rec->data + rec->length) <= 0)
147             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
148                                 (unsigned int)rec->length) <= 0
149             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
150             || (size_t)(lenu + lenf) != rec->length) {
151         return -1;
152     }
153     if (sending) {
154         /* Add the tag */
155         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
156                                 rec->data + rec->length) <= 0) {
157             SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
158                      ERR_R_INTERNAL_ERROR);
159             return -1;
160         }
161         rec->length += taglen;
162     }
163
164     return 1;
165 }