ssl_context_util.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """ndg_httpsclient SSL Context utilities module containing convenience routines
  2. for setting SSL context configuration.
  3. """
  4. __author__ = "P J Kershaw (STFC)"
  5. __date__ = "09/12/11"
  6. __copyright__ = "(C) 2012 Science and Technology Facilities Council"
  7. __license__ = "BSD - see LICENSE file in top-level directory"
  8. __contact__ = "Philip.Kershaw@stfc.ac.uk"
  9. __revision__ = '$Id$'
  10. import urlparse
  11. from OpenSSL import SSL
  12. from ndg.httpsclient.ssl_peer_verification import ServerSSLCertVerification
  13. class SSlContextConfig(object):
  14. """
  15. Holds configuration options for creating a SSL context. This is used as a
  16. template to create the contexts with specific verification callbacks.
  17. """
  18. def __init__(self, key_file=None, cert_file=None, pem_file=None, ca_dir=None,
  19. verify_peer=False):
  20. self.key_file = key_file
  21. self.cert_file = cert_file
  22. self.pem_file = pem_file
  23. self.ca_dir = ca_dir
  24. self.verify_peer = verify_peer
  25. def make_ssl_context_from_config(ssl_config=False, url=None):
  26. return make_ssl_context(ssl_config.key_file, ssl_config.cert_file,
  27. ssl_config.pem_file, ssl_config.ca_dir,
  28. ssl_config.verify_peer, url)
  29. def make_ssl_context(key_file=None, cert_file=None, pem_file=None, ca_dir=None,
  30. verify_peer=False, url=None, method=SSL.SSLv23_METHOD):
  31. """
  32. Creates SSL context containing certificate and key file locations.
  33. """
  34. ssl_context = SSL.Context(method)
  35. # Key file defaults to certificate file if present.
  36. if cert_file:
  37. ssl_context.use_certificate_file(cert_file)
  38. if key_file:
  39. ssl_context.use_privatekey_file(key_file)
  40. else:
  41. if cert_file:
  42. ssl_context.use_privatekey_file(cert_file)
  43. if pem_file or ca_dir:
  44. ssl_context.load_verify_locations(pem_file, ca_dir)
  45. def _callback(conn, x509, errnum, errdepth, preverify_ok):
  46. """Default certification verification callback.
  47. Performs no checks and returns the status passed in.
  48. """
  49. return preverify_ok
  50. verify_callback = _callback
  51. if verify_peer:
  52. ssl_context.set_verify_depth(9)
  53. if url:
  54. set_peer_verification_for_url_hostname(ssl_context, url)
  55. else:
  56. ssl_context.set_verify(SSL.VERIFY_PEER, verify_callback)
  57. else:
  58. ssl_context.set_verify(SSL.VERIFY_NONE, verify_callback)
  59. return ssl_context
  60. def set_peer_verification_for_url_hostname(ssl_context, url,
  61. if_verify_enabled=False):
  62. '''Convenience routine to set peer verification callback based on
  63. ServerSSLCertVerification class'''
  64. if not if_verify_enabled or (ssl_context.get_verify_mode() & SSL.VERIFY_PEER):
  65. urlObj = urlparse.urlparse(url)
  66. hostname = urlObj.hostname
  67. verify_callback = ServerSSLCertVerification(hostname=hostname)
  68. ssl_context.set_verify(SSL.VERIFY_PEER, verify_callback)