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 = 100;
 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			} else {
261				publishOwnDeviceId(deviceIds);
262			}
263			for (Integer deviceId : deviceIds) {
264				AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
265				if (sessions.get(ownDeviceAddress) == null) {
266					buildSessionFromPEP(ownDeviceAddress);
267				}
268			}
269		}
270		Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toString()));
271		expiredDevices.removeAll(deviceIds);
272		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.TRUSTED,
273				XmppAxolotlSession.Trust.INACTIVE_TRUSTED);
274		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNDECIDED,
275				XmppAxolotlSession.Trust.INACTIVE_UNDECIDED);
276		setTrustOnSessions(jid, expiredDevices, XmppAxolotlSession.Trust.UNTRUSTED,
277				XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED);
278		Set<Integer> newDevices = new HashSet<>(deviceIds);
279		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_TRUSTED,
280				XmppAxolotlSession.Trust.TRUSTED);
281		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNDECIDED,
282				XmppAxolotlSession.Trust.UNDECIDED);
283		setTrustOnSessions(jid, newDevices, XmppAxolotlSession.Trust.INACTIVE_UNTRUSTED,
284				XmppAxolotlSession.Trust.UNTRUSTED);
285		this.deviceIds.put(jid, deviceIds);
286		mXmppConnectionService.keyStatusUpdated();
287	}
288
289	public void wipeOtherPepDevices() {
290		Set<Integer> deviceIds = new HashSet<>();
291		deviceIds.add(getOwnDeviceId());
292		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
293		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
294		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
295			@Override
296			public void onIqPacketReceived(Account account, IqPacket packet) {
297				// TODO: implement this!
298			}
299		});
300	}
301
302	public void purgeKey(IdentityKey identityKey) {
303		axolotlStore.setFingerprintTrust(identityKey.getFingerprint().replaceAll("\\s", ""), XmppAxolotlSession.Trust.COMPROMISED);
304	}
305
306	public void publishOwnDeviceIdIfNeeded() {
307		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
308		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
309			@Override
310			public void onIqPacketReceived(Account account, IqPacket packet) {
311				if (packet.getType() == IqPacket.TYPE.RESULT) {
312					Element item = mXmppConnectionService.getIqParser().getItem(packet);
313					Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
314					if (!deviceIds.contains(getOwnDeviceId())) {
315						publishOwnDeviceId(deviceIds);
316					}
317				} else {
318					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while retrieving Device Ids" + packet.findChild("error"));
319				}
320			}
321		});
322	}
323
324	public void publishOwnDeviceId(Set<Integer> deviceIds) {
325		deviceIds.add(getOwnDeviceId());
326		IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
327		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist. Publishing: " + publish);
328		mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
329			@Override
330			public void onIqPacketReceived(Account account, IqPacket packet) {
331				if (packet.getType() != IqPacket.TYPE.RESULT) {
332					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
333				}
334			}
335		});
336	}
337
338	public void publishBundlesIfNeeded() {
339		IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
340		mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
341			@Override
342			public void onIqPacketReceived(Account account, IqPacket packet) {
343				if (packet.getType() == IqPacket.TYPE.RESULT) {
344					PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
345					Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
346					boolean flush = false;
347					if (bundle == null) {
348						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
349						bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
350						flush = true;
351					}
352					if (keys == null) {
353						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
354					}
355					try {
356						boolean changed = false;
357						// Validate IdentityKey
358						IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
359						if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
360							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
361							changed = true;
362						}
363
364						// Validate signedPreKeyRecord + ID
365						SignedPreKeyRecord signedPreKeyRecord;
366						int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
367						try {
368							signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
369							if (flush
370									|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
371									|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
372								Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
373								signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
374								axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
375								changed = true;
376							}
377						} catch (InvalidKeyIdException e) {
378							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
379							signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
380							axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
381							changed = true;
382						}
383
384						// Validate PreKeys
385						Set<PreKeyRecord> preKeyRecords = new HashSet<>();
386						if (keys != null) {
387							for (Integer id : keys.keySet()) {
388								try {
389									PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
390									if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
391										preKeyRecords.add(preKeyRecord);
392									}
393								} catch (InvalidKeyIdException ignored) {
394								}
395							}
396						}
397						int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
398						if (newKeys > 0) {
399							List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
400									axolotlStore.getCurrentPreKeyId() + 1, newKeys);
401							preKeyRecords.addAll(newRecords);
402							for (PreKeyRecord record : newRecords) {
403								axolotlStore.storePreKey(record.getId(), record);
404							}
405							changed = true;
406							Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
407						}
408
409
410						if (changed) {
411							IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
412									signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
413									preKeyRecords, getOwnDeviceId());
414							Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
415							mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
416								@Override
417								public void onIqPacketReceived(Account account, IqPacket packet) {
418									// TODO: implement this!
419									Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Published bundle, got: " + packet);
420								}
421							});
422						}
423					} catch (InvalidKeyException e) {
424						Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
425						return;
426					}
427				} else {
428					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing Bundle:" + packet.findChild("error"));
429				}
430			}
431		});
432	}
433
434	public boolean isContactAxolotlCapable(Contact contact) {
435
436		Jid jid = contact.getJid().toBareJid();
437		AxolotlAddress address = new AxolotlAddress(jid.toString(), 0);
438		return sessions.hasAny(address) ||
439				(deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
440	}
441
442	public XmppAxolotlSession.Trust getFingerprintTrust(String fingerprint) {
443		return axolotlStore.getFingerprintTrust(fingerprint);
444	}
445
446	public void setFingerprintTrust(String fingerprint, XmppAxolotlSession.Trust trust) {
447		axolotlStore.setFingerprintTrust(fingerprint, trust);
448	}
449
450	private void buildSessionFromPEP(final AxolotlAddress address) {
451		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new sesstion for " + address.getDeviceId());
452
453		try {
454			IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
455					Jid.fromString(address.getName()), address.getDeviceId());
456			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
457			mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
458				private void finish() {
459					AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
460					if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
461							&& !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
462						mXmppConnectionService.keyStatusUpdated();
463					}
464				}
465
466				@Override
467				public void onIqPacketReceived(Account account, IqPacket packet) {
468					if (packet.getType() == IqPacket.TYPE.RESULT) {
469						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
470						final IqParser parser = mXmppConnectionService.getIqParser();
471						final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
472						final PreKeyBundle bundle = parser.bundle(packet);
473						if (preKeyBundleList.isEmpty() || bundle == null) {
474							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
475							fetchStatusMap.put(address, FetchStatus.ERROR);
476							finish();
477							return;
478						}
479						Random random = new Random();
480						final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
481						if (preKey == null) {
482							//should never happen
483							fetchStatusMap.put(address, FetchStatus.ERROR);
484							finish();
485							return;
486						}
487
488						final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
489								preKey.getPreKeyId(), preKey.getPreKey(),
490								bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
491								bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
492
493						axolotlStore.saveIdentity(address.getName(), bundle.getIdentityKey());
494
495						try {
496							SessionBuilder builder = new SessionBuilder(axolotlStore, address);
497							builder.process(preKeyBundle);
498							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
499							sessions.put(address, session);
500							fetchStatusMap.put(address, FetchStatus.SUCCESS);
501						} catch (UntrustedIdentityException | InvalidKeyException e) {
502							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
503									+ e.getClass().getName() + ", " + e.getMessage());
504							fetchStatusMap.put(address, FetchStatus.ERROR);
505						}
506
507						finish();
508					} else {
509						fetchStatusMap.put(address, FetchStatus.ERROR);
510						Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
511						finish();
512						return;
513					}
514				}
515			});
516		} catch (InvalidJidException e) {
517			Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
518		}
519	}
520
521	public Set<AxolotlAddress> findDevicesWithoutSession(final Conversation conversation) {
522		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
523		Jid contactJid = conversation.getContact().getJid().toBareJid();
524		Set<AxolotlAddress> addresses = new HashSet<>();
525		if (deviceIds.get(contactJid) != null) {
526			for (Integer foreignId : this.deviceIds.get(contactJid)) {
527				AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
528				if (sessions.get(address) == null) {
529					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
530					if (identityKey != null) {
531						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
532						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
533						sessions.put(address, session);
534					} else {
535						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
536						addresses.add(new AxolotlAddress(contactJid.toString(), foreignId));
537					}
538				}
539			}
540		} else {
541			Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
542		}
543		if (deviceIds.get(account.getJid().toBareJid()) != null) {
544			for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
545				AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
546				if (sessions.get(address) == null) {
547					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
548					if (identityKey != null) {
549						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
550						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
551						sessions.put(address, session);
552					} else {
553						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
554						addresses.add(new AxolotlAddress(account.getJid().toBareJid().toString(), ownId));
555					}
556				}
557			}
558		}
559
560		return addresses;
561	}
562
563	public boolean createSessionsIfNeeded(final Conversation conversation) {
564		Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
565		boolean newSessions = false;
566		Set<AxolotlAddress> addresses = findDevicesWithoutSession(conversation);
567		for (AxolotlAddress address : addresses) {
568			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
569			FetchStatus status = fetchStatusMap.get(address);
570			if (status == null || status == FetchStatus.ERROR) {
571				fetchStatusMap.put(address, FetchStatus.PENDING);
572				this.buildSessionFromPEP(address);
573				newSessions = true;
574			} else if (status == FetchStatus.PENDING) {
575				newSessions = true;
576			} else {
577				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
578			}
579		}
580
581		return newSessions;
582	}
583
584	public boolean hasPendingKeyFetches(Conversation conversation) {
585		AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
586		AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(), 0);
587		return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
588				|| fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
589
590	}
591
592	@Nullable
593	private XmppAxolotlMessage buildHeader(Contact contact) {
594		final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(
595				contact.getJid().toBareJid(), getOwnDeviceId());
596
597		Set<XmppAxolotlSession> contactSessions = findSessionsforContact(contact);
598		Set<XmppAxolotlSession> ownSessions = findOwnSessions();
599		if (contactSessions.isEmpty()) {
600			return null;
601		}
602		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
603		for (XmppAxolotlSession session : contactSessions) {
604			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
605			axolotlMessage.addDevice(session);
606		}
607		Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
608		for (XmppAxolotlSession session : ownSessions) {
609			Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
610			axolotlMessage.addDevice(session);
611		}
612
613		return axolotlMessage;
614	}
615
616	@Nullable
617	public XmppAxolotlMessage encrypt(Message message) {
618		XmppAxolotlMessage axolotlMessage = buildHeader(message.getContact());
619
620		if (axolotlMessage != null) {
621			final String content;
622			if (message.hasFileOnRemoteHost()) {
623				content = message.getFileParams().url.toString();
624			} else {
625				content = message.getBody();
626			}
627			try {
628				axolotlMessage.encrypt(content);
629			} catch (CryptoFailedException e) {
630				Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
631				return null;
632			}
633		}
634
635		return axolotlMessage;
636	}
637
638	public void preparePayloadMessage(final Message message, final boolean delay) {
639		executor.execute(new Runnable() {
640			@Override
641			public void run() {
642				XmppAxolotlMessage axolotlMessage = encrypt(message);
643				if (axolotlMessage == null) {
644					mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
645					//mXmppConnectionService.updateConversationUi();
646				} else {
647					Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
648					messageCache.put(message.getUuid(), axolotlMessage);
649					mXmppConnectionService.resendMessage(message, delay);
650				}
651			}
652		});
653	}
654
655	public void prepareKeyTransportMessage(final Contact contact, final OnMessageCreatedCallback onMessageCreatedCallback) {
656		executor.execute(new Runnable() {
657			@Override
658			public void run() {
659				XmppAxolotlMessage axolotlMessage = buildHeader(contact);
660				onMessageCreatedCallback.run(axolotlMessage);
661			}
662		});
663	}
664
665	public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
666		XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
667		if (axolotlMessage != null) {
668			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
669			messageCache.remove(message.getUuid());
670		} else {
671			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
672		}
673		return axolotlMessage;
674	}
675
676	private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
677		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
678		return (identityKey != null)
679				? new XmppAxolotlSession(account, axolotlStore, address,
680						identityKey.getFingerprint().replaceAll("\\s", ""))
681				: null;
682	}
683
684	private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
685		AxolotlAddress senderAddress = new AxolotlAddress(message.getFrom().toString(),
686				message.getSenderDeviceId());
687		XmppAxolotlSession session = sessions.get(senderAddress);
688		if (session == null) {
689			Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
690			session = recreateUncachedSession(senderAddress);
691			if (session == null) {
692				session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
693			}
694		}
695		return session;
696	}
697
698	public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message) {
699		XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
700
701		XmppAxolotlSession session = getReceivingSession(message);
702		try {
703			plaintextMessage = message.decrypt(session, getOwnDeviceId());
704			Integer preKeyId = session.getPreKeyId();
705			if (preKeyId != null) {
706				publishBundlesIfNeeded();
707				session.resetPreKeyId();
708			}
709		} catch (CryptoFailedException e) {
710			Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());
711		}
712
713		if (session.isFresh() && plaintextMessage != null) {
714			sessions.put(session);
715		}
716
717		return plaintextMessage;
718	}
719
720	public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message) {
721		XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage = null;
722
723		XmppAxolotlSession session = getReceivingSession(message);
724		keyTransportMessage = message.getParameters(session, getOwnDeviceId());
725
726		if (session.isFresh() && keyTransportMessage != null) {
727			sessions.put(session);
728		}
729
730		return keyTransportMessage;
731	}
732}