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