0

I am trying to do a POST call to a secure website:

Steps:

1) Go to https://www.mywebsite.com (For reference)

2) Export the keystore of the above website to C:\Program Files\Java\jre7\lib\security.

3) Import it to cacerts to add a self signed signature using keytool as

keytool -import -alias my-cert -file c:\cert.crt -keystore %JAVA_HOME%/jre/lib/security/cacerts

After doing the above steps I used the following program:

String keystorePathAndName = "C:\\Program Files\\Java\\jre7\\lib\\security\\mykeystore";
String keystoreType = "der";

String keystorePass = "";

Integer port = 8089;
if (System.getProperty("port") != null) {
    port = Integer.valueOf(System.getProperty("port"));
}
// Create a server listening on port 8089
Component component = new Component();
Server server = component.getServers().add(Protocol.HTTPS, port);
Series<Parameter> params = server.getContext().getParameters();
params.add("keystorePath", keystorePathAndName);
params.add("keystoreType", keystoreType);

params.add("keystorePassword", keystorePass);


component.getDefaultHost().attach("/Simulator",new UserApplication());
component.start();

Error Logs:

Exception in thread "main" java.security.KeyStoreException: der not found
    at java.security.KeyStore.getInstance(KeyStore.java:616)
    at org.restlet.engine.ssl.DefaultSslContextFactory.createSslContext(DefaultSslContextFactory.java:299)
    at org.restlet.engine.connector.HttpsServerHelper.start(HttpsServerHelper.java:92)
    at org.restlet.Server.start(Server.java:579)
    at org.restlet.Component.startServers(Component.java:642)
    at org.restlet.Component.start(Component.java:567)
    at com.user.app.userapp.main.AppServer.main(AppServer.java:78)
Caused by: java.security.NoSuchAlgorithmException: der KeyStore not available
    at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
    at java.security.Security.getImpl(Security.java:695)
    at java.security.KeyStore.getInstance(KeyStore.java:613)
    ... 6 more

UPDATE:

In response to the Lolo's answer, I am not getting PKCS12 OR JKS formats when trying to save the keystore.

enter image description here

sjain
  • 23,126
  • 28
  • 107
  • 185

1 Answers1

2

The answer is in the exception message: "der not found" upon calling KeyStore.getInstance() means that der is not a known format for your key store. You shoud try one of the known formats, such as JKS (the default for Oracle's JVM) or PKCS12. See this other post for more details on keystore formats.

Edit:

As shown in your screenshot, the file you are saving is a certificate file. Assuming that you then imported it into a truststore using Oracle's 'keytool' and that you didn't specify the truststore format, then the default format JKSfor Oracle's JVM should be used in your code, instead of der:

String keystoreType = "JKS";
Community
  • 1
  • 1
Lolo
  • 4,277
  • 2
  • 25
  • 24
  • Please check my update in the question. My save dialog is not giving me the options to save as type `JKS` or `PKSC12`. – sjain Apr 20 '15 at 09:05
  • I'm confused. Your screenshot shows that you're saving a certificate, not a key- or trust- store. You need to effectively save the certificate file (which may have the ".der" extension), _then import it into a truststore_ with an appropriate format (using `keytool`). Or am I missing something? – Lolo Apr 20 '15 at 09:30
  • yes this is what I am doing. I saved it as a `.der` and then imported using keytool `keytool -import -alias my-cert -file c:\cert.crt -keystore %JAVA_HOME%/jre/lib/security/cacerts` then my code gives exception as I mentioned `NoSuchAlgorithmException: der KeyStore not available`. – sjain Apr 20 '15 at 09:42