Skip to content

Commit 0bc631e

Browse files
committed
Merge pull request #72
832927e First iteration of instructions on how to set up an HTTPS JSON-RPC connection (Pierre Rochard) 6ab33df Adding SSLContext to replace the deprecated key_file and cert_file arguments in HTTPSConnection (Pierre Rochard)
2 parents 9718ef0 + 832927e commit 0bc631e

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-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,

examples/ssl-rpc-connection.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright (C) 2014 The python-bitcoinlib developers
4+
#
5+
# This file is part of python-bitcoinlib.
6+
#
7+
# It is subject to the license terms in the LICENSE file found in the top-level
8+
# directory of this distribution.
9+
#
10+
# No part of python-bitcoinlib, including this file, may be copied, modified,
11+
# propagated, or distributed except according to the terms contained in the
12+
# LICENSE file.
13+
14+
15+
## Instructions
16+
17+
# This sets up SSL on a localhost connection. Not terribly useful but it will be iterated on.
18+
19+
# Linux: cd ~/.bitcoin
20+
# Mac: cd ~/Library/Application\ Support/Bitcoin/
21+
# openssl genrsa -out server.pem 2048
22+
# openssl req -new -x509 -nodes -sha256 -days 3650 -key server.pem > server.cert
23+
# The prompts are optional, you can just hit enter
24+
25+
# Verify that your bitcoin.conf exists in the above directory and contains the following lines:
26+
# server=1
27+
# rpcssl=1
28+
# rpcuser=CHANGETHIS
29+
# rpcpassword=CHANGETHAT
30+
# rpcsslciphers=TLSv1_2
31+
# rpcsslprivatekeyfile=server.pem
32+
# rpcsslcertificatechainfile=server.cert
33+
34+
import bitcoin.rpc
35+
36+
proxy_connection = bitcoin.rpc.Proxy()
37+
print(proxy_connection.getnewaddress())

0 commit comments

Comments
 (0)