CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android

前端 未结 9 2036
庸人自扰
庸人自扰 2020-11-29 16:07

I am creating an android application which uses https for communication with the server. I am using retrofit and OkHttp for making req

9条回答
  •  余生分开走
    2020-11-29 16:27

    You are converting cert into BKS Keystore, why aren't you using .cert directly, from https://developer.android.com/training/articles/security-ssl.html:

    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    InputStream instream = context.getResources().openRawResource(R.raw.gtux_cert);
    Certificate ca;
    try {
        ca = cf.generateCertificate(instream);
    } finally {
        caInput.close();
    }
    
    KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType());
    kStore.load(null, null);
    kStore.setCertificateEntry("ca", ca);
    
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm(););
    tmf.init(kStore);
    
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);
    
    okHttpClient.setSslSocketFactory(context.getSocketFactory());
    

提交回复
热议问题