-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add name of corrupted certificate to CryptographicException on Mac #54477
Conversation
When trying to create a CertificateData out of raw X509 byte array it might throw if the data is corrupted. The existing exception message does not provide any information which might help the user identify the corrupted certificate and fix it. This change, makes a native call to SecCertificateCopySubjectSummary which will provide a human readable summary of the certificate, and will generate an exception message using this string. fix dotnet#54336
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq, @GrabYourPitchforks Issue DetailsWhen trying to create a CertificateData out of raw X509 byte array it might throw if the data is corrupted. The existing exception message does not provide any information which might help the user identify the corrupted certificate and fix it. This change, makes a native call to SecCertificateCopySubjectSummary which will provide a human readable summary of the certificate, and will generate an exception message using this string. fix #54336
|
Couldn't find a way to automate a test for this, but performed a manual test by following @vcsjones advice and creating a Test keychain, and importing the following corrupted key into it:
Then the following c# code: X509Store store = new X509Store("Test");
store.Open(OpenFlags.ReadOnly);
try
{
foreach (var cert in store.Certificates)
{
Console.WriteLine(cert.Subject);
}
}
catch (Exception r)
{
Console.WriteLine(r);
} Will produce the following result:
Note that the extra error with the actual certificate data is generated only in Debug mode. |
src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.X509.cs
Outdated
Show resolved
Hide resolved
...urity.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs
Outdated
Show resolved
Hide resolved
...urity.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs
Outdated
Show resolved
Hide resolved
…c/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
…c/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs Co-authored-by: Jeremy Barton <jbarton@microsoft.com>
In addition to the failing tests for trailing white space, I think runtime/src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/entrypoints.c Line 105 in 0049c62
Though I am not clear why there wasn't a build failure for that. There is (or, was if there was not) a build check for this. |
This file must be relatively new. Because initially I made the change against the net5.0 tag and I searched for AppleCryptoNative_X509GetRawData everywhere. Also definitely weird that it built for me when I switched to 6. Thanks, I will fix it |
/backport to release/5.0 |
Started backporting to release/5.0: https://github.com/dotnet/runtime/actions/runs/964761518 |
@bartonjs backporting to release/5.0 failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Add name of corrupted certificate to CryptographicException on Mac
Using index info to reconstruct a base tree...
M src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.X509.cs
M src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.c
M src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.h
M src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs
M src/libraries/System.Security.Cryptography.X509Certificates/src/Resources/Strings.resx
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Security.Cryptography.X509Certificates/src/Resources/Strings.resx
Auto-merging src/libraries/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.OSX/AppleCertificatePal.cs
Auto-merging src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.h
CONFLICT (content): Merge conflict in src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.h
Auto-merging src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.c
CONFLICT (content): Merge conflict in src/libraries/Native/Unix/System.Security.Cryptography.Native.Apple/pal_x509.c
Auto-merging src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.X509.cs
CONFLICT (content): Merge conflict in src/libraries/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.X509.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Add name of corrupted certificate to CryptographicException on Mac
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128 Please backport manually! |
When trying to create a CertificateData out of raw X509 byte array it might throw if the data is corrupted. The existing exception message does not provide any information which might help the user identify the corrupted certificate and fix it.
This change, makes a native call to SecCertificateCopySubjectSummary which will provide a human readable summary of the certificate, and will generate an exception message using this string.
fix #54336