Various review fixes for PSK early_data support
[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|.
16  *
17  * Returns:
18  *    0: (in non-constant time) if the record is publically invalid (i.e. too
19  *        short etc).
20  *    1: if the record encryption was successful.
21  *   -1: if the record's AEAD-authenticator is invalid or, if sending,
22  *       an internal error occurred.
23  */
24 int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
25 {
26     EVP_CIPHER_CTX *ctx;
27     unsigned char iv[EVP_MAX_IV_LENGTH];
28     size_t ivlen, taglen, offset, loop;
29     unsigned char *staticiv;
30     unsigned char *seq;
31     int lenu, lenf;
32     SSL3_RECORD *rec = &recs[0];
33     uint32_t alg_enc;
34
35     if (n_recs != 1) {
36         /* Should not happen */
37         /* TODO(TLS1.3): Support pipelining */
38         return -1;
39     }
40
41     if (sending) {
42         ctx = s->enc_write_ctx;
43         staticiv = s->write_iv;
44         seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
45     } else {
46         ctx = s->enc_read_ctx;
47         staticiv = s->read_iv;
48         seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
49     }
50
51     if (ctx == NULL) {
52         memmove(rec->data, rec->input, rec->length);
53         rec->input = rec->data;
54         return 1;
55     }
56
57     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
58
59     if (s->early_data_state == SSL_EARLY_DATA_WRITING
60             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
61         if (s->session != NULL && s->session->ext.max_early_data > 0) {
62             alg_enc = s->session->cipher->algorithm_enc;
63         } else {
64             if (!ossl_assert(s->psksession != NULL
65                              && s->psksession->ext.max_early_data > 0))
66                 return -1;
67             alg_enc = s->psksession->cipher->algorithm_enc;
68         }
69     } else {
70         /*
71          * To get here we must have selected a ciphersuite - otherwise ctx would
72          * be NULL
73          */
74         if (!ossl_assert(s->s3->tmp.new_cipher != NULL))
75             return -1;
76         alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
77     }
78
79     if (alg_enc & SSL_AESCCM) {
80         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
81             taglen = EVP_CCM8_TLS_TAG_LEN;
82          else
83             taglen = EVP_CCM_TLS_TAG_LEN;
84          if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
85                                          NULL) <= 0)
86             return -1;
87     } else if (alg_enc & SSL_AESGCM) {
88         taglen = EVP_GCM_TLS_TAG_LEN;
89     } else if (alg_enc & SSL_CHACHA20) {
90         taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
91     } else {
92         return -1;
93     }
94
95     if (!sending) {
96         /*
97          * Take off tag. There must be at least one byte of content type as
98          * well as the tag
99          */
100         if (rec->length < taglen + 1)
101             return 0;
102         rec->length -= taglen;
103     }
104
105     /* Set up IV */
106     if (ivlen < SEQ_NUM_SIZE) {
107         /* Should not happen */
108         return -1;
109     }
110     offset = ivlen - SEQ_NUM_SIZE;
111     memcpy(iv, staticiv, offset);
112     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
113         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
114
115     /* Increment the sequence counter */
116     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
117         ++seq[loop - 1];
118         if (seq[loop - 1] != 0)
119             break;
120     }
121     if (loop == 0) {
122         /* Sequence has wrapped */
123         return -1;
124     }
125
126     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
127     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
128             || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
129                                              taglen,
130                                              rec->data + rec->length) <= 0)
131             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
132                                 (unsigned int)rec->length) <= 0
133             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
134             || (size_t)(lenu + lenf) != rec->length) {
135         return -1;
136     }
137     if (sending) {
138         /* Add the tag */
139         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
140                                 rec->data + rec->length) <= 0)
141             return -1;
142         rec->length += taglen;
143     }
144
145     return 1;
146 }