Skip to content

macOS: Cannot add to X509Store [regression from 3.1] #39603

Closed

Description

@vcsjones

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.

Activity

  1. added
    untriaged
    on Jul 18, 2020
  2. added
    os-macos
    on Jul 18, 2020
  3. vcsjones commented on Jul 20, 2020

    @vcsjones
    MemberAuthor

    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.

  4. added this to the 5.0.0 milestone on Aug 19, 2020
  5. removed
    untriaged
    on Aug 19, 2020
  6. jeffhandley commented on Aug 19, 2020

    @jeffhandley
    Member

    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).

  7. vcsjones commented on Aug 19, 2020

    @vcsjones
    MemberAuthor

    @jeffhandley

    Are you planning to investigate this further?

    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.

  8. jeffhandley commented on Aug 21, 2020

    @jeffhandley
    Member

    @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.

  9. vcsjones commented on Aug 22, 2020

    @vcsjones
    MemberAuthor

    Hm, I think I can pull down 3.1 and build an unsigned version and see if it starts failing.

  10. jeffhandley commented on Aug 22, 2020

    @jeffhandley
    Member

    Thanks, @vcsjones. Here's what I understand:

    • If it fails with an unsigned apphost:
      • Entitlements are not enforced, so there's a product regression
    • If it fails only using a signed apphost:
      • There might be a new entitlement that needs to be added
  11. jeffhandley commented on Aug 22, 2020

    @jeffhandley
    Member

    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.

    • OS: macOS Catalina
    • SDK: dotnet-sdk-5.0.100.rc.1.20422.1-osx-x64.pkg

    Repro Steps:

    1. dotnet new console
    2. Update Program.cs to have the same code as is shown in the issue description
    3. openssl req -x509 -days 365 -newkey rsa:4096 -keyout cert.key -out cert.crt (using "test" as the PEM pass phrase)
    4. openssl pkcs12 -export -in cert.crt -inkey cert.key -out cert.pfx (using "test" as the export password)
    5. dotnet run

    I'm going to try this against .NET Core 3.1 on this setup.

  12. jeffhandley commented on Aug 22, 2020

    @jeffhandley
    Member

    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>
      • Without <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 run succeeds
        • dotnet run bin/Debug/netcoreapp3.1/x509.dll succeeds
      • With <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 run succeeds
        • dotnet run bin/Debug/netcoreapp3.1/x509.dll succeeds
    • <TargetFramework>net5.0</TargetFramework>
      • Without <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 run fails
        • dotnet run bin/Debug/net5.0/x509.dll fails
      • With <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 run fails
        • dotnet run bin/Debug/net5.0/x509.dll fails

    @jeffschwMSFT do you have suggestions on where to go next from here in the investigation?

  13. 29 remaining items

  14. jkotas commented on Aug 26, 2020

    @jkotas
    Member

    , 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.

  15. AndyAyersMS commented on Aug 26, 2020

    @AndyAyersMS
    Member

    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.SafeTemporaryKeychainHandle
    000127 48894590             mov      gword ptr [rbp-70H], rax
    00012B EB09                 jmp      SHORT G_M61524_IG08
    						;; bbWeight=1    PerfScore 13.50
    G_M61524_IG07:
    00012D E8CEFAFFFF           call     AppleCrypto:SecKeychainCopyDefault():System.Security.Cryptography.Apple.SafeKeychainHandle
    000132 48894590             mov      gword ptr [rbp-70H], rax
    						;; bbWeight=1    PerfScore 2.00
    G_M61524_IG08:
    000136 488B7D90             mov      rdi, gword ptr [rbp-70H]
    00013A 48897DC0             mov      gword ptr [rbp-40H], rdi
    00013E 488B7DC0             mov      rdi, gword ptr [rbp-40H]
    000142 48897DB8             mov      gword ptr [rbp-48H], rdi
    						;; bbWeight=1    PerfScore 4.00
    G_M61524_IG09:
    000146 488B7DF0             mov      rdi, bword ptr [rbp-10H]
    00014A 488B75F8             mov      rsi, qword ptr [rbp-08H]
    00014E 488B55E8             mov      rdx, gword ptr [rbp-18H]
    000152 8B4DCC               mov      ecx, dword ptr [rbp-34H]
    000155 4C8B45C0             mov      r8, 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.ICertificatePal
    00015E 488945B0             mov      gword ptr [rbp-50H], rax
    
    ;; stack slots rbp-70, rbp-40, rbp-48 are untracked
    
    Register 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 17 at instr offset 0x127 to Live.
    Set state of slot 17 at instr offset 0x12d to Dead.
    Set state of slot 17 at instr offset 0x132 to Live.
    Set state of slot 17 at instr offset 0x136 to Dead.
    Set state of slot 19 at instr offset 0x13a to Live.
    Set state of slot 19 at instr offset 0x146 to Dead.
    Set state of slot 16 at instr offset 0x14a to Live.
    Set state of slot 20 at instr offset 0x152 to Live.
    Set state of slot 21 at instr offset 0x159 to Live.
    Set state of slot 17 at instr offset 0x15e to Live.
    
  16. jeffhandley commented on Sep 1, 2020

    @jeffhandley
    Member

    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. 💯

  17. jeffhandley commented on Sep 1, 2020

    @jeffhandley
    Member

    I'm editing the title to remove "Big Sur" since this repro'd for me on Catalina as well.

  18. changed the title [-]macOS Big Sur: Cannot add to X509Store[/-] [+]macOS: Cannot add to X509Store [regression from 3.1][/+] on Sep 1, 2020
  19. vcsjones commented on Sep 1, 2020

    @vcsjones
    MemberAuthor

    @wfurt is going to take a look into this this week.

    Thanks a ton. Sorry I've just been underwater with other things lately.

  20. wfurt commented on Sep 4, 2020

    @wfurt
    Member

    reopening to 5.0. Master is now for 6.0

  21. reopened this on Sep 4, 2020
  22. danmoseley commented on Sep 4, 2020

    @danmoseley
    Contributor

    I was wondering what regressed it, and I guess it was probably the big PFX reader/writer change: af6ab5f dotnet/corefx#42226

  23. jeffhandley commented on Sep 5, 2020

    @jeffhandley
    Member

    Fixed in 5.0.0 as well with #41882.

  24. ghost locked as resolved and limited conversation to collaborators on Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions