Java: ssl certificates for testing on windows? - java

I'm looking at coding up a very simple server using an ssl connection - but had two questions
How can i use the java keytool in windows to generate a certificate I can use for testing? Most tutorials i can see (for echoservers etc) suggest i run the following command, which doesnt appear to work in the command prompt (running win 7)?
keytool -keystore mySrvKeystore -keypasswd 123456 -genkey -keyalg RSA -alias mycert
Also considering ill be doing some basic AES and RSA based encryption - is there an advantage in using bouncy castle instead of the tools provided off the shelf in the JCE?
Many thanks,

The -genkey option must come first.
If JCE supports the algorithms you want to use (and it does support AES and RSA), switching to BouncyCastle is extra hassle with no benefit. BouncyCastle would be a great choice if you need something like ECDH, or if you want to use its S/MIME or PGP libraries.

If you already have a private key in PEM or DER format and a signed certificate for it, you may use pkeytool to create keystore file from it ( basically import private key into pkcs file format ).

Related

Java Webstart: howto share certificate with users

I developed an application that uses Java Webstart. It needs write access to the disk so it has to be signed, but it would only be used by a limited group of users so I don't want to pay for a trusted certificate.
What is the easiest option to let the users trust my application/self signed certificate? I don't want them to use keytool because it is not really user friendly. I though about creating a certificate that can be imported via the Java Control Panel and first tried creating an importable certificate using the following commands:
keytool -genkeypair -keystore patrickgotthard.jks -alias patrickgotthard
keytool -exportcert -keystore patrickgotthard.jks -alias patrickgotthard -file patrickgotthard.cer
But it is not possible to import the generated .cer file. Then I found Cannot import certificate into java control panel and used the following command:
keytool -genkey -alias patrickgotthard -keystore patrickgotthard.p12 -storetype pkcs12
But as far as I understand, the .p12 file also contains my private key - I don't think that the private key should be shared?! Can you tell me how to create a self signed certificate that can be imported via Java Control Panel but does not contain my private key? Or do you know a better solution for my problem?
In this scenario, it may be sufficient to check the SHA1 fingerprint associated with the self-signed certificate used to sign the JAR:
Sign the JAR with your self-signed certificate.
Use keytool -v -list to determine your certificate's fingerprint.
Communicate the certificate's fingerprint in a mutually agreed way.
Instruct user's to add your secure site to the Exception Site List in the Security tab of the Java Control Panel; this allows the user to retain the minimum recommended security level setting, High.
At the security prompt, click on More Information to compare the communicated fingerprint with the one received.
This approach does not confer trust, but it reduces the risk of the user accepting an altered JAR.
Addendum: The article Self-signed certificates for a known community discusses how to export a self-signed certificate. Members of the community can then import the certificate as warranted.
I was developing Java Web Start app years ago with NetBeans. It did everything for me automatically.
You just have to put permissions tags in JNLP file.
Also 6 month after I had problems to create a new self-signed certificate.
But self signed app still works. (many years after, no modification, certs to install)
Clients do not have to install any certificates.
It just warns everytime when it starts that is signed with self signed cert.
Another option is to have this "limited group of users" lower the security level for Java. In Windows, you go to the Control Panel->Java and then one of the tabs is for Security (sorry on my mac right now and it is not quite the same). I believe that if you push that security level lower to Medium, you can get away with the self-signed certificate.

Convert PKCS7 to PKCS12 using java.security.KeyStore

When I try to import a renewed X509 certificate on Chrome / Firefox (and probably other browser), it does not recognize that the certificate I am trying to import uses the same private key stored in my expiring user certificate with the same DN. I don't know if this is a bug with the implementation of certificate import tool of all major browsers, but I can (somewhat) work around this problem by storing my user certificate inside a private key-less PKCS12 file using openssl tool like following.
openssl pkcs12 -export -in usercert.pem -nokeys -name "CN: Same Name" -out certonly.p12
(-nokeys is the trick here)
I don't know why this works, but I can then import certonly.p12 to Chrome / Firefox and it will attach my private key from the last year; although it creates a separate certificate entry so that I need to remove my old certificate manually.
So, I assume that, in order to renew my user certificate on my browser, I will need to package my certificate in PKCS12 format (with or without a pass-phrase).
Now, I need to do this in my Java application using java.security.KeyStore or similar libraries, because it is actually my web application which is generating user's renewed certificates. I don't want to ask our users to use the openssl command themselve to convert it to pkcs12 before importing to their browsers. My application should do this automatically, and provide them the renewed certificate contained inside a PKCS12 file.
I've looked many places, but so far I can't find a concrete example of how to do this using Java. Does anyone know how to output a pass-phrase less PKCS12 with only an issued certificate (or pkcs7) without a private key?
I tried something like following but it did not work.
java.security.cert.Certificate[] chain = CertificateManager.parsePKCS7(renewed_cert_in_pkcs7);
KeyStore p12 = KeyStore.getInstance("PKCS12");
p12.load(null, null);
p12.setKeyEntry("USER Cert 123", null, "".toCharArray(), chain);
response.setContentType("application/x-pkcs12");
response.setHeader("Content-Disposition", "attachment; filename=user_certificate_only.p12");
p12.store(response.getOutputStream(), "".toCharArray());

Converting .jks truststores and keystores to ios

I have created a java webservice that is going to be communicating with iPads using restlet on the server side that communicates over HTTPS with mutual authentication. I have generated two .jks keystores using this guide
http://www.herongyang.com/JDK/ssl_client_auth.html
I have implemented a client for testing purpose in java and everything worked out fine.
I assume that it isn't possible to use the format .jks in ios so should I convert the client.jks to a pkcs12 file in order to make it compatible with ios?
I am having trouble finding information about this.
Thanks!
If I understand your situation correctly, and I may not, you shouldn't need to change the .jks at all. The KeyStore for your application is just a container of certificates used by your system. To achieve certificate integration on IOS you may have to add the client and ca certs to your local system key chain ( check out : iOS: Pre install SSL certificate in keychain - programmatically ) but the JKS itself should transfer to the other platform with no modifications required.
Also, here is an example of using a keystore on IOS to do apple push notifications. iOS Push Notification - JavaPNS - keystore.p12 file security If you want to do it exactly the same way that this person did (using a local p12 rather than a loaded store) just follow the instructions over here: Converting .jks to p12
For more information about the differences between these files check out: Difference between .keystore file and .jks file
Best of luck with your project.
Converting the .jks to pkcs12 sounds like a good bet. The certificates generated by iOS provisioning portal can be exported (by KeyChain Access) to pkcs12 format, so it's safe to assume this format is compatible with iOS.
You can use keytool to convert your jks to pkcs12. I used it in the opposite direction (converted a pkcs12 file obtained from Apple to jks), and it should work with no problems in your case too.
This command should do the trick :
keytool -importkeystore -srckeystore input.jks -destkeystore output.p12 -srcstoretype JKS -storetype PKCS12

Creating an SSL Connection in Java

I looked around and did not see any questions that fully answered what I wanted, though if this is a duplicate, point me to the question and I will be on my way.
Right now I am trying to write a Java server that will receive data from an SSLServerSocket and for now, just print it out. I would eventually like to have this data come from an Android, but right now it throws an SSLException before it even starts listening for data.
code:
System.setProperty("javax.net.ssl.keyStore","C:\\ProgramFiles\\jre6\\bin\\server.jks");
System.setProperty("javax.net.ssl.keyStorePassword","password");
SSLServerSocketFactory factory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
ServerSocket ss = factory.createServerSocket(6543);
Socket s = ss.accept();
There is more code after that to process it, but it gets hung up and throws the exception there, so I'm not sure posting it will help any, but if it will, just comment for it.
I created the certificate following a tutorial for openssl on Ubuntu and transferred it over and created my keystore using:
keytool -import -file "C:\Documents and Settings\matt\Desktop\server.crt" -keystore server.jks
I can easily admit that I don't fully understand how a large portion of this works, so any help would be appreciated. Also, I suppose I am going to leave it outside the scope of this question becauseI feel like this is a pretty big question on its own, butI would also like some insight as to how to connect the client if possible. Sorry for all the trouble and thanks ahead of time for all the help.
EDIT:
the tutorial I followed is here:
http://www.akadia.com/services/ssh_test_certificate.html
Thanks again!
EDIT:
The Exception being throw is:
javax.net.ssl.SSLException: No available certificate or key corresponds to the SSL cipher suites which are enabled
I tried to Google the exception and most everything was a tutorial describing how to create a keystore (which I am under the impression that I already have). I will continue to sift through these search results.
Thanks!
When you create a keystore like this, you only put a certificate in your keystore:
keytool -import -file "server.crt" -keystore server.jks
What you need is to have a private key + a certificate.
Either you import them from somewhere else if you already have a certificate issued by a Certification Authority, or you can create a self-signed certificate if it's for limited use.
If the certificate you've created with OpenSSL is self-signed (or from a mini CA for your own use, e.g. with CA.pl), it's probably not worth the trouble of doing the conversion. You might as well generate a self-signed certificate directly with keytool. See the "Generating Your Key Pair" example in the official keytool documentation:
keytool -genkeypair -dname "cn=Mark Jones, ou=JavaSoft, o=Sun, c=US"
-alias business -keypass kpi135 -keystore /working/mykeystore
-storepass ab987c -validity 180
Make sure you use cn=your.fqdn.host.name (or cn=localhost if it's for local tests only). (I think keytool provided with Java 7 also has support for subject alternative names, which would be better.)
If you already have a private key + certificate you want to re-use in PKCS#12 format (usually .p12 file), you can import it using the method described in this question.
If what you've produced with OpenSSL is in PEM format, it might be easier to bundle them in a PKCS#12 file with OpenSSL and then import them as above. This can be done with this:
openssl pkcs12 -export -in cert.pem -inkey key.pem -out creds.p12

Java Sign jars with server certificate

Is it possible to use a server certificate to sign a java web start app? What I want to know is if will it work. I have a trusted certificate for my server, and I would like to reuse the same certificate to sign an app.
Right now, I have this warning:
This jar contains entries whose signer certificate's ExtendedKeyUsage
extension doesn't allow code signing. This jar contains entries whose
signer certificate's NetscapeCertType extension doesn't allow code
signing.
Will I be able to launch my app without the warning that the certificate is not trusted?
You will get warning if you don't use a code signing certificate. For most CA, code signing cert costs more than the server cert. In my opinion, this is just a marketing scheme to make you to pay for another cert. There is no technical difference between two certs. Some CA may provide combo deals with usage for both.
I assume you have created the JKS file using the KEY and CRT of your SSL and hence you get the error..
I have a simple solution here:
As you know you can create a JKS using the following command
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias
and when you use this JKS you get self signed certification message which is absolutely fine to make the app live at Google play store.. But buying code signing certificate is good if you can afford it ..

Categories

Resources