71
71
import org .jruby .ext .openssl .x509store .PEMInputOutput ;
72
72
import static org .jruby .ext .openssl .OpenSSL .*;
73
73
import static org .jruby .ext .openssl .PKey ._PKey ;
74
- import static org .jruby .ext .openssl .PKey .cipherSpec ;
75
74
import static org .jruby .ext .openssl .impl .PKey .readRSAPrivateKey ;
76
75
import static org .jruby .ext .openssl .impl .PKey .readRSAPublicKey ;
77
76
import static org .jruby .ext .openssl .impl .PKey .toDerRSAKey ;
@@ -107,6 +106,10 @@ public static RaiseException newRSAError(Ruby runtime, String message) {
107
106
return Utils .newError (runtime , _PKey (runtime ).getClass ("RSAError" ), message );
108
107
}
109
108
109
+ static RaiseException newRSAError (Ruby runtime , Throwable cause ) {
110
+ return Utils .newError (runtime , _PKey (runtime ).getClass ("RSAError" ), cause .getMessage (), cause );
111
+ }
112
+
110
113
public PKeyRSA (Ruby runtime , RubyClass type ) {
111
114
super (runtime , type );
112
115
}
@@ -149,44 +152,45 @@ public PKeyRSA(Ruby runtime, RubyClass type, RSAPrivateCrtKey privKey, RSAPublic
149
152
150
153
@ JRubyMethod (name = "generate" , meta = true , rest = true )
151
154
public static IRubyObject generate (IRubyObject self , IRubyObject [] args ) {
155
+ final Ruby runtime = self .getRuntime ();
152
156
BigInteger exp = RSAKeyGenParameterSpec .F4 ;
153
- if ( Arity .checkArgumentCount (self . getRuntime () , args , 1 , 2 ) == 2 ) {
157
+ if ( Arity .checkArgumentCount (runtime , args , 1 , 2 ) == 2 ) {
154
158
if (args [1 ] instanceof RubyFixnum ) {
155
159
exp = BigInteger .valueOf (RubyNumeric .num2long (args [1 ]));
156
160
} else {
157
161
exp = ((RubyBignum ) args [1 ]).getValue ();
158
162
}
159
163
}
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 );
164
166
}
165
167
166
168
/*
167
169
* c: rsa_generate
168
170
*/
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 {
170
173
try {
171
174
KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("RSA" );
172
175
if ( "IBMJCEFIPS" .equals ( gen .getProvider ().getName () ) ) {
173
- gen .initialize (keysize ); // IBMJCEFIPS does not support parameters
176
+ gen .initialize (keySize ); // IBMJCEFIPS does not support parameters
174
177
} else {
175
- gen .initialize (new RSAKeyGenParameterSpec (keysize , exp ), new SecureRandom ());
178
+ gen .initialize (new RSAKeyGenParameterSpec (keySize , exp ), new SecureRandom ());
176
179
}
177
180
KeyPair pair = gen .generateKeyPair ();
178
181
rsa .privateKey = (RSAPrivateCrtKey ) pair .getPrivate ();
179
182
rsa .publicKey = (RSAPublicKey ) pair .getPublic ();
180
183
}
181
184
catch (NoSuchAlgorithmException e ) {
182
- throw newRSAError (rsa . getRuntime () , e .getMessage ());
185
+ throw newRSAError (runtime , e .getMessage ());
183
186
}
184
187
catch (InvalidAlgorithmParameterException e ) {
185
- throw newRSAError (rsa . getRuntime () , e .getMessage ());
188
+ throw newRSAError (runtime , e .getMessage ());
186
189
}
187
190
catch (RuntimeException e ) {
188
- throw newRSAError (rsa .getRuntime (), e . getMessage () );
191
+ throw newRSAError (rsa .getRuntime (), e );
189
192
}
193
+ return rsa ;
190
194
}
191
195
192
196
static PKeyRSA newInstance (final Ruby runtime , final PublicKey publicKey ) {
@@ -207,12 +211,12 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
207
211
if ( args .length > 1 ) pass = args [1 ];
208
212
209
213
if ( arg instanceof RubyFixnum ) {
210
- int keysize = RubyNumeric .fix2int ((RubyFixnum ) arg );
214
+ int keySize = RubyNumeric .fix2int ((RubyFixnum ) arg );
211
215
BigInteger exp = RSAKeyGenParameterSpec .F4 ;
212
216
if ( pass != null && ! pass .isNil () ) {
213
217
exp = BigInteger .valueOf (RubyNumeric .num2long (pass ));
214
218
}
215
- rsaGenerate (this , keysize , exp ); return this ;
219
+ return rsaGenerate (runtime , this , keySize , exp );
216
220
}
217
221
218
222
final char [] passwd = password (pass );
@@ -441,7 +445,7 @@ public RubyString to_pem(final IRubyObject[] args) {
441
445
442
446
private String getPadding (final int padding ) {
443
447
if ( padding < 1 || padding > 4 ) {
444
- throw newRSAError (getRuntime (), null );
448
+ throw newRSAError (getRuntime (), "" );
445
449
}
446
450
// BC accepts "/NONE/*" but SunJCE doesn't. use "/ECB/*"
447
451
String p = "/ECB/PKCS1Padding" ;
@@ -461,7 +465,7 @@ public IRubyObject private_encrypt(final ThreadContext context, final IRubyObjec
461
465
if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
462
466
padding = RubyNumeric .fix2int (args [1 ]);
463
467
}
464
- if ( privateKey == null ) throw newRSAError (context .runtime , "private key needed. " );
468
+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA " );
465
469
return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , privateKey );
466
470
}
467
471
@@ -471,7 +475,7 @@ public IRubyObject private_decrypt(final ThreadContext context, final IRubyObjec
471
475
if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
472
476
padding = RubyNumeric .fix2int (args [1 ]);
473
477
}
474
- if ( privateKey == null ) throw newRSAError (context .runtime , "private key needed. " );
478
+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA " );
475
479
return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , privateKey );
476
480
}
477
481
@@ -481,6 +485,7 @@ public IRubyObject public_encrypt(final ThreadContext context, final IRubyObject
481
485
if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
482
486
padding = RubyNumeric .fix2int (args [1 ]);
483
487
}
488
+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
484
489
return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , publicKey );
485
490
}
486
491
@@ -490,6 +495,7 @@ public IRubyObject public_decrypt(final ThreadContext context, final IRubyObject
490
495
if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
491
496
padding = RubyNumeric .fix2int (args [1 ]);
492
497
}
498
+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
493
499
return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , publicKey );
494
500
}
495
501
@@ -545,7 +551,7 @@ public synchronized IRubyObject set_dmp1(final ThreadContext context, IRubyObjec
545
551
if ( privateKey != null ) {
546
552
throw newRSAError (context .runtime , "illegal modification" );
547
553
}
548
- rsa_dmp1 = BN .getBigInteger (value );
554
+ rsa_dmp1 = BN .asBigInteger (value );
549
555
generatePrivateKeyIfParams (context );
550
556
return value ;
551
557
}
@@ -555,7 +561,7 @@ public synchronized IRubyObject set_dmq1(final ThreadContext context, IRubyObjec
555
561
if ( privateKey != null ) {
556
562
throw newRSAError (context .runtime , "illegal modification" );
557
563
}
558
- rsa_dmq1 = BN .getBigInteger (value );
564
+ rsa_dmq1 = BN .asBigInteger (value );
559
565
generatePrivateKeyIfParams (context );
560
566
return value ;
561
567
}
@@ -565,7 +571,7 @@ public synchronized IRubyObject set_iqmp(final ThreadContext context, IRubyObjec
565
571
if ( privateKey != null ) {
566
572
throw newRSAError (context .runtime , "illegal modification" );
567
573
}
568
- rsa_iqmp = BN .getBigInteger (value );
574
+ rsa_iqmp = BN .asBigInteger (value );
569
575
generatePrivateKeyIfParams (context );
570
576
return value ;
571
577
}
0 commit comments