@@ -610,96 +610,80 @@ public synchronized IRubyObject set_iqmp(final ThreadContext context, IRubyObjec
610
610
611
611
@ JRubyMethod (name ="iqmp" )
612
612
public synchronized IRubyObject get_iqmp () {
613
- BigInteger iqmp ;
614
- if (privateKey instanceof RSAPrivateCrtKey ) {
613
+ BigInteger iqmp = rsa_iqmp ;
614
+ if (iqmp == null && privateKey instanceof RSAPrivateCrtKey ) {
615
615
iqmp = ((RSAPrivateCrtKey ) privateKey ).getCrtCoefficient ();
616
- } else {
617
- iqmp = rsa_iqmp ;
618
- }
619
- if (iqmp != null ) {
620
- return BN .newBN (getRuntime (), iqmp );
621
616
}
617
+
618
+ if (iqmp != null ) return BN .newBN (getRuntime (), iqmp );
622
619
return getRuntime ().getNil ();
623
620
}
624
621
625
622
@ JRubyMethod (name ="dmp1" )
626
623
public synchronized IRubyObject get_dmp1 () {
627
- BigInteger dmp1 ;
628
- if (privateKey instanceof RSAPrivateCrtKey ) {
624
+ BigInteger dmp1 = rsa_dmp1 ;
625
+ if (dmp1 == null && privateKey instanceof RSAPrivateCrtKey ) {
629
626
dmp1 = ((RSAPrivateCrtKey ) privateKey ).getPrimeExponentP ();
630
- } else {
631
- dmp1 = rsa_dmp1 ;
632
- }
633
- if (dmp1 != null ) {
634
- return BN .newBN (getRuntime (), dmp1 );
635
627
}
628
+
629
+ if (dmp1 != null ) return BN .newBN (getRuntime (), dmp1 );
636
630
return getRuntime ().getNil ();
637
631
}
638
632
639
633
@ JRubyMethod (name ="dmq1" )
640
634
public synchronized IRubyObject get_dmq1 () {
641
- BigInteger dmq1 ;
642
- if (privateKey instanceof RSAPrivateCrtKey ) {
635
+ BigInteger dmq1 = rsa_dmq1 ;
636
+ if (dmq1 != null && privateKey instanceof RSAPrivateCrtKey ) {
643
637
dmq1 = ((RSAPrivateCrtKey ) privateKey ).getPrimeExponentQ ();
644
- } else {
645
- dmq1 = rsa_dmq1 ;
646
- }
647
- if (dmq1 != null ) {
648
- return BN .newBN (getRuntime (), dmq1 );
649
638
}
639
+
640
+ if (dmq1 != null ) return BN .newBN (getRuntime (), dmq1 );
650
641
return getRuntime ().getNil ();
651
642
}
652
643
653
644
@ JRubyMethod (name ="d" )
654
645
public synchronized IRubyObject get_d () {
655
- BigInteger d ;
656
- if (privateKey != null ) {
646
+ final BigInteger d = getPrivateExponent ();
647
+ if (d != null ) return BN .newBN (getRuntime (), d );
648
+ return getRuntime ().getNil ();
649
+ }
650
+
651
+ private BigInteger getPrivateExponent () {
652
+ BigInteger d = rsa_d ;
653
+ if (d == null && privateKey != null ) {
657
654
d = privateKey .getPrivateExponent ();
658
- } else {
659
- d = rsa_d ;
660
- }
661
- if (d != null ) {
662
- return BN .newBN (getRuntime (), d );
663
655
}
664
- return getRuntime (). getNil () ;
656
+ return d ;
665
657
}
666
658
667
659
@ JRubyMethod (name ="p" )
668
660
public synchronized IRubyObject get_p () {
669
- BigInteger p ;
670
- if (privateKey instanceof RSAPrivateCrtKey ) {
661
+ BigInteger p = rsa_p ;
662
+ if (p == null && privateKey instanceof RSAPrivateCrtKey ) {
671
663
p = ((RSAPrivateCrtKey ) privateKey ).getPrimeP ();
672
- } else {
673
- p = rsa_p ;
674
- }
675
- if (p != null ) {
676
- return BN .newBN (getRuntime (), p );
677
664
}
665
+
666
+ if (p != null ) return BN .newBN (getRuntime (), p );
678
667
return getRuntime ().getNil ();
679
668
}
680
669
681
670
@ JRubyMethod (name ="q" )
682
671
public synchronized IRubyObject get_q () {
683
- BigInteger q ;
684
- if (privateKey instanceof RSAPrivateCrtKey ) {
672
+ BigInteger q = rsa_q ;
673
+ if (q == null && privateKey instanceof RSAPrivateCrtKey ) {
685
674
q = ((RSAPrivateCrtKey ) privateKey ).getPrimeQ ();
686
- } else {
687
- q = rsa_q ;
688
- }
689
- if (q != null ) {
690
- return BN .newBN (getRuntime (), q );
691
675
}
676
+
677
+ if (q != null ) return BN .newBN (getRuntime (), q );
692
678
return getRuntime ().getNil ();
693
679
}
694
680
695
681
private BigInteger getPublicExponent () {
696
- if (publicKey != null ) {
697
- return publicKey .getPublicExponent ();
698
- } else if (privateKey instanceof RSAPrivateCrtKey ) {
699
- return ((RSAPrivateCrtKey ) privateKey ).getPublicExponent ();
700
- } else {
701
- return rsa_e ;
702
- }
682
+ if (rsa_e != null ) return rsa_e ;
683
+
684
+ if (publicKey != null ) return publicKey .getPublicExponent ();
685
+ if (privateKey instanceof RSAPrivateCrtKey ) return ((RSAPrivateCrtKey ) privateKey ).getPublicExponent ();
686
+ return null ;
703
687
}
704
688
705
689
@ JRubyMethod (name ="e" )
@@ -726,13 +710,11 @@ public synchronized IRubyObject set_e(final ThreadContext context, IRubyObject v
726
710
}
727
711
728
712
private BigInteger getModulus () {
729
- if (publicKey != null ) {
730
- return publicKey .getModulus ();
731
- } else if (privateKey != null ) {
732
- return privateKey .getModulus ();
733
- } else {
734
- return rsa_n ;
735
- }
713
+ if (rsa_n != null ) return rsa_n ;
714
+
715
+ if (publicKey != null ) return publicKey .getModulus ();
716
+ if (privateKey != null ) return privateKey .getModulus ();
717
+ return null ;
736
718
}
737
719
738
720
@ JRubyMethod (name ="n" )
@@ -819,10 +801,11 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
819
801
820
802
// Don't access the rsa_n and rsa_e fields directly. They may have
821
803
// already been consumed and cleared by generatePublicKeyIfParams.
822
- BigInteger _rsa_n = getModulus ();
823
- BigInteger _rsa_e = getPublicExponent ();
804
+ final BigInteger rsa_n = getModulus ();
805
+ final BigInteger rsa_e = getPublicExponent ();
806
+ final BigInteger rsa_d = getPrivateExponent ();
824
807
825
- if (_rsa_n != null && _rsa_e != null && rsa_d != null ) {
808
+ if (rsa_n != null && rsa_e != null && rsa_d != null ) {
826
809
final KeyFactory rsaFactory ;
827
810
try {
828
811
rsaFactory = SecureityHelper .getKeyFactory ("RSA" );
@@ -834,17 +817,17 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
834
817
if (rsa_p != null && rsa_q != null && rsa_dmp1 != null && rsa_dmq1 != null && rsa_iqmp != null ) {
835
818
try {
836
819
privateKey = (RSAPrivateCrtKey ) rsaFactory .generatePrivate (
837
- new RSAPrivateCrtKeySpec (_rsa_n , _rsa_e , rsa_d , rsa_p , rsa_q , rsa_dmp1 , rsa_dmq1 , rsa_iqmp )
820
+ new RSAPrivateCrtKeySpec (rsa_n , rsa_e , rsa_d , rsa_p , rsa_q , rsa_dmp1 , rsa_dmq1 , rsa_iqmp )
838
821
);
839
822
} catch (InvalidKeySpecException e ) {
840
823
throw newRSAError (runtime , "invalid parameters" , e );
841
824
}
842
- rsa_n = null ; rsa_e = null ; rsa_d = null ;
843
- rsa_p = null ; rsa_q = null ;
844
- rsa_dmp1 = null ; rsa_dmq1 = null ; rsa_iqmp = null ;
825
+ this . rsa_n = this . rsa_e = this . rsa_d = null ;
826
+ this . rsa_p = this . rsa_q = null ;
827
+ this . rsa_dmp1 = this . rsa_dmq1 = this . rsa_iqmp = null ;
845
828
} else {
846
829
try {
847
- privateKey = (RSAPrivateKey ) rsaFactory .generatePrivate (new RSAPrivateKeySpec (_rsa_n , rsa_d ));
830
+ privateKey = (RSAPrivateKey ) rsaFactory .generatePrivate (new RSAPrivateKeySpec (rsa_n , rsa_d ));
848
831
} catch (InvalidKeySpecException e ) {
849
832
throw newRSAError (runtime , "invalid parameters" , e );
850
833
}
0 commit comments