From 1c2a8a2f400fc7f6f83ed19071c932239c352584 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 12 Sep 2018 20:23:50 -0500 Subject: [PATCH] work on forward secrecy --- onionr/onionrusers.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/onionr/onionrusers.py b/onionr/onionrusers.py index 4b74258e..898f9ae4 100644 --- a/onionr/onionrusers.py +++ b/onionr/onionrusers.py @@ -55,26 +55,43 @@ class OnionrUser: return decrypted def forwardEncrypt(self, data): + 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") return def forwardDecrypt(self, encrypted): + retData = '' return def _getLatestForwardKey(self): # Get the latest forward secrecy key for a peer conn = sqlite3.connect(self._core.peerDB) c = conn.cursor() - # Prepare the insert - time = self._core._utils.getEpoch() - key = '' - for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE DATE=(SELECT max(date) FROM forwardKeys);"): + for row in c.execute("SELECT forwardKey FROM forwardKeys WHERE peerKey = ? AND date=(SELECT max(date) FROM forwardKeys)", (self.publicKey,)): key = row[0] break conn.commit() conn.close() return key + + 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) def addForwardKey(self, newKey): if not self._core._utils.validatePubKey(newKey):