context_factory.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # Copyright 2014-2016 OpenMarket Ltd
  2. # Copyright 2019 New Vector Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from service_identity import VerificationError
  17. from service_identity.pyopenssl import verify_hostname, verify_ip_address
  18. from zope.interface import implementer
  19. from OpenSSL import SSL, crypto
  20. from twisted.internet._sslverify import _defaultCurveName
  21. from twisted.internet.abstract import isIPAddress, isIPv6Address
  22. from twisted.internet.interfaces import IOpenSSLClientConnectionCreator
  23. from twisted.internet.ssl import (
  24. CertificateOptions,
  25. ContextFactory,
  26. TLSVersion,
  27. platformTrust,
  28. )
  29. from twisted.python.failure import Failure
  30. from twisted.web.iweb import IPolicyForHTTPS
  31. logger = logging.getLogger(__name__)
  32. _TLS_VERSION_MAP = {
  33. "1": TLSVersion.TLSv1_0,
  34. "1.1": TLSVersion.TLSv1_1,
  35. "1.2": TLSVersion.TLSv1_2,
  36. "1.3": TLSVersion.TLSv1_3,
  37. }
  38. class ServerContextFactory(ContextFactory):
  39. """Factory for PyOpenSSL SSL contexts that are used to handle incoming
  40. connections.
  41. TODO: replace this with an implementation of IOpenSSLServerConnectionCreator,
  42. per https://github.com/matrix-org/synapse/issues/1691
  43. """
  44. def __init__(self, config):
  45. # TODO: once pyOpenSSL exposes TLS_METHOD and SSL_CTX_set_min_proto_version,
  46. # switch to those (see https://github.com/pyca/cryptography/issues/5379).
  47. #
  48. # note that, despite the confusing name, SSLv23_METHOD does *not* enforce SSLv2
  49. # or v3, but is a synonym for TLS_METHOD, which allows the client and server
  50. # to negotiate an appropriate version of TLS constrained by the version options
  51. # set with context.set_options.
  52. #
  53. self._context = SSL.Context(SSL.SSLv23_METHOD)
  54. self.configure_context(self._context, config)
  55. @staticmethod
  56. def configure_context(context, config):
  57. try:
  58. _ecCurve = crypto.get_elliptic_curve(_defaultCurveName)
  59. context.set_tmp_ecdh(_ecCurve)
  60. except Exception:
  61. logger.exception("Failed to enable elliptic curve for TLS")
  62. context.set_options(
  63. SSL.OP_NO_SSLv2 | SSL.OP_NO_SSLv3 | SSL.OP_NO_TLSv1 | SSL.OP_NO_TLSv1_1
  64. )
  65. context.use_certificate_chain_file(config.tls_certificate_file)
  66. context.use_privatekey(config.tls_private_key)
  67. # https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
  68. context.set_cipher_list(
  69. "ECDH+AESGCM:ECDH+CHACHA20:ECDH+AES256:ECDH+AES128:!aNULL:!SHA1:!AESCCM"
  70. )
  71. def getContext(self):
  72. return self._context
  73. @implementer(IPolicyForHTTPS)
  74. class FederationPolicyForHTTPS:
  75. """Factory for Twisted SSLClientConnectionCreators that are used to make connections
  76. to remote servers for federation.
  77. Uses one of two OpenSSL context objects for all connections, depending on whether
  78. we should do SSL certificate verification.
  79. get_options decides whether we should do SSL certificate verification and
  80. constructs an SSLClientConnectionCreator factory accordingly.
  81. """
  82. def __init__(self, config):
  83. self._config = config
  84. # Check if we're using a custom list of a CA certificates
  85. trust_root = config.federation_ca_trust_root
  86. if trust_root is None:
  87. # Use CA root certs provided by OpenSSL
  88. trust_root = platformTrust()
  89. # "insecurelyLowerMinimumTo" is the argument that will go lower than
  90. # Twisted's default, which is why it is marked as "insecure" (since
  91. # Twisted's defaults are reasonably secure). But, since Twisted is
  92. # moving to TLS 1.2 by default, we want to respect the config option if
  93. # it is set to 1.0 (which the alternate option, raiseMinimumTo, will not
  94. # let us do).
  95. minTLS = _TLS_VERSION_MAP[config.federation_client_minimum_tls_version]
  96. _verify_ssl = CertificateOptions(
  97. trustRoot=trust_root, insecurelyLowerMinimumTo=minTLS
  98. )
  99. self._verify_ssl_context = _verify_ssl.getContext()
  100. self._verify_ssl_context.set_info_callback(_context_info_cb)
  101. _no_verify_ssl = CertificateOptions(insecurelyLowerMinimumTo=minTLS)
  102. self._no_verify_ssl_context = _no_verify_ssl.getContext()
  103. self._no_verify_ssl_context.set_info_callback(_context_info_cb)
  104. self._should_verify = self._config.federation_verify_certificates
  105. self._federation_certificate_verification_whitelist = (
  106. self._config.federation_certificate_verification_whitelist
  107. )
  108. def get_options(self, host: bytes):
  109. # IPolicyForHTTPS.get_options takes bytes, but we want to compare
  110. # against the str whitelist. The hostnames in the whitelist are already
  111. # IDNA-encoded like the hosts will be here.
  112. ascii_host = host.decode("ascii")
  113. # Check if certificate verification has been enabled
  114. should_verify = self._should_verify
  115. # Check if we've disabled certificate verification for this host
  116. if self._should_verify:
  117. for regex in self._federation_certificate_verification_whitelist:
  118. if regex.match(ascii_host):
  119. should_verify = False
  120. break
  121. ssl_context = (
  122. self._verify_ssl_context if should_verify else self._no_verify_ssl_context
  123. )
  124. return SSLClientConnectionCreator(host, ssl_context, should_verify)
  125. def creatorForNetloc(self, hostname, port):
  126. """Implements the IPolicyForHTTPS interface so that this can be passed
  127. directly to agents.
  128. """
  129. return self.get_options(hostname)
  130. @implementer(IPolicyForHTTPS)
  131. class RegularPolicyForHTTPS:
  132. """Factory for Twisted SSLClientConnectionCreators that are used to make connections
  133. to remote servers, for other than federation.
  134. Always uses the same OpenSSL context object, which uses the default OpenSSL CA
  135. trust root.
  136. """
  137. def __init__(self):
  138. trust_root = platformTrust()
  139. self._ssl_context = CertificateOptions(trustRoot=trust_root).getContext()
  140. self._ssl_context.set_info_callback(_context_info_cb)
  141. def creatorForNetloc(self, hostname, port):
  142. return SSLClientConnectionCreator(hostname, self._ssl_context, True)
  143. def _context_info_cb(ssl_connection, where, ret):
  144. """The 'information callback' for our openssl context objects.
  145. Note: Once this is set as the info callback on a Context object, the Context should
  146. only be used with the SSLClientConnectionCreator.
  147. """
  148. # we assume that the app_data on the connection object has been set to
  149. # a TLSMemoryBIOProtocol object. (This is done by SSLClientConnectionCreator)
  150. tls_protocol = ssl_connection.get_app_data()
  151. try:
  152. # ... we further assume that SSLClientConnectionCreator has set the
  153. # '_synapse_tls_verifier' attribute to a ConnectionVerifier object.
  154. tls_protocol._synapse_tls_verifier.verify_context_info_cb(ssl_connection, where)
  155. except: # noqa: E722, taken from the twisted implementation
  156. logger.exception("Error during info_callback")
  157. f = Failure()
  158. tls_protocol.failVerification(f)
  159. @implementer(IOpenSSLClientConnectionCreator)
  160. class SSLClientConnectionCreator:
  161. """Creates openssl connection objects for client connections.
  162. Replaces twisted.internet.ssl.ClientTLSOptions
  163. """
  164. def __init__(self, hostname: bytes, ctx, verify_certs: bool):
  165. self._ctx = ctx
  166. self._verifier = ConnectionVerifier(hostname, verify_certs)
  167. def clientConnectionForTLS(self, tls_protocol):
  168. context = self._ctx
  169. connection = SSL.Connection(context, None)
  170. # as per twisted.internet.ssl.ClientTLSOptions, we set the application
  171. # data to our TLSMemoryBIOProtocol...
  172. connection.set_app_data(tls_protocol)
  173. # ... and we also gut-wrench a '_synapse_tls_verifier' attribute into the
  174. # tls_protocol so that the SSL context's info callback has something to
  175. # call to do the cert verification.
  176. setattr(tls_protocol, "_synapse_tls_verifier", self._verifier)
  177. return connection
  178. class ConnectionVerifier:
  179. """Set the SNI, and do cert verification
  180. This is a thing which is attached to the TLSMemoryBIOProtocol, and is called by
  181. the ssl context's info callback.
  182. """
  183. # This code is based on twisted.internet.ssl.ClientTLSOptions.
  184. def __init__(self, hostname: bytes, verify_certs: bool):
  185. self._verify_certs = verify_certs
  186. _decoded = hostname.decode("ascii")
  187. if isIPAddress(_decoded) or isIPv6Address(_decoded):
  188. self._is_ip_address = True
  189. else:
  190. self._is_ip_address = False
  191. self._hostnameBytes = hostname
  192. self._hostnameASCII = self._hostnameBytes.decode("ascii")
  193. def verify_context_info_cb(self, ssl_connection, where):
  194. if where & SSL.SSL_CB_HANDSHAKE_START and not self._is_ip_address:
  195. ssl_connection.set_tlsext_host_name(self._hostnameBytes)
  196. if where & SSL.SSL_CB_HANDSHAKE_DONE and self._verify_certs:
  197. try:
  198. if self._is_ip_address:
  199. verify_ip_address(ssl_connection, self._hostnameASCII)
  200. else:
  201. verify_hostname(ssl_connection, self._hostnameASCII)
  202. except VerificationError:
  203. f = Failure()
  204. tls_protocol = ssl_connection.get_app_data()
  205. tls_protocol.failVerification(f)