Skip to content

Instantly share code, notes, and snippets.

@jinal90
Created May 24, 2020 21:29
Show Gist options
  • Save jinal90/d78cb8c9ed1cd44ce7644c9fa894e4dc to your computer and use it in GitHub Desktop.
Save jinal90/d78cb8c9ed1cd44ce7644c9fa894e4dc to your computer and use it in GitHub Desktop.
Certificate comparison to confirm secure SSL handshake using URLSession delegate
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