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
|
using Dino.Entities;
using Signal;
using Qlite;
using Xmpp;
using Gee;
namespace Dino.Omemo {
public class Manager : StreamInteractionModule, Object {
public const string id = "omemo_manager";
private StreamInteractor stream_interactor;
private Database db;
private ArrayList<Entities.Message> to_send_after_devicelist = new ArrayList<Entities.Message>();
private ArrayList<Entities.Message> to_send_after_session = new ArrayList<Entities.Message>();
private Manager(StreamInteractor stream_interactor, Database db) {
this.stream_interactor = stream_interactor;
this.db = db;
stream_interactor.account_added.connect(on_account_added);
MessageManager.get_instance(stream_interactor).pre_message_received.connect(on_pre_message_received);
MessageManager.get_instance(stream_interactor).pre_message_send.connect(on_pre_message_send);
}
private void on_pre_message_received(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) {
if (MessageFlag.get_flag(message_stanza) != null && MessageFlag.get_flag(message_stanza).decrypted) {
message.encryption = Encryption.OMEMO;
}
}
private void on_pre_message_send(Entities.Message message, Xmpp.Message.Stanza message_stanza, Conversation conversation) {
if (message.encryption == Encryption.OMEMO) {
Module module = Module.get_module(stream_interactor.get_stream(conversation.account));
EncryptStatus status = module.encrypt(message_stanza, conversation.account.bare_jid.to_string());
if (status.other_failure > 0 || (status.other_lost == status.other_devices && status.other_devices > 0)) {
message.marked = Entities.Message.Marked.WONTSEND;
} else if (status.other_unknown > 0 || status.own_devices == 0) {
message.marked = Entities.Message.Marked.UNSENT;
} else if (!status.encrypted) {
message.marked = Entities.Message.Marked.WONTSEND;
}
if (status.other_unknown > 0) {
bool cont = true;
lock(to_send_after_session) {
foreach(Entities.Message msg in to_send_after_session) {
if (msg.counterpart.bare_jid.to_string() == message.counterpart.bare_jid.to_string()) cont = false;
}
to_send_after_session.add(message);
}
if (cont) module.start_sessions_with(stream_interactor.get_stream(conversation.account), message.counterpart.bare_jid.to_string());
}
if (status.own_unknown > 0) {
module.start_sessions_with(stream_interactor.get_stream(conversation.account), conversation.account.bare_jid.to_string());
}
if (status.own_devices == 0) {
lock (to_send_after_session) {
to_send_after_devicelist.add(message);
}
}
}
}
private void on_account_added(Account account) {
stream_interactor.module_manager.get_module(account, Module.IDENTITY).store_created.connect((context, store) => on_store_created(account, context, store));
stream_interactor.module_manager.get_module(account, Module.IDENTITY).device_list_loaded.connect(() => on_device_list_loaded(account));
stream_interactor.module_manager.get_module(account, Module.IDENTITY).session_started.connect((jid, device_id) => on_session_started(account, jid));
}
private void on_session_started(Account account, string jid) {
lock(to_send_after_session) {
Iterator<Entities.Message> iter = to_send_after_session.iterator();
while (iter.next()) {
Entities.Message msg = iter.get();
if (msg.account.bare_jid.to_string() == account.bare_jid.to_string() && msg.counterpart.bare_jid.to_string() == jid) {
Entities.Conversation conv = ConversationManager.get_instance(stream_interactor).get_conversation(msg.counterpart, account);
MessageManager.get_instance(stream_interactor).send_xmpp_message(msg, conv, true);
iter.remove();
}
}
}
}
private void on_device_list_loaded(Account account) {
lock(to_send_after_devicelist) {
Iterator<Entities.Message> iter = to_send_after_devicelist.iterator();
while (iter.next()) {
Entities.Message msg = iter.get();
if (msg.account.bare_jid.to_string() == account.bare_jid.to_string()) {
Entities.Conversation conv = ConversationManager.get_instance(stream_interactor).get_conversation(msg.counterpart, account);
MessageManager.get_instance(stream_interactor).send_xmpp_message(msg, conv, true);
iter.remove();
}
}
}
}
private void on_store_created(Account account, Context context, Store store) {
Qlite.Row? row = null;
try {
row = db.identity.row_with(db.identity.account_id, account.id);
} catch (Error e) {
// Ignore error
}
int identity_id = -1;
if (row == null) {
// OMEMO not yet initialized, starting with empty base
store.identity_key_store.local_registration_id = Random.int_range(1, int32.MAX);
Signal.ECKeyPair key_pair = context.generate_key_pair();
store.identity_key_store.identity_key_private = key_pair.private.serialize();
store.identity_key_store.identity_key_public = key_pair.public.serialize();
try {
identity_id = (int) db.identity.insert().or("REPLACE")
.value(db.identity.account_id, account.id)
.value(db.identity.device_id, (int) store.local_registration_id)
.value(db.identity.identity_key_private_base64, Base64.encode(store.identity_key_store.identity_key_private))
.value(db.identity.identity_key_public_base64, Base64.encode(store.identity_key_store.identity_key_public))
.perform();
} catch (Error e) {
// Ignore error
}
} else {
store.identity_key_store.local_registration_id = row[db.identity.device_id];
store.identity_key_store.identity_key_private = Base64.decode(row[db.identity.identity_key_private_base64]);
store.identity_key_store.identity_key_public = Base64.decode(row[db.identity.identity_key_public_base64]);
identity_id = row[db.identity.id];
}
if (identity_id >= 0) {
store.signed_pre_key_store = new BackedSignedPreKeyStore(db, identity_id);
store.pre_key_store = new BackedPreKeyStore(db, identity_id);
store.session_store = new BackedSessionStore(db, identity_id);
} else {
print(@"WARN: OMEMO store for $(account.bare_jid) is not persisted");
}
}
private class BackedSignedPreKeyStore : SimpleSignedPreKeyStore {
private Database db;
private int identity_id;
public BackedSignedPreKeyStore(Database db, int identity_id) {
this.db = db;
this.identity_id = identity_id;
init();
}
private void init() {
foreach (Row row in db.signed_pre_key.select().with(db.signed_pre_key.identity_id, "=", identity_id)) {
store_signed_pre_key(row[db.signed_pre_key.signed_pre_key_id], Base64.decode(row[db.signed_pre_key.record_base64]));
}
signed_pre_key_stored.connect(on_signed_pre_key_stored);
signed_pre_key_deleted.connect(on_signed_pre_key_deleted);
}
public void on_signed_pre_key_stored(SignedPreKeyStore.Key key) {
db.signed_pre_key.insert().or("REPLACE")
.value(db.signed_pre_key.identity_id, identity_id)
.value(db.signed_pre_key.signed_pre_key_id, (int) key.key_id)
.value(db.signed_pre_key.record_base64, Base64.encode(key.record))
.perform();
}
public void on_signed_pre_key_deleted(SignedPreKeyStore.Key key) {
db.signed_pre_key.delete()
.with(db.signed_pre_key.identity_id, "=", identity_id)
.with(db.signed_pre_key.signed_pre_key_id, "=", (int) key.key_id)
.perform();
}
}
private class BackedPreKeyStore : SimplePreKeyStore {
private Database db;
private int identity_id;
public BackedPreKeyStore(Database db, int identity_id) {
this.db = db;
this.identity_id = identity_id;
init();
}
private void init() {
foreach (Row row in db.pre_key.select().with(db.pre_key.identity_id, "=", identity_id)) {
store_pre_key(row[db.pre_key.pre_key_id], Base64.decode(row[db.pre_key.record_base64]));
}
pre_key_stored.connect(on_pre_key_stored);
pre_key_deleted.connect(on_pre_key_deleted);
}
public void on_pre_key_stored(PreKeyStore.Key key) {
db.pre_key.insert().or("REPLACE")
.value(db.pre_key.identity_id, identity_id)
.value(db.pre_key.pre_key_id, (int) key.key_id)
.value(db.pre_key.record_base64, Base64.encode(key.record))
.perform();
}
public void on_pre_key_deleted(PreKeyStore.Key key) {
db.pre_key.delete()
.with(db.pre_key.identity_id, "=", identity_id)
.with(db.pre_key.pre_key_id, "=", (int) key.key_id)
.perform();
}
}
private class BackedSessionStore : SimpleSessionStore {
private Database db;
private int identity_id;
public BackedSessionStore(Database db, int identity_id) {
this.db = db;
this.identity_id = identity_id;
init();
}
private void init() {
Address addr = new Address();
foreach (Row row in db.session.select().with(db.session.identity_id, "=", identity_id)) {
addr.name = row[db.session.address_name];
addr.device_id = row[db.session.device_id];
store_session(addr, Base64.decode(row[db.session.record_base64]));
}
session_stored.connect(on_session_stored);
session_removed.connect(on_session_deleted);
}
public void on_session_stored(SessionStore.Session session) {
db.session.insert().or("REPLACE")
.value(db.session.identity_id, identity_id)
.value(db.session.address_name, session.name)
.value(db.session.device_id, session.device_id)
.value(db.session.record_base64, Base64.encode(session.record))
.perform();
}
public void on_session_deleted(SessionStore.Session session) {
db.session.delete()
.with(db.session.identity_id, "=", identity_id)
.with(db.session.address_name, "=", session.name)
.with(db.session.device_id, "=", session.device_id)
.perform();
}
}
public bool con_encrypt(Entities.Conversation conversation) {
return true; // TODO
}
internal string get_id() {
return id;
}
public static void start(StreamInteractor stream_interactor, Database db) {
Manager m = new Manager(stream_interactor, db);
stream_interactor.add_module(m);
}
public static Manager? get_instance(StreamInteractor stream_interactor) {
return (Manager) stream_interactor.get_module(id);
}
}
}
|