使用 CocoaAsyncSockets 和 Java 套接字的 SSL/TSL
我想在 cocoa 中创建一个套接字服务器,使用 AsyncSockets 并与 Java 连接。如果 Java 是服务器而 cocoa 是客户端,则启用 SSL 是有效的,但我需要相反的方式才能工作。我想我只是缺少可可中的一些设置,所以这是我的java代码: 我通过传递:
-Djavax.net.ssl.trustStore=${project_loc}/myKeystore
-Djavax.net.ssl.trustStorePassword=myPass
-Djavax.net.debug=all
_
public class Client {
public static void main(String[] arstring) {
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(
"localhost", 9999);
InputStream inputstream = System.in;
InputStreamReader inputstreamreader = new InputStreamReader(
inputstream);
BufferedReader bufferedreader = new BufferedReader(
inputstreamreader);
OutputStream outputstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
outputstream);
BufferedWriter bufferedwriter = new BufferedWriter(
outputstreamwriter);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
bufferedwriter.write(string + "\r\n");
bufferedwriter.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
和我的可可代码位来启动客户端:
- (void)socket:(GCDAsyncSocket *)socket didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"new conn");
// read again for further messages with undefined timeout
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:3];
[settings setObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
[settings setObject:(NSString*)kCFStreamPropertySocketSecurityLevel
forKey:(NSString*)kCFStreamSocketSecurityLevelNegotiatedSSL];
DDLogVerbose(@"Starting TLS with settings:\n%@", settings);
[newSocket startTLS:settings];
[newSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];
}
一旦我从java客户端向可可服务器发送消息,我就会收到以下错误:
main, READ: TLSv1 Handshake, length = 117
main, handling exception: javax.net.ssl.SSLProtocolException: Illegal client handshake msg, 1
main, SEND TLSv1 ALERT: fatal, description = unexpected_message
main, WRITE: TLSv1 Alert, length = 2
[Raw write]: length = 7
0000: 15 03 01 00 02 02 0A .......
main, called closeSocket()
javax.net.ssl.SSLProtocolException: Illegal client handshake msg, 1
有人知道如何正确使用它吗?
I want to create a socket server in cocoa, using AsyncSockets and connect with Java. Enabling SSL works if Java is the server and cocoa the client, but I require the other way around to work. I guess I'm just missing some settings within cocoa, so heres my java code:
I'm starting the client by passing:
-Djavax.net.ssl.trustStore=${project_loc}/myKeystore
-Djavax.net.ssl.trustStorePassword=myPass
-Djavax.net.debug=all
_
public class Client {
public static void main(String[] arstring) {
try {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory
.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(
"localhost", 9999);
InputStream inputstream = System.in;
InputStreamReader inputstreamreader = new InputStreamReader(
inputstream);
BufferedReader bufferedreader = new BufferedReader(
inputstreamreader);
OutputStream outputstream = sslsocket.getOutputStream();
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(
outputstream);
BufferedWriter bufferedwriter = new BufferedWriter(
outputstreamwriter);
String string = null;
while ((string = bufferedreader.readLine()) != null) {
bufferedwriter.write(string + "\r\n");
bufferedwriter.flush();
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
}
and bits of my cocoa code:
- (void)socket:(GCDAsyncSocket *)socket didAcceptNewSocket:(GCDAsyncSocket *)newSocket
{
NSLog(@"new conn");
// read again for further messages with undefined timeout
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:3];
[settings setObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];
[settings setObject:(NSString*)kCFStreamPropertySocketSecurityLevel
forKey:(NSString*)kCFStreamSocketSecurityLevelNegotiatedSSL];
DDLogVerbose(@"Starting TLS with settings:\n%@", settings);
[newSocket startTLS:settings];
[newSocket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];
}
as soon as I send a message from the java client to the cocoa server, I get following error:
main, READ: TLSv1 Handshake, length = 117
main, handling exception: javax.net.ssl.SSLProtocolException: Illegal client handshake msg, 1
main, SEND TLSv1 ALERT: fatal, description = unexpected_message
main, WRITE: TLSv1 Alert, length = 2
[Raw write]: length = 7
0000: 15 03 01 00 02 02 0A .......
main, called closeSocket()
javax.net.ssl.SSLProtocolException: Illegal client handshake msg, 1
Does anybody have a hint how to use it properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论