aboutsummaryrefslogtreecommitdiff
path: root/python/olm/sas.py
blob: c12b7bc874d8c924c69ff3253f5c9a41bb9fa0a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
209
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
250
251
252
253
254
255
256
257
# -*- coding: utf-8 -*-
# libolm python bindings
# Copyright © 2019 Damir Jelić <poljar@termina.org.uk>
#
# 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.

"""libolm SAS module.

This module contains functions to perform key verification using the Short
Authentication String (SAS) method.

Examples:
    >>> sas = Sas()
    >>> bob_key = "3p7bfXt9wbTTW2HC7OQ1Nz+DQ8hbeGdNrfx+FG+IK08"
    >>> message = "Hello world!"
    >>> extra_info = "MAC"
    >>> sas_alice.set_their_pubkey(bob_key)
    >>> sas_alice.calculate_mac(message, extra_info)
    >>> sas_alice.generate_bytes(extra_info, 5)

"""

from functools import wraps
from builtins import bytes
from typing import Optional

from future.utils import bytes_to_native_str

from _libolm import ffi, lib

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


def other_pubkey_set(func):
    """Ensure that the other pubkey is added to the Sas object."""
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        if not self.other_key_set:
            raise OlmSasError("The other public key isn't set.")
        return func(self, *args, **kwargs)
    return wrapper


def _clear_sas(sas):
    # type: (ffi.cdata) -> None
    lib.olm_clear_sas(sas)


class OlmSasError(Exception):
    """libolm Sas error exception."""


class Sas(object):
    """libolm Short Authenticaton String (SAS) class."""

    def __init__(self, other_users_pubkey=None):
        # type: (Optional[str]) -> None
        """Create a new SAS object.

        Args:
            other_users_pubkey(str, optional): The other users public key, this
                key is necesary to generate bytes for the authentication string
                as well as to calculate the MAC.

        Attributes:
            other_key_set (bool): A boolean flag that tracks if we set the
                other users public key for this SAS object.

        Raises OlmSasError on failure.

        """
        self._buf = ffi.new("char[]", lib.olm_sas_size())
        self._sas = lib.olm_sas(self._buf)
        self.other_key_set = False
        track_for_finalization(self, self._sas, _clear_sas)

        random_length = lib.olm_create_sas_random_length(self._sas)
        random = URANDOM(random_length)

        self._create_sas(random, random_length)

        if other_users_pubkey:
            self.set_their_pubkey(other_users_pubkey)

    def _create_sas(self, buffer, buffer_length):
        self._check_error(
            lib.olm_create_sas(
                self._sas,
                ffi.from_buffer(buffer),
                buffer_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_sas_last_error(self._sas))))

        raise OlmSasError(last_error)

    @property
    def pubkey(self):
        # type: () -> str
        """Get the public key for the SAS object.

        This returns the public key of the SAS object that can then be shared
        with another user to perform the authentication process.

        Raises OlmSasError on failure.

        """
        pubkey_length = lib.olm_sas_pubkey_length(self._sas)
        pubkey_buffer = ffi.new("char[]", pubkey_length)

        self._check_error(
            lib.olm_sas_get_pubkey(self._sas, pubkey_buffer, pubkey_length)
        )

        return bytes_to_native_str(ffi.unpack(pubkey_buffer, pubkey_length))

    def set_their_pubkey(self, key):
        # type: (str) -> None
        """Set the public key of the other user.

        This sets the public key of the other user, it needs to be set before
        bytes can be generated for the authentication string and a MAC can be
        calculated.

        Args:
            key (str): The other users public key.

        Raises OlmSasError on failure.

        """
        byte_key = to_bytearray(key)

        self._check_error(
            lib.olm_sas_set_their_key(
                self._sas,
                ffi.from_buffer(byte_key),
                len(byte_key)
            )
        )
        self.other_key_set = True

    @other_pubkey_set
    def generate_bytes(self, extra_info, length):
        # type: (str, int) -> bytes
        """Generate bytes to use for the short authentication string.

        Args:
            extra_info (str): Extra information to mix in when generating the
                bytes.
            length (int): The number of bytes to generate.

        Raises OlmSasError if the other users persons public key isn't set or
        an internal Olm error happens.

        """
        if length < 1:
            raise ValueError("The length needs to be a positive integer value")

        byte_info = to_bytearray(extra_info)
        out_buffer = ffi.new("char[]", length)

        self._check_error(
            lib.olm_sas_generate_bytes(
                self._sas,
                ffi.from_buffer(byte_info),
                len(byte_info),
                out_buffer,
                length
            )
        )

        return ffi.unpack(out_buffer, length)

    @other_pubkey_set
    def calculate_mac(self, message, extra_info):
        # type: (str, str) -> str
        """Generate a message authentication code based on the shared secret.

        Args:
            message (str): The message to produce the authentication code for.
            extra_info (str): Extra information to mix in when generating the
                MAC

        Raises OlmSasError on failure.

        """
        byte_message = to_bytes(message)
        byte_info = to_bytes(extra_info)

        mac_length = lib.olm_sas_mac_length(self._sas)
        mac_buffer = ffi.new("char[]", mac_length)

        self._check_error(
            lib.olm_sas_calculate_mac(
                self._sas,
                ffi.from_buffer(byte_message),
                len(byte_message),
                ffi.from_buffer(byte_info),
                len(byte_info),
                mac_buffer,
                mac_length
            )
        )
        return bytes_to_native_str(ffi.unpack(mac_buffer, mac_length))

    @other_pubkey_set
    def calculate_mac_long_kdf(self, message, extra_info):
        # type: (str, str) -> str
        """Generate a message authentication code based on the shared secret.

        This function should not be used unless compatibility with an older
        non-tagged Olm version is required.

        Args:
            message (str): The message to produce the authentication code for.
            extra_info (str): Extra information to mix in when generating the
                MAC

        Raises OlmSasError on failure.

        """
        byte_message = to_bytes(message)
        byte_info = to_bytes(extra_info)

        mac_length = lib.olm_sas_mac_length(self._sas)
        mac_buffer = ffi.new("char[]", mac_length)

        self._check_error(
            lib.olm_sas_calculate_mac_long_kdf(
                self._sas,
                ffi.from_buffer(byte_message),
                len(byte_message),
                ffi.from_buffer(byte_info),
                len(byte_info),
                mac_buffer,
                mac_length
            )
        )
        return bytes_to_native_str(ffi.unpack(mac_buffer, mac_length))