gpt4 book ai didi

java - 网络 SSL : how to write a TrustManager

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:18:29 26 4
gpt4 key购买 nike

我已经阅读了很多关于设置我的 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);
}

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

最佳答案

How do you implement this class properly?

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

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

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

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

Why do they put null instead of tmf.getTrustManagers() in the line serverContext.init(kmf.getKeyManagers(), null, null); ?

信任管理器的

nullSecureRandomnull 回退到默认值。这将是使用默认 TMF 算法(通常是 PKIX)初始化的默认信任管理器,使用默认信任库(使用 javax.net.ssl.trustStore 中的位置,或返回到 jssecacerts 文件或 cacerts)。更多详细信息,请参阅 JSSE reference guide .

关于java - 网络 SSL : how to write a TrustManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11396412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com