流套接字连接中的SSL

时间:2013-04-09 07:32:55

标签: ios ssl https nsstream

我在iOS申请中工作。 我尝试使用https连接连接到NSStream服务器。 使用NSURLConnection,这项工作正常,我可以信任这样的挑战:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

}

但我的问题是Stream中唯一可用的代理是:

- (void) stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

} 

如何管理?

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方案。感谢Wireshark

        [inStream setProperty:(id)kCFBooleanFalse forKey:(NSString *)kCFStreamPropertyShouldCloseNativeSocket];
        [outStream setProperty:(id)kCFBooleanFalse forKey:(NSString *)kCFStreamPropertyShouldCloseNativeSocket];
        NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:1];
        [settings setObject:_certificates forKey:(id)kCFStreamSSLCertificates];
        [settings setObject:(NSString *)NSStreamSocketSecurityLevelNegotiatedSSL forKey:(NSString *)kCFStreamSSLLevel];
        [settings setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];
        [settings setObject:@"MY HOST" forKey:(NSString *)kCFStreamSSLPeerName];

        CFReadStreamSetProperty((CFReadStreamRef)inStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);
        CFWriteStreamSetProperty((CFWriteStreamRef)outStream, kCFStreamPropertySSLSettings, (CFTypeRef)settings);

最重要的是设置没有当前证书的证书。 您应该在链中设置当前标识和其他证书。 这应该由iOSX使用Identity设置,通常私有密钥将可用于流。

希望这个帮助