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				Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
 578				if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
 579					Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for device list. pushing node configuration");
 580					mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
 581						@Override
 582						public void onPushSucceeded() {
 583							publishDeviceIdsAndRefineAccessModel(ids, false);
 584						}
 585
 586						@Override
 587						public void onPushFailed() {
 588							publishDeviceIdsAndRefineAccessModel(ids, false);
 589						}
 590					});
 591				} else {
 592					if (AxolotlService.this.changeAccessMode.compareAndSet(true, false)) {
 593						Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": done changing access mode");
 594						account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, false);
 595						mXmppConnectionService.databaseBackend.updateAccount(account);
 596					}
 597					if (packet.getType() == IqPacket.TYPE.ERROR) {
 598						pepBroken = true;
 599						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
 600					}
 601				}
 602			}
 603		});
 604	}
 605
 606	public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord,
 607	                                               final Set<PreKeyRecord> preKeyRecords,
 608	                                               final boolean announceAfter,
 609	                                               final boolean wipe) {
 610		try {
 611			IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
 612			PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
 613			X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
 614			Signature verifier = Signature.getInstance("sha256WithRSA");
 615			verifier.initSign(x509PrivateKey, mXmppConnectionService.getRNG());
 616			verifier.update(axolotlPublicKey.serialize());
 617			byte[] signature = verifier.sign();
 618			IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
 619			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device " + getOwnDeviceId());
 620			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
 621				@Override
 622				public void onIqPacketReceived(final Account account, IqPacket packet) {
 623					String node = AxolotlService.PEP_VERIFICATION + ":" + getOwnDeviceId();
 624					mXmppConnectionService.pushNodeConfiguration(account, node, PublishOptions.openAccess(), new XmppConnectionService.OnConfigurationPushed() {
 625						@Override
 626						public void onPushSucceeded() {
 627							Log.d(Config.LOGTAG, getLogprefix(account) + "configured verification node to be world readable");
 628							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
 629						}
 630
 631						@Override
 632						public void onPushFailed() {
 633							Log.d(Config.LOGTAG, getLogprefix(account) + "unable to set access model on verification node");
 634							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
 635						}
 636					});
 637				}
 638			});
 639		} catch (Exception e) {
 640			e.printStackTrace();
 641		}
 642	}
 643
 644	public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
 645		if (pepBroken) {
 646			Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
 647			return;
 648		}
 649
 650		if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
 651			this.changeAccessMode.set(account.isOptionSet(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE));
 652		} else {
 653			if (account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true)) {
 654				Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server doesn’t support publish-options. setting for later access mode change");
 655				mXmppConnectionService.databaseBackend.updateAccount(account);
 656			}
 657		}
 658		if (this.changeAccessMode.get()) {
 659			Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": server gained publish-options capabilities. changing access model");
 660		}
 661		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().asBareJid(), getOwnDeviceId());
 662		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
 663			@Override
 664			public void onIqPacketReceived(Account account, IqPacket packet) {
 665
 666				if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
 667					return; //ignore timeout. do nothing
 668				}
 669
 670				if (packet.getType() == IqPacket.TYPE.ERROR) {
 671					Element error = packet.findChild("error");
 672					if (error == null || !error.hasChild("item-not-found")) {
 673						pepBroken = true;
 674						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
 675						return;
 676					}
 677				}
 678
 679				PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
 680				Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
 681				boolean flush = false;
 682				if (bundle == null) {
 683					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
 684					bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
 685					flush = true;
 686				}
 687				if (keys == null) {
 688					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
 689				}
 690				try {
 691					boolean changed = false;
 692					// Validate IdentityKey
 693					IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
 694					if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
 695						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
 696						changed = true;
 697					}
 698
 699					// Validate signedPreKeyRecord + ID
 700					SignedPreKeyRecord signedPreKeyRecord;
 701					int numSignedPreKeys = axolotlStore.getSignedPreKeysCount();
 702					try {
 703						signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
 704						if (flush
 705								|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
 706								|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
 707							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
 708							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
 709							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
 710							changed = true;
 711						}
 712					} catch (InvalidKeyIdException e) {
 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
 719					// Validate PreKeys
 720					Set<PreKeyRecord> preKeyRecords = new HashSet<>();
 721					if (keys != null) {
 722						for (Integer id : keys.keySet()) {
 723							try {
 724								PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
 725								if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
 726									preKeyRecords.add(preKeyRecord);
 727								}
 728							} catch (InvalidKeyIdException ignored) {
 729							}
 730						}
 731					}
 732					int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
 733					if (newKeys > 0) {
 734						List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
 735								axolotlStore.getCurrentPreKeyId() + 1, newKeys);
 736						preKeyRecords.addAll(newRecords);
 737						for (PreKeyRecord record : newRecords) {
 738							axolotlStore.storePreKey(record.getId(), record);
 739						}
 740						changed = true;
 741						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
 742					}
 743
 744
 745					if (changed || changeAccessMode.get()) {
 746						if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
 747							mXmppConnectionService.publishDisplayName(account);
 748							publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
 749						} else {
 750							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
 751						}
 752					} else {
 753						Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
 754						if (wipe) {
 755							wipeOtherPepDevices();
 756						} else if (announce) {
 757							Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
 758							publishOwnDeviceIdIfNeeded();
 759						}
 760					}
 761				} catch (InvalidKeyException e) {
 762					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
 763				}
 764			}
 765		});
 766	}
 767
 768	private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord,
 769	                                 Set<PreKeyRecord> preKeyRecords,
 770	                                 final boolean announceAfter,
 771	                                 final boolean wipe) {
 772		publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, true);
 773	}
 774
 775	private void publishDeviceBundle(final SignedPreKeyRecord signedPreKeyRecord,
 776	                                 final Set<PreKeyRecord> preKeyRecords,
 777	                                 final boolean announceAfter,
 778	                                 final boolean wipe,
 779	                                 final boolean firstAttempt) {
 780		final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
 781		IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
 782				signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
 783				preKeyRecords, getOwnDeviceId(), publishOptions);
 784		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing...");
 785		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
 786			@Override
 787			public void onIqPacketReceived(final Account account, IqPacket packet) {
 788				Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
 789				if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
 790					Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": precondition wasn't met for bundle. pushing node configuration");
 791					final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId();
 792					mXmppConnectionService.pushNodeConfiguration(account, node, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
 793						@Override
 794						public void onPushSucceeded() {
 795							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
 796						}
 797
 798						@Override
 799						public void onPushFailed() {
 800							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
 801						}
 802					});
 803				} else if (packet.getType() == IqPacket.TYPE.RESULT) {
 804					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
 805					if (wipe) {
 806						wipeOtherPepDevices();
 807					} else if (announceAfter) {
 808						Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
 809						publishOwnDeviceIdIfNeeded();
 810					}
 811				} else if (packet.getType() == IqPacket.TYPE.ERROR) {
 812					pepBroken = true;
 813					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
 814				}
 815			}
 816		});
 817	}
 818
 819	public enum AxolotlCapability {
 820		FULL,
 821		MISSING_PRESENCE,
 822		MISSING_KEYS,
 823		WRONG_CONFIGURATION,
 824		NO_MEMBERS
 825	}
 826
 827	public boolean isConversationAxolotlCapable(Conversation conversation) {
 828		return conversation.isSingleOrPrivateAndNonAnonymous();
 829	}
 830
 831	public Pair<AxolotlCapability, Jid> isConversationAxolotlCapableDetailed(Conversation conversation) {
 832		if (conversation.isSingleOrPrivateAndNonAnonymous()) {
 833			final List<Jid> jids = getCryptoTargets(conversation);
 834			for (Jid jid : jids) {
 835				if (!hasAny(jid) && (!deviceIds.containsKey(jid) || deviceIds.get(jid).isEmpty())) {
 836					if (conversation.getAccount().getRoster().getContact(jid).mutualPresenceSubscription()) {
 837						return new Pair<>(AxolotlCapability.MISSING_KEYS, jid);
 838					} else {
 839						return new Pair<>(AxolotlCapability.MISSING_PRESENCE, jid);
 840					}
 841				}
 842			}
 843			if (jids.size() > 0) {
 844				return new Pair<>(AxolotlCapability.FULL, null);
 845			} else {
 846				return new Pair<>(AxolotlCapability.NO_MEMBERS, null);
 847			}
 848		} else {
 849			return new Pair<>(AxolotlCapability.WRONG_CONFIGURATION, null);
 850		}
 851	}
 852
 853	public List<Jid> getCryptoTargets(Conversation conversation) {
 854		final List<Jid> jids;
 855		if (conversation.getMode() == Conversation.MODE_SINGLE) {
 856			jids = new ArrayList<>();
 857			jids.add(conversation.getJid().asBareJid());
 858		} else {
 859			jids = conversation.getMucOptions().getMembers();
 860		}
 861		return jids;
 862	}
 863
 864	public FingerprintStatus getFingerprintTrust(String fingerprint) {
 865		return axolotlStore.getFingerprintStatus(fingerprint);
 866	}
 867
 868	public X509Certificate getFingerprintCertificate(String fingerprint) {
 869		return axolotlStore.getFingerprintCertificate(fingerprint);
 870	}
 871
 872	public void setFingerprintTrust(String fingerprint, FingerprintStatus status) {
 873		axolotlStore.setFingerprintStatus(fingerprint, status);
 874	}
 875
 876	private void verifySessionWithPEP(final XmppAxolotlSession session) {
 877		Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
 878		final SignalProtocolAddress address = session.getRemoteAddress();
 879		final IdentityKey identityKey = session.getIdentityKey();
 880		try {
 881			IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.of(address.getName()), address.getDeviceId());
 882			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
 883				@Override
 884				public void onIqPacketReceived(Account account, IqPacket packet) {
 885					Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
 886					if (verification != null) {
 887						try {
 888							Signature verifier = Signature.getInstance("sha256WithRSA");
 889							verifier.initVerify(verification.first[0]);
 890							verifier.update(identityKey.serialize());
 891							if (verifier.verify(verification.second)) {
 892								try {
 893									mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
 894									String fingerprint = session.getFingerprint();
 895									Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
 896									setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
 897									axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
 898									fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
 899									Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
 900									try {
 901										final String cn = information.getString("subject_cn");
 902										final Jid jid = Jid.of(address.getName());
 903										Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
 904										account.getRoster().getContact(jid).setCommonName(cn);
 905									} catch (final IllegalArgumentException ignored) {
 906										//ignored
 907									}
 908									finishBuildingSessionsFromPEP(address);
 909									return;
 910								} catch (Exception e) {
 911									Log.d(Config.LOGTAG, "could not verify certificate");
 912								}
 913							}
 914						} catch (Exception e) {
 915							Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
 916						}
 917					} else {
 918						Log.d(Config.LOGTAG, "no verification found");
 919					}
 920					fetchStatusMap.put(address, FetchStatus.SUCCESS);
 921					finishBuildingSessionsFromPEP(address);
 922				}
 923			});
 924		} catch (IllegalArgumentException e) {
 925			fetchStatusMap.put(address, FetchStatus.SUCCESS);
 926			finishBuildingSessionsFromPEP(address);
 927		}
 928	}
 929
 930	private final Set<Integer> PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT = new HashSet<>();
 931
 932	private void finishBuildingSessionsFromPEP(final SignalProtocolAddress address) {
 933		SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), 0);
 934		Map<Integer, FetchStatus> own = fetchStatusMap.getAll(ownAddress.getName());
 935		Map<Integer, FetchStatus> remote = fetchStatusMap.getAll(address.getName());
 936		if (!own.containsValue(FetchStatus.PENDING) && !remote.containsValue(FetchStatus.PENDING)) {
 937			FetchStatus report = null;
 938			if (own.containsValue(FetchStatus.SUCCESS) || remote.containsValue(FetchStatus.SUCCESS)) {
 939				report = FetchStatus.SUCCESS;
 940			} else if (own.containsValue(FetchStatus.SUCCESS_VERIFIED) || remote.containsValue(FetchStatus.SUCCESS_VERIFIED)) {
 941				report = FetchStatus.SUCCESS_VERIFIED;
 942			} else if (own.containsValue(FetchStatus.SUCCESS_TRUSTED) || remote.containsValue(FetchStatus.SUCCESS_TRUSTED)) {
 943				report = FetchStatus.SUCCESS_TRUSTED;
 944			} else if (own.containsValue(FetchStatus.ERROR) || remote.containsValue(FetchStatus.ERROR)) {
 945				report = FetchStatus.ERROR;
 946			}
 947			mXmppConnectionService.keyStatusUpdated(report);
 948		}
 949		if (Config.REMOVE_BROKEN_DEVICES) {
 950			Set<Integer> ownDeviceIds = new HashSet<>(getOwnDeviceIds());
 951			boolean publish = false;
 952			for (Map.Entry<Integer, FetchStatus> entry : own.entrySet()) {
 953				int id = entry.getKey();
 954				if (entry.getValue() == FetchStatus.ERROR && PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT.add(id) && ownDeviceIds.remove(id)) {
 955					publish = true;
 956					Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": error fetching own device with id " + id + ". removing from announcement");
 957				}
 958			}
 959			if (publish) {
 960				publishOwnDeviceId(ownDeviceIds);
 961			}
 962		}
 963	}
 964
 965	public boolean hasEmptyDeviceList(Jid jid) {
 966		return !hasAny(jid) && (!deviceIds.containsKey(jid) || deviceIds.get(jid).isEmpty());
 967	}
 968
 969	public interface OnDeviceIdsFetched {
 970		void fetched(Jid jid, Set<Integer> deviceIds);
 971	}
 972
 973	public interface OnMultipleDeviceIdFetched {
 974		void fetched();
 975	}
 976
 977	public void fetchDeviceIds(final Jid jid) {
 978		fetchDeviceIds(jid, null);
 979	}
 980
 981	public void fetchDeviceIds(final Jid jid, OnDeviceIdsFetched callback) {
 982		synchronized (this.fetchDeviceIdsMap) {
 983			List<OnDeviceIdsFetched> callbacks = this.fetchDeviceIdsMap.get(jid);
 984			if (callbacks != null) {
 985				if (callback != null) {
 986					callbacks.add(callback);
 987				}
 988				Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid + " already running. adding callback");
 989			} else {
 990				callbacks = new ArrayList<>();
 991				if (callback != null) {
 992					callbacks.add(callback);
 993				}
 994				this.fetchDeviceIdsMap.put(jid, callbacks);
 995				Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": fetching device ids for " + jid);
 996				IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(jid);
 997				mXmppConnectionService.sendIqPacket(account, packet, (account, response) -> {
 998					synchronized (fetchDeviceIdsMap) {
 999						List<OnDeviceIdsFetched> callbacks1 = fetchDeviceIdsMap.remove(jid);
1000						if (response.getType() == IqPacket.TYPE.RESULT) {
1001							fetchDeviceListStatus.put(jid, true);
1002							Element item = mXmppConnectionService.getIqParser().getItem(response);
1003							Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
1004							registerDevices(jid, deviceIds);
1005							if (callbacks1 != null) {
1006								for (OnDeviceIdsFetched callback1 : callbacks1) {
1007									callback1.fetched(jid, deviceIds);
1008								}
1009							}
1010						} else {
1011							if (response.getType() == IqPacket.TYPE.TIMEOUT) {
1012								fetchDeviceListStatus.remove(jid);
1013							} else {
1014								fetchDeviceListStatus.put(jid, false);
1015							}
1016							Log.d(Config.LOGTAG, response.toString());
1017							if (callbacks1 != null) {
1018								for (OnDeviceIdsFetched callback1 : callbacks1) {
1019									callback1.fetched(jid, null);
1020								}
1021							}
1022						}
1023					}
1024				});
1025			}
1026		}
1027	}
1028
1029	private void fetchDeviceIds(List<Jid> jids, final OnMultipleDeviceIdFetched callback) {
1030		final ArrayList<Jid> unfinishedJids = new ArrayList<>(jids);
1031		synchronized (unfinishedJids) {
1032			for (Jid jid : unfinishedJids) {
1033				fetchDeviceIds(jid, (j, deviceIds) -> {
1034					synchronized (unfinishedJids) {
1035						unfinishedJids.remove(j);
1036						if (unfinishedJids.size() == 0 && callback != null) {
1037							callback.fetched();
1038						}
1039					}
1040				});
1041			}
1042		}
1043	}
1044
1045	private void buildSessionFromPEP(final SignalProtocolAddress address) {
1046		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new session for " + address.toString());
1047		if (address.equals(getOwnAxolotlAddress())) {
1048			throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
1049		}
1050
1051		try {
1052			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
1053					Jid.of(address.getName()), address.getDeviceId());
1054			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
1055			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
1056
1057				@Override
1058				public void onIqPacketReceived(Account account, IqPacket packet) {
1059					if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1060						fetchStatusMap.put(address, FetchStatus.TIMEOUT);
1061					} else if (packet.getType() == IqPacket.TYPE.RESULT) {
1062						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
1063						final IqParser parser = mXmppConnectionService.getIqParser();
1064						final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
1065						final PreKeyBundle bundle = parser.bundle(packet);
1066						if (preKeyBundleList.isEmpty() || bundle == null) {
1067							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
1068							fetchStatusMap.put(address, FetchStatus.ERROR);
1069							finishBuildingSessionsFromPEP(address);
1070							return;
1071						}
1072						Random random = new Random();
1073						final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
1074						if (preKey == null) {
1075							//should never happen
1076							fetchStatusMap.put(address, FetchStatus.ERROR);
1077							finishBuildingSessionsFromPEP(address);
1078							return;
1079						}
1080
1081						final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
1082								preKey.getPreKeyId(), preKey.getPreKey(),
1083								bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
1084								bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
1085
1086						try {
1087							SessionBuilder builder = new SessionBuilder(axolotlStore, address);
1088							builder.process(preKeyBundle);
1089							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
1090							sessions.put(address, session);
1091							if (Config.X509_VERIFICATION) {
1092								verifySessionWithPEP(session);
1093							} else {
1094								FingerprintStatus status = getFingerprintTrust(CryptoHelper.bytesToHex(bundle.getIdentityKey().getPublicKey().serialize()));
1095								FetchStatus fetchStatus;
1096								if (status != null && status.isVerified()) {
1097									fetchStatus = FetchStatus.SUCCESS_VERIFIED;
1098								} else if (status != null && status.isTrusted()) {
1099									fetchStatus = FetchStatus.SUCCESS_TRUSTED;
1100								} else {
1101									fetchStatus = FetchStatus.SUCCESS;
1102								}
1103								fetchStatusMap.put(address, fetchStatus);
1104								finishBuildingSessionsFromPEP(address);
1105							}
1106						} catch (UntrustedIdentityException | InvalidKeyException e) {
1107							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
1108									+ e.getClass().getName() + ", " + e.getMessage());
1109							fetchStatusMap.put(address, FetchStatus.ERROR);
1110							finishBuildingSessionsFromPEP(address);
1111						}
1112					} else {
1113						fetchStatusMap.put(address, FetchStatus.ERROR);
1114						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
1115						finishBuildingSessionsFromPEP(address);
1116					}
1117				}
1118			});
1119		} catch (IllegalArgumentException e) {
1120			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
1121		}
1122	}
1123
1124	public Set<SignalProtocolAddress> findDevicesWithoutSession(final Conversation conversation) {
1125		Set<SignalProtocolAddress> addresses = new HashSet<>();
1126		for (Jid jid : getCryptoTargets(conversation)) {
1127			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
1128			if (deviceIds.get(jid) != null) {
1129				for (Integer foreignId : this.deviceIds.get(jid)) {
1130					SignalProtocolAddress address = new SignalProtocolAddress(jid.toString(), foreignId);
1131					if (sessions.get(address) == null) {
1132						IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1133						if (identityKey != null) {
1134							Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1135							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1136							sessions.put(address, session);
1137						} else {
1138							Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + jid + ":" + foreignId);
1139							if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1140								addresses.add(address);
1141							} else {
1142								Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1143							}
1144						}
1145					}
1146				}
1147			} else {
1148				mXmppConnectionService.keyStatusUpdated(FetchStatus.ERROR);
1149				Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
1150			}
1151		}
1152		if (deviceIds.get(account.getJid().asBareJid()) != null) {
1153			for (Integer ownId : this.deviceIds.get(account.getJid().asBareJid())) {
1154				SignalProtocolAddress address = new SignalProtocolAddress(account.getJid().asBareJid().toString(), ownId);
1155				if (sessions.get(address) == null) {
1156					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1157					if (identityKey != null) {
1158						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1159						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1160						sessions.put(address, session);
1161					} else {
1162						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().asBareJid() + ":" + ownId);
1163						if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1164							addresses.add(address);
1165						} else {
1166							Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1167						}
1168					}
1169				}
1170			}
1171		}
1172
1173		return addresses;
1174	}
1175
1176	public boolean createSessionsIfNeeded(final Conversation conversation) {
1177		final List<Jid> jidsWithEmptyDeviceList = getCryptoTargets(conversation);
1178		for (Iterator<Jid> iterator = jidsWithEmptyDeviceList.iterator(); iterator.hasNext(); ) {
1179			final Jid jid = iterator.next();
1180			if (!hasEmptyDeviceList(jid)) {
1181				iterator.remove();
1182			}
1183		}
1184		Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": createSessionsIfNeeded() - jids with empty device list: " + jidsWithEmptyDeviceList);
1185		if (jidsWithEmptyDeviceList.size() > 0) {
1186			fetchDeviceIds(jidsWithEmptyDeviceList, new OnMultipleDeviceIdFetched() {
1187				@Override
1188				public void fetched() {
1189					createSessionsIfNeededActual(conversation);
1190				}
1191			});
1192			return true;
1193		} else {
1194			return createSessionsIfNeededActual(conversation);
1195		}
1196	}
1197
1198	private boolean createSessionsIfNeededActual(final Conversation conversation) {
1199		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
1200		boolean newSessions = false;
1201		Set<SignalProtocolAddress> addresses = findDevicesWithoutSession(conversation);
1202		for (SignalProtocolAddress address : addresses) {
1203			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
1204			FetchStatus status = fetchStatusMap.get(address);
1205			if (status == null || status == FetchStatus.TIMEOUT) {
1206				fetchStatusMap.put(address, FetchStatus.PENDING);
1207				this.buildSessionFromPEP(address);
1208				newSessions = true;
1209			} else if (status == FetchStatus.PENDING) {
1210				newSessions = true;
1211			} else {
1212				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
1213			}
1214		}
1215
1216		return newSessions;
1217	}
1218
1219	public boolean trustedSessionVerified(final Conversation conversation) {
1220		final Set<XmppAxolotlSession> sessions = new HashSet<>();
1221		sessions.addAll(findSessionsForConversation(conversation));
1222		sessions.addAll(findOwnSessions());
1223		boolean verified = false;
1224		for (XmppAxolotlSession session : sessions) {
1225			if (session.getTrust().isTrustedAndActive()) {
1226				if (session.getTrust().getTrust() == FingerprintStatus.Trust.VERIFIED_X509) {
1227					verified = true;
1228				} else {
1229					return false;
1230				}
1231			}
1232		}
1233		return verified;
1234	}
1235
1236	public boolean hasPendingKeyFetches(Account account, List<Jid> jids) {
1237		SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().asBareJid().toString(), 0);
1238		if (fetchStatusMap.getAll(ownAddress.getName()).containsValue(FetchStatus.PENDING)) {
1239			return true;
1240		}
1241		synchronized (this.fetchDeviceIdsMap) {
1242			for (Jid jid : jids) {
1243				SignalProtocolAddress foreignAddress = new SignalProtocolAddress(jid.asBareJid().toString(), 0);
1244				if (fetchStatusMap.getAll(foreignAddress.getName()).containsValue(FetchStatus.PENDING) || this.fetchDeviceIdsMap.containsKey(jid)) {
1245					return true;
1246				}
1247			}
1248		}
1249		return false;
1250	}
1251
1252	@Nullable
1253	private boolean buildHeader(XmppAxolotlMessage axolotlMessage, Conversation c) {
1254		Set<XmppAxolotlSession> remoteSessions = findSessionsForConversation(c);
1255		final boolean acceptEmpty = (c.getMode() == Conversation.MODE_MULTI && c.getMucOptions().getUserCount() == 0) || c.getContact().isSelf();
1256		Collection<XmppAxolotlSession> ownSessions = findOwnSessions();
1257		if (remoteSessions.isEmpty() && !acceptEmpty) {
1258			return false;
1259		}
1260		for (XmppAxolotlSession session : remoteSessions) {
1261			axolotlMessage.addDevice(session);
1262		}
1263		for (XmppAxolotlSession session : ownSessions) {
1264			axolotlMessage.addDevice(session);
1265		}
1266
1267		return true;
1268	}
1269
1270	//this is being used for private muc messages only
1271	private boolean buildHeader(XmppAxolotlMessage axolotlMessage, Jid jid) {
1272		if (jid == null) {
1273			return false;
1274		}
1275		HashSet<XmppAxolotlSession> sessions = new HashSet<>();
1276		sessions.addAll(this.sessions.getAll(getAddressForJid(jid).getName()).values());
1277		if (sessions.isEmpty()) {
1278			return false;
1279		}
1280		sessions.addAll(findOwnSessions());
1281		for(XmppAxolotlSession session : sessions) {
1282			axolotlMessage.addDevice(session);
1283		}
1284		return true;
1285	}
1286
1287	@Nullable
1288	public XmppAxolotlMessage encrypt(Message message) {
1289		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1290		final String content;
1291		if (message.hasFileOnRemoteHost()) {
1292			content = message.getFileParams().url.toString();
1293		} else {
1294			content = message.getBody();
1295		}
1296		try {
1297			axolotlMessage.encrypt(content);
1298		} catch (CryptoFailedException e) {
1299			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
1300			return null;
1301		}
1302
1303		final boolean success;
1304		if (message.getType() == Message.TYPE_PRIVATE) {
1305			success = buildHeader(axolotlMessage, message.getTrueCounterpart());
1306		} else {
1307			success = buildHeader(axolotlMessage, message.getConversation());
1308		}
1309		return success ? axolotlMessage : null;
1310	}
1311
1312	public void preparePayloadMessage(final Message message, final boolean delay) {
1313		executor.execute(new Runnable() {
1314			@Override
1315			public void run() {
1316				XmppAxolotlMessage axolotlMessage = encrypt(message);
1317				if (axolotlMessage == null) {
1318					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
1319					//mXmppConnectionService.updateConversationUi();
1320				} else {
1321					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
1322					messageCache.put(message.getUuid(), axolotlMessage);
1323					mXmppConnectionService.resendMessage(message, delay);
1324				}
1325			}
1326		});
1327	}
1328
1329	public void prepareKeyTransportMessage(final Conversation conversation, final OnMessageCreatedCallback onMessageCreatedCallback) {
1330		executor.execute(new Runnable() {
1331			@Override
1332			public void run() {
1333				final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1334				if (buildHeader(axolotlMessage, conversation)) {
1335					onMessageCreatedCallback.run(axolotlMessage);
1336				} else {
1337					onMessageCreatedCallback.run(null);
1338				}
1339			}
1340		});
1341	}
1342
1343	public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
1344		XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
1345		if (axolotlMessage != null) {
1346			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
1347			messageCache.remove(message.getUuid());
1348		} else {
1349			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
1350		}
1351		return axolotlMessage;
1352	}
1353
1354	private XmppAxolotlSession recreateUncachedSession(SignalProtocolAddress address) {
1355		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1356		return (identityKey != null)
1357				? new XmppAxolotlSession(account, axolotlStore, address, identityKey)
1358				: null;
1359	}
1360
1361	private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
1362		SignalProtocolAddress senderAddress = new SignalProtocolAddress(message.getFrom().toString(),
1363				message.getSenderDeviceId());
1364		XmppAxolotlSession session = sessions.get(senderAddress);
1365		if (session == null) {
1366			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
1367			session = recreateUncachedSession(senderAddress);
1368			if (session == null) {
1369				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
1370			}
1371		}
1372		return session;
1373	}
1374
1375	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message, boolean postponePreKeyMessageHandling) {
1376		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
1377
1378		XmppAxolotlSession session = getReceivingSession(message);
1379		try {
1380			plaintextMessage = message.decrypt(session, getOwnDeviceId());
1381			Integer preKeyId = session.getPreKeyIdAndReset();
1382			if (preKeyId != null) {
1383				postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling);
1384			}
1385		} catch (CryptoFailedException e) {
1386			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message from " + message.getFrom() + ": " + e.getMessage());
1387		}
1388
1389		if (session.isFresh() && plaintextMessage != null) {
1390			putFreshSession(session);
1391		}
1392
1393		return plaintextMessage;
1394	}
1395
1396	private void postPreKeyMessageHandling(final XmppAxolotlSession session, int preKeyId, final boolean postpone) {
1397		if (postpone) {
1398			postponedSessions.add(session);
1399		} else {
1400			//TODO: do not republish if we already removed this preKeyId
1401			publishBundlesIfNeeded(false, false);
1402			completeSession(session);
1403		}
1404	}
1405
1406	public void processPostponed() {
1407		if (postponedSessions.size() > 0) {
1408			publishBundlesIfNeeded(false, false);
1409		}
1410		Iterator<XmppAxolotlSession> iterator = postponedSessions.iterator();
1411		while (iterator.hasNext()) {
1412			completeSession(iterator.next());
1413			iterator.remove();
1414		}
1415	}
1416
1417	private void completeSession(XmppAxolotlSession session) {
1418		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().asBareJid(), getOwnDeviceId());
1419		axolotlMessage.addDevice(session);
1420		try {
1421			Jid jid = Jid.of(session.getRemoteAddress().getName());
1422			MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateKeyTransportMessage(jid, axolotlMessage);
1423			mXmppConnectionService.sendMessagePacket(account, packet);
1424		} catch (IllegalArgumentException e) {
1425			throw new Error("Remote addresses are created from jid and should convert back to jid", e);
1426		}
1427	}
1428
1429
1430	public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message, final boolean postponePreKeyMessageHandling) {
1431		XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
1432
1433		XmppAxolotlSession session = getReceivingSession(message);
1434		try {
1435			keyTransportMessage = message.getParameters(session, getOwnDeviceId());
1436			Integer preKeyId = session.getPreKeyIdAndReset();
1437			if (preKeyId != null) {
1438				postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling);
1439			}
1440		} catch (CryptoFailedException e) {
1441			Log.d(Config.LOGTAG, "could not decrypt keyTransport message " + e.getMessage());
1442			keyTransportMessage = null;
1443		}
1444
1445		if (session.isFresh() && keyTransportMessage != null) {
1446			putFreshSession(session);
1447		}
1448
1449		return keyTransportMessage;
1450	}
1451
1452	private void putFreshSession(XmppAxolotlSession session) {
1453		Log.d(Config.LOGTAG, "put fresh session");
1454		sessions.put(session);
1455		if (Config.X509_VERIFICATION) {
1456			if (session.getIdentityKey() != null) {
1457				verifySessionWithPEP(session);
1458			} else {
1459				Log.e(Config.LOGTAG, account.getJid().asBareJid() + ": identity key was empty after reloading for x509 verification");
1460			}
1461		}
1462	}
1463}