I am consuming a rest service for my web application. And the service provider has provided a .p12 file with a password to connect to their service.
I installed the certificate file in postman for testing purposes and it works fine.Now I have to integrate it to my java code.
This is my java code for integration.
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.net.ssl.KeyManagerFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
@Service
public class DemoIntegration
{
String key = "XYX";
String value = "12BN";
String encVal= "343fhh22343mm90ddfd61lcsert";
private static String certPw = "44vvxxffx"; //Password to cerfificate file
public void checkConnection()
{
try
{
RestTemplate restTemplate = new RestTemplate();
HttpHeaders httpHeaders = new HttpHeaders();
String uri = "https://my_demo_uri";
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(new FileInputStream("C:\\Users\\my_cert.p12"), certPw.toCharArray()); //my_cert.p12 is my cerfificate file
KeyStore jks = KeyStore.getInstance("JKS");
jks.load(null);
for (Enumeration<String> t = ks.aliases(); t.hasMoreElements();)
{
String alias = t.nextElement();
System.out.println("@:" + alias);
if (ks.isKeyEntry(alias))
{
Certificate[] a = ks.getCertificateChain(alias);
for (int i = 0; i < a.length; i++)
{
X509Certificate x509 = (X509Certificate) a[i];
System.out.println(x509.getSubjectDN().toString());
if (i > 0)
{
jks.setCertificateEntry(x509.getSubjectDN().toString(), x509);
}
System.out.println(ks.getCertificateAlias(x509));
System.out.println("ok");
}
}
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, certPw.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(jks);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("API_KEY", api_key);
httpHeaders.set("Signature", SHA256Val);
String r2 = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(httpHeaders), String.class).getBody();
System.out.println("Result " + r2);
}
catch (Exception ex)
{
System.out.println("Error " + ex.toString());
Logger.getLogger(DemoIntegration.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
And always I get the response like below. This is the same response I got when I tested it with postman without adding the certificate file to it.
400 Bad Request:{
"error": {
"message": "No SSL cetificate"
}
}
Can someone point out what am I doing wrong here?
I am a newbie when it comes to java security area.To be honest I followed a guide to write this code/Is there a checklist to follow when trying to connect? (Like adding the file to keystore or truststore.At least a guide would help me here)
Thanks a lot in advance.