2016-12-01 16:48:04 +01:00
|
|
|
"""
|
|
|
|
SSL/TLS negotiation.
|
|
|
|
"""
|
|
|
|
|
2017-04-04 10:46:01 +02:00
|
|
|
from network.advanceddispatcher import AdvancedDispatcher
|
|
|
|
import network.asyncore_pollchoose as asyncore
|
2016-12-01 16:48:04 +01:00
|
|
|
import socket
|
|
|
|
import ssl
|
2017-01-12 07:26:04 +01:00
|
|
|
import sys
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-01-11 20:47:27 +01:00
|
|
|
import protocol
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-04-04 10:46:01 +02:00
|
|
|
class TLSDispatcher(AdvancedDispatcher):
|
2016-12-01 16:48:04 +01:00
|
|
|
def __init__(self, address=None, sock=None,
|
2017-04-04 10:46:01 +02:00
|
|
|
certfile=None, keyfile=None, server_side=False, ciphers=protocol.sslProtocolCiphers):
|
2016-12-01 16:48:04 +01:00
|
|
|
self.want_read = self.want_write = True
|
2017-04-04 10:46:01 +02:00
|
|
|
if certfile is None:
|
|
|
|
self.certfile = os.path.join(paths.codePath(), 'sslkeys', 'cert.pem')
|
|
|
|
else:
|
|
|
|
self.certfile = certfile
|
|
|
|
if keyfile is None:
|
|
|
|
self.keyfile = os.path.join(paths.codePath(), 'sslkeys', 'key.pem')
|
|
|
|
else:
|
|
|
|
self.keyfile = keyfile
|
2016-12-01 16:48:04 +01:00
|
|
|
self.server_side = server_side
|
|
|
|
self.ciphers = ciphers
|
2017-04-04 10:46:01 +02:00
|
|
|
self.tlsStarted = False
|
2016-12-01 16:48:04 +01:00
|
|
|
self.tlsDone = False
|
2017-04-04 10:46:01 +02:00
|
|
|
self.isSSL = False
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-04-04 10:46:01 +02:00
|
|
|
def state_tls_init(self):
|
|
|
|
self.isSSL = True
|
2016-12-01 16:48:04 +01:00
|
|
|
# Once the connection has been established, it's safe to wrap the
|
|
|
|
# socket.
|
2017-01-11 20:47:27 +01:00
|
|
|
if sys.version_info >= (2,7,9):
|
|
|
|
context = ssl.create_default_context(purpose = ssl.Purpose.SERVER_AUTH if self.server_side else ssl.Purpose.CLIENT_AUTH)
|
2017-01-12 07:26:04 +01:00
|
|
|
context.set_ciphers(self.ciphers)
|
2017-04-04 10:46:01 +02:00
|
|
|
context.set_ecdh_curve("secp256k1")
|
2017-01-11 20:47:27 +01:00
|
|
|
context.check_hostname = False
|
|
|
|
context.verify_mode = ssl.CERT_NONE
|
|
|
|
# also exclude TLSv1 and TLSv1.1 in the future
|
2017-04-04 10:46:01 +02:00
|
|
|
context.options = ssl.OP_ALL | ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_SINGLE_ECDH_USE | ssl.OP_CIPHER_SERVER_PREFERENCE
|
|
|
|
self.sslSocket = context.wrap_socket(self.sock, server_side = self.server_side, do_handshake_on_connect=False)
|
2017-01-11 20:47:27 +01:00
|
|
|
else:
|
|
|
|
self.sslSocket = ssl.wrap_socket(self.socket,
|
2016-12-01 16:48:04 +01:00
|
|
|
server_side=self.server_side,
|
2017-01-11 20:47:27 +01:00
|
|
|
ssl_version=protocol.sslProtocolVersion,
|
2016-12-01 16:48:04 +01:00
|
|
|
certfile=self.certfile,
|
|
|
|
keyfile=self.keyfile,
|
|
|
|
ciphers=self.ciphers,
|
|
|
|
do_handshake_on_connect=False)
|
|
|
|
self.sslSocket.setblocking(0)
|
|
|
|
self.want_read = self.want_write = True
|
|
|
|
# if hasattr(self.socket, "context"):
|
|
|
|
# self.socket.context.set_ecdh_curve("secp256k1")
|
|
|
|
|
|
|
|
def writable(self):
|
2017-04-04 10:46:01 +02:00
|
|
|
if self.tlsStarted and not self.tlsDone:
|
|
|
|
return self.want_write
|
|
|
|
else:
|
|
|
|
return AdvancedDispacher.writable(self)
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
def readable(self):
|
2017-04-04 10:46:01 +02:00
|
|
|
if self.tlsStarted and not self.tlsDone:
|
|
|
|
return self.want_read
|
|
|
|
else:
|
|
|
|
return AdvancedDispacher.readable(self)
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
def handle_read(self):
|
2017-04-04 10:46:01 +02:00
|
|
|
if self.tlsStarted and not self.tlsDone:
|
2016-12-01 16:48:04 +01:00
|
|
|
self._handshake()
|
2017-04-04 10:46:01 +02:00
|
|
|
else:
|
|
|
|
return AdvancedDispacher.handle_read(self)
|
2016-12-01 16:48:04 +01:00
|
|
|
|
|
|
|
def handle_write(self):
|
2017-04-04 10:46:01 +02:00
|
|
|
if self.tlsStarted and not not self.tlsDone:
|
2016-12-01 16:48:04 +01:00
|
|
|
self._handshake()
|
2017-04-04 10:46:01 +02:00
|
|
|
else:
|
|
|
|
return AdvancedDispacher.handle_write(self)
|
2016-12-01 16:48:04 +01:00
|
|
|
|
2017-04-04 10:46:01 +02:00
|
|
|
def state_tls_handshake(self):
|
2016-12-01 16:48:04 +01:00
|
|
|
"""
|
|
|
|
Perform the handshake.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
self.sslSocket.do_handshake()
|
|
|
|
except ssl.SSLError, err:
|
|
|
|
self.want_read = self.want_write = False
|
|
|
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
|
|
|
self.want_read = True
|
|
|
|
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
|
|
|
self.want_write = True
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
# The handshake has completed, so remove this channel and...
|
|
|
|
self.del_channel()
|
|
|
|
self.set_socket(self.sslSocket)
|
|
|
|
self.tlsDone = True
|