【问题标题】:Netty SSL : how to write a TrustManagerNetty SSL:如何编写 TrustManager
【发布时间】:2012-07-08 22:21:49
【问题描述】:

我已阅读大量资料来设置我的 SSL 客户端/服务器系统(无 HTTP)。

我的灵感来自 the secure chat examplethe websocket ssl server example。 已经使用命令创建了我的 cert.jks 文件

keytool -genkey -alias app-keysize 2048 -validity 36500
-keyalg RSA -dname "CN=app"
-keypass mysecret-storepass mysecret
-keystore cert.jks

在安全聊天示例中有这个类:

public class SecureChatTrustManagerFactory extends TrustManagerFactorySpi {

    private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        @Override
        public void checkClientTrusted(
                X509Certificate[] chain, String authType) throws CertificateException {
            // Always trust - it is an example.
            // You should do something in the real world.
            // You will reach here only if you enabled client certificate auth,
            // as described in SecureChatSslContextFactory.
            System.err.println(
                    "UNKNOWN CLIENT CERTIFICATE: " + chain[0].getSubjectDN());
        }

        @Override
        public void checkServerTrusted(
                X509Certificate[] chain, String authType) throws CertificateException {
            // Always trust - it is an example.
            // You should do something in the real world.
            System.err.println(
                    "UNKNOWN SERVER CERTIFICATE: " + chain[0].getSubjectDN());
        }
    };

    public static TrustManager[] getTrustManagers() {
        return new TrustManager[] { DUMMY_TRUST_MANAGER };
    }

    @Override
    protected TrustManager[] engineGetTrustManagers() {
        return getTrustManagers();
    }

    @Override
    protected void engineInit(KeyStore keystore) throws KeyStoreException {
        // Unused
    }

    @Override
    protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
            throws InvalidAlgorithmParameterException {
        // Unused
    }
}

你如何正确地实现这个类?

在这段代码中(在 SecureChatSslContextFactory 类中):

    SSLContext serverContext = null;
    SSLContext clientContext = null;
    try {
        KeyStore ks = KeyStore.getInstance("JKS");
        ks.load(SecureChatKeyStore.asInputStream(),
                SecureChatKeyStore.getKeyStorePassword());

        // Set up key manager factory to use our key store
        KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
        kmf.init(ks, SecureChatKeyStore.getCertificatePassword());

        // Initialize the SSLContext to work with our key managers.
        serverContext = SSLContext.getInstance(PROTOCOL);
        serverContext.init(kmf.getKeyManagers(), null, null);
    } catch (Exception e) {
        throw new Error(
                "Failed to initialize the server-side SSLContext", e);
    }

    try {
        clientContext = SSLContext.getInstance(PROTOCOL);
        clientContext.init(null, SecureChatTrustManagerFactory.getTrustManagers(), null);
    } catch (Exception e) {
        throw new Error(
                "Failed to initialize the client-side SSLContext", e);
    }

为什么他们将null 而不是tmf.getTrustManagers() 放在serverContext.init(kmf.getKeyManagers(), null, null); 行中?

【问题讨论】:

    标签: java ssl netty


    【解决方案1】:

    你如何正确地实现这个类?

    您需要定义一种方式来检查您是否信任chain[0] 上的证书。如果您不这样做,请发送CertificateException。 (这里SecureChatTrustManagerFactory从不抛出任何东西,所以它绕过了验证,这会使连接对MITM攻击开放。)

    如果您想半手动进行此验证,您可以使用Java PKI API,尽管it can be a bit tedious,即使在相对简单的用例上也是如此。

    一般来说,正确的做法是实现你自己的。将其留给TrustManagerFactory(或多或少与KeyManagerFactory 相同)。顺便说一句,在这两种情况下,我都建议使用Key/TrustManagerFactory.getDefaultAlgorithm() 作为algorithm 的值,除非你有充分的理由不这样做。它至少比 SunX509 更好的默认值,我在很多情况下都看到过硬编码(实际上这不是默认的 TMF 算法值)。

    您可以从自己的信任库初始化 TMF(例如,您可以专门为此连接加载的 KeyStore 实例)。

    为什么他们在行中放置 null 而不是 tmf.getTrustManagers() serverContext.init(kmf.getKeyManagers(), null, null); ?

    null 用于信任管理器,null 用于SecureRandom 回退到默认值。这将是一个使用默认 TMF 算法(通常为PKIX)初始化的默认信任管理器,使用默认信任存储(使用javax.net.ssl.trustStore 中的位置,或回退到jssecacerts 文件或cacerts)。更多详情请关注JSSE reference guide

    【讨论】:

    • 仍然无法使某些东西正常工作...我不知道如何实现我自己的 TrustManagerFactory
    • 不要实现自己的 TMF,使用现有的。
    • 抱歉,我的意思是我无法实现自己的 SecureChatSslContextFactory
    猜你喜欢
    • 1970-01-01
    • 2016-02-18
    • 1970-01-01
    • 2012-01-16
    • 2018-07-10
    • 2013-09-13
    • 2016-12-21
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多