| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
""" |
|---|
| 7 |
SSL transport. Requires PyOpenSSL (http://pyopenssl.sf.net). |
|---|
| 8 |
|
|---|
| 9 |
SSL connections require a ContextFactory so they can create SSL contexts. |
|---|
| 10 |
End users should only use the ContextFactory classes directly - for SSL |
|---|
| 11 |
connections use the reactor.connectSSL/listenSSL and so on, as documented |
|---|
| 12 |
in IReactorSSL. |
|---|
| 13 |
|
|---|
| 14 |
All server context factories should inherit from ContextFactory, and all |
|---|
| 15 |
client context factories should inherit from ClientContextFactory. At the |
|---|
| 16 |
moment this is not enforced, but in the future it might be. |
|---|
| 17 |
|
|---|
| 18 |
Future Plans: |
|---|
| 19 |
- split module so reactor-specific classes are in a separate module |
|---|
| 20 |
- support for switching TCP into SSL |
|---|
| 21 |
- more options |
|---|
| 22 |
|
|---|
| 23 |
Maintainer: Itamar Shtull-Trauring |
|---|
| 24 |
""" |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
supported = False |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
from OpenSSL import SSL |
|---|
| 47 |
from zope.interface import implements, implementsOnly, implementedBy |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
from twisted.internet import tcp, interfaces, base, address |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
class ContextFactory: |
|---|
| 54 |
"""A factory for SSL context objects, for server SSL connections.""" |
|---|
| 55 |
|
|---|
| 56 |
isClient = 0 |
|---|
| 57 |
|
|---|
| 58 |
def getContext(self): |
|---|
| 59 |
"""Return a SSL.Context object. override in subclasses.""" |
|---|
| 60 |
raise NotImplementedError |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
class DefaultOpenSSLContextFactory(ContextFactory): |
|---|
| 64 |
""" |
|---|
| 65 |
L{DefaultOpenSSLContextFactory} is a factory for server-side SSL context |
|---|
| 66 |
objects. These objects define certain parameters related to SSL |
|---|
| 67 |
handshakes and the subsequent connection. |
|---|
| 68 |
|
|---|
| 69 |
@ivar _contextFactory: A callable which will be used to create new |
|---|
| 70 |
context objects. This is typically L{SSL.Context}. |
|---|
| 71 |
""" |
|---|
| 72 |
_context = None |
|---|
| 73 |
|
|---|
| 74 |
def __init__(self, privateKeyFileName, certificateFileName, |
|---|
| 75 |
sslmethod=SSL.SSLv23_METHOD, _contextFactory=SSL.Context): |
|---|
| 76 |
""" |
|---|
| 77 |
@param privateKeyFileName: Name of a file containing a private key |
|---|
| 78 |
@param certificateFileName: Name of a file containing a certificate |
|---|
| 79 |
@param sslmethod: The SSL method to use |
|---|
| 80 |
""" |
|---|
| 81 |
self.privateKeyFileName = privateKeyFileName |
|---|
| 82 |
self.certificateFileName = certificateFileName |
|---|
| 83 |
self.sslmethod = sslmethod |
|---|
| 84 |
self._contextFactory = _contextFactory |
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
self.cacheContext() |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
def cacheContext(self): |
|---|
| 93 |
if self._context is None: |
|---|
| 94 |
ctx = self._contextFactory(self.sslmethod) |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
ctx.set_options(SSL.OP_NO_SSLv2) |
|---|
| 98 |
ctx.use_certificate_file(self.certificateFileName) |
|---|
| 99 |
ctx.use_privatekey_file(self.privateKeyFileName) |
|---|
| 100 |
self._context = ctx |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
def __getstate__(self): |
|---|
| 104 |
d = self.__dict__.copy() |
|---|
| 105 |
del d['_context'] |
|---|
| 106 |
return d |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
def __setstate__(self, state): |
|---|
| 110 |
self.__dict__ = state |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
def getContext(self): |
|---|
| 114 |
""" |
|---|
| 115 |
Return an SSL context. |
|---|
| 116 |
""" |
|---|
| 117 |
return self._context |
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
class ClientContextFactory: |
|---|
| 121 |
"""A context factory for SSL clients.""" |
|---|
| 122 |
|
|---|
| 123 |
isClient = 1 |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
method = SSL.SSLv23_METHOD |
|---|
| 128 |
|
|---|
| 129 |
_contextFactory = SSL.Context |
|---|
| 130 |
|
|---|
| 131 |
def getContext(self): |
|---|
| 132 |
ctx = self._contextFactory(self.method) |
|---|
| 133 |
|
|---|
| 134 |
ctx.set_options(SSL.OP_NO_SSLv2) |
|---|
| 135 |
return ctx |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
class Client(tcp.Client): |
|---|
| 140 |
"""I am an SSL client.""" |
|---|
| 141 |
|
|---|
| 142 |
implementsOnly(interfaces.ISSLTransport, |
|---|
| 143 |
*[i for i in implementedBy(tcp.Client) if i != interfaces.ITLSTransport]) |
|---|
| 144 |
|
|---|
| 145 |
def __init__(self, host, port, bindAddress, ctxFactory, connector, reactor=None): |
|---|
| 146 |
|
|---|
| 147 |
self.ctxFactory = ctxFactory |
|---|
| 148 |
tcp.Client.__init__(self, host, port, bindAddress, connector, reactor) |
|---|
| 149 |
|
|---|
| 150 |
def getHost(self): |
|---|
| 151 |
"""Returns the address from which I am connecting.""" |
|---|
| 152 |
h, p = self.socket.getsockname() |
|---|
| 153 |
return address.IPv4Address('TCP', h, p, 'SSL') |
|---|
| 154 |
|
|---|
| 155 |
def getPeer(self): |
|---|
| 156 |
"""Returns the address that I am connected.""" |
|---|
| 157 |
return address.IPv4Address('TCP', self.addr[0], self.addr[1], 'SSL') |
|---|
| 158 |
|
|---|
| 159 |
def _connectDone(self): |
|---|
| 160 |
self.startTLS(self.ctxFactory) |
|---|
| 161 |
self.startWriting() |
|---|
| 162 |
tcp.Client._connectDone(self) |
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
class Server(tcp.Server): |
|---|
| 166 |
"""I am an SSL server. |
|---|
| 167 |
""" |
|---|
| 168 |
|
|---|
| 169 |
implements(interfaces.ISSLTransport) |
|---|
| 170 |
|
|---|
| 171 |
def getHost(self): |
|---|
| 172 |
"""Return server's address.""" |
|---|
| 173 |
h, p = self.socket.getsockname() |
|---|
| 174 |
return address.IPv4Address('TCP', h, p, 'SSL') |
|---|
| 175 |
|
|---|
| 176 |
def getPeer(self): |
|---|
| 177 |
"""Return address of peer.""" |
|---|
| 178 |
h, p = self.client |
|---|
| 179 |
return address.IPv4Address('TCP', h, p, 'SSL') |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
class Port(tcp.Port): |
|---|
| 183 |
"""I am an SSL port.""" |
|---|
| 184 |
_socketShutdownMethod = 'sock_shutdown' |
|---|
| 185 |
|
|---|
| 186 |
transport = Server |
|---|
| 187 |
|
|---|
| 188 |
def __init__(self, port, factory, ctxFactory, backlog=50, interface='', reactor=None): |
|---|
| 189 |
tcp.Port.__init__(self, port, factory, backlog, interface, reactor) |
|---|
| 190 |
self.ctxFactory = ctxFactory |
|---|
| 191 |
|
|---|
| 192 |
def createInternetSocket(self): |
|---|
| 193 |
"""(internal) create an SSL socket |
|---|
| 194 |
""" |
|---|
| 195 |
sock = tcp.Port.createInternetSocket(self) |
|---|
| 196 |
return SSL.Connection(self.ctxFactory.getContext(), sock) |
|---|
| 197 |
|
|---|
| 198 |
def _preMakeConnection(self, transport): |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
transport._startTLS() |
|---|
| 202 |
return tcp.Port._preMakeConnection(self, transport) |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
class Connector(base.BaseConnector): |
|---|
| 206 |
def __init__(self, host, port, factory, contextFactory, timeout, bindAddress, reactor=None): |
|---|
| 207 |
self.host = host |
|---|
| 208 |
self.port = port |
|---|
| 209 |
self.bindAddress = bindAddress |
|---|
| 210 |
self.contextFactory = contextFactory |
|---|
| 211 |
base.BaseConnector.__init__(self, factory, timeout, reactor) |
|---|
| 212 |
|
|---|
| 213 |
def _makeTransport(self): |
|---|
| 214 |
return Client(self.host, self.port, self.bindAddress, self.contextFactory, self, self.reactor) |
|---|
| 215 |
|
|---|
| 216 |
def getDestination(self): |
|---|
| 217 |
return address.IPv4Address('TCP', self.host, self.port, 'SSL') |
|---|
| 218 |
|
|---|
| 219 |
from twisted.internet._sslverify import DistinguishedName, DN, Certificate |
|---|
| 220 |
from twisted.internet._sslverify import CertificateRequest, PrivateCertificate |
|---|
| 221 |
from twisted.internet._sslverify import KeyPair |
|---|
| 222 |
from twisted.internet._sslverify import OpenSSLCertificateOptions as CertificateOptions |
|---|
| 223 |
|
|---|
| 224 |
__all__ = [ |
|---|
| 225 |
"ContextFactory", "DefaultOpenSSLContextFactory", "ClientContextFactory", |
|---|
| 226 |
|
|---|
| 227 |
'DistinguishedName', 'DN', |
|---|
| 228 |
'Certificate', 'CertificateRequest', 'PrivateCertificate', |
|---|
| 229 |
'KeyPair', |
|---|
| 230 |
'CertificateOptions', |
|---|
| 231 |
] |
|---|
| 232 |
|
|---|
| 233 |
supported = True |
|---|