1package org.bouncycastle.cms;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7// BEGIN android-removed
8// import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
9// END android-removed
10import org.bouncycastle.asn1.eac.EACObjectIdentifiers;
11import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
12import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
13import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
14import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
15import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
16import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
17import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
18
19public class DefaultCMSSignatureAlgorithmNameGenerator
20    implements CMSSignatureAlgorithmNameGenerator
21{
22    private final Map encryptionAlgs = new HashMap();
23    private final Map     digestAlgs = new HashMap();
24
25    private void addEntries(ASN1ObjectIdentifier alias, String digest, String encryption)
26    {
27        digestAlgs.put(alias, digest);
28        encryptionAlgs.put(alias, encryption);
29    }
30
31    public DefaultCMSSignatureAlgorithmNameGenerator()
32    {
33        addEntries(NISTObjectIdentifiers.dsa_with_sha224, "SHA224", "DSA");
34        addEntries(NISTObjectIdentifiers.dsa_with_sha256, "SHA256", "DSA");
35        addEntries(NISTObjectIdentifiers.dsa_with_sha384, "SHA384", "DSA");
36        addEntries(NISTObjectIdentifiers.dsa_with_sha512, "SHA512", "DSA");
37        addEntries(OIWObjectIdentifiers.dsaWithSHA1, "SHA1", "DSA");
38        // BEGIN android-removed
39        // addEntries(OIWObjectIdentifiers.md4WithRSA, "MD4", "RSA");
40        // addEntries(OIWObjectIdentifiers.md4WithRSAEncryption, "MD4", "RSA");
41        // END android-removed
42        addEntries(OIWObjectIdentifiers.md5WithRSA, "MD5", "RSA");
43        addEntries(OIWObjectIdentifiers.sha1WithRSA, "SHA1", "RSA");
44        // BEGIN android-removed
45        // addEntries(PKCSObjectIdentifiers.md2WithRSAEncryption, "MD2", "RSA");
46        // addEntries(PKCSObjectIdentifiers.md4WithRSAEncryption, "MD4", "RSA");
47        // END android-removed
48        addEntries(PKCSObjectIdentifiers.md5WithRSAEncryption, "MD5", "RSA");
49        addEntries(PKCSObjectIdentifiers.sha1WithRSAEncryption, "SHA1", "RSA");
50        addEntries(PKCSObjectIdentifiers.sha224WithRSAEncryption, "SHA224", "RSA");
51        addEntries(PKCSObjectIdentifiers.sha256WithRSAEncryption, "SHA256", "RSA");
52        addEntries(PKCSObjectIdentifiers.sha384WithRSAEncryption, "SHA384", "RSA");
53        addEntries(PKCSObjectIdentifiers.sha512WithRSAEncryption, "SHA512", "RSA");
54        addEntries(X9ObjectIdentifiers.ecdsa_with_SHA1, "SHA1", "ECDSA");
55        addEntries(X9ObjectIdentifiers.ecdsa_with_SHA224, "SHA224", "ECDSA");
56        addEntries(X9ObjectIdentifiers.ecdsa_with_SHA256, "SHA256", "ECDSA");
57        addEntries(X9ObjectIdentifiers.ecdsa_with_SHA384, "SHA384", "ECDSA");
58        addEntries(X9ObjectIdentifiers.ecdsa_with_SHA512, "SHA512", "ECDSA");
59        addEntries(X9ObjectIdentifiers.id_dsa_with_sha1, "SHA1", "DSA");
60        addEntries(EACObjectIdentifiers.id_TA_ECDSA_SHA_1, "SHA1", "ECDSA");
61        addEntries(EACObjectIdentifiers.id_TA_ECDSA_SHA_224, "SHA224", "ECDSA");
62        addEntries(EACObjectIdentifiers.id_TA_ECDSA_SHA_256, "SHA256", "ECDSA");
63        addEntries(EACObjectIdentifiers.id_TA_ECDSA_SHA_384, "SHA384", "ECDSA");
64        addEntries(EACObjectIdentifiers.id_TA_ECDSA_SHA_512, "SHA512", "ECDSA");
65        addEntries(EACObjectIdentifiers.id_TA_RSA_v1_5_SHA_1, "SHA1", "RSA");
66        addEntries(EACObjectIdentifiers.id_TA_RSA_v1_5_SHA_256, "SHA256", "RSA");
67        addEntries(EACObjectIdentifiers.id_TA_RSA_PSS_SHA_1, "SHA1", "RSAandMGF1");
68        addEntries(EACObjectIdentifiers.id_TA_RSA_PSS_SHA_256, "SHA256", "RSAandMGF1");
69
70        encryptionAlgs.put(X9ObjectIdentifiers.id_dsa, "DSA");
71        encryptionAlgs.put(PKCSObjectIdentifiers.rsaEncryption, "RSA");
72        encryptionAlgs.put(TeleTrusTObjectIdentifiers.teleTrusTRSAsignatureAlgorithm, "RSA");
73        encryptionAlgs.put(X509ObjectIdentifiers.id_ea_rsa, "RSA");
74        encryptionAlgs.put(PKCSObjectIdentifiers.id_RSASSA_PSS, "RSAandMGF1");
75        // BEGIN android-removed
76        // encryptionAlgs.put(CryptoProObjectIdentifiers.gostR3410_94, "GOST3410");
77        // encryptionAlgs.put(CryptoProObjectIdentifiers.gostR3410_2001, "ECGOST3410");
78        // encryptionAlgs.put(new ASN1ObjectIdentifier("1.3.6.1.4.1.5849.1.6.2"), "ECGOST3410");
79        // encryptionAlgs.put(new ASN1ObjectIdentifier("1.3.6.1.4.1.5849.1.1.5"), "GOST3410");
80        // encryptionAlgs.put(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001, "ECGOST3410");
81        // encryptionAlgs.put(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94, "GOST3410");
82        //
83        // digestAlgs.put(PKCSObjectIdentifiers.md2, "MD2");
84        // digestAlgs.put(PKCSObjectIdentifiers.md4, "MD4");
85        // END android-removed
86        digestAlgs.put(PKCSObjectIdentifiers.md5, "MD5");
87        digestAlgs.put(OIWObjectIdentifiers.idSHA1, "SHA1");
88        digestAlgs.put(NISTObjectIdentifiers.id_sha224, "SHA224");
89        digestAlgs.put(NISTObjectIdentifiers.id_sha256, "SHA256");
90        digestAlgs.put(NISTObjectIdentifiers.id_sha384, "SHA384");
91        digestAlgs.put(NISTObjectIdentifiers.id_sha512, "SHA512");
92        // BEGIN android-removed
93        // digestAlgs.put(TeleTrusTObjectIdentifiers.ripemd128, "RIPEMD128");
94        // digestAlgs.put(TeleTrusTObjectIdentifiers.ripemd160, "RIPEMD160");
95        // digestAlgs.put(TeleTrusTObjectIdentifiers.ripemd256, "RIPEMD256");
96        // digestAlgs.put(CryptoProObjectIdentifiers.gostR3411,  "GOST3411");
97        // digestAlgs.put(new ASN1ObjectIdentifier("1.3.6.1.4.1.5849.1.2.1"),  "GOST3411");
98        // END android-removed
99    }
100
101    /**
102     * Return the digest algorithm using one of the standard JCA string
103     * representations rather than the algorithm identifier (if possible).
104     */
105    private String getDigestAlgName(
106        ASN1ObjectIdentifier digestAlgOID)
107    {
108        String algName = (String)digestAlgs.get(digestAlgOID);
109
110        if (algName != null)
111        {
112            return algName;
113        }
114
115        return digestAlgOID.getId();
116    }
117
118    /**
119     * Return the digest encryption algorithm using one of the standard
120     * JCA string representations rather the the algorithm identifier (if
121     * possible).
122     */
123    private String getEncryptionAlgName(
124        ASN1ObjectIdentifier encryptionAlgOID)
125    {
126        String algName = (String)encryptionAlgs.get(encryptionAlgOID);
127
128        if (algName != null)
129        {
130            return algName;
131        }
132
133        return encryptionAlgOID.getId();
134    }
135
136    /**
137     * Set the mapping for the encryption algorithm used in association with a SignedData generation
138     * or interpretation.
139     *
140     * @param oid object identifier to map.
141     * @param algorithmName algorithm name to use.
142     */
143    protected void setSigningEncryptionAlgorithmMapping(ASN1ObjectIdentifier oid, String algorithmName)
144    {
145        encryptionAlgs.put(oid, algorithmName);
146    }
147
148    /**
149     * Set the mapping for the digest algorithm to use in conjunction with a SignedData generation
150     * or interpretation.
151     *
152     * @param oid object identifier to map.
153     * @param algorithmName algorithm name to use.
154     */
155    protected void setSigningDigestAlgorithmMapping(ASN1ObjectIdentifier oid, String algorithmName)
156    {
157        digestAlgs.put(oid, algorithmName);
158    }
159
160    public String getSignatureName(AlgorithmIdentifier digestAlg, AlgorithmIdentifier encryptionAlg)
161    {
162        return getDigestAlgName(digestAlg.getAlgorithm()) + "with" + getEncryptionAlgName(encryptionAlg.getAlgorithm());
163    }
164}
165