Created
May 24, 2020 21:29
-
-
Save jinal90/d78cb8c9ed1cd44ce7644c9fa894e4dc to your computer and use it in GitHub Desktop.
Certificate comparison to confirm secure SSL handshake using URLSession delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { | |
if let trust = challenge.protectionSpace.serverTrust, | |
SecTrustGetCertificateCount(trust) > 0 { | |
if let certificate = SecTrustGetCertificateAtIndex(trust, 0) { | |
let data = SecCertificateCopyData(certificate) as Data | |
if certificates.contains(data) { | |
completionHandler(.useCredential, URLCredential(trust: trust)) | |
return | |
} | |
} | |
} | |
completionHandler(.cancelAuthenticationChallenge, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment