Search in sources :

Example 16 with TrustManager

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

the class DefaultTrustedSocketFactory method createSocket.

public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    TrustManager[] trustManagers = new TrustManager[] { TrustManagerFactory.get(host, port) };
    KeyManager[] keyManagers = null;
    if (!TextUtils.isEmpty(clientCertificateAlias)) {
        keyManagers = new KeyManager[] { new KeyChainKeyManager(context, clientCertificateAlias) };
    }
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    SSLSocketFactory socketFactory = sslContext.getSocketFactory();
    Socket trustedSocket;
    if (socket == null) {
        trustedSocket = socketFactory.createSocket();
    } else {
        trustedSocket = socketFactory.createSocket(socket, host, port, true);
    }
    SSLSocket sslSocket = (SSLSocket) trustedSocket;
    hardenSocket(sslSocket);
    setSniHost(socketFactory, sslSocket, host);
    return trustedSocket;
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) KeyManager(javax.net.ssl.KeyManager) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) TrustManager(javax.net.ssl.TrustManager)

Example 17 with TrustManager

use of javax.net.ssl.TrustManager in project okhttputils by hongyangAndroid.

the class HttpsUtils method prepareTrustManager.

private static TrustManager[] prepareTrustManager(InputStream... certificates) {
    if (certificates == null || certificates.length <= 0)
        return null;
    try {
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null);
        int index = 0;
        for (InputStream certificate : certificates) {
            String certificateAlias = Integer.toString(index++);
            keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
            try {
                if (certificate != null)
                    certificate.close();
            } catch (IOException e) {
            }
        }
        TrustManagerFactory trustManagerFactory = null;
        trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        return trustManagers;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 18 with TrustManager

use of javax.net.ssl.TrustManager in project netty-socketio by mrniko.

the class SocketIOChannelInitializer method createSSLContext.

private SSLContext createSSLContext(Configuration configuration) throws Exception {
    TrustManager[] managers = null;
    if (configuration.getTrustStore() != null) {
        KeyStore ts = KeyStore.getInstance(configuration.getTrustStoreFormat());
        ts.load(configuration.getTrustStore(), configuration.getTrustStorePassword().toCharArray());
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(ts);
        managers = tmf.getTrustManagers();
    }
    KeyStore ks = KeyStore.getInstance(configuration.getKeyStoreFormat());
    ks.load(configuration.getKeyStore(), configuration.getKeyStorePassword().toCharArray());
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(configuration.getKeyManagerFactoryAlgorithm());
    kmf.init(ks, configuration.getKeyStorePassword().toCharArray());
    SSLContext serverContext = SSLContext.getInstance(configuration.getSSLProtocol());
    serverContext.init(kmf.getKeyManagers(), managers, null);
    return serverContext;
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 19 with TrustManager

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

the class InProcessBuilderTest method trustAllSSLCerts.

private void trustAllSSLCerts() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { 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;
        }
    } };
    // Install the all-trusting trust manager
    SSLContext sc = SSLContext.getInstance("TLS");
    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 20 with TrustManager

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

the class HttpsAccessIT method startServer.

@Before
public void startServer() throws NoSuchAlgorithmException, KeyManagementException, IOException {
    server = server().withHttpsEnabled().usingDataDir(folder.directory(name.getMethodName()).getAbsolutePath()).build();
    httpsUri = server.httpsUri().get().toASCIIString();
    // Because we are generating a non-CA-signed certificate, we need to turn off verification in the client.
    // This is ironic, since there is no proper verification on the CA side in the first place, but I digress.
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    // Install the all-trusting trust manager
    SSLContext sc = SSLContext.getInstance("TLS");
    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) Before(org.junit.Before)

Aggregations

TrustManager (javax.net.ssl.TrustManager)186 SSLContext (javax.net.ssl.SSLContext)116 X509TrustManager (javax.net.ssl.X509TrustManager)111 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)78 X509Certificate (java.security.cert.X509Certificate)53 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)51 SecureRandom (java.security.SecureRandom)51 IOException (java.io.IOException)48 KeyManagementException (java.security.KeyManagementException)47 KeyStore (java.security.KeyStore)47 KeyManager (javax.net.ssl.KeyManager)44 CertificateException (java.security.cert.CertificateException)36 KeyStoreException (java.security.KeyStoreException)35 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)31 GeneralSecurityException (java.security.GeneralSecurityException)22 HostnameVerifier (javax.net.ssl.HostnameVerifier)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)15 InputStream (java.io.InputStream)14 UnrecoverableKeyException (java.security.UnrecoverableKeyException)13 FileInputStream (java.io.FileInputStream)11