account.py 8.86 KB
Newer Older
1
2
3
4
# -*- coding: utf-8 -*-
# libolm python bindings
# Copyright © 2015-2017 OpenMarket Ltd
# Copyright © 2018 Damir Jelić <poljar@termina.org.uk>
5
6
7
8
9
10
11
12
13
14
15
16
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""libolm Account module.

This module contains the account part of the Olm library. It contains a single
Account class which handles the creation of new accounts as well as the storing
and restoring of them.

Examples:
    >>> acc = Account()
    >>> account.identity_keys()
    >>> account.generate_one_time_keys(1)

"""

import json
# pylint: disable=redefined-builtin,unused-import
from builtins import bytes, super
from typing import AnyStr, Dict, Optional, Type

from future.utils import bytes_to_native_str

# pylint: disable=no-name-in-module
from _libolm import ffi, lib  # type: ignore

from ._compat import URANDOM, to_bytes
from ._finalize import track_for_finalization

# This is imported only for type checking purposes
if False:
    from .session import Session  # pragma: no cover


def _clear_account(account):
    # type: (ffi.cdata) -> None
    lib.olm_clear_account(account)


class OlmAccountError(Exception):
    """libolm Account error exception."""


class Account(object):
    """libolm Account class."""

    def __new__(cls):
        # type: (Type[Account]) -> Account
        obj = super().__new__(cls)
        obj._buf = ffi.new("char[]", lib.olm_account_size())
        obj._account = lib.olm_account(obj._buf)
        track_for_finalization(obj, obj._account, _clear_account)
        return obj

    def __init__(self):
        # type: () -> None
        """Create a new Olm account.

        Creates a new account and its matching identity key pair.

        Raises OlmAccountError on failure. If there weren't enough random bytes
        for the account creation the error message for the exception will be
        NOT_ENOUGH_RANDOM.
        """
        # This is needed to silence mypy not knowing the type of _account.
        # There has to be a better way for this.
        if False:  # pragma: no cover
            self._account = self._account  # type: ffi.cdata

        random_length = lib.olm_create_account_random_length(self._account)
        random = URANDOM(random_length)
        random_buffer = ffi.new("char[]", random)

        self._check_error(
            lib.olm_create_account(self._account, random_buffer,
                                   random_length))

    def _check_error(self, ret):
        # type: (int) -> None
        if ret != lib.olm_error():
            return

        last_error = bytes_to_native_str(
            ffi.string((lib.olm_account_last_error(self._account))))

        raise OlmAccountError(last_error)

    def pickle(self, passphrase=""):
        # type: (Optional[str]) -> bytes
        """Store an Olm account.

        Stores an account as a base64 string. Encrypts the account using the
        supplied passphrase. Returns a byte object containing the base64
        encoded string of the pickled account. Raises OlmAccountError on
        failure.

        Args:
            passphrase(str, optional): The passphrase to be used to encrypt
                the account.
        """
        byte_key = bytes(passphrase, "utf-8") if passphrase else b""
        key_buffer = ffi.new("char[]", byte_key)

        pickle_length = lib.olm_pickle_account_length(self._account)
        pickle_buffer = ffi.new("char[]", pickle_length)

        self._check_error(
            lib.olm_pickle_account(self._account, key_buffer, len(byte_key),
                                   pickle_buffer, pickle_length))
        return ffi.unpack(pickle_buffer, pickle_length)

    @classmethod
    def from_pickle(cls, pickle, passphrase=""):
        # type: (bytes, Optional[str]) -> Account
        """Load a previously stored olm account.

        Loads an account from a pickled base64-encoded string and returns an
        Account object. Decrypts the account using the supplied passphrase.
        Raises OlmAccountError on failure. If the passphrase doesn't match the
        one used to encrypt the account then the error message for the
        exception will be "BAD_ACCOUNT_KEY". If the base64 couldn't be decoded
        then the error message will be "INVALID_BASE64".

        Args:
            pickle(bytes): Base64 encoded byte string containing the pickled
                account
            passphrase(str, optional): The passphrase used to encrypt the
                account.
        """
        if not pickle:
            raise ValueError("Pickle can't be empty")

        byte_key = bytes(passphrase, "utf-8") if passphrase else b""
        key_buffer = ffi.new("char[]", byte_key)
        pickle_buffer = ffi.new("char[]", pickle)

        obj = cls.__new__(cls)

        ret = lib.olm_unpickle_account(obj._account, key_buffer, len(byte_key),
                                       pickle_buffer, len(pickle))
        obj._check_error(ret)

        return obj

    @property
    def identity_keys(self):
        # type: () -> Dict[str, str]
        """dict: Public part of the identity keys of the account."""
        out_length = lib.olm_account_identity_keys_length(self._account)
        out_buffer = ffi.new("char[]", out_length)

        self._check_error(
            lib.olm_account_identity_keys(self._account, out_buffer,
                                          out_length))
        return json.loads(ffi.unpack(out_buffer, out_length).decode("utf-8"))

    def sign(self, message):
        # type: (AnyStr) -> str
        """Signs a message with this account.

        Signs a message with the private ed25519 identity key of this account.
        Returns the signature.
        Raises OlmAccountError on failure.

        Args:
            message(str): The message to sign.
        """
        bytes_message = to_bytes(message)
        out_length = lib.olm_account_signature_length(self._account)
        message_buffer = ffi.new("char[]", bytes_message)
        out_buffer = ffi.new("char[]", out_length)

        self._check_error(
            lib.olm_account_sign(self._account, message_buffer,
                                 len(bytes_message), out_buffer, out_length))

        return bytes_to_native_str(ffi.unpack(out_buffer, out_length))

    @property
    def max_one_time_keys(self):
        # type: () -> int
        """int: The maximum number of one-time keys the account can store."""
        return lib.olm_account_max_number_of_one_time_keys(self._account)

    def mark_keys_as_published(self):
        # type: () -> None
        """Mark the current set of one-time keys as being published."""
        lib.olm_account_mark_keys_as_published(self._account)

    def generate_one_time_keys(self, count):
        # type: (int) -> None
        """Generate a number of new one-time keys.

        If the total number of keys stored by this account exceeds
        max_one_time_keys() then the old keys are discarded.
209
        Raises OlmAccountError on error.
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

        Args:
            count(int): The number of keys to generate.
        """
        random_length = lib.olm_account_generate_one_time_keys_random_length(
            self._account, count)
        random = URANDOM(random_length)
        random_buffer = ffi.new("char[]", random)
        self._check_error(
            lib.olm_account_generate_one_time_keys(
                self._account, count, random_buffer, random_length))

    @property
    def one_time_keys(self):
        # type: () -> Dict[str, Dict[str, str]]
        """dict: The public part of the one-time keys for this account."""
        out_length = lib.olm_account_one_time_keys_length(self._account)
        out_buffer = ffi.new("char[]", out_length)

        self._check_error(
            lib.olm_account_one_time_keys(self._account, out_buffer,
                                          out_length))

        return json.loads(ffi.unpack(out_buffer, out_length).decode("utf-8"))

    def remove_one_time_keys(self, session):
        # type: (Session) -> None
        """Remove used one-time keys.

        Removes the one-time keys that the session used from the account.
        Raises OlmAccountError on failure. If the account doesn't have any
        matching one-time keys then the error message of the exception will be
        "BAD_MESSAGE_KEY_ID".

        Args:
            session(Session): An Olm Session object that was created with this
            account.
        """
        self._check_error(lib.olm_remove_one_time_keys(self._account,
                                                       session._session))