2012-01-19 08:11:36 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
import os
|
|
|
|
import re
|
2013-10-01 02:24:55 -07:00
|
|
|
import socket
|
|
|
|
import ssl
|
2015-06-02 08:03:33 -07:00
|
|
|
import sys
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import traceback
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2014-09-28 14:19:52 -07:00
|
|
|
import requests
|
|
|
|
ca_path = requests.certs.where()
|
|
|
|
|
2015-05-06 16:16:42 -07:00
|
|
|
import util
|
|
|
|
import x509
|
2015-08-07 02:58:59 -07:00
|
|
|
import pem
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2013-10-02 00:22:13 -07:00
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
def Connection(server, queue, config_path):
|
2015-10-06 13:03:24 -07:00
|
|
|
"""Makes asynchronous connections to a remote electrum server.
|
2015-06-02 08:03:33 -07:00
|
|
|
Returns the running thread that is making the connection.
|
2015-05-04 22:02:51 -07:00
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
Once the thread has connected, it finishes, placing a tuple on the
|
|
|
|
queue of the form (server, socket), where socket is None if
|
|
|
|
connection failed.
|
2015-05-04 22:02:51 -07:00
|
|
|
"""
|
2014-07-29 00:28:27 -07:00
|
|
|
host, port, protocol = server.split(':')
|
2015-06-02 08:03:33 -07:00
|
|
|
if not protocol in 'st':
|
|
|
|
raise Exception('Unknown protocol: %s' % protocol)
|
|
|
|
c = TcpConnection(server, queue, config_path)
|
|
|
|
c.start()
|
|
|
|
return c
|
2015-05-23 06:59:29 -07:00
|
|
|
|
2015-09-06 05:40:00 -07:00
|
|
|
class TcpConnection(threading.Thread, util.PrintError):
|
2012-10-11 11:10:12 -07:00
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
def __init__(self, server, queue, config_path):
|
2013-10-01 00:01:46 -07:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.daemon = True
|
2015-06-02 08:03:33 -07:00
|
|
|
self.config_path = config_path
|
|
|
|
self.queue = queue
|
2013-10-06 03:28:45 -07:00
|
|
|
self.server = server
|
2014-07-29 00:28:27 -07:00
|
|
|
self.host, self.port, self.protocol = self.server.split(':')
|
2015-07-01 00:09:00 -07:00
|
|
|
self.host = str(self.host)
|
2014-07-29 00:28:27 -07:00
|
|
|
self.port = int(self.port)
|
|
|
|
self.use_ssl = (self.protocol == 's')
|
2013-10-01 00:01:46 -07:00
|
|
|
|
2015-09-06 05:40:00 -07:00
|
|
|
def diagnostic_name(self):
|
|
|
|
return self.host
|
2015-06-02 08:03:33 -07:00
|
|
|
|
|
|
|
def check_host_name(self, peercert, name):
|
|
|
|
"""Simple certificate/host name checker. Returns True if the
|
|
|
|
certificate matches, False otherwise. Does not support
|
|
|
|
wildcards."""
|
|
|
|
# Check that the peer has supplied a certificate.
|
|
|
|
# None/{} is not acceptable.
|
|
|
|
if not peercert:
|
|
|
|
return False
|
|
|
|
if peercert.has_key("subjectAltName"):
|
|
|
|
for typ, val in peercert["subjectAltName"]:
|
|
|
|
if typ == "DNS" and val == name:
|
|
|
|
return True
|
2012-03-17 15:32:36 -07:00
|
|
|
else:
|
2015-06-02 08:03:33 -07:00
|
|
|
# Only check the subject DN if there is no subject alternative
|
|
|
|
# name.
|
|
|
|
cn = None
|
|
|
|
for attr, val in peercert["subject"]:
|
|
|
|
# Use most-specific (last) commonName attribute.
|
|
|
|
if attr == "commonName":
|
|
|
|
cn = val
|
|
|
|
if cn is not None:
|
|
|
|
return cn == name
|
|
|
|
return False
|
2012-03-19 07:57:47 -07:00
|
|
|
|
2014-09-19 00:25:44 -07:00
|
|
|
def get_simple_socket(self):
|
|
|
|
try:
|
|
|
|
l = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM)
|
|
|
|
except socket.gaierror:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("cannot resolve hostname")
|
2014-09-19 00:25:44 -07:00
|
|
|
return
|
|
|
|
for res in l:
|
|
|
|
try:
|
|
|
|
s = socket.socket(res[0], socket.SOCK_STREAM)
|
|
|
|
s.connect(res[4])
|
2015-06-02 02:13:36 -07:00
|
|
|
s.settimeout(2)
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
|
2014-09-19 00:25:44 -07:00
|
|
|
return s
|
2015-02-28 10:45:10 -08:00
|
|
|
except BaseException as e:
|
2014-09-19 00:25:44 -07:00
|
|
|
continue
|
|
|
|
else:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("failed to connect", str(e))
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-09-19 00:25:44 -07:00
|
|
|
def get_socket(self):
|
2013-09-30 05:01:49 -07:00
|
|
|
if self.use_ssl:
|
2015-06-02 08:03:33 -07:00
|
|
|
cert_path = os.path.join(self.config_path, 'certs', self.host)
|
2013-09-30 05:01:49 -07:00
|
|
|
if not os.path.exists(cert_path):
|
2013-10-01 18:24:14 -07:00
|
|
|
is_new = True
|
2014-09-19 00:25:44 -07:00
|
|
|
s = self.get_simple_socket()
|
|
|
|
if s is None:
|
2013-12-13 00:22:39 -08:00
|
|
|
return
|
2014-09-19 00:25:44 -07:00
|
|
|
# try with CA first
|
|
|
|
try:
|
2015-10-27 02:44:36 -07:00
|
|
|
s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv23, cert_reqs=ssl.CERT_REQUIRED, ca_certs=ca_path, do_handshake_on_connect=True)
|
2014-09-19 00:25:44 -07:00
|
|
|
except ssl.SSLError, e:
|
2014-09-19 01:18:33 -07:00
|
|
|
s = None
|
2015-06-02 08:03:33 -07:00
|
|
|
if s and self.check_host_name(s.getpeercert(), self.host):
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("SSL certificate signed by CA")
|
2014-09-19 01:18:33 -07:00
|
|
|
return s
|
2013-11-13 11:35:35 -08:00
|
|
|
|
2014-09-19 00:25:44 -07:00
|
|
|
# get server certificate.
|
|
|
|
# Do not use ssl.get_server_certificate because it does not work with proxy
|
|
|
|
s = self.get_simple_socket()
|
2015-06-02 02:13:36 -07:00
|
|
|
if s is None:
|
|
|
|
return
|
2014-09-19 00:25:44 -07:00
|
|
|
try:
|
2015-10-27 02:44:36 -07:00
|
|
|
s = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_SSLv23, cert_reqs=ssl.CERT_NONE, ca_certs=None)
|
2014-09-19 00:25:44 -07:00
|
|
|
except ssl.SSLError, e:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("SSL error retrieving SSL certificate:", e)
|
2013-09-30 05:01:49 -07:00
|
|
|
return
|
2013-10-01 02:24:55 -07:00
|
|
|
|
|
|
|
dercert = s.getpeercert(True)
|
|
|
|
s.close()
|
|
|
|
cert = ssl.DER_cert_to_PEM_cert(dercert)
|
2013-10-08 04:08:13 -07:00
|
|
|
# workaround android bug
|
2013-10-08 04:24:54 -07:00
|
|
|
cert = re.sub("([^\n])-----END CERTIFICATE-----","\\1\n-----END CERTIFICATE-----",cert)
|
2013-10-02 00:22:13 -07:00
|
|
|
temporary_path = cert_path + '.temp'
|
|
|
|
with open(temporary_path,"w") as f:
|
2013-09-30 05:01:49 -07:00
|
|
|
f.write(cert)
|
2013-10-01 18:24:14 -07:00
|
|
|
else:
|
|
|
|
is_new = False
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-09-19 00:25:44 -07:00
|
|
|
s = self.get_simple_socket()
|
2013-11-14 00:23:38 -08:00
|
|
|
if s is None:
|
2013-10-01 00:01:46 -07:00
|
|
|
return
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2012-10-17 02:35:24 -07:00
|
|
|
if self.use_ssl:
|
2013-09-30 05:01:49 -07:00
|
|
|
try:
|
|
|
|
s = ssl.wrap_socket(s,
|
2015-10-27 02:44:36 -07:00
|
|
|
ssl_version=ssl.PROTOCOL_SSLv23,
|
2013-09-30 05:01:49 -07:00
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
2013-10-02 00:22:13 -07:00
|
|
|
ca_certs= (temporary_path if is_new else cert_path),
|
2013-09-30 05:01:49 -07:00
|
|
|
do_handshake_on_connect=True)
|
2013-10-01 00:01:46 -07:00
|
|
|
except ssl.SSLError, e:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("SSL error:", e)
|
2013-10-02 01:43:02 -07:00
|
|
|
if e.errno != 1:
|
|
|
|
return
|
2013-10-01 18:33:45 -07:00
|
|
|
if is_new:
|
2013-11-11 07:59:36 -08:00
|
|
|
rej = cert_path + '.rej'
|
|
|
|
if os.path.exists(rej):
|
|
|
|
os.unlink(rej)
|
|
|
|
os.rename(temporary_path, rej)
|
2013-10-02 01:36:29 -07:00
|
|
|
else:
|
2014-06-27 02:36:51 -07:00
|
|
|
with open(cert_path) as f:
|
|
|
|
cert = f.read()
|
|
|
|
try:
|
2015-08-07 02:58:59 -07:00
|
|
|
b = pem.dePem(cert, 'CERTIFICATE')
|
|
|
|
x = x509.X509(b)
|
2014-06-27 02:36:51 -07:00
|
|
|
except:
|
2014-07-29 00:28:27 -07:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("wrong certificate")
|
2014-06-27 02:36:51 -07:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
x.check_date()
|
|
|
|
except:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("certificate has expired:", cert_path)
|
2013-10-02 01:36:29 -07:00
|
|
|
os.unlink(cert_path)
|
2014-06-27 02:36:51 -07:00
|
|
|
return
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("wrong certificate")
|
2013-10-01 00:01:46 -07:00
|
|
|
return
|
2015-02-21 05:45:09 -08:00
|
|
|
except BaseException, e:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error(e)
|
2015-02-21 05:45:09 -08:00
|
|
|
if e.errno == 104:
|
|
|
|
return
|
2014-07-29 00:28:27 -07:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
2013-09-30 05:01:49 -07:00
|
|
|
return
|
|
|
|
|
2013-10-02 00:22:13 -07:00
|
|
|
if is_new:
|
2015-03-12 13:39:05 -07:00
|
|
|
self.print_error("saving certificate")
|
2013-10-02 00:22:13 -07:00
|
|
|
os.rename(temporary_path, cert_path)
|
|
|
|
|
2014-08-22 01:33:13 -07:00
|
|
|
return s
|
2014-10-21 10:05:51 -07:00
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
def run(self):
|
|
|
|
socket = self.get_socket()
|
|
|
|
if socket:
|
|
|
|
self.print_error("connected")
|
|
|
|
self.queue.put((self.server, socket))
|
|
|
|
|
2015-09-06 05:40:00 -07:00
|
|
|
class Interface(util.PrintError):
|
2015-06-02 08:03:33 -07:00
|
|
|
"""The Interface class handles a socket connected to a single remote
|
|
|
|
electrum server. It's exposed API is:
|
|
|
|
|
|
|
|
- Member functions close(), fileno(), get_responses(), has_timed_out(),
|
|
|
|
ping_required(), queue_request(), send_requests()
|
|
|
|
- Member variable server.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, server, socket):
|
|
|
|
self.server = server
|
|
|
|
self.host, _, _ = server.split(':')
|
|
|
|
self.socket = socket
|
|
|
|
|
|
|
|
self.pipe = util.SocketPipe(socket)
|
|
|
|
self.pipe.set_timeout(0.0) # Don't wait for data
|
|
|
|
# Dump network messages. Set at runtime from the console.
|
|
|
|
self.debug = False
|
|
|
|
self.unsent_requests = []
|
|
|
|
self.unanswered_requests = {}
|
|
|
|
# Set last ping to zero to ensure immediate ping
|
|
|
|
self.last_request = time.time()
|
|
|
|
self.last_ping = 0
|
|
|
|
self.closed_remotely = False
|
|
|
|
|
2015-09-06 05:40:00 -07:00
|
|
|
def diagnostic_name(self):
|
|
|
|
return self.host
|
2015-06-02 08:03:33 -07:00
|
|
|
|
|
|
|
def fileno(self):
|
|
|
|
# Needed for select
|
|
|
|
return self.socket.fileno()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if not self.closed_remotely:
|
|
|
|
self.socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
self.socket.close()
|
|
|
|
|
2015-09-09 04:41:08 -07:00
|
|
|
def queue_request(self, *args): # method, params, _id
|
|
|
|
'''Queue a request, later to be send with send_requests when the
|
|
|
|
socket is available for writing.
|
|
|
|
'''
|
2015-05-06 08:25:29 -07:00
|
|
|
self.request_time = time.time()
|
2015-09-09 04:41:08 -07:00
|
|
|
self.unsent_requests.append(args)
|
2015-05-06 07:42:18 -07:00
|
|
|
|
|
|
|
def send_requests(self):
|
2015-06-02 08:03:33 -07:00
|
|
|
'''Sends all queued requests. Returns False on failure.'''
|
2015-09-09 04:41:08 -07:00
|
|
|
make_dict = lambda (m, p, i): {'method': m, 'params': p, 'id': i}
|
|
|
|
wire_requests = map(make_dict, self.unsent_requests)
|
2015-06-02 08:03:33 -07:00
|
|
|
try:
|
2015-09-09 04:41:08 -07:00
|
|
|
self.pipe.send_all(wire_requests)
|
2015-06-02 08:03:33 -07:00
|
|
|
except socket.error, e:
|
|
|
|
self.print_error("socket error:", e)
|
|
|
|
return False
|
2015-09-09 04:41:08 -07:00
|
|
|
for request in self.unsent_requests:
|
|
|
|
if self.debug:
|
|
|
|
self.print_error("-->", request)
|
|
|
|
self.unanswered_requests[request[2]] = request
|
2015-06-02 08:03:33 -07:00
|
|
|
self.unsent_requests = []
|
|
|
|
return True
|
|
|
|
|
|
|
|
def ping_required(self):
|
|
|
|
'''Maintains time since last ping. Returns True if a ping should
|
|
|
|
be sent.
|
|
|
|
'''
|
|
|
|
now = time.time()
|
|
|
|
if now - self.last_ping > 60:
|
|
|
|
self.last_ping = now
|
|
|
|
return True
|
|
|
|
return False
|
2012-10-20 17:57:31 -07:00
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
def has_timed_out(self):
|
|
|
|
'''Returns True if the interface has timed out.'''
|
|
|
|
if (self.unanswered_requests and time.time() - self.request_time > 10
|
|
|
|
and self.pipe.idle_time() > 10):
|
|
|
|
self.print_error("timeout", len(self.unanswered_requests))
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_responses(self):
|
|
|
|
'''Call if there is data available on the socket. Returns a list of
|
2015-09-09 04:41:08 -07:00
|
|
|
(request, response) pairs. Notifications are singleton
|
|
|
|
unsolicited responses presumably as a result of prior
|
|
|
|
subscriptions, so request is None and there is no 'id' member.
|
|
|
|
Otherwise it is a response, which has an 'id' member and a
|
|
|
|
corresponding request. If the connection was closed remotely
|
|
|
|
or the remote server is misbehaving, a (None, None) will appear.
|
2015-06-02 08:03:33 -07:00
|
|
|
'''
|
2015-09-09 04:41:08 -07:00
|
|
|
responses = []
|
2015-06-02 08:03:33 -07:00
|
|
|
while True:
|
2015-05-05 08:16:44 -07:00
|
|
|
try:
|
|
|
|
response = self.pipe.get()
|
|
|
|
except util.timeout:
|
2015-06-02 08:03:33 -07:00
|
|
|
break
|
2015-05-05 08:16:44 -07:00
|
|
|
if response is None:
|
2015-09-09 04:41:08 -07:00
|
|
|
responses.append((None, None))
|
2015-06-02 08:03:33 -07:00
|
|
|
self.closed_remotely = True
|
2015-05-06 16:16:42 -07:00
|
|
|
self.print_error("connection closed remotely")
|
2015-06-02 08:03:33 -07:00
|
|
|
break
|
|
|
|
if self.debug:
|
|
|
|
self.print_error("<--", response)
|
2015-09-09 04:41:08 -07:00
|
|
|
wire_id = response.get('id', None)
|
|
|
|
if wire_id is None: # Notification
|
|
|
|
responses.append((None, response))
|
2015-05-06 16:16:42 -07:00
|
|
|
else:
|
2015-09-09 04:41:08 -07:00
|
|
|
request = self.unanswered_requests.pop(wire_id, None)
|
|
|
|
if request:
|
|
|
|
responses.append((request, response))
|
|
|
|
else:
|
|
|
|
self.print_error("unknown wire ID", wire_id)
|
|
|
|
responses.append(None, None) # Signal
|
|
|
|
break
|
2015-05-05 08:16:44 -07:00
|
|
|
|
2015-09-09 04:41:08 -07:00
|
|
|
return responses
|
2013-09-30 05:01:49 -07:00
|
|
|
|
2014-07-27 15:13:40 -07:00
|
|
|
|
2014-06-27 02:36:51 -07:00
|
|
|
def check_cert(host, cert):
|
|
|
|
try:
|
2015-08-07 02:58:59 -07:00
|
|
|
b = pem.dePem(cert, 'CERTIFICATE')
|
|
|
|
x = x509.X509(b)
|
2014-06-27 02:36:51 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
x.check_date()
|
|
|
|
expired = False
|
|
|
|
except:
|
|
|
|
expired = True
|
|
|
|
|
|
|
|
m = "host: %s\n"%host
|
|
|
|
m += "has_expired: %s\n"% expired
|
2015-03-12 13:39:05 -07:00
|
|
|
util.print_msg(m)
|
2014-06-27 02:36:51 -07:00
|
|
|
|
|
|
|
|
2015-06-02 08:03:33 -07:00
|
|
|
# Used by tests
|
|
|
|
def _match_hostname(name, val):
|
|
|
|
if val == name:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return val.startswith('*.') and name.endswith(val[1:])
|
|
|
|
|
2014-06-27 02:36:51 -07:00
|
|
|
def test_certificates():
|
2015-06-02 08:03:33 -07:00
|
|
|
from simple_config import SimpleConfig
|
2014-06-27 02:36:51 -07:00
|
|
|
config = SimpleConfig()
|
|
|
|
mydir = os.path.join(config.path, "certs")
|
|
|
|
certs = os.listdir(mydir)
|
|
|
|
for c in certs:
|
|
|
|
print c
|
|
|
|
p = os.path.join(mydir,c)
|
|
|
|
with open(p) as f:
|
|
|
|
cert = f.read()
|
|
|
|
check_cert(c, cert)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_certificates()
|