0

I am trying to consume a service that is protected by an SSL certificate. For that, I did this implementation, but it throws this error

java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

I have the certificate in the Raw directory and it is a .pem file

I am trying to identify the error but with no luck, this is my source code:

public OkHttpClient gereratSecureOkHttpClient(Context context) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException, KeyManagementException {
    OkHttpClient.Builder client = new OkHttpClient().newBuilder()
            .readTimeout(60, TimeUnit.SECONDS)
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS);

    InputStream caInput = context.getResources().openRawResource(R.raw.movilexitopruebas2021);
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    keyStore.load(caInput, "Password".toCharArray());

    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("X509");
    keyManagerFactory.init(keyStore, "Password".toCharArray());

    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

    return client
            .sslSocketFactory(sslContext.getSocketFactory())
            .build();
}

I already invoke it this way:

Retrofit retrofit = null;
retrofit = new Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(gereratSecureOkHttpClient(context))
                        .build();
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [Trust Anchor not found for Android SSL Connection](https://stackoverflow.com/questions/6825226/trust-anchor-not-found-for-android-ssl-connection) – nighthawk Jan 04 '22 at 18:56

0 Answers0