Search in sources :

Example 11 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project languagetool by languagetool-org.

the class HTTPTools method disableCertChecks.

/**
   * For testing, we disable all checks because we use a self-signed certificate on the server
   * side and we want this test to run everywhere without importing the certificate into the JVM's trust store.
   *
   * See http://stackoverflow.com/questions/2893819/telling-java-to-accept-self-signed-ssl-certificate
   */
static void disableCertChecks() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = { new X509TrustManager() {

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

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 12 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project c-geo by just-radovan.

the class cgBase method trustAllHosts.

public static void trustAllHosts() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }

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

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        Log.e(cgSettings.tag, "cgBase.trustAllHosts: " + e.toString());
    }
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) SocketException(java.net.SocketException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 13 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project k-9 by k9mail.

the class TrustManagerFactoryTest method testLocallyTrustedCertificateChain.

@Test
public void testLocallyTrustedCertificateChain() throws Exception {
    mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert3);
    X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1);
    trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) Test(org.junit.Test)

Example 14 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project k-9 by k9mail.

the class TrustManagerFactoryTest method testCertificateOfOtherHost.

@Test
public void testCertificateOfOtherHost() throws Exception {
    mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
    mKeyStore.addCertificate(MATCHING_HOST, PORT2, mCert2);
    X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1);
    assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 });
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) Test(org.junit.Test)

Example 15 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project k-9 by k9mail.

the class TrustManagerFactoryTest method testLocallyTrustedCertificateChainNotMatchingHost.

@Test
public void testLocallyTrustedCertificateChainNotMatchingHost() throws Exception {
    mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert3);
    X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1);
    trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) Test(org.junit.Test)

Aggregations

X509TrustManager (javax.net.ssl.X509TrustManager)150 TrustManager (javax.net.ssl.TrustManager)87 X509Certificate (java.security.cert.X509Certificate)79 SSLContext (javax.net.ssl.SSLContext)70 CertificateException (java.security.cert.CertificateException)49 IOException (java.io.IOException)40 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)40 SecureRandom (java.security.SecureRandom)35 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)25 KeyManagementException (java.security.KeyManagementException)22 KeyStore (java.security.KeyStore)16 GeneralSecurityException (java.security.GeneralSecurityException)15 Test (org.junit.Test)15 KeyStoreException (java.security.KeyStoreException)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)14 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)14 SSLException (javax.net.ssl.SSLException)13 SSLSession (javax.net.ssl.SSLSession)11 OkHttpClient (okhttp3.OkHttpClient)8 X509KeyManager (javax.net.ssl.X509KeyManager)7