aboutsummaryrefslogtreecommitdiff
path: root/plugins/signal-protocol/src/store.vala
blob: 8a0e5853fe1df27fce01bc59404e05889a6ea96d (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
namespace Signal {

public abstract class IdentityKeyStore : Object {
    public abstract uint8[] identity_key_private { get; set; }
    public abstract uint8[] identity_key_public { get; set; }
    public abstract uint32 local_registration_id { get; set; }

    public signal void trusted_identity_added(TrustedIdentity id);
    public signal void trusted_identity_updated(TrustedIdentity id);

    public abstract void save_identity(Address address, uint8[] key) throws Error ;

    public abstract bool is_trusted_identity(Address address, uint8[] key) throws Error ;

    public class TrustedIdentity {
        public uint8[] key { get; set; }
        public string name { get; private set; }
        public int device_id { get; private set; }

        public TrustedIdentity(string name, int device_id, uint8[] key) {
            this.key = key;
            this.name = name;
            this.device_id = device_id;
        }

        public TrustedIdentity.by_address(Address address, uint8[] key) {
            this(address.name, address.device_id, key);
        }
    }
}

public abstract class SessionStore : Object {

    public signal void session_stored(Session session);
    public signal void session_removed(Session session);
    public abstract uint8[]? load_session(Address address) throws Error ;

    public abstract IntList get_sub_device_sessions(string name) throws Error ;

    public abstract void store_session(Address address, uint8[] record) throws Error ;

    public abstract bool contains_session(Address address) throws Error ;

    public abstract void delete_session(Address address) throws Error ;

    public abstract void delete_all_sessions(string name) throws Error ;

    public class Session {
        public string name;
        public int device_id;
        public uint8[] record;
    }
}

public abstract class PreKeyStore : Object {

    public signal void pre_key_stored(Key key);
    public signal void pre_key_deleted(Key key);

    public abstract uint8[]? load_pre_key(uint32 pre_key_id) throws Error ;

    public abstract void store_pre_key(uint32 pre_key_id, uint8[] record) throws Error ;

    public abstract bool contains_pre_key(uint32 pre_key_id) throws Error ;

    public abstract void delete_pre_key(uint32 pre_key_id) throws Error ;

    public class Key {
        public uint32 key_id { get; private set; }
        public uint8[] record { get; private set; }

        public Key(uint32 key_id, uint8[] record) {
            this.key_id = key_id;
            this.record = record;
        }
    }
}

public abstract class SignedPreKeyStore : Object {

    public signal void signed_pre_key_stored(Key key);
    public signal void signed_pre_key_deleted(Key key);

    public abstract uint8[]? load_signed_pre_key(uint32 pre_key_id) throws Error ;

    public abstract void store_signed_pre_key(uint32 pre_key_id, uint8[] record) throws Error ;

    public abstract bool contains_signed_pre_key(uint32 pre_key_id) throws Error ;

    public abstract void delete_signed_pre_key(uint32 pre_key_id) throws Error ;

    public class Key {
        public uint32 key_id { get; private set; }
        public uint8[] record { get; private set; }

        public Key(uint32 key_id, uint8[] record) {
            this.key_id = key_id;
            this.record = record;
        }
    }
}

public class Store : Object {
    public Context context { get; private set; }
    public IdentityKeyStore identity_key_store { get; set; default = new SimpleIdentityKeyStore(); }
    public SessionStore session_store { get; set; default = new SimpleSessionStore(); }
    public PreKeyStore pre_key_store { get; set; default = new SimplePreKeyStore(); }
    public SignedPreKeyStore signed_pre_key_store { get; set; default = new SimpleSignedPreKeyStore(); }
    public uint32 local_registration_id { get { return identity_key_store.local_registration_id; } }
    internal NativeStoreContext native_context {get { return native_store_context_; }}
    private NativeStoreContext native_store_context_;

    static int iks_get_identity_key_pair(out Buffer public_data, out Buffer private_data, void* user_data) {
        Store store = (Store) user_data;
        public_data = new Buffer.from(store.identity_key_store.identity_key_public);
        private_data = new Buffer.from(store.identity_key_store.identity_key_private);
        return 0;
    }

    static int iks_get_local_registration_id(void* user_data, out uint32 registration_id) {
        Store store = (Store) user_data;
        registration_id = store.identity_key_store.local_registration_id;
        return 0;
    }

    static int iks_save_identity(Address address, uint8[] key, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.identity_key_store.save_identity(address, key);
            return 0;
        });
    }

    static int iks_is_trusted_identity(Address address, uint8[] key, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            return store.identity_key_store.is_trusted_identity(address, key) ? 1 : 0;
        });
    }

    static int iks_destroy_func(void* user_data) {
        return 0;
    }

    static int ss_load_session_func(out Buffer? buffer, Address address, void* user_data) {
        Store store = (Store) user_data;
        uint8[]? res = null;
        try {
            res = store.session_store.load_session(address);
        } catch (Error e) {
            buffer = null;
            return e.code;
        }
        if (res == null) {
            buffer = null;
            return 0;
        }
        buffer = new Buffer.from((!)res);
        if (buffer == null) return ErrorCode.NOMEM;
        return 1;
    }

    static int ss_get_sub_device_sessions_func(out IntList? sessions, char[] name, void* user_data) {
        Store store = (Store) user_data;
        try {
            sessions = store.session_store.get_sub_device_sessions(carr_to_string(name));
        } catch (Error e) {
            sessions = null;
            return e.code;
        }
        return 0;
    }

    static int ss_store_session_func(Address address, uint8[] record, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.session_store.store_session(address, record);
            return 0;
        });
    }

    static int ss_contains_session_func(Address address, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            return store.session_store.contains_session(address) ? 1 : 0;
        });
    }

    static int ss_delete_session_func(Address address, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.session_store.delete_session(address);
            return 0;
        });
    }

    static int ss_delete_all_sessions_func(char[] name, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.session_store.delete_all_sessions(carr_to_string(name));
            return 0;
        });
    }

    static int ss_destroy_func(void* user_data) {
        return 0;
    }

    static int pks_load_pre_key(out Buffer? record, uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        uint8[]? res = null;
        try {
            res = store.pre_key_store.load_pre_key(pre_key_id);
        } catch (Error e) {
            record = null;
            return e.code;
        }
        if (res == null) {
            record = new Buffer(0);
            return 0;
        }
        record = new Buffer.from((!)res);
        if (record == null) return ErrorCode.NOMEM;
        return 1;
    }

    static int pks_store_pre_key(uint32 pre_key_id, uint8[] record, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.pre_key_store.store_pre_key(pre_key_id, record);
            return 0;
        });
    }

    static int pks_contains_pre_key(uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            return store.pre_key_store.contains_pre_key(pre_key_id) ? 1 : 0;
        });
    }

    static int pks_remove_pre_key(uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.pre_key_store.delete_pre_key(pre_key_id);
            return 0;
        });
    }

    static int pks_destroy_func(void* user_data) {
        return 0;
    }

    static int spks_load_signed_pre_key(out Buffer? record, uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        uint8[]? res = null;
        try {
            res = store.signed_pre_key_store.load_signed_pre_key(pre_key_id);
        } catch (Error e) {
            record = null;
            return e.code;
        }
        if (res == null) {
            record = new Buffer(0);
            return 0;
        }
        record = new Buffer.from((!)res);
        if (record == null) return ErrorCode.NOMEM;
        return 1;
    }

    static int spks_store_signed_pre_key(uint32 pre_key_id, uint8[] record, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.signed_pre_key_store.store_signed_pre_key(pre_key_id, record);
            return 0;
        });
    }

    static int spks_contains_signed_pre_key(uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            return store.signed_pre_key_store.contains_signed_pre_key(pre_key_id) ? 1 : 0;
        });
    }

    static int spks_remove_signed_pre_key(uint32 pre_key_id, void* user_data) {
        Store store = (Store) user_data;
        return catch_to_code(() => {
            store.signed_pre_key_store.delete_signed_pre_key(pre_key_id);
            return 0;
        });
    }

    static int spks_destroy_func(void* user_data) {
        return 0;
    }

    internal Store(Context context) {
        this.context = context;
        NativeStoreContext.create(out native_store_context_, context.native_context);

        NativeIdentityKeyStore iks = NativeIdentityKeyStore() {
            get_identity_key_pair = iks_get_identity_key_pair,
            get_local_registration_id = iks_get_local_registration_id,
            save_identity = iks_save_identity,
            is_trusted_identity = iks_is_trusted_identity,
            destroy_func = iks_destroy_func,
            user_data = this
        };
        native_context.set_identity_key_store(iks);

        NativeSessionStore ss = NativeSessionStore() {
            load_session_func = ss_load_session_func,
            get_sub_device_sessions_func = ss_get_sub_device_sessions_func,
            store_session_func = ss_store_session_func,
            contains_session_func = ss_contains_session_func,
            delete_session_func = ss_delete_session_func,
            delete_all_sessions_func = ss_delete_all_sessions_func,
            destroy_func = ss_destroy_func,
            user_data = this
        };
        native_context.set_session_store(ss);

        NativePreKeyStore pks = NativePreKeyStore() {
            load_pre_key = pks_load_pre_key,
            store_pre_key = pks_store_pre_key,
            contains_pre_key = pks_contains_pre_key,
            remove_pre_key = pks_remove_pre_key,
            destroy_func = pks_destroy_func,
            user_data = this
        };
        native_context.set_pre_key_store(pks);

        NativeSignedPreKeyStore spks = NativeSignedPreKeyStore() {
            load_signed_pre_key = spks_load_signed_pre_key,
            store_signed_pre_key = spks_store_signed_pre_key,
            contains_signed_pre_key = spks_contains_signed_pre_key,
            remove_signed_pre_key = spks_remove_signed_pre_key,
            destroy_func = spks_destroy_func,
            user_data = this
        };
        native_context.set_signed_pre_key_store(spks);
    }

    public SessionBuilder create_session_builder(Address other) throws Error {
        SessionBuilder builder;
        throw_by_code(session_builder_create(out builder, native_context, other, context.native_context), "Error creating session builder");
        return builder;
    }

    public SessionCipher create_session_cipher(Address other) throws Error {
        SessionCipher cipher;
        throw_by_code(session_cipher_create(out cipher, native_context, other, context.native_context));
        return cipher;
    }

    public IdentityKeyPair identity_key_pair {
        owned get {
            IdentityKeyPair pair;
            Protocol.Identity.get_key_pair(native_context, out pair);
            return pair;
        }
    }

    public bool is_trusted_identity(Address address, ECPublicKey key) throws Error {
        return throw_by_code(Protocol.Identity.is_trusted_identity(native_context, address, key)) == 1;
    }

    public void save_identity(Address address, ECPublicKey key) throws Error {
        throw_by_code(Protocol.Identity.save_identity(native_context, address, key));
    }

    public bool contains_session(Address other) throws Error {
        return throw_by_code(Protocol.Session.contains_session(native_context, other)) == 1;
    }

    public void delete_session(Address address) throws Error {
        throw_by_code(Protocol.Session.delete_session(native_context, address));
    }

    public SessionRecord load_session(Address other) throws Error {
        SessionRecord record;
        throw_by_code(Protocol.Session.load_session(native_context, out record, other));
        return record;
    }

    public bool contains_pre_key(uint32 pre_key_id) throws Error {
        return throw_by_code(Protocol.PreKey.contains_key(native_context, pre_key_id)) == 1;
    }

    public void store_pre_key(PreKeyRecord record) throws Error {
        throw_by_code(Protocol.PreKey.store_key(native_context, record));
    }

    public PreKeyRecord load_pre_key(uint32 pre_key_id) throws Error {
        PreKeyRecord res;
        throw_by_code(Protocol.PreKey.load_key(native_context, out res, pre_key_id));
        return res;
    }

    public bool contains_signed_pre_key(uint32 pre_key_id) throws Error {
        return throw_by_code(Protocol.SignedPreKey.contains_key(native_context, pre_key_id)) == 1;
    }

    public void store_signed_pre_key(SignedPreKeyRecord record) throws Error {
        throw_by_code(Protocol.SignedPreKey.store_key(native_context, record));
    }

    public SignedPreKeyRecord load_signed_pre_key(uint32 pre_key_id) throws Error {
        SignedPreKeyRecord res;
        throw_by_code(Protocol.SignedPreKey.load_key(native_context, out res, pre_key_id));
        return res;
    }
}

}