You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
macOS: Cannot add to X509Store [regression from 3.1] #39603
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.
Some other background on this, removing from the keychain does work, so it seems not related to the keychain being locked or in an inconsistent state. I did note that Big Sur is getting changes to how importing certificates as root is handled, but saw nothing describing this exact use case.
Other thing to note: I can't reproduce this in netcoreapp3.1. I wonder if this has anything to do with how the binaries are signed and/or if 5 previews are signed differently than 3.1 official builds. Or, something broke between 3.1 and 5.0.
Thank you, @vcsjones! Are you planning to investigate this further? If not, we'll get someone from the team assigned to it so we can get it into 5.0 (probably RC2).
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.
@vcsjones I got some guidance on how we could determine if this is related to signing/notarization or if there's a potential functional regression here. I'm trying to get my machine set up to be able to check this, but it looks like it'll take me some time, so I wanted to share with you in case this is quick for you to verify.
We would need to compare when running with a signed apphost to running with an unsigned apphost.
, I am surprised that this is failing out of scope.
Note that this is all new code in 5.0. ImportPkcs12 call at #39603 (comment) 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.
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 ImportPkcs12 looks reasonable:
000122 E829FBFFFF call AppleCrypto:CreateTemporaryKeychain():System.Security.Cryptography.Apple.SafeTemporaryKeychainHandle00012748894590mov gword ptr [rbp-70H],rax00012B EB09 jmp SHORT G_M61524_IG08 ;; bbWeight=1 PerfScore 13.50G_M61524_IG07:00012D E8CEFAFFFF call AppleCrypto:SecKeychainCopyDefault():System.Security.Cryptography.Apple.SafeKeychainHandle00013248894590mov gword ptr [rbp-70H],rax ;; bbWeight=1 PerfScore 2.00G_M61524_IG08:000136 488B7D90 movrdi, gword ptr [rbp-70H]00013A 48897DC0 mov gword ptr [rbp-40H],rdi00013E 488B7DC0 movrdi, gword ptr [rbp-40H]000142 48897DB8 mov gword ptr [rbp-48H],rdi ;; bbWeight=1 PerfScore 4.00G_M61524_IG09:000146 488B7DF0 movrdi, bword ptr [rbp-10H]00014A 488B75F8 movrsi, qword ptr [rbp-08H]00014E 488B55E8 movrdx, gword ptr [rbp-18H]000152 8B4DCC movecx, dword ptr [rbp-34H]000155 4C8B45C0 movr8, gword ptr [rbp-40H]000159 E85AC0FFFF call Internal.Cryptography.Pal.AppleCertificatePal:ImportPkcs12(System.ReadOnlySpan`1[Byte],Microsoft.Win32.SafeHandles.SafePasswordHandle,bool,System.Security.Cryptography.Apple.SafeKeychainHandle):Internal.Cryptography.ICertificatePal00015E 488945B0 mov gword ptr [rbp-50H],rax;; stack slots rbp-70, rbp-40, rbp-48 are untrackedRegister slot id for reg rax = 17.Register slot id for reg rsi = 18.Register slot id for reg rdi = 19.Register slot id for reg rdx = 20.Register slot id for reg r8 = 21.Register slot id for reg rcx = 22.Set state of slot 17atinstr offset 0x127 to Live.Set state of slot 17atinstr offset 0x12d to Dead.Set state of slot 17atinstr offset 0x132 to Live.Set state of slot 17atinstr offset 0x136 to Dead.Set state of slot 19atinstr offset 0x13a to Live.Set state of slot 19atinstr offset 0x146 to Dead.Set state of slot 16atinstr offset 0x14a to Live.Set state of slot 20atinstr offset 0x152 to Live.Set state of slot 21atinstr offset 0x159 to Live.Set state of slot 17atinstr offset 0x15e to Live.
Hey @vcsjones -- @wfurt is going to take a look into this this week. Thanks for your help on the investigation; you've given us a great place to continue from. 💯
Activity
vcsjones commentedon Jul 20, 2020
Some other background on this, removing from the keychain does work, so it seems not related to the keychain being locked or in an inconsistent state. I did note that Big Sur is getting changes to how importing certificates as root is handled, but saw nothing describing this exact use case.
Other thing to note: I can't reproduce this in netcoreapp3.1. I wonder if this has anything to do with how the binaries are signed and/or if 5 previews are signed differently than 3.1 official builds. Or, something broke between 3.1 and 5.0.
jeffhandley commentedon Aug 19, 2020
Thank you, @vcsjones! Are you planning to investigate this further? If not, we'll get someone from the team assigned to it so we can get it into 5.0 (probably RC2).
vcsjones commentedon Aug 19, 2020
@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.
jeffhandley commentedon Aug 21, 2020
@vcsjones I got some guidance on how we could determine if this is related to signing/notarization or if there's a potential functional regression here. I'm trying to get my machine set up to be able to check this, but it looks like it'll take me some time, so I wanted to share with you in case this is quick for you to verify.
We would need to compare when running with a signed apphost to running with an unsigned apphost.
vcsjones commentedon Aug 22, 2020
Hm, I think I can pull down 3.1 and build an unsigned version and see if it starts failing.
jeffhandley commentedon Aug 22, 2020
Thanks, @vcsjones. Here's what I understand:
jeffhandley commentedon Aug 22, 2020
I set this repro up on my MacBook while still on Catalina, and I was able to repro to get the same error as reported.
Repro Steps:
dotnet new consoleProgram.csto have the same code as is shown in the issue descriptionopenssl req -x509 -days 365 -newkey rsa:4096 -keyout cert.key -out cert.crt(using "test" as the PEM pass phrase)openssl pkcs12 -export -in cert.crt -inkey cert.key -out cert.pfx(using "test" as the export password)dotnet runI'm going to try this against .NET Core 3.1 on this setup.
jeffhandley commentedon Aug 22, 2020
It runs fine against .NET Core 3.1 on my Catalina machine but fails with .NET 5.0 on Catalina.
Combinations / Results
<TargetFramework>netcoreapp3.1</TargetFramework><UseAppHost>true</UseAppHost>bin/Debug/netcoreapp3.1/x509.dll: code object is not signed at all(codesign -d bin/Debug/netcoreapp3.1/x509.dll)dotnet runsucceedsdotnet run bin/Debug/netcoreapp3.1/x509.dllsucceeds<UseAppHost>true</UseAppHost>bin/Debug/netcoreapp3.1/x509.dll: code object is not signed at all(codesign -d bin/Debug/netcoreapp3.1/x509.dll)dotnet runsucceedsdotnet run bin/Debug/netcoreapp3.1/x509.dllsucceeds<TargetFramework>net5.0</TargetFramework><UseAppHost>true</UseAppHost>bin/Debug/net5.0/x509.dll: code object is not signed at all(codesign -d bin/Debug/net5.0/x509.dll)dotnet runfailsdotnet run bin/Debug/net5.0/x509.dllfails<UseAppHost>true</UseAppHost>bin/Debug/net5.0/x509.dll: code object is not signed at all(codesign -d bin/Debug/net5.0/x509.dll)dotnet runfailsdotnet run bin/Debug/net5.0/x509.dllfails@jeffschwMSFT do you have suggestions on where to go next from here in the investigation?
29 remaining items
jkotas commentedon Aug 26, 2020
Note that this is all new code in 5.0. ImportPkcs12 call at #39603 (comment) 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.
AndyAyersMS commentedon Aug 26, 2020
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:jeffhandley commentedon Sep 1, 2020
Hey @vcsjones -- @wfurt is going to take a look into this this week. Thanks for your help on the investigation; you've given us a great place to continue from. 💯
jeffhandley commentedon Sep 1, 2020
I'm editing the title to remove "Big Sur" since this repro'd for me on Catalina as well.
[-]macOS Big Sur: Cannot add to X509Store[/-][+]macOS: Cannot add to X509Store [regression from 3.1][/+]vcsjones commentedon Sep 1, 2020
Thanks a ton. Sorry I've just been underwater with other things lately.
wfurt commentedon Sep 4, 2020
reopening to 5.0. Master is now for 6.0
danmoseley commentedon Sep 4, 2020
I was wondering what regressed it, and I guess it was probably the big PFX reader/writer change: af6ab5f dotnet/corefx#42226
jeffhandley commentedon Sep 5, 2020
Fixed in 5.0.0 as well with #41882.