Reformat code

This commit is contained in:
Andreas Straub 2015-07-31 18:05:32 +02:00
parent 5c421da1e1
commit 50b14434ee
3 changed files with 87 additions and 86 deletions

View file

@ -126,7 +126,7 @@ public class AxolotlService {
private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) { private void putDevicesForJid(String bareJid, List<Integer> deviceIds, SQLiteAxolotlStore store) {
for (Integer deviceId : deviceIds) { for (Integer deviceId : deviceIds) {
AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId); AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building session for remote address: "+axolotlAddress.toString()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString());
String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", ""); String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint)); this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
} }
@ -165,7 +165,7 @@ public class AxolotlService {
} }
public static String getLogprefix(Account account) { public static String getLogprefix(Account account) {
return LOGPREFIX+" ("+account.getJid().toBareJid().toString()+"): "; return LOGPREFIX + " (" + account.getJid().toBareJid().toString() + "): ";
} }
public AxolotlService(Account account, XmppConnectionService connectionService) { public AxolotlService(Account account, XmppConnectionService connectionService) {
@ -238,7 +238,7 @@ public class AxolotlService {
private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds, private void setTrustOnSessions(final Jid jid, @NonNull final Set<Integer> deviceIds,
final SQLiteAxolotlStore.Trust from, final SQLiteAxolotlStore.Trust from,
final SQLiteAxolotlStore.Trust to) { final SQLiteAxolotlStore.Trust to) {
for(Integer deviceId:deviceIds) { for (Integer deviceId : deviceIds) {
AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId); AxolotlAddress address = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
XmppAxolotlSession session = sessions.get(address); XmppAxolotlSession session = sessions.get(address);
if (session != null && session.getFingerprint() != null if (session != null && session.getFingerprint() != null
@ -249,13 +249,13 @@ public class AxolotlService {
} }
public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) { public void registerDevices(final Jid jid, @NonNull final Set<Integer> deviceIds) {
if(jid.toBareJid().equals(account.getJid().toBareJid())) { if (jid.toBareJid().equals(account.getJid().toBareJid())) {
if (deviceIds.contains(getOwnDeviceId())) { if (deviceIds.contains(getOwnDeviceId())) {
deviceIds.remove(getOwnDeviceId()); deviceIds.remove(getOwnDeviceId());
} }
for(Integer deviceId : deviceIds) { for (Integer deviceId : deviceIds) {
AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(),deviceId); AxolotlAddress ownDeviceAddress = new AxolotlAddress(jid.toBareJid().toString(), deviceId);
if(sessions.get(ownDeviceAddress) == null) { if (sessions.get(ownDeviceAddress) == null) {
buildSessionFromPEP(null, ownDeviceAddress, false); buildSessionFromPEP(null, ownDeviceAddress, false);
} }
} }
@ -276,7 +276,7 @@ public class AxolotlService {
Set<Integer> deviceIds = new HashSet<>(); Set<Integer> deviceIds = new HashSet<>();
deviceIds.add(getOwnDeviceId()); deviceIds.add(getOwnDeviceId());
IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds); IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Wiping all other devices from Pep:" + publish); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Wiping all other devices from Pep:" + publish);
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
@ -286,7 +286,7 @@ public class AxolotlService {
} }
public void purgeKey(IdentityKey identityKey) { public void purgeKey(IdentityKey identityKey) {
axolotlStore.setFingerprintTrust(identityKey.getFingerprint().replaceAll("\\s",""), SQLiteAxolotlStore.Trust.COMPROMISED); axolotlStore.setFingerprintTrust(identityKey.getFingerprint().replaceAll("\\s", ""), SQLiteAxolotlStore.Trust.COMPROMISED);
} }
public void publishOwnDeviceIdIfNeeded() { public void publishOwnDeviceIdIfNeeded() {
@ -302,7 +302,7 @@ public class AxolotlService {
if (!deviceIds.contains(getOwnDeviceId())) { if (!deviceIds.contains(getOwnDeviceId())) {
deviceIds.add(getOwnDeviceId()); deviceIds.add(getOwnDeviceId());
IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds); IqPacket publish = mXmppConnectionService.getIqGenerator().publishDeviceIds(deviceIds);
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Own device " + getOwnDeviceId() + " not in PEP devicelist. Publishing: " + publish); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Own device " + getOwnDeviceId() + " not in PEP devicelist. Publishing: " + publish);
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
@ -323,19 +323,19 @@ public class AxolotlService {
Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet); Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
boolean flush = false; boolean flush = false;
if (bundle == null) { if (bundle == null) {
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received invalid bundle:" + packet); Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
bundle = new PreKeyBundle(-1, -1, -1 , null, -1, null, null, null); bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
flush = true; flush = true;
} }
if (keys == null) { if (keys == null) {
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received invalid prekeys:" + packet); Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
} }
try { try {
boolean changed = false; boolean changed = false;
// Validate IdentityKey // Validate IdentityKey
IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair(); IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) { if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP."); Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
changed = true; changed = true;
} }
@ -344,16 +344,16 @@ public class AxolotlService {
int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size(); int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
try { try {
signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId()); signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
if ( flush if (flush
||!bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey()) || !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) { || !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP."); Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1); signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord); axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
changed = true; changed = true;
} }
} catch (InvalidKeyIdException e) { } catch (InvalidKeyIdException e) {
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP."); Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1); signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord); axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
changed = true; changed = true;
@ -375,31 +375,31 @@ public class AxolotlService {
int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size(); int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
if (newKeys > 0) { if (newKeys > 0) {
List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys( List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
axolotlStore.getCurrentPreKeyId()+1, newKeys); axolotlStore.getCurrentPreKeyId() + 1, newKeys);
preKeyRecords.addAll(newRecords); preKeyRecords.addAll(newRecords);
for (PreKeyRecord record : newRecords) { for (PreKeyRecord record : newRecords) {
axolotlStore.storePreKey(record.getId(), record); axolotlStore.storePreKey(record.getId(), record);
} }
changed = true; changed = true;
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Adding " + newKeys + " new preKeys to PEP."); Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
} }
if(changed) { if (changed) {
IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles( IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(), signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
preKeyRecords, getOwnDeviceId()); preKeyRecords, getOwnDeviceId());
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+ ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
// TODO: implement this! // TODO: implement this!
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Published bundle, got: " + packet); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Published bundle, got: " + packet);
} }
}); });
} }
} catch (InvalidKeyException e) { } catch (InvalidKeyException e) {
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage()); Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
return; return;
} }
} }
@ -411,8 +411,9 @@ public class AxolotlService {
Jid jid = contact.getJid().toBareJid(); Jid jid = contact.getJid().toBareJid();
AxolotlAddress address = new AxolotlAddress(jid.toString(), 0); AxolotlAddress address = new AxolotlAddress(jid.toString(), 0);
return sessions.hasAny(address) || return sessions.hasAny(address) ||
( deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty()); (deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
} }
public SQLiteAxolotlStore.Trust getFingerprintTrust(String fingerprint) { public SQLiteAxolotlStore.Trust getFingerprintTrust(String fingerprint) {
return axolotlStore.getFingerprintTrust(fingerprint); return axolotlStore.getFingerprintTrust(fingerprint);
} }
@ -427,10 +428,10 @@ public class AxolotlService {
try { try {
IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice( IqPacket bundlesPacket = mXmppConnectionService.getIqGenerator().retrieveBundlesForDevice(
Jid.fromString(address.getName()), address.getDeviceId()); Jid.fromString(address.getName()), address.getDeviceId());
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Retrieving bundle: " + bundlesPacket); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Retrieving bundle: " + bundlesPacket);
mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() { mXmppConnectionService.sendIqPacket(account, bundlesPacket, new OnIqPacketReceived() {
private void finish() { private void finish() {
AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(),0); AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING) if (!fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
&& !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) { && !fetchStatusMap.getAll(address).containsValue(FetchStatus.PENDING)) {
if (flushWaitingQueueAfterFetch && conversation != null) { if (flushWaitingQueueAfterFetch && conversation != null) {
@ -438,7 +439,7 @@ public class AxolotlService {
new Conversation.OnMessageFound() { new Conversation.OnMessageFound() {
@Override @Override
public void onMessageFound(Message message) { public void onMessageFound(Message message) {
processSending(message,false); processSending(message, false);
} }
}); });
} }
@ -448,12 +449,12 @@ public class AxolotlService {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Received preKey IQ packet, processing..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received preKey IQ packet, processing...");
final IqParser parser = mXmppConnectionService.getIqParser(); final IqParser parser = mXmppConnectionService.getIqParser();
final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet); final List<PreKeyBundle> preKeyBundleList = parser.preKeys(packet);
final PreKeyBundle bundle = parser.bundle(packet); final PreKeyBundle bundle = parser.bundle(packet);
if (preKeyBundleList.isEmpty() || bundle == null) { if (preKeyBundleList.isEmpty() || bundle == null) {
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"preKey IQ packet invalid: " + packet); Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "preKey IQ packet invalid: " + packet);
fetchStatusMap.put(address, FetchStatus.ERROR); fetchStatusMap.put(address, FetchStatus.ERROR);
finish(); finish();
return; return;
@ -480,8 +481,8 @@ public class AxolotlService {
XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", "")); XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
sessions.put(address, session); sessions.put(address, session);
fetchStatusMap.put(address, FetchStatus.SUCCESS); fetchStatusMap.put(address, FetchStatus.SUCCESS);
} catch (UntrustedIdentityException|InvalidKeyException e) { } catch (UntrustedIdentityException | InvalidKeyException e) {
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Error building session for " + address + ": " Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Error building session for " + address + ": "
+ e.getClass().getName() + ", " + e.getMessage()); + e.getClass().getName() + ", " + e.getMessage());
fetchStatusMap.put(address, FetchStatus.ERROR); fetchStatusMap.put(address, FetchStatus.ERROR);
} }
@ -490,7 +491,7 @@ public class AxolotlService {
} }
}); });
} catch (InvalidJidException e) { } catch (InvalidJidException e) {
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Got address with invalid jid: " + address.getName()); Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got address with invalid jid: " + address.getName());
} }
} }
@ -498,13 +499,13 @@ public class AxolotlService {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Finding devices without session for " + conversation.getContact().getJid().toBareJid());
Jid contactJid = conversation.getContact().getJid().toBareJid(); Jid contactJid = conversation.getContact().getJid().toBareJid();
Set<AxolotlAddress> addresses = new HashSet<>(); Set<AxolotlAddress> addresses = new HashSet<>();
if(deviceIds.get(contactJid) != null) { if (deviceIds.get(contactJid) != null) {
for(Integer foreignId:this.deviceIds.get(contactJid)) { for (Integer foreignId : this.deviceIds.get(contactJid)) {
AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId); AxolotlAddress address = new AxolotlAddress(contactJid.toString(), foreignId);
if(sessions.get(address) == null) { if (sessions.get(address) == null) {
IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey(); IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
if ( identityKey != null ) { if (identityKey != null) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already have session for " + address.toString() + ", adding to cache..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", "")); XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
sessions.put(address, session); sessions.put(address, session);
} else { } else {
@ -516,13 +517,13 @@ public class AxolotlService {
} else { } else {
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!"); Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Have no target devices in PEP!");
} }
if(deviceIds.get(account.getJid().toBareJid()) != null) { if (deviceIds.get(account.getJid().toBareJid()) != null) {
for(Integer ownId:this.deviceIds.get(account.getJid().toBareJid())) { for (Integer ownId : this.deviceIds.get(account.getJid().toBareJid())) {
AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId); AxolotlAddress address = new AxolotlAddress(account.getJid().toBareJid().toString(), ownId);
if(sessions.get(address) == null) { if (sessions.get(address) == null) {
IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey(); IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
if ( identityKey != null ) { if (identityKey != null) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already have session for " + address.toString() + ", adding to cache..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", "")); XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
sessions.put(address, session); sessions.put(address, session);
} else { } else {
@ -543,12 +544,12 @@ public class AxolotlService {
for (AxolotlAddress address : addresses) { for (AxolotlAddress address : addresses) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Processing device: " + address.toString());
FetchStatus status = fetchStatusMap.get(address); FetchStatus status = fetchStatusMap.get(address);
if ( status == null || status == FetchStatus.ERROR ) { if (status == null || status == FetchStatus.ERROR) {
fetchStatusMap.put(address, FetchStatus.PENDING); fetchStatusMap.put(address, FetchStatus.PENDING);
this.buildSessionFromPEP(conversation, address, flushWaitingQueueAfterFetch); this.buildSessionFromPEP(conversation, address, flushWaitingQueueAfterFetch);
newSessions = true; newSessions = true;
} else { } else {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Already fetching bundle for " + address.toString()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already fetching bundle for " + address.toString());
} }
} }
@ -556,15 +557,15 @@ public class AxolotlService {
} }
public boolean hasPendingKeyFetches(Conversation conversation) { public boolean hasPendingKeyFetches(Conversation conversation) {
AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(),0); AxolotlAddress ownAddress = new AxolotlAddress(account.getJid().toBareJid().toString(), 0);
AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(),0); AxolotlAddress foreignAddress = new AxolotlAddress(conversation.getJid().toBareJid().toString(), 0);
return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING) return fetchStatusMap.getAll(ownAddress).containsValue(FetchStatus.PENDING)
||fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING); || fetchStatusMap.getAll(foreignAddress).containsValue(FetchStatus.PENDING);
} }
@Nullable @Nullable
public XmppAxolotlMessage encrypt(Message message ){ public XmppAxolotlMessage encrypt(Message message) {
final String content; final String content;
if (message.hasFileOnRemoteHost()) { if (message.hasFileOnRemoteHost()) {
content = message.getFileParams().url.toString(); content = message.getFileParams().url.toString();
@ -580,17 +581,17 @@ public class AxolotlService {
return null; return null;
} }
if(findSessionsforContact(message.getContact()).isEmpty()) { if (findSessionsforContact(message.getContact()).isEmpty()) {
return null; return null;
} }
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building axolotl foreign keyElements..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl foreign keyElements...");
for (XmppAxolotlSession session : findSessionsforContact(message.getContact())) { for (XmppAxolotlSession session : findSessionsforContact(message.getContact())) {
Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account)+session.getRemoteAddress().toString()); Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
axolotlMessage.addKeyElement(session.processSending(axolotlMessage.getInnerKey())); axolotlMessage.addKeyElement(session.processSending(axolotlMessage.getInnerKey()));
} }
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Building axolotl own keyElements..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building axolotl own keyElements...");
for (XmppAxolotlSession session : findOwnSessions()) { for (XmppAxolotlSession session : findOwnSessions()) {
Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account)+session.getRemoteAddress().toString()); Log.v(Config.LOGTAG, AxolotlService.getLogprefix(account) + session.getRemoteAddress().toString());
axolotlMessage.addKeyElement(session.processSending(axolotlMessage.getInnerKey())); axolotlMessage.addKeyElement(session.processSending(axolotlMessage.getInnerKey()));
} }
@ -606,19 +607,19 @@ public class AxolotlService {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED); mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
//mXmppConnectionService.updateConversationUi(); //mXmppConnectionService.updateConversationUi();
} else { } else {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Generated message, caching: " + message.getUuid()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Generated message, caching: " + message.getUuid());
messageCache.put(message.getUuid(), axolotlMessage); messageCache.put(message.getUuid(), axolotlMessage);
mXmppConnectionService.resendMessage(message,delay); mXmppConnectionService.resendMessage(message, delay);
} }
} }
}); });
} }
public void prepareMessage(final Message message,final boolean delay) { public void prepareMessage(final Message message, final boolean delay) {
if (!messageCache.containsKey(message.getUuid())) { if (!messageCache.containsKey(message.getUuid())) {
boolean newSessions = createSessionsIfNeeded(message.getConversation(), true); boolean newSessions = createSessionsIfNeeded(message.getConversation(), true);
if (!newSessions) { if (!newSessions) {
this.processSending(message,delay); this.processSending(message, delay);
} }
} }
} }
@ -626,10 +627,10 @@ public class AxolotlService {
public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) { public XmppAxolotlMessage fetchAxolotlMessageFromCache(Message message) {
XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid()); XmppAxolotlMessage axolotlMessage = messageCache.get(message.getUuid());
if (axolotlMessage != null) { if (axolotlMessage != null) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Cache hit: " + message.getUuid()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache hit: " + message.getUuid());
messageCache.remove(message.getUuid()); messageCache.remove(message.getUuid());
} else { } else {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Cache miss: " + message.getUuid()); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Cache miss: " + message.getUuid());
} }
return axolotlMessage; return axolotlMessage;
} }
@ -642,9 +643,9 @@ public class AxolotlService {
boolean newSession = false; boolean newSession = false;
XmppAxolotlSession session = sessions.get(senderAddress); XmppAxolotlSession session = sessions.get(senderAddress);
if (session == null) { if (session == null) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Account: "+account.getJid()+" No axolotl session found while parsing received message " + message); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Account: " + account.getJid() + " No axolotl session found while parsing received message " + message);
IdentityKey identityKey = axolotlStore.loadSession(senderAddress).getSessionState().getRemoteIdentityKey(); IdentityKey identityKey = axolotlStore.loadSession(senderAddress).getSessionState().getRemoteIdentityKey();
if ( identityKey != null ) { if (identityKey != null) {
session = new XmppAxolotlSession(account, axolotlStore, senderAddress, identityKey.getFingerprint().replaceAll("\\s", "")); session = new XmppAxolotlSession(account, axolotlStore, senderAddress, identityKey.getFingerprint().replaceAll("\\s", ""));
} else { } else {
session = new XmppAxolotlSession(account, axolotlStore, senderAddress); session = new XmppAxolotlSession(account, axolotlStore, senderAddress);
@ -654,11 +655,11 @@ public class AxolotlService {
for (XmppAxolotlMessage.XmppAxolotlKeyElement keyElement : message.getKeyElements()) { for (XmppAxolotlMessage.XmppAxolotlKeyElement keyElement : message.getKeyElements()) {
if (keyElement.getRecipientDeviceId() == getOwnDeviceId()) { if (keyElement.getRecipientDeviceId() == getOwnDeviceId()) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Found axolotl keyElement matching own device ID, processing..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found axolotl keyElement matching own device ID, processing...");
byte[] payloadKey = session.processReceiving(keyElement); byte[] payloadKey = session.processReceiving(keyElement);
if (payloadKey != null) { if (payloadKey != null) {
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account)+"Got payload key from axolotl keyElement. Decrypting message..."); Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Got payload key from axolotl keyElement. Decrypting message...");
try{ try {
plaintextMessage = message.decrypt(session, payloadKey, session.getFingerprint()); plaintextMessage = message.decrypt(session, payloadKey, session.getFingerprint());
} catch (CryptoFailedException e) { } catch (CryptoFailedException e) {
Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage()); Log.w(Config.LOGTAG, getLogprefix(account) + "Failed to decrypt message: " + e.getMessage());

View file

@ -56,11 +56,11 @@ public class XmppAxolotlMessage {
} }
public XmppAxolotlKeyElement(Element keyElement) { public XmppAxolotlKeyElement(Element keyElement) {
if(TAGNAME.equals(keyElement.getName())) { if (TAGNAME.equals(keyElement.getName())) {
this.recipientDeviceId = Integer.parseInt(keyElement.getAttribute(REMOTEID)); this.recipientDeviceId = Integer.parseInt(keyElement.getAttribute(REMOTEID));
this.content = Base64.decode(keyElement.getContent(),Base64.DEFAULT); this.content = Base64.decode(keyElement.getContent(), Base64.DEFAULT);
} else { } else {
throw new IllegalArgumentException("Argument not a <"+TAGNAME+"> Element!"); throw new IllegalArgumentException("Argument not a <" + TAGNAME + "> Element!");
} }
} }
@ -109,24 +109,24 @@ public class XmppAxolotlMessage {
Element header = axolotlMessage.findChild(HEADER); Element header = axolotlMessage.findChild(HEADER);
this.sourceDeviceId = Integer.parseInt(header.getAttribute(SOURCEID)); this.sourceDeviceId = Integer.parseInt(header.getAttribute(SOURCEID));
this.keyElements = new HashSet<>(); this.keyElements = new HashSet<>();
for(Element keyElement:header.getChildren()) { for (Element keyElement : header.getChildren()) {
switch(keyElement.getName()) { switch (keyElement.getName()) {
case XmppAxolotlKeyElement.TAGNAME: case XmppAxolotlKeyElement.TAGNAME:
keyElements.add(new XmppAxolotlKeyElement(keyElement)); keyElements.add(new XmppAxolotlKeyElement(keyElement));
break; break;
case IVTAG: case IVTAG:
if ( this.iv != null) { if (this.iv != null) {
throw new IllegalArgumentException("Duplicate iv entry"); throw new IllegalArgumentException("Duplicate iv entry");
} }
iv = Base64.decode(keyElement.getContent(),Base64.DEFAULT); iv = Base64.decode(keyElement.getContent(), Base64.DEFAULT);
break; break;
default: default:
Log.w(Config.LOGTAG, "Unexpected element in header: "+ keyElement.toString()); Log.w(Config.LOGTAG, "Unexpected element in header: " + keyElement.toString());
break; break;
} }
} }
Element payloadElement = axolotlMessage.findChild(PAYLOAD); Element payloadElement = axolotlMessage.findChild(PAYLOAD);
if ( payloadElement != null ) { if (payloadElement != null) {
ciphertext = Base64.decode(payloadElement.getContent(), Base64.DEFAULT); ciphertext = Base64.decode(payloadElement.getContent(), Base64.DEFAULT);
} }
} }
@ -139,7 +139,7 @@ public class XmppAxolotlMessage {
this.innerKey = generateKey(); this.innerKey = generateKey();
} }
public XmppAxolotlMessage(Jid from, int sourceDeviceId, String plaintext) throws CryptoFailedException{ public XmppAxolotlMessage(Jid from, int sourceDeviceId, String plaintext) throws CryptoFailedException {
this(from, sourceDeviceId); this(from, sourceDeviceId);
this.encrypt(plaintext); this.encrypt(plaintext);
} }
@ -199,7 +199,7 @@ public class XmppAxolotlMessage {
} }
} }
public byte[] getInnerKey(){ public byte[] getInnerKey() {
return innerKey; return innerKey;
} }
@ -208,14 +208,14 @@ public class XmppAxolotlMessage {
} }
public Element toXml() { public Element toXml() {
Element encryptionElement= new Element(TAGNAME, AxolotlService.PEP_PREFIX); Element encryptionElement = new Element(TAGNAME, AxolotlService.PEP_PREFIX);
Element headerElement = encryptionElement.addChild(HEADER); Element headerElement = encryptionElement.addChild(HEADER);
headerElement.setAttribute(SOURCEID, sourceDeviceId); headerElement.setAttribute(SOURCEID, sourceDeviceId);
for(XmppAxolotlKeyElement header: keyElements) { for (XmppAxolotlKeyElement header : keyElements) {
headerElement.addChild(header.toXml()); headerElement.addChild(header.toXml());
} }
headerElement.addChild(IVTAG).setContent(Base64.encodeToString(iv, Base64.DEFAULT)); headerElement.addChild(IVTAG).setContent(Base64.encodeToString(iv, Base64.DEFAULT));
if ( ciphertext != null ) { if (ciphertext != null) {
Element payload = encryptionElement.addChild(PAYLOAD); Element payload = encryptionElement.addChild(PAYLOAD);
payload.setContent(Base64.encodeToString(ciphertext, Base64.DEFAULT)); payload.setContent(Base64.encodeToString(ciphertext, Base64.DEFAULT));
} }

View file

@ -65,7 +65,7 @@ public class XmppAxolotlSession {
protected SQLiteAxolotlStore.Trust getTrust() { protected SQLiteAxolotlStore.Trust getTrust() {
SQLiteAxolotlStore.Trust trust = sqLiteAxolotlStore.getFingerprintTrust(fingerprint); SQLiteAxolotlStore.Trust trust = sqLiteAxolotlStore.getFingerprintTrust(fingerprint);
return (trust == null)? SQLiteAxolotlStore.Trust.UNDECIDED : trust; return (trust == null) ? SQLiteAxolotlStore.Trust.UNDECIDED : trust;
} }
@Nullable @Nullable