66Contains the TLS/SSL logic for use in hyper.
77"""
88import os.path as path
9-
9+ from .common.exceptions import MissingCertFile
1010from .compat import ignore_missing, ssl
1111
1212
@@ -29,14 +29,17 @@ def wrap_socket(sock, server_hostname, ssl_context=None, force_proto=None):
2929 A vastly simplified SSL wrapping function. We'll probably extend this to
3030 do more things later.
3131 """
32- global _context
3332
34- # create the singleton SSLContext we use
35- if _context is None: # pragma: no cover
36- _context = init_context()
33+ global _context
3734
38- # if an SSLContext is provided then use it instead of default context
39- _ssl_context = ssl_context or _context
35+ if ssl_context:
36+ # if an SSLContext is provided then use it instead of default context
37+ _ssl_context = ssl_context
38+ else:
39+ # create the singleton SSLContext we use
40+ if _context is None: # pragma: no cover
41+ _context = init_context()
42+ _ssl_context = _context
4043
4144 # the spec requires SNI support
4245 ssl_sock = _ssl_context.wrap_socket(sock, server_hostname=server_hostname)
@@ -94,9 +97,17 @@ def init_context(cert_path=None, cert=None, cert_password=None):
9497 encrypted and no password is needed.
9598 :returns: An ``SSLContext`` correctly set up for HTTP/2.
9699 """
100+ cafile = cert_path or cert_loc
101+ if not cafile or not path.exists(cafile):
102+ err_msg = ("No certificate found at " + str(cafile) + ". Either " +
103+ "ensure the default cert.pem file is included in the " +
104+ "distribution or provide a custom certificate when " +
105+ "creating the connection.")
106+ raise MissingCertFile(err_msg)
107+
97108 context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
98109 context.set_default_verify_paths()
99- context.load_verify_locations(cafile=cert_path or cert_loc )
110+ context.load_verify_locations(cafile=cafile )
100111 context.verify_mode = ssl.CERT_REQUIRED
101112 context.check_hostname = True
102113
0 commit comments