Skip to content

Commit cb5a868

Browse files
committed
[fix] implement (missing) PKey::DSA#params
1 parent f61a45a commit cb5a868

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/main/java/org/jruby/ext/openssl/PKeyDSA.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.math.BigInteger;
3535
import java.security.*;
3636
import java.security.interfaces.DSAKey;
37+
import java.security.interfaces.DSAParams;
3738
import java.security.interfaces.DSAPrivateKey;
3839
import java.security.interfaces.DSAPublicKey;
3940
import java.security.spec.DSAPrivateKeySpec;
@@ -45,6 +46,7 @@
4546
import org.jruby.RubyBoolean;
4647
import org.jruby.RubyClass;
4748
import org.jruby.RubyFixnum;
49+
import org.jruby.RubyHash;
4850
import org.jruby.RubyModule;
4951
import org.jruby.RubyNumeric;
5052
import org.jruby.RubyString;
@@ -348,6 +350,31 @@ public PKeyDSA public_key() {
348350
return new PKeyDSA(getRuntime(), this.publicKey);
349351
}
350352

353+
@JRubyMethod
354+
public IRubyObject params(final ThreadContext context) {
355+
final Ruby runtime = context.runtime;
356+
RubyHash hash = RubyHash.newHash(runtime);
357+
if (publicKey != null) {
358+
if (publicKey.getParams() != null) {
359+
setParams(context, runtime, hash, publicKey.getParams());
360+
}
361+
hash.op_aset(context, runtime.newString("pub_key"), BN.newBN(runtime, publicKey.getY()));
362+
}
363+
if (privateKey != null) {
364+
if (publicKey == null && privateKey.getParams() != null) {
365+
setParams(context, runtime, hash, privateKey.getParams());
366+
}
367+
hash.op_aset(context, runtime.newString("priv_key"), BN.newBN(runtime, privateKey.getX()));
368+
}
369+
return hash;
370+
}
371+
372+
private static void setParams(ThreadContext context, Ruby runtime, RubyHash hash, DSAParams params) {
373+
hash.op_aset(context, runtime.newString("p"), BN.newBN(runtime, params.getP()));
374+
hash.op_aset(context, runtime.newString("q"), BN.newBN(runtime, params.getQ()));
375+
hash.op_aset(context, runtime.newString("g"), BN.newBN(runtime, params.getG()));
376+
}
377+
351378
@Override
352379
@JRubyMethod(name = { "to_pem", "to_s" }, alias = "export", rest = true)
353380
public RubyString to_pem(final ThreadContext context, final IRubyObject[] args) {

0 commit comments

Comments
 (0)