My iOS app contains 2 versions: server and client. Server posts a service via Bonjour and waits for a client. Client searches that services and connects to the server. After that all communications implemented through CFSockets.
Here is my code to set up streams for such communications:
- (BOOL)setupSocketStreams {
if ( readStream == nil || writeStream == nil ) {
return NO;
}
incomingDataBuffer = [[NSMutableData alloc] init];
outgoingDataBuffer = [[NSMutableData alloc] init];
// Turning SSL on
NSDictionary *sslSettings = [NSDictionary dictionaryWithObjectsAndKeys:(id)kCFBooleanFalse, (id)kCFStreamSSLValidatesCertificateChain, nil];
CFReadStreamSetProperty(readStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFReadStreamSetProperty(readStream, kCFStreamPropertySSLSettings, sslSettings);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySocketSecurityLevel, kCFStreamSocketSecurityLevelNegotiatedSSL);
CFWriteStreamSetProperty(writeStream, kCFStreamPropertySSLSettings, sslSettings);
CFReadStreamSetProperty(
readStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue
);
CFWriteStreamSetProperty(
writeStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue
);
CFOptionFlags registeredEvents =
kCFStreamEventOpenCompleted |
kCFStreamEventHasBytesAvailable |
kCFStreamEventCanAcceptBytes |
kCFStreamEventEndEncountered |
kCFStreamEventErrorOccurred
;
CFStreamClientContext ctx = {
0, // version
self, // info
NULL, // retain callback
NULL, // release callback
NULL // copyDescription callback
};
CFReadStreamSetClient(
readStream,
registeredEvents,
readStreamEventHandler,
&ctx
);
CFWriteStreamSetClient(
writeStream,
registeredEvents,
writeStreamEventHandler,
&ctx
);
CFReadStreamScheduleWithRunLoop(
readStream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes
);
CFWriteStreamScheduleWithRunLoop(
writeStream,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes
);
if ( ! CFReadStreamOpen(readStream) || ! CFWriteStreamOpen(writeStream)) {
[self close];
return NO;
}
if ([self.delegate respondsToSelector:@selector(establishedConnection:)]) {
[self.delegate establishedConnection:self];
}
return YES;
}
Without part that turning SSL on this code works fine, but otherwise I'm getting error:
Error:
The operation couldn’t be completed. (OSStatus error -9800.)
What kind of problem can it be?
Thanks!
There is a known issue for TLS1.2 support in iOS 5: https://developer.apple.com/library/ios/#technotes/tn2287/_index.html#//apple_ref/doc/uid/DTS40011309
By setting kCFStreamSSLLevel to @"kCFStreamSocketSecurityLevelTLSv1_0SSLv3" I was able to fix the -9800 issue in my app.