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