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().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				String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
150				this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
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	private enum FetchStatus {
179		PENDING,
180		SUCCESS,
181		ERROR
182	}
183
184	private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
185
186	}
187
188	public static String getLogprefix(Account account) {
189		return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
190	}
191
192	public AxolotlService(Account account, XmppConnectionService connectionService) {
193		if (Security.getProvider("BC") == null) {
194			Security.addProvider(new BouncyCastleProvider());
195		}
196		this.mXmppConnectionService = connectionService;
197		this.account = account;
198		this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
199		this.deviceIds = new HashMap<>();
200		this.messageCache = new HashMap<>();
201		this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
202		this.fetchStatusMap = new FetchStatusMap();
203		this.executor = new SerialSingleThreadExecutor();
204	}
205
206	public String getOwnFingerprint() {
207		return axolotlStore.getIdentityKeyPair().getPublicKey().getFingerprint().replaceAll("\\s", "");
208	}
209
210	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust) {
211		return axolotlStore.getContactKeysWithTrust(account.getJid().toBareJid().toString(), trust);
212	}
213
214	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust, Contact contact) {
215		return axolotlStore.getContactKeysWithTrust(contact.getJid().toBareJid().toString(), trust);
216	}
217
218	public long getNumTrustedKeys(Contact contact) {
219		return axolotlStore.getContactNumTrustedKeys(contact.getJid().toBareJid().toString());
220	}
221
222	private AxolotlAddress getAddressForJid(Jid jid) {
223		return new AxolotlAddress(jid.toString(), 0);
224	}
225
226	private Set<XmppAxolotlSession> findOwnSessions() {
227		AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
228		return new HashSet<>(this.sessions.getAll(ownAddress).values());
229	}
230
231	private Set<XmppAxolotlSession> findSessionsforContact(Contact contact) {
232		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
233		return new HashSet<>(this.sessions.getAll(contactAddress).values());
234	}
235
236	public Set<String> getFingerprintsForOwnSessions() {
237		Set<String> fingerprints = new HashSet<>();
238		for (XmppAxolotlSession session : findOwnSessions()) {
239			fingerprints.add(session.getFingerprint());
240		}
241		return fingerprints;
242	}
243
244	public Set<String> getFingerprintsForContact(final Contact contact) {
245		Set<String> fingerprints = new HashSet<>();
246		for (XmppAxolotlSession session : findSessionsforContact(contact)) {
247			fingerprints.add(session.getFingerprint());
248		}
249		return fingerprints;
250	}
251
252	private boolean hasAny(Contact contact) {
253		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
254		return sessions.hasAny(contactAddress);
255	}
256
257	public boolean isPepBroken() {
258		return this.pepBroken;
259	}
260
261	public void regenerateKeys(boolean wipeOther) {
262		axolotlStore.regenerate();
263		sessions.clear();
264		fetchStatusMap.clear();
265		publishBundlesIfNeeded(true, wipeOther);
266	}
267
268	public int getOwnDeviceId() {
269		return axolotlStore.getLocalRegistrationId();
270	}
271
272	public Set<Integer> getOwnDeviceIds() {
273		return this.deviceIds.get(account.getJid().toBareJid());
274	}
275
276	private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds,
277	                                final XmppAxolotlSession.Trust from,
278	                                final XmppAxolotlSession.Trust to) {
279		for (Integer deviceId : deviceIds) {
280			AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
281			XmppAxolotlSession session = sessions.get(address);
282			if (session != null && session.getFingerprint() != null
283					&& session.getTrust() == from) {
284				session.setTrust(to);
285			}
286		}
287	}
288
289	public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
290		if (jid.toBareJid().equals(account.getJid().toBareJid())) {
291			if (!deviceIds.isEmpty()) {
292				Log.d(Config.LOGTAG, getLogprefix(account) + "Received non-empty own device list. Resetting publish attemps and pepBroken status.");
293				pepBroken = false;
294				numPublishTriesOnEmptyPep = 0;
295			}
296			if (deviceIds.contains(getOwnDeviceId())) {
297				deviceIds.remove(getOwnDeviceId());
298			} else {
299				publishOwnDeviceId(deviceIds);
300			}
301			for (Integer deviceId : deviceIds) {
302				AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
303				if (sessions.get(ownDeviceAddress) == null) {
304					buildSessionFromPEP(ownDeviceAddress);
305				}
306			}
307		}
308		Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toString()));
309		expiredDevices.removeAll(deviceIds);
310		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED,
311				XmppAxolotlSession.Trust.INACTIVE_TRUSTED);
312		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNDECIDED,
313				XmppAxolotlSession.Trust.INACTIVE_UNDECIDED);
314		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNTRUSTED,
315				XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED);
316		Set<Integer> newDevices = new HashSet<>(deviceIds);
317		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED,
318				XmppAxolotlSession.Trust.TRUSTED);
319		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNDECIDED,
320				XmppAxolotlSession.Trust.UNDECIDED);
321		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
322				XmppAxolotlSession.Trust.UNTRUSTED);
323		this.deviceIds.put(jid, deviceIds);
324		mXmppConnectionService.keyStatusUpdated();
325	}
326
327	public void wipeOtherPepDevices() {
328		if (pepBroken) {
329			Log.d(Config.LOGTAG, getLogprefix(account) + "wipeOtherPepDevices called, but PEP is broken. Ignoring... ");
330			return;
331		}
332		Set<Integer> deviceIds = new HashSet<>();
333		deviceIds.add(getOwnDeviceId());
334		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
335		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
336		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
337			@Override
338			public void onIqPacketReceived(Account account, IqPacket packet) {
339				// TODO: implement this!
340			}
341		});
342	}
343
344	public void purgeKey(final String fingerprint) {
345		axolotlStore.setFingerprintTrust(fingerprint.replaceAll("\\s", ""), XmppAxolotlSession.Trust.COMPROMISED);
346	}
347
348	public void publishOwnDeviceIdIfNeeded() {
349		if (pepBroken) {
350			Log.d(Config.LOGTAG, getLogprefix(account) + "publishOwnDeviceIdIfNeeded called, but PEP is broken. Ignoring... ");
351			return;
352		}
353		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
354		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
355			@Override
356			public void onIqPacketReceived(Account account, IqPacket packet) {
357				if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
358					Log.d(Config.LOGTAG, getLogprefix(account) + "Timeout received while retrieving own Device Ids.");
359				} else {
360					Element item = mXmppConnectionService.getIqParser().getItem(packet);
361					Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
362					if (!deviceIds.contains(getOwnDeviceId())) {
363						publishOwnDeviceId(deviceIds);
364					}
365				}
366			}
367		});
368	}
369
370	public void publishOwnDeviceId(Set<Integer> deviceIds) {
371		Set<Integer> deviceIdsCopy = new HashSet<>(deviceIds);
372		if (!deviceIdsCopy.contains(getOwnDeviceId())) {
373			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist.");
374			if (deviceIdsCopy.isEmpty()) {
375				if (numPublishTriesOnEmptyPep >= publishTriesThreshold) {
376					Log.w(Config.LOGTAG, getLogprefix(account) + "Own device publish attempt threshold exceeded, aborting...");
377					pepBroken = true;
378					return;
379				} else {
380					numPublishTriesOnEmptyPep++;
381					Log.w(Config.LOGTAG, getLogprefix(account) + "Own device list empty, attempting to publish (try " + numPublishTriesOnEmptyPep + ")");
382				}
383			} else {
384				numPublishTriesOnEmptyPep = 0;
385			}
386			deviceIdsCopy.add(getOwnDeviceId());
387			IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIdsCopy);
388			mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
389				@Override
390				public void onIqPacketReceived(Account account, IqPacket packet) {
391					if (packet.getType() != IqPacket.TYPE.RESULT) {
392						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
393					}
394				}
395			});
396		}
397	}
398
399	public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord,
400												   final Set<PreKeyRecord> preKeyRecords,
401												   final boolean announceAfter,
402												   final boolean wipe) {
403		try {
404			IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
405			PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
406			X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
407			Signature verifier = Signature.getInstance("sha256WithRSA");
408			verifier.initSign(x509PrivateKey,mXmppConnectionService.getRNG());
409			verifier.update(axolotlPublicKey.serialize());
410			byte[] signature = verifier.sign();
411			IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
412			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device "+getOwnDeviceId());
413			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
414				@Override
415				public void onIqPacketReceived(Account account, IqPacket packet) {
416					publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
417				}
418			});
419		} catch (Exception  e) {
420			e.printStackTrace();
421		}
422	}
423
424	public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
425		if (pepBroken) {
426			Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
427			return;
428		}
429		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
430		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
431			@Override
432			public void onIqPacketReceived(Account account, IqPacket packet) {
433
434				if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
435					return; //ignore timeout. do nothing
436				}
437
438				if (packet.getType() == IqPacket.TYPE.ERROR) {
439					Element error = packet.findChild("error");
440					if (error == null || !error.hasChild("item-not-found")) {
441						pepBroken = true;
442						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
443						return;
444					}
445				}
446
447				PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
448				Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
449				boolean flush = false;
450				if (bundle == null) {
451					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
452					bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
453					flush = true;
454				}
455				if (keys == null) {
456					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
457				}
458				try {
459					boolean changed = false;
460					// Validate IdentityKey
461					IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
462					if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
463						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
464						changed = true;
465					}
466
467					// Validate signedPreKeyRecord + ID
468					SignedPreKeyRecord signedPreKeyRecord;
469					int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
470					try {
471						signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
472						if (flush
473								|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
474								|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
475							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
476							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
477							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
478							changed = true;
479						}
480					} catch (InvalidKeyIdException e) {
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
487					// Validate PreKeys
488					Set<PreKeyRecord> preKeyRecords = new HashSet<>();
489					if (keys != null) {
490						for (Integer id : keys.keySet()) {
491							try {
492								PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
493								if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
494									preKeyRecords.add(preKeyRecord);
495								}
496							} catch (InvalidKeyIdException ignored) {
497							}
498						}
499					}
500					int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
501					if (newKeys > 0) {
502						List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
503								axolotlStore.getCurrentPreKeyId() + 1, newKeys);
504						preKeyRecords.addAll(newRecords);
505						for (PreKeyRecord record : newRecords) {
506							axolotlStore.storePreKey(record.getId(), record);
507						}
508						changed = true;
509						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
510					}
511
512
513					if (changed) {
514						if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
515							publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
516						} else {
517							publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
518						}
519					} else {
520						Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
521						if (wipe) {
522							wipeOtherPepDevices();
523						} else if (announce) {
524							Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
525							publishOwnDeviceIdIfNeeded();
526						}
527					}
528				} catch (InvalidKeyException e) {
529					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
530				}
531			}
532		});
533	}
534
535	private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord,
536									 Set<PreKeyRecord> preKeyRecords,
537									 final boolean announceAfter,
538									 final boolean wipe) {
539		IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
540				signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
541				preKeyRecords, getOwnDeviceId());
542		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
543		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
544			@Override
545			public void onIqPacketReceived(Account account, IqPacket packet) {
546				if (packet.getType() == IqPacket.TYPE.RESULT) {
547					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
548					if (wipe) {
549						wipeOtherPepDevices();
550					} else if (announceAfter) {
551						Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
552						publishOwnDeviceIdIfNeeded();
553					}
554				} else {
555					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
556				}
557			}
558		});
559	}
560
561	public boolean isContactAxolotlCapable(Contact contact) {
562		Jid jid = contact.getJid().toBareJid();
563		return hasAny(contact) ||
564				(deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
565	}
566
567	public XmppAxolotlSession.Trust getFingerprintTrust(String fingerprint) {
568		return axolotlStore.getFingerprintTrust(fingerprint);
569	}
570
571	public void setFingerprintTrust(String fingerprint, XmppAxolotlSession.Trust trust) {
572		axolotlStore.setFingerprintTrust(fingerprint, trust);
573	}
574
575	private void verifySessionWithPEP(final XmppAxolotlSession session, final IdentityKey identityKey) {
576		final AxolotlAddress address = session.getRemoteAddress();
577		try {
578			IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
579			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
580				@Override
581				public void onIqPacketReceived(Account account, IqPacket packet) {
582					Pair<X509Certificate[],byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
583					if (verification != null) {
584						try {
585							Signature verifier = Signature.getInstance("sha256WithRSA");
586							verifier.initVerify(verification.first[0]);
587							verifier.update(identityKey.serialize());
588							if (verifier.verify(verification.second)) {
589								try {
590									mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
591									Log.d(Config.LOGTAG, "verified session with x.509 signature");
592									setFingerprintTrust(session.getFingerprint(), XmppAxolotlSession.Trust.TRUSTED);
593								} catch (Exception e) {
594									Log.d(Config.LOGTAG,"could not verify certificate");
595								}
596							}
597						} catch (Exception e) {
598							Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
599						}
600					} else {
601						Log.d(Config.LOGTAG, " unable to parse verification");
602					}
603					finishBuildingSessionsFromPEP(address);
604				}
605			});
606		} catch (InvalidJidException e) {
607			finishBuildingSessionsFromPEP(address);
608		}
609	}
610
611	private void finishBuildingSessionsFromPEP(final AxolotlAddress address) {
612		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
613		if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
614				&& !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
615			mXmppConnectionService.keyStatusUpdated();
616		}
617	}
618
619	private void buildSessionFromPEP(final AxolotlAddress address) {
620		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.toString());
621		if (address.getDeviceId() == getOwnDeviceId()) {
622			throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
623		}
624
625		try {
626			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
627					Jid.fromString(address.getName()), address.getDeviceId());
628			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
629			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
630
631				@Override
632				public void onIqPacketReceived(Account account, IqPacket packet) {
633					if (packet.getType() == IqPacket.TYPE.RESULT) {
634						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
635						final IqParser parser = mXmppConnectionService.getIqParser();
636						final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
637						final PreKeyBundle bundle = parser.bundle(packet);
638						if (preKeyBundleList.isEmpty() || bundle == null) {
639							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
640							fetchStatusMap.put(address, FetchStatus.ERROR);
641							finishBuildingSessionsFromPEP(address);
642							return;
643						}
644						Random random = new Random();
645						final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
646						if (preKey == null) {
647							//should never happen
648							fetchStatusMap.put(address, FetchStatus.ERROR);
649							finishBuildingSessionsFromPEP(address);
650							return;
651						}
652
653						final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
654								preKey.getPreKeyId(), preKey.getPreKey(),
655								bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
656								bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
657
658						try {
659							SessionBuilder builder = new SessionBuilder(axolotlStore, address);
660							builder.process(preKeyBundle);
661							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
662							sessions.put(address, session);
663							fetchStatusMap.put(address, FetchStatus.SUCCESS);
664							if (Config.X509_VERIFICATION) {
665								verifySessionWithPEP(session, bundle.getIdentityKey());
666							} else {
667								finishBuildingSessionsFromPEP(address);
668							}
669						} catch (UntrustedIdentityException | InvalidKeyException e) {
670							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
671									+ e.getClass().getName() + ", " + e.getMessage());
672							fetchStatusMap.put(address, FetchStatus.ERROR);
673							finishBuildingSessionsFromPEP(address);
674						}
675					} else {
676						fetchStatusMap.put(address, FetchStatus.ERROR);
677						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
678						finishBuildingSessionsFromPEP(address);
679					}
680				}
681			});
682		} catch (InvalidJidException e) {
683			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
684		}
685	}
686
687	public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
688		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
689		Jid contactJid = conversation.getContact().getJid().toBareJid();
690		Set<AxolotlAddress> addresses = new HashSet<>();
691		if (deviceIds.get(contactJid) != null) {
692			for (Integer foreignId : this.deviceIds.get(contactJid)) {
693				AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
694				if (sessions.get(address) == null) {
695					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
696					if (identityKey != null) {
697						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
698						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
699						sessions.put(address, session);
700					} else {
701						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
702						addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
703					}
704				}
705			}
706		} else {
707			Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
708		}
709		if (deviceIds.get(account.getJid().toBareJid()) != null) {
710			for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
711				AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
712				if (sessions.get(address) == null) {
713					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
714					if (identityKey != null) {
715						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
716						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
717						sessions.put(address, session);
718					} else {
719						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
720						addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
721					}
722				}
723			}
724		}
725
726		return addresses;
727	}
728
729	public boolean createSessionsIfNeeded(final Conversation conversation) {
730		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
731		boolean newSessions = false;
732		Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
733		for (AxolotlAddress address : addresses) {
734			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
735			FetchStatus status = fetchStatusMap.get(address);
736			if (status == null || status == FetchStatus.ERROR) {
737				fetchStatusMap.put(address, FetchStatus.PENDING);
738				this.buildSessionFromPEP(address);
739				newSessions = true;
740			} else if (status == FetchStatus.PENDING) {
741				newSessions = true;
742			} else {
743				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
744			}
745		}
746
747		return newSessions;
748	}
749
750	public boolean hasPendingKeyFetches(Account account, Contact contact) {
751		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
752		AxolotlAddress foreignAddress = new AxolotlAddress(contact.getJid().toBareJid().toString(), 0);
753		return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
754				|| fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
755
756	}
757
758	@Nullable
759	private XmppAxolotlMessage buildHeader(Contact contact) {
760		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(
761				contact.getJid().toBareJid(), getOwnDeviceId());
762
763		Set<XmppAxolotlSession> contactSessions = findSessionsforContact(contact);
764		Set<XmppAxolotlSession> ownSessions = findOwnSessions();
765		if (contactSessions.isEmpty()) {
766			return null;
767		}
768		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
769		for (XmppAxolotlSession session : contactSessions) {
770			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
771			axolotlMessage.addDevice(session);
772		}
773		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
774		for (XmppAxolotlSession session : ownSessions) {
775			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
776			axolotlMessage.addDevice(session);
777		}
778
779		return axolotlMessage;
780	}
781
782	@Nullable
783	public XmppAxolotlMessage encrypt(Message message) {
784		XmppAxolotlMessage axolotlMessage = buildHeader(message.getContact());
785
786		if (axolotlMessage != null) {
787			final String content;
788			if (message.hasFileOnRemoteHost()) {
789				content = message.getFileParams().url.toString();
790			} else {
791				content = message.getBody();
792			}
793			try {
794				axolotlMessage.encrypt(content);
795			} catch (CryptoFailedException e) {
796				Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
797				return null;
798			}
799		}
800
801		return axolotlMessage;
802	}
803
804	public void preparePayloadMessage(final Message message, final boolean delay) {
805		executor.execute(new Runnable() {
806			@Override
807			public void run() {
808				XmppAxolotlMessage axolotlMessage = encrypt(message);
809				if (axolotlMessage == null) {
810					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
811					//mXmppConnectionService.updateConversationUi();
812				} else {
813					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
814					messageCache.put(message.getUuid(), axolotlMessage);
815					mXmppConnectionService.resendMessage(message, delay);
816				}
817			}
818		});
819	}
820
821	public void prepareKeyTransportMessage(final Contact contact, final OnMessageCreatedCallback onMessageCreatedCallback) {
822		executor.execute(new Runnable() {
823			@Override
824			public void run() {
825				XmppAxolotlMessage axolotlMessage = buildHeader(contact);
826				onMessageCreatedCallback.run(axolotlMessage);
827			}
828		});
829	}
830
831	public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
832		XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
833		if (axolotlMessage != null) {
834			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
835			messageCache.remove(message.getUuid());
836		} else {
837			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
838		}
839		return axolotlMessage;
840	}
841
842	private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
843		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
844		return (identityKey != null)
845				? new XmppAxolotlSession(account, axolotlStore, address,
846						identityKey.getFingerprint().replaceAll("\\s", ""))
847				: null;
848	}
849
850	private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
851		AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
852				message.getSenderDeviceId());
853		XmppAxolotlSession session = sessions.get(senderAddress);
854		if (session == null) {
855			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
856			session = recreateUncachedSession(senderAddress);
857			if (session == null) {
858				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
859			}
860		}
861		return session;
862	}
863
864	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message) {
865		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
866
867		XmppAxolotlSession session = getReceivingSession(message);
868		try {
869			plaintextMessage = message.decrypt(session, getOwnDeviceId());
870			Integer preKeyId = session.getPreKeyId();
871			if (preKeyId != null) {
872				publishBundlesIfNeeded(false, false);
873				session.resetPreKeyId();
874			}
875		} catch (CryptoFailedException e) {
876			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());
877		}
878
879		if (session.isFresh() && plaintextMessage != null) {
880			sessions.put(session);
881		}
882
883		return plaintextMessage;
884	}
885
886	public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message) {
887		XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
888
889		XmppAxolotlSession session = getReceivingSession(message);
890		keyTransportMessage = message.getParameters(session, getOwnDeviceId());
891
892		if (session.isFresh() && keyTransportMessage != null) {
893			sessions.put(session);
894		}
895
896		return keyTransportMessage;
897	}
898}