我对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;
}
}
答案 0 :(得分:2)
TLS密码套件有4个部分,所有部分都必须指定。他们是:
AES-GCM和ECDHE都不对证书或密钥施加任何限制。
ECDSA 要求服务器拥有ECDSA证书和密钥。
因此,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256和TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384可以使用RSA证书,如果您没有ECDSA证书,那么每个人都建议您今天部署这些证书。
答案 1 :(得分:0)
我明白了。要使用基于GCM的密码,我需要在keytool中使用Elliptic Curve选项生成密钥对。我愚蠢地使用了之前的RSA。
使用EC和256密钥大小生成密钥对:
keytool -genkeypair -alias sergey -keyalg EC -keysize 256 -validity 365 -keystore testkeystore.jks -storepass <insertPasswordHere>
将密钥导出到证书中:
keytool -exportcert -keystore testkeystore.jks -storepass <insertPasswordHere> -file testCert.crt -alias <insertAliasHere>
将该证书导入服务器的信任库:
keytool -importcert -trustcacerts -file testCert.crt -alias <sameAliasAsAbove> -keystore testtruststore.jks -storepass <insertPasswordHere>