1package eu.siacs.conversations.crypto.axolotl;
2
3import android.support.annotation.NonNull;
4import android.support.annotation.Nullable;
5import android.util.Log;
6
7import org.bouncycastle.jce.provider.BouncyCastleProvider;
8import org.whispersystems.libaxolotl.AxolotlAddress;
9import org.whispersystems.libaxolotl.IdentityKey;
10import org.whispersystems.libaxolotl.IdentityKeyPair;
11import org.whispersystems.libaxolotl.InvalidKeyException;
12import org.whispersystems.libaxolotl.InvalidKeyIdException;
13import org.whispersystems.libaxolotl.SessionBuilder;
14import org.whispersystems.libaxolotl.UntrustedIdentityException;
15import org.whispersystems.libaxolotl.ecc.ECPublicKey;
16import org.whispersystems.libaxolotl.state.PreKeyBundle;
17import org.whispersystems.libaxolotl.state.PreKeyRecord;
18import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
19import org.whispersystems.libaxolotl.util.KeyHelper;
20
21import java.security.Security;
22import java.util.Arrays;
23import java.util.HashMap;
24import java.util.HashSet;
25import java.util.List;
26import java.util.Map;
27import java.util.Random;
28import java.util.Set;
29
30import eu.siacs.conversations.Config;
31import eu.siacs.conversations.entities.Account;
32import eu.siacs.conversations.entities.Contact;
33import eu.siacs.conversations.entities.Conversation;
34import eu.siacs.conversations.entities.Message;
35import eu.siacs.conversations.parser.IqParser;
36import eu.siacs.conversations.services.XmppConnectionService;
37import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
38import eu.siacs.conversations.xml.Element;
39import eu.siacs.conversations.xmpp.OnIqPacketReceived;
40import eu.siacs.conversations.xmpp.jid.InvalidJidException;
41import eu.siacs.conversations.xmpp.jid.Jid;
42import eu.siacs.conversations.xmpp.stanzas.IqPacket;
43
44public class AxolotlService {
45
46 public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
47 public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
48 public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";
49
50 public static final String LOGPREFIX = "AxolotlService";
51
52 public static final int NUM_KEYS_TO_PUBLISH = 100;
53 public static final int publishTriesThreshold = 3;
54
55 private final Account account;
56 private final XmppConnectionService mXmppConnectionService;
57 private final SQLiteAxolotlStore axolotlStore;
58 private final SessionMap sessions;
59 private final Map<Jid, Set<Integer>> deviceIds;
60 private final Map<String, XmppAxolotlMessage> messageCache;
61 private final FetchStatusMap fetchStatusMap;
62 private final SerialSingleThreadExecutor executor;
63 private int numPublishTriesOnEmptyPep = 0;
64 private boolean pepBroken = false;
65
66 private static class AxolotlAddressMap<T> {
67 protected Map<String, Map<Integer, T>> map;
68 protected final Object MAP_LOCK = new Object();
69
70 public AxolotlAddressMap() {
71 this.map = new HashMap<>();
72 }
73
74 public void put(AxolotlAddress address, T value) {
75 synchronized (MAP_LOCK) {
76 Map<Integer, T> devices = map.get(address.getName());
77 if (devices == null) {
78 devices = new HashMap<>();
79 map.put(address.getName(), devices);
80 }
81 devices.put(address.getDeviceId(), value);
82 }
83 }
84
85 public T get(AxolotlAddress address) {
86 synchronized (MAP_LOCK) {
87 Map<Integer, T> devices = map.get(address.getName());
88 if (devices == null) {
89 return null;
90 }
91 return devices.get(address.getDeviceId());
92 }
93 }
94
95 public Map<Integer, T> getAll(AxolotlAddress address) {
96 synchronized (MAP_LOCK) {
97 Map<Integer, T> devices = map.get(address.getName());
98 if (devices == null) {
99 return new HashMap<>();
100 }
101 return devices;
102 }
103 }
104
105 public boolean hasAny(AxolotlAddress address) {
106 synchronized (MAP_LOCK) {
107 Map<Integer, T> devices = map.get(address.getName());
108 return devices != null && !devices.isEmpty();
109 }
110 }
111
112 public void clear() {
113 map.clear();
114 }
115
116 }
117
118 private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
119 private final XmppConnectionService xmppConnectionService;
120 private final Account account;
121
122 public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
123 super();
124 this.xmppConnectionService = service;
125 this.account = account;
126 this.fillMap(store);
127 }
128
129 private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
130 for (Integer deviceId : deviceIds) {
131 AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
132 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString());
133 String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
134 this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
135 }
136 }
137
138 private void fillMap(SQLiteAxolotlStore store) {
139 List<Integer> deviceIds = store.getSubDeviceSessions(account.getJid().toBareJid().toString());
140 putDevicesForJid(account.getJid().toBareJid().toString(), deviceIds, store);
141 for (Contact contact : account.getRoster().getContacts()) {
142 Jid bareJid = contact.getJid().toBareJid();
143 if (bareJid == null) {
144 continue; // FIXME: handle this?
145 }
146 String address = bareJid.toString();
147 deviceIds = store.getSubDeviceSessions(address);
148 putDevicesForJid(address, deviceIds, store);
149 }
150
151 }
152
153 @Override
154 public void put(AxolotlAddress address, XmppAxolotlSession value) {
155 super.put(address, value);
156 value.setNotFresh();
157 xmppConnectionService.syncRosterToDisk(account);
158 }
159
160 public void put(XmppAxolotlSession session) {
161 this.put(session.getRemoteAddress(), session);
162 }
163 }
164
165 private static enum FetchStatus {
166 PENDING,
167 SUCCESS,
168 ERROR
169 }
170
171 private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
172
173 }
174
175 public static String getLogprefix(Account account) {
176 return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
177 }
178
179 public AxolotlService(Account account, XmppConnectionService connectionService) {
180 if (Security.getProvider("BC") == null) {
181 Security.addProvider(new BouncyCastleProvider());
182 }
183 this.mXmppConnectionService = connectionService;
184 this.account = account;
185 this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
186 this.deviceIds = new HashMap<>();
187 this.messageCache = new HashMap<>();
188 this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
189 this.fetchStatusMap = new FetchStatusMap();
190 this.executor = new SerialSingleThreadExecutor();
191 }
192
193 public String getOwnFingerprint() {
194 return axolotlStore.getIdentityKeyPair().getPublicKey().getFingerprint().replaceAll("\\s", "");
195 }
196
197 public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust) {
198 return axolotlStore.getContactKeysWithTrust(account.getJid().toBareJid().toString(), trust);
199 }
200
201 public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust, Contact contact) {
202 return axolotlStore.getContactKeysWithTrust(contact.getJid().toBareJid().toString(), trust);
203 }
204
205 public long getNumTrustedKeys(Contact contact) {
206 return axolotlStore.getContactNumTrustedKeys(contact.getJid().toBareJid().toString());
207 }
208
209 private AxolotlAddress getAddressForJid(Jid jid) {
210 return new AxolotlAddress(jid.toString(), 0);
211 }
212
213 private Set<XmppAxolotlSession> findOwnSessions() {
214 AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
215 Set<XmppAxolotlSession> ownDeviceSessions = new HashSet<>(this.sessions.getAll(ownAddress).values());
216 return ownDeviceSessions;
217 }
218
219 private Set<XmppAxolotlSession> findSessionsforContact(Contact contact) {
220 AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
221 Set<XmppAxolotlSession> sessions = new HashSet<>(this.sessions.getAll(contactAddress).values());
222 return sessions;
223 }
224
225 public Set<String> getFingerprintsForOwnSessions() {
226 Set<String> fingerprints = new HashSet<>();
227 for (XmppAxolotlSession session : findOwnSessions()) {
228 fingerprints.add(session.getFingerprint());
229 }
230 return fingerprints;
231 }
232
233 public Set<String> getFingerprintsForContact(final Contact contact) {
234 Set<String> fingerprints = new HashSet<>();
235 for (XmppAxolotlSession session : findSessionsforContact(contact)) {
236 fingerprints.add(session.getFingerprint());
237 }
238 return fingerprints;
239 }
240
241 private boolean hasAny(Contact contact) {
242 AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
243 return sessions.hasAny(contactAddress);
244 }
245
246 public void regenerateKeys() {
247 axolotlStore.regenerate();
248 sessions.clear();
249 fetchStatusMap.clear();
250 publishBundlesIfNeeded(true);
251 }
252
253 public int getOwnDeviceId() {
254 return axolotlStore.getLocalRegistrationId();
255 }
256
257 public Set<Integer> getOwnDeviceIds() {
258 return this.deviceIds.get(account.getJid().toBareJid());
259 }
260
261 private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds,
262 final XmppAxolotlSession.Trust from,
263 final XmppAxolotlSession.Trust to) {
264 for (Integer deviceId : deviceIds) {
265 AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
266 XmppAxolotlSession session = sessions.get(address);
267 if (session != null && session.getFingerprint() != null
268 && session.getTrust() == from) {
269 session.setTrust(to);
270 }
271 }
272 }
273
274 public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
275 if (jid.toBareJid().equals(account.getJid().toBareJid())) {
276 if (!deviceIds.isEmpty()) {
277 Log.d(Config.LOGTAG, getLogprefix(account) + "Received non-empty own device list. Resetting publish attemps and pepBroken status.");
278 pepBroken = false;
279 numPublishTriesOnEmptyPep = 0;
280 }
281 if (deviceIds.contains(getOwnDeviceId())) {
282 deviceIds.remove(getOwnDeviceId());
283 } else {
284 publishOwnDeviceId(deviceIds);
285 }
286 for (Integer deviceId : deviceIds) {
287 AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
288 if (sessions.get(ownDeviceAddress) == null) {
289 buildSessionFromPEP(ownDeviceAddress);
290 }
291 }
292 }
293 Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toString()));
294 expiredDevices.removeAll(deviceIds);
295 setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED,
296 XmppAxolotlSession.Trust.INACTIVE_TRUSTED);
297 setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNDECIDED,
298 XmppAxolotlSession.Trust.INACTIVE_UNDECIDED);
299 setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNTRUSTED,
300 XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED);
301 Set<Integer> newDevices = new HashSet<>(deviceIds);
302 setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED,
303 XmppAxolotlSession.Trust.TRUSTED);
304 setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNDECIDED,
305 XmppAxolotlSession.Trust.UNDECIDED);
306 setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
307 XmppAxolotlSession.Trust.UNTRUSTED);
308 this.deviceIds.put(jid, deviceIds);
309 mXmppConnectionService.keyStatusUpdated();
310 }
311
312 public void wipeOtherPepDevices() {
313 if (pepBroken) {
314 Log.d(Config.LOGTAG, getLogprefix(account) + "wipeOtherPepDevices called, but PEP is broken. Ignoring... ");
315 return;
316 }
317 Set<Integer> deviceIds = new HashSet<>();
318 deviceIds.add(getOwnDeviceId());
319 IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
320 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
321 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
322 @Override
323 public void onIqPacketReceived(Account account, IqPacket packet) {
324 // TODO: implement this!
325 }
326 });
327 }
328
329 public void purgeKey(final String fingerprint) {
330 axolotlStore.setFingerprintTrust(fingerprint.replaceAll("\\s", ""), XmppAxolotlSession.Trust.COMPROMISED);
331 }
332
333 public void publishOwnDeviceIdIfNeeded() {
334 if (pepBroken) {
335 Log.d(Config.LOGTAG, getLogprefix(account) + "publishOwnDeviceIdIfNeeded called, but PEP is broken. Ignoring... ");
336 return;
337 }
338 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
339 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
340 @Override
341 public void onIqPacketReceived(Account account, IqPacket packet) {
342 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
343 Log.d(Config.LOGTAG, getLogprefix(account) + "Timeout received while retrieving own Device Ids.");
344 } else {
345 Element item = mXmppConnectionService.getIqParser().getItem(packet);
346 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
347 if (!deviceIds.contains(getOwnDeviceId())) {
348 publishOwnDeviceId(deviceIds);
349 }
350 }
351 }
352 });
353 }
354
355 public void publishOwnDeviceId(Set<Integer> deviceIds) {
356 Set<Integer> deviceIdsCopy = new HashSet<>(deviceIds);
357 if (!deviceIdsCopy.contains(getOwnDeviceId())) {
358 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist.");
359 if (deviceIdsCopy.isEmpty()) {
360 if (numPublishTriesOnEmptyPep >= publishTriesThreshold) {
361 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device publish attempt threshold exceeded, aborting...");
362 pepBroken = true;
363 return;
364 } else {
365 numPublishTriesOnEmptyPep++;
366 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device list empty, attempting to publish (try " + numPublishTriesOnEmptyPep + ")");
367 }
368 } else {
369 numPublishTriesOnEmptyPep = 0;
370 }
371 deviceIdsCopy.add(getOwnDeviceId());
372 IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIdsCopy);
373 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
374 @Override
375 public void onIqPacketReceived(Account account, IqPacket packet) {
376 if (packet.getType() != IqPacket.TYPE.RESULT) {
377 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
378 }
379 }
380 });
381 }
382 }
383
384 public void publishBundlesIfNeeded(final boolean announceAfter) {
385 if (pepBroken) {
386 Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
387 return;
388 }
389 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
390 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
391 @Override
392 public void onIqPacketReceived(Account account, IqPacket packet) {
393 PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
394 Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
395 boolean flush = false;
396 if (bundle == null) {
397 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
398 bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
399 flush = true;
400 }
401 if (keys == null) {
402 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
403 }
404 try {
405 boolean changed = false;
406 // Validate IdentityKey
407 IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
408 if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
409 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
410 changed = true;
411 }
412
413 // Validate signedPreKeyRecord + ID
414 SignedPreKeyRecord signedPreKeyRecord;
415 int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
416 try {
417 signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
418 if (flush
419 || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
420 || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
421 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
422 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
423 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
424 changed = true;
425 }
426 } catch (InvalidKeyIdException e) {
427 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
428 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
429 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
430 changed = true;
431 }
432
433 // Validate PreKeys
434 Set<PreKeyRecord> preKeyRecords = new HashSet<>();
435 if (keys != null) {
436 for (Integer id : keys.keySet()) {
437 try {
438 PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
439 if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
440 preKeyRecords.add(preKeyRecord);
441 }
442 } catch (InvalidKeyIdException ignored) {
443 }
444 }
445 }
446 int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
447 if (newKeys > 0) {
448 List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
449 axolotlStore.getCurrentPreKeyId() + 1, newKeys);
450 preKeyRecords.addAll(newRecords);
451 for (PreKeyRecord record : newRecords) {
452 axolotlStore.storePreKey(record.getId(), record);
453 }
454 changed = true;
455 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
456 }
457
458
459 if (changed) {
460 IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
461 signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
462 preKeyRecords, getOwnDeviceId());
463 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
464 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
465 @Override
466 public void onIqPacketReceived(Account account, IqPacket packet) {
467 if (packet.getType() == IqPacket.TYPE.RESULT) {
468 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
469 if (announceAfter) {
470 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
471 publishOwnDeviceIdIfNeeded();
472 }
473 } else {
474 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
475 }
476 }
477 });
478 } else {
479 Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
480 if (announceAfter) {
481 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
482 publishOwnDeviceIdIfNeeded();
483 }
484 }
485 } catch (InvalidKeyException e) {
486 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
487 return;
488 }
489 }
490 });
491 }
492
493 public boolean isContactAxolotlCapable(Contact contact) {
494
495 Jid jid = contact.getJid().toBareJid();
496 AxolotlAddress address = new AxolotlAddress(jid.toString(), 0);
497 return sessions.hasAny(address) ||
498 (deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
499 }
500
501 public XmppAxolotlSession.Trust getFingerprintTrust(String fingerprint) {
502 return axolotlStore.getFingerprintTrust(fingerprint);
503 }
504
505 public void setFingerprintTrust(String fingerprint, XmppAxolotlSession.Trust trust) {
506 axolotlStore.setFingerprintTrust(fingerprint, trust);
507 }
508
509 private void buildSessionFromPEP(final AxolotlAddress address) {
510 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.toString());
511 if (address.getDeviceId() == getOwnDeviceId()) {
512 throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
513 }
514
515 try {
516 IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
517 Jid.fromString(address.getName()), address.getDeviceId());
518 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
519 mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
520 private void finish() {
521 AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
522 if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
523 && !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
524 mXmppConnectionService.keyStatusUpdated();
525 }
526 }
527
528 @Override
529 public void onIqPacketReceived(Account account, IqPacket packet) {
530 if (packet.getType() == IqPacket.TYPE.RESULT) {
531 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
532 final IqParser parser = mXmppConnectionService.getIqParser();
533 final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
534 final PreKeyBundle bundle = parser.bundle(packet);
535 if (preKeyBundleList.isEmpty() || bundle == null) {
536 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
537 fetchStatusMap.put(address, FetchStatus.ERROR);
538 finish();
539 return;
540 }
541 Random random = new Random();
542 final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
543 if (preKey == null) {
544 //should never happen
545 fetchStatusMap.put(address, FetchStatus.ERROR);
546 finish();
547 return;
548 }
549
550 final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
551 preKey.getPreKeyId(), preKey.getPreKey(),
552 bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
553 bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
554
555 try {
556 SessionBuilder builder = new SessionBuilder(axolotlStore, address);
557 builder.process(preKeyBundle);
558 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
559 sessions.put(address, session);
560 fetchStatusMap.put(address, FetchStatus.SUCCESS);
561 } catch (UntrustedIdentityException | InvalidKeyException e) {
562 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
563 + e.getClass().getName() + ", " + e.getMessage());
564 fetchStatusMap.put(address, FetchStatus.ERROR);
565 }
566
567 finish();
568 } else {
569 fetchStatusMap.put(address, FetchStatus.ERROR);
570 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
571 finish();
572 return;
573 }
574 }
575 });
576 } catch (InvalidJidException e) {
577 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
578 }
579 }
580
581 public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
582 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
583 Jid contactJid = conversation.getContact().getJid().toBareJid();
584 Set<AxolotlAddress> addresses = new HashSet<>();
585 if (deviceIds.get(contactJid) != null) {
586 for (Integer foreignId : this.deviceIds.get(contactJid)) {
587 AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
588 if (sessions.get(address) == null) {
589 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
590 if (identityKey != null) {
591 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
592 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
593 sessions.put(address, session);
594 } else {
595 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
596 addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
597 }
598 }
599 }
600 } else {
601 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
602 }
603 if (deviceIds.get(account.getJid().toBareJid()) != null) {
604 for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
605 AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
606 if (sessions.get(address) == null) {
607 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
608 if (identityKey != null) {
609 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
610 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
611 sessions.put(address, session);
612 } else {
613 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
614 addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
615 }
616 }
617 }
618 }
619
620 return addresses;
621 }
622
623 public boolean createSessionsIfNeeded(final Conversation conversation) {
624 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
625 boolean newSessions = false;
626 Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
627 for (AxolotlAddress address : addresses) {
628 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
629 FetchStatus status = fetchStatusMap.get(address);
630 if (status == null || status == FetchStatus.ERROR) {
631 fetchStatusMap.put(address, FetchStatus.PENDING);
632 this.buildSessionFromPEP(address);
633 newSessions = true;
634 } else if (status == FetchStatus.PENDING) {
635 newSessions = true;
636 } else {
637 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
638 }
639 }
640
641 return newSessions;
642 }
643
644 public boolean hasPendingKeyFetches(Conversation conversation) {
645 AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
646 AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(), 0);
647 return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
648 || fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
649
650 }
651
652 @Nullable
653 private XmppAxolotlMessage buildHeader(Contact contact) {
654 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(
655 contact.getJid().toBareJid(), getOwnDeviceId());
656
657 Set<XmppAxolotlSession> contactSessions = findSessionsforContact(contact);
658 Set<XmppAxolotlSession> ownSessions = findOwnSessions();
659 if (contactSessions.isEmpty()) {
660 return null;
661 }
662 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
663 for (XmppAxolotlSession session : contactSessions) {
664 Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
665 axolotlMessage.addDevice(session);
666 }
667 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
668 for (XmppAxolotlSession session : ownSessions) {
669 Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
670 axolotlMessage.addDevice(session);
671 }
672
673 return axolotlMessage;
674 }
675
676 @Nullable
677 public XmppAxolotlMessage encrypt(Message message) {
678 XmppAxolotlMessage axolotlMessage = buildHeader(message.getContact());
679
680 if (axolotlMessage != null) {
681 final String content;
682 if (message.hasFileOnRemoteHost()) {
683 content = message.getFileParams().url.toString();
684 } else {
685 content = message.getBody();
686 }
687 try {
688 axolotlMessage.encrypt(content);
689 } catch (CryptoFailedException e) {
690 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
691 return null;
692 }
693 }
694
695 return axolotlMessage;
696 }
697
698 public void preparePayloadMessage(final Message message, final boolean delay) {
699 executor.execute(new Runnable() {
700 @Override
701 public void run() {
702 XmppAxolotlMessage axolotlMessage = encrypt(message);
703 if (axolotlMessage == null) {
704 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
705 //mXmppConnectionService.updateConversationUi();
706 } else {
707 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
708 messageCache.put(message.getUuid(), axolotlMessage);
709 mXmppConnectionService.resendMessage(message, delay);
710 }
711 }
712 });
713 }
714
715 public void prepareKeyTransportMessage(final Contact contact, final OnMessageCreatedCallback onMessageCreatedCallback) {
716 executor.execute(new Runnable() {
717 @Override
718 public void run() {
719 XmppAxolotlMessage axolotlMessage = buildHeader(contact);
720 onMessageCreatedCallback.run(axolotlMessage);
721 }
722 });
723 }
724
725 public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
726 XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
727 if (axolotlMessage != null) {
728 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
729 messageCache.remove(message.getUuid());
730 } else {
731 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
732 }
733 return axolotlMessage;
734 }
735
736 private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
737 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
738 return (identityKey != null)
739 ? new XmppAxolotlSession(account, axolotlStore, address,
740 identityKey.getFingerprint().replaceAll("\\s", ""))
741 : null;
742 }
743
744 private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
745 AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
746 message.getSenderDeviceId());
747 XmppAxolotlSession session = sessions.get(senderAddress);
748 if (session == null) {
749 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
750 session = recreateUncachedSession(senderAddress);
751 if (session == null) {
752 session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
753 }
754 }
755 return session;
756 }
757
758 public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message) {
759 XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
760
761 XmppAxolotlSession session = getReceivingSession(message);
762 try {
763 plaintextMessage = message.decrypt(session, getOwnDeviceId());
764 Integer preKeyId = session.getPreKeyId();
765 if (preKeyId != null) {
766 publishBundlesIfNeeded(false);
767 session.resetPreKeyId();
768 }
769 } catch (CryptoFailedException e) {
770 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());
771 }
772
773 if (session.isFresh() && plaintextMessage != null) {
774 sessions.put(session);
775 }
776
777 return plaintextMessage;
778 }
779
780 public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message) {
781 XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage = null;
782
783 XmppAxolotlSession session = getReceivingSession(message);
784 keyTransportMessage = message.getParameters(session, getOwnDeviceId());
785
786 if (session.isFresh() && keyTransportMessage != null) {
787 sessions.put(session);
788 }
789
790 return keyTransportMessage;
791 }
792}