2016-05-29 16:20:44 -04:00
|
|
|
import base64
|
2018-03-16 05:50:37 -04:00
|
|
|
import math
|
2018-04-04 08:14:22 -04:00
|
|
|
import RNS
|
2018-03-19 11:39:08 -04:00
|
|
|
|
2016-05-29 16:20:44 -04:00
|
|
|
from cryptography.fernet import Fernet
|
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
from cryptography.hazmat.backends import default_backend
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import rsa
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding
|
|
|
|
|
2018-04-16 11:13:39 -04:00
|
|
|
class Callbacks:
|
|
|
|
def __init__(self):
|
|
|
|
self.link_established = None
|
|
|
|
self.packet = None
|
2018-04-17 11:46:48 -04:00
|
|
|
self.proof_requested = None
|
2018-03-19 11:39:08 -04:00
|
|
|
|
2016-05-29 16:20:44 -04:00
|
|
|
class Destination:
|
2018-04-04 08:14:22 -04:00
|
|
|
KEYSIZE = RNS.Identity.KEYSIZE;
|
|
|
|
PADDINGSIZE= RNS.Identity.PADDINGSIZE;
|
2018-03-16 05:50:37 -04:00
|
|
|
|
|
|
|
# Constants
|
2018-03-20 07:32:41 -04:00
|
|
|
SINGLE = 0x00
|
|
|
|
GROUP = 0x01
|
|
|
|
PLAIN = 0x02
|
|
|
|
LINK = 0x03
|
2018-03-19 11:39:08 -04:00
|
|
|
types = [SINGLE, GROUP, PLAIN, LINK]
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-03-20 07:32:41 -04:00
|
|
|
PROVE_NONE = 0x21
|
|
|
|
PROVE_APP = 0x22
|
|
|
|
PROVE_ALL = 0x23
|
|
|
|
proof_strategies = [PROVE_NONE, PROVE_APP, PROVE_ALL]
|
|
|
|
|
2016-05-29 16:20:44 -04:00
|
|
|
IN = 0x11;
|
|
|
|
OUT = 0x12;
|
|
|
|
directions = [IN, OUT]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getDestinationName(app_name, *aspects):
|
|
|
|
# Check input values and build name string
|
|
|
|
if "." in app_name: raise ValueError("Dots can't be used in app names")
|
|
|
|
|
|
|
|
name = app_name
|
|
|
|
for aspect in aspects:
|
|
|
|
if "." in aspect: raise ValueError("Dots can't be used in aspects")
|
|
|
|
name = name + "." + aspect
|
|
|
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def getDestinationHash(app_name, *aspects):
|
|
|
|
name = Destination.getDestinationName(app_name, *aspects)
|
|
|
|
|
|
|
|
# Create a digest for the destination
|
|
|
|
digest = hashes.Hash(hashes.SHA256(), backend=default_backend())
|
|
|
|
digest.update(name)
|
|
|
|
|
|
|
|
return digest.finalize()[:10]
|
|
|
|
|
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
def __init__(self, identity, direction, type, app_name, *aspects):
|
2016-05-29 16:20:44 -04:00
|
|
|
# Check input values and build name string
|
|
|
|
if "." in app_name: raise ValueError("Dots can't be used in app names")
|
|
|
|
if not type in Destination.types: raise ValueError("Unknown destination type")
|
|
|
|
if not direction in Destination.directions: raise ValueError("Unknown destination direction")
|
2018-04-16 11:13:39 -04:00
|
|
|
self.callbacks = Callbacks()
|
2016-05-29 16:20:44 -04:00
|
|
|
self.type = type
|
|
|
|
self.direction = direction
|
2018-03-20 07:32:41 -04:00
|
|
|
self.proof_strategy = Destination.PROVE_NONE
|
2016-06-03 13:02:02 -04:00
|
|
|
self.mtu = 0
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-04-16 11:13:39 -04:00
|
|
|
self.links = []
|
|
|
|
|
2018-03-20 07:32:41 -04:00
|
|
|
if identity != None and type == Destination.SINGLE:
|
|
|
|
aspects = aspects+(identity.hexhash,)
|
|
|
|
|
2020-03-06 06:55:05 -05:00
|
|
|
if identity == None and direction == Destination.IN and self.type != Destination.PLAIN:
|
|
|
|
identity = RNS.Identity()
|
2018-03-20 07:32:41 -04:00
|
|
|
aspects = aspects+(identity.hexhash,)
|
2018-03-16 06:40:37 -04:00
|
|
|
|
|
|
|
self.identity = identity
|
|
|
|
|
2016-05-29 16:20:44 -04:00
|
|
|
self.name = Destination.getDestinationName(app_name, *aspects)
|
|
|
|
self.hash = Destination.getDestinationHash(app_name, *aspects)
|
|
|
|
self.hexhash = self.hash.encode("hex_codec")
|
|
|
|
|
|
|
|
self.callback = None
|
2018-03-20 07:32:41 -04:00
|
|
|
self.proofcallback = None
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-04-04 08:14:22 -04:00
|
|
|
RNS.Transport.registerDestination(self)
|
2016-06-03 13:02:02 -04:00
|
|
|
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<"+self.name+"/"+self.hexhash+">"
|
|
|
|
|
|
|
|
|
2018-04-16 11:13:39 -04:00
|
|
|
def link_established_callback(self, callback):
|
|
|
|
self.callbacks.link_established = callback
|
|
|
|
|
|
|
|
def packet_callback(self, callback):
|
|
|
|
self.callbacks.packet = callback
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-04-17 11:46:48 -04:00
|
|
|
def proof_requested_callback(self, callback):
|
|
|
|
self.callbacks.proof_requested = callback
|
2018-03-20 07:32:41 -04:00
|
|
|
|
2018-04-24 18:20:57 -04:00
|
|
|
def set_proof_strategy(self, proof_strategy):
|
2018-03-20 07:32:41 -04:00
|
|
|
if not proof_strategy in Destination.proof_strategies:
|
|
|
|
raise TypeError("Unsupported proof strategy")
|
|
|
|
else:
|
|
|
|
self.proof_strategy = proof_strategy
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-03-20 07:32:41 -04:00
|
|
|
def receive(self, packet):
|
|
|
|
plaintext = self.decrypt(packet.data)
|
2018-04-16 11:13:39 -04:00
|
|
|
if plaintext != None:
|
|
|
|
if packet.packet_type == RNS.Packet.LINKREQUEST:
|
|
|
|
self.incomingLinkRequest(plaintext, packet)
|
|
|
|
|
2018-04-16 16:09:23 -04:00
|
|
|
if packet.packet_type == RNS.Packet.DATA:
|
2018-04-16 11:13:39 -04:00
|
|
|
if self.callbacks.packet != None:
|
|
|
|
self.callbacks.packet(plaintext, packet)
|
|
|
|
|
|
|
|
def incomingLinkRequest(self, data, packet):
|
|
|
|
link = RNS.Link.validateRequest(self, data, packet)
|
|
|
|
if link != None:
|
|
|
|
self.links.append(link)
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
def createKeys(self):
|
2016-05-29 16:20:44 -04:00
|
|
|
if self.type == Destination.PLAIN:
|
|
|
|
raise TypeError("A plain destination does not hold any keys")
|
|
|
|
|
|
|
|
if self.type == Destination.SINGLE:
|
2018-03-16 06:40:37 -04:00
|
|
|
raise TypeError("A single destination holds keys through an Identity instance")
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
if self.type == Destination.GROUP:
|
|
|
|
self.prv_bytes = Fernet.generate_key()
|
|
|
|
self.prv = Fernet(self.prv_bytes)
|
|
|
|
|
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
def getPrivateKey(self):
|
2016-05-29 16:20:44 -04:00
|
|
|
if self.type == Destination.PLAIN:
|
|
|
|
raise TypeError("A plain destination does not hold any keys")
|
2018-03-16 06:40:37 -04:00
|
|
|
elif self.type == Destination.SINGLE:
|
|
|
|
raise TypeError("A single destination holds keys through an Identity instance")
|
2016-05-29 16:20:44 -04:00
|
|
|
else:
|
|
|
|
return self.prv_bytes
|
|
|
|
|
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
def loadPrivateKey(self, key):
|
2016-05-29 16:20:44 -04:00
|
|
|
if self.type == Destination.PLAIN:
|
|
|
|
raise TypeError("A plain destination does not hold any keys")
|
|
|
|
|
|
|
|
if self.type == Destination.SINGLE:
|
2018-03-16 06:40:37 -04:00
|
|
|
raise TypeError("A single destination holds keys through an Identity instance")
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
if self.type == Destination.GROUP:
|
|
|
|
self.prv_bytes = key
|
|
|
|
self.prv = Fernet(self.prv_bytes)
|
|
|
|
|
|
|
|
def loadPublicKey(self, key):
|
|
|
|
if self.type != Destination.SINGLE:
|
|
|
|
raise TypeError("Only the \"single\" destination type can hold a public key")
|
2018-03-16 06:40:37 -04:00
|
|
|
else:
|
|
|
|
raise TypeError("A single destination holds keys through an Identity instance")
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
def encrypt(self, plaintext):
|
|
|
|
if self.type == Destination.PLAIN:
|
|
|
|
return plaintext
|
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
if self.type == Destination.SINGLE and self.identity != None:
|
|
|
|
return self.identity.encrypt(plaintext)
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
if self.type == Destination.GROUP and self.prv != None:
|
|
|
|
try:
|
|
|
|
return base64.urlsafe_b64decode(self.prv.encrypt(plaintext))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt(self, ciphertext):
|
|
|
|
if self.type == Destination.PLAIN:
|
2016-06-03 13:02:02 -04:00
|
|
|
return ciphertext
|
2016-05-29 16:20:44 -04:00
|
|
|
|
2018-03-16 06:40:37 -04:00
|
|
|
if self.type == Destination.SINGLE and self.identity != None:
|
|
|
|
return self.identity.decrypt(ciphertext)
|
2016-05-29 16:20:44 -04:00
|
|
|
|
|
|
|
if self.type == Destination.GROUP:
|
|
|
|
return self.prv.decrypt(base64.urlsafe_b64encode(ciphertext))
|
|
|
|
|
|
|
|
|
|
|
|
def sign(self, message):
|
2018-03-16 06:40:37 -04:00
|
|
|
if self.type == Destination.SINGLE and self.identity != None:
|
|
|
|
return self.identity.sign(message)
|
2016-05-29 16:20:44 -04:00
|
|
|
else:
|
2018-03-16 06:40:37 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# Creates an announce packet for this destination.
|
|
|
|
# Application specific data can be added to the announce.
|
2020-03-06 08:28:26 -05:00
|
|
|
def announce(self, app_data=None, path_response=False):
|
2018-03-19 13:11:50 -04:00
|
|
|
destination_hash = self.hash
|
2018-04-18 17:31:17 -04:00
|
|
|
random_hash = RNS.Identity.getRandomHash()
|
2018-03-19 13:11:50 -04:00
|
|
|
|
|
|
|
signed_data = self.hash+self.identity.getPublicKey()+random_hash
|
|
|
|
if app_data != None:
|
|
|
|
signed_data += app_data
|
|
|
|
|
|
|
|
signature = self.identity.sign(signed_data)
|
|
|
|
|
2019-11-09 17:47:42 -05:00
|
|
|
# TODO: Check if this could be optimised by only
|
|
|
|
# carrying the hash in the destination field, not
|
|
|
|
# also redundantly inside the signed blob as here
|
2018-03-19 13:11:50 -04:00
|
|
|
announce_data = self.hash+self.identity.getPublicKey()+random_hash+signature
|
2019-11-09 17:47:42 -05:00
|
|
|
|
2018-03-19 13:11:50 -04:00
|
|
|
if app_data != None:
|
|
|
|
announce_data += app_data
|
|
|
|
|
2020-03-06 08:28:26 -05:00
|
|
|
if path_response:
|
|
|
|
announce_context = RNS.Packet.PATH_RESPONSE
|
|
|
|
else:
|
|
|
|
announce_context = RNS.Packet.NONE
|
|
|
|
|
|
|
|
RNS.Packet(self, announce_data, RNS.Packet.ANNOUNCE, context = announce_context).send()
|
2018-03-19 13:11:50 -04:00
|
|
|
|