runtime: macOS: Cannot add to X509Store [regression from 3.1]
I cannot seem to write to an X509Store in macOS. Code to reproduce:
using System.Security.Cryptography.X509Certificates;
namespace scratch {
class Program {
static void Main(string[] args) {
using X509Certificate2 cert = new X509Certificate2("cert.pfx", "test", X509KeyStorageFlags.Exportable);
using X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
}
}
}
This seems to always fail on store.Add(cert). Exception:
Unhandled exception. Interop+AppleCrypto+AppleCommonCryptoCryptographicException: UNIX[Undefined error: 0]
at Interop.AppleCrypto.X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)
at Internal.Cryptography.Pal.StorePal.AppleKeychainStore.Add(ICertificatePal cert)
at System.Security.Cryptography.X509Certificates.X509Store.Add(X509Certificate2 certificate)
at scratch.Program.Main(String[] args) in /Users/kjones/Projects/scratch/Program.cs:line 9
The unit tests for this appear to be in X509StoreMutableTests_OSX. When I attempt to run them, all of them are skipped because the “can I write to the store?” conditional fails:
Writing precondition failed with kPOSIXErrorBase, skipping tests.
This is not an SSH session, or a VM, or anything out of the ordinary, it’s a login zsh shell.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 45 (45 by maintainers)
As a test, if that code is changed to:
Then the problem goes away, too.
Thanks a ton. Sorry I’ve just been underwater with other things lately.
Since this fails with debug codegen it’s also unlikely to be a jit GC tracking issue.
GC info leading up to the call to
ImportPkcs12looks reasonable:Note that this is all new code in 5.0. ImportPkcs12 call at https://github.com/dotnet/runtime/issues/39603#issuecomment-681047300 did not exist in 3.1. I think it is most likely a bug in the new code, it is unlikely to be a bug in the core runtime.
I don’t think this is a GC issue - I threw in the SuppressFinalize so the GC wouldn’t clean up the handle (which deletes the temp keychain) and force it to leak. That snippet was solely there to at least confirm that is the source of some object-lifetime issue.
I have repro using latest daily 5.0 build:
I will take a look and share findings.
SecKeychainItemCreateCopyhttps://github.com/dotnet/runtime/blob/ec59f65100d741539b00e308294cc53f04fc326b/src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_keychain.c#L354
The resulting OS Status is kPOSIXErrorBase. That isn’t a very descriptive error code.
Native stack:
It looks like it is having trouble opening the keychain in the temporary directory.
@jeffhandley
Yeah, but I haven’t made a ton of progress. I think a good first step would be to see if someone other than me can repro it, and do the unit tests actually run (not just skipped). Maybe it just doesn’t like me.
I can still repro it on Big Sur beta 4, I’m installing 5 right now.