6
6
Contains the TLS/SSL logic for use in hyper.
7
7
"""
8
8
import os .path as path
9
-
9
+ from . common . exceptions import MissingCertFile
10
10
from .compat import ignore_missing , ssl
11
11
12
12
@@ -29,14 +29,17 @@ def wrap_socket(sock, server_hostname, ssl_context=None, force_proto=None):
29
29
A vastly simplified SSL wrapping function. We'll probably extend this to
30
30
do more things later.
31
31
"""
32
- global _context
33
32
34
- # create the singleton SSLContext we use
35
- if _context is None : # pragma: no cover
36
- _context = init_context ()
33
+ global _context
37
34
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
40
43
41
44
# the spec requires SNI support
42
45
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):
94
97
encrypted and no password is needed.
95
98
:returns: An ``SSLContext`` correctly set up for HTTP/2.
96
99
"""
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
+
97
108
context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
98
109
context .set_default_verify_paths ()
99
- context .load_verify_locations (cafile = cert_path or cert_loc )
110
+ context .load_verify_locations (cafile = cafile )
100
111
context .verify_mode = ssl .CERT_REQUIRED
101
112
context .check_hostname = True
102
113
0 commit comments