2022-08-31 05:30:28 +00:00
|
|
|
import os, uuid
|
|
|
|
from random import randint
|
|
|
|
from time import sleep
|
2022-09-14 17:35:20 +00:00
|
|
|
from nacl.signing import SigningKey, VerifyKey
|
|
|
|
import nacl
|
2022-08-31 05:30:28 +00:00
|
|
|
import secrets
|
2022-09-15 06:27:46 +00:00
|
|
|
from enum import IntEnum, auto
|
2022-08-31 05:30:28 +00:00
|
|
|
import onionrblocks
|
|
|
|
|
|
|
|
|
|
|
|
TEST_DIR = 'testdata/%s-%s' % (str(uuid.uuid4())[:6], os.path.basename(__file__)) + '/'
|
|
|
|
print("Test directory:", TEST_DIR)
|
|
|
|
os.environ["ONIONR_HOME"] = TEST_DIR
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import sys
|
|
|
|
sys.path.append(".")
|
2022-09-26 20:06:05 +00:00
|
|
|
sys.path.append('static-data/official-plugins/wot/wot')
|
2022-08-31 05:30:28 +00:00
|
|
|
sys.path.append("src/")
|
2022-09-14 17:35:20 +00:00
|
|
|
import identity
|
2022-09-16 01:03:29 +00:00
|
|
|
from identity import identities
|
2022-09-14 17:35:20 +00:00
|
|
|
|
2022-08-31 05:30:28 +00:00
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
class WotCommand(IntEnum):
|
|
|
|
TRUST = 1
|
|
|
|
REVOKE_TRUST = auto()
|
|
|
|
ANNOUNCE = auto()
|
|
|
|
REVOKE = auto()
|
|
|
|
|
2022-09-14 17:35:20 +00:00
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
class TrustSignatureProcessing(unittest.TestCase):
|
2022-09-14 17:35:20 +00:00
|
|
|
def test_processing_trust_payload_without_announced_identity(self):
|
2022-09-14 02:40:39 +00:00
|
|
|
# reset identity set
|
2022-09-14 17:35:20 +00:00
|
|
|
identities.clear()
|
2022-09-14 02:40:39 +00:00
|
|
|
|
|
|
|
fake_pubkey = secrets.token_bytes(32)
|
|
|
|
signing_key = SigningKey.generate()
|
|
|
|
|
2022-09-14 17:35:20 +00:00
|
|
|
identities.add(identity.Identity(signing_key.verify_key, "test"))
|
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
wot_cmd = int(WotCommand.TRUST).to_bytes(1, 'big')
|
|
|
|
|
|
|
|
trust_signature = signing_key.sign(wot_cmd + fake_pubkey)
|
|
|
|
trust_signature_payload = wot_cmd + \
|
|
|
|
bytes(signing_key.verify_key) + fake_pubkey + \
|
2022-09-14 17:35:20 +00:00
|
|
|
trust_signature.signature
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-09-14 17:35:20 +00:00
|
|
|
for iden in identities:
|
|
|
|
if iden.key == signing_key.verify_key:
|
|
|
|
for i in iden.trusted:
|
|
|
|
if i.key == VerifyKey(fake_pubkey):
|
|
|
|
raise AssertionError("Signed identity found")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError("Signing identity not found")
|
|
|
|
|
|
|
|
def test_processing_invalid_trust_payloads(self):
|
|
|
|
# reset identity set
|
|
|
|
identities.clear()
|
|
|
|
|
|
|
|
fake_pubkey = secrets.token_bytes(32)
|
|
|
|
signing_key = SigningKey.generate()
|
|
|
|
|
|
|
|
identities.add(identity.Identity(signing_key.verify_key, "test"))
|
|
|
|
identities.add(identity.Identity(VerifyKey(fake_pubkey), "test2"))
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
wot_cmd = int(WotCommand.TRUST).to_bytes(1, 'big')
|
|
|
|
|
|
|
|
trust_signature = signing_key.sign(wot_cmd + fake_pubkey)
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
trust_signature = bytearray(trust_signature.signature)
|
|
|
|
trust_signature[34] = 0
|
|
|
|
trust_signature = bytes(trust_signature)
|
|
|
|
trust_signature_payload = wot_cmd + bytes(signing_key.verify_key) + fake_pubkey + \
|
|
|
|
trust_signature
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-09-14 17:35:20 +00:00
|
|
|
self.assertRaises(
|
|
|
|
nacl.exceptions.BadSignatureError, identity.process_trust_signature, trust_signature_payload)
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-09-14 17:35:20 +00:00
|
|
|
for iden in identities:
|
2022-09-14 02:40:39 +00:00
|
|
|
if iden.key == signing_key.verify_key:
|
2022-09-14 17:35:20 +00:00
|
|
|
for i in iden.trusted:
|
|
|
|
if i.key == VerifyKey(fake_pubkey):
|
|
|
|
raise AssertionError("Signed identity found")
|
2022-09-14 02:40:39 +00:00
|
|
|
break
|
2022-09-14 17:35:20 +00:00
|
|
|
else:
|
|
|
|
raise AssertionError("Signing identity not found")
|
|
|
|
|
|
|
|
def test_processing_trust_payloads(self):
|
|
|
|
# reset identity set
|
|
|
|
identities.clear()
|
|
|
|
|
|
|
|
fake_pubkey = secrets.token_bytes(32)
|
|
|
|
signing_key = SigningKey.generate()
|
|
|
|
|
|
|
|
identities.add(identity.Identity(signing_key.verify_key, "test"))
|
|
|
|
identities.add(identity.Identity(VerifyKey(fake_pubkey), "test2"))
|
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
wot_cmd = int(WotCommand.TRUST).to_bytes(1, 'big')
|
2022-09-14 17:35:20 +00:00
|
|
|
|
2022-09-15 06:27:46 +00:00
|
|
|
trust_signature = signing_key.sign(wot_cmd + fake_pubkey)
|
|
|
|
trust_signature_payload = wot_cmd + bytes(signing_key.verify_key) + fake_pubkey + \
|
2022-09-14 17:35:20 +00:00
|
|
|
trust_signature.signature
|
|
|
|
|
|
|
|
identity.process_trust_signature(trust_signature_payload)
|
|
|
|
|
|
|
|
for iden in identities:
|
|
|
|
if iden.key == signing_key.verify_key:
|
|
|
|
|
|
|
|
for i in iden.trusted:
|
|
|
|
if i.key == VerifyKey(fake_pubkey):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError("Signed identity not found")
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise AssertionError("Signing identity not found")
|
2022-09-14 02:40:39 +00:00
|
|
|
|
2022-08-31 05:30:28 +00:00
|
|
|
|
|
|
|
unittest.main()
|
2022-09-14 17:35:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|