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