Java keytool 从 url/port 添加服务器证书的简单方法(对于 Windows)

Java keytool easy way to add server cert from url/port (for Windows)

我想将 CA 证书导入 Java 密钥库。 我发现 this answer 有一些 Unix 命令。

我知道 keytool 在 Windows 中的工作方式与在 Linux 中的工作方式相同,不是吗?

但是 openssl 呢?

我在 Windows 中安装了 openssl,然后 运行 它可以正常工作。但是如何替换下面的代码?

</dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert

或者我可以用其他东西替换 openssl 吗?

可能最简单的方法是使用 KeyStore Explorer。只需使用 "Examine SSL" 功能,然后单击 "Import":

或者,您可以将 </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert 替换为手动步骤。如果你执行

openssl s_client -connect -connect www.google.com:443

它会输出这样的东西:

depth=2 OU = GlobalSign Root CA - R2, O = GlobalSign, CN = GlobalSign
verify return:1
depth=1 C = US, O = Google Trust Services, CN = Google Internet Authority G3
verify return:1
depth=0 C = US, ST = California, L = Mountain View, O = Google LLC, CN = www.google.com
verify return:1
---
Certificate chain
 0 s:/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
   i:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
 1 s:/C=US/O=Google Trust Services/CN=Google Internet Authority G3
   i:/OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
---
Server certificate
-----BEGIN CERTIFICATE-----
MIIEgjCCA2qgAwIBAgIIZ20sUt50wGgwDQYJKoZIhvcNAQELBQAwVDELMAkGA1UE
BhMCVVMxHjAcBgNVBAoTFUdvb2dsZSBUcnVzdCBTZXJ2aWNlczElMCMGA1UEAxMc
...
htkxJVFaLUXScpkNQagWzehkj9BGdV4oztKMgTU8hcZEsiDKF0aZmaMfAXAF6u0r
0n9sKu3Ap0uSR0+G4PVDcJkJzw2UiUsu8IkkHy3HjKt4zCCrATjZ4FPgtFHSWv8d
ZmUdWJhgJ30s/EvOKn1uQ6QlPWaCJKc9W6JaJQTX6PjPDMs0sK90ss9vCIJ0dDw5
ud+EC4cT
-----END CERTIFICATE-----
subject=/C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
issuer=/C=US/O=Google Trust Services/CN=Google Internet Authority G3
---
No client certificate CA names sent

标记从 -----BEGIN CERTIFICATE----------END CERTIFICATE----- 的行并将它们复制到文本编辑器中。将其保存为 [host].crt。然后执行keytool命令。

如果您想在脚本中自动执行这些步骤,则必须安装 Cygwin or one of the suggestions in this answer here