将带有 subjectAltName (SAN) 的 X509 证书导入 JKS 密钥库
Import X509 certificate with subjectAltName (SAN) into JKS keystore
我正在使用 pyOpenSSL
创建 X509 证书。我需要将此证书导入 Java JKS 密钥库,以便我的 Java 应用程序可以使用它。只要我不向证书添加 subjectAltName
扩展名,这就可以正常工作。如果证书设置了备用主题,则导入 JKS 密钥库失败:
root@51561a8a1e01:~# /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)
root@51561a8a1e01:~#
如果我在命令行上使用 OpenSSL 打印此证书,我会得到以下输出:
root@51561a8a1e01:~# 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
扩展。
当我尝试使用 keytool 本身生成所有内容时 - 这似乎有效 - 我注意到 keytool 生成的证书有第二个扩展名 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
将此扩展添加到我的证书中。但是正确的值是多少?!
嗯,在写下这个问题的所有内容后,我注意到使用 pyOpenSSL
生成的证书与使用密钥工具生成的证书之间存在第二个区别。 keytool 证书声明 Version: 3 (0x2)
而另一个证书声明 Version: 1 (0x0)
.
我不太了解 X509 规范,但由于扩展都以 X509v3
为前缀,我猜扩展支持不适用于版本 1 证书。
并且在调整我的 python 代码以将版本设置为 3 之后(实际上是 2,因为版本是基于 0 的),导入到 keytool 中按预期工作:
_req = OpenSSL.crypto.X509Req()
_req.set_version(2)
...
我正在使用 pyOpenSSL
创建 X509 证书。我需要将此证书导入 Java JKS 密钥库,以便我的 Java 应用程序可以使用它。只要我不向证书添加 subjectAltName
扩展名,这就可以正常工作。如果证书设置了备用主题,则导入 JKS 密钥库失败:
root@51561a8a1e01:~# /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)
root@51561a8a1e01:~#
如果我在命令行上使用 OpenSSL 打印此证书,我会得到以下输出:
root@51561a8a1e01:~# 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
扩展。
当我尝试使用 keytool 本身生成所有内容时 - 这似乎有效 - 我注意到 keytool 生成的证书有第二个扩展名 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
将此扩展添加到我的证书中。但是正确的值是多少?!
嗯,在写下这个问题的所有内容后,我注意到使用 pyOpenSSL
生成的证书与使用密钥工具生成的证书之间存在第二个区别。 keytool 证书声明 Version: 3 (0x2)
而另一个证书声明 Version: 1 (0x0)
.
我不太了解 X509 规范,但由于扩展都以 X509v3
为前缀,我猜扩展支持不适用于版本 1 证书。
并且在调整我的 python 代码以将版本设置为 3 之后(实际上是 2,因为版本是基于 0 的),导入到 keytool 中按预期工作:
_req = OpenSSL.crypto.X509Req()
_req.set_version(2)
...