gpt4 book ai didi

c# - SSL 连接在没有客户端身份验证的情况下失败

转载 作者:太空宇宙 更新时间:2023-11-03 13:21:40 25 4
gpt4 key购买 nike

我读到客户端身份验证是可选的,因此我尝试连接到我的 SSL 服务器而不在客户端进行身份验证。当我这样做时,我在服务器上收到以下错误:

, in accept_connection
ssl_version=ssl.PROTOCOL_SSLv23
File "/usr/lib/python2.7/ssl.py", line 933, in wrap_socket
ciphers=ciphers)
File "/usr/lib/python2.7/ssl.py", line 601, in __init__
self.do_handshake()
File "/usr/lib/python2.7/ssl.py", line 830, in do_handshake
self._sslobj.do_handshake()
SSLEOFError: EOF occurred in violation of protocol (_ssl.c:590)

这是工作的服务器和客户端代码,当我在客户端进行身份验证时一切正常,但是当我注释掉该行时出现错误。

服务器( python ):

def accept_connection(self, sock):
client, (addr, port) = sock.accept()
sslclient = ssl.wrap_socket(
client,
server_side=True,
certfile=self.ssl_cert_file,
keyfile=self.ssl_key_file,
ssl_version=ssl.PROTOCOL_SSLv23
)

客户端(C#):

public bool Connect()
{
try
{
client = new TcpClient(this.ServerAddress, this.ServerPort);

sslStream = new SslStream(
client.GetStream(),
false,
new RemoteCertificateValidationCallback(ValidateCert),
null
);

try
{
sslStream.AuthenticateAsClient(this.ServerAddress);
}
catch (AuthenticationException e)
{
sslStream = null;
client.Close();
client = null;
return false;
}
}
catch (Exception e)
{
return false;
}

return true;
}

以上是没有错误的工作代码。

当我在客户端注释掉下面的代码时:

//sslStream.AuthenticateAsClient(this.ServerAddress);

上面提到的 python 错误出现了,客户端连接没有抛出任何异常并继续运行直到第一次读取,然后失败:

This operation is only allowed using a successfully authenticated context.

如果我不调用 AuthenticateAsClient,我该如何进行这项工作?

这就是我生成证书文件和 key 文件的方式

openssl req -x509 -newkey rsa:1024 -keyout my.key -out my.crt -days 365 -nodes -subj "/C=US/ST=VA/L=Junk/O=None/OU=Junk/CN=example.local"

Python 是第 2 版。

也许我刚刚被误导为调用 AuthenticateAsClient 是可选的?

最佳答案

Ssl 客户端证书 身份验证确实是可选的。这是客户端将其证书发送到服务器时,服务器验证此证书有效,是服务器所期望的,并使用该证书对客户端进行身份验证。

然而,这不是由

执行的
sslStream.AuthenticateAsClient(this.ServerAddress);

如文档所述,此方法是

Called by clients to authenticate the server and optionally the client in a client-server connection.

验证服务器(验证其证书是否有效,是否为预期的域颁发,是否由受信任的机构颁发)是 ssl 握手的必需步骤。例如,使用 this link 的描述我们看到第 3 步是:

The SSL or TLS client verifies the server's digital certificate

服务器等待客户端此步骤的结果以继续。出于这个原因,如果您通过注释对 AuthenticateAsClient 的调用来跳过此步骤 - 您的 python 服务器会正​​确地提示违反协议(protocol)。

要使用客户端身份验证,您将使用另一个重载:

X509CertificateCollection clientCerts = GetClientCerts();
sslStream.AuthenticateAsClient(this.ServerAddress, clientCerts, true);

因为您没有这样做 - 您没有执行(可选的)客户端身份验证。

关于c# - SSL 连接在没有客户端身份验证的情况下失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com