Update state machine to be closer to TLS1.3
[openssl.git] / ssl / tls13_enc.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 <stdlib.h>
11 #include "ssl_locl.h"
12 #include <openssl/evp.h>
13 #include <openssl/kdf.h>
14
15 #define TLS13_MAX_LABEL_LEN     246
16
17 /* Always filled with zeros */
18 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
19
20 static const unsigned char keylabel[] = "key";
21 static const unsigned char ivlabel[] = "iv";
22
23 /*
24  * Given a |secret|; a |label| of length |labellen|; and a |hash| of the
25  * handshake messages, derive a new secret |outlen| bytes long and store it in
26  * the location pointed to be |out|. The |hash| value may be NULL.
27  *
28  * Returns 1 on success  0 on failure.
29  */
30 static int tls13_hkdf_expand(SSL *s, const unsigned char *secret,
31                              const unsigned char *label, size_t labellen,
32                              const unsigned char *hash,
33                              unsigned char *out, size_t outlen)
34 {
35     const unsigned char label_prefix[] = "TLS 1.3, ";
36     const EVP_MD *md = ssl_handshake_md(s);
37     EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
38     int ret;
39     size_t hkdflabellen;
40     size_t hashlen;
41     /*
42      * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
43      * prefix and label + bytes for the label itself + bytes for the hash
44      */
45     unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
46                             + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
47                             + EVP_MAX_MD_SIZE];
48     WPACKET pkt;
49
50     if (pctx == NULL)
51         return 0;
52
53     hashlen = EVP_MD_size(md);
54
55     if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
56             || !WPACKET_put_bytes_u16(&pkt, outlen)
57             || !WPACKET_start_sub_packet_u8(&pkt)
58             || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
59             || !WPACKET_memcpy(&pkt, label, labellen)
60             || !WPACKET_close(&pkt)
61             || !WPACKET_sub_memcpy_u8(&pkt, hash, (hash == NULL) ? 0 : hashlen)
62             || !WPACKET_get_total_written(&pkt, &hkdflabellen)
63             || !WPACKET_finish(&pkt)) {
64         WPACKET_cleanup(&pkt);
65         return 0;
66     }
67
68     ret = EVP_PKEY_derive_init(pctx) <= 0
69             || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
70                <= 0
71             || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
72             || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
73             || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
74             || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
75
76     EVP_PKEY_CTX_free(pctx);
77
78     return ret == 0;
79 }
80
81 /*
82  * Given a input secret |insecret| and a |label| of length |labellen|, derive a
83  * new |secret|. This will be the length of the current hash output size and
84  * will be based on the current state of the handshake hashes.
85  *
86  * Returns 1 on success  0 on failure.
87  */
88 int tls13_derive_secret(SSL *s, const unsigned char *insecret,
89                         const unsigned char *label, size_t labellen,
90                         unsigned char *secret)
91 {
92     unsigned char hash[EVP_MAX_MD_SIZE];
93     size_t hashlen;
94
95     if (!ssl3_digest_cached_records(s, 1))
96         return 0;
97
98     if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
99         return 0;
100
101     return tls13_hkdf_expand(s, insecret, label, labellen, hash, secret,
102                              hashlen);
103 }
104
105 /*
106  * Given a |secret| generate a |key| of length |keylen| bytes.
107  *
108  * Returns 1 on success  0 on failure.
109  */
110 int tls13_derive_key(SSL *s, const unsigned char *secret, unsigned char *key,
111                      size_t keylen)
112 {
113     return tls13_hkdf_expand(s, secret, keylabel, sizeof(keylabel) - 1, NULL,
114                              key, keylen);
115 }
116
117 /*
118  * Given a |secret| generate an |iv| of length |ivlen| bytes.
119  *
120  * Returns 1 on success  0 on failure.
121  */
122 int tls13_derive_iv(SSL *s, const unsigned char *secret, unsigned char *iv,
123                     size_t ivlen)
124 {
125     return tls13_hkdf_expand(s, secret, ivlabel, sizeof(ivlabel) - 1, NULL,
126                              iv, ivlen);
127 }
128
129 /*
130  * Given the previous secret |prevsecret| and a new input secret |insecret| of
131  * length |insecretlen|, generate a new secret and store it in the location
132  * pointed to by |outsecret|.
133  *
134  * Returns 1 on success  0 on failure.
135  */
136 static int tls13_generate_secret(SSL *s, const unsigned char *prevsecret,
137                                  const unsigned char *insecret,
138                                  size_t insecretlen,
139                                  unsigned char *outsecret)
140 {
141     const EVP_MD *md = ssl_handshake_md(s);
142     size_t mdlen, prevsecretlen;
143     int ret;
144     EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
145
146     if (pctx == NULL)
147         return 0;
148
149     mdlen = EVP_MD_size(md);
150
151     if (insecret == NULL) {
152         insecret = default_zeros;
153         insecretlen = mdlen;
154     }
155     if (prevsecret == NULL) {
156         prevsecret = default_zeros;
157         prevsecretlen = 0;
158     } else {
159         prevsecretlen = mdlen;
160     }
161
162     ret = EVP_PKEY_derive_init(pctx) <= 0
163             || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
164                <= 0
165             || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
166             || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
167             || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
168                <= 0
169             || EVP_PKEY_derive(pctx, outsecret, &mdlen)
170                <= 0;
171
172     EVP_PKEY_CTX_free(pctx);
173     return ret == 0;
174 }
175
176 /*
177  * Given an input secret |insecret| of length |insecretlen| generate the early
178  * secret.
179  *
180  * Returns 1 on success  0 on failure.
181  */
182 int tls13_generate_early_secret(SSL *s, const unsigned char *insecret,
183                                 size_t insecretlen)
184 {
185     return tls13_generate_secret(s, NULL, insecret, insecretlen,
186                                  (unsigned char *)&s->early_secret);
187 }
188
189 /*
190  * Given an input secret |insecret| of length |insecretlen| generate the
191  * handshake secret. This requires the early secret to already have been
192  * generated.
193  *
194  * Returns 1 on success  0 on failure.
195  */
196 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
197                                 size_t insecretlen)
198 {
199     return tls13_generate_secret(s, s->early_secret, insecret, insecretlen,
200                                  (unsigned char *)&s->handshake_secret);
201 }
202
203 /*
204  * Given the handshake secret |prev| of length |prevlen| generate the master
205  * secret and store its length in |*secret_size|
206  *
207  * Returns 1 on success  0 on failure.
208  */
209 int tls13_generate_master_secret(SSL *s, unsigned char *out,
210                                  unsigned char *prev, size_t prevlen,
211                                  size_t *secret_size)
212 {
213     *secret_size = EVP_MD_size(ssl_handshake_md(s));
214     return tls13_generate_secret(s, prev, NULL, 0, out);
215 }
216
217 /*
218  * Generates the mac for the Finished message.
219  *
220  * Returns the length of the MAC or 0 on error.
221  */
222 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
223                              unsigned char *out)
224 {
225     size_t hashlen;
226     const EVP_MD *md;
227
228     /*
229      * TODO(TLS1.3): This is a dummy implementation for now. We need to come
230      * back and fill this in.
231      */
232     md = ssl_handshake_md(s);
233     hashlen = EVP_MD_size(md);
234     memset(out, 0, hashlen);
235
236     return hashlen;
237 }
238
239 /*
240  * There isn't really a key block in TLSv1.3, but we still need this function
241  * for initialising the cipher and hash.
242  *
243  * Returns 1 on success or 0 on failure.
244  */
245 int tls13_setup_key_block(SSL *s)
246 {
247     const EVP_CIPHER *c;
248     const EVP_MD *hash;
249     int mac_type = NID_undef;
250
251     s->session->cipher = s->s3->tmp.new_cipher;
252     if (!ssl_cipher_get_evp
253         (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
254         SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
255         return 0;
256     }
257
258     s->s3->tmp.new_sym_enc = c;
259     s->s3->tmp.new_hash = hash;
260
261     return 1;
262 }
263
264 const unsigned char client_handshake_traffic[] =
265     "client handshake traffic secret";
266 const unsigned char client_application_traffic[] =
267     "client application traffic secret";
268 const unsigned char server_handshake_traffic[] =
269     "server handshake traffic secret";
270 const unsigned char server_application_traffic[] =
271     "server application traffic secret";
272
273 int tls13_change_cipher_state(SSL *s, int which)
274 {
275     unsigned char key[EVP_MAX_KEY_LENGTH];
276     unsigned char iv[EVP_MAX_IV_LENGTH];
277     unsigned char secret[EVP_MAX_MD_SIZE];
278     unsigned char *insecret;
279     EVP_CIPHER_CTX *ciph_ctx;
280     const EVP_CIPHER *ciph = s->s3->tmp.new_sym_enc;;
281     size_t ivlen, keylen;
282     const unsigned char *label;
283     size_t labellen;
284
285     if (which & SSL3_CC_READ) {
286         if (s->enc_read_ctx != NULL) {
287             EVP_CIPHER_CTX_reset(s->enc_read_ctx);
288         } else {
289             s->enc_read_ctx = EVP_CIPHER_CTX_new();
290             if (s->enc_read_ctx == NULL) {
291                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
292                 goto err;
293             }
294         }
295         ciph_ctx = s->enc_read_ctx;
296
297         RECORD_LAYER_reset_read_sequence(&s->rlayer);
298     } else {
299         if (s->enc_write_ctx != NULL) {
300             EVP_CIPHER_CTX_reset(s->enc_write_ctx);
301         } else {
302             s->enc_write_ctx = EVP_CIPHER_CTX_new();
303             if (s->enc_write_ctx == NULL) {
304                 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
305                 goto err;
306             }
307         }
308         ciph_ctx = s->enc_write_ctx;
309
310         RECORD_LAYER_reset_write_sequence(&s->rlayer);
311     }
312
313     if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
314             || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
315         if (which & SSL3_CC_HANDSHAKE) {
316             insecret = s->handshake_secret;
317             label = client_handshake_traffic;
318             labellen = sizeof(client_handshake_traffic) - 1;
319         } else {
320             insecret = s->session->master_key;
321             label = client_application_traffic;
322             labellen = sizeof(client_application_traffic) - 1;
323         }
324     } else {
325         if (which & SSL3_CC_HANDSHAKE) {
326             insecret = s->handshake_secret;
327             label = server_handshake_traffic;
328             labellen = sizeof(server_handshake_traffic) - 1;
329         } else {
330             insecret = s->session->master_key;
331             label = server_application_traffic;
332             labellen = sizeof(server_application_traffic) - 1;
333         }
334     }
335
336     if (!tls13_derive_secret(s, insecret, label, labellen, secret)) {
337         SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
338         goto err;
339     }
340
341     /* TODO(size_t): convert me */
342     keylen = EVP_CIPHER_key_length(ciph);
343
344     if (EVP_CIPHER_mode(ciph) == EVP_CIPH_GCM_MODE)
345         ivlen = EVP_GCM_TLS_FIXED_IV_LEN;
346     else if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE)
347         ivlen = EVP_CCM_TLS_FIXED_IV_LEN;
348     else
349         ivlen = EVP_CIPHER_iv_length(ciph);
350
351     if (!tls13_derive_key(s, secret, key, keylen)
352             || !tls13_derive_iv(s, secret, iv, ivlen)) {
353         SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
354         goto err;
355     }
356
357     if (EVP_CIPHER_mode(ciph) == EVP_CIPH_GCM_MODE) {
358         if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, key, NULL,
359                                (which & SSL3_CC_WRITE))
360                 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_GCM_SET_IV_FIXED,
361                                         (int)ivlen, iv)) {
362             SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_EVP_LIB);
363             goto err;
364         }
365     } else if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
366         int taglen;
367         if (s->s3->tmp.new_cipher->algorithm_enc
368                 & (SSL_AES128CCM8 | SSL_AES256CCM8))
369             taglen = 8;
370         else
371             taglen = 16;
372         if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL,
373                                (which & SSL3_CC_WRITE))
374                 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, 12,
375                                         NULL)
376                 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
377                                         NULL)
378                 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_CCM_SET_IV_FIXED,
379                                         (int)ivlen, iv)
380                 || !EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1)) {
381             SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_EVP_LIB);
382             goto err;
383         }
384     } else {
385         if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, key, iv,
386                                (which & SSL3_CC_WRITE))) {
387             SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_EVP_LIB);
388             goto err;
389         }
390     }
391
392 #ifdef OPENSSL_SSL_TRACE_CRYPTO
393     if (s->msg_callback) {
394         int wh = which & SSL3_CC_WRITE ? TLS1_RT_CRYPTO_WRITE : 0;
395
396         if (ciph->key_len)
397             s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
398                             key, ciph->key_len, s, s->msg_callback_arg);
399         if (ivlen) {
400             if (EVP_CIPHER_mode(ciph) == EVP_CIPH_GCM_MODE)
401                 wh |= TLS1_RT_CRYPTO_FIXED_IV;
402             else
403                 wh |= TLS1_RT_CRYPTO_IV;
404             s->msg_callback(2, s->version, wh, iv, ivlen, s,
405                             s->msg_callback_arg);
406         }
407     }
408 #endif
409
410     OPENSSL_cleanse(secret, sizeof(secret));
411     OPENSSL_cleanse(key, sizeof(key));
412     OPENSSL_cleanse(iv, sizeof(iv));
413     return 1;
414
415  err:
416     OPENSSL_cleanse(secret, sizeof(secret));
417     OPENSSL_cleanse(key, sizeof(key));
418     OPENSSL_cleanse(iv, sizeof(iv));
419     return 0;
420 }