2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
16 #define TLS13_MAX_LABEL_LEN 246
18 /* Always filled with zeros */
19 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
22 * Given a |secret|; a |label| of length |labellen|; and |data| of length
23 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
24 * secret |outlen| bytes long and store it in the location pointed to be |out|.
25 * The |data| value may be zero length. Returns 1 on success 0 on failure.
27 int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
28 const unsigned char *label, size_t labellen,
29 const unsigned char *data, size_t datalen,
30 unsigned char *out, size_t outlen)
32 const unsigned char label_prefix[] = "tls13 ";
33 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
38 * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
39 * prefix and label + bytes for the label itself + bytes for the hash
41 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
42 + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
49 hashlen = EVP_MD_size(md);
51 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
52 || !WPACKET_put_bytes_u16(&pkt, outlen)
53 || !WPACKET_start_sub_packet_u8(&pkt)
54 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
55 || !WPACKET_memcpy(&pkt, label, labellen)
56 || !WPACKET_close(&pkt)
57 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
58 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
59 || !WPACKET_finish(&pkt)) {
60 EVP_PKEY_CTX_free(pctx);
61 WPACKET_cleanup(&pkt);
65 ret = EVP_PKEY_derive_init(pctx) <= 0
66 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
68 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
69 || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
70 || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
71 || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
73 EVP_PKEY_CTX_free(pctx);
79 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
80 * success 0 on failure.
82 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
83 unsigned char *key, size_t keylen)
85 static const unsigned char keylabel[] = "key";
87 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
88 NULL, 0, key, keylen);
92 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
93 * success 0 on failure.
95 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
96 unsigned char *iv, size_t ivlen)
98 static const unsigned char ivlabel[] = "iv";
100 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
104 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
105 const unsigned char *secret,
106 unsigned char *fin, size_t finlen)
108 static const unsigned char finishedlabel[] = "finished";
110 return tls13_hkdf_expand(s, md, secret, finishedlabel,
111 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
115 * Given the previous secret |prevsecret| and a new input secret |insecret| of
116 * length |insecretlen|, generate a new secret and store it in the location
117 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
119 int tls13_generate_secret(SSL *s, const EVP_MD *md,
120 const unsigned char *prevsecret,
121 const unsigned char *insecret,
123 unsigned char *outsecret)
125 size_t mdlen, prevsecretlen;
127 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
128 static const char derived_secret_label[] = "derived";
129 unsigned char preextractsec[EVP_MAX_MD_SIZE];
134 mdlen = EVP_MD_size(md);
136 if (insecret == NULL) {
137 insecret = default_zeros;
140 if (prevsecret == NULL) {
141 prevsecret = default_zeros;
144 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
145 unsigned char hash[EVP_MAX_MD_SIZE];
147 /* The pre-extract derive step uses a hash of no messages */
149 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
150 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
151 EVP_MD_CTX_free(mctx);
152 EVP_PKEY_CTX_free(pctx);
155 EVP_MD_CTX_free(mctx);
157 /* Generate the pre-extract secret */
158 if (!tls13_hkdf_expand(s, md, prevsecret,
159 (unsigned char *)derived_secret_label,
160 sizeof(derived_secret_label) - 1, hash, mdlen,
161 preextractsec, mdlen)) {
162 EVP_PKEY_CTX_free(pctx);
166 prevsecret = preextractsec;
167 prevsecretlen = mdlen;
170 ret = EVP_PKEY_derive_init(pctx) <= 0
171 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
173 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
174 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
175 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
177 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
180 EVP_PKEY_CTX_free(pctx);
181 if (prevsecret == preextractsec)
182 OPENSSL_cleanse(preextractsec, mdlen);
187 * Given an input secret |insecret| of length |insecretlen| generate the
188 * handshake secret. This requires the early secret to already have been
189 * generated. Returns 1 on success 0 on failure.
191 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
194 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
195 insecret, insecretlen,
196 (unsigned char *)&s->handshake_secret);
200 * Given the handshake secret |prev| of length |prevlen| generate the master
201 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
204 int tls13_generate_master_secret(SSL *s, unsigned char *out,
205 unsigned char *prev, size_t prevlen,
208 const EVP_MD *md = ssl_handshake_md(s);
210 *secret_size = EVP_MD_size(md);
211 return tls13_generate_secret(s, md, prev, NULL, 0, out);
215 * Generates the mac for the Finished message. Returns the length of the MAC or
218 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
221 const EVP_MD *md = ssl_handshake_md(s);
222 unsigned char hash[EVP_MAX_MD_SIZE];
223 size_t hashlen, ret = 0;
224 EVP_PKEY *key = NULL;
225 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
227 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
230 if (str == s->method->ssl3_enc->server_finished_label)
231 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
232 s->server_finished_secret, hashlen);
234 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
235 s->client_finished_secret, hashlen);
239 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
240 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
241 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
247 EVP_MD_CTX_free(ctx);
252 * There isn't really a key block in TLSv1.3, but we still need this function
253 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
255 int tls13_setup_key_block(SSL *s)
259 int mac_type = NID_undef;
261 s->session->cipher = s->s3->tmp.new_cipher;
262 if (!ssl_cipher_get_evp
263 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
264 SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
268 s->s3->tmp.new_sym_enc = c;
269 s->s3->tmp.new_hash = hash;
274 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
275 const EVP_CIPHER *ciph,
276 const unsigned char *insecret,
277 const unsigned char *hash,
278 const unsigned char *label,
279 size_t labellen, unsigned char *secret,
280 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
282 unsigned char key[EVP_MAX_KEY_LENGTH];
283 size_t ivlen, keylen, taglen;
284 size_t hashlen = EVP_MD_size(md);
286 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
288 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
292 /* TODO(size_t): convert me */
293 keylen = EVP_CIPHER_key_length(ciph);
294 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
297 ivlen = EVP_CCM_TLS_IV_LEN;
298 if (s->s3->tmp.new_cipher == NULL) {
299 /* We've not selected a cipher yet - we must be doing early data */
300 algenc = s->session->cipher->algorithm_enc;
302 algenc = s->s3->tmp.new_cipher->algorithm_enc;
304 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
305 taglen = EVP_CCM8_TLS_TAG_LEN;
307 taglen = EVP_CCM_TLS_TAG_LEN;
309 ivlen = EVP_CIPHER_iv_length(ciph);
313 if (!tls13_derive_key(s, md, secret, key, keylen)
314 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
315 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
319 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
320 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
321 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
323 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
324 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB);
330 OPENSSL_cleanse(key, sizeof(key));
334 int tls13_change_cipher_state(SSL *s, int which)
336 static const unsigned char client_early_traffic[] = "c e traffic";
337 static const unsigned char client_handshake_traffic[] = "c hs traffic";
338 static const unsigned char client_application_traffic[] = "c ap traffic";
339 static const unsigned char server_handshake_traffic[] = "s hs traffic";
340 static const unsigned char server_application_traffic[] = "s ap traffic";
341 static const unsigned char exporter_master_secret[] = "exp master";
342 static const unsigned char resumption_master_secret[] = "res master";
344 unsigned char secret[EVP_MAX_MD_SIZE];
345 unsigned char hashval[EVP_MAX_MD_SIZE];
346 unsigned char *hash = hashval;
347 unsigned char *insecret;
348 unsigned char *finsecret = NULL;
349 const char *log_label = NULL;
350 EVP_CIPHER_CTX *ciph_ctx;
351 size_t finsecretlen = 0;
352 const unsigned char *label;
353 size_t labellen, hashlen = 0;
355 const EVP_MD *md = NULL;
356 const EVP_CIPHER *cipher = NULL;
358 if (which & SSL3_CC_READ) {
359 if (s->enc_read_ctx != NULL) {
360 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
362 s->enc_read_ctx = EVP_CIPHER_CTX_new();
363 if (s->enc_read_ctx == NULL) {
364 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
368 ciph_ctx = s->enc_read_ctx;
371 RECORD_LAYER_reset_read_sequence(&s->rlayer);
373 if (s->enc_write_ctx != NULL) {
374 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
376 s->enc_write_ctx = EVP_CIPHER_CTX_new();
377 if (s->enc_write_ctx == NULL) {
378 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
382 ciph_ctx = s->enc_write_ctx;
385 RECORD_LAYER_reset_write_sequence(&s->rlayer);
388 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
389 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
390 if (which & SSL3_CC_EARLY) {
391 EVP_MD_CTX *mdctx = NULL;
394 unsigned int hashlenui;
395 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
397 insecret = s->early_secret;
398 label = client_early_traffic;
399 labellen = sizeof(client_early_traffic) - 1;
400 log_label = CLIENT_EARLY_LABEL;
402 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
404 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE,
405 SSL_R_BAD_HANDSHAKE_LENGTH);
409 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
410 && s->max_early_data > 0
411 && s->session->ext.max_early_data == 0) {
413 * If we are attempting to send early data, and we've decided to
414 * actually do it but max_early_data in s->session is 0 then we
415 * must be using an external PSK.
417 if (!ossl_assert(s->psksession != NULL
418 && s->max_early_data ==
419 s->psksession->ext.max_early_data)) {
420 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE,
421 ERR_R_INTERNAL_ERROR);
424 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
426 if (sslcipher == NULL) {
427 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
432 * We need to calculate the handshake digest using the digest from
433 * the session. We haven't yet selected our ciphersuite so we can't
434 * use ssl_handshake_md().
436 mdctx = EVP_MD_CTX_new();
438 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
441 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
442 md = ssl_md(sslcipher->algorithm2);
443 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
444 || !EVP_DigestUpdate(mdctx, hdata, handlen)
445 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
446 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
447 EVP_MD_CTX_free(mdctx);
451 EVP_MD_CTX_free(mdctx);
452 } else if (which & SSL3_CC_HANDSHAKE) {
453 insecret = s->handshake_secret;
454 finsecret = s->client_finished_secret;
455 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
456 label = client_handshake_traffic;
457 labellen = sizeof(client_handshake_traffic) - 1;
458 log_label = CLIENT_HANDSHAKE_LABEL;
460 * The handshake hash used for the server read/client write handshake
461 * traffic secret is the same as the hash for the server
462 * write/client read handshake traffic secret. However, if we
463 * processed early data then we delay changing the server
464 * read/client write cipher state until later, and the handshake
465 * hashes have moved on. Therefore we use the value saved earlier
466 * when we did the server write/client read change cipher state.
468 hash = s->handshake_traffic_hash;
470 insecret = s->master_secret;
471 label = client_application_traffic;
472 labellen = sizeof(client_application_traffic) - 1;
473 log_label = CLIENT_APPLICATION_LABEL;
475 * For this we only use the handshake hashes up until the server
476 * Finished hash. We do not include the client's Finished, which is
477 * what ssl_handshake_hash() would give us. Instead we use the
478 * previously saved value.
480 hash = s->server_finished_hash;
483 /* Early data never applies to client-read/server-write */
484 if (which & SSL3_CC_HANDSHAKE) {
485 insecret = s->handshake_secret;
486 finsecret = s->server_finished_secret;
487 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
488 label = server_handshake_traffic;
489 labellen = sizeof(server_handshake_traffic) - 1;
490 log_label = SERVER_HANDSHAKE_LABEL;
492 insecret = s->master_secret;
493 label = server_application_traffic;
494 labellen = sizeof(server_application_traffic) - 1;
495 log_label = SERVER_APPLICATION_LABEL;
499 if (!(which & SSL3_CC_EARLY)) {
500 md = ssl_handshake_md(s);
501 cipher = s->s3->tmp.new_sym_enc;
502 if (!ssl3_digest_cached_records(s, 1)
503 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
504 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
510 * Save the hash of handshakes up to now for use when we calculate the
511 * client application traffic secret
513 if (label == server_application_traffic)
514 memcpy(s->server_finished_hash, hashval, hashlen);
516 if (label == server_handshake_traffic)
517 memcpy(s->handshake_traffic_hash, hashval, hashlen);
519 if (label == client_application_traffic) {
521 * We also create the resumption master secret, but this time use the
522 * hash for the whole handshake including the Client Finished
524 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
525 resumption_master_secret,
526 sizeof(resumption_master_secret) - 1,
527 hashval, hashlen, s->session->master_key,
529 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
532 s->session->master_key_length = hashlen;
534 /* Now we create the exporter master secret */
535 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
536 exporter_master_secret,
537 sizeof(exporter_master_secret) - 1,
538 hash, hashlen, s->exporter_master_secret,
540 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
545 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
546 insecret, hash, label, labellen, secret, iv,
551 if (label == server_application_traffic)
552 memcpy(s->server_app_traffic_secret, secret, hashlen);
553 else if (label == client_application_traffic)
554 memcpy(s->client_app_traffic_secret, secret, hashlen);
556 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
557 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
561 if (finsecret != NULL
562 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
563 finsecret, finsecretlen)) {
564 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
570 OPENSSL_cleanse(secret, sizeof(secret));
574 int tls13_update_key(SSL *s, int sending)
576 static const unsigned char application_traffic[] = "traffic upd";
577 const EVP_MD *md = ssl_handshake_md(s);
578 size_t hashlen = EVP_MD_size(md);
579 unsigned char *insecret, *iv;
580 unsigned char secret[EVP_MAX_MD_SIZE];
581 EVP_CIPHER_CTX *ciph_ctx;
584 if (s->server == sending)
585 insecret = s->server_app_traffic_secret;
587 insecret = s->client_app_traffic_secret;
591 ciph_ctx = s->enc_write_ctx;
592 RECORD_LAYER_reset_write_sequence(&s->rlayer);
595 ciph_ctx = s->enc_read_ctx;
596 RECORD_LAYER_reset_read_sequence(&s->rlayer);
599 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
600 s->s3->tmp.new_sym_enc, insecret, NULL,
602 sizeof(application_traffic) - 1, secret, iv,
606 memcpy(insecret, secret, hashlen);
610 OPENSSL_cleanse(secret, sizeof(secret));
614 int tls13_alert_code(int code)
616 if (code == SSL_AD_MISSING_EXTENSION)
619 return tls1_alert_code(code);
622 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
623 const char *label, size_t llen,
624 const unsigned char *context,
625 size_t contextlen, int use_context)
627 unsigned char exportsecret[EVP_MAX_MD_SIZE];
628 static const unsigned char exporterlabel[] = "exporter";
629 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
630 const EVP_MD *md = ssl_handshake_md(s);
631 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
632 unsigned int hashsize, datalen;
635 if (ctx == NULL || !SSL_is_init_finished(s))
641 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
642 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
643 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
644 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
645 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
646 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
647 (const unsigned char *)label, llen,
648 data, datalen, exportsecret, hashsize)
649 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
650 sizeof(exporterlabel) - 1, hash, hashsize,
656 EVP_MD_CTX_free(ctx);