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