I am trying to write a code that will allow me to connect to to a server using a SSL connection with a certificate.
I have the certificate, its a .crt
file.
I Have assembled a code but it does not work:
public class SSLClient {
public static void main(String[] args) throws Exception{
String strServerName = "Some Server"; // SSL Server Name
int intSSLport = 443; // Port where the SSL Server is listening
PrintWriter out = null;
BufferedReader in = null;
{
// Registering the JSSE provider
Security.addProvider(new Provider());
}
try {
// Creating Client Sockets
SSLSocketFactory sslfac = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket)sslfac.createSocket(strServerName,intSSLport);
System.setProperty("javax.net.ssl.keyStore", "D:/projects/TemporaryTesting/certificate.crt");
// Initializing the streams for Communication with the Server
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput = "Hello Testing ";
out.println(userInput);
System.out.println("echo: " + in.readLine());
// Closing the Streams and the Socket
out.close();
in.close();
stdIn.close();
socket.close();
}
catch(Exception exp)
{
System.out.println(" Exception occurred .... " +exp);
exp.printStackTrace();
}
}}
This is the error i am getting:
javax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetjavax.net.ssl.SSLException: Connection has been shutdown: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
What am i doing wrong? Do i have to register the certificate some how with java before i can use it? How do i fix it? This is my first encounter with SSL connections, so any help would be appreciated.