ruby-changes:61244
From: Kazuki <ko1@a...>
Date: Wed, 13 May 2020 15:48:27 +0900 (JST)
Subject: [ruby-changes:61244] 6f008c9d2f (master): [ruby/openssl] pkey: add PKey#inspect and #oid
https://git.ruby-lang.org/ruby.git/commit/?id=6f008c9d2f From 6f008c9d2fec52f2c2d39c04ad83e87c7975105c Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi <k@r...> Date: Tue, 21 Apr 2020 02:12:29 +0900 Subject: [ruby/openssl] 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. https://github.com/ruby/openssl/commit/dafbb1b3e6 diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index fc08ebf..2320408 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -299,6 +299,42 @@ ossl_pkey_initialize(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.c#L299 return self; } +/* + * call-seq: + * pkey.oid -> string + * + * Returns the short name of the OID associated with _pkey_. + */ +static VALUE +ossl_pkey_oid(VALUE self) +{ + EVP_PKEY *pkey; + int nid; + + GetPKey(self, pkey); + nid = EVP_PKEY_id(pkey); + return rb_str_new_cstr(OBJ_nid2sn(nid)); +} + +/* + * call-seq: + * pkey.inspect -> string + * + * Returns a string describing the PKey object. + */ +static VALUE +ossl_pkey_inspect(VALUE self) +{ + EVP_PKEY *pkey; + int nid; + + GetPKey(self, pkey); + nid = EVP_PKEY_id(pkey); + return rb_sprintf("#<%"PRIsVALUE":%p oid=%s>", + rb_class_name(CLASS_OF(self)), (void *)self, + OBJ_nid2sn(nid)); +} + static VALUE do_pkcs8_export(int argc, VALUE *argv, VALUE self, int to_der) { @@ -615,6 +651,8 @@ Init_ossl_pkey(void) https://github.com/ruby/ruby/blob/trunk/ext/openssl/ossl_pkey.c#L651 rb_define_alloc_func(cPKey, ossl_pkey_alloc); rb_define_method(cPKey, "initialize", ossl_pkey_initialize, 0); + rb_define_method(cPKey, "oid", ossl_pkey_oid, 0); + rb_define_method(cPKey, "inspect", ossl_pkey_inspect, 0); rb_define_method(cPKey, "private_to_der", ossl_pkey_private_to_der, -1); rb_define_method(cPKey, "private_to_pem", ossl_pkey_private_to_pem, -1); rb_define_method(cPKey, "public_to_der", ossl_pkey_public_to_der, 0); diff --git a/test/openssl/test_pkey.rb b/test/openssl/test_pkey.rb new file mode 100644 index 0000000..0bdc979 --- /dev/null +++ b/test/openssl/test_pkey.rb @@ -0,0 +1,28 @@ https://github.com/ruby/ruby/blob/trunk/test/openssl/test_pkey.rb#L1 +# frozen_string_literal: true +require_relative "utils" + +class OpenSSL::TestPKey < OpenSSL::PKeyTestCase + def test_generic_oid_inspect + # RSA private key + rsa = Fixtures.pkey("rsa-1") + assert_instance_of OpenSSL::PKey::RSA, rsa + assert_equal "rsaEncryption", rsa.oid + assert_match %r{oid=rsaEncryption}, rsa.inspect + + # X25519 private key + x25519_pem = <<~EOF + -----BEGIN PRIVATE KEY----- + MC4CAQAwBQYDK2VuBCIEIHcHbQpzGKV9PBbBclGyZkXfTC+H68CZKrF3+6UduSwq + -----END PRIVATE KEY----- + EOF + begin + x25519 = OpenSSL::PKey.read(x25519_pem) + rescue OpenSSL::PKey::PKeyError + # OpenSSL < 1.1.0 + pend "X25519 is not implemented" + end + assert_instance_of OpenSSL::PKey::PKey, x25519 + assert_equal "X25519", x25519.oid + assert_match %r{oid=X25519}, x25519.inspect + end +end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/