1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.x509;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
3c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.io.IOException;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.InvalidKeyException;
5c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.NoSuchAlgorithmException;
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.NoSuchProviderException;
7c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.PrivateKey;
8c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.Provider;
9c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.SecureRandom;
10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.Security;
11c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.Signature;
12c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.SignatureException;
13c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.ArrayList;
14c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Enumeration;
15c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.HashSet;
16c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Hashtable;
17c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Iterator;
18c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.List;
19c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Set;
20c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
216e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport javax.security.auth.x500.X500Principal;
226e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
236e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.ASN1Encodable;
244c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Encoding;
254c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1Integer;
266e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.DERNull;
276e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.DERObjectIdentifier;
28e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom// BEGIN android-removed
29e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom// import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
30e6bf3e8dfa2804891a82075cb469b736321b4827Brian Carlstrom// END android-removed
316e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
326e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
336e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
346e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
354c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom// BEGIN android-removed
364c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom// import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
374c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom// END android-removed
386e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.x509.AlgorithmIdentifier;
396e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
406e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.jce.X509Principal;
416e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstromimport org.bouncycastle.util.Strings;
426e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamclass X509Util
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
45b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private static Hashtable algorithms = new Hashtable();
46c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static Hashtable params = new Hashtable();
47b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    private static Set       noParams = new HashSet();
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
49b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
51c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-removed
52c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // algorithms.put("MD2WITHRSAENCRYPTION", PKCSObjectIdentifiers.md2WithRSAEncryption);
53c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // algorithms.put("MD2WITHRSA", PKCSObjectIdentifiers.md2WithRSAEncryption);
54c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-removed
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("MD5WITHRSAENCRYPTION", PKCSObjectIdentifiers.md5WithRSAEncryption);
56c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("MD5WITHRSA", PKCSObjectIdentifiers.md5WithRSAEncryption);
57c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA1WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha1WithRSAEncryption);
58c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA1WITHRSA", PKCSObjectIdentifiers.sha1WithRSAEncryption);
596e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
606e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("SHA224WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha224WithRSAEncryption);
616e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("SHA224WITHRSA", PKCSObjectIdentifiers.sha224WithRSAEncryption);
626e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
63b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA256WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha256WithRSAEncryption);
64b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA256WITHRSA", PKCSObjectIdentifiers.sha256WithRSAEncryption);
65b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA384WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha384WithRSAEncryption);
66b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA384WITHRSA", PKCSObjectIdentifiers.sha384WithRSAEncryption);
67b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA512WITHRSAENCRYPTION", PKCSObjectIdentifiers.sha512WithRSAEncryption);
68b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithms.put("SHA512WITHRSA", PKCSObjectIdentifiers.sha512WithRSAEncryption);
69c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA1WITHRSAANDMGF1", PKCSObjectIdentifiers.id_RSASSA_PSS);
706e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
716e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("SHA224WITHRSAANDMGF1", PKCSObjectIdentifiers.id_RSASSA_PSS);
726e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA256WITHRSAANDMGF1", PKCSObjectIdentifiers.id_RSASSA_PSS);
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA384WITHRSAANDMGF1", PKCSObjectIdentifiers.id_RSASSA_PSS);
75c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithms.put("SHA512WITHRSAANDMGF1", PKCSObjectIdentifiers.id_RSASSA_PSS);
766e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
776e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD160WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160);
786e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD160WITHRSA", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd160);
796e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD128WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128);
806e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD128WITHRSA", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd128);
816e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD256WITHRSAENCRYPTION", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256);
826e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("RIPEMD256WITHRSA", TeleTrusTObjectIdentifiers.rsaSignatureWithripemd256);
836e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
8400c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA1WITHDSA", X9ObjectIdentifiers.id_dsa_with_sha1);
8500c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("DSAWITHSHA1", X9ObjectIdentifiers.id_dsa_with_sha1);
866e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
876e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("SHA224WITHDSA", NISTObjectIdentifiers.dsa_with_sha224);
886e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
8900c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA256WITHDSA", NISTObjectIdentifiers.dsa_with_sha256);
906e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        algorithms.put("SHA384WITHDSA", NISTObjectIdentifiers.dsa_with_sha384);
916e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        algorithms.put("SHA512WITHDSA", NISTObjectIdentifiers.dsa_with_sha512);
9200c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA1WITHECDSA", X9ObjectIdentifiers.ecdsa_with_SHA1);
9300c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("ECDSAWITHSHA1", X9ObjectIdentifiers.ecdsa_with_SHA1);
946e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
956e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("SHA224WITHECDSA", X9ObjectIdentifiers.ecdsa_with_SHA224);
966e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
9700c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA256WITHECDSA", X9ObjectIdentifiers.ecdsa_with_SHA256);
9800c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA384WITHECDSA", X9ObjectIdentifiers.ecdsa_with_SHA384);
9900c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        algorithms.put("SHA512WITHECDSA", X9ObjectIdentifiers.ecdsa_with_SHA512);
1006e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
1016e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("GOST3411WITHGOST3410", CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94);
1026e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("GOST3411WITHGOST3410-94", CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94);
1036e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("GOST3411WITHECGOST3410", CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001);
1046e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("GOST3411WITHECGOST3410-2001", CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001);
1056e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // algorithms.put("GOST3411WITHGOST3410-2001", CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001);
1066e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
107c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
108b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        //
109b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // According to RFC 3279, the ASN.1 encoding SHALL (id-dsa-with-sha1) or MUST (ecdsa-with-SHA*) omit the parameters field.
110b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        // The parameters field SHALL be NULL for RSA based signature algorithms.
111b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        //
11200c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA1);
1136e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
1146e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA224);
1156e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
11600c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA256);
11700c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA384);
11800c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(X9ObjectIdentifiers.ecdsa_with_SHA512);
11900c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(X9ObjectIdentifiers.id_dsa_with_sha1);
1206e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
1216e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // noParams.add(NISTObjectIdentifiers.dsa_with_sha224);
1226e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
12300c567c8177f6a571ee7e12443c84b3d37673e8cBrian Carlstrom        noParams.add(NISTObjectIdentifiers.dsa_with_sha256);
1246e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        noParams.add(NISTObjectIdentifiers.dsa_with_sha384);
1256e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        noParams.add(NISTObjectIdentifiers.dsa_with_sha512);
1266e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom
127c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
128c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // RFC 4491
129c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
1306e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
1316e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // noParams.add(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_94);
1326e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // noParams.add(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001);
1336e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
134c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
135c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
136c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // explicit params
137c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
138c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-changed
139c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AlgorithmIdentifier sha1AlgId = new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1, DERNull.INSTANCE);
140c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-changed
141c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        params.put("SHA1WITHRSAANDMGF1", creatPSSParams(sha1AlgId, 20));
142c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
1436e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // BEGIN android-removed
1446e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // // BEGIN android-changed
1456e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // AlgorithmIdentifier sha224AlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha224, DERNull.INSTANCE);
1466e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // // END android-changed
1476e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // params.put("SHA224WITHRSAANDMGF1", creatPSSParams(sha224AlgId, 28));
1486e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom        // END android-removed
149c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
150c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-changed
151c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AlgorithmIdentifier sha256AlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, DERNull.INSTANCE);
152c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-changed
153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        params.put("SHA256WITHRSAANDMGF1", creatPSSParams(sha256AlgId, 32));
154c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
155c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-changed
156c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AlgorithmIdentifier sha384AlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha384, DERNull.INSTANCE);
157c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-changed
158c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        params.put("SHA384WITHRSAANDMGF1", creatPSSParams(sha384AlgId, 48));
159c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
160c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-changed
161c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AlgorithmIdentifier sha512AlgId = new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512, DERNull.INSTANCE);
162c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-changed
163c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        params.put("SHA512WITHRSAANDMGF1", creatPSSParams(sha512AlgId, 64));
164c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
165c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
166c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static RSASSAPSSparams creatPSSParams(AlgorithmIdentifier hashAlgId, int saltSize)
167c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
168c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return new RSASSAPSSparams(
169c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            hashAlgId,
170c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            new AlgorithmIdentifier(PKCSObjectIdentifiers.id_mgf1, hashAlgId),
1714c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            new ASN1Integer(saltSize),
1724c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            new ASN1Integer(1));
173b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
174c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
175b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static DERObjectIdentifier getAlgorithmOID(
176b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        String algorithmName)
177b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
178b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        algorithmName = Strings.toUpperCase(algorithmName);
179b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
180b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (algorithms.containsKey(algorithmName))
181b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
182b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return (DERObjectIdentifier)algorithms.get(algorithmName);
183b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
184b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
185b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return new DERObjectIdentifier(algorithmName);
186b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
187b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
188b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static AlgorithmIdentifier getSigAlgID(
189c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObjectIdentifier sigOid,
190c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String              algorithmName)
191b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
192b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        if (noParams.contains(sigOid))
193b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
194b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new AlgorithmIdentifier(sigOid);
195b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
196c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
197c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithmName = Strings.toUpperCase(algorithmName);
198c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
199c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (params.containsKey(algorithmName))
200c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
2014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return new AlgorithmIdentifier(sigOid, (ASN1Encodable)params.get(algorithmName));
202c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
203b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        else
204b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
205b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // BEGIN android-changed
206c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return new AlgorithmIdentifier(sigOid, DERNull.INSTANCE);
207b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            // END android-changed
208b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
209b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
210b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
211b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static Iterator getAlgNames()
212b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
213b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        Enumeration e = algorithms.keys();
214b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        List        l = new ArrayList();
215b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
216b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        while (e.hasMoreElements())
217b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
218b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            l.add(e.nextElement());
219b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
220b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
221b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        return l.iterator();
222b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
223b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
224c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static Signature getSignatureInstance(
225c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String algorithm)
226c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws NoSuchAlgorithmException
227c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
228c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return Signature.getInstance(algorithm);
229c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
230c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
231c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static Signature getSignatureInstance(
232c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String algorithm,
233c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String provider)
234c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws NoSuchProviderException, NoSuchAlgorithmException
235c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
236c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (provider != null)
237c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
238c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return Signature.getInstance(algorithm, provider);
239c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
240c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else
241c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
242c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return Signature.getInstance(algorithm);
243c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
244c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
245c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
246c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static byte[] calculateSignature(
247c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObjectIdentifier sigOid,
248c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String              sigName,
249c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        PrivateKey          key,
250c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        SecureRandom        random,
251c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        ASN1Encodable       object)
252c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException
253c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
254c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Signature sig;
255c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
256c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (sigOid == null)
257c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
258c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new IllegalStateException("no signature algorithm specified");
259c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
260c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
261c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        sig = X509Util.getSignatureInstance(sigName);
262c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
263c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (random != null)
264c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
265c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            sig.initSign(key, random);
266c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
267c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else
268c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
269c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            sig.initSign(key);
270c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
271c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
2724c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        sig.update(object.toASN1Primitive().getEncoded(ASN1Encoding.DER));
273c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
274c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return sig.sign();
275c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
276c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
277c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static byte[] calculateSignature(
278c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        DERObjectIdentifier sigOid,
279c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String              sigName,
280c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String              provider,
281c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        PrivateKey          key,
282c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        SecureRandom        random,
283c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        ASN1Encodable       object)
284c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException
285c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
286c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Signature sig;
287c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
288c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (sigOid == null)
289c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
290c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new IllegalStateException("no signature algorithm specified");
291c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
292c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
293c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        sig = X509Util.getSignatureInstance(sigName, provider);
294c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
295c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (random != null)
296c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
297c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            sig.initSign(key, random);
298c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
299c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        else
300c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
301c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            sig.initSign(key);
302c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
303c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
3044c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        sig.update(object.toASN1Primitive().getEncoded(ASN1Encoding.DER));
305c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
306c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return sig.sign();
307c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
308c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
309b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    static X509Principal convertPrincipal(
310b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        X500Principal principal)
311b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
312b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        try
313b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
314b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            return new X509Principal(principal.getEncoded());
315b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
316b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        catch (IOException e)
317b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        {
318b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam            throw new IllegalArgumentException("cannot convert principal");
319b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        }
320b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
321c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
322c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static class Implementation
323c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
324c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Object      engine;
325c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Provider provider;
326c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
327c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Implementation(
328c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            Object      engine,
329c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            Provider    provider)
330c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
331c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            this.engine = engine;
332c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            this.provider = provider;
333c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
334c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
335c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Object getEngine()
336c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
337c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return engine;
338c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
339c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
340c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Provider getProvider()
341c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
342c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            return provider;
343c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
344c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
345c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
346c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
347c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * see if we can find an algorithm (or its alias and what it represents) in
348c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * the property table for the given provider.
349c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
350c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static Implementation getImplementation(
351c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      baseName,
352c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      algorithm,
353c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Provider    prov)
354c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws NoSuchAlgorithmException
355c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
356c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        algorithm = Strings.toUpperCase(algorithm);
357c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
358c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      alias;
359c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
360c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        while ((alias = prov.getProperty("Alg.Alias." + baseName + "." + algorithm)) != null)
361c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
362c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            algorithm = alias;
363c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
364c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
365c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      className = prov.getProperty(baseName + "." + algorithm);
366c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
367c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (className != null)
368c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
369c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            try
370c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
371c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                Class       cls;
372c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                ClassLoader clsLoader = prov.getClass().getClassLoader();
373c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
374c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (clsLoader != null)
375c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
376c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    cls = clsLoader.loadClass(className);
377c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
378c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
379c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
380c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    cls = Class.forName(className);
381c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
382c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
383c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return new Implementation(cls.newInstance(), prov);
384c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
385c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            catch (ClassNotFoundException e)
386c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
387c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                throw new IllegalStateException(
388c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    "algorithm " + algorithm + " in provider " + prov.getName() + " but no class \"" + className + "\" found!");
389c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
390c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            catch (Exception e)
391c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
392c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                throw new IllegalStateException(
393c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    "algorithm " + algorithm + " in provider " + prov.getName() + " but class \"" + className + "\" inaccessible!");
394c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
395c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
396c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
397c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new NoSuchAlgorithmException("cannot find implementation " + algorithm + " for provider " + prov.getName());
398c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
399c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
400c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /**
401c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * return an implementation for a given algorithm/provider.
402c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * If the provider is null, we grab the first avalaible who has the required algorithm.
403c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
404c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static Implementation getImplementation(
405c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      baseName,
406c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        String      algorithm)
407c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws NoSuchAlgorithmException
408c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
409c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Provider[] prov = Security.getProviders();
410c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
411c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
412c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // search every provider looking for the algorithm we want.
413c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
414c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != prov.length; i++)
415c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
416c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            //
417c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            // try case insensitive
418c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            //
419c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            Implementation imp = getImplementation(baseName, Strings.toUpperCase(algorithm), prov[i]);
420c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (imp != null)
421c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
422c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return imp;
423c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
424c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
425c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            try
426c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
427c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                imp = getImplementation(baseName, algorithm, prov[i]);
428c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
429c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            catch (NoSuchAlgorithmException e)
430c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
431c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                // continue
432c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
433c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
434c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
435c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throw new NoSuchAlgorithmException("cannot find implementation " + algorithm);
436c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
437c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
438c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    static Provider getProvider(String provider)
439c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        throws NoSuchProviderException
440c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
441c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        Provider prov = Security.getProvider(provider);
442c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
443c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        if (prov == null)
444c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
445c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            throw new NoSuchProviderException("Provider " + provider + " not found");
446c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
447c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
448c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        return prov;
449c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
450b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
451