about summary refs log tree commit diff stats
path: root/src/omemo/store.c
blob: eee58e19226f3fa82b8f4df60b7da2f5c4166940 (plain) (blame)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <glib.h>
#include <signal/signal_protocol.h>

#include "config.h"
#include "omemo/omemo.h"
#include "omemo/store.h"

GHashTable *
session_store_new(void)
{
    return g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
}

GHashTable *
pre_key_store_new(void)
{
    return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
}

GHashTable *
signed_pre_key_store_new(void)
{
    return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
}

void
identity_key_store_new(identity_key_store_t *identity_key_store)
{
    identity_key_store->identity_key_store = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)signal_buffer_free);
    identity_key_store->private = NULL;
    identity_key_store->public = NULL;
}

#ifdef HAVE_LIBSIGNAL_LT_2_3_2
int
load_session(signal_buffer **record, const signal_protocol_address *address,
    void *user_data)
#else
int
load_session(signal_buffer **record, signal_buffer **user_record,
    const signal_protocol_address *address, void *user_data)
#endif
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;

    device_store = g_hash_table_lookup(session_store, address->name);
    if (!device_store) {
        *record = NULL;
        return 0;
    }

    signal_buffer *original = g_hash_table_lookup(device_store, GINT_TO_POINTER(address->device_id));
    if (!original) {
        *record = NULL;
        return 0;
    }
    *record = signal_buffer_copy(original);
    return 1;
}

int
get_sub_device_sessions(signal_int_list **sessions, const char *name,
    size_t name_len, void *user_data)
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;
    GHashTableIter iter;
    gpointer key, value;

    device_store = g_hash_table_lookup(session_store, name);
    if (!device_store) {
        return SG_SUCCESS;
    }

    *sessions = signal_int_list_alloc();
    g_hash_table_iter_init(&iter, device_store);
    while (g_hash_table_iter_next(&iter, &key, &value)) {
        signal_int_list_push_back(*sessions, GPOINTER_TO_INT(key));
    }


    return SG_SUCCESS;
}

#ifdef HAVE_LIBSIGNAL_LT_2_3_2
int
store_session(const signal_protocol_address *address, uint8_t *record,
    size_t record_len, void *user_data)
#else
int
store_session(const signal_protocol_address *address,
    uint8_t *record, size_t record_len,
    uint8_t *user_record, size_t user_record_len,
    void *user_data)
#endif
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;

    device_store = g_hash_table_lookup(session_store, (void *)address->name);
    if (!device_store) {
        device_store = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free);
        g_hash_table_insert(session_store, strdup(address->name), device_store);
    }

    signal_buffer *buffer = signal_buffer_create(record, record_len);
    g_hash_table_insert(device_store, GINT_TO_POINTER(address->device_id), buffer);


    char *record_b64 = g_base64_encode(record, record_len);
    g_key_file_set_string(omemo_sessions_keyfile(), address->name, g_strdup_printf("%d", address->device_id), record_b64);

    omemo_sessions_keyfile_save();

    return SG_SUCCESS;
}

int
contains_session(const signal_protocol_address *address, void *user_data)
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;

    device_store = g_hash_table_lookup(session_store, address->name);
    if (!device_store) {
        return 0;
    }

    if (!g_hash_table_lookup(device_store, GINT_TO_POINTER(address->device_id))) {
        return 0;
    }

    return 1;
}

int
delete_session(const signal_protocol_address *address, void *user_data)
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;

    device_store = g_hash_table_lookup(session_store, address->name);
    if (!device_store) {
        return SG_SUCCESS;
    }

    return g_hash_table_remove(device_store, GINT_TO_POINTER(address->device_id));
}

int
delete_all_sessions(const char *name, size_t name_len, void *user_data)
{
    GHashTable *session_store = (GHashTable *)user_data;
    GHashTable *device_store = NULL;

    device_store = g_hash_table_lookup(session_store, name);
    if (!device_store) {
        return SG_SUCCESS;
    }

    guint len = g_hash_table_size(device_store);
    g_hash_table_remove_all(device_store);
    return len;
}

int
load_pre_key(signal_buffer **record, uint32_t pre_key_id, void *user_data)
{
    signal_buffer *original;
    GHashTable *pre_key_store = (GHashTable *)user_data;

    original = g_hash_table_lookup(pre_key_store, GINT_TO_POINTER(pre_key_id));
    if (original == NULL) {
        return SG_ERR_INVALID_KEY_ID;
    }

    *record = signal_buffer_copy(original);
    return SG_SUCCESS;
}

int
store_pre_key(uint32_t pre_key_id, uint8_t *record, size_t record_len,
    void *user_data)
{
    GHashTable *pre_key_store = (GHashTable *)user_data;

    signal_buffer *buffer = signal_buffer_create(record, record_len);
    g_hash_table_insert(pre_key_store, GINT_TO_POINTER(pre_key_id), buffer);
    return SG_SUCCESS;
}

int
contains_pre_key(uint32_t pre_key_id, void *user_data)
{
    GHashTable *pre_key_store = (GHashTable *)user_data;

    return g_hash_table_lookup(pre_key_store, GINT_TO_POINTER(pre_key_id)) != NULL;
}

int
remove_pre_key(uint32_t pre_key_id, void *user_data)
{
    GHashTable *pre_key_store = (GHashTable *)user_data;

    return g_hash_table_remove(pre_key_store, GINT_TO_POINTER(pre_key_id));
}

int
load_signed_pre_key(signal_buffer **record, uint32_t signed_pre_key_id,
    void *user_data)
{
    signal_buffer *original;
    GHashTable *signed_pre_key_store = (GHashTable *)user_data;

    original = g_hash_table_lookup(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id));
    if (!original) {
        return SG_ERR_INVALID_KEY_ID;
    }

    *record = signal_buffer_copy(original);
    return SG_SUCCESS;
}

int
store_signed_pre_key(uint32_t signed_pre_key_id, uint8_t *record,
    size_t record_len, void *user_data)
{
    GHashTable *signed_pre_key_store = (GHashTable *)user_data;

    signal_buffer *buffer = signal_buffer_create(record, record_len);
    g_hash_table_insert(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id), buffer);
    return SG_SUCCESS;
}

int
contains_signed_pre_key(uint32_t signed_pre_key_id, void *user_data)
{
    GHashTable *signed_pre_key_store = (GHashTable *)user_data;

    return g_hash_table_lookup(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id)) != NULL;
}

int
remove_signed_pre_key(uint32_t signed_pre_key_id, void *user_data)
{
    GHashTable *signed_pre_key_store = (GHashTable *)user_data;

    return g_hash_table_remove(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id));
}

int
get_identity_key_pair(signal_buffer **public_data, signal_buffer **private_data,
    void *user_data)
{
    identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data;

    *public_data = signal_buffer_copy(identity_key_store->public);
    *private_data = signal_buffer_copy(identity_key_store->private);

    return SG_SUCCESS;
}

int
get_local_registration_id(void *user_data, uint32_t *registration_id)
{
    identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data;

    *registration_id = identity_key_store->registration_id;

    return SG_SUCCESS;
}

int
save_identity(const signal_protocol_address *address, uint8_t *key_data,
    size_t key_len, void *user_data)
{
    identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data;
    char *node = g_strdup_printf("%s:%d", address->name, address->device_id);

    signal_buffer *buffer = signal_buffer_create(key_data, key_len);
    g_hash_table_insert(identity_key_store->identity_key_store, node, buffer);

    return SG_SUCCESS;
}

int
is_trusted_identity(const signal_protocol_address *address, uint8_t *key_data,
    size_t key_len, void *user_data)
{
    identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data;
    char *node = g_strdup_printf("%s:%d", address->name, address->device_id);

    signal_buffer *buffer = signal_buffer_create(key_data, key_len);
    signal_buffer *original = g_hash_table_lookup(identity_key_store->identity_key_store, node);

    return original == NULL || signal_buffer_compare(buffer, original) == 0;
}

int
store_sender_key(const signal_protocol_sender_key_name *sender_key_name,
    uint8_t *record, size_t record_len, uint8_t *user_record,
    size_t user_record_len, void *user_data)
{
    return SG_SUCCESS;
}

int
load_sender_key(signal_buffer **record, signal_buffer **user_record,
                const signal_protocol_sender_key_name *sender_key_name,
                void *user_data)
{
    return SG_SUCCESS;
}