Repository navigation
Closed
Labels
Description
This is essentially regression of #39603 (which was supposed to be fixed by #41787) in .NET 6. We get the following error on our CI tests couple of times per day:
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 MailClient.Abstractions.Test.Security.Integration.SMimeTest.CertificateInstaller..ctor(Nullable`1 windowsStore, String resourcePath, SecurityManager securityManager, String password) in /Users/teamcity/actions-runner/_work/emclient/emclient/MailClient.Abstractions.Test/Security.Integration/SMimeTest.cs:line 338
at MailClient.Abstractions.Test.Security.Integration.SMimeTest.SMimeUsualSigningEncryptionTest(TestCaseParameters data) in /Users/teamcity/actions-runner/_work/emclient/emclient/MailClient.Abstractions.Test/Security.Integration/SMimeTest.cs:line 65
at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.GetResult()
at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.BeforeTestCommand.Execute(TestExecutionContext context)
at NUnit.Framework.Internal.Commands.TimeoutCommand.<>c__DisplayClass5_0.<RunTestOnSeparateThread>b__0()
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at NUnit.Framework.Internal.Commands.TimeoutCommand.Execute(TestExecutionContext context)
The tests run on two different machines - Intel and ARM Mac Mini - and it only fails on the ARM64 (ie. osx-arm64). There may be other differences between the machines and the keychains on those machines.
Activity
filipnavara commentedon Jan 4, 2023
The relevant part of the test code:
bartonjs commentedon Jan 4, 2023
Does macOS (at our minimum target) support non-persisted keychains yet? Or ways of working with SecIdentifyRef that aren't backed by keychains?
filipnavara commentedon Jan 4, 2023
Generally speaking yes. There may be some issues with interoperability with the old-style keychain keys though.
vcsjones commentedon Jan 5, 2023
Are you referring to the data protection keychain, or something else?
filipnavara commentedon Jan 5, 2023
Yep, the data protection keychain (the terminology is little fuzzy on that but let's say the iOS-style APIs).
filipnavara commentedon Feb 15, 2023
Turns out the fix in #41787 was incomplete. It fixed the intialization of
new X509Certificate(...)from PKCS#12 data by holding a reference to the keychain for the lifetime of the certificate object.The same general problem happens for this code:
In this case the temporary keychain gets disposed here while the certificates get created here without the reference to the keychain.
filipnavara commentedon Feb 15, 2023
This fix passes our unit tests. I currently don't have time to polish it though.
filipnavara commentedon Feb 15, 2023
Here's a more-or-less reliable standalone repro:
I don't see how to easily turn it into unit test since it requires precise GC and it modifies a user's keychain which is undesirable.