简体   繁体   中英

SSL Connection between IPhone App and Java ServerSocket

I'm trying to establish a SSL Connection between an IPhone App and an Java SSLServerSocket. My Java Server looks like that:

SSLServerSocketFactory ssf = null;
    try {
        // set up key manager to do server authentication
        SSLContext ctx;
        KeyManagerFactory kmf;
        KeyStore ks;
        char[] passphrase = "passphrase".toCharArray();

        ctx = SSLContext.getInstance("TLS");
        kmf = KeyManagerFactory.getInstance("SunX509");
        ks = KeyStore.getInstance("JKS");

        ks.load(new FileInputStream("fsKeystore"), passphrase);
        kmf.init(ks, passphrase);
        ctx.init(kmf.getKeyManagers(), null, null);

        ssf = ctx.getServerSocketFactory();

        System.out.println("Waiting for Connection");
        SSLServerSocket sslsocketServer = (SSLServerSocket) ssf.createServerSocket(9999);
        SSLSocket sslsocket = (SSLSocket) sslsocketServer.accept();
        sslsocket.addHandshakeCompletedListener(new HandshakeCompletedListener() {

            @Override
            public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                System.out.println("Handshake finished");
            }
        });
        InputStream inputstream = sslsocket.getInputStream();
        InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
        BufferedReader bufferedreader;
        bufferedreader = new BufferedReader(inputstreamreader);

        String string = "";
        while ((string = bufferedreader.readLine()) != null) {
            string = bufferedreader.readLine();
            System.out.println(System.currentTimeMillis());
            System.out.println(string);
            System.out.flush();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

In Objective-c I have implemented this solution:

-(id) initWithUrl: (NSString*) host onPort: (NSInteger) port withDelegate:(id<TCPDelegate>) delegate{
self = [super init];
if(self){

    //initTimer
    sendBuffer=[[NSMutableArray alloc]init];
    NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timerTick:) userInfo:nil repeats:YES];
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];

    del=delegate;
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    CFStreamCreatePairWithSocketToHost(NULL,(__bridge CFStringRef)host, port,&readStream,&writeStream);

    iStream = (__bridge_transfer NSInputStream *)readStream;
    oStream = (__bridge_transfer NSOutputStream *)writeStream;

    [iStream setDelegate:self];
    [oStream setDelegate:self];

    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];


    [iStream open];
    [oStream open];

    [iStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                   forKey:NSStreamSocketSecurityLevelKey];
    [oStream setProperty:NSStreamSocketSecurityLevelNegotiatedSSL
                    forKey:NSStreamSocketSecurityLevelKey];
     line = @"";
}
return self;
}

in the Iphone App I get

CFNetwork SSLHandshake failed (-9807)

Any Idea what the problem might be?

You may want to investigate using NSURLConnection. It has a delegate that you can use for authentication challenges.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/AuthenticationChallenges.html#//apple_ref/doc/uid/TP40009507-SW3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM