23
23
import javax .crypto .Cipher ;
24
24
import javax .crypto .KeyAgreement ;
25
25
26
+ import org .bouncycastle .asn1 .ASN1Encodable ;
26
27
import org .bouncycastle .asn1 .ASN1Integer ;
27
28
import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
28
29
import org .bouncycastle .asn1 .ASN1Sequence ;
30
+ import org .bouncycastle .asn1 .DERNull ;
31
+ import org .bouncycastle .asn1 .DERSequence ;
29
32
import org .bouncycastle .asn1 .cryptopro .CryptoProObjectIdentifiers ;
30
33
import org .bouncycastle .asn1 .kisa .KISAObjectIdentifiers ;
31
34
import org .bouncycastle .asn1 .nist .NISTObjectIdentifiers ;
32
35
import org .bouncycastle .asn1 .ntt .NTTObjectIdentifiers ;
33
36
import org .bouncycastle .asn1 .oiw .OIWObjectIdentifiers ;
34
37
import org .bouncycastle .asn1 .pkcs .PKCSObjectIdentifiers ;
38
+ import org .bouncycastle .asn1 .pkcs .RSAESOAEPparams ;
35
39
import org .bouncycastle .asn1 .pkcs .RSASSAPSSparams ;
36
40
import org .bouncycastle .asn1 .teletrust .TeleTrusTObjectIdentifiers ;
37
41
import org .bouncycastle .asn1 .x509 .AlgorithmIdentifier ;
@@ -55,6 +59,10 @@ class OperatorHelper
55
59
56
60
private static DefaultSignatureNameFinder sigFinder = new DefaultSignatureNameFinder ();
57
61
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
+
58
66
static
59
67
{
60
68
oids .put (OIWObjectIdentifiers .idSHA1 , "SHA1" );
@@ -101,6 +109,17 @@ class OperatorHelper
101
109
symmetricKeyAlgNames .put (PKCSObjectIdentifiers .RC2_CBC , "RC2" );
102
110
}
103
111
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
+
104
123
private JcaJceHelper helper ;
105
124
106
125
OperatorHelper (JcaJceHelper helper )
@@ -185,9 +204,10 @@ KeyAgreement createKeyAgreement(ASN1ObjectIdentifier algorithm)
185
204
}
186
205
}
187
206
188
- Cipher createAsymmetricWrapper (ASN1ObjectIdentifier algorithm , Map extraAlgNames )
207
+ Cipher createAsymmetricWrapper (AlgorithmIdentifier algorithmID , Map extraAlgNames )
189
208
throws OperatorCreationException
190
209
{
210
+ ASN1ObjectIdentifier algorithm = algorithmID .getAlgorithm ();
191
211
try
192
212
{
193
213
String cipherName = null ;
@@ -200,6 +220,35 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
200
220
if (cipherName == null )
201
221
{
202
222
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
+ }
203
252
}
204
253
205
254
if (cipherName != null )
@@ -223,6 +272,18 @@ Cipher createAsymmetricWrapper(ASN1ObjectIdentifier algorithm, Map extraAlgNames
223
272
// Ignore
224
273
}
225
274
}
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
+ }
226
287
// Ignore
227
288
}
228
289
}
0 commit comments