我有一个用 ssl 证书标识自己的服务器。证书是自签名的。
如果用户尝试替换服务器并将“假”证书添加到操作系统信任的证书列表中,我想确保该软件不会发送数据。
到目前为止,这是我想出的:
ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)
{
bool result = false;
if (sender is HttpWebRequest)
{
var wr = sender as HttpWebRequest;
if (wr.Address.Host == "<my host>")
{
if (cert == null)
{
return false;
}
if (cert.GetCertHashString() == "<certificate hash string>")
{
return true;
}
return false;
}
}
return result;
}
我理解代码的方式,它将信任我的证书my host
,但它不会受到用户主机替换的保护。
我希望该软件my host
仅在使用 证书标识自己时才接受<certificate hash string>
,并且如果my host
提供其他证书,则必须拒绝连接。
我怎么做?