创建java识别的证书链的过程

问题描述

tl;dr - 获取 KeyStore Explorer (http://keystore-explorer.org/) 并为自己省去麻烦。

附言- 密钥库别名设置对于某些 Java 应用程序非常重要(例如:iDempiere 在其 Jetty 提供程序 ssl 配置 (. ./jettyhome/etc/jetty-ssl-context.xml) 中。在这些情况下,必须确保别名java 正在寻找的证书的名称与它实际用来查找它的别名相匹配。

O.P.

我需要在 Java 应用程序中使用私有 CA 及其证书。我无法发现如何将私有 CA 根证书及其中间体添加到 Java 受信任证书中。我已经找到并阅读了多篇关于如何完成这项工作的文章,但我的努力无法实现我所需要的。

我正在使用 OpenJDK11。 java cacerts 文件位于/usr/local/openjdk11/lib/security/cacerts。这,我相信包含 Java 使用的可信证书列表。

我已手动将私有 CA 根证书和中间证书添加到此存储:

cp -p /usr/local/openjdk11/lib/security/cacerts /usr/local/openjdk11/lib/security/cacerts.cln
cp -p /usr/local/openjdk11/lib/security/cacerts /root/hll_jdk11_cacerts

JAVA_VERSION="11" keytool -import   \
  -trustcacerts   \
  -file /usr/local/etc/pki/tls/certs/CA_HLL_ROOT_2016.crt  \
  -alias 'hartelyneroot2016 [hll]'  \
  -keystore /root/hll_jdk11_cacerts

JAVA_VERSION="11" keytool -import  \
   -trustcacerts  \
   -file /usr/local/etc/pki/tls/certs/CA_HLL_ISSUER_2016.crt \
   -alias 'hartelyneissuer2016 [hll]'  \
   -keystore /root/hll_jdk11_cacerts

JAVA_VERSION="11" keytool -list  -rfc  -keystore /root/hll_jdk11_cacerts | grep hll
Enter keystore password:  changeit
Alias name: hartelyneissuer2016 [hll]
Alias name: hartelyneroot2016 [hll]

cp -p /root/hll_jdk11_cacerts /usr/local/openjdk11/lib/security/cacerts

据我所知,CA_HLL_ISSUER_2016 和 CA_HLL_ROOT_2016 颁发的证书现在应该被该主机上的 java 识别为信任的。但是,他们不是。我需要找出原因。

JAVA_VERSION="11" java SSLPoke google.ca 443
Successfully connected

JAVA_VERSION="11" java SSLPoke webmail.harte-lyne.ca 443
sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchor

但我对 openssl s_client 没有任何问题:

openssl s_client -connect webmail.harte-lyne.ca:443
CONNECTED(00000003)
depth=2 CN = CA_HLL_ROOT_2016,ST = Ontario,O = Harte & Lyne Limited,OU = Networked Data Services,C = CA,DC = harte-lyne,DC = ca,L = Hamilton
verify return:1
depth=1 CN = CA_HLL_ISSUER_2016,L = Hamilton,DC = ca
verify return:1
depth=0 CN = webmail.hamilton.harte-lyne.ca,DC = hamilton,DC = ca
verify return:1
---
Certificate chain
 0 s:CN = webmail.hamilton.harte-lyne.ca,DC = ca
   i:CN = CA_HLL_ISSUER_2016,DC = ca
 1 s:CN = CA_HLL_ISSUER_2016,DC = ca
   i:CN = CA_HLL_ROOT_2016,L = Hamilton
 2 s:CN = CA_HLL_ROOT_2016,L = Hamilton
   i:CN = CA_HLL_ROOT_2016,L = Hamilton
---
Server certificate
-----BEGIN CERTIFICATE-----

。 . .

---
Acceptable client certificate CA names
. . .
CN = CA_HLL_ROOT_2016,L = Hamilton
. . .
CN = CA_HLL_ISSUER_2016,DC = ca
. . .

我在这里错过了什么?如何将私有 CA 添加到 Java 信任库?

按照答案中给出的建议,我完全按照给定的顺序执行此操作:

openssl s_client -connect webmail.harte-lyne.ca:443 -showcerts > harte.crt

JAVA_VERSION="11" keytool -import -alias harte -file harte.crt -keystore cacerts -storepass changeit
. . .
Trust this certificate? [no]:  yes
Certificate was added to keystore

JAVA_VERSION="11" java  SSLPoke webmail.harte-lyne.ca 443
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

我在 OpenJDK 中遇到了错误?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)