SSL certificate pinning for Android App

Following code will load the certificate from the server and create a trust manager with CertificateExpiredException so that when it expired it will throw an exception and we have to load the certificate again from the server and add create a trust manager.

// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.xyz.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
try {
    ca = cf.generateCertificate(caInput);
    System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
    caInput.close();
}

// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);

// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);


TrustManager[] trustManagers = tmf.getTrustManagers();
final X509TrustManager origTrustmanager = (X509TrustManager)trustManagers[0];

TrustManager[] wrappedTrustManagers = new TrustManager[]{
   new X509TrustManager() {
       public java.security.cert.X509Certificate[] getAcceptedIssuers() {
          return origTrustmanager.getAcceptedIssuers();
       }

       public void checkClientTrusted(X509Certificate[] certs, String authType) {
           origTrustmanager.checkClientTrusted(certs, authType);
       }

       public void checkServerTrusted(X509Certificate[] certs, String authType) {
           try {
               origTrustmanager.checkServerTrusted(certs, authType);
           } catch (CertificateExpiredException e) {}
       }
   }
};


// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, wrappedTrustManagers, null);

// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = new URL("https://certs.cac.washington.edu/CAtest/");
HttpsURLConnection urlConnection =
    (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);

virtual hosting. When sharing a server for more than one hostname with HTTP, the web server can tell from the HTTP/1.1 request which target hostname the client is looking for. Unfortunately this is complicated with HTTPS, because the server has to know which certificate to return before it sees the HTTP request. To address this problem, newer versions of SSL, specifically TLSv.1.0 and later, support Server Name Indication (SNI), which allows the SSL client to specify the intended hostname to the server so the proper certificate can be returned.

Fortunately, HttpsURLConnection supports SNI since Android 2.3. 

So in above example we are not verifying hostname explicitly but while SSL handshake which ever hostname is used in HttpsURLConnection server will present that host's certificate for verification

Hi Mitul Sheth, I need a little help related to SSL Pinning, can you please look at this issue on stackoverflow https://stackoverflow.com/questions/68758091/java-io-ioexception-stream-does-not-represent-a-pkcs12-key-store

Like
Reply

To view or add a comment, sign in

More articles by Mitul Sheth

  • Android App Security While Communicating with Server

    1) Use SSL/TLS Confidentiality (Certificate Pinning) Authentication (Auth Token) 2) Validation Host-name Verification…

    3 Comments
  • Android App Security

    Android app runs under their own sandbox under separate process. So one app can not access the resources, data and…

  • Android Shared Element Transitions – RecyclerView

    Implementing Material Design shared element transition using Glide in RecyclerView. This article explain Activity to…

  • Personalization, Contextual Targeting and IoTs

    It’s a given but we will never stress it enough: the creation of an app goes well beyond the early stages of its…

Others also viewed

Explore content categories