与Glassfish3 / 4或Tomcat 8的双向(相互)SSL和自签名证书

时间:2016-02-10 16:31:49

标签: java tomcat ssl x509certificate mutual-authentication

我试图实现双向(相互)SSL身份验证,但我经常在Glassfish3 / 4和Tomcat 8服务器上获得以下异常(堆栈跟踪来自Tomcat 8):

10-Feb-2016 17:13:41.579 SEVERE [http-nio2-18443-exec-2] org.apache.tomcat.util.net.Nio2Endpoint$SocketProcessor.doRun Error running socket processor
java.lang.RuntimeException: Field length overflow, the field length (7189180) should be less than 65536
    at sun.security.ssl.Handshaker.checkThrown(Handshaker.java:1373)
    at sun.security.ssl.SSLEngineImpl.checkTaskThrown(SSLEngineImpl.java:529)
    at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:807)
    at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:775)
    at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
    at org.apache.tomcat.util.net.SecureNio2Channel.handshakeUnwrap(SecureNio2Channel.java:394)
    at org.apache.tomcat.util.net.SecureNio2Channel.handshakeInternal(SecureNio2Channel.java:267)
    at org.apache.tomcat.util.net.SecureNio2Channel.handshake(SecureNio2Channel.java:204)
    at org.apache.tomcat.util.net.Nio2Endpoint$SocketProcessor.doRun(Nio2Endpoint.java:1064)
    at org.apache.tomcat.util.net.Nio2Endpoint$SocketProcessor.run(Nio2Endpoint.java:1046)
    at org.apache.tomcat.util.net.Nio2Endpoint.processSocket0(Nio2Endpoint.java:598)
    at org.apache.tomcat.util.net.Nio2Endpoint.processSocket(Nio2Endpoint.java:583)
    at org.apache.tomcat.util.net.SecureNio2Channel$1.completed(SecureNio2Channel.java:83)
    at org.apache.tomcat.util.net.SecureNio2Channel$1.completed(SecureNio2Channel.java:76)
    at sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:126)
    at sun.nio.ch.Invoker$2.run(Invoker.java:218)
    at sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

经过长时间的搜索,我发现this link显示了同样的问题但是在JBoss上。这真的与密钥/信任库中的条目大小/数量有关吗?我现在已经存储了大约66.000个证书,但随着开发的进行,我们希望方式更多(例如,数十万)。

使用几乎为空的密钥库(仅包含服务器和一个测试客户端证书),使用curl测试SSL连接是成功的......

更新

好的,我想我发现它确实与truststore有关。以下是sun.security.ssl.HandshakeMessage.CertificateRequest的代码段:

        // put certificate_authorities
        int len = 0;
        for (int i = 0; i < authorities.length; i++) {
            len += authorities[i].length();
        }

        output.putInt16(len);

putInt()方法中触发了异常,所以我假设我确实存储了太多的证书。我现在正在生成自签名证书,因此我必须将每个生成的证书作为服务器上的CA来信任客户端,这一定是问题所在。现在的问题是,有没有办法从Java生成自签名的证书(例如我可以使用glassfish作为CA的自签名证书 - 或者我可以)?

1 个答案:

答案 0 :(得分:1)

好的,我通过创建证书并将发行者设置为服务器自己的证书(在Glassfish中,特别是别名s1as)并使用服务器的私钥对其进行签名来使其工作。现在我不必在服务器上存储任何东西,因为它识别客户端,因为它在其trustStore(本质上,它本身)中知道它们的发行者是CA.