Rename pre key id offset field

This commit is contained in:
AsamK 2023-10-09 19:08:48 +02:00
parent c2ea7045f5
commit 91ab0b12b0
3 changed files with 38 additions and 38 deletions

View file

@ -121,7 +121,7 @@ public class PreKeyHelper {
private List<PreKeyRecord> generatePreKeys(ServiceIdType serviceIdType) { private List<PreKeyRecord> generatePreKeys(ServiceIdType serviceIdType) {
final var accountData = account.getAccountData(serviceIdType); final var accountData = account.getAccountData(serviceIdType);
final var offset = accountData.getPreKeyMetadata().getPreKeyIdOffset(); final var offset = accountData.getPreKeyMetadata().getNextPreKeyId();
var records = KeyUtils.generatePreKeyRecords(offset); var records = KeyUtils.generatePreKeyRecords(offset);
account.addPreKeys(serviceIdType, records); account.addPreKeys(serviceIdType, records);
@ -158,7 +158,7 @@ public class PreKeyHelper {
ServiceIdType serviceIdType, final IdentityKeyPair identityKeyPair ServiceIdType serviceIdType, final IdentityKeyPair identityKeyPair
) { ) {
final var accountData = account.getAccountData(serviceIdType); final var accountData = account.getAccountData(serviceIdType);
final var offset = accountData.getPreKeyMetadata().getKyberPreKeyIdOffset(); final var offset = accountData.getPreKeyMetadata().getNextKyberPreKeyId();
var records = KeyUtils.generateKyberPreKeyRecords(offset, identityKeyPair.getPrivateKey()); var records = KeyUtils.generateKyberPreKeyRecords(offset, identityKeyPair.getPrivateKey());
account.addKyberPreKeys(serviceIdType, records); account.addKyberPreKeys(serviceIdType, records);
@ -186,7 +186,7 @@ public class PreKeyHelper {
ServiceIdType serviceIdType, IdentityKeyPair identityKeyPair ServiceIdType serviceIdType, IdentityKeyPair identityKeyPair
) { ) {
final var accountData = account.getAccountData(serviceIdType); final var accountData = account.getAccountData(serviceIdType);
final var signedPreKeyId = accountData.getPreKeyMetadata().getKyberPreKeyIdOffset(); final var signedPreKeyId = accountData.getPreKeyMetadata().getNextKyberPreKeyId();
var record = KeyUtils.generateKyberPreKeyRecord(signedPreKeyId, identityKeyPair.getPrivateKey()); var record = KeyUtils.generateKyberPreKeyRecord(signedPreKeyId, identityKeyPair.getPrivateKey());
account.addLastResortKyberPreKey(serviceIdType, record); account.addLastResortKyberPreKey(serviceIdType, record);

View file

@ -270,7 +270,7 @@ public class RegistrationManagerImpl implements RegistrationManager {
final var signedPreKey = KeyUtils.generateSignedPreKeyRecord(nextSignedPreKeyId, keyPair); final var signedPreKey = KeyUtils.generateSignedPreKeyRecord(nextSignedPreKeyId, keyPair);
final var privateKey = keyPair.getPrivateKey(); final var privateKey = keyPair.getPrivateKey();
final var kyberPreKeyIdOffset = preKeyMetadata.getKyberPreKeyIdOffset(); final var kyberPreKeyIdOffset = preKeyMetadata.getNextKyberPreKeyId();
final var lastResortKyberPreKey = KeyUtils.generateKyberPreKeyRecord(kyberPreKeyIdOffset, privateKey); final var lastResortKyberPreKey = KeyUtils.generateKyberPreKeyRecord(kyberPreKeyIdOffset, privateKey);
return new PreKeyCollection(keyPair.getPublicKey(), signedPreKey, lastResortKyberPreKey); return new PreKeyCollection(keyPair.getPublicKey(), signedPreKey, lastResortKyberPreKey);

View file

@ -561,10 +561,10 @@ public class SignalAccount implements Closeable {
final var keyPair = KeyUtils.getIdentityKeyPair(publicKeyBytes, privateKeyBytes); final var keyPair = KeyUtils.getIdentityKeyPair(publicKeyBytes, privateKeyBytes);
accountData.setIdentityKeyPair(keyPair); accountData.setIdentityKeyPair(keyPair);
} }
accountData.preKeyMetadata.preKeyIdOffset = storage.nextPreKeyId; accountData.preKeyMetadata.nextPreKeyId = storage.nextPreKeyId;
accountData.preKeyMetadata.nextSignedPreKeyId = storage.nextSignedPreKeyId; accountData.preKeyMetadata.nextSignedPreKeyId = storage.nextSignedPreKeyId;
accountData.preKeyMetadata.activeSignedPreKeyId = storage.activeSignedPreKeyId; accountData.preKeyMetadata.activeSignedPreKeyId = storage.activeSignedPreKeyId;
accountData.preKeyMetadata.kyberPreKeyIdOffset = storage.nextKyberPreKeyId; accountData.preKeyMetadata.nextKyberPreKeyId = storage.nextKyberPreKeyId;
accountData.preKeyMetadata.activeLastResortKyberPreKeyId = storage.activeLastResortKyberPreKeyId; accountData.preKeyMetadata.activeLastResortKyberPreKeyId = storage.activeLastResortKyberPreKeyId;
} }
@ -653,9 +653,9 @@ public class SignalAccount implements Closeable {
getKeyValueStore().storeEntry(storageManifestVersion, rootNode.get("storageManifestVersion").asLong()); getKeyValueStore().storeEntry(storageManifestVersion, rootNode.get("storageManifestVersion").asLong());
} }
if (rootNode.hasNonNull("preKeyIdOffset")) { if (rootNode.hasNonNull("preKeyIdOffset")) {
aciAccountData.preKeyMetadata.preKeyIdOffset = rootNode.get("preKeyIdOffset").asInt(1); aciAccountData.preKeyMetadata.nextPreKeyId = rootNode.get("preKeyIdOffset").asInt(1);
} else { } else {
aciAccountData.preKeyMetadata.preKeyIdOffset = getRandomPreKeyIdOffset(); aciAccountData.preKeyMetadata.nextPreKeyId = getRandomPreKeyIdOffset();
} }
if (rootNode.hasNonNull("nextSignedPreKeyId")) { if (rootNode.hasNonNull("nextSignedPreKeyId")) {
aciAccountData.preKeyMetadata.nextSignedPreKeyId = rootNode.get("nextSignedPreKeyId").asInt(1); aciAccountData.preKeyMetadata.nextSignedPreKeyId = rootNode.get("nextSignedPreKeyId").asInt(1);
@ -668,9 +668,9 @@ public class SignalAccount implements Closeable {
aciAccountData.preKeyMetadata.activeSignedPreKeyId = -1; aciAccountData.preKeyMetadata.activeSignedPreKeyId = -1;
} }
if (rootNode.hasNonNull("pniPreKeyIdOffset")) { if (rootNode.hasNonNull("pniPreKeyIdOffset")) {
pniAccountData.preKeyMetadata.preKeyIdOffset = rootNode.get("pniPreKeyIdOffset").asInt(1); pniAccountData.preKeyMetadata.nextPreKeyId = rootNode.get("pniPreKeyIdOffset").asInt(1);
} else { } else {
pniAccountData.preKeyMetadata.preKeyIdOffset = getRandomPreKeyIdOffset(); pniAccountData.preKeyMetadata.nextPreKeyId = getRandomPreKeyIdOffset();
} }
if (rootNode.hasNonNull("pniNextSignedPreKeyId")) { if (rootNode.hasNonNull("pniNextSignedPreKeyId")) {
pniAccountData.preKeyMetadata.nextSignedPreKeyId = rootNode.get("pniNextSignedPreKeyId").asInt(1); pniAccountData.preKeyMetadata.nextSignedPreKeyId = rootNode.get("pniNextSignedPreKeyId").asInt(1);
@ -683,9 +683,9 @@ public class SignalAccount implements Closeable {
pniAccountData.preKeyMetadata.activeSignedPreKeyId = -1; pniAccountData.preKeyMetadata.activeSignedPreKeyId = -1;
} }
if (rootNode.hasNonNull("kyberPreKeyIdOffset")) { if (rootNode.hasNonNull("kyberPreKeyIdOffset")) {
aciAccountData.preKeyMetadata.kyberPreKeyIdOffset = rootNode.get("kyberPreKeyIdOffset").asInt(1); aciAccountData.preKeyMetadata.nextKyberPreKeyId = rootNode.get("kyberPreKeyIdOffset").asInt(1);
} else { } else {
aciAccountData.preKeyMetadata.kyberPreKeyIdOffset = getRandomPreKeyIdOffset(); aciAccountData.preKeyMetadata.nextKyberPreKeyId = getRandomPreKeyIdOffset();
} }
if (rootNode.hasNonNull("activeLastResortKyberPreKeyId")) { if (rootNode.hasNonNull("activeLastResortKyberPreKeyId")) {
aciAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = rootNode.get("activeLastResortKyberPreKeyId") aciAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = rootNode.get("activeLastResortKyberPreKeyId")
@ -694,9 +694,9 @@ public class SignalAccount implements Closeable {
aciAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = -1; aciAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = -1;
} }
if (rootNode.hasNonNull("pniKyberPreKeyIdOffset")) { if (rootNode.hasNonNull("pniKyberPreKeyIdOffset")) {
pniAccountData.preKeyMetadata.kyberPreKeyIdOffset = rootNode.get("pniKyberPreKeyIdOffset").asInt(1); pniAccountData.preKeyMetadata.nextKyberPreKeyId = rootNode.get("pniKyberPreKeyIdOffset").asInt(1);
} else { } else {
pniAccountData.preKeyMetadata.kyberPreKeyIdOffset = getRandomPreKeyIdOffset(); pniAccountData.preKeyMetadata.nextKyberPreKeyId = getRandomPreKeyIdOffset();
} }
if (rootNode.hasNonNull("pniActiveLastResortKyberPreKeyId")) { if (rootNode.hasNonNull("pniActiveLastResortKyberPreKeyId")) {
pniAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = rootNode.get( pniAccountData.preKeyMetadata.activeLastResortKyberPreKeyId = rootNode.get(
@ -1052,7 +1052,7 @@ public class SignalAccount implements Closeable {
final var accountData = getAccountData(serviceIdType); final var accountData = getAccountData(serviceIdType);
final var preKeyMetadata = accountData.getPreKeyMetadata(); final var preKeyMetadata = accountData.getPreKeyMetadata();
preKeyMetadata.nextSignedPreKeyId = preKeyCollection.getSignedPreKey().getId(); preKeyMetadata.nextSignedPreKeyId = preKeyCollection.getSignedPreKey().getId();
preKeyMetadata.kyberPreKeyIdOffset = preKeyCollection.getLastResortKyberPreKey().getId(); preKeyMetadata.nextKyberPreKeyId = preKeyCollection.getLastResortKyberPreKey().getId();
accountData.getPreKeyStore().removeAllPreKeys(); accountData.getPreKeyStore().removeAllPreKeys();
accountData.getSignedPreKeyStore().removeAllSignedPreKeys(); accountData.getSignedPreKeyStore().removeAllSignedPreKeys();
@ -1066,7 +1066,7 @@ public class SignalAccount implements Closeable {
public void resetPreKeyOffsets(final ServiceIdType serviceIdType) { public void resetPreKeyOffsets(final ServiceIdType serviceIdType) {
final var preKeyMetadata = getAccountData(serviceIdType).getPreKeyMetadata(); final var preKeyMetadata = getAccountData(serviceIdType).getPreKeyMetadata();
preKeyMetadata.preKeyIdOffset = getRandomPreKeyIdOffset(); preKeyMetadata.nextPreKeyId = getRandomPreKeyIdOffset();
preKeyMetadata.nextSignedPreKeyId = getRandomPreKeyIdOffset(); preKeyMetadata.nextSignedPreKeyId = getRandomPreKeyIdOffset();
preKeyMetadata.activeSignedPreKeyId = -1; preKeyMetadata.activeSignedPreKeyId = -1;
save(); save();
@ -1082,16 +1082,16 @@ public class SignalAccount implements Closeable {
logger.debug("Adding {} {} pre keys with offset {}", logger.debug("Adding {} {} pre keys with offset {}",
records.size(), records.size(),
serviceIdType, serviceIdType,
preKeyMetadata.preKeyIdOffset); preKeyMetadata.nextPreKeyId);
accountData.getSignalServiceAccountDataStore() accountData.getSignalServiceAccountDataStore()
.markAllOneTimeEcPreKeysStaleIfNecessary(System.currentTimeMillis()); .markAllOneTimeEcPreKeysStaleIfNecessary(System.currentTimeMillis());
for (var record : records) { for (var record : records) {
if (preKeyMetadata.preKeyIdOffset != record.getId()) { if (preKeyMetadata.nextPreKeyId != record.getId()) {
logger.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata.preKeyIdOffset); logger.error("Invalid pre key id {}, expected {}", record.getId(), preKeyMetadata.nextPreKeyId);
throw new AssertionError("Invalid pre key id"); throw new AssertionError("Invalid pre key id");
} }
accountData.getPreKeyStore().storePreKey(record.getId(), record); accountData.getPreKeyStore().storePreKey(record.getId(), record);
preKeyMetadata.preKeyIdOffset = (preKeyMetadata.preKeyIdOffset + 1) % PREKEY_MAXIMUM_ID; preKeyMetadata.nextPreKeyId = (preKeyMetadata.nextPreKeyId + 1) % PREKEY_MAXIMUM_ID;
} }
save(); save();
} }
@ -1114,7 +1114,7 @@ public class SignalAccount implements Closeable {
public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType) { public void resetKyberPreKeyOffsets(final ServiceIdType serviceIdType) {
final var preKeyMetadata = getAccountData(serviceIdType).getPreKeyMetadata(); final var preKeyMetadata = getAccountData(serviceIdType).getPreKeyMetadata();
preKeyMetadata.kyberPreKeyIdOffset = getRandomPreKeyIdOffset(); preKeyMetadata.nextKyberPreKeyId = getRandomPreKeyIdOffset();
preKeyMetadata.activeLastResortKyberPreKeyId = -1; preKeyMetadata.activeLastResortKyberPreKeyId = -1;
save(); save();
} }
@ -1125,18 +1125,18 @@ public class SignalAccount implements Closeable {
logger.debug("Adding {} {} kyber pre keys with offset {}", logger.debug("Adding {} {} kyber pre keys with offset {}",
records.size(), records.size(),
serviceIdType, serviceIdType,
preKeyMetadata.kyberPreKeyIdOffset); preKeyMetadata.nextKyberPreKeyId);
accountData.getSignalServiceAccountDataStore() accountData.getSignalServiceAccountDataStore()
.markAllOneTimeEcPreKeysStaleIfNecessary(System.currentTimeMillis()); .markAllOneTimeEcPreKeysStaleIfNecessary(System.currentTimeMillis());
for (var record : records) { for (var record : records) {
if (preKeyMetadata.kyberPreKeyIdOffset != record.getId()) { if (preKeyMetadata.nextKyberPreKeyId != record.getId()) {
logger.error("Invalid kyber pre key id {}, expected {}", logger.error("Invalid kyber pre key id {}, expected {}",
record.getId(), record.getId(),
preKeyMetadata.kyberPreKeyIdOffset); preKeyMetadata.nextKyberPreKeyId);
throw new AssertionError("Invalid kyber pre key id"); throw new AssertionError("Invalid kyber pre key id");
} }
accountData.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record); accountData.getKyberPreKeyStore().storeKyberPreKey(record.getId(), record);
preKeyMetadata.kyberPreKeyIdOffset = (preKeyMetadata.kyberPreKeyIdOffset + 1) % PREKEY_MAXIMUM_ID; preKeyMetadata.nextKyberPreKeyId = (preKeyMetadata.nextKyberPreKeyId + 1) % PREKEY_MAXIMUM_ID;
} }
save(); save();
} }
@ -1146,16 +1146,16 @@ public class SignalAccount implements Closeable {
final var preKeyMetadata = accountData.getPreKeyMetadata(); final var preKeyMetadata = accountData.getPreKeyMetadata();
logger.debug("Adding {} last resort kyber pre key with offset {}", logger.debug("Adding {} last resort kyber pre key with offset {}",
serviceIdType, serviceIdType,
preKeyMetadata.kyberPreKeyIdOffset); preKeyMetadata.nextKyberPreKeyId);
if (preKeyMetadata.kyberPreKeyIdOffset != record.getId()) { if (preKeyMetadata.nextKyberPreKeyId != record.getId()) {
logger.error("Invalid last resort kyber pre key id {}, expected {}", logger.error("Invalid last resort kyber pre key id {}, expected {}",
record.getId(), record.getId(),
preKeyMetadata.kyberPreKeyIdOffset); preKeyMetadata.nextKyberPreKeyId);
throw new AssertionError("Invalid last resort kyber pre key id"); throw new AssertionError("Invalid last resort kyber pre key id");
} }
accountData.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record); accountData.getKyberPreKeyStore().storeLastResortKyberPreKey(record.getId(), record);
preKeyMetadata.activeLastResortKyberPreKeyId = record.getId(); preKeyMetadata.activeLastResortKyberPreKeyId = record.getId();
preKeyMetadata.kyberPreKeyIdOffset = (preKeyMetadata.kyberPreKeyIdOffset + 1) % PREKEY_MAXIMUM_ID; preKeyMetadata.nextKyberPreKeyId = (preKeyMetadata.nextKyberPreKeyId + 1) % PREKEY_MAXIMUM_ID;
save(); save();
} }
@ -1404,7 +1404,7 @@ public class SignalAccount implements Closeable {
preKeyMetadata.nextSignedPreKeyId = pniSignedPreKey.getId(); preKeyMetadata.nextSignedPreKeyId = pniSignedPreKey.getId();
addSignedPreKey(ServiceIdType.PNI, pniSignedPreKey); addSignedPreKey(ServiceIdType.PNI, pniSignedPreKey);
if (lastResortKyberPreKey != null) { if (lastResortKyberPreKey != null) {
preKeyMetadata.kyberPreKeyIdOffset = lastResortKyberPreKey.getId(); preKeyMetadata.nextKyberPreKeyId = lastResortKyberPreKey.getId();
addLastResortKyberPreKey(ServiceIdType.PNI, lastResortKyberPreKey); addLastResortKyberPreKey(ServiceIdType.PNI, lastResortKyberPreKey);
} }
save(); save();
@ -1695,14 +1695,14 @@ public class SignalAccount implements Closeable {
public static class PreKeyMetadata { public static class PreKeyMetadata {
private int preKeyIdOffset = 1; private int nextPreKeyId = 1;
private int nextSignedPreKeyId = 1; private int nextSignedPreKeyId = 1;
private int activeSignedPreKeyId = -1; private int activeSignedPreKeyId = -1;
private int kyberPreKeyIdOffset = 1; private int nextKyberPreKeyId = 1;
private int activeLastResortKyberPreKeyId = -1; private int activeLastResortKyberPreKeyId = -1;
public int getPreKeyIdOffset() { public int getNextPreKeyId() {
return preKeyIdOffset; return nextPreKeyId;
} }
public int getNextSignedPreKeyId() { public int getNextSignedPreKeyId() {
@ -1713,8 +1713,8 @@ public class SignalAccount implements Closeable {
return activeSignedPreKeyId; return activeSignedPreKeyId;
} }
public int getKyberPreKeyIdOffset() { public int getNextKyberPreKeyId() {
return kyberPreKeyIdOffset; return nextKyberPreKeyId;
} }
public int getActiveLastResortKyberPreKeyId() { public int getActiveLastResortKyberPreKeyId() {
@ -1853,10 +1853,10 @@ public class SignalAccount implements Closeable {
accountData.getIdentityKeyPair() == null accountData.getIdentityKeyPair() == null
? null ? null
: base64.encodeToString(accountData.getIdentityKeyPair().getPublicKey().serialize()), : base64.encodeToString(accountData.getIdentityKeyPair().getPublicKey().serialize()),
preKeyMetadata.getPreKeyIdOffset(), preKeyMetadata.getNextPreKeyId(),
preKeyMetadata.getNextSignedPreKeyId(), preKeyMetadata.getNextSignedPreKeyId(),
preKeyMetadata.getActiveSignedPreKeyId(), preKeyMetadata.getActiveSignedPreKeyId(),
preKeyMetadata.getKyberPreKeyIdOffset(), preKeyMetadata.getNextKyberPreKeyId(),
preKeyMetadata.getActiveLastResortKyberPreKeyId()); preKeyMetadata.getActiveLastResortKyberPreKeyId());
} }
} }