@@ -63,7 +63,7 @@ def wrap_socket(sock, server_hostname, ssl_context=None):
63
63
return (ssl_sock , proto )
64
64
65
65
66
- def init_context (cert_path = None ):
66
+ def init_context (cert_path = None , cert = None , cert_password = None ):
67
67
"""
68
68
Create a new ``SSLContext`` that is correctly set up for an HTTP/2 connection.
69
69
This SSL context object can be customized and passed as a parameter to the
@@ -72,7 +72,24 @@ def init_context(cert_path=None):
72
72
certificate. The path to the certificate can be absolute or relative
73
73
to your working directory.
74
74
75
- :param cert_path: (optional) The path to the certificate file.
75
+ :param cert_path: (optional) The path to the certificate file of
76
+ “certification authority” (CA) certificates
77
+ :param cert: (optional) if string, path to ssl client cert file (.pem).
78
+ If tuple, ('cert', 'key') pair.
79
+ The certfile string must be the path to a single file in PEM format
80
+ containing the certificate as well as any number of CA certificates
81
+ needed to establish the certificate’s authenticity. The keyfile string,
82
+ if present, must point to a file containing the private key in.
83
+ Otherwise the private key will be taken from certfile as well.
84
+ :param cert_password: (optional) The password argument may be a function to
85
+ call to get the password for decrypting the private key. It will only
86
+ be called if the private key is encrypted and a password is necessary.
87
+ It will be called with no arguments, and it should return a string,
88
+ bytes, or bytearray. If the return value is a string it will be
89
+ encoded as UTF-8 before using it to decrypt the key. Alternatively a
90
+ string, bytes, or bytearray value may be supplied directly as the
91
+ password argument. It will be ignored if the private key is not
92
+ encrypted and no password is needed.
76
93
:returns: An ``SSLContext`` correctly set up for HTTP/2.
77
94
"""
78
95
context = ssl .SSLContext (ssl .PROTOCOL_SSLv23 )
@@ -90,4 +107,14 @@ def init_context(cert_path=None):
90
107
# required by the spec
91
108
context .options |= ssl .OP_NO_COMPRESSION
92
109
110
+ if cert is not None :
111
+ try :
112
+ basestring
113
+ except NameError :
114
+ basestring = str
115
+ if not isinstance (cert , basestring ):
116
+ context .load_cert_chain (cert [0 ], cert [1 ], cert_password )
117
+ else :
118
+ context .load_cert_chain (cert , password = cert_password )
119
+
93
120
return context
0 commit comments