postpone prekey removal and republish to after mam

This commit is contained in:
Daniel Gultsch 2018-12-05 10:42:56 +01:00
parent 19c3c54f24
commit bcfd3f20c2
3 changed files with 33 additions and 9 deletions

View file

@ -1444,7 +1444,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
plaintextMessage = message.decrypt(session, ownDeviceId); plaintextMessage = message.decrypt(session, ownDeviceId);
Integer preKeyId = session.getPreKeyIdAndReset(); Integer preKeyId = session.getPreKeyIdAndReset();
if (preKeyId != null) { if (preKeyId != null) {
postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling); postPreKeyMessageHandling(session, postponePreKeyMessageHandling);
} }
} catch (NotEncryptedForThisDeviceException e) { } catch (NotEncryptedForThisDeviceException e) {
if (account.getJid().asBareJid().equals(message.getFrom().asBareJid()) && message.getSenderDeviceId() == ownDeviceId) { if (account.getJid().asBareJid().equals(message.getFrom().asBareJid()) && message.getSenderDeviceId() == ownDeviceId) {
@ -1494,19 +1494,24 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
} }
} }
private void postPreKeyMessageHandling(final XmppAxolotlSession session, int preKeyId, final boolean postpone) { private void postPreKeyMessageHandling(final XmppAxolotlSession session, final boolean postpone) {
if (postpone) { if (postpone) {
postponedSessions.add(session); postponedSessions.add(session);
} else { } else {
//TODO: do not republish if we already removed this preKeyId if (axolotlStore.flushPreKeys()) {
publishBundlesIfNeeded(false, false); publishBundlesIfNeeded(false, false);
} else {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": nothing to flush. Not republishing key");
}
completeSession(session); completeSession(session);
} }
} }
public void processPostponed() { public void processPostponed() {
if (postponedSessions.size() > 0) { if (postponedSessions.size() > 0) {
publishBundlesIfNeeded(false, false); if (axolotlStore.flushPreKeys()) {
publishBundlesIfNeeded(false, false);
}
} }
Iterator<XmppAxolotlSession> iterator = postponedSessions.iterator(); Iterator<XmppAxolotlSession> iterator = postponedSessions.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
@ -1541,7 +1546,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
keyTransportMessage = message.getParameters(session, getOwnDeviceId()); keyTransportMessage = message.getParameters(session, getOwnDeviceId());
Integer preKeyId = session.getPreKeyIdAndReset(); Integer preKeyId = session.getPreKeyIdAndReset();
if (preKeyId != null) { if (preKeyId != null) {
postPreKeyMessageHandling(session, preKeyId, postponePreKeyMessageHandling); postPreKeyMessageHandling(session, postponePreKeyMessageHandling);
} }
} catch (CryptoFailedException e) { } catch (CryptoFailedException e) {
Log.d(Config.LOGTAG, "could not decrypt keyTransport message " + e.getMessage()); Log.d(Config.LOGTAG, "could not decrypt keyTransport message " + e.getMessage());

View file

@ -16,6 +16,7 @@ import org.whispersystems.libsignal.state.SignedPreKeyRecord;
import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.KeyHelper;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -55,6 +56,8 @@ public class SQLiteAxolotlStore implements SignalProtocolStore {
private int localRegistrationId; private int localRegistrationId;
private int currentPreKeyId = 0; private int currentPreKeyId = 0;
private final HashSet<Integer> preKeysMarkedForRemoval = new HashSet<>();
private final LruCache<String, FingerprintStatus> trustCache = private final LruCache<String, FingerprintStatus> trustCache =
new LruCache<String, FingerprintStatus>(NUM_TRUSTS_TO_CACHE) { new LruCache<String, FingerprintStatus>(NUM_TRUSTS_TO_CACHE) {
@Override @Override
@ -385,7 +388,23 @@ public class SQLiteAxolotlStore implements SignalProtocolStore {
*/ */
@Override @Override
public void removePreKey(int preKeyId) { public void removePreKey(int preKeyId) {
mXmppConnectionService.databaseBackend.deletePreKey(account, preKeyId); Log.d(Config.LOGTAG,"mark prekey for removal "+preKeyId);
synchronized (preKeysMarkedForRemoval) {
preKeysMarkedForRemoval.add(preKeyId);
}
}
public boolean flushPreKeys() {
Log.d(Config.LOGTAG,"flushing pre keys");
int count = 0;
synchronized (preKeysMarkedForRemoval) {
for(Integer preKeyId : preKeysMarkedForRemoval) {
count += mXmppConnectionService.databaseBackend.deletePreKey(account, preKeyId);
}
preKeysMarkedForRemoval.clear();
}
return count > 0;
} }
// -------------------------------------- // --------------------------------------

View file

@ -1178,10 +1178,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values); db.insert(SQLiteAxolotlStore.PREKEY_TABLENAME, null, values);
} }
public void deletePreKey(Account account, int preKeyId) { public int deletePreKey(Account account, int preKeyId) {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
String[] args = {account.getUuid(), Integer.toString(preKeyId)}; String[] args = {account.getUuid(), Integer.toString(preKeyId)};
db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME, return db.delete(SQLiteAxolotlStore.PREKEY_TABLENAME,
SQLiteAxolotlStore.ACCOUNT + "=? AND " SQLiteAxolotlStore.ACCOUNT + "=? AND "
+ SQLiteAxolotlStore.ID + "=?", + SQLiteAxolotlStore.ID + "=?",
args); args);