Example usage for android.net SSLCertificateSocketFactory getInsecure

List of usage examples for android.net SSLCertificateSocketFactory getInsecure

Introduction

In this page you can find the example usage for android.net SSLCertificateSocketFactory getInsecure.

Prototype

public static SSLSocketFactory getInsecure(int handshakeTimeoutMillis, SSLSessionCache cache) 

Source Link

Document

Returns a new instance of a socket factory with all SSL security checks disabled, using an optional handshake timeout and SSL session cache.

Usage

From source file:Main.java

/**
 * Returns a {@link javax.net.ssl.SSLSocketFactory}.
 * Optionally bypass all SSL certificate checks.
 *
 * @param insecure if true, bypass all SSL certificate checks
 *//*from   w w  w  . j a  va  2s . com*/
public synchronized static SSLCertificateSocketFactory getSSLSocketFactory(boolean insecure) {
    if (insecure) {
        if (sInsecureFactory == null) {
            sInsecureFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getInsecure(0, null);
        }
        return sInsecureFactory;
    } else {
        if (sSecureFactory == null) {
            sSecureFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0, null);
        }
        return sSecureFactory;
    }
}

From source file:org.mariotaku.twidere.util.net.HostResolvedSSLSocketFactory.java

public HostResolvedSSLSocketFactory(HostAddressResolver resolver, boolean ignoreError) {
    if (ignoreError) {
        defaultFactory = SSLCertificateSocketFactory.getInsecure(0, null);
    } else {//  w  ww.  j a v  a2 s  .com
        defaultFactory = SSLCertificateSocketFactory.getDefault(0, null);
    }
    this.resolver = resolver;
}

From source file:net.sileht.lullaby.Utils.java

public static void setSSLCheck(Boolean insecure) {
    if (insecure) {
        HttpsURLConnection.setDefaultSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(-1, null));
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } else {//from   w  ww  . j a va 2  s .c o  m
        HttpsURLConnection.setDefaultSSLSocketFactory(SSLCertificateSocketFactory.getDefault(-1, null));
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }
}

From source file:org.bombusim.networking.NetworkSocketDataStream.java

public void setTLS() throws IOException {
    LimeLog.i("Socket", "Switching to secure socket layer", null);

    //TODO: check on different devices:
    // !!! ENSURE TLS enabled in account settings before test
    // 1. emulator/2.2 - SSLPeerUnverifiedException (jabber.ru, google.com) - bug in emulator v2.2
    // 2. cyanogen/2.3 - works (all hosts)
    // 3. emulator/ics - works
    // 4. Gratia/2.2 - works
    SSLSocketFactory sf =/*from  w w  w .  ja  v  a 2 s  .  c  om*/
            //SSLCertificateSocketFactory.getDefault(20000, null);
            SSLCertificateSocketFactory.getInsecure(20000, null);

    //TODO: check on different devices:
    // 1. emulator/2.2 - works
    // 2. cyanogen/2.3 - works
    //KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
    //trustStore.load(null, null); 
    //SSLSocketFactory sf = new AndroidSSLSocketFactory(trustStore); 
    //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    final SSLSocket ssls = (SSLSocket) sf.createSocket(socket, host, port, true);

    ssls.addHandshakeCompletedListener(new HandshakeCompletedListener() {
        @Override
        public void handshakeCompleted(HandshakeCompletedEvent event) {
            X509Certificate[] certs;
            try {
                certs = ssls.getSession().getPeerCertificateChain();
            } catch (SSLPeerUnverifiedException e) {
                return;
            }

            StringBuilder so = new StringBuilder();

            for (X509Certificate cert : certs) {
                so.append("X509 Certificate:\n").append(" Subject:");
                appendPrincipal(so, cert.getSubjectDN());
                so.append("\n Issued by:");
                appendPrincipal(so, cert.getIssuerDN());
                so.append("\n Valid from:    ").append(DateFormat.getInstance().format(cert.getNotBefore()));
                so.append("\n Expired after: ").append(DateFormat.getInstance().format(cert.getNotAfter()));
                so.append("\n\n");
            }

            certificateInfo = so.toString();
            LimeLog.i("Socket", "Certificate chain verified", certificateInfo);
        }

        private void appendPrincipal(StringBuilder so, Principal p) {
            String name = p.getName();
            if (name == null) {
                so.append("<null>\n");
                return;
            }

            String elements[] = name.split(",");
            for (String e : elements) {
                so.append("\n   ").append(e);
            }

            so.append("\n");
        }
    });

    ssls.startHandshake();
    socket = ssls;

    istream = socket.getInputStream();
    ostream = socket.getOutputStream();

}

From source file:org.andstatus.app.net.http.TlsSniSocketFactory.java

public TlsSniSocketFactory(SslModeEnum sslMode) {
    secure = sslMode == SslModeEnum.SECURE;
    if (secure) {
        sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory
                .getDefault(MyPreferences.getConnectionTimeoutMs());
    } else {//from   w w w  .j a  v a 2 s .c om
        sslSocketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory
                .getInsecure(MyPreferences.getConnectionTimeoutMs(), null);
        MyLog.i(this, "Insecure SSL allowed");
    }
}

From source file:fr.bmartel.android.tictactoe.GameSingleton.java

private GameSingleton(Context context) {

    this.context = context.getApplicationContext();
    this.executor = Executors.newFixedThreadPool(1);

    //queue = Volley.newRequestQueue(context.getApplicationContext());
    HttpStack hurlStack = new HurlStack() {
        @Override//  w  w w .j a  va 2  s. c o  m
        protected HttpURLConnection createConnection(URL url) throws IOException {
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
            try {
                httpsURLConnection.setSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(0, null));
                httpsURLConnection.setHostnameVerifier(new AllowAllHostnameVerifier());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return httpsURLConnection;
        }
    };
    queue = Volley.newRequestQueue(context, hurlStack);

    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

    //load device id from shared preference
    DEVICE_ID = sharedPreferences.getString(RequestConstants.DEVICE_ID, "");
    deviceName = sharedPreferences.getString(RequestConstants.DEVICE_NAME, RequestConstants.DEFAULT_USERNAME);

    if (DEVICE_ID.equals("")) {
        //register deviceId in shared preference
        SharedPreferences.Editor editor = sharedPreferences.edit();
        DEVICE_ID = new RandomGen(DEVICE_ID_SIZE).nextString();
        editor.putString(RequestConstants.DEVICE_ID, DEVICE_ID);
        editor.commit();
    }

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(BuildConfig.APP_ROUTE + "/connect",
            RequestBuilder.buildConnectionRequest(DEVICE_ID, deviceName), new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.i(TAG, "response received connect : " + response.toString());
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub
                    error.printStackTrace();
                }
            });
    jsObjRequest.setShouldCache(false);

    queue.add(jsObjRequest);

    Log.i(TAG, "device id " + DEVICE_ID + " initialized");

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(context, RegistrationIntentService.class);
        context.startService(intent);
    }
}

From source file:com.android.emailcommon.utility.SSLUtils.java

/**
 * Returns a {@link javax.net.ssl.SSLSocketFactory}.
 * Optionally bypass all SSL certificate checks.
 *
 * @param insecure if true, bypass all SSL certificate checks
 *//*from  w  w w. j a  v  a 2 s .  co m*/
public synchronized static SSLCertificateSocketFactory getSSLSocketFactory(Context context, HostAuth hostAuth,
        boolean insecure) {
    if (insecure) {
        SSLCertificateSocketFactory insecureFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory
                .getInsecure(0, null);
        insecureFactory.setTrustManagers(
                new TrustManager[] { new SameCertificateCheckingTrustManager(context, hostAuth) });
        return insecureFactory;
    } else {
        if (sSecureFactory == null) {
            sSecureFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(0, null);
        }
        return sSecureFactory;
    }
}