在iOS8.0上,AsyncSocket startTLS:CFNetwork SSLHandshake失败(-9806)

时间:2014-10-27 03:47:33

标签: ios ssl asyncsocket starttls

我的应用程序使用自签名ssl证书连接到tcp服务器。它在iOS7.1之前工作,在iOS8.0之后,它在关闭净重时出现问题,提示:CFNetwork SSLHandshake失败(-9806)并且无法重新连接。

我使用的方法是:startTLS用于设置ssl,代码如下:

// Configure SSL/TLS settings

NSMutableDictionary *sslSettings = [[NSMutableDictionary alloc] init];

NSData *pkcs12data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:certificate ofType:@"p12"]];

CFDataRef inPKCS12Data = (CFDataRef)CFBridgingRetain(pkcs12data);

CFStringRef password = CFSTR("123456");

const void *keys[] = { kSecImportExportPassphrase };

const void *values[] = { password };

CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);

OSStatus securityError = SecPKCS12Import(inPKCS12Data, options, &items);

CFRelease(options);

CFRelease(password);

[pkcs12data release];

if(securityError == errSecSuccess) {

    NSLog(@"Success opening p12 certificate.");

}

CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);

SecIdentityRef myIdent = (SecIdentityRef)CFDictionaryGetValue(identityDict,
                                                              kSecImportItemIdentity);

SecIdentityRef  certArray[1] = { myIdent };

CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, 1, NULL);

[sslSettings setObject:(id)CFBridgingRelease(myCerts) forKey:(NSString *)kCFStreamSSLCertificates];

[sslSettings setObject:NSStreamSocketSecurityLevelSSLv2 forKey:(NSString *)kCFStreamSSLLevel];

[sslSettings setObject:(id)kCFBooleanFalse forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];

0 个答案:

没有答案