Skip to content

Commit dafbb1b

Browse files
rheniumioquatix
authored andcommitted
pkey: add PKey#inspect and #oid
Implement OpenSSL::PKey::PKey#oid as a wrapper around EVP_PKEY_id(). This allows user code to check the type of a PKey object. EVP_PKEY can have a pkey type for which we do not provide a dedicated subclass. In other words, an EVP_PKEY that is not any of {RSA,DSA,DH,EC} can exist. It is currently not possible to distinguish such a pkey. Also, implement PKey#inspect to include the key type for convenience.
1 parent 033fb4f commit dafbb1b

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

ext/openssl/ossl_pkey.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,42 @@ ossl_pkey_initialize(VALUE self)
299299
return self;
300300
}
301301

302+
/*
303+
* call-seq:
304+
* pkey.oid -> string
305+
*
306+
* Returns the short name of the OID associated with _pkey_.
307+
*/
308+
static VALUE
309+
ossl_pkey_oid(VALUE self)
310+
{
311+
EVP_PKEY *pkey;
312+
int nid;
313+
314+
GetPKey(self, pkey);
315+
nid = EVP_PKEY_id(pkey);
316+
return rb_str_new_cstr(OBJ_nid2sn(nid));
317+
}
318+
319+
/*
320+
* call-seq:
321+
* pkey.inspect -> string
322+
*
323+
* Returns a string describing the PKey object.
324+
*/
325+
static VALUE
326+
ossl_pkey_inspect(VALUE self)
327+
{
328+
EVP_PKEY *pkey;
329+
int nid;
330+
331+
GetPKey(self, pkey);
332+
nid = EVP_PKEY_id(pkey);
333+
return rb_sprintf("#<%"PRIsVALUE":%p oid=%s>",
334+
rb_class_name(CLASS_OF(self)), (void *)self,
335+
OBJ_nid2sn(nid));
336+
}
337+
302338
static VALUE
303339
do_pkcs8_export(int argc, VALUE *argv, VALUE self, int to_der)
304340
{
@@ -615,6 +651,8 @@ Init_ossl_pkey(void)
615651

616652
rb_define_alloc_func(cPKey, ossl_pkey_alloc);
617653
rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0);
654+
rb_define_method(cPKey, "oid", ossl_pkey_oid, 0);
655+
rb_define_method(cPKey, "inspect", ossl_pkey_inspect, 0);
618656
rb_define_method(cPKey, "private_to_der", ossl_pkey_private_to_der, -1);
619657
rb_define_method(cPKey, "private_to_pem", ossl_pkey_private_to_pem, -1);
620658
rb_define_method(cPKey, "public_to_der", ossl_pkey_public_to_der, 0);

test/openssl/test_pkey.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
require_relative "utils"
3+
4+
class OpenSSL::TestPKey < OpenSSL::PKeyTestCase
5+
def test_generic_oid_inspect
6+
# RSA private key
7+
rsa = Fixtures.pkey("rsa-1")
8+
assert_instance_of OpenSSL::PKey::RSA, rsa
9+
assert_equal "rsaEncryption", rsa.oid
10+
assert_match %r{oid=rsaEncryption}, rsa.inspect
11+
12+
# X25519 private key
13+
x25519_pem = <<~EOF
14+
-----BEGIN PRIVATE KEY-----
15+
MC4CAQAwBQYDK2VuBCIEIHcHbQpzGKV9PBbBclGyZkXfTC+H68CZKrF3+6UduSwq
16+
-----END PRIVATE KEY-----
17+
EOF
18+
begin
19+
x25519 = OpenSSL::PKey.read(x25519_pem)
20+
rescue OpenSSL::PKey::PKeyError
21+
# OpenSSL < 1.1.0
22+
pend "X25519 is not implemented"
23+
end
24+
assert_instance_of OpenSSL::PKey::PKey, x25519
25+
assert_equal "X25519", x25519.oid
26+
assert_match %r{oid=X25519}, x25519.inspect
27+
end
28+
end

0 commit comments

Comments
 (0)