【问题标题】:Understanding the SSL Trust Strategy了解 SSL 信任策略
【发布时间】:2020-04-06 08:58:15
【问题描述】:

我试图了解TrustStrategyloadTrustMaterial 方法采用什么。

 public SSLContextBuilder loadTrustMaterial(KeyStore truststore,
                                  TrustStrategy trustStrategy)
                                    throws NoSuchAlgorithmException,
                                           KeyStoreException

我找到了四个不同的例子,我很想知道这四个之间的区别,因为描述太少,无法理解区别/用法/优点/缺点。

以下是四个不同的代码示例:

TrustStrategy:这似乎是我们正在覆盖标准的 JSSE 证书验证过程,但它总是返回 true,所以它是否也信任无效证书?

TrustStrategy trustStrategy = new TrustStrategy() {
    @Override
    public boolean isTrusted(X509Certificate[] x509Certificates, String authType) throws CertificateException {
        return true;
    }
    };
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
    .loadTrustMaterial(trustStore, trustStrategy);

NULL:我们没有给出任何策略,它会做什么?

SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                            .loadTrustMaterial(trustStore, null);

TrustAllStrategy:它会信任所有已签名的证书,但这样安全吗?

SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                            .loadTrustMaterial(trustStore, new TrustAllStrategy());

TrustSelfSignedStrategy:这和 TrustAllStrategy 有什么区别?

SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                            .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());

请帮我理解这四个版本的例子之间的区别,好吗?提前致谢。

【问题讨论】:

    标签: java ssl ssl-certificate truststore


    【解决方案1】:

    首先,强烈建议不要信任所有证书。而是将证书添加到信任库。

    TrustStategy 是一个接口,由某些类型实现。

    这里的所有这些方法都来自 apache httpclient - 第一个(覆盖 isTrusted 方法)或多或少等于 TrustAllStrategy 并且只是创建 TrustStrategy 的自定义实例,您可以在其中定义您的以自己的方式来确定证书是否受信任。

    在此处查看TrustAllStrategy 的源代码:

    public class TrustAllStrategy implements TrustStrategy {
    
        public static final TrustAllStrategy INSTANCE = new TrustAllStrategy();
    
        @Override
        public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
            return true;
        }
    

    TrustStrategy 设置为null 将导致没有任何TrustManager

       public SSLContextBuilder loadTrustMaterial(
                final KeyStore truststore,
                final TrustStrategy trustStrategy) throws NoSuchAlgorithmException, KeyStoreException {
            final TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(
                    trustManagerFactoryAlgorithm == null ? TrustManagerFactory.getDefaultAlgorithm()
                            : trustManagerFactoryAlgorithm);
            tmfactory.init(truststore);
            final TrustManager[] tms = tmfactory.getTrustManagers();
            if (tms != null) {
                if (trustStrategy != null) {
                    for (int i = 0; i < tms.length; i++) {
                        final TrustManager tm = tms[i];
                        if (tm instanceof X509TrustManager) {
                            tms[i] = new TrustManagerDelegate(
                                    (X509TrustManager) tm, trustStrategy);
                        }
                    }
                }
                for (final TrustManager tm : tms) {
                    this.trustManagers.add(tm);
                }
            }
            return this;
        }
    

    TrustSelfSignedStrategy 的工作原理如下:

    @Override
    public boolean isTrusted(
            final X509Certificate[] chain, final String authType) throws CertificateException {
        return chain.length == 1;
    }
    

    自签名证书由证书的目标颁发。它在许多应用程序中默认生成,并且经常用于 Intranet 目的。

    【讨论】:

    • 当传递 null 时,我们确实有 TrustManager,因为“final TrustManager[] tms”不是一个空数组。问题是在策略中传递 null 比传递“TrustSelfSignedStrategy”“更安全”吗?我们应该仅将“TrustSelfSignedStrategy”用于测试吗?
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2018-11-10
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多