Skip to content
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

Harden SecKeyCopyExternalRepresentation against sensitive keys #99840

Merged
merged 3 commits into from
Mar 26, 2024

Conversation

vcsjones
Copy link
Member

The previous approach relied on examining the error code returned from SecKeyCopyExternalRepresentation when a key needed to be exported with a password. Apple has changed the error code which resulted in breaking the detection of sensitive values.

This change looks for the kSecAttrIsSensitive attribute on a key which according to the Apple documentation, "When set to kCFBooleanTrue, the item can only be exported in an encrypted format". This should be less brittle change checking for the error code.

The previous approach relied on examining the error code returned from SecKeyCopyExternalRepresentation when a key needed to be exported with a password. Apple has changed the error code which resulted in breaking the detection of sensitive values.

This change looks for the kSecAttrIsSensitive attribute on a key which according to the Apple documentation, "When set to kCFBooleanTrue, the item can only be exported in an encrypted format". This should be less brittle change checking for the error code.
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@filipnavara
Copy link
Member

I will check it tomorrow morning. There were some odd cases around the old CSSM keys and I don’t quite remember them all.

Thanks for looking into it!

@vcsjones
Copy link
Member Author

vcsjones commented Mar 15, 2024

Yeah, that's why my initial PR was just "handle the new error code" since I would have way more confidence with that in a backport.

The way I see it we have a few of options.

  1. Trust that kSecAttrIsSensitive is present when we need it (what this PR does)
  2. Close this PR and just handle new codes as they happen. This makes it feel too reactionary to me. On the other hand, its only happened once so far and doesn't really establish a pattern.
  3. "Do both". In this PR I eliminated the error handling code path, but I suppose we could do both. Go down the SecItemExport path if either the attribute is present or we get an error code back
  4. We try the SecItemExport path on any error.

@filipnavara

This comment was marked as outdated.

@filipnavara
Copy link
Member

filipnavara commented Mar 16, 2024

I did some sanity check first to make sure that the assumptions are correct:

  1. Modern SecKey implementations (non-CSSM) always have kSecAttrIsSensitive = false, kSecAttrIsExtractable = true, and SecKeyCopyExternalRepresentation essentially cannot fail.
  2. Legacy SecKey implementations (CSSM; used in certs and keychains) mirror CSSM_KEYATTR_SENSITIVE, CSSM_KEYATTR_EXTRACTABLE flags.
  3. CSSMERR_CSP_INVALID_KEYATTR_MASK/errSecInvalidKeyAttributeMask error is used to cover two situations:
    a) The key is not extractable (kSecAttrIsExtractable == false)
    b) The key is extractable but sensitive and thus password protection is required (kSecAttrIsExtractable == true && kSecAttrIsSensitive == true)

To summarize, it's safe to skip SecKeyCopyExternalRepresentation if kSecAttrIsSensitive is set to true because it's guaranteed to fail (and in the current Apple's implementation it can only happen for CSSM keys). That said, if we are checking the attributes already, maybe it would make more sense to move the check higher up and also produce more meaningful error message for kSecAttrIsExtractable == false.

@vcsjones vcsjones merged commit 969d424 into dotnet:main Mar 26, 2024
99 of 101 checks passed
@vcsjones vcsjones deleted the seccopyextern-sensitive branch March 26, 2024 19:39
@vcsjones vcsjones added this to the 9.0.0 milestone Apr 5, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 6, 2024
@bartonjs bartonjs added the cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release. label Aug 15, 2024
@bartonjs bartonjs added the tracking This issue is tracking the completion of other related issues. label Aug 27, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security cryptographic-docs-impact Issues impacting cryptographic docs. Cleared and reused after documentation is updated each release. tracking This issue is tracking the completion of other related issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants