在 HTTPS 请求期间验证远程 SSL 证书

发布于 2024-09-06 21:09:15 字数 915 浏览 0 评论 0原文

当向远程 Web 服务器发出 HTTPS 请求时,我使用 WebRequest,它与远程 Web 服务器建立安全连接。在开发过程中,我在服务器上使用自签名证书,并且 WebRequest 无法建立安全连接,因为证书无效,这是预期的行为。

我发现此代码“远程”证书检查,通过调用以下代码中的 SetCertificatePolicy() 方法激活。

public static void SetCertificatePolicy()
{
    ServicePointManager.ServerCertificateValidationCallback
                += RemoteCertificateValidate;
}

/// <summary>
/// Remotes the certificate validate.
/// </summary>
private static bool RemoteCertificateValidate(
    object sender, X509Certificate cert,
    X509Chain chain, SslPolicyErrors error)
{
    // trust any certificate!!!
    System.Console.WriteLine("Warning, trust any certificate");
    return true;
}

我想知道是否可以对远程 SSL 证书进行特殊检查(例如,使用上面的代码),以便我可以验证远程 Web 服务器是否使用有效的 SSL 证书,而不仅仅是任何有效的证书,而是确切的证书我想?例如,我想确保我正在与 www.someplace.com 网站交谈,证书颁发给 ACME Inc,指纹为 00:11:22:.....

对此的“最佳实践”方法是什么设想?

谢谢!

When making HTTPS request to remote web server, I use WebRequest, which establishes secure connection with remote web server. During development, I use self-signed cert on server, and WebRequest fails to establish secure connection, since cert is not valid, which is expected behavior.

I have found this code that "remotes" cert check, activated by calling SetCertificatePolicy() method in following code.

public static void SetCertificatePolicy()
{
    ServicePointManager.ServerCertificateValidationCallback
                += RemoteCertificateValidate;
}

/// <summary>
/// Remotes the certificate validate.
/// </summary>
private static bool RemoteCertificateValidate(
    object sender, X509Certificate cert,
    X509Chain chain, SslPolicyErrors error)
{
    // trust any certificate!!!
    System.Console.WriteLine("Warning, trust any certificate");
    return true;
}

I am wondering, if it is possible to do special checks on remote SSL cert (using above code, for instance), so that I can verify that remote web server uses valid SSL cert, and not just any valid cert, but exactly the one I want? For instance, I want to make sure that I'm talking to www.someplace.com website, cert issued to ACME Inc, with fingerprint 00:11:22:.....

What is a "best practice" approach for this scenario?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2024-09-13 21:09:15

如果您确实想将其确定为一个特定证书,则可以将证书数据(DER 格式)与 certificate.GetRawCertData() 中的 byte[] 进行比较。

您还可以在 RemoteCertificateValidate 中的 certificate 参数上使用 GetCertHashString()Subject 来获取您要获取的信息。后。主机名应位于证书的使用者备用名称中,或者如果没有使用者备用名称,则应位于使用者(可分辨)名称的 CN 中。考虑到 .NET 格式化主题字符串的方式,这应该是您在那里找到的第一个 CN=。

如果 certificateX509Certificate2 的实例,您还将获得更多数据。然后,您将能够获取 SubjectName 作为 X500PrincipalName 以及 Extensions(以检查主题备用名称扩展)。使用 BouncyCastle 等工具来解析主题名称可能会很有用。

您还可能会获得有关您尝试在发送方中联系的主机名的更多信息,具体取决于其运行时类型。

If you really want to nail it down to one particular certificate, you can compare the certificate data (in DER format) to the byte[] in certificate.GetRawCertData().

You can also use GetCertHashString() and Subject on the certificate parameter in RemoteCertificateValidate to get the information you're after. The hostname ought to be in the subject alternative name of the certificate or, if there isn't a subject alternative name, in the CN of the subject (distinguished) name. Considering the way .NET formats the subject string, this ought to be the first CN= you find there.

You'll also get more data if certificate is an instance of X509Certificate2. You'll then be able to get SubjectName as an X500PrincipalName and also the Extensions (to check the subject alternative name extension). It might be useful to use tools such as BouncyCastle for parsing the subject name.

You're also likely to get more information about the hostname you're trying to contact in the sender, depending on its runtime type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文