Permit unencrypted key exports from CNG - #109119
Merged
Merged
Conversation
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"; |
Member
There was a problem hiding this comment.
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.
Member
Author
There was a problem hiding this comment.
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.
bartonjs
reviewed
Oct 22, 2024
bartonjs
reviewed
Oct 22, 2024
bartonjs
reviewed
Oct 22, 2024
bartonjs
reviewed
Oct 22, 2024
vcsjones
marked this pull request as ready for review
October 22, 2024 19:18
bartonjs
approved these changes
Oct 22, 2024
This was referenced Oct 22, 2024
Open
Member
|
/ba-g Windows-only change, won't affect wasm. |
Member
Author
|
@GrabYourPitchforks ran 3811dde through credscan and reported it came back "clean", so, merging. |
Member
Author
|
/backport to release/9.0 |
Contributor
|
Started backporting to release/9.0: https://github.com/dotnet/runtime/actions/runs/11470191357 |
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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