diff --git a/onionr/coredb/keydb/addkeys.py b/onionr/coredb/keydb/addkeys.py index d54a4f7b..cee465c6 100644 --- a/onionr/coredb/keydb/addkeys.py +++ b/onionr/coredb/keydb/addkeys.py @@ -23,11 +23,12 @@ from onionrutils import stringvalidators from . import listkeys from utils import gettransports from .. import dbfiles +import onionrcrypto def add_peer(peerID, name=''): ''' Adds a public key to the key database (misleading function name) ''' - if peerID in listkeys.list_peers() or peerID == core_inst._crypto.pubKey: + if peerID in listkeys.list_peers() or peerID == onionrcrypto.pub_key: raise ValueError("specified id is already known") # This function simply adds a peer to the DB @@ -37,7 +38,7 @@ def add_peer(peerID, name=''): #events.event('pubkey_add', data = {'key': peerID}, onionr = core_inst.onionrInst) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) - hashID = core_inst._crypto.pubKeyHashID(peerID) + hashID = "" c = conn.cursor() t = (peerID, name, 'unknown', hashID, 0) diff --git a/onionr/httpapi/insertblock.py b/onionr/httpapi/insertblock.py index bdc29f72..21ed1d42 100644 --- a/onionr/httpapi/insertblock.py +++ b/onionr/httpapi/insertblock.py @@ -57,5 +57,5 @@ def client_api_insert_block(): meta = json.loads(bData['meta']) except KeyError: pass - threading.Thread(target=onionrblocks.insert.insert_block, args=(message,), kwargs={'header': bType, 'encryptType': encryptType, 'sign':sign, 'asymPeer': to, 'meta': meta}).start() + threading.Thread(target=onionrblocks.insert, args=(message,), kwargs={'header': bType, 'encryptType': encryptType, 'sign':sign, 'asymPeer': to, 'meta': meta}).start() return Response('success') \ No newline at end of file diff --git a/onionr/onionrblocks/insert.py b/onionr/onionrblocks/insert.py index af49f8e8..330dee42 100644 --- a/onionr/onionrblocks/insert.py +++ b/onionr/onionrblocks/insert.py @@ -5,7 +5,7 @@ import onionrevents as events from etc import powchoice, onionrvalues import config, onionrcrypto as crypto, subprocesspow, onionrexceptions from onionrusers import onionrusers -from onionrutils import localcommand, blockmetadata +from onionrutils import localcommand, blockmetadata, stringvalidators import coredb def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asymPeer='', meta = {}, expire=None, disableForward=False): ''' @@ -66,7 +66,7 @@ def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asym if encryptType == 'asym': meta['rply'] = createTime # Duplicate the time in encrypted messages to prevent replays - if not disableForward and sign and asymPeer != crypto.pubKey: + if not disableForward and sign and asymPeer != crypto.pub_key: try: forwardEncrypted = onionrusers.OnionrUser(asymPeer).forwardEncrypt(data) data = forwardEncrypted[0] @@ -82,7 +82,7 @@ def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asym plaintextMeta = jsonMeta if sign: signature = crypto.signing.ed_sign(jsonMeta.encode() + data, key=crypto.priv_key, encodeResult=True) - signer = crypto.pubKey + signer = crypto.pub_key if len(jsonMeta) > 1000: raise onionrexceptions.InvalidMetadata('meta in json encoded form must not exceed 1000 bytes') @@ -96,8 +96,8 @@ def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asym jsonMeta = json.dumps(meta) jsonMeta = crypto.encryption.pub_key_encrypt(jsonMeta, asymPeer, encodedData=True).decode() data = crypto.encryption.pub_key_encrypt(data, asymPeer, encodedData=True).decode() - signature = crypto.pub_key_encrypt(signature, asymPeer, encodedData=True).decode() - signer = crypto.pub_key_encrypt(signer, asymPeer, encodedData=True).decode() + signature = crypto.encryption.pub_key_encrypt(signature, asymPeer, encodedData=True).decode() + signer = crypto.encryption.pub_key_encrypt(signer, asymPeer, encodedData=True).decode() try: onionrusers.OnionrUser(asymPeer, saveUser=True) except ValueError: diff --git a/onionr/onionrcrypto/__init__.py b/onionr/onionrcrypto/__init__.py index d650d647..573faa2c 100755 --- a/onionr/onionrcrypto/__init__.py +++ b/onionr/onionrcrypto/__init__.py @@ -19,8 +19,8 @@ ''' from . import generate, hashers, getourkeypair, signing, encryption - +generate = generate.generate_pub_key keypair = getourkeypair.get_keypair() pub_key = keypair[0] -priv_key = keypair[1] +priv_key = keypair[1] \ No newline at end of file diff --git a/onionr/onionrcrypto/cryptoutils/replayvalidation.py b/onionr/onionrcrypto/cryptoutils/replayvalidation.py index fbb581f3..59c338eb 100644 --- a/onionr/onionrcrypto/cryptoutils/replayvalidation.py +++ b/onionr/onionrcrypto/cryptoutils/replayvalidation.py @@ -1,6 +1,6 @@ -import utils # onionr utils epoch, not this utils +from onionrutils import epoch def replay_timestamp_validation(timestamp): - if utils.epoch.get_epoch() - int(timestamp) > 2419200: + if epoch.get_epoch() - int(timestamp) > 2419200: return False else: return True \ No newline at end of file diff --git a/onionr/onionrcrypto/encryption/__init__.py b/onionr/onionrcrypto/encryption/__init__.py index d334bc78..f9d1664b 100644 --- a/onionr/onionrcrypto/encryption/__init__.py +++ b/onionr/onionrcrypto/encryption/__init__.py @@ -1,6 +1,7 @@ import nacl.encoding, nacl.public, nacl.signing from .. import getourkeypair import unpaddedbase32 +from onionrutils import bytesconverter, stringvalidators pair = getourkeypair.get_keypair() our_pub_key = pair[0] our_priv_key = pair[1] diff --git a/onionr/onionrusers/onionrusers.py b/onionr/onionrusers/onionrusers.py index 1df6e75d..51798c78 100755 --- a/onionr/onionrusers/onionrusers.py +++ b/onionr/onionrusers/onionrusers.py @@ -22,6 +22,7 @@ from onionrutils import stringvalidators, bytesconverter, epoch import unpaddedbase32 import nacl.exceptions from coredb import keydb, dbfiles +import onionrcrypto def deleteExpiredKeys(): # Fetch the keys we generated for the peer, that are still around @@ -48,18 +49,16 @@ def deleteTheirExpiredKeys(pubkey): conn.close() DEFAULT_KEY_EXPIRE = 604800 -#DEFAULT_KEY_EXPIRE = 600 class OnionrUser: - def __init__(self, crypto_inst, publicKey, saveUser=False): + def __init__(self, publicKey, saveUser=False): ''' OnionrUser is an abstraction for "users" of the network. Takes a base32 encoded ed25519 public key, and a bool saveUser saveUser determines if we should add a user to our peer database or not. ''' - self.crypto = crypto_inst publicKey = unpaddedbase32.repad(bytesconverter.str_to_bytes(publicKey)).decode() self.trust = 0 @@ -94,11 +93,11 @@ class OnionrUser: return retData def encrypt(self, data): - encrypted = self.crypto.pubKeyEncrypt(data, self.publicKey, encodedData=True) + encrypted = onionrcrypto.encryption.pub_key_encrypt(data, self.publicKey, encodedData=True) return encrypted def decrypt(self, data): - decrypted = self.crypto.pubKeyDecrypt(data, self.publicKey, encodedData=True) + decrypted = onionrcrypto.encryption.pub_key_decrypt(data, self.publicKey, encodedData=True) return decrypted def forwardEncrypt(self, data): @@ -107,7 +106,7 @@ class OnionrUser: retData = '' forwardKey = self._getLatestForwardKey() if stringvalidators.validate_pub_key(forwardKey[0]): - retData = self.crypto.pubKeyEncrypt(data, forwardKey[0], encodedData=True) + retData = onionrcrypto.encryption.pub_key_encrypt(data, forwardKey[0], encodedData=True) else: raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user") #self.generateForwardKey() @@ -117,7 +116,7 @@ class OnionrUser: retData = "" for key in self.getGeneratedForwardKeys(False): try: - retData = self.crypto.pubKeyDecrypt(encrypted, privkey=key[1], encodedData=True) + retData = onionrcrypto.encryption.pub_key_decrypt(encrypted, privkey=key[1], encodedData=True) except nacl.exceptions.CryptoError: retData = False else: @@ -162,7 +161,7 @@ class OnionrUser: c = conn.cursor() # Prepare the insert time = epoch.get_epoch() - newKeys = self.crypto.generatePubKey() + newKeys = onionrcrypto.generate() newPub = bytesconverter.bytes_to_str(newKeys[0]) newPriv = bytesconverter.bytes_to_str(newKeys[1]) diff --git a/onionr/onionrutils/basicrequests.py b/onionr/onionrutils/basicrequests.py index f1a563f4..f0415200 100644 --- a/onionr/onionrutils/basicrequests.py +++ b/onionr/onionrutils/basicrequests.py @@ -44,11 +44,15 @@ def do_post_request(onionr_inst, url, data={}, port=0, proxyType='tor', max_size retData = False return retData -def do_get_request(onionr_inst, url, port=0, proxyType='tor', ignoreAPI=False, returnHeaders=False, max_size=5242880): +def do_get_request(url, port=0, proxyType='tor', ignoreAPI=False, returnHeaders=False, max_size=5242880, onionr_inst=None): ''' Do a get request through a local tor or i2p instance ''' - API_VERSION = onionr_inst.API_VERSION + if not ignoreAPI: + if onionr_inst is None: + raise ValueError("Onionr inst must be set if ignoreAPI is False") + else: + API_VERSION = onionr_inst.API_VERSION retData = False if proxyType == 'tor': if port == 0: