2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-11 13:10:45 +00:00
This commit is contained in:
quadrismegistus 2020-09-07 23:27:29 +01:00
parent 7dfedf8a32
commit f2251cceed
2 changed files with 64 additions and 21 deletions

View File

@ -123,8 +123,8 @@ class Keymaker(Logger):
def getkey(self, keyname, keychain={}, uri=None): def getkey(self, keyname, keychain={}, uri=None):
# cached? # cached?
if hasattr(self,'_'+keyname) and getattr(self,'_'+keyname): # if hasattr(self,'_'+keyname) and getattr(self,'_'+keyname):
return getattr(self,'_'+keyname) # return getattr(self,'_'+keyname)
if keyname in self._keychain: return self._keychain[keyname] if keyname in self._keychain: return self._keychain[keyname]
# self.log(f'keyname={keyname}, keychain={keychain.keys()}, uri={uri}') # self.log(f'keyname={keyname}, keychain={keychain.keys()}, uri={uri}')
@ -176,30 +176,36 @@ class Keymaker(Logger):
# decrypt! # decrypt!
try: try:
# self.log(f'>> decrypting {key_encr_name} with {key_decr_name}\n({key_encr} with cell {decr_cell}') self.log(f'>> decrypting {key_encr_name} with {key_decr_name}\n({key_encr} with cell {decr_cell}')
key = decr_cell.decrypt(key_encr) key = decr_cell.decrypt(key_encr)
# self.log('assembled_key built:',key) # self.log('assembled_key built:',key)
return key return key
except ThemisError as e: except ThemisError as e:
# self.log('!! decryption failed:',e) self.log('!! decryption failed:',e)
return return
# Concrete keys # Concrete keys
## (1) Final keys ## (1) Final keys
def pubkey(self, force=False, **kwargs): def pubkey(self, force=False, **kwargs):
if force or not hasattr(self,'_pubkey') or not self._pubkey: # if force or not hasattr(self,'_pubkey') or not self._pubkey:
self._pubkey = self.getkey(keyname='pubkey',uri=self.name,**kwargs) # self._pubkey = self.getkey(keyname='pubkey',uri=self.name,**kwargs)
return self._pubkey # return self._pubkey
x=self.getkey(keyname='pubkey',uri=self.name,**kwargs)
print('weee',x)
return x
def privkey(self, force=False, **kwargs): def privkey(self, force=False, **kwargs):
if force or not hasattr(self,'_privkey') or not self._privkey: # if force or not hasattr(self,'_privkey') or not self._privkey:
self._privkey=self.getkey(keyname='privkey',uri=self.pubkey(**kwargs),**kwargs) # self._privkey=self.getkey(keyname='privkey',uri=self.pubkey(**kwargs),**kwargs)
return self._privkey # return self._privkey
return self.getkey(keyname='privkey',uri=self.pubkey(**kwargs),**kwargs)
def adminkey(self, force=False, **kwargs): def adminkey(self, force=False, **kwargs):
if force or not hasattr(self,'_adminkey') or not self._adminkey: # if force or not hasattr(self,'_adminkey') or not self._adminkey:
self._adminkey=self.getkey(keyname='adminkey',uri=self.privkey(**kwargs),**kwargs) # self._adminkey=self.getkey(keyname='adminkey',uri=self.privkey(**kwargs),**kwargs)
return self._adminkey # return self._adminkey
return self.getkey(keyname='adminkey',uri=self.privkey(**kwargs),**kwargs)
## (1-X) Encrypted halves ## (1-X) Encrypted halves
def pubkey_encr(self, **kwargs): def pubkey_encr(self, **kwargs):
@ -562,26 +568,62 @@ class Keymaker(Logger):
return valid_kc return valid_kc
def assemble(self,_keychain):
# last minute assemblies?
encr_keys = [k for k in _keychain if k.endswith('_encr')]
for ekey in encr_keys:
eval=_keychain[ekey]
if not eval: continue
unencrkey = ekey[:-len('_encr')]
if unencrkey in _keychain: continue
decrkey = unencrkey+'_decr'
if decrkey not in _keychain: continue
dval=_keychain[decrkey]
if not dval: continue
self.log(ekey,decrkey,'??')
self.log(eval,dval,'????')
new_val = self.assemble_key(eval,dval)
self.log('!!#!',new_val)
if new_val:
_keychain[unencrkey] = new_val
return _keychain
def keychain(self,passphrase=None,force=False,allow_builtin=True,extra_keys={},keys_to_gen=KEYMAKER_DEFAULT_KEYS_TO_GEN,**kwargs): def keychain(self,passphrase=None,force=False,allow_builtin=True,extra_keys={},keys_to_gen=KEYMAKER_DEFAULT_KEYS_TO_GEN,**kwargs):
# assemble as many keys as we can! # assemble as many keys as we can!
# if not force and hasattr(self,'_keychain') and self._keychain: return self._keychain # if not force and hasattr(self,'_keychain') and self._keychain: return self._keychain
if passphrase: self.passphrase=passphrase if passphrase: self.passphrase=passphrase
_keychain = {**extra_keys, **self._keychain} _keychain = {**extra_keys, **self._keychain}
self.log('_keychain at start of keychain() =',_keychain) self.log('_keychain at start of keychain() =',_keychain)
for keyname in keys_to_gen: for keyname in keys_to_gen+keys_to_gen:
if keyname in _keychain and _keychain[keyname]: continue # if keyname in _keychain and _keychain[keyname]: continue
# self.log('??',keyname,'...') # self.log('??',keyname,keyname in self._keychain,'...')
if hasattr(self,keyname): if hasattr(self,keyname):
method=getattr(self,keyname) method=getattr(self,keyname)
res=method(keychain=_keychain, **kwargs) res=method(keychain=_keychain, **kwargs)
# self.log('res <--',res) # self.log('res <--',res)
if res: if res:
_keychain[keyname]=res _keychain[keyname]=res
_keychain = self.assemble(_keychain)
_keychain = self.assemble(_keychain)
self._keychain = _keychain self._keychain = _keychain
return _keychain return _keychain
return _keychain
if __name__ == '__main__': if __name__ == '__main__':
keymaker = Keymaker('marx69') keymaker = Keymaker('marx69')
keychain = keymaker.forge_new_keys() keychain = keymaker.forge_new_keys()

View File

@ -131,10 +131,14 @@ class Operator(Keymaker):
encrypted_message_from_telephone_to_op = b'' encrypted_message_from_telephone_to_op = b''
encrypted_message_from_caller_to_op = b'' encrypted_message_from_caller_to_op = b''
encrypted_message_from_caller_to_caller = b'' encrypted_message_from_caller_to_caller = b''
if not from_phone: from_phone=self.phone
if not to_phone: to_phone=self.op
from_phone_keychain = from_phone.keychain() from_phone_keychain = from_phone.keychain()
self.log('from_phone',type(from_phone),'to_phone',type(to_phone))
self.log('from_phone_keychain',from_phone_keychain)
# self.log('to_phone_keychain',to_phone_keychain)
eeeee
from_phone_pubkey_encr=from_phone_keychain.get('pubkey_encr') from_phone_pubkey_encr=from_phone_keychain.get('pubkey_encr')
from_phone_privkey=from_phone_keychain.get('privkey') from_phone_privkey=from_phone_keychain.get('privkey')
@ -142,9 +146,6 @@ class Operator(Keymaker):
to_phone_pubkey_decr=to_phone_keychain.get('pubkey_decr') to_phone_pubkey_decr=to_phone_keychain.get('pubkey_decr')
to_phone_pubkey=to_phone_keychain.get('pubkey') to_phone_pubkey=to_phone_keychain.get('pubkey')
self.log('data_encr_phone2phone',data_encr_phone2phone)
self.log('from_phone_pubkey',from_phone_pubkey,from_phone)
self.log('to_phone_privkey',to_phone_privkey,to_phone)
### LAYERS OF ENCRYPTION: ### LAYERS OF ENCRYPTION:
# 1) unencr header # 1) unencr header