Skip to content

Load X509Certificate2 with CreateFromPemFile #93319

@lukeschlather

Description

@lukeschlather

Description

X509Certificate2.CreateFromPemFile doesn't seem to load private keys properly on Windows.

Reproduction Steps

I created a $cert and $key with:

openssl req -nodes -new  -sha256 -keyout $key -out $csr -config $cnf
openssl ca -batch -config $sslcnffile -policy policy_match -extensions usr_cert -out $cert -infiles $csr 

My project is <TargetFramework>net6.0</TargetFramework>

I created an X509Certificate2 object with:

var cert = X509Certificate2.CreateFromPemFile("$cert", "$pem");

For comparison I also created a pfx with

openssl pkcs12 -export -out $pfx -inkey $cert -in $key

And loaded it with

var pfxCert = new X509Certificate2("/secrets/localhost.pfx");

Expected behavior

The pfxCert object should be functionally identical to the cert object, or there should be an error thrown loading the pem keyfile.

Actual behavior

The cert object is created without error but the private key is not present.

Regression?

I've found some similar bugs and it generally seems the assumption is pems don't work on Windows, but the docs don't make any mention of this:

https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2.createfrompemfile?view=net-7.0

Known Workarounds

Use a pfx.

Configuration

I have been working on some other issues so I may be mistaken, but I believe this works fine in an Ubuntu Docker container but does not work in Windows 10.

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions