2018-08-27 03:44:32 +00:00
|
|
|
'''
|
|
|
|
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-08-27 03:44:32 +00:00
|
|
|
class OnionrUser:
|
|
|
|
def __init__(self, coreInst, publicKey):
|
|
|
|
self.trust = 0
|
|
|
|
self._core = coreInst
|
|
|
|
self.publicKey = 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:
|
2018-08-27 03:44:32 +00:00
|
|
|
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
|
2018-08-27 03:44:32 +00:00
|
|
|
|
|
|
|
def encrypt(self, data):
|
|
|
|
encrypted = coreInst._crypto.pubKeyEncrypt(data, self.publicKey, encodedData=True)
|
|
|
|
return encrypted
|
|
|
|
|
2018-09-12 02:58:51 +00:00
|
|
|
def decrypt(self, data, anonymous=True):
|
2018-08-27 03:44:32 +00:00
|
|
|
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):
|
|
|
|
encrypted = self._core._crypto.pubKeyEncrypt(data, forwardKey, encodedData=True)
|
|
|
|
else:
|
|
|
|
raise Exception("No valid forward key available for this user")
|
2018-08-27 03:44:32 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def forwardDecrypt(self, encrypted):
|
2018-09-13 01:23:50 +00:00
|
|
|
retData = ''
|
2018-08-27 03:44:32 +00:00
|
|
|
return
|
2018-09-12 02:58:51 +00:00
|
|
|
|
|
|
|
def _getLatestForwardKey(self):
|
|
|
|
# Get the latest forward secrecy key for a peer
|
|
|
|
conn = sqlite3.connect(self._core.peerDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
|
2018-09-13 01:23:50 +00:00
|
|
|
for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE peerKey = ? AND date=(SELECT max(date) FROM forwardKeys)", (self.publicKey,)):
|
2018-09-12 02:58:51 +00:00
|
|
|
key = row[0]
|
|
|
|
break
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return key
|
2018-09-13 01:23:50 +00:00
|
|
|
|
|
|
|
def _getForwardKeys(self):
|
|
|
|
conn = sqlite3.connect(self._core.peerDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
keyList = []
|
|
|
|
for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE peerKey = ?", (self.publicKey,)):
|
|
|
|
key = row[0]
|
|
|
|
keyList.append(key)
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
return list(keyList)
|
2018-09-12 02:58:51 +00:00
|
|
|
|
2018-09-13 17:26:22 +00:00
|
|
|
def generateForwardKey(self, expire=432000):
|
|
|
|
|
|
|
|
# Generate a forward secrecy key for the peer
|
|
|
|
conn = sqlite3.connect(self._core.forwardKeysFile)
|
|
|
|
c = conn.cursor()
|
|
|
|
# Prepare the insert
|
|
|
|
time = self._core._utils.getEpoch()
|
|
|
|
newKeys = self._core._crypto.generatePubKey()
|
|
|
|
newPub = newKeys[0]
|
|
|
|
newPriv = newKeys[1]
|
|
|
|
|
|
|
|
time = self._core._utils.getEpoch()
|
|
|
|
command = (self.publicKey, newPub, newPriv, time, expire)
|
|
|
|
|
|
|
|
c.execute("INSERT INTO myForwardKeys VALUES(?, ?, ?, ?);", command)
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
2018-09-11 19:45:06 +00:00
|
|
|
def addForwardKey(self, newKey):
|
2018-09-12 02:58:51 +00:00
|
|
|
if not self._core._utils.validatePubKey(newKey):
|
|
|
|
raise onionrexceptions.InvalidPubkey
|
2018-09-11 19:45:06 +00:00
|
|
|
# Add a forward secrecy key for the peer
|
|
|
|
conn = sqlite3.connect(self._core.peerDB)
|
|
|
|
c = conn.cursor()
|
|
|
|
# Prepare the insert
|
|
|
|
time = self._core._utils.getEpoch()
|
|
|
|
command = (self.publicKey, newKey, time)
|
|
|
|
|
|
|
|
c.execute("INSERT INTO forwardKeys VALUES(?, ?, ?);", command)
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
return
|
|
|
|
|
2018-08-29 01:09:27 +00:00
|
|
|
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():
|
2018-08-29 03:02:32 +00:00
|
|
|
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
|