Skip to content

X509Certificate2/CommonCrypto: Exporting a certificate without a private key to a PKCS12 store with a password fails #23636

Closed
dotnet/corefx
#42226

Description

@qmfrederik

If you have a X509Certificate2 object which represents a certificate without a private key, exporting that certificate to a Pkcs12 store will work on Windows and Linux but fails on Mac (which use CommonCrypto).

This is a regression on macOS as this worked with .NET Core 1.x (when using OpenSSL) and no longer works with .NET Core 2.0.

The following unit test reproduces the behavior:

using System;
using System.Security.Cryptography.X509Certificates;
using Xunit;

namespace temp
{
  public class UnitTest1
  {
    [Fact]
    public void Test1()
    {
      var cert = new X509Certificate2("my_cert.crt");
      var data = cert.Export(X509ContentType.Pkcs12, "password");
      Assert.NotNull(data);
    }
  }
}

The test passes on Windows & Linux but fails on macOS with the following error message:

Failed   certs.UnitTest1.Test1
Error Message:
 Interop+AppleCrypto+AppleCommonCryptoCryptographicException : One or more parameters passed to a function were not valid.
Stack Trace:
   at Interop.AppleCrypto.X509Export(X509ContentType contentType, SafeCreateHandle cfPassphrase, IntPtr[] certHandles)
   at Interop.AppleCrypto.X509ExportPfx(IntPtr[] certHandles, SafePasswordHandle exportPassword)
   at Internal.Cryptography.Pal.StorePal.AppleCertificateExporter.ExportPkcs12(SafePasswordHandle password)
   at Internal.Cryptography.Pal.StorePal.AppleCertificateExporter.Export(X509ContentType contentType, SafePasswordHandle password)
   at System.Security.Cryptography.X509Certificates.X509Certificate.Export(X509ContentType contentType, String password)
   at certs.UnitTest1.Test1() in /Users/quamotion/scratch/certs/UnitTest1.cs:line 13

my_cert.crt can be generated using the following script:

openssl genrsa -out my_key.key 2048
openssl req -new -key my_key.key -out my_request.csr
openssl x509 -req -days 3650 -in my_request.csr -signkey my_key.key -out my_cert.crt

Activity

  1. qmfrederik commented on Sep 23, 2017

    @qmfrederik
    ContributorAuthor
  2. jnm2 commented on Sep 24, 2017

    @jnm2
    Contributor

    This is off topic, but I'm curious what the use case is for password-protecting a public key.

  3. qmfrederik commented on Sep 25, 2017

    @qmfrederik
    ContributorAuthor

    @bartonjs Looking at that list, it appears the issue on macOS is writing PKCS12 files which contain no private keys.

    Is it an option to have corefx do that in managed code instead of calling the native layer?

    (For now, my workaround will most likely be to use BouncyCastle for that)

  4. qmfrederik commented on Sep 26, 2017

    @qmfrederik
    ContributorAuthor

    Just as a FYI for others who may hit the same limitaiton, this is how, as a workaround, you can create the PKCS#12 store on macOS using BouncyCastle:

    var bouncyCert = DotNetUtilities.FromX509Certificate(certificate);
    var certificateEntry = new X509CertificateEntry(bouncyCert);
    Pkcs12Store store = new Pkcs12StoreBuilder().Build();
    store.SetCertificateEntry("certificate", certificateEntry);
    
    using (MemoryStream stream = new MemoryStream())
    {
        store.Save(stream, this.Password.ToCharArray(), new SecureRandom());
        data = stream.ToArray();
    }
  5. bartonjs commented on Oct 4, 2017

    @bartonjs
    Member

    Is it an option to have corefx do that in managed code instead of calling the native layer?

    Maybe. There's a cost-of-work vs benefit-of-feature problem, though. PKCS12 is a very complicated spec, and even limiting it to the case where we called into the OS and it returned an error and then we see that !certs.Any(cert => cert.HasPrivateKey) it's a lot of code to carry around for what Apple presumably believes to be a low value scenario.

    The naive question on my end is: If you don't have any private keys, why aren't you just exporting as PKCS7?

  6. transferred this issue fromdotnet/corefxon Jan 31, 2020
  7. added this to the 5.0 milestone on Jan 31, 2020
  8. ghost locked as resolved and limited conversation to collaborators on Dec 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions