Search in sources :

Example 51 with TrustManager

use of javax.net.ssl.TrustManager in project AisenWeiBo by wangdan.

the class HttpsUtility method getOkHttpClient.

public OkHttpClient getOkHttpClient() {
    if (mOKHttpClient == null) {
        try {
            mOKHttpClient = new OkHttpClient();
            mOKHttpClient.setConnectTimeout(GlobalContext.CONN_TIMEOUT, TimeUnit.MILLISECONDS);
            mOKHttpClient.setReadTimeout(GlobalContext.READ_TIMEOUT, TimeUnit.MILLISECONDS);
            TrustManager tm = new X509TrustManager() {

                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { tm }, null);
            mOKHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    return mOKHttpClient;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 52 with TrustManager

use of javax.net.ssl.TrustManager in project wildfly by wildfly.

the class Util method forDomain.

static SSLContext forDomain(JSSESecurityDomain securityDomain) throws IOException {
    SSLContext sslCtx = null;
    try {
        sslCtx = SSLContext.getInstance("TLS");
        KeyManager[] keyManagers = securityDomain.getKeyManagers();
        if (keyManagers == null)
            throw IIOPLogger.ROOT_LOGGER.errorObtainingKeyManagers(securityDomain.getSecurityDomain());
        TrustManager[] trustManagers = securityDomain.getTrustManagers();
        sslCtx.init(keyManagers, trustManagers, null);
        return sslCtx;
    } catch (NoSuchAlgorithmException e) {
        throw IIOPLogger.ROOT_LOGGER.failedToGetSSLContext(e);
    } catch (KeyManagementException e) {
        throw IIOPLogger.ROOT_LOGGER.failedToGetSSLContext(e);
    } catch (SecurityException e) {
        throw IIOPLogger.ROOT_LOGGER.failedToGetSSLContext(e);
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManager(javax.net.ssl.KeyManager) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager)

Example 53 with TrustManager

use of javax.net.ssl.TrustManager in project undertow by undertow-io.

the class DefaultServer method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, boolean client) throws IOException {
    KeyManager[] keyManagers;
    try {
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, STORE_PASSWORD);
        keyManagers = keyManagerFactory.getKeyManagers();
    } catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) {
        throw new IOException("Unable to initialise KeyManager[]", e);
    }
    TrustManager[] trustManagers = null;
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        trustManagers = trustManagerFactory.getTrustManagers();
    } catch (NoSuchAlgorithmException | KeyStoreException e) {
        throw new IOException("Unable to initialise TrustManager[]", e);
    }
    SSLContext sslContext;
    try {
        if (openssl && !client) {
            sslContext = SSLContext.getInstance("openssl.TLS");
        } else {
            sslContext = SSLContext.getInstance("TLS");
        }
        sslContext.init(keyManagers, trustManagers, null);
    } catch (NoSuchAlgorithmException | KeyManagementException e) {
        throw new IOException("Unable to create and initialise the SSLContext", e);
    }
    return sslContext;
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) KeyManagementException(java.security.KeyManagementException) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager) UnrecoverableKeyException(java.security.UnrecoverableKeyException) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyManager(javax.net.ssl.KeyManager)

Example 54 with TrustManager

use of javax.net.ssl.TrustManager in project undertow by undertow-io.

the class Http2Server method createSSLContext.

private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore) throws Exception {
    KeyManager[] keyManagers;
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password("key"));
    keyManagers = keyManagerFactory.getKeyManagers();
    TrustManager[] trustManagers;
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(trustStore);
    trustManagers = trustManagerFactory.getTrustManagers();
    SSLContext sslContext;
    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    return sslContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyManager(javax.net.ssl.KeyManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManager(javax.net.ssl.TrustManager)

Example 55 with TrustManager

use of javax.net.ssl.TrustManager in project pictureapp by EyeSeeTea.

the class UnsafeOkHttpsClientFactory method getUnsafeOkHttpClient.

public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

TrustManager (javax.net.ssl.TrustManager)229 SSLContext (javax.net.ssl.SSLContext)139 X509TrustManager (javax.net.ssl.X509TrustManager)139 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)90 X509Certificate (java.security.cert.X509Certificate)70 IOException (java.io.IOException)60 KeyStore (java.security.KeyStore)60 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)60 SecureRandom (java.security.SecureRandom)58 KeyManagementException (java.security.KeyManagementException)54 KeyManager (javax.net.ssl.KeyManager)52 CertificateException (java.security.cert.CertificateException)43 KeyStoreException (java.security.KeyStoreException)37 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)34 HostnameVerifier (javax.net.ssl.HostnameVerifier)23 URL (java.net.URL)22 GeneralSecurityException (java.security.GeneralSecurityException)22 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)20 InputStream (java.io.InputStream)18 FileInputStream (java.io.FileInputStream)16