2323import javax .crypto .Cipher ;
2424import javax .crypto .KeyAgreement ;
2525
26- import org .bouncycastle .asn1 .ASN1Encodable ;
26+ import org .bouncycastle .asn1 .ASN1Encoding ;
2727import org .bouncycastle .asn1 .ASN1Integer ;
2828import org .bouncycastle .asn1 .ASN1ObjectIdentifier ;
29- import org .bouncycastle .asn1 .ASN1Primitive ;
3029import org .bouncycastle .asn1 .ASN1Sequence ;
3130import org .bouncycastle .asn1 .DERNull ;
32- import org .bouncycastle .asn1 .DERSequence ;
3331import org .bouncycastle .asn1 .cryptopro .CryptoProObjectIdentifiers ;
3432import org .bouncycastle .asn1 .kisa .KISAObjectIdentifiers ;
3533import org .bouncycastle .asn1 .nist .NISTObjectIdentifiers ;
4846import org .bouncycastle .jcajce .util .MessageDigestUtils ;
4947import org .bouncycastle .operator .DefaultSignatureNameFinder ;
5048import org .bouncycastle .operator .OperatorCreationException ;
49+ import org .bouncycastle .util .Arrays ;
5150import org .bouncycastle .util .Integers ;
5251
5352class OperatorHelper
@@ -57,13 +56,11 @@ class OperatorHelper
5756 private static final Map symmetricWrapperAlgNames = new HashMap ();
5857 private static final Map symmetricKeyAlgNames = new HashMap ();
5958 private static final Map symmetricWrapperKeySizes = new HashMap ();
59+ // ASN1ObjectIdentifier -> OAEPParamsValue
60+ private static final Map oaepParamsMap = new HashMap ();
6061
6162 private static DefaultSignatureNameFinder sigFinder = new DefaultSignatureNameFinder ();
6263
63- private static final RSAESOAEPparams oaepParams_sha256 = calculateDefForDigest (NISTObjectIdentifiers .id_sha256 );
64- private static final RSAESOAEPparams oaepParams_sha384 = calculateDefForDigest (NISTObjectIdentifiers .id_sha384 );
65- private static final RSAESOAEPparams oaepParams_sha512 = calculateDefForDigest (NISTObjectIdentifiers .id_sha512 );
66-
6764 static
6865 {
6966 oids .put (OIWObjectIdentifiers .idSHA1 , "SHA1" );
@@ -108,17 +105,12 @@ class OperatorHelper
108105 symmetricKeyAlgNames .put (NISTObjectIdentifiers .id_aes256_CBC , "AES" );
109106 symmetricKeyAlgNames .put (PKCSObjectIdentifiers .des_EDE3_CBC , "DESede" );
110107 symmetricKeyAlgNames .put (PKCSObjectIdentifiers .RC2_CBC , "RC2" );
111- }
112108
113- private static RSAESOAEPparams calculateDefForDigest (ASN1ObjectIdentifier digest )
114- {
115- AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier (
116- digest ,
117- DERNull .INSTANCE );
118- AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier (
119- PKCSObjectIdentifiers .id_mgf1 ,
120- new AlgorithmIdentifier (digest , DERNull .INSTANCE ));
121- return new RSAESOAEPparams (hashAlgorithm , maskGenAlgorithm , RSAESOAEPparams .DEFAULT_P_SOURCE_ALGORITHM );
109+ OAEPParamsValue .add (oaepParamsMap , "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" , OIWObjectIdentifiers .idSHA1 );
110+ OAEPParamsValue .add (oaepParamsMap , "RSA/ECB/OAEPWithSHA-224AndMGF1Padding" , NISTObjectIdentifiers .id_sha224 );
111+ OAEPParamsValue .add (oaepParamsMap , "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" , NISTObjectIdentifiers .id_sha256 );
112+ OAEPParamsValue .add (oaepParamsMap , "RSA/ECB/OAEPWithSHA-384AndMGF1Padding" , NISTObjectIdentifiers .id_sha384 );
113+ OAEPParamsValue .add (oaepParamsMap , "RSA/ECB/OAEPWithSHA-512AndMGF1Padding" , NISTObjectIdentifiers .id_sha512 );
122114 }
123115
124116 private JcaJceHelper helper ;
@@ -232,36 +224,25 @@ Cipher createAsymmetricWrapper(AlgorithmIdentifier algorithmID, Map extraAlgName
232224 {
233225 if (cipherName .indexOf ("OAEPPadding" ) > 0 )
234226 {
235- ASN1Encodable algParams = algorithmID .getParameters ();
236- if (algParams != null )
227+ try
237228 {
238- ASN1Primitive primitive = algParams . toASN1Primitive ( );
239- if (( primitive instanceof ASN1Sequence ) )
229+ RSAESOAEPparams oaepParams = RSAESOAEPparams . getInstance ( algorithmID . getParameters () );
230+ if (oaepParams != null )
240231 {
241- ASN1Sequence oaepParams = (ASN1Sequence )primitive ;
242- if (oaepParams .size () == 0 )
243- {
244- cipherName = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding" ;
245- }
246- else if (oaepParams .size () >= 2 )
232+ ASN1ObjectIdentifier digestOID = oaepParams .getHashAlgorithm ().getAlgorithm ();
233+ OAEPParamsValue oaepParamsValue = (OAEPParamsValue )oaepParamsMap .get (digestOID );
234+
235+ // Note that the original pSourceAlgorithm is ignored for this comparison
236+ if (oaepParamsValue != null && oaepParamsValue .matches (oaepParams .withDefaultPSource ()))
247237 {
248- // we only check the first 2 as pSource may be different
249- oaepParams = new DERSequence (new ASN1Encodable []{ oaepParams .getObjectAt (0 ), oaepParams .getObjectAt (1 ) });
250- if (oaepParams_sha256 .equals (oaepParams ))
251- {
252- cipherName = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" ;
253- }
254- else if (oaepParams_sha512 .equals (oaepParams ))
255- {
256- cipherName = "RSA/ECB/OAEPWithSHA-512AndMGF1Padding" ;
257- }
258- else if (oaepParams_sha384 .equals (oaepParams ))
259- {
260- cipherName = "RSA/ECB/OAEPWithSHA-384AndMGF1Padding" ;
261- }
238+ cipherName = oaepParamsValue .getCipherName ();
262239 }
263240 }
264241 }
242+ catch (Exception e )
243+ {
244+ // Ignore
245+ }
265246 }
266247
267248 try
@@ -637,4 +618,52 @@ private boolean notDefaultPSSParams(ASN1Sequence seq)
637618
638619 return pssParams .getSaltLength ().intValue () != digest .getDigestLength ();
639620 }
621+
622+ private static class OAEPParamsValue
623+ {
624+ static void add (Map oaepParamsMap , String cipherName , ASN1ObjectIdentifier digestOID )
625+ {
626+ try
627+ {
628+ RSAESOAEPparams oaepParams = createOAEPParams (digestOID );
629+ byte [] derEncoding = getDEREncoding (oaepParams );
630+ oaepParamsMap .put (digestOID , new OAEPParamsValue (cipherName , derEncoding ));
631+ }
632+ catch (Exception e )
633+ {
634+ throw new RuntimeException (e );
635+ }
636+ }
637+
638+ private String cipherName ;
639+ private byte [] derEncoding ;
640+
641+ private OAEPParamsValue (String cipherName , byte [] derEncoding )
642+ {
643+ this .cipherName = cipherName ;
644+ this .derEncoding = derEncoding ;
645+ }
646+
647+ String getCipherName ()
648+ {
649+ return cipherName ;
650+ }
651+
652+ boolean matches (RSAESOAEPparams oaepParams ) throws IOException
653+ {
654+ return Arrays .areEqual (derEncoding , getDEREncoding (oaepParams ));
655+ }
656+
657+ private static RSAESOAEPparams createOAEPParams (ASN1ObjectIdentifier digestOID )
658+ {
659+ AlgorithmIdentifier hashAlgorithm = new AlgorithmIdentifier (digestOID , DERNull .INSTANCE );
660+ AlgorithmIdentifier maskGenAlgorithm = new AlgorithmIdentifier (PKCSObjectIdentifiers .id_mgf1 , hashAlgorithm );
661+ return new RSAESOAEPparams (hashAlgorithm , maskGenAlgorithm , RSAESOAEPparams .DEFAULT_P_SOURCE_ALGORITHM );
662+ }
663+
664+ private static byte [] getDEREncoding (RSAESOAEPparams oaepParams ) throws IOException
665+ {
666+ return oaepParams .getEncoded (ASN1Encoding .DER );
667+ }
668+ }
640669}
0 commit comments