Skip to content

Commit bd3e322

Browse files
committed
pkey: handle EVP_PKEY_KEYMGMT return by EVP_PKEY_id()
For algorithms implemented solely in an OpenSSL 3 provider, without an associated EVP_PKEY_METHOD, EVP_PKEY_id() returns a special value EVP_PKEY_KEYMGMT. Let OpenSSL::PKey::PKey#oid raise an exception as necessary. Update PKey#inspect to include the string returned by EVP_PKEY_get0_type_name(), if available.
1 parent f831bb6 commit bd3e322

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ ossl_pkey_oid(VALUE self)
711711

712712
GetPKey(self, pkey);
713713
nid = EVP_PKEY_id(pkey);
714+
#ifdef OSSL_USE_PROVIDER
715+
if (nid == EVP_PKEY_KEYMGMT)
716+
ossl_raise(ePKeyError, "EVP_PKEY_id");
717+
#endif
714718
return rb_str_new_cstr(OBJ_nid2sn(nid));
715719
}
716720

@@ -724,13 +728,23 @@ static VALUE
724728
ossl_pkey_inspect(VALUE self)
725729
{
726730
EVP_PKEY *pkey;
727-
int nid;
728731

729732
GetPKey(self, pkey);
730-
nid = EVP_PKEY_id(pkey);
731-
return rb_sprintf("#<%"PRIsVALUE":%p oid=%s>",
732-
rb_class_name(CLASS_OF(self)), (void *)self,
733-
OBJ_nid2sn(nid));
733+
VALUE str = rb_sprintf("#<%"PRIsVALUE":%p",
734+
rb_obj_class(self), (void *)self);
735+
int nid = EVP_PKEY_id(pkey);
736+
#ifdef OSSL_USE_PROVIDER
737+
if (nid != EVP_PKEY_KEYMGMT)
738+
#endif
739+
rb_str_catf(str, " oid=%s", OBJ_nid2sn(nid));
740+
#ifdef OSSL_USE_PROVIDER
741+
rb_str_catf(str, " type_name=%s", EVP_PKEY_get0_type_name(pkey));
742+
const OSSL_PROVIDER *prov = EVP_PKEY_get0_provider(pkey);
743+
if (prov)
744+
rb_str_catf(str, " provider=%s", OSSL_PROVIDER_get0_name(prov));
745+
#endif
746+
rb_str_catf(str, ">");
747+
return str;
734748
}
735749

736750
/*

test/openssl/test_pkey.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,7 @@ def test_generic_oid_inspect_rsa
88
assert_instance_of OpenSSL::PKey::RSA, rsa
99
assert_equal "rsaEncryption", rsa.oid
1010
assert_match %r{oid=rsaEncryption}, rsa.inspect
11-
end
12-
13-
def test_generic_oid_inspect_x25519
14-
omit_on_fips
15-
16-
# X25519 private key
17-
x25519 = OpenSSL::PKey.generate_key("X25519")
18-
assert_instance_of OpenSSL::PKey::PKey, x25519
19-
assert_equal "X25519", x25519.oid
20-
assert_match %r{oid=X25519}, x25519.inspect
11+
assert_match %r{type_name=RSA}, rsa.inspect if openssl?(3, 0, 0)
2112
end
2213

2314
def test_s_generate_parameters
@@ -152,6 +143,8 @@ def test_x25519
152143
alice = OpenSSL::PKey.read(alice_pem)
153144
bob = OpenSSL::PKey.read(bob_pem)
154145
assert_instance_of OpenSSL::PKey::PKey, alice
146+
assert_equal "X25519", alice.oid
147+
assert_match %r{oid=X25519}, alice.inspect
155148
assert_equal alice_pem, alice.private_to_pem
156149
assert_equal bob_pem, bob.public_to_pem
157150
assert_equal [shared_secret].pack("H*"), alice.derive(bob)

0 commit comments

Comments
 (0)