Can anyone tell me how to use .cer certificate file inside java code.
Printable View
Can anyone tell me how to use .cer certificate file inside java code.
What do you want to do with it? Normally when I've worked with certificates in the past I just loaded them into a keystore\truststore and then referenced that in java. I think there is also a java keystore somewhere within the installation directories of Java.
If you could provide a little more information it might make it easier to help you.
I used .cer file to put a Digital certificate on my pdf file.
I created .cer file by using cmd.
you can use the followng commad to convert .cer file to keystore
1.keytool -import -keystore client.keystore -file WSS.cer
2.Input password for your output file(say abc)
This will create a file named client.keystore
Then you can use it in your java code.
System.setProperty("javax.net.ssl.trustStore", "client.keystore");
System.setProperty("javax.net.ssl.trustStorePasswo rd","abc");
There is a web service built in .Net and running on SSL. I am using axis client to connect to it. First I used a .jks certificate to connect to it. But now I have to use .cer file to connect. I need to be able to connect to the web service. I have used keytool for keystore generation but I am not able to connect to web service.
I use
keytool -import -keystore client.keystore -file WSS.cer
to generate the keystore and use it in the code using
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword ", password);
System.setProperty("javax.net.ssl.trustStore", keystorePath);
System.setProperty("javax.net.ssl.trustStorePasswo rd", password);
But I am not able to connect to it.
Can someone please guide me how to connect to it.
what does it means??
"But I am not able to connect to it"
What message/Exception you are getting?
and you only need to set the following
System.setProperty("javax.net.ssl.trustStore", keystorePath);
System.setProperty("javax.net.ssl.trustStorePasswo rd", password);
Don't use
System.setProperty("javax.net.ssl.keyStore", keystorePath);
System.setProperty("javax.net.ssl.keyStorePassword ", password);
I have tried using it but I am getting the following exception
org.apache.axis2.AxisFault: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at org.apache.axis2.AxisFault.makeFault(AxisFault.jav a:430)
at org.apache.axis2.transport.http.AxisRequestEntity. writeRequest(AxisRequestEntity.java:98)
at org.apache.commons.httpclient.methods.EntityEnclos ingMethod.writeRequestBody(EntityEnclosingMethod.j ava:499)
at org.apache.commons.httpclient.HttpMethodBase.write Request(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execu te(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.e xecuteWithRetry(HttpMethodDirector.java:398)[INFO] Unable to sendViaPost to url[https://csr.pmex.com.pk/ncel.webserv...radedata.asmx]
org.apache.axis2.AxisFault: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at org.apache.axis2.AxisFault.makeFault(AxisFault.jav a:430)
at org.apache.axis2.transport.http.AxisRequestEntity. writeRequest(AxisRequestEntity.java:98)
at org.apache.commons.httpclient.methods.EntityEnclos ingMethod.writeRequestBody(EntityEnclosingMethod.j ava:499)
at org.apache.commons.httpclient.HttpMethodBase.write Request(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execu te(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.e xecuteWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.e xecuteMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMe thod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMe thod(HttpClient.java:346)
at org.apache.axis2.transport.http.AbstractHTTPSender .executeMethod(AbstractHTTPSender.java:560)
at org.apache.axis2.transport.http.HTTPSender.sendVia Post(HTTPSender.java:199)
at org.apache.axis2.transport.http.HTTPSender.send(HT TPSender.java:76)
at org.apache.axis2.transport.http.CommonsHTTPTranspo rtSender.writeMessageWithCommons(CommonsHTTPTransp ortSender.java:400)
at org.apache.axis2.transport.http.CommonsHTTPTranspo rtSender.invoke(CommonsHTTPTransportSender.java:22 5)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine .java:438)
at org.apache.axis2.description.OutInAxisOperationCli ent.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationCli ent.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(Op erationClient.java:165)
at org.tempuri.WSTradeDataStub.getFxRates(WSTradeData Stub.java:2151)
at Driver2.main(Driver2.java:179)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLExceptio n(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(S SLSocketImpl.java:1611)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Ha ndshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Ha ndshaker.java:181)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serv erCertificate(ClientHandshaker.java:1035)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.proc essMessage(ClientHandshaker.java:124)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoo p(Handshaker.java:516)
at com.sun.net.ssl.internal.ssl.Handshaker.process_re cord(Handshaker.java:454)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRec ord(SSLSocketImpl.java:884)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.perform InitialHandshake(SSLSocketImpl.java:1112)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRe cord(SSLSocketImpl.java:623)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write (AppOutputStream.java:59)
at java.io.BufferedOutputStream.flushBuffer(BufferedO utputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputS tream.java:123)
at org.apache.axis2.transport.http.AxisRequestEntity. writeRequest(AxisRequestEntity.java:94)
... 18 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXV alidator.java:285)
at sun.security.validator.PKIXValidator.engineValidat e(PKIXValidator.java:191)
at sun.security.validator.Validator.validate(Validato r.java:218)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. validate(X509TrustManagerImpl.java:126)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. checkServerTrusted(X509TrustManagerImpl.java:209)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. checkServerTrusted(X509TrustManagerImpl.java:249)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serv erCertificate(ClientHandshaker.java:1014)
... 28 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder. engineBuild(SunCertPathBuilder.java:174)
at java.security.cert.CertPathBuilder.build(CertPathB uilder.java:238)
at sun.security.validator.PKIXValidator.doBuild(PKIXV alidator.java:280)
... 34 more
at org.apache.commons.httpclient.HttpMethodDirector.e xecuteMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMe thod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMe thod(HttpClient.java:346)
at org.apache.axis2.transport.http.AbstractHTTPSender .executeMethod(AbstractHTTPSender.java:560)
at org.apache.axis2.transport.http.HTTPSender.sendVia Post(HTTPSender.java:199)
at org.apache.axis2.transport.http.HTTPSender.send(HT TPSender.java:76)
at org.apache.axis2.transport.http.CommonsHTTPTranspo rtSender.writeMessageWithCommons(CommonsHTTPTransp ortSender.java:400)
at org.apache.axis2.transport.http.CommonsHTTPTranspo rtSender.invoke(CommonsHTTPTransportSender.java:22 5)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine .java:438)
at org.apache.axis2.description.OutInAxisOperationCli ent.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationCli ent.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(Op erationClient.java:165)
at org.tempuri.WSTradeDataStub.getFxRates(WSTradeData Stub.java:2151)
at Driver2.main(Driver2.java:179)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLExceptio n(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(S SLSocketImpl.java:1611)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Ha ndshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Ha ndshaker.java:181)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serv erCertificate(ClientHandshaker.java:1035)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.proc essMessage(ClientHandshaker.java:124)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoo p(Handshaker.java:516)
at com.sun.net.ssl.internal.ssl.Handshaker.process_re cord(Handshaker.java:454)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRec ord(SSLSocketImpl.java:884)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.perform InitialHandshake(SSLSocketImpl.java:1112)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRe cord(SSLSocketImpl.java:623)
at com.sun.net.ssl.internal.ssl.AppOutputStream.write (AppOutputStream.java:59)
at java.io.BufferedOutputStream.flushBuffer(BufferedO utputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputS tream.java:123)
at org.apache.axis2.transport.http.AxisRequestEntity. writeRequest(AxisRequestEntity.java:94)
... 18 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXV alidator.java:285)
at sun.security.validator.PKIXValidator.engineValidat e(PKIXValidator.java:191)
at sun.security.validator.Validator.validate(Validato r.java:218)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. validate(X509TrustManagerImpl.java:126)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. checkServerTrusted(X509TrustManagerImpl.java:209)
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl. checkServerTrusted(X509TrustManagerImpl.java:249)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serv erCertificate(ClientHandshaker.java:1014)
... 28 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderE xception: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder. engineBuild(SunCertPathBuilder.java:174)
at java.security.cert.CertPathBuilder.build(CertPathB uilder.java:238)
at sun.security.validator.PKIXValidator.doBuild(PKIXV alidator.java:280)
... 34 more