2020-09-08 11:23:41 +00:00
|
|
|
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-08 15:15:40 +00:00
|
|
|
from komrade.backend import *
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-12 15:00:38 +00:00
|
|
|
# def create_phonelines():
|
|
|
|
# prime_entities = [
|
|
|
|
# {
|
|
|
|
# 'name':OPERATOR_NAME,
|
|
|
|
# 'keys_to_save_on_srv': ['pubkey','privkey_encr'],
|
|
|
|
# 'keys_to_'
|
|
|
|
|
|
|
|
# }
|
|
|
|
# ]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
### CREATE PRIME ENTITIES
|
|
|
|
def create_phonelines():
|
|
|
|
## CREATE OPERATOR
|
2020-09-10 10:00:34 +00:00
|
|
|
op = Keymaker(name=OPERATOR_NAME)
|
2020-09-08 11:23:41 +00:00
|
|
|
op_keys_to_keep_on_client = ['pubkey'] # kept on app, stored under name
|
2020-09-12 15:00:38 +00:00
|
|
|
op_keys_to_keep_on_3rdparty = [] # kept on .onion site
|
2020-09-08 11:23:41 +00:00
|
|
|
op_keys_to_keep_on_server = ['pubkey', # stored under name
|
|
|
|
'privkey_encr',
|
2020-09-12 15:00:38 +00:00
|
|
|
'privkey_decr'] # kept on op server
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
## create phone
|
2020-09-10 10:00:34 +00:00
|
|
|
phone = Keymaker(name=TELEPHONE_NAME)
|
2020-09-12 15:00:38 +00:00
|
|
|
phone_keys_to_keep_on_client = ['pubkey','privkey'] # kept on app; need both to init connection
|
|
|
|
phone_keys_to_keep_on_3rdparty = [''] # dl by phone
|
2020-09-08 11:23:41 +00:00
|
|
|
phone_keys_to_keep_on_server = ['pubkey'] # kept on op server
|
|
|
|
|
2020-09-10 09:54:47 +00:00
|
|
|
## create phone
|
2020-09-10 10:00:34 +00:00
|
|
|
world = Keymaker(name=WORLD_NAME)
|
2020-09-12 15:00:38 +00:00
|
|
|
world_keys_to_keep_on_client = ['pubkey','privkey_encr','privkey_decr']
|
|
|
|
world_keys_to_keep_on_3rdparty = []
|
|
|
|
world_keys_to_keep_on_server = ['pubkey']
|
2020-09-10 09:54:47 +00:00
|
|
|
|
2020-09-12 14:32:03 +00:00
|
|
|
|
|
|
|
# key types
|
|
|
|
key_types = {
|
|
|
|
'pubkey':KomradeAsymmetricPublicKey,
|
|
|
|
'privkey':KomradeAsymmetricPrivateKey,
|
|
|
|
'privkey_encr':KomradeEncryptedAsymmetricPrivateKey,
|
|
|
|
'privkey_decr':KomradeSymmetricKeyWithoutPassphrase,
|
|
|
|
'adminkey':KomradeSymmetricKeyWithoutPassphrase,
|
|
|
|
'adminkey_encr':KomradeEncryptedSymmetricKey,
|
|
|
|
'adminkey_decr':KomradeSymmetricKeyWithPassphrase,
|
|
|
|
}
|
|
|
|
|
2020-09-10 09:54:47 +00:00
|
|
|
|
|
|
|
|
2020-09-08 11:23:41 +00:00
|
|
|
# create keys for Op
|
2020-09-12 14:37:29 +00:00
|
|
|
op_decr_keys = op.forge_new_keys(
|
2020-09-12 15:00:38 +00:00
|
|
|
# key_types=key_types,
|
2020-09-08 11:23:41 +00:00
|
|
|
keys_to_save=op_keys_to_keep_on_server,
|
|
|
|
keys_to_return=op_keys_to_keep_on_client + op_keys_to_keep_on_3rdparty # on clients only
|
2020-09-12 14:32:03 +00:00
|
|
|
|
2020-09-08 11:23:41 +00:00
|
|
|
)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('op!',op_uri)
|
2020-09-12 14:38:09 +00:00
|
|
|
print(op_decr_keys)
|
2020-09-12 15:00:38 +00:00
|
|
|
exit()
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# create keys for phone
|
2020-09-12 14:37:29 +00:00
|
|
|
phone_decr_keys = phone.forge_new_keys(
|
2020-09-12 14:32:03 +00:00
|
|
|
key_types=key_types,
|
2020-09-08 11:23:41 +00:00
|
|
|
keys_to_save=phone_keys_to_keep_on_server, # on server only
|
|
|
|
keys_to_return=phone_keys_to_keep_on_client + phone_keys_to_keep_on_3rdparty # on clients only
|
|
|
|
)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('phone!',op_uri)
|
2020-09-12 14:38:09 +00:00
|
|
|
print(phone_decr_keys)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
2020-09-10 09:54:47 +00:00
|
|
|
# create keys for world
|
2020-09-12 14:37:29 +00:00
|
|
|
world_decr_keys = world.forge_new_keys(
|
2020-09-12 14:32:03 +00:00
|
|
|
key_types=key_types,
|
2020-09-10 09:54:47 +00:00
|
|
|
keys_to_save=world_keys_to_keep_on_server,
|
|
|
|
keys_to_return=world_keys_to_keep_on_client + world_keys_to_keep_on_3rdparty # on clients only
|
|
|
|
)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('world!',op_uri)
|
2020-09-12 14:38:09 +00:00
|
|
|
print(world_decr_keys)
|
2020-09-08 11:23:41 +00:00
|
|
|
## store remote keys
|
2020-09-10 09:54:47 +00:00
|
|
|
THIRD_PARTY_DICT = {OPERATOR_NAME:{}, TELEPHONE_NAME:{}, WORLD_NAME:{}}
|
2020-09-08 11:23:41 +00:00
|
|
|
for key in op_keys_to_keep_on_3rdparty:
|
|
|
|
if key in op_decr_keys:
|
|
|
|
THIRD_PARTY_DICT[OPERATOR_NAME][key]=op_decr_keys[key]
|
|
|
|
for key in phone_keys_to_keep_on_3rdparty:
|
|
|
|
if key in phone_decr_keys:
|
|
|
|
THIRD_PARTY_DICT[TELEPHONE_NAME][key]=phone_decr_keys[key]
|
2020-09-10 09:54:47 +00:00
|
|
|
for key in world_keys_to_keep_on_3rdparty:
|
|
|
|
if key in world_decr_keys:
|
|
|
|
THIRD_PARTY_DICT[WORLD_NAME][key]=world_decr_keys[key]
|
2020-09-08 11:23:41 +00:00
|
|
|
|
2020-09-12 14:52:55 +00:00
|
|
|
print('THIRD_PARTY_DICT',THIRD_PARTY_DICT)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# store local keys
|
2020-09-10 09:54:47 +00:00
|
|
|
STORE_IN_APP = {OPERATOR_NAME:{}, TELEPHONE_NAME:{}, WORLD_NAME:{}}
|
2020-09-08 11:23:41 +00:00
|
|
|
for key in op_keys_to_keep_on_client:
|
|
|
|
if key in op_decr_keys:
|
|
|
|
STORE_IN_APP[OPERATOR_NAME][key]=op_decr_keys[key]
|
|
|
|
for key in phone_keys_to_keep_on_client:
|
|
|
|
if key in phone_decr_keys:
|
|
|
|
STORE_IN_APP[TELEPHONE_NAME][key]=phone_decr_keys[key]
|
2020-09-10 09:54:47 +00:00
|
|
|
for key in world_keys_to_keep_on_client:
|
|
|
|
if key in world_decr_keys:
|
|
|
|
STORE_IN_APP[WORLD_NAME][key]=world_decr_keys[key]
|
2020-09-12 14:52:55 +00:00
|
|
|
print('STORE_IN_APP',STORE_IN_APP)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# package
|
2020-09-10 09:57:50 +00:00
|
|
|
import pickle
|
|
|
|
STORE_IN_APP_pkg = pickle.dumps(STORE_IN_APP) #pickle.dumps(STORE_IN_APP[TELEPHONE_NAME]) + BSEP + pickle.dumps(STORE_IN_APP[OPERATOR_NAME])
|
|
|
|
THIRD_PARTY_DICT_pkg = pickle.dumps(THIRD_PARTY_DICT) #pickle.dumps(THIRD_PARTY_DICT[TELEPHONE_NAME]) + BSEP + pickle.dumps(THIRD_PARTY_DICT[OPERATOR_NAME])
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# encrypt
|
|
|
|
omega_key = KomradeSymmetricKeyWithoutPassphrase()
|
|
|
|
STORE_IN_APP_encr = b64encode(omega_key.encrypt(STORE_IN_APP_pkg))
|
|
|
|
THIRD_PARTY_totalpkg = b64encode(omega_key.data + BSEP + omega_key.encrypt(THIRD_PARTY_DICT_pkg))
|
2020-09-12 14:52:55 +00:00
|
|
|
#print('THIRD_PARTY_totalpkg',THIRD_PARTY_totalpkg)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# save
|
|
|
|
with open(PATH_BUILTIN_KEYCHAIN,'wb') as of:
|
|
|
|
of.write(STORE_IN_APP_encr)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('STORE_IN_APP_encr',STORE_IN_APP_encr)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
with open(PATH_OPERATOR_WEB_KEYS_FILE,'wb') as of:
|
|
|
|
of.write(THIRD_PARTY_totalpkg)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('THIRD_PARTY_DICT_encr',THIRD_PARTY_totalpkg)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def connect_phonelines():
|
|
|
|
# globals
|
2020-09-10 09:54:47 +00:00
|
|
|
global OMEGA_KEY,OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN,WORLD_KEYCHAIN
|
|
|
|
if OMEGA_KEY and OPERATOR_KEYCHAIN and TELEPHONE_KEYCHAIN and WORLD_KEYCHAIN:
|
|
|
|
return (OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN,WORLD_KEYCHAIN,OMEGA_KEY)
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
# import
|
|
|
|
from komrade.backend.mazes import tor_request
|
|
|
|
from komrade.backend import PATH_OPERATOR_WEB_KEYS_URL
|
|
|
|
|
|
|
|
# load remote keys
|
|
|
|
r = komrade_request(PATH_OPERATOR_WEB_KEYS_URL)
|
|
|
|
if r.status_code!=200:
|
2020-09-10 10:42:27 +00:00
|
|
|
# return
|
|
|
|
raise KomradeException('oh no!')
|
2020-09-08 11:23:41 +00:00
|
|
|
pkg = r.text
|
|
|
|
pkg = b64decode(pkg)
|
|
|
|
OMEGA_KEY_b,remote_builtin_keychain_encr = pkg.split(BSEP)
|
|
|
|
OMEGA_KEY = KomradeSymmetricKeyWithoutPassphrase(key=OMEGA_KEY_b)
|
2020-09-10 10:29:43 +00:00
|
|
|
remote_builtin_keychain = pickle.loads(OMEGA_KEY.decrypt(remote_builtin_keychain_encr))
|
2020-09-10 09:54:47 +00:00
|
|
|
(
|
|
|
|
remote_builtin_keychain_phone_json,
|
|
|
|
remote_builtin_keychain_op_json,
|
|
|
|
remote_builtin_keychain_world_json
|
|
|
|
) = (
|
|
|
|
remote_builtin_keychain[TELEPHONE_NAME],
|
|
|
|
remote_builtin_keychain[OPERATOR_NAME],
|
|
|
|
remote_builtin_keychain[WORLD_NAME]
|
|
|
|
)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('remote!',
|
2020-09-10 11:23:17 +00:00
|
|
|
# remote_builtin_keychain_phone_json,
|
|
|
|
# remote_builtin_keychain_op_json,
|
|
|
|
# remote_builtin_keychain_world_json
|
|
|
|
# #)
|
2020-09-10 09:54:47 +00:00
|
|
|
|
|
|
|
# load local keys
|
|
|
|
if not os.path.exists(PATH_BUILTIN_KEYCHAIN):
|
|
|
|
return
|
|
|
|
with open(PATH_BUILTIN_KEYCHAIN,'rb') as f:
|
|
|
|
local_builtin_keychain_encr = b64decode(f.read())
|
2020-09-10 10:32:58 +00:00
|
|
|
local_builtin_keychain = pickle.loads(OMEGA_KEY.decrypt(local_builtin_keychain_encr))
|
|
|
|
(
|
|
|
|
local_builtin_keychain_phone_json,
|
|
|
|
local_builtin_keychain_op_json,
|
|
|
|
local_builtin_keychain_world_json
|
|
|
|
) = (local_builtin_keychain[TELEPHONE_NAME],
|
|
|
|
local_builtin_keychain[OPERATOR_NAME],
|
|
|
|
local_builtin_keychain[WORLD_NAME]
|
|
|
|
)
|
2020-09-10 11:22:47 +00:00
|
|
|
#print('local!',
|
2020-09-10 11:23:17 +00:00
|
|
|
# local_builtin_keychain_phone_json,
|
|
|
|
# local_builtin_keychain_op_json,
|
|
|
|
# local_builtin_keychain_world_json
|
|
|
|
# )
|
2020-09-10 10:32:58 +00:00
|
|
|
|
2020-09-08 11:23:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
# set builtin keychains
|
|
|
|
TELEPHONE_KEYCHAIN={}
|
|
|
|
OPERATOR_KEYCHAIN={}
|
2020-09-10 09:54:47 +00:00
|
|
|
WORLD_KEYCHAIN={}
|
2020-09-08 11:23:41 +00:00
|
|
|
dict_merge(TELEPHONE_KEYCHAIN,local_builtin_keychain_phone_json)
|
|
|
|
dict_merge(OPERATOR_KEYCHAIN,local_builtin_keychain_op_json)
|
2020-09-10 09:54:47 +00:00
|
|
|
dict_merge(WORLD_KEYCHAIN,local_builtin_keychain_world_json)
|
2020-09-08 11:23:41 +00:00
|
|
|
dict_merge(TELEPHONE_KEYCHAIN,remote_builtin_keychain_phone_json)
|
|
|
|
dict_merge(OPERATOR_KEYCHAIN,remote_builtin_keychain_op_json)
|
2020-09-10 09:54:47 +00:00
|
|
|
dict_merge(WORLD_KEYCHAIN,remote_builtin_keychain_world_json)
|
2020-09-10 11:06:08 +00:00
|
|
|
|
|
|
|
# @hack: make sure world saved as contact?
|
|
|
|
ofnfn=os.path.join(PATH_QRCODES,WORLD_NAME+'.png')
|
|
|
|
if not os.path.exists(ofnfn):
|
|
|
|
import pyqrcode
|
|
|
|
uri_id = b64encode(WORLD_KEYCHAIN['pubkey'])
|
|
|
|
qr = pyqrcode.create(uri_id)
|
|
|
|
qr.png(ofnfn,scale=5)
|
2020-09-10 11:07:09 +00:00
|
|
|
qr_str = qr.terminal()
|
2020-09-10 11:22:47 +00:00
|
|
|
#print(f'Saved world to QR:\n{qr_str}')
|
2020-09-08 11:23:41 +00:00
|
|
|
|
2020-09-10 11:22:47 +00:00
|
|
|
# ##print('>>>> loaded OPERATOR_KEYCHAIN',OPERATOR_KEYCHAIN)
|
|
|
|
# ##print('>>>> loaded TELEPHONE_KEYCHAIN',TELEPHONE_KEYCHAIN)
|
2020-09-10 09:54:47 +00:00
|
|
|
return (OPERATOR_KEYCHAIN,TELEPHONE_KEYCHAIN,WORLD_KEYCHAIN,OMEGA_KEY)
|
2020-09-10 10:42:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
phone = TheTelephone()
|
|
|
|
op = TheOperator()
|
|
|
|
|
2020-09-10 11:22:47 +00:00
|
|
|
p#print(phone.keychain())
|
|
|
|
p#print(op.keychain())
|