JSSE 是否使用 PrivateKeyEntry 中的证书作为信任锚?

Does JSSE use a certificate in a PrivateKeyEntry as a trust anchor?

如果包含一个或多个 PrivateKeyEntry 的密钥库被指定为信任库,JSSE 会根据每个条目中的最终实体证书创建信任锚吗?

换句话说,如果我们有一个包含可信条目和私有条目的密钥库,那么在 PrivateKeyEntry 下拥有一个证书就足够了吗?或者,我们是否还必须将该证书添加为 TrustedCertificateEntry?

将证书放在 PrivateKeyEntrytrustedCertEntry 下并不重要,JVM trusts 主机从证书反正。

本地测试。

运行 具有 https 和密钥库的本地服务器 只有一个 PrivateKeyEntry

和 运行 客户端代码:

public static String getHTML(String urlToRead) throws Exception {
    StringBuilder result = new StringBuilder();
    URL url = new URL(urlToRead);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}

public static void main(String[] args) throws Exception {
    String testUrl="https://localhost/test";
    System.out.println(getHTML(testUrl));
}

没有任何:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

使用仅包含一个 PrivateKeyEntrytruststore(与服务器用作密钥库的同一 jks 文件):

<!DOCTYPE....</html> 

Is it enough to have certificate under PrivateKeyEntry if we have one keystore with both trusted and private entries

你永远不应该拥有这样的密钥库。

or shall we add also certificate as trustedCertEntry in order to make requests to themself/other node under proxy ?

A trustedCertEntry 用于 传入 证书。私钥条目用于 传出 证书。

您将两种不同的东西混为一谈,实际上是密钥库的两种不同用途。

  1. 包含 trustedCertEntry 的密钥库文件是 truststore,在 javax.net.ssl.trustStore 的意义上,它告诉 JSSE 传入 直接或间接信任的证书。

  2. 包含 PrivateKeyEntry 的密钥库文件是 keystore,在 javax.net.ssl.keyStore 的意义上,它告诉 JSSE 哪些证书用于 出站 证书。

  3. 包含两者的密钥库文件格式完全错误。信任库只是要信任的证书列表。这不是秘密。 KeyStore 包含您的私钥,这对每个人来说都是最高机密。将两者混为一谈是一个重大的安全漏洞。

如果没关系,为什么会有两种不同类型的条目?

这甚至不是一个合适的问题。如果您在应该是受信任证书的地方有一个私钥,那意味着您有其他人的私钥,这是一个 初步证据 安全漏洞。