From 38bfee5344f48532aa2e112a899b347b7be72c83 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 21 Feb 2018 03:32:31 -0600 Subject: [PATCH] work on seperating pubkey from tor/i2p --- onionr/core.py | 53 +++++++++++++++++++++++++++---------------- onionr/onionrutils.py | 20 ++++++++++------ onionr/tests.py | 2 +- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/onionr/core.py b/onionr/core.py index 29d84a55..8e72520d 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -40,6 +40,7 @@ class Core: self.peerDB = 'data/peers.db' self.blockDB = 'data/blocks.db' self.blockDataLocation = 'data/blocks/' + self.addressDB = 'data/address.db' self._utils = onionrutils.OnionrUtils(self) # Initialize the crypto object @@ -61,7 +62,7 @@ class Core: DOES NO SAFETY CHECKS if the ID is valid, but prepares the insertion ''' # This function simply adds a peer to the DB - if not self._utils.validateID(peerID): + if not self._utils.validatePubKey(peerID): return False conn = sqlite3.connect(self.peerDB) c = conn.cursor() @@ -70,6 +71,29 @@ class Core: conn.commit() conn.close() return True + + def createAddressDB(self): + ''' + Generate the address database + + types: + 1: I2P b32 address + 2: Tor v2 (like facebookcorewwwi.onion) + 3: Tor v3 + ''' + conn = sqlite3.connect(self.addressDB) + c = conn.cursor() + c.execute('''CREATE TABLE adders( + address text, + type int, + knownPeer text, + speed int, + success int, + failure int + ); + ''') + conn.commit() + conn.close() def createPeerDB(self): ''' @@ -81,7 +105,7 @@ class Core: c.execute('''CREATE TABLE peers( ID text not null, name text, - pubkey text, + adders text, blockDBHash text, forwardKey text, dateSeen not null, @@ -90,7 +114,6 @@ class Core: ''') conn.commit() conn.close() - return def createBlockDB(self): @@ -278,14 +301,6 @@ class Core: return - def generateHMAC(self, length=32): - ''' - Generate and return an HMAC key - ''' - key = base64.b64encode(os.urandom(length)) - - return key - def listPeers(self, randomOrder=True): ''' Return a list of peers @@ -300,7 +315,7 @@ class Core: peers = c.execute('SELECT * FROM peers;') peerList = [] for i in peers: - peerList.append(i[0]) + peerList.append(i[2]) conn.close() return peerList @@ -311,17 +326,17 @@ class Core: id text 0 name text, 1 - hmacKey text, 3 - blockDBHash text, 4 - forwardKey text, 5 - dateSeen not null, 7 - bytesStored int, 8 - trust int 9 + adders text, 2 + blockDBHash text, 3 + forwardKey text, 4 + dateSeen not null, 5 + bytesStored int, 6 + trust int 7 ''' conn = sqlite3.connect(self.peerDB) c = conn.cursor() command = (peer,) - infoNumbers = {'id': 0, 'name': 1, 'hmacKey': 3, 'blockDBHash': 4, 'forwardKey': 5, 'dateSeen': 6, 'bytesStored': 7, 'trust': 8} + infoNumbers = {'id': 0, 'name': 1, 'adders': 2, 'blockDBHash': 3, 'forwardKey': 4, 'dateSeen': 5, 'bytesStored': 6, 'trust': 7} info = infoNumbers[info] iterCount = 0 retVal = '' diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index ba082b98..ab57a288 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -19,6 +19,7 @@ ''' # Misc functions that do not fit in the main api, but are useful import getpass, sys, requests, configparser, os, socket, hashlib, logger, sqlite3 +import nacl.signing, nacl.encoding if sys.version_info < (3, 6): try: import sha3 @@ -140,10 +141,20 @@ class OnionrUtils: retVal = False return retVal + + def validatePubKey(self, key): + '''Validate if a string is a valid base32 encoded Ed25519 key''' + retVal = False + try: + nacl.signing.SigningKey(self, seed=key, encoder=nacl.encoding.Base32Encoder) + except nacl.exceptions.ValueError: + pass + return retVal + def validateID(self, id): ''' - Validate if a user ID is a valid tor or i2p hidden service + Validate if an address is a valid tor or i2p hidden service ''' idLength = len(id) retVal = True @@ -183,9 +194,4 @@ class OnionrUtils: if not idNoDomain.isalnum(): retVal = False - return retVal - - def sendPM(self, peer, message): - '''Send an encrypted private message to a user''' - - return + return retVal \ No newline at end of file diff --git a/onionr/tests.py b/onionr/tests.py index 557e8885..5728055c 100755 --- a/onionr/tests.py +++ b/onionr/tests.py @@ -54,7 +54,7 @@ class OnionrTests(unittest.TestCase): myCore = core.Core() if not os.path.exists('data/peers.db'): myCore.createPeerDB() - if myCore.addPeer('2ks5c5bm6zk3ejqg.onion') and not myCore.addPeer('invalidpeer.onion'): + if myCore.addPeer('6M5MXL237OK57ITHVYN5WGHANPGOMKS5C3PJLHBBNKFFJQOIDOJA====') and not myCore.addPeer('NFXHMYLMNFSAU==='): self.assertTrue(True) else: self.assertTrue(False)