Skip to content

Commit e7f04ed

Browse files
authored
breaking change: X509Certificate and PublicKey key parameters can be null (#45343)
* Add documentation for breaking change: X509Certificate and PublicKey key parameters can be null * Revert "Add documentation for breaking change: X509Certificate and PublicKey key parameters can be null" This reverts commit 96e0acd. * [Breaking change]: X509Certificate and PublicKey key parameters can be null Fixes #45325 * whoops, thanks @adegeo
1 parent e58c849 commit e7f04ed

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

docs/core/compatibility/10.0.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ If you're migrating an app to .NET 10, the breaking changes listed here might af
4141

4242
## Cryptography
4343

44-
| Title | Type of change | Introduced version |
45-
|------------------------------------------------------------------------------------------------------------|-------------------|--------------------|
46-
| [X500DistinguishedName validation is stricter](cryptography/10.0/x500distinguishedname-validation.md) | Behavioral change | Preview 1 |
47-
| [Environment variable renamed to DOTNET_OPENSSL_VERSION_OVERRIDE](cryptography/10.0/version-override.md) | Behavioral change | Preview 1 |
44+
| Title | Type of change | Introduced version |
45+
|----------------------------------------------------------------------------------------------------------|---------------------------------------|--------------------|
46+
| [X500DistinguishedName validation is stricter](cryptography/10.0/x500distinguishedname-validation.md) | Behavioral change | Preview 1 |
47+
| [X509Certificate and PublicKey key parameters can be null](cryptography/10.0/x509-publickey-null.md) | Behavioral/source incompatible change | Preview 3 |
48+
| [Environment variable renamed to DOTNET_OPENSSL_VERSION_OVERRIDE](cryptography/10.0/version-override.md) | Behavioral change | Preview 1 |
4849

4950
## SDK
5051

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: "Breaking change - X509Certificate and PublicKey key parameters can be null"
3+
description: "Learn about the breaking change in .NET 10 Preview 3 where key parameters in X509Certificate and PublicKey can be null."
4+
ms.date: 3/13/2025
5+
ai-usage: ai-assisted
6+
ms.custom: https://github.com/dotnet/docs/issues/45325
7+
---
8+
9+
# X509Certificate and PublicKey key parameters can be null
10+
11+
In .NET 10, the behavior of <xref:System.Security.Cryptography.X509Certificates.X509Certificate> and <xref:System.Security.Cryptography.X509Certificates.PublicKey> has changed. When these objects contain a key without algorithm parameters, they now return `null` instead of an empty array.
12+
13+
## Version introduced
14+
15+
.NET 10 Preview 3
16+
17+
## Previous behavior
18+
19+
<xref:System.Security.Cryptography.X509Certificates.X509Certificate> or <xref:System.Security.Cryptography.X509Certificates.PublicKey> objects that contained a key without algorithm parameters would return an empty array when accessing the key algorithm parameters.
20+
21+
```csharp
22+
byte[] parameters = certificate.GetKeyAlgorithmParameters();
23+
// parameters would be an empty array if no algorithm parameters were present
24+
```
25+
26+
## New behavior
27+
28+
<xref:System.Security.Cryptography.X509Certificates.X509Certificate> or <xref:System.Security.Cryptography.X509Certificates.PublicKey> objects that contain a key without algorithm parameters will return `null` when accessing the key algorithm parameters.
29+
30+
```csharp
31+
byte[] parameters = certificate.GetKeyAlgorithmParameters();
32+
// parameters will be null if no algorithm parameters are present
33+
```
34+
35+
## Type of breaking change
36+
37+
This is both a [behavioral](../../categories.md#behavioral-change) and [source compatibility](../../categories.md#source-compatibility) change.
38+
39+
## Reason for change
40+
41+
The <xref:System.Security.Cryptography.X509Certificates.X509Certificate>, <xref:System.Security.Cryptography.X509Certificates.X509Certificate2>, and <xref:System.Security.Cryptography.X509Certificates.PublicKey> classes expose information about the *Subject Public Key Info*. One of the properties of the *Subject Public Key Info* is the parameters for the algorithm. A *Subject Public Key Info* is not required to contain algorithm parameters. Previously, this was represented as an empty byte array, which is not valid ASN.1. Attempting to encode or decode it would result in an exception. To more clearly represent absent key parameters, `null` is now returned, and the members that return algorithm parameters have been annotated to return nullable values.
42+
43+
## Recommended action
44+
45+
When accessing a member that returns information about a subject public key info's algorithm parameters, expect the member to possibly return `null` and handle the `null` value accordingly.
46+
47+
```csharp
48+
byte[] parameters = certificate.GetKeyAlgorithmParameters();
49+
if (parameters == null)
50+
{
51+
// Handle the absence of algorithm parameters
52+
}
53+
```
54+
55+
## Affected APIs
56+
57+
- <xref:System.Security.Cryptography.X509Certificates.X509Certificate.GetKeyAlgorithmParameters?displayProperty=fullName>
58+
- <xref:System.Security.Cryptography.X509Certificates.X509Certificate.GetKeyAlgorithmParametersString?displayProperty=fullName>
59+
- <xref:System.Security.Cryptography.X509Certificates.PublicKey.%23ctor(System.Security.Cryptography.Oid,System.Security.Cryptography.AsnEncodedData,System.Security.Cryptography.AsnEncodedData)?displayProperty=fullName>
60+
- <xref:System.Security.Cryptography.X509Certificates.PublicKey.EncodedParameters?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ items:
3232
items:
3333
- name: X500DistinguishedName validation is stricter
3434
href: cryptography/10.0/x500distinguishedname-validation.md
35+
- name: X509Certificate and PublicKey key parameters can be null
36+
href: cryptography/10.0/x509-publickey-null.md
3537
- name: Environment variable renamed to DOTNET_OPENSSL_VERSION_OVERRIDE
3638
href: cryptography/10.0/version-override.md
3739
- name: Globalization
@@ -1618,6 +1620,8 @@ items:
16181620
items:
16191621
- name: X500DistinguishedName validation is stricter
16201622
href: cryptography/10.0/x500distinguishedname-validation.md
1623+
- name: X509Certificate and PublicKey key parameters can be null
1624+
href: cryptography/10.0/x509-publickey-null.md
16211625
- name: Environment variable renamed to DOTNET_OPENSSL_VERSION_OVERRIDE
16221626
href: cryptography/10.0/version-override.md
16231627
- name: .NET 9

0 commit comments

Comments
 (0)