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