java - 如何将指纹添加到 keystore

标签 java ssl ssl-certificate keystore

写入 SSL 套接字时出现以下异常

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

目标服务器的证书似乎不受信任。我在 Windows 7 机器、jdk7 和 tomcat7 上测试了我的实现,没有任何问题。在带有 openJDK 6 和 tomcat7 的 ubuntu 10 LTS 上抛出异常。我获得了套接字目标服务器的 sha1 和 md5 指纹。我尝试流式传输的服务器的证书在我的 ubuntu 服务器上不受信任,对吗?我可以将指纹添加到 tomcats keystore 中吗?如果是这样,我该怎么做?

最佳答案

您需要添加到信任存储区的不是指纹,而是实际的证书。

您可以添加服务器证书本身或添加链中的 CA 证书之一(如果您希望信任来自该 CA 的所有证书,而不仅仅是该特定服务器)。

要了解证书是什么,您可以使用 OpenSSL:

openssl s_client -showcerts -connect your.host.name:443

(将主机名和 443 替换为您正在使用的实际端口。)

--BEGIN/END CERT...--之间的 block 是PEM格式的证书。您可以使用 openssl x509 -text -noout 检查其内容(并将每个 block 粘贴到此处)。

将要导入的证书保存到纯文本文件中(例如 certificate.pem)。您应该只导入您信任的证书。这里有一定的信念飞跃。 (例如,您可能需要连接浏览器并检查 key Material 是否匹配。)

要导入到您的信任库中,请使用:

keytool -importcert -keystore truststore.jks -file certificate.pem

(您可能需要使用 -alias some_name_you_choose 指定别名。)

如果您希望这影响您的默认信任库,请将 truststore.jks 替换为 Java 主目录中 lib/security/cacerts 的路径(默认密码应为是changeit)。


由于目标服务器似乎来自知名的 CA(并且它适用于某些版本的 JRE),因此最简单的修复方法当然是手动更新您的 cacerts 文件,采取从与其一起工作的 JRE 中复制。毕竟,作为JSSE Reference Guide says :

IMPORTANT NOTE: The JDK ships with a limited number of trusted root certificates in the /lib/security/cacerts file. As documented in keytool, it is your responsibility to maintain (that is, add/remove) the certificates contained in this file if you use this file as a truststore.

Depending on the certificate configuration of the servers you contact, you may need to add additional root certificate(s). Obtain the needed specific root certificate(s) from the appropriate vendor.


事实证明,这肯定是 certificate chain order 的问题。 (在本网站上这是不正确的),如Qualys SSL Labs tester所示.

关于java - 如何将指纹添加到 keystore ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10316127/

相关文章:

java - 为什么 getClass 返回一个 Class<?扩展 |X|>?

node.js - 从 Nodemailer 到 Postfix 的加密连接失败,出现 "SSL23_GET_SERVER_HELLO:unknown protocol"

android - Cordova SSL 固定错误 500 : "There was an error with the request"

c# - 获取网站 SSL 证书的公钥

java - Kafka 不会从 PEM 证书开始

java - 无法从 Rest 方法发送响应

java - 在 Java 中创建对象时会发生什么?

java - Java 中的 URL 格式

c++ - 来自自定义库的 Openssl 和 gsoap

ssl - 远程登录到 gmail.com