1b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampackage org.bouncycastle.jce.provider;
2b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
34c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport java.io.IOException;
4c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.AccessController;
54c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport java.security.PrivateKey;
6c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.security.PrivilegedAction;
7b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallamimport java.security.Provider;
84c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport java.security.PublicKey;
94c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport java.util.HashMap;
10c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstromimport java.util.Map;
11b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.ASN1ObjectIdentifier;
134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
154c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.config.ProviderConfiguration;
174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstromimport org.bouncycastle.jcajce.provider.util.AsymmetricKeyInfoConverter;
19b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
20b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam/**
21b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * To add the provider at runtime use:
22b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <pre>
23b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * import java.security.Security;
24b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * import org.bouncycastle.jce.provider.BouncyCastleProvider;
25b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *
26b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * Security.addProvider(new BouncyCastleProvider());
27b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </pre>
28b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * The provider can also be configured as part of your environment via
29b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * static registration by adding an entry to the java.security properties
30b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * file (found in $JAVA_HOME/jre/lib/security/java.security, where
31b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * $JAVA_HOME is the location of your JDK/JRE distribution). You'll find
32b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * detailed instructions in the file but basically it comes down to adding
33b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * a line:
34b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <pre>
35b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * <code>
36b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam *    security.provider.&lt;n&gt;=org.bouncycastle.jce.provider.BouncyCastleProvider
37b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </code>
38b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * </pre>
39b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * Where &lt;n&gt; is the preference you want the provider at (1 being the
40c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * most preferred).
41c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom * <p>Note: JCE algorithm names should be upper-case only so the case insensitive
42b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam * test for getInstance works.
43b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam */
44b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallampublic final class BouncyCastleProvider extends Provider
45c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    implements ConfigurableProvider
46b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam{
47028ab6e01e3b911024b9b9243e9a0f4ac377c0faSergio Giro    private static String info = "BouncyCastle Security Provider v1.52";
48b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
49b3a9a89b23849a25f69192e943c8ffa2cee7adf0Jesse Wilson    public static final String PROVIDER_NAME = "BC";
50b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
514c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static final ProviderConfiguration CONFIGURATION = new BouncyCastleProviderConfiguration();
524c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
534c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private static final Map keyInfoConverters = new HashMap();
544c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
55c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    /*
56c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Configurable symmetric ciphers
57c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
58a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String SYMMETRIC_PACKAGE = "org.bouncycastle.jcajce.provider.symmetric.";
59a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
60a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String[] SYMMETRIC_GENERIC =
61a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    {
62a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        "PBEPBKDF2", "PBEPKCS12"
63a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    };
64a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
65a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String[] SYMMETRIC_MACS =
66a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    {
67a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // BEGIN android-removed
68a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // "SipHash"
69a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // END android-removed
70a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    };
71a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
72c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final String[] SYMMETRIC_CIPHERS =
73c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
74c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-removed
755db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        // "AES", "ARC4", "Blowfish", "Camellia", "CAST5", "CAST6", "ChaCha", "DES", "DESede",
765db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        // "GOST28147", "Grainv1", "Grain128", "HC128", "HC256", "IDEA", "Noekeon", "RC2", "RC5",
775db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        // "RC6", "Rijndael", "Salsa20", "SEED", "Serpent", "Shacal2", "Skipjack", "TEA", "Twofish", "Threefish",
785db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        // "VMPC", "VMPCKSA3", "XTEA", "XSalsa20"
79c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-removed
80c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-added
815db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        "AES", "ARC4", "Blowfish", "DES", "DESede", "RC2", "Twofish",
82c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-added
83c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    };
84c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
854c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     /*
86c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     * Configurable asymmetric ciphers
87c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom     */
88a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String ASYMMETRIC_PACKAGE = "org.bouncycastle.jcajce.provider.asymmetric.";
894c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
904c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    // this one is required for GNU class path - it needs to be loaded first as the
914c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    // later ones configure it.
924c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private static final String[] ASYMMETRIC_GENERIC =
934c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
94a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // BEGIN android-removed
95a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // "X509", "IES"
96a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // END android-removed
97a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // BEGIN android-added
984c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        "X509"
99a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // END android-added
1004c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    };
1014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
102c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private static final String[] ASYMMETRIC_CIPHERS =
103c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
1044c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // BEGIN android-removed
105e1142c149e244797ce73b0e7fad40816e447a817Brian Carlstrom        // "DSA", "DH", "EC", "RSA", "GOST", "ECGOST", "ElGamal", "DSTU4145"
1064c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // END android-removed
1074c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // BEGIN android-added
1084c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        "DSA", "DH", "EC", "RSA",
1094c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // END android-added
1104c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    };
1114c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
1124c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    /*
1134c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     * Configurable digests
1144c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom     */
1154c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private static final String DIGEST_PACKAGE = "org.bouncycastle.jcajce.provider.digest.";
1164c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    private static final String[] DIGESTS =
1174c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
1184c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // BEGIN android-removed
1195db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root        // "GOST3411", "MD2", "MD4", "MD5", "SHA1", "RIPEMD128", "RIPEMD160", "RIPEMD256", "RIPEMD320", "SHA224", "SHA256", "SHA384", "SHA512", "SHA3", "Skein", "SM3", "Tiger", "Whirlpool"
1204c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // END android-removed
1214c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // BEGIN android-added
12287490acd76f544251011cf49753d4d0a61f86a66Kenny Root        "MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512",
1234c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        // END android-added
124c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    };
125c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
126a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    /*
1275db505e1f6a68c8d5dfdb0fed0b8607dea7bed96Kenny Root     * Configurable keystores
128a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom     */
129a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String KEYSTORE_PACKAGE = "org.bouncycastle.jcajce.provider.keystore.";
130a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    private static final String[] KEYSTORES =
131a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    {
132a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        "BC", "PKCS12"
133a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom    };
134a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
135b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    /**
136b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * Construct a new provider.  This should only be required when
137b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * using runtime registration of the provider using the
138b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     * <code>Security.addProvider()</code> mechanism.
139b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam     */
140b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    public BouncyCastleProvider()
141b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
142028ab6e01e3b911024b9b9243e9a0f4ac377c0faSergio Giro        super(PROVIDER_NAME, 1.52, info);
143c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
144c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        AccessController.doPrivileged(new PrivilegedAction()
145c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
146c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            public Object run()
147c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
148c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                setup();
149c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                return null;
150c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
151c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        });
152c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
153c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
154c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void setup()
155c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
1564c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        loadAlgorithms(DIGEST_PACKAGE, DIGESTS);
1574c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
158a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(SYMMETRIC_PACKAGE, SYMMETRIC_GENERIC);
159a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
160a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(SYMMETRIC_PACKAGE, SYMMETRIC_MACS);
161a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
162a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(SYMMETRIC_PACKAGE, SYMMETRIC_CIPHERS);
163a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom
164a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(ASYMMETRIC_PACKAGE, ASYMMETRIC_GENERIC);
1654c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
166a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(ASYMMETRIC_PACKAGE, ASYMMETRIC_CIPHERS);
1674c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
168a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        loadAlgorithms(KEYSTORE_PACKAGE, KEYSTORES);
169c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
170c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-removed
171c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // //
172c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // // X509Store
173c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // //
174c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CERTIFICATE/COLLECTION", "org.bouncycastle.jce.provider.X509StoreCertCollection");
175c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.ATTRIBUTECERTIFICATE/COLLECTION", "org.bouncycastle.jce.provider.X509StoreAttrCertCollection");
176c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CRL/COLLECTION", "org.bouncycastle.jce.provider.X509StoreCRLCollection");
177c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CERTIFICATEPAIR/COLLECTION", "org.bouncycastle.jce.provider.X509StoreCertPairCollection");
178c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
179c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CERTIFICATE/LDAP", "org.bouncycastle.jce.provider.X509StoreLDAPCerts");
180c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CRL/LDAP", "org.bouncycastle.jce.provider.X509StoreLDAPCRLs");
181c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.ATTRIBUTECERTIFICATE/LDAP", "org.bouncycastle.jce.provider.X509StoreLDAPAttrCerts");
182c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509Store.CERTIFICATEPAIR/LDAP", "org.bouncycastle.jce.provider.X509StoreLDAPCertPairs");
183c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
184c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // //
185c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // // X509StreamParser
186c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // //
187c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509StreamParser.CERTIFICATE", "org.bouncycastle.jce.provider.X509CertParser");
188c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509StreamParser.ATTRIBUTECERTIFICATE", "org.bouncycastle.jce.provider.X509AttrCertParser");
189c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509StreamParser.CRL", "org.bouncycastle.jce.provider.X509CRLParser");
190c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("X509StreamParser.CERTIFICATEPAIR", "org.bouncycastle.jce.provider.X509CertPairParser");
191b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        //
192a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // //
193a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // // cipher engines
194a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // //
1957a6b43b187fb942402daa61e0b92496746f5bc1cBrian Carlstrom        // put("Cipher.BROKENPBEWITHMD5ANDDES", "org.bouncycastle.jce.provider.BrokenJCEBlockCipher$BrokePBEWithMD5AndDES");
1964c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        //
197a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // put("Cipher.BROKENPBEWITHSHA1ANDDES", "org.bouncycastle.jce.provider.BrokenJCEBlockCipher$BrokePBEWithSHA1AndDES");
198e1142c149e244797ce73b0e7fad40816e447a817Brian Carlstrom        //
199c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        //
200a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // put("Cipher.OLDPBEWITHSHAANDTWOFISH-CBC", "org.bouncycastle.jce.provider.BrokenJCEBlockCipher$OldPBEWithSHAAndTwofish");
2014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        //
202a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // // Certification Path API
203c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("CertPathValidator.RFC3281", "org.bouncycastle.jce.provider.PKIXAttrCertPathValidatorSpi");
204c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("CertPathBuilder.RFC3281", "org.bouncycastle.jce.provider.PKIXAttrCertPathBuilderSpi");
205a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // put("CertPathValidator.RFC3280", "org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi");
206a198e1ecc615e26a167d0f2dca9fa7e5fc62de10Brian Carlstrom        // put("CertPathBuilder.RFC3280", "org.bouncycastle.jce.provider.PKIXCertPathBuilderSpi");
207c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-removed
208b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        put("CertPathValidator.PKIX", "org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi");
209b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        put("CertPathBuilder.PKIX", "org.bouncycastle.jce.provider.PKIXCertPathBuilderSpi");
210b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam        put("CertStore.Collection", "org.bouncycastle.jce.provider.CertStoreCollectionSpi");
211c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // BEGIN android-removed
212c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("CertStore.LDAP", "org.bouncycastle.jce.provider.X509LDAPCertStoreSpi");
213c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("CertStore.Multi", "org.bouncycastle.jce.provider.MultiCertStoreSpi");
214c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // put("Alg.Alias.CertStore.X509LDAP", "LDAP");
215c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        // END android-removed
216b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
217c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
218c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    private void loadAlgorithms(String packageName, String[] names)
219c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
220c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        for (int i = 0; i != names.length; i++)
221c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        {
222c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            Class clazz = null;
223c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            try
224c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
225c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                ClassLoader loader = this.getClass().getClassLoader();
226c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
227c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                if (loader != null)
228c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
2296e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom                    clazz = loader.loadClass(packageName + names[i] + "$Mappings");
230c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
231c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                else
232c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
2336e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom                    clazz = Class.forName(packageName + names[i] + "$Mappings");
234c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
235c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
236c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            catch (ClassNotFoundException e)
237c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
238c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                // ignore
239c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
240c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
241c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            if (clazz != null)
242c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            {
243c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                try
244c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {
2454c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom                    ((AlgorithmProvider)clazz.newInstance()).configure(this);
246c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
247c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                catch (Exception e)
248c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                {   // this should never ever happen!!
249c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                    throw new InternalError("cannot create instance of "
2506e736056d64d0e33b26cf9f7c4e351b496241fdeBrian Carlstrom                        + packageName + names[i] + "$Mappings : " + e);
251c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom                }
252c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom            }
253c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom        }
254c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
255c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom
2564c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public void setParameter(String parameterName, Object parameter)
257b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
2584c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        synchronized (CONFIGURATION)
2594c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
2604c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            ((BouncyCastleProviderConfiguration)CONFIGURATION).setParameter(parameterName, parameter);
2614c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
262b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
263b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2644c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public boolean hasAlgorithm(String type, String name)
265b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    {
2664c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return containsKey(type + "." + name) || containsKey("Alg.Alias." + type + "." + name);
267b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
268b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2694c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public void addAlgorithm(String key, String value)
2704c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
2714c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (containsKey(key))
2724c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
2734c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            throw new IllegalStateException("duplicate provider key (" + key + ") found");
2744c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
275b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2764c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        put(key, value);
2774c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
278b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2794c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public void addKeyInfoConverter(ASN1ObjectIdentifier oid, AsymmetricKeyInfoConverter keyInfoConverter)
2804c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
2814c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        keyInfoConverters.put(oid, keyInfoConverter);
2824c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    }
283b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2844c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static PublicKey getPublicKey(SubjectPublicKeyInfo publicKeyInfo)
2854c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        throws IOException
2864c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    {
2874c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        AsymmetricKeyInfoConverter converter = (AsymmetricKeyInfoConverter)keyInfoConverters.get(publicKeyInfo.getAlgorithm().getAlgorithm());
288b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2894c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (converter == null)
2904c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
2914c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return null;
2924c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
293b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2944c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return converter.generatePublic(publicKeyInfo);
295b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam    }
296b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam
2974c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom    public static PrivateKey getPrivateKey(PrivateKeyInfo privateKeyInfo)
2984c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        throws IOException
299c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    {
3004c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        AsymmetricKeyInfoConverter converter = (AsymmetricKeyInfoConverter)keyInfoConverters.get(privateKeyInfo.getPrivateKeyAlgorithm().getAlgorithm());
3014c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
3024c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        if (converter == null)
3034c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        {
3044c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom            return null;
3054c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        }
3064c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom
3074c111300c39cb2e27f07fc2ae3b00e23ed4443b2Brian Carlstrom        return converter.generatePrivate(privateKeyInfo);
308c37f4a04ef89e73a39a59f3c5a179af8c8ab5974Brian Carlstrom    }
309b61a96e7ef1a78acf013bbf08fe537e5b5f129caPeter Hallam}
310