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?

Alx
  • 6,275
  • 7
  • 32
  • 54
  • In settings, you have to indicate that you have server with kCFStreamSSLIsServer flag, also you have to provide certificates, like here: http://stackoverflow.com/questions/11258911/how-to-make-iphonehttpserver-secure-server – Alexey F Sep 25 '13 at 14:22
  • Thanks for your advice. I dropped the project so I'm not able right now to give it a try, but maybe someone else facing the same issue might find it helpful. – Alx Nov 02 '13 at 17:01
  • @80leaves Yes right here :D – Kametrixom Jul 13 '15 at 19:46
  • @Kametrixom I have no clue, but what Alexey posted seems legit.. – Alx Jul 19 '15 at 09:46

0 Answers0