|
| 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> |
0 commit comments