inbound_group_session.py 4.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import json

from ._base import *

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

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

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

14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def inbound_group_session_errcheck(res, func, args):
    if res == ERR:
        raise OlmError("%s: %s" % (
            func.__name__, lib.olm_inbound_group_session_last_error(args[0])
        ))
    return res


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


inbound_group_session_function(
30
31
    lib.olm_pickle_inbound_group_session,
    c_void_p, c_size_t, c_void_p, c_size_t,
32
33
)
inbound_group_session_function(
34
35
    lib.olm_unpickle_inbound_group_session,
    c_void_p, c_size_t, c_void_p, c_size_t,
36
37
38
)

inbound_group_session_function(
39
    lib.olm_init_inbound_group_session, c_void_p, c_size_t
40
41
)

42
43
44
45
inbound_group_session_function(
    lib.olm_import_inbound_group_session, c_void_p, c_size_t
)

46
47
48
49
50
inbound_group_session_function(
    lib.olm_group_decrypt_max_plaintext_length, c_void_p, c_size_t
)
inbound_group_session_function(
    lib.olm_group_decrypt,
51
52
53
    c_void_p, c_size_t,  # message
    c_void_p, c_size_t,  # plaintext
    POINTER(c_uint32),  # message_index
54
55
)

56
57
58
59
60
61
62
inbound_group_session_function(
    lib.olm_inbound_group_session_id_length,
)
inbound_group_session_function(
    lib.olm_inbound_group_session_id,
    c_void_p, c_size_t,
)
63

64
65
66
lib.olm_inbound_group_session_first_known_index.argtypes = (c_void_p,)
lib.olm_inbound_group_session_first_known_index.restypes = c_uint32

67
68
69
70
71
72
inbound_group_session_function(
    lib.olm_export_inbound_group_session_length,
)
inbound_group_session_function(
    lib.olm_export_inbound_group_session, c_void_p, c_size_t, c_uint32,
)
73
74


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class InboundGroupSession(object):
    def __init__(self):
        self.buf = create_string_buffer(lib.olm_inbound_group_session_size())
        self.ptr = lib.olm_inbound_group_session(self.buf)

    def pickle(self, key):
        key_buffer = create_string_buffer(key)
        pickle_length = lib.olm_pickle_inbound_group_session_length(self.ptr)
        pickle_buffer = create_string_buffer(pickle_length)
        lib.olm_pickle_inbound_group_session(
            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_inbound_group_session(
            self.ptr, key_buffer, len(key), pickle_buffer, len(pickle)
        )

96
    def init(self, session_key):
97
98
        key_buffer = create_string_buffer(session_key)
        lib.olm_init_inbound_group_session(
99
            self.ptr, key_buffer, len(session_key)
100
101
        )

102
103
104
105
106
107
    def import_session(self, session_key):
        key_buffer = create_string_buffer(session_key)
        lib.olm_import_inbound_group_session(
            self.ptr, key_buffer, len(session_key)
        )

108
109
110
111
112
113
114
    def decrypt(self, message):
        message_buffer = create_string_buffer(message)
        max_plaintext_length = lib.olm_group_decrypt_max_plaintext_length(
            self.ptr, message_buffer, len(message)
        )
        plaintext_buffer = create_string_buffer(max_plaintext_length)
        message_buffer = create_string_buffer(message)
115
116

        message_index = c_uint32()
117
118
        plaintext_length = lib.olm_group_decrypt(
            self.ptr, message_buffer, len(message),
119
120
            plaintext_buffer, max_plaintext_length,
            byref(message_index)
121
        )
122
        return plaintext_buffer.raw[:plaintext_length], message_index
123
124
125
126

    def session_id(self):
        id_length = lib.olm_inbound_group_session_id_length(self.ptr)
        id_buffer = create_string_buffer(id_length)
127
        lib.olm_inbound_group_session_id(self.ptr, id_buffer, id_length)
128
        return id_buffer.raw
129
130
131
132
133
134
135
136
137
138

    def first_known_index(self):
        return lib.olm_inbound_group_session_first_known_index(self.ptr)

    def export_session(self, message_index):
        length = lib.olm_export_inbound_group_session_length(self.ptr)
        buffer = create_string_buffer(length)
        lib.olm_export_inbound_group_session(self.ptr, buffer, length,
                                             message_index)
        return buffer.raw