I'm using the following code to Verify Certificate Chain, Expiry Date, Domain Name, Trusted CA and Principal Signature
of the server.
SSLContext sslContext = CustomSSLContext.getSSLContext(mContext);
SSLSocketFactory factory = sslContext.getSocketFactory();
NetworkUtils.mSSLPrimarySocket= (SSLSocket) factory.createSocket(businessIp, businessPort);
NetworkUtils.mSSLPrimarySocket.setEnabledProtocols(new String[]{"TLSv1.2"});
NetworkUtils.mSSLPrimarySocket.setEnabledCipherSuites(sslContext.getServerSocketFactory().
getSupportedCipherSuites());
NetworkUtils.mSSLPrimarySocket.setUseClientMode(true);
NetworkUtils.mSSLPrimarySocket.addHandshakeCompletedListener(
new HandshakeCompletedListener() {
public void handshakeCompleted(
HandshakeCompletedEvent event) {
try {
Certificate[] chain = NetworkUtils.mSSLPrimarySocket.getSession().getPeerCertificates();
X509Certificate subject = (X509Certificate) chain[0];
X509Certificate issuer = (X509Certificate) chain[1];
X509Certificate ca = (X509Certificate) chain[2];
Date certifcateExpiryDate = neml.getNotAfter();
// Next Steps :- Verify Certificate Chain, Expiry Date, Domain Name, Trusted CA and Principal Signature
.
.
.
} catch (Exception e) {
Logger.log(e);
}
}
}
);
NetworkUtils.mSSLPrimarySocket.startHandshake();
I can assure that the code works fine for the most part, it connects to the server and perform all the validation on completion of the handshake
but the problem arises when I exit from my app and try to open the app again to create a new Socket
connection and perform the handshake
.
NetworkUtils.mSSLPrimarySocket.getSession().getPeerCertificates();
The above line inside the Handshake completed listener throws the following exception :-
javax.net.ssl.SSLPeerUnverifiedException: No peer Certificate