CFStream SSL错误-9800

时间:2012-04-23 07:09:01

标签: iphone objective-c ios ssl cfstream


我的iOS应用程序包含2个版本:服务器和客户端。服务器通过Bonjour发布服务并等待客户端。客户端搜索该服务并连接到服务器。之后所有通信都通过CFSockets实现 以下是我为此类通信设置流的代码:

- (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;
}

没有任何一部分,在此代码上启用SSL工作正常,但否则我收到错误:

Error:
The operation couldn’t be completed. (OSStatus error -9800.)

它会出现什么问题?
谢谢!

1 个答案:

答案 0 :(得分:7)

iOS 5中存在TLS1.2支持的已知问题:https://developer.apple.com/library/ios/#technotes/tn2287/_index.html#//apple_ref/doc/uid/DTS40011309

通过将kCFStreamSSLLevel设置为@“kCFStreamSocketSecurityLevelTLSv1_0SSLv3”,我能够在我的应用中修复-9800问题。