twisted passing certificate to ssl handler

2019-06-01 00:17发布

问题:

i am designing an ssl server where i am using twisted for it with ssl and it requires client certificate authentication to continue to the program , when i verify the ssl certificate of the client it returns True but i want to pass the commanname and emailaddress in the client certificate so that i can get settings for that specific client in the handler class, so can you help me ?

from OpenSSL import SSL
from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.web import server, resource, static, twcgi

class Handler(Protocol):
    def dataReceived(self, data):
        self.transport.write(data)

    def connectionMade(self):
        self.transport.write('hello world')

def verifyCallback(connection, x509, errnum, errdepth, ok):
    global client_username
    if not ok:
        return False
    else:           
        return True

if __name__ == '__main__':

    #setting up ssl json server
    factory = Factory()
    factory.protocol = Handler
    myContextFactory = ssl.DefaultOpenSSLContextFactory('server.key', 'server.crt',SSL.TLSv1_METHOD)
    ctx = myContextFactory.getContext()
    ctx.load_verify_locations("ca.crt")
    ctx.set_verify(SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT,verifyCallback)
    reactor.listenSSL(8080, factory,myContextFactory)
    reactor.run()

回答1:

Call transport.getPeerCertificate in Protocol.dataReceived or another protocol method (only after you have received some data).