java client side handshaking using PKCS12 certificate

408 Views Asked by At

I am connecting to dc bidden server using following handshaking code

System.setProperty("javax.net.ssl.keyStore",".p12 file path");
System.setProperty("javax.net.ssl.keyStorePassword",keystorePassword);
System.setProperty("javax.net.ssl.keyStoreType",""pkcs12"");

i am able to connect to the first server with this code but for next server tomcat ignore the recent property set so i am not able to connect to next server with same kind of dc bidden server.

Thanks in advance

1

There are 1 best solutions below

0
dave_thompson_085 On

If by 'connecting https URL' you mean specifically the java.net.URL class and something like new java.net.URL("https://something") .openConnection() which returns an implementation (subclass) of javax.net.HttpsURLConnection here are two examples as I referenced:

static void SO49993912ExampleClientPKCS12 (String[] args) throws Exception {
    FileInputStream fis = new FileInputStream (args[0]);
    KeyStore ks = KeyStore.getInstance("PKCS12"); 
    ks.load (fis, args[1].toCharArray()); fis.close();
    KeyManagerFactory kf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
    kf.init (ks, args[1].toCharArray());
    SSLContext ctx = SSLContext.getInstance ("TLS"); 
    ctx.init (kf.getKeyManagers(), null /*default TM(s)*/, null);
    // method 1
    HttpsURLConnection conn1 = (HttpsURLConnection) new URL (args[2]).openConnection();
    conn1.setSSLSocketFactory(ctx.getSocketFactory());
    conn1.connect(); System.out.println (conn1.getResponseCode()); conn1.disconnect();
    // method 2
    HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());
    HttpsURLConnection conn2 = (HttpsURLConnection) new URL (args[2]).openConnection();
    conn1.connect(); System.out.println (conn2.getResponseCode()); conn2.disconnect();
}

However there are lots and lots of other ways of connecting to https (and other) URLs in Java; if you actually meant something else you have to be more specific.