Search in sources :

Example 11 with KeyStore

use of java.security.KeyStore in project zookeeper by apache.

the class X509Util method createKeyManager.

public static X509KeyManager createKeyManager(String keyStoreLocation, String keyStorePassword) throws KeyManagerException {
    FileInputStream inputStream = null;
    try {
        char[] keyStorePasswordChars = keyStorePassword.toCharArray();
        File keyStoreFile = new File(keyStoreLocation);
        KeyStore ks = KeyStore.getInstance("JKS");
        inputStream = new FileInputStream(keyStoreFile);
        ks.load(inputStream, keyStorePasswordChars);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, keyStorePasswordChars);
        for (KeyManager km : kmf.getKeyManagers()) {
            if (km instanceof X509KeyManager) {
                return (X509KeyManager) km;
            }
        }
        throw new KeyManagerException("Couldn't find X509KeyManager");
    } catch (Exception e) {
        throw new KeyManagerException(e);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : KeyManagerException(org.apache.zookeeper.common.X509Exception.KeyManagerException) X509KeyManager(javax.net.ssl.X509KeyManager) IOException(java.io.IOException) File(java.io.File) KeyStore(java.security.KeyStore) X509KeyManager(javax.net.ssl.X509KeyManager) KeyManager(javax.net.ssl.KeyManager) FileInputStream(java.io.FileInputStream) TrustManagerException(org.apache.zookeeper.common.X509Exception.TrustManagerException) IOException(java.io.IOException) KeyManagerException(org.apache.zookeeper.common.X509Exception.KeyManagerException) SSLContextException(org.apache.zookeeper.common.X509Exception.SSLContextException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 12 with KeyStore

use of java.security.KeyStore in project UltimateAndroid by cymcsg.

the class HttpsUtils method getKeyManagerFactory.

private static KeyManagerFactory getKeyManagerFactory(InputStream key, String keyPassword) {
    KeyManagerFactory kmf = null;
    try {
        String keyStoreType = "BKS";
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(key, keyPassword.toCharArray());
        String kmfAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
        kmf = KeyManagerFactory.getInstance(kmfAlgorithm);
        kmf.init(keyStore, keyPassword.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return kmf;
}
Also used : KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 13 with KeyStore

use of java.security.KeyStore in project dropwizard by dropwizard.

the class HttpsConnectorFactory method configureSslContextFactory.

protected SslContextFactory configureSslContextFactory(SslContextFactory factory) {
    if (keyStorePath != null) {
        factory.setKeyStorePath(keyStorePath);
    }
    final String keyStoreType = getKeyStoreType();
    if (keyStoreType.startsWith("Windows-")) {
        try {
            final KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            factory.setKeyStore(keyStore);
        } catch (Exception e) {
            throw new IllegalStateException("Windows key store not supported", e);
        }
    } else {
        factory.setKeyStoreType(keyStoreType);
        factory.setKeyStorePassword(keyStorePassword);
    }
    if (keyStoreProvider != null) {
        factory.setKeyStoreProvider(keyStoreProvider);
    }
    final String trustStoreType = getTrustStoreType();
    if (trustStoreType.startsWith("Windows-")) {
        try {
            final KeyStore keyStore = KeyStore.getInstance(trustStoreType);
            keyStore.load(null, null);
            factory.setTrustStore(keyStore);
        } catch (Exception e) {
            throw new IllegalStateException("Windows key store not supported", e);
        }
    } else {
        if (trustStorePath != null) {
            factory.setTrustStorePath(trustStorePath);
        }
        if (trustStorePassword != null) {
            factory.setTrustStorePassword(trustStorePassword);
        }
        factory.setTrustStoreType(trustStoreType);
    }
    if (trustStoreProvider != null) {
        factory.setTrustStoreProvider(trustStoreProvider);
    }
    if (keyManagerPassword != null) {
        factory.setKeyManagerPassword(keyManagerPassword);
    }
    if (needClientAuth != null) {
        factory.setNeedClientAuth(needClientAuth);
    }
    if (wantClientAuth != null) {
        factory.setWantClientAuth(wantClientAuth);
    }
    if (certAlias != null) {
        factory.setCertAlias(certAlias);
    }
    if (crlPath != null) {
        factory.setCrlPath(crlPath.getAbsolutePath());
    }
    if (enableCRLDP != null) {
        factory.setEnableCRLDP(enableCRLDP);
    }
    if (enableOCSP != null) {
        factory.setEnableOCSP(enableOCSP);
    }
    if (maxCertPathLength != null) {
        factory.setMaxCertPathLength(maxCertPathLength);
    }
    if (ocspResponderUrl != null) {
        factory.setOcspResponderURL(ocspResponderUrl.toASCIIString());
    }
    if (jceProvider != null) {
        factory.setProvider(jceProvider);
    }
    factory.setRenegotiationAllowed(allowRenegotiation);
    factory.setEndpointIdentificationAlgorithm(endpointIdentificationAlgorithm);
    factory.setValidateCerts(validateCerts);
    factory.setValidatePeerCerts(validatePeers);
    if (supportedProtocols != null) {
        factory.setIncludeProtocols(Iterables.toArray(supportedProtocols, String.class));
    }
    if (excludedProtocols != null) {
        factory.setExcludeProtocols(Iterables.toArray(excludedProtocols, String.class));
    }
    if (supportedCipherSuites != null) {
        factory.setIncludeCipherSuites(Iterables.toArray(supportedCipherSuites, String.class));
    }
    if (excludedCipherSuites != null) {
        factory.setExcludeCipherSuites(Iterables.toArray(excludedCipherSuites, String.class));
    }
    return factory;
}
Also used : KeyStore(java.security.KeyStore)

Example 14 with KeyStore

use of java.security.KeyStore in project Trello-Android by chrisHoekstra.

the class TrelloService method getHttpClient.

public HttpClient getHttpClient() {
    DefaultHttpClient client = null;
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new CustomSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        // Setting up parameters
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "utf-8");
        params.setBooleanParameter("http.protocol.expect-continue", true);
        // Setting timeout
        HttpConnectionParams.setConnectionTimeout(params, 100000);
        HttpConnectionParams.setSoTimeout(params, 100000);
        // Registering schemes for both HTTP and HTTPS
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        // Creating thread safe client connection manager
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        // Creating HTTP client
        client = new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        client = new DefaultHttpClient();
    }
    return client;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams) KeyStore(java.security.KeyStore) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) UnknownHostException(java.net.UnknownHostException)

Example 15 with KeyStore

use of java.security.KeyStore in project dropwizard by dropwizard.

the class DropwizardSSLConnectionSocketFactory method loadTrustMaterial.

private void loadTrustMaterial(SSLContextBuilder sslContextBuilder) throws Exception {
    KeyStore trustStore = null;
    if (configuration.getTrustStorePath() != null) {
        trustStore = loadKeyStore(configuration.getTrustStoreType(), configuration.getTrustStorePath(), configuration.getTrustStorePassword());
    }
    TrustStrategy trustStrategy = null;
    if (configuration.isTrustSelfSignedCertificates()) {
        trustStrategy = new TrustSelfSignedStrategy();
    }
    sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
}
Also used : TrustStrategy(org.apache.http.ssl.TrustStrategy) KeyStore(java.security.KeyStore) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

KeyStore (java.security.KeyStore)738 IOException (java.io.IOException)190 X509Certificate (java.security.cert.X509Certificate)189 FileInputStream (java.io.FileInputStream)163 KeyStoreException (java.security.KeyStoreException)151 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)138 InputStream (java.io.InputStream)125 Certificate (java.security.cert.Certificate)124 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)119 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)114 SSLContext (javax.net.ssl.SSLContext)112 PrivateKey (java.security.PrivateKey)94 CertificateException (java.security.cert.CertificateException)94 File (java.io.File)82 ByteArrayInputStream (java.io.ByteArrayInputStream)75 CertificateFactory (java.security.cert.CertificateFactory)75 Key (java.security.Key)61 UnrecoverableKeyException (java.security.UnrecoverableKeyException)55 TrustManager (javax.net.ssl.TrustManager)47 KeyManagementException (java.security.KeyManagementException)40