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