2323import javax .crypto .Cipher ;
2424import javax .crypto .KeyAgreement ;
2525
26+ import org .bouncycastle .asn1 .ASN1Encodable ;
2627import org .bouncycastle .asn1 .ASN1Integer ;
2728import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
2829import org .bouncycastle .asn1 .ASN1Sequence ;
30+ import org .bouncycastle .asn1 .DERNull ;
31+ import org .bouncycastle .asn1 .DERSequence ;
2932import org .bouncycastle .asn1 .cryptopro .CryptoProObjectIdentifiers ;
3033import org .bouncycastle .asn1 .kisa .KISAObjectIdentifiers ;
3134import org .bouncycastle .asn1 .nist .NISTObjectIdentifiers ;
3235import org .bouncycastle .asn1 .ntt .NTTObjectIdentifiers ;
3336import org .bouncycastle .asn1 .oiw .OIWObjectIdentifiers ;
3437import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
38+ import org .bouncycastle .asn1 .pkcs .RSAESOAEPparams ;
3539import org .bouncycastle .asn1 .pkcs .RSASSAPSSparams ;
3640import org .bouncycastle .asn1 .teletrust .TeleTrusTObjectIdentifiers ;
3741import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
@@ -55,6 +59,10 @@ class OperatorHelper
5559
5660 private static DefaultSignatureNameFinder sigFinder = new DefaultSignatureNameFinder ();
5761
62+ private static final RSAESOAEPparams oaepParams_sha256 = calculateDefForDigest (NISTObjectIdentifiers .id_sha256 );
63+ private static final RSAESOAEPparams oaepParams_sha384 = calculateDefForDigest (NISTObjectIdentifiers .id_sha384 );
64+ private static final RSAESOAEPparams oaepParams_sha512 = calculateDefForDigest (NISTObjectIdentifiers .id_sha512 );
65+
5866 static
5967 {
6068 oids .put (OIWObjectIdentifiers .idSHA1 , "SHA1" );
@@ -101,6 +109,17 @@ class OperatorHelper
101109 symmetricKeyAlgNames .put (PKCSObjectIdentifiers .RC2_CBC , "RC2" );
102110 }
103111
112+ private static RSAESOAEPparams calculateDefForDigest (ASN1ObjectIdentifier digest )
113+ {
114+ AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier (
115+ digest ,
116+ DERNull .INSTANCE );
117+ AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier (
118+ PKCSObjectIdentifiers .id_mgf1 ,
119+ new AlgorithmIdentifier (digest , DERNull .INSTANCE ));
120+ return new RSAESOAEPparams (hashAlgorithm , maskGenAlgorithm , RSAESOAEPparams .DEFAULT_P_SOURCE_ALGORITHM );
121+ }
122+
104123 private JcaJceHelper helper ;
105124
106125 OperatorHelper (JcaJceHelper helper )
@@ -185,9 +204,10 @@ KeyAgreement createKeyAgreement(ASN1ObjectIdentifier algorithm)
185204 }
186205 }
187206
188- Cipher createAsymmetricWrapper (ASN1ObjectIdentifier algorithm , Map extraAlgNames )
207+ Cipher createAsymmetricWrapper (AlgorithmIdentifier algorithmID , Map extraAlgNames )
189208 throws OperatorCreationException
190209 {
210+ ASN1ObjectIdentifier algorithm = algorithmID .getAlgorithm ();
191211 try
192212 {
193213 String cipherName = null ;
@@ -200,6 +220,35 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
200220 if (cipherName == null )
201221 {
202222 cipherName = (String )asymmetricWrapperAlgNames .get (algorithm );
223+ if (cipherName .indexOf ("OAEPPadding" ) > 0 )
224+ {
225+ ASN1Encodable params = algorithmID .getParameters ().toASN1Primitive ();
226+ if ((params instanceof ASN1Sequence ))
227+ {
228+ ASN1Sequence paramSeq = ASN1Sequence .getInstance (params );
229+ if (paramSeq .size () == 0 )
230+ {
231+ cipherName = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ;
232+ }
233+ else if (paramSeq .size () >= 2 )
234+ {
235+ // we only check the first 2 as pSource may be different
236+ paramSeq = new DERSequence (new ASN1Encodable []{ paramSeq .getObjectAt (0 ), paramSeq .getObjectAt (1 ) });
237+ if (oaepParams_sha256 .equals (paramSeq ))
238+ {
239+ cipherName = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ;
240+ }
241+ else if (oaepParams_sha512 .equals (paramSeq ))
242+ {
243+ cipherName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding" ;
244+ }
245+ else if (oaepParams_sha384 .equals (paramSeq ))
246+ {
247+ cipherName = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding" ;
248+ }
249+ }
250+ }
251+ }
203252 }
204253
205254 if (cipherName != null )
@@ -223,6 +272,18 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
223272 // Ignore
224273 }
225274 }
275+ else if (cipherName .indexOf ("ECB/OAEPWith" ) > 0 )
276+ {
277+ int start = cipherName .indexOf ("ECB" );
278+ try
279+ {
280+ return helper .createCipher (cipherName .substring (0 , start ) + "NONE" + cipherName .substring (start + 3 ));
281+ }
282+ catch (NoSuchAlgorithmException ex )
283+ {
284+ // Ignore
285+ }
286+ }
226287 // Ignore
227288 }
228289 }
0 commit comments