2011-09-23 76 views
11

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

如果我做这样的事情:

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密钥库打印如下:

$ 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的一个问题,这就是我所关心的。

所以,我怎么能出口整个产业链从JKS的格式(例如,PEM),它与两个nginx的服务器和Java客户端的工作?我错过了什么?

+0

回答最后一个问题,你问是我的回答如下结束。 – djangofan

回答

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格式提交的目录。

9

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

要开始,得到“复制”您现有的密钥库及以下跳到第五命令,或者创建自己这样的:

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

然后,可选,创建一个2年的CSR,然后进口CSR响应,在接下来的3个步骤:

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格式。为了得到它进入RSA格式与Apache(PKCS#12?)的作品,你可以发出以下命令:

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

您可以轻松地转换一个JKS文件转换成PKCS12文件:

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

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

openssl pkcs12 -in keystore.p12