1package eu.siacs.conversations.crypto.axolotl;
2
3import android.os.Bundle;
4import android.security.KeyChain;
5import android.support.annotation.NonNull;
6import android.support.annotation.Nullable;
7import android.util.Log;
8import android.util.Pair;
9
10import org.bouncycastle.jce.provider.BouncyCastleProvider;
11import org.whispersystems.libsignal.SignalProtocolAddress;
12import org.whispersystems.libsignal.IdentityKey;
13import org.whispersystems.libsignal.IdentityKeyPair;
14import org.whispersystems.libsignal.InvalidKeyException;
15import org.whispersystems.libsignal.InvalidKeyIdException;
16import org.whispersystems.libsignal.SessionBuilder;
17import org.whispersystems.libsignal.UntrustedIdentityException;
18import org.whispersystems.libsignal.ecc.ECPublicKey;
19import org.whispersystems.libsignal.state.PreKeyBundle;
20import org.whispersystems.libsignal.state.PreKeyRecord;
21import org.whispersystems.libsignal.state.SignedPreKeyRecord;
22import org.whispersystems.libsignal.util.KeyHelper;
23
24import java.security.PrivateKey;
25import java.security.Security;
26import java.security.Signature;
27import java.security.cert.X509Certificate;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.HashMap;
33import java.util.HashSet;
34import java.util.Iterator;
35import java.util.List;
36import java.util.Map;
37import java.util.Random;
38import java.util.Set;
39import java.util.concurrent.atomic.AtomicBoolean;
40
41import eu.siacs.conversations.Config;
42import eu.siacs.conversations.entities.Account;
43import eu.siacs.conversations.entities.Contact;
44import eu.siacs.conversations.entities.Conversation;
45import eu.siacs.conversations.entities.Message;
46import eu.siacs.conversations.parser.IqParser;
47import eu.siacs.conversations.services.XmppConnectionService;
48import eu.siacs.conversations.utils.CryptoHelper;
49import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
50import eu.siacs.conversations.xml.Element;
51import eu.siacs.conversations.xml.Namespace;
52import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
53import eu.siacs.conversations.xmpp.OnIqPacketReceived;
54import eu.siacs.conversations.xmpp.pep.PublishOptions;
55import eu.siacs.conversations.xmpp.stanzas.IqPacket;
56import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
57import rocks.xmpp.addr.Jid;
58
59public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
60
61 public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
62 public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
63 public static final String PEP_DEVICE_LIST_NOTIFY = PEP_DEVICE_LIST + "+notify";
64 public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";
65 public static final String PEP_VERIFICATION = PEP_PREFIX + ".verification";
66 public static final String PEP_OMEMO_WHITELISTED = PEP_PREFIX + ".whitelisted";
67
68 public static final String LOGPREFIX = "AxolotlService";
69
70 public static final int NUM_KEYS_TO_PUBLISH = 100;
71 public static final int publishTriesThreshold = 3;
72
73 private final Account account;
74 private final XmppConnectionService mXmppConnectionService;
75 private final SQLiteAxolotlStore axolotlStore;
76 private final SessionMap sessions;
77 private final Map<Jid, Set<Integer>> deviceIds;
78 private final Map<String, XmppAxolotlMessage> messageCache;
79 private final FetchStatusMap fetchStatusMap;
80 private final Map<Jid, Boolean> fetchDeviceListStatus = new HashMap<>();
81 private final HashMap<Jid, List<OnDeviceIdsFetched>> fetchDeviceIdsMap = new HashMap<>();
82 private final SerialSingleThreadExecutor executor;
83 private int numPublishTriesOnEmptyPep = 0;
84 private boolean pepBroken = false;
85 private final Set<SignalProtocolAddress> healingAttempts = new HashSet<>();
86 private int lastDeviceListNotificationHash = 0;
87 private final HashSet<Integer> cleanedOwnDeviceIds = new HashSet<>();
88 private Set<XmppAxolotlSession> postponedSessions = new HashSet<>(); //sessions stored here will receive after mam catchup treatment
89 private Set<SignalProtocolAddress> postponedHealing = new HashSet<>(); //addresses stored here will need a healing notification after mam catchup
90
91 private AtomicBoolean changeAccessMode = new AtomicBoolean(false);
92
93 @Override
94 public void onAdvancedStreamFeaturesAvailable(Account account) {
95 if (Config.supportOmemo()
96 && account.getXmppConnection() != null
97 && account.getXmppConnection().getFeatures().pep()) {
98 publishBundlesIfNeeded(true, false);
99 } else {
100 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": skipping OMEMO initialization");
101 }
102 }
103
104 private boolean hasErrorFetchingDeviceList(Jid jid) {
105 Boolean status = fetchDeviceListStatus.get(jid);
106 return status != null && !status;
107 }
108
109 public boolean hasErrorFetchingDeviceList(List<Jid> jids) {
110 for(Jid jid : jids) {
111 if (hasErrorFetchingDeviceList(jid)) {
112 return true;
113 }
114 }
115 return false;
116 }
117
118 public boolean fetchMapHasErrors(List<Jid> jids) {
119 for (Jid jid : jids) {
120 if (deviceIds.get(jid) != null) {
121 for (Integer foreignId : this.deviceIds.get(jid)) {
122 SignalProtocolAddress address = new SignalProtocolAddress(jid.toString(), foreignId);
123 if (fetchStatusMap.getAll(address.getName()).containsValue(FetchStatus.ERROR)) {
124 return true;
125 }
126 }
127 }
128 }
129 return false;
130 }
131
132 public void preVerifyFingerprint(Contact contact, String fingerprint) {
133 axolotlStore.preVerifyFingerprint(contact.getAccount(), contact.getJid().asBareJid().toString(), fingerprint);
134 }
135
136 public void preVerifyFingerprint(Account account, String fingerprint) {
137 axolotlStore.preVerifyFingerprint(account, account.getJid().asBareJid().toString(), fingerprint);
138 }
139
140 public boolean hasVerifiedKeys(String name) {
141 for (XmppAxolotlSession session : this.sessions.getAll(name).values()) {
142 if (session.getTrust().isVerified()) {
143 return true;
144 }
145 }
146 return false;
147 }
148
149 private static class AxolotlAddressMap<T> {
150 protected Map<String, Map<Integer, T>> map;
151 protected final Object MAP_LOCK = new Object();
152
153 public AxolotlAddressMap() {
154 this.map = new HashMap<>();
155 }
156
157 public void put(SignalProtocolAddress address, T value) {
158 synchronized (MAP_LOCK) {
159 Map<Integer, T> devices = map.get(address.getName());
160 if (devices == null) {
161 devices = new HashMap<>();
162 map.put(address.getName(), devices);
163 }
164 devices.put(address.getDeviceId(), value);
165 }
166 }
167
168 public T get(SignalProtocolAddress address) {
169 synchronized (MAP_LOCK) {
170 Map<Integer, T> devices = map.get(address.getName());
171 if (devices == null) {
172 return null;
173 }
174 return devices.get(address.getDeviceId());
175 }
176 }
177
178 public Map<Integer, T> getAll(String name) {
179 synchronized (MAP_LOCK) {
180 Map<Integer, T> devices = map.get(name);
181 if (devices == null) {
182 return new HashMap<>();
183 }
184 return devices;
185 }
186 }
187
188 public boolean hasAny(SignalProtocolAddress address) {
189 synchronized (MAP_LOCK) {
190 Map<Integer, T> devices = map.get(address.getName());
191 return devices != null && !devices.isEmpty();
192 }
193 }
194
195 public void clear() {
196 map.clear();
197 }
198
199 }
200
201 private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
202 private final XmppConnectionService xmppConnectionService;
203 private final Account account;
204
205 public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
206 super();
207 this.xmppConnectionService = service;
208 this.account = account;
209 this.fillMap(store);
210 }
211
212 public Set<Jid> findCounterpartsForSourceId(Integer sid) {
213 Set<Jid> candidates = new HashSet<>();
214 synchronized (MAP_LOCK) {
215 for(Map.Entry<String,Map<Integer,XmppAxolotlSession>> entry : map.entrySet()) {
216 String key = entry.getKey();
217 if (entry.getValue().containsKey(sid)) {
218 candidates.add(Jid.of(key));
219 }
220 }
221 }
222 return candidates;
223 }
224
225 private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
226 for (Integer deviceId : deviceIds) {
227 SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(bareJid, deviceId);
228 IdentityKey identityKey = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey();
229 if (Config.X509_VERIFICATION) {
230 X509Certificate certificate = store.getFingerprintCertificate(CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()));
231 if (certificate != null) {
232 Bundle information = CryptoHelper.extractCertificateInformation(certificate);
233 try {
234 final String cn = information.getString("subject_cn");
235 final Jid jid = Jid.of(bareJid);
236 Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
237 account.getRoster().getContact(jid).setCommonName(cn);
238 } catch (final IllegalArgumentException ignored) {
239 //ignored
240 }
241 }
242 }
243 this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, identityKey));
244 }
245 }
246
247 private void fillMap(SQLiteAxolotlStore store) {
248 List<Integer> deviceIds = store.getSubDeviceSessions(account.getJid().asBareJid().toString());
249 putDevicesForJid(account.getJid().asBareJid().toString(), deviceIds, store);
250 for (String address : store.getKnownAddresses()) {
251 deviceIds = store.getSubDeviceSessions(address);
252 putDevicesForJid(address, deviceIds, store);
253 }
254 }
255
256 @Override
257 public void put(SignalProtocolAddress address, XmppAxolotlSession value) {
258 super.put(address, value);
259 value.setNotFresh();
260 }
261
262 public void put(XmppAxolotlSession session) {
263 this.put(session.getRemoteAddress(), session);
264 }
265 }
266
267 public enum FetchStatus {
268 PENDING,
269 SUCCESS,
270 SUCCESS_VERIFIED,
271 TIMEOUT,
272 SUCCESS_TRUSTED,
273 ERROR
274 }
275
276 private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
277
278 public void clearErrorFor(Jid jid) {
279 synchronized (MAP_LOCK) {
280 Map<Integer, FetchStatus> devices = this.map.get(jid.asBareJid().toString());
281 if (devices == null) {
282 return;
283 }
284 for (Map.Entry<Integer, FetchStatus> entry : devices.entrySet()) {
285 if (entry.getValue() == FetchStatus.ERROR) {
286 Log.d(Config.LOGTAG, "resetting error for " + jid.asBareJid() + "(" + entry.getKey() + ")");
287 entry.setValue(FetchStatus.TIMEOUT);
288 }
289 }
290 }
291 }
292 }
293
294 public static String getLogprefix(Account account) {
295 return LOGPREFIX + " (" + account.getJid().asBareJid().toString() + "): ";
296 }
297
298 public AxolotlService(Account account, XmppConnectionService connectionService) {
299 if (account == null || connectionService == null) {
300 throw new IllegalArgumentException("account and service cannot be null");
301 }
302 if (Security.getProvider("BC") == null) {
303 Security.addProvider(new BouncyCastleProvider());
304 }
305 this.mXmppConnectionService = connectionService;
306 this.account = account;
307 this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
308 this.deviceIds = new HashMap<>();
309 this.messageCache = new HashMap<>();
310 this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
311 this.fetchStatusMap = new FetchStatusMap();
312 this.executor = new SerialSingleThreadExecutor("Axolotl");
313 }
314
315 public String getOwnFingerprint() {
316 return CryptoHelper.bytesToHex(axolotlStore.getIdentityKeyPair().getPublicKey().serialize());
317 }
318
319 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status) {
320 return axolotlStore.getContactKeysWithTrust(account.getJid().asBareJid().toString(), status);
321 }
322
323 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status, Jid jid) {
324 return axolotlStore.getContactKeysWithTrust(jid.asBareJid().toString(), status);
325 }
326
327 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status, List<Jid> jids) {
328 Set<IdentityKey> keys = new HashSet<>();
329 for (Jid jid : jids) {
330 keys.addAll(axolotlStore.getContactKeysWithTrust(jid.toString(), status));
331 }
332 return keys;
333 }
334
335 public Set<Jid> findCounterpartsBySourceId(int sid) {
336 return sessions.findCounterpartsForSourceId(sid);
337 }
338
339 public long getNumTrustedKeys(Jid jid) {
340 return axolotlStore.getContactNumTrustedKeys(jid.asBareJid().toString());
341 }
342
343 public boolean anyTargetHasNoTrustedKeys(List<Jid> jids) {
344 for (Jid jid : jids) {
345 if (axolotlStore.getContactNumTrustedKeys(jid.asBareJid().toString()) == 0) {
346 return true;
347 }
348 }
349 return false;
350 }
351
352 private SignalProtocolAddress getAddressForJid(Jid jid) {
353 return new SignalProtocolAddress(jid.toString(), 0);
354 }
355
356 public Collection<XmppAxolotlSession> findOwnSessions() {
357 SignalProtocolAddress ownAddress = getAddressForJid(account.getJid().asBareJid());
358 ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(ownAddress.getName()).values());
359 Collections.sort(s);
360 return s;
361 }
362
363
364 public Collection<XmppAxolotlSession> findSessionsForContact(Contact contact) {
365 SignalProtocolAddress contactAddress = getAddressForJid(contact.getJid());
366 ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(contactAddress.getName()).values());
367 Collections.sort(s);
368 return s;
369 }
370
371 private Set<XmppAxolotlSession> findSessionsForConversation(Conversation conversation) {
372 if (conversation.getContact().isSelf()) {
373 //will be added in findOwnSessions()
374 return Collections.emptySet();
375 }
376 HashSet<XmppAxolotlSession> sessions = new HashSet<>();
377 for (Jid jid : conversation.getAcceptedCryptoTargets()) {
378 sessions.addAll(this.sessions.getAll(getAddressForJid(jid).getName()).values());
379 }
380 return sessions;
381 }
382
383 private boolean hasAny(Jid jid) {
384 return sessions.hasAny(getAddressForJid(jid));
385 }
386
387 public boolean isPepBroken() {
388 return this.pepBroken;
389 }
390
391 public void resetBrokenness() {
392 this.pepBroken = false;
393 this.numPublishTriesOnEmptyPep = 0;
394 this.lastDeviceListNotificationHash = 0;
395 this.healingAttempts.clear();
396 }
397
398 public void clearErrorsInFetchStatusMap(Jid jid) {
399 fetchStatusMap.clearErrorFor(jid);
400 fetchDeviceListStatus.remove(jid);
401 }
402
403 public void regenerateKeys(boolean wipeOther) {
404 axolotlStore.regenerate();
405 sessions.clear();
406 fetchStatusMap.clear();
407 fetchDeviceIdsMap.clear();
408 fetchDeviceListStatus.clear();
409 publishBundlesIfNeeded(true, wipeOther);
410 }
411
412 public void destroy() {
413 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": destroying old axolotl service. no longer in use");
414 mXmppConnectionService.databaseBackend.wipeAxolotlDb(account);
415 }
416
417 public AxolotlService makeNew() {
418 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": make new axolotl service");
419 return new AxolotlService(this.account, this.mXmppConnectionService);
420 }
421
422 public int getOwnDeviceId() {
423 return axolotlStore.getLocalRegistrationId();
424 }
425
426 public SignalProtocolAddress getOwnAxolotlAddress() {
427 return new SignalProtocolAddress(account.getJid().asBareJid().toString(), getOwnDeviceId());
428 }
429
430 public Set<Integer> getOwnDeviceIds() {
431 return this.deviceIds.get(account.getJid().asBareJid());
432 }
433
434 public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
435 final int hash = deviceIds.hashCode();
436 final boolean me = jid.asBareJid().equals(account.getJid().asBareJid());
437 if (me) {
438 if (hash != 0 && hash == this.lastDeviceListNotificationHash) {
439 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring duplicate own device id list");
440 return;
441 }
442 this.lastDeviceListNotificationHash = hash;
443 }
444 boolean needsPublishing = me && !deviceIds.contains(getOwnDeviceId());
445 if (me) {
446 deviceIds.remove(getOwnDeviceId());
447 }
448 Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.asBareJid().toString()));
449 expiredDevices.removeAll(deviceIds);
450 for (Integer deviceId : expiredDevices) {
451 SignalProtocolAddress address = new SignalProtocolAddress(jid.asBareJid().toString(), deviceId);
452 XmppAxolotlSession session = sessions.get(address);
453 if (session != null && session.getFingerprint() != null) {
454 if (session.getTrust().isActive()) {
455 session.setTrust(session.getTrust().toInactive());
456 }
457 }
458 }
459 Set<Integer> newDevices = new HashSet<>(deviceIds);
460 for (Integer deviceId : newDevices) {
461 SignalProtocolAddress address = new SignalProtocolAddress(jid.asBareJid().toString(), deviceId);
462 XmppAxolotlSession session = sessions.get(address);
463 if (session != null && session.getFingerprint() != null) {
464 if (!session.getTrust().isActive()) {
465 Log.d(Config.LOGTAG, "reactivating device with fingerprint " + session.getFingerprint());
466 session.setTrust(session.getTrust().toActive());
467 }
468 }
469 }
470 if (me) {
471 if (Config.OMEMO_AUTO_EXPIRY != 0) {
472 needsPublishing |= deviceIds.removeAll(getExpiredDevices());
473 }
474 needsPublishing |= this.changeAccessMode.get();
475 for (Integer deviceId : deviceIds) {
476 SignalProtocolAddress ownDeviceAddress = new SignalProtocolAddress(jid.asBareJid().toString(), deviceId);
477 if (sessions.get(ownDeviceAddress) == null) {
478 FetchStatus status = fetchStatusMap.get(ownDeviceAddress);
479 if (status == null || status == FetchStatus.TIMEOUT) {
480 fetchStatusMap.put(ownDeviceAddress, FetchStatus.PENDING);
481 this.buildSessionFromPEP(ownDeviceAddress);
482 }
483 }
484 }
485 if (needsPublishing) {
486 publishOwnDeviceId(deviceIds);
487 }
488 }
489 final Set<Integer> oldSet = this.deviceIds.get(jid);
490 final boolean changed = oldSet == null || oldSet.hashCode() != hash;
491 this.deviceIds.put(jid, deviceIds);
492 if (changed) {
493 mXmppConnectionService.updateConversationUi(); //update the lock icon
494 mXmppConnectionService.keyStatusUpdated(null);
495 if (me) {
496 mXmppConnectionService.updateAccountUi();
497 }
498 } else {
499 Log.d(Config.LOGTAG,"skipped device list update because it hasn't changed");
500 }
501 }
502
503 public void wipeOtherPepDevices() {
504 if (pepBroken) {
505 Log.d(Config.LOGTAG, getLogprefix(account) + "wipeOtherPepDevices called, but PEP is broken. Ignoring... ");
506 return;
507 }
508 Set<Integer> deviceIds = new HashSet<>();
509 deviceIds.add(getOwnDeviceId());
510 publishDeviceIdsAndRefineAccessModel(deviceIds);
511 }
512
513 public void distrustFingerprint(final String fingerprint) {
514 final String fp = fingerprint.replaceAll("\\s", "");
515 final FingerprintStatus fingerprintStatus = axolotlStore.getFingerprintStatus(fp);
516 axolotlStore.setFingerprintStatus(fp, fingerprintStatus.toUntrusted());
517 }
518
519 private void publishOwnDeviceIdIfNeeded() {
520 if (pepBroken) {
521 Log.d(Config.LOGTAG, getLogprefix(account) + "publishOwnDeviceIdIfNeeded called, but PEP is broken. Ignoring... ");
522 return;
523 }
524 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().asBareJid());
525 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
526 @Override
527 public void onIqPacketReceived(Account account, IqPacket packet) {
528 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
529 Log.d(Config.LOGTAG, getLogprefix(account) + "Timeout received while retrieving own Device Ids.");
530 } else {
531 //TODO consider calling registerDevices only after item-not-found to account for broken PEPs
532 Element item = mXmppConnectionService.getIqParser().getItem(packet);
533 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
534 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": retrieved own device list: " + deviceIds);
535 registerDevices(account.getJid().asBareJid(), deviceIds);
536 }
537 }
538 });
539 }
540
541 private Set<Integer> getExpiredDevices() {
542 Set<Integer> devices = new HashSet<>();
543 for (XmppAxolotlSession session : findOwnSessions()) {
544 if (session.getTrust().isActive()) {
545 long diff = System.currentTimeMillis() - session.getTrust().getLastActivation();
546 if (diff > Config.OMEMO_AUTO_EXPIRY) {
547 long lastMessageDiff = System.currentTimeMillis() - mXmppConnectionService.databaseBackend.getLastTimeFingerprintUsed(account, session.getFingerprint());
548 long hours = Math.round(lastMessageDiff / (1000 * 60.0 * 60.0));
549 if (lastMessageDiff > Config.OMEMO_AUTO_EXPIRY) {
550 devices.add(session.getRemoteAddress().getDeviceId());
551 session.setTrust(session.getTrust().toInactive());
552 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": added own device " + session.getFingerprint() + " to list of expired devices. Last message received " + hours + " hours ago");
553 } else {
554 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": own device " + session.getFingerprint() + " was active " + hours + " hours ago");
555 }
556 } //TODO print last activation diff
557 }
558 }
559 return devices;
560 }
561
562 private void publishOwnDeviceId(Set<Integer> deviceIds) {
563 Set<Integer> deviceIdsCopy = new HashSet<>(deviceIds);
564 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "publishing own device ids");
565 if (deviceIdsCopy.isEmpty()) {
566 if (numPublishTriesOnEmptyPep >= publishTriesThreshold) {
567 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device publish attempt threshold exceeded, aborting...");
568 pepBroken = true;
569 return;
570 } else {
571 numPublishTriesOnEmptyPep++;
572 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device list empty, attempting to publish (try " + numPublishTriesOnEmptyPep + ")");
573 }
574 } else {
575 numPublishTriesOnEmptyPep = 0;
576 }
577 deviceIdsCopy.add(getOwnDeviceId());
578 publishDeviceIdsAndRefineAccessModel(deviceIdsCopy);
579 }
580
581 private void publishDeviceIdsAndRefineAccessModel(Set<Integer> ids) {
582 publishDeviceIdsAndRefineAccessModel(ids, true);
583 }
584
585 private void publishDeviceIdsAndRefineAccessModel(final Set<Integer> ids, final boolean firstAttempt) {
586 final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
587 IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(ids, publishOptions);
588 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
589 @Override
590 public void onIqPacketReceived(Account account, IqPacket packet) {
591 final Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
592 final boolean preConditionNotMet = PublishOptions.preconditionNotMet(packet);
593 if (firstAttempt && preConditionNotMet) {
594 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for device list. pushing node configuration");
595 mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
596 @Override
597 public void onPushSucceeded() {
598 publishDeviceIdsAndRefineAccessModel(ids, false);
599 }
600
601 @Override
602 public void onPushFailed() {
603 publishDeviceIdsAndRefineAccessModel(ids, false);
604 }
605 });
606 } else {
607 if (AxolotlService.this.changeAccessMode.compareAndSet(true, false)) {
608 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": done changing access mode");
609 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, false);
610 mXmppConnectionService.databaseBackend.updateAccount(account);
611 }
612 if (packet.getType() == IqPacket.TYPE.ERROR) {
613 if (preConditionNotMet) {
614 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": device list pre condition still not met on second attempt");
615 } else if (error != null) {
616 pepBroken = true;
617 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
618 }
619
620 }
621 }
622 }
623 });
624 }
625
626 public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord,
627 final Set<PreKeyRecord> preKeyRecords,
628 final boolean announceAfter,
629 final boolean wipe) {
630 try {
631 IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
632 PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
633 X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
634 Signature verifier = Signature.getInstance("sha256WithRSA");
635 verifier.initSign(x509PrivateKey, mXmppConnectionService.getRNG());
636 verifier.update(axolotlPublicKey.serialize());
637 byte[] signature = verifier.sign();
638 IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
639 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device " + getOwnDeviceId());
640 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
641 @Override
642 public void onIqPacketReceived(final Account account, IqPacket packet) {
643 String node = AxolotlService.PEP_VERIFICATION + ":" + getOwnDeviceId();
644 mXmppConnectionService.pushNodeConfiguration(account, node, PublishOptions.openAccess(), new XmppConnectionService.OnConfigurationPushed() {
645 @Override
646 public void onPushSucceeded() {
647 Log.d(Config.LOGTAG, getLogprefix(account) + "configured verification node to be world readable");
648 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
649 }
650
651 @Override
652 public void onPushFailed() {
653 Log.d(Config.LOGTAG, getLogprefix(account) + "unable to set access model on verification node");
654 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
655 }
656 });
657 }
658 });
659 } catch (Exception e) {
660 e.printStackTrace();
661 }
662 }
663
664 public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
665 if (pepBroken) {
666 Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
667 return;
668 }
669
670 if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
671 this.changeAccessMode.set(account.isOptionSet(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE));
672 } else {
673 if (account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true)) {
674 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server doesn’t support publish-options. setting for later access mode change");
675 mXmppConnectionService.databaseBackend.updateAccount(account);
676 }
677 }
678 if (this.changeAccessMode.get()) {
679 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server gained publish-options capabilities. changing access model");
680 }
681 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().asBareJid(), getOwnDeviceId());
682 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
683 @Override
684 public void onIqPacketReceived(Account account, IqPacket packet) {
685
686 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
687 return; //ignore timeout. do nothing
688 }
689
690 if (packet.getType() == IqPacket.TYPE.ERROR) {
691 Element error = packet.findChild("error");
692 if (error == null || !error.hasChild("item-not-found")) {
693 pepBroken = true;
694 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
695 return;
696 }
697 }
698
699 PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
700 Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
701 boolean flush = false;
702 if (bundle == null) {
703 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
704 bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
705 flush = true;
706 }
707 if (keys == null) {
708 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
709 }
710 try {
711 boolean changed = false;
712 // Validate IdentityKey
713 IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
714 if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
715 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
716 changed = true;
717 }
718
719 // Validate signedPreKeyRecord + ID
720 SignedPreKeyRecord signedPreKeyRecord;
721 int numSignedPreKeys = axolotlStore.getSignedPreKeysCount();
722 try {
723 signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
724 if (flush
725 || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
726 || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
727 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
728 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
729 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
730 changed = true;
731 }
732 } catch (InvalidKeyIdException e) {
733 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
734 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
735 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
736 changed = true;
737 }
738
739 // Validate PreKeys
740 Set<PreKeyRecord> preKeyRecords = new HashSet<>();
741 if (keys != null) {
742 for (Integer id : keys.keySet()) {
743 try {
744 PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
745 if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
746 preKeyRecords.add(preKeyRecord);
747 }
748 } catch (InvalidKeyIdException ignored) {
749 }
750 }
751 }
752 int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
753 if (newKeys > 0) {
754 List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
755 axolotlStore.getCurrentPreKeyId() + 1, newKeys);
756 preKeyRecords.addAll(newRecords);
757 for (PreKeyRecord record : newRecords) {
758 axolotlStore.storePreKey(record.getId(), record);
759 }
760 changed = true;
761 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
762 }
763
764
765 if (changed || changeAccessMode.get()) {
766 if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
767 mXmppConnectionService.publishDisplayName(account);
768 publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
769 } else {
770 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
771 }
772 } else {
773 Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
774 if (wipe) {
775 wipeOtherPepDevices();
776 } else if (announce) {
777 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
778 publishOwnDeviceIdIfNeeded();
779 }
780 }
781 } catch (InvalidKeyException e) {
782 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
783 }
784 }
785 });
786 }
787
788 private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord,
789 Set<PreKeyRecord> preKeyRecords,
790 final boolean announceAfter,
791 final boolean wipe) {
792 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, true);
793 }
794
795 private void publishDeviceBundle(final SignedPreKeyRecord signedPreKeyRecord,
796 final Set<PreKeyRecord> preKeyRecords,
797 final boolean announceAfter,
798 final boolean wipe,
799 final boolean firstAttempt) {
800 final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
801 IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
802 signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
803 preKeyRecords, getOwnDeviceId(), publishOptions);
804 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing...");
805 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
806 @Override
807 public void onIqPacketReceived(final Account account, IqPacket packet) {
808 final boolean preconditionNotMet = PublishOptions.preconditionNotMet(packet);
809 if (firstAttempt && preconditionNotMet) {
810 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for bundle. pushing node configuration");
811 final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId();
812 mXmppConnectionService.pushNodeConfiguration(account, node, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
813 @Override
814 public void onPushSucceeded() {
815 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
816 }
817
818 @Override
819 public void onPushFailed() {
820 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
821 }
822 });
823 } else if (packet.getType() == IqPacket.TYPE.RESULT) {
824 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
825 if (wipe) {
826 wipeOtherPepDevices();
827 } else if (announceAfter) {
828 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
829 publishOwnDeviceIdIfNeeded();
830 }
831 } else if (packet.getType() == IqPacket.TYPE.ERROR) {
832 if (preconditionNotMet) {
833 Log.d(Config.LOGTAG,getLogprefix(account) + "bundle precondition still not met after second attempt");
834 } else {
835 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.toString());
836 }
837 pepBroken = true;
838 }
839 }
840 });
841 }
842
843 public List<Jid> getCryptoTargets(Conversation conversation) {
844 final List<Jid> jids;
845 if (conversation.getMode() == Conversation.MODE_SINGLE) {
846 jids = new ArrayList<>();
847 jids.add(conversation.getJid().asBareJid());
848 } else {
849 jids = conversation.getMucOptions().getMembers(false);
850 }
851 return jids;
852 }
853
854 public FingerprintStatus getFingerprintTrust(String fingerprint) {
855 return axolotlStore.getFingerprintStatus(fingerprint);
856 }
857
858 public X509Certificate getFingerprintCertificate(String fingerprint) {
859 return axolotlStore.getFingerprintCertificate(fingerprint);
860 }
861
862 public void setFingerprintTrust(String fingerprint, FingerprintStatus status) {
863 axolotlStore.setFingerprintStatus(fingerprint, status);
864 }
865
866 private void verifySessionWithPEP(final XmppAxolotlSession session) {
867 Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
868 final SignalProtocolAddress address = session.getRemoteAddress();
869 final IdentityKey identityKey = session.getIdentityKey();
870 try {
871 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.of(address.getName()), address.getDeviceId());
872 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
873 @Override
874 public void onIqPacketReceived(Account account, IqPacket packet) {
875 Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
876 if (verification != null) {
877 try {
878 Signature verifier = Signature.getInstance("sha256WithRSA");
879 verifier.initVerify(verification.first[0]);
880 verifier.update(identityKey.serialize());
881 if (verifier.verify(verification.second)) {
882 try {
883 mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
884 String fingerprint = session.getFingerprint();
885 Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
886 setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
887 axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
888 fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
889 Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
890 try {
891 final String cn = information.getString("subject_cn");
892 final Jid jid = Jid.of(address.getName());
893 Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
894 account.getRoster().getContact(jid).setCommonName(cn);
895 } catch (final IllegalArgumentException ignored) {
896 //ignored
897 }
898 finishBuildingSessionsFromPEP(address);
899 return;
900 } catch (Exception e) {
901 Log.d(Config.LOGTAG, "could not verify certificate");
902 }
903 }
904 } catch (Exception e) {
905 Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
906 }
907 } else {
908 Log.d(Config.LOGTAG, "no verification found");
909 }
910 fetchStatusMap.put(address, FetchStatus.SUCCESS);
911 finishBuildingSessionsFromPEP(address);
912 }
913 });
914 } catch (IllegalArgumentException e) {
915 fetchStatusMap.put(address, FetchStatus.SUCCESS);
916 finishBuildingSessionsFromPEP(address);
917 }
918 }
919
920 private final Set<Integer> PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT = new HashSet<>();
921
922 private void finishBuildingSessionsFromPEP(final SignalProtocolAddress address) {
923 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), 0);
924 Map<Integer, FetchStatus> own = fetchStatusMap.getAll(ownAddress.getName());
925 Map<Integer, FetchStatus> remote = fetchStatusMap.getAll(address.getName());
926 if (!own.containsValue(FetchStatus.PENDING) && !remote.containsValue(FetchStatus.PENDING)) {
927 FetchStatus report = null;
928 if (own.containsValue(FetchStatus.SUCCESS) || remote.containsValue(FetchStatus.SUCCESS)) {
929 report = FetchStatus.SUCCESS;
930 } else if (own.containsValue(FetchStatus.SUCCESS_VERIFIED) || remote.containsValue(FetchStatus.SUCCESS_VERIFIED)) {
931 report = FetchStatus.SUCCESS_VERIFIED;
932 } else if (own.containsValue(FetchStatus.SUCCESS_TRUSTED) || remote.containsValue(FetchStatus.SUCCESS_TRUSTED)) {
933 report = FetchStatus.SUCCESS_TRUSTED;
934 } else if (own.containsValue(FetchStatus.ERROR) || remote.containsValue(FetchStatus.ERROR)) {
935 report = FetchStatus.ERROR;
936 }
937 mXmppConnectionService.keyStatusUpdated(report);
938 }
939 if (Config.REMOVE_BROKEN_DEVICES) {
940 Set<Integer> ownDeviceIds = new HashSet<>(getOwnDeviceIds());
941 boolean publish = false;
942 for (Map.Entry<Integer, FetchStatus> entry : own.entrySet()) {
943 int id = entry.getKey();
944 if (entry.getValue() == FetchStatus.ERROR && PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT.add(id) && ownDeviceIds.remove(id)) {
945 publish = true;
946 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error fetching own device with id " + id + ". removing from announcement");
947 }
948 }
949 if (publish) {
950 publishOwnDeviceId(ownDeviceIds);
951 }
952 }
953 }
954
955 public boolean hasEmptyDeviceList(Jid jid) {
956 return !hasAny(jid) && (!deviceIds.containsKey(jid) || deviceIds.get(jid).isEmpty());
957 }
958
959 public interface OnDeviceIdsFetched {
960 void fetched(Jid jid, Set<Integer> deviceIds);
961 }
962
963 public interface OnMultipleDeviceIdFetched {
964 void fetched();
965 }
966
967 public void fetchDeviceIds(final Jid jid) {
968 fetchDeviceIds(jid, null);
969 }
970
971 private void fetchDeviceIds(final Jid jid, OnDeviceIdsFetched callback) {
972 IqPacket packet;
973 synchronized (this.fetchDeviceIdsMap) {
974 List<OnDeviceIdsFetched> callbacks = this.fetchDeviceIdsMap.get(jid);
975 if (callbacks != null) {
976 if (callback != null) {
977 callbacks.add(callback);
978 }
979 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid + " already running. adding callback");
980 packet = null;
981 } else {
982 callbacks = new ArrayList<>();
983 if (callback != null) {
984 callbacks.add(callback);
985 }
986 this.fetchDeviceIdsMap.put(jid, callbacks);
987 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid);
988 packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(jid);
989 }
990 }
991 if (packet != null) {
992 mXmppConnectionService.sendIqPacket(account, packet, (account, response) -> {
993 if (response.getType() == IqPacket.TYPE.RESULT) {
994 fetchDeviceListStatus.put(jid, true);
995 Element item = mXmppConnectionService.getIqParser().getItem(response);
996 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
997 registerDevices(jid, deviceIds);
998 final List<OnDeviceIdsFetched> callbacks;
999 synchronized (fetchDeviceIdsMap) {
1000 callbacks = fetchDeviceIdsMap.remove(jid);
1001 }
1002 if (callbacks != null) {
1003 for (OnDeviceIdsFetched c : callbacks) {
1004 c.fetched(jid, deviceIds);
1005 }
1006 }
1007 } else {
1008 if (response.getType() == IqPacket.TYPE.TIMEOUT) {
1009 fetchDeviceListStatus.remove(jid);
1010 } else {
1011 fetchDeviceListStatus.put(jid, false);
1012 }
1013 final List<OnDeviceIdsFetched> callbacks;
1014 synchronized (fetchDeviceIdsMap) {
1015 callbacks = fetchDeviceIdsMap.remove(jid);
1016 }
1017 if (callbacks != null) {
1018 for (OnDeviceIdsFetched c : callbacks) {
1019 c.fetched(jid, null);
1020 }
1021 }
1022 }
1023 });
1024 }
1025 }
1026
1027 private void fetchDeviceIds(List<Jid> jids, final OnMultipleDeviceIdFetched callback) {
1028 final ArrayList<Jid> unfinishedJids = new ArrayList<>(jids);
1029 synchronized (unfinishedJids) {
1030 for (Jid jid : unfinishedJids) {
1031 fetchDeviceIds(jid, (j, deviceIds) -> {
1032 synchronized (unfinishedJids) {
1033 unfinishedJids.remove(j);
1034 if (unfinishedJids.size() == 0 && callback != null) {
1035 callback.fetched();
1036 }
1037 }
1038 });
1039 }
1040 }
1041 }
1042
1043 interface OnSessionBuildFromPep {
1044 void onSessionBuildSuccessful();
1045 void onSessionBuildFailed();
1046 }
1047
1048 private void buildSessionFromPEP(final SignalProtocolAddress address) {
1049 buildSessionFromPEP(address, null);
1050 }
1051
1052 private void buildSessionFromPEP(final SignalProtocolAddress address, OnSessionBuildFromPep callback) {
1053 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new session for " + address.toString());
1054 if (address.equals(getOwnAxolotlAddress())) {
1055 throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
1056 }
1057
1058 final Jid jid = Jid.of(address.getName());
1059 final boolean oneOfOurs = jid.asBareJid().equals(account.getJid().asBareJid());
1060 IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(jid, address.getDeviceId());
1061 mXmppConnectionService.sendIqPacket(account, bundlesPacket, (account, packet) -> {
1062 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1063 fetchStatusMap.put(address, FetchStatus.TIMEOUT);
1064 } else if (packet.getType() == IqPacket.TYPE.RESULT) {
1065 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
1066 final IqParser parser = mXmppConnectionService.getIqParser();
1067 final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
1068 final PreKeyBundle bundle = parser.bundle(packet);
1069 if (preKeyBundleList.isEmpty() || bundle == null) {
1070 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
1071 fetchStatusMap.put(address, FetchStatus.ERROR);
1072 finishBuildingSessionsFromPEP(address);
1073 if (callback != null) {
1074 callback.onSessionBuildFailed();
1075 }
1076 return;
1077 }
1078 Random random = new Random();
1079 final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
1080 if (preKey == null) {
1081 //should never happen
1082 fetchStatusMap.put(address, FetchStatus.ERROR);
1083 finishBuildingSessionsFromPEP(address);
1084 if (callback != null) {
1085 callback.onSessionBuildFailed();
1086 }
1087 return;
1088 }
1089
1090 final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
1091 preKey.getPreKeyId(), preKey.getPreKey(),
1092 bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
1093 bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
1094
1095 try {
1096 SessionBuilder builder = new SessionBuilder(axolotlStore, address);
1097 builder.process(preKeyBundle);
1098 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
1099 sessions.put(address, session);
1100 if (Config.X509_VERIFICATION) {
1101 verifySessionWithPEP(session); //TODO; maybe inject callback in here too
1102 } else {
1103 FingerprintStatus status = getFingerprintTrust(CryptoHelper.bytesToHex(bundle.getIdentityKey().getPublicKey().serialize()));
1104 FetchStatus fetchStatus;
1105 if (status != null && status.isVerified()) {
1106 fetchStatus = FetchStatus.SUCCESS_VERIFIED;
1107 } else if (status != null && status.isTrusted()) {
1108 fetchStatus = FetchStatus.SUCCESS_TRUSTED;
1109 } else {
1110 fetchStatus = FetchStatus.SUCCESS;
1111 }
1112 fetchStatusMap.put(address, fetchStatus);
1113 finishBuildingSessionsFromPEP(address);
1114 if (callback != null) {
1115 callback.onSessionBuildSuccessful();
1116 }
1117 }
1118 } catch (UntrustedIdentityException | InvalidKeyException e) {
1119 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
1120 + e.getClass().getName() + ", " + e.getMessage());
1121 fetchStatusMap.put(address, FetchStatus.ERROR);
1122 finishBuildingSessionsFromPEP(address);
1123 if (oneOfOurs && cleanedOwnDeviceIds.add(address.getDeviceId())) {
1124 removeFromDeviceAnnouncement(address.getDeviceId());
1125 }
1126 if (callback != null) {
1127 callback.onSessionBuildFailed();
1128 }
1129 }
1130 } else {
1131 fetchStatusMap.put(address, FetchStatus.ERROR);
1132 Element error = packet.findChild("error");
1133 boolean itemNotFound = error != null && error.hasChild("item-not-found");
1134 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
1135 finishBuildingSessionsFromPEP(address);
1136 if (oneOfOurs && itemNotFound && cleanedOwnDeviceIds.add(address.getDeviceId())) {
1137 removeFromDeviceAnnouncement(address.getDeviceId());
1138 }
1139 if (callback != null) {
1140 callback.onSessionBuildFailed();
1141 }
1142 }
1143 });
1144 }
1145
1146 private void removeFromDeviceAnnouncement(Integer id) {
1147 HashSet<Integer> temp = new HashSet<>(getOwnDeviceIds());
1148 if (temp.remove(id)) {
1149 Log.d(Config.LOGTAG,account.getJid().asBareJid()+" remove own device id "+id+" from announcement. devices left:"+temp);
1150 publishOwnDeviceId(temp);
1151 }
1152 }
1153
1154 public Set<SignalProtocolAddress> findDevicesWithoutSession(final Conversation conversation) {
1155 Set<SignalProtocolAddress> addresses = new HashSet<>();
1156 for (Jid jid : getCryptoTargets(conversation)) {
1157 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
1158 final Set<Integer> ids = deviceIds.get(jid);
1159 if (ids != null && !ids.isEmpty()) {
1160 for (Integer foreignId : ids) {
1161 SignalProtocolAddress address = new SignalProtocolAddress(jid.toString(), foreignId);
1162 if (sessions.get(address) == null) {
1163 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1164 if (identityKey != null) {
1165 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1166 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1167 sessions.put(address, session);
1168 } else {
1169 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + jid + ":" + foreignId);
1170 if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1171 addresses.add(address);
1172 } else {
1173 Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1174 }
1175 }
1176 }
1177 }
1178 } else {
1179 mXmppConnectionService.keyStatusUpdated(FetchStatus.ERROR);
1180 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
1181 }
1182 }
1183 Set<Integer> ownIds = this.deviceIds.get(account.getJid().asBareJid());
1184 for (Integer ownId : (ownIds != null ? ownIds : new HashSet<Integer>())) {
1185 SignalProtocolAddress address = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownId);
1186 if (sessions.get(address) == null) {
1187 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1188 if (identityKey != null) {
1189 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1190 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1191 sessions.put(address, session);
1192 } else {
1193 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().asBareJid() + ":" + ownId);
1194 if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1195 addresses.add(address);
1196 } else {
1197 Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1198 }
1199 }
1200 }
1201 }
1202
1203 return addresses;
1204 }
1205
1206 public boolean createSessionsIfNeeded(final Conversation conversation) {
1207 final List<Jid> jidsWithEmptyDeviceList = getCryptoTargets(conversation);
1208 for (Iterator<Jid> iterator = jidsWithEmptyDeviceList.iterator(); iterator.hasNext(); ) {
1209 final Jid jid = iterator.next();
1210 if (!hasEmptyDeviceList(jid)) {
1211 iterator.remove();
1212 }
1213 }
1214 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": createSessionsIfNeeded() - jids with empty device list: " + jidsWithEmptyDeviceList);
1215 if (jidsWithEmptyDeviceList.size() > 0) {
1216 fetchDeviceIds(jidsWithEmptyDeviceList, () -> createSessionsIfNeededActual(conversation));
1217 return true;
1218 } else {
1219 return createSessionsIfNeededActual(conversation);
1220 }
1221 }
1222
1223 private boolean createSessionsIfNeededActual(final Conversation conversation) {
1224 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
1225 boolean newSessions = false;
1226 Set<SignalProtocolAddress> addresses = findDevicesWithoutSession(conversation);
1227 for (SignalProtocolAddress address : addresses) {
1228 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
1229 FetchStatus status = fetchStatusMap.get(address);
1230 if (status == null || status == FetchStatus.TIMEOUT) {
1231 fetchStatusMap.put(address, FetchStatus.PENDING);
1232 this.buildSessionFromPEP(address);
1233 newSessions = true;
1234 } else if (status == FetchStatus.PENDING) {
1235 newSessions = true;
1236 } else {
1237 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
1238 }
1239 }
1240
1241 return newSessions;
1242 }
1243
1244 public boolean trustedSessionVerified(final Conversation conversation) {
1245 final Set<XmppAxolotlSession> sessions = new HashSet<>();
1246 sessions.addAll(findSessionsForConversation(conversation));
1247 sessions.addAll(findOwnSessions());
1248 boolean verified = false;
1249 for (XmppAxolotlSession session : sessions) {
1250 if (session.getTrust().isTrustedAndActive()) {
1251 if (session.getTrust().getTrust() == FingerprintStatus.Trust.VERIFIED_X509) {
1252 verified = true;
1253 } else {
1254 return false;
1255 }
1256 }
1257 }
1258 return verified;
1259 }
1260
1261 public boolean hasPendingKeyFetches(List<Jid> jids) {
1262 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), 0);
1263 if (fetchStatusMap.getAll(ownAddress.getName()).containsValue(FetchStatus.PENDING)) {
1264 return true;
1265 }
1266 synchronized (this.fetchDeviceIdsMap) {
1267 for (Jid jid : jids) {
1268 SignalProtocolAddress foreignAddress = new SignalProtocolAddress(jid.asBareJid().toString(), 0);
1269 if (fetchStatusMap.getAll(foreignAddress.getName()).containsValue(FetchStatus.PENDING) || this.fetchDeviceIdsMap.containsKey(jid)) {
1270 return true;
1271 }
1272 }
1273 }
1274 return false;
1275 }
1276
1277 @Nullable
1278 private boolean buildHeader(XmppAxolotlMessage axolotlMessage, Conversation c) {
1279 Set<XmppAxolotlSession> remoteSessions = findSessionsForConversation(c);
1280 final boolean acceptEmpty = (c.getMode() == Conversation.MODE_MULTI && c.getMucOptions().getUserCount() == 0) || c.getContact().isSelf();
1281 Collection<XmppAxolotlSession> ownSessions = findOwnSessions();
1282 if (remoteSessions.isEmpty() && !acceptEmpty) {
1283 return false;
1284 }
1285 for (XmppAxolotlSession session : remoteSessions) {
1286 axolotlMessage.addDevice(session);
1287 }
1288 for (XmppAxolotlSession session : ownSessions) {
1289 axolotlMessage.addDevice(session);
1290 }
1291
1292 return true;
1293 }
1294
1295 //this is being used for private muc messages only
1296 private boolean buildHeader(XmppAxolotlMessage axolotlMessage, Jid jid) {
1297 if (jid == null) {
1298 return false;
1299 }
1300 HashSet<XmppAxolotlSession> sessions = new HashSet<>();
1301 sessions.addAll(this.sessions.getAll(getAddressForJid(jid).getName()).values());
1302 if (sessions.isEmpty()) {
1303 return false;
1304 }
1305 sessions.addAll(findOwnSessions());
1306 for(XmppAxolotlSession session : sessions) {
1307 axolotlMessage.addDevice(session);
1308 }
1309 return true;
1310 }
1311
1312 @Nullable
1313 public XmppAxolotlMessage encrypt(Message message) {
1314 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1315 final String content;
1316 if (message.hasFileOnRemoteHost()) {
1317 content = message.getFileParams().url.toString();
1318 } else {
1319 content = message.getBody();
1320 }
1321 try {
1322 axolotlMessage.encrypt(content);
1323 } catch (CryptoFailedException e) {
1324 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
1325 return null;
1326 }
1327
1328 final boolean success;
1329 if (message.getType() == Message.TYPE_PRIVATE) {
1330 success = buildHeader(axolotlMessage, message.getTrueCounterpart());
1331 } else {
1332 success = buildHeader(axolotlMessage, (Conversation) message.getConversation());
1333 }
1334 return success ? axolotlMessage : null;
1335 }
1336
1337 public void preparePayloadMessage(final Message message, final boolean delay) {
1338 executor.execute(new Runnable() {
1339 @Override
1340 public void run() {
1341 XmppAxolotlMessage axolotlMessage = encrypt(message);
1342 if (axolotlMessage == null) {
1343 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
1344 //mXmppConnectionService.updateConversationUi();
1345 } else {
1346 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
1347 messageCache.put(message.getUuid(), axolotlMessage);
1348 mXmppConnectionService.resendMessage(message, delay);
1349 }
1350 }
1351 });
1352 }
1353
1354 public void prepareKeyTransportMessage(final Conversation conversation, final OnMessageCreatedCallback onMessageCreatedCallback) {
1355 executor.execute(new Runnable() {
1356 @Override
1357 public void run() {
1358 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1359 if (buildHeader(axolotlMessage, conversation)) {
1360 onMessageCreatedCallback.run(axolotlMessage);
1361 } else {
1362 onMessageCreatedCallback.run(null);
1363 }
1364 }
1365 });
1366 }
1367
1368 public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
1369 XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
1370 if (axolotlMessage != null) {
1371 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
1372 messageCache.remove(message.getUuid());
1373 } else {
1374 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
1375 }
1376 return axolotlMessage;
1377 }
1378
1379 private XmppAxolotlSession recreateUncachedSession(SignalProtocolAddress address) {
1380 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1381 return (identityKey != null)
1382 ? new XmppAxolotlSession(account, axolotlStore, address, identityKey)
1383 : null;
1384 }
1385
1386 private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
1387 SignalProtocolAddress senderAddress = new SignalProtocolAddress(message.getFrom().toString(), message.getSenderDeviceId());
1388 return getReceivingSession(senderAddress);
1389
1390 }
1391
1392 private XmppAxolotlSession getReceivingSession(SignalProtocolAddress senderAddress) {
1393 XmppAxolotlSession session = sessions.get(senderAddress);
1394 if (session == null) {
1395 //Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
1396 session = recreateUncachedSession(senderAddress);
1397 if (session == null) {
1398 session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
1399 }
1400 }
1401 return session;
1402 }
1403
1404 public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message, boolean postponePreKeyMessageHandling) throws NotEncryptedForThisDeviceException, BrokenSessionException {
1405 XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
1406
1407 XmppAxolotlSession session = getReceivingSession(message);
1408 int ownDeviceId = getOwnDeviceId();
1409 try {
1410 plaintextMessage = message.decrypt(session, ownDeviceId);
1411 Integer preKeyId = session.getPreKeyIdAndReset();
1412 if (preKeyId != null) {
1413 postPreKeyMessageHandling(session, postponePreKeyMessageHandling);
1414 }
1415 } catch (NotEncryptedForThisDeviceException e) {
1416 if (account.getJid().asBareJid().equals(message.getFrom().asBareJid()) && message.getSenderDeviceId() == ownDeviceId) {
1417 Log.w(Config.LOGTAG, getLogprefix(account) + "Reflected omemo message received");
1418 } else {
1419 throw e;
1420 }
1421 } catch (final BrokenSessionException e) {
1422 throw e;
1423 } catch (CryptoFailedException e) {
1424 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message from " + message.getFrom(), e);
1425 }
1426
1427 if (session.isFresh() && plaintextMessage != null) {
1428 putFreshSession(session);
1429 }
1430
1431 return plaintextMessage;
1432 }
1433
1434 public void reportBrokenSessionException(BrokenSessionException e, boolean postpone) {
1435 Log.e(Config.LOGTAG,account.getJid().asBareJid()+": broken session with "+e.getSignalProtocolAddress().toString()+" detected", e);
1436 if (postpone) {
1437 postponedHealing.add(e.getSignalProtocolAddress());
1438 } else {
1439 notifyRequiresHealing(e.getSignalProtocolAddress());
1440 }
1441 }
1442
1443 private void notifyRequiresHealing(final SignalProtocolAddress signalProtocolAddress) {
1444 if (healingAttempts.add(signalProtocolAddress)) {
1445 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": attempt to heal "+signalProtocolAddress);
1446 buildSessionFromPEP(signalProtocolAddress, new OnSessionBuildFromPep() {
1447 @Override
1448 public void onSessionBuildSuccessful() {
1449 Log.d(Config.LOGTAG, "successfully build new session from pep after detecting broken session");
1450 completeSession(getReceivingSession(signalProtocolAddress));
1451 }
1452
1453 @Override
1454 public void onSessionBuildFailed() {
1455 Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": unable to build new session from pep after detecting broken session");
1456 }
1457 });
1458 } else {
1459 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": do not attempt to heal "+signalProtocolAddress+" again");
1460 }
1461 }
1462
1463 private void postPreKeyMessageHandling(final XmppAxolotlSession session, final boolean postpone) {
1464 if (postpone) {
1465 postponedSessions.add(session);
1466 } else {
1467 if (axolotlStore.flushPreKeys()) {
1468 publishBundlesIfNeeded(false, false);
1469 } else {
1470 Log.d(Config.LOGTAG,account.getJid().asBareJid()+": nothing to flush. Not republishing key");
1471 }
1472 completeSession(session);
1473 }
1474 }
1475
1476 public void processPostponed() {
1477 if (postponedSessions.size() > 0) {
1478 if (axolotlStore.flushPreKeys()) {
1479 publishBundlesIfNeeded(false, false);
1480 }
1481 }
1482 Iterator<XmppAxolotlSession> iterator = postponedSessions.iterator();
1483 while (iterator.hasNext()) {
1484 completeSession(iterator.next());
1485 iterator.remove();
1486 }
1487 Iterator<SignalProtocolAddress> postponedHealingAttemptsIterator = postponedHealing.iterator();
1488 while (postponedHealingAttemptsIterator.hasNext()) {
1489 notifyRequiresHealing(postponedHealingAttemptsIterator.next());
1490 postponedHealingAttemptsIterator.remove();
1491 }
1492 }
1493
1494 private void completeSession(XmppAxolotlSession session) {
1495 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1496 axolotlMessage.addDevice(session, true);
1497 try {
1498 Jid jid = Jid.of(session.getRemoteAddress().getName());
1499 MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateKeyTransportMessage(jid, axolotlMessage);
1500 mXmppConnectionService.sendMessagePacket(account, packet);
1501 } catch (IllegalArgumentException e) {
1502 throw new Error("Remote addresses are created from jid and should convert back to jid", e);
1503 }
1504 }
1505
1506
1507 public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message, final boolean postponePreKeyMessageHandling) {
1508 XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
1509
1510 XmppAxolotlSession session = getReceivingSession(message);
1511 try {
1512 keyTransportMessage = message.getParameters(session, getOwnDeviceId());
1513 Integer preKeyId = session.getPreKeyIdAndReset();
1514 if (preKeyId != null) {
1515 postPreKeyMessageHandling(session, postponePreKeyMessageHandling);
1516 }
1517 } catch (CryptoFailedException e) {
1518 Log.d(Config.LOGTAG, "could not decrypt keyTransport message " + e.getMessage());
1519 keyTransportMessage = null;
1520 }
1521
1522 if (session.isFresh() && keyTransportMessage != null) {
1523 putFreshSession(session);
1524 }
1525
1526 return keyTransportMessage;
1527 }
1528
1529 private void putFreshSession(XmppAxolotlSession session) {
1530 Log.d(Config.LOGTAG, "put fresh session");
1531 sessions.put(session);
1532 if (Config.X509_VERIFICATION) {
1533 if (session.getIdentityKey() != null) {
1534 verifySessionWithPEP(session);
1535 } else {
1536 Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": identity key was empty after reloading for x509 verification");
1537 }
1538 }
1539 }
1540}