簡體   English   中英

Python tcp socket with ssl encryption [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)

[英]Python tcp socket with ssl encryption [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)

我正在使用 Windows 10 計算機和 Windows 10 虛擬機。 我創建了一個 tcp 套接字服務器/客戶端應用程序,該應用程序通過 python ssl 保護。 我的代碼如下所示:

服務器:

context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.set_ciphers('ECDHE-ECDSA-AES256-GCM-SHA384') 
context.load_cert_chain(certfile="cert.pem", keyfile="prKey.pem")

tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((ip, port))
    
ssocket = context.wrap_socket(tcpsock, server_side=True)

客戶:

context = ssl.SSLContext(ssl.PROTOCOL_TLS)
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True
context.load_verify_locations("cert.pem")

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock = context.wrap_socket(sock, server_hostname=server_ip)

我的朋友創建了一個官方證書用於我的服務器端身份驗證。 如果我理解正確,這是通過 OpenSSL 創建和驗證的。 但是,當我嘗試使用我的客戶端連接到正在運行的服務器時,我收到以下錯誤消息(客戶端錯誤):

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)

我將證書 + 匹配的私鑰(pem 格式)放在虛擬機上,並且在我的本地主機上有一份證書(pem 格式)的副本。

我還有一個 cer 文件格式的證書,我將它安裝到根 windows 證書存儲(客戶端和服務器端)。

由於這不起作用,我一直在研究並發現有關certifi的信息。 我安裝了它,並通過使用print(certifi.where())找到了一個cacert.pem文件。 其中包含多個證書。 我通過復制和粘貼將我的朋友證書添加到該文件,安全,但我仍然收到相同的錯誤。

現在我真的有點迷失了什么是錯的。 當我禁用context.verify_mode = ssl.CERT_REQUIRED我的代碼工作得很好,但我真的想要我的代碼的服務器端身份驗證..

我將客戶的代碼更改為:

context = ssl.create_default_context()
context.verify_mode = ssl.CERT_REQUIRED
context.check_hostname = True

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock = context.wrap_socket(sock, server_hostname=self.ip)

這樣我就不必手動交出證書文件。 相反,似乎我的 Windows 根證書庫被掃描並自動找到我的 VM 證書(因為我安裝了 .cer 文件)。


在服務器端,我只需要傳遞 certificate.cer 文件而不是 pem 格式。
 context.load_cert_chain(certfile="cert.cer", keyfile="prKey.pem")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM