Add multiple .bks files for SSL pinning in Android

  android, certificate-pinning, ssl-certificate

I am using OkHttpClient library for HTTP requests and having two ‘.bks’ files in the ‘res/raw’ folder for doing SSL pinning. I have written the following code for fetching the one ‘.bks’ file. But how to add the multiple files?

  • dkhymnow.bks

  • dpsandroid.bks

I hope somebody can help me!

private OkHttpClient getOkHttpClient() {
    try {
      OkHttpClient.Builder builder = new OkHttpClient.Builder();
      builder.sslSocketFactory(getPinnedCertSslSocketFactory(context));
      builder.connectTimeout(30, TimeUnit.SECONDS);
      builder.writeTimeout(30, TimeUnit.SECONDS);
      builder.readTimeout(30, TimeUnit.SECONDS);

      OkHttpClient okHttpClient = builder.build();
      return okHttpClient;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  private SSLSocketFactory getPinnedCertSslSocketFactory(Context context) {
    try {
      KeyStore trusted = KeyStore.getInstance("BKS");
      InputStream in = context.getResources().openRawResource(R.raw.dkhymnow);
      trusted.load(in, STORE_PASS);
      SSLContext sslContext = SSLContext.getInstance("TLS");
      TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
      trustManagerFactory.init(trusted);
      sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
      return sslContext.getSocketFactory();
    } catch (Exception e) {
      Log.e("MyApp", e.getMessage(), e);
    }
    return null;
  }

Source: Android Questions

LEAVE A COMMENT