CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android
DISCLAIMER: this answer is from Jul 2015 and uses Retrofit and OkHttp from that time.
Check this link for more info on Retrofit v2 and this one for the current OkHttp methods.
Okay, I got it working using Android Developers guide.
Just as OP, I'm trying to use Retrofit and OkHttp to connect to a self-signed SSL-enabled server.
Here's the code that got things working (I've removed the try/catch blocks):
public static RestAdapter createAdapter(Context context) {
// loading CAs from an InputStream
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream cert = context.getResources().openRawResource(R.raw.my_cert);
Certificate ca;
try {
ca = cf.generateCertificate(cert);
} finally { cert.close(); }
// creating a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// creating a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// creating an SSLSocketFactory that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
// creating an OkHttpClient that uses our SSLSocketFactory
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
// creating a RestAdapter that uses this custom client
return new RestAdapter.Builder()
.setEndpoint(UrlRepository.API_BASE)
.setClient(new OkClient(okHttpClient))
.build();
}
To help in debugging, I also added .setLogLevel(RestAdapter.LogLevel.FULL)
to my RestAdapter creation commands and I could see it connecting and getting the response from the server.
All it took was my original .crt file saved in main/res/raw
.
The .crt file, aka the certificate, is one of the two files created when you create a certificate using openssl
. Generally, it is a .crt or .cert file, while the other is a .key file.
Afaik, the .crt file is your public key and the .key file is your private key.
As I can see, you already have a .cert file, which is the same, so try to use it.
PS: For those that read it in the future and only have a .pem file, according to this answer, you only need this to convert one to the other:
openssl x509 -outform der -in your-cert.pem -out your-cert.crt
PS²: For those that don't have any file at all, you can use the following command (bash) to extract the public key (aka certificate) from any server:
echo -n | openssl s_client -connect your.server.com:443 | \
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/my_cert.crt
Just replace the your.server.com
and the port (if it is not standard HTTPS) and choose a valid path for your output file to be created.
Android - Retrofit2 - java.security.cert.CertPathValidatorException: Trust anchor for certification path not found
You server url is https
and certificate is already not valid.
Change https
to http
and it will work.
Else you can install valid SSL certificate on the server.
CertPathValidatorException: Trust anchor for certification path not found. in android after update gradle and gradle plugin
This would mean that certificate is issued for www.example.com , and thus can not be trusted for whatever.example.com - this server need proper signed certificate
Related Topics
Alertdialog.Getbutton() Method Gives Null Pointer Exception Android
How to Send Data Message Using Firebase Console
How Does Native Android Code Written for Arm Run on X86
Proguard with Ormlite on Android
Touch and Drag Image in Android
Android Listview Row Delete Animation
Android: Notifydatasetchanged(); Not Working
Google Maps Fragment Returning Null Inside a Fragment
Adding Your Own SQLite Database to an Android Application
Parse JSON Array Response Using Retrofit & Gson
Which Can Replace Capturepicture Function
Calling Hidden API in Android to Turn Screen Off
Simple Httpurlconnection Post File Multipart/Form-Data from Android to Google Blobstore