Skip to content

Instantly share code, notes, and snippets.

@davidmtamas
Created April 2, 2020 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmtamas/f9f2d0f923edcea915969d9c35f354e7 to your computer and use it in GitHub Desktop.
Save davidmtamas/f9f2d0f923edcea915969d9c35f354e7 to your computer and use it in GitHub Desktop.
// 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