/**
* Get SSL Context from the self signed certificate file on assets folder
* @return
* @throws CertificateException
* @throws IOException
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*
*/
private SSLContext getSSLContext() throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException{
/**
* Load CAs from an InputStream
*/
CertificateFactory cf =CertificateFactory.getInstance("X.509");
InputStream caInput =new BufferedInputStream(this.context.getAssets().open("telis1.cer"));
Certificate ca;
try{
ca = cf.generateCertificate(caInput);
}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);
// Create an SSLContext that uses our TrustManager
SSLContext sslContext =SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(),null);
return sslContext;
}
where telis1.cer is the certificate file in the assets folder
Set Https connection and set SSL Socket Factory
URL url;
HttpsURLConnection con=null;
InputStream is=null;
OutputStreamWriter writer=null;
try {
/**
* Enstablish connection
*/
url = new URL(URLS);
con = (HttpsURLConnection) url.openConnection();
con.setSSLSocketFactory(this.getSSLContext().getSocketFactory());
......