python - 如何使用 Python 创建 SSL 套接字服务器

标签 python sockets ssl websocket

我如何使用 Python 创建 SSL 套接字服务器?
我从一个站点获取了 Ssl 文件,我想在另一台服务器上使用它们。我可以这样做吗?
我的 SSL 文件:
ca.pem : 证书颁发机构包
证书.crt : 证书
key.key : 私钥
我的服务器 Python 代码:

from socket import *
import hashlib,base64,ssl,datetime

Socket = socket(2,1)
Socket.bind((IP,Port))
Socket.listen()

print("Service Started on "+IP+":"+str(Port))

while True:
    Connection,Address = Socket.accept()
    Secure_Connection = ssl.wrap_socket(Connection, server_side=True, ca_certs="ca.pem", certfile="cert.crt", keyfile="key.key", cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1_2);
    
    # Get certificate from the client
    Client_Cert = Secure_Connection.getpeercert();
    
    CLT_Subject    = dict(item[0] for item in Client_Cert['subject']);
    CLT_Common_Name = CLT_Subject['commonName'];

    # Check the client certificate bears the expected name as per server's policy
    if not Client_Cert:
        raise Exception("Unable to get the certificate from the client");
        
    if CLT_Common_Name != 'DemoClt':
        raise Exception("Incorrect common name in client certificate");

    # Check time validity of the client certificate
    T1  = ssl.cert_time_to_seconds(Client_Cert['notBefore']);
    T2  = ssl.cert_time_to_seconds(Client_Cert['notAfter']);
    TS  = time.time();

    if TS < T1:
        raise Exception("Client certificate not yet active");
        
    if TS > T2:
        raise Exception("Expired client certificate");

    # Send current server time to the client
    Time = "%s"%datetime.datetime.now();
    Secure_Connection.send(Time.encode());
    print("Securely sent %s to %s"%(Time,Address[0]));

    # Close the connection to the client
    Secure_Connection.close();
但我得到这个 SSL 错误:
Secure_Connection = ssl.wrap_socket(Connection, server_side=True, ca_certs="ca.pem", certfile="cert.crt", keyfile="key.key", cert_reqs=ssl.CERT_REQUIRED, ssl_version=ssl.PROTOCOL_TLSv1_2);
File "C:\Program Files\Python39\lib\ssl.py", line 1405, in wrap_socket
return context.wrap_socket(
File "C:\Program Files\Python39\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Program Files\Python39\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Program Files\Python39\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN] sslv3 alert certificate unknown (_ssl.c:1129)
此错误的原因是什么?

最佳答案

该模块基于传输层安全性(通常称为安全套接字层)为网络套接字提供加密和对​​等身份验证功能。 OpenSSL 用于实现该模块。假设平台上安装了 OpenSSL,几乎所有现代 Unix 系统、Windows、macOS 和可能的其他平台。
请注意:
由于使用操作系统套接字 API,某些行为可能与平台相关。安装不同版本的 OpenSSL 也可能会改变行为。一个很好的例子是带有 OpenSSL 版本 1.1.1 的 TLSv1.3。
笔记:
该模块在使用前需要仔细阅读。默认 SSL 模块设置可能会给人以错误的安全印象,因为您的应用程序可能不适合这些设置。
它在本节中解释了 SSL 模块中包含的对象和功能。
SSLSocket 是从 socket.socket 派生的,它提供了一个类似套接字的包装器,可以对套接字内的数据进行 SSL 加密和解密。使用 getpeercert() 从连接的另一端获取对等证书,或者使用 cipher() 方法获取用于安全连接的密码。
通过 SSLContext 的 wrap_socket() 方法,复杂的应用程序可以继承 SSL 设置和证书。 SSL.SSLContext 是用于管理 SSL 设置和证书的类之一。
在 3.5.3 版中改进了与 OpenSSL 1.1.0 的链接
OpenSSL 版本 0.9.8、1.0.0 和 1.0.1 已被弃用。将来需要至少安装 OpenSSL 1.0.2 或 1.1.0 才能使 SSL 工作。
3.10 版更新:已实现 PEP 644。 SSL 模块需要 1.1.1 的 OpenSSL 版本。
对于已弃用的常量和函数,存在弃用警告。

关于python - 如何使用 Python 创建 SSL 套接字服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69836044/

相关文章:

python - 快速删除列表中的连续重复项和另一个列表中的相应项目

python - 在 CentOS 中安装 python 2.6

Python 列表理解、解包和多重操作

Python Windows 原始套接字无效绑定(bind)参数

android - Spring RestTemplate I/O 错误 : No peer certificate

python - container.__iter__() 和 iterator.__iter__() 有什么区别?

c - 套接字:连接被拒绝

sockets - 如何使用sockJS在onopen中获取参数

java - Android 应用程序中 keystore 中的空别名列表

java - SMTP 和 startssl jenkins 异常