@@ -648,17 +648,19 @@ public synchronized IRubyObject get_q() {
648
648
return getRuntime ().getNil ();
649
649
}
650
650
651
- @ JRubyMethod (name ="e" )
652
- public synchronized IRubyObject get_e () {
653
- RSAPublicKey key ;
654
- BigInteger e ;
655
- if ((key = publicKey ) != null ) {
656
- e = key .getPublicExponent ();
657
- } else if (privateKey != null ) {
658
- e = privateKey .getPublicExponent ();
651
+ private BigInteger getPublicExponent () {
652
+ if (publicKey != null ) {
653
+ return publicKey .getPublicExponent ();
654
+ } else if (privateKey != null ) {
655
+ return privateKey .getPublicExponent ();
659
656
} else {
660
- e = rsa_e ;
657
+ return rsa_e ;
661
658
}
659
+ }
660
+
661
+ @ JRubyMethod (name ="e" )
662
+ public synchronized IRubyObject get_e () {
663
+ BigInteger e = getPublicExponent ();
662
664
if (e != null ) {
663
665
return BN .newBN (getRuntime (), e );
664
666
}
@@ -679,17 +681,19 @@ public synchronized IRubyObject set_e(final ThreadContext context, IRubyObject v
679
681
return value ;
680
682
}
681
683
682
- @ JRubyMethod (name ="n" )
683
- public synchronized IRubyObject get_n () {
684
- RSAPublicKey key ;
685
- BigInteger n ;
686
- if ((key = publicKey ) != null ) {
687
- n = key .getModulus ();
688
- } else if (privateKey != null ) {
689
- n = privateKey .getModulus ();
684
+ private BigInteger getModulus () {
685
+ if (publicKey != null ) {
686
+ return publicKey .getModulus ();
687
+ } else if (privateKey != null ) {
688
+ return privateKey .getModulus ();
690
689
} else {
691
- n = rsa_n ;
690
+ return rsa_n ;
692
691
}
692
+ }
693
+
694
+ @ JRubyMethod (name ="n" )
695
+ public synchronized IRubyObject get_n () {
696
+ BigInteger n = getModulus ();
693
697
if (n != null ) {
694
698
return BN .newBN (getRuntime (), n );
695
699
}
@@ -715,8 +719,12 @@ private void generatePublicKeyIfParams(final ThreadContext context) {
715
719
716
720
if ( publicKey != null ) throw newRSAError (runtime , "illegal modification" );
717
721
718
- BigInteger e , n ;
719
- if ( (e = rsa_e ) != null && (n = rsa_n ) != null ) {
722
+ // Don't access the rsa_n and rsa_e fields directly. They may have
723
+ // already been consumed and cleared by generatePrivateKeyIfParams.
724
+ BigInteger _rsa_n = getModulus ();
725
+ BigInteger _rsa_e = getPublicExponent ();
726
+
727
+ if (_rsa_n != null && _rsa_e != null ) {
720
728
final KeyFactory rsaFactory ;
721
729
try {
722
730
rsaFactory = SecurityHelper .getKeyFactory ("RSA" );
@@ -726,7 +734,7 @@ private void generatePublicKeyIfParams(final ThreadContext context) {
726
734
}
727
735
728
736
try {
729
- publicKey = (RSAPublicKey ) rsaFactory .generatePublic (new RSAPublicKeySpec (n , e ));
737
+ publicKey = (RSAPublicKey ) rsaFactory .generatePublic (new RSAPublicKeySpec (_rsa_n , _rsa_e ));
730
738
}
731
739
catch (InvalidKeySpecException ex ) {
732
740
throw newRSAError (runtime , "invalid parameters" );
@@ -741,7 +749,12 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
741
749
742
750
if ( privateKey != null ) throw newRSAError (runtime , "illegal modification" );
743
751
744
- if (rsa_e != null && rsa_n != null && rsa_p != null && rsa_q != null && rsa_d != null && rsa_dmp1 != null && rsa_dmq1 != null && rsa_iqmp != null ) {
752
+ // Don't access the rsa_n and rsa_e fields directly. They may have
753
+ // already been consumed and cleared by generatePublicKeyIfParams.
754
+ BigInteger _rsa_n = getModulus ();
755
+ BigInteger _rsa_e = getPublicExponent ();
756
+
757
+ if (_rsa_n != null && _rsa_e != null && rsa_p != null && rsa_q != null && rsa_d != null && rsa_dmp1 != null && rsa_dmq1 != null && rsa_iqmp != null ) {
745
758
final KeyFactory rsaFactory ;
746
759
try {
747
760
rsaFactory = SecurityHelper .getKeyFactory ("RSA" );
@@ -752,7 +765,7 @@ private void generatePrivateKeyIfParams(final ThreadContext context) {
752
765
753
766
try {
754
767
privateKey = (RSAPrivateCrtKey ) rsaFactory .generatePrivate (
755
- new RSAPrivateCrtKeySpec (rsa_n , rsa_e , rsa_d , rsa_p , rsa_q , rsa_dmp1 , rsa_dmq1 , rsa_iqmp )
768
+ new RSAPrivateCrtKeySpec (_rsa_n , _rsa_e , rsa_d , rsa_p , rsa_q , rsa_dmp1 , rsa_dmq1 , rsa_iqmp )
756
769
);
757
770
}
758
771
catch (InvalidKeySpecException e ) {
0 commit comments