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