fixing up forward secrecy more, added test for it

master
Kevin Froman 2019-02-20 00:09:18 -06:00
parent ee5c620cc6
commit 651e2b173b
2 changed files with 44 additions and 2 deletions

View File

@ -112,7 +112,8 @@ class OnionrUser:
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,)):
# TODO: account for keys created at the same time (same epoch)
for row in c.execute("SELECT forwardKey, max(DATE) FROM forwardKeys WHERE peerKey = ?", (self.publicKey,)):
key = row[0]
break
@ -189,4 +190,4 @@ class OnionrUser:
conn.commit()
conn.close()
return
return True

View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
import sys, os, random
sys.path.append(".")
import unittest, uuid
TEST_DIR_1 = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
TEST_DIR_2 = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
import core, onionr, time
import onionrexceptions
from onionrusers import onionrusers
from onionrusers import contactmanager
class OnionrForwardSecrecyTests(unittest.TestCase):
'''
Tests both the onionrusers class and the contactmanager (which inherits it)
'''
def test_forward_decrypt(self):
os.environ["ONIONR_HOME"] = TEST_DIR_1
o = onionr.Onionr()
friend = o.onionrCore._crypto.generatePubKey()
friendUser = onionrusers.OnionrUser(o.onionrCore, friend[0], saveUser=True)
for x in range(3):
message = 'hello world %s' % (random.randint(1, 1000))
forwardKey = friendUser.generateForwardKey()
fakeForwardPair = o.onionrCore._crypto.generatePubKey()
self.assertTrue(friendUser.addForwardKey(fakeForwardPair[0]))
encrypted = friendUser.forwardEncrypt(message)
decrypted = o.onionrCore._crypto.pubKeyDecrypt(encrypted[0], privkey=fakeForwardPair[1], encodedData=True)
self.assertTrue(decrypted == message.encode())
time.sleep(1)
return
unittest.main()