当前位置:   article > 正文

用python创建及使用一个tls_使用TLS和Python进行身份验证

python mutual tls

一句话:是的,这是完全可能的,所有必要的东西都是

移植到Python3—我在Mac上测试了Python3.4下的所有内容

工作顺利。在

简单的回答是

“use ^{}”

但考虑到要达到这一点,需要进行大量的设置

可能,我已经构建了一个完全有效的示例,您应该能够

试一试,再接再厉。在

对于后代来说,首先需要生成一些客户端证书

由同一CA签署。您可能已经完成了此操作,但其他人可以

了解答案并自己尝试(这样我就可以测试我的

回答我自己;—)),他们需要这样的代码:# newcert.py

from twisted.python.filepath import FilePath

from twisted.internet.ssl import PrivateCertificate, KeyPair, DN

def getCAPrivateCert():

privatePath = FilePath(b"ca-private-cert.pem")

if privatePath.exists():

return PrivateCertificate.loadPEM(privatePath.getContent())

else:

caKey = KeyPair.generate(size=4096)

caCert = caKey.selfSignedCert(1, CN="the-authority")

privatePath.setContent(caCert.dumpPEM())

return caCert

def clientCertFor(name):

signingCert = getCAPrivateCert()

clientKey = KeyPair.generate(size=4096)

csr = clientKey.requestObject(DN(CN=name), "sha1")

clientCert = signingCert.signRequestObject(

csr, serialNumber=1, digestAlgorithm="sha1")

return PrivateCertificate.fromCertificateAndKeyPair(clientCert, clientKey)

if __name__ == '__main__':

import sys

name = sys.argv[1]

pem = clientCertFor(name.encode("utf-8")).dumpPEM()

FilePath(name.encode("utf-8") + b".client.private.pem").setContent(pem)

使用此程序,您可以创建如下证书:

^{pr2}$

现在您应该有一些可以使用的文件:$ ls -1 *.pem

a.client.private.pem

b.client.private.pem

ca-private-cert.pem

然后您将需要一个使用这些证书之一并发送一些证书的客户机

数据:# tlsclient.py

from twisted.python.filepath import FilePath

from twisted.internet.endpoints import SSL4ClientEndpoint

from twisted.internet.ssl import (

PrivateCertificate, Certificate, optionsForClientTLS)

from twisted.internet.defer import Deferred, inlineCallbacks

from twisted.internet.task import react

from twisted.internet.protocol import Protocol, Factory

class SendAnyData(Protocol):

def connectionMade(self):

self.deferred = Deferred()

self.transport.write(b"HELLO\r\n")

def connectionLost(self, reason):

self.deferred.callback(None)

@inlineCallbacks

def main(reactor, name):

pem = FilePath(name.encode("utf-8") + b".client.private.pem").getContent()

caPem = FilePath(b"ca-private-cert.pem").getContent()

clientEndpoint = SSL4ClientEndpoint(

reactor, u"localhost", 4321,

optionsForClientTLS(u"the-authority", Certificate.loadPEM(caPem),

PrivateCertificate.loadPEM(pem)),

)

proto = yield clientEndpoint.connect(Factory.forProtocol(SendAnyData))

yield proto.deferred

import sys

react(main, sys.argv[1:])

最后,一个可以区分它们的服务器:# whichclient.py

from twisted.python.filepath import FilePath

from twisted.internet.endpoints import SSL4ServerEndpoint

from twisted.internet.ssl import PrivateCertificate, Certificate

from twisted.internet.defer import Deferred

from twisted.internet.task import react

from twisted.internet.protocol import Protocol, Factory

class ReportWhichClient(Protocol):

def dataReceived(self, data):

peerCertificate = Certificate.peerFromTransport(self.transport)

print(peerCertificate.getSubject().commonName.decode('utf-8'))

self.transport.loseConnection()

def main(reactor):

pemBytes = FilePath(b"ca-private-cert.pem").getContent()

certificateAuthority = Certificate.loadPEM(pemBytes)

myCertificate = PrivateCertificate.loadPEM(pemBytes)

serverEndpoint = SSL4ServerEndpoint(

reactor, 4321, myCertificate.options(certificateAuthority)

)

serverEndpoint.listen(Factory.forProtocol(ReportWhichClient))

return Deferred()

react(main, [])

为了简单起见,我们将重复使用CA自己的证书

服务器,但在更现实的场景中,您显然希望

适当的证书。在

现在您可以在一个窗口中运行whichclient.py,然后在另一个窗口中运行python tlsclient.py a;

python tlsclient.py b,并查看whichclient.py打印输出

a和{},通过commonName识别客户机

证书主题中的字段。在

这里需要注意的一点是,您最初可能希望将该调用

Certificate.peerFromTransport到connectionMade方法中;这不会

工作。

Twisted does not presently have a callback for "TLS handshake complete";

希望它最终会,但在它出现之前,你必须等到

从对等方接收到一些经过身份验证的数据,以确保握手

完整的。对于几乎所有的应用程序,这是好的,因为

已经收到了执行任何操作的指示(在您的情况下,下载更新)

对等方必须已经发送了证书。在

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/59789
推荐阅读
相关标签
  

闽ICP备14008679号