7171import org .jruby .ext .openssl .x509store .PEMInputOutput ;
7272import static org .jruby .ext .openssl .OpenSSL .*;
7373import static org .jruby .ext .openssl .PKey ._PKey ;
74- import static org .jruby .ext .openssl .PKey .cipherSpec ;
7574import static org .jruby .ext .openssl .impl .PKey .readRSAPrivateKey ;
7675import static org .jruby .ext .openssl .impl .PKey .readRSAPublicKey ;
7776import static org .jruby .ext .openssl .impl .PKey .toDerRSAKey ;
@@ -107,6 +106,10 @@ public static RaiseException newRSAError(Ruby runtime, String message) {
107106 return Utils .newError (runtime , _PKey (runtime ).getClass ("RSAError" ), message );
108107 }
109108
109+ static RaiseException newRSAError (Ruby runtime , Throwable cause ) {
110+ return Utils .newError (runtime , _PKey (runtime ).getClass ("RSAError" ), cause .getMessage (), cause );
111+ }
112+
110113 public PKeyRSA (Ruby runtime , RubyClass type ) {
111114 super (runtime , type );
112115 }
@@ -149,44 +152,45 @@ public PKeyRSA(Ruby runtime, RubyClass type, RSAPrivateCrtKey privKey, RSAPublic
149152
150153 @ JRubyMethod (name = "generate" , meta = true , rest = true )
151154 public static IRubyObject generate (IRubyObject self , IRubyObject [] args ) {
155+ final Ruby runtime = self .getRuntime ();
152156 BigInteger exp = RSAKeyGenParameterSpec .F4 ;
153- if ( Arity .checkArgumentCount (self . getRuntime () , args , 1 , 2 ) == 2 ) {
157+ if ( Arity .checkArgumentCount (runtime , args , 1 , 2 ) == 2 ) {
154158 if (args [1 ] instanceof RubyFixnum ) {
155159 exp = BigInteger .valueOf (RubyNumeric .num2long (args [1 ]));
156160 } else {
157161 exp = ((RubyBignum ) args [1 ]).getValue ();
158162 }
159163 }
160- int keysize = RubyNumeric .fix2int (args [0 ]);
161- PKeyRSA rsa = new PKeyRSA (self .getRuntime (), (RubyClass ) self );
162- rsaGenerate (rsa , keysize , exp );
163- return rsa ;
164+ final int keySize = RubyNumeric .fix2int (args [0 ]);
165+ return rsaGenerate (runtime , new PKeyRSA (runtime , (RubyClass ) self ), keySize , exp );
164166 }
165167
166168 /*
167169 * c: rsa_generate
168170 */
169- private static void rsaGenerate (PKeyRSA rsa , int keysize , BigInteger exp ) throws RaiseException {
171+ private static PKeyRSA rsaGenerate (final Ruby runtime ,
172+ PKeyRSA rsa , int keySize , BigInteger exp ) throws RaiseException {
170173 try {
171174 KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("RSA" );
172175 if ( "IBMJCEFIPS" .equals ( gen .getProvider ().getName () ) ) {
173- gen .initialize (keysize ); // IBMJCEFIPS does not support parameters
176+ gen .initialize (keySize ); // IBMJCEFIPS does not support parameters
174177 } else {
175- gen .initialize (new RSAKeyGenParameterSpec (keysize , exp ), new SecureRandom ());
178+ gen .initialize (new RSAKeyGenParameterSpec (keySize , exp ), new SecureRandom ());
176179 }
177180 KeyPair pair = gen .generateKeyPair ();
178181 rsa .privateKey = (RSAPrivateCrtKey ) pair .getPrivate ();
179182 rsa .publicKey = (RSAPublicKey ) pair .getPublic ();
180183 }
181184 catch (NoSuchAlgorithmException e ) {
182- throw newRSAError (rsa . getRuntime () , e .getMessage ());
185+ throw newRSAError (runtime , e .getMessage ());
183186 }
184187 catch (InvalidAlgorithmParameterException e ) {
185- throw newRSAError (rsa . getRuntime () , e .getMessage ());
188+ throw newRSAError (runtime , e .getMessage ());
186189 }
187190 catch (RuntimeException e ) {
188- throw newRSAError (rsa .getRuntime (), e . getMessage () );
191+ throw newRSAError (rsa .getRuntime (), e );
189192 }
193+ return rsa ;
190194 }
191195
192196 static PKeyRSA newInstance (final Ruby runtime , final PublicKey publicKey ) {
@@ -207,12 +211,12 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
207211 if ( args .length > 1 ) pass = args [1 ];
208212
209213 if ( arg instanceof RubyFixnum ) {
210- int keysize = RubyNumeric .fix2int ((RubyFixnum ) arg );
214+ int keySize = RubyNumeric .fix2int ((RubyFixnum ) arg );
211215 BigInteger exp = RSAKeyGenParameterSpec .F4 ;
212216 if ( pass != null && ! pass .isNil () ) {
213217 exp = BigInteger .valueOf (RubyNumeric .num2long (pass ));
214218 }
215- rsaGenerate (this , keysize , exp ); return this ;
219+ return rsaGenerate (runtime , this , keySize , exp );
216220 }
217221
218222 final char [] passwd = password (pass );
@@ -441,7 +445,7 @@ public RubyString to_pem(final IRubyObject[] args) {
441445
442446 private String getPadding (final int padding ) {
443447 if ( padding < 1 || padding > 4 ) {
444- throw newRSAError (getRuntime (), null );
448+ throw newRSAError (getRuntime (), "" );
445449 }
446450 // BC accepts "/NONE/*" but SunJCE doesn't. use "/ECB/*"
447451 String p = "/ECB/PKCS1Padding" ;
@@ -461,7 +465,7 @@ public IRubyObject private_encrypt(final ThreadContext context, final IRubyObjec
461465 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
462466 padding = RubyNumeric .fix2int (args [1 ]);
463467 }
464- if ( privateKey == null ) throw newRSAError (context .runtime , "private key needed. " );
468+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA " );
465469 return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , privateKey );
466470 }
467471
@@ -471,7 +475,7 @@ public IRubyObject private_decrypt(final ThreadContext context, final IRubyObjec
471475 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
472476 padding = RubyNumeric .fix2int (args [1 ]);
473477 }
474- if ( privateKey == null ) throw newRSAError (context .runtime , "private key needed. " );
478+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA " );
475479 return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , privateKey );
476480 }
477481
@@ -481,6 +485,7 @@ public IRubyObject public_encrypt(final ThreadContext context, final IRubyObject
481485 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
482486 padding = RubyNumeric .fix2int (args [1 ]);
483487 }
488+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
484489 return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , publicKey );
485490 }
486491
@@ -490,6 +495,7 @@ public IRubyObject public_decrypt(final ThreadContext context, final IRubyObject
490495 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
491496 padding = RubyNumeric .fix2int (args [1 ]);
492497 }
498+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
493499 return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , publicKey );
494500 }
495501
@@ -545,7 +551,7 @@ public synchronized IRubyObject set_dmp1(final ThreadContext context, IRubyObjec
545551 if ( privateKey != null ) {
546552 throw newRSAError (context .runtime , "illegal modification" );
547553 }
548- rsa_dmp1 = BN .getBigInteger (value );
554+ rsa_dmp1 = BN .asBigInteger (value );
549555 generatePrivateKeyIfParams (context );
550556 return value ;
551557 }
@@ -555,7 +561,7 @@ public synchronized IRubyObject set_dmq1(final ThreadContext context, IRubyObjec
555561 if ( privateKey != null ) {
556562 throw newRSAError (context .runtime , "illegal modification" );
557563 }
558- rsa_dmq1 = BN .getBigInteger (value );
564+ rsa_dmq1 = BN .asBigInteger (value );
559565 generatePrivateKeyIfParams (context );
560566 return value ;
561567 }
@@ -565,7 +571,7 @@ public synchronized IRubyObject set_iqmp(final ThreadContext context, IRubyObjec
565571 if ( privateKey != null ) {
566572 throw newRSAError (context .runtime , "illegal modification" );
567573 }
568- rsa_iqmp = BN .getBigInteger (value );
574+ rsa_iqmp = BN .asBigInteger (value );
569575 generatePrivateKeyIfParams (context );
570576 return value ;
571577 }
0 commit comments