【问题标题】:Self-signed certificate used when wrapping sockets包装套接字时使用的自签名证书
【发布时间】:2017-11-16 20:49:11
【问题描述】:

我正在尝试在 ssl 握手中使用使用加密模块生成的自签名 x509 证书。我正在按照the documentation 中的规定生成 PEM 文件的证书和密钥,并使用以下函数将它们写入文件:

def write_key_and_cert(self, certname="cert.pem", keyname="key.pem"):
    with open(certname, "wb") as f:
        f.write(self.cert.public_bytes(serialization.Encoding.PEM))
    with open(keyname, "wb") as f:

    f.write(self.private_key.private_bytes(encoding=serialization.Encoding.PEM,
                                           format=serialization.PrivateFormat.TraditionalOpenSSL,
                                           encryption_algorithm=serialization.BestAvailableEncryption(b"passphrase"),),)

问题是在封装socket的过程中,服务器无法使用certfile和keyfile,导致挂起。我相信这是由于密钥文件被加密(ssl 包装没有解密密钥文件)。有没有办法使用加密模块生成的证书文件/密钥文件,如果有,如何使用?

【问题讨论】:

    标签: python python-3.x python-cryptography


    【解决方案1】:

    通过在加载证书链时创建上下文并指定密码解决了这个问题:

    context = ssl.create_default_context()
    context.load_cert_chain(certfile=self.certfile, keyfile=self.keyfile, password=b"passphrase")
    

    这让 ssl 模块可以对密钥文件进行解密并正确加载。

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 2017-12-11
      • 1970-01-01
      • 2021-10-22
      • 2011-09-22
      • 2017-05-10
      • 2012-12-26
      • 1970-01-01
      相关资源
      最近更新 更多