-
Notifications
You must be signed in to change notification settings - Fork 694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use obsolete X509Certificate2 constructor on net9.0 #5911
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,9 @@ internal static class CertificateProvider | |
|
||
private const int MACOS_INVALID_CERT = -25257; | ||
|
||
#if NET9_0_OR_GREATER | ||
private const int CRYPT_E_BAD_DECODE = unchecked((int)0x80092002); | ||
#endif | ||
|
||
#if IS_SIGNING_SUPPORTED && IS_CORECLR | ||
//Generic exception ASN1 corrupted data | ||
|
@@ -83,6 +86,9 @@ public static async Task<X509Certificate2Collection> GetCertificatesAsync(Certif | |
options.CertificatePath))); | ||
|
||
case CRYPT_E_NO_MATCH_HRESULT: | ||
#if NET9_0_OR_GREATER | ||
case CRYPT_E_BAD_DECODE: | ||
#endif | ||
#if IS_SIGNING_SUPPORTED && IS_CORECLR | ||
case OPENSSL_ASN1_CORRUPTED_DATA_ERROR: | ||
#else | ||
|
@@ -122,7 +128,12 @@ private static async Task<X509Certificate2> LoadCertificateFromFileAsync(Certifi | |
|
||
if (!string.IsNullOrEmpty(options.CertificatePassword)) | ||
{ | ||
cert = new X509Certificate2(options.CertificatePath, options.CertificatePassword); // use the password if the user provided it. | ||
// use the password if the user provided it | ||
#if NET9_0_OR_GREATER | ||
cert = X509CertificateLoader.LoadPkcs12FromFile(options.CertificatePath, options.CertificatePassword); | ||
#else | ||
cert = new X509Certificate2(options.CertificatePath, options.CertificatePassword); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The preferred fix is to add a package reference to Microsoft.Bcl.Cryptography and always call X509CertificateLoader; but we don't have the power to mark things Obsolete in older TFMs. If adding the package is unacceptable, then this works. The main concern is "don't get surprised by a PFX", but this expects a PFX. There are also some subtle differences in how PFX loading works on Windows between the two, mainly fixing issues with loading very old files or loading the same file in parallel (making Microsoft.Bcl.Cryptography/X509CertificateLoader the better choice) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is For this reason, plus the fact that NuGet doesn't use the Arcade SDK, means I don't think NuGet can use this package for .NET 9.0.100 SDK. Maybe after .NET 9 goes GA. For now, I think we need to take this conditional compilation change. |
||
#endif | ||
} | ||
else | ||
{ | ||
|
@@ -147,8 +158,12 @@ private static async Task<X509Certificate2> LoadCertificateFromFileAsync(Certifi | |
throw; | ||
} | ||
} | ||
#else | ||
#if NET9_0_OR_GREATER | ||
cert = X509CertificateLoader.LoadPkcs12FromFile(options.CertificatePath, null); | ||
#else | ||
cert = new X509Certificate2(options.CertificatePath); | ||
#endif | ||
#endif | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bartonjs @vcsjones FYI this was uncovered by the ExecuteCommandAsync_WithEmptyPkcs7File_RaisesErrorsOnceAsync test, X509CertificateLoader returns a different hresult in this case on !Windows because we're now returning this from managed code.
Might be something we should call out in the breaking change doc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HResult values from exceptions aren't guaranteed (especially when they can change because of an OS change); so I don't anticipate putting it in the breaking change documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd agree with you in an ideal world, but this real world code makes me think we should at least give a heads up in the doc :)