2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2019-02-18 22:18:05 -07:00
|
|
|
# Copyright 2019 New Vector Ltd
|
2014-09-03 02:43:11 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2019-02-18 22:18:05 -07:00
|
|
|
|
2018-06-26 12:41:05 -06:00
|
|
|
import logging
|
2014-09-03 02:43:11 -06:00
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
from service_identity import VerificationError
|
2019-06-10 08:58:35 -06:00
|
|
|
from service_identity.pyopenssl import verify_hostname, verify_ip_address
|
2018-07-29 11:47:08 -06:00
|
|
|
from zope.interface import implementer
|
|
|
|
|
2018-04-30 09:21:11 -06:00
|
|
|
from OpenSSL import SSL, crypto
|
2019-06-09 07:01:32 -06:00
|
|
|
from twisted.internet._sslverify import _defaultCurveName
|
2019-01-24 02:34:44 -07:00
|
|
|
from twisted.internet.abstract import isIPAddress, isIPv6Address
|
2018-06-26 12:41:05 -06:00
|
|
|
from twisted.internet.interfaces import IOpenSSLClientConnectionCreator
|
2019-06-28 02:19:09 -06:00
|
|
|
from twisted.internet.ssl import (
|
|
|
|
CertificateOptions,
|
|
|
|
ContextFactory,
|
|
|
|
TLSVersion,
|
|
|
|
platformTrust,
|
|
|
|
)
|
2018-08-09 13:04:22 -06:00
|
|
|
from twisted.python.failure import Failure
|
2019-07-30 10:42:56 -06:00
|
|
|
from twisted.web.iweb import IPolicyForHTTPS
|
2014-10-24 12:27:12 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-09-01 10:55:35 -06:00
|
|
|
|
2014-10-30 05:10:17 -06:00
|
|
|
|
2019-06-28 02:19:09 -06:00
|
|
|
_TLS_VERSION_MAP = {
|
|
|
|
"1": TLSVersion.TLSv1_0,
|
|
|
|
"1.1": TLSVersion.TLSv1_1,
|
|
|
|
"1.2": TLSVersion.TLSv1_2,
|
|
|
|
"1.3": TLSVersion.TLSv1_3,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-26 12:41:05 -06:00
|
|
|
class ServerContextFactory(ContextFactory):
|
2014-09-01 10:55:35 -06:00
|
|
|
"""Factory for PyOpenSSL SSL contexts that are used to handle incoming
|
2020-09-21 05:34:06 -06:00
|
|
|
connections.
|
|
|
|
|
|
|
|
TODO: replace this with an implementation of IOpenSSLServerConnectionCreator,
|
|
|
|
per https://github.com/matrix-org/synapse/issues/1691
|
|
|
|
"""
|
2014-09-01 10:55:35 -06:00
|
|
|
|
|
|
|
def __init__(self, config):
|
2020-08-07 08:14:29 -06:00
|
|
|
# TODO: once pyOpenSSL exposes TLS_METHOD and SSL_CTX_set_min_proto_version,
|
|
|
|
# switch to those (see https://github.com/pyca/cryptography/issues/5379).
|
|
|
|
#
|
|
|
|
# note that, despite the confusing name, SSLv23_METHOD does *not* enforce SSLv2
|
|
|
|
# or v3, but is a synonym for TLS_METHOD, which allows the client and server
|
|
|
|
# to negotiate an appropriate version of TLS constrained by the version options
|
|
|
|
# set with context.set_options.
|
|
|
|
#
|
2014-09-01 10:55:35 -06:00
|
|
|
self._context = SSL.Context(SSL.SSLv23_METHOD)
|
|
|
|
self.configure_context(self._context, config)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def configure_context(context, config):
|
2014-09-01 15:29:31 -06:00
|
|
|
try:
|
2018-04-30 09:21:11 -06:00
|
|
|
_ecCurve = crypto.get_elliptic_curve(_defaultCurveName)
|
|
|
|
context.set_tmp_ecdh(_ecCurve)
|
2017-10-23 08:52:32 -06:00
|
|
|
except Exception:
|
2015-07-08 11:53:41 -06:00
|
|
|
logger.exception("Failed to enable elliptic curve for TLS")
|
2019-06-28 02:19:09 -06:00
|
|
|
|
|
|
|
context.set_options(
|
|
|
|
SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3 | SSL.OP_NO_TLSv1 | SSL.OP_NO_TLSv1_1
|
|
|
|
)
|
2015-07-08 17:45:41 -06:00
|
|
|
context.use_certificate_chain_file(config.tls_certificate_file)
|
2019-02-11 14:30:59 -07:00
|
|
|
context.use_privatekey(config.tls_private_key)
|
2015-03-06 04:34:06 -07:00
|
|
|
|
2019-01-22 03:58:50 -07:00
|
|
|
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
|
|
|
|
context.set_cipher_list(
|
2019-06-28 02:19:09 -06:00
|
|
|
"ECDH+AESGCM:ECDH+CHACHA20:ECDH+AES256:ECDH+AES128:!aNULL:!SHA1:!AESCCM"
|
2019-01-22 03:58:50 -07:00
|
|
|
)
|
2014-09-01 10:55:35 -06:00
|
|
|
|
|
|
|
def getContext(self):
|
|
|
|
return self._context
|
2018-06-24 14:38:43 -06:00
|
|
|
|
|
|
|
|
2019-07-30 10:42:56 -06:00
|
|
|
@implementer(IPolicyForHTTPS)
|
2020-09-04 04:54:56 -06:00
|
|
|
class FederationPolicyForHTTPS:
|
2019-06-09 07:01:32 -06:00
|
|
|
"""Factory for Twisted SSLClientConnectionCreators that are used to make connections
|
|
|
|
to remote servers for federation.
|
2018-08-09 13:04:22 -06:00
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
Uses one of two OpenSSL context objects for all connections, depending on whether
|
|
|
|
we should do SSL certificate verification.
|
2018-08-09 13:04:22 -06:00
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
get_options decides whether we should do SSL certificate verification and
|
|
|
|
constructs an SSLClientConnectionCreator factory accordingly.
|
2018-06-26 12:41:05 -06:00
|
|
|
"""
|
2018-06-24 14:38:43 -06:00
|
|
|
|
|
|
|
def __init__(self, config):
|
2019-04-25 07:22:49 -06:00
|
|
|
self._config = config
|
|
|
|
|
|
|
|
# Check if we're using a custom list of a CA certificates
|
|
|
|
trust_root = config.federation_ca_trust_root
|
|
|
|
if trust_root is None:
|
|
|
|
# Use CA root certs provided by OpenSSL
|
|
|
|
trust_root = platformTrust()
|
|
|
|
|
2019-06-28 02:19:09 -06:00
|
|
|
# "insecurelyLowerMinimumTo" is the argument that will go lower than
|
|
|
|
# Twisted's default, which is why it is marked as "insecure" (since
|
|
|
|
# Twisted's defaults are reasonably secure). But, since Twisted is
|
|
|
|
# moving to TLS 1.2 by default, we want to respect the config option if
|
|
|
|
# it is set to 1.0 (which the alternate option, raiseMinimumTo, will not
|
|
|
|
# let us do).
|
|
|
|
minTLS = _TLS_VERSION_MAP[config.federation_client_minimum_tls_version]
|
|
|
|
|
2020-03-17 15:32:25 -06:00
|
|
|
_verify_ssl = CertificateOptions(
|
2019-06-28 02:19:09 -06:00
|
|
|
trustRoot=trust_root, insecurelyLowerMinimumTo=minTLS
|
|
|
|
)
|
2020-03-17 15:32:25 -06:00
|
|
|
self._verify_ssl_context = _verify_ssl.getContext()
|
|
|
|
self._verify_ssl_context.set_info_callback(_context_info_cb)
|
2018-06-24 14:38:43 -06:00
|
|
|
|
2020-03-17 15:32:25 -06:00
|
|
|
_no_verify_ssl = CertificateOptions(insecurelyLowerMinimumTo=minTLS)
|
|
|
|
self._no_verify_ssl_context = _no_verify_ssl.getContext()
|
|
|
|
self._no_verify_ssl_context.set_info_callback(_context_info_cb)
|
2019-04-25 07:22:49 -06:00
|
|
|
|
2021-01-28 10:38:59 -07:00
|
|
|
self._should_verify = self._config.federation_verify_certificates
|
|
|
|
|
|
|
|
self._federation_certificate_verification_whitelist = (
|
|
|
|
self._config.federation_certificate_verification_whitelist
|
|
|
|
)
|
2019-09-13 12:58:38 -06:00
|
|
|
|
2021-01-28 10:38:59 -07:00
|
|
|
def get_options(self, host: bytes):
|
2019-09-13 12:58:38 -06:00
|
|
|
# IPolicyForHTTPS.get_options takes bytes, but we want to compare
|
|
|
|
# against the str whitelist. The hostnames in the whitelist are already
|
|
|
|
# IDNA-encoded like the hosts will be here.
|
|
|
|
ascii_host = host.decode("ascii")
|
|
|
|
|
2019-04-25 07:22:49 -06:00
|
|
|
# Check if certificate verification has been enabled
|
2021-01-28 10:38:59 -07:00
|
|
|
should_verify = self._should_verify
|
2019-04-25 07:22:49 -06:00
|
|
|
|
|
|
|
# Check if we've disabled certificate verification for this host
|
2021-01-28 10:38:59 -07:00
|
|
|
if self._should_verify:
|
|
|
|
for regex in self._federation_certificate_verification_whitelist:
|
2019-09-13 12:58:38 -06:00
|
|
|
if regex.match(ascii_host):
|
2019-04-25 07:22:49 -06:00
|
|
|
should_verify = False
|
|
|
|
break
|
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
ssl_context = (
|
|
|
|
self._verify_ssl_context if should_verify else self._no_verify_ssl_context
|
|
|
|
)
|
|
|
|
|
|
|
|
return SSLClientConnectionCreator(host, ssl_context, should_verify)
|
|
|
|
|
2019-07-30 10:42:56 -06:00
|
|
|
def creatorForNetloc(self, hostname, port):
|
2020-10-23 10:38:40 -06:00
|
|
|
"""Implements the IPolicyForHTTPS interface so that this can be passed
|
2019-07-30 10:42:56 -06:00
|
|
|
directly to agents.
|
|
|
|
"""
|
|
|
|
return self.get_options(hostname)
|
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
|
2020-03-17 15:32:25 -06:00
|
|
|
@implementer(IPolicyForHTTPS)
|
2020-09-04 04:54:56 -06:00
|
|
|
class RegularPolicyForHTTPS:
|
2020-03-17 15:32:25 -06:00
|
|
|
"""Factory for Twisted SSLClientConnectionCreators that are used to make connections
|
|
|
|
to remote servers, for other than federation.
|
|
|
|
|
|
|
|
Always uses the same OpenSSL context object, which uses the default OpenSSL CA
|
|
|
|
trust root.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
trust_root = platformTrust()
|
|
|
|
self._ssl_context = CertificateOptions(trustRoot=trust_root).getContext()
|
|
|
|
self._ssl_context.set_info_callback(_context_info_cb)
|
|
|
|
|
|
|
|
def creatorForNetloc(self, hostname, port):
|
|
|
|
return SSLClientConnectionCreator(hostname, self._ssl_context, True)
|
|
|
|
|
|
|
|
|
|
|
|
def _context_info_cb(ssl_connection, where, ret):
|
|
|
|
"""The 'information callback' for our openssl context objects.
|
|
|
|
|
|
|
|
Note: Once this is set as the info callback on a Context object, the Context should
|
|
|
|
only be used with the SSLClientConnectionCreator.
|
|
|
|
"""
|
|
|
|
# we assume that the app_data on the connection object has been set to
|
|
|
|
# a TLSMemoryBIOProtocol object. (This is done by SSLClientConnectionCreator)
|
|
|
|
tls_protocol = ssl_connection.get_app_data()
|
|
|
|
try:
|
|
|
|
# ... we further assume that SSLClientConnectionCreator has set the
|
|
|
|
# '_synapse_tls_verifier' attribute to a ConnectionVerifier object.
|
|
|
|
tls_protocol._synapse_tls_verifier.verify_context_info_cb(ssl_connection, where)
|
2021-03-24 07:34:30 -06:00
|
|
|
except BaseException: # taken from the twisted implementation
|
2020-03-17 15:32:25 -06:00
|
|
|
logger.exception("Error during info_callback")
|
|
|
|
f = Failure()
|
|
|
|
tls_protocol.failVerification(f)
|
|
|
|
|
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
@implementer(IOpenSSLClientConnectionCreator)
|
2020-09-04 04:54:56 -06:00
|
|
|
class SSLClientConnectionCreator:
|
2019-06-09 07:01:32 -06:00
|
|
|
"""Creates openssl connection objects for client connections.
|
|
|
|
|
|
|
|
Replaces twisted.internet.ssl.ClientTLSOptions
|
|
|
|
"""
|
2019-06-10 10:51:11 -06:00
|
|
|
|
2019-09-13 12:58:38 -06:00
|
|
|
def __init__(self, hostname: bytes, ctx, verify_certs: bool):
|
2019-06-09 07:01:32 -06:00
|
|
|
self._ctx = ctx
|
|
|
|
self._verifier = ConnectionVerifier(hostname, verify_certs)
|
|
|
|
|
|
|
|
def clientConnectionForTLS(self, tls_protocol):
|
|
|
|
context = self._ctx
|
|
|
|
connection = SSL.Connection(context, None)
|
|
|
|
|
|
|
|
# as per twisted.internet.ssl.ClientTLSOptions, we set the application
|
|
|
|
# data to our TLSMemoryBIOProtocol...
|
|
|
|
connection.set_app_data(tls_protocol)
|
|
|
|
|
2019-06-10 10:51:11 -06:00
|
|
|
# ... and we also gut-wrench a '_synapse_tls_verifier' attribute into the
|
2019-06-09 07:01:32 -06:00
|
|
|
# tls_protocol so that the SSL context's info callback has something to
|
|
|
|
# call to do the cert verification.
|
2021-03-16 12:19:27 -06:00
|
|
|
tls_protocol._synapse_tls_verifier = self._verifier
|
2019-06-09 07:01:32 -06:00
|
|
|
return connection
|
|
|
|
|
|
|
|
|
2020-09-04 04:54:56 -06:00
|
|
|
class ConnectionVerifier:
|
2019-06-09 07:01:32 -06:00
|
|
|
"""Set the SNI, and do cert verification
|
|
|
|
|
|
|
|
This is a thing which is attached to the TLSMemoryBIOProtocol, and is called by
|
|
|
|
the ssl context's info callback.
|
|
|
|
"""
|
2019-06-10 10:51:11 -06:00
|
|
|
|
2019-06-10 08:55:12 -06:00
|
|
|
# This code is based on twisted.internet.ssl.ClientTLSOptions.
|
|
|
|
|
2021-01-04 08:04:50 -07:00
|
|
|
def __init__(self, hostname: bytes, verify_certs: bool):
|
2019-06-09 07:01:32 -06:00
|
|
|
self._verify_certs = verify_certs
|
2019-06-10 08:55:12 -06:00
|
|
|
|
2019-09-13 12:58:38 -06:00
|
|
|
_decoded = hostname.decode("ascii")
|
|
|
|
if isIPAddress(_decoded) or isIPv6Address(_decoded):
|
2019-06-10 08:58:35 -06:00
|
|
|
self._is_ip_address = True
|
2019-06-09 07:01:32 -06:00
|
|
|
else:
|
2019-06-10 08:58:35 -06:00
|
|
|
self._is_ip_address = False
|
2019-06-09 07:01:32 -06:00
|
|
|
|
2019-09-13 12:58:38 -06:00
|
|
|
self._hostnameBytes = hostname
|
2019-06-09 07:01:32 -06:00
|
|
|
self._hostnameASCII = self._hostnameBytes.decode("ascii")
|
|
|
|
|
|
|
|
def verify_context_info_cb(self, ssl_connection, where):
|
2019-06-10 08:58:35 -06:00
|
|
|
if where & SSL.SSL_CB_HANDSHAKE_START and not self._is_ip_address:
|
2019-06-09 07:01:32 -06:00
|
|
|
ssl_connection.set_tlsext_host_name(self._hostnameBytes)
|
2019-06-10 08:55:12 -06:00
|
|
|
|
2019-06-09 07:01:32 -06:00
|
|
|
if where & SSL.SSL_CB_HANDSHAKE_DONE and self._verify_certs:
|
|
|
|
try:
|
2019-06-10 08:58:35 -06:00
|
|
|
if self._is_ip_address:
|
|
|
|
verify_ip_address(ssl_connection, self._hostnameASCII)
|
|
|
|
else:
|
|
|
|
verify_hostname(ssl_connection, self._hostnameASCII)
|
2019-06-09 07:01:32 -06:00
|
|
|
except VerificationError:
|
|
|
|
f = Failure()
|
|
|
|
tls_protocol = ssl_connection.get_app_data()
|
|
|
|
tls_protocol.failVerification(f)
|