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

2024-04-26 00:16:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我读到客户端身份验证是可选的,所以我尝试连接到我的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):

^{pr2}$

客户(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,我该怎么做呢?在

这就是我生成certfile和keyfile的方法

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的调用是可选的?在


Tags: 代码inpy服务器client身份验证客户端ssl
1条回答
网友
1楼 · 发布于 2024-04-26 00:16:05

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服务器会正确地抱怨协议冲突。在

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

^{pr2}$

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

相关问题 更多 >