2016-08-26 182 views
0

我使用pyOpenSSL創建X509證書。我需要將此證書導入到Java JKS密鑰庫中,以使其可用於我的Java應用程序。只要我不向證書添加subjectAltName擴展名,就可以正常工作。如果證書具有替代主體集合,導入到JKS密鑰庫失敗:將subjectAltName(SAN)的X509證書導入JKS密鑰庫

[email protected]:~# /opt/oracle/java/jdk64-1.8.0_92/bin/keytool -keystore keystore -storepass changeit -noprompt -importcert -alias example -file certificate.crt -v 
keytool error: java.lang.Exception: Input not an X.509 certificate 
java.lang.Exception: Input not an X.509 certificate 
    at sun.security.tools.keytool.Main.doCommands(Main.java:1009)655) 
    at sun.security.tools.keytool.Main.main(Main.java:336) 
[email protected]:~# 

如果我打印在命令行上使用OpenSSL這個證書,我得到這樣的輸出:

[email protected]:~# openssl x509 -in certificate.crt -text -noout 
Certificate: 
    Data: 
     Version: 1 (0x0) 
     Serial Number: 0 (0x0) 
    Signature Algorithm: sha256WithRSAEncryption 
     Issuer: OU=example.com, CN=my-server.example.com, O=example.com 
     Validity 
      Not Before: Aug 26 12:03:03 2016 GMT 
      Not After : Aug 25 12:03:03 2021 GMT 
     Subject: OU=example.com, CN=my-server.example.com, O=example.com 
     Subject Public Key Info: 
      Public Key Algorithm: rsaEncryption 
       Public-Key: (2048 bit) 
       Modulus: 
        00:cc:a7:53:5a:38:...:11:2f 
       Exponent: 65537 (0x10001) 
     X509v3 extensions: 
      X509v3 Subject Alternative Name: 
       DNS:localhost 
    Signature Algorithm: sha256WithRSAEncryption 
     ab:51:12:fb:a6:a6:...:0d:4b 

這是證書顯然是有效的。根據oracle's documentation,Java 8 keytool應該支持SubjectAlternativeName擴展。

當我試圖生成密鑰工具本身的一切 - 這似乎工作 - 我注意到,通過密鑰工具生成的證書具有第二擴充X509v3 Subject Key Identifier

Certificate: 
    Data: 
     Version: 3 (0x2) 
     Serial Number: 1510484556 (0x5a082a4c) 
    Signature Algorithm: sha256WithRSAEncryption 
     Issuer: O=example.com, OU=example.com, CN=my-server.example.com 
     Validity 
      Not Before: Aug 26 12:52:43 2016 GMT 
      Not After : Nov 24 12:52:43 2016 GMT 
     Subject: O=example.com, OU=example.com, CN=my-server.example.com 
     Subject Public Key Info: 
      Public Key Algorithm: rsaEncryption 
       Public-Key: (2048 bit) 
       Modulus: 
        00:99:b6:b1:11:a6:...:7b:39 
       Exponent: 65537 (0x10001) 
     X509v3 extensions: 
      X509v3 Subject Alternative Name: 
       DNS:localhost 
      X509v3 Subject Key Identifier: 
       66:75:AD:7A:A5:19:AB:43:DE:55:E4:A7:4F:C2:3D:53:55:49:CE:48 
    Signature Algorithm: sha256WithRSAEncryption 
     50:7c:fe:c8:5d:1b:...:da:27 

我需要這個擴展添加到我的證書也使用pyOpenSSL。但是,什麼是正確的價值?!

回答

1

那麼,在寫下這個問題的一切之後,我注意到在使用pyOpenSSL生成的證書和keytool之間存在第二個差異。 keytool證書狀態Version: 3 (0x2),而另一個說Version: 1 (0x0)

我沒有太多的X509規格,但作爲擴展名前綴X509v3我想這擴展支持不適用於版本1證書。

和適應我的Python代碼的版本設置爲3後(實際上2爲版本爲0爲主),導入到密鑰工具按預期工作:

_req = OpenSSL.crypto.X509Req() 
_req.set_version(2) 
...