gpt4 book ai didi

Java SSL : client handshake failure and server no cipher suites in common

转载 作者:行者123 更新时间:2023-11-28 22:46:32 35 4
gpt4 key购买 nike

我正在尝试通过 Java 中的 SSL 套接字从客户端连接到服务器。在客户端接收到 handshake_failure 并且在服务器端(www)没有共同的密码套件。

服务器配置为 SSL Tomcat 9,这里是连接器:

        <Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
disableUploadTimeout="true" enableLookups="false" maxThreads="25"
port="8443" keystoreFile="C:/Users/xxx/.keystore" keystorePass="xxx"
keystoreType="PKCS12"
protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
secure="true" sslProtocol="TLS" />

客户端是 Java 项目。双方在 keytool 中都有证书。证书已从 keytool 导出并由 keytool 在客户端导入。因此,在客户端和服务器端我们导入了 server.cer。Tomcat 在端口 8443 上启动,套接字使用端口 443。

我尝试了什么?- 在代码中设置协议(protocol)-设置密码套件-改变端口-从8080和8443端口启动tomcat

服务器:

    void mySSLServerSocket(int port) throws IOException {

SSLServerSocketFactory sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket sslServerSocket = (SSLServerSocket) sslServerSocketFactory.createServerSocket(port);
sslServerSocket.setEnabledProtocols(new String[] {"TLSv1.2"});
sslServerSocket.setEnabledCipherSuites(new String[] {"TLS_RSA_WITH_AES_256_CBC_SHA"});
SSLSocket socket = (SSLSocket) sslServerSocket.accept();
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();
while(true) {
int orderNumber = in.read();
System.out.println("order number readed: " + orderNumber);
}
}

客户:

    void mySocket(String ip, int port) throws IOException {

SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(ip, port);
sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});
sslSocket.setEnabledCipherSuites( new String[] {"TLS_RSA_WITH_AES_256_CBC_SHA"});
sslSocket.startHandshake();
InputStream in = sslSocket.getInputStream();
OutputStream out = sslSocket.getOutputStream();
out.write(1);
while (in.available() > 0) {
System.out.print(in.read());
}

}

客户端错误:

Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2020)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1127)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1367)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1395)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1379)
at com.company.SocketUtil.mySocket(SocketUtil.java:31)
at com.company.Main.main(Main.java:15)

服务器(错误来自网络 - JSP 页面):

Type Exception Report

Message no cipher suites in common

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.net.ssl.SSLHandshakeException: no cipher suites in common
java.base/sun.security.ssl.Alert.createSSLException(Alert.java:131)
java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:307)
java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:263)
java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:254)
java.base/sun.security.ssl.ServerHello$T12ServerHelloProducer.chooseCipherSuite(ServerHello.java:460)
java.base/sun.security.ssl.ServerHello$T12ServerHelloProducer.produce(ServerHello.java:295)
java.base/sun.security.ssl.SSLHandshake.produce(SSLHandshake.java:436)
java.base/sun.security.ssl.ClientHello$T12ClientHelloConsumer.consume(ClientHello.java:1102)
java.base/sun.security.ssl.ClientHello$ClientHelloConsumer.onClientHello(ClientHello.java:854)
java.base/sun.security.ssl.ClientHello$ClientHelloConsumer.consume(ClientHello.java:813)
java.base/sun.security.ssl.SSLHandshake.consume(SSLHandshake.java:392)
java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:443)
java.base/sun.security.ssl.HandshakeContext.dispatch(HandshakeContext.java:421)
java.base/sun.security.ssl.TransportContext.dispatch(TransportContext.java:177)
java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:164)
java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1180)
java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1091)
java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:402)
java.base/sun.security.ssl.SSLSocketImpl.ensureNegotiated(SSLSocketImpl.java:721)
java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:804)
java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:763)
ServerSocketUtils.mySSLServerSocket(ServerSocketUtils.java:38)
MusicControl.doPost(MusicControl.java:24)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

错误说没有密码套件,但我检查了支持的密码并选择了一个,双方都可用。声明了 TSL 协议(protocol),但仍然没有成功。

我想从客户端成功连接到服务器,但不知道下一步该怎么做,因为在互联网上找不到任何建议。

我读过一些关于进行连接所需的私钥的内容。它在哪里?我假设 server.cer 只有公钥。

当tomcat在8443上运行时,套接字使用端口443是否合适?也许我应该在某处指定这些端口?

最佳答案

您的问题的一部分是关于“证书”的,我认为它代表证书。

你是对的,证书只包含一个由证书颁发机构 (CA) 签名的公钥。公钥只是非对称 key 对(例如 RSA)的公开部分。服务器需要有私钥。否则它不能解密用公钥加密的内容,也不能签署发送给客户端的内容。

也就是说,您需要创建一个 RSA key 对,并将其提供给 tomcat。要么让它由知名 CA 签名(制作证书),要么使用 key 工具生成自签名证书。在第一种情况下,客户端不需要特殊操作,因为 java(或浏览器)已经知道众所周知的 CA 公钥,需要这些公钥来验证服务器提供的证书。如果是自签名证书,您必须在客户端将其作为可信证书导入。

关于Java SSL : client handshake failure and server no cipher suites in common,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58674628/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com