Search in sources :

Example 46 with Provider

use of java.security.Provider in project nhin-d by DirectProject.

the class AbstractPKCS11TokenKeyStoreProtectionManager method loadProvider.

protected void loadProvider() throws CryptoException {
    try {
        if (!StringUtils.isEmpty(this.keyStoreProviderName)) {
            if (this.keyStoreProviderName.equals(SUNPKCS11_KEYSTORE_PROVIDER_NAME)) {
                // this provider requires a config file
                if (StringUtils.isEmpty(this.pcks11ConfigFile))
                    throw new IllegalStateException("SunPKCS11 providers require a configuration file.  There is not one set.");
                // check and see if this is one of the same providers that is already loaded
                final InputStream inStream = FileUtils.openInputStream(new File(this.pcks11ConfigFile));
                final Properties props = new Properties();
                props.load(inStream);
                IOUtils.closeQuietly(inStream);
                boolean providerFound = false;
                final String requestedName = props.getProperty("name");
                // check if this provider exists
                if (!StringUtils.isEmpty(requestedName) && Security.getProvider(requestedName) != null)
                    providerFound = true;
                if (!providerFound) {
                    // dynamic load... some class loaders may have issues, so use dynamic loading
                    final Class<?> provider = this.getClass().getClassLoader().loadClass("sun.security.pkcs11.SunPKCS11");
                    final Constructor<?> ctor = provider.getConstructor(String.class);
                    Security.addProvider((Provider) ctor.newInstance(this.pcks11ConfigFile));
                }
            } else {
                // create the new provider
                final Class<?> provider = this.getClass().getClassLoader().loadClass(this.keyStoreProviderName);
                // check if the provider is already loaded
                boolean providerFound = false;
                for (Provider existingProv : Security.getProviders()) {
                    if (existingProv.getClass().equals(provider)) {
                        providerFound = true;
                        break;
                    }
                }
                if (!providerFound)
                    Security.addProvider((Provider) provider.newInstance());
            }
        }
    } catch (Exception e) {
        throw new CryptoException("Error loading PKCS11 provder", e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) File(java.io.File) CryptoException(org.nhindirect.common.crypto.exceptions.CryptoException) Provider(java.security.Provider)

Example 47 with Provider

use of java.security.Provider in project jdk8u_jdk by JetBrains.

the class TestPremaster method main.

public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");
    KeyGenerator kg;
    kg = KeyGenerator.getInstance("SunTlsRsaPremasterSecret", provider);
    try {
        kg.generateKey();
        throw new Exception("no exception");
    } catch (IllegalStateException e) {
        System.out.println("OK: " + e);
    }
    int[] protocolVersions = { 0x0300, 0x0301, 0x0302, 0x0400 };
    for (int clientVersion : protocolVersions) {
        for (int serverVersion : protocolVersions) {
            test(kg, clientVersion, serverVersion);
            if (serverVersion >= clientVersion) {
                break;
            }
        }
    }
    System.out.println("Done.");
}
Also used : KeyGenerator(javax.crypto.KeyGenerator) Provider(java.security.Provider)

Example 48 with Provider

use of java.security.Provider in project jdk8u_jdk by JetBrains.

the class AccessKeyStore method main.

public static void main(String[] args) throws Exception {
    // Check that a security manager has been installed
    if (System.getSecurityManager() == null) {
        throw new Exception("A security manager has not been installed");
    }
    Provider p = Security.getProvider("SunMSCAPI");
    System.out.println("SunMSCAPI provider classname is " + p.getClass().getName());
    KeyStore keyStore = KeyStore.getInstance("Windows-MY", p);
    /*
         * If a SecurityManager exists then this will trigger a
         * SecurityException if the following permission has not
         * been granted:
         *
         *     SecurityPermission("authProvider.SunMSCAPI")
         */
    try {
        keyStore.load(null, null);
        if (args.length > 0 && "-deny".equals(args[0])) {
            throw new Exception("Expected KeyStore.load to throw a SecurityException");
        }
    } catch (SecurityException se) {
        if (args.length > 0 && "-deny".equals(args[0])) {
            System.out.println("Caught the expected exception: " + se);
            return;
        } else {
            throw se;
        }
    }
    int i = 0;
    for (Enumeration<String> e = keyStore.aliases(); e.hasMoreElements(); ) {
        String alias = e.nextElement();
        displayEntry(keyStore, alias, i++);
    }
}
Also used : Provider(java.security.Provider)

Example 49 with Provider

use of java.security.Provider in project jdk8u_jdk by JetBrains.

the class GetInstance method main.

public static void main(String[] argv) throws Exception {
    Provider stubProvider = new StubProvider();
    CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX", stubProvider);
    System.out.println("Test passed.");
}
Also used : CertPathBuilder(java.security.cert.CertPathBuilder) Provider(java.security.Provider)

Example 50 with Provider

use of java.security.Provider in project jdk8u_jdk by JetBrains.

the class TestJSSE method server.

public static void server(String testProtocol, String testCipher, int testPort, String... exception) throws Exception {
    String expectedException = exception.length >= 1 ? exception[0] : null;
    out.println(" This is Server");
    out.println(" Testing Protocol: " + testProtocol);
    out.println(" Testing Cipher: " + testCipher);
    out.println(" Testing Port: " + testPort);
    Provider p = new sun.security.ec.SunEC();
    Security.insertProviderAt(p, 1);
    try {
        CipherTestUtils.main(new JSSEFactory(null, testPort, testProtocol, testCipher, "Server JSSE"), "Server", expectedException);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Provider(java.security.Provider)

Aggregations

Provider (java.security.Provider)243 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)49 ArrayList (java.util.ArrayList)26 MessageDigest (java.security.MessageDigest)21 List (java.util.List)20 Key (java.security.Key)19 KeyStore (java.security.KeyStore)19 Service (java.security.Provider.Service)15 ExemptionMechanism (javax.crypto.ExemptionMechanism)14 SpiEngUtils (org.apache.harmony.security.tests.support.SpiEngUtils)14 InvalidKeyException (java.security.InvalidKeyException)13 SecureRandom (java.security.SecureRandom)13 IOException (java.io.IOException)12 NoSuchProviderException (java.security.NoSuchProviderException)12 SecretKey (javax.crypto.SecretKey)12 KeyStoreException (java.security.KeyStoreException)11 CertificateException (java.security.cert.CertificateException)11 Cipher (javax.crypto.Cipher)11 KeyGenerator (javax.crypto.KeyGenerator)11 MyExemptionMechanismSpi.tmpKey (org.apache.harmony.crypto.tests.support.MyExemptionMechanismSpi.tmpKey)11