Hallo,
I have the following function in my AsyncTask:
In the doInBackground-Function im loading some resources from my server with a self-signed certificate:
And sometimes (!) I get following Error on the last line of the code posted:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
But why?
I have the following function in my AsyncTask:
Code:
private SSLContext trustCert() throws Exception {
SSLContext context = null;
context = SSLContext.getInstance("TLS");
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = this.context.getResources().openRawResource(R.raw.cert);
Certificate ca = cf.generateCertificate(caInput);
caInput.close();
// Create a KeyStore containing our trusted CAs
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
context.init(null, tmf.getTrustManagers(), null);
return context;
}
In the doInBackground-Function im loading some resources from my server with a self-signed certificate:
Code:
SSLContext sslContext = this.trustCert();
HttpsURLConnection conn = (HttpsURLConnection) address.openConnection();
conn.setSSLSocketFactory(sslContext.getSocketFactory());
......
conn.connect(); //here i get the error
And sometimes (!) I get following Error on the last line of the code posted:
java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
But why?