2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-16 00:12:48 +00:00
Comrad/comrad/backend/phonelines.py

176 lines
5.0 KiB
Python
Raw Normal View History

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__),'..')),'..')))
2020-09-29 13:09:56 +00:00
from comrad import *
from comrad.backend import *
2020-09-08 11:23:41 +00:00
2020-09-12 16:08:47 +00:00
def create_phonelines():
# crypt
keycrypt = Crypt(fn=PATH_CRYPT_OP_KEYS)
2020-09-14 17:43:56 +00:00
# print(keycrypt.fn)
2020-09-12 16:08:47 +00:00
# Operator
2020-09-29 13:09:56 +00:00
op_keypair = ComradAsymmetricKey()
2020-09-12 16:08:47 +00:00
op_pubkey,op_privkey = op_keypair.pubkey_obj,op_keypair.privkey_obj
2020-09-17 05:50:51 +00:00
op_uri = op_pubkey.data_b64
2020-09-17 14:12:39 +00:00
2020-09-29 13:09:56 +00:00
op_privkey_decr = ComradSymmetricKeyWithoutPassphrase()
op_privkey_encr = ComradEncryptedAsymmetricPrivateKey(
2020-09-17 14:11:28 +00:00
data=op_privkey_decr.encrypt(op_privkey.data)
)
2020-09-17 15:22:44 +00:00
2020-09-18 05:52:20 +00:00
## world
2020-09-29 13:09:56 +00:00
world_keypair = ComradAsymmetricKey()
2020-09-18 05:52:20 +00:00
world_pubkey,world_privkey = world_keypair.pubkey_obj,world_keypair.privkey_obj
world_uri = world_pubkey.data_b64
2020-09-29 13:09:56 +00:00
world_privkey_decr = ComradSymmetricKeyWithoutPassphrase()
world_privkey_encr = ComradEncryptedAsymmetricPrivateKey(
2020-09-18 05:51:34 +00:00
data=world_privkey_decr.encrypt(world_privkey.data)
)
2020-09-12 16:08:47 +00:00
keycrypt.set(OPERATOR_NAME,op_pubkey.data,prefix='/pubkey/')
2020-09-13 06:56:04 +00:00
keycrypt.set(op_uri,OPERATOR_NAME,prefix='/name/')
2020-09-18 05:51:34 +00:00
op_world_pkg = [
op_privkey_decr.data,
op_privkey_encr.data,
world_privkey_decr.data,
world_privkey_encr.data
]
2020-09-14 17:43:56 +00:00
with open(PATH_SUPER_SECRET_OP_KEY,'wb') as of:
2020-09-18 05:51:34 +00:00
of.write(b64enc(BSEP.join(op_world_pkg)))
2020-09-14 17:56:32 +00:00
with open(PATH_OPERATOR_WEB_KEYS_FILE,'w') as of:
of.write(op_pubkey.data_b64_s)
2020-09-12 16:08:47 +00:00
## Telephone
2020-09-29 13:09:56 +00:00
phone_keypair = ComradAsymmetricKey()
2020-09-12 16:08:47 +00:00
phone_pubkey,phone_privkey = phone_keypair.pubkey_obj,phone_keypair.privkey_obj
2020-09-17 05:50:51 +00:00
phone_uri = phone_pubkey.data_b64
2020-09-12 16:08:47 +00:00
keycrypt.set(TELEPHONE_NAME,phone_pubkey.data,prefix='/pubkey/')
2020-09-13 06:56:04 +00:00
keycrypt.set(phone_uri,TELEPHONE_NAME,prefix='/name/')
2020-09-12 16:08:47 +00:00
2020-09-18 05:52:20 +00:00
2020-09-17 05:54:05 +00:00
2020-09-18 05:51:34 +00:00
2020-09-17 05:54:05 +00:00
2020-09-12 16:08:47 +00:00
keycrypt.set(WORLD_NAME,world_pubkey.data,prefix='/pubkey/')
2020-09-13 06:56:04 +00:00
keycrypt.set(world_uri,WORLD_NAME,prefix='/name/')
2020-09-17 05:54:05 +00:00
# keycrypt.set(world_uri,world_privkey.data,prefix='/privkey/')
2020-09-18 05:51:34 +00:00
#keycrypt.set(world_uri,world_privkey_encr.data,prefix='/privkey_encr/')
#keycrypt.set(world_uri,world_privkey_decr.data,prefix='/privkey_decr/')
2020-09-12 16:08:47 +00:00
# keys to save built in
2020-09-12 16:45:21 +00:00
builtin_keys = {
OPERATOR_NAME: {
2020-09-12 16:08:47 +00:00
'pubkey':op_pubkey.data,
},
2020-09-12 16:45:21 +00:00
TELEPHONE_NAME: {
2020-09-12 16:08:47 +00:00
'pubkey':phone_pubkey.data,
2020-09-12 16:38:23 +00:00
'privkey':phone_privkey.data,
2020-09-12 16:08:47 +00:00
},
2020-09-12 16:45:21 +00:00
WORLD_NAME: {
2020-09-12 16:08:47 +00:00
'pubkey':world_pubkey.data,
}
2020-09-12 16:45:21 +00:00
}
2020-09-12 16:08:47 +00:00
import pickle
from base64 import b64encode
builtin_keys_b = pickle.dumps(builtin_keys)
2020-09-29 13:09:56 +00:00
# omega_key = ComradSymmetricKeyWithoutPassphrase()
2020-09-12 18:18:37 +00:00
# builtin_keys_b_encr = omega_key.encrypt(builtin_keys_b)
# builtin_keys_b_encr_with_key = omega_key.data + BSEP + builtin_keys_b_encr
# builtin_keys_b_encr_with_key_b64 = b64encode(builtin_keys_b_encr_with_key)
builtin_keys_b64 = b64encode(builtin_keys_b)
2020-09-12 16:08:47 +00:00
with open(PATH_BUILTIN_KEYCHAIN,'wb') as of:
2020-09-12 18:18:37 +00:00
of.write(builtin_keys_b64)
# of.write(builtin_keys_b_encr_with_key_b64)
2020-09-12 16:08:47 +00:00
print('>> saved:',PATH_BUILTIN_KEYCHAIN)
2020-09-12 18:18:37 +00:00
# print(builtin_keys_b_encr_with_key_b64)
print(builtin_keys_b64)
2020-09-12 16:08:47 +00:00
2020-09-12 15:00:38 +00:00
2020-09-12 20:57:09 +00:00
2020-09-12 15:00:38 +00:00
2020-09-12 16:33:54 +00:00
def check_phonelines():
# if needed
create_secret()
# is world there?
2020-09-12 16:34:57 +00:00
keycrypt = Crypt(fn=PATH_CRYPT_OP_KEYS)
2020-09-12 16:33:54 +00:00
# builtins
with open(PATH_BUILTIN_KEYCHAIN,'rb') as f:
2020-09-12 18:18:37 +00:00
# builtin_keys_encr_b64 = f.read()
builtin_keys_b64 = f.read()
# builtin_keys_encr = b64decode(builtin_keys_encr_b64)
# omega_key_b,builtin_keys_encr = builtin_keys_encr.split(BSEP)
2020-09-29 13:09:56 +00:00
# omega_key = ComradSymmetricKeyWithoutPassphrase(omega_key_b)
2020-09-12 18:18:37 +00:00
# builtin_keys_b = omega_key.decrypt(builtin_keys_encr)
builtin_keys = pickle.loads(b64decode(builtin_keys_b64))
2020-09-12 16:33:54 +00:00
# print(builtin_keys)
2020-09-12 20:28:29 +00:00
for name in builtin_keys:
pubkey=builtin_keys[name]['pubkey']
uri_id=b64encode(pubkey)
2020-09-12 20:57:09 +00:00
# print(name,pubkey,uri_id)
2020-09-12 20:30:54 +00:00
keycrypt.set(
uri_id.decode(),
name,
prefix=f'/name/',override=True
)
2020-09-12 20:28:29 +00:00
for keyname,keyval in builtin_keys[name].items():
uri=name if keyname=='pubkey' else uri_id
2020-09-12 20:30:54 +00:00
keycrypt.set(
uri,
keyval,
prefix=f'/{keyname}/',override=True
)
2020-09-12 17:00:06 +00:00
# make sure world's qr is there too
ofnfn = os.path.join(PATH_QRCODES,WORLD_NAME+'.png')
if not os.path.exists(ofnfn):
world_uri = b64encode(builtin_keys[WORLD_NAME]['pubkey'])
import pyqrcode
qr = pyqrcode.create(world_uri)
qr.png(ofnfn,scale=5)
#print('>> saved:',ofnfn)
2020-09-12 16:33:54 +00:00
return builtin_keys
2020-09-12 15:00:38 +00:00
2020-09-12 16:33:54 +00:00
def test_phonelines():
2020-09-29 13:09:56 +00:00
from comrad.backend.the_telephone import TheTelephone
from comrad.backend.the_operator import TheOperator
2020-09-12 19:11:54 +00:00
from getpass import getpass
2020-09-10 10:42:27 +00:00
phone = TheTelephone()
2020-09-12 19:11:54 +00:00
op = TheOperator(passphrase=getpass())
2020-09-12 16:33:54 +00:00
2020-09-12 19:20:14 +00:00
print('phone',dict_format(phone.keychain()))
print('op',dict_format(op.keychain()))
2020-09-12 16:33:54 +00:00
2020-09-12 19:20:14 +00:00
print(op.privkey)
2020-09-12 16:33:54 +00:00
if __name__ == '__main__':
test_phonelines()