diff --git a/src/main/java/org/jruby/ext/openssl/PKeyDSA.java b/src/main/java/org/jruby/ext/openssl/PKeyDSA.java index aebd1e68..be364fdf 100644 --- a/src/main/java/org/jruby/ext/openssl/PKeyDSA.java +++ b/src/main/java/org/jruby/ext/openssl/PKeyDSA.java @@ -67,6 +67,7 @@ import static org.jruby.ext.openssl.impl.PKey.readDSAPublicKey; import static org.jruby.ext.openssl.impl.PKey.toASN1Primitive; import static org.jruby.ext.openssl.impl.PKey.toDerDSAKey; +import static org.jruby.ext.openssl.impl.PKey.toDerDSAPublicKey; /** * @author Ola Bini @@ -298,6 +299,21 @@ public RubyBoolean private_p() { return privateKey != null ? getRuntime().getTrue() : getRuntime().getFalse(); } + @JRubyMethod(name = "public_to_der") + public RubyString public_to_der(ThreadContext context) { + final byte[] bytes; + try { + bytes = toDerDSAPublicKey(publicKey); + } + catch (NoClassDefFoundError e) { + throw newDSAError(getRuntime(), bcExceptionMessage(e)); + } + catch (IOException e) { + throw newDSAError(getRuntime(), e.getMessage(), e); + } + return StringHelper.newString(context.runtime, bytes); + } + @Override @JRubyMethod(name = "to_der") public RubyString to_der() { @@ -398,6 +414,21 @@ public RubyString to_pem(final ThreadContext context, final IRubyObject[] args) } } + @JRubyMethod + public RubyString public_to_pem(ThreadContext context) { + try { + final StringWriter writer = new StringWriter(); + PEMInputOutput.writeDSAPublicKey(writer, publicKey); + return RubyString.newString(context.runtime, writer.getBuffer()); + } + catch (NoClassDefFoundError ncdfe) { + throw newDSAError(context.runtime, bcExceptionMessage(ncdfe)); + } + catch (IOException e) { + throw newDSAError(context.runtime, e.getMessage(), e); + } + } + @JRubyMethod // ossl_dsa_sign public IRubyObject syssign(IRubyObject data) { final Ruby runtime = getRuntime(); diff --git a/src/test/ruby/dsa/test_dsa.rb b/src/test/ruby/dsa/test_dsa.rb index e2c998c3..3235777d 100644 --- a/src/test/ruby/dsa/test_dsa.rb +++ b/src/test/ruby/dsa/test_dsa.rb @@ -102,26 +102,26 @@ def test_DSAPrivateKey # OpenSSL DSAPrivateKey format; similar to RSAPrivateKey dsa512 = Fixtures.pkey("dsa512") asn1 = OpenSSL::ASN1::Sequence([ - OpenSSL::ASN1::Integer(0), - OpenSSL::ASN1::Integer(dsa512.p), - OpenSSL::ASN1::Integer(dsa512.q), - OpenSSL::ASN1::Integer(dsa512.g), - OpenSSL::ASN1::Integer(dsa512.pub_key), - OpenSSL::ASN1::Integer(dsa512.priv_key) - ]) + OpenSSL::ASN1::Integer(0), + OpenSSL::ASN1::Integer(dsa512.p), + OpenSSL::ASN1::Integer(dsa512.q), + OpenSSL::ASN1::Integer(dsa512.g), + OpenSSL::ASN1::Integer(dsa512.pub_key), + OpenSSL::ASN1::Integer(dsa512.priv_key) + ]) key = OpenSSL::PKey::DSA.new(asn1.to_der) assert_predicate key, :private? assert_same_dsa dsa512, key - pem = <<-EOF ------BEGIN DSA PRIVATE KEY----- -MIH4AgEAAkEA5lB4GvEwjrsMlGDqGsxrbqeFRh6o9OWt6FgTYiEEHaOYhkIxv0Ok -RZPDNwOG997mDjBnvDJ1i56OmS3MbTnovwIVAJgub/aDrSDB4DZGH7UyarcaGy6D -AkB9HdFw/3td8K4l1FZHv7TCZeJ3ZLb7dF3TWoGUP003RCqoji3/lHdKoVdTQNuR -S/m6DlCwhjRjiQ/lBRgCLCcaAkEAjN891JBjzpMj4bWgsACmMggFf57DS0Ti+5++ -Q1VB8qkJN7rA7/2HrCR3gTsWNb1YhAsnFsoeRscC+LxXoXi9OAIUBG98h4tilg6S -55jreJD3Se3slps= ------END DSA PRIVATE KEY----- + pem = <<~EOF + -----BEGIN DSA PRIVATE KEY----- + MIH4AgEAAkEA5lB4GvEwjrsMlGDqGsxrbqeFRh6o9OWt6FgTYiEEHaOYhkIxv0Ok + RZPDNwOG997mDjBnvDJ1i56OmS3MbTnovwIVAJgub/aDrSDB4DZGH7UyarcaGy6D + AkB9HdFw/3td8K4l1FZHv7TCZeJ3ZLb7dF3TWoGUP003RCqoji3/lHdKoVdTQNuR + S/m6DlCwhjRjiQ/lBRgCLCcaAkEAjN891JBjzpMj4bWgsACmMggFf57DS0Ti+5++ + Q1VB8qkJN7rA7/2HrCR3gTsWNb1YhAsnFsoeRscC+LxXoXi9OAIUBG98h4tilg6S + 55jreJD3Se3slps= + -----END DSA PRIVATE KEY----- EOF key = OpenSSL::PKey::DSA.new(pem) assert_same_dsa dsa512, key @@ -133,18 +133,18 @@ def test_DSAPrivateKey def test_DSAPrivateKey_encrypted # key = abcdef dsa512 = Fixtures.pkey("dsa512") - pem = <<-EOF ------BEGIN DSA PRIVATE KEY----- -Proc-Type: 4,ENCRYPTED -DEK-Info: AES-128-CBC,F8BB7BFC7EAB9118AC2E3DA16C8DB1D9 - -D2sIzsM9MLXBtlF4RW42u2GB9gX3HQ3prtVIjWPLaKBYoToRUiv8WKsjptfZuLSB -74ZPdMS7VITM+W1HIxo/tjS80348Cwc9ou8H/E6WGat8ZUk/igLOUEII+coQS6qw -QpuLMcCIavevX0gjdjEIkojBB81TYDofA1Bp1z1zDI/2Zhw822xapI79ZF7Rmywt -OSyWzFaGipgDpdFsGzvT6//z0jMr0AuJVcZ0VJ5lyPGQZAeVBlbYEI4T72cC5Cz7 -XvLiaUtum6/sASD2PQqdDNpgx/WA6Vs1Po2kIUQIM5TIwyJI0GdykZcYm6xIK/ta -Wgx6c8K+qBAIVrilw3EWxw== ------END DSA PRIVATE KEY----- + pem = <<~EOF + -----BEGIN DSA PRIVATE KEY----- + Proc-Type: 4,ENCRYPTED + DEK-Info: AES-128-CBC,F8BB7BFC7EAB9118AC2E3DA16C8DB1D9 + + D2sIzsM9MLXBtlF4RW42u2GB9gX3HQ3prtVIjWPLaKBYoToRUiv8WKsjptfZuLSB + 74ZPdMS7VITM+W1HIxo/tjS80348Cwc9ou8H/E6WGat8ZUk/igLOUEII+coQS6qw + QpuLMcCIavevX0gjdjEIkojBB81TYDofA1Bp1z1zDI/2Zhw822xapI79ZF7Rmywt + OSyWzFaGipgDpdFsGzvT6//z0jMr0AuJVcZ0VJ5lyPGQZAeVBlbYEI4T72cC5Cz7 + XvLiaUtum6/sASD2PQqdDNpgx/WA6Vs1Po2kIUQIM5TIwyJI0GdykZcYm6xIK/ta + Wgx6c8K+qBAIVrilw3EWxw== + -----END DSA PRIVATE KEY----- EOF key = OpenSSL::PKey::DSA.new(pem, "abcdef") assert_same_dsa dsa512, key @@ -196,6 +196,12 @@ def test_PUBKEY ## assert_equal der, key.to_der + assert_equal pem, key.export + + assert_equal der, dsa512.public_to_der + assert_equal der, key.public_to_der + assert_equal pem, dsa512.public_to_pem + assert_equal pem, key.public_to_pem dup_der = dup_public(dsa512).to_der # pp OpenSSL::ASN1.decode(dup_der)