Skip to content

Commit 4193da0

Browse files
committed
[fix] DSA key compatibility when set_pqg
1 parent f4c86f3 commit 4193da0

File tree

4 files changed

+83
-30
lines changed

4 files changed

+83
-30
lines changed

lib/jopenssl/_compat23.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,6 @@ def set_key(pub_key, priv_key)
3333
self
3434
end
3535

36-
def set_pqg(p, q, g)
37-
self.p = p
38-
self.q = q
39-
self.g = g
40-
self
41-
end
42-
4336
end
4437

4538
end

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

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,11 @@ private IRubyObject toBN(BigInteger value) {
450450
}
451451

452452
private synchronized BigInteger getP() {
453+
if (dsa_p != null) return dsa_p;
454+
453455
DSAKey key = getDsaKey();
454-
if (key != null) {
455-
return key.getParams().getP();
456-
}
457-
return dsa_p;
456+
if (key != null) return key.getParams().getP();
457+
return null;
458458
}
459459

460460
@JRubyMethod(name = "p")
@@ -468,11 +468,11 @@ public synchronized IRubyObject set_p(IRubyObject p) {
468468
}
469469

470470
private synchronized BigInteger getQ() {
471+
if (dsa_q != null) return dsa_q;
472+
471473
DSAKey key = getDsaKey();
472-
if (key != null) {
473-
return key.getParams().getQ();
474-
}
475-
return dsa_q;
474+
if (key != null) return key.getParams().getQ();
475+
return null;
476476
}
477477

478478
@JRubyMethod(name = "q")
@@ -486,11 +486,11 @@ public synchronized IRubyObject set_q(IRubyObject q) {
486486
}
487487

488488
private synchronized BigInteger getG() {
489+
if (dsa_g != null) return dsa_g;
490+
489491
DSAKey key = getDsaKey();
490-
if (key != null) {
491-
return key.getParams().getG();
492-
}
493-
return dsa_g;
492+
if (key != null) return key.getParams().getG();
493+
return null;
494494
}
495495

496496
@JRubyMethod(name = "g")
@@ -503,6 +503,15 @@ public synchronized IRubyObject set_g(IRubyObject g) {
503503
return setKeySpecComponent(SPEC_G, g);
504504
}
505505

506+
@JRubyMethod
507+
public IRubyObject set_pqg(IRubyObject p, IRubyObject q, IRubyObject g) {
508+
this.dsa_p = BN.getBigInteger(p);
509+
this.dsa_q = BN.getBigInteger(q);
510+
this.dsa_g = BN.getBigInteger(g);
511+
generateKeyInternal();
512+
return this;
513+
}
514+
506515
@JRubyMethod(name = "priv_key")
507516
public synchronized IRubyObject get_priv_key() {
508517
DSAPrivateKey key;
@@ -533,7 +542,6 @@ public synchronized IRubyObject set_pub_key(IRubyObject pub_key) {
533542

534543
private IRubyObject setKeySpecComponent(final int index, final IRubyObject value) {
535544
final BigInteger val = BN.getBigInteger(value);
536-
537545
switch (index) {
538546
case SPEC_X: this.dsa_x = val; break;
539547
case SPEC_Y: this.dsa_y = val; break;
@@ -542,19 +550,49 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
542550
case SPEC_G: this.dsa_g = val; break;
543551
}
544552

553+
generateKeyInternal();
554+
return value;
555+
}
556+
557+
private BigInteger getX() {
558+
if (dsa_x != null) return dsa_x;
559+
560+
DSAPrivateKey key;
561+
if ((key = this.privateKey) != null) {
562+
return key.getX();
563+
}
564+
return null;
565+
}
566+
567+
private BigInteger getY() {
568+
if (dsa_y != null) return dsa_y;
569+
570+
DSAPublicKey key;
571+
if ((key = this.publicKey) != null) {
572+
return key.getY();
573+
}
574+
return null;
575+
}
576+
577+
private void generateKeyInternal() {
545578
// Don't access the dsa_p, dsa_q and dsa_g fields directly. They may
546579
// have already been consumed and cleared.
547-
BigInteger _dsa_p = getP();
548-
BigInteger _dsa_q = getQ();
549-
BigInteger _dsa_g = getG();
580+
final BigInteger dsa_p = getP();
581+
final BigInteger dsa_q = getQ();
582+
final BigInteger dsa_g = getG();
583+
584+
final BigInteger dsa_x = getX();
585+
final BigInteger dsa_y = getY();
550586

551-
if ( dsa_x != null && _dsa_p != null && _dsa_q != null && _dsa_g != null ) {
587+
if ( dsa_x != null && dsa_p != null && dsa_q != null && dsa_g != null ) {
552588
// we now have all private key components. create the key :
553-
DSAPrivateKeySpec spec = new DSAPrivateKeySpec(dsa_x, _dsa_p, _dsa_q, _dsa_g);
589+
DSAPrivateKeySpec spec = new DSAPrivateKeySpec(dsa_x, dsa_p, dsa_q, dsa_g);
554590
try {
555591
this.privateKey = (DSAPrivateKey) SecurityHelper.getKeyFactory("DSA").generatePrivate(spec);
556592
}
557593
catch (InvalidKeySpecException e) {
594+
e.printStackTrace();
595+
558596
throw newDSAError(getRuntime(), "invalid keyspec", e);
559597
}
560598
catch (NoSuchAlgorithmException e) {
@@ -564,9 +602,9 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
564602
this.dsa_x = this.dsa_p = this.dsa_q = this.dsa_g = null;
565603
}
566604

567-
if ( dsa_y != null && _dsa_p != null && _dsa_q != null && _dsa_g != null ) {
605+
if ( dsa_y != null && dsa_p != null && dsa_q != null && dsa_g != null ) {
568606
// we now have all public key components. create the key :
569-
DSAPublicKeySpec spec = new DSAPublicKeySpec(dsa_y, _dsa_p, _dsa_q, _dsa_g);
607+
DSAPublicKeySpec spec = new DSAPublicKeySpec(dsa_y, dsa_p, dsa_q, dsa_g);
570608
try {
571609
this.publicKey = (DSAPublicKey) SecurityHelper.getKeyFactory("DSA").generatePublic(spec);
572610
}
@@ -579,8 +617,6 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
579617
// clear out the specValues
580618
this.dsa_y = this.dsa_p = this.dsa_q = this.dsa_g = null;
581619
}
582-
583-
return value;
584620
}
585621

586622
private static final int SPEC_X = 0;

src/test/ruby/dsa/test_dsa.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ def setup
77
super
88
end
99

10+
def test_dup
11+
key = Fixtures.pkey("dsa1024")
12+
key2 = key.dup
13+
assert_equal key.params, key2.params
14+
15+
# PKey is immutable in OpenSSL >= 3.0
16+
#if !openssl?(3, 0, 0)
17+
key2.set_pqg(key2.p, key2.q, key2.g + 1)
18+
assert_not_equal key.params, key2.params
19+
#end
20+
end
21+
1022
def test_dsa_param_accessors
1123
key_file = File.join(File.dirname(__FILE__), 'private_key.pem')
1224
key = OpenSSL::PKey::DSA.new(File.read(key_file))
@@ -65,7 +77,7 @@ def test_dsa_sys_sign_verify
6577
doc = 'Sign ME!'
6678
digest = OpenSSL::Digest::SHA1.digest(doc)
6779
sig = dsa.syssign(digest)
68-
puts sig.inspect if $VERBOSE
80+
#puts sig.inspect if $VERBOSE
6981
assert dsa.sysverify(digest, sig).eql?(true)
7082
end
7183

src/test/ruby/fixtures/pkey/dsa1024

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-----BEGIN DSA PRIVATE KEY-----
2+
MIIBugIBAAKBgQCH9aAoXvWWThIjkA6D+nI1F9ksF9iDq594rkiGNOT9sPDOdB+n
3+
D+qeeeeloRlj19ymCSADPI0ZLRgkchkAEnY2RnqnhHOjVf/roGgRbW+iQDMbQ9wa
4+
/pvc6/fAbsu1goE1hBYjm98/sZEeXavj8tR56IXnjF1b6Nx0+sgeUKFKEQIVAMiz
5+
4BJUFeTtddyM4uadBM7HKLPRAoGAZdLBSYNGiij7vAjesF5mGUKTIgPd+JKuBEDx
6+
OaBclsgfdoyoF/TMOkIty+PVlYD+//Vl2xnoUEIRaMXHwHfm0r2xUX++oeRaSScg
7+
YizJdUxe5jvBuBszGPRc/mGpb9YvP0sB+FL1KmuxYmdODfCe51zl8uM/CVhouJ3w
8+
DjmRGscCgYAuFlfC7p+e8huCKydfcv/beftqjewiOPpQ3u5uI6KPCtCJPpDhs3+4
9+
IihH2cPsAlqwGF4tlibW1+/z/OZ1AZinPK3y7b2jSJASEaPeEltVzB92hcd1khk2
10+
jTYcmSsV4VddplOPK9czytR/GbbibxsrhhgZUbd8LPbvIgaiadJ1PgIUBnJ/5vN2
11+
CVArsEzlPUCbohPvZnE=
12+
-----END DSA PRIVATE KEY-----

0 commit comments

Comments
 (0)