summary refs log tree commit diff
path: root/synapse/crypto
diff options
context:
space:
mode:
authorJeroen <vo.jeroen@gmail.com>2018-08-09 21:04:22 +0200
committerJeroen <vo.jeroen@gmail.com>2018-08-09 21:04:22 +0200
commit64899341dc8988b40b86dd49034aae27ddcb8c44 (patch)
treeb9532f7ab7829241d0a8fc483e769731f87984a5 /synapse/crypto
parentupdated docstring for ServerContextFactory (diff)
downloadsynapse-64899341dc8988b40b86dd49034aae27ddcb8c44.tar.xz
include private functions from twisted
Diffstat (limited to 'synapse/crypto')
-rw-r--r--synapse/crypto/context_factory.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/synapse/crypto/context_factory.py b/synapse/crypto/context_factory.py
index 29a75e1873..08c41a92b6 100644
--- a/synapse/crypto/context_factory.py
+++ b/synapse/crypto/context_factory.py
@@ -16,10 +16,10 @@ import logging
 from zope.interface import implementer
 
 from OpenSSL import SSL, crypto
-from twisted.internet._idna import _idnaBytes
-from twisted.internet._sslverify import _defaultCurveName, _tolerateErrors
+from twisted.internet._sslverify import _defaultCurveName
 from twisted.internet.interfaces import IOpenSSLClientConnectionCreator
 from twisted.internet.ssl import CertificateOptions, ContextFactory
+from twisted.python.failure import Failure
 
 logger = logging.getLogger(__name__)
 
@@ -53,6 +53,39 @@ class ServerContextFactory(ContextFactory):
         return self._context
 
 
+def _idnaBytes(text):
+    """
+    Convert some text typed by a human into some ASCII bytes. This is a
+    copy of twisted.internet._idna._idnaBytes. For documentation, see the
+    twisted documentation.
+    """
+    try:
+        import idna
+    except ImportError:
+        return text.encode("idna")
+    else:
+        return idna.encode(text)
+
+
+def _tolerateErrors(wrapped):
+    """
+    Wrap up an info_callback for pyOpenSSL so that if something goes wrong
+    the error is immediately logged and the connection is dropped if possible.
+    This is a copy of twisted.internet._sslverify._tolerateErrors. For
+    documentation, see the twisted documentation.
+    """
+
+    def infoCallback(connection, where, ret):
+        try:
+            return wrapped(connection, where, ret)
+        except:  # noqa: E722, taken from the twisted implementation
+            f = Failure()
+            logger.exception("Error during info_callback")
+            connection.get_app_data().failVerification(f)
+
+    return infoCallback
+
+
 @implementer(IOpenSSLClientConnectionCreator)
 class ClientTLSOptions(object):
     """