AxolotlService.java

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