Onionr/onionr/onionrusers.py

204 lines
7.3 KiB
Python
Raw Normal View History

'''
Onionr - P2P Anonymous Storage Network
Contains abstractions for interacting with users of Onionr
'''
'''
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 <https://www.gnu.org/licenses/>.
'''
2018-09-11 19:45:06 +00:00
import onionrblockapi, logger, onionrexceptions, json, sqlite3
2018-10-09 23:36:52 +00:00
import nacl.exceptions
2018-11-09 19:07:26 +00:00
def deleteExpiredKeys(coreInst):
# Fetch the keys we generated for the peer, that are still around
conn = sqlite3.connect(coreInst.forwardKeysFile, timeout=10)
c = conn.cursor()
curTime = coreInst._utils.getEpoch()
c.execute("DELETE from myForwardKeys where expire <= ?", (curTime,))
conn.commit()
conn.execute("VACUUM")
conn.close()
return
class OnionrUser:
2018-12-09 17:29:39 +00:00
def __init__(self, coreInst, publicKey, saveUser=False):
'''
OnionrUser is an abstraction for "users" of the network.
Takes an instance of onionr core, a base32 encoded ed25519 public key, and a bool saveUser
saveUser determines if we should add a user to our peer database or not.
'''
if ' ' in coreInst._utils.bytesToStr(publicKey).strip():
publicKey = coreInst._utils.convertHumanReadableID(publicKey)
self.trust = 0
self._core = coreInst
self.publicKey = publicKey
2018-12-09 17:29:39 +00:00
if saveUser:
self._core.addPeer(publicKey)
self.trust = self._core.getPeerInfo(self.publicKey, 'trust')
return
def setTrust(self, newTrust):
'''Set the peers trust. 0 = not trusted, 1 = friend, 2 = ultimate'''
self._core.setPeerInfo(self.publicKey, 'trust', newTrust)
def isFriend(self):
2018-08-28 04:45:31 +00:00
if self._core.getPeerInfo(self.publicKey, 'trust') == 1:
return True
return False
2018-08-28 04:45:31 +00:00
def getName(self):
retData = 'anonymous'
name = self._core.getPeerInfo(self.publicKey, 'name')
try:
if len(name) > 0:
retData = name
except ValueError:
pass
return retData
def encrypt(self, data):
encrypted = coreInst._crypto.pubKeyEncrypt(data, self.publicKey, encodedData=True)
return encrypted
def decrypt(self, data, anonymous=True):
decrypted = coreInst._crypto.pubKeyDecrypt(data, self.publicKey, encodedData=True)
return decrypted
def forwardEncrypt(self, data):
2018-09-13 01:23:50 +00:00
retData = ''
forwardKey = self._getLatestForwardKey()
if self._core._utils.validatePubKey(forwardKey):
2018-10-08 05:11:46 +00:00
retData = self._core._crypto.pubKeyEncrypt(data, forwardKey, encodedData=True, anonymous=True)
2018-09-13 01:23:50 +00:00
else:
raise onionrexceptions.InvalidPubkey("No valid forward secrecy key available for this user")
2018-10-09 23:36:52 +00:00
#self.generateForwardKey()
2018-10-07 05:06:44 +00:00
return (retData, forwardKey)
def forwardDecrypt(self, encrypted):
2018-10-07 05:06:44 +00:00
retData = ""
2018-11-09 19:07:26 +00:00
for key in self.getGeneratedForwardKeys(False):
2018-10-09 23:36:52 +00:00
try:
retData = self._core._crypto.pubKeyDecrypt(encrypted, privkey=key[1], anonymous=True, encodedData=True)
except nacl.exceptions.CryptoError:
retData = False
else:
2018-10-07 05:06:44 +00:00
break
else:
raise onionrexceptions.DecryptionError("Could not decrypt forward secrecy content")
return retData
def _getLatestForwardKey(self):
# Get the latest forward secrecy key for a peer
2018-10-06 18:06:46 +00:00
key = ""
2018-09-28 17:29:07 +00:00
conn = sqlite3.connect(self._core.peerDB, timeout=10)
c = conn.cursor()
for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE peerKey = ? ORDER BY date DESC", (self.publicKey,)):
key = row[0]
break
conn.commit()
conn.close()
2018-11-17 07:23:10 +00:00
return key
2018-09-13 01:23:50 +00:00
def _getForwardKeys(self):
2018-09-28 17:29:07 +00:00
conn = sqlite3.connect(self._core.peerDB, timeout=10)
2018-09-13 01:23:50 +00:00
c = conn.cursor()
keyList = []
2018-11-17 07:23:10 +00:00
for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE peerKey = ? ORDER BY date DESC", (self.publicKey,)):
2018-09-13 01:23:50 +00:00
key = row[0]
keyList.append(key)
conn.commit()
conn.close()
return list(keyList)
2018-11-09 19:07:26 +00:00
def generateForwardKey(self, expire=604800):
# Generate a forward secrecy key for the peer
2018-09-28 17:29:07 +00:00
conn = sqlite3.connect(self._core.forwardKeysFile, timeout=10)
c = conn.cursor()
# Prepare the insert
time = self._core._utils.getEpoch()
newKeys = self._core._crypto.generatePubKey()
2018-10-09 23:36:52 +00:00
newPub = self._core._utils.bytesToStr(newKeys[0])
newPriv = self._core._utils.bytesToStr(newKeys[1])
time = self._core._utils.getEpoch()
2018-11-09 19:07:26 +00:00
command = (self.publicKey, newPub, newPriv, time, expire + time)
2018-10-07 20:39:22 +00:00
c.execute("INSERT INTO myForwardKeys VALUES(?, ?, ?, ?, ?);", command)
conn.commit()
conn.close()
2018-10-06 18:06:46 +00:00
return newPub
2018-11-09 19:07:26 +00:00
def getGeneratedForwardKeys(self, genNew=True):
2018-10-06 18:06:46 +00:00
# Fetch the keys we generated for the peer, that are still around
2018-10-07 20:39:22 +00:00
conn = sqlite3.connect(self._core.forwardKeysFile, timeout=10)
2018-10-06 18:06:46 +00:00
c = conn.cursor()
2018-10-09 23:36:52 +00:00
pubkey = self.publicKey
pubkey = self._core._utils.bytesToStr(pubkey)
command = (pubkey,)
2018-10-06 18:06:46 +00:00
keyList = [] # list of tuples containing pub, private for peer
2018-11-17 07:23:10 +00:00
for result in c.execute("SELECT * FROM myForwardKeys WHERE peer = ?", command):
2018-10-06 18:06:46 +00:00
keyList.append((result[1], result[2]))
2018-11-17 07:23:10 +00:00
2018-10-09 23:36:52 +00:00
if len(keyList) == 0:
2018-11-09 19:07:26 +00:00
if genNew:
self.generateForwardKey()
keyList = self.getGeneratedForwardKeys()
2018-10-09 23:36:52 +00:00
return list(keyList)
2018-11-09 19:07:26 +00:00
def addForwardKey(self, newKey, expire=604800):
if not self._core._utils.validatePubKey(newKey):
raise onionrexceptions.InvalidPubkey(newKey)
if newKey in self._getForwardKeys():
return False
2018-09-11 19:45:06 +00:00
# Add a forward secrecy key for the peer
2018-09-28 17:29:07 +00:00
conn = sqlite3.connect(self._core.peerDB, timeout=10)
2018-09-11 19:45:06 +00:00
c = conn.cursor()
# Prepare the insert
time = self._core._utils.getEpoch()
2018-11-09 19:07:26 +00:00
command = (self.publicKey, newKey, time, time + expire)
2018-09-11 19:45:06 +00:00
2018-10-07 20:39:22 +00:00
c.execute("INSERT INTO forwardKeys VALUES(?, ?, ?, ?);", command)
2018-09-11 19:45:06 +00:00
conn.commit()
conn.close()
return
def findAndSetID(self):
'''Find any info about the user from existing blocks and cache it to their DB entry'''
infoBlocks = []
for bHash in self._core.getBlocksByType('userInfo'):
block = onionrblockapi.Block(bHash, core=self._core)
if block.signer == self.publicKey:
if block.verifySig():
newName = block.getMetadata('name')
if newName.isalnum():
logger.info('%s is now using the name %s.' % (self.publicKey, self._core._utils.escapeAnsi(newName)))
self._core.setPeerInfo(self.publicKey, 'name', newName)
else:
raise onionrexceptions.InvalidPubkey