Search in sources :

Example 1 with SSLCertificateSocketFactory

use of android.net.SSLCertificateSocketFactory in project k-9 by k9mail.

the class DefaultTrustedSocketFactory method setSniHost.

public static void setSniHost(SSLSocketFactory factory, SSLSocket socket, String hostname) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && factory instanceof android.net.SSLCertificateSocketFactory) {
        SSLCertificateSocketFactory sslCertificateSocketFactory = (SSLCertificateSocketFactory) factory;
        sslCertificateSocketFactory.setHostname(socket, hostname);
    } else {
        setHostnameViaReflection(socket, hostname);
    }
}
Also used : SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory)

Example 2 with SSLCertificateSocketFactory

use of android.net.SSLCertificateSocketFactory in project grpc-java by grpc.

the class TesterOkHttpChannelBuilder method getSslCertificateSocketFactory.

@TargetApi(14)
private static SSLCertificateSocketFactory getSslCertificateSocketFactory(@Nullable InputStream testCa, String androidSocketFatoryTls) throws Exception {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) /* API level 14 */
    {
        throw new RuntimeException("android_socket_factory_tls doesn't work with API level less than 14.");
    }
    SSLCertificateSocketFactory factory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(5000);
    // Use HTTP/2.0
    byte[] h2 = "h2".getBytes();
    byte[][] protocols = new byte[][] { h2 };
    if (androidSocketFatoryTls.equals("alpn")) {
        Method setAlpnProtocols = factory.getClass().getDeclaredMethod("setAlpnProtocols", byte[][].class);
        setAlpnProtocols.invoke(factory, new Object[] { protocols });
    } else if (androidSocketFatoryTls.equals("npn")) {
        Method setNpnProtocols = factory.getClass().getDeclaredMethod("setNpnProtocols", byte[][].class);
        setNpnProtocols.invoke(factory, new Object[] { protocols });
    } else {
        throw new RuntimeException("Unknown protocol: " + androidSocketFatoryTls);
    }
    if (testCa != null) {
        factory.setTrustManagers(getTrustManagers(testCa));
    }
    return factory;
}
Also used : SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) Method(java.lang.reflect.Method) TargetApi(android.annotation.TargetApi)

Example 3 with SSLCertificateSocketFactory

use of android.net.SSLCertificateSocketFactory in project baker-android by bakerframework.

the class AndroidHttpClient method newInstance.

/**
     * Create a new HttpClient with reasonable defaults (which you can update).
     *
     * @param userAgent to report in your HTTP requests
     * @param context to use for caching SSL sessions (may be null for no caching)
     * @return AndroidHttpClient for you to use for all your requests.
     */
public static AndroidHttpClient newInstance(String userAgent, Context context) {
    HttpParams params = new BasicHttpParams();
    // Turn off stale checking.  Our connections break all the time anyway,
    // and it's not worth it to pay the penalty of checking every time.
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setConnectionTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    // Don't handle redirects -- return them to the caller.  Our code
    // often wants to re-POST after a redirect, which we must do ourselves.
    HttpClientParams.setRedirecting(params, false);
    Object sessionCache = null;
    // Use a session cache for SSL sockets -- Froyo only
    if (null != context && null != sSslSessionCacheClass) {
        Constructor<?> ct;
        try {
            ct = sSslSessionCacheClass.getConstructor(Context.class);
            sessionCache = ct.newInstance(context);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // Set the specified user agent and register standard protocols.
    HttpProtocolParams.setUserAgent(params, userAgent);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    SocketFactory sslCertificateSocketFactory = null;
    if (null != sessionCache) {
        Method getHttpSocketFactoryMethod;
        try {
            getHttpSocketFactoryMethod = SSLCertificateSocketFactory.class.getDeclaredMethod("getHttpSocketFactory", Integer.TYPE, sSslSessionCacheClass);
            sslCertificateSocketFactory = (SocketFactory) getHttpSocketFactoryMethod.invoke(null, SOCKET_OPERATION_TIMEOUT, sessionCache);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if (null == sslCertificateSocketFactory) {
        sslCertificateSocketFactory = SSLSocketFactory.getSocketFactory();
    }
    schemeRegistry.register(new Scheme("https", sslCertificateSocketFactory, 443));
    ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry);
    // parameters without the funny call-a-static-method dance.
    return new AndroidHttpClient(manager, params);
}
Also used : Context(android.content.Context) ClientContext(org.apache.http.client.protocol.ClientContext) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) Scheme(org.apache.http.conn.scheme.Scheme) PlainSocketFactory(org.apache.http.conn.scheme.PlainSocketFactory) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SocketFactory(org.apache.http.conn.scheme.SocketFactory) Method(java.lang.reflect.Method) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) InvocationTargetException(java.lang.reflect.InvocationTargetException) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SSLCertificateSocketFactory(android.net.SSLCertificateSocketFactory) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) BasicHttpParams(org.apache.http.params.BasicHttpParams)

Aggregations

SSLCertificateSocketFactory (android.net.SSLCertificateSocketFactory)3 Method (java.lang.reflect.Method)2 TargetApi (android.annotation.TargetApi)1 Context (android.content.Context)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ClientContext (org.apache.http.client.protocol.ClientContext)1 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)1 PlainSocketFactory (org.apache.http.conn.scheme.PlainSocketFactory)1 Scheme (org.apache.http.conn.scheme.Scheme)1 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)1 SocketFactory (org.apache.http.conn.scheme.SocketFactory)1 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)1 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 HttpParams (org.apache.http.params.HttpParams)1 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)1 HttpContext (org.apache.http.protocol.HttpContext)1