Skip to content

Commit

Permalink
Handle empty param in EVP_PKEY_CTX_add1_hkdf_info
Browse files Browse the repository at this point in the history
Fixes #24130
The regression was introduced in PR #23456.

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24141)

(cherry picked from commit 299996f)
  • Loading branch information
trinity-1686a authored and t8m committed Apr 17, 2024
1 parent 5fbb133 commit 45c2a82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crypto/evp/pmeth_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,8 @@ static int evp_pkey_ctx_add1_octet_string(EVP_PKEY_CTX *ctx, int fallback,
if (datalen < 0) {
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
return 0;
} else if (datalen == 0) {
return 1;
}

/* Get the original value length */
Expand Down
42 changes: 42 additions & 0 deletions test/evp_extra_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,47 @@ static int test_emptyikm_HKDF(void)
return ret;
}

static int test_empty_salt_info_HKDF(void)
{
EVP_PKEY_CTX *pctx;
unsigned char out[20];
size_t outlen;
int ret = 0;
unsigned char salt[] = "";
unsigned char key[] = "012345678901234567890123456789";
unsigned char info[] = "";
const unsigned char expected[] = {
0x67, 0x12, 0xf9, 0x27, 0x8a, 0x8a, 0x3a, 0x8f, 0x7d, 0x2c, 0xa3, 0x6a,
0xaa, 0xe9, 0xb3, 0xb9, 0x52, 0x5f, 0xe0, 0x06,
};
size_t expectedlen = sizeof(expected);

if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(testctx, "HKDF", testpropq)))
goto done;

outlen = sizeof(out);
memset(out, 0, outlen);

if (!TEST_int_gt(EVP_PKEY_derive_init(pctx), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_salt(pctx, salt,
sizeof(salt) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_set1_hkdf_key(pctx, key,
sizeof(key) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_CTX_add1_hkdf_info(pctx, info,
sizeof(info) - 1), 0)
|| !TEST_int_gt(EVP_PKEY_derive(pctx, out, &outlen), 0)
|| !TEST_mem_eq(out, outlen, expected, expectedlen))
goto done;

ret = 1;

done:
EVP_PKEY_CTX_free(pctx);

return ret;
}

#ifndef OPENSSL_NO_EC
static int test_X509_PUBKEY_inplace(void)
{
Expand Down Expand Up @@ -5385,6 +5426,7 @@ int setup_tests(void)
#endif
ADD_TEST(test_HKDF);
ADD_TEST(test_emptyikm_HKDF);
ADD_TEST(test_empty_salt_info_HKDF);
#ifndef OPENSSL_NO_EC
ADD_TEST(test_X509_PUBKEY_inplace);
ADD_TEST(test_X509_PUBKEY_dup);
Expand Down

0 comments on commit 45c2a82

Please sign in to comment.