1
0
mirror of https://github.com/owncloud/android-library.git synced 2025-06-07 16:06:08 +00:00

clean up http client

This commit is contained in:
Schabi 2021-01-18 11:05:55 +01:00 committed by Abel García de Prada
parent 0313c1e103
commit 79e4287223

View File

@ -74,35 +74,41 @@ public class HttpClient {
try { try {
final X509TrustManager trustManager = new AdvancedX509TrustManager( final X509TrustManager trustManager = new AdvancedX509TrustManager(
NetworkUtils.getKnownServersStore(sContext)); NetworkUtils.getKnownServersStore(sContext));
final SSLSocketFactory sslSocketFactory = getNewSslSocketFactory(trustManager);
final SSLContext sslContext = buildSSLContext();
sslContext.init(null, new TrustManager[]{trustManager}, null);
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
// Automatic cookie handling, NOT PERSISTENT // Automatic cookie handling, NOT PERSISTENT
final CookieJar cookieJar = new CookieJarImpl(mCookieStore); final CookieJar cookieJar = new CookieJarImpl(mCookieStore);
// TODO: Not verifying the hostname against certificate. ask owncloud security human if this is ok.
//.hostnameVerifier(new BrowserCompatHostnameVerifier());
mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar); mOkHttpClient = buildNewOkHttpClient(sslSocketFactory, trustManager, cookieJar);
} catch(NoSuchAlgorithmException nsae){
Timber.e(nsae, "Could not setup SSL system.");
throw new RuntimeException("Could not setup okHttp client.", nsae);
} catch (Exception e) { } catch (Exception e) {
Timber.e(e, "Could not setup SSL system."); Timber.e(e, "Could not setup okHttp client.");
throw new RuntimeException("Could not setup okHttp client.", e);
} }
} }
return mOkHttpClient; return mOkHttpClient;
} }
private static SSLContext getSslContext() throws NoSuchAlgorithmException { private SSLContext buildSSLContext() throws NoSuchAlgorithmException {
try { try {
return SSLContext.getInstance(TlsVersion.TLS_1_3.javaName()); return SSLContext.getInstance("TLSv1.3");
} catch (NoSuchAlgorithmException tlsv13Exception) { } catch (NoSuchAlgorithmException tlsv13Exception) {
try { try {
Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2"); Timber.w("TLSv1.3 is not supported in this device; falling through TLSv1.2");
return SSLContext.getInstance(TlsVersion.TLS_1_2.javaName()); return SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException tlsv12Exception) { } catch (NoSuchAlgorithmException tlsv12Exception) {
try { try {
Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1"); Timber.w("TLSv1.2 is not supported in this device; falling through TLSv1.1");
return SSLContext.getInstance(TlsVersion.TLS_1_1.javaName()); return SSLContext.getInstance("TLSv1.1");
} catch (NoSuchAlgorithmException tlsv11Exception) { } catch (NoSuchAlgorithmException tlsv11Exception) {
Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0"); Timber.w("TLSv1.1 is not supported in this device; falling through TLSv1.0");
return SSLContext.getInstance(TlsVersion.TLS_1_0.javaName()); return SSLContext.getInstance("TLSv1");
// should be available in any device; see reference of supported protocols in // should be available in any device; see reference of supported protocols in
// http://developer.android.com/reference/javax/net/ssl/SSLSocket.html // http://developer.android.com/reference/javax/net/ssl/SSLSocket.html
} }
@ -110,13 +116,6 @@ public class HttpClient {
} }
} }
private static SSLSocketFactory getNewSslSocketFactory(X509TrustManager trustManager)
throws NoSuchAlgorithmException, KeyManagementException {
final SSLContext sslContext = getSslContext();
sslContext.init(null, new TrustManager[]{trustManager}, null);
return sslContext.getSocketFactory();
}
private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager, private OkHttpClient buildNewOkHttpClient(SSLSocketFactory sslSocketFactory, X509TrustManager trustManager,
CookieJar cookieJar) { CookieJar cookieJar) {
return new OkHttpClient.Builder() return new OkHttpClient.Builder()