【问题标题】:Certificate Generation for Python Twisted SSLPython Twisted SSL 的证书生成
【发布时间】:2019-06-14 18:03:00
【问题描述】:

我试图弄清楚如何使用 Python 库 Twisted 设置 SSL 链接。我已经设法创建了一个在服务器端工作的证书,但是当涉及到客户端时我完全被卡住了。

来自扭曲的website 的例子说明:

以下示例依赖于文件 server.pem(私钥和 自签名证书一起)和public.pem(服务器的公共 证书本身)。

我已经使用 OpenSSL 为自己生成了一个证书和密钥:

# Generate Private Key:
openssl genrsa -des3 -out certs/server.key 2048

# Generate Certificate Signing Request:
openssl req -new -key certs/server.key -sha256 -out certs/server.csr

# Generate a Self-Signed Certificate:
openssl x509 -req -days 365 -in certs/server.csr -signkey certs/server.key -sha256 -out certs/server.crt

# Convert the CRT to PEM format:
openssl x509 -in certs/server.crt -out certs/server.pem -outform PEM

对于服务器端,我将 certs/server.crt 和 certs/server.key 结合起来创建 server.pem 并尝试将 server.crt 用于公共。

当我尝试使用以下命令运行我的测试程序时:

certificate = ssl.PrivateCertificate.loadPEM(certData)

我收到关于没有起跑线的错误。如果不是 server.crt,我应该为客户端使用哪个证书?

【问题讨论】:

    标签: python python-3.x ssl twisted pki


    【解决方案1】:

    当我尝试使用以下命令运行我的测试程序时:

    certificate = ssl.PrivateCertificate.loadPEM(certData)我收到一个错误 关于不起跑线。我应该使用哪个证书 客户端如果不是server.crt好吗?

    如果您查看Twisted howto page. 上的示例,这应该是ssl.Certificate.LoadPEM(certData)

    【讨论】:

      【解决方案2】:

      如果您还想对客户端进行基于证书的身份验证:

      我前段时间遇到过这个问题,并写了一封blog post 关于我的解决方案。 它还包含创建证书并使用自己的证书颁发机构对其进行签名的指南。你可以在GitHub找到python示例代码。

      它使用 Twisted 作为一个简单的 JSONRPCServer 为服务器和客户端提供基于证书的身份验证。

      主要是为客户端定义一个自己的AltCtxFactory:

      # Use our own context factory to use our certificate to authenticate
      # against the server and ensure that we are using a strong SSL/TLS
      # encryption method
      class AltCtxFactory(ssl.ClientContextFactory):
          def getContext(self):
              # Used TLS/SSL encryption method
              sslMethod = SSL.TLSv1_2_METHOD
              # Clients private Key, used for authentication
              privKey = "<PATH TO YOUR PRIVATE KEY>"
              # Clients certificate, used for authentication
              certificate = "<PATH TO YOUR CERTIFICATE>"
              # Our trusted Certificate Authority for server connections
              accepted_ca = "<PATH TO YOUR ACCEPTED CERTIFICATE AUTHORITY>"
      
              self.method = sslMethod
              ctx = ssl.ClientContextFactory.getContext(self)
              # Ensure that we verify server's certificate and use our own
              # verifyCallback method to get further details of invalid certificates
              ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,
                         verifyCallback)
              # Ensure that we only trust our CA
              ctx.load_verify_locations(accepted_ca)
              # Use our own Callback mehtod if a password is needed to decrypt our
              # private key
              ctx.set_passwd_cb(password_cb)
              # Use our certificate for authentication against server
              ctx.use_certificate_file(certificate)
              # Use our private key for authentication against server
              ctx.use_privatekey_file(privKey)
              return ctx
      

      请随意在您的项目中使用代码。

      【讨论】:

      • 您应该提供代码(或至少是其中的一部分)以及答案,而不仅仅是链接。如果链接失效,您的答案将变得毫无价值,这就是为什么粘贴带有答案的代码被认为是好的做法。不过不错的博文。
      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 2020-01-26
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      相关资源
      最近更新 更多