Reset pre key offset if it somehow gets corrupted

Fixes #1055
This commit is contained in:
AsamK 2022-10-18 17:55:51 +02:00
parent 266129c61b
commit 228713ebb5
2 changed files with 25 additions and 4 deletions

View file

@ -53,6 +53,18 @@ public class PreKeyHelper {
if (accountId == null) { if (accountId == null) {
return; return;
} }
try {
refreshPreKeys(serviceIdType, identityKeyPair);
} catch (Exception e) {
logger.warn("Failed to store new pre keys, resetting preKey id offset", e);
account.resetPreKeyOffsets(serviceIdType);
refreshPreKeys(serviceIdType, identityKeyPair);
}
}
private void refreshPreKeys(
final ServiceIdType serviceIdType, final IdentityKeyPair identityKeyPair
) throws IOException {
final var oneTimePreKeys = generatePreKeys(serviceIdType); final var oneTimePreKeys = generatePreKeys(serviceIdType);
final var signedPreKeyRecord = generateSignedPreKey(serviceIdType, identityKeyPair); final var signedPreKeyRecord = generateSignedPreKey(serviceIdType, identityKeyPair);

View file

@ -294,10 +294,8 @@ public class SignalAccount implements Closeable {
} }
private void clearAllPreKeys() { private void clearAllPreKeys() {
this.aciPreKeyIdOffset = new SecureRandom().nextInt(Medium.MAX_VALUE); resetPreKeyOffsets(ServiceIdType.ACI);
this.aciNextSignedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE); resetPreKeyOffsets(ServiceIdType.PNI);
this.pniPreKeyIdOffset = new SecureRandom().nextInt(Medium.MAX_VALUE);
this.pniNextSignedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
this.getAciPreKeyStore().removeAllPreKeys(); this.getAciPreKeyStore().removeAllPreKeys();
this.getAciSignedPreKeyStore().removeAllSignedPreKeys(); this.getAciSignedPreKeyStore().removeAllSignedPreKeys();
this.getPniPreKeyStore().removeAllPreKeys(); this.getPniPreKeyStore().removeAllPreKeys();
@ -998,6 +996,17 @@ public class SignalAccount implements Closeable {
} }
} }
public void resetPreKeyOffsets(final ServiceIdType serviceIdType) {
if (serviceIdType.equals(ServiceIdType.ACI)) {
this.aciPreKeyIdOffset = new SecureRandom().nextInt(Medium.MAX_VALUE);
this.aciNextSignedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
} else {
this.pniPreKeyIdOffset = new SecureRandom().nextInt(Medium.MAX_VALUE);
this.pniNextSignedPreKeyId = new SecureRandom().nextInt(Medium.MAX_VALUE);
}
save();
}
public void addPreKeys(ServiceIdType serviceIdType, List<PreKeyRecord> records) { public void addPreKeys(ServiceIdType serviceIdType, List<PreKeyRecord> records) {
if (serviceIdType.equals(ServiceIdType.ACI)) { if (serviceIdType.equals(ServiceIdType.ACI)) {
addAciPreKeys(records); addAciPreKeys(records);