aboutsummaryrefslogtreecommitdiff
path: root/python/olm/account.py
blob: 3c9ea88ad16109479557a55877ba46dfced9eb82 (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
import json
from os import urandom

from ._base import *

lib.olm_account_size.argtypes = []
lib.olm_account_size.restype = c_size_t

lib.olm_account.argtypes = [c_void_p]
lib.olm_account.restype = c_void_p

lib.olm_account_last_error.argtypes = [c_void_p]
lib.olm_account_last_error.restype = c_char_p


def account_errcheck(res, func, args):
    if res == ERR:
        raise OlmError("%s: %s" % (
            func.__name__, lib.olm_account_last_error(args[0])
        ))
    return res


def account_function(func, *types):
    func.argtypes = (c_void_p,) + types
    func.restypes = c_size_t
    func.errcheck = account_errcheck


account_function(
    lib.olm_pickle_account, c_void_p, c_size_t, c_void_p, c_size_t
)
account_function(
    lib.olm_unpickle_account, c_void_p, c_size_t, c_void_p, c_size_t
)
account_function(lib.olm_create_account_random_length)
account_function(lib.olm_create_account, c_void_p, c_size_t)
account_function(lib.olm_account_identity_keys_length)
account_function(lib.olm_account_identity_keys, c_void_p, c_size_t)
account_function(lib.olm_account_signature_length)
account_function(lib.olm_account_sign, c_void_p, c_size_t, c_void_p, c_size_t)
account_function(lib.olm_account_one_time_keys_length)
account_function(lib.olm_account_one_time_keys, c_void_p, c_size_t)
account_function(lib.olm_account_mark_keys_as_published)
account_function(lib.olm_account_max_number_of_one_time_keys)
account_function(lib.olm_pickle_account_length)
account_function(
    lib.olm_account_generate_one_time_keys_random_length,
    c_size_t
)
account_function(
    lib.olm_account_generate_one_time_keys,
    c_size_t,
    c_void_p, c_size_t
)


class Account(object):
    def __init__(self):
        self.buf = create_string_buffer(lib.olm_account_size())
        self.ptr = lib.olm_account(self.buf)

    def create(self):
        random_length = lib.olm_create_account_random_length(self.ptr)
        random = urandom(random_length)
        random_buffer = create_string_buffer(random)
        lib.olm_create_account(self.ptr, random_buffer, random_length)

    def pickle(self, key):
        key_buffer = create_string_buffer(key)
        pickle_length = lib.olm_pickle_account_length(self.ptr)
        pickle_buffer = create_string_buffer(pickle_length)
        lib.olm_pickle_account(
            self.ptr, key_buffer, len(key), pickle_buffer, pickle_length
        )
        return pickle_buffer.raw

    def unpickle(self, key, pickle):
        key_buffer = create_string_buffer(key)
        pickle_buffer = create_string_buffer(pickle)
        lib.olm_unpickle_account(
            self.ptr, key_buffer, len(key), pickle_buffer, len(pickle)
        )

    def identity_keys(self):
        out_length = lib.olm_account_identity_keys_length(self.ptr)
        out_buffer = create_string_buffer(out_length)
        lib.olm_account_identity_keys(
            self.ptr,
            out_buffer, out_length
        )
        return json.loads(out_buffer.raw)

    def sign(self, message):
        out_length = lib.olm_account_signature_length(self.ptr)
        message_buffer = create_string_buffer(message)
        out_buffer = create_string_buffer(out_length)
        lib.olm_account_sign(
            self.ptr, message_buffer, len(message), out_buffer, out_length
        )
        return out_buffer.raw

    def one_time_keys(self):
        out_length = lib.olm_account_one_time_keys_length(self.ptr)
        out_buffer = create_string_buffer(out_length)
        lib.olm_account_one_time_keys(self.ptr, out_buffer, out_length)
        return json.loads(out_buffer.raw)

    def mark_keys_as_published(self):
        lib.olm_account_mark_keys_as_published(self.ptr)

    def max_number_of_one_time_keys(self):
        return lib.olm_account_max_number_of_one_time_keys(self.ptr)

    def generate_one_time_keys(self, count):
        random_length = lib.olm_account_generate_one_time_keys_random_length(
            self.ptr, count
        )
        random = urandom(random_length)
        random_buffer = create_string_buffer(random)
        lib.olm_account_generate_one_time_keys(
            self.ptr, count, random_buffer, random_length
        )

    def clear(self):
        pass