1*4724848cSchristos /*
2*4724848cSchristos * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3*4724848cSchristos *
4*4724848cSchristos * Licensed under the OpenSSL license (the "License"). You may not use
5*4724848cSchristos * this file except in compliance with the License. You can obtain a copy
6*4724848cSchristos * in the file LICENSE in the source distribution or at
7*4724848cSchristos * https://www.openssl.org/source/license.html
8*4724848cSchristos */
9*4724848cSchristos
10*4724848cSchristos #include <stdlib.h>
11*4724848cSchristos #include "ssl_local.h"
12*4724848cSchristos #include "internal/cryptlib.h"
13*4724848cSchristos #include <openssl/evp.h>
14*4724848cSchristos #include <openssl/kdf.h>
15*4724848cSchristos
16*4724848cSchristos #define TLS13_MAX_LABEL_LEN 249
17*4724848cSchristos
18*4724848cSchristos /* Always filled with zeros */
19*4724848cSchristos static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
20*4724848cSchristos
21*4724848cSchristos /*
22*4724848cSchristos * Given a |secret|; a |label| of length |labellen|; and |data| of length
23*4724848cSchristos * |datalen| (e.g. typically a hash of the handshake messages), derive a new
24*4724848cSchristos * secret |outlen| bytes long and store it in the location pointed to be |out|.
25*4724848cSchristos * The |data| value may be zero length. Any errors will be treated as fatal if
26*4724848cSchristos * |fatal| is set. Returns 1 on success 0 on failure.
27*4724848cSchristos */
tls13_hkdf_expand(SSL * s,const EVP_MD * md,const unsigned char * secret,const unsigned char * label,size_t labellen,const unsigned char * data,size_t datalen,unsigned char * out,size_t outlen,int fatal)28*4724848cSchristos int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
29*4724848cSchristos const unsigned char *label, size_t labellen,
30*4724848cSchristos const unsigned char *data, size_t datalen,
31*4724848cSchristos unsigned char *out, size_t outlen, int fatal)
32*4724848cSchristos {
33*4724848cSchristos #ifdef CHARSET_EBCDIC
34*4724848cSchristos static const unsigned char label_prefix[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
35*4724848cSchristos #else
36*4724848cSchristos static const unsigned char label_prefix[] = "tls13 ";
37*4724848cSchristos #endif
38*4724848cSchristos EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
39*4724848cSchristos int ret;
40*4724848cSchristos size_t hkdflabellen;
41*4724848cSchristos size_t hashlen;
42*4724848cSchristos /*
43*4724848cSchristos * 2 bytes for length of derived secret + 1 byte for length of combined
44*4724848cSchristos * prefix and label + bytes for the label itself + 1 byte length of hash
45*4724848cSchristos * + bytes for the hash itself
46*4724848cSchristos */
47*4724848cSchristos unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t)
48*4724848cSchristos + (sizeof(label_prefix) - 1) + TLS13_MAX_LABEL_LEN
49*4724848cSchristos + 1 + EVP_MAX_MD_SIZE];
50*4724848cSchristos WPACKET pkt;
51*4724848cSchristos
52*4724848cSchristos if (pctx == NULL)
53*4724848cSchristos return 0;
54*4724848cSchristos
55*4724848cSchristos if (labellen > TLS13_MAX_LABEL_LEN) {
56*4724848cSchristos if (fatal) {
57*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
58*4724848cSchristos ERR_R_INTERNAL_ERROR);
59*4724848cSchristos } else {
60*4724848cSchristos /*
61*4724848cSchristos * Probably we have been called from SSL_export_keying_material(),
62*4724848cSchristos * or SSL_export_keying_material_early().
63*4724848cSchristos */
64*4724848cSchristos SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
65*4724848cSchristos }
66*4724848cSchristos EVP_PKEY_CTX_free(pctx);
67*4724848cSchristos return 0;
68*4724848cSchristos }
69*4724848cSchristos
70*4724848cSchristos hashlen = EVP_MD_size(md);
71*4724848cSchristos
72*4724848cSchristos if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
73*4724848cSchristos || !WPACKET_put_bytes_u16(&pkt, outlen)
74*4724848cSchristos || !WPACKET_start_sub_packet_u8(&pkt)
75*4724848cSchristos || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
76*4724848cSchristos || !WPACKET_memcpy(&pkt, label, labellen)
77*4724848cSchristos || !WPACKET_close(&pkt)
78*4724848cSchristos || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
79*4724848cSchristos || !WPACKET_get_total_written(&pkt, &hkdflabellen)
80*4724848cSchristos || !WPACKET_finish(&pkt)) {
81*4724848cSchristos EVP_PKEY_CTX_free(pctx);
82*4724848cSchristos WPACKET_cleanup(&pkt);
83*4724848cSchristos if (fatal)
84*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
85*4724848cSchristos ERR_R_INTERNAL_ERROR);
86*4724848cSchristos else
87*4724848cSchristos SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
88*4724848cSchristos return 0;
89*4724848cSchristos }
90*4724848cSchristos
91*4724848cSchristos ret = EVP_PKEY_derive_init(pctx) <= 0
92*4724848cSchristos || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
93*4724848cSchristos <= 0
94*4724848cSchristos || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
95*4724848cSchristos || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
96*4724848cSchristos || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
97*4724848cSchristos || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
98*4724848cSchristos
99*4724848cSchristos EVP_PKEY_CTX_free(pctx);
100*4724848cSchristos
101*4724848cSchristos if (ret != 0) {
102*4724848cSchristos if (fatal)
103*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
104*4724848cSchristos ERR_R_INTERNAL_ERROR);
105*4724848cSchristos else
106*4724848cSchristos SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
107*4724848cSchristos }
108*4724848cSchristos
109*4724848cSchristos return ret == 0;
110*4724848cSchristos }
111*4724848cSchristos
112*4724848cSchristos /*
113*4724848cSchristos * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
114*4724848cSchristos * success 0 on failure.
115*4724848cSchristos */
tls13_derive_key(SSL * s,const EVP_MD * md,const unsigned char * secret,unsigned char * key,size_t keylen)116*4724848cSchristos int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
117*4724848cSchristos unsigned char *key, size_t keylen)
118*4724848cSchristos {
119*4724848cSchristos #ifdef CHARSET_EBCDIC
120*4724848cSchristos static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
121*4724848cSchristos #else
122*4724848cSchristos static const unsigned char keylabel[] = "key";
123*4724848cSchristos #endif
124*4724848cSchristos
125*4724848cSchristos return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
126*4724848cSchristos NULL, 0, key, keylen, 1);
127*4724848cSchristos }
128*4724848cSchristos
129*4724848cSchristos /*
130*4724848cSchristos * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
131*4724848cSchristos * success 0 on failure.
132*4724848cSchristos */
tls13_derive_iv(SSL * s,const EVP_MD * md,const unsigned char * secret,unsigned char * iv,size_t ivlen)133*4724848cSchristos int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
134*4724848cSchristos unsigned char *iv, size_t ivlen)
135*4724848cSchristos {
136*4724848cSchristos #ifdef CHARSET_EBCDIC
137*4724848cSchristos static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
138*4724848cSchristos #else
139*4724848cSchristos static const unsigned char ivlabel[] = "iv";
140*4724848cSchristos #endif
141*4724848cSchristos
142*4724848cSchristos return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
143*4724848cSchristos NULL, 0, iv, ivlen, 1);
144*4724848cSchristos }
145*4724848cSchristos
tls13_derive_finishedkey(SSL * s,const EVP_MD * md,const unsigned char * secret,unsigned char * fin,size_t finlen)146*4724848cSchristos int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
147*4724848cSchristos const unsigned char *secret,
148*4724848cSchristos unsigned char *fin, size_t finlen)
149*4724848cSchristos {
150*4724848cSchristos #ifdef CHARSET_EBCDIC
151*4724848cSchristos static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
152*4724848cSchristos #else
153*4724848cSchristos static const unsigned char finishedlabel[] = "finished";
154*4724848cSchristos #endif
155*4724848cSchristos
156*4724848cSchristos return tls13_hkdf_expand(s, md, secret, finishedlabel,
157*4724848cSchristos sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
158*4724848cSchristos }
159*4724848cSchristos
160*4724848cSchristos /*
161*4724848cSchristos * Given the previous secret |prevsecret| and a new input secret |insecret| of
162*4724848cSchristos * length |insecretlen|, generate a new secret and store it in the location
163*4724848cSchristos * pointed to by |outsecret|. Returns 1 on success 0 on failure.
164*4724848cSchristos */
tls13_generate_secret(SSL * s,const EVP_MD * md,const unsigned char * prevsecret,const unsigned char * insecret,size_t insecretlen,unsigned char * outsecret)165*4724848cSchristos int tls13_generate_secret(SSL *s, const EVP_MD *md,
166*4724848cSchristos const unsigned char *prevsecret,
167*4724848cSchristos const unsigned char *insecret,
168*4724848cSchristos size_t insecretlen,
169*4724848cSchristos unsigned char *outsecret)
170*4724848cSchristos {
171*4724848cSchristos size_t mdlen, prevsecretlen;
172*4724848cSchristos int mdleni;
173*4724848cSchristos int ret;
174*4724848cSchristos EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
175*4724848cSchristos #ifdef CHARSET_EBCDIC
176*4724848cSchristos static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
177*4724848cSchristos #else
178*4724848cSchristos static const char derived_secret_label[] = "derived";
179*4724848cSchristos #endif
180*4724848cSchristos unsigned char preextractsec[EVP_MAX_MD_SIZE];
181*4724848cSchristos
182*4724848cSchristos if (pctx == NULL) {
183*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
184*4724848cSchristos ERR_R_INTERNAL_ERROR);
185*4724848cSchristos return 0;
186*4724848cSchristos }
187*4724848cSchristos
188*4724848cSchristos mdleni = EVP_MD_size(md);
189*4724848cSchristos /* Ensure cast to size_t is safe */
190*4724848cSchristos if (!ossl_assert(mdleni >= 0)) {
191*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
192*4724848cSchristos ERR_R_INTERNAL_ERROR);
193*4724848cSchristos EVP_PKEY_CTX_free(pctx);
194*4724848cSchristos return 0;
195*4724848cSchristos }
196*4724848cSchristos mdlen = (size_t)mdleni;
197*4724848cSchristos
198*4724848cSchristos if (insecret == NULL) {
199*4724848cSchristos insecret = default_zeros;
200*4724848cSchristos insecretlen = mdlen;
201*4724848cSchristos }
202*4724848cSchristos if (prevsecret == NULL) {
203*4724848cSchristos prevsecret = default_zeros;
204*4724848cSchristos prevsecretlen = 0;
205*4724848cSchristos } else {
206*4724848cSchristos EVP_MD_CTX *mctx = EVP_MD_CTX_new();
207*4724848cSchristos unsigned char hash[EVP_MAX_MD_SIZE];
208*4724848cSchristos
209*4724848cSchristos /* The pre-extract derive step uses a hash of no messages */
210*4724848cSchristos if (mctx == NULL
211*4724848cSchristos || EVP_DigestInit_ex(mctx, md, NULL) <= 0
212*4724848cSchristos || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
213*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
214*4724848cSchristos ERR_R_INTERNAL_ERROR);
215*4724848cSchristos EVP_MD_CTX_free(mctx);
216*4724848cSchristos EVP_PKEY_CTX_free(pctx);
217*4724848cSchristos return 0;
218*4724848cSchristos }
219*4724848cSchristos EVP_MD_CTX_free(mctx);
220*4724848cSchristos
221*4724848cSchristos /* Generate the pre-extract secret */
222*4724848cSchristos if (!tls13_hkdf_expand(s, md, prevsecret,
223*4724848cSchristos (unsigned char *)derived_secret_label,
224*4724848cSchristos sizeof(derived_secret_label) - 1, hash, mdlen,
225*4724848cSchristos preextractsec, mdlen, 1)) {
226*4724848cSchristos /* SSLfatal() already called */
227*4724848cSchristos EVP_PKEY_CTX_free(pctx);
228*4724848cSchristos return 0;
229*4724848cSchristos }
230*4724848cSchristos
231*4724848cSchristos prevsecret = preextractsec;
232*4724848cSchristos prevsecretlen = mdlen;
233*4724848cSchristos }
234*4724848cSchristos
235*4724848cSchristos ret = EVP_PKEY_derive_init(pctx) <= 0
236*4724848cSchristos || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
237*4724848cSchristos <= 0
238*4724848cSchristos || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
239*4724848cSchristos || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
240*4724848cSchristos || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
241*4724848cSchristos <= 0
242*4724848cSchristos || EVP_PKEY_derive(pctx, outsecret, &mdlen)
243*4724848cSchristos <= 0;
244*4724848cSchristos
245*4724848cSchristos if (ret != 0)
246*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
247*4724848cSchristos ERR_R_INTERNAL_ERROR);
248*4724848cSchristos
249*4724848cSchristos EVP_PKEY_CTX_free(pctx);
250*4724848cSchristos if (prevsecret == preextractsec)
251*4724848cSchristos OPENSSL_cleanse(preextractsec, mdlen);
252*4724848cSchristos return ret == 0;
253*4724848cSchristos }
254*4724848cSchristos
255*4724848cSchristos /*
256*4724848cSchristos * Given an input secret |insecret| of length |insecretlen| generate the
257*4724848cSchristos * handshake secret. This requires the early secret to already have been
258*4724848cSchristos * generated. Returns 1 on success 0 on failure.
259*4724848cSchristos */
tls13_generate_handshake_secret(SSL * s,const unsigned char * insecret,size_t insecretlen)260*4724848cSchristos int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
261*4724848cSchristos size_t insecretlen)
262*4724848cSchristos {
263*4724848cSchristos /* Calls SSLfatal() if required */
264*4724848cSchristos return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
265*4724848cSchristos insecret, insecretlen,
266*4724848cSchristos (unsigned char *)&s->handshake_secret);
267*4724848cSchristos }
268*4724848cSchristos
269*4724848cSchristos /*
270*4724848cSchristos * Given the handshake secret |prev| of length |prevlen| generate the master
271*4724848cSchristos * secret and store its length in |*secret_size|. Returns 1 on success 0 on
272*4724848cSchristos * failure.
273*4724848cSchristos */
tls13_generate_master_secret(SSL * s,unsigned char * out,unsigned char * prev,size_t prevlen,size_t * secret_size)274*4724848cSchristos int tls13_generate_master_secret(SSL *s, unsigned char *out,
275*4724848cSchristos unsigned char *prev, size_t prevlen,
276*4724848cSchristos size_t *secret_size)
277*4724848cSchristos {
278*4724848cSchristos const EVP_MD *md = ssl_handshake_md(s);
279*4724848cSchristos
280*4724848cSchristos *secret_size = EVP_MD_size(md);
281*4724848cSchristos /* Calls SSLfatal() if required */
282*4724848cSchristos return tls13_generate_secret(s, md, prev, NULL, 0, out);
283*4724848cSchristos }
284*4724848cSchristos
285*4724848cSchristos /*
286*4724848cSchristos * Generates the mac for the Finished message. Returns the length of the MAC or
287*4724848cSchristos * 0 on error.
288*4724848cSchristos */
tls13_final_finish_mac(SSL * s,const char * str,size_t slen,unsigned char * out)289*4724848cSchristos size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
290*4724848cSchristos unsigned char *out)
291*4724848cSchristos {
292*4724848cSchristos const EVP_MD *md = ssl_handshake_md(s);
293*4724848cSchristos unsigned char hash[EVP_MAX_MD_SIZE];
294*4724848cSchristos size_t hashlen, ret = 0;
295*4724848cSchristos EVP_PKEY *key = NULL;
296*4724848cSchristos EVP_MD_CTX *ctx = EVP_MD_CTX_new();
297*4724848cSchristos
298*4724848cSchristos if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
299*4724848cSchristos /* SSLfatal() already called */
300*4724848cSchristos goto err;
301*4724848cSchristos }
302*4724848cSchristos
303*4724848cSchristos if (str == s->method->ssl3_enc->server_finished_label) {
304*4724848cSchristos key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
305*4724848cSchristos s->server_finished_secret, hashlen);
306*4724848cSchristos } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
307*4724848cSchristos key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
308*4724848cSchristos s->client_finished_secret, hashlen);
309*4724848cSchristos } else {
310*4724848cSchristos unsigned char finsecret[EVP_MAX_MD_SIZE];
311*4724848cSchristos
312*4724848cSchristos if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
313*4724848cSchristos s->client_app_traffic_secret,
314*4724848cSchristos finsecret, hashlen))
315*4724848cSchristos goto err;
316*4724848cSchristos
317*4724848cSchristos key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
318*4724848cSchristos hashlen);
319*4724848cSchristos OPENSSL_cleanse(finsecret, sizeof(finsecret));
320*4724848cSchristos }
321*4724848cSchristos
322*4724848cSchristos if (key == NULL
323*4724848cSchristos || ctx == NULL
324*4724848cSchristos || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
325*4724848cSchristos || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
326*4724848cSchristos || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
327*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
328*4724848cSchristos ERR_R_INTERNAL_ERROR);
329*4724848cSchristos goto err;
330*4724848cSchristos }
331*4724848cSchristos
332*4724848cSchristos ret = hashlen;
333*4724848cSchristos err:
334*4724848cSchristos EVP_PKEY_free(key);
335*4724848cSchristos EVP_MD_CTX_free(ctx);
336*4724848cSchristos return ret;
337*4724848cSchristos }
338*4724848cSchristos
339*4724848cSchristos /*
340*4724848cSchristos * There isn't really a key block in TLSv1.3, but we still need this function
341*4724848cSchristos * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
342*4724848cSchristos */
tls13_setup_key_block(SSL * s)343*4724848cSchristos int tls13_setup_key_block(SSL *s)
344*4724848cSchristos {
345*4724848cSchristos const EVP_CIPHER *c;
346*4724848cSchristos const EVP_MD *hash;
347*4724848cSchristos
348*4724848cSchristos s->session->cipher = s->s3->tmp.new_cipher;
349*4724848cSchristos if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, NULL, 0)) {
350*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
351*4724848cSchristos SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
352*4724848cSchristos return 0;
353*4724848cSchristos }
354*4724848cSchristos
355*4724848cSchristos s->s3->tmp.new_sym_enc = c;
356*4724848cSchristos s->s3->tmp.new_hash = hash;
357*4724848cSchristos
358*4724848cSchristos return 1;
359*4724848cSchristos }
360*4724848cSchristos
derive_secret_key_and_iv(SSL * s,int sending,const EVP_MD * md,const EVP_CIPHER * ciph,const unsigned char * insecret,const unsigned char * hash,const unsigned char * label,size_t labellen,unsigned char * secret,unsigned char * iv,EVP_CIPHER_CTX * ciph_ctx)361*4724848cSchristos static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
362*4724848cSchristos const EVP_CIPHER *ciph,
363*4724848cSchristos const unsigned char *insecret,
364*4724848cSchristos const unsigned char *hash,
365*4724848cSchristos const unsigned char *label,
366*4724848cSchristos size_t labellen, unsigned char *secret,
367*4724848cSchristos unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
368*4724848cSchristos {
369*4724848cSchristos unsigned char key[EVP_MAX_KEY_LENGTH];
370*4724848cSchristos size_t ivlen, keylen, taglen;
371*4724848cSchristos int hashleni = EVP_MD_size(md);
372*4724848cSchristos size_t hashlen;
373*4724848cSchristos
374*4724848cSchristos /* Ensure cast to size_t is safe */
375*4724848cSchristos if (!ossl_assert(hashleni >= 0)) {
376*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
377*4724848cSchristos ERR_R_EVP_LIB);
378*4724848cSchristos goto err;
379*4724848cSchristos }
380*4724848cSchristos hashlen = (size_t)hashleni;
381*4724848cSchristos
382*4724848cSchristos if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
383*4724848cSchristos secret, hashlen, 1)) {
384*4724848cSchristos /* SSLfatal() already called */
385*4724848cSchristos goto err;
386*4724848cSchristos }
387*4724848cSchristos
388*4724848cSchristos /* TODO(size_t): convert me */
389*4724848cSchristos keylen = EVP_CIPHER_key_length(ciph);
390*4724848cSchristos if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
391*4724848cSchristos uint32_t algenc;
392*4724848cSchristos
393*4724848cSchristos ivlen = EVP_CCM_TLS_IV_LEN;
394*4724848cSchristos if (s->s3->tmp.new_cipher != NULL) {
395*4724848cSchristos algenc = s->s3->tmp.new_cipher->algorithm_enc;
396*4724848cSchristos } else if (s->session->cipher != NULL) {
397*4724848cSchristos /* We've not selected a cipher yet - we must be doing early data */
398*4724848cSchristos algenc = s->session->cipher->algorithm_enc;
399*4724848cSchristos } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
400*4724848cSchristos /* We must be doing early data with out-of-band PSK */
401*4724848cSchristos algenc = s->psksession->cipher->algorithm_enc;
402*4724848cSchristos } else {
403*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
404*4724848cSchristos ERR_R_EVP_LIB);
405*4724848cSchristos goto err;
406*4724848cSchristos }
407*4724848cSchristos if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
408*4724848cSchristos taglen = EVP_CCM8_TLS_TAG_LEN;
409*4724848cSchristos else
410*4724848cSchristos taglen = EVP_CCM_TLS_TAG_LEN;
411*4724848cSchristos } else {
412*4724848cSchristos ivlen = EVP_CIPHER_iv_length(ciph);
413*4724848cSchristos taglen = 0;
414*4724848cSchristos }
415*4724848cSchristos
416*4724848cSchristos if (!tls13_derive_key(s, md, secret, key, keylen)
417*4724848cSchristos || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
418*4724848cSchristos /* SSLfatal() already called */
419*4724848cSchristos goto err;
420*4724848cSchristos }
421*4724848cSchristos
422*4724848cSchristos if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
423*4724848cSchristos || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
424*4724848cSchristos || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
425*4724848cSchristos taglen, NULL))
426*4724848cSchristos || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
427*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
428*4724848cSchristos ERR_R_EVP_LIB);
429*4724848cSchristos goto err;
430*4724848cSchristos }
431*4724848cSchristos
432*4724848cSchristos return 1;
433*4724848cSchristos err:
434*4724848cSchristos OPENSSL_cleanse(key, sizeof(key));
435*4724848cSchristos return 0;
436*4724848cSchristos }
437*4724848cSchristos
tls13_change_cipher_state(SSL * s,int which)438*4724848cSchristos int tls13_change_cipher_state(SSL *s, int which)
439*4724848cSchristos {
440*4724848cSchristos #ifdef CHARSET_EBCDIC
441*4724848cSchristos static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
442*4724848cSchristos static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
443*4724848cSchristos static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
444*4724848cSchristos static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
445*4724848cSchristos static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
446*4724848cSchristos static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
447*4724848cSchristos static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
448*4724848cSchristos static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
449*4724848cSchristos #else
450*4724848cSchristos static const unsigned char client_early_traffic[] = "c e traffic";
451*4724848cSchristos static const unsigned char client_handshake_traffic[] = "c hs traffic";
452*4724848cSchristos static const unsigned char client_application_traffic[] = "c ap traffic";
453*4724848cSchristos static const unsigned char server_handshake_traffic[] = "s hs traffic";
454*4724848cSchristos static const unsigned char server_application_traffic[] = "s ap traffic";
455*4724848cSchristos static const unsigned char exporter_master_secret[] = "exp master";
456*4724848cSchristos static const unsigned char resumption_master_secret[] = "res master";
457*4724848cSchristos static const unsigned char early_exporter_master_secret[] = "e exp master";
458*4724848cSchristos #endif
459*4724848cSchristos unsigned char *iv;
460*4724848cSchristos unsigned char secret[EVP_MAX_MD_SIZE];
461*4724848cSchristos unsigned char hashval[EVP_MAX_MD_SIZE];
462*4724848cSchristos unsigned char *hash = hashval;
463*4724848cSchristos unsigned char *insecret;
464*4724848cSchristos unsigned char *finsecret = NULL;
465*4724848cSchristos const char *log_label = NULL;
466*4724848cSchristos EVP_CIPHER_CTX *ciph_ctx;
467*4724848cSchristos size_t finsecretlen = 0;
468*4724848cSchristos const unsigned char *label;
469*4724848cSchristos size_t labellen, hashlen = 0;
470*4724848cSchristos int ret = 0;
471*4724848cSchristos const EVP_MD *md = NULL;
472*4724848cSchristos const EVP_CIPHER *cipher = NULL;
473*4724848cSchristos
474*4724848cSchristos if (which & SSL3_CC_READ) {
475*4724848cSchristos if (s->enc_read_ctx != NULL) {
476*4724848cSchristos EVP_CIPHER_CTX_reset(s->enc_read_ctx);
477*4724848cSchristos } else {
478*4724848cSchristos s->enc_read_ctx = EVP_CIPHER_CTX_new();
479*4724848cSchristos if (s->enc_read_ctx == NULL) {
480*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
481*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
482*4724848cSchristos goto err;
483*4724848cSchristos }
484*4724848cSchristos }
485*4724848cSchristos ciph_ctx = s->enc_read_ctx;
486*4724848cSchristos iv = s->read_iv;
487*4724848cSchristos
488*4724848cSchristos RECORD_LAYER_reset_read_sequence(&s->rlayer);
489*4724848cSchristos } else {
490*4724848cSchristos s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
491*4724848cSchristos if (s->enc_write_ctx != NULL) {
492*4724848cSchristos EVP_CIPHER_CTX_reset(s->enc_write_ctx);
493*4724848cSchristos } else {
494*4724848cSchristos s->enc_write_ctx = EVP_CIPHER_CTX_new();
495*4724848cSchristos if (s->enc_write_ctx == NULL) {
496*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
497*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
498*4724848cSchristos goto err;
499*4724848cSchristos }
500*4724848cSchristos }
501*4724848cSchristos ciph_ctx = s->enc_write_ctx;
502*4724848cSchristos iv = s->write_iv;
503*4724848cSchristos
504*4724848cSchristos RECORD_LAYER_reset_write_sequence(&s->rlayer);
505*4724848cSchristos }
506*4724848cSchristos
507*4724848cSchristos if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
508*4724848cSchristos || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
509*4724848cSchristos if (which & SSL3_CC_EARLY) {
510*4724848cSchristos EVP_MD_CTX *mdctx = NULL;
511*4724848cSchristos long handlen;
512*4724848cSchristos void *hdata;
513*4724848cSchristos unsigned int hashlenui;
514*4724848cSchristos const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
515*4724848cSchristos
516*4724848cSchristos insecret = s->early_secret;
517*4724848cSchristos label = client_early_traffic;
518*4724848cSchristos labellen = sizeof(client_early_traffic) - 1;
519*4724848cSchristos log_label = CLIENT_EARLY_LABEL;
520*4724848cSchristos
521*4724848cSchristos handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
522*4724848cSchristos if (handlen <= 0) {
523*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
524*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE,
525*4724848cSchristos SSL_R_BAD_HANDSHAKE_LENGTH);
526*4724848cSchristos goto err;
527*4724848cSchristos }
528*4724848cSchristos
529*4724848cSchristos if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
530*4724848cSchristos && s->max_early_data > 0
531*4724848cSchristos && s->session->ext.max_early_data == 0) {
532*4724848cSchristos /*
533*4724848cSchristos * If we are attempting to send early data, and we've decided to
534*4724848cSchristos * actually do it but max_early_data in s->session is 0 then we
535*4724848cSchristos * must be using an external PSK.
536*4724848cSchristos */
537*4724848cSchristos if (!ossl_assert(s->psksession != NULL
538*4724848cSchristos && s->max_early_data ==
539*4724848cSchristos s->psksession->ext.max_early_data)) {
540*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
541*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE,
542*4724848cSchristos ERR_R_INTERNAL_ERROR);
543*4724848cSchristos goto err;
544*4724848cSchristos }
545*4724848cSchristos sslcipher = SSL_SESSION_get0_cipher(s->psksession);
546*4724848cSchristos }
547*4724848cSchristos if (sslcipher == NULL) {
548*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
549*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
550*4724848cSchristos goto err;
551*4724848cSchristos }
552*4724848cSchristos
553*4724848cSchristos /*
554*4724848cSchristos * We need to calculate the handshake digest using the digest from
555*4724848cSchristos * the session. We haven't yet selected our ciphersuite so we can't
556*4724848cSchristos * use ssl_handshake_md().
557*4724848cSchristos */
558*4724848cSchristos mdctx = EVP_MD_CTX_new();
559*4724848cSchristos if (mdctx == NULL) {
560*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
561*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
562*4724848cSchristos goto err;
563*4724848cSchristos }
564*4724848cSchristos cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
565*4724848cSchristos md = ssl_md(sslcipher->algorithm2);
566*4724848cSchristos if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
567*4724848cSchristos || !EVP_DigestUpdate(mdctx, hdata, handlen)
568*4724848cSchristos || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
569*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
570*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
571*4724848cSchristos EVP_MD_CTX_free(mdctx);
572*4724848cSchristos goto err;
573*4724848cSchristos }
574*4724848cSchristos hashlen = hashlenui;
575*4724848cSchristos EVP_MD_CTX_free(mdctx);
576*4724848cSchristos
577*4724848cSchristos if (!tls13_hkdf_expand(s, md, insecret,
578*4724848cSchristos early_exporter_master_secret,
579*4724848cSchristos sizeof(early_exporter_master_secret) - 1,
580*4724848cSchristos hashval, hashlen,
581*4724848cSchristos s->early_exporter_master_secret, hashlen,
582*4724848cSchristos 1)) {
583*4724848cSchristos SSLfatal(s, SSL_AD_INTERNAL_ERROR,
584*4724848cSchristos SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
585*4724848cSchristos goto err;
586*4724848cSchristos }
587*4724848cSchristos
588*4724848cSchristos if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
589*4724848cSchristos s->early_exporter_master_secret, hashlen)) {
590*4724848cSchristos /* SSLfatal() already called */
591*4724848cSchristos goto err;
592*4724848cSchristos }
593*4724848cSchristos } else if (which & SSL3_CC_HANDSHAKE) {
594*4724848cSchristos insecret = s->handshake_secret;
595*4724848cSchristos finsecret = s->client_finished_secret;
596*4724848cSchristos finsecretlen = EVP_MD_size(ssl_handshake_md(s));
597*4724848cSchristos label = client_handshake_traffic;
598*4724848cSchristos labellen = sizeof(client_handshake_traffic) - 1;
599*4724848cSchristos log_label = CLIENT_HANDSHAKE_LABEL;
600*4724848cSchristos /*
601*4724848cSchristos * The handshake hash used for the server read/client write handshake
602*4724848cSchristos * traffic secret is the same as the hash for the server
603*4724848cSchristos * write/client read handshake traffic secret. However, if we
604*4724848cSchristos * processed early data then we delay changing the server
605*4724848cSchristos * read/client write cipher state until later, and the handshake
606*4724848cSchristos * hashes have moved on. Therefore we use the value saved earlier
607*4724848cSchristos * when we did the server write/client read change cipher state.
608*4724848cSchristos */
609*4724848cSchristos hash = s->handshake_traffic_hash;
610*4724848cSchristos } else {
611*4724848cSchristos insecret = s->master_secret;
612*4724848cSchristos label = client_application_traffic;
613*4724848cSchristos labellen = sizeof(client_application_traffic) - 1;
614*4724848cSchristos log_label = CLIENT_APPLICATION_LABEL;
615*4724848cSchristos /*
616*4724848cSchristos * For this we only use the handshake hashes up until the server
617*4724848cSchristos * Finished hash. We do not include the client's Finished, which is
618*4724848cSchristos * what ssl_handshake_hash() would give us. Instead we use the
619*4724848cSchristos * previously saved value.
620*4724848cSchristos */
621*4724848cSchristos hash = s->server_finished_hash;
622*4724848cSchristos }
623*4724848cSchristos } else {
624*4724848cSchristos /* Early data never applies to client-read/server-write */
625*4724848cSchristos if (which & SSL3_CC_HANDSHAKE) {
626*4724848cSchristos insecret = s->handshake_secret;
627*4724848cSchristos finsecret = s->server_finished_secret;
628*4724848cSchristos finsecretlen = EVP_MD_size(ssl_handshake_md(s));
629*4724848cSchristos label = server_handshake_traffic;
630*4724848cSchristos labellen = sizeof(server_handshake_traffic) - 1;
631*4724848cSchristos log_label = SERVER_HANDSHAKE_LABEL;
632*4724848cSchristos } else {
633*4724848cSchristos insecret = s->master_secret;
634*4724848cSchristos label = server_application_traffic;
635*4724848cSchristos labellen = sizeof(server_application_traffic) - 1;
636*4724848cSchristos log_label = SERVER_APPLICATION_LABEL;
637*4724848cSchristos }
638*4724848cSchristos }
639*4724848cSchristos
640*4724848cSchristos if (!(which & SSL3_CC_EARLY)) {
641*4724848cSchristos md = ssl_handshake_md(s);
642*4724848cSchristos cipher = s->s3->tmp.new_sym_enc;
643*4724848cSchristos if (!ssl3_digest_cached_records(s, 1)
644*4724848cSchristos || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
645*4724848cSchristos /* SSLfatal() already called */;
646*4724848cSchristos goto err;
647*4724848cSchristos }
648*4724848cSchristos }
649*4724848cSchristos
650*4724848cSchristos /*
651*4724848cSchristos * Save the hash of handshakes up to now for use when we calculate the
652*4724848cSchristos * client application traffic secret
653*4724848cSchristos */
654*4724848cSchristos if (label == server_application_traffic)
655*4724848cSchristos memcpy(s->server_finished_hash, hashval, hashlen);
656*4724848cSchristos
657*4724848cSchristos if (label == server_handshake_traffic)
658*4724848cSchristos memcpy(s->handshake_traffic_hash, hashval, hashlen);
659*4724848cSchristos
660*4724848cSchristos if (label == client_application_traffic) {
661*4724848cSchristos /*
662*4724848cSchristos * We also create the resumption master secret, but this time use the
663*4724848cSchristos * hash for the whole handshake including the Client Finished
664*4724848cSchristos */
665*4724848cSchristos if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
666*4724848cSchristos resumption_master_secret,
667*4724848cSchristos sizeof(resumption_master_secret) - 1,
668*4724848cSchristos hashval, hashlen, s->resumption_master_secret,
669*4724848cSchristos hashlen, 1)) {
670*4724848cSchristos /* SSLfatal() already called */
671*4724848cSchristos goto err;
672*4724848cSchristos }
673*4724848cSchristos }
674*4724848cSchristos
675*4724848cSchristos if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
676*4724848cSchristos insecret, hash, label, labellen, secret, iv,
677*4724848cSchristos ciph_ctx)) {
678*4724848cSchristos /* SSLfatal() already called */
679*4724848cSchristos goto err;
680*4724848cSchristos }
681*4724848cSchristos
682*4724848cSchristos if (label == server_application_traffic) {
683*4724848cSchristos memcpy(s->server_app_traffic_secret, secret, hashlen);
684*4724848cSchristos /* Now we create the exporter master secret */
685*4724848cSchristos if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
686*4724848cSchristos exporter_master_secret,
687*4724848cSchristos sizeof(exporter_master_secret) - 1,
688*4724848cSchristos hash, hashlen, s->exporter_master_secret,
689*4724848cSchristos hashlen, 1)) {
690*4724848cSchristos /* SSLfatal() already called */
691*4724848cSchristos goto err;
692*4724848cSchristos }
693*4724848cSchristos
694*4724848cSchristos if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
695*4724848cSchristos hashlen)) {
696*4724848cSchristos /* SSLfatal() already called */
697*4724848cSchristos goto err;
698*4724848cSchristos }
699*4724848cSchristos } else if (label == client_application_traffic)
700*4724848cSchristos memcpy(s->client_app_traffic_secret, secret, hashlen);
701*4724848cSchristos
702*4724848cSchristos if (!ssl_log_secret(s, log_label, secret, hashlen)) {
703*4724848cSchristos /* SSLfatal() already called */
704*4724848cSchristos goto err;
705*4724848cSchristos }
706*4724848cSchristos
707*4724848cSchristos if (finsecret != NULL
708*4724848cSchristos && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
709*4724848cSchristos finsecret, finsecretlen)) {
710*4724848cSchristos /* SSLfatal() already called */
711*4724848cSchristos goto err;
712*4724848cSchristos }
713*4724848cSchristos
714*4724848cSchristos if (!s->server && label == client_early_traffic)
715*4724848cSchristos s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
716*4724848cSchristos else
717*4724848cSchristos s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
718*4724848cSchristos ret = 1;
719*4724848cSchristos err:
720*4724848cSchristos OPENSSL_cleanse(secret, sizeof(secret));
721*4724848cSchristos return ret;
722*4724848cSchristos }
723*4724848cSchristos
tls13_update_key(SSL * s,int sending)724*4724848cSchristos int tls13_update_key(SSL *s, int sending)
725*4724848cSchristos {
726*4724848cSchristos #ifdef CHARSET_EBCDIC
727*4724848cSchristos static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
728*4724848cSchristos #else
729*4724848cSchristos static const unsigned char application_traffic[] = "traffic upd";
730*4724848cSchristos #endif
731*4724848cSchristos const EVP_MD *md = ssl_handshake_md(s);
732*4724848cSchristos size_t hashlen = EVP_MD_size(md);
733*4724848cSchristos unsigned char *insecret, *iv;
734*4724848cSchristos unsigned char secret[EVP_MAX_MD_SIZE];
735*4724848cSchristos EVP_CIPHER_CTX *ciph_ctx;
736*4724848cSchristos int ret = 0;
737*4724848cSchristos
738*4724848cSchristos if (s->server == sending)
739*4724848cSchristos insecret = s->server_app_traffic_secret;
740*4724848cSchristos else
741*4724848cSchristos insecret = s->client_app_traffic_secret;
742*4724848cSchristos
743*4724848cSchristos if (sending) {
744*4724848cSchristos s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
745*4724848cSchristos iv = s->write_iv;
746*4724848cSchristos ciph_ctx = s->enc_write_ctx;
747*4724848cSchristos RECORD_LAYER_reset_write_sequence(&s->rlayer);
748*4724848cSchristos } else {
749*4724848cSchristos iv = s->read_iv;
750*4724848cSchristos ciph_ctx = s->enc_read_ctx;
751*4724848cSchristos RECORD_LAYER_reset_read_sequence(&s->rlayer);
752*4724848cSchristos }
753*4724848cSchristos
754*4724848cSchristos if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
755*4724848cSchristos s->s3->tmp.new_sym_enc, insecret, NULL,
756*4724848cSchristos application_traffic,
757*4724848cSchristos sizeof(application_traffic) - 1, secret, iv,
758*4724848cSchristos ciph_ctx)) {
759*4724848cSchristos /* SSLfatal() already called */
760*4724848cSchristos goto err;
761*4724848cSchristos }
762*4724848cSchristos
763*4724848cSchristos memcpy(insecret, secret, hashlen);
764*4724848cSchristos
765*4724848cSchristos s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
766*4724848cSchristos ret = 1;
767*4724848cSchristos err:
768*4724848cSchristos OPENSSL_cleanse(secret, sizeof(secret));
769*4724848cSchristos return ret;
770*4724848cSchristos }
771*4724848cSchristos
tls13_alert_code(int code)772*4724848cSchristos int tls13_alert_code(int code)
773*4724848cSchristos {
774*4724848cSchristos /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
775*4724848cSchristos if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
776*4724848cSchristos return code;
777*4724848cSchristos
778*4724848cSchristos return tls1_alert_code(code);
779*4724848cSchristos }
780*4724848cSchristos
tls13_export_keying_material(SSL * s,unsigned char * out,size_t olen,const char * label,size_t llen,const unsigned char * context,size_t contextlen,int use_context)781*4724848cSchristos int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
782*4724848cSchristos const char *label, size_t llen,
783*4724848cSchristos const unsigned char *context,
784*4724848cSchristos size_t contextlen, int use_context)
785*4724848cSchristos {
786*4724848cSchristos unsigned char exportsecret[EVP_MAX_MD_SIZE];
787*4724848cSchristos #ifdef CHARSET_EBCDIC
788*4724848cSchristos static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
789*4724848cSchristos #else
790*4724848cSchristos static const unsigned char exporterlabel[] = "exporter";
791*4724848cSchristos #endif
792*4724848cSchristos unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
793*4724848cSchristos const EVP_MD *md = ssl_handshake_md(s);
794*4724848cSchristos EVP_MD_CTX *ctx = EVP_MD_CTX_new();
795*4724848cSchristos unsigned int hashsize, datalen;
796*4724848cSchristos int ret = 0;
797*4724848cSchristos
798*4724848cSchristos if (ctx == NULL || !ossl_statem_export_allowed(s))
799*4724848cSchristos goto err;
800*4724848cSchristos
801*4724848cSchristos if (!use_context)
802*4724848cSchristos contextlen = 0;
803*4724848cSchristos
804*4724848cSchristos if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
805*4724848cSchristos || EVP_DigestUpdate(ctx, context, contextlen) <= 0
806*4724848cSchristos || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
807*4724848cSchristos || EVP_DigestInit_ex(ctx, md, NULL) <= 0
808*4724848cSchristos || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
809*4724848cSchristos || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
810*4724848cSchristos (const unsigned char *)label, llen,
811*4724848cSchristos data, datalen, exportsecret, hashsize, 0)
812*4724848cSchristos || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
813*4724848cSchristos sizeof(exporterlabel) - 1, hash, hashsize,
814*4724848cSchristos out, olen, 0))
815*4724848cSchristos goto err;
816*4724848cSchristos
817*4724848cSchristos ret = 1;
818*4724848cSchristos err:
819*4724848cSchristos EVP_MD_CTX_free(ctx);
820*4724848cSchristos return ret;
821*4724848cSchristos }
822*4724848cSchristos
tls13_export_keying_material_early(SSL * s,unsigned char * out,size_t olen,const char * label,size_t llen,const unsigned char * context,size_t contextlen)823*4724848cSchristos int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
824*4724848cSchristos const char *label, size_t llen,
825*4724848cSchristos const unsigned char *context,
826*4724848cSchristos size_t contextlen)
827*4724848cSchristos {
828*4724848cSchristos #ifdef CHARSET_EBCDIC
829*4724848cSchristos static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
830*4724848cSchristos #else
831*4724848cSchristos static const unsigned char exporterlabel[] = "exporter";
832*4724848cSchristos #endif
833*4724848cSchristos unsigned char exportsecret[EVP_MAX_MD_SIZE];
834*4724848cSchristos unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
835*4724848cSchristos const EVP_MD *md;
836*4724848cSchristos EVP_MD_CTX *ctx = EVP_MD_CTX_new();
837*4724848cSchristos unsigned int hashsize, datalen;
838*4724848cSchristos int ret = 0;
839*4724848cSchristos const SSL_CIPHER *sslcipher;
840*4724848cSchristos
841*4724848cSchristos if (ctx == NULL || !ossl_statem_export_early_allowed(s))
842*4724848cSchristos goto err;
843*4724848cSchristos
844*4724848cSchristos if (!s->server && s->max_early_data > 0
845*4724848cSchristos && s->session->ext.max_early_data == 0)
846*4724848cSchristos sslcipher = SSL_SESSION_get0_cipher(s->psksession);
847*4724848cSchristos else
848*4724848cSchristos sslcipher = SSL_SESSION_get0_cipher(s->session);
849*4724848cSchristos
850*4724848cSchristos md = ssl_md(sslcipher->algorithm2);
851*4724848cSchristos
852*4724848cSchristos /*
853*4724848cSchristos * Calculate the hash value and store it in |data|. The reason why
854*4724848cSchristos * the empty string is used is that the definition of TLS-Exporter
855*4724848cSchristos * is like so:
856*4724848cSchristos *
857*4724848cSchristos * TLS-Exporter(label, context_value, key_length) =
858*4724848cSchristos * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
859*4724848cSchristos * "exporter", Hash(context_value), key_length)
860*4724848cSchristos *
861*4724848cSchristos * Derive-Secret(Secret, Label, Messages) =
862*4724848cSchristos * HKDF-Expand-Label(Secret, Label,
863*4724848cSchristos * Transcript-Hash(Messages), Hash.length)
864*4724848cSchristos *
865*4724848cSchristos * Here Transcript-Hash is the cipher suite hash algorithm.
866*4724848cSchristos */
867*4724848cSchristos if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
868*4724848cSchristos || EVP_DigestUpdate(ctx, context, contextlen) <= 0
869*4724848cSchristos || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
870*4724848cSchristos || EVP_DigestInit_ex(ctx, md, NULL) <= 0
871*4724848cSchristos || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
872*4724848cSchristos || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
873*4724848cSchristos (const unsigned char *)label, llen,
874*4724848cSchristos data, datalen, exportsecret, hashsize, 0)
875*4724848cSchristos || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
876*4724848cSchristos sizeof(exporterlabel) - 1, hash, hashsize,
877*4724848cSchristos out, olen, 0))
878*4724848cSchristos goto err;
879*4724848cSchristos
880*4724848cSchristos ret = 1;
881*4724848cSchristos err:
882*4724848cSchristos EVP_MD_CTX_free(ctx);
883*4724848cSchristos return ret;
884*4724848cSchristos }
885