2022-12-09 22:55:33 +01:00
|
|
|
import json
|
2022-12-10 17:30:27 +01:00
|
|
|
from pathlib import Path
|
2022-12-10 22:03:29 +01:00
|
|
|
|
2022-12-10 22:19:57 +01:00
|
|
|
class Decryption():
|
2022-12-09 20:10:12 +01:00
|
|
|
|
2022-12-10 22:19:57 +01:00
|
|
|
def __init__(self,cli,paths):
|
2022-12-09 20:10:12 +01:00
|
|
|
self.user_id='0';
|
|
|
|
self.user_password=''
|
2022-12-10 22:03:29 +01:00
|
|
|
self.cli = cli
|
2022-12-10 22:19:57 +01:00
|
|
|
self.paths = paths
|
2022-12-09 20:10:12 +01:00
|
|
|
|
2022-12-10 12:21:43 +01:00
|
|
|
def initializeUser(self,user_id):
|
2022-12-09 20:10:12 +01:00
|
|
|
self.user_id=str(user_id)
|
2022-12-10 22:19:57 +01:00
|
|
|
self.user_file_decrypted_path = self.paths.getUserFilePath(self.user_id,self.paths.TYPE_DECRYPTED)
|
2022-12-10 12:21:43 +01:00
|
|
|
|
|
|
|
def initializeUserDataDecryption(self):
|
|
|
|
self.decryptUserFile()
|
|
|
|
self.user_data = self.loadJsonFile(self.user_file_decrypted_path)
|
|
|
|
self.initializeNeededDecryptersAmount()
|
|
|
|
self.initializeValidDecrypterIds()
|
2022-12-10 17:30:27 +01:00
|
|
|
|
|
|
|
def initializeGroupDataEncryption(self):
|
|
|
|
self.group_name = self.getDecryptersGroupName()
|
2022-12-10 22:19:57 +01:00
|
|
|
self.encrypted_group_file_path = self.paths.getGroupFilePath(self.group_name, self.paths.TYPE_DECRYPTED)
|
2022-12-10 17:30:27 +01:00
|
|
|
self.decryptGroupFile()
|
2022-12-10 21:20:26 +01:00
|
|
|
self.master_password = self.loadTxtFile(self.encrypted_group_file_path).strip()
|
2022-12-10 12:21:43 +01:00
|
|
|
|
|
|
|
def initializeNeededDecryptersAmount(self):
|
|
|
|
self.needed_decrypters_amount = len(str(list(self.user_data['groups'].keys())[0]))
|
|
|
|
|
|
|
|
def initializeValidDecrypterIds(self):
|
|
|
|
self.valid_decrypter_ids = []
|
|
|
|
self.valid_decrypter_ids.append(int(self.user_id))
|
|
|
|
for contact_id in self.user_data['contacts']:
|
|
|
|
self.valid_decrypter_ids.append(int(contact_id))
|
2022-12-09 20:10:12 +01:00
|
|
|
|
|
|
|
def setUserPassword(self,user_password):
|
|
|
|
self.user_password = str(user_password)
|
2022-12-10 12:21:43 +01:00
|
|
|
|
|
|
|
def resetDecrypterIds(self):
|
|
|
|
self.decrypter_ids = []
|
|
|
|
self.addDecrypterId(self.user_id)
|
2022-12-10 14:42:11 +01:00
|
|
|
|
|
|
|
def resetPasswordShare(self):
|
|
|
|
self.password_parts = {}
|
|
|
|
self.addPasswordShare(self.user_id,self.getPasswordShare())
|
|
|
|
|
|
|
|
def addPasswordShare(self,user_id,password_share):
|
|
|
|
self.password_parts[str(user_id)] = password_share
|
|
|
|
|
2022-12-10 17:30:27 +01:00
|
|
|
def getGroupPassword(self):
|
2022-12-10 14:42:11 +01:00
|
|
|
shared_password = ''
|
|
|
|
for password_share_index in sorted(self.password_parts):
|
|
|
|
shared_password += str(self.password_parts[password_share_index])
|
|
|
|
return shared_password
|
2022-12-10 12:21:43 +01:00
|
|
|
|
2022-12-10 17:30:27 +01:00
|
|
|
def getMasterPassword(self):
|
|
|
|
return self.master_password
|
|
|
|
|
2022-12-10 12:21:43 +01:00
|
|
|
def addDecrypterId(self,decrypter_id):
|
|
|
|
decrypter_id = int(decrypter_id)
|
|
|
|
if decrypter_id not in self.valid_decrypter_ids:
|
|
|
|
raise Exception("The encrypter id is not valid. Valid encrypter ids are: " + str(self.valid_decrypter_ids))
|
|
|
|
if len(self.decrypter_ids) >= self.needed_decrypters_amount:
|
|
|
|
raise Exception("There are already sufficients decrypters (" + str(len(self.decrypter_ids)) + ") defined!")
|
|
|
|
if decrypter_id in self.decrypter_ids:
|
|
|
|
raise Exception("The decrypter is already in the list.")
|
|
|
|
self.decrypter_ids.append(decrypter_id)
|
2022-12-10 14:42:11 +01:00
|
|
|
|
|
|
|
def getUserId(self):
|
|
|
|
return self.user_id
|
|
|
|
|
|
|
|
def getCoDecrypterIds(self):
|
|
|
|
co_decrypter_ids = self.decrypter_ids[:]
|
|
|
|
co_decrypter_ids.remove(int(self.user_id))
|
|
|
|
return co_decrypter_ids
|
|
|
|
|
|
|
|
def getDecrypterIds(self):
|
2022-12-10 12:21:43 +01:00
|
|
|
return self.decrypter_ids
|
2022-12-10 14:42:11 +01:00
|
|
|
|
|
|
|
def getDecryptersGroupName(self):
|
|
|
|
self.decrypter_ids.sort()
|
|
|
|
return ''.join(str(x) for x in self.decrypter_ids)
|
|
|
|
|
|
|
|
def getPasswordShare(self):
|
|
|
|
return self.user_data['groups'][str(self.getDecryptersGroupName())]
|
2022-12-10 12:21:43 +01:00
|
|
|
|
2022-12-10 18:31:49 +01:00
|
|
|
def getNeededDecryptersAmount(self):
|
|
|
|
return self.needed_decrypters_amount
|
2022-12-09 20:10:12 +01:00
|
|
|
|
2022-12-10 17:30:27 +01:00
|
|
|
def loadTxtFile(self,file_path):
|
|
|
|
return Path(file_path).read_text()
|
|
|
|
|
2022-12-09 22:55:33 +01:00
|
|
|
def loadJsonFile(self,file_path):
|
|
|
|
file = open(file_path)
|
|
|
|
data = json.load(file)
|
|
|
|
file.close()
|
|
|
|
return data
|
|
|
|
|
2022-12-09 20:39:00 +01:00
|
|
|
def decryptFile(self,password,input_file_path,output_file_path):
|
2022-12-10 22:03:29 +01:00
|
|
|
self.cli.executeCommand('gpg --batch --passphrase "'+ password + '" -o "' + output_file_path +'" "'+ input_file_path+'"')
|
2022-12-09 20:10:12 +01:00
|
|
|
|
|
|
|
def decryptUserFile(self):
|
2022-12-10 22:19:57 +01:00
|
|
|
input_file_path = self.paths.getUserFilePath(self.user_id,self.paths.TYPE_ENCRYPTED)
|
2022-12-09 22:55:33 +01:00
|
|
|
self.decryptFile(self.user_password, input_file_path, self.user_file_decrypted_path)
|
2022-12-09 20:54:33 +01:00
|
|
|
|
2022-12-10 17:30:27 +01:00
|
|
|
def decryptGroupFile(self):
|
2022-12-10 22:19:57 +01:00
|
|
|
input_file_path = self.paths.getGroupFilePath(self.group_name, self.paths.TYPE_ENCRYPTED)
|
2022-12-10 17:30:27 +01:00
|
|
|
self.decryptFile(self.getGroupPassword(), input_file_path, self.encrypted_group_file_path)
|
|
|
|
|
2022-12-09 20:54:33 +01:00
|
|
|
def decryptAccumulatedFile(self):
|
2022-12-10 22:19:57 +01:00
|
|
|
input_file_path = self.paths.getAccumulatedFilePath(self.paths.TYPE_ENCRYPTED)
|
|
|
|
output_file_path = self.paths.getAccumulatedFilePath(self.paths.TYPE_DECRYPTED)
|
2022-12-10 20:19:26 +01:00
|
|
|
self.decryptFile(self.user_password, input_file_path, output_file_path)
|
|
|
|
|
|
|
|
def decryptMainData(self):
|
2022-12-11 10:19:35 +01:00
|
|
|
self.cli.executeCommand('gpg --batch --passphrase "' + self.getMasterPassword() + '" -d "' + self.paths.getEncryptedMainDataFile() + '" | tar --one-top-level="' + self.paths.getDecryptedMainDataStandartFolder() + '" -xvzf -')
|
|
|
|
print(self.cli.getCommandString())
|
|
|
|
print(self.cli.getOutputString())
|