0
我正在尝试使用Netty构建单向身份验证套接字服务器。
首先,我使用keytool为服务器和客户机生成密钥库,自签名证书,信任库,并且我在服务器/客户机中编写了一些代码,SSL身份验证正在运行。
这是我的问题:
有没有什么办法可以让我不需要向我的客户端添加信任库,我只将密钥库添加到我的服务器上,它仍然可以正常工作?我认为单向认证意味着只有服务器持有证书?使用Netty进行单向SSL身份验证
以下是我在我的服务器/客户端写道添加SslHandler至今:
服务器:
private void addSslHandlerOneWay(SocketChannel ch) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream(new File("svrks.jks")), "kspassword1".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, "kspassword2".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLEngine sslEngine = sslContext.createSSLEngine();
sslEngine.setUseClientMode(false);
sslEngine.setNeedClientAuth(false);//one-way
sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols());
sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites());
sslEngine.setEnableSessionCreation(true);
ch.pipeline().addFirst("SSL", new SslHandler(sslEngine));
}
客户端:
private void addSslHandlerOneWay(SocketChannel ch) throws Exception {
SSLContext sslContext = SSLContext.getInstance("TLS");
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(getInputStream("clits.jks"), "tspassword2".toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ts, "tspassword1".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ts);
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
SSLEngine sslEngine = sslContext.createSSLEngine();
sslEngine.setUseClientMode(true);//client
sslEngine.setEnabledProtocols(sslEngine.getSupportedProtocols());
sslEngine.setEnabledCipherSuites(sslEngine.getSupportedCipherSuites());
sslEngine.setEnableSessionCreation(true);
ch.pipeline().addFirst("SSL", new SslHandler(sslEngine));
}
谢谢,伙计们。
除了Steffen的回答:大多数SSL/TLS客户端(包括所有Web浏览器)都有一组默认信任的预先验证的CA,例如Verisign GoDaddy等。如果您获得并使用来自其中一个CA则无需对客户进行任何操作或更改。大多数“真正的”CA都会收取费用,但通过购物可以每年只能获得几美元的基本证书; LetsEncrypt什么也不收费,现在已经被广泛信任 - 但直到8u101之前,Java中才会默认使用它。 –