Hi there, I'm continuing to build up the API on keychain, I'm trying to implement the ability to create an own certificate chain for validation purposes, similar to ssl. To this extent I need to retrieve the certificates from the System's stores but I can't seem to find a way to do this in code?
Creating a query with kSecMatchTrustedOnly
only returns certificates which are seemingly manually marked as trusted or otherwise just skips over the System roots keychain.
As far as I understand using kSecUseKeychain
doesn't work either, since (besides SecKeychain
being deprecated) it only works with SecItemAdd
.
This is MacOS
OK.
ideally I'd like to be able to support both Intel and ARM versions, though I assume that they should be the same?
Correct.
Historically macOS stored all trusted anchors in the System keychain. That’s not been the case for a while. The default trusted anchors now come from a separate trust store, shown in Keychain Access as System Roots. That is actually saved in a keychain [1], but that’s now considered an implementation detail [2]. The keychain is not in the default search list:
% security list-keychains
"/Users/quinn/Library/Keychains/login.keychain-db"
"/Library/Keychains/System.keychain"
That’s why SecItemCopyMatching
isn’t finding these items.
Back in your original post you wrote:
I'm trying to implement the ability to create an own certificate chain for validation purposes, similar to ssl.
The canonical way to do this is with a trust object (SecTrust
). You give it the leaf, zero on more intermediates, and zero or more additional trusted anchors, and it’ll try to build a chain of trust. This will consult the system trust store, so you don’t need direct access to it.
If you want to do your own trust evaluation, a trust object is the best path forward because it does all the standard stuff that users expect to just work. It handles fetching missing intermediates via AIA extension, custom trust settings, revocation, Certificate Transparency, and so on. Replicating all of this yourself is a lot of work, and your implementation will inevitably diverge from the built-in one.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] On macOS. On iOS this is different, and that’s one of the reasons why it’s important to treat this as an implementation detail.
[2] If you’re curious about the implementation, do this:
% security dump-keychain /System/Library/Keychains/SystemRootCertificates.keychain