-6

I need the source code of the default implementation of trust manager

2 Answers2

0

Android use several different trust managers. A lot of the code is in extensions to the Google Conscrypt library. There's an example here

There's another, non-Constrypt example here. It mostly delegates calls. In my, admittedly small experience, the delegate is one of the Conscrypt trust managers, mentioned above.

E_net4
  • 27,810
  • 13
  • 101
  • 139
G. Blake Meike
  • 6,615
  • 3
  • 24
  • 40
-2

you could try this way. I believe this is best practise.

  try {
                TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                trustManagerFactory.init((KeyStore) null);
                TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
                X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
                httpClient.sslSocketFactory(getSSLConfig(getApplicationContext()).getSocketFactory(), trustManager);
            } catch (Exception e) {
                Timber.e(String.valueOf(e));
            }


private static SSLContext getSSLConfig(Context context) throws CertificateException, IOException,
            KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        Certificate ca;
        try (InputStream cert = context.getResources().openRawResource(R.raw.yourCERT)) {
            ca = cf.generateCertificate(cert);
        }
        String keyStoreType = KeyStore.getDefaultType();
        KeyStore keyStore = KeyStore.getInstance(keyStoreType);
        keyStore.load(null, null);
        keyStore.setCertificateEntry("ca", ca);
        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(keyStore);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        return sslContext;
    }
mbakgun
  • 75
  • 5