Search in sources :

Example 1 with DecoratedHostnameVerifier

use of keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier in project keepass2android by PhilippC.

the class WebDavStorage method getClient.

private OkHttpClient getClient(ConnectionInfo ci) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    final Map<String, CachingAuthenticator> authCache = new ConcurrentHashMap<>();
    com.burgstaller.okhttp.digest.Credentials credentials = new com.burgstaller.okhttp.digest.Credentials(ci.username, ci.password);
    final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
    final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
    // note that all auth schemes should be registered as lowercase!
    DispatchingAuthenticator authenticator = new DispatchingAuthenticator.Builder().with("digest", digestAuthenticator).with("basic", basicAuthenticator).build();
    builder = builder.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache)).addInterceptor(new AuthenticationCacheInterceptor(authCache));
    if ((mCertificateErrorHandler != null) && (!mCertificateErrorHandler.alwaysFailOnValidationError())) {
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init((KeyStore) null);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
            throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
        }
        X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
        trustManager = new DecoratedTrustManager(trustManager, mCertificateErrorHandler);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[] { trustManager }, null);
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        builder = builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new DecoratedHostnameVerifier(OkHostnameVerifier.INSTANCE, mCertificateErrorHandler));
    }
    OkHttpClient client = builder.build();
    return client;
}
Also used : OkHttpClient(okhttp3.OkHttpClient) DispatchingAuthenticator(com.burgstaller.okhttp.DispatchingAuthenticator) BasicAuthenticator(com.burgstaller.okhttp.basic.BasicAuthenticator) AuthenticationCacheInterceptor(com.burgstaller.okhttp.AuthenticationCacheInterceptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) CachingAuthenticator(com.burgstaller.okhttp.digest.CachingAuthenticator) CachingAuthenticatorDecorator(com.burgstaller.okhttp.CachingAuthenticatorDecorator) SSLContext(javax.net.ssl.SSLContext) TrustManager(javax.net.ssl.TrustManager) DecoratedTrustManager(keepass2android.javafilestorage.webdav.DecoratedTrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) DecoratedHostnameVerifier(keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier) DecoratedTrustManager(keepass2android.javafilestorage.webdav.DecoratedTrustManager) DigestAuthenticator(com.burgstaller.okhttp.digest.DigestAuthenticator) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Aggregations

AuthenticationCacheInterceptor (com.burgstaller.okhttp.AuthenticationCacheInterceptor)1 CachingAuthenticatorDecorator (com.burgstaller.okhttp.CachingAuthenticatorDecorator)1 DispatchingAuthenticator (com.burgstaller.okhttp.DispatchingAuthenticator)1 BasicAuthenticator (com.burgstaller.okhttp.basic.BasicAuthenticator)1 CachingAuthenticator (com.burgstaller.okhttp.digest.CachingAuthenticator)1 DigestAuthenticator (com.burgstaller.okhttp.digest.DigestAuthenticator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 SSLContext (javax.net.ssl.SSLContext)1 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)1 TrustManager (javax.net.ssl.TrustManager)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 X509TrustManager (javax.net.ssl.X509TrustManager)1 DecoratedHostnameVerifier (keepass2android.javafilestorage.webdav.DecoratedHostnameVerifier)1 DecoratedTrustManager (keepass2android.javafilestorage.webdav.DecoratedTrustManager)1 OkHttpClient (okhttp3.OkHttpClient)1