Search in sources :

Example 46 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project android_frameworks_base by DirtyUnicorns.

the class X509TrustManagerExtensionsTest method testNormalUseCase.

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

Example 47 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project AisenWeiBo by wangdan.

the class HttpsUtility method getOkHttpClient.

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;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 48 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project wildfly by wildfly.

the class TestHttpClientUtils method getHttpsClient.

/**
     *@param credentialsProvider optional cred provider
     * @return client that doesn't verify https connections
     */
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;
    }
}
Also used : SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) CertificateException(java.security.cert.CertificateException)

Example 49 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project pictureapp by EyeSeeTea.

the class UnsafeOkHttpsClientFactory method getUnsafeOkHttpClient.

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);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 50 with X509TrustManager

use of javax.net.ssl.X509TrustManager in project quorrabot by GloriousEggroll.

the class SingularityAPI method StartService.

public void StartService() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

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

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }
    } };
    try {
        SSLContext mySSLContext = SSLContext.getInstance("TLS");
        mySSLContext.init(null, null, null);
        IO.Options opts = new IO.Options();
        opts.sslContext = mySSLContext;
        opts.hostnameVerifier = new NullHostnameVerifier();
        webSocket = IO.socket(apiURL);
        webSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                //com.gmt2001.Console.out.println("GameWisp API: Connected to Singularity");
                webSocket.emit("authentication", new JSONObject().put("key", gwIdentifier).put("access_token", AccessToken));
            }
        });
        webSocket.on("unauthorized", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                JSONObject jsonObject = new JSONObject(args[0].toString());
                com.gmt2001.Console.err.println("GameWisp API: Authorization Failed: " + jsonObject.getString("message"));
                com.gmt2001.Console.err.println("Token: " + AccessToken + " Session ID: " + SessionID + " Client ID: " + gwIdentifier);
            }
        });
        webSocket.on("authenticated", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                //com.gmt2001.Console.out.println("GameWisp API: Authenticated");
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if (!jsonObject.has("session")) {
                    com.gmt2001.Console.err.println("GameWisp API: Missing Session in Authenticated Return JSON");
                    Authenticated = false;
                    return;
                }
                SessionID = jsonObject.getString("session");
                Authenticated = true;
            }
        });
        webSocket.on("app-channel-connected", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                if (Authenticated) {
                    if (Quorrabot.enableDebugging) {
                        com.gmt2001.Console.out.println("GameWisp API: Connected to Channel");
                    } else {
                        com.gmt2001.Console.out.println("GameWisp API: Connected and Ready for Requests");
                    }
                    ChannelConnected = true;
                } else {
                    com.gmt2001.Console.out.println("GameWisp API: Connected to Channel; Missing Session ID; Unusable Session");
                    ChannelConnected = false;
                }
            }
        });
        webSocket.on("subscriber-new", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                com.gmt2001.Console.out.println("GameWisp API: subscriber-new received");
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if (!jsonObject.has("data")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("usernames")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("tier")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("tier").has("level")) {
                    return;
                }
                String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
                int tier = jsonObject.getJSONObject("data").getJSONObject("tier").getInt("level");
                EventBus.instance().post(new GameWispSubscribeEvent(username, tier));
            }
        });
        webSocket.on("subscriber-anniversary", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                com.gmt2001.Console.out.println("GameWisp API: subscriber-anniversary received");
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if (!jsonObject.has("data")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("subscriber")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("subscriber").has("usernames")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("usernames").has("twitch")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("month_count")) {
                    return;
                }
                String username = jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("usernames").getString("twitch");
                int tier = jsonObject.getJSONObject("data").getJSONObject("subscriber").getJSONObject("tier").getInt("level");
                int months = jsonObject.getJSONObject("data").getInt("month_count");
                EventBus.instance().post(new GameWispAnniversaryEvent(username, months, tier));
            }
        });
        webSocket.on("subscriber-benefits-change", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                com.gmt2001.Console.out.println("GameWisp API: subscriber-benefits-change received");
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if (!jsonObject.has("data")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("usernames")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
                    return;
                }
                if (!jsonObject.has("tier")) {
                    return;
                }
                if (!jsonObject.getJSONObject("tier").has("level")) {
                    return;
                }
                String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
                int tier = jsonObject.getJSONObject("tier").getInt("level");
                EventBus.instance().post(new GameWispBenefitsEvent(username, tier));
            }
        });
        /**
             * Status Change Values:
             * https://gamewisp.readme.io/docs/subscriber-new active - a
             * currently active subscriber trial - a subscriber on a trial code
             * grace_period - a canceled subscriber that is still received
             * benefits billing_grace_period - a canceled subscriber still
             * receiving benefits that was canceled due to a payment processing
             * error inactive - a subscriber that is canceled and receiving no
             * benefits twitch - a subscriber that is receiving free benefits
             * from a partnered Twitch streamer.
             */
        webSocket.on("subscriber-status-change", new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                com.gmt2001.Console.out.println("GameWisp API: subscriber-status-changed received");
                JSONObject jsonObject = new JSONObject(args[0].toString());
                if (!jsonObject.has("data")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("usernames")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").getJSONObject("usernames").has("twitch")) {
                    return;
                }
                if (!jsonObject.getJSONObject("data").has("status")) {
                    return;
                }
                String username = jsonObject.getJSONObject("data").getJSONObject("usernames").getString("twitch");
                String status = jsonObject.getJSONObject("data").getString("status");
                EventBus.instance().post(new GameWispChangeEvent(username, status));
            }
        });
        webSocket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                com.gmt2001.Console.out.println("GameWisp API: Disconnected");
            }
        });
        webSocket.connect();
    } catch (Exception ex) {
        com.gmt2001.Console.err.println("GameWisp API: Exception: " + ex.getMessage());
    }
}
Also used : Options(io.socket.client.IO.Options) Emitter(io.socket.emitter.Emitter) GameWispSubscribeEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispSubscribeEvent) IO(io.socket.client.IO) GameWispChangeEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispChangeEvent) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) IllegalArgumentException(java.lang.IllegalArgumentException) InterruptedException(java.lang.InterruptedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) Options(io.socket.client.IO.Options) GameWispBenefitsEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispBenefitsEvent) JSONObject(org.json.JSONObject) GameWispAnniversaryEvent(me.gloriouseggroll.quorrabot.event.gamewisp.GameWispAnniversaryEvent) X509TrustManager(javax.net.ssl.X509TrustManager) JSONObject(org.json.JSONObject)

Aggregations

X509TrustManager (javax.net.ssl.X509TrustManager)183 TrustManager (javax.net.ssl.TrustManager)114 X509Certificate (java.security.cert.X509Certificate)96 SSLContext (javax.net.ssl.SSLContext)88 CertificateException (java.security.cert.CertificateException)54 IOException (java.io.IOException)50 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)45 SecureRandom (java.security.SecureRandom)44 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)32 KeyManagementException (java.security.KeyManagementException)28 Test (org.junit.Test)21 HostnameVerifier (javax.net.ssl.HostnameVerifier)19 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)19 KeyStore (java.security.KeyStore)17 GeneralSecurityException (java.security.GeneralSecurityException)15 SSLSession (javax.net.ssl.SSLSession)15 KeyStoreException (java.security.KeyStoreException)14 SSLException (javax.net.ssl.SSLException)14 URL (java.net.URL)11 OkHttpClient (okhttp3.OkHttpClient)10