progress in removing core

This commit is contained in:
Kevin Froman 2019-07-21 11:15:20 -05:00
parent 50a8e8958a
commit 4025983ca7
8 changed files with 27 additions and 22 deletions

View File

@ -23,11 +23,12 @@ from onionrutils import stringvalidators
from . import listkeys from . import listkeys
from utils import gettransports from utils import gettransports
from .. import dbfiles from .. import dbfiles
import onionrcrypto
def add_peer(peerID, name=''): def add_peer(peerID, name=''):
''' '''
Adds a public key to the key database (misleading function 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") raise ValueError("specified id is already known")
# This function simply adds a peer to the DB # 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) #events.event('pubkey_add', data = {'key': peerID}, onionr = core_inst.onionrInst)
conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30) conn = sqlite3.connect(dbfiles.user_id_info_db, timeout=30)
hashID = core_inst._crypto.pubKeyHashID(peerID) hashID = ""
c = conn.cursor() c = conn.cursor()
t = (peerID, name, 'unknown', hashID, 0) t = (peerID, name, 'unknown', hashID, 0)

View File

@ -57,5 +57,5 @@ def client_api_insert_block():
meta = json.loads(bData['meta']) meta = json.loads(bData['meta'])
except KeyError: except KeyError:
pass 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') return Response('success')

View File

@ -5,7 +5,7 @@ import onionrevents as events
from etc import powchoice, onionrvalues from etc import powchoice, onionrvalues
import config, onionrcrypto as crypto, subprocesspow, onionrexceptions import config, onionrcrypto as crypto, subprocesspow, onionrexceptions
from onionrusers import onionrusers from onionrusers import onionrusers
from onionrutils import localcommand, blockmetadata from onionrutils import localcommand, blockmetadata, stringvalidators
import coredb import coredb
def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asymPeer='', meta = {}, expire=None, disableForward=False): 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': if encryptType == 'asym':
meta['rply'] = createTime # Duplicate the time in encrypted messages to prevent replays 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: try:
forwardEncrypted = onionrusers.OnionrUser(asymPeer).forwardEncrypt(data) forwardEncrypted = onionrusers.OnionrUser(asymPeer).forwardEncrypt(data)
data = forwardEncrypted[0] data = forwardEncrypted[0]
@ -82,7 +82,7 @@ def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asym
plaintextMeta = jsonMeta plaintextMeta = jsonMeta
if sign: if sign:
signature = crypto.signing.ed_sign(jsonMeta.encode() + data, key=crypto.priv_key, encodeResult=True) 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: if len(jsonMeta) > 1000:
raise onionrexceptions.InvalidMetadata('meta in json encoded form must not exceed 1000 bytes') 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 = json.dumps(meta)
jsonMeta = crypto.encryption.pub_key_encrypt(jsonMeta, asymPeer, encodedData=True).decode() jsonMeta = crypto.encryption.pub_key_encrypt(jsonMeta, asymPeer, encodedData=True).decode()
data = crypto.encryption.pub_key_encrypt(data, 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() signature = crypto.encryption.pub_key_encrypt(signature, asymPeer, encodedData=True).decode()
signer = crypto.pub_key_encrypt(signer, asymPeer, encodedData=True).decode() signer = crypto.encryption.pub_key_encrypt(signer, asymPeer, encodedData=True).decode()
try: try:
onionrusers.OnionrUser(asymPeer, saveUser=True) onionrusers.OnionrUser(asymPeer, saveUser=True)
except ValueError: except ValueError:

View File

@ -19,8 +19,8 @@
''' '''
from . import generate, hashers, getourkeypair, signing, encryption from . import generate, hashers, getourkeypair, signing, encryption
generate = generate.generate_pub_key
keypair = getourkeypair.get_keypair() keypair = getourkeypair.get_keypair()
pub_key = keypair[0] pub_key = keypair[0]
priv_key = keypair[1] priv_key = keypair[1]

View File

@ -1,6 +1,6 @@
import utils # onionr utils epoch, not this utils from onionrutils import epoch
def replay_timestamp_validation(timestamp): def replay_timestamp_validation(timestamp):
if utils.epoch.get_epoch() - int(timestamp) > 2419200: if epoch.get_epoch() - int(timestamp) > 2419200:
return False return False
else: else:
return True return True

View File

@ -1,6 +1,7 @@
import nacl.encoding, nacl.public, nacl.signing import nacl.encoding, nacl.public, nacl.signing
from .. import getourkeypair from .. import getourkeypair
import unpaddedbase32 import unpaddedbase32
from onionrutils import bytesconverter, stringvalidators
pair = getourkeypair.get_keypair() pair = getourkeypair.get_keypair()
our_pub_key = pair[0] our_pub_key = pair[0]
our_priv_key = pair[1] our_priv_key = pair[1]

View File

@ -22,6 +22,7 @@ from onionrutils import stringvalidators, bytesconverter, epoch
import unpaddedbase32 import unpaddedbase32
import nacl.exceptions import nacl.exceptions
from coredb import keydb, dbfiles from coredb import keydb, dbfiles
import onionrcrypto
def deleteExpiredKeys(): def deleteExpiredKeys():
# Fetch the keys we generated for the peer, that are still around # Fetch the keys we generated for the peer, that are still around
@ -48,18 +49,16 @@ def deleteTheirExpiredKeys(pubkey):
conn.close() conn.close()
DEFAULT_KEY_EXPIRE = 604800 DEFAULT_KEY_EXPIRE = 604800
#DEFAULT_KEY_EXPIRE = 600
class OnionrUser: 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. OnionrUser is an abstraction for "users" of the network.
Takes a base32 encoded ed25519 public key, and a bool saveUser 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. 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() publicKey = unpaddedbase32.repad(bytesconverter.str_to_bytes(publicKey)).decode()
self.trust = 0 self.trust = 0
@ -94,11 +93,11 @@ class OnionrUser:
return retData return retData
def encrypt(self, data): 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 return encrypted
def decrypt(self, data): 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 return decrypted
def forwardEncrypt(self, data): def forwardEncrypt(self, data):
@ -107,7 +106,7 @@ class OnionrUser:
retData = '' retData = ''
forwardKey = self._getLatestForwardKey() forwardKey = self._getLatestForwardKey()
if stringvalidators.validate_pub_key(forwardKey[0]): 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: else:
raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user") raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user")
#self.generateForwardKey() #self.generateForwardKey()
@ -117,7 +116,7 @@ class OnionrUser:
retData = "" retData = ""
for key in self.getGeneratedForwardKeys(False): for key in self.getGeneratedForwardKeys(False):
try: 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: except nacl.exceptions.CryptoError:
retData = False retData = False
else: else:
@ -162,7 +161,7 @@ class OnionrUser:
c = conn.cursor() c = conn.cursor()
# Prepare the insert # Prepare the insert
time = epoch.get_epoch() time = epoch.get_epoch()
newKeys = self.crypto.generatePubKey() newKeys = onionrcrypto.generate()
newPub = bytesconverter.bytes_to_str(newKeys[0]) newPub = bytesconverter.bytes_to_str(newKeys[0])
newPriv = bytesconverter.bytes_to_str(newKeys[1]) newPriv = bytesconverter.bytes_to_str(newKeys[1])

View File

@ -44,11 +44,15 @@ def do_post_request(onionr_inst, url, data={}, port=0, proxyType='tor', max_size
retData = False retData = False
return retData 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 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 retData = False
if proxyType == 'tor': if proxyType == 'tor':
if port == 0: if port == 0: