001: package org.bouncycastle.ocsp;
002:
003: import org.bouncycastle.asn1.DERNull;
004: import org.bouncycastle.asn1.DERObjectIdentifier;
005: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
006: import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
007: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
008: import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
009: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
010: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
011: import org.bouncycastle.util.Strings;
012:
013: import java.util.ArrayList;
014: import java.util.Enumeration;
015: import java.util.HashSet;
016: import java.util.Hashtable;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Set;
020:
021: class OCSPUtil {
022: private static Hashtable algorithms = new Hashtable();
023: private static Hashtable oids = new Hashtable();
024: private static Set noParams = new HashSet();
025:
026: static {
027: algorithms.put("MD2WITHRSAENCRYPTION",
028: PKCSObjectIdentifiers.md2WithRSAEncryption);
029: algorithms.put("MD2WITHRSA",
030: PKCSObjectIdentifiers.md2WithRSAEncryption);
031: algorithms.put("MD5WITHRSAENCRYPTION",
032: PKCSObjectIdentifiers.md5WithRSAEncryption);
033: algorithms.put("MD5WITHRSA",
034: PKCSObjectIdentifiers.md5WithRSAEncryption);
035: algorithms.put("SHA1WITHRSAENCRYPTION",
036: PKCSObjectIdentifiers.sha1WithRSAEncryption);
037: algorithms.put("SHA1WITHRSA",
038: PKCSObjectIdentifiers.sha1WithRSAEncryption);
039: algorithms.put("SHA224WITHRSAENCRYPTION",
040: PKCSObjectIdentifiers.sha224WithRSAEncryption);
041: algorithms.put("SHA224WITHRSA",
042: PKCSObjectIdentifiers.sha224WithRSAEncryption);
043: algorithms.put("SHA256WITHRSAENCRYPTION",
044: PKCSObjectIdentifiers.sha256WithRSAEncryption);
045: algorithms.put("SHA256WITHRSA",
046: PKCSObjectIdentifiers.sha256WithRSAEncryption);
047: algorithms.put("SHA384WITHRSAENCRYPTION",
048: PKCSObjectIdentifiers.sha384WithRSAEncryption);
049: algorithms.put("SHA384WITHRSA",
050: PKCSObjectIdentifiers.sha384WithRSAEncryption);
051: algorithms.put("SHA512WITHRSAENCRYPTION",
052: PKCSObjectIdentifiers.sha512WithRSAEncryption);
053: algorithms.put("SHA512WITHRSA",
054: PKCSObjectIdentifiers.sha512WithRSAEncryption);
055: algorithms.put("RIPEMD160WITHRSAENCRYPTION",
056: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160);
057: algorithms.put("RIPEMD160WITHRSA",
058: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160);
059: algorithms.put("RIPEMD128WITHRSAENCRYPTION",
060: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128);
061: algorithms.put("RIPEMD128WITHRSA",
062: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128);
063: algorithms.put("RIPEMD256WITHRSAENCRYPTION",
064: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256);
065: algorithms.put("RIPEMD256WITHRSA",
066: TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256);
067: algorithms.put("SHA1WITHDSA",
068: X9ObjectIdentifiers.id_dsa_with_sha1);
069: algorithms.put("DSAWITHSHA1",
070: X9ObjectIdentifiers.id_dsa_with_sha1);
071: algorithms.put("SHA224WITHDSA",
072: NISTObjectIdentifiers.dsa_with_sha224);
073: algorithms.put("SHA256WITHDSA",
074: NISTObjectIdentifiers.dsa_with_sha256);
075: algorithms.put("SHA1WITHECDSA",
076: X9ObjectIdentifiers.ecdsa_with_SHA1);
077: algorithms.put("ECDSAWITHSHA1",
078: X9ObjectIdentifiers.ecdsa_with_SHA1);
079: algorithms.put("SHA224WITHECDSA",
080: X9ObjectIdentifiers.ecdsa_with_SHA224);
081: algorithms.put("SHA256WITHECDSA",
082: X9ObjectIdentifiers.ecdsa_with_SHA256);
083: algorithms.put("SHA384WITHECDSA",
084: X9ObjectIdentifiers.ecdsa_with_SHA384);
085: algorithms.put("SHA512WITHECDSA",
086: X9ObjectIdentifiers.ecdsa_with_SHA512);
087: algorithms
088: .put(
089: "GOST3411WITHGOST3410",
090: CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94);
091: algorithms
092: .put(
093: "GOST3411WITHGOST3410-94",
094: CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94);
095:
096: oids.put(PKCSObjectIdentifiers.md2WithRSAEncryption,
097: "MD2WITHRSA");
098: oids.put(PKCSObjectIdentifiers.md5WithRSAEncryption,
099: "MD5WITHRSA");
100: oids.put(PKCSObjectIdentifiers.sha1WithRSAEncryption,
101: "SHA1WITHRSA");
102: oids.put(PKCSObjectIdentifiers.sha224WithRSAEncryption,
103: "SHA224WITHRSA");
104: oids.put(PKCSObjectIdentifiers.sha256WithRSAEncryption,
105: "SHA256WITHRSA");
106: oids.put(PKCSObjectIdentifiers.sha384WithRSAEncryption,
107: "SHA384WITHRSA");
108: oids.put(PKCSObjectIdentifiers.sha512WithRSAEncryption,
109: "SHA512WITHRSA");
110: oids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160,
111: "RIPEMD160WITHRSA");
112: oids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128,
113: "RIPEMD128WITHRSA");
114: oids.put(TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256,
115: "RIPEMD256WITHRSA");
116: oids.put(X9ObjectIdentifiers.id_dsa_with_sha1, "SHA1WITHDSA");
117: oids
118: .put(NISTObjectIdentifiers.dsa_with_sha224,
119: "SHA224WITHDSA");
120: oids
121: .put(NISTObjectIdentifiers.dsa_with_sha256,
122: "SHA256WITHDSA");
123: oids.put(X9ObjectIdentifiers.ecdsa_with_SHA1, "SHA1WITHECDSA");
124: oids.put(X9ObjectIdentifiers.ecdsa_with_SHA224,
125: "SHA224WITHECDSA");
126: oids.put(X9ObjectIdentifiers.ecdsa_with_SHA256,
127: "SHA256WITHECDSA");
128: oids.put(X9ObjectIdentifiers.ecdsa_with_SHA384,
129: "SHA384WITHECDSA");
130: oids.put(X9ObjectIdentifiers.ecdsa_with_SHA512,
131: "SHA512WITHECDSA");
132: oids
133: .put(
134: CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94,
135: "GOST3411WITHGOST3410");
136:
137: //
138: // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
139: // The parameters field SHALL be NULL for RSA based signature algorithms.
140: //
141: noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA1);
142: noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA224);
143: noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA256);
144: noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA384);
145: noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA512);
146: noParams.add(X9ObjectIdentifiers.id_dsa_with_sha1);
147: noParams.add(NISTObjectIdentifiers.dsa_with_sha224);
148: noParams.add(NISTObjectIdentifiers.dsa_with_sha256);
149: }
150:
151: static DERObjectIdentifier getAlgorithmOID(String algorithmName) {
152: algorithmName = Strings.toUpperCase(algorithmName);
153:
154: if (algorithms.containsKey(algorithmName)) {
155: return (DERObjectIdentifier) algorithms.get(algorithmName);
156: }
157:
158: return new DERObjectIdentifier(algorithmName);
159: }
160:
161: static String getAlgorithmName(DERObjectIdentifier oid) {
162: if (oids.containsKey(oid)) {
163: return (String) oids.get(oid);
164: }
165:
166: return oid.getId();
167: }
168:
169: static AlgorithmIdentifier getSigAlgID(DERObjectIdentifier sigOid) {
170: if (noParams.contains(sigOid)) {
171: return new AlgorithmIdentifier(sigOid);
172: } else {
173: return new AlgorithmIdentifier(sigOid, new DERNull());
174: }
175: }
176:
177: static Iterator getAlgNames() {
178: Enumeration e = algorithms.keys();
179: List l = new ArrayList();
180:
181: while (e.hasMoreElements()) {
182: l.add(e.nextElement());
183: }
184:
185: return l.iterator();
186: }
187: }
|