浏览器会警告弱签名算法,但不会通过 C# 发出警告?
Weak signature algorithm is warned by browser but not via C#?
我们使用第三方服务 - 当通过浏览器访问时 - 它会产生此错误:
好的 - 我们应该给他们打电话,可能会告诉他们自己解决这个问题。
但是 -
问题:
查看这个简单的 C# 代码 - 为什么我没有看到关于此警告的任何异常,或者换句话说 - 如何让 C# 反映此警告或不安全访问?
NB 我已经知道我可以使用更高级的 webrequest class using other class - 但这个问题并不重要。 (恕我直言).
void Main()
{
Console.WriteLine(CreatePost("https://------", "dummy")); // No exception/warning here
}
private string CreatePost(string uri, string data)
{
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
byte[] postBytes = Encoding.GetEncoding("UTF-8").GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")).ReadToEnd();
}
此外 - 我知道浏览器 url 地址正在使用 GET(与 C# post 动词不同) - 但我认为他们没有将此操作重定向到静音警告)
通过 C# 访问它时您没有看到任何警告,因为 Google Chrome 正在检查 SSL 的设置方式并将警告放在试图保护您的方式中(并且所述服务的用户)。当您从 C# 访问它时,它永远不会触及 Chrome,因此您不会收到警告。
您会在其他一些浏览器中收到类似的警告,但这不是对您发出的请求的响应的一部分 - 只是浏览器试图保护您的安全。
您可以手动检查代码中的签名算法,如果不是您认为的则抛出异常"secure"。
编辑:您可以通过向 ServicePointManager
添加自定义验证回调来检查签名算法,如下所示:
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
(sender, certificate, chain, errors) => {
var insecureAlgorithms = new List<String> { "SHA1" };
var sslCertificate = (X509Certificate2) certificate;
var signingAlgorithm = sslCertificate.SignatureAlgorithm;
if (insecureAlgorithms.Contains(signingAlgorithm.FriendlyName))
{
return false;
}
// do some other checks here...
return true;
}
);
我们使用第三方服务 - 当通过浏览器访问时 - 它会产生此错误:
好的 - 我们应该给他们打电话,可能会告诉他们自己解决这个问题。
但是 -
问题:
查看这个简单的 C# 代码 - 为什么我没有看到关于此警告的任何异常,或者换句话说 - 如何让 C# 反映此警告或不安全访问?
NB 我已经知道我可以使用更高级的 webrequest class using other class - 但这个问题并不重要。 (恕我直言).
void Main()
{
Console.WriteLine(CreatePost("https://------", "dummy")); // No exception/warning here
}
private string CreatePost(string uri, string data)
{
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
byte[] postBytes = Encoding.GetEncoding("UTF-8").GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")).ReadToEnd();
}
此外 - 我知道浏览器 url 地址正在使用 GET(与 C# post 动词不同) - 但我认为他们没有将此操作重定向到静音警告)
通过 C# 访问它时您没有看到任何警告,因为 Google Chrome 正在检查 SSL 的设置方式并将警告放在试图保护您的方式中(并且所述服务的用户)。当您从 C# 访问它时,它永远不会触及 Chrome,因此您不会收到警告。
您会在其他一些浏览器中收到类似的警告,但这不是对您发出的请求的响应的一部分 - 只是浏览器试图保护您的安全。
您可以手动检查代码中的签名算法,如果不是您认为的则抛出异常"secure"。
编辑:您可以通过向 ServicePointManager
添加自定义验证回调来检查签名算法,如下所示:
ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(
(sender, certificate, chain, errors) => {
var insecureAlgorithms = new List<String> { "SHA1" };
var sslCertificate = (X509Certificate2) certificate;
var signingAlgorithm = sslCertificate.SignatureAlgorithm;
if (insecureAlgorithms.Contains(signingAlgorithm.FriendlyName))
{
return false;
}
// do some other checks here...
return true;
}
);