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

Implementation of X509 certificate APIs on iOS #49289

Closed
1 of 2 tasks
filipnavara opened this issue Mar 8, 2021 · 6 comments · Fixed by #52191
Closed
1 of 2 tasks

Implementation of X509 certificate APIs on iOS #49289

filipnavara opened this issue Mar 8, 2021 · 6 comments · Fixed by #52191
Labels
area-System.Security os-ios Apple iOS untriaged New issue has not been triaged by the area owner

Comments

@filipnavara
Copy link
Member

filipnavara commented Mar 8, 2021

Issues #47533 and #47910 track APIs that are missing from Apple Crypto interop on iOS. When I started removing the managed part of the Interop APIs from iOS build of System.Security.Cryptography.X509Certificates I realised I'd basically end up with a useless assembly that throws PlatformNotSupportedException for everything. The underlying reason is that iOS doesn't have the macOS keychain API and even the most basic X509 manipulation relies on a working export which is implemented through this API and hence not available. To make it at least marginally working the following things probably need to be done:

  • Export certificates through SecCertificateCopyData API and expose it as AppleCryptoNative_X509GetRawData
  • Import certificates through SecCertificateCreateWithData/SecPKCS12Import API instead of temporary keychains

This should probably be analysed by someone who is more familiar with the API surface on the Apple side. It also looks like enumerating any certificate store, including the trusted root certificates, seems impossible on iOS.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Security untriaged New issue has not been triaged by the area owner labels Mar 8, 2021
@ghost
Copy link

ghost commented Mar 8, 2021

Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks
See info in area-owners.md if you want to be subscribed.

Issue Details

Issues #47533 and #47910 track APIs that are missing from Apple Crypto interop on iOS. When I started removing the managed part of the Interop APIs from iOS build of System.Security.Cryptography.X509Certificates I realised I'd basically end up with a useless assembly that throws PlatformNotSupportedException for everything. The underlying reason is that iOS doesn't have the macOS keychain API and even the most basic X509 manipulation relies on a working export which is implemented through this API and hence not available. To make it at least marginally working the following things probably need to be done:

  • Export certificates through SecCertificateCopyData API and expose it as AppleCryptoNative_X509GetRawData
  • Import certificates through SecCertificateCreateWithData API instead of temporary keychains

This should probably be analysed by someone who is more familiar with the API surface on the Apple side. It also looks like enumerating any certificate store, including the trusted root certificates, seems impossible on iOS.

Author: filipnavara
Assignees: -
Labels:

area-System.Security, untriaged

Milestone: -

@bartonjs
Copy link
Member

Import certificates through SecCertificateCreateWithData/SecPKCS12Import API instead of temporary keychains

If I recall correctly, all the stuff about temporary keychains is because "copy to keychain" (for X509Store.Add) on a keychainless certificate actually modifies the live object, which made some later operations get weird (which almost certainly means tests failed).

So this may mean that things just have to work differently between iOS and macOS. If SecPKCS12Import doesn't automatically add things to the default keychain and its SecIdentityRef values work fine across the test suite, then great. (Seems like PKCS7 collection importing is still missing, though).

@filipnavara
Copy link
Member Author

So this may mean that things just have to work differently between iOS and macOS

Very likely yes. Certainly the iOS code has to use different APIs to work at all so that's a time investment that has to be made regardless of the situation on macOS. We can try to see how the iOS-compatible code would behave on macOS later and evaluate whether there's any way to enable more sharing. On macOS there's an additional SecIdentityCreateWithCertificate API that would likely cover few more use cases that currently use the temporary keychains.

Seems like PKCS7 collection importing is still missing, though

Yep, it is. I am currently looking to establish at least parity with what Xamarin/Mono used to provide which, quite honestly, was not much.

@steveisok steveisok added the os-ios Apple iOS label Mar 12, 2021
@filipnavara
Copy link
Member Author

After #51926 is merged the functional changes for S.S.C.Algorithms are basically done. There's still some work with marking few APIs with unsupported attributes, etc. I started looking into S.S.C.X509Certificates again and I am leaning towards a completely separate PAL implementation on the managed side:

  • The certificate import can be done through SecCertificateCreateWithData/SecPKCS12Import
  • Copying certificates with private key can be done by re-exporting small PKCS12 blob and importing it through SecPKCS12Import. Unfortunately the SecIdentityCreate API is private.
  • There is a single keychain which can be used with SecItemAdd/SecItemUpdate/SecItemDelete/SecItemCopyMatching. In theory named keychains can be emulated with labels but I don't think it's worth it. It needs some additional entitlement which is currently not enabled for the Xunit tests.
  • Root certificates cannot be enumerated at all. Apple publishes the list of the root certificates so if desired it would be possible to make it an option to embed that in the application.

@filipnavara
Copy link
Member Author

First attempts seem promising. My first draft with lot of stubbed places passed around 65% of the unit tests. I will look more into it over the next couple of days.

@filipnavara
Copy link
Member Author

Seems like PKCS7 collection importing is still missing, though

On macOS PKCS7 APIs actually exist - CMSDecoderCreate, CMSDecoderCopySignerCert - and can be used for the import if desired. On iOS they are not supported unfortunately.

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels May 3, 2021
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label May 19, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Security os-ios Apple iOS untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants