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