1package eu.siacs.conversations.crypto.axolotl;
2
3import android.os.Bundle;
4import android.security.KeyChain;
5import android.support.annotation.NonNull;
6import android.support.annotation.Nullable;
7import android.util.Log;
8import android.util.Pair;
9
10import org.bouncycastle.jce.provider.BouncyCastleProvider;
11import org.whispersystems.libsignal.SignalProtocolAddress;
12import org.whispersystems.libsignal.IdentityKey;
13import org.whispersystems.libsignal.IdentityKeyPair;
14import org.whispersystems.libsignal.InvalidKeyException;
15import org.whispersystems.libsignal.InvalidKeyIdException;
16import org.whispersystems.libsignal.SessionBuilder;
17import org.whispersystems.libsignal.UntrustedIdentityException;
18import org.whispersystems.libsignal.ecc.ECPublicKey;
19import org.whispersystems.libsignal.state.PreKeyBundle;
20import org.whispersystems.libsignal.state.PreKeyRecord;
21import org.whispersystems.libsignal.state.SignedPreKeyRecord;
22import org.whispersystems.libsignal.util.KeyHelper;
23
24import java.security.PrivateKey;
25import java.security.Security;
26import java.security.Signature;
27import java.security.cert.X509Certificate;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.Collection;
31import java.util.Collections;
32import java.util.HashMap;
33import java.util.HashSet;
34import java.util.Iterator;
35import java.util.List;
36import java.util.Map;
37import java.util.Random;
38import java.util.Set;
39import java.util.concurrent.atomic.AtomicBoolean;
40
41import eu.siacs.conversations.Config;
42import eu.siacs.conversations.entities.Account;
43import eu.siacs.conversations.entities.Contact;
44import eu.siacs.conversations.entities.Conversation;
45import eu.siacs.conversations.entities.Message;
46import eu.siacs.conversations.parser.IqParser;
47import eu.siacs.conversations.services.XmppConnectionService;
48import eu.siacs.conversations.utils.CryptoHelper;
49import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
50import eu.siacs.conversations.xml.Element;
51import eu.siacs.conversations.xml.Namespace;
52import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
53import eu.siacs.conversations.xmpp.OnIqPacketReceived;
54import eu.siacs.conversations.xmpp.jid.InvalidJidException;
55import eu.siacs.conversations.xmpp.jid.Jid;
56import eu.siacs.conversations.xmpp.pep.PublishOptions;
57import eu.siacs.conversations.xmpp.stanzas.IqPacket;
58import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
59
60public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
61
62 public static final String PEP_PREFIX = "eu.siacs.conversations.axolotl";
63 public static final String PEP_DEVICE_LIST = PEP_PREFIX + ".devicelist";
64 public static final String PEP_DEVICE_LIST_NOTIFY = PEP_DEVICE_LIST + "+notify";
65 public static final String PEP_BUNDLES = PEP_PREFIX + ".bundles";
66 public static final String PEP_VERIFICATION = PEP_PREFIX + ".verification";
67 public static final String PEP_OMEMO_WHITELISTED = PEP_PREFIX + ".whitelisted";
68
69 public static final String LOGPREFIX = "AxolotlService";
70
71 public static final int NUM_KEYS_TO_PUBLISH = 100;
72 public static final int publishTriesThreshold = 3;
73
74 private final Account account;
75 private final XmppConnectionService mXmppConnectionService;
76 private final SQLiteAxolotlStore axolotlStore;
77 private final SessionMap sessions;
78 private final Map<Jid, Set<Integer>> deviceIds;
79 private final Map<String, XmppAxolotlMessage> messageCache;
80 private final FetchStatusMap fetchStatusMap;
81 private final HashMap<Jid, List<OnDeviceIdsFetched>> fetchDeviceIdsMap = new HashMap<>();
82 private final SerialSingleThreadExecutor executor;
83 private int numPublishTriesOnEmptyPep = 0;
84 private boolean pepBroken = false;
85 private int lastDeviceListNotificationHash = 0;
86 private Set<XmppAxolotlSession> postponedSessions = new HashSet<>(); //sessions stored here will receive after mam catchup treatment
87
88 private AtomicBoolean changeAccessMode = new AtomicBoolean(false);
89
90 @Override
91 public void onAdvancedStreamFeaturesAvailable(Account account) {
92 if (Config.supportOmemo()
93 && account.getXmppConnection() != null
94 && account.getXmppConnection().getFeatures().pep()) {
95 publishBundlesIfNeeded(true, false);
96 } else {
97 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": skipping OMEMO initialization");
98 }
99 }
100
101 public boolean fetchMapHasErrors(List<Jid> jids) {
102 for (Jid jid : jids) {
103 if (deviceIds.get(jid) != null) {
104 for (Integer foreignId : this.deviceIds.get(jid)) {
105 SignalProtocolAddress address = new SignalProtocolAddress(jid.toPreppedString(), foreignId);
106 if (fetchStatusMap.getAll(address.getName()).containsValue(FetchStatus.ERROR)) {
107 return true;
108 }
109 }
110 }
111 }
112 return false;
113 }
114
115 public void preVerifyFingerprint(Contact contact, String fingerprint) {
116 axolotlStore.preVerifyFingerprint(contact.getAccount(), contact.getJid().toBareJid().toPreppedString(), fingerprint);
117 }
118
119 public void preVerifyFingerprint(Account account, String fingerprint) {
120 axolotlStore.preVerifyFingerprint(account, account.getJid().toBareJid().toPreppedString(), fingerprint);
121 }
122
123 public boolean hasVerifiedKeys(String name) {
124 for (XmppAxolotlSession session : this.sessions.getAll(name).values()) {
125 if (session.getTrust().isVerified()) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 private static class AxolotlAddressMap<T> {
133 protected Map<String, Map<Integer, T>> map;
134 protected final Object MAP_LOCK = new Object();
135
136 public AxolotlAddressMap() {
137 this.map = new HashMap<>();
138 }
139
140 public void put(SignalProtocolAddress address, T value) {
141 synchronized (MAP_LOCK) {
142 Map<Integer, T> devices = map.get(address.getName());
143 if (devices == null) {
144 devices = new HashMap<>();
145 map.put(address.getName(), devices);
146 }
147 devices.put(address.getDeviceId(), value);
148 }
149 }
150
151 public T get(SignalProtocolAddress address) {
152 synchronized (MAP_LOCK) {
153 Map<Integer, T> devices = map.get(address.getName());
154 if (devices == null) {
155 return null;
156 }
157 return devices.get(address.getDeviceId());
158 }
159 }
160
161 public Map<Integer, T> getAll(String name) {
162 synchronized (MAP_LOCK) {
163 Map<Integer, T> devices = map.get(name);
164 if (devices == null) {
165 return new HashMap<>();
166 }
167 return devices;
168 }
169 }
170
171 public boolean hasAny(SignalProtocolAddress address) {
172 synchronized (MAP_LOCK) {
173 Map<Integer, T> devices = map.get(address.getName());
174 return devices != null && !devices.isEmpty();
175 }
176 }
177
178 public void clear() {
179 map.clear();
180 }
181
182 }
183
184 private static class SessionMap extends AxolotlAddressMap<XmppAxolotlSession> {
185 private final XmppConnectionService xmppConnectionService;
186 private final Account account;
187
188 public SessionMap(XmppConnectionService service, SQLiteAxolotlStore store, Account account) {
189 super();
190 this.xmppConnectionService = service;
191 this.account = account;
192 this.fillMap(store);
193 }
194
195 private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
196 for (Integer deviceId : deviceIds) {
197 SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(bareJid, deviceId);
198 IdentityKey identityKey = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey();
199 if (Config.X509_VERIFICATION) {
200 X509Certificate certificate = store.getFingerprintCertificate(CryptoHelper.bytesToHex(identityKey.getPublicKey().serialize()));
201 if (certificate != null) {
202 Bundle information = CryptoHelper.extractCertificateInformation(certificate);
203 try {
204 final String cn = information.getString("subject_cn");
205 final Jid jid = Jid.fromString(bareJid);
206 Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
207 account.getRoster().getContact(jid).setCommonName(cn);
208 } catch (final InvalidJidException ignored) {
209 //ignored
210 }
211 }
212 }
213 this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, identityKey));
214 }
215 }
216
217 private void fillMap(SQLiteAxolotlStore store) {
218 List<Integer> deviceIds = store.getSubDeviceSessions(account.getJid().toBareJid().toPreppedString());
219 putDevicesForJid(account.getJid().toBareJid().toPreppedString(), deviceIds, store);
220 for (String address : store.getKnownAddresses()) {
221 deviceIds = store.getSubDeviceSessions(address);
222 putDevicesForJid(address, deviceIds, store);
223 }
224 }
225
226 @Override
227 public void put(SignalProtocolAddress address, XmppAxolotlSession value) {
228 super.put(address, value);
229 value.setNotFresh();
230 }
231
232 public void put(XmppAxolotlSession session) {
233 this.put(session.getRemoteAddress(), session);
234 }
235 }
236
237 public enum FetchStatus {
238 PENDING,
239 SUCCESS,
240 SUCCESS_VERIFIED,
241 TIMEOUT,
242 SUCCESS_TRUSTED,
243 ERROR
244 }
245
246 private static class FetchStatusMap extends AxolotlAddressMap<FetchStatus> {
247
248 public void clearErrorFor(Jid jid) {
249 synchronized (MAP_LOCK) {
250 Map<Integer, FetchStatus> devices = this.map.get(jid.toBareJid().toPreppedString());
251 if (devices == null) {
252 return;
253 }
254 for (Map.Entry<Integer, FetchStatus> entry : devices.entrySet()) {
255 if (entry.getValue() == FetchStatus.ERROR) {
256 Log.d(Config.LOGTAG, "resetting error for " + jid.toBareJid() + "(" + entry.getKey() + ")");
257 entry.setValue(FetchStatus.TIMEOUT);
258 }
259 }
260 }
261 }
262 }
263
264 public static String getLogprefix(Account account) {
265 return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
266 }
267
268 public AxolotlService(Account account, XmppConnectionService connectionService) {
269 if (account == null || connectionService == null) {
270 throw new IllegalArgumentException("account and service cannot be null");
271 }
272 if (Security.getProvider("BC") == null) {
273 Security.addProvider(new BouncyCastleProvider());
274 }
275 this.mXmppConnectionService = connectionService;
276 this.account = account;
277 this.axolotlStore = new SQLiteAxolotlStore(this.account, this.mXmppConnectionService);
278 this.deviceIds = new HashMap<>();
279 this.messageCache = new HashMap<>();
280 this.sessions = new SessionMap(mXmppConnectionService, axolotlStore, account);
281 this.fetchStatusMap = new FetchStatusMap();
282 this.executor = new SerialSingleThreadExecutor("Axolotl");
283 }
284
285 public String getOwnFingerprint() {
286 return CryptoHelper.bytesToHex(axolotlStore.getIdentityKeyPair().getPublicKey().serialize());
287 }
288
289 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status) {
290 return axolotlStore.getContactKeysWithTrust(account.getJid().toBareJid().toPreppedString(), status);
291 }
292
293 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status, Jid jid) {
294 return axolotlStore.getContactKeysWithTrust(jid.toBareJid().toPreppedString(), status);
295 }
296
297 public Set<IdentityKey> getKeysWithTrust(FingerprintStatus status, List<Jid> jids) {
298 Set<IdentityKey> keys = new HashSet<>();
299 for (Jid jid : jids) {
300 keys.addAll(axolotlStore.getContactKeysWithTrust(jid.toPreppedString(), status));
301 }
302 return keys;
303 }
304
305 public long getNumTrustedKeys(Jid jid) {
306 return axolotlStore.getContactNumTrustedKeys(jid.toBareJid().toPreppedString());
307 }
308
309 public boolean anyTargetHasNoTrustedKeys(List<Jid> jids) {
310 for (Jid jid : jids) {
311 if (axolotlStore.getContactNumTrustedKeys(jid.toBareJid().toPreppedString()) == 0) {
312 return true;
313 }
314 }
315 return false;
316 }
317
318 private SignalProtocolAddress getAddressForJid(Jid jid) {
319 return new SignalProtocolAddress(jid.toPreppedString(), 0);
320 }
321
322 public Collection<XmppAxolotlSession> findOwnSessions() {
323 SignalProtocolAddress ownAddress = getAddressForJid(account.getJid().toBareJid());
324 ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(ownAddress.getName()).values());
325 Collections.sort(s);
326 return s;
327 }
328
329
330 public Collection<XmppAxolotlSession> findSessionsForContact(Contact contact) {
331 SignalProtocolAddress contactAddress = getAddressForJid(contact.getJid());
332 ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(contactAddress.getName()).values());
333 Collections.sort(s);
334 return s;
335 }
336
337 private Set<XmppAxolotlSession> findSessionsForConversation(Conversation conversation) {
338 HashSet<XmppAxolotlSession> sessions = new HashSet<>();
339 for (Jid jid : conversation.getAcceptedCryptoTargets()) {
340 sessions.addAll(this.sessions.getAll(getAddressForJid(jid).getName()).values());
341 }
342 return sessions;
343 }
344
345 private boolean hasAny(Jid jid) {
346 return sessions.hasAny(getAddressForJid(jid));
347 }
348
349 public boolean isPepBroken() {
350 return this.pepBroken;
351 }
352
353 public void resetBrokenness() {
354 this.pepBroken = false;
355 this.numPublishTriesOnEmptyPep = 0;
356 this.lastDeviceListNotificationHash = 0;
357 }
358
359 public void clearErrorsInFetchStatusMap(Jid jid) {
360 fetchStatusMap.clearErrorFor(jid);
361 }
362
363 public void regenerateKeys(boolean wipeOther) {
364 axolotlStore.regenerate();
365 sessions.clear();
366 fetchStatusMap.clear();
367 fetchDeviceIdsMap.clear();
368 publishBundlesIfNeeded(true, wipeOther);
369 }
370
371 public void destroy() {
372 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": destroying old axolotl service. no longer in use");
373 mXmppConnectionService.databaseBackend.wipeAxolotlDb(account);
374 }
375
376 public AxolotlService makeNew() {
377 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": make new axolotl service");
378 return new AxolotlService(this.account, this.mXmppConnectionService);
379 }
380
381 public int getOwnDeviceId() {
382 return axolotlStore.getLocalRegistrationId();
383 }
384
385 public SignalProtocolAddress getOwnAxolotlAddress() {
386 return new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), getOwnDeviceId());
387 }
388
389 public Set<Integer> getOwnDeviceIds() {
390 return this.deviceIds.get(account.getJid().toBareJid());
391 }
392
393 public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
394 final int hash = deviceIds.hashCode();
395 final boolean me = jid.toBareJid().equals(account.getJid().toBareJid());
396 if (me) {
397 if (hash != 0 && hash == this.lastDeviceListNotificationHash) {
398 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": ignoring duplicate own device id list");
399 return;
400 }
401 this.lastDeviceListNotificationHash = hash;
402 }
403 boolean needsPublishing = me && !deviceIds.contains(getOwnDeviceId());
404 if (me) {
405 deviceIds.remove(getOwnDeviceId());
406 }
407 Set<Integer> expiredDevices = new HashSet<>(axolotlStore.getSubDeviceSessions(jid.toBareJid().toPreppedString()));
408 expiredDevices.removeAll(deviceIds);
409 for (Integer deviceId : expiredDevices) {
410 SignalProtocolAddress address = new SignalProtocolAddress(jid.toBareJid().toPreppedString(), deviceId);
411 XmppAxolotlSession session = sessions.get(address);
412 if (session != null && session.getFingerprint() != null) {
413 if (session.getTrust().isActive()) {
414 session.setTrust(session.getTrust().toInactive());
415 }
416 }
417 }
418 Set<Integer> newDevices = new HashSet<>(deviceIds);
419 for (Integer deviceId : newDevices) {
420 SignalProtocolAddress address = new SignalProtocolAddress(jid.toBareJid().toPreppedString(), deviceId);
421 XmppAxolotlSession session = sessions.get(address);
422 if (session != null && session.getFingerprint() != null) {
423 if (!session.getTrust().isActive()) {
424 Log.d(Config.LOGTAG, "reactivating device with fingerprint " + session.getFingerprint());
425 session.setTrust(session.getTrust().toActive());
426 }
427 }
428 }
429 if (me) {
430 if (Config.OMEMO_AUTO_EXPIRY != 0) {
431 needsPublishing |= deviceIds.removeAll(getExpiredDevices());
432 }
433 needsPublishing |= this.changeAccessMode.get();
434 for (Integer deviceId : deviceIds) {
435 SignalProtocolAddress ownDeviceAddress = new SignalProtocolAddress(jid.toBareJid().toPreppedString(), deviceId);
436 if (sessions.get(ownDeviceAddress) == null) {
437 FetchStatus status = fetchStatusMap.get(ownDeviceAddress);
438 if (status == null || status == FetchStatus.TIMEOUT) {
439 fetchStatusMap.put(ownDeviceAddress, FetchStatus.PENDING);
440 this.buildSessionFromPEP(ownDeviceAddress);
441 }
442 }
443 }
444 if (needsPublishing) {
445 publishOwnDeviceId(deviceIds);
446 }
447 }
448 this.deviceIds.put(jid, deviceIds);
449 mXmppConnectionService.updateConversationUi(); //update the lock icon
450 mXmppConnectionService.keyStatusUpdated(null);
451 }
452
453 public void wipeOtherPepDevices() {
454 if (pepBroken) {
455 Log.d(Config.LOGTAG, getLogprefix(account) + "wipeOtherPepDevices called, but PEP is broken. Ignoring... ");
456 return;
457 }
458 Set<Integer> deviceIds = new HashSet<>();
459 deviceIds.add(getOwnDeviceId());
460 publishDeviceIdsAndRefineAccessModel(deviceIds);
461 }
462
463 public void distrustFingerprint(final String fingerprint) {
464 final String fp = fingerprint.replaceAll("\\s", "");
465 final FingerprintStatus fingerprintStatus = axolotlStore.getFingerprintStatus(fp);
466 axolotlStore.setFingerprintStatus(fp, fingerprintStatus.toUntrusted());
467 }
468
469 public void publishOwnDeviceIdIfNeeded() {
470 if (pepBroken) {
471 Log.d(Config.LOGTAG, getLogprefix(account) + "publishOwnDeviceIdIfNeeded called, but PEP is broken. Ignoring... ");
472 return;
473 }
474 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(account.getJid().toBareJid());
475 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
476 @Override
477 public void onIqPacketReceived(Account account, IqPacket packet) {
478 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
479 Log.d(Config.LOGTAG, getLogprefix(account) + "Timeout received while retrieving own Device Ids.");
480 } else {
481 Element item = mXmppConnectionService.getIqParser().getItem(packet);
482 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
483 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": retrieved own device list: " + deviceIds);
484 registerDevices(account.getJid().toBareJid(), deviceIds);
485 }
486 }
487 });
488 }
489
490 private Set<Integer> getExpiredDevices() {
491 Set<Integer> devices = new HashSet<>();
492 for (XmppAxolotlSession session : findOwnSessions()) {
493 if (session.getTrust().isActive()) {
494 long diff = System.currentTimeMillis() - session.getTrust().getLastActivation();
495 if (diff > Config.OMEMO_AUTO_EXPIRY) {
496 long lastMessageDiff = System.currentTimeMillis() - mXmppConnectionService.databaseBackend.getLastTimeFingerprintUsed(account, session.getFingerprint());
497 long hours = Math.round(lastMessageDiff / (1000 * 60.0 * 60.0));
498 if (lastMessageDiff > Config.OMEMO_AUTO_EXPIRY) {
499 devices.add(session.getRemoteAddress().getDeviceId());
500 session.setTrust(session.getTrust().toInactive());
501 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": added own device " + session.getFingerprint() + " to list of expired devices. Last message received " + hours + " hours ago");
502 } else {
503 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": own device " + session.getFingerprint() + " was active " + hours + " hours ago");
504 }
505 }
506 }
507 }
508 return devices;
509 }
510
511 public void publishOwnDeviceId(Set<Integer> deviceIds) {
512 Set<Integer> deviceIdsCopy = new HashSet<>(deviceIds);
513 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "publishing own device ids");
514 if (deviceIdsCopy.isEmpty()) {
515 if (numPublishTriesOnEmptyPep >= publishTriesThreshold) {
516 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device publish attempt threshold exceeded, aborting...");
517 pepBroken = true;
518 return;
519 } else {
520 numPublishTriesOnEmptyPep++;
521 Log.w(Config.LOGTAG, getLogprefix(account) + "Own device list empty, attempting to publish (try " + numPublishTriesOnEmptyPep + ")");
522 }
523 } else {
524 numPublishTriesOnEmptyPep = 0;
525 }
526 deviceIdsCopy.add(getOwnDeviceId());
527 publishDeviceIdsAndRefineAccessModel(deviceIdsCopy);
528 }
529
530 private void publishDeviceIdsAndRefineAccessModel(Set<Integer> ids) {
531 publishDeviceIdsAndRefineAccessModel(ids, true);
532 }
533
534 private void publishDeviceIdsAndRefineAccessModel(final Set<Integer> ids, final boolean firstAttempt) {
535 final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
536 IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(ids, publishOptions);
537 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
538 @Override
539 public void onIqPacketReceived(Account account, IqPacket packet) {
540 Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
541 if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
542 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for device list. pushing node configuration");
543 mXmppConnectionService.pushNodeConfiguration(account, AxolotlService.PEP_DEVICE_LIST, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
544 @Override
545 public void onPushSucceeded() {
546 publishDeviceIdsAndRefineAccessModel(ids, false);
547 }
548
549 @Override
550 public void onPushFailed() {
551 publishDeviceIdsAndRefineAccessModel(ids, false);
552 }
553 });
554 } else {
555 if (AxolotlService.this.changeAccessMode.compareAndSet(true, false)) {
556 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": done changing access mode");
557 account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, false);
558 mXmppConnectionService.databaseBackend.updateAccount(account);
559 }
560 if (packet.getType() == IqPacket.TYPE.ERROR) {
561 pepBroken = true;
562 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing own device id" + packet.findChild("error"));
563 }
564 }
565 }
566 });
567 }
568
569 public void publishDeviceVerificationAndBundle(final SignedPreKeyRecord signedPreKeyRecord,
570 final Set<PreKeyRecord> preKeyRecords,
571 final boolean announceAfter,
572 final boolean wipe) {
573 try {
574 IdentityKey axolotlPublicKey = axolotlStore.getIdentityKeyPair().getPublicKey();
575 PrivateKey x509PrivateKey = KeyChain.getPrivateKey(mXmppConnectionService, account.getPrivateKeyAlias());
576 X509Certificate[] chain = KeyChain.getCertificateChain(mXmppConnectionService, account.getPrivateKeyAlias());
577 Signature verifier = Signature.getInstance("sha256WithRSA");
578 verifier.initSign(x509PrivateKey, mXmppConnectionService.getRNG());
579 verifier.update(axolotlPublicKey.serialize());
580 byte[] signature = verifier.sign();
581 IqPacket packet = mXmppConnectionService.getIqGenerator().publishVerification(signature, chain, getOwnDeviceId());
582 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": publish verification for device " + getOwnDeviceId());
583 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
584 @Override
585 public void onIqPacketReceived(final Account account, IqPacket packet) {
586 String node = AxolotlService.PEP_VERIFICATION + ":" + getOwnDeviceId();
587 mXmppConnectionService.pushNodeConfiguration(account, node, PublishOptions.openAccess(), new XmppConnectionService.OnConfigurationPushed() {
588 @Override
589 public void onPushSucceeded() {
590 Log.d(Config.LOGTAG, getLogprefix(account) + "configured verification node to be world readable");
591 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
592 }
593
594 @Override
595 public void onPushFailed() {
596 Log.d(Config.LOGTAG, getLogprefix(account) + "unable to set access model on verification node");
597 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe);
598 }
599 });
600 }
601 });
602 } catch (Exception e) {
603 e.printStackTrace();
604 }
605 }
606
607 public void publishBundlesIfNeeded(final boolean announce, final boolean wipe) {
608 if (pepBroken) {
609 Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
610 return;
611 }
612
613 if (account.getXmppConnection().getFeatures().pepPublishOptions()) {
614 this.changeAccessMode.set(account.isOptionSet(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE));
615 } else {
616 if (account.setOption(Account.OPTION_REQUIRES_ACCESS_MODE_CHANGE, true)) {
617 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server doesn’t support publish-options. setting for later access mode change");
618 mXmppConnectionService.databaseBackend.updateAccount(account);
619 }
620 }
621 if (this.changeAccessMode.get()) {
622 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server gained publish-options capabilities. changing access model");
623 }
624 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(account.getJid().toBareJid(), getOwnDeviceId());
625 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
626 @Override
627 public void onIqPacketReceived(Account account, IqPacket packet) {
628
629 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
630 return; //ignore timeout. do nothing
631 }
632
633 if (packet.getType() == IqPacket.TYPE.ERROR) {
634 Element error = packet.findChild("error");
635 if (error == null || !error.hasChild("item-not-found")) {
636 pepBroken = true;
637 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "request for device bundles came back with something other than item-not-found" + packet);
638 return;
639 }
640 }
641
642 PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
643 Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
644 boolean flush = false;
645 if (bundle == null) {
646 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
647 bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
648 flush = true;
649 }
650 if (keys == null) {
651 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
652 }
653 try {
654 boolean changed = false;
655 // Validate IdentityKey
656 IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
657 if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
658 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
659 changed = true;
660 }
661
662 // Validate signedPreKeyRecord + ID
663 SignedPreKeyRecord signedPreKeyRecord;
664 int numSignedPreKeys = axolotlStore.getSignedPreKeysCount();
665 try {
666 signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
667 if (flush
668 || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
669 || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
670 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
671 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
672 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
673 changed = true;
674 }
675 } catch (InvalidKeyIdException e) {
676 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
677 signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
678 axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
679 changed = true;
680 }
681
682 // Validate PreKeys
683 Set<PreKeyRecord> preKeyRecords = new HashSet<>();
684 if (keys != null) {
685 for (Integer id : keys.keySet()) {
686 try {
687 PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
688 if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
689 preKeyRecords.add(preKeyRecord);
690 }
691 } catch (InvalidKeyIdException ignored) {
692 }
693 }
694 }
695 int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
696 if (newKeys > 0) {
697 List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
698 axolotlStore.getCurrentPreKeyId() + 1, newKeys);
699 preKeyRecords.addAll(newRecords);
700 for (PreKeyRecord record : newRecords) {
701 axolotlStore.storePreKey(record.getId(), record);
702 }
703 changed = true;
704 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
705 }
706
707
708 if (changed || changeAccessMode.get()) {
709 if (account.getPrivateKeyAlias() != null && Config.X509_VERIFICATION) {
710 mXmppConnectionService.publishDisplayName(account);
711 publishDeviceVerificationAndBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
712 } else {
713 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announce, wipe);
714 }
715 } else {
716 Log.d(Config.LOGTAG, getLogprefix(account) + "Bundle " + getOwnDeviceId() + " in PEP was current");
717 if (wipe) {
718 wipeOtherPepDevices();
719 } else if (announce) {
720 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
721 publishOwnDeviceIdIfNeeded();
722 }
723 }
724 } catch (InvalidKeyException e) {
725 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
726 }
727 }
728 });
729 }
730
731 private void publishDeviceBundle(SignedPreKeyRecord signedPreKeyRecord,
732 Set<PreKeyRecord> preKeyRecords,
733 final boolean announceAfter,
734 final boolean wipe) {
735 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, true);
736 }
737
738 private void publishDeviceBundle(final SignedPreKeyRecord signedPreKeyRecord,
739 final Set<PreKeyRecord> preKeyRecords,
740 final boolean announceAfter,
741 final boolean wipe,
742 final boolean firstAttempt) {
743 final Bundle publishOptions = account.getXmppConnection().getFeatures().pepPublishOptions() ? PublishOptions.openAccess() : null;
744 IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
745 signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
746 preKeyRecords, getOwnDeviceId(), publishOptions);
747 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing...");
748 mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
749 @Override
750 public void onIqPacketReceived(final Account account, IqPacket packet) {
751 Element error = packet.getType() == IqPacket.TYPE.ERROR ? packet.findChild("error") : null;
752 if (firstAttempt && error != null && error.hasChild("precondition-not-met", Namespace.PUBSUB_ERROR)) {
753 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": precondition wasn't met for bundle. pushing node configuration");
754 final String node = AxolotlService.PEP_BUNDLES + ":" + getOwnDeviceId();
755 mXmppConnectionService.pushNodeConfiguration(account, node, publishOptions, new XmppConnectionService.OnConfigurationPushed() {
756 @Override
757 public void onPushSucceeded() {
758 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
759 }
760
761 @Override
762 public void onPushFailed() {
763 publishDeviceBundle(signedPreKeyRecord, preKeyRecords, announceAfter, wipe, false);
764 }
765 });
766 } else if (packet.getType() == IqPacket.TYPE.RESULT) {
767 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Successfully published bundle. ");
768 if (wipe) {
769 wipeOtherPepDevices();
770 } else if (announceAfter) {
771 Log.d(Config.LOGTAG, getLogprefix(account) + "Announcing device " + getOwnDeviceId());
772 publishOwnDeviceIdIfNeeded();
773 }
774 } else if (packet.getType() == IqPacket.TYPE.ERROR) {
775 pepBroken = true;
776 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing bundle: " + packet.findChild("error"));
777 }
778 }
779 });
780 }
781
782 public enum AxolotlCapability {
783 FULL,
784 MISSING_PRESENCE,
785 MISSING_KEYS,
786 WRONG_CONFIGURATION,
787 NO_MEMBERS
788 }
789
790 public boolean isConversationAxolotlCapable(Conversation conversation) {
791 return conversation.isSingleOrPrivateAndNonAnonymous();
792 }
793
794 public Pair<AxolotlCapability, Jid> isConversationAxolotlCapableDetailed(Conversation conversation) {
795 if (conversation.isSingleOrPrivateAndNonAnonymous()) {
796 final List<Jid> jids = getCryptoTargets(conversation);
797 for (Jid jid : jids) {
798 if (!hasAny(jid) && (!deviceIds.containsKey(jid) || deviceIds.get(jid).isEmpty())) {
799 if (conversation.getAccount().getRoster().getContact(jid).mutualPresenceSubscription()) {
800 return new Pair<>(AxolotlCapability.MISSING_KEYS, jid);
801 } else {
802 return new Pair<>(AxolotlCapability.MISSING_PRESENCE, jid);
803 }
804 }
805 }
806 if (jids.size() > 0) {
807 return new Pair<>(AxolotlCapability.FULL, null);
808 } else {
809 return new Pair<>(AxolotlCapability.NO_MEMBERS, null);
810 }
811 } else {
812 return new Pair<>(AxolotlCapability.WRONG_CONFIGURATION, null);
813 }
814 }
815
816 public List<Jid> getCryptoTargets(Conversation conversation) {
817 final List<Jid> jids;
818 if (conversation.getMode() == Conversation.MODE_SINGLE) {
819 jids = new ArrayList<>();
820 jids.add(conversation.getJid().toBareJid());
821 } else {
822 jids = conversation.getMucOptions().getMembers();
823 }
824 return jids;
825 }
826
827 public FingerprintStatus getFingerprintTrust(String fingerprint) {
828 return axolotlStore.getFingerprintStatus(fingerprint);
829 }
830
831 public X509Certificate getFingerprintCertificate(String fingerprint) {
832 return axolotlStore.getFingerprintCertificate(fingerprint);
833 }
834
835 public void setFingerprintTrust(String fingerprint, FingerprintStatus status) {
836 axolotlStore.setFingerprintStatus(fingerprint, status);
837 }
838
839 private void verifySessionWithPEP(final XmppAxolotlSession session) {
840 Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
841 final SignalProtocolAddress address = session.getRemoteAddress();
842 final IdentityKey identityKey = session.getIdentityKey();
843 try {
844 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
845 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
846 @Override
847 public void onIqPacketReceived(Account account, IqPacket packet) {
848 Pair<X509Certificate[], byte[]> verification = mXmppConnectionService.getIqParser().verification(packet);
849 if (verification != null) {
850 try {
851 Signature verifier = Signature.getInstance("sha256WithRSA");
852 verifier.initVerify(verification.first[0]);
853 verifier.update(identityKey.serialize());
854 if (verifier.verify(verification.second)) {
855 try {
856 mXmppConnectionService.getMemorizingTrustManager().getNonInteractive().checkClientTrusted(verification.first, "RSA");
857 String fingerprint = session.getFingerprint();
858 Log.d(Config.LOGTAG, "verified session with x.509 signature. fingerprint was: " + fingerprint);
859 setFingerprintTrust(fingerprint, FingerprintStatus.createActiveVerified(true));
860 axolotlStore.setFingerprintCertificate(fingerprint, verification.first[0]);
861 fetchStatusMap.put(address, FetchStatus.SUCCESS_VERIFIED);
862 Bundle information = CryptoHelper.extractCertificateInformation(verification.first[0]);
863 try {
864 final String cn = information.getString("subject_cn");
865 final Jid jid = Jid.fromString(address.getName());
866 Log.d(Config.LOGTAG, "setting common name for " + jid + " to " + cn);
867 account.getRoster().getContact(jid).setCommonName(cn);
868 } catch (final InvalidJidException ignored) {
869 //ignored
870 }
871 finishBuildingSessionsFromPEP(address);
872 return;
873 } catch (Exception e) {
874 Log.d(Config.LOGTAG, "could not verify certificate");
875 }
876 }
877 } catch (Exception e) {
878 Log.d(Config.LOGTAG, "error during verification " + e.getMessage());
879 }
880 } else {
881 Log.d(Config.LOGTAG, "no verification found");
882 }
883 fetchStatusMap.put(address, FetchStatus.SUCCESS);
884 finishBuildingSessionsFromPEP(address);
885 }
886 });
887 } catch (InvalidJidException e) {
888 fetchStatusMap.put(address, FetchStatus.SUCCESS);
889 finishBuildingSessionsFromPEP(address);
890 }
891 }
892
893 private final Set<Integer> PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT = new HashSet<>();
894
895 private void finishBuildingSessionsFromPEP(final SignalProtocolAddress address) {
896 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), 0);
897 Map<Integer, FetchStatus> own = fetchStatusMap.getAll(ownAddress.getName());
898 Map<Integer, FetchStatus> remote = fetchStatusMap.getAll(address.getName());
899 if (!own.containsValue(FetchStatus.PENDING) && !remote.containsValue(FetchStatus.PENDING)) {
900 FetchStatus report = null;
901 if (own.containsValue(FetchStatus.SUCCESS) || remote.containsValue(FetchStatus.SUCCESS)) {
902 report = FetchStatus.SUCCESS;
903 } else if (own.containsValue(FetchStatus.SUCCESS_VERIFIED) || remote.containsValue(FetchStatus.SUCCESS_VERIFIED)) {
904 report = FetchStatus.SUCCESS_VERIFIED;
905 } else if (own.containsValue(FetchStatus.SUCCESS_TRUSTED) || remote.containsValue(FetchStatus.SUCCESS_TRUSTED)) {
906 report = FetchStatus.SUCCESS_TRUSTED;
907 } else if (own.containsValue(FetchStatus.ERROR) || remote.containsValue(FetchStatus.ERROR)) {
908 report = FetchStatus.ERROR;
909 }
910 mXmppConnectionService.keyStatusUpdated(report);
911 }
912 if (Config.REMOVE_BROKEN_DEVICES) {
913 Set<Integer> ownDeviceIds = new HashSet<>(getOwnDeviceIds());
914 boolean publish = false;
915 for (Map.Entry<Integer, FetchStatus> entry : own.entrySet()) {
916 int id = entry.getKey();
917 if (entry.getValue() == FetchStatus.ERROR && PREVIOUSLY_REMOVED_FROM_ANNOUNCEMENT.add(id) && ownDeviceIds.remove(id)) {
918 publish = true;
919 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": error fetching own device with id " + id + ". removing from announcement");
920 }
921 }
922 if (publish) {
923 publishOwnDeviceId(ownDeviceIds);
924 }
925 }
926 }
927
928 public boolean hasEmptyDeviceList(Jid jid) {
929 return !hasAny(jid) && (!deviceIds.containsKey(jid) || deviceIds.get(jid).isEmpty());
930 }
931
932 public interface OnDeviceIdsFetched {
933 void fetched(Jid jid, Set<Integer> deviceIds);
934 }
935
936 public interface OnMultipleDeviceIdFetched {
937 void fetched();
938 }
939
940 public void fetchDeviceIds(final Jid jid) {
941 fetchDeviceIds(jid, null);
942 }
943
944 public void fetchDeviceIds(final Jid jid, OnDeviceIdsFetched callback) {
945 synchronized (this.fetchDeviceIdsMap) {
946 List<OnDeviceIdsFetched> callbacks = this.fetchDeviceIdsMap.get(jid);
947 if (callbacks != null) {
948 if (callback != null) {
949 callbacks.add(callback);
950 }
951 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching device ids for " + jid + " already running. adding callback");
952 } else {
953 callbacks = new ArrayList<>();
954 if (callback != null) {
955 callbacks.add(callback);
956 }
957 this.fetchDeviceIdsMap.put(jid, callbacks);
958 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching device ids for " + jid);
959 IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveDeviceIds(jid);
960 mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
961 @Override
962 public void onIqPacketReceived(Account account, IqPacket packet) {
963 synchronized (fetchDeviceIdsMap) {
964 List<OnDeviceIdsFetched> callbacks = fetchDeviceIdsMap.remove(jid);
965 if (packet.getType() == IqPacket.TYPE.RESULT) {
966 Element item = mXmppConnectionService.getIqParser().getItem(packet);
967 Set<Integer> deviceIds = mXmppConnectionService.getIqParser().deviceIds(item);
968 registerDevices(jid, deviceIds);
969 if (callbacks != null) {
970 for (OnDeviceIdsFetched callback : callbacks) {
971 callback.fetched(jid, deviceIds);
972 }
973 }
974 } else {
975 Log.d(Config.LOGTAG, packet.toString());
976 if (callbacks != null) {
977 for (OnDeviceIdsFetched callback : callbacks) {
978 callback.fetched(jid, null);
979 }
980 }
981 }
982 }
983 }
984 });
985 }
986 }
987 }
988
989 private void fetchDeviceIds(List<Jid> jids, final OnMultipleDeviceIdFetched callback) {
990 final ArrayList<Jid> unfinishedJids = new ArrayList<>(jids);
991 synchronized (unfinishedJids) {
992 for (Jid jid : unfinishedJids) {
993 fetchDeviceIds(jid, new OnDeviceIdsFetched() {
994 @Override
995 public void fetched(Jid jid, Set<Integer> deviceIds) {
996 synchronized (unfinishedJids) {
997 unfinishedJids.remove(jid);
998 if (unfinishedJids.size() == 0 && callback != null) {
999 callback.fetched();
1000 }
1001 }
1002 }
1003 });
1004 }
1005 }
1006 }
1007
1008 private void buildSessionFromPEP(final SignalProtocolAddress address) {
1009 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building new session for " + address.toString());
1010 if (address.equals(getOwnAxolotlAddress())) {
1011 throw new AssertionError("We should NEVER build a session with ourselves. What happened here?!");
1012 }
1013
1014 try {
1015 IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
1016 Jid.fromString(address.getName()), address.getDeviceId());
1017 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
1018 mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
1019
1020 @Override
1021 public void onIqPacketReceived(Account account, IqPacket packet) {
1022 if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
1023 fetchStatusMap.put(address, FetchStatus.TIMEOUT);
1024 } else if (packet.getType() == IqPacket.TYPE.RESULT) {
1025 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
1026 final IqParser parser = mXmppConnectionService.getIqParser();
1027 final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
1028 final PreKeyBundle bundle = parser.bundle(packet);
1029 if (preKeyBundleList.isEmpty() || bundle == null) {
1030 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
1031 fetchStatusMap.put(address, FetchStatus.ERROR);
1032 finishBuildingSessionsFromPEP(address);
1033 return;
1034 }
1035 Random random = new Random();
1036 final PreKeyBundle preKey = preKeyBundleList.get(random.nextInt(preKeyBundleList.size()));
1037 if (preKey == null) {
1038 //should never happen
1039 fetchStatusMap.put(address, FetchStatus.ERROR);
1040 finishBuildingSessionsFromPEP(address);
1041 return;
1042 }
1043
1044 final PreKeyBundle preKeyBundle = new PreKeyBundle(0, address.getDeviceId(),
1045 preKey.getPreKeyId(), preKey.getPreKey(),
1046 bundle.getSignedPreKeyId(), bundle.getSignedPreKey(),
1047 bundle.getSignedPreKeySignature(), bundle.getIdentityKey());
1048
1049 try {
1050 SessionBuilder builder = new SessionBuilder(axolotlStore, address);
1051 builder.process(preKeyBundle);
1052 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
1053 sessions.put(address, session);
1054 if (Config.X509_VERIFICATION) {
1055 verifySessionWithPEP(session);
1056 } else {
1057 FingerprintStatus status = getFingerprintTrust(CryptoHelper.bytesToHex(bundle.getIdentityKey().getPublicKey().serialize()));
1058 FetchStatus fetchStatus;
1059 if (status != null && status.isVerified()) {
1060 fetchStatus = FetchStatus.SUCCESS_VERIFIED;
1061 } else if (status != null && status.isTrusted()) {
1062 fetchStatus = FetchStatus.SUCCESS_TRUSTED;
1063 } else {
1064 fetchStatus = FetchStatus.SUCCESS;
1065 }
1066 fetchStatusMap.put(address, fetchStatus);
1067 finishBuildingSessionsFromPEP(address);
1068 }
1069 } catch (UntrustedIdentityException | InvalidKeyException e) {
1070 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
1071 + e.getClass().getName() + ", " + e.getMessage());
1072 fetchStatusMap.put(address, FetchStatus.ERROR);
1073 finishBuildingSessionsFromPEP(address);
1074 }
1075 } else {
1076 fetchStatusMap.put(address, FetchStatus.ERROR);
1077 Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while building session:" + packet.findChild("error"));
1078 finishBuildingSessionsFromPEP(address);
1079 }
1080 }
1081 });
1082 } catch (InvalidJidException e) {
1083 Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
1084 }
1085 }
1086
1087 public Set<SignalProtocolAddress> findDevicesWithoutSession(final Conversation conversation) {
1088 Set<SignalProtocolAddress> addresses = new HashSet<>();
1089 for (Jid jid : getCryptoTargets(conversation)) {
1090 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + jid);
1091 if (deviceIds.get(jid) != null) {
1092 for (Integer foreignId : this.deviceIds.get(jid)) {
1093 SignalProtocolAddress address = new SignalProtocolAddress(jid.toPreppedString(), foreignId);
1094 if (sessions.get(address) == null) {
1095 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1096 if (identityKey != null) {
1097 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1098 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1099 sessions.put(address, session);
1100 } else {
1101 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + jid + ":" + foreignId);
1102 if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1103 addresses.add(address);
1104 } else {
1105 Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1106 }
1107 }
1108 }
1109 }
1110 } else {
1111 mXmppConnectionService.keyStatusUpdated(FetchStatus.ERROR);
1112 Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
1113 }
1114 }
1115 if (deviceIds.get(account.getJid().toBareJid()) != null) {
1116 for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
1117 SignalProtocolAddress address = new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), ownId);
1118 if (sessions.get(address) == null) {
1119 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1120 if (identityKey != null) {
1121 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
1122 XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
1123 sessions.put(address, session);
1124 } else {
1125 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
1126 if (fetchStatusMap.get(address) != FetchStatus.ERROR) {
1127 addresses.add(address);
1128 } else {
1129 Log.d(Config.LOGTAG, getLogprefix(account) + "skipping over " + address + " because it's broken");
1130 }
1131 }
1132 }
1133 }
1134 }
1135
1136 return addresses;
1137 }
1138
1139 public boolean createSessionsIfNeeded(final Conversation conversation) {
1140 final List<Jid> jidsWithEmptyDeviceList = getCryptoTargets(conversation);
1141 for (Iterator<Jid> iterator = jidsWithEmptyDeviceList.iterator(); iterator.hasNext(); ) {
1142 final Jid jid = iterator.next();
1143 if (!hasEmptyDeviceList(jid)) {
1144 iterator.remove();
1145 }
1146 }
1147 Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": createSessionsIfNeeded() - jids with empty device list: " + jidsWithEmptyDeviceList);
1148 if (jidsWithEmptyDeviceList.size() > 0) {
1149 fetchDeviceIds(jidsWithEmptyDeviceList, new OnMultipleDeviceIdFetched() {
1150 @Override
1151 public void fetched() {
1152 createSessionsIfNeededActual(conversation);
1153 }
1154 });
1155 return true;
1156 } else {
1157 return createSessionsIfNeededActual(conversation);
1158 }
1159 }
1160
1161 private boolean createSessionsIfNeededActual(final Conversation conversation) {
1162 Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Creating axolotl sessions if needed...");
1163 boolean newSessions = false;
1164 Set<SignalProtocolAddress> addresses = findDevicesWithoutSession(conversation);
1165 for (SignalProtocolAddress address : addresses) {
1166 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
1167 FetchStatus status = fetchStatusMap.get(address);
1168 if (status == null || status == FetchStatus.TIMEOUT) {
1169 fetchStatusMap.put(address, FetchStatus.PENDING);
1170 this.buildSessionFromPEP(address);
1171 newSessions = true;
1172 } else if (status == FetchStatus.PENDING) {
1173 newSessions = true;
1174 } else {
1175 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
1176 }
1177 }
1178
1179 return newSessions;
1180 }
1181
1182 public boolean trustedSessionVerified(final Conversation conversation) {
1183 Set<XmppAxolotlSession> sessions = findSessionsForConversation(conversation);
1184 sessions.addAll(findOwnSessions());
1185 boolean verified = false;
1186 for (XmppAxolotlSession session : sessions) {
1187 if (session.getTrust().isTrustedAndActive()) {
1188 if (session.getTrust().getTrust() == FingerprintStatus.Trust.VERIFIED_X509) {
1189 verified = true;
1190 } else {
1191 return false;
1192 }
1193 }
1194 }
1195 return verified;
1196 }
1197
1198 public boolean hasPendingKeyFetches(Account account, List<Jid> jids) {
1199 SignalProtocolAddress ownAddress = new SignalProtocolAddress(account.getJid().toBareJid().toPreppedString(), 0);
1200 if (fetchStatusMap.getAll(ownAddress.getName()).containsValue(FetchStatus.PENDING)) {
1201 return true;
1202 }
1203 synchronized (this.fetchDeviceIdsMap) {
1204 for (Jid jid : jids) {
1205 SignalProtocolAddress foreignAddress = new SignalProtocolAddress(jid.toBareJid().toPreppedString(), 0);
1206 if (fetchStatusMap.getAll(foreignAddress.getName()).containsValue(FetchStatus.PENDING) || this.fetchDeviceIdsMap.containsKey(jid)) {
1207 return true;
1208 }
1209 }
1210 }
1211 return false;
1212 }
1213
1214 @Nullable
1215 private boolean buildHeader(XmppAxolotlMessage axolotlMessage, Conversation c) {
1216 Set<XmppAxolotlSession> remoteSessions = findSessionsForConversation(c);
1217 final boolean acceptEmpty = c.getMode() == Conversation.MODE_MULTI && c.getMucOptions().getUserCount() == 0;
1218 Collection<XmppAxolotlSession> ownSessions = findOwnSessions();
1219 if (remoteSessions.isEmpty() && !acceptEmpty) {
1220 return false;
1221 }
1222 for (XmppAxolotlSession session : remoteSessions) {
1223 axolotlMessage.addDevice(session);
1224 }
1225 for (XmppAxolotlSession session : ownSessions) {
1226 axolotlMessage.addDevice(session);
1227 }
1228
1229 return true;
1230 }
1231
1232 @Nullable
1233 public XmppAxolotlMessage encrypt(Message message) {
1234 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().toBareJid(), getOwnDeviceId());
1235 final String content;
1236 if (message.hasFileOnRemoteHost()) {
1237 content = message.getFileParams().url.toString();
1238 } else {
1239 content = message.getBody();
1240 }
1241 try {
1242 axolotlMessage.encrypt(content);
1243 } catch (CryptoFailedException e) {
1244 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to encrypt message: " + e.getMessage());
1245 return null;
1246 }
1247 //TODO: fix this for MUC PMs - Don't encrypt to all participants
1248 if (!buildHeader(axolotlMessage, message.getConversation())) {
1249 return null;
1250 }
1251
1252 return axolotlMessage;
1253 }
1254
1255 public void preparePayloadMessage(final Message message, final boolean delay) {
1256 executor.execute(new Runnable() {
1257 @Override
1258 public void run() {
1259 XmppAxolotlMessage axolotlMessage = encrypt(message);
1260 if (axolotlMessage == null) {
1261 mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
1262 //mXmppConnectionService.updateConversationUi();
1263 } else {
1264 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
1265 messageCache.put(message.getUuid(), axolotlMessage);
1266 mXmppConnectionService.resendMessage(message, delay);
1267 }
1268 }
1269 });
1270 }
1271
1272 public void prepareKeyTransportMessage(final Conversation conversation, final OnMessageCreatedCallback onMessageCreatedCallback) {
1273 executor.execute(new Runnable() {
1274 @Override
1275 public void run() {
1276 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().toBareJid(), getOwnDeviceId());
1277 if (buildHeader(axolotlMessage, conversation)) {
1278 onMessageCreatedCallback.run(axolotlMessage);
1279 } else {
1280 onMessageCreatedCallback.run(null);
1281 }
1282 }
1283 });
1284 }
1285
1286 public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
1287 XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
1288 if (axolotlMessage != null) {
1289 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
1290 messageCache.remove(message.getUuid());
1291 } else {
1292 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
1293 }
1294 return axolotlMessage;
1295 }
1296
1297 private XmppAxolotlSession recreateUncachedSession(SignalProtocolAddress address) {
1298 IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
1299 return (identityKey != null)
1300 ? new XmppAxolotlSession(account, axolotlStore, address, identityKey)
1301 : null;
1302 }
1303
1304 private XmppAxolotlSession getReceivingSession(XmppAxolotlMessage message) {
1305 SignalProtocolAddress senderAddress = new SignalProtocolAddress(message.getFrom().toPreppedString(),
1306 message.getSenderDeviceId());
1307 XmppAxolotlSession session = sessions.get(senderAddress);
1308 if (session == null) {
1309 Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
1310 session = recreateUncachedSession(senderAddress);
1311 if (session == null) {
1312 session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
1313 }
1314 }
1315 return session;
1316 }
1317
1318 public XmppAxolotlMessage.XmppAxolotlPlaintextMessage processReceivingPayloadMessage(XmppAxolotlMessage message, boolean postponePreKeyMessageHandling) {
1319 XmppAxolotlMessage.XmppAxolotlPlaintextMessage plaintextMessage = null;
1320
1321 XmppAxolotlSession session = getReceivingSession(message);
1322 try {
1323 plaintextMessage = message.decrypt(session, getOwnDeviceId());
1324 Integer preKeyId = session.getPreKeyIdAndReset();
1325 if (preKeyId != null) {
1326 postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling);
1327 }
1328 } catch (CryptoFailedException e) {
1329 Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message from " + message.getFrom() + ": " + e.getMessage());
1330 }
1331
1332 if (session.isFresh() && plaintextMessage != null) {
1333 putFreshSession(session);
1334 }
1335
1336 return plaintextMessage;
1337 }
1338
1339 private void postPreKeyMessageHandling(final XmppAxolotlSession session, int preKeyId, final boolean postpone) {
1340 if (postpone) {
1341 postponedSessions.add(session);
1342 } else {
1343 //TODO: do not republish if we already removed this preKeyId
1344 publishBundlesIfNeeded(false, false);
1345 completeSession(session);
1346 }
1347 }
1348
1349 public void processPostponed() {
1350 if (postponedSessions.size() > 0) {
1351 publishBundlesIfNeeded(false, false);
1352 }
1353 Iterator<XmppAxolotlSession> iterator = postponedSessions.iterator();
1354 while (iterator.hasNext()) {
1355 completeSession(iterator.next());
1356 iterator.remove();
1357 }
1358 }
1359
1360 private void completeSession(XmppAxolotlSession session) {
1361 final XmppAxolotlMessage axolotlMessage = new XmppAxolotlMessage(account.getJid().toBareJid(), getOwnDeviceId());
1362 axolotlMessage.addDevice(session);
1363 try {
1364 Jid jid = Jid.fromString(session.getRemoteAddress().getName());
1365 MessagePacket packet = mXmppConnectionService.getMessageGenerator().generateKeyTransportMessage(jid, axolotlMessage);
1366 mXmppConnectionService.sendMessagePacket(account, packet);
1367 } catch (InvalidJidException e) {
1368 throw new Error("Remote addresses are created from jid and should convert back to jid", e);
1369 }
1370 }
1371
1372
1373 public XmppAxolotlMessage.XmppAxolotlKeyTransportMessage processReceivingKeyTransportMessage(XmppAxolotlMessage message, final boolean postponePreKeyMessageHandling) {
1374 XmppAxolotlMessage.XmppAxolotlKeyTransportMessage keyTransportMessage;
1375
1376 XmppAxolotlSession session = getReceivingSession(message);
1377 try {
1378 keyTransportMessage = message.getParameters(session, getOwnDeviceId());
1379 Integer preKeyId = session.getPreKeyIdAndReset();
1380 if (preKeyId != null) {
1381 postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling);
1382 }
1383 } catch (CryptoFailedException e) {
1384 Log.d(Config.LOGTAG, "could not decrypt keyTransport message " + e.getMessage());
1385 keyTransportMessage = null;
1386 }
1387
1388 if (session.isFresh() && keyTransportMessage != null) {
1389 putFreshSession(session);
1390 }
1391
1392 return keyTransportMessage;
1393 }
1394
1395 private void putFreshSession(XmppAxolotlSession session) {
1396 Log.d(Config.LOGTAG, "put fresh session");
1397 sessions.put(session);
1398 if (Config.X509_VERIFICATION) {
1399 if (session.getIdentityKey() != null) {
1400 verifySessionWithPEP(session);
1401 } else {
1402 Log.e(Config.LOGTAG, account.getJid().toBareJid() + ": identity key was empty after reloading for x509 verification");
1403 }
1404 }
1405 }
1406}