Skip to content

Commit

Permalink
include private functions from twisted
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen committed Aug 9, 2018
1 parent d5c0ce4 commit 6489934
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions synapse/crypto/context_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
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__)

Expand Down Expand Up @@ -53,6 +53,39 @@ def getContext(self):
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):
"""
Expand Down

0 comments on commit 6489934

Please sign in to comment.