@@ -220,32 +220,33 @@ public static IRubyObject generate(IRubyObject self, IRubyObject[] args) {
220220 return rsaGenerate (runtime , new PKeyRSA (runtime , (RubyClass ) self ), keySize , exp );
221221 }
222222
223+ static PKeyRSA generateImpl (final Ruby runtime , PKeyRSA rsa , int keySize , BigInteger exp )
224+ throws NoSuchAlgorithmException , InvalidAlgorithmParameterException {
225+ KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("RSA" );
226+ if ( "IBMJCEFIPS" .equals ( gen .getProvider ().getName () ) ) {
227+ gen .initialize (keySize ); // IBMJCEFIPS does not support parameters
228+ } else {
229+ gen .initialize (new RSAKeyGenParameterSpec (keySize , exp ), getSecureRandom (runtime ));
230+ }
231+ KeyPair pair = gen .generateKeyPair ();
232+ rsa .privateKey = (RSAPrivateCrtKey ) pair .getPrivate ();
233+ rsa .publicKey = (RSAPublicKey ) pair .getPublic ();
234+ return rsa ;
235+ }
236+
223237 /*
224238 * c: rsa_generate
225239 */
226- private static PKeyRSA rsaGenerate (final Ruby runtime ,
227- PKeyRSA rsa , int keySize , BigInteger exp ) throws RaiseException {
240+ static PKeyRSA rsaGenerate (final Ruby runtime , PKeyRSA rsa , int keySize , BigInteger exp ) throws RaiseException {
228241 try {
229- KeyPairGenerator gen = SecurityHelper .getKeyPairGenerator ("RSA" );
230- if ( "IBMJCEFIPS" .equals ( gen .getProvider ().getName () ) ) {
231- gen .initialize (keySize ); // IBMJCEFIPS does not support parameters
232- } else {
233- gen .initialize (new RSAKeyGenParameterSpec (keySize , exp ), getSecureRandom (runtime ));
234- }
235- KeyPair pair = gen .generateKeyPair ();
236- rsa .privateKey = (RSAPrivateCrtKey ) pair .getPrivate ();
237- rsa .publicKey = (RSAPublicKey ) pair .getPublic ();
238- }
239- catch (NoSuchAlgorithmException e ) {
240- throw newRSAError (runtime , e .getMessage ());
242+ return generateImpl (runtime , rsa , keySize , exp );
241243 }
242- catch (InvalidAlgorithmParameterException e ) {
244+ catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e ) {
243245 throw newRSAError (runtime , e .getMessage ());
244246 }
245247 catch (RuntimeException e ) {
246- throw newRSAError (rsa . getRuntime () , e );
248+ throw newRSAError (runtime , e );
247249 }
248- return rsa ;
249250 }
250251
251252 @ JRubyMethod (rest = true , visibility = Visibility .PRIVATE )
@@ -336,7 +337,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
336337 if ( key == null ) key = tryPKCS8EncodedKey (runtime , rsaFactory , str .getBytes ());
337338 if ( key == null ) key = tryX509EncodedKey (runtime , rsaFactory , str .getBytes ());
338339
339- if ( key == null ) throw newPKeyError (runtime , "Neither PUB key nor PRIV key:" );
340+ if ( key == null ) throw newRSAError (runtime , "Neither PUB key nor PRIV key:" );
340341
341342 if ( key instanceof KeyPair ) {
342343 PublicKey publicKey = ((KeyPair ) key ).getPublic ();
@@ -615,7 +616,7 @@ private static ASN1ObjectIdentifier osslNameToCipherOid(final String osslName) {
615616
616617 private String getPadding (final int padding ) {
617618 if ( padding < 1 || padding > 4 ) {
618- throw newPKeyError (getRuntime (), "" );
619+ throw newRSAError (getRuntime (), "" );
619620 }
620621 // BC accepts "/NONE/*" but SunJCE doesn't. use "/ECB/*"
621622 String p = "/ECB/PKCS1Padding" ;
@@ -635,7 +636,7 @@ public IRubyObject private_encrypt(final ThreadContext context, final IRubyObjec
635636 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
636637 padding = RubyNumeric .fix2int (args [1 ]);
637638 }
638- if ( privateKey == null ) throw newPKeyError (context .runtime , "incomplete RSA" );
639+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
639640 return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , privateKey );
640641 }
641642
@@ -645,7 +646,7 @@ public IRubyObject private_decrypt(final ThreadContext context, final IRubyObjec
645646 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
646647 padding = RubyNumeric .fix2int (args [1 ]);
647648 }
648- if ( privateKey == null ) throw newPKeyError (context .runtime , "incomplete RSA" );
649+ if ( privateKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
649650 return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , privateKey );
650651 }
651652
@@ -655,7 +656,7 @@ public IRubyObject public_encrypt(final ThreadContext context, final IRubyObject
655656 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil ()) {
656657 padding = RubyNumeric .fix2int (args [1 ]);
657658 }
658- if ( publicKey == null ) throw newPKeyError (context .runtime , "incomplete RSA" );
659+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
659660 return doCipherRSA (context .runtime , args [0 ], padding , ENCRYPT_MODE , publicKey );
660661 }
661662
@@ -665,7 +666,7 @@ public IRubyObject public_decrypt(final ThreadContext context, final IRubyObject
665666 if ( Arity .checkArgumentCount (context .runtime , args , 1 , 2 ) == 2 && ! args [1 ].isNil () ) {
666667 padding = RubyNumeric .fix2int (args [1 ]);
667668 }
668- if ( publicKey == null ) throw newPKeyError (context .runtime , "incomplete RSA" );
669+ if ( publicKey == null ) throw newRSAError (context .runtime , "incomplete RSA" );
669670 return doCipherRSA (context .runtime , args [0 ], padding , DECRYPT_MODE , publicKey );
670671 }
671672
@@ -699,7 +700,7 @@ public IRubyObject oid() {
699700 @ JRubyMethod (name = "sign_raw" , required = 2 , optional = 1 )
700701 public IRubyObject sign_raw (ThreadContext context , IRubyObject [] args ) {
701702 final Ruby runtime = context .runtime ;
702- if (privateKey == null ) throw newPKeyError (runtime , "Private RSA key needed!" );
703+ if (privateKey == null ) throw newRSAError (runtime , "Private RSA key needed!" );
703704
704705 final String digestAlg = getDigestAlgName (args [0 ]);
705706 final byte [] hashBytes = args [1 ].convertToString ().getBytes ();
@@ -715,7 +716,7 @@ public IRubyObject sign_raw(ThreadContext context, IRubyObject[] args) {
715716 try {
716717 return StringHelper .newString (runtime , signWithPSS (hashBytes , digestAlg , mgf1Alg , saltLen ));
717718 } catch (IllegalArgumentException | CryptoException e ) {
718- throw (RaiseException ) newPKeyError (runtime , e .getMessage ()).initCause (e );
719+ throw (RaiseException ) newRSAError (runtime , e .getMessage ()).initCause (e );
719720 }
720721 }
721722 }
@@ -726,13 +727,13 @@ public IRubyObject sign_raw(ThreadContext context, IRubyObject[] args) {
726727 ByteList signed = sign ("NONEwithRSA" , privateKey , new ByteList (digestInfoBytes , false ));
727728 return RubyString .newString (runtime , signed );
728729 } catch (IOException e ) {
729- throw newPKeyError (runtime , "failed to encode DigestInfo: " + e .getMessage ());
730+ throw newRSAError (runtime , "failed to encode DigestInfo: " + e .getMessage ());
730731 } catch (NoSuchAlgorithmException e ) {
731- throw newPKeyError (runtime , "unsupported algorithm: NONEwithRSA" );
732+ throw newRSAError (runtime , "unsupported algorithm: NONEwithRSA" );
732733 } catch (InvalidKeyException e ) {
733- throw newPKeyError (runtime , "invalid key" );
734+ throw newRSAError (runtime , "invalid key" );
734735 } catch (SignatureException e ) {
735- throw newPKeyError (runtime , e .getMessage ());
736+ throw newRSAError (runtime , e .getMessage ());
736737 }
737738 }
738739
@@ -765,11 +766,11 @@ public IRubyObject verify_raw(ThreadContext context, IRubyObject[] args) {
765766 new ByteList (sigBytes , false ));
766767 return runtime .newBoolean (ok );
767768 } catch (IOException e ) {
768- throw newPKeyError (runtime , "failed to encode DigestInfo: " + e .getMessage ());
769+ throw newRSAError (runtime , "failed to encode DigestInfo: " + e .getMessage ());
769770 } catch (NoSuchAlgorithmException e ) {
770- throw newPKeyError (runtime , "unsupported algorithm: NONEwithRSA" );
771+ throw newRSAError (runtime , "unsupported algorithm: NONEwithRSA" );
771772 } catch (InvalidKeyException e ) {
772- throw newPKeyError (runtime , "invalid key" );
773+ throw newRSAError (runtime , "invalid key" );
773774 } catch (SignatureException e ) {
774775 return runtime .getFalse ();
775776 }
@@ -819,7 +820,7 @@ public IRubyObject sign(ThreadContext context, IRubyObject[] args) {
819820 if (!(opts instanceof RubyHash )) throw runtime .newTypeError ("expected Hash" );
820821 String paddingMode = Utils .extractStringOpt (context , opts , "rsa_padding_mode" , true );
821822 if ("pss" .equalsIgnoreCase (paddingMode )) {
822- if (privateKey == null ) throw newPKeyError (runtime , "Private RSA key needed!" );
823+ if (privateKey == null ) throw newRSAError (runtime , "Private RSA key needed!" );
823824 final String digestAlg = getDigestAlgName (digest );
824825 int saltLen = Utils .extractIntOpt (context , opts , "rsa_pss_saltlen" , -1 , true );
825826 String mgf1Alg = Utils .extractStringOpt (context , opts , "rsa_mgf1_md" , true );
@@ -830,7 +831,7 @@ public IRubyObject sign(ThreadContext context, IRubyObject[] args) {
830831 try {
831832 signedData = signDataWithPSS (runtime , data .convertToString (), digestAlg , mgf1Alg , saltLen );
832833 } catch (IllegalArgumentException | DataLengthException | CryptoException e ) {
833- throw (RaiseException ) newPKeyError (runtime , e .getMessage ()).initCause (e );
834+ throw (RaiseException ) newRSAError (runtime , e .getMessage ()).initCause (e );
834835 }
835836 return StringHelper .newString (runtime , signedData );
836837 }
@@ -843,7 +844,7 @@ public IRubyObject sign(ThreadContext context, IRubyObject[] args) {
843844 @ JRubyMethod (name = "sign_pss" , required = 2 , optional = 1 )
844845 public IRubyObject sign_pss (ThreadContext context , IRubyObject [] args ) {
845846 final Ruby runtime = context .runtime ;
846- if (privateKey == null ) throw newPKeyError (runtime , "Private RSA key needed!" );
847+ if (privateKey == null ) throw newRSAError (runtime , "Private RSA key needed!" );
847848 final String digestAlg = getDigestAlgName (args [0 ]);
848849 final IRubyObject opts = args .length > 2 ? args [2 ] : context .nil ;
849850 final int maxSalt = maxPSSSaltLength (digestAlg , privateKey .getModulus ().bitLength ());
@@ -869,7 +870,7 @@ public IRubyObject sign_pss(ThreadContext context, IRubyObject[] args) {
869870 try {
870871 signedData = signDataWithPSS (runtime , args [1 ].convertToString (), digestAlg , mgf1Alg , saltLen );
871872 } catch (IllegalArgumentException | DataLengthException | CryptoException e ) {
872- throw (RaiseException ) newPKeyError (runtime , e .getMessage ()).initCause (e );
873+ throw (RaiseException ) newRSAError (runtime , e .getMessage ()).initCause (e );
873874 }
874875 return StringHelper .newString (runtime , signedData );
875876 }
0 commit comments