2323cert_loc = path .join (path .dirname (__file__ ), 'certs.pem' )
2424
2525
26- def wrap_socket (sock , server_hostname ):
26+ def wrap_socket (sock , server_hostname , ssl_context = None ):
2727 """
2828 A vastly simplified SSL wrapping function. We'll probably extend this to
2929 do more things later.
3030 """
3131 global _context
3232
33+ # create the singleton SSLContext we use
3334 if _context is None : # pragma: no cover
34- _context = _init_context ()
35+ _context = init_context ()
36+
37+ # if an SSLContext is provided then use it instead of default context
38+ _ssl_context = ssl_context or _context
3539
3640 # the spec requires SNI support
37- ssl_sock = _context .wrap_socket (sock , server_hostname = server_hostname )
41+ ssl_sock = _ssl_context .wrap_socket (sock , server_hostname = server_hostname )
3842 # Setting SSLContext.check_hostname to True only verifies that the
3943 # post-handshake servername matches that of the certificate. We also need
4044 # to check that it matches the requested one.
@@ -58,13 +62,21 @@ def wrap_socket(sock, server_hostname):
5862 return (ssl_sock , proto )
5963
6064
61- def _init_context ( ):
65+ def init_context ( cert_path = None ):
6266 """
63- Creates the singleton SSLContext we use.
67+ Create a new ``SSLContext`` that is correctly set up for an HTTP/2 connection.
68+ This SSL context object can be customized and passed as a parameter to the
69+ :class:`HTTPConnection <hyper.HTTPConnection>` class. Provide your
70+ own certificate file in case you don’t want to use hyper’s default
71+ certificate. The path to the certificate can be absolute or relative
72+ to your working directory.
73+
74+ :param cert_path: (optional) The path to the certificate file.
75+ :returns: An ``SSLContext`` correctly set up for HTTP/2.
6476 """
6577 context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
6678 context .set_default_verify_paths ()
67- context .load_verify_locations (cafile = cert_loc )
79+ context .load_verify_locations (cafile = cert_path or cert_loc )
6880 context .verify_mode = ssl .CERT_REQUIRED
6981 context .check_hostname = True
7082
0 commit comments