【发布时间】:2015-11-11 14:31:36
【问题描述】:
我对 Java 安全性非常陌生,所以这个问题的答案可能很明显。我正在尝试在客户端和服务器之间执行简单的握手,我希望他们使用 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 或 TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 进行握手。根据我阅读的所有地方,Java 8 支持这些密码,并且我已经安装了 JCE Unlimited Strength Jurisdiction Policy Files。因此,当我打印出 Java 安装中所有启用的密码时,这两个密码都存在,这意味着默认情况下它们是启用的。但由于某种原因,握手失败,因为客户端和服务器没有共同的密码套件。我也启用了 TLSv1.2 协议。客户端的公钥已导入服务器的信任库,其他密码(如 TLS_RSA_WITH_AES_128_CBC_SHA256 等)的握手成功。我正在运行 Java 8 v1.8.0_60。我还缺少什么?
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
public class GCMCiphersJava8Test {
private static final String SERVER_KEY_STORE = "testkeystore.jks";
private static final String CLIENT_KEY_STORE = "testtruststore.jks";
private static final String HOST = "localhost";
private static final String PASSWORD = "Pa55word";
private static final int SSL_PORT = 8443;
private static final String[] TLS_12 = new String[]{"TLSv1.2"};
private static String serverKeyStorePath = null;
private static String clientKeyStorePath = null;
private Server server = null;
@BeforeClass
public static void setup() {
serverKeyStorePath = GCMCiphersJava8Test.class.getResource(SERVER_KEY_STORE).getFile();
clientKeyStorePath = GCMCiphersJava8Test.class.getResource(CLIENT_KEY_STORE).getFile();
}
@Test
public void testGCMCiphersInJava8() throws Exception{
SSLSession session = null;
startServer(TLS_12, null, new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, null);
SSLSocket sslSocket = createSslSocket(TLS_12, null, new String[]{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, null);
if (this.server.isRunning()){
session = sslSocket.getSession();
}
assertEquals("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", session.getCipherSuite());
}
private void startServer(String[] includeProtocols, String[] excludeProtocols, String[] includeCiphers, String[] excludeCiphers) throws Exception{
this.server = new Server();
SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
ssl_connector.setPort(SSL_PORT);
SslContextFactory cf = ssl_connector.getSslContextFactory();
cf.setKeyStorePath(serverKeyStorePath);
cf.setKeyStorePassword(PASSWORD);
cf.setKeyManagerPassword(PASSWORD);
if (includeCiphers != null){
cf.setIncludeCipherSuites(includeCiphers);
}
if (excludeCiphers != null){
cf.setExcludeCipherSuites(excludeCiphers);
}
if (includeProtocols != null){
cf.setIncludeProtocols(includeProtocols);
}
if (excludeProtocols != null){
cf.setExcludeProtocols(excludeProtocols);
}
this.server.setConnectors(new Connector[]{ssl_connector});
this.server.setHandler(new AbstractHandler() {
@Override
public void handle(String target,Request baseRequest,HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello World</h1>");
}
});
this.server.start();
}
@After
public void stopServer() throws Exception{
this.server.stop();
}
private SSLSocket createSslSocket(String[] includeProtocols, String[] excludeProtocols, String[] includeCiphers, String[] excludeCiphers){
SSLSocket sslSocket = null;
try {
System.setProperty("javax.net.ssl.trustStore", clientKeyStorePath);
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslSocket = (SSLSocket) factory.createSocket(HOST, SSL_PORT);
if (includeCiphers != null){
sslSocket.setEnabledCipherSuites(includeCiphers);
}
if (includeProtocols != null){
sslSocket.setEnabledProtocols(includeProtocols);
}
sslSocket.addHandshakeCompletedListener(e -> {
System.out.println("Handshake succesful!");
System.out.println("Using cipher suite: " + e.getCipherSuite());
});
} catch (Exception e) {
e.printStackTrace();
}
return sslSocket;
}
}
【问题讨论】: