Changed ssl layer to use EVP_KDF API for TLS1_PRF and HKDF.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/9010)
This commit is contained in:
David Makepeace 2019-05-27 09:29:43 +10:00 committed by Shane Lontis
parent 19f43f02aa
commit 324954640e
2 changed files with 42 additions and 38 deletions

View file

@ -31,7 +31,7 @@ static int tls1_PRF(SSL *s,
unsigned char *out, size_t olen, int fatal) unsigned char *out, size_t olen, int fatal)
{ {
const EVP_MD *md = ssl_prf_md(s); const EVP_MD *md = ssl_prf_md(s);
EVP_PKEY_CTX *pctx = NULL; EVP_KDF_CTX *kctx = NULL;
int ret = 0; int ret = 0;
if (md == NULL) { if (md == NULL) {
@ -43,16 +43,22 @@ static int tls1_PRF(SSL *s,
SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR); SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
return 0; return 0;
} }
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL); kctx = EVP_KDF_CTX_new_id(EVP_PKEY_TLS1_PRF);
if (pctx == NULL || EVP_PKEY_derive_init(pctx) <= 0 if (kctx == NULL
|| EVP_PKEY_CTX_set_tls1_prf_md(pctx, md) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) <= 0
|| EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, sec, (int)slen) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_TLS_SECRET,
|| EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed1, (int)seed1_len) <= 0 sec, (size_t)slen) <= 0
|| EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed2, (int)seed2_len) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
|| EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed3, (int)seed3_len) <= 0 seed1, (size_t)seed1_len) <= 0
|| EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed4, (int)seed4_len) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
|| EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, seed5, (int)seed5_len) <= 0 seed2, (size_t)seed2_len) <= 0
|| EVP_PKEY_derive(pctx, out, &olen) <= 0) { || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
seed3, (size_t)seed3_len) <= 0
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
seed4, (size_t)seed4_len) <= 0
|| EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED,
seed5, (size_t)seed5_len) <= 0
|| EVP_KDF_derive(kctx, out, olen) <= 0) {
if (fatal) if (fatal)
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF, SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
ERR_R_INTERNAL_ERROR); ERR_R_INTERNAL_ERROR);
@ -64,7 +70,7 @@ static int tls1_PRF(SSL *s,
ret = 1; ret = 1;
err: err:
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
return ret; return ret;
} }

View file

@ -31,7 +31,7 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
unsigned char *out, size_t outlen, int fatal) unsigned char *out, size_t outlen, int fatal)
{ {
static const unsigned char label_prefix[] = "tls13 "; static const unsigned char label_prefix[] = "tls13 ";
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); EVP_KDF_CTX *kctx = EVP_KDF_CTX_new_id(EVP_PKEY_HKDF);
int ret; int ret;
size_t hkdflabellen; size_t hkdflabellen;
size_t hashlen; size_t hashlen;
@ -45,7 +45,7 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
+ 1 + EVP_MAX_MD_SIZE]; + 1 + EVP_MAX_MD_SIZE];
WPACKET pkt; WPACKET pkt;
if (pctx == NULL) if (kctx == NULL)
return 0; return 0;
if (labellen > TLS13_MAX_LABEL_LEN) { if (labellen > TLS13_MAX_LABEL_LEN) {
@ -59,7 +59,7 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
*/ */
SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL); SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
} }
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
return 0; return 0;
} }
@ -74,7 +74,7 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
|| !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen) || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
|| !WPACKET_get_total_written(&pkt, &hkdflabellen) || !WPACKET_get_total_written(&pkt, &hkdflabellen)
|| !WPACKET_finish(&pkt)) { || !WPACKET_finish(&pkt)) {
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
WPACKET_cleanup(&pkt); WPACKET_cleanup(&pkt);
if (fatal) if (fatal)
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND, SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
@ -84,15 +84,15 @@ int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
return 0; return 0;
} }
ret = EVP_PKEY_derive_init(pctx) <= 0 ret = EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_HKDF_MODE,
|| EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) <= 0
<= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) <= 0
|| EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, secret, hashlen) <= 0
|| EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_HKDF_INFO,
|| EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0 hkdflabel, hkdflabellen) <= 0
|| EVP_PKEY_derive(pctx, out, &outlen) <= 0; || EVP_KDF_derive(kctx, out, outlen) <= 0;
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
if (ret != 0) { if (ret != 0) {
if (fatal) if (fatal)
@ -155,11 +155,11 @@ int tls13_generate_secret(SSL *s, const EVP_MD *md,
size_t mdlen, prevsecretlen; size_t mdlen, prevsecretlen;
int mdleni; int mdleni;
int ret; int ret;
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); EVP_KDF_CTX *kctx = EVP_KDF_CTX_new_id(EVP_PKEY_HKDF);
static const char derived_secret_label[] = "derived"; static const char derived_secret_label[] = "derived";
unsigned char preextractsec[EVP_MAX_MD_SIZE]; unsigned char preextractsec[EVP_MAX_MD_SIZE];
if (pctx == NULL) { if (kctx == NULL) {
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
ERR_R_INTERNAL_ERROR); ERR_R_INTERNAL_ERROR);
return 0; return 0;
@ -192,7 +192,7 @@ int tls13_generate_secret(SSL *s, const EVP_MD *md,
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
ERR_R_INTERNAL_ERROR); ERR_R_INTERNAL_ERROR);
EVP_MD_CTX_free(mctx); EVP_MD_CTX_free(mctx);
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
return 0; return 0;
} }
EVP_MD_CTX_free(mctx); EVP_MD_CTX_free(mctx);
@ -203,7 +203,7 @@ int tls13_generate_secret(SSL *s, const EVP_MD *md,
sizeof(derived_secret_label) - 1, hash, mdlen, sizeof(derived_secret_label) - 1, hash, mdlen,
preextractsec, mdlen, 1)) { preextractsec, mdlen, 1)) {
/* SSLfatal() already called */ /* SSLfatal() already called */
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
return 0; return 0;
} }
@ -211,21 +211,19 @@ int tls13_generate_secret(SSL *s, const EVP_MD *md,
prevsecretlen = mdlen; prevsecretlen = mdlen;
} }
ret = EVP_PKEY_derive_init(pctx) <= 0 ret = EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_HKDF_MODE,
|| EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) <= 0
<= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, md) <= 0
|| EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, insecret, insecretlen) <= 0
|| EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0 || EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
|| EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen) prevsecret, prevsecretlen) <= 0
<= 0 || EVP_KDF_derive(kctx, outsecret, mdlen) <= 0;
|| EVP_PKEY_derive(pctx, outsecret, &mdlen)
<= 0;
if (ret != 0) if (ret != 0)
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET, SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
ERR_R_INTERNAL_ERROR); ERR_R_INTERNAL_ERROR);
EVP_PKEY_CTX_free(pctx); EVP_KDF_CTX_free(kctx);
if (prevsecret == preextractsec) if (prevsecret == preextractsec)
OPENSSL_cleanse(preextractsec, mdlen); OPENSSL_cleanse(preextractsec, mdlen);
return ret == 0; return ret == 0;