Example usage for javax.net.ssl X509TrustManager X509TrustManager

List of usage examples for javax.net.ssl X509TrustManager X509TrustManager

Introduction

In this page you can find the example usage for javax.net.ssl X509TrustManager X509TrustManager.

Prototype

X509TrustManager

Source Link

Usage

From source file:com.intel.cosbench.client.http.HttpClientUtil.java

@SuppressWarnings({ "deprecation" })
private static SSLSocketFactory createSSLSocketFactory() {
    try {/*w w  w . ja  v a  2 s  . co  m*/
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
            }

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

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };
        ctx.init(null, new X509TrustManager[] { tm }, null);
        String[] enabled = { "SSL_RSA_WITH_NULL_MD5", "SSL_RSA_WITH_NULL_SHA" };
        ctx.createSSLEngine().setEnabledCipherSuites(enabled);

        SSLSocketFactory ssf = new SSLSocketFactory(ctx);
        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        return ssf;
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.fujitsu.dc.client.http.InsecureSSLSocketFactory.java

/**
 * This is the parameterized constructor used to initialize fields.
 * @param truststore ./*w w  w . j  a  v a2s  .  c o  m*/
 * @throws NoSuchAlgorithmException NoSuchAlgorithmException
 * @throws KeyManagementException KeyManagementException
 * @throws KeyStoreException KeyStoreException
 * @throws UnrecoverableKeyException UnrecoverableKeyException
 */
public InsecureSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);
    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.init(null, new TrustManager[] { tm }, null);
}

From source file:tk.egsf.ddns.JSON_helper.java

public static String getJsonUrl(String URL) {
    String ret = "";

    String https_url = URL;
    URL url;/*from   w ww.  j av  a  2 s .  c om*/
    try {

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }

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

            @Override
            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());

        url = new URL(https_url);
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String input;
        while ((input = br.readLine()) != null) {
            ret += input;
        }
        br.close();

        System.out.println(ret);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ConnectException e) {
        Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, "Exgotou o tempo de resposta");
        System.out.println("");
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
    } catch (KeyManagementException ex) {
        Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException e) {
        e.printStackTrace();

    }

    return ret;
}

From source file:wptools.lib.Misc.java

/**
 * Bypass the normal SSL certificate authentication. If the passed
 * fingerprint is null, bypasses all authentication (dangerous).
 * Else trust anything whose chain contains a cert with the specified
 * fingerprint.//from w ww  .  ja va2s.co m
 * @param fing      Fingerprint
 */
public static void bypassSslAuth(final byte[] fing) {
    // Determine fingerprint type from its length
    final String type;
    if (fing == null) {
        type = null;
    } else {
        switch (fing.length) {
        case MD5_LEN:
            type = "MD5";
            break;
        case SHA1_LEN:
            type = "SHA-1";
            break;
        case SHA256_LEN:
            type = "SHA-256";
            break;
        default:
            throw new IllegalArgumentException("Invalid hash.");
        }
    }

    // Create a trust manager
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

        private void matchFing(X509Certificate[] certs) throws CertificateException {
            if (fing == null)
                return;
            MessageDigest md = null;
            try {
                md = MessageDigest.getInstance(type);
            } catch (NoSuchAlgorithmException e) {
                throw new CertificateException(e);
            }
            for (X509Certificate cert : certs) {
                md.reset();
                if (Arrays.equals(md.digest(cert.getEncoded()), fing))
                    return;
            }
            throw new CertificateException("No matching fingerprint found.");
        }
    } };

    // Install the trust manager
    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    // Create empty HostnameVerifier
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    };

    try {
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:org.jsnap.request.SSLSocketFactory.java

private SSLSocketFactory(boolean trustAll) {
    sf = null;/*  w w w  .j  a  va 2  s . c  o  m*/
    if (trustAll) {
        // 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) {
            }
        } };
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);
            sf = sc.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        } catch (KeyManagementException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        }
    } else {
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, null, null);
            sf = sc.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        } catch (KeyManagementException e) {
            Logger.getLogger(SSLSocketFactory.class).log(Level.WARN, "Unable to instantiate SSLSocketFactory",
                    e);
        }
    }
}

From source file:net.vexelon.myglob.utils.TrustAllSocketFactory.java

public TrustAllSocketFactory() throws InvalidAlgorithmParameterException {
    super();/*from   w  w  w.j a v  a  2  s .  c o  m*/

    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }

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

        }

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

        }
    } };

    SecureRandom secureRND = new SecureRandom();

    try {
        sslContext = SSLContext.getInstance(org.apache.http.conn.ssl.SSLSocketFactory.TLS);
        sslContext.init(null, trustAllCerts, secureRND);
    } catch (NoSuchAlgorithmException e) {
        throw new InvalidAlgorithmParameterException("Failed to initlize TLS context!", e);
    } catch (KeyManagementException e) {
        throw new InvalidAlgorithmParameterException("Failed to init SSL context!", e);
    }

    socketFactory = sslContext.getSocketFactory();
}

From source file:com.qingstor.sdk.request.QSOkHttpRequestClient.java

private static OkHttpClient getUnsafeOkHttpClient() {
    try {//  w ww .j a va  2s. c om
        // 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 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 SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(QSConstant.HTTPCLIENT_CONNECTION_TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(QSConstant.HTTPCLIENT_READ_TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(QSConstant.HTTPCLIENT_WRITE_TIME_OUT, TimeUnit.SECONDS);
        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) {
        logger.log(Level.SEVERE, e.getMessage());
        throw new RuntimeException(e);
    }
}

From source file:hochschuledarmstadt.photostream_tools.AndroidSocket.java

public static SSLContext createSslContext() throws KeyManagementException, NoSuchAlgorithmException {

    SSLContext sslContext = SSLContext.getInstance("TLS");

    TrustManager tm = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }//from www. ja v a 2s .c  o  m

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

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

    sslContext.init(null, new TrustManager[] { tm }, null);

    return sslContext;
}

From source file:org.jboss.as.test.http.util.TestHttpClientUtils.java

/**
 *@param credentialsProvider optional cred provider
 * @return client that doesn't verify https connections
 *///www .ja  va2 s.  co  m
public static CloseableHttpClient getHttpsClient(CredentialsProvider credentialsProvider) {
    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);

        ctx.init(null, new TrustManager[] { tm }, null);

        SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(ctx,
                new NoopHostnameVerifier());

        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("https", sslConnectionFactory).build();
        HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
        HttpClientBuilder builder = HttpClientBuilder.create().setSSLSocketFactory(sslConnectionFactory)
                .setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm);

        if (credentialsProvider != null) {
            builder.setDefaultCredentialsProvider(credentialsProvider);
        }
        return builder.build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.eviware.soapui.impl.wsdl.support.http.SoapUISSLSocketFactory.java

@SuppressWarnings("deprecation")
public SoapUISSLSocketFactory(KeyStore keyStore, String keystorePassword)
        throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
    super(keyStore);

    // trust everyone!
    X509TrustManager tm = new X509TrustManager() {
        @Override//w ww. j  a  v a2 s .  c  o  m
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }

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

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

    if (keyStore != null) {
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keyStore, keystorePassword != null ? keystorePassword.toCharArray() : null);
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        sslContext.init(keymanagers, new TrustManager[] { tm }, null);
    } else {
        sslContext.init(null, new TrustManager[] { tm }, null);
    }

    setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

}