I am working on an test application which connects to a service using HTTPS. The certificate of this connection is using a custom root certificate. So I implemented the delegate for `NSURLSessionDelegate' and implemented it like this:
func URLSession(session: NSURLSession, didReceiveChallenge challenge: NSURLAuthenticationChallenge, completionHandler: (NSURLSessionAuthChallengeDisposition, NSURLCredential?) -> Void) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
let trust: SecTrustRef = challenge.protectionSpace.serverTrust!
var secresult: SecTrustResultType = SecTrustResultType(kSecTrustResultInvalid)
if SecTrustEvaluate(trust, &secresult) == errSecSuccess {
switch (Int(secresult)) {
case kSecTrustResultUnspecified:
break
case kSecTrustResultProceed:
let credential = NSURLCredential(forTrust: trust)
completionHandler(.UseCredential, credential)
return
default:
print("default")
}
}
}
completionHandler(.CancelAuthenticationChallenge, nil)
}
I get into the case kSecTrustResultProceed
, but the code shown here results in a endless loop. I always run into this error:
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
I suspect it is because the certificate is not trusted by the user, so I read about SFCertificateTrustPanel
where the user can accept the trust for a certificate. Yet this method seems not available from Swift.
How do I use SFCertificateTrustPanel
in swift?
Is there another way how I can trust a certificate, e.g. based on a fingerprint?