Skip to content

Commit

Permalink
Avoid EVP_PKEY_set_type in EVP_PKEY_new_raw_*_key
Browse files Browse the repository at this point in the history
These are effectively just APIs for creating Ed25519 and X25519 keys. We
may want to rethink this a bit later, but for now let's just do this.

Bug: 497
Change-Id: I01ae06fa86af96da993fd41611472838475bf094
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/67128
Reviewed-by: Bob Beck <[email protected]>
Commit-Queue: David Benjamin <[email protected]>
(cherry picked from commit 660973695bd20a22201e979a6e6f8c335f939cfe)
  • Loading branch information
davidben authored and samuel40791765 committed Dec 18, 2024
1 parent 8003f87 commit a1fcb33
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
9 changes: 9 additions & 0 deletions crypto/evp_extra/evp_extra_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3352,3 +3352,12 @@ TEST(EVPExtraTest, DSADigestSignVerify) {
ASSERT_TRUE(EVP_DigestVerify(md_ctx.get(), sig.data(), sig.size(), (const uint8_t*)data, data_len));
}
}

TEST(EVPExtraTest, RawKeyUnsupported) {
static const uint8_t kKey[] = {1, 2, 3, 4};
EXPECT_FALSE(
EVP_PKEY_new_raw_public_key(EVP_PKEY_RSA, nullptr, kKey, sizeof(kKey)));
EXPECT_FALSE(
EVP_PKEY_new_raw_private_key(EVP_PKEY_RSA, nullptr, kKey, sizeof(kKey)));
}

47 changes: 35 additions & 12 deletions crypto/fipsmodule/evp/evp.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,16 +471,29 @@ int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,
const uint8_t *in, size_t len) {
SET_DIT_AUTO_RESET;
EVP_PKEY *ret = EVP_PKEY_new();
if (ret == NULL ||
!EVP_PKEY_set_type(ret, type)) {
goto err;
// To avoid pulling in all key types, look for specifically the key types that
// support |set_priv_raw|.
const EVP_PKEY_ASN1_METHOD *method;
switch (type) {
case EVP_PKEY_X25519:
method = &x25519_asn1_meth;
break;
case EVP_PKEY_ED25519:
method = &ed25519_asn1_meth;
break;
case EVP_PKEY_HMAC:
method = &hmac_asn1_meth;
break;
default:
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
return 0;
}

if (ret->ameth->set_priv_raw == NULL) {
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
EVP_PKEY *ret = EVP_PKEY_new();
if (ret == NULL) {
goto err;
}
evp_pkey_set_method(ret, method);

if (!ret->ameth->set_priv_raw(ret, in, len, NULL, 0)) {
goto err;
Expand All @@ -495,16 +508,26 @@ EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *unused,

EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *unused,
const uint8_t *in, size_t len) {
EVP_PKEY *ret = EVP_PKEY_new();
if (ret == NULL ||
!EVP_PKEY_set_type(ret, type)) {
goto err;
// To avoid pulling in all key types, look for specifically the key types that
// support |set_pub_raw|.
const EVP_PKEY_ASN1_METHOD *method;
switch (type) {
case EVP_PKEY_X25519:
method = &x25519_asn1_meth;
break;
case EVP_PKEY_ED25519:
method = &ed25519_asn1_meth;
break;
default:
OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
return 0;
}

if (ret->ameth->set_pub_raw == NULL) {
OPENSSL_PUT_ERROR(EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
EVP_PKEY *ret = EVP_PKEY_new();
if (ret == NULL) {
goto err;
}
evp_pkey_set_method(ret, method);

if (!ret->ameth->set_pub_raw(ret, in, len)) {
goto err;
Expand Down

0 comments on commit a1fcb33

Please sign in to comment.