- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 838
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
Improve the exception message in case of certificate name mismatch #1554
Improve the exception message in case of certificate name mismatch #1554
Conversation
c0db4c8
to
610fcd4
Compare
Before this commit, the message was: > An error occurred while attempting to establish an SSL or TLS connection. > > The host name did not match the name given in the server's SSL certificate. After this commit, the message becomes: > An error occurred while attempting to establish an SSL or TLS connection. > > The host name (smtp.contoso.com) did not match any of the names given in the server's SSL certificate: > • *.mail.eo.outlook.com > • *.mail.protection.outlook.com > • *.mail.protection.outlook.de > • *.olc.protection.outlook.com > • *.pamx1.hotmail.com > • mail.messaging.microsoft.com > • mail.protection.outlook.com > • outlook.com Providing all the certificate subject alternative names directly in the error message will greatly help diagnosing certificate name mismatch issues. It would have helped me to understand that `smtp.contoso.com` is an alias for `contoso-com.mail.protection.outlook.com` and that using the latter instead of the former solves the connection issue.
610fcd4
to
7dc6a53
Compare
I like the idea, but I'm a bit uncomfortable with copying code from StackOverflow since code on StackOverflow is not licensed and that could end up being a potential legal problem. The other problem is that the Asn1 APIs are only available for >= NET 5.0 which means that this patch will break the build for .NET Framework 4.x. I'll look at ways of accomplishing the same thing but I can't use this patch as-is. |
The Asn1 APIs are available through the System.Formats.Asn1 NuGet package for .NET Framework 4.x. I have added the package reference in the csproj: <ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Formats.Asn1" Version="7.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup> About the licensing of StackOverflow content, it seems it's available under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) but I am not a lawyer. I used it to understand how to use the AsnReader class because the official documentation lacks any example. 🙄 Also, @skrysmanski, the author of the sample AsnReader code is also on GitHub so maybe he could give his consent. Bug again, I am not a lawyer. |
Instead of linking to my StackOverflow answer, you could link to this file (which is basically the same code): This file is licensed under MIT license which should be easier to use. Also, I don't have a problem with you using this code :) |
@skrysmanski Thanks for chiming in and licensing this code under MIT (same as MimeKit/MailKit)! |
Thanks to Sebastian Krysmanski for the code to extract the alternative DNS names from an SSL certificate and to Cédric Luthi for the original patch & idea. Fixes PR #1554
I modified the patch a bit, mostly for formatting and to give attribution to Sebastian and the link he provided to use instead of the StackOverflow link. Thanks again for the patch, this was a really nice idea to help improve the SslHandshakeException diagnostics message! |
Awesome, it will be much easier to diagnose future connection errors next year when I won't remember to use the non-alias smtp host. 😁 I had picked version 7.0.0 of |
Don't worry, neither do I. I went with 6.0.0 because that's the version I'm using for everything else. I'm not sure if I should target 7.0.0 or not. I've been assuming 6.0.0 coincides with net6.0 but I'm not sure. |
Before this commit, the message was:
After this commit, the message becomes:
Providing all the certificate subject alternative names directly in the error message will greatly help diagnosing certificate name mismatch issues.
It would have helped me to understand that
smtp.contoso.com
is an alias forcontoso-com.mail.protection.outlook.com
and that using the latter instead of the former solves the connection issue.