【问题标题】:Allow only one specific untrusted certificate by the TrustManagerTrustManager 只允许一个特定的不受信任的证书
【发布时间】:2014-02-13 17:16:54
【问题描述】:

我正在围绕我们内部使用的一些其他 web 服务构建一个小型包装 java 类。我们使用安全的 HTTPS 连接。通常,SSL 只允许受信任的证书。我希望包装器类的用户能够将 X509Certificate 对象作为包装器构造函数的输入。此证书通常是不安全的,但这就是重点 - 我们希望在内部进行测试而不需要安全的证书。

public ServicesWrapper(String serviceURL, X509Certificate additionalCertificate)
{
    //Add the certificate to the trustmanager of the sslcontext.
}

我已经多次看到“信任所有证书”TrustManager 黑客攻击 (like here),但这不是我需要的。我真的很想指定给定的(不受信任的)证书现在应该被认为是受信任的。其他不受信任的证书不好。所以我实现了自己的 TrustManager,但我卡在了 checkTrusted 步骤:

public class NaiveTrustManager implements X509TrustManager
{
    //The extra certificate that should be trusted.
    private X509Certificate additionalCertificate;

    //Constructor
    public NaiveTrustManager(X509Certificate _additionalCertificate)
    {
        additionalCertificate = _additionalCertificate;
    }

    ...other methods here...


    public void checkServerTrusted(java.security.cert.X509Certificate[] certificateChain, String authType)
    {
        for(X509Certificate c : certificateChain)
        {
            **?????????????????**
                    pseudocode:
                    if (c is equal to additionalcertificate)
                        stop execution, we trust it!
                    if (nothing matches)
                        throw certificatEexception
        }
    }
}

对照证书链检查证书的正确语法是什么?我检查了一些属性,但证书似乎没有名称或 ID 或任何可以匹配或比较它们的内容。

【问题讨论】:

  • 您是否需要这个信任管理器来信任其他受信任的证书,或者真的只是这个。

标签: java ssl https x509certificate


【解决方案1】:

不要为此编写代码。只需将自签名证书添加到您的测试环境信任库。

【讨论】:

  • 有没有办法在java中编写“将证书添加到环境信任库”?比如,我的用户提供了他的证书(文件,或者文件的位置,或者证书的某些属性),我的 java 代码自动将此证书添加到信任库?我希望最终用户使用自签名证书的过程尽可能简单,即使这意味着编写冗长/乏味的代码来自动化该过程。
  • 好的,我刚刚与我的同事核实过,这是不行的,因为我们不希望证书适用于任何其他应用程序。将其导入 cacerts 将使其成为任何应用程序的受信任证书...
猜你喜欢
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-27
  • 2016-02-13
  • 1970-01-01
  • 2012-10-13
  • 2010-11-15
相关资源
最近更新 更多