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