@@ -450,11 +450,11 @@ private IRubyObject toBN(BigInteger value) {
450
450
}
451
451
452
452
private synchronized BigInteger getP () {
453
+ if (dsa_p != null ) return dsa_p ;
454
+
453
455
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 ;
458
458
}
459
459
460
460
@ JRubyMethod (name = "p" )
@@ -468,11 +468,11 @@ public synchronized IRubyObject set_p(IRubyObject p) {
468
468
}
469
469
470
470
private synchronized BigInteger getQ () {
471
+ if (dsa_q != null ) return dsa_q ;
472
+
471
473
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 ;
476
476
}
477
477
478
478
@ JRubyMethod (name = "q" )
@@ -486,11 +486,11 @@ public synchronized IRubyObject set_q(IRubyObject q) {
486
486
}
487
487
488
488
private synchronized BigInteger getG () {
489
+ if (dsa_g != null ) return dsa_g ;
490
+
489
491
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 ;
494
494
}
495
495
496
496
@ JRubyMethod (name = "g" )
@@ -503,6 +503,15 @@ public synchronized IRubyObject set_g(IRubyObject g) {
503
503
return setKeySpecComponent (SPEC_G , g );
504
504
}
505
505
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
+
506
515
@ JRubyMethod (name = "priv_key" )
507
516
public synchronized IRubyObject get_priv_key () {
508
517
DSAPrivateKey key ;
@@ -533,7 +542,6 @@ public synchronized IRubyObject set_pub_key(IRubyObject pub_key) {
533
542
534
543
private IRubyObject setKeySpecComponent (final int index , final IRubyObject value ) {
535
544
final BigInteger val = BN .getBigInteger (value );
536
-
537
545
switch (index ) {
538
546
case SPEC_X : this .dsa_x = val ; break ;
539
547
case SPEC_Y : this .dsa_y = val ; break ;
@@ -542,19 +550,49 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
542
550
case SPEC_G : this .dsa_g = val ; break ;
543
551
}
544
552
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 () {
545
578
// Don't access the dsa_p, dsa_q and dsa_g fields directly. They may
546
579
// 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 ();
550
586
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 ) {
552
588
// 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 );
554
590
try {
555
591
this .privateKey = (DSAPrivateKey ) SecurityHelper .getKeyFactory ("DSA" ).generatePrivate (spec );
556
592
}
557
593
catch (InvalidKeySpecException e ) {
594
+ e .printStackTrace ();
595
+
558
596
throw newDSAError (getRuntime (), "invalid keyspec" , e );
559
597
}
560
598
catch (NoSuchAlgorithmException e ) {
@@ -564,9 +602,9 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
564
602
this .dsa_x = this .dsa_p = this .dsa_q = this .dsa_g = null ;
565
603
}
566
604
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 ) {
568
606
// 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 );
570
608
try {
571
609
this .publicKey = (DSAPublicKey ) SecurityHelper .getKeyFactory ("DSA" ).generatePublic (spec );
572
610
}
@@ -579,8 +617,6 @@ private IRubyObject setKeySpecComponent(final int index, final IRubyObject value
579
617
// clear out the specValues
580
618
this .dsa_y = this .dsa_p = this .dsa_q = this .dsa_g = null ;
581
619
}
582
-
583
- return value ;
584
620
}
585
621
586
622
private static final int SPEC_X = 0 ;
0 commit comments