将CA签名的JKS密钥库转换为PEM

时间:2011-09-23 12:30:28

标签: openssl keystore keytool pem jks

我有一个由CA签署的证书的JKS密钥库。我需要以PEM格式导出它以便与nginx一起使用。我需要这样做,它包括整个链,以便我的客户可以验证签名。

如果我这样做:

keytool -exportcert -keystore mykestore.jks -file mycert.crt -alias myalias
openssl x509 -out mycert.crt.pem -outform pem -in mycert.crt -inform der

它只包含最低级别的证书。验证失败:

$ openssl s_client -connect localhost:443
CONNECTED(00000003)
depth=0 /O=*.mydomain.com/OU=Domain Control Validated/CN=*.mydomain.com
verify error:num=20:unable to get local issuer certificate
verify return:1
depth=0 /O=*.mydomain.com/OU=Domain Control Validated/CN=*.mydomain.com
verify error:num=27:certificate not trusted
verify return:1
depth=0 /O=*.mydomain.com/OU=Domain Control Validated/CN=*.mydomain.com
verify error:num=21:unable to verify the first certificate
verify return:1
---
Certificate chain
 0 s:/O=*.mydomain.com/OU=Domain Control Validated/CN=*.mydomain.com
   i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certificates.godaddy.com/repository/CN=Go Daddy Secure Certification Authority/serialNumber=123123
... (only one certificate!)
...
SSL-Session:
    ...
    Verify return code: 21 (unable to verify the first certificate)

来自Java:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

而具有相同JKS密钥库的Jetty会打印以下内容:

$ openssl s_client -connect localhost:8084
CONNECTED(00000003)
depth=2 /C=US/O=The Go Daddy Group, Inc./OU=Go Daddy Class 2 Certification Authority
verify error:num=19:self signed certificate in certificate chain
verify return:0
---
Certificate chain
 0 s:/O=*.mydomain.com/OU=Domain Control Validated/CN=*.mydomain.com
   i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certificates.godaddy.com/repository/CN=Go Daddy Secure Certification Authority/serialNumber=1234
 1 s:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certificates.godaddy.com/repository/CN=Go Daddy Secure Certification Authority/serialNumber=1234
   i:/C=US/O=The Go Daddy Group, Inc./OU=Go Daddy Class 2 Certification Authority
 2 s:/C=US/O=The Go Daddy Group, Inc./OU=Go Daddy Class 2 Certification Authority
   i:/C=US/O=The Go Daddy Group, Inc./OU=Go Daddy Class 2 Certification Authority
...
SSL-Session:
    Verify return code: 19 (self signed certificate in certificate chain)

尽管openssl返回了19个错误,但它不再是Java HttpsURLConnection的问题,而这就是我所关心的。

那么,如何以与nginx服务器和Java客户端兼容的格式(例如PEM)从JKS导出 整个链 ?我错过了什么?

3 个答案:

答案 0 :(得分:11)

我经常遇到的一个相当大的问题是,在生成CSR以获取我们的证书时,密钥库(Sun格式化的jks密钥库)不会输出.key或提供任何获取.key的工具。所以我总是得到一个.pem / .crt而无法在Apache2上使用它,它无法读取像Tomcat那样的JKS密钥库,而是需要一个未打包的.key + .pem / .crt对。

首先,获取现有密钥库的“副本”并跳至下面的第5个命令,或者创建自己的密钥库:

C:\Temp>keytool -genkey -alias tomcat -keyalg RSA -keystore
 keystore.jks -keysize 2048 -validity 730 -storepass changeit

然后,可选择创建一个2年的CSR,然后在下一个3个步骤中导入CSR响应:

C:\Temp>keytool -certreq -alias mydomain -keystore keystore.jks
 -file mydomain.csr
C:\Temp>keytool -import -trustcacerts -alias root -file
 RootPack.crt -keystore keystore.jks -storepass changeit
C:\Temp>keytool -import -trustcacerts -alias tomcat -file mydomain.response.crt
 -keystore keystore.jks -storepass changeit

要使其正常工作,并且您已经拥有用于Tomcat应用程序服务器的JKS密钥库文件,请执行以下步骤:

首先,将DER(二进制)格式的证书放入名为“exported-der.crt”的文件中:

C:\Temp>keytool -export -alias tomcat -keystore keystore.jks -file
 exported-der.crt

然后,查看&验证它:

C:\Temp>openssl x509 -noout -text -in exported-der.crt -inform der

现在您需要将其转换为PEM格式,这种格式在Apache和OpenSSL等应用程序中更广泛地用于PKCS12转换:

C:\Temp>openssl x509 -in exported-der.crt -out exported-pem.crt 
-outform pem -inform der

然后,下载并使用ExportPriv从密钥库中获取未加密的私钥:

C:\Temp>java ExportPriv <keystore> <alias> <password> > exported-pkcs8.key

现在您可能已经意识到,私钥正在导出为PKCS#8 PEM格式。要使其适用于与Apache(PKCS#12 ??)一起使用的RSA格式,您可以发出以下命令:

C:\Temp>openssl pkcs8 -inform PEM -nocrypt -in exported-pkcs8.key
 -out exported-pem.key

答案 1 :(得分:8)

您可以轻松地将JKS文件转换为PKCS12文件:

keytool -importkeystore -srckeystore keystore.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore keystore.p12

然后,您可以使用以下内容提取私钥和任何证书:

openssl pkcs12 -in keystore.p12

答案 2 :(得分:0)

我不确定是否可以使用keytool提取链,但可以使用小型Java程序来完成:

public void extract(KeyStore ks, String alias, char[] password, File dstdir) throws Exception
{
    KeyStore.PasswordProtection pwd = new KeyStore.PasswordProtection(password);
    KeyStore.PrivateKeyEntry entry = (KeyStore.PasswordKeyEntry)ks.getEntry(alias, pwd);
    Certificate[] chain = entry.getCertificateChain();
    for (int i = 0; i < chain.length; i++) {
        Certificate c = chain[i];
        FileOutputStream out = new FileOutputStream(new File(dstdir, String.format("%s.%d.crt", alias, i)));
        out.write(c.getEncoded());
        out.close();
    }
}

此代码应在提交的目录中以DER格式编写链的所有证书。

相关问题