runtime: Loading PFX without admin causes CryptographicException

Description

After upgrading to .Net 9.0 on a API project the following codes throws the error “System.Security.Cryptography.CryptographicException: ‘Access denied.’” when not run as Admin. This works fine without admin on .net 8.0.

builder.WebHost.UseKestrel(options =>
{
    options.ListenAnyIP(443, builder =>
    {
            builder.UseHttps("D:\\private_cert.pfx", "password");
    });
});

Reproduction Steps

  1. Create a new .Net 9.0 API project.
  2. In Program.cs load a certifcate using the code provided

Expected behavior

Project runs without issue

Actual behavior

Exception thrown: System.Security.Cryptography.CryptographicException: ‘Access denied.’

 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.ImportPfx(System.ReadOnlySpan<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12NoLimits(System.ReadOnlyMemory<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, ref System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return earlyReturn)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(System.ReadOnlyMemory<byte> data, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadFromFile<System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return>(string path, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits, System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadFromFileFunc<System.Security.Cryptography.X509Certificates.X509CertificateLoader.Pkcs12Return> loader)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12PalFromFile(string path, System.ReadOnlySpan<char> password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags, System.Security.Cryptography.X509Certificates.Pkcs12LoaderLimits loaderLimits)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.CertificatePal.FromBlobOrFile(System.ReadOnlySpan<byte> rawData, string fileName, Microsoft.Win32.SafeHandles.SafePasswordHandle password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags)	Unknown
 	System.Security.Cryptography.dll!System.Security.Cryptography.X509Certificates.X509Certificate.X509Certificate(string fileName, string password, System.Security.Cryptography.X509Certificates.X509KeyStorageFlags keyStorageFlags)	Unknown
 	Microsoft.AspNetCore.Server.Kestrel.Core.dll!Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.UseHttps(Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions listenOptions, string fileName, string password)	Unknown
>	Program.<Main>$.AnonymousMethod__0_18(Microsoft.AspNetCore.Server.Kestrel.Core.ListenOptions builder) Line 52	C#

Regression?

Yes, works on .Net 8.0

Known Workarounds

No response

Configuration

.Net 9.0 Windows 11 26100.2314 x64

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 8
  • Comments: 22 (6 by maintainers)

Most upvoted comments

I was able to get the file method working by converting the pfx file to a p12 file.

This approach was mentioned in on this comment https://stackoverflow.com/a/73095429.

I don’t understand enough about the pfx/p12 difference to know whether this is a good approach or not but it resolved my problem without changing system folder permissions and without running as admin.

.\openssl pkcs12 -in c:\certs\cert.pfx -out c:\certs\cert.pem -passin pass:FooBlaa -passout pass:FooBlaa
.\openssl pkcs12 -export -in c:\certs\cert.pem -out c:\certs\cert.p12 -passin pass:FooBlaa -passout pass:FooBlaa

Update the path to reference the cert.p12 file.

{
  "Kestrel": {
    "EndPoints": {
      "Https": {
        "Url": "https://thehostname:7269"
      }
    },
    "Certificates": {
      "Default": {
        "Path": "C:\\certs\\cert.p12",
        "Password": "FooBlaa"
      }
    }
  }
}

The app then ran ok without admin privileges and used the specified cert in the HTTPS connection.

In case it’s relevant in terms of reproducing the error - we run as non admin users and use a distinct account to elevate to admin.

same issue, but when I was trying to create a self-signed cert. I have the following piece of code, which worked fine on .NET8

            X509Certificate2? newCert = null;
            using (RSA parent = RSA.Create(2048))
            {
                CertificateRequest parentReq = new CertificateRequest("cn=SelfSignedName", parent, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

                parentReq.CertificateExtensions.Add(item: new X509BasicConstraintsExtension(certificateAuthority: false, hasPathLengthConstraint: false, pathLengthConstraint: 0, critical: false));
                parentReq.CertificateExtensions.Add(item: new X509SubjectKeyIdentifierExtension(key: parentReq.PublicKey, critical: false));

                using (X509Certificate2 parentCert = parentReq.CreateSelfSigned(TimeProvider.GetUtcNow().AddDays(-5), TimeProvider.GetUtcNow().AddDays(365)))
                {
                    newCert = new X509Certificate2(parentCert.Export(X509ContentType.Pkcs12), string.Empty, 
X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
                }
                return newCert;
            }

Currently on .NET9, creation of newCert fails with the following stack -

StackTrace:System.Security.Cryptography.CryptographicException: Access denied.
   at SafeCertStoreHandle System.Security.Cryptography.X509Certificates.X509CertificateLoader.ImportPfx(ReadOnlySpan<byte> data, ReadOnlySpan<char> password, X509KeyStorageFlags keyStorageFlags)
   at void System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12NoLimits(ReadOnlyMemory<byte> data, ReadOnlySpan<char> password, X509KeyStorageFlags keyStorageFlags, ref Pkcs12Return earlyReturn)
   at Pkcs12Return System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(ReadOnlyMemory<byte> data, ReadOnlySpan<char> password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at ICertificatePal System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12Pal(ReadOnlySpan<byte> data, ReadOnlySpan<char> password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at CertificatePal System.Security.Cryptography.X509Certificates.CertificatePal.FromBlobOrFile(ReadOnlySpan<byte> rawData, string fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at new System.Security.Cryptography.X509Certificates.X509Certificate(byte[] rawData, string password, X509KeyStorageFlags keyStorageFlags)

I verified that running it as admin solves this but sadly, I cannot run this code as admin as the nominal flow. Any other workarounds?

We’ve got the same issue after .net 9 upgrade. Even when using the X509KeyStorageFlags option I still get “Access denied”. Only workaround is to run as admin ☹

I was having the same issue with: new X509Certificate2(path, password);

And when using the X509CertificateLoader I got an exception (Win32Exception: The credentials supplied to the package were not recognized) when the connection was made, because the private key was not loaded correctly: X509CertificateLoader.LoadPkcs12FromFile(path, password)

I was able to solve the issue by adding X509KeyStorageFlags: X509CertificateLoader.LoadPkcs12FromFile(path, password,, X509KeyStorageFlags.UserKeySet);

@krwq - any update on this ?

We have this code, works fine in .NET 8 (admin and normal mode). Moving to .NET 9 , it works in admin mode. In normal mode , getting access denied.

Edit: It works fine on MacOS , is specific to Windows

internal static X509Certificate2 CreateSelfSignedCertificate(string commonName, string country, string state, string locality, string organization, string organizationUnit, SecureString password, string friendlyName, DateTimeOffset from, DateTimeOffset to, string[] sanNames = null)
{
	SubjectAlternativeNameBuilder sanBuilder = new SubjectAlternativeNameBuilder();
	if (sanNames != null)
	{
		foreach (var sanName in sanNames)
		{
			sanBuilder.AddDnsName(sanName);
		}
	}
	else
	{
		sanBuilder.AddDnsName("localhost");
		sanBuilder.AddDnsName(Environment.MachineName);
	}

	var x500Values = new List<string>();
	if (!string.IsNullOrWhiteSpace(commonName)) x500Values.Add($"CN={commonName}");
	if (!string.IsNullOrWhiteSpace(country)) x500Values.Add($"C={country}");
	if (!string.IsNullOrWhiteSpace(state)) x500Values.Add($"S={state}");
	if (!string.IsNullOrWhiteSpace(locality)) x500Values.Add($"L={locality}");
	if (!string.IsNullOrWhiteSpace(organization)) x500Values.Add($"O={organization}");
	if (!string.IsNullOrWhiteSpace(organizationUnit)) x500Values.Add($"OU={organizationUnit}");

	string distinguishedNameString = string.Join("; ", x500Values);

	X500DistinguishedName distinguishedName = new X500DistinguishedName(distinguishedNameString);

	using (RSA rsa = RSA.Create(2048))
	{                
		var request = new CertificateRequest(distinguishedName, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

		request.CertificateExtensions.Add(
			new X509KeyUsageExtension(X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DigitalSignature, false));

		request.CertificateExtensions.Add(
		   new X509EnhancedKeyUsageExtension(
			   new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") }, false));

		request.CertificateExtensions.Add(sanBuilder.Build());

		var certificate = request.CreateSelfSigned(from, to);

		if (Platform.IsWindows)
		{
			certificate.FriendlyName = friendlyName;
		}

		return new X509Certificate2(certificate.Export(X509ContentType.Pfx, password), password, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
	}
}

AFAIK p12 and pfx should be both PKCS#12. The only reason I can see this could help is re-export - I’m guessing something changed in the inner structure which causes it to now work.

@krwq

I tried upgrading to .NET 9 and am facing the exact same issue. Here is how the certificate is created and loaded:

appsettings.Development.json:

	"Kestrel": {
		"Certificates": {
			"Default": {
				"Path": "../data/certs/wildcard.local.pfx",
				"Password": "P@ssw0rd",
				"AllowInvalid": true
			}
		}
	},

Local dev wildcard SSL certificate

  • Create Certificate from Powershell

    $devCert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -Subject "*.mydomain.local" -DnsName "mydomain.local", "*.mydomain.local" -FriendlyName "LocalWildcard" -NotAfter (Get-Date).AddYears(10)
    
    Export-PfxCertificate -Cert $devCert -FilePath 'C:\dev\data\certs\wildcard.mydomain.local.pfx' -Password (ConvertTo-SecureString -AsPlainText 'P@ssw0rd' -Force)
    
    wsl
    cd /mnt/c/dev/data/certs
    openssl pkcs12 -in wildcard.mydomain.local.pfx -nocerts -nodes -out mydomain.local.key
    openssl pkcs12 -in wildcard.mydomain.local.pfx -nokeys -out mydomain.local.crt
    
  • Configure permissions in Windows Certificate Store

    1. Goto to Run and type mmc.exe.
    2. Click on files and then Add Certificates Snap-In
    3. Click on Certificates and Add. Select Computer Account and then Local Computer and Finish.
    4. Now go into the Certificate Store you added. Go to Personal then Certicicates and you can see mydomain.local.
    5. Right click, go to All Tasks / Manager Private Keys. Add Users with “Read Access”.
    6. Copy this certificate into Trusted People / Certificates

Previous “Access Denied” resolution (but not working here)

You need Read/Write on Everyone. See MS documentation and Stack Overflow Post, otherwise you will get the following error in the console when running the Web Server when running as non-Admin:

System.Security.Cryptography.CryptographicException: Access denied.
   at System.Security.Cryptography.X509Certificates.CertificatePal.FilterPFXStore(ReadOnlySpan`1 rawData, SafePasswordHandle password, PfxCertStoreFlags pfxCertStoreFlags)
   at System.Security.Cryptography.X509Certificates.CertificatePal.FromBlobOrFile(ReadOnlySpan`1 rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Certificates.CertificateConfigLoader.LoadCertificate(CertificateConfig certInfo, String endpointName)
   at Microsoft.AspNetCore.Server.Kestrel.Core.TlsConfigurationLoader.LoadDefaultCertificate(ConfigurationReader configurationReader)
   ...

But the current .NET 9 error is:

"Exception": "System.Security.Cryptography.CryptographicException: Access denied.
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.ImportPfx(ReadOnlySpan`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12NoLimits(ReadOnlyMemory`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12Return& earlyReturn)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(ReadOnlyMemory`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadFromFile[T](String path, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits, LoadFromFileFunc`1 loader)
   at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12PalFromFile(String path, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
   at System.Security.Cryptography.X509Certificates.CertificatePal.FromBlobOrFile(ReadOnlySpan`1 rawData, String fileName, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags)

“Workarounds”

  1. When running my web server as admin, no error.

OR

Non-admin by loading from the Certificate Store in appsettings.Development.json:

	"Kestrel": {
		"Certificates": {
			"Default": {
				"Store": "My",
				"Location": "LocalMachine",
				"Subject": "*.mydomain.local",
				"Password": "P@ssw0rd",
				"AllowInvalid": true
			}
		}
	},

I have a different scenario which is running into trouble with .NET9 and which is failing for potentially the same reason.

I understand that

X509Certificate2 certificate = new X509Certificate2(byte[] data, string password)

… now is obsolete with .NET9, which is why I replaced it by

X509Certificate2 certificate = X509CertificateLoader.LoadPkcs12(byte[] data, string password)

The certificate itself is used by the Kestrel server:

builder.WebHost.ConfigureKestrel(kestrel =>
{
	kestrel.ListenAnyIP(port, portOptions =>
	{
		portOptions.UseHttps(certificate);
	});
});

This works fine if the Kestrel server is running on the target (Linux) server. However, for development and debugging purposes I’m running the Kestrel server on my local Windows 11 machine as well where a SSH script reroutes the remote server port to my local machine.

Code above only works on my local machine if I’m running Visual Studio as administrator. The local Kestrel server can’t be reached if running as non-admin.

Can you please confirm/deny that the source of trouble is the same as reports above? Thanks in advance

Same issue here: dotnet/aspnetcore#59300