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