AxolotlService.java

  1package eu.siacs.conversations.crypto.axolotl;
  2
  3import android.security.KeyChain;
  4import android.support.annotation.NonNull;
  5import android.support.annotation.Nullable;
  6import android.util.Log;
  7import android.util.Pair;
  8
  9import org.bouncycastle.jce.provider.BouncyCastleProvider;
 10import org.whispersystems.libaxolotl.AxolotlAddress;
 11import org.whispersystems.libaxolotl.IdentityKey;
 12import org.whispersystems.libaxolotl.IdentityKeyPair;
 13import org.whispersystems.libaxolotl.InvalidKeyException;
 14import org.whispersystems.libaxolotl.InvalidKeyIdException;
 15import org.whispersystems.libaxolotl.SessionBuilder;
 16import org.whispersystems.libaxolotl.UntrustedIdentityException;
 17import org.whispersystems.libaxolotl.ecc.ECPublicKey;
 18import org.whispersystems.libaxolotl.state.PreKeyBundle;
 19import org.whispersystems.libaxolotl.state.PreKeyRecord;
 20import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
 21import org.whispersystems.libaxolotl.util.KeyHelper;
 22
 23import java.security.PrivateKey;
 24import java.security.Security;
 25import java.security.Signature;
 26import java.security.cert.X509Certificate;
 27import java.util.Arrays;
 28import java.util.HashMap;
 29import java.util.HashSet;
 30import java.util.List;
 31import java.util.Map;
 32import java.util.Random;
 33import java.util.Set;
 34
 35import eu.siacs.conversations.Config;
 36import eu.siacs.conversations.entities.Account;
 37import eu.siacs.conversations.entities.Contact;
 38import eu.siacs.conversations.entities.Conversation;
 39import eu.siacs.conversations.entities.Message;
 40import eu.siacs.conversations.parser.IqParser;
 41import eu.siacs.conversations.services.XmppConnectionService;
 42import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
 43import eu.siacs.conversations.xml.Element;
 44import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 45import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 46import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 47import eu.siacs.conversations.xmpp.jid.Jid;
 48import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 49
 50public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 51
 52	public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
 53	public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
 54	public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";
 55	public static final String PEP_VERIFICATION = PEP_PREFIX + ".verification";
 56
 57	public static final String LOGPREFIX = "AxolotlService";
 58
 59	public static final int NUM_KEYS_TO_PUBLISH = 100;
 60	public static final int publishTriesThreshold = 3;
 61
 62	private final Account account;
 63	private final XmppConnectionService mXmppConnectionService;
 64	private final SQLiteAxolotlStore axolotlStore;
 65	private final SessionMap sessions;
 66	private final Map<Jid, Set<Integer>> deviceIds;
 67	private final Map<String, XmppAxolotlMessage> messageCache;
 68	private final FetchStatusMap fetchStatusMap;
 69	private final SerialSingleThreadExecutor executor;
 70	private int numPublishTriesOnEmptyPep = 0;
 71	private boolean pepBroken = false;
 72
 73	@Override
 74	public void onAdvancedStreamFeaturesAvailable(Account account) {
 75		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().pep()) {
 76			publishBundlesIfNeeded(true, false);
 77		} else {
 78			Log.d(Config.LOGTAG,account.getJid().toBareJid()+": skipping OMEMO initialization");
 79		}
 80	}
 81
 82	private static class AxolotlAddressMap<T> {
 83		protected Map<String, Map<Integer, T>> map;
 84		protected final Object MAP_LOCK = new Object();
 85
 86		public AxolotlAddressMap() {
 87			this.map = new HashMap<>();
 88		}
 89
 90		public void put(AxolotlAddress address, T value) {
 91			synchronized (MAP_LOCK) {
 92				Map<Integer, T> devices = map.get(address.getName());
 93				if (devices == null) {
 94					devices = new HashMap<>();
 95					map.put(address.getName(), devices);
 96				}
 97				devices.put(address.getDeviceId(), value);
 98			}
 99		}
100
101		public T get(AxolotlAddress address) {
102			synchronized (MAP_LOCK) {
103				Map<Integer, T> devices = map.get(address.getName());
104				if (devices == null) {
105					return null;
106				}
107				return devices.get(address.getDeviceId());
108			}
109		}
110
111		public Map<Integer, T> getAll(AxolotlAddress address) {
112			synchronized (MAP_LOCK) {
113				Map<Integer, T> devices = map.get(address.getName());
114				if (devices == null) {
115					return new HashMap<>();
116				}
117				return devices;
118			}
119		}
120
121		public boolean hasAny(AxolotlAddress address) {
122			synchronized (MAP_LOCK) {
123				Map<Integer, T> devices = map.get(address.getName());
124				return devices != null && !devices.isEmpty();
125			}
126		}
127
128		public void clear() {
129			map.clear();
130		}
131
132	}
133
134	private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
135		private final XmppConnectionService xmppConnectionService;
136		private final Account account;
137
138		public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
139			super();
140			this.xmppConnectionService = service;
141			this.account = account;
142			this.fillMap(store);
143		}
144
145		private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
146			for (Integer deviceId : deviceIds) {
147				AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
148				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString());
149				IdentityKey identityKey = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey();
150				this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, identityKey));
151			}
152		}
153
154		private void fillMap(SQLiteAxolotlStore store) {
155			List<Integer> deviceIds = store.getSubDeviceSessions(account.getJid().toBareJid().toString());
156			putDevicesForJid(account.getJid().toBareJid().toString(), deviceIds, store);
157			for (Contact contact : account.getRoster().getContacts()) {
158				Jid bareJid = contact.getJid().toBareJid();
159				String address = bareJid.toString();
160				deviceIds = store.getSubDeviceSessions(address);
161				putDevicesForJid(address, deviceIds, store);
162			}
163
164		}
165
166		@Override
167		public void put(AxolotlAddress address, XmppAxolotlSession value) {
168			super.put(address, value);
169			value.setNotFresh();
170			xmppConnectionService.syncRosterToDisk(account);
171		}
172
173		public void put(XmppAxolotlSession session) {
174			this.put(session.getRemoteAddress(), session);
175		}
176	}
177
178	public enum FetchStatus {
179		PENDING,
180		SUCCESS,
181		SUCCESS_VERIFIED,
182		TIMEOUT,
183		ERROR
184	}
185
186	private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
187
188	}
189
190	public static String getLogprefix(Account account) {
191		return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
192	}
193
194	public AxolotlService(Account account, XmppConnectionService connectionService) {
195		if (Security.getProvider("BC") == null) {
196			Security.addProvider(new BouncyCastleProvider());
197		}
198		this.mXmppConnectionService = connectionService;
199		this.account = account;
200		this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
201		this.deviceIds = new HashMap<>();
202		this.messageCache = new HashMap<>();
203		this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
204		this.fetchStatusMap = new FetchStatusMap();
205		this.executor = new SerialSingleThreadExecutor();
206	}
207
208	public String getOwnFingerprint() {
209		return axolotlStore.getIdentityKeyPair().getPublicKey().getFingerprint().replaceAll("\\s", "");
210	}
211
212	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust) {
213		return axolotlStore.getContactKeysWithTrust(account.getJid().toBareJid().toString(), trust);
214	}
215
216	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust, Contact contact) {
217		return axolotlStore.getContactKeysWithTrust(contact.getJid().toBareJid().toString(), trust);
218	}
219
220	public long getNumTrustedKeys(Contact contact) {
221		return axolotlStore.getContactNumTrustedKeys(contact.getJid().toBareJid().toString());
222	}
223
224	private AxolotlAddress getAddressForJid(Jid jid) {
225		return new AxolotlAddress(jid.toString(), 0);
226	}
227
228	private Set<XmppAxolotlSession> findOwnSessions() {
229		AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
230		return new HashSet<>(this.sessions.getAll(ownAddress).values());
231	}
232
233	private Set<XmppAxolotlSession> findSessionsforContact(Contact contact) {
234		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
235		return new HashSet<>(this.sessions.getAll(contactAddress).values());
236	}
237
238	public Set<String> getFingerprintsForOwnSessions() {
239		Set<String> fingerprints = new HashSet<>();
240		for (XmppAxolotlSession session : findOwnSessions()) {
241			fingerprints.add(session.getFingerprint());
242		}
243		return fingerprints;
244	}
245
246	public Set<String> getFingerprintsForContact(final Contact contact) {
247		Set<String> fingerprints = new HashSet<>();
248		for (XmppAxolotlSession session : findSessionsforContact(contact)) {
249			fingerprints.add(session.getFingerprint());
250		}
251		return fingerprints;
252	}
253
254	private boolean hasAny(Contact contact) {
255		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
256		return sessions.hasAny(contactAddress);
257	}
258
259	public boolean isPepBroken() {
260		return this.pepBroken;
261	}
262
263	public void regenerateKeys(boolean wipeOther) {
264		axolotlStore.regenerate();
265		sessions.clear();
266		fetchStatusMap.clear();
267		publishBundlesIfNeeded(true, wipeOther);
268	}
269
270	public int getOwnDeviceId() {
271		return axolotlStore.getLocalRegistrationId();
272	}
273
274	public Set<Integer> getOwnDeviceIds() {
275		return this.deviceIds.get(account.getJid().toBareJid());
276	}
277
278	private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds,
279	                                final XmppAxolotlSession.Trust from,
280	                                final XmppAxolotlSession.Trust to) {
281		for (Integer deviceId : deviceIds) {
282			AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
283			XmppAxolotlSession session = sessions.get(address);
284			if (session != null && session.getFingerprint() != null
285					&& session.getTrust() == from) {
286				session.setTrust(to);
287			}
288		}
289	}
290
291	public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
292		if (jid.toBareJid().equals(account.getJid().toBareJid())) {
293			if (!deviceIds.isEmpty()) {
294				Log.d(Config.LOGTAG, getLogprefix(account) + "Received non-empty own device list. Resetting publish attemps and pepBroken status.");
295				pepBroken = false;
296				numPublishTriesOnEmptyPep = 0;
297			}
298			if (deviceIds.contains(getOwnDeviceId())) {
299				deviceIds.remove(getOwnDeviceId());
300			} else {
301				publishOwnDeviceId(deviceIds);
302			}
303			for (Integer deviceId : deviceIds) {
304				AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
305				if (sessions.get(ownDeviceAddress) == null) {
306					buildSessionFromPEP(ownDeviceAddress);
307				}
308			}
309		}
310		Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toString()));
311		expiredDevices.removeAll(deviceIds);
312		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED,
313				XmppAxolotlSession.Trust.INACTIVE_TRUSTED);
314		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED_X509,
315				XmppAxolotlSession.Trust.INACTIVE_TRUSTED_X509);
316		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNDECIDED,
317				XmppAxolotlSession.Trust.INACTIVE_UNDECIDED);
318		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNTRUSTED,
319				XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED);
320		Set<Integer> newDevices = new HashSet<>(deviceIds);
321		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED,
322				XmppAxolotlSession.Trust.TRUSTED);
323		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED_X509,
324				XmppAxolotlSession.Trust.TRUSTED_X509);
325		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNDECIDED,
326				XmppAxolotlSession.Trust.UNDECIDED);
327		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
328				XmppAxolotlSession.Trust.UNTRUSTED);
329		this.deviceIds.put(jid, deviceIds);
330		mXmppConnectionService.keyStatusUpdated(null);
331	}
332
333	public void wipeOtherPepDevices() {
334		if (pepBroken) {
335			Log.d(Config.LOGTAG, getLogprefix(account) + "wipeOtherPepDevices called, but PEP is broken. Ignoring... ");
336			return;
337		}
338		Set<Integer> deviceIds = new HashSet<>();
339		deviceIds.add(getOwnDeviceId());
340		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
341		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
342		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
343			@Override
344			public void onIqPacketReceived(Account account, IqPacket packet) {
345				// TODO: implement this!
346			}
347		});
348	}
349
350	public void purgeKey(final String fingerprint) {
351		axolotlStore.setFingerprintTrust(fingerprint.replaceAll("\\s", ""), XmppAxolotlSession.Trust.COMPROMISED);
352	}
353
354	public void publishOwnDeviceIdIfNeeded() {
355		if (pepBroken) {
356			Log.d(Config.LOGTAG, getLogprefix(account) + "publishOwnDeviceIdIfNeeded called, but PEP is broken. Ignoring... ");
357			return;
358		}
359		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
360		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
361			@Override
362			public void onIqPacketReceived(Account account, IqPacket packet) {
363				if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
364					Log.d(Config.LOGTAG, getLogprefix(account) + "Timeout received while retrieving own Device Ids.");
365				} else {
366					Element item = mXmppConnectionService.getIqParser().getItem(packet);
367					Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
368					if (!deviceIds.contains(getOwnDeviceId())) {
369						publishOwnDeviceId(deviceIds);
370					}
371				}
372			}
373		});
374	}
375
376	public void publishOwnDeviceId(Set<Integer> deviceIds) {
377		Set<Integer> deviceIdsCopy = new HashSet<>(deviceIds);
378		if (!deviceIdsCopy.contains(getOwnDeviceId())) {
379			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist.");
380			if (deviceIdsCopy.isEmpty()) {
381				if (numPublishTriesOnEmptyPep >= publishTriesThreshold) {
382					Log.w(Config.LOGTAG, getLogprefix(account) + "Own device publish attempt threshold exceeded, aborting...");
383					pepBroken = true;
384					return;
385				} else {
386					numPublishTriesOnEmptyPep++;
387					Log.w(Config.LOGTAG, getLogprefix(account) + "Own device list empty, attempting to publish (try " + numPublishTriesOnEmptyPep + ")");
388				}
389			} else {
390				numPublishTriesOnEmptyPep = 0;
391			}
392			deviceIdsCopy.add(getOwnDeviceId());
393			IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIdsCopy);
394			mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
395				@Override
396				public void onIqPacketReceived(Account account, IqPacket packet) {
397					if (packet.getType() != IqPacket.TYPE.RESULT) {
398						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
399					}
400				}
401			});
402		}
403	}
404
405	public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord,
406												   final Set<PreKeyRecord> preKeyRecords,
407												   final boolean announceAfter,
408												   final boolean wipe) {
409		try {
410			IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
411			PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
412			X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
413			Signature verifier = Signature.getInstance("sha256WithRSA");
414			verifier.initSign(x509PrivateKey,mXmppConnectionService.getRNG());
415			verifier.update(axolotlPublicKey.serialize());
416			byte[] signature = verifier.sign();
417			IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
418			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device "+getOwnDeviceId());
419			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
420				@Override
421				public void onIqPacketReceived(Account account, IqPacket packet) {
422					publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
423				}
424			});
425		} catch (Exception  e) {
426			e.printStackTrace();
427		}
428	}
429
430	public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
431		if (pepBroken) {
432			Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
433			return;
434		}
435		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
436		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
437			@Override
438			public void onIqPacketReceived(Account account, IqPacket packet) {
439
440				if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
441					return; //ignore timeout. do nothing
442				}
443
444				if (packet.getType() == IqPacket.TYPE.ERROR) {
445					Element error = packet.findChild("error");
446					if (error == null || !error.hasChild("item-not-found")) {
447						pepBroken = true;
448						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
449						return;
450					}
451				}
452
453				PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
454				Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
455				boolean flush = false;
456				if (bundle == null) {
457					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
458					bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
459					flush = true;
460				}
461				if (keys == null) {
462					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
463				}
464				try {
465					boolean changed = false;
466					// Validate IdentityKey
467					IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
468					if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
469						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
470						changed = true;
471					}
472
473					// Validate signedPreKeyRecord + ID
474					SignedPreKeyRecord signedPreKeyRecord;
475					int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
476					try {
477						signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
478						if (flush
479								|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
480								|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
481							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
482							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
483							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
484							changed = true;
485						}
486					} catch (InvalidKeyIdException e) {
487						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
488						signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
489						axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
490						changed = true;
491					}
492
493					// Validate PreKeys
494					Set<PreKeyRecord> preKeyRecords = new HashSet<>();
495					if (keys != null) {
496						for (Integer id : keys.keySet()) {
497							try {
498								PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
499								if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
500									preKeyRecords.add(preKeyRecord);
501								}
502							} catch (InvalidKeyIdException ignored) {
503							}
504						}
505					}
506					int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
507					if (newKeys > 0) {
508						List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
509								axolotlStore.getCurrentPreKeyId() + 1, newKeys);
510						preKeyRecords.addAll(newRecords);
511						for (PreKeyRecord record : newRecords) {
512							axolotlStore.storePreKey(record.getId(), record);
513						}
514						changed = true;
515						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
516					}
517
518
519					if (changed) {
520						if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
521							mXmppConnectionService.publishDisplayName(account);
522							publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
523						} else {
524							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
525						}
526					} else {
527						Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
528						if (wipe) {
529							wipeOtherPepDevices();
530						} else if (announce) {
531							Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
532							publishOwnDeviceIdIfNeeded();
533						}
534					}
535				} catch (InvalidKeyException e) {
536					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
537				}
538			}
539		});
540	}
541
542	private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord,
543									 Set<PreKeyRecord> preKeyRecords,
544									 final boolean announceAfter,
545									 final boolean wipe) {
546		IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
547				signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
548				preKeyRecords, getOwnDeviceId());
549		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
550		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
551			@Override
552			public void onIqPacketReceived(Account account, IqPacket packet) {
553				if (packet.getType() == IqPacket.TYPE.RESULT) {
554					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
555					if (wipe) {
556						wipeOtherPepDevices();
557					} else if (announceAfter) {
558						Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
559						publishOwnDeviceIdIfNeeded();
560					}
561				} else {
562					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
563				}
564			}
565		});
566	}
567
568	public boolean isContactAxolotlCapable(Contact contact) {
569		Jid jid = contact.getJid().toBareJid();
570		return hasAny(contact) ||
571				(deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
572	}
573
574	public XmppAxolotlSession.Trust getFingerprintTrust(String fingerprint) {
575		return axolotlStore.getFingerprintTrust(fingerprint);
576	}
577
578	public void setFingerprintTrust(String fingerprint, XmppAxolotlSession.Trust trust) {
579		axolotlStore.setFingerprintTrust(fingerprint, trust);
580	}
581
582	private void verifySessionWithPEP(final XmppAxolotlSession session) {
583		Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
584		final AxolotlAddress address = session.getRemoteAddress();
585		final IdentityKey identityKey = session.getIdentityKey();
586		try {
587			IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
588			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
589				@Override
590				public void onIqPacketReceived(Account account, IqPacket packet) {
591					Pair<X509Certificate[],byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
592					if (verification != null) {
593						try {
594							Signature verifier = Signature.getInstance("sha256WithRSA");
595							verifier.initVerify(verification.first[0]);
596							verifier.update(identityKey.serialize());
597							if (verifier.verify(verification.second)) {
598								try {
599									mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
600									Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: "+session.getFingerprint());
601									setFingerprintTrust(session.getFingerprint(), XmppAxolotlSession.Trust.TRUSTED_X509);
602									fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
603									finishBuildingSessionsFromPEP(address);
604									return;
605								} catch (Exception e) {
606									Log.d(Config.LOGTAG,"could not verify certificate");
607								}
608							}
609						} catch (Exception e) {
610							Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
611						}
612					} else {
613						Log.d(Config.LOGTAG,"no verification found");
614					}
615					fetchStatusMap.put(address, FetchStatus.SUCCESS);
616					finishBuildingSessionsFromPEP(address);
617				}
618			});
619		} catch (InvalidJidException e) {
620			fetchStatusMap.put(address, FetchStatus.SUCCESS);
621			finishBuildingSessionsFromPEP(address);
622		}
623	}
624
625	private void finishBuildingSessionsFromPEP(final AxolotlAddress address) {
626		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
627		if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
628				&& !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
629			FetchStatus report = null;
630			if (fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.SUCCESS_VERIFIED)
631					| fetchStatusMap.getAll(address).containsValue(FetchStatus.SUCCESS_VERIFIED)) {
632				report = FetchStatus.SUCCESS_VERIFIED;
633			} else if (fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.ERROR)
634					|| fetchStatusMap.getAll(address).containsValue(FetchStatus.ERROR)) {
635				report = FetchStatus.ERROR;
636			}
637			mXmppConnectionService.keyStatusUpdated(report);
638		}
639	}
640
641	private void buildSessionFromPEP(final AxolotlAddress address) {
642		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.toString());
643		if (address.getDeviceId() == getOwnDeviceId()) {
644			throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
645		}
646
647		try {
648			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
649					Jid.fromString(address.getName()), address.getDeviceId());
650			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
651			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
652
653				@Override
654				public void onIqPacketReceived(Account account, IqPacket packet) {
655					if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
656						fetchStatusMap.put(address, FetchStatus.TIMEOUT);
657					} else if (packet.getType() == IqPacket.TYPE.RESULT) {
658						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
659						final IqParser parser = mXmppConnectionService.getIqParser();
660						final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
661						final PreKeyBundle bundle = parser.bundle(packet);
662						if (preKeyBundleList.isEmpty() || bundle == null) {
663							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
664							fetchStatusMap.put(address, FetchStatus.ERROR);
665							finishBuildingSessionsFromPEP(address);
666							return;
667						}
668						Random random = new Random();
669						final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
670						if (preKey == null) {
671							//should never happen
672							fetchStatusMap.put(address, FetchStatus.ERROR);
673							finishBuildingSessionsFromPEP(address);
674							return;
675						}
676
677						final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
678								preKey.getPreKeyId(), preKey.getPreKey(),
679								bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
680								bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
681
682						try {
683							SessionBuilder builder = new SessionBuilder(axolotlStore, address);
684							builder.process(preKeyBundle);
685							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
686							sessions.put(address, session);
687							if (Config.X509_VERIFICATION) {
688								verifySessionWithPEP(session);
689							} else {
690								fetchStatusMap.put(address, FetchStatus.SUCCESS);
691								finishBuildingSessionsFromPEP(address);
692							}
693						} catch (UntrustedIdentityException | InvalidKeyException e) {
694							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
695									+ e.getClass().getName() + ", " + e.getMessage());
696							fetchStatusMap.put(address, FetchStatus.ERROR);
697							finishBuildingSessionsFromPEP(address);
698						}
699					} else {
700						fetchStatusMap.put(address, FetchStatus.ERROR);
701						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
702						finishBuildingSessionsFromPEP(address);
703					}
704				}
705			});
706		} catch (InvalidJidException e) {
707			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
708		}
709	}
710
711	public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
712		return findDevicesWithoutSession(conversation.getContact().getJid().toBareJid());
713	}
714
715	public Set<AxolotlAddress> findDevicesWithoutSession(final Jid contactJid) {
716		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + contactJid);
717		Set<AxolotlAddress> addresses = new HashSet<>();
718		if (deviceIds.get(contactJid) != null) {
719			for (Integer foreignId : this.deviceIds.get(contactJid)) {
720				AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
721				if (sessions.get(address) == null) {
722					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
723					if (identityKey != null) {
724						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
725						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
726						sessions.put(address, session);
727					} else {
728						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
729						if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
730							addresses.add(address);
731						} else {
732							Log.d(Config.LOGTAG,getLogprefix(account)+"skipping over "+address+" because it's broken");
733						}
734					}
735				}
736			}
737		} else {
738			Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
739		}
740		if (deviceIds.get(account.getJid().toBareJid()) != null) {
741			for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
742				AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
743				if (sessions.get(address) == null) {
744					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
745					if (identityKey != null) {
746						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
747						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
748						sessions.put(address, session);
749					} else {
750						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
751						if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
752							addresses.add(address);
753						} else {
754							Log.d(Config.LOGTAG,getLogprefix(account)+"skipping over "+address+" because it's broken");
755						}
756					}
757				}
758			}
759		}
760
761		return addresses;
762	}
763
764	public boolean createSessionsIfNeeded(final Conversation conversation) {
765		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
766		boolean newSessions = false;
767		Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
768		for (AxolotlAddress address : addresses) {
769			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
770			FetchStatus status = fetchStatusMap.get(address);
771			if (status == null || status == FetchStatus.TIMEOUT) {
772				fetchStatusMap.put(address, FetchStatus.PENDING);
773				this.buildSessionFromPEP(address);
774				newSessions = true;
775			} else if (status == FetchStatus.PENDING) {
776				newSessions = true;
777			} else {
778				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
779			}
780		}
781
782		return newSessions;
783	}
784
785	public boolean trustedSessionVerified(final Conversation conversation) {
786		Set<XmppAxolotlSession> sessions = findSessionsforContact(conversation.getContact());
787		sessions.addAll(findOwnSessions());
788		boolean verified = false;
789		for(XmppAxolotlSession session : sessions) {
790			if (session.getTrust().trusted()) {
791				if (session.getTrust() == XmppAxolotlSession.Trust.TRUSTED_X509) {
792					verified = true;
793				} else {
794					return false;
795				}
796			}
797		}
798		return verified;
799	}
800
801	public boolean hasPendingKeyFetches(Account account, Contact contact) {
802		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
803		AxolotlAddress foreignAddress = new AxolotlAddress(contact.getJid().toBareJid().toString(), 0);
804		return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
805				|| fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
806
807	}
808
809	@Nullable
810	private XmppAxolotlMessage buildHeader(Contact contact) {
811		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(
812				contact.getJid().toBareJid(), getOwnDeviceId());
813
814		Set<XmppAxolotlSession> contactSessions = findSessionsforContact(contact);
815		Set<XmppAxolotlSession> ownSessions = findOwnSessions();
816		if (contactSessions.isEmpty()) {
817			return null;
818		}
819		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
820		for (XmppAxolotlSession session : contactSessions) {
821			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
822			axolotlMessage.addDevice(session);
823		}
824		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
825		for (XmppAxolotlSession session : ownSessions) {
826			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
827			axolotlMessage.addDevice(session);
828		}
829
830		return axolotlMessage;
831	}
832
833	@Nullable
834	public XmppAxolotlMessage encrypt(Message message) {
835		XmppAxolotlMessage axolotlMessage = buildHeader(message.getContact());
836
837		if (axolotlMessage != null) {
838			final String content;
839			if (message.hasFileOnRemoteHost()) {
840				content = message.getFileParams().url.toString();
841			} else {
842				content = message.getBody();
843			}
844			try {
845				axolotlMessage.encrypt(content);
846			} catch (CryptoFailedException e) {
847				Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
848				return null;
849			}
850		}
851
852		return axolotlMessage;
853	}
854
855	public void preparePayloadMessage(final Message message, final boolean delay) {
856		executor.execute(new Runnable() {
857			@Override
858			public void run() {
859				XmppAxolotlMessage axolotlMessage = encrypt(message);
860				if (axolotlMessage == null) {
861					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
862					//mXmppConnectionService.updateConversationUi();
863				} else {
864					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
865					messageCache.put(message.getUuid(), axolotlMessage);
866					mXmppConnectionService.resendMessage(message, delay);
867				}
868			}
869		});
870	}
871
872	public void prepareKeyTransportMessage(final Contact contact, final OnMessageCreatedCallback onMessageCreatedCallback) {
873		executor.execute(new Runnable() {
874			@Override
875			public void run() {
876				XmppAxolotlMessage axolotlMessage = buildHeader(contact);
877				onMessageCreatedCallback.run(axolotlMessage);
878			}
879		});
880	}
881
882	public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
883		XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
884		if (axolotlMessage != null) {
885			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
886			messageCache.remove(message.getUuid());
887		} else {
888			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
889		}
890		return axolotlMessage;
891	}
892
893	private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
894		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
895		return (identityKey != null)
896				? new XmppAxolotlSession(account, axolotlStore, address, identityKey)
897				: null;
898	}
899
900	private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
901		AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
902				message.getSenderDeviceId());
903		XmppAxolotlSession session = sessions.get(senderAddress);
904		if (session == null) {
905			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
906			session = recreateUncachedSession(senderAddress);
907			if (session == null) {
908				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
909			}
910		}
911		return session;
912	}
913
914	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message) {
915		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
916
917		XmppAxolotlSession session = getReceivingSession(message);
918		try {
919			plaintextMessage = message.decrypt(session, getOwnDeviceId());
920			Integer preKeyId = session.getPreKeyId();
921			if (preKeyId != null) {
922				publishBundlesIfNeeded(false, false);
923				session.resetPreKeyId();
924			}
925		} catch (CryptoFailedException e) {
926			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());
927		}
928
929		if (session.isFresh() && plaintextMessage != null) {
930			putFreshSession(session);
931		}
932
933		return plaintextMessage;
934	}
935
936	public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message) {
937		XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
938
939		XmppAxolotlSession session = getReceivingSession(message);
940		keyTransportMessage = message.getParameters(session, getOwnDeviceId());
941
942		if (session.isFresh() && keyTransportMessage != null) {
943			putFreshSession(session);
944		}
945
946		return keyTransportMessage;
947	}
948
949	private void putFreshSession(XmppAxolotlSession session) {
950		Log.d(Config.LOGTAG,"put fresh session");
951		sessions.put(session);
952		if (Config.X509_VERIFICATION) {
953			if (session.getIdentityKey() != null) {
954				verifySessionWithPEP(session);
955			} else {
956				Log.e(Config.LOGTAG,account.getJid().toBareJid()+": identity key was empty after reloading for x509 verification");
957			}
958		}
959	}
960}