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_encryptionpair_encrypt_decrypt():
    '''Test the encryption and decryption code for the EncryptionPair class'''

    public_key = CryptoString(
        r"CURVE25519:(B2XX5|<+lOSR>_0mQ=KX4o<aOvXe6M`Z5ldINd`")
    private_key = CryptoString(
        r"CURVE25519:(Rj5)mmd1|YqlLCUP0vE;YZ#o;tJxtlAIzmPD7b&")
    kp = encryption.EncryptionPair(public_key, private_key)

    test_data = 'This is some encryption test data'
    estatus = kp.encrypt(test_data.encode())
    assert not estatus.error(
    ), 'test_encryptionpair_encrypt_decrypt: error encrypting test data'

    dstatus = kp.decrypt(estatus['data'])
    assert not dstatus.error(
    ), 'test_encryptionpair_encrypt_decrypt: error decrypting test data'
    assert dstatus['data'] == test_data, 'decoded data mismatch'
Exemplo n.º 4
0
def test_encryptionpair_save():
    '''Tests the save code of the EncryptionPair class'''
    test_folder = setup_test('encryption_encryptionpair_save')

    public_key = CryptoString(
        "CURVE25519:(B2XX5|<+lOSR>_0mQ=KX4o<aOvXe6M`Z5ldINd`")
    private_key = CryptoString(
        "CURVE25519:(Rj5)mmd1|YqlLCUP0vE;YZ#o;tJxtlAIzmPD7b&")
    kp = encryption.EncryptionPair(public_key, private_key)

    keypair_path = os.path.join(test_folder, 'testpair.jk')
    status = kp.save(keypair_path)
    assert not status.error(
    ), f"Failed to create saved encryption pair file: {status.info()}"

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

    assert filedata['PublicKey'] == public_key.as_string(
    ), "Saved data does not match input data"
    assert filedata['PrivateKey'] == private_key.as_string(
    ), "Saved data does not match input data"
Exemplo n.º 5
0
def test_encryptionpair_load():
    '''Tests the load code of the EncryptionPair class'''
    test_folder = setup_test('encryption_encryptionpair_load')

    public_key = CryptoString(
        "CURVE25519:(B2XX5|<+lOSR>_0mQ=KX4o<aOvXe6M`Z5ldINd`")
    private_key = CryptoString(
        "CURVE25519:(Rj5)mmd1|YqlLCUP0vE;YZ#o;tJxtlAIzmPD7b&")
    kp = encryption.EncryptionPair(public_key, private_key)

    keypair_path = os.path.join(test_folder, 'testpair.jk')
    status = kp.save(keypair_path)
    assert not status.error(
    ), f"Failed to create saved encryption pair file: {status.info()}"

    status = encryption.load_encryptionpair(keypair_path)
    assert not status.error(
    ), f"Failed to load saved pair file: {status.info()}"

    testpair = status['keypair']

    assert testpair.enctype == kp.enctype, "Loaded data does not match input data"
    assert testpair.public == public_key, "Loaded data does not match input data"
    assert testpair.private == private_key, "Loaded data does not match input data"