1package org.bouncycastle.operator;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7import org.bouncycastle.asn1.DERNull;
8// BEGIN android-removed
9// import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
10// END android-removed
11import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
12import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
13import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
14import org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
15import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
16import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
17import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
18
19public class DefaultDigestAlgorithmIdentifierFinder
20    implements DigestAlgorithmIdentifierFinder
21{
22    private static Map digestOids = new HashMap();
23    private static Map digestNameToOids = new HashMap();
24
25    static
26    {
27        //
28        // digests
29        //
30        // BEGIN android-removed
31        // digestOids.put(OIWObjectIdentifiers.md4WithRSAEncryption, PKCSObjectIdentifiers.md4);
32        // digestOids.put(OIWObjectIdentifiers.md4WithRSA, PKCSObjectIdentifiers.md4);
33        // END android-removed
34        digestOids.put(OIWObjectIdentifiers.sha1WithRSA, OIWObjectIdentifiers.idSHA1);
35
36        digestOids.put(PKCSObjectIdentifiers.sha224WithRSAEncryption, NISTObjectIdentifiers.id_sha224);
37        digestOids.put(PKCSObjectIdentifiers.sha256WithRSAEncryption, NISTObjectIdentifiers.id_sha256);
38        digestOids.put(PKCSObjectIdentifiers.sha384WithRSAEncryption, NISTObjectIdentifiers.id_sha384);
39        digestOids.put(PKCSObjectIdentifiers.sha512WithRSAEncryption, NISTObjectIdentifiers.id_sha512);
40        // BEGIN android-removed
41        // digestOids.put(PKCSObjectIdentifiers.md2WithRSAEncryption, PKCSObjectIdentifiers.md2);
42        // digestOids.put(PKCSObjectIdentifiers.md4WithRSAEncryption, PKCSObjectIdentifiers.md4);
43        // END android-removed
44        digestOids.put(PKCSObjectIdentifiers.md5WithRSAEncryption, PKCSObjectIdentifiers.md5);
45        digestOids.put(PKCSObjectIdentifiers.sha1WithRSAEncryption, OIWObjectIdentifiers.idSHA1);
46
47        digestOids.put(X9ObjectIdentifiers.ecdsa_with_SHA1, OIWObjectIdentifiers.idSHA1);
48        digestOids.put(X9ObjectIdentifiers.ecdsa_with_SHA224, NISTObjectIdentifiers.id_sha224);
49        digestOids.put(X9ObjectIdentifiers.ecdsa_with_SHA256, NISTObjectIdentifiers.id_sha256);
50        digestOids.put(X9ObjectIdentifiers.ecdsa_with_SHA384, NISTObjectIdentifiers.id_sha384);
51        digestOids.put(X9ObjectIdentifiers.ecdsa_with_SHA512, NISTObjectIdentifiers.id_sha512);
52        digestOids.put(X9ObjectIdentifiers.id_dsa_with_sha1, OIWObjectIdentifiers.idSHA1);
53
54        digestOids.put(NISTObjectIdentifiers.dsa_with_sha224, NISTObjectIdentifiers.id_sha224);
55        digestOids.put(NISTObjectIdentifiers.dsa_with_sha256, NISTObjectIdentifiers.id_sha256);
56        digestOids.put(NISTObjectIdentifiers.dsa_with_sha384, NISTObjectIdentifiers.id_sha384);
57        digestOids.put(NISTObjectIdentifiers.dsa_with_sha512, NISTObjectIdentifiers.id_sha512);
58
59        // BEGIN android-removed
60        // digestOids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128, TeleTrusTObjectIdentifiers.ripemd128);
61        // digestOids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160, TeleTrusTObjectIdentifiers.ripemd160);
62        // digestOids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256, TeleTrusTObjectIdentifiers.ripemd256);
63        //
64        // digestOids.put(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94, CryptoProObjectIdentifiers.gostR3411);
65        // digestOids.put(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001, CryptoProObjectIdentifiers.gostR3411);
66        // END android-removed
67
68        digestNameToOids.put("SHA-1", OIWObjectIdentifiers.idSHA1);
69        digestNameToOids.put("SHA-224", NISTObjectIdentifiers.id_sha224);
70        digestNameToOids.put("SHA-256", NISTObjectIdentifiers.id_sha256);
71        digestNameToOids.put("SHA-384", NISTObjectIdentifiers.id_sha384);
72        digestNameToOids.put("SHA-512", NISTObjectIdentifiers.id_sha512);
73
74        // BEGIN android-removed
75        // digestNameToOids.put("GOST3411", CryptoProObjectIdentifiers.gostR3411);
76        //
77        // digestNameToOids.put("MD2", PKCSObjectIdentifiers.md2);
78        // digestNameToOids.put("MD4", PKCSObjectIdentifiers.md4);
79        // END android-removed
80        digestNameToOids.put("MD5", PKCSObjectIdentifiers.md5);
81
82        // BEGIN android-removed
83        // digestNameToOids.put("RIPEMD128", TeleTrusTObjectIdentifiers.ripemd128);
84        // digestNameToOids.put("RIPEMD160", TeleTrusTObjectIdentifiers.ripemd160);
85        // digestNameToOids.put("RIPEMD256", TeleTrusTObjectIdentifiers.ripemd256);
86        // END android-removed
87    }
88
89    public AlgorithmIdentifier find(AlgorithmIdentifier sigAlgId)
90    {
91        AlgorithmIdentifier digAlgId;
92
93        if (sigAlgId.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS))
94        {
95            digAlgId = RSASSAPSSparams.getInstance(sigAlgId.getParameters()).getHashAlgorithm();
96        }
97        else
98        {
99            digAlgId = new AlgorithmIdentifier((ASN1ObjectIdentifier)digestOids.get(sigAlgId.getAlgorithm()), DERNull.INSTANCE);
100        }
101
102        return digAlgId;
103    }
104
105    public AlgorithmIdentifier find(String digAlgName)
106    {
107        return new AlgorithmIdentifier((ASN1ObjectIdentifier)digestNameToOids.get(digAlgName), DERNull.INSTANCE);
108    }
109}