Search in sources :

Example 86 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project Payara by payara.

the class JspTest method disableCertValidation.

public static void disableCertValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            return;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            return;
        }
    } };
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        return;
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 87 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project Payara by payara.

the class SecureWebAppTest method disableCertValidation.

public static void disableCertValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            return;
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            return;
        }
    } };
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        return;
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 88 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project Payara by payara.

the class SecuritySupportImpl method getTrustManagers.

public TrustManager[] getTrustManagers(String algorithm) throws IOException, KeyStoreException, NoSuchAlgorithmException {
    KeyStore[] tstores = getTrustStores();
    ArrayList<TrustManager> trustManagers = new ArrayList<TrustManager>();
    for (KeyStore tstore : tstores) {
        checkCertificateDates(tstore);
        TrustManagerFactory tmf = TrustManagerFactory.getInstance((algorithm != null) ? algorithm : TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(tstore);
        TrustManager[] tmgrs = tmf.getTrustManagers();
        if (tmgrs != null) {
            trustManagers.addAll(Arrays.asList(tmgrs));
        }
    }
    TrustManager trustManager;
    if (trustManagers.size() == 1) {
        trustManager = trustManagers.get(0);
    } else {
        trustManager = new UnifiedX509TrustManager(trustManagers.toArray(new X509TrustManager[trustManagers.size()]));
    }
    return new TrustManager[] { trustManager };
}
Also used : UnifiedX509TrustManager(com.sun.enterprise.security.ssl.manager.UnifiedX509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ArrayList(java.util.ArrayList) KeyStore(java.security.KeyStore) TrustManager(javax.net.ssl.TrustManager) UnifiedX509TrustManager(com.sun.enterprise.security.ssl.manager.UnifiedX509TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 89 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project mica2 by obiba.

the class AgateRestService method getSocketFactory.

/**
 * Do not check anything from the remote host (Agate server is trusted).
 *
 * @return
 * @throws NoSuchAlgorithmException
 * @throws KeyManagementException
 */
private SSLConnectionSocketFactory getSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
    // Accepts any SSL certificate
    TrustManager tm = new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, new TrustManager[] { tm }, null);
    return new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 90 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project web3sdk by FISCO-BCOS.

the class CertificateManager method createCertificateChainTrustManager.

private static CertificateChainTrustManager createCertificateChainTrustManager(KeyStore keyStore) throws NoSuchAlgorithmException, KeyStoreException {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);
    X509TrustManager defaultTrustManager = (X509TrustManager) trustManagerFactory.getTrustManagers()[0];
    return new CertificateChainTrustManager(defaultTrustManager);
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Aggregations

X509TrustManager (javax.net.ssl.X509TrustManager)183 TrustManager (javax.net.ssl.TrustManager)114 X509Certificate (java.security.cert.X509Certificate)96 SSLContext (javax.net.ssl.SSLContext)88 CertificateException (java.security.cert.CertificateException)54 IOException (java.io.IOException)50 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)45 SecureRandom (java.security.SecureRandom)44 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)32 KeyManagementException (java.security.KeyManagementException)28 Test (org.junit.Test)21 HostnameVerifier (javax.net.ssl.HostnameVerifier)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)19 KeyStore (java.security.KeyStore)17 GeneralSecurityException (java.security.GeneralSecurityException)15 SSLSession (javax.net.ssl.SSLSession)15 KeyStoreException (java.security.KeyStoreException)14 SSLException (javax.net.ssl.SSLException)14 URL (java.net.URL)11 OkHttpClient (okhttp3.OkHttpClient)10