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