Skip to content

Permit unencrypted key exports from CNG - #109119

Merged
vcsjones merged 6 commits into
dotnet:mainfrom
vcsjones:fix-cng-export
Oct 22, 2024
Merged

Permit unencrypted key exports from CNG#109119
vcsjones merged 6 commits into
dotnet:mainfrom
vcsjones:fix-cng-export

Conversation

@vcsjones

Copy link
Copy Markdown
Member

CNG, by default, loads PKCS#12 certificate private keys as "AllowExport", not "AllowsPlaintextExport". When users attempt to export the private key from a loaded PKCS#12, they will receive an error that the operation is not permitted because they are expected to perform an encrypted export.

This is counter-intuitive to some people, as the general expectation is that they can export private keys they just loaded. Starting in .NET 9, we are loading more PKCS#12 private keys in CNG instead of the legacy CSP, meaning users will hit this problem more. This is also a regression from .NET 8. The default provider changed, meaning keys that were once exportable no longer are.

This pull request makes a change similar to what we do for macOS. If a user asks for an unencrypted export of the private key, and the key does not permit that, we will ask CNG for an encrypted export of the private key and decrypt it for them. This makes the unencrypted exports "just work", as they do on other platforms.

Fixes #109059

CNG, by default, loads PKCS#12 certificate private keys as "AllowExport", not "AllowsPlaintextExport". When users attempt to export the private key from a loaded PKCS#12, they will receive an error that the operation is not permitted because they are expected to perform an encrypted export.

This is counter-intuitive to some people, as the general expectation is that they can export private keys they just loaded. Starting in .NET 9, we are loading more PKCS#12 private keys in CNG instead of the legacy CSP, meaning users will hit this problem more. This is also a regression from .NET 8. The default provider changed, meaning keys that were once exportable no longer are.

This pull request makes a change similar to what we do for macOS. If a user asks for an unencrypted export of the private key, and the key does not permit that, we will ask CNG for an encrypted export of the private key and decrypt it for them. This makes the unencrypted exports "just work", as they do on other platforms.
{
ECParameters ecparams = default;

const string TemporaryExportPassword = "DotnetExportPhrase";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to have a problem with CredScan after we merge this? We should try pushing this change to the internal mirror and make sure it doesn't trip CredScan before we merge this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was back-channeling with @GrabYourPitchforks on this - we (well, Levi) is going to run it - especially since this might be a thing we try to fix for 9.0.

Comment thread src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs Outdated
Comment thread src/libraries/Common/src/System/Security/Cryptography/ECDsaCng.ImportExport.cs Outdated
Comment thread src/libraries/System.Security.Cryptography/tests/X509Certificates/ExportTests.cs Outdated
@vcsjones
vcsjones marked this pull request as ready for review October 22, 2024 19:18
@bartonjs

Copy link
Copy Markdown
Member

/ba-g Windows-only change, won't affect wasm.

@vcsjones

Copy link
Copy Markdown
Member Author

@GrabYourPitchforks ran 3811dde through credscan and reported it came back "clean", so, merging.

@vcsjones
vcsjones merged commit 9b10a46 into dotnet:main Oct 22, 2024
@vcsjones
vcsjones deleted the fix-cng-export branch October 22, 2024 23:32
@vcsjones

Copy link
Copy Markdown
Member Author

/backport to release/9.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/11470191357

@vcsjones
vcsjones restored the fix-cng-export branch October 22, 2024 23:35
@vcsjones
vcsjones deleted the fix-cng-export branch October 23, 2024 14:36
@vcsjones vcsjones added this to the 10.0.0 milestone Oct 23, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot export RSAParameters from X509Certificate2 in .NET 9 RC2

3 participants