2
0
mirror of https://github.com/ComradCollective/Comrad synced 2024-11-07 15:20:23 +00:00
Comrad/komrade/backend/crypt.py

247 lines
6.8 KiB
Python
Raw Normal View History

2020-09-03 21:11:58 +00:00
"""
Storage for both keys and data
"""
2020-09-06 17:59:38 +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-03 21:11:58 +00:00
from simplekv.fs import FilesystemStore
2020-09-17 14:37:24 +00:00
# from simplekv.memory.redisstore import RedisStore
# import redis
2020-09-03 21:11:58 +00:00
import hashlib,os
import zlib
2020-09-17 14:36:10 +00:00
from pythemis.exception import ThemisError
2020-09-06 17:59:38 +00:00
2020-09-03 21:11:58 +00:00
2020-09-15 06:42:42 +00:00
LOG_GET_SET = 0
2020-09-03 21:11:58 +00:00
2020-09-17 08:16:52 +00:00
2020-09-04 12:46:22 +00:00
class Crypt(Logger):
2020-09-17 14:22:41 +00:00
def __init__(self,
name=None,
fn=None,
use_secret=CRYPT_USE_SECRET,
path_secret=PATH_CRYPT_SECRET,
2020-09-17 15:15:15 +00:00
encrypt_values=False,
2020-09-17 14:56:56 +00:00
encryptor_func=None,
decryptor_func=None):
2020-09-17 14:22:41 +00:00
# defaults
2020-09-03 21:11:58 +00:00
if not name and fn: name=os.path.basename(fn).replace('.','_')
2020-09-17 14:22:41 +00:00
self.name,self.fn=name,fn
2020-09-03 21:11:58 +00:00
2020-09-17 14:56:56 +00:00
2020-09-17 14:22:41 +00:00
# use secret? for salting
2020-09-10 09:00:11 +00:00
if use_secret and path_secret:
if not os.path.exists(path_secret):
self.secret = get_random_binary_id()
2020-09-12 15:02:53 +00:00
from komrade.backend.keymaker import make_key_discreet
2020-09-12 15:02:29 +00:00
self.log('shhh! creating secret:',make_key_discreet(self.secret))
2020-09-10 09:00:11 +00:00
with open(path_secret,'wb') as of:
of.write(self.secret)
else:
with open(path_secret,'rb') as f:
self.secret = f.read()
else:
self.secret = b''
2020-09-12 20:46:49 +00:00
self.encrypt_values = encrypt_values
2020-09-17 14:56:56 +00:00
if self.secret and encrypt_values and (not encryptor_func or not decryptor_func):
2020-09-17 14:58:16 +00:00
from komrade.backend.keymaker import KomradeSymmetricKeyWithPassphrase
2020-09-17 14:56:56 +00:00
self.key = KomradeSymmetricKeyWithPassphrase(
passphrase=self.secret
)
encryptor_func = self.key.encrypt
decryptor_func = self.key.decrypt
2020-09-17 14:22:41 +00:00
self.encryptor_func=encryptor_func
self.decryptor_func=decryptor_func
2020-09-03 21:11:58 +00:00
self.store = FilesystemStore(self.fn)
2020-09-17 14:22:41 +00:00
2020-09-04 12:46:22 +00:00
def log(self,*x):
if LOG_GET_SET:
super().log(*x)
2020-09-03 21:11:58 +00:00
def hash(self,binary_data):
2020-09-12 14:32:03 +00:00
return hasher(binary_data,self.secret)
2020-09-03 21:11:58 +00:00
def force_binary(self,k_b):
2020-09-06 18:40:56 +00:00
if k_b is None: return None
2020-09-03 21:11:58 +00:00
if type(k_b)==str: k_b=k_b.encode()
2020-09-06 14:45:40 +00:00
if type(k_b)!=bytes: k_b=k_b.decode()
2020-09-03 21:11:58 +00:00
return k_b
2020-09-04 00:06:47 +00:00
def package_key(self,k,prefix=''):
2020-09-06 18:43:18 +00:00
if not k: return b''
2020-09-03 21:11:58 +00:00
k_b = self.force_binary(k)
2020-09-06 18:12:18 +00:00
k_b2 = self.force_binary(prefix) + k_b
2020-09-06 18:24:51 +00:00
return k_b2
2020-09-03 21:11:58 +00:00
2020-09-17 14:36:10 +00:00
def package_val(self,k,encrypt=None):
if encrypt is None: encrypt=self.encrypt_values
2020-09-03 21:11:58 +00:00
k_b = self.force_binary(k)
2020-09-17 14:36:10 +00:00
if encrypt:
try:
k_b = self.encryptor_func(k_b)
except ThemisError as e:
self.log('!! ENCRYPTION ERROR:',e)
2020-09-12 20:19:53 +00:00
return k_b
2020-09-03 21:11:58 +00:00
2020-09-17 14:36:10 +00:00
def unpackage_val(self,k_b,encrypt=None):
if encrypt is None: encrypt=self.encrypt_values
2020-09-17 14:43:31 +00:00
if encrypt:
2020-09-17 14:36:10 +00:00
try:
k_b = self.decryptor_func(k_b)
2020-09-17 14:40:44 +00:00
except ThemisError as e:
2020-09-17 14:36:10 +00:00
self.log('!! DECRYPTION ERROR:',e)
2020-09-12 20:19:53 +00:00
return k_b
2020-09-03 21:11:58 +00:00
2020-09-10 09:00:11 +00:00
def has(self,k,prefix=''):
2020-09-13 12:01:44 +00:00
return bool(self.get(k,prefix=prefix))
2020-09-10 09:00:11 +00:00
2020-09-03 21:11:58 +00:00
2020-09-17 14:36:10 +00:00
def set(self,k,v,prefix='',override=False,encrypt=True):
2020-09-15 06:16:27 +00:00
if self.has(k,prefix=prefix) and not override:
2020-09-17 14:22:41 +00:00
self.log(f"I'm afraid I can't let you do that, overwrite someone's data!\n\nat {prefix}{k} = {v}")
2020-09-13 10:53:17 +00:00
return False #(False,None,None)
2020-09-10 09:00:11 +00:00
2020-09-04 00:06:47 +00:00
k_b=self.package_key(k,prefix=prefix)
2020-09-06 18:24:51 +00:00
k_b_hash = self.hash(k_b)
2020-09-17 14:36:10 +00:00
v_b=self.package_val(v,encrypt = (self.encrypt_values and encrypt))
2020-09-15 06:42:42 +00:00
if not override:
self.log(f'''Crypt.set(\n\t{k_b}\n\n\t{k_b_hash}\n\n\t{v_b}\n)''')
2020-09-13 12:11:11 +00:00
self.store.put(k_b_hash,v_b)
2020-09-13 10:53:17 +00:00
return True
2020-09-03 21:11:58 +00:00
2020-09-05 14:09:31 +00:00
def exists(self,k,prefix=''):
2020-09-10 09:00:11 +00:00
return self.has(k,prefix=prefix)
2020-09-05 14:09:31 +00:00
2020-09-10 12:52:07 +00:00
def key2hash(self,k,prefix=''):
return self.hash(
self.package_key(
prefix + k
)
)
2020-09-14 04:43:59 +00:00
def delete(self,k,prefix=''):
k_b=self.package_key(k,prefix=prefix)
k_b_hash = self.hash(k_b)
r=self.store.delete(k_b_hash)
return r
2020-09-04 00:06:47 +00:00
def get(self,k,prefix=''):
k_b=self.package_key(k,prefix=prefix)
2020-09-06 18:32:03 +00:00
k_b_hash = self.hash(k_b)
2020-09-04 00:06:47 +00:00
try:
2020-09-06 18:32:03 +00:00
v=self.store.get(k_b_hash)
2020-09-04 00:06:47 +00:00
except KeyError:
return None
2020-09-03 21:11:58 +00:00
v_b=self.unpackage_val(v)
return v_b
class KeyCrypt(Crypt):
def __init__(self):
2020-09-06 09:33:46 +00:00
return super().__init__(name=PATH_CRYPT_CA_KEYS.replace('.','_'))
2020-09-03 21:11:58 +00:00
class DataCrypt(Crypt):
def __init__(self):
2020-09-06 09:33:46 +00:00
return super().__init__(name=PATH_CRYPT_CA_DATA.replace('.','_'))
2020-09-03 21:11:58 +00:00
2020-09-17 08:16:52 +00:00
class CryptList(Crypt): # like inbox
def __init__(self,
crypt,
keyname,
prefix='',
encryptor_func=lambda x: x,
decryptor_func=lambda x: x):
self.crypt=crypt
self.keyname=keyname
self.prefix=prefix
self.encryptor_func=encryptor_func
self.decryptor_func=decryptor_func
2020-09-06 16:48:47 +00:00
2020-09-17 08:16:52 +00:00
@property
def val_b_encr(self):
return self.crypt.get(
self.keyname,
prefix=self.prefix
)
2020-09-06 16:48:47 +00:00
2020-09-17 08:16:52 +00:00
@property
def val_b(self):
val_b_encr=self.val_b_encr
if not val_b_encr: return None
return self.decryptor_func(val_b_encr)
@property
def values(self):
2020-09-17 14:22:41 +00:00
val_b=self.val_b
2020-09-17 08:16:52 +00:00
if not val_b: return []
return pickle.loads(val_b)
2020-09-17 10:37:58 +00:00
def prepend(self,x_l):
2020-09-17 08:16:52 +00:00
return self.append(x,insert=0)
2020-09-17 10:37:58 +00:00
def append(self,x_l,insert=None):
if type(x_l)!=list: x_l=[x_l]
2020-09-17 08:16:52 +00:00
val_l = self.values
2020-09-17 10:37:58 +00:00
x_l = [x for x in x_l if not x in set(val_l)]
2020-09-17 08:16:52 +00:00
# print('val_l =',val_l)
2020-09-17 10:37:58 +00:00
for x in x_l:
if insert is not None:
val_l.insert(insert,x)
else:
val_l.append(x)
# print('val_l2 =',val_l)
2020-09-17 08:16:52 +00:00
return self.set(val_l)
def set(self,val_l):
val_b = pickle.dumps(val_l)
val_b_encr = self.encryptor_func(val_b)
return self.crypt.set(
self.keyname,
val_b_encr,
prefix=self.prefix,
override=True
)
def remove(self,l):
if type(l)!=list: l=[l]
lset=set(l)
values = [x for x in self.values if x not in lset]
return self.set(values)
2020-09-06 16:48:47 +00:00
2020-09-03 21:11:58 +00:00
if __name__=='__main__':
crypt = Crypt('testt')
2020-09-17 08:16:52 +00:00
from komrade import KomradeSymmetricKeyWithPassphrase
key = KomradeSymmetricKeyWithPassphrase()
crypt_list = CryptList(
crypt=crypt,
keyname='MyInbox2',
prefix='inbox',
encryptor_func=key.encrypt,
decryptor_func=key.decrypt
)
print(crypt_list.values)
print(crypt_list.remove('cool thing 0'))
# print(crypt_list.append('cool thing 1'))
print(crypt_list.prepend('cool thing 0'))
2020-09-03 21:11:58 +00:00
2020-09-17 08:16:52 +00:00
print(crypt_list.values)