我正在开发一个应用程序,在该应用程序中我将在两个设备之间建立TLS连接。我使用的是TLSv1.3,但当我尝试设置这样的密码套件时:
String[] protocolCHACHA = new String[1];
protocolCHACHA[0]= "TLS_CHACHA20_POLY1305_SHA256";
String [] tlsVersion = new String[1];
tlsVersion [0] = "TLSv1.3";
SSLServerSocket serverSocket = (SSLServerSocket) serverSSLContext.getServerSocketFactory().createServerSocket(0);
serverSocket.setEnabledProtocols(tlsVersion);
serverSocket.setEnabledCipherSuites(protocolCHACHA);
我得到一个javax.net.ssl.SSLHandshakeException
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: javax.net.ssl.SSLHandshakeException: Handshake failed
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:305)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.ConscryptFileDescriptorSocket.waitForHandshake(ConscryptFileDescriptorSocket.java:510)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.ConscryptFileDescriptorSocket.getInputStream(ConscryptFileDescriptorSocket.java:473)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.example.testaware.AppServer.lambda$new$0$AppServer(AppServer.java:124)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.example.testaware.-$$Lambda$AppServer$vHnVsGrXrL1ybKsPtFi9r-AJTKQ.run(Unknown Source:8)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at java.lang.Thread.run(Thread.java:919)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x7282fc4908: Failure in SSL library, usually a protocol error
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: error:100000ae:SSL routines:OPENSSL_internal:NO_CERTIFICATE_SET (external/boringssl/src/ssl/tls13_server.cc:689 0x7278a83e6b:0x00000000)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.NativeSsl.doHandshake(NativeSsl.java:387)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:234)
2021-05-05 11:40:19.131 2932-15842/com.example.testaware W/System.err: ... 5 more
有人知道为什么吗?难道不能决定使用哪一套密码吗?如果删除serverSocket.setEnabledCipherSuites(protocolCHACHA);
,则不会出现异常。
根据Conscrypt的能力文档,
TLS 1.3密码套件无法自定义;当启用TLS 1.3时,它们始终处于启用状态,并且任何通过调用setEnabledCipherSuites((来禁用它们的尝试都将被忽略。
尽管如此,我实际上是通过谷歌搜索相同的";NO_CERTIFICATE_SET";我收到错误消息。我的代码也试图设置密码,所以我想你刚刚解决了我的问题。
//只需创建自己的SSLSocketFactory,如下所示:
公共最终类MySSSLSocketFactory扩展SSLSocketFactory{
//...
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.3"});
((SSLSocket) socket).setEnabledCipherSuites(new String[]{"TLS_AES_256_GCM_SHA384"/*, "TLS_CHACHA20_POLY1305_SHA256"*/});
return socket;
}
//...
}