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