Java Examples for javax.net.ssl.TrustManager

The following java examples will help you to understand the usage of javax.net.ssl.TrustManager. These source code samples are taken from different open source projects.

Example 1
Project: blend4j-master  File: SslHacking.java View source code
public static void disableCertificateCheck() {
    javax.net.ssl.TrustManager x509 = new javax.net.ssl.X509TrustManager() {

        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
            return;
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
            return;
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("SSL");
        ctx.init(null, new javax.net.ssl.TrustManager[] { x509 }, null);
    } catch (java.security.GeneralSecurityException ex) {
    }
    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
}
Example 2
Project: jgalaxy-master  File: Ssl.java View source code
public static void disableCertificateCheck() {
    javax.net.ssl.TrustManager x509 = new javax.net.ssl.X509TrustManager() {

        public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
            return;
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
            return;
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("SSL");
        ctx.init(null, new javax.net.ssl.TrustManager[] { x509 }, null);
    } catch (java.security.GeneralSecurityException ex) {
    }
    // Create all-trusting host name verifier
    HostnameVerifier allHostsValid = new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
}
Example 3
Project: spritely-master  File: SSLBypass.java View source code
public static void bypassSSL(HttpClientBuilder builder) {
    // Imports: javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager
    try {
        // 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;
            }
        } };
        // 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
        builder.setSslcontext(sslContext);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Example 4
Project: find-sec-bugs-master  File: SecurityBypasser.java View source code
public static void destroyAllSSLSecurityForTheEntireVMForever() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new TrustAllManager() };
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new AllHosts());
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (final KeyManagementException e) {
        e.printStackTrace();
    }
}
Example 5
Project: molecule-master  File: Trust.java View source code
public static TrustManager allCertificates() {
    return new X509TrustManager() {

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

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

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    };
}
Example 6
Project: aws-toolkit-eclipse-master  File: CertificateUtils.java View source code
public static void disableCertValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
    }
}
Example 7
Project: email-master  File: TestTrustedSocketFactory.java View source code
@Override
public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    TrustManager[] trustManagers = new TrustManager[] { new VeryTrustingTrustManager() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    return sslSocketFactory.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
}
Example 8
Project: k-9-master  File: TestTrustedSocketFactory.java View source code
@Override
public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    TrustManager[] trustManagers = new TrustManager[] { new VeryTrustingTrustManager() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    return sslSocketFactory.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
}
Example 9
Project: redmine-java-api-master  File: NaiveSSLFactory.java View source code
/**
     * @return Return naive SSL socket factory (authorize any SSL/TSL host)
     */
public static SSLSocketFactory createNaiveSSLSocketFactory() {
    X509TrustManager manager = new NaiveX509TrustManager();
    SSLContext sslcontext = null;
    try {
        TrustManager[] managers = new TrustManager[] { manager };
        sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, managers, null);
    } catch (NoSuchAlgorithmExceptionKeyManagementException |  e) {
        e.printStackTrace();
    }
    return new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
Example 10
Project: android-signage-client-master  File: TrustManager.java View source code
/**
     * Create a trust manager that does not validate certificate chains
     * @return 
     */
public static HostnameVerifier overrideCertificateChainValidation() {
    try {
        javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[] { new X509TrustManager() {

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

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // return the all-trusting host verifier
        return allHostsValid;
    } catch (KeyManagementException e) {
        Log.e(TAG, e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, e.toString());
    }
    return null;
}
Example 11
Project: cirmlive-master  File: AllowAnySSL.java View source code
public void installPermissiveTrustmanager() {
    // Imports: javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager
    try {
        // 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;
            }
        } };
        // 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);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
Example 12
Project: AppWorkUtils-master  File: CryptServiceProvider.java View source code
@Override
protected TrustManager[] engineGetTrustManagers() {
    return new TrustManager[] { new X509TrustManager() {

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
}
Example 13
Project: AsyncOkHttp-master  File: CustomSSLSocketFactoryCreator.java View source code
public SSLSocketFactory create() {
    SSLSocketFactory sslSocketFactory = null;
    X509TrustManager tm = new X509TrustManager() {

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

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

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    try {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[] { tm }, null);
        sslSocketFactory = sc.getSocketFactory();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return sslSocketFactory;
}
Example 14
Project: bnd-master  File: HttpsUtil.java View source code
static void disableServerVerification(URLConnection connection) throws GeneralSecurityException {
    if (!(connection instanceof HttpsURLConnection))
        return;
    HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
        }
    } };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
    httpsConnection.setSSLSocketFactory(sslSocketFactory);
    HostnameVerifier trustAnyHost = new HostnameVerifier() {

        public boolean verify(String string, SSLSession session) {
            return true;
        }
    };
    httpsConnection.setHostnameVerifier(trustAnyHost);
}
Example 15
Project: cagrid2-master  File: TrustServiceJettySSLContextFactory.java View source code
@Override
protected TrustManager[] getTrustManagers(KeyStore trustStore, Collection<? extends CRL> crls) throws Exception {
    if (getTrustService() == null) {
        log.warn("A trust service was not specified, using the default trust managers");
        return super.getTrustManagers(trustStore, crls);
    } else if (getTrustService().getTrustManager() == null) {
        log.warn("The trust service did NOT provide a trust manager to use, using the default trust managers");
        return super.getTrustManagers(trustStore, crls);
    } else {
        return new TrustManager[] { getTrustService().getTrustManager() };
    }
}
Example 16
Project: checkstyle-idea-master  File: InsecureHTTPURLConfigurationLocation.java View source code
@NotNull
@Override
protected InputStream resolveFile() throws IOException {
    TrustManager[] trustAllCerts = new TrustManager[] { new AllTrustingTrustManager() };
    try {
        final SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ignored) {
    }
    return super.resolveFile();
}
Example 17
Project: geoserver-master  File: SSLUtilities.java View source code
public static void registerKeyStore(String keyStoreName) {
    try {
        ClassLoader classLoader = SSLUtilities.class.getClassLoader();
        InputStream keyStoreInputStream = classLoader.getResourceAsStream(keyStoreName);
        if (keyStoreInputStream == null) {
            throw new FileNotFoundException("Could not find file named '" + keyStoreName + "' in the CLASSPATH");
        }
        // load the keystore
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(keyStoreInputStream, null);
        // add to known keystore
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(keystore);
        // default SSL connections are initialized with the keystore above
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustManagers, null);
        SSLContext.setDefault(sc);
    } catch (IOExceptionGeneralSecurityException |  e) {
        throw new RuntimeException(e);
    }
}
Example 18
Project: identity-sample-apps-master  File: SSLValidationDisabler.java View source code
public static void disableSSLValidation() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    } catch (GeneralSecurityException e) {
    }
}
Example 19
Project: karate-master  File: HttpUtils.java View source code
public static SSLContext getSslContext(String algorithm) {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

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

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

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    SSLContext ctx = null;
    if (algorithm == null) {
        algorithm = "TLS";
    }
    try {
        ctx = SSLContext.getInstance(algorithm);
        ctx.init(null, certs, new SecureRandom());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return ctx;
}
Example 20
Project: keystone-master  File: ConfigTest.java View source code
@Test
public void testUnequalConfigSsl() {
    Config firstConfig = Config.newConfig();
    try {
        SSLContext firstSslContext = SSLContext.getInstance("SSL");
        firstSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        firstConfig.withSSLContext(firstSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Config secondConfig = Config.newConfig();
    try {
        SSLContext secondSslContext = SSLContext.getInstance("SSL");
        secondSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        secondConfig.withSSLContext(secondSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNotEquals(firstConfig, secondConfig);
}
Example 21
Project: ldaptive-master  File: SslConfigPropertyInvoker.java View source code
@Override
protected Object convertValue(final Class<?> type, final String value) {
    Object newValue = value;
    if (type != String.class) {
        if (CredentialConfig.class.isAssignableFrom(type)) {
            if ("null".equals(value)) {
                newValue = null;
            } else {
                if (CredentialConfigParser.isCredentialConfig(value)) {
                    final CredentialConfigParser configParser = new CredentialConfigParser(value);
                    newValue = configParser.initializeType();
                } else if (PropertyValueParser.isConfig(value)) {
                    final PropertyValueParser configParser = new PropertyValueParser(value);
                    newValue = configParser.initializeType();
                } else {
                    newValue = instantiateType(SslConfig.class, value);
                }
            }
        } else if (TrustManager[].class.isAssignableFrom(type)) {
            newValue = createArrayTypeFromPropertyValue(TrustManager.class, value);
        } else if (HandshakeCompletedListener[].class.isAssignableFrom(type)) {
            newValue = createArrayTypeFromPropertyValue(HandshakeCompletedListener.class, value);
        } else {
            newValue = convertSimpleType(type, value);
        }
    }
    return newValue;
}
Example 22
Project: LittleProxy-mitm-master  File: MergeTrustManager.java View source code
private X509TrustManager defaultTrustManager(KeyStore trustStore) throws NoSuchAlgorithmException, KeyStoreException {
    String tma = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tma);
    tmf.init(trustStore);
    TrustManager[] trustManagers = tmf.getTrustManagers();
    for (TrustManager each : trustManagers) {
        if (each instanceof X509TrustManager) {
            return (X509TrustManager) each;
        }
    }
    throw new IllegalStateException("Missed X509TrustManager in " + Arrays.toString(trustManagers));
}
Example 23
Project: mylyn-redmine-connector-master  File: NaiveSSLFactory.java View source code
/**
     * @return Return naive SSL socket factory (authorize any SSL/TSL host)
     */
public static SSLSocketFactory createNaiveSSLSocketFactory() {
    X509TrustManager manager = new NaiveX509TrustManager();
    SSLContext sslcontext = null;
    try {
        TrustManager[] managers = new TrustManager[] { manager };
        sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, managers, null);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
Example 24
Project: neutron-master  File: ConfigTest.java View source code
@Test
public void testUnequalConfigSsl() {
    Config firstConfig = Config.newConfig();
    try {
        SSLContext firstSslContext = SSLContext.getInstance("SSL");
        firstSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        firstConfig.withSSLContext(firstSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Config secondConfig = Config.newConfig();
    try {
        SSLContext secondSslContext = SSLContext.getInstance("SSL");
        secondSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        secondConfig.withSSLContext(secondSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNotEquals(firstConfig, secondConfig);
}
Example 25
Project: openstack4j-master  File: ConfigTest.java View source code
@Test
public void testUnequalConfigSsl() {
    Config firstConfig = Config.newConfig();
    try {
        SSLContext firstSslContext = SSLContext.getInstance("SSL");
        firstSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        firstConfig.withSSLContext(firstSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Config secondConfig = Config.newConfig();
    try {
        SSLContext secondSslContext = SSLContext.getInstance("SSL");
        secondSslContext.init(null, new TrustManager[] { null }, new SecureRandom());
        secondConfig.withSSLContext(secondSslContext);
    } catch (Exception e) {
        e.printStackTrace();
    }
    Assert.assertNotEquals(firstConfig, secondConfig);
}
Example 26
Project: osgi-jax-rs-connector-master  File: ClientHelper.java View source code
public static SSLContext createSSLContext() {
    TrustManager[] certs = new TrustManager[] { new X509TrustManager() {

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

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

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // no content
        }
    } };
    SSLContext ctx = null;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, certs, new SecureRandom());
        return ctx;
    } catch (java.security.GeneralSecurityException shouldNotHappen) {
        throw new IllegalStateException(shouldNotHappen);
    }
}
Example 27
Project: redmineReport-master  File: NaiveSSLFactory.java View source code
/**
     * @return Return naive SSL socket factory (authorize any SSL/TSL host)
     */
public static SSLSocketFactory createNaiveSSLSocketFactory() {
    X509TrustManager manager = new NaiveX509TrustManager();
    SSLContext sslcontext = null;
    try {
        TrustManager[] managers = new TrustManager[] { manager };
        sslcontext = SSLContext.getInstance("SSL");
        sslcontext.init(null, managers, null);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
Example 28
Project: ssl_npn-master  File: SSLContextCreator.java View source code
public static SSLContext newContext() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
    KeyStore store = KeyStore.getInstance("PKCS12");
    FileInputStream stream = new FileInputStream("server.pkcs12");
    try {
        store.load(stream, "test123".toCharArray());
    } finally {
        stream.close();
    }
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(store, "test123".toCharArray());
    SSLContext context = SSLContext.getInstance("TLSv1.2", new sslnpn.net.ssl.internal.ssl.Provider());
    context.init(kmf.getKeyManagers(), new TrustManager[] { new NaiveTrustManager() }, new SecureRandom());
    return context;
}
Example 29
Project: zandy-master  File: WebDavTrust.java View source code
// Kudos and blame to http://stackoverflow.com/a/1201102/950790
public static void installAllTrustingCertificate() {
    // 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) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception ignored) {
    }
}
Example 30
Project: fiware-sdc-master  File: ConnectionSetup.java View source code
public HttpURLConnection createConnection(URL url) throws NoSuchAlgorithmException, KeyManagementException, IOException {
    // Install the all-trusting trust manager
    SSLContext sslctx = SSLContext.getInstance("SSL");
    javax.net.ssl.TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    sslctx.init(null, trustAllCerts, null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslctx.getSocketFactory());
    HostnameVerifier allHostsValid = new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    return con;
}
Example 31
Project: GlanceReader-master  File: TrustManager.java View source code
private static Pair<javax.net.ssl.TrustManager[], SSLContext> setupTrustManagement(Context context) {
    CertificateFactory cf = null;
    try {
        cf = CertificateFactory.getInstance("X.509");
        InputStream inStream = context.getResources().openRawResource(sCertResId);
        X509Certificate caCertificate = (X509Certificate) cf.generateCertificate(inStream);
        // Create a KeyStore containing our trusted CAs
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", caCertificate);
        // Create a TrustManager that trusts the CAs in our KeyStore
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
        // Create an SSLContext that uses our TrustManager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        return new Pair<javax.net.ssl.TrustManager[], SSLContext>(tmf.getTrustManagers(), sslContext);
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    return null;
}
Example 32
Project: JBossAS51-master  File: Context.java View source code
/*
    * Returns an initialized JSSE SSLContext that uses the KeyManagerFactory
    * and TrustManagerFactory objects encapsulated by a given JBossSX 
    * SecurityDomain.
    */
static SSLContext forDomain(SecurityDomain securityDomain) throws IOException {
    SSLContext sslCtx = null;
    try {
        sslCtx = SSLContext.getInstance("TLS");
        KeyManagerFactory keyMgr = securityDomain.getKeyManagerFactory();
        if (keyMgr == null)
            throw new IOException("KeyManagerFactory is null for security domain: " + securityDomain.getSecurityDomain());
        TrustManagerFactory trustMgr = securityDomain.getTrustManagerFactory();
        TrustManager[] trustMgrs = null;
        if (trustMgr != null)
            trustMgrs = trustMgr.getTrustManagers();
        sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
        return sslCtx;
    } catch (NoSuchAlgorithmException e) {
        log.error("Failed to get SSLContext for TLS algorithm", e);
        throw new IOException("Failed to get SSLContext for TLS algorithm");
    } catch (KeyManagementException e) {
        log.error("Failed to init SSLContext", e);
        throw new IOException("Failed to init SSLContext");
    } catch (SecurityException e) {
        log.error("Failed to init SSLContext", e);
        throw new IOException("Failed to init SSLContext");
    }
}
Example 33
Project: JBossAS_5_1_EDG-master  File: Context.java View source code
/*
    * Returns an initialized JSSE SSLContext that uses the KeyManagerFactory
    * and TrustManagerFactory objects encapsulated by a given JBossSX 
    * SecurityDomain.
    */
static SSLContext forDomain(SecurityDomain securityDomain) throws IOException {
    SSLContext sslCtx = null;
    try {
        sslCtx = SSLContext.getInstance("TLS");
        KeyManagerFactory keyMgr = securityDomain.getKeyManagerFactory();
        if (keyMgr == null)
            throw new IOException("KeyManagerFactory is null for security domain: " + securityDomain.getSecurityDomain());
        TrustManagerFactory trustMgr = securityDomain.getTrustManagerFactory();
        TrustManager[] trustMgrs = null;
        if (trustMgr != null)
            trustMgrs = trustMgr.getTrustManagers();
        sslCtx.init(keyMgr.getKeyManagers(), trustMgrs, null);
        return sslCtx;
    } catch (NoSuchAlgorithmException e) {
        log.error("Failed to get SSLContext for TLS algorithm", e);
        throw new IOException("Failed to get SSLContext for TLS algorithm");
    } catch (KeyManagementException e) {
        log.error("Failed to init SSLContext", e);
        throw new IOException("Failed to init SSLContext");
    } catch (SecurityException e) {
        log.error("Failed to init SSLContext", e);
        throw new IOException("Failed to init SSLContext");
    }
}
Example 34
Project: QuickBuild-Tray-Monitor-master  File: MainApp.java View source code
private static void trustAll() {
    TrustManager[] trustAllCerts = new TrustManager[1];
    trustAllCerts[0] = new TrustAllManager();
    SSLContext sc;
    try {
        sc = javax.net.ssl.SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, null);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {

        public boolean verify(String urlHostName, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}
Example 35
Project: seed-master  File: SSLLoader.java View source code
/**
     * Gets the {@link javax.net.ssl.TrustManager}s from the ssl TrustStore.
     *
     * @return an array of KeyManagers
     */
TrustManager[] getTrustManager(KeyStore trustStore) {
    try {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        return trustManagerFactory.getTrustManagers();
    } catch (NoSuchAlgorithmException e) {
        throw SeedException.wrap(e, CryptoErrorCode.ALGORITHM_CANNOT_BE_FOUND);
    } catch (KeyStoreException e) {
        throw SeedException.wrap(e, CryptoErrorCode.UNEXPECTED_EXCEPTION);
    }
}
Example 36
Project: advanced-networking-master  File: CustomSSLSocketFactory.java View source code
public static SSLSocketFactory getInstance() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, KeyManagementException, IOException {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        String trustStore = System.getProperty("javax.net.ssl.trustStore");
        String trustStorePassword = System.getProperty("javax.net.ssl.trustStorePassword");
        if (trustStore == null || trustStorePassword == null) {
            throw new IOException("javax.net.ssl.trustStore/javax.net.ssl.trustStorePassword property - not set");
        }
        FileInputStream keystoreStream = new FileInputStream(trustStore);
        try {
            keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(keystoreStream, trustStorePassword.toCharArray());
        } finally {
            keystoreStream.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    trustManagerFactory.init(keystore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);
    SSLContext.setDefault(sslContext);
    return new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
}
Example 37
Project: AisenWeiBo-master  File: HttpsUtility.java View source code
public OkHttpClient getOkHttpClient() {
    if (mOKHttpClient == null) {
        try {
            mOKHttpClient = new OkHttpClient();
            mOKHttpClient.setConnectTimeout(GlobalContext.CONN_TIMEOUT, TimeUnit.MILLISECONDS);
            mOKHttpClient.setReadTimeout(GlobalContext.READ_TIMEOUT, TimeUnit.MILLISECONDS);
            TrustManager tm = new X509TrustManager() {

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

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

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { tm }, null);
            mOKHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    return mOKHttpClient;
}
Example 38
Project: Android-Backup-master  File: AllTrustedSocketFactory.java View source code
@Override
public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    TrustManager[] trustManagers = new TrustManager[] { new InsecureX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);
    SSLSocketFactory socketFactory = sslContext.getSocketFactory();
    Socket trustedSocket;
    if (socket == null) {
        trustedSocket = socketFactory.createSocket();
    } else {
        trustedSocket = socketFactory.createSocket(socket, host, port, true);
    }
    return trustedSocket;
}
Example 39
Project: android-sdk-sources-for-api-level-23-master  File: X509TrustManagerExtensionsTest.java View source code
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();
}
Example 40
Project: android-security-master  File: HttpClientProvider.java View source code
private static void setupTls(OkHttpClient.Builder builder) {
    try {
        TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        factory.init((KeyStore) null);
        for (TrustManager trustManager : factory.getTrustManagers()) {
            if (trustManager instanceof X509TrustManager) {
                builder.sslSocketFactory(new Tls12SslSocketFactory(), (X509TrustManager) trustManager);
                break;
            }
        }
    } catch (GeneralSecurityException e) {
        Log.e(TAG, "Failed to initialize SSL Socket Factory", e);
    }
}
Example 41
Project: Android-Templates-And-Utilities-master  File: SelfSignedSSLUtility.java View source code
public static SSLContext createSSLContext() throws GeneralSecurityException {
    KeyStore keyStore = loadKeyStore();
    SelfSignedTrustManager selfSignedTrustManager = new SelfSignedTrustManager(keyStore);
    TrustManager[] tms = new TrustManager[] { selfSignedTrustManager };
    KeyManager[] kms = null;
    KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kmf.init(keyStore, ExampleConfig.SSL_KEYSTORE_PASSWORD.toCharArray());
    kms = kmf.getKeyManagers();
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(kms, tms, new SecureRandom());
    return context;
}
Example 42
Project: android-tutorials-glide-master  File: UnsafeOkHttpClient.java View source code
public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        // 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();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setProtocols(Arrays.asList(Protocol.HTTP_1_1));
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 43
Project: AndroidSource-master  File: SslSocketFactory.java View source code
private static SSLContext createSSLContext(InputStream keyStore, String keyStorePassword) throws GeneralSecurityException {
    SSLContext sslcontext = null;
    try {
        sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { new SsX509TrustManager(keyStore, keyStorePassword) }, null);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    } catch (KeyManagementException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    }
    return sslcontext;
}
Example 44
Project: android_frameworks_base-master  File: X509TrustManagerExtensionsTest.java View source code
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();
}
Example 45
Project: android_volley_examples-master  File: SslSocketFactory.java View source code
private static SSLContext createSSLContext(InputStream keyStore, String keyStorePassword) throws GeneralSecurityException {
    SSLContext sslcontext = null;
    try {
        sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, new TrustManager[] { new SsX509TrustManager(keyStore, keyStorePassword) }, null);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    } catch (KeyManagementException e) {
        throw new IllegalStateException("Failure initializing default SSL context", e);
    }
    return sslcontext;
}
Example 46
Project: AppUpdate-master  File: TrustAllCertificates.java View source code
public static void install() {
    try {
        TrustAllCertificates trustAll = new TrustAllCertificates();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { trustAll }, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(trustAll);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Failed setting up all thrusting certificate manager.", e);
    } catch (KeyManagementException e) {
        throw new RuntimeException("Failed setting up all thrusting certificate manager.", e);
    }
}
Example 47
Project: audit-master  File: MainActivity.java View source code
private void GetHttps() {
    String https = "https://mail.qq.com/cgi-bin/loginpage";
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, new TrustManager[] { new MyTrustManager() }, new SecureRandom());
        HttpsURLConnection conn = (HttpsURLConnection) new URL(https).openConnection();
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.connect();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) sb.append(line);
        Log.e("testcaseLog", sb.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Example 48
Project: caelum-stella-master  File: CertificateAndPrivateKey.java View source code
public void enableSSLForServer(InputStream serverCertificateFile, String password) {
    try {
        KeyStore trustStore = KeyStore.getInstance("JKS");
        trustStore.load(serverCertificateFile, password.toCharArray());
        String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(defaultAlgorithm);
        trustManagerFactory.init(trustStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        KeyManager[] keyManagers = { new HSKeyManager(certificate, privateKey) };
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyManagers, trustManagers, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 49
Project: cas-master  File: SimpleHttpClientTests.java View source code
private static SSLConnectionSocketFactory getFriendlyToAllSSLSocketFactory() throws Exception {
    final TrustManager trm = new X509TrustManager() {

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

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

        @Override
        public void checkServerTrusted(final X509Certificate[] certs, final String authType) {
        }
    };
    final SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, new TrustManager[] { trm }, null);
    return new SSLConnectionSocketFactory(sc, new NoopHostnameVerifier());
}
Example 50
Project: cats-master  File: SSLUtil.java View source code
/**
     * to disable the certificate validation.
     * 
     */
public static void disableCertificateValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 51
Project: Cliques-master  File: FakeX509TrustManager.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new FakeX509TrustManager() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
Example 52
Project: cloudbees-api-client-master  File: TrustAllSocketFactory.java View source code
public static SSLSocketFactory create() {
    try {
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {

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

            public void checkServerTrusted(X509Certificate[] x509Certificates, String authType) throws CertificateException {
                if (LOGGER.isLoggable(FINE)) {
                    LOGGER.fine("Got the certificate: " + Arrays.asList(x509Certificates));
                }
            // TODO: define a mechanism for users to accept a certificate from UI
            //                    try {
            //                        CertificateUtil.validatePath(Arrays.asList(x509Certificates));
            //                    } catch (GeneralSecurityException e) {
            //                        e.printStackTrace();
            //                    }
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        return context.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        throw new Error(e);
    } catch (KeyManagementException e) {
        throw new Error(e);
    }
}
Example 53
Project: cloudbreak-master  File: CertificateTrustManager.java View source code
public static SSLContext sslContext() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            LOGGER.info("accept all issuer");
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            LOGGER.info("checkClientTrusted");
        // Trust everything
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            LOGGER.info("checkServerTrusted");
        // Trust everything
        }
    } };
    try {
        // Install the all-trusting trust manager
        SSLContext sc = SslConfigurator.newInstance().createSSLContext();
        sc.init(null, trustAllCerts, new SecureRandom());
        LOGGER.warn("Trust all SSL cerificates has been installed");
        return sc;
    } catch (KeyManagementException e) {
        LOGGER.error(e.getMessage(), e);
        throw new RuntimeException("F", e);
    }
}
Example 54
Project: Composer-Java-Bindings-master  File: Downloader.java View source code
private void registerSSLContext(HttpClient client) throws Exception {
    X509TrustManager tm = new ComposerTrustManager();
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = client.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, ssf));
}
Example 55
Project: cpush-apns-master  File: SecureSslContextFactory.java View source code
public static SSLContext getSSLContext(Credentials conf) {
    SSLContext clientContext = CLIENT_CONTEXT.get(conf);
    if (clientContext == null) {
        try {
            String algorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
            if (algorithm == null) {
                algorithm = "SunX509";
            }
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            keyStore.load(new ByteArrayInputStream(conf.getCertification()), conf.getPassword().toCharArray());
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
            kmf.init(keyStore, conf.getPassword().toCharArray());
            clientContext = SSLContext.getInstance(PROTOCOL);
            clientContext.init(kmf.getKeyManagers(), new TrustManager[] { new X509TrustManager() {

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

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

                @Override
                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    throw new CertificateException("Client is not trusted.");
                }
            } }, null);
            CLIENT_CONTEXT.putIfAbsent(conf, clientContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return clientContext;
}
Example 56
Project: DavidWebb-master  File: TestWebb_Ssl.java View source code
public void testHttpsInvalidCertificateAndHostnameIgnore() throws Exception {
    webb.setBaseUri(null);
    TrustManager[] trustAllCerts = new TrustManager[] { new AlwaysTrustManager() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
    webb.setSSLSocketFactory(sslContext.getSocketFactory());
    webb.setHostnameVerifier(new TrustingHostnameVerifier());
    Response<Void> response = webb.get("https://localhost:13003/").asVoid();
    assertTrue(response.isSuccess() || response.getStatusCode() == 301);
}
Example 57
Project: deepnighttwo-master  File: CategoryYank.java View source code
/**
	 * @param args
	 * @throws Exception
	 */
public static void main(String[] args) throws Exception {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        ;
    }
    // TODO Auto-generated method stub
    getPV("");
}
Example 58
Project: ecf-master  File: HttpClientDefaultSSLSocketFactoryModifier.java View source code
public synchronized SSLContext getSSLContext(String protocols) {
    SSLContext rtvContext = null;
    if (protocols != null) {
        //$NON-NLS-1$
        String protocolNames[] = StringUtils.split(protocols, ",");
        for (int i = 0; i < protocolNames.length; i++) {
            try {
                rtvContext = SSLContext.getInstance(protocolNames[i]);
                sslContext.init(null, new TrustManager[] { new HttpClientSslTrustManager() }, null);
                break;
            } catch (Exception e) {
            }
        }
    }
    return rtvContext;
}
Example 59
Project: egdownloader-master  File: HttpsUtils.java View source code
private static HttpsURLConnection getHttpsConnection(String urlStr, Proxy proxy) throws IOException, NoSuchAlgorithmException, KeyManagementException {
    URL url = new URL(urlStr);
    HttpsURLConnection conn = null;
    if (proxy != null) {
        conn = (HttpsURLConnection) url.openConnection(proxy);
    } else {
        conn = (HttpsURLConnection) url.openConnection();
    }
    // ä¸?验è¯?æœ?务器主机å??å’Œè¯?书
    conn.setHostnameVerifier(new IgnoreHostnameVerifier());
    TrustManager[] tm = { new X509TrustManager4Portal() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tm, null);
    SSLSocketFactory ssf = sslContext.getSocketFactory();
    conn.setSSLSocketFactory(ssf);
    return conn;
}
Example 60
Project: flexmls_api4j-master  File: ConnectionApacheHttps.java View source code
@Override
protected final void resetChild() {
    try {
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { new FullTrustManager() }, null);
        SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Scheme https = new Scheme("https", SSL_PORT, sf);
        getClient().getConnectionManager().getSchemeRegistry().register(https);
        setHost(new HttpHost(getConfig().getEndpoint(), SSL_PORT, "https"));
    } catch (Exception e) {
        logger.error("Failed to setup SSL authentication for the client (https disabled).", e);
    }
}
Example 61
Project: Git.NB-master  File: FakeX509TrustManager.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new FakeX509TrustManager() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
Example 62
Project: hammock-master  File: SSLBypass.java View source code
static void disableSSLChecks() {
    HttpsURLConnection.setDefaultHostnameVerifier(( s,  sslSession) -> true);
    try {
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] c, String s) {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] c, String s) {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, SECURE_RANDOM);
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    } catch (GeneralSecurityException gse) {
        throw new RuntimeException(gse.getMessage());
    }
}
Example 63
Project: hudson_plugins-master  File: TrustAllSocketFactory.java View source code
@Override
protected void initFactory() throws IOException {
    try {
        SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, new TrustManager[] { new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (NoSuchAlgorithmException e) {
        throw new Error(e);
    } catch (KeyManagementException e) {
        throw new Error(e);
    }
}
Example 64
Project: ijoomer-adv-sdk-master  File: TrustManagerManipulator.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new TrustManagerManipulator() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
Example 65
Project: IliConnect-master  File: HttpsClient.java View source code
public static HttpClient createHttpsClient(HttpClient client) {
    X509TrustManager tm = new X509TrustManager() {

        public X509Certificate[] getAcceptedIssuers() {
            // TODO Auto-generated method stub
            return null;
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // TODO Auto-generated method stub				
        }

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        // TODO Auto-generated method stub
        }
    };
    SSLContext ctx;
    try {
        ctx = SSLContext.getInstance("TLS");
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new MySSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = client.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, client.getParams());
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return null;
}
Example 66
Project: imok_android-master  File: DisableSSLCertificateCheckUtil.java View source code
/**
     * Disable trust checks for SSL connections.
     */
public static void disableChecks() throws NoSuchAlgorithmException, KeyManagementException {
    try {
        new URL("https://0.0.0.0/").getContent();
    } catch (IOException e) {
    }
    try {
        SSLContext sslc;
        sslc = SSLContext.getInstance("TLS");
        TrustManager[] trustManagerArray = { new NullX509TrustManager() };
        sslc.init(null, trustManagerArray, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Example 67
Project: instantcom-mm7-master  File: X509TrustManagerImpl.java View source code
private void initDefaultTrustManager() throws Exception {
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(certificateTrustStore);
    TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    for (TrustManager trustManager : trustManagers) {
        if (trustManager instanceof X509TrustManager) {
            defaultTrustManager = (X509TrustManager) trustManager;
            break;
        }
    }
}
Example 68
Project: jira-rest-client-master  File: HttpConnectionUtil.java View source code
public static void disableSslVerification() {
    try {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
}
Example 69
Project: joreman-master  File: HTTPHelper.java View source code
public static SSLContext createTrustfulSSLContex() {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        return ctx;
    } catch (Exception ex) {
        logger.error("Error while creating SSL Context", ex);
        return null;
    }
}
Example 70
Project: keratin-irc-master  File: TrustAllTrustManager.java View source code
/**
     * Get a SSLSocketFactory using this X509TrustManager.
     */
public static synchronized SSLSocketFactory getSSLSocketFactory() {
    if (trustAllSocketFactory == null) {
        try {
            KeyManager[] km = new KeyManager[0];
            TrustManager[] tm = new TrustManager[] { new TrustAllTrustManager() };
            SSLContext context = SSLContext.getInstance("SSL");
            context.init(km, tm, new SecureRandom());
            trustAllSocketFactory = (SSLSocketFactory) context.getSocketFactory();
        } catch (KeyManagementExceptionNoSuchAlgorithmException |  e) {
            Logger.error(e, "Error creating custom SSLSocketFactory");
            throw new RuntimeException("Error creating custom SSLSocketFactory", e);
        }
    }
    return trustAllSocketFactory;
}
Example 71
Project: keycloak-master  File: SslUtil.java View source code
public static SSLContext createSSLContext(final KeyStore keyStore, String password, final KeyStore trustStore) throws Exception {
    KeyManager[] keyManagers;
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    keyManagerFactory.init(keyStore, password.toCharArray());
    keyManagers = keyManagerFactory.getKeyManagers();
    TrustManager[] trustManagers = null;
    if (trustStore != null) {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        trustManagers = trustManagerFactory.getTrustManagers();
    }
    SSLContext sslContext;
    sslContext = SSLContext.getInstance("TLS");
    sslContext.init(keyManagers, trustManagers, null);
    return sslContext;
}
Example 72
Project: loli.io-master  File: HttpsClientFactory.java View source code
public static org.apache.http.client.HttpClient getInstance() {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

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

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

            public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
        HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);
        return HttpClients.custom().setConnectionManager(cm).build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
Example 73
Project: LuckyMonkey-master  File: AllCertificatesAndHostsTruster.java View source code
public static void apply() {
    final TrustManager[] trustAllCerts = new TrustManager[] { new AllCertificatesAndHostsTruster() };
    try {
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    } catch (Exception e) {
        LOGGER.log(Level.INFO, SSL_FAILED);
    }
}
Example 74
Project: ManalithBot-master  File: UrlUtils.java View source code
/**
	 * SSL 요청시 호스트를 따지지 않고 접�하�� 설정한다.
	 * https://github.com/mmx900/ManalithBot/issues/100 참고. TODO optional
	 * 
	 * @throws Exception
	 */
public static void setTrustAllHosts() throws Exception {
    // ��서를 검�하지 않는다.
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    SSLContext context = SSLContext.getInstance("SSL");
    context.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    // 호스트명� 검�하지 않는다.
    HttpsURLConnection.setDefaultHostnameVerifier(( hostname,  session) -> true);
}
Example 75
Project: Mineshafter-Launcher-master  File: GameStarter.java View source code
private static void bypassTls() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };
    SSLContext sc;
    try {
        sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
}
Example 76
Project: mobilecloud-15-master  File: UnsafeHttpsClient.java View source code
public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        // 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();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 77
Project: monsiaj-master  File: JarVerifier.java View source code
public static boolean verify(JarFile jar) throws Exception {
    X509Certificate[] certs = null;
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init((KeyStore) null);
    for (TrustManager trustManager : trustManagerFactory.getTrustManagers()) {
        if (trustManager instanceof X509TrustManager) {
            X509TrustManager x509TrustManager = (X509TrustManager) trustManager;
            certs = x509TrustManager.getAcceptedIssuers();
        }
    }
    boolean result = false;
    Enumeration<JarEntry> entries = jar.entries();
    while (entries.hasMoreElements()) {
        JarEntry entry = entries.nextElement();
        try {
            InputStream iis = jar.getInputStream(entry);
        } catch (SecurityException se) {
            return false;
        }
        if (verifyCert(entry.getCertificates(), certs)) {
            result = true;
        }
    }
    return result;
}
Example 78
Project: neembuu-uploader-master  File: SSLUtils.java View source code
/**
     * This static method disable the Validation Certificate. Use it for
     * <b>HttpURLConnection</b>.
     */
public static void disableCertificateValidation() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    } };
    // Ignore differences between given hostname and certificate hostname
    HostnameVerifier hv = new HostnameVerifier() {

        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(hv);
    } catch (Exception e) {
    }
}
Example 79
Project: ninuxmobile-master  File: DataLoader.java View source code
public HttpResponse secureLoadData(String url) throws ClientProtocolException, IOException, NoSuchAlgorithmException, KeyManagementException, URISyntaxException, KeyStoreException, UnrecoverableKeyException {
    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, new TrustManager[] { new CustomX509TrustManager() }, new SecureRandom());
    HttpClient client = new DefaultHttpClient();
    SSLSocketFactory ssf = new CustomSSLSocketFactory(ctx);
    ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = client.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", ssf, 443));
    DefaultHttpClient sslClient = new DefaultHttpClient(ccm, client.getParams());
    HttpGet get = new HttpGet(new URI(url));
    HttpResponse response = sslClient.execute(get);
    return response;
}
Example 80
Project: OpenETSMobile2-master  File: AppletsApiCalendarRequest.java View source code
@Override
public EventList loadDataFromNetwork() throws Exception {
    String url = context.getString(R.string.applets_api_calendar, "ets", startDate, endDate);
    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 {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier allHostsValid = new HostnameVerifier() {

        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    return getRestTemplate().getForObject(url, EventList.class);
}
Example 81
Project: opg-master  File: HTTPSecureWrapper.java View source code
public static DefaultHttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        return null;
    }
}
Example 82
Project: ORCID-Source-master  File: DevJerseyClientConfig.java View source code
private SSLContext createSslContext() {
    try {
        // DANGER!!! Accepts all certs!
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext ssl = SSLContext.getInstance("TLS");
        ssl.init(null, trustAllCerts, new SecureRandom());
        return ssl;
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
}
Example 83
Project: platform_frameworks_base-master  File: X509TrustManagerExtensionsTest.java View source code
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();
}
Example 84
Project: POCenter-master  File: UnsafeOkHttpUtils.java View source code
/**
     * don't verify certificate
     * @return
     */
public static OkHttpClient getClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[] {};
            }
        } };
        // 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 javax.net.ssl.SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.sslSocketFactory(sslSocketFactory);
        builder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 85
Project: portal-de-servicos-master  File: PiwikConfig.java View source code
@SneakyThrows
private void trustSelfSignedSSL() {
    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        // não implementa nenhuma checagem
        }

        @Override
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        // não implementa nenhuma checagem
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    };
    ctx.init(null, new TrustManager[] { tm }, null);
    SSLContext.setDefault(ctx);
}
Example 86
Project: Posterize-Android-master  File: AllCertificatesAndHostsTruster.java View source code
public static void apply() {
    final TrustManager[] trustAllCerts = new TrustManager[] { new AllCertificatesAndHostsTruster() };
    try {
        final SSLContext context = SSLContext.getInstance("SSL");
        context.init(null, trustAllCerts, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
    } catch (Exception e) {
        LOGGER.log(Level.INFO, SSL_FAILED);
    }
}
Example 87
Project: programming-cloud-services-for-android-master  File: UnsafeHttpsClient.java View source code
public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };
        // 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();
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Example 88
Project: property-db-master  File: X509TrustManagerExtensionsTest.java View source code
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();
}
Example 89
Project: Qmusic-master  File: QMusicHTTPSTrustManager.java View source code
public static SSLContext getSSLContext() {
    SSLContext context = null;
    try {
        context = // or SSL
        SSLContext.getInstance(// or SSL
        "TLS");
        TrustManager[] trustManagers = null;
        // Option 1:
        // KeyStore trustStore =
        // KeyStore.getInstance(KeyStore.getDefaultType());
        // // load the CA here
        // trustStore.load(null, null);
        // String algorithm = TrustManagerFactory.getDefaultAlgorithm();
        // TrustManagerFactory tmf =
        // TrustManagerFactory.getInstance(algorithm);
        // tmf.init(trustStore);
        // trustManagers = tmf.getTrustManagers();
        // Option 2:
        trustManagers = new TrustManager[] { new QMusicHTTPSTrustManager() };
        context.init(null, trustManagers, null);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return context;
}
Example 90
Project: QTPaySDK-Android-master  File: HttpsTrustManager.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new HttpsTrustManager() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
Example 91
Project: rsimulator-master  File: SecurityHandler.java View source code
public void initialize() {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

        public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
        }
    } };
    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {

            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    } catch (Exception e) {
        log.error("Can not initialize.", e);
    }
}
Example 92
Project: scalampp-master  File: SecurityHelper.java View source code
/**
     * Use this method for the TLS negotiation step referred in the XMPP Core RFC (Section 5.3).
     * Use the streams of the resulting socket to proceed with communication.
     * @param socket Your currently open socket to the destiny XMPP server
     * @param server Your destiny XMPP server
     * @param port The port of the destiny XMPP server 
     * @return A secure socket
     * @throws NoSuchAlgorithmException
     * @throws KeyManagementException
     * @throws IOException
     */
public static Socket executeTLSNegotiation(Socket socket, String server, int port) throws NoSuchAlgorithmException, KeyManagementException, IOException {
    Socket result;
    SSLContext context = SSLContext.getInstance("TLS");
    // Verify certificate presented by the server
    context.init(// KeyManager not required
    null, new javax.net.ssl.TrustManager[] { new ServerTrustManager(server, new ConnectionConfiguration(server, port)) }, new java.security.SecureRandom());
    Socket plain = socket;
    // Secure the plain connection
    result = context.getSocketFactory().createSocket(plain, plain.getInetAddress().getHostName(), plain.getPort(), true);
    result.setSoTimeout(0);
    result.setKeepAlive(true);
    // Proceed to do the handshake
    ((SSLSocket) result).startHandshake();
    return result;
}
Example 93
Project: scdl-master  File: SecureSoundcloudApiQueryImpl.java View source code
/**
	 * Applies the pinning SSL manager to the connection.
	 * 
	 * @param connection
	 */
private void pinSSLConnection(final HttpURLConnection connection) {
    if (!(connection instanceof HttpsURLConnection)) {
        throw new IllegalStateException("Not an SSL connection!");
    }
    final TrustManager[] trustManagers = getPinningTrustManagers();
    final SSLContext sslContext;
    try {
        sslContext = SSLContext.getInstance("TLS");
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    }
    try {
        sslContext.init(null, trustManagers, null);
    } catch (final KeyManagementException e) {
        throw new IllegalStateException(e);
    }
    ((HttpsURLConnection) connection).setSSLSocketFactory(sslContext.getSocketFactory());
}
Example 94
Project: selfoss-android-master  File: SelfossApiRequestFactory.java View source code
private void trustAllHosts() {
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { this }, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Example 95
Project: Sgk-Saglik-Provizyon-App-master  File: Utils.java View source code
public static HttpClient getHttpsClient(HttpClient client) {
    try {
        X509TrustManager x509TrustManager = new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            // TODO Auto-generated method stub
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                // TODO Auto-generated method stub
                return null;
            }
        };
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { x509TrustManager }, null);
        SSLSocketFactory sslSocketFactory = new ExSSLSocketFactory(sslContext);
        sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager clientConnectionManager = client.getConnectionManager();
        SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
        schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
        return new DefaultHttpClient(clientConnectionManager, client.getParams());
    } catch (Exception ex) {
        return null;
    }
}
Example 96
Project: sissi-master  File: CertificateContextBuilder.java View source code
private TrustManager[] getTrustManagers(Certificate trust) throws Exception {
    TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
    InputStream certificate = trust.getFile().openStream();
    try {
        KeyStore ks = KeyStore.getInstance(this.keystore);
        ks.load(certificate, trust.getPassword());
        factory.init(ks);
    } finally {
        IOUtil.closeQuietly(certificate);
    }
    return factory.getTrustManagers();
}
Example 97
Project: SmartCommunity-master  File: VolleyX509TrustManager.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            // TODO Auto-generated method stub  
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new VolleyX509TrustManager() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}
Example 98
Project: sms-backup-plus-master  File: AllTrustedSocketFactory.java View source code
@Override
public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias) throws NoSuchAlgorithmException, KeyManagementException, MessagingException, IOException {
    TrustManager[] trustManagers = new TrustManager[] { new InsecureX509TrustManager() };
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, trustManagers, null);
    SSLSocketFactory socketFactory = sslContext.getSocketFactory();
    Socket trustedSocket;
    if (socket == null) {
        trustedSocket = socketFactory.createSocket();
    } else {
        trustedSocket = socketFactory.createSocket(socket, host, port, true);
    }
    return trustedSocket;
}
Example 99
Project: stratos-master  File: WebClientWrapper.java View source code
public static HttpClient wrapClient(HttpClient base) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {

            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new TrustManager[] { tm }, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", ssf, 443));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        return null;
    }
}
Example 100
Project: stubby4j-master  File: FakeX509TrustManager.java View source code
public void allowAllSSL() throws KeyManagementException, NoSuchAlgorithmException {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(final String hostname, final SSLSession session) {
            return true;
        }
    });
    final SSLContext defaultSslContext = SSLContext.getInstance("SSL");
    defaultSslContext.init(new KeyManager[] {}, new TrustManager[] { this }, null);
    SSLContext.setDefault(defaultSslContext);
    HttpsURLConnection.setDefaultSSLSocketFactory(defaultSslContext.getSocketFactory());
}
Example 101
Project: study-master  File: HTTPSTrustManager.java View source code
public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            // TODO Auto-generated method stub
            return true;
        }
    });
    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[] { new HTTPSTrustManager() };
    }
    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
}