@@ -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 ;
0 commit comments