【发布时间】:2018-10-06 11:31:16
【问题描述】:
我读到客户端身份验证是可选的,所以我试图连接到我的 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,我该怎么做?
这就是我生成证书文件和密钥文件的方式
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 的调用是可选的?
【问题讨论】:
标签: c# python ssl ssl-certificate