help: ValidatorException: No trusted certificate found
I am writing a client application that would load a .p12 certificate with a private key, establish an SSL/TLS connection, and then HTTPS connection over it.
However, I keep getting this error:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found
If anyone can help that would be great.
Here's my code:
public class Main {
// create an SSL contect with the keystore
static SSLContext createSSLContext() throws Exception
{
// set up a key manager for our local credentials
KeyManagerFactory mgrFact = KeyManagerFactory.getInstance("SunX509");
KeyStore clientStore = KeyStore.getInstance("PKCS12", "BC");
String filename = "MyCertificate.p12";
clientStore.load(new FileInputStream(filename), "myPassWord".toCharArray());
mgrFact.init(clientStore, "myPassWord".toCharArray());
// create a context and set up a socket factory
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(mgrFact.getKeyManagers(), null, null);
return sslContext;
}
/**
* Verifier to check host has identified itself using "Test CA Certificate".
* ? into this part further
*/
private static class Validator implements HostnameVerifier
{
public boolean verify(String hostName, SSLSession session)
{
try
{
return true;
}
catch (Exception e)
{
return false;
}
}
}
public static void main(String[] args) {
try {
SSLContextsslContext = createSSLContext();
SSLSocketFactory fact = sslContext.getSocketFactory();
int HTTPS_PORT = 9020;
URL myURL = new URL ("https://www.someplace.com/" + ":" + HTTPS_PORT);
HttpsURLConnection httpsConn = (HttpsURLConnection) myURL.openConnection();
httpsConn.setSSLSocketFactory(fact);
httpsConn.setHostnameVerifier(new Validator());
httpsConn.connect();
httpsConn.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
System.out.println(line);
}
in.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}

