![](/img/trans.png)
[英]Python Twisted : SSL routines , ssl3_get_server_certificate errors
[英]Certificate Generation for Python Twisted SSL
我試圖弄清楚如何使用Python庫Twisted設置SSL鏈接。 我已經設法創建一個在服務器端工作的證書,但是當涉及到客戶端時我完全陷入困境。
來自扭曲網站的例子說:
以下示例依賴於文件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用於public。
當我嘗試運行我的測試程序時使用:
certificate = ssl.PrivateCertificate.loadPEM(certData)
我得到一個關於不開始行的錯誤。 如果客戶端不是server.crt,我應該使用哪個證書?
當我嘗試運行我的測試程序時使用:
certificate = ssl.PrivateCertificate.loadPEM(certData)我收到一個關於不開始行的錯誤。 如果客戶端不是server.crt,我應該使用哪個證書?
如果您查看Twisted howto頁面上的示例,這應該是ssl.Certificate.LoadPEM(certData) 。
如果您想要為客戶端進行基於證書的身份驗證:
我前段時間遇到過這個問題並撰寫了一篇關於我的解決方案的博文 。 它還包含創建證書的指南,並使用自己的證書頒發機構對其進行簽名。 你可以在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
隨意使用項目中的代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.