如何修复OpenSSL.SSL.错误:Twisted框架中的[('SSL routines','','certificate verify failed')]

dwbf0jvd  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(213)

我正在使用macos ventura和pycharm,并试图在twisted服务器/客户端与tls之间建立连接:TLS服务器通过客户端证书验证进行客户端身份验证因此,我创建了一个像this example一样的自签名证书:

openssl req -x509 -newkey rsa:4096 -days 365 -nodes -keyout ca-key.pem -out ca-cert.pem -subj "/C=/ST=/L=/O=/OU=/CN=*.example.com/emailAddress="

openssl req -newkey rsa:4096 -nodes -keyout server-key.pem -out server-req.pem -subj "/C=/ST=/L=/O=/OU=/CN=*.example.com/emailAddress="

openssl x509 -req -in server-req.pem -days 60 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile server-ext.cnf

server-ext.cnf包含:subjectAltName=DNS:*.example.comIP:0.0.0.0
openssl验证-CAfile ca-cert.pem服务器证书。pem返回OK
我在server.pem中合并了server-key.pem和server-cert.pem,并在public.pem中重命名了server-cert.pem
为服务器运行此

\#!/usr/bin/env python

# Copyright (c) Twisted Matrix Laboratories.

# See LICENSE for details.

import sys

from twisted.internet import ssl, protocol, task, defer
from twisted.python import log
from twisted.python.modules import getModule

import echoserv

def main(reactor):
log.startLogging(sys.stdout)
certData = getModule(__name__).filePath.sibling('public.pem').getContent()
authData = getModule(__name__).filePath.sibling('server.pem').getContent()
authority = ssl.Certificate.loadPEM(certData)
certificate = ssl.PrivateCertificate.loadPEM(authData)
factory = protocol.Factory.forProtocol(echoserv.Echo)
reactor.listenSSL(8000, factory, certificate.options(authority))
return defer.Deferred()

if __name__ == '__main__':
import ssl_clientauth_server
task.react(ssl_clientauth_server.main)

而对于客户端

#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

from twisted.internet import ssl, task, protocol, endpoints, defer
from twisted.python.modules import getModule

import echoclient

@defer.inlineCallbacks
def main(reactor):
    factory = protocol.Factory.forProtocol(echoclient.EchoClient)
    certData = getModule(__name__).filePath.sibling('public.pem').getContent()
    authData = getModule(__name__).filePath.sibling('server.pem').getContent()
    clientCertificate = ssl.PrivateCertificate.loadPEM(authData)
    authority = ssl.Certificate.loadPEM(certData)
    options = ssl.optionsForClientTLS(u'example.com', authority,
                                      clientCertificate)
    endpoint = endpoints.SSL4ClientEndpoint(reactor, 'localhost', 8000,
                                            options)
    echoClient = yield endpoint.connect(factory)

    done = defer.Deferred()
    # echoClient.connectionLost = lambda reason: done.callback(None)
    echoClient.connectionLost = lambda reason: done.callback(reason)
    yield done

if __name__ == '__main__':
    import ssl_clientauth_client
    task.react(ssl_clientauth_client.main)

服务器代码正常工作,但客户端代码引发错误:OpenSSL.SSL.Error:[('SSL例程','','证书验证失败')]
我尝试了这个Answer中的步骤,通过homebrew安装了openssl,certifici,确实导出了SSL_CERT_FILE="$(python -m certifici)",安装了service-identity,但到目前为止没有任何帮助。
我被卡住了,到目前为止,将感谢您的帮助。谢谢!我期待着服务器和客户端之间的连接使用TLS

rkkpypqq

rkkpypqq1#

使用以下命令重新创建密钥/证书后:

openssl req \
   -newkey rsa:2048 -nodes -keyout domain.key \
   -x509 -days 365 -out domain.crt

错误:OpenSSL.SSL.错误:[('SSL routines','','certificate verify failed')]消失。

相关问题