I'm using Retrofit2
with RxJava2
and OkHttp3
to make an HTTPS
call to an internal server for testing a SSL
certificate, but I always get an exception stating:
HTTP FAILED: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
According to the Google developer documentation, there are 3 reasons why this might occur (unknown CA, self signed certificate or server configuration is missing intermediate CA)
I cannot post the URL, as it is an internal testing server, but opening the URL in a browser on my computer or on my phone in Chrome works fine, this is what I can see from the certificate:
Therefore I think that it can't be option 1, because the browser recognises the certificate, only my app and OkHttp don't seem so and as it is no self signed certificate, option 2 also shouldn't be the cause.
And this is how my OkHttp
client is provided by Dagger
:
@Provides
@PerApplication
OkHttpClient provideOkHttpClient(@ApplicationContext Context context) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
File cacheDirectory = new File(context.getCacheDir().getAbsolutePath(), "HttpCache");
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder()
.connectionSpecs(Collections.singletonList(spec))
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.cache(new Cache(cacheDirectory, cacheSize));
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClientBuilder.addInterceptor(loggingInterceptor);
}
return httpClientBuilder.build();
}
I added the CollectionSpec
to define that I'm using TLS 1.2, but this makes no difference, I still receive the Trust anchor error.
Is there something else that I need to set in order to use the ssl certificate? Any ideas why the certificate is not accepted?