SSL 证书名称不匹配
我正在致力于制作一个 TCP SSL 服务器,仅使用 .NET 技术。
首先,使用此处,创建了客户端/服务器代码,接下来,创建我使用的证书 this (除了注册网络服务的部分......不起作用)
与 SSL 服务器的启动通信正在工作,但因为我需要验证客户端/服务器证书文件,我
public static bool ValidateServerCertificate(
object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
// Do not allow this client to communicate with unauthenticated servers.
Console.WriteLine(" > Remote Certificate Error : {0}", sslPolicyErrors);
return false;
}
添加了这个:和这个:
public static X509Certificate ValidateClientCertificate(
object sender,
string targetHost,
X509CertificateCollection localCertificates,
X509Certificate remoteCertificate,
string[] acceptableIssuers)
{
Console.WriteLine(" > Client is selecting a local certificate.");
if (acceptableIssuers != null &&
acceptableIssuers.Length > 0 &&
localCertificates != null &&
localCertificates.Count > 0)
{
// Use the first certificate that is from an acceptable issuer.
foreach (X509Certificate certificate in localCertificates)
{
string issuer = certificate.Issuer;
if (Array.IndexOf(acceptableIssuers, issuer) != -1)
return certificate;
}
}
if (localCertificates != null &&
localCertificates.Count > 0)
return localCertificates[0];
return null;
}
在服务器和客户端上
。以这种方式创建连接:
SslStream sslStream = new SslStream(
client.GetStream(),
true,
new RemoteCertificateValidationCallback(ValidateServerCertificate),
new LocalCertificateSelectionCallback(ValidateClientCertificate)
);
// The server name must match the name on the server certificate.
try
{
sslStream.AuthenticateAsClient(serverName, CertColection, SslProtocols.Tls, false);
}...(etc)...
客户端
SslStream flujo = new SslStream(tcpClient.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), new LocalCertificateSelectionCallback(ValidateClientCertificate));
//Código anterior: NetworkStream flujo = tcpClient.GetStream();
//Continúa, intentar autentificar la conexión.
Console.WriteLine(" > Autentificando");
try
{
flujo.AuthenticateAsServer(certificadoServidor, false, SslProtocols.Tls, true);
}...(etc)...
说:“RemoteCertificateNameMismatch” 服务器说:“RemoteCertificateNotAvailable”
在其各自的 sslPolicyErrors 信息上。
我做错了什么? 我错过了哪一步?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论