2020-09-06 06:50:23 +00:00
|
|
|
"""
|
|
|
|
There is only one operator!
|
|
|
|
Running on node prime.
|
|
|
|
"""
|
|
|
|
# internal imports
|
|
|
|
import os,sys; sys.path.append(os.path.abspath(os.path.join(os.path.abspath(os.path.join(os.path.dirname(__file__),'..')),'..')))
|
|
|
|
from komrade import *
|
2020-09-06 20:17:47 +00:00
|
|
|
from komrade.backend import *
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TheOperator(Operator):
|
|
|
|
"""
|
2020-09-06 20:05:03 +00:00
|
|
|
The remote operator
|
2020-09-06 06:50:23 +00:00
|
|
|
"""
|
2020-09-06 20:17:47 +00:00
|
|
|
@property
|
|
|
|
def phone(self):
|
2020-09-06 20:25:18 +00:00
|
|
|
global TELEPHONE
|
2020-09-06 20:30:36 +00:00
|
|
|
from komrade.backend.the_telephone import TheTelephone
|
2020-09-06 23:13:16 +00:00
|
|
|
if not TELEPHONE: TELEPHONE=TheTelephone(allow_builtin=False)
|
2020-09-06 20:17:47 +00:00
|
|
|
return TELEPHONE
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
2020-09-06 22:09:36 +00:00
|
|
|
def __init__(self, name = OPERATOR_NAME, passphrase='acc', allow_builtin=True):
|
2020-09-06 06:50:23 +00:00
|
|
|
"""
|
|
|
|
Boot up the operator. Requires knowing or setting a password of memory.
|
|
|
|
"""
|
|
|
|
# init req paths
|
2020-09-06 09:30:15 +00:00
|
|
|
# if not os.path.exists(PATH_OPERATOR): os.makedirs(PATH_OPERATOR)
|
2020-09-06 06:50:23 +00:00
|
|
|
if not passphrase:
|
|
|
|
passphrase=getpass.getpass('Hello, this is the Operator speaking. What is the passphrase?\n> ')
|
2020-09-06 22:09:36 +00:00
|
|
|
self.allow_builtin=allow_builtin
|
2020-09-06 19:39:44 +00:00
|
|
|
super().__init__(name,passphrase,path_crypt_keys=PATH_CRYPT_OP_KEYS,path_crypt_data=PATH_CRYPT_OP_DATA)
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 08:41:22 +00:00
|
|
|
def decrypt_incoming(self,data):
|
2020-09-06 06:50:23 +00:00
|
|
|
# step 1 split:
|
2020-09-06 21:06:09 +00:00
|
|
|
data_unencr,data_encr_by_phone,data_encr_by_caller = data.split(BSEP)
|
|
|
|
data_unencr_by_phone,data_unencr_by_caller = None,None
|
2020-09-06 20:17:47 +00:00
|
|
|
|
2020-09-07 05:49:03 +00:00
|
|
|
# my_kc = self.keychain(allow_builtin=False,force=True)
|
|
|
|
# ph_kc = self.phone.keychain(allow_builtin=False,force=True)
|
|
|
|
# self.log('my keychain:',my_kc)
|
|
|
|
# self.log('phone keychain:',ph_kc)
|
|
|
|
# weeee
|
2020-09-06 23:11:38 +00:00
|
|
|
|
2020-09-06 21:06:09 +00:00
|
|
|
self.log('data_unencr =',data_unencr)
|
2020-09-06 20:17:47 +00:00
|
|
|
self.log('data_encr_by_phone =',data_encr_by_phone)
|
|
|
|
self.log('data_encr_by_caller =',data_encr_by_caller)
|
|
|
|
|
2020-09-06 21:09:40 +00:00
|
|
|
DATA = {}
|
2020-09-06 21:50:40 +00:00
|
|
|
KEYCHAIN = self.keychain(allow_builtin=False,force=True)
|
|
|
|
self.log('as of now 1, I the operator have these keys:',KEYCHAIN.keys())
|
2020-09-06 22:20:55 +00:00
|
|
|
# stop1
|
2020-09-06 23:08:07 +00:00
|
|
|
PHONE_PUBKEY=None
|
|
|
|
MY_PRIVKEY=None
|
2020-09-06 22:16:33 +00:00
|
|
|
|
2020-09-06 21:06:09 +00:00
|
|
|
if data_unencr:
|
|
|
|
self.log('unencrypted data:',data_unencr)
|
2020-09-06 23:00:52 +00:00
|
|
|
|
|
|
|
if BSEP2 in data_unencr:
|
|
|
|
my_privkey_decr,phone_pubkey_decr = data_unencr.split(BSEP2)
|
2020-09-06 23:08:07 +00:00
|
|
|
self.log('my_privkey_decr',my_privkey_decr)
|
|
|
|
self.log('phone_pubkey_decr',phone_pubkey_decr)
|
|
|
|
|
|
|
|
# get phone pubkey
|
2020-09-06 23:09:02 +00:00
|
|
|
new_phone_keychain = self.phone.keychain()#extra_keys={'pubkey_decr':phone_pubkey_decr})
|
2020-09-06 23:09:51 +00:00
|
|
|
new_op_keychain = self.keychain()#extra_keys={'privkey_decr':my_privkey_decr})
|
2020-09-06 23:08:07 +00:00
|
|
|
|
|
|
|
PHONE_PUBKEY = new_phone_keychain.get('pubkey')
|
|
|
|
MY_PRIVKEY = new_op_keychain.get('privkey')
|
|
|
|
|
2020-09-06 23:09:51 +00:00
|
|
|
self.log('PHONE_PUBKEY',PHONE_PUBKEY)
|
|
|
|
self.log('MY_PRIVKEY',MY_PRIVKEY)
|
|
|
|
stopppp
|
|
|
|
|
|
|
|
|
2020-09-06 21:09:40 +00:00
|
|
|
|
2020-09-06 23:00:52 +00:00
|
|
|
if DATA.get('_keychain'):
|
2020-09-06 22:16:33 +00:00
|
|
|
DATA['_keychain'] = self.valid_keychain(DATA['_keychain'])
|
2020-09-06 23:00:52 +00:00
|
|
|
# self.log('found keys in unencrypted data:',DATA['_keychain'])
|
2020-09-06 22:30:14 +00:00
|
|
|
# stop2
|
2020-09-06 22:16:33 +00:00
|
|
|
|
2020-09-06 22:17:02 +00:00
|
|
|
KEYCHAIN = self.keychain(allow_builtin=False,force=True,extra_keys=DATA['_keychain'])
|
2020-09-06 22:19:12 +00:00
|
|
|
self.log('as of now 2, I the operator have these keys:',KEYCHAIN.keys())
|
2020-09-06 22:29:05 +00:00
|
|
|
# stopppppppp
|
2020-09-06 21:09:40 +00:00
|
|
|
self.log('DATA as of now!?',DATA)
|
2020-09-06 21:35:21 +00:00
|
|
|
# stop
|
2020-09-06 21:06:09 +00:00
|
|
|
|
2020-09-06 20:17:47 +00:00
|
|
|
if data_encr_by_phone:
|
2020-09-06 21:50:40 +00:00
|
|
|
|
2020-09-06 20:17:47 +00:00
|
|
|
# then try to unwrap telephone encryption
|
2020-09-06 22:29:05 +00:00
|
|
|
me_privkey = KEYCHAIN.get('privkey') #self.privkey(keychain = DATA.get('_keychain',{}))
|
|
|
|
if not me_privkey:
|
|
|
|
self.log('!! could not assemble my private key. failing.')
|
|
|
|
return OPERATOR_INTERCEPT_MESSAGE
|
2020-09-06 22:29:34 +00:00
|
|
|
|
2020-09-06 22:29:05 +00:00
|
|
|
# self.log('as of now 3, I the operator have these keys:',self.keychain().keys())
|
2020-09-06 21:37:58 +00:00
|
|
|
self.log('me_privkey now',me_privkey)
|
2020-09-06 22:29:05 +00:00
|
|
|
# print(me_privkey, '<--',them_pubkey)
|
2020-09-06 20:17:47 +00:00
|
|
|
try:
|
2020-09-06 22:29:05 +00:00
|
|
|
data_unencr_by_phone = SMessage(me_privkey, self.phone.pubkey_).unwrap(data_encr_by_phone)
|
2020-09-06 20:17:47 +00:00
|
|
|
self.log('decrypted data !!!:',data_unencr_by_phone)
|
2020-09-06 20:37:47 +00:00
|
|
|
except ThemisError as e:
|
|
|
|
self.log('not really from the telephone?',e)
|
2020-09-06 20:17:47 +00:00
|
|
|
return OPERATOR_INTERCEPT_MESSAGE
|
2020-09-06 20:19:52 +00:00
|
|
|
|
2020-09-06 22:36:15 +00:00
|
|
|
data_unencr_by_phone_json = json.loads(data_unencr_by_phone.decode())
|
|
|
|
self.log('data_unencr_by_phone_json',data_unencr_by_phone_json)
|
|
|
|
if type(data_unencr_by_phone_json)== dict:
|
|
|
|
dict_merge(DATA, data_unencr_by_phone_json)
|
2020-09-06 20:17:47 +00:00
|
|
|
|
2020-09-06 22:33:45 +00:00
|
|
|
self.log('DATA as of now 3!?',DATA)
|
|
|
|
stop3
|
2020-09-06 20:17:47 +00:00
|
|
|
|
2020-09-06 20:19:52 +00:00
|
|
|
if data_encr_by_caller and 'name' in data_unencr_by_phone:
|
|
|
|
name=data_unencr_by_phone['name']
|
2020-09-06 21:01:29 +00:00
|
|
|
keychain=data_unencr_by_phone.get('_keychain',{})
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
# decrypt using this user's pubkey on record
|
2020-09-06 20:17:47 +00:00
|
|
|
caller = Caller(name)
|
2020-09-06 20:19:52 +00:00
|
|
|
data_unencr2 = SMessage(self.privkey_, caller.pubkey_).unwrap(data_encr_by_caller)
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 20:19:52 +00:00
|
|
|
if type(data_unencr_by_phone)==dict and type(data_encr_by_caller)==dict:
|
|
|
|
data = data_unencr_by_phone
|
|
|
|
dict_merge(data_encr_by_caller, data)
|
2020-09-06 06:50:23 +00:00
|
|
|
else:
|
2020-09-06 20:19:52 +00:00
|
|
|
data=(data_unencr_by_phone,data_encr_by_caller)
|
2020-09-06 06:50:23 +00:00
|
|
|
else:
|
2020-09-06 20:19:52 +00:00
|
|
|
data = data_unencr_by_phone
|
2020-09-06 08:41:22 +00:00
|
|
|
return data
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 20:05:03 +00:00
|
|
|
def receive(self,data):
|
|
|
|
# decrypt
|
2020-09-06 08:41:22 +00:00
|
|
|
data = self.decrypt_incoming(data)
|
2020-09-06 20:05:03 +00:00
|
|
|
|
|
|
|
# decode
|
2020-09-06 21:01:29 +00:00
|
|
|
data_s = data.decode()
|
|
|
|
data_json = json.loads(data_s)
|
2020-09-06 20:05:03 +00:00
|
|
|
|
|
|
|
|
2020-09-06 20:33:32 +00:00
|
|
|
self.log('DATA =',type(data),data)
|
2020-09-06 21:01:29 +00:00
|
|
|
self.log('DATA_s =',type(data_s),data_s)
|
|
|
|
self.log('DATA_json =',type(data_json),data_s)
|
2020-09-06 20:41:45 +00:00
|
|
|
|
2020-09-06 21:01:29 +00:00
|
|
|
return self.route(data_json)
|
2020-09-06 20:05:03 +00:00
|
|
|
|
|
|
|
def route(self, data):
|
|
|
|
# data = self.decrypt_incoming(data)
|
|
|
|
# self.log('DATA =',data)
|
|
|
|
return data# 'success!'
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_operators():
|
2020-09-06 21:54:59 +00:00
|
|
|
op = TheOperator()
|
|
|
|
phone = TheTelephone()
|
2020-09-06 16:31:35 +00:00
|
|
|
|
2020-09-06 19:10:02 +00:00
|
|
|
op_decr_keys = op.forge_new_keys(
|
2020-09-06 21:01:29 +00:00
|
|
|
keys_to_save=['pubkey','privkey_encr','adminkey_encr','adminkey_decr_encr','adminkey_decr_decr'],
|
|
|
|
keys_to_return=['pubkey','privkey_decr']
|
2020-09-06 19:10:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
phone_decr_keys = phone.forge_new_keys(
|
2020-09-06 22:40:53 +00:00
|
|
|
keys_to_save=['pubkey_encr'],
|
|
|
|
keys_to_return=['pubkey_decr','privkey']
|
2020-09-06 19:10:02 +00:00
|
|
|
)
|
2020-09-06 14:45:40 +00:00
|
|
|
|
2020-09-06 14:46:57 +00:00
|
|
|
print('\n'*5)
|
|
|
|
print('OPERATOR_KEYCHAIN =',op_decr_keys)
|
|
|
|
print()
|
|
|
|
print('TELEPHONE_KEYCHAIN =',phone_decr_keys)
|
2020-09-07 05:55:10 +00:00
|
|
|
# stop
|
2020-09-06 12:00:45 +00:00
|
|
|
|
2020-09-06 15:32:12 +00:00
|
|
|
# stringify
|
|
|
|
for k,v in phone_decr_keys.items():
|
2020-09-07 05:56:14 +00:00
|
|
|
v_s = b64encode(v) #.decode('utf-8')
|
2020-09-06 17:20:45 +00:00
|
|
|
phone_decr_keys[k]=v_s
|
2020-09-06 15:32:12 +00:00
|
|
|
for k,v in op_decr_keys.items():
|
2020-09-07 05:56:14 +00:00
|
|
|
v_s = b64encode(v) #.decode('utf-8')
|
2020-09-06 17:20:45 +00:00
|
|
|
op_decr_keys[k]=v_s
|
2020-09-06 15:32:12 +00:00
|
|
|
|
2020-09-07 05:55:10 +00:00
|
|
|
print('\n'*5)
|
|
|
|
print('OPERATOR_KEYCHAIN =',op_decr_keys)
|
|
|
|
print()
|
|
|
|
print('TELEPHONE_KEYCHAIN =',phone_decr_keys)
|
2020-09-07 06:04:43 +00:00
|
|
|
print('\n'*5)
|
|
|
|
# stoppwppepe
|
2020-09-06 17:19:30 +00:00
|
|
|
|
2020-09-07 06:04:43 +00:00
|
|
|
# # print('total_d',total_d)
|
|
|
|
# builtin_keychains = {OPERATOR_NAME:op_decr_keys, TELEPHONE_NAME:phone_decr_keys}
|
|
|
|
# builtin_keychains_s = json.dumps(builtin_keychains)
|
2020-09-06 16:56:45 +00:00
|
|
|
|
2020-09-07 06:04:43 +00:00
|
|
|
# print('builtin_keychains =',builtin_keychains)
|
2020-09-06 16:57:28 +00:00
|
|
|
|
2020-09-07 06:04:43 +00:00
|
|
|
# print('builtin_keychains_s =',builtin_keychains_s)
|
|
|
|
# builtin_keychains_b = builtin_keychains_s.encode('utf-8')
|
2020-09-06 15:32:12 +00:00
|
|
|
|
2020-09-07 06:04:43 +00:00
|
|
|
# builtin_keychains_b_decr = KomradeSymmetricKeyWithoutPassphrase()
|
|
|
|
# builtin_keychains_b_encr = builtin_keychains_b_decr.encrypt(builtin_keychains_b)
|
|
|
|
|
|
|
|
# builtin_keychains_b_decr_b64 = b64encode(builtin_keychains_b_decr.key)
|
|
|
|
# builtin_keychains_b_encr_b64 = b64encode(builtin_keychains_b_encr)
|
|
|
|
|
|
|
|
# with open(PATH_BUILTIN_KEYCHAINS_ENCR,'wb') as of:
|
|
|
|
# of.write(builtin_keychains_b_encr_b64)
|
|
|
|
# print('>> saved:',PATH_BUILTIN_KEYCHAINS_ENCR)
|
|
|
|
# print(builtin_keychains_b_encr_b64,'\n\n')
|
|
|
|
# with open(PATH_BUILTIN_KEYCHAINS_DECR,'wb') as of:
|
|
|
|
# of.write(builtin_keychains_b_decr_b64)
|
|
|
|
# print('>> saved:',PATH_BUILTIN_KEYCHAINS_DECR)
|
|
|
|
# print(builtin_keychains_b_decr_b64,'\n')
|
2020-09-06 15:32:12 +00:00
|
|
|
|
|
|
|
|
2020-09-06 10:34:04 +00:00
|
|
|
|
2020-09-06 13:07:27 +00:00
|
|
|
# op_pub = op.pubkey_decr_
|
|
|
|
# phone_pub = phone.pubkey_decr_
|
|
|
|
# phone_priv = phone.privkey_decr_
|
2020-09-06 10:34:04 +00:00
|
|
|
|
2020-09-06 13:07:27 +00:00
|
|
|
# print('OPERATOR_PUBKEY_DECR =',b64encode(op_pub))
|
|
|
|
# print('TELEPHONE_PUBKEY_DECR =',b64encode(phone_pub))
|
|
|
|
# print('TELEPHONE_PRIVKEY_DECR =',b64encode(phone_priv))
|
2020-09-06 10:34:04 +00:00
|
|
|
# return {
|
2020-09-06 10:34:35 +00:00
|
|
|
# 'op.keychain()':op.keychain(),
|
|
|
|
# 'phone.keychain()':phone.keychain()
|
|
|
|
# }
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_op():
|
2020-09-06 19:39:44 +00:00
|
|
|
from komrade.backend.the_telephone import TheTelephone
|
|
|
|
|
|
|
|
|
|
|
|
op = TheOperator()
|
|
|
|
# op.boot()
|
|
|
|
|
2020-09-06 19:19:44 +00:00
|
|
|
keychain_op = op.keychain(force=True)
|
2020-09-06 19:39:44 +00:00
|
|
|
|
2020-09-06 19:19:44 +00:00
|
|
|
|
2020-09-06 19:39:44 +00:00
|
|
|
phone = TheTelephone()
|
|
|
|
# phone.boot()
|
2020-09-06 19:19:44 +00:00
|
|
|
keychain_ph = phone.keychain(force=True)
|
|
|
|
|
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
from pprint import pprint
|
2020-09-06 19:19:44 +00:00
|
|
|
print('REASSEMBLED OPERATOR KEYCHAIN')
|
|
|
|
pprint(keychain_op)
|
|
|
|
# stop
|
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
print('REASSEMBLED TELEPHONE KEYCHAIN')
|
2020-09-06 19:19:44 +00:00
|
|
|
pprint(keychain_ph)
|
2020-09-06 19:18:43 +00:00
|
|
|
|
2020-09-06 17:44:08 +00:00
|
|
|
# print(op.pubkey(keychain=keychain))
|
2020-09-06 18:52:47 +00:00
|
|
|
# print(op.crypt_keys.get(op.pubkey(), prefix='/privkey_encr/'))
|
2020-09-06 18:51:25 +00:00
|
|
|
# print(op.crypt_keys.get(op.name, prefix='/pubkey_encr/'))
|
2020-09-06 19:18:43 +00:00
|
|
|
# print(op.pubkey_)
|
2020-09-06 18:52:47 +00:00
|
|
|
|
2020-09-06 17:42:57 +00:00
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
# stop
|
2020-09-06 06:50:23 +00:00
|
|
|
|
2020-09-06 19:18:43 +00:00
|
|
|
# pubkey = op.keychain()['pubkey']
|
|
|
|
# pubkey_b64 = b64encode(pubkey)
|
|
|
|
# print(pubkey)
|
2020-09-06 06:50:23 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__': test_op()
|