Exemplo n.º 1
0
    def generate(self, userid: str, server: str, wid: str,
                 pw: encryption.Password) -> RetVal:
        '''Creates all the data needed for an individual workspace account'''

        self.uid = userid
        self.wid = wid
        self.domain = server

        # Add workspace
        status = self.add_to_db(pw)
        if status.error():
            return status

        address = '/'.join([wid, server])

        # Generate user's encryption keys
        keys = {
            'identity': encryption.EncryptionPair(),
            'conrequest': encryption.EncryptionPair(),
            'broadcast': encryption.SecretKey(),
            'folder': encryption.SecretKey()
        }

        # Add encryption keys
        for key in keys.values():
            out = auth.add_key(self.db, key, address)
            if out.error():
                status = self.remove_workspace_entry(wid, server)
                if status.error():
                    return status

        # Add folder mappings
        foldermap = encryption.FolderMapping()

        folderlist = [
            'messages', 'contacts', 'events', 'tasks', 'notes', 'files',
            'files attachments'
        ]

        for folder in folderlist:
            foldermap.MakeID()
            foldermap.Set(address, keys['folder'].get_id(), folder, 'root')
            self.add_folder(foldermap)

        # Create the folders themselves
        try:
            self.path.mkdir(parents=True, exist_ok=True)
        except Exception as e:
            self.remove_from_db()
            return RetVal(ExceptionThrown, e.__str__())

        self.path.joinpath('files').mkdir(exist_ok=True)
        self.path.joinpath('files', 'attachments').mkdir(exist_ok=True)

        self.set_userid(userid)
        return RetVal()
Exemplo n.º 2
0
def get_key(db: sqlite3.Connection, keyid: str) -> RetVal:
    '''Gets the specified key.
	Parameters:
	keyid : uuid

	Returns:
	'error' : string
	'key' : CryptoKey object
	'''

    cursor = db.cursor()
    cursor.execute(
        '''
		SELECT address,type,category,private,public,algorithm
		FROM keys WHERE keyid=?''', (keyid, ))
    results = cursor.fetchone()
    if not results or not results[0]:
        return RetVal(ResourceNotFound)

    if results[1] == 'asymmetric':
        public = base64.b85decode(results[4])
        private = base64.b85decode(results[3])
        key = encryption.EncryptionPair(public, private)
        return RetVal().set_value('key', key)

    if results[1] == 'symmetric':
        private = base64.b85decode(results[3])
        key = encryption.SecretKey(private)
        return RetVal().set_value('key', key)

    return RetVal(BadParameterValue, "Key must be 'asymmetric' or 'symmetric'")
Exemplo n.º 3
0
def test_secretkey_encrypt_decrypt():
    '''Tests SecretKey encryption/decryption'''

    testdata = b'1234567890'

    sk = encryption.SecretKey()
    encdata = sk.encrypt(testdata)

    newdata = sk.decrypt(encdata)
    assert testdata == newdata, "Decrypted data didn't match"
Exemplo n.º 4
0
def test_secretkey_save():
    '''Tests the save code of the SecretKey class'''
    test_folder = setup_test('encryption_secretkey_save')

    key = CryptoString(r"XSALSA20:J~T^ko3HCFb$1Z7NudpcJA-dzDpF52IF1Oysh+CY")
    sk = encryption.SecretKey(key)

    key_path = os.path.join(test_folder, 'testkey.jk')
    status = sk.save(key_path)
    assert not status.error(), "Failed to create saved encryption pair file"

    fhandle = open(key_path)
    filedata = json.load(fhandle)
    fhandle.close()

    assert filedata['SecretKey'] == key.as_string(
    ), "Saved data does not match input data"
Exemplo n.º 5
0
def test_secretkey_load():
    '''Tests the load code of the SecretKey class'''
    test_folder = setup_test('encryption_secretkey_load')

    key = CryptoString(r"XSALSA20:J~T^ko3HCFb$1Z7NudpcJA-dzDpF52IF1Oysh+CY")
    sk = encryption.SecretKey(key)

    key_path = os.path.join(test_folder, 'testkey.jk')
    status = sk.save(key_path)
    assert not status.error(
    ), f"Failed to create saved secret key file: {status.info()}"

    status = encryption.load_secretkey(key_path)
    assert not status.error(
    ), f"Failed to load saved secret key file: {status.info()}"

    testpair = status['key']

    assert testpair.type == sk.type, "Loaded data does not match input data"
    assert testpair.enctype == sk.enctype, "Loaded data does not match input data"
    assert testpair.key == key, "Loaded data does not match input data"