From e0c6f9bee013c93bb302153ee2db09cdaff591f9 Mon Sep 17 00:00:00 2001 From: kares Date: Mon, 8 Apr 2024 10:15:57 +0200 Subject: [PATCH] [feat] implement #oid method for PKey classes (#281) --- src/main/java/org/jruby/ext/openssl/PKeyDSA.java | 5 +++++ src/main/java/org/jruby/ext/openssl/PKeyEC.java | 5 +++++ src/main/java/org/jruby/ext/openssl/PKeyRSA.java | 5 +++++ src/test/ruby/ec/test_ec.rb | 5 +++++ src/test/ruby/rsa/test_rsa.rb | 5 +++++ 5 files changed, 25 insertions(+) diff --git a/src/main/java/org/jruby/ext/openssl/PKeyDSA.java b/src/main/java/org/jruby/ext/openssl/PKeyDSA.java index 0e0ebc54..d8b8be9b 100644 --- a/src/main/java/org/jruby/ext/openssl/PKeyDSA.java +++ b/src/main/java/org/jruby/ext/openssl/PKeyDSA.java @@ -441,6 +441,11 @@ public IRubyObject sysverify(IRubyObject data, IRubyObject sign) { } } + @JRubyMethod + public IRubyObject oid() { + return getRuntime().newString("DSA"); + } + private DSAKey getDsaKey() { DSAKey result; return (result = publicKey) != null ? result : privateKey; diff --git a/src/main/java/org/jruby/ext/openssl/PKeyEC.java b/src/main/java/org/jruby/ext/openssl/PKeyEC.java index 3352d095..f820b572 100644 --- a/src/main/java/org/jruby/ext/openssl/PKeyEC.java +++ b/src/main/java/org/jruby/ext/openssl/PKeyEC.java @@ -542,6 +542,11 @@ public IRubyObject dh_compute_key(final ThreadContext context, final IRubyObject } } + @JRubyMethod + public IRubyObject oid() { + return getRuntime().newString("id-ecPublicKey"); + } + private Group getGroup(boolean required) { if (group == null) { if (publicKey != null) { diff --git a/src/main/java/org/jruby/ext/openssl/PKeyRSA.java b/src/main/java/org/jruby/ext/openssl/PKeyRSA.java index 66a6885c..b4ee265e 100644 --- a/src/main/java/org/jruby/ext/openssl/PKeyRSA.java +++ b/src/main/java/org/jruby/ext/openssl/PKeyRSA.java @@ -541,6 +541,11 @@ private RubyString doCipherRSA(final Ruby runtime, } } + @JRubyMethod + public IRubyObject oid() { + return getRuntime().newString("rsaEncryption"); + } + @JRubyMethod(name="d=") public synchronized IRubyObject set_d(final ThreadContext context, IRubyObject value) { if ( privateKey != null ) { diff --git a/src/test/ruby/ec/test_ec.rb b/src/test/ruby/ec/test_ec.rb index bcc2312e..6cba7090 100644 --- a/src/test/ruby/ec/test_ec.rb +++ b/src/test/ruby/ec/test_ec.rb @@ -3,6 +3,11 @@ class TestEC < TestCase + def test_oid + key = OpenSSL::PKey::EC.new + assert_equal 'id-ecPublicKey', key.oid + end + def test_read_pem key_file = File.join(File.dirname(__FILE__), 'private_key.pem') diff --git a/src/test/ruby/rsa/test_rsa.rb b/src/test/ruby/rsa/test_rsa.rb index f7b22c83..bdc89356 100644 --- a/src/test/ruby/rsa/test_rsa.rb +++ b/src/test/ruby/rsa/test_rsa.rb @@ -9,6 +9,11 @@ def setup require 'base64' end + def test_oid + key = OpenSSL::PKey::RSA.new + assert_equal 'rsaEncryption', key.oid + end + def test_rsa_private_decrypt key_file = File.join(File.dirname(__FILE__), 'private_key.pem') base64_cipher = "oj1VB1Lnh6j5Ahoq4dllIXkStZHaT9RvizB0x+yIUDtzi6grSh9vXoCchb+U\nkyLOcMmIXopv1Oe7h2te+XS63AG0EAfUhKTFVDYkm7VmcXue25MPr+P+0w+7\nWjZci4VRBLq3T2qZa3IJhQPsNAtEE1DYXnEjNe0jcFa2bu8TPNscoogo5aAw\nQGT+3cKe7A053czG47Sip7aIo+4NlJHE9kFMOTLaWi3fvv/M9/VKo3Bmm/88\n8Ai09LncNTpq787CRHw/wfjuPlQJOiLt+i7AZHBl6x0jK9bqkhPK5YwP0vmc\nuL52QLzgPxj9E78crg47iJDOgNwU/ux1/VuKnlQ9PQ==\n"