Skip to content

Commit 8b328de

Browse files
Merge pull request #42859 from dotnet/main
Merge main into live
2 parents 4799ca0 + 2629fb7 commit 8b328de

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

docs/core/compatibility/9.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ If you're migrating an app to .NET 9, the breaking changes listed here might aff
5454
|-------|----------------|--------------------|
5555
| [SafeEvpPKeyHandle.DuplicateHandle up-refs the handle](cryptography/9.0/evp-pkey-handle.md) | Behavioral change | Preview 7 |
5656
| [Some X509Certificate2 and X509Certificate constructors are obsolete](cryptography/9.0/x509-certificates.md) | Source incompatible | Preview 7 |
57+
| [Windows private key lifetime simplified](cryptography/9.0/private-key-lifetime.md) | Behavioral change | Preview 7 |
5758

5859
## Deployment
5960

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: "Breaking change: Windows private key lifetime simplified"
3+
description: Learn about the .NET 9 breaking change in cryptography where the lifetime of a Windows private key has been simplified.
4+
ms.date: 10/04/2024
5+
---
6+
# Windows private key lifetime simplified
7+
8+
When a workload loads a PKCS#12/PFX on Windows without setting either the <xref:System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.PersistKeySet> or <xref:System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.EphemeralKeySet> storage options, .NET determines when the private key is no longer needed and should be erased. In previous versions of .NET (and in .NET Framework), two different sets of logic were used. In .NET 9, there's a single set of logic.
9+
10+
## Previous behavior
11+
12+
Previously, when loading a certificate (and its private key) from a PKCS#12/PFX with `new X509Certificate2(pfx, password, flags)`, the loaded certificate represented the lifetime of the private key. When this certificate object was disposed (or finalized if it was garbage-collected without being disposed), the associated private key was deleted. No shared ownership or transfer of ownership occurred.
13+
14+
When loading a certificate (and its private key) from a PKCS#12/PFX with `X509Certificate2Collection.Import(pfx, password, flags)`, each loaded certificate that had a private key tracked the lifetime, as with the single certificate load. But, in addition, a marker was placed on the native copy of the certificate to indicate that any copies should also track the private key lifetime. If a second <xref:System.Security.Cryptography.X509Certificates.X509Certificate2> object was created in terms of the same underlying `PCERT_CONTEXT` value, then whichever copy was disposed (or finalized) first erased the private key out from under the other copy.
15+
16+
The following code failed (either with a <xref:System.Security.Cryptography.CryptographicException> or a <xref:System.NullReferenceException>) because the private key was deleted:
17+
18+
```csharp
19+
X509Certificate2Collection coll = new X509Certificate2Collection(pfx, password, X509KeyStorageFlags.DefaultKeySet);
20+
X509Certificate2Collection coll2 = coll.Find(X509FindType.FindBySubjectName, "", false);
21+
22+
coll2 = null;
23+
GC.Collect();
24+
GC.WaitForPendingFinalizers();
25+
26+
using (RSA key = coll[0].GetRSAPrivateKey())
27+
{
28+
key.SignData(pfx, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
29+
}
30+
```
31+
32+
## New behavior
33+
34+
Starting in .NET 9, the lifetime is always associated with the <xref:System.Security.Cryptography.X509Certificates.X509Certificate2> instance that was directly produced from the PKCS#12/PFX load.
35+
36+
The same code snippet in the [Previous behavior](#previous-behavior) section now succeeds.
37+
38+
## Version introduced
39+
40+
.NET 9 Preview 7
41+
42+
## Type of breaking change
43+
44+
This change is a [behavioral change](../../categories.md#behavioral-change).
45+
46+
## Reason for change
47+
48+
Most workloads that load a PKCS#12/PFX use the single certificate load and understand the lifetime mechanics associated with that method. The mechanics associated with the collection load were often surprising, and sometimes lead to premature key erasure.
49+
50+
## Recommended action
51+
52+
If you understood the collection-load lifetime management and depended on calling `Dispose` on a clone to cause key erasure, ensure that you're also (or instead) calling `Dispose` on the original loaded object.
53+
54+
## Affected APIs
55+
56+
- <xref:System.Security.Cryptography.X509Certificates.X509Certificate2Collection.Import*?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ items:
5454
href: cryptography/9.0/evp-pkey-handle.md
5555
- name: Some X509Certificate2 and X509Certificate constructors are obsolete
5656
href: cryptography/9.0/x509-certificates.md
57+
- name: Windows private key lifetime simplified
58+
href: cryptography/9.0/private-key-lifetime.md
5759
- name: Deployment
5860
items:
5961
- name: Deprecated desktop Windows/macOS/Linux MonoVM runtime packages
@@ -1470,6 +1472,8 @@ items:
14701472
href: cryptography/9.0/evp-pkey-handle.md
14711473
- name: Some X509Certificate2 and X509Certificate constructors are obsolete
14721474
href: cryptography/9.0/x509-certificates.md
1475+
- name: Windows private key lifetime simplified
1476+
href: cryptography/9.0/private-key-lifetime.md
14731477
- name: .NET 8
14741478
items:
14751479
- name: AesGcm authentication tag size on macOS

0 commit comments

Comments
 (0)