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