ssl_helpers.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Helper classes for twisted.test.test_ssl.
  5. They are in a separate module so they will not prevent test_ssl importing if
  6. pyOpenSSL is unavailable.
  7. """
  8. from __future__ import division, absolute_import
  9. from twisted.python.compat import nativeString
  10. from twisted.internet import ssl
  11. from twisted.python.filepath import FilePath
  12. from OpenSSL import SSL
  13. certPath = nativeString(FilePath(__file__.encode("utf-8")
  14. ).sibling(b"server.pem").path)
  15. class ClientTLSContext(ssl.ClientContextFactory):
  16. isClient = 1
  17. def getContext(self):
  18. return SSL.Context(SSL.TLSv1_METHOD)
  19. class ServerTLSContext:
  20. isClient = 0
  21. def __init__(self, filename=certPath, method=SSL.TLSv1_METHOD):
  22. self.filename = filename
  23. self._method = method
  24. def getContext(self):
  25. ctx = SSL.Context(self._method)
  26. ctx.use_certificate_file(self.filename)
  27. ctx.use_privatekey_file(self.filename)
  28. return ctx