Skip to content

Commit 6ab33df

Browse files
author
Pierre Rochard
committed
Adding SSLContext to replace the deprecated key_file and cert_file arguments in HTTPSConnection
1 parent d4f7ed3 commit 6ab33df

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

bitcoin/rpc.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""Bitcoin Core RPC support"""
1414

1515
from __future__ import absolute_import, division, print_function, unicode_literals
16+
import ssl
1617

1718
try:
1819
import http.client as httplib
@@ -100,6 +101,22 @@ def __init__(self, service_url=None,
100101
else:
101102
raise ValueError('Unknown rpcssl value %r' % conf['rpcssl'])
102103

104+
if conf['rpcssl'] and 'rpcsslcertificatechainfile' in conf and 'rpcsslprivatekeyfile' in conf:
105+
self.__ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
106+
if os.path.exists(conf['rpcsslcertificatechainfile']):
107+
certificate = conf['rpcsslcertificatechainfile']
108+
elif os.path.exists(os.path.join(os.path.dirname(btc_conf_file), conf['rpcsslcertificatechainfile'])):
109+
certificate = os.path.join(os.path.dirname(btc_conf_file), conf['rpcsslcertificatechainfile'])
110+
else:
111+
raise ValueError('The value of rpcsslcertificatechainfile is not correctly specified in the configuration file: %s' % btc_conf_file)
112+
if os.path.exists(conf['rpcsslprivatekeyfile']):
113+
private_key = conf['rpcsslprivatekeyfile']
114+
elif os.path.exists(os.path.join(os.path.dirname(btc_conf_file), conf['rpcsslprivatekeyfile'])):
115+
private_key = os.path.join(os.path.dirname(btc_conf_file), conf['rpcsslprivatekeyfile'])
116+
else:
117+
raise ValueError('The value of rpcsslprivatekeyfile is not correctly specified in the configuration file: %s' % btc_conf_file)
118+
self.__ssl_context.load_cert_chain(certificate, private_key)
119+
103120
if 'rpcpassword' not in conf:
104121
raise ValueError('The value of rpcpassword not specified in the configuration file: %s' % btc_conf_file)
105122

@@ -128,7 +145,7 @@ def __init__(self, service_url=None,
128145

129146
if self.__url.scheme == 'https':
130147
self.__conn = httplib.HTTPSConnection(self.__url.hostname, port=port,
131-
key_file=None, cert_file=None,
148+
context=self.__ssl_context,
132149
timeout=timeout)
133150
else:
134151
self.__conn = httplib.HTTPConnection(self.__url.hostname, port=port,

0 commit comments

Comments
 (0)