]>
Commit | Line | Data |
---|---|---|
34574f19 | 1 | /* |
33388b44 | 2 | * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved. |
34574f19 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
34574f19 MC |
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> | |
706457b7 | 11 | #include "ssl_local.h" |
cc10b56d VF |
12 | #include "internal/ktls.h" |
13 | #include "record/record_local.h" | |
08717544 | 14 | #include "internal/cryptlib.h" |
34574f19 MC |
15 | #include <openssl/evp.h> |
16 | #include <openssl/kdf.h> | |
ce3b1bb4 | 17 | #include <openssl/core_names.h> |
34574f19 | 18 | |
0fb2815b | 19 | #define TLS13_MAX_LABEL_LEN 249 |
34574f19 MC |
20 | |
21 | /* Always filled with zeros */ | |
22 | static const unsigned char default_zeros[EVP_MAX_MD_SIZE]; | |
23 | ||
34574f19 | 24 | /* |
a19ae67d MC |
25 | * Given a |secret|; a |label| of length |labellen|; and |data| of length |
26 | * |datalen| (e.g. typically a hash of the handshake messages), derive a new | |
27 | * secret |outlen| bytes long and store it in the location pointed to be |out|. | |
0fb2815b MC |
28 | * The |data| value may be zero length. Any errors will be treated as fatal if |
29 | * |fatal| is set. Returns 1 on success 0 on failure. | |
34574f19 | 30 | */ |
ec15acb6 | 31 | int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret, |
34574f19 | 32 | const unsigned char *label, size_t labellen, |
a19ae67d | 33 | const unsigned char *data, size_t datalen, |
0fb2815b | 34 | unsigned char *out, size_t outlen, int fatal) |
34574f19 | 35 | { |
48102247 | 36 | #ifdef CHARSET_EBCDIC |
37 | static const unsigned char label_prefix[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 }; | |
38 | #else | |
0fb2815b | 39 | static const unsigned char label_prefix[] = "tls13 "; |
48102247 | 40 | #endif |
c8f6c28a MC |
41 | EVP_KDF *kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_HKDF, |
42 | s->ctx->propq); | |
ce3b1bb4 P |
43 | EVP_KDF_CTX *kctx; |
44 | OSSL_PARAM params[5], *p = params; | |
45 | int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY; | |
46 | const char *mdname = EVP_MD_name(md); | |
34574f19 MC |
47 | int ret; |
48 | size_t hkdflabellen; | |
49 | size_t hashlen; | |
50 | /* | |
0fb2815b MC |
51 | * 2 bytes for length of derived secret + 1 byte for length of combined |
52 | * prefix and label + bytes for the label itself + 1 byte length of hash | |
53 | * + bytes for the hash itself | |
34574f19 | 54 | */ |
2de5a5fb | 55 | unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) |
0b4233f5 | 56 | + (sizeof(label_prefix) - 1) + TLS13_MAX_LABEL_LEN |
0fb2815b | 57 | + 1 + EVP_MAX_MD_SIZE]; |
34574f19 MC |
58 | WPACKET pkt; |
59 | ||
765d04c9 | 60 | kctx = EVP_KDF_new_ctx(kdf); |
ce3b1bb4 | 61 | EVP_KDF_free(kdf); |
32495464 | 62 | if (kctx == NULL) |
34574f19 MC |
63 | return 0; |
64 | ||
0fb2815b MC |
65 | if (labellen > TLS13_MAX_LABEL_LEN) { |
66 | if (fatal) { | |
67 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND, | |
68 | ERR_R_INTERNAL_ERROR); | |
69 | } else { | |
70 | /* | |
71 | * Probably we have been called from SSL_export_keying_material(), | |
72 | * or SSL_export_keying_material_early(). | |
73 | */ | |
74 | SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); | |
75 | } | |
765d04c9 | 76 | EVP_KDF_free_ctx(kctx); |
0fb2815b MC |
77 | return 0; |
78 | } | |
79 | ||
34574f19 MC |
80 | hashlen = EVP_MD_size(md); |
81 | ||
82 | if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0) | |
83 | || !WPACKET_put_bytes_u16(&pkt, outlen) | |
84 | || !WPACKET_start_sub_packet_u8(&pkt) | |
85 | || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1) | |
86 | || !WPACKET_memcpy(&pkt, label, labellen) | |
87 | || !WPACKET_close(&pkt) | |
a19ae67d | 88 | || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen) |
34574f19 MC |
89 | || !WPACKET_get_total_written(&pkt, &hkdflabellen) |
90 | || !WPACKET_finish(&pkt)) { | |
765d04c9 | 91 | EVP_KDF_free_ctx(kctx); |
34574f19 | 92 | WPACKET_cleanup(&pkt); |
0fb2815b MC |
93 | if (fatal) |
94 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND, | |
95 | ERR_R_INTERNAL_ERROR); | |
96 | else | |
97 | SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR); | |
34574f19 MC |
98 | return 0; |
99 | } | |
100 | ||
ce3b1bb4 P |
101 | *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
102 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, | |
8b6ffd40 | 103 | (char *)mdname, 0); |
ce3b1bb4 P |
104 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, |
105 | (unsigned char *)secret, hashlen); | |
106 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, | |
107 | hkdflabel, hkdflabellen); | |
108 | *p++ = OSSL_PARAM_construct_end(); | |
109 | ||
765d04c9 | 110 | ret = EVP_KDF_set_ctx_params(kctx, params) <= 0 |
32495464 | 111 | || EVP_KDF_derive(kctx, out, outlen) <= 0; |
34574f19 | 112 | |
765d04c9 | 113 | EVP_KDF_free_ctx(kctx); |
34574f19 | 114 | |
0fb2815b MC |
115 | if (ret != 0) { |
116 | if (fatal) | |
117 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND, | |
118 | ERR_R_INTERNAL_ERROR); | |
119 | else | |
120 | SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR); | |
121 | } | |
f63a17d6 | 122 | |
34574f19 MC |
123 | return ret == 0; |
124 | } | |
125 | ||
34574f19 | 126 | /* |
f5ca0b04 MC |
127 | * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on |
128 | * success 0 on failure. | |
34574f19 | 129 | */ |
d49e23ec MC |
130 | int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret, |
131 | unsigned char *key, size_t keylen) | |
34574f19 | 132 | { |
48102247 | 133 | #ifdef CHARSET_EBCDIC |
134 | static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 }; | |
135 | #else | |
136 | static const unsigned char keylabel[] = "key"; | |
137 | #endif | |
f5ca0b04 | 138 | |
d49e23ec | 139 | return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1, |
0fb2815b | 140 | NULL, 0, key, keylen, 1); |
34574f19 MC |
141 | } |
142 | ||
143 | /* | |
f5ca0b04 MC |
144 | * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on |
145 | * success 0 on failure. | |
34574f19 | 146 | */ |
d49e23ec MC |
147 | int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret, |
148 | unsigned char *iv, size_t ivlen) | |
34574f19 | 149 | { |
48102247 | 150 | #ifdef CHARSET_EBCDIC |
151 | static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 }; | |
152 | #else | |
153 | static const unsigned char ivlabel[] = "iv"; | |
154 | #endif | |
f5ca0b04 | 155 | |
d49e23ec | 156 | return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1, |
0fb2815b | 157 | NULL, 0, iv, ivlen, 1); |
34574f19 MC |
158 | } |
159 | ||
ec15acb6 MC |
160 | int tls13_derive_finishedkey(SSL *s, const EVP_MD *md, |
161 | const unsigned char *secret, | |
162 | unsigned char *fin, size_t finlen) | |
6484776f | 163 | { |
48102247 | 164 | #ifdef CHARSET_EBCDIC |
165 | static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 }; | |
166 | #else | |
167 | static const unsigned char finishedlabel[] = "finished"; | |
168 | #endif | |
f5ca0b04 | 169 | |
ec15acb6 | 170 | return tls13_hkdf_expand(s, md, secret, finishedlabel, |
0fb2815b | 171 | sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1); |
6484776f MC |
172 | } |
173 | ||
34574f19 MC |
174 | /* |
175 | * Given the previous secret |prevsecret| and a new input secret |insecret| of | |
176 | * length |insecretlen|, generate a new secret and store it in the location | |
f5ca0b04 | 177 | * pointed to by |outsecret|. Returns 1 on success 0 on failure. |
34574f19 | 178 | */ |
ec15acb6 MC |
179 | int tls13_generate_secret(SSL *s, const EVP_MD *md, |
180 | const unsigned char *prevsecret, | |
181 | const unsigned char *insecret, | |
182 | size_t insecretlen, | |
183 | unsigned char *outsecret) | |
34574f19 | 184 | { |
34574f19 | 185 | size_t mdlen, prevsecretlen; |
bceae201 | 186 | int mdleni; |
34574f19 | 187 | int ret; |
ce3b1bb4 P |
188 | EVP_KDF *kdf; |
189 | EVP_KDF_CTX *kctx; | |
190 | OSSL_PARAM params[5], *p = params; | |
191 | int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY; | |
192 | const char *mdname = EVP_MD_name(md); | |
48102247 | 193 | #ifdef CHARSET_EBCDIC |
194 | static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 }; | |
195 | #else | |
17aa119e | 196 | static const char derived_secret_label[] = "derived"; |
48102247 | 197 | #endif |
3e0458fb | 198 | unsigned char preextractsec[EVP_MAX_MD_SIZE]; |
34574f19 | 199 | |
c8f6c28a | 200 | kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_HKDF, s->ctx->propq); |
765d04c9 | 201 | kctx = EVP_KDF_new_ctx(kdf); |
ce3b1bb4 | 202 | EVP_KDF_free(kdf); |
32495464 | 203 | if (kctx == NULL) { |
f63a17d6 MC |
204 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, |
205 | ERR_R_INTERNAL_ERROR); | |
34574f19 | 206 | return 0; |
f63a17d6 | 207 | } |
34574f19 | 208 | |
bceae201 MC |
209 | mdleni = EVP_MD_size(md); |
210 | /* Ensure cast to size_t is safe */ | |
211 | if (!ossl_assert(mdleni >= 0)) { | |
212 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, | |
213 | ERR_R_INTERNAL_ERROR); | |
765d04c9 | 214 | EVP_KDF_free_ctx(kctx); |
bceae201 MC |
215 | return 0; |
216 | } | |
217 | mdlen = (size_t)mdleni; | |
34574f19 MC |
218 | |
219 | if (insecret == NULL) { | |
220 | insecret = default_zeros; | |
221 | insecretlen = mdlen; | |
222 | } | |
223 | if (prevsecret == NULL) { | |
224 | prevsecret = default_zeros; | |
225 | prevsecretlen = 0; | |
226 | } else { | |
3e0458fb MC |
227 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
228 | unsigned char hash[EVP_MAX_MD_SIZE]; | |
229 | ||
230 | /* The pre-extract derive step uses a hash of no messages */ | |
231 | if (mctx == NULL | |
232 | || EVP_DigestInit_ex(mctx, md, NULL) <= 0 | |
233 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { | |
f63a17d6 MC |
234 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, |
235 | ERR_R_INTERNAL_ERROR); | |
3e0458fb | 236 | EVP_MD_CTX_free(mctx); |
765d04c9 | 237 | EVP_KDF_free_ctx(kctx); |
3e0458fb MC |
238 | return 0; |
239 | } | |
240 | EVP_MD_CTX_free(mctx); | |
241 | ||
242 | /* Generate the pre-extract secret */ | |
243 | if (!tls13_hkdf_expand(s, md, prevsecret, | |
244 | (unsigned char *)derived_secret_label, | |
a19ae67d | 245 | sizeof(derived_secret_label) - 1, hash, mdlen, |
0fb2815b | 246 | preextractsec, mdlen, 1)) { |
f63a17d6 | 247 | /* SSLfatal() already called */ |
765d04c9 | 248 | EVP_KDF_free_ctx(kctx); |
3e0458fb | 249 | return 0; |
71d8c138 | 250 | } |
3e0458fb MC |
251 | |
252 | prevsecret = preextractsec; | |
34574f19 MC |
253 | prevsecretlen = mdlen; |
254 | } | |
255 | ||
ce3b1bb4 P |
256 | *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
257 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, | |
8b6ffd40 | 258 | (char *)mdname, 0); |
ce3b1bb4 P |
259 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, |
260 | (unsigned char *)insecret, | |
261 | insecretlen); | |
262 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, | |
263 | (unsigned char *)prevsecret, | |
264 | prevsecretlen); | |
265 | *p++ = OSSL_PARAM_construct_end(); | |
266 | ||
765d04c9 | 267 | ret = EVP_KDF_set_ctx_params(kctx, params) <= 0 |
32495464 | 268 | || EVP_KDF_derive(kctx, outsecret, mdlen) <= 0; |
34574f19 | 269 | |
f63a17d6 MC |
270 | if (ret != 0) |
271 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, | |
272 | ERR_R_INTERNAL_ERROR); | |
273 | ||
765d04c9 | 274 | EVP_KDF_free_ctx(kctx); |
3e0458fb MC |
275 | if (prevsecret == preextractsec) |
276 | OPENSSL_cleanse(preextractsec, mdlen); | |
34574f19 MC |
277 | return ret == 0; |
278 | } | |
279 | ||
34574f19 MC |
280 | /* |
281 | * Given an input secret |insecret| of length |insecretlen| generate the | |
282 | * handshake secret. This requires the early secret to already have been | |
f5ca0b04 | 283 | * generated. Returns 1 on success 0 on failure. |
34574f19 MC |
284 | */ |
285 | int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret, | |
286 | size_t insecretlen) | |
287 | { | |
f63a17d6 | 288 | /* Calls SSLfatal() if required */ |
ec15acb6 MC |
289 | return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret, |
290 | insecret, insecretlen, | |
34574f19 MC |
291 | (unsigned char *)&s->handshake_secret); |
292 | } | |
293 | ||
294 | /* | |
295 | * Given the handshake secret |prev| of length |prevlen| generate the master | |
f5ca0b04 MC |
296 | * secret and store its length in |*secret_size|. Returns 1 on success 0 on |
297 | * failure. | |
34574f19 MC |
298 | */ |
299 | int tls13_generate_master_secret(SSL *s, unsigned char *out, | |
300 | unsigned char *prev, size_t prevlen, | |
301 | size_t *secret_size) | |
302 | { | |
ec15acb6 MC |
303 | const EVP_MD *md = ssl_handshake_md(s); |
304 | ||
305 | *secret_size = EVP_MD_size(md); | |
f63a17d6 | 306 | /* Calls SSLfatal() if required */ |
ec15acb6 | 307 | return tls13_generate_secret(s, md, prev, NULL, 0, out); |
34574f19 MC |
308 | } |
309 | ||
92760c21 | 310 | /* |
f5ca0b04 MC |
311 | * Generates the mac for the Finished message. Returns the length of the MAC or |
312 | * 0 on error. | |
92760c21 MC |
313 | */ |
314 | size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen, | |
315 | unsigned char *out) | |
316 | { | |
c8f6c28a MC |
317 | const char *mdname = EVP_MD_name(ssl_handshake_md(s)); |
318 | EVP_MAC *hmac = EVP_MAC_fetch(s->ctx->libctx, "HMAC", s->ctx->propq); | |
6484776f | 319 | unsigned char hash[EVP_MAX_MD_SIZE]; |
c8f6c28a | 320 | unsigned char finsecret[EVP_MAX_MD_SIZE]; |
6484776f | 321 | size_t hashlen, ret = 0; |
c8f6c28a MC |
322 | EVP_MAC_CTX *ctx = NULL; |
323 | OSSL_PARAM params[4], *p = params; | |
324 | ||
325 | if (hmac == NULL) { | |
326 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC, | |
327 | ERR_R_INTERNAL_ERROR); | |
328 | goto err; | |
329 | } | |
330 | ||
331 | /* Safe to cast away const here since we're not "getting" any data */ | |
332 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, | |
333 | (char *)mdname, 0); | |
334 | if (s->ctx->propq != NULL) | |
335 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES, | |
336 | (char *)s->ctx->propq, | |
337 | 0); | |
92760c21 | 338 | |
d4d2f3a4 MC |
339 | if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) { |
340 | /* SSLfatal() already called */ | |
6484776f | 341 | goto err; |
d4d2f3a4 | 342 | } |
6484776f | 343 | |
de9f5b35 | 344 | if (str == s->method->ssl3_enc->server_finished_label) { |
c8f6c28a MC |
345 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, |
346 | s->server_finished_secret, | |
347 | hashlen); | |
de9f5b35 | 348 | } else if (SSL_IS_FIRST_HANDSHAKE(s)) { |
c8f6c28a MC |
349 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, |
350 | s->client_finished_secret, | |
351 | hashlen); | |
de9f5b35 | 352 | } else { |
de9f5b35 MC |
353 | if (!tls13_derive_finishedkey(s, ssl_handshake_md(s), |
354 | s->client_app_traffic_secret, | |
355 | finsecret, hashlen)) | |
356 | goto err; | |
357 | ||
c8f6c28a MC |
358 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, finsecret, |
359 | hashlen); | |
de9f5b35 | 360 | } |
c8f6c28a | 361 | *p++ = OSSL_PARAM_construct_end(); |
6484776f | 362 | |
865adf97 | 363 | ctx = EVP_MAC_CTX_new(hmac); |
c8f6c28a | 364 | if (ctx == NULL |
865adf97 | 365 | || !EVP_MAC_CTX_set_params(ctx, params) |
c8f6c28a MC |
366 | || !EVP_MAC_init(ctx) |
367 | || !EVP_MAC_update(ctx, hash, hashlen) | |
368 | /* outsize as per sizeof(peer_finish_md) */ | |
369 | || !EVP_MAC_final(ctx, out, &hashlen, EVP_MAX_MD_SIZE * 2)) { | |
d4d2f3a4 MC |
370 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC, |
371 | ERR_R_INTERNAL_ERROR); | |
6484776f | 372 | goto err; |
d4d2f3a4 | 373 | } |
92760c21 | 374 | |
6484776f MC |
375 | ret = hashlen; |
376 | err: | |
c8f6c28a | 377 | OPENSSL_cleanse(finsecret, sizeof(finsecret)); |
865adf97 | 378 | EVP_MAC_CTX_free(ctx); |
c8f6c28a | 379 | EVP_MAC_free(hmac); |
6484776f | 380 | return ret; |
92760c21 MC |
381 | } |
382 | ||
383 | /* | |
384 | * There isn't really a key block in TLSv1.3, but we still need this function | |
f5ca0b04 | 385 | * for initialising the cipher and hash. Returns 1 on success or 0 on failure. |
92760c21 MC |
386 | */ |
387 | int tls13_setup_key_block(SSL *s) | |
388 | { | |
389 | const EVP_CIPHER *c; | |
390 | const EVP_MD *hash; | |
92760c21 | 391 | |
555cbb32 | 392 | s->session->cipher = s->s3.tmp.new_cipher; |
c8f6c28a MC |
393 | if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, NULL, |
394 | 0)) { | |
f63a17d6 MC |
395 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK, |
396 | SSL_R_CIPHER_OR_HASH_UNAVAILABLE); | |
92760c21 MC |
397 | return 0; |
398 | } | |
399 | ||
c8f6c28a | 400 | ssl_evp_cipher_free(s->s3.tmp.new_sym_enc); |
555cbb32 | 401 | s->s3.tmp.new_sym_enc = c; |
c8f6c28a | 402 | ssl_evp_md_free(s->s3.tmp.new_hash); |
555cbb32 | 403 | s->s3.tmp.new_hash = hash; |
92760c21 MC |
404 | |
405 | return 1; | |
406 | } | |
407 | ||
d1186c30 | 408 | static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md, |
d49e23ec | 409 | const EVP_CIPHER *ciph, |
57389a32 MC |
410 | const unsigned char *insecret, |
411 | const unsigned char *hash, | |
412 | const unsigned char *label, | |
413 | size_t labellen, unsigned char *secret, | |
cc10b56d VF |
414 | unsigned char *key, unsigned char *iv, |
415 | EVP_CIPHER_CTX *ciph_ctx) | |
57389a32 | 416 | { |
57389a32 | 417 | size_t ivlen, keylen, taglen; |
bceae201 MC |
418 | int hashleni = EVP_MD_size(md); |
419 | size_t hashlen; | |
420 | ||
421 | /* Ensure cast to size_t is safe */ | |
422 | if (!ossl_assert(hashleni >= 0)) { | |
423 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV, | |
424 | ERR_R_EVP_LIB); | |
cc10b56d | 425 | return 0; |
bceae201 MC |
426 | } |
427 | hashlen = (size_t)hashleni; | |
57389a32 | 428 | |
a19ae67d | 429 | if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen, |
0fb2815b | 430 | secret, hashlen, 1)) { |
f63a17d6 | 431 | /* SSLfatal() already called */ |
cc10b56d | 432 | return 0; |
57389a32 MC |
433 | } |
434 | ||
435 | /* TODO(size_t): convert me */ | |
436 | keylen = EVP_CIPHER_key_length(ciph); | |
437 | if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) { | |
c117af67 MC |
438 | uint32_t algenc; |
439 | ||
57389a32 | 440 | ivlen = EVP_CCM_TLS_IV_LEN; |
2e1a4f6a | 441 | if (s->s3.tmp.new_cipher != NULL) { |
442 | algenc = s->s3.tmp.new_cipher->algorithm_enc; | |
443 | } else if (s->session->cipher != NULL) { | |
c117af67 MC |
444 | /* We've not selected a cipher yet - we must be doing early data */ |
445 | algenc = s->session->cipher->algorithm_enc; | |
2e1a4f6a | 446 | } else if (s->psksession != NULL && s->psksession->cipher != NULL) { |
447 | /* We must be doing early data with out-of-band PSK */ | |
448 | algenc = s->psksession->cipher->algorithm_enc; | |
c117af67 | 449 | } else { |
2e1a4f6a | 450 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV, |
451 | ERR_R_EVP_LIB); | |
cc10b56d | 452 | return 0; |
c117af67 MC |
453 | } |
454 | if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8)) | |
57389a32 MC |
455 | taglen = EVP_CCM8_TLS_TAG_LEN; |
456 | else | |
457 | taglen = EVP_CCM_TLS_TAG_LEN; | |
458 | } else { | |
459 | ivlen = EVP_CIPHER_iv_length(ciph); | |
460 | taglen = 0; | |
461 | } | |
462 | ||
d49e23ec MC |
463 | if (!tls13_derive_key(s, md, secret, key, keylen) |
464 | || !tls13_derive_iv(s, md, secret, iv, ivlen)) { | |
f63a17d6 | 465 | /* SSLfatal() already called */ |
cc10b56d | 466 | return 0; |
57389a32 MC |
467 | } |
468 | ||
d1186c30 | 469 | if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0 |
57389a32 MC |
470 | || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) |
471 | || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, | |
472 | taglen, NULL)) | |
473 | || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) { | |
f63a17d6 MC |
474 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV, |
475 | ERR_R_EVP_LIB); | |
cc10b56d | 476 | return 0; |
57389a32 MC |
477 | } |
478 | ||
57389a32 | 479 | return 1; |
57389a32 MC |
480 | } |
481 | ||
0d9824c1 MC |
482 | int tls13_change_cipher_state(SSL *s, int which) |
483 | { | |
48102247 | 484 | #ifdef CHARSET_EBCDIC |
485 | static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00}; | |
486 | static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00}; | |
487 | static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00}; | |
488 | static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00}; | |
489 | static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00}; | |
490 | static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00}; | |
491 | static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00}; | |
492 | static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00}; | |
493 | #else | |
17aa119e MC |
494 | static const unsigned char client_early_traffic[] = "c e traffic"; |
495 | static const unsigned char client_handshake_traffic[] = "c hs traffic"; | |
496 | static const unsigned char client_application_traffic[] = "c ap traffic"; | |
497 | static const unsigned char server_handshake_traffic[] = "s hs traffic"; | |
498 | static const unsigned char server_application_traffic[] = "s ap traffic"; | |
0ca8d1ec | 499 | static const unsigned char exporter_master_secret[] = "exp master"; |
17aa119e | 500 | static const unsigned char resumption_master_secret[] = "res master"; |
b38ede80 | 501 | static const unsigned char early_exporter_master_secret[] = "e exp master"; |
48102247 | 502 | #endif |
bebc0c7d | 503 | unsigned char *iv; |
cc10b56d | 504 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
0d9824c1 | 505 | unsigned char secret[EVP_MAX_MD_SIZE]; |
ace081c1 MC |
506 | unsigned char hashval[EVP_MAX_MD_SIZE]; |
507 | unsigned char *hash = hashval; | |
0d9824c1 | 508 | unsigned char *insecret; |
6484776f | 509 | unsigned char *finsecret = NULL; |
2c7bd692 | 510 | const char *log_label = NULL; |
0d9824c1 | 511 | EVP_CIPHER_CTX *ciph_ctx; |
57389a32 | 512 | size_t finsecretlen = 0; |
0d9824c1 | 513 | const unsigned char *label; |
ace081c1 | 514 | size_t labellen, hashlen = 0; |
6530c490 | 515 | int ret = 0; |
42f50fdf MC |
516 | const EVP_MD *md = NULL; |
517 | const EVP_CIPHER *cipher = NULL; | |
cc10b56d VF |
518 | #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13) |
519 | # ifndef __FreeBSD__ | |
520 | struct tls_crypto_info_all crypto_info; | |
521 | BIO *bio; | |
522 | # endif | |
523 | #endif | |
0d9824c1 MC |
524 | |
525 | if (which & SSL3_CC_READ) { | |
526 | if (s->enc_read_ctx != NULL) { | |
527 | EVP_CIPHER_CTX_reset(s->enc_read_ctx); | |
528 | } else { | |
529 | s->enc_read_ctx = EVP_CIPHER_CTX_new(); | |
530 | if (s->enc_read_ctx == NULL) { | |
f63a17d6 MC |
531 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
532 | SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
0d9824c1 MC |
533 | goto err; |
534 | } | |
535 | } | |
536 | ciph_ctx = s->enc_read_ctx; | |
bebc0c7d | 537 | iv = s->read_iv; |
0d9824c1 MC |
538 | |
539 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
540 | } else { | |
7426cd34 | 541 | s->statem.enc_write_state = ENC_WRITE_STATE_INVALID; |
0d9824c1 MC |
542 | if (s->enc_write_ctx != NULL) { |
543 | EVP_CIPHER_CTX_reset(s->enc_write_ctx); | |
544 | } else { | |
545 | s->enc_write_ctx = EVP_CIPHER_CTX_new(); | |
546 | if (s->enc_write_ctx == NULL) { | |
f63a17d6 MC |
547 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
548 | SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
0d9824c1 MC |
549 | goto err; |
550 | } | |
551 | } | |
552 | ciph_ctx = s->enc_write_ctx; | |
bebc0c7d | 553 | iv = s->write_iv; |
0d9824c1 MC |
554 | |
555 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
556 | } | |
557 | ||
558 | if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE)) | |
559 | || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) { | |
d49e23ec MC |
560 | if (which & SSL3_CC_EARLY) { |
561 | EVP_MD_CTX *mdctx = NULL; | |
562 | long handlen; | |
563 | void *hdata; | |
564 | unsigned int hashlenui; | |
565 | const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session); | |
566 | ||
567 | insecret = s->early_secret; | |
568 | label = client_early_traffic; | |
569 | labellen = sizeof(client_early_traffic) - 1; | |
570 | log_label = CLIENT_EARLY_LABEL; | |
571 | ||
555cbb32 | 572 | handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata); |
d49e23ec | 573 | if (handlen <= 0) { |
f63a17d6 MC |
574 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
575 | SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
576 | SSL_R_BAD_HANDSHAKE_LENGTH); | |
d49e23ec MC |
577 | goto err; |
578 | } | |
add8d0e9 | 579 | |
08717544 MC |
580 | if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
581 | && s->max_early_data > 0 | |
582 | && s->session->ext.max_early_data == 0) { | |
583 | /* | |
584 | * If we are attempting to send early data, and we've decided to | |
585 | * actually do it but max_early_data in s->session is 0 then we | |
586 | * must be using an external PSK. | |
587 | */ | |
588 | if (!ossl_assert(s->psksession != NULL | |
589 | && s->max_early_data == | |
590 | s->psksession->ext.max_early_data)) { | |
f63a17d6 MC |
591 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
592 | SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
593 | ERR_R_INTERNAL_ERROR); | |
08717544 MC |
594 | goto err; |
595 | } | |
add8d0e9 | 596 | sslcipher = SSL_SESSION_get0_cipher(s->psksession); |
08717544 | 597 | } |
d49e23ec | 598 | if (sslcipher == NULL) { |
f63a17d6 MC |
599 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
600 | SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK); | |
d49e23ec MC |
601 | goto err; |
602 | } | |
603 | ||
604 | /* | |
605 | * We need to calculate the handshake digest using the digest from | |
606 | * the session. We haven't yet selected our ciphersuite so we can't | |
607 | * use ssl_handshake_md(). | |
608 | */ | |
609 | mdctx = EVP_MD_CTX_new(); | |
610 | if (mdctx == NULL) { | |
f63a17d6 MC |
611 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
612 | SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
d49e23ec MC |
613 | goto err; |
614 | } | |
9727f4e7 MC |
615 | |
616 | /* | |
617 | * This ups the ref count on cipher so we better make sure we free | |
618 | * it again | |
619 | */ | |
620 | if (!ssl_cipher_get_evp_cipher(s->ctx, sslcipher, &cipher)) { | |
621 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, | |
622 | SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
623 | SSL_R_ALGORITHM_FETCH_FAILED); | |
67d744b9 | 624 | EVP_MD_CTX_free(mdctx); |
9727f4e7 MC |
625 | goto err; |
626 | } | |
627 | ||
c8f6c28a | 628 | md = ssl_md(s->ctx, sslcipher->algorithm2); |
d49e23ec MC |
629 | if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL) |
630 | || !EVP_DigestUpdate(mdctx, hdata, handlen) | |
631 | || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) { | |
f63a17d6 MC |
632 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
633 | SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
d49e23ec MC |
634 | EVP_MD_CTX_free(mdctx); |
635 | goto err; | |
636 | } | |
637 | hashlen = hashlenui; | |
638 | EVP_MD_CTX_free(mdctx); | |
b38ede80 TT |
639 | |
640 | if (!tls13_hkdf_expand(s, md, insecret, | |
641 | early_exporter_master_secret, | |
642 | sizeof(early_exporter_master_secret) - 1, | |
643 | hashval, hashlen, | |
0fb2815b MC |
644 | s->early_exporter_master_secret, hashlen, |
645 | 1)) { | |
b38ede80 TT |
646 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
647 | SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
648 | goto err; | |
649 | } | |
01a2a654 PW |
650 | |
651 | if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL, | |
652 | s->early_exporter_master_secret, hashlen)) { | |
653 | /* SSLfatal() already called */ | |
654 | goto err; | |
655 | } | |
d49e23ec | 656 | } else if (which & SSL3_CC_HANDSHAKE) { |
0d9824c1 | 657 | insecret = s->handshake_secret; |
6484776f | 658 | finsecret = s->client_finished_secret; |
6612d87b | 659 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
660 | label = client_handshake_traffic; |
661 | labellen = sizeof(client_handshake_traffic) - 1; | |
2c7bd692 | 662 | log_label = CLIENT_HANDSHAKE_LABEL; |
fe5e20fd | 663 | /* |
69687aa8 | 664 | * The handshake hash used for the server read/client write handshake |
f7e393be MC |
665 | * traffic secret is the same as the hash for the server |
666 | * write/client read handshake traffic secret. However, if we | |
667 | * processed early data then we delay changing the server | |
668 | * read/client write cipher state until later, and the handshake | |
669 | * hashes have moved on. Therefore we use the value saved earlier | |
670 | * when we did the server write/client read change cipher state. | |
fe5e20fd | 671 | */ |
f7e393be | 672 | hash = s->handshake_traffic_hash; |
0d9824c1 | 673 | } else { |
ec15acb6 | 674 | insecret = s->master_secret; |
0d9824c1 MC |
675 | label = client_application_traffic; |
676 | labellen = sizeof(client_application_traffic) - 1; | |
2c7bd692 | 677 | log_label = CLIENT_APPLICATION_LABEL; |
ace081c1 MC |
678 | /* |
679 | * For this we only use the handshake hashes up until the server | |
680 | * Finished hash. We do not include the client's Finished, which is | |
681 | * what ssl_handshake_hash() would give us. Instead we use the | |
682 | * previously saved value. | |
683 | */ | |
684 | hash = s->server_finished_hash; | |
0d9824c1 MC |
685 | } |
686 | } else { | |
d49e23ec | 687 | /* Early data never applies to client-read/server-write */ |
0d9824c1 MC |
688 | if (which & SSL3_CC_HANDSHAKE) { |
689 | insecret = s->handshake_secret; | |
6484776f | 690 | finsecret = s->server_finished_secret; |
6612d87b | 691 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
692 | label = server_handshake_traffic; |
693 | labellen = sizeof(server_handshake_traffic) - 1; | |
2c7bd692 | 694 | log_label = SERVER_HANDSHAKE_LABEL; |
0d9824c1 | 695 | } else { |
ec15acb6 | 696 | insecret = s->master_secret; |
0d9824c1 MC |
697 | label = server_application_traffic; |
698 | labellen = sizeof(server_application_traffic) - 1; | |
2c7bd692 | 699 | log_label = SERVER_APPLICATION_LABEL; |
0d9824c1 MC |
700 | } |
701 | } | |
702 | ||
d49e23ec MC |
703 | if (!(which & SSL3_CC_EARLY)) { |
704 | md = ssl_handshake_md(s); | |
555cbb32 | 705 | cipher = s->s3.tmp.new_sym_enc; |
d49e23ec MC |
706 | if (!ssl3_digest_cached_records(s, 1) |
707 | || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) { | |
f63a17d6 | 708 | /* SSLfatal() already called */; |
d49e23ec MC |
709 | goto err; |
710 | } | |
ace081c1 MC |
711 | } |
712 | ||
ec15acb6 MC |
713 | /* |
714 | * Save the hash of handshakes up to now for use when we calculate the | |
715 | * client application traffic secret | |
716 | */ | |
717 | if (label == server_application_traffic) | |
718 | memcpy(s->server_finished_hash, hashval, hashlen); | |
719 | ||
f7e393be | 720 | if (label == server_handshake_traffic) |
fe5e20fd MC |
721 | memcpy(s->handshake_traffic_hash, hashval, hashlen); |
722 | ||
ec15acb6 MC |
723 | if (label == client_application_traffic) { |
724 | /* | |
725 | * We also create the resumption master secret, but this time use the | |
726 | * hash for the whole handshake including the Client Finished | |
727 | */ | |
728 | if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret, | |
729 | resumption_master_secret, | |
730 | sizeof(resumption_master_secret) - 1, | |
4ff1a526 | 731 | hashval, hashlen, s->resumption_master_secret, |
0fb2815b | 732 | hashlen, 1)) { |
f63a17d6 | 733 | /* SSLfatal() already called */ |
ec15acb6 MC |
734 | goto err; |
735 | } | |
ec15acb6 MC |
736 | } |
737 | ||
cc10b56d VF |
738 | /* check whether cipher is known */ |
739 | if(!ossl_assert(cipher != NULL)) | |
740 | goto err; | |
741 | ||
d49e23ec | 742 | if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher, |
cc10b56d VF |
743 | insecret, hash, label, labellen, secret, key, |
744 | iv, ciph_ctx)) { | |
f63a17d6 | 745 | /* SSLfatal() already called */ |
57389a32 | 746 | goto err; |
ec07b1d8 | 747 | } |
0d9824c1 | 748 | |
2221ec10 | 749 | if (label == server_application_traffic) { |
57389a32 | 750 | memcpy(s->server_app_traffic_secret, secret, hashlen); |
2221ec10 TT |
751 | /* Now we create the exporter master secret */ |
752 | if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret, | |
753 | exporter_master_secret, | |
754 | sizeof(exporter_master_secret) - 1, | |
755 | hash, hashlen, s->exporter_master_secret, | |
0fb2815b | 756 | hashlen, 1)) { |
2221ec10 TT |
757 | /* SSLfatal() already called */ |
758 | goto err; | |
759 | } | |
6329ce8f PW |
760 | |
761 | if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret, | |
762 | hashlen)) { | |
763 | /* SSLfatal() already called */ | |
764 | goto err; | |
765 | } | |
2221ec10 | 766 | } else if (label == client_application_traffic) |
57389a32 MC |
767 | memcpy(s->client_app_traffic_secret, secret, hashlen); |
768 | ||
2c7bd692 | 769 | if (!ssl_log_secret(s, log_label, secret, hashlen)) { |
f63a17d6 | 770 | /* SSLfatal() already called */ |
2c7bd692 CB |
771 | goto err; |
772 | } | |
773 | ||
57389a32 MC |
774 | if (finsecret != NULL |
775 | && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret, | |
776 | finsecret, finsecretlen)) { | |
f63a17d6 | 777 | /* SSLfatal() already called */ |
0d9824c1 MC |
778 | goto err; |
779 | } | |
780 | ||
7426cd34 MC |
781 | if (!s->server && label == client_early_traffic) |
782 | s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS; | |
783 | else | |
784 | s->statem.enc_write_state = ENC_WRITE_STATE_VALID; | |
cc10b56d VF |
785 | #ifndef OPENSSL_NO_KTLS |
786 | # if defined(OPENSSL_KTLS_TLS13) | |
787 | # ifndef __FreeBSD__ | |
788 | if (!(which & SSL3_CC_WRITE) || !(which & SSL3_CC_APPLICATION) | |
789 | || ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX))) | |
790 | goto skip_ktls; | |
791 | ||
792 | /* ktls supports only the maximum fragment size */ | |
793 | if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH) | |
794 | goto skip_ktls; | |
795 | ||
796 | /* ktls does not support record padding */ | |
797 | if (s->record_padding_cb != NULL) | |
798 | goto skip_ktls; | |
799 | ||
800 | /* check that cipher is supported */ | |
801 | if (!ktls_check_supported_cipher(cipher, ciph_ctx)) | |
802 | goto skip_ktls; | |
803 | ||
804 | bio = s->wbio; | |
805 | ||
806 | if (!ossl_assert(bio != NULL)) { | |
807 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
808 | ERR_R_INTERNAL_ERROR); | |
809 | goto err; | |
810 | } | |
811 | ||
812 | /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */ | |
813 | if (BIO_flush(bio) <= 0) | |
814 | goto skip_ktls; | |
815 | ||
816 | /* configure kernel crypto structure */ | |
817 | if (!ktls_configure_crypto(cipher, s->version, ciph_ctx, | |
818 | RECORD_LAYER_get_write_sequence(&s->rlayer), | |
819 | &crypto_info, NULL, iv, key)) | |
820 | goto skip_ktls; | |
821 | ||
822 | /* ktls works with user provided buffers directly */ | |
823 | if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) | |
824 | ssl3_release_write_buffer(s); | |
825 | # endif | |
826 | skip_ktls: | |
827 | # endif | |
828 | #endif | |
57389a32 MC |
829 | ret = 1; |
830 | err: | |
9727f4e7 MC |
831 | if ((which & SSL3_CC_EARLY) != 0) { |
832 | /* We up-refed this so now we need to down ref */ | |
833 | ssl_evp_cipher_free(cipher); | |
834 | } | |
cc10b56d | 835 | OPENSSL_cleanse(key, sizeof(key)); |
57389a32 MC |
836 | OPENSSL_cleanse(secret, sizeof(secret)); |
837 | return ret; | |
838 | } | |
0d9824c1 | 839 | |
d1186c30 | 840 | int tls13_update_key(SSL *s, int sending) |
57389a32 | 841 | { |
48102247 | 842 | #ifdef CHARSET_EBCDIC |
843 | static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00}; | |
844 | #else | |
845 | static const unsigned char application_traffic[] = "traffic upd"; | |
846 | #endif | |
57389a32 MC |
847 | const EVP_MD *md = ssl_handshake_md(s); |
848 | size_t hashlen = EVP_MD_size(md); | |
cc10b56d | 849 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
57389a32 MC |
850 | unsigned char *insecret, *iv; |
851 | unsigned char secret[EVP_MAX_MD_SIZE]; | |
852 | EVP_CIPHER_CTX *ciph_ctx; | |
853 | int ret = 0; | |
0d9824c1 | 854 | |
d1186c30 | 855 | if (s->server == sending) |
57389a32 MC |
856 | insecret = s->server_app_traffic_secret; |
857 | else | |
858 | insecret = s->client_app_traffic_secret; | |
bebc0c7d | 859 | |
d1186c30 | 860 | if (sending) { |
7426cd34 | 861 | s->statem.enc_write_state = ENC_WRITE_STATE_INVALID; |
57389a32 MC |
862 | iv = s->write_iv; |
863 | ciph_ctx = s->enc_write_ctx; | |
864 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
865 | } else { | |
866 | iv = s->read_iv; | |
867 | ciph_ctx = s->enc_read_ctx; | |
868 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
0d9824c1 | 869 | } |
57389a32 | 870 | |
d1186c30 | 871 | if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s), |
555cbb32 | 872 | s->s3.tmp.new_sym_enc, insecret, NULL, |
d49e23ec | 873 | application_traffic, |
cc10b56d VF |
874 | sizeof(application_traffic) - 1, secret, key, |
875 | iv, ciph_ctx)) { | |
f63a17d6 | 876 | /* SSLfatal() already called */ |
57389a32 | 877 | goto err; |
f63a17d6 | 878 | } |
57389a32 MC |
879 | |
880 | memcpy(insecret, secret, hashlen); | |
0d9824c1 | 881 | |
7426cd34 | 882 | s->statem.enc_write_state = ENC_WRITE_STATE_VALID; |
6530c490 | 883 | ret = 1; |
0d9824c1 | 884 | err: |
cc10b56d | 885 | OPENSSL_cleanse(key, sizeof(key)); |
0d9824c1 | 886 | OPENSSL_cleanse(secret, sizeof(secret)); |
6530c490 | 887 | return ret; |
0d9824c1 | 888 | } |
04904312 MC |
889 | |
890 | int tls13_alert_code(int code) | |
891 | { | |
43a0f273 MC |
892 | /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */ |
893 | if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED) | |
04904312 MC |
894 | return code; |
895 | ||
896 | return tls1_alert_code(code); | |
897 | } | |
0ca8d1ec MC |
898 | |
899 | int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen, | |
900 | const char *label, size_t llen, | |
901 | const unsigned char *context, | |
902 | size_t contextlen, int use_context) | |
903 | { | |
904 | unsigned char exportsecret[EVP_MAX_MD_SIZE]; | |
48102247 | 905 | #ifdef CHARSET_EBCDIC |
906 | static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00}; | |
907 | #else | |
0ca8d1ec | 908 | static const unsigned char exporterlabel[] = "exporter"; |
48102247 | 909 | #endif |
c8b93876 | 910 | unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE]; |
0ca8d1ec MC |
911 | const EVP_MD *md = ssl_handshake_md(s); |
912 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | |
c8b93876 | 913 | unsigned int hashsize, datalen; |
0ca8d1ec MC |
914 | int ret = 0; |
915 | ||
1f5878b8 | 916 | if (ctx == NULL || !ossl_statem_export_allowed(s)) |
0ca8d1ec MC |
917 | goto err; |
918 | ||
919 | if (!use_context) | |
920 | contextlen = 0; | |
921 | ||
922 | if (EVP_DigestInit_ex(ctx, md, NULL) <= 0 | |
923 | || EVP_DigestUpdate(ctx, context, contextlen) <= 0 | |
924 | || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0 | |
c8b93876 TT |
925 | || EVP_DigestInit_ex(ctx, md, NULL) <= 0 |
926 | || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0 | |
0ca8d1ec | 927 | || !tls13_hkdf_expand(s, md, s->exporter_master_secret, |
c8b93876 | 928 | (const unsigned char *)label, llen, |
0fb2815b | 929 | data, datalen, exportsecret, hashsize, 0) |
0ca8d1ec | 930 | || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel, |
a19ae67d | 931 | sizeof(exporterlabel) - 1, hash, hashsize, |
0fb2815b | 932 | out, olen, 0)) |
0ca8d1ec MC |
933 | goto err; |
934 | ||
935 | ret = 1; | |
936 | err: | |
937 | EVP_MD_CTX_free(ctx); | |
938 | return ret; | |
939 | } | |
b38ede80 TT |
940 | |
941 | int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen, | |
942 | const char *label, size_t llen, | |
943 | const unsigned char *context, | |
944 | size_t contextlen) | |
945 | { | |
48102247 | 946 | #ifdef CHARSET_EBCDIC |
947 | static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00}; | |
948 | #else | |
949 | static const unsigned char exporterlabel[] = "exporter"; | |
950 | #endif | |
b38ede80 TT |
951 | unsigned char exportsecret[EVP_MAX_MD_SIZE]; |
952 | unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE]; | |
953 | const EVP_MD *md; | |
954 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | |
955 | unsigned int hashsize, datalen; | |
956 | int ret = 0; | |
957 | const SSL_CIPHER *sslcipher; | |
958 | ||
959 | if (ctx == NULL || !ossl_statem_export_early_allowed(s)) | |
960 | goto err; | |
961 | ||
962 | if (!s->server && s->max_early_data > 0 | |
963 | && s->session->ext.max_early_data == 0) | |
964 | sslcipher = SSL_SESSION_get0_cipher(s->psksession); | |
965 | else | |
966 | sslcipher = SSL_SESSION_get0_cipher(s->session); | |
967 | ||
c8f6c28a | 968 | md = ssl_md(s->ctx, sslcipher->algorithm2); |
b38ede80 TT |
969 | |
970 | /* | |
971 | * Calculate the hash value and store it in |data|. The reason why | |
972 | * the empty string is used is that the definition of TLS-Exporter | |
973 | * is like so: | |
974 | * | |
975 | * TLS-Exporter(label, context_value, key_length) = | |
976 | * HKDF-Expand-Label(Derive-Secret(Secret, label, ""), | |
977 | * "exporter", Hash(context_value), key_length) | |
978 | * | |
979 | * Derive-Secret(Secret, Label, Messages) = | |
980 | * HKDF-Expand-Label(Secret, Label, | |
981 | * Transcript-Hash(Messages), Hash.length) | |
982 | * | |
983 | * Here Transcript-Hash is the cipher suite hash algorithm. | |
984 | */ | |
985 | if (EVP_DigestInit_ex(ctx, md, NULL) <= 0 | |
986 | || EVP_DigestUpdate(ctx, context, contextlen) <= 0 | |
987 | || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0 | |
988 | || EVP_DigestInit_ex(ctx, md, NULL) <= 0 | |
989 | || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0 | |
990 | || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret, | |
991 | (const unsigned char *)label, llen, | |
0fb2815b | 992 | data, datalen, exportsecret, hashsize, 0) |
b38ede80 TT |
993 | || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel, |
994 | sizeof(exporterlabel) - 1, hash, hashsize, | |
0fb2815b | 995 | out, olen, 0)) |
b38ede80 TT |
996 | goto err; |
997 | ||
998 | ret = 1; | |
999 | err: | |
1000 | EVP_MD_CTX_free(ctx); | |
1001 | return ret; | |
1002 | } |