Skip to content

Add support for the Data Protection Keychain #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 20, 2025

Conversation

maraino
Copy link
Contributor

@maraino maraino commented Jun 18, 2025

Description

This PR adds support for storing certificates in the Data Protection Keychain. This functionality is only available for code-signed applications.

Additionally, this commit includes a fix that enables retrieving a key from the Data Protection Keychain or not based on URI parameters.

This last fix slightly changes the behavior: previously, keys stored in the Secure Enclave could be retrieved without specifying se=true, but it was not possible to retrieve the proper key if a key in the Secure Enclave shares the same label as a key in the login keychain.

This commit allows to store certificates from the Data Protection
Keychain, This functionality only works on code-signed applications.

This commit also includes a fix that allows to retrieve a key from the
data protection key chain depending on the uri parameters.
@maraino maraino requested a review from hslatman June 18, 2025 02:04
@maraino maraino requested a review from joshdrake June 18, 2025 02:04
Comment on lines 1082 to 1087
// Apple recommends to set KSecUseDataProtectionKeychain parameters always
// to true for all keychains operations. This option requires to use a
// code-signed application.
if u.useDataProtectionKeychain {
dict[security.KSecUseDataProtectionKeychain] = cf.True
// The data in the keychain item cannot be accessed after a restart
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to detect the required entitlements are available in the program? If so, when the intention is to use the data protection keychain, and the application isn't codesigned / missing entitlements, can it return an error indicating that case? This may not be necessary if errors from the OS are clear enough, but I've seen enough OSError codes that weren't overly clear 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently it should be possible to know this using SecCodeCheckValidity . We can add this in a different PR.

The current behavior is:

  • When loading a certificate you will get a not found error. Something like:
mackms LoadCertificate failed: not found
  • When storing a certificate you will get an error showing that an entitlement is missing:
mackms StoreCertificateChain failed: OSStatus -34018: A required entitlement isn't present.

maraino and others added 2 commits June 18, 2025 10:32
Co-authored-by: Herman Slatman <hslatman@users.noreply.github.com>
@maraino maraino requested a review from hslatman June 18, 2025 19:05
joshdrake
joshdrake previously approved these changes Jun 18, 2025
@maraino maraino merged commit a744b21 into master Jun 20, 2025
12 checks passed
@maraino maraino deleted the mariano/data-protection-keychain branch June 20, 2025 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants