Using SSL in Twisted

  1. Overview
  2. SSL echo server and client without client authentication
  3. Using startTLS
  4. Client authentication
  5. Other facilities
  6. Conclusion

Overview

This document describes how to use SSL in Twisted servers and clients. It assumes that you know what SSL is, what some of the major reasons to use it are, and how to generate your own SSL certificates, in particular self-signed certificates. It also assumes that you are comfortable with creating TCP servers and clients as described in the server howto and client howto. After reading this document you should be able to create servers and clients that can use SSL to encrypt their connections, switch from using an unencrypted channel to an encrypted one mid-connection, and require client authentication.

Using SSL in Twisted requires that you have pyOpenSSL installed. A quick test to verify that you do is to run from OpenSSL import SSL at a python prompt and not get an error.

SSL connections require SSL contexts. These contexts are generated by a ContextFactory that maintains state like the SSL method, private key file name, and certificate file name.

Instead of using listenTCP and connectTCP to create a connection, use listenSSL and connectSSL for a server and client respectively. These methods take a contextFactory as an additional argument.

The basic server context factory is twisted.internet.ssl.ContextFactory, and the basic client context factory is twisted.internet.ssl.ClientContextFactory. They can be used as-is or subclassed. twisted.internet.ssl.DefaultOpenSSLContextFactory is a convenience server class that subclasses ContextFactory and adds default parameters to the SSL handshake and connection. Another useful class is twisted.internet.ssl.CertificateOptions; it is a factory for SSL context objects that lets you specify many of the common verification and session options so it can do the proper pyOpenSSL initialization for you.

Those are the big immediate differences between TCP and SSL connections, so let's look at an example. In it and all subsequent examples it is assumed that keys and certificates for the server, certificate authority, and client should they exist live in a keys/ subdirectory of the directory containing the example code, and that the certificates are self-signed.

SSL echo server and client without client authentication

Authentication and encryption are two separate parts of the SSL protocol. The server almost always needs a key and certificate to authenticate itself to the client but is usually configured to allow encrypted connections with unauthenticated clients who don't have certificates. This common case is demonstrated first by adding SSL support to the echo client and server in the core examples.

SSL echo server

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

from twisted.internet import ssl, reactor from twisted.internet.protocol import Factory, Protocol class Echo(Protocol): def dataReceived(self, data): """As soon as any data is received, write it back.""" self.transport.write(data) if __name__ == '__main__': factory = Factory() factory.protocol = Echo reactor.listenSSL(8000, factory, ssl.DefaultOpenSSLContextFactory( 'keys/server.key', 'keys/server.crt')) reactor.run()

SSL echo client

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

from twisted.internet import ssl, reactor from twisted.internet.protocol import ClientFactory, Protocol class EchoClient(Protocol): def connectionMade(self): print "hello, world" self.transport.write("hello, world!") def dataReceived(self, data): print "Server said:", data self.transport.loseConnection() class EchoClientFactory(ClientFactory): protocol = EchoClient def clientConnectionFailed(self, connector, reason): print "Connection failed - goodbye!" reactor.stop() def clientConnectionLost(self, connector, reason): print "Connection lost - goodbye!" reactor.stop() if __name__ == '__main__': factory = EchoClientFactory() reactor.connectSSL('localhost', 8000, factory, ssl.ClientContextFactory()) reactor.run()

Contexts are created according to a specified method. SSLv3_METHOD, SSLv23_METHOD, and TLSv1_METHOD are the valid constants that represent SSL methods to use when creating a context object. DefaultOpenSSLContextFactory and ClientContextFactory default to using SSL.SSLv23_METHOD as their method, and it is compatible for communication with all the other methods listed above. An older method constant, SSLv2_METHOD, exists but is explicitly disallowed in both DefaultOpenSSLContextFactory and ClientContextFactory for being insecure by calling set_options(SSL.OP_NO_SSLv2) on their contexts. See twisted.internet.ssl for additional comments.

Using startTLS

If you want to switch from unencrypted to encrypted traffic mid-connection, you'll need to turn on SSL with startTLS on both ends of the connection at the same time via some agreed-upon signal like the reception of a particular message. You can readily verify the switch to an encrypted channel by examining the packet payloads with a tool like Wireshark.

startTLS server

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

from OpenSSL import SSL from twisted.internet import reactor, ssl from twisted.internet.protocol import ServerFactory from twisted.protocols.basic import LineReceiver class TLSServer(LineReceiver): def lineReceived(self, line): print "received: " + line if line == "STARTTLS": print "-- Switching to TLS" self.sendLine('READY') ctx = ServerTLSContext( privateKeyFileName='keys/server.key', certificateFileName='keys/server.crt', ) self.transport.startTLS(ctx, self.factory) class ServerTLSContext(ssl.DefaultOpenSSLContextFactory): def __init__(self, *args, **kw): kw['sslmethod'] = SSL.TLSv1_METHOD ssl.DefaultOpenSSLContextFactory.__init__(self, *args, **kw) if __name__ == '__main__': factory = ServerFactory() factory.protocol = TLSServer reactor.listenTCP(8000, factory) reactor.run()

startTLS client

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

from OpenSSL import SSL from twisted.internet import reactor, ssl from twisted.internet.protocol import ClientFactory from twisted.protocols.basic import LineReceiver class ClientTLSContext(ssl.ClientContextFactory): isClient = 1 def getContext(self): return SSL.Context(SSL.TLSv1_METHOD) class TLSClient(LineReceiver): pretext = [ "first line", "last thing before TLS starts", "STARTTLS"] posttext = [ "first thing after TLS started", "last thing ever"] def connectionMade(self): for l in self.pretext: self.sendLine(l) def lineReceived(self, line): print "received: " + line if line == "READY": ctx = ClientTLSContext() self.transport.startTLS(ctx, self.factory) for l in self.posttext: self.sendLine(l) self.transport.loseConnection() class TLSClientFactory(ClientFactory): protocol = TLSClient def clientConnectionFailed(self, connector, reason): print "connection failed: ", reason.getErrorMessage() reactor.stop() def clientConnectionLost(self, connector, reason): print "connection lost: ", reason.getErrorMessage() reactor.stop() if __name__ == "__main__": factory = TLSClientFactory() reactor.connectTCP('localhost', 8000, factory) reactor.run()

startTLS is a transport method that gets passed a context. It is invoked at an agreed-upon time in the data reception method of the client and server protocols. The ServerTLSContext and ClientTLSContext classes used above inherit from the basic server and client context factories used in the earlier echo examples and illustrate two more ways of setting an SSL method.

Client authentication

Server and client-side changes to require client authentication fall largely under the dominion of pyOpenSSL, but few examples seem to exist on the web so for completeness a sample server and client are provided here.

Client-authenticating server

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

from OpenSSL import SSL from twisted.internet import ssl, reactor from twisted.internet.protocol import Factory, Protocol class Echo(Protocol): def dataReceived(self, data): self.transport.write(data) def verifyCallback(connection, x509, errnum, errdepth, ok): if not ok: print 'invalid cert from subject:', x509.get_subject() return False else: print "Certs are fine" return True if __name__ == '__main__': factory = Factory() factory.protocol = Echo myContextFactory = ssl.DefaultOpenSSLContextFactory( 'keys/server.key', 'keys/server.crt' ) ctx = myContextFactory.getContext() ctx.set_verify( SSL.VERIFY_PEER | SSL.VERIFY_FAIL_IF_NO_PEER_CERT, verifyCallback ) # Since we have self-signed certs we have to explicitly # tell the server to trust them. ctx.load_verify_locations("keys/ca.pem") reactor.listenSSL(8000, factory, myContextFactory) reactor.run()

Use the set_verify method to set the verification mode for a context object and the verification callback. The mode is either VERIFY_NONE or VERIFY_PEER. If VERIFY_PEER is set, the mode can be augmented by VERIFY_FAIL_IF_NO_PEER_CERT and/or VERIFY_CLIENT_ONCE.

The callback takes as its arguments a connection object, X509 object, error number, error depth, and return code. The purpose of the callback is to allow you to enforce additional restrictions on the verification. Thus, if the return code is False, you should return False; if the return code is True and further verification passes, return True.

Client with certificates

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

from OpenSSL import SSL from twisted.internet import ssl, reactor from twisted.internet.protocol import ClientFactory, Protocol class EchoClient(Protocol): def connectionMade(self): print "hello, world" self.transport.write("hello, world!") def dataReceived(self, data): print "Server said:", data self.transport.loseConnection() class EchoClientFactory(ClientFactory): protocol = EchoClient def clientConnectionFailed(self, connector, reason): print "Connection failed - goodbye!" reactor.stop() def clientConnectionLost(self, connector, reason): print "Connection lost - goodbye!" reactor.stop() class CtxFactory(ssl.ClientContextFactory): def getContext(self): self.method = SSL.SSLv23_METHOD ctx = ssl.ClientContextFactory.getContext(self) ctx.use_certificate_file('keys/client.crt') ctx.use_privatekey_file('keys/client.key') return ctx if __name__ == '__main__': factory = EchoClientFactory() reactor.connectSSL('localhost', 8000, factory, CtxFactory()) reactor.run()

Other facilities

twisted.protocols.amp supports encrypted connections and exposes a startTLS method one can use or subclass. twisted.web has built-in SSL support in its client, http, and xmlrpc modules.

Conclusion

After reading through this tutorial, you should be able to:

  • Use listenSSL and connectSSL to create servers and clients that use SSL
  • Use startTLS to switch a channel from being unencrypted to using SSL mid-connection
  • Add server and client support for client authentication

Index

Version: 12.0.0