Last active
July 3, 2024 05:19
-
-
Save iangray001/d1a7ad9cfda411131171c427563fd38e to your computer and use it in GitHub Desktop.
How to fetch certificates from the user's keychain using Swift
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
import Foundation | |
import Security | |
var dataTypeRef: CFTypeRef? | |
let rv = SecItemCopyMatching( | |
[ | |
kSecClass: kSecClassCertificate, | |
kSecReturnAttributes: true, | |
kSecReturnRef: true, | |
kSecReturnData: true, | |
kSecMatchLimit: kSecMatchLimitAll | |
] as CFDictionary, | |
&dataTypeRef | |
) | |
var data : NSArray? | |
data = dataTypeRef as? NSArray | |
for element in data! { | |
let dict : NSDictionary = element as! NSDictionary | |
let cert : SecCertificate = dict.object(forKey: kSecValueRef) as! SecCertificate | |
let summary = SecCertificateCopySubjectSummary(cert); | |
print(summary ?? "") | |
} |
Yes I put this together back in the days of Swift 1 and 2, it is amazing how much the language has advanced. I've updated it in case someone finds it again, thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may want to update that code. First part with Swift 5 would be: