private RequestQueue getPinnedRequestQueue(Context context) throws CertificateException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// Generate the certificate using the certificate file under res/raw/cert.cer
InputStream caInput = new BufferedInputStream(context.getResources().openRawResource(R.raw.your_ssl_cert));
final Certificate ca = cf.generateCertificate(caInput);
caInput.close();
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore trusted = KeyStore.getInstance(keyStoreType);
trusted.load(null, null);
trusted.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(trusted);
// Create an SSLContext that uses our TrustManager
SSLContext sslContext = SSLContext.getInstance("TLSV1.2");
sslContext.init(null, tmf.getTrustManagers(), null);
SSLSocketFactory sf = sslContext.getSocketFactory();
HurlStack hurlStack = new HurlStack(null, sf) {
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
LogUtil.info(TAG, "Before createConnection");
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url);
LogUtil.info(TAG, "After createConnection");
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@DebugLog
@Override
public boolean verify(String hostName, SSLSession sslSession) {
String certificateDomainName = ((X509Certificate) ca).getSubjectDN().toString();
LogUtil.info(TAG, "Index : " + certificateDomainName.indexOf("CN=") + " Len : " + certificateDomainName.codePointCount(certificateDomainName.indexOf("CN="), certificateDomainName.indexOf(",")));
String certificateName = certificateDomainName.substring(certificateDomainName.indexOf("CN="), certificateDomainName.codePointCount(certificateDomainName.indexOf("CN="), certificateDomainName.indexOf(",")));
certificateName = certificateName.replace("CN=", "");
LogUtil.info(TAG, "hostName : " + hostName + " certificateName : " + certificateName);
if (certificateName.isEmpty())
return false;
return certificateName.equals(hostName);
}
});
return httpsURLConnection;
}
};
return new Volley().newRequestQueue(context, hurlStack);
}
Instead of using requestQueue = new Volley().newRequestQueue(context);
Use requestQueue = getPinnedRequestQueue(context);