用python创建及使用一个tls,Twisted Python中的TLS-如何创建server.pem?

I've just discovered Twisted in Python, and have been looking at the echoserv_ssl.py sample as found here: https://twistedmatrix.com/documents/14.0.0/core/howto/ssl.html. Just above the sample code, the page quotes "The following examples rely on the files server.pem (private key and self-signed certificate together) and public.pem (the server’s public certificate by itself)."

So being something of a novice when it comes to SSL certs and keys, I did some research, and found http://www.thegeekscope.com/how-to-generate-self-signed-ssl-certificate-in-linux/

I followed the instructions there, and pointed my echoserv_ssl.py at the created file (step 3). However, that, together with any of the cert files that we have locally on our system seem to produce the same error:

# python echoserv_ssl.py

2014-09-17 15:12:54+0000 [-] Log opened.

2014-09-17 15:12:54+0000 [-] Traceback (most recent call last):

2014-09-17 15:12:54+0000 [-] File "echoserv_ssl.py", line 23, in

2014-09-17 15:12:54+0000 [-] task.react(echoserv_ssl.main)

2014-09-17 15:12:54+0000 [-] File "/usr/lib64/python2.6/site-packages/twisted/internet/task.py", line 875, in react

2014-09-17 15:12:54+0000 [-] finished = main(_reactor, *argv)

2014-09-17 15:12:54+0000 [-] File "/root/Robot/Twisted/echoserv_ssl.py", line 16, in main

2014-09-17 15:12:54+0000 [-] certificate = ssl.PrivateCertificate.loadPEM(certData)

2014-09-17 15:12:54+0000 [-] File "/usr/lib64/python2.6/site-packages/twisted/internet/_sslverify.py", line 619, in loadPEM

2014-09-17 15:12:54+0000 [-] return Class.load(data, KeyPair.load(data, crypto.FILETYPE_PEM),

2014-09-17 15:12:54+0000 [-] File "/usr/lib64/python2.6/site-packages/twisted/internet/_sslverify.py", line 725, in load

2014-09-17 15:12:54+0000 [-] return Class(crypto.load_privatekey(format, data))

2014-09-17 15:12:54+0000 [-] File "build/bdist.linux-x86_64/egg/OpenSSL/crypto.py", line 2010, in load_privatekey

2014-09-17 15:12:54+0000 [-] File "build/bdist.linux-x86_64/egg/OpenSSL/_util.py", line 22, in exception_from_error_queue

2014-09-17 15:12:54+0000 [-] OpenSSL.crypto.Error: []

This rather empty error code doesn't seem that informative to me. Can anyone explain where I'm going wrong?

Thanks!

解决方案

There are lots of ways to do this, as you've probably noticed.

Coincidentally, I had to generate a key and self-signed certificate just a couple days ago. I decided to save the code (and check it in to Twisted) rather than lose it and have to recreate it later. Perhaps it will help you:

As I mentioned, this is far from the only way to do it and probably not nearly the best way, either. However, I'm sure that it works (the resulting certificate is used in Twisted's TLS test suite).

Notice that the file is a runnable Python program (running it generates a new key and certificate) and a loadable pem file containing a private key and certificate.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值