AxolotlService.java

  1package eu.siacs.conversations.crypto.axolotl;
  2
  3import android.support.annotation.NonNull;
  4import android.support.annotation.Nullable;
  5import android.util.Log;
  6
  7import org.bouncycastle.jce.provider.BouncyCastleProvider;
  8import org.whispersystems.libaxolotl.AxolotlAddress;
  9import org.whispersystems.libaxolotl.IdentityKey;
 10import org.whispersystems.libaxolotl.IdentityKeyPair;
 11import org.whispersystems.libaxolotl.InvalidKeyException;
 12import org.whispersystems.libaxolotl.InvalidKeyIdException;
 13import org.whispersystems.libaxolotl.SessionBuilder;
 14import org.whispersystems.libaxolotl.UntrustedIdentityException;
 15import org.whispersystems.libaxolotl.ecc.ECPublicKey;
 16import org.whispersystems.libaxolotl.state.PreKeyBundle;
 17import org.whispersystems.libaxolotl.state.PreKeyRecord;
 18import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
 19import org.whispersystems.libaxolotl.util.KeyHelper;
 20
 21import java.security.Security;
 22import java.util.Arrays;
 23import java.util.HashMap;
 24import java.util.HashSet;
 25import java.util.List;
 26import java.util.Map;
 27import java.util.Random;
 28import java.util.Set;
 29
 30import eu.siacs.conversations.Config;
 31import eu.siacs.conversations.entities.Account;
 32import eu.siacs.conversations.entities.Contact;
 33import eu.siacs.conversations.entities.Conversation;
 34import eu.siacs.conversations.entities.Message;
 35import eu.siacs.conversations.parser.IqParser;
 36import eu.siacs.conversations.services.XmppConnectionService;
 37import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
 38import eu.siacs.conversations.xml.Element;
 39import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 40import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 41import eu.siacs.conversations.xmpp.jid.Jid;
 42import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 43
 44public class AxolotlService {
 45
 46	public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
 47	public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
 48	public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";
 49
 50	public static final String LOGPREFIX = "AxolotlService";
 51
 52	public static final int NUM_KEYS_TO_PUBLISH = 10;
 53
 54	private final Account account;
 55	private final XmppConnectionService mXmppConnectionService;
 56	private final SQLiteAxolotlStore axolotlStore;
 57	private final SessionMap sessions;
 58	private final Map<Jid, Set<Integer>> deviceIds;
 59	private final Map<String, XmppAxolotlMessage> messageCache;
 60	private final FetchStatusMap fetchStatusMap;
 61	private final SerialSingleThreadExecutor executor;
 62
 63	private static class AxolotlAddressMap<T> {
 64		protected Map<String, Map<Integer, T>> map;
 65		protected final Object MAP_LOCK = new Object();
 66
 67		public AxolotlAddressMap() {
 68			this.map = new HashMap<>();
 69		}
 70
 71		public void put(AxolotlAddress address, T value) {
 72			synchronized (MAP_LOCK) {
 73				Map<Integer, T> devices = map.get(address.getName());
 74				if (devices == null) {
 75					devices = new HashMap<>();
 76					map.put(address.getName(), devices);
 77				}
 78				devices.put(address.getDeviceId(), value);
 79			}
 80		}
 81
 82		public T get(AxolotlAddress address) {
 83			synchronized (MAP_LOCK) {
 84				Map<Integer, T> devices = map.get(address.getName());
 85				if (devices == null) {
 86					return null;
 87				}
 88				return devices.get(address.getDeviceId());
 89			}
 90		}
 91
 92		public Map<Integer, T> getAll(AxolotlAddress address) {
 93			synchronized (MAP_LOCK) {
 94				Map<Integer, T> devices = map.get(address.getName());
 95				if (devices == null) {
 96					return new HashMap<>();
 97				}
 98				return devices;
 99			}
100		}
101
102		public boolean hasAny(AxolotlAddress address) {
103			synchronized (MAP_LOCK) {
104				Map<Integer, T> devices = map.get(address.getName());
105				return devices != null && !devices.isEmpty();
106			}
107		}
108
109		public void clear() {
110			map.clear();
111		}
112
113	}
114
115	private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
116		private final XmppConnectionService xmppConnectionService;
117		private final Account account;
118
119		public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
120			super();
121			this.xmppConnectionService = service;
122			this.account = account;
123			this.fillMap(store);
124		}
125
126		private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
127			for (Integer deviceId : deviceIds) {
128				AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
129				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString());
130				String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
131				this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
132			}
133		}
134
135		private void fillMap(SQLiteAxolotlStore store) {
136			List<Integer> deviceIds = store.getSubDeviceSessions(account.getJid().toBareJid().toString());
137			putDevicesForJid(account.getJid().toBareJid().toString(), deviceIds, store);
138			for (Contact contact : account.getRoster().getContacts()) {
139				Jid bareJid = contact.getJid().toBareJid();
140				if (bareJid == null) {
141					continue; // FIXME: handle this?
142				}
143				String address = bareJid.toString();
144				deviceIds = store.getSubDeviceSessions(address);
145				putDevicesForJid(address, deviceIds, store);
146			}
147
148		}
149
150		@Override
151		public void put(AxolotlAddress address, XmppAxolotlSession value) {
152			super.put(address, value);
153			value.setNotFresh();
154			xmppConnectionService.syncRosterToDisk(account);
155		}
156
157		public void put(XmppAxolotlSession session) {
158			this.put(session.getRemoteAddress(), session);
159		}
160	}
161
162	private static enum FetchStatus {
163		PENDING,
164		SUCCESS,
165		ERROR
166	}
167
168	private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
169
170	}
171
172	public static String getLogprefix(Account account) {
173		return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
174	}
175
176	public AxolotlService(Account account, XmppConnectionService connectionService) {
177		if (Security.getProvider("BC") == null) {
178			Security.addProvider(new BouncyCastleProvider());
179		}
180		this.mXmppConnectionService = connectionService;
181		this.account = account;
182		this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
183		this.deviceIds = new HashMap<>();
184		this.messageCache = new HashMap<>();
185		this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
186		this.fetchStatusMap = new FetchStatusMap();
187		this.executor = new SerialSingleThreadExecutor();
188	}
189
190	public IdentityKey getOwnPublicKey() {
191		return axolotlStore.getIdentityKeyPair().getPublicKey();
192	}
193
194	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust) {
195		return axolotlStore.getContactKeysWithTrust(account.getJid().toBareJid().toString(), trust);
196	}
197
198	public Set<IdentityKey> getKeysWithTrust(XmppAxolotlSession.Trust trust, Contact contact) {
199		return axolotlStore.getContactKeysWithTrust(contact.getJid().toBareJid().toString(), trust);
200	}
201
202	public long getNumTrustedKeys(Contact contact) {
203		return axolotlStore.getContactNumTrustedKeys(contact.getJid().toBareJid().toString());
204	}
205
206	private AxolotlAddress getAddressForJid(Jid jid) {
207		return new AxolotlAddress(jid.toString(), 0);
208	}
209
210	private Set<XmppAxolotlSession> findOwnSessions() {
211		AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
212		Set<XmppAxolotlSession> ownDeviceSessions = new HashSet<>(this.sessions.getAll(ownAddress).values());
213		return ownDeviceSessions;
214	}
215
216	private Set<XmppAxolotlSession> findSessionsforContact(Contact contact) {
217		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
218		Set<XmppAxolotlSession> sessions = new HashSet<>(this.sessions.getAll(contactAddress).values());
219		return sessions;
220	}
221
222	private boolean hasAny(Contact contact) {
223		AxolotlAddress contactAddress = getAddressForJid(contact.getJid());
224		return sessions.hasAny(contactAddress);
225	}
226
227	public void regenerateKeys() {
228		axolotlStore.regenerate();
229		sessions.clear();
230		fetchStatusMap.clear();
231		publishBundlesIfNeeded();
232		publishOwnDeviceIdIfNeeded();
233	}
234
235	public int getOwnDeviceId() {
236		return axolotlStore.getLocalRegistrationId();
237	}
238
239	public Set<Integer> getOwnDeviceIds() {
240		return this.deviceIds.get(account.getJid().toBareJid());
241	}
242
243	private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds,
244	                                final XmppAxolotlSession.Trust from,
245	                                final XmppAxolotlSession.Trust to) {
246		for (Integer deviceId : deviceIds) {
247			AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
248			XmppAxolotlSession session = sessions.get(address);
249			if (session != null && session.getFingerprint() != null
250					&& session.getTrust() == from) {
251				session.setTrust(to);
252			}
253		}
254	}
255
256	public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
257		if (jid.toBareJid().equals(account.getJid().toBareJid())) {
258			if (deviceIds.contains(getOwnDeviceId())) {
259				deviceIds.remove(getOwnDeviceId());
260			}
261			for (Integer deviceId : deviceIds) {
262				AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
263				if (sessions.get(ownDeviceAddress) == null) {
264					buildSessionFromPEP(ownDeviceAddress);
265				}
266			}
267		}
268		Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toString()));
269		expiredDevices.removeAll(deviceIds);
270		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED,
271				XmppAxolotlSession.Trust.INACTIVE_TRUSTED);
272		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNDECIDED,
273				XmppAxolotlSession.Trust.INACTIVE_UNDECIDED);
274		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNTRUSTED,
275				XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED);
276		Set<Integer> newDevices = new HashSet<>(deviceIds);
277		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED,
278				XmppAxolotlSession.Trust.TRUSTED);
279		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNDECIDED,
280				XmppAxolotlSession.Trust.UNDECIDED);
281		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
282				XmppAxolotlSession.Trust.UNTRUSTED);
283		this.deviceIds.put(jid, deviceIds);
284		mXmppConnectionService.keyStatusUpdated();
285		publishOwnDeviceIdIfNeeded();
286	}
287
288	public void wipeOtherPepDevices() {
289		Set<Integer> deviceIds = new HashSet<>();
290		deviceIds.add(getOwnDeviceId());
291		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
292		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
293		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
294			@Override
295			public void onIqPacketReceived(Account account, IqPacket packet) {
296				// TODO: implement this!
297			}
298		});
299	}
300
301	public void purgeKey(IdentityKey identityKey) {
302		axolotlStore.setFingerprintTrust(identityKey.getFingerprint().replaceAll("\\s", ""), XmppAxolotlSession.Trust.COMPROMISED);
303	}
304
305	public void publishOwnDeviceIdIfNeeded() {
306		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
307		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
308			@Override
309			public void onIqPacketReceived(Account account, IqPacket packet) {
310				Element item = mXmppConnectionService.getIqParser().getItem(packet);
311				Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
312				if (deviceIds == null) {
313					deviceIds = new HashSet<Integer>();
314				}
315				if (!deviceIds.contains(getOwnDeviceId())) {
316					deviceIds.add(getOwnDeviceId());
317					IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
318					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist. Publishing: " + publish);
319					mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
320						@Override
321						public void onIqPacketReceived(Account account, IqPacket packet) {
322							// TODO: implement this!
323						}
324					});
325				}
326			}
327		});
328	}
329
330	public void publishBundlesIfNeeded() {
331		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
332		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
333			@Override
334			public void onIqPacketReceived(Account account, IqPacket packet) {
335				PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
336				Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
337				boolean flush = false;
338				if (bundle == null) {
339					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
340					bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
341					flush = true;
342				}
343				if (keys == null) {
344					Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
345				}
346				try {
347					boolean changed = false;
348					// Validate IdentityKey
349					IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
350					if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
351						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
352						changed = true;
353					}
354
355					// Validate signedPreKeyRecord + ID
356					SignedPreKeyRecord signedPreKeyRecord;
357					int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
358					try {
359						signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
360						if (flush
361								|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
362								|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
363							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
364							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
365							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
366							changed = true;
367						}
368					} catch (InvalidKeyIdException e) {
369						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
370						signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
371						axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
372						changed = true;
373					}
374
375					// Validate PreKeys
376					Set<PreKeyRecord> preKeyRecords = new HashSet<>();
377					if (keys != null) {
378						for (Integer id : keys.keySet()) {
379							try {
380								PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
381								if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
382									preKeyRecords.add(preKeyRecord);
383								}
384							} catch (InvalidKeyIdException ignored) {
385							}
386						}
387					}
388					int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
389					if (newKeys > 0) {
390						List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
391								axolotlStore.getCurrentPreKeyId() + 1, newKeys);
392						preKeyRecords.addAll(newRecords);
393						for (PreKeyRecord record : newRecords) {
394							axolotlStore.storePreKey(record.getId(), record);
395						}
396						changed = true;
397						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
398					}
399
400
401					if (changed) {
402						IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
403								signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
404								preKeyRecords, getOwnDeviceId());
405						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
406						mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
407							@Override
408							public void onIqPacketReceived(Account account, IqPacket packet) {
409								// TODO: implement this!
410								Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Published bundle, got: " + packet);
411							}
412						});
413					}
414				} catch (InvalidKeyException e) {
415					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
416					return;
417				}
418			}
419		});
420	}
421
422	public boolean isContactAxolotlCapable(Contact contact) {
423
424		Jid jid = contact.getJid().toBareJid();
425		AxolotlAddress address = new AxolotlAddress(jid.toString(), 0);
426		return sessions.hasAny(address) ||
427				(deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
428	}
429
430	public XmppAxolotlSession.Trust getFingerprintTrust(String fingerprint) {
431		return axolotlStore.getFingerprintTrust(fingerprint);
432	}
433
434	public void setFingerprintTrust(String fingerprint, XmppAxolotlSession.Trust trust) {
435		axolotlStore.setFingerprintTrust(fingerprint, trust);
436	}
437
438	private void buildSessionFromPEP(final AxolotlAddress address) {
439		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.getDeviceId());
440
441		try {
442			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
443					Jid.fromString(address.getName()), address.getDeviceId());
444			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
445			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
446				private void finish() {
447					AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
448					if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
449							&& !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
450						mXmppConnectionService.keyStatusUpdated();
451					}
452				}
453
454				@Override
455				public void onIqPacketReceived(Account account, IqPacket packet) {
456					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
457					final IqParser parser = mXmppConnectionService.getIqParser();
458					final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
459					final PreKeyBundle bundle = parser.bundle(packet);
460					if (preKeyBundleList.isEmpty() || bundle == null) {
461						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
462						fetchStatusMap.put(address, FetchStatus.ERROR);
463						finish();
464						return;
465					}
466					Random random = new Random();
467					final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
468					if (preKey == null) {
469						//should never happen
470						fetchStatusMap.put(address, FetchStatus.ERROR);
471						finish();
472						return;
473					}
474
475					final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
476							preKey.getPreKeyId(), preKey.getPreKey(),
477							bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
478							bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
479
480					axolotlStore.saveIdentity(address.getName(), bundle.getIdentityKey());
481
482					try {
483						SessionBuilder builder = new SessionBuilder(axolotlStore, address);
484						builder.process(preKeyBundle);
485						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
486						sessions.put(address, session);
487						fetchStatusMap.put(address, FetchStatus.SUCCESS);
488					} catch (UntrustedIdentityException | InvalidKeyException e) {
489						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
490								+ e.getClass().getName() + ", " + e.getMessage());
491						fetchStatusMap.put(address, FetchStatus.ERROR);
492					}
493
494					finish();
495				}
496			});
497		} catch (InvalidJidException e) {
498			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
499		}
500	}
501
502	public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
503		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
504		Jid contactJid = conversation.getContact().getJid().toBareJid();
505		Set<AxolotlAddress> addresses = new HashSet<>();
506		if (deviceIds.get(contactJid) != null) {
507			for (Integer foreignId : this.deviceIds.get(contactJid)) {
508				AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
509				if (sessions.get(address) == null) {
510					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
511					if (identityKey != null) {
512						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
513						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
514						sessions.put(address, session);
515					} else {
516						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
517						addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
518					}
519				}
520			}
521		} else {
522			Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
523		}
524		if (deviceIds.get(account.getJid().toBareJid()) != null) {
525			for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
526				AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
527				if (sessions.get(address) == null) {
528					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
529					if (identityKey != null) {
530						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
531						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
532						sessions.put(address, session);
533					} else {
534						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
535						addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
536					}
537				}
538			}
539		}
540
541		return addresses;
542	}
543
544	public boolean createSessionsIfNeeded(final Conversation conversation) {
545		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
546		boolean newSessions = false;
547		Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
548		for (AxolotlAddress address : addresses) {
549			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
550			FetchStatus status = fetchStatusMap.get(address);
551			if (status == null || status == FetchStatus.ERROR) {
552				fetchStatusMap.put(address, FetchStatus.PENDING);
553				this.buildSessionFromPEP(address);
554				newSessions = true;
555			} else if (status == FetchStatus.PENDING) {
556				newSessions = true;
557			} else {
558				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
559			}
560		}
561
562		return newSessions;
563	}
564
565	public boolean hasPendingKeyFetches(Conversation conversation) {
566		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
567		AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(), 0);
568		return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
569				|| fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
570
571	}
572
573	@Nullable
574	private XmppAxolotlMessage buildHeader(Contact contact) {
575		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(
576				contact.getJid().toBareJid(), getOwnDeviceId());
577
578		Set<XmppAxolotlSession> contactSessions = findSessionsforContact(contact);
579		Set<XmppAxolotlSession> ownSessions = findOwnSessions();
580		if (contactSessions.isEmpty()) {
581			return null;
582		}
583		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
584		for (XmppAxolotlSession session : contactSessions) {
585			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
586			axolotlMessage.addDevice(session);
587		}
588		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
589		for (XmppAxolotlSession session : ownSessions) {
590			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
591			axolotlMessage.addDevice(session);
592		}
593
594		return axolotlMessage;
595	}
596
597	@Nullable
598	public XmppAxolotlMessage encrypt(Message message) {
599		XmppAxolotlMessage axolotlMessage = buildHeader(message.getContact());
600
601		if (axolotlMessage != null) {
602			final String content;
603			if (message.hasFileOnRemoteHost()) {
604				content = message.getFileParams().url.toString();
605			} else {
606				content = message.getBody();
607			}
608			try {
609				axolotlMessage.encrypt(content);
610			} catch (CryptoFailedException e) {
611				Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
612				return null;
613			}
614		}
615
616		return axolotlMessage;
617	}
618
619	public void preparePayloadMessage(final Message message, final boolean delay) {
620		executor.execute(new Runnable() {
621			@Override
622			public void run() {
623				XmppAxolotlMessage axolotlMessage = encrypt(message);
624				if (axolotlMessage == null) {
625					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
626					//mXmppConnectionService.updateConversationUi();
627				} else {
628					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
629					messageCache.put(message.getUuid(), axolotlMessage);
630					mXmppConnectionService.resendMessage(message, delay);
631				}
632			}
633		});
634	}
635
636	public void prepareKeyTransportMessage(final Contact contact, final OnMessageCreatedCallback onMessageCreatedCallback) {
637		executor.execute(new Runnable() {
638			@Override
639			public void run() {
640				XmppAxolotlMessage axolotlMessage = buildHeader(contact);
641				onMessageCreatedCallback.run(axolotlMessage);
642			}
643		});
644	}
645
646	public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
647		XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
648		if (axolotlMessage != null) {
649			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
650			messageCache.remove(message.getUuid());
651		} else {
652			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
653		}
654		return axolotlMessage;
655	}
656
657	private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
658		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
659		return (identityKey != null)
660				? new XmppAxolotlSession(account, axolotlStore, address,
661						identityKey.getFingerprint().replaceAll("\\s", ""))
662				: null;
663	}
664
665	private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
666		AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
667				message.getSenderDeviceId());
668		XmppAxolotlSession session = sessions.get(senderAddress);
669		if (session == null) {
670			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
671			session = recreateUncachedSession(senderAddress);
672			if (session == null) {
673				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
674			}
675		}
676		return session;
677	}
678
679	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message) {
680		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
681
682		XmppAxolotlSession session = getReceivingSession(message);
683		try {
684			plaintextMessage = message.decrypt(session, getOwnDeviceId());
685			Integer preKeyId = session.getPreKeyId();
686			if (preKeyId != null) {
687				publishBundlesIfNeeded();
688				session.resetPreKeyId();
689			}
690		} catch (CryptoFailedException e) {
691			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());
692		}
693
694		if (session.isFresh() && plaintextMessage != null) {
695			sessions.put(session);
696		}
697
698		return plaintextMessage;
699	}
700
701	public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message) {
702		XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage = null;
703
704		XmppAxolotlSession session = getReceivingSession(message);
705		keyTransportMessage = message.getParameters(session, getOwnDeviceId());
706
707		if (session.isFresh() && keyTransportMessage != null) {
708			sessions.put(session);
709		}
710
711		return keyTransportMessage;
712	}
713}