I use iOS 10. I rate the self-signed certificate itself below
-(void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
if ([protectionSpace authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
SecTrustRef trust = [protectionSpace serverTrust];
SecPolicyRef policyOverride = SecPolicyCreateSSL(true, (CFStringRef)@"HOSTNAME");
SecTrustSetPolicies(trust, policyOverride);
CFMutableArrayRef certificates = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
CFIndex count = SecTrustGetCertificateCount(trust);
CFIndex i=0;
for (i = 0; i < count; i++) {
SecCertificateRef item = SecTrustGetCertificateAtIndex(trust, i);
CFArrayAppendValue(certificates, item);
}
SecTrustRef newtrust = NULL;
if (SecTrustCreateWithCertificates(certificates, policyOverride, &newtrust) != errSecSuccess) {
NSLog(@"Error in SecTrustCreateWithCertificates");
[connection cancel];
return;
}
CFRelease(policyOverride);
SecTrustResultType secresult = kSecTrustResultInvalid;
if (SecTrustEvaluate(trust, &secresult) != errSecSuccess) {
[connection cancel];
return;
}
switch (secresult) {
case kSecTrustResultUnspecified:
case kSecTrustResultProceed:
{
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
return;
}
default: ;
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
break;
}
[connection cancel];
}
}
The result after the evaluation is equal to ` kSecTrustResultUnspecified`, and again the same method ' willSendRequestForAuthenticationChallenge' is called recursively. I don’t know why the method is called recursively. Let me know any code issues.
thank
source
share