我正在尝试从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中,我已将套接字更改为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的内容?