我正在为 iOS 5 开发,并且真的不想使用非 ARC 代码,所以我选择自己实现这个而不是使用 AFNetworking。这也可能是一个大问题,所以我把它分成两个较小的部分。
1) 在 iOS 5 中使用 https 连接到服务器。我在这里使用从“iOS 5 Programming Pushing the Limits”中提取的代码。因为我正在为 iOS 5 开发,所以我不在我的项目中使用不推荐使用的方法。“RNSecTrustEvaluateAsX509”是一种将证书重新评估为简单 X.509 证书而不是 SSL 握手的一部分的方法。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLProtectionSpace *protSpace = challenge.protectionSpace;
SecTrustRef trust = protSpace.serverTrust;
SecTrustResultType result = kSecTrustResultFatalTrustFailure;
OSStatus status = SecTrustEvaluate(trust, &result);
if (status == errSecSuccess && result == kSecTrustResultRecoverableTrustFailure) {
SecCertificateRef cert = SecTrustGetCertificateAtIndex(trust, 0);
CFStringRef subject = SecCertificateCopySubjectSummary(cert);
NSLog(@"Trying to access %@. Got %@.", protSpace.host,
(__bridge id)subject);
CFRange range = CFStringFind(subject, CFSTR("192.168.1.100"), kCFCompareAnchored|kCFCompareBackwards);
if (range.location != kCFNotFound) {
NSLog(@"Creating new trust certificate.Ignoring the hostname.");
status = RNSecTrustEvaluateAsX509(trust, &result);
}
CFRelease(subject);
}
if (status == errSecSuccess) {
switch (result) {
case kSecTrustResultInvalid:
case kSecTrustResultDeny:
case kSecTrustResultFatalTrustFailure:
case kSecTrustResultOtherError:
case kSecTrustResultRecoverableTrustFailure: {
NSLog(@"Failing due to result: %lu", result);
[challenge.sender cancelAuthenticationChallenge:challenge];
}
break;
case kSecTrustResultProceed:
case kSecTrustResultUnspecified: {
NSLog(@"Successing with result: %lu", result);
NSURLCredential *cred = [NSURLCredential credentialForTrust:trust];
[challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
}
break;
default:
NSAssert(NO,@"Unexpected result from trust evaluation: %d", result);
break;
}
}
else {
// Something was broken
NSLog(@"Complete failure with code: %lu", status);
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
它连接到服务器,但我总是收到一条错误消息“无法完成操作(NSURLErrorDomain 错误 -1012)”。控制台显示“由于结果 5 而失败”,这意味着我得到了 kSecTrustResultRecoverableTrustFailure。我怀疑这是因为我在服务器上使用自签名证书。这导致如下第二个问题。
2)自签名证书导致问题。所以我添加了这些行
// Self-signed certificates need to be validated manually.
NSArray *anchors = [self serverAnchors];
SecTrustSetAnchorCertificates(trust, (__bridge CFArrayRef)anchors);
SecTrustSetAnchorCertificatesOnly(trust, YES);
就在之前
OSStatus status = SecTrustEvaluate(trust, &result);
在上面的 willSendRequestForAuthenticationChallenge 方法中。我还创建了一个方法:
- (NSArray *)serverAnchors
{
static NSArray *anchors = nil;
if (!anchors) {
NSData *caData = [CA_CERTS dataUsingEncoding:NSUTF8StringEncoding];
SecCertificateRef caRef = SecCertificateCreateWithData(kCFAllocatorDefault, (__bridge CFDataRef) caData);
anchors = [NSArray arrayWithObjects:(__bridge id)caRef, nil];
if (caRef) {
CFRelease(caRef);
}
}
return anchors;
}
我将 CA_CERTS 定义为“der”格式的证书数据,这是我通过 SecCertificateCopyData 从服务器获取的 NSString。但我仍然不断收到 kSecTrustResultRecoverableTrustFailure。我真的不知道我在这里做的是否正确。如何使用自己的数据手动验证来自服务器的自签名证书?更具体地说,如何从 iOS 获取其数据?