Search in sources :

Example 41 with TrustManager

use of javax.net.ssl.TrustManager in project CloudReader by youlookwhat.

the class HttpUtils method getUnsafeOkHttpClient.

public OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.readTimeout(20, TimeUnit.SECONDS);
        okBuilder.connectTimeout(10, TimeUnit.SECONDS);
        okBuilder.writeTimeout(20, TimeUnit.SECONDS);
        okBuilder.addInterceptor(new HttpHeadInterceptor());
        okBuilder.addInterceptor(getInterceptor());
        okBuilder.sslSocketFactory(sslSocketFactory);
        okBuilder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                //                    Log.d("HttpUtils", "==come");
                return true;
            }
        });
        return okBuilder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 42 with TrustManager

use of javax.net.ssl.TrustManager in project opennms by OpenNMS.

the class VmwareConfigBuilder method main.

public static void main(String[] args) throws ParseException {
    String hostname = null;
    String username = null;
    String password = null;
    String rrdRepository = null;
    final Options options = new Options();
    options.addOption("rrdRepository", true, "set rrdRepository path for generated config files, default: '/opt/opennms/share/rrd/snmp/'");
    final CommandLineParser parser = new PosixParser();
    final CommandLine cmd = parser.parse(options, args);
    @SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
    if (arguments.size() < 3) {
        usage(options, cmd);
        System.exit(1);
    }
    hostname = arguments.remove(0);
    username = arguments.remove(0);
    password = arguments.remove(0);
    if (cmd.hasOption("rrdRepository")) {
        rrdRepository = cmd.getOptionValue("rrdRepository");
    } else {
        rrdRepository = "/opt/opennms/share/rrd/snmp/";
    }
    TrustManager[] trustAllCerts = new TrustManager[] { new AnyServerX509TrustManager() };
    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {

        @Override
        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
    VmwareConfigBuilder vmwareConfigBuilder;
    vmwareConfigBuilder = new VmwareConfigBuilder(hostname, username, password);
    try {
        vmwareConfigBuilder.generateData(rrdRepository);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Options(org.apache.commons.cli.Options) PosixParser(org.apache.commons.cli.PosixParser) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyManagementException(java.security.KeyManagementException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) ParseException(org.apache.commons.cli.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) AnyServerX509TrustManager(org.opennms.core.utils.AnyServerX509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) CommandLine(org.apache.commons.cli.CommandLine) List(java.util.List) AnyServerX509TrustManager(org.opennms.core.utils.AnyServerX509TrustManager) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Example 43 with TrustManager

use of javax.net.ssl.TrustManager in project Tusky by Vavassor.

the class OkHttpUtils method enableHigherTlsOnPreLollipop.

private static OkHttpClient.Builder enableHigherTlsOnPreLollipop(OkHttpClient.Builder builder) {
    if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
        try {
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init((KeyStore) null);
            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
            if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
                throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
            }
            X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { trustManager }, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            builder.sslSocketFactory(new SSLSocketFactoryCompat(sslSocketFactory), trustManager);
        } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
            Log.e(TAG, "Failed enabling TLS 1.1 & 1.2. " + e.getMessage());
        }
    }
    return builder;
}
Also used : X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 44 with TrustManager

use of javax.net.ssl.TrustManager in project voltdb by VoltDB.

the class SSLConfiguration method createSslContext.

public static SSLContext createSslContext(SslConfig sslConfig) {
    if (sslConfig == null) {
        throw new IllegalArgumentException("sslConfig is null");
    }
    KeyManager[] keyManagers = null;
    TrustManager[] trustManagers = null;
    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        if (sslConfig.keyStorePath != null && sslConfig.keyStorePassword != null) {
            keyManagers = createKeyManagers(sslConfig.keyStorePath, sslConfig.keyStorePassword, sslConfig.keyStorePassword);
        }
        if (sslConfig.trustStorePath != null && sslConfig.trustStorePassword != null) {
            trustManagers = createTrustManagers(sslConfig.trustStorePath, sslConfig.trustStorePassword);
        }
        sslContext.init(keyManagers, trustManagers, new SecureRandom());
        return sslContext;
    } catch (IOException | NoSuchAlgorithmException | KeyStoreException | CertificateException | UnrecoverableKeyException | KeyManagementException ex) {
        throw new IllegalArgumentException("Failed to initialize SSL using " + sslConfig, ex);
    }
}
Also used : SecureRandom(java.security.SecureRandom) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyManager(javax.net.ssl.KeyManager)

Example 45 with TrustManager

use of javax.net.ssl.TrustManager in project java-chassis by ServiceComb.

the class SSLManager method createSSLContext.

public static SSLContext createSSLContext(SSLOption option, SSLCustom custom) {
    try {
        String keyStoreName = custom.getFullPath(option.getKeyStore());
        KeyManager[] keymanager;
        if (keyStoreName != null && new File(keyStoreName).exists()) {
            char[] keyStoreValue = custom.decode(option.getKeyStoreValue().toCharArray());
            KeyStore keyStore = KeyStoreUtil.createKeyStore(keyStoreName, option.getKeyStoreType(), keyStoreValue);
            keymanager = KeyStoreUtil.createKeyManagers(keyStore, keyStoreValue);
        } else {
            keymanager = null;
        }
        String trustStoreName = custom.getFullPath(option.getTrustStore());
        TrustManager[] trustManager;
        if (trustStoreName != null && new File(trustStoreName).exists()) {
            char[] trustStoreValue = custom.decode(option.getTrustStoreValue().toCharArray());
            KeyStore trustStore = KeyStoreUtil.createKeyStore(trustStoreName, option.getTrustStoreType(), trustStoreValue);
            trustManager = KeyStoreUtil.createTrustManagers(trustStore);
        } else {
            trustManager = new TrustManager[] { new TrustAllManager() };
        }
        TrustManager[] wrapped = new TrustManager[trustManager.length];
        for (int i = 0; i < trustManager.length; i++) {
            wrapped[i] = new TrustManagerExt((X509ExtendedTrustManager) trustManager[i], option, custom);
        }
        // ?: ssl context version
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keymanager, wrapped, new SecureRandom());
        return context;
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("NoSuchAlgorithmException." + e.getMessage());
    } catch (KeyManagementException e) {
        throw new IllegalArgumentException("KeyManagementException." + e.getMessage());
    }
}
Also used : X509ExtendedTrustManager(javax.net.ssl.X509ExtendedTrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) TrustManager(javax.net.ssl.TrustManager) X509ExtendedTrustManager(javax.net.ssl.X509ExtendedTrustManager) KeyManager(javax.net.ssl.KeyManager) File(java.io.File)

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