all I will be so grateful if you could help me solve this. I am developing an android app lately. And now I have to connect to server with self signed certificate through Https. I need to make it clear that the certificate is self signed and of course it is not trusted on my android device. So I followed the steps on https://developer.android.com/training/articles/security-ssl.htmlcarefully. Codes are as follows:
SSLSocketFactory sslSocketFactory = getSSL(context);
URL url = new URL(address);
HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(sslSocketFactory);
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
protected static SSLSocketFactory getSSL(Context context) {
SSLSocketFactory sslSocketFactory = null;
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = context.getResources().getAssets().open("softRSAroot.cer");
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
Log.d("maggie", "ca= " + ((X509Certificate) ca).getSubjectDN());
}finally {
caInput.close();
}
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);
sslSocketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
e.printStackTrace();
}
return sslSocketFactory;
}
Then I got this :
java.util.concurrent.ExecutionException: javax.net.ssl.SSLPeerUnverifiedException: Hostname 192.168.3.33 not verified: certificate: sha1/gJuuHkIr9IeY7Q42In3rrg0PvKw= DN: CN=192.168.3.33,OU=YJY,O=xxx,L=xxx,ST=xxx,C=CN subjectAltNames: []
The ip address matches the CN, I am keeping working on this hard. Thanks for reading and helping me.