使用 CocoaAsyncSockets 和 Java 套接字的 SSL/TSL

发布于 2024-12-01 09:14:07 字数 2791 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文