我正在尝试将一些数据从 android (4.1) 应用程序发送到 node.js https 服务器,为此我使用 Gottox 的 socket.io 和 Weberknecht websockets。
我已经实现了通过 TLS 1.0 与密码套件通信应用程序和服务器
TLS_RSA_WITH_AES_128_CBC_SHA
将其添加到服务器的选项中:
var options = {
key: fs.readFileSync(...),
cert: fs.readFileSync(...),
ciphers:('AES128-SHA'),
}
但我想使用 TLS v1.2 的密码套件
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
这应该在 android 4.1 中受支持,所以我将密码选项更改为
ciphers:('ECDHE-ECDSA-AES256-SHA'),
此外,为了使其工作,在 Weberknecht websockets 的 WebSocketConnection.java 中,我将 Sockets 更改为 SSLSockets,因此我可以使用以下代码启用 android 支持的所有密码套件:
socket = (SSLSocket) sslSocketFactory.createSocket(host, port);
String[] suites = socket.getSupportedCipherSuites();
socket.setEnabledCipherSuites(suites);
但它不起作用,如果我删除服务器中的密码选项,或者我再次放置它所做的第一个密码套件,但仅限于 TLS 1.0。
堆栈错误是:
03-14 16:44:01.215: W/System.err(3762): io.socket.SocketIOException: Error while handshaking
03-14 16:44:01.220: W/System.err(3762): at io.socket.IOConnection.handshake(IOConnection.java:313)
03-14 16:44:01.220: W/System.err(3762): at io.socket.IOConnection.access$7(IOConnection.java:283)
03-14 16:44:01.220: W/System.err(3762): at io.socket.IOConnection$ConnectThread.run(IOConnection.java:199)
03-14 16:44:01.225: W/System.err(3762): Caused by: javax.net.ssl.SSLException: Connection closed by peer
03-14 16:44:01.225: W/System.err(3762): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
03-14 16:44:01.225: W/System.err(3762): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:371)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:442)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
03-14 16:44:01.225: W/System.err(3762): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
03-14 16:44:01.230: W/System.err(3762): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
03-14 16:44:01.230: W/System.err(3762): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
03-14 16:44:01.230: W/System.err(3762): at io.socket.IOConnection.handshake(IOConnection.java:304)
03-14 16:44:01.230: W/System.err(3762): ... 2 more
我一直在经历 WebSocketHandshake.java 和 IOConnection.java 但我没有找到问题的根源......
有什么建议吗?
如果需要,可以在此处查看客户端的代码:
https://github.com/Javi44/LocAALTOn/tree/WebSockets-Gottox
编辑:
这是引发异常的方法:
private void handshake() {
URL url;
String response;
URLConnection connection;
try {
setState(STATE_HANDSHAKE);
url = new URL(IOConnection.this.url.toString() + SOCKET_IO_1);
connection = url.openConnection();
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection)
.setSSLSocketFactory(sslSocketFactory);
}
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(connectTimeout);
/* Setting the request headers */
for (Entry<Object, Object> entry : headers.entrySet()) {
connection.setRequestProperty((String) entry.getKey(),
(String) entry.getValue());
}
InputStream stream = connection.getInputStream();
Scanner in = new Scanner(stream);
response = in.nextLine();
String[] data = response.split(":");
sessionId = data[0];
heartbeatTimeout = Long.parseLong(data[1]) * 1000;
closingTimeout = Long.parseLong(data[2]) * 1000;
protocols = Arrays.asList(data[3].split(","));
} catch (Exception e) {
error(new SocketIOException("Error while handshaking", e));
}
}
可能是使用 url 连接而不是 SSLSocket 的问题?是否可以将 url 连接更改为具有相同功能但使用 SSLSockets 的东西?