<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Java%2Fkeystore</id>
	<title>Java/keystore - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Java%2Fkeystore"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Java/keystore&amp;action=history"/>
	<updated>2026-05-07T03:46:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=Java/keystore&amp;diff=4779&amp;oldid=prev</id>
		<title>Kenneth: /* Conversion from Apache PEM to Java Keytool */</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Java/keystore&amp;diff=4779&amp;oldid=prev"/>
		<updated>2018-06-24T03:06:32Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Conversion from Apache PEM to Java Keytool&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Notes ==&lt;br /&gt;
&lt;br /&gt;
READ: Dealing with java keystores - http://www.startux.de/index.php/java/44-dealing-with-java-keystoresyvComment44&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
keytool - Key and Certificate Management Tool [http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/keytool.html]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;The cacerts Certificates File&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;A certificates file named &amp;quot;cacerts&amp;quot; resides in the security properties directory, java.home\lib\security, where java.home is the runtime environment&amp;#039;s directory (the jre directory in the SDK or the top-level directory of the Java 2 Runtime Environment).&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The &amp;quot;cacerts&amp;quot; file represents a system-wide keystore with CA certificates. System administrators can configure and manage that file using keytool, specifying &amp;quot;jks&amp;quot; as the keystore type. The &amp;quot;cacerts&amp;quot; keystore file ships with several root CA certificates with the following aliases and X.500 owner distinguished names&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;The initial password of the &amp;quot;cacerts&amp;quot; keystore file is &amp;quot;changeit&amp;quot;. System administrators should change that password and the default access permission of that file upon installing the SDK.&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 # to create a custom keystore&lt;br /&gt;
 keytool -import -file MYCERT.cer -keystore /etc/keystore&lt;br /&gt;
 &lt;br /&gt;
 # to use the custom keystore&lt;br /&gt;
 -Djavax.net.ssl.trustStore=/etc/keystore&lt;br /&gt;
&lt;br /&gt;
 # Update default keystore&lt;br /&gt;
 cp /usr/java/jdk1.5.0_14/jre/lib/security/cacerts /usr/java/jdk1.5.0_14/jre/lib/security/cacerts.original&lt;br /&gt;
 keytool -import -file /root/tomcat.oeey.com.der.crt -keystore /usr/java/jdk1.5.0_14/jre/lib/security/cacerts&lt;br /&gt;
&lt;br /&gt;
Verisign’s New Intermediate CA and You [http://monduke.com/2006/10/]&lt;br /&gt;
&lt;br /&gt;
 Import the CA Certificate into cacerts&lt;br /&gt;
 &lt;br /&gt;
 keytool -import -keystore &amp;lt;JAVA_HOME&amp;gt;jre\lib\security\cacerts -file NewVerisignIntermediateCA.cer&lt;br /&gt;
 &lt;br /&gt;
 The default cacerts password is “changeit”.&lt;br /&gt;
 &lt;br /&gt;
 Import the Signed Certificate into the Keystore&lt;br /&gt;
 &lt;br /&gt;
 keytool -import -keystore &amp;lt;keystore&amp;gt; -alias &amp;lt;alias&amp;gt; -file cert.cer -trustcacerts&lt;br /&gt;
&lt;br /&gt;
Other References:&lt;br /&gt;
&lt;br /&gt;
Java&amp;#039;s Keytool and Client Digital Certificates [http://xmlgateway.its.utexas.edu/docs/java_digital_certs.html]&lt;br /&gt;
&lt;br /&gt;
==Quick Import==&lt;br /&gt;
To import key to custom keystore (use any password):&lt;br /&gt;
 $ keytool -import -keystore /keystore -alias localhost -trustcacerts -file localhost.der.crt&lt;br /&gt;
&lt;br /&gt;
To view that the key was imported to keystore (use password entered when&lt;br /&gt;
creating keystore):&lt;br /&gt;
 $ keytool -list -keystore /keystore&lt;br /&gt;
&lt;br /&gt;
To use custom keystore, append this to application startup parameters:&lt;br /&gt;
 -Djavax.net.ssl.trustStore=/keystore&lt;br /&gt;
&lt;br /&gt;
==Default Keystore==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&amp;quot;Obtaining a copy of the certificate in X.509 format requires Microsoft’s Internet Explorer. By placing the https URL into the browser window, a dialog will pop up requesting permission to accept the certificate. Click the View Certificate button and then the Details tab. In this tab, click the Copy to File button, then click Next and select the Base-64 encoded X.509 (.CER) option. After that, click Next to save the resulting file.&amp;quot; [http://www.testearly.com/2006/04/05/testing-with-untrusted-https/] &amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The default keystore is located:&lt;br /&gt;
 .../jre/lib/sercurity/cacerts&lt;br /&gt;
 password: changeit&lt;br /&gt;
&lt;br /&gt;
To import a certificate into the default keystore:&lt;br /&gt;
 cd .../jre/lib/security/&lt;br /&gt;
 keytool.exe -import -storepass changeit -file mycert.cer -keystore cacerts -alias mycert&lt;br /&gt;
&lt;br /&gt;
===Keytool===&lt;br /&gt;
View certificates&lt;br /&gt;
 $ keytool -list -keystore &amp;lt;keystore&amp;gt;&lt;br /&gt;
 $ keytool -list -v -keystore &amp;lt;keystore&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conversion from Apache PEM to Java Keytool ==&lt;br /&gt;
&lt;br /&gt;
Converting the certificate is easy.  Converting the private key is less so.&lt;br /&gt;
&lt;br /&gt;
*OpenSSL to Keytool Conversion tips [http://mark.foster.cc/kb/openssl-keytool.html]&lt;br /&gt;
**[http://mark.foster.cc/wiki/index.php/OpenSSL_to_Keytool_Conversion_tips OpenSSL to Keytool Conversion tips]&lt;br /&gt;
**[http://mark.foster.cc/wiki/index.php/Keytool_to_OpenSSL_Conversion_tips Keytool to OpenSSL Conversion tips]&lt;br /&gt;
*Conflicts OpenSSL versus KeyTool [http://forum.java.sun.com/thread.jspa?forumID=60&amp;amp;tstart=30&amp;amp;threadID=484622&amp;amp;trange=15]&lt;br /&gt;
*Using OpenSSL to set up your own CA [http://marc.info/?l=tomcat-user&amp;amp;m=106293430225790&amp;amp;w=2]&lt;br /&gt;
&lt;br /&gt;
The .crt files provided by the CA do not need conversion, as they are in a standard format.&lt;br /&gt;
&lt;br /&gt;
Convert openssl to keytool using Jetty:&lt;br /&gt;
 # convert to pkcs12&lt;br /&gt;
 openssl pkcs12 -export -in [domain].crt -inkey [domain].key -out keystore.pkcs12&lt;br /&gt;
&lt;br /&gt;
 # convert to java keystore (jks)&lt;br /&gt;
 # wget ftp://ftp.mortbay.org/pub/jetty-6.1.x/jetty-6.1.3.zip&lt;br /&gt;
 wget http://dist.codehaus.org/jetty/jetty-6.1.x/jetty-6.1.3.zip&lt;br /&gt;
 unzip jetty-6.1.3.zip&lt;br /&gt;
 java -classpath jetty-6.1.3/lib/jetty-6.1.3.jar org.mortbay.jetty.security.PKCS12Import keystore.pkcs12 keystore.jks&lt;br /&gt;
&lt;br /&gt;
 # verify&lt;br /&gt;
 keytool -list -v -keystore keystore.jks&lt;br /&gt;
&lt;br /&gt;
 # rename default alias of &amp;#039;1&amp;#039; to something else like &amp;#039;tomcat&amp;#039;&lt;br /&gt;
 keytool -keyclone -keystore keystore.jks -alias 1 -dest tomcat&lt;br /&gt;
 keytool -delete -keystore keystore.jks -alias 1&lt;br /&gt;
&lt;br /&gt;
Godaddy Converion (for mail and jira server with intermediate chain) [https://certs.godaddy.com/Tomcat.go]:&lt;br /&gt;
 keytool -import -keystore keystore.jks -alias godaddy -trustcacerts -file [godaddy].crt&lt;br /&gt;
 cp keystore.jks ~/.keystore&lt;br /&gt;
&lt;br /&gt;
 wget https://certs.godaddy.com/repository/gd_bundle.crt&lt;br /&gt;
 openssl pkcs12 -export -chain -CAfile gd_bundle.crt -in &amp;lt;name of your certificate&amp;gt; \&lt;br /&gt;
   -inkey &amp;lt;name of your certificate private key file&amp;gt; -out keystore.tomcat -name tomcat -passout pass:changeit&lt;br /&gt;
 &lt;br /&gt;
 # Add the following directives to the Connector tag:&lt;br /&gt;
   keystoreFile=&amp;lt;path to&amp;gt;/keystore.tomcat&lt;br /&gt;
   keystorePass=&amp;quot;changeit&amp;quot;&lt;br /&gt;
   keystoreType=&amp;quot;PKCS12&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you only need to import a certificate (not private key), this is easy: [http://stackoverflow.com/questions/2138940/import-pem-into-java-key-store]&lt;br /&gt;
 keytool -import -alias [alias] -keystore cacerts -file [cert.pem]&lt;br /&gt;
&lt;br /&gt;
Makefile:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
all:&lt;br /&gt;
        cat TrustedSecureCertificateAuthorityDV.crt USERTrustRSAAddTrustCA.crt AddTrustExternalCARoot.crt &amp;gt; bundle.crt&lt;br /&gt;
        rm tomcat.keystore&lt;br /&gt;
        openssl pkcs12 -export -chain -CAfile bundle.crt -in ssl.oeey.com.crt -inkey ssl.oeey.com.key -out tomcat.keystore&lt;br /&gt;
-name tomcat -passout pass:PASSWORD&lt;br /&gt;
        cp tomcat.keystore /etc&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Conversion from Java Keytool to Apache PEM ==&lt;br /&gt;
&lt;br /&gt;
[http://stackoverflow.com/questions/652916/converting-a-java-keystore-into-pem-format encryption - Converting a Java Keystore into PEM Format. - Stack Overflow]&lt;br /&gt;
&lt;br /&gt;
http://www.google.com/#sclient=psy&amp;amp;q=convert+java+keystore+to+pem&lt;br /&gt;
&lt;br /&gt;
 keytool -keystore foo.jks -genkeypair -alias foo \&lt;br /&gt;
        -dname &amp;#039;CN=foo.example.com,L=Melbourne,ST=Victoria,C=AU&amp;#039;&lt;br /&gt;
&lt;br /&gt;
 keytool -keystore foo.jks -exportcert -alias foo | \&lt;br /&gt;
       openssl x509 -inform der -text&lt;br /&gt;
&lt;br /&gt;
 keytool -importkeystore -srckeystore foo.jks \&lt;br /&gt;
       -destkeystore foo.p12 \&lt;br /&gt;
       -srcstoretype jks \&lt;br /&gt;
       -deststoretype pkcs12&lt;br /&gt;
&lt;br /&gt;
 openssl pkcs12 -in foo.p12 -out foo.pem&lt;br /&gt;
&lt;br /&gt;
 openssl x509 -text -in foo.pem&lt;br /&gt;
&lt;br /&gt;
 openssl dsa -text -in foo.pem&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Command summary - to create JKS keystore:&lt;br /&gt;
&lt;br /&gt;
 keytool -keystore foo.jks -genkeypair -alias foo \&lt;br /&gt;
    -dname &amp;#039;CN=foo.example.com,L=Melbourne,ST=Victoria,C=AU&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Command summary - to convert JKS keystore into PKCS#12 keystore, then into PEM file:&lt;br /&gt;
&lt;br /&gt;
 keytool -importkeystore -srckeystore foo.jks \&lt;br /&gt;
   -destkeystore foo.p12 \&lt;br /&gt;
   -srcstoretype jks \&lt;br /&gt;
   -deststoretype pkcs12&lt;br /&gt;
&lt;br /&gt;
 openssl pkcs12 -in foo.p12 -out foo.pem&lt;br /&gt;
&lt;br /&gt;
if you have more than one certificate in your JKS keystore, and you want to only export the certificate and key associated with one of the aliases, you can use the following variation:&lt;br /&gt;
&lt;br /&gt;
 keytool -importkeystore -srckeystore foo.jks \&lt;br /&gt;
   -destkeystore foo.p12 \&lt;br /&gt;
   -srcalias foo \&lt;br /&gt;
   -srcstoretype jks \&lt;br /&gt;
   -deststoretype pkcs12&lt;br /&gt;
&lt;br /&gt;
 openssl pkcs12 -in foo.p12 -out foo.pem&lt;br /&gt;
&lt;br /&gt;
Command summary - to compare JKS keystore to PEM file:&lt;br /&gt;
&lt;br /&gt;
 keytool -keystore foo.jks -exportcert -alias foo | \&lt;br /&gt;
   openssl x509 -inform der -text&lt;br /&gt;
&lt;br /&gt;
 openssl x509 -text -in foo.pem&lt;br /&gt;
&lt;br /&gt;
 openssl dsa -text -in foo.pem&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Well, [http://gagravarr.org/writing/openssl-certs/general.shtml OpenSSL should do it handily] from a #12 file:&lt;br /&gt;
 openssl pkcs12 -in pkcs-12-certificate-file -out pem-certificate-file&lt;br /&gt;
 openssl pkcs12 -in pkcs-12-certificate-and-key-file -out pem-certificate-and-key-file&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
*[http://www.testearly.com/2006/04/05/testing-with-untrusted-https/ Testing with untrusted Https]&lt;br /&gt;
&lt;br /&gt;
[http://article.gmane.org/gmane.comp.java.jasig.cas.devel/468 Sample of PKIX error]&lt;br /&gt;
&lt;br /&gt;
==How to Import a Certificate into your Java Keystore==&lt;br /&gt;
How to Import a Certificate into your Java Keystore&amp;lt;br/&amp;gt;&lt;br /&gt;
Author: Kenneth Burgener&amp;lt;br/&amp;gt;&lt;br /&gt;
Created: 2007.06.28&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Summary===&lt;br /&gt;
When you attempt to access a website protected by SSL (HTTPS) that is not trusted, the browser is kind enough to present you with a warning message, and give you the opportunity to accept the certificate.  Java, by default, is not as nice.  The default behavior for the Java connector attempting to access an untrusted certificate is to throw an exception.  This exception generally looks like this:&lt;br /&gt;
&lt;br /&gt;
 Caused by: javax.net.ssl.SSLHandshakeException :&lt;br /&gt;
   sun.security.validator.ValidatorException: PKIX path building failed:&lt;br /&gt;
   sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target&lt;br /&gt;
&lt;br /&gt;
To resolve this issue, there are two options.  You can either override the Java connector behavior (requires modifying code), or you can import the untrusted certificate into a keystore and implicitly trust the certificate.&lt;br /&gt;
&lt;br /&gt;
===Override Java Connector Behavior===&lt;br /&gt;
Since this is probably not the preferred method I will simply provide some references here.  One example of why you may want to change the default behavior would be if you would like to provide a prompt to the user asking if they wish to accept the certificate, much as any Internet browser would.&lt;br /&gt;
&lt;br /&gt;
====References====&lt;br /&gt;
Customizing SSL in HttpClient&amp;lt;br/&amp;gt;&lt;br /&gt;
http://jakarta.apache.org/commons/httpclient/sslguide.html&lt;br /&gt;
&lt;br /&gt;
SSL and Self Signed Certs&amp;lt;br/&amp;gt;&lt;br /&gt;
http://weblogs.java.net/blog/rbair/archive/2006/10/ssl_and_self_si_1.html&lt;br /&gt;
&lt;br /&gt;
How to make JVM to accept self-sign certificate from server&amp;lt;br/&amp;gt;&lt;br /&gt;
http://forum.java.sun.com/thread.jspa?threadID=735919&amp;amp;messageID=4228600&lt;br /&gt;
&lt;br /&gt;
Java Certification Path API Programmer&amp;#039;s Guide&amp;lt;br/&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/guide/security/certpath/CertPathProgGuide.html&lt;br /&gt;
&lt;br /&gt;
===Importing Certificate into a Keystore===&lt;br /&gt;
A keystore is simply a Java container file which contains certificates and private keys. To import a certificate into a keystore, you will use &amp;quot;keytool&amp;quot;.  Key management in Java is done with the &amp;quot;keytool&amp;quot; application.&lt;br /&gt;
&lt;br /&gt;
keytool - Key and Certificate Management Tool&amp;lt;br/&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/keytool.html&lt;br /&gt;
&lt;br /&gt;
The first step will be to obtain a copy of the certificate you wish to import.  This is done easily with Internet Explorer.  Simply visit the site you wish to obtain the certificate from.  Since the certificate is untrusted, you will be presented with a warning message.  Accept the warning.  The next step is to click on the &amp;quot;lock&amp;quot; icon in your browser (bottom right of window in IE6, top next to address bar in IE7).&lt;br /&gt;
&lt;br /&gt;
Clicking on the lock will bring up a short message.  Click on the &amp;quot;View Certificate&amp;quot; link.  The first tab, titled &amp;quot;General&amp;quot; gives you the option to import the certificate into Internet Explorer&amp;#039;s trusted list.  This would be a good time to do this if you wish to be able to visit the site in the future without warnings.  The second tab titled &amp;quot;Details&amp;quot; has a &amp;quot;Copy to File&amp;quot; button.  Click on the &amp;quot;Copy to File&amp;quot; button to begin the certificate export wizard.  Click next to be presented with the &amp;quot;Export File Format&amp;quot; options.  Select &amp;quot;Base-64 encoded X.509 (.CER)&amp;quot; option and click next.  Give the file a name and a location (ie. C:\mycert.cer) and click next.  Click the finish button and the file will be exported.  Now that we have a certificate we can import it into a keystore.&lt;br /&gt;
&lt;br /&gt;
There are two keystore options for storing a certificate.  We can import the certificate into a keystore that we create, and tell our application where to find the keystore in the startup parameters.  The second option is to import the certificate into the JRE&amp;#039;s default keystore.&lt;br /&gt;
&lt;br /&gt;
====Import Certificate to Custom Keystore====&lt;br /&gt;
To create a custom keystore we will use the keytool application as such:&lt;br /&gt;
 keytool -import -keystore c:\keystore -file c:\mycert.cer -alias mycert&lt;br /&gt;
&lt;br /&gt;
The location of the keystore file is arbitrary.  The alias simply needs to be a unique name in the keystore.  This alias is what will be used to reference the certificate in the future.&lt;br /&gt;
&lt;br /&gt;
To use the keystore with your Java application, simply append the following parameter: &lt;br /&gt;
 -Djavax.net.ssl.trustStore=c:\keystore&lt;br /&gt;
&lt;br /&gt;
If your application needs to modify the keystore, you will probably need to append the keystore password paremeter as well:&lt;br /&gt;
 -Djavax.net.ssl.trustStorePassword=XXX&lt;br /&gt;
&lt;br /&gt;
Now your application should work without SSL warning issues.&lt;br /&gt;
&lt;br /&gt;
====Import Certificate to Default Keystore====&lt;br /&gt;
The Java JVM contains a default keystore which contains many root level CAs (Certificate Authorities), such as Verisign and Thawte.  Certificates that are signed by these CAs are implicitly trusted by Java.  We can add our self signed certificate to this keystore, which will then allow our certificate to be trusted by any Java application that uses this JVM.  This keystore is located here:&lt;br /&gt;
 $JVM_PATH/lib/security/cacerts&lt;br /&gt;
&lt;br /&gt;
This could be something like one of the following, or it could be within your application&amp;#039;s folder if a JVM was installed with the application:&lt;br /&gt;
 C:\Program Files\Java\jdk1.5.0_08\jre\lib\security\cacerts&lt;br /&gt;
 C:\Program Files\Java\jre1.6.0_01\lib\security\cacerts&lt;br /&gt;
&lt;br /&gt;
To import a certificate to the default keystore we will use the keytool application as such:&lt;br /&gt;
 keytool -import -keystore C:\Program Files\Java\jdk1.5.0_08\jre\lib\security\cacerts&lt;br /&gt;
     -file c:\mycert.cer -alias mycert -storepass changeit&lt;br /&gt;
&lt;br /&gt;
The cacerts keystore contains a default password of &amp;quot;changeit&amp;quot;.  The location of the cacerts keystore file is dependent on the location of your JVM.  The alias simply needs to be a unique name in the keystore.  This alias is what will be used to reference the certificate in the future.&lt;br /&gt;
&lt;br /&gt;
Now your application should work without SSL warning issues.&lt;br /&gt;
&lt;br /&gt;
===Other References===&lt;br /&gt;
Testing with untrusted Https&amp;lt;br/&amp;gt;&lt;br /&gt;
http://www.testearly.com/2006/04/05/testing-with-untrusted-https/&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
[[SSL Certificates]] [[openssl]] [[keytool]] [[Java Keystore]]&lt;br /&gt;
&lt;br /&gt;
== keywords ==&lt;br /&gt;
&lt;br /&gt;
[[Category:Java]]&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>