Created
April 2, 2020 07:12
-
-
Save davidmtamas/f9f2d0f923edcea915969d9c35f354e7 to your computer and use it in GitHub Desktop.
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
// For each certificate in the valid trust: | |
for index in 0..<SecTrustGetCertificateCount(serverTrust) { | |
// Get the public key data for the certificate at the current index of the loop. | |
guard let certificate = SecTrustGetCertificateAtIndex(serverTrust, index), | |
let publicKey = SecCertificateCopyPublicKey(certificate), | |
let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, nil) else { | |
return false | |
} | |
// Hash the key, and check it's validity. | |
let keyHash = hash(data: (publicKeyData as NSData) as Data) | |
if hashes.contains(keyHash) { | |
// Success! This is our server! | |
return true | |
} | |
} | |
// If none of the calculated hashes match any of our stored hashes, the connection we tried to establish is untrusted. | |
return false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment