2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-01 21:40:32 +00:00
Comrad/komrade/backend/operators.py

157 lines
5.0 KiB
Python
Raw Normal View History

2020-09-05 16:26:37 +00:00
# internal imports
2020-09-04 15:50:08 +00:00
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
2020-09-05 16:26:37 +00:00
from komrade import *
2020-09-05 21:11:42 +00:00
from komrade.backend.crypt import *
from komrade.backend.keymaker import *
from komrade.backend.mazes import *
2020-09-05 22:15:15 +00:00
from komrade.backend.switchboard import *
2020-09-04 15:50:08 +00:00
2020-09-06 06:50:23 +00:00
2020-09-04 15:50:08 +00:00
class Operator(Keymaker):
2020-09-08 07:20:42 +00:00
def __init__(self, name, passphrase=DEBUG_DEFAULT_PASSPHRASE, keychain = {}, path_crypt_keys=PATH_CRYPT_CA_KEYS, path_crypt_data=PATH_CRYPT_CA_DATA):
2020-09-07 17:11:52 +00:00
super().__init__(name=name,passphrase=passphrase, keychain=keychain,
path_crypt_keys=path_crypt_keys, path_crypt_data=path_crypt_data)
2020-09-06 19:39:44 +00:00
self.boot(create=False)
2020-09-04 15:50:08 +00:00
2020-09-05 14:09:31 +00:00
def boot(self,create=False):
# Do I have my keys?
have_keys = self.exists()
# If not, forge them -- only once!
if not have_keys and create:
self.get_new_keys()
2020-09-08 07:01:35 +00:00
2020-09-07 17:11:52 +00:00
@property
def phone(self):
2020-09-07 20:00:21 +00:00
from komrade.backend.the_telephone import TheTelephone
if type(self)==TheTelephone: return self
2020-09-07 17:50:58 +00:00
if hasattr(self,'_phone'): return self._phone
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
global TELEPHONE,TELEPHONE_KEYCHAIN
if TELEPHONE: return TELEPHONE
2020-09-07 20:00:21 +00:00
2020-09-07 17:50:58 +00:00
self._phone=TELEPHONE=TheTelephone()
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
return TELEPHONE
@property
def op(self):
2020-09-07 20:00:21 +00:00
from komrade.backend.the_operator import TheOperator
if type(self)==TheOperator: return self
if hasattr(self,'_op'): return self._op
2020-09-07 17:11:52 +00:00
global OPERATOR,OPERATOR_KEYCHAIN
if OPERATOR: return OPERATOR
2020-09-07 20:00:21 +00:00
self._op=OPERATOR=TheOperator()
2020-09-07 17:11:52 +00:00
return OPERATOR
def encrypt_to_send(self,msg_json,from_privkey,to_pubkey):
2020-09-07 21:57:49 +00:00
self.log('msg_json',msg_json)
self.log('from_privkey',from_privkey)
self.log('to_pubkey',to_pubkey)
if not msg_json or not from_privkey or not to_pubkey:
2020-09-07 21:10:28 +00:00
self.log('not enough info!',msg_json,from_privkey,to_pubkey)
2020-09-07 21:58:53 +00:00
whattttttt
return b''
2020-09-07 21:31:40 +00:00
self.log('packing for transmission: msg_json',type(msg_json),msg_json)
msg_b = package_for_transmission(msg_json)
2020-09-07 21:31:40 +00:00
self.log('packing for transmission: msg_b',type(msg_b),msg_b)
2020-09-07 21:56:57 +00:00
# try:
2020-09-08 15:14:48 +00:00
self.log('from privkey =',from_privkey)
self.log('to pubkey =',to_pubkey)
2020-09-07 21:56:57 +00:00
msg_encr = SMessage(
from_privkey,
to_pubkey,
).wrap(msg_b)
2020-09-07 21:58:53 +00:00
self.log('msg_encr',msg_encr)
2020-09-07 22:46:39 +00:00
# stop
2020-09-07 21:56:57 +00:00
return msg_encr
# except ThemisError as e:
# self.log('unable to encrypt to send!',e)
# return b''
2020-09-07 18:20:05 +00:00
def decrypt_from_send(self,msg_encr,from_pubkey,to_privkey):
2020-09-07 18:20:37 +00:00
if not msg_encr or not from_pubkey or not to_privkey:
2020-09-07 21:10:28 +00:00
self.log('not enough info!',msg_encr,from_pubkey,to_privkey)
2020-09-07 18:36:05 +00:00
return {}
try:
# decrypt
msg_b = SMessage(
to_privkey,
from_pubkey,
).unwrap(msg_encr)
# decode
2020-09-07 18:22:56 +00:00
self.log('msg_b??',msg_b)
msg_json = unpackage_from_transmission(msg_b)
2020-09-07 18:22:56 +00:00
self.log('msg_json??',msg_json)
# return
return msg_json
except ThemisError as e:
self.log('unable to decrypt from send!',e)
2020-09-07 18:36:05 +00:00
return {}
2020-09-07 20:00:21 +00:00
2020-09-08 11:23:41 +00:00
def package_msg_to(self,msg,another):
2020-09-08 15:31:16 +00:00
self.log('KEYCHAINNN ',self.keychain())
self.log('my privkey',self.privkey)
self.log('my pubkey',self.pubkey,self.keychain().get('pubkey','!!!?!!?!?!?!?'))
2020-09-08 15:14:48 +00:00
if not self.privkey or not self.pubkey:
self.log('why do I have no pub/privkey pair!?',self.privkey,self,self.name)
return b''
if not another.name or not another.pubkey:
self.log('why do I not know whom I\'m writing to?')
return b''
# otherwise send msg
2020-09-08 11:23:41 +00:00
msg = {
'_from_pub':self.pubkey,
'_from_name':self.name,
'_to_pub':another.pubkey,
2020-09-08 15:14:48 +00:00
'_to_name':another.name,
2020-09-08 11:23:41 +00:00
'_msg':msg,
}
2020-09-08 15:14:48 +00:00
self.log(f'I am a {type(self)} packaging a message to {another}')
2020-09-08 11:23:41 +00:00
return self.encrypt_to_send(msg, self.privkey, another.pubkey)
2020-09-07 20:00:21 +00:00
2020-09-07 17:11:52 +00:00
2020-09-08 11:23:41 +00:00
def unpackage_msg_from(self,msg_encr_b,another):
2020-09-08 15:36:01 +00:00
return self.decrypt_from_send(
msg_encr_b,
from_pubkey=another.pubkey,
to_privkey=self.privkey
)
2020-09-08 12:11:13 +00:00
# def ring(self,with_msg,to_whom=None,by_way_of=None):
# # ring 1: encrypt from me to 'whom'
# msg_encr = self.package_msg_to(
# with_msg,
# whom
# )
# self.log(f'msg_encr --> {whom} layer 1',msg_encr)
# # ring 2: keep ringing via mediator
# resp_msg_encr = by_way_of.ring(
# msg_encr
# )
# self.log('resp_msg_encr',resp_msg_encr)
# # ring 3: decrypt and send back
# resp_msg = self.unpackage_msg_from(
# resp_msg_encr,
# whom
# )
# self.log('resp_msg',resp_msg)
# return resp_msg