Search in sources :

Example 36 with TrustManager

use of javax.net.ssl.TrustManager in project Conversations by siacs.

the class MemorizingTrustManager method getTrustManager.

X509TrustManager getTrustManager(KeyStore ks) {
    try {
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
        tmf.init(ks);
        for (TrustManager t : tmf.getTrustManagers()) {
            if (t instanceof X509TrustManager) {
                return (X509TrustManager) t;
            }
        }
    } catch (Exception e) {
        // Here, we are covering up errors. It might be more useful
        // however to throw them out of the constructor so the
        // embedding app knows something went wrong.
        LOGGER.log(Level.SEVERE, "getTrustManager(" + ks + ")", e);
    }
    return null;
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) KeyStoreException(java.security.KeyStoreException) JSONException(org.json.JSONException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 37 with TrustManager

use of javax.net.ssl.TrustManager in project platform_frameworks_base by android.

the class X509TrustManagerExtensionsTest method testNormalUseCase.

public void testNormalUseCase() throws Exception {
    String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(defaultAlgorithm);
    String defaultKeystoreType = KeyStore.getDefaultType();
    tmf.init(KeyStore.getInstance(defaultKeystoreType));
    TrustManager[] tms = tmf.getTrustManagers();
    for (TrustManager tm : tms) {
        if (tm instanceof X509TrustManager) {
            new X509TrustManagerExtensions((X509TrustManager) tm);
            return;
        }
    }
    fail();
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager)

Example 38 with TrustManager

use of javax.net.ssl.TrustManager in project camel by apache.

the class RabbitMQComponent method createEndpoint.

@Override
protected RabbitMQEndpoint createEndpoint(String uri, String remaining, Map<String, Object> params) throws Exception {
    URI host = new URI("http://" + remaining);
    String hostname = host.getHost();
    int portNumber = host.getPort();
    // We need to support the exchange to be "" the path is empty
    String exchangeName = "";
    if (host.getPath().trim().length() > 1) {
        exchangeName = host.getPath().substring(1);
    }
    // ConnectionFactory reference
    ConnectionFactory connectionFactory = resolveAndRemoveReferenceParameter(params, "connectionFactory", ConnectionFactory.class);
    @SuppressWarnings("unchecked") Map<String, Object> clientProperties = resolveAndRemoveReferenceParameter(params, "clientProperties", Map.class);
    TrustManager trustManager = resolveAndRemoveReferenceParameter(params, "trustManager", TrustManager.class);
    RabbitMQEndpoint endpoint;
    if (connectionFactory == null) {
        endpoint = new RabbitMQEndpoint(uri, this);
    } else {
        endpoint = new RabbitMQEndpoint(uri, this, connectionFactory);
    }
    endpoint.setHostname(hostname);
    endpoint.setPortNumber(portNumber);
    endpoint.setExchangeName(exchangeName);
    endpoint.setClientProperties(clientProperties);
    endpoint.setTrustManager(trustManager);
    setProperties(endpoint, params);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Creating RabbitMQEndpoint with host {}:{} and exchangeName: {}", new Object[] { endpoint.getHostname(), endpoint.getPortNumber(), endpoint.getExchangeName() });
    }
    HashMap<String, Object> args = new HashMap<>();
    args.putAll(IntrospectionSupport.extractProperties(params, ARG_PREFIX));
    endpoint.setArgs(args);
    HashMap<String, Object> argsCopy = new HashMap<>(args);
    // Combine the three types of rabbit arguments with their individual endpoint properties
    endpoint.getExchangeArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, EXCHANGE_ARG_PREFIX));
    endpoint.getQueueArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, QUEUE_ARG_PREFIX));
    endpoint.getBindingArgs().putAll(IntrospectionSupport.extractProperties(argsCopy, BINDING_ARG_PREFIX));
    return endpoint;
}
Also used : ConnectionFactory(com.rabbitmq.client.ConnectionFactory) HashMap(java.util.HashMap) URI(java.net.URI) TrustManager(javax.net.ssl.TrustManager)

Example 39 with TrustManager

use of javax.net.ssl.TrustManager in project cassandra by apache.

the class SSLFactory method createSSLContext.

@SuppressWarnings("resource")
public static SSLContext createSSLContext(EncryptionOptions options, boolean buildTruststore) throws IOException {
    FileInputStream tsf = null;
    FileInputStream ksf = null;
    SSLContext ctx;
    try {
        ctx = SSLContext.getInstance(options.protocol);
        TrustManager[] trustManagers = null;
        if (buildTruststore) {
            tsf = new FileInputStream(options.truststore);
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(options.algorithm);
            KeyStore ts = KeyStore.getInstance(options.store_type);
            ts.load(tsf, options.truststore_password.toCharArray());
            tmf.init(ts);
            trustManagers = tmf.getTrustManagers();
        }
        ksf = new FileInputStream(options.keystore);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(options.algorithm);
        KeyStore ks = KeyStore.getInstance(options.store_type);
        ks.load(ksf, options.keystore_password.toCharArray());
        if (!checkedExpiry) {
            for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements(); ) {
                String alias = aliases.nextElement();
                if (ks.getCertificate(alias).getType().equals("X.509")) {
                    Date expires = ((X509Certificate) ks.getCertificate(alias)).getNotAfter();
                    if (expires.before(new Date()))
                        logger.warn("Certificate for {} expired on {}", alias, expires);
                }
            }
            checkedExpiry = true;
        }
        kmf.init(ks, options.keystore_password.toCharArray());
        ctx.init(kmf.getKeyManagers(), trustManagers, null);
    } catch (Exception e) {
        throw new IOException("Error creating the initializing the SSL Context", e);
    } finally {
        FileUtils.closeQuietly(tsf);
        FileUtils.closeQuietly(ksf);
    }
    return ctx;
}
Also used : SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) TrustManager(javax.net.ssl.TrustManager) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Example 40 with TrustManager

use of javax.net.ssl.TrustManager in project camel by apache.

the class JettySolrFactory method installAllTrustingClientSsl.

private static void installAllTrustingClientSsl() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    // // Create a trust manager that does not validate certificate chains
    final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        @Override
        public void checkServerTrusted(final X509Certificate[] chain, final String authType) {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    final SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    SSLContext.setDefault(sslContext);
// // 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();
// HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory);
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) SSLContextBuilder(org.apache.http.conn.ssl.SSLContextBuilder) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

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