sslproxy.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright 2014 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Extends BaseHTTPRequestHandler with SSL certificate generation."""
  15. import logging
  16. import socket
  17. import certutils
  18. def _SetUpUsingDummyCert(handler):
  19. """Sets up connection providing the certificate to the client.
  20. This method handles Server Name Indication (SNI) using dummy certs.
  21. Args:
  22. handler: an instance of BaseHTTPServer.BaseHTTPRequestHandler that is used
  23. by some instance of BaseHTTPServer.HTTPServer.
  24. """
  25. # One of: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or TLSv1_METHOD
  26. context = certutils.get_ssl_context()
  27. def handle_servername(connection):
  28. """A SNI callback that happens during do_handshake()."""
  29. try:
  30. host = connection.get_servername()
  31. if host:
  32. cert_str = (
  33. handler.server.get_certificate(host))
  34. new_context = certutils.get_ssl_context()
  35. cert = certutils.load_cert(cert_str)
  36. new_context.use_certificate(cert)
  37. new_context.use_privatekey_file(handler.server.ca_cert_path)
  38. connection.set_context(new_context)
  39. return new_context
  40. # else: fail with 'no shared cipher'
  41. except Exception, e:
  42. # Do not leak any exceptions or else openssl crashes.
  43. logging.error('Exception in SNI handler: %s', e)
  44. context.set_tlsext_servername_callback(handle_servername)
  45. handler.connection = certutils.get_ssl_connection(context, handler.connection)
  46. handler.connection.set_accept_state()
  47. try:
  48. handler.connection.do_handshake()
  49. except certutils.Error, v:
  50. host = handler.connection.get_servername()
  51. if not host:
  52. logging.error('Dropping request without SNI')
  53. return ''
  54. raise certutils.Error('SSL handshake error %s: %s' % (host, str(v)))
  55. # Re-wrap the read/write streams with our new connection.
  56. handler.rfile = socket._fileobject(handler.connection, 'rb', handler.rbufsize,
  57. close=False)
  58. handler.wfile = socket._fileobject(handler.connection, 'wb', handler.wbufsize,
  59. close=False)
  60. def wrap_handler(handler_class):
  61. """Wraps a BaseHTTPHandler with SSL MITM certificates."""
  62. if certutils.openssl_import_error:
  63. # pylint: disable=raising-bad-type
  64. raise certutils.openssl_import_error
  65. class WrappedHandler(handler_class):
  66. def setup(self):
  67. handler_class.setup(self)
  68. _SetUpUsingDummyCert(self)
  69. def finish(self):
  70. handler_class.finish(self)
  71. self.connection.shutdown()
  72. self.connection.close()
  73. return WrappedHandler