首页 文章

TrustManager仅允许一个特定的不受信任的证书

提问于
浏览
1

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

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

我已经多次看到"trust all certificates" 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或任何匹配相等或比较它们 .

1 回答

  • 0

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

相关问题