mirror of
https://github.com/moparisthebest/Conversations
synced 2024-11-28 03:32:15 -05:00
Fix key publishing
Remove invalid check for result code, which prevented publishing if the node was empty to begin with. Fix pepBroken check
This commit is contained in:
parent
05d3015421
commit
0eeaccd974
@ -366,7 +366,7 @@ public class AxolotlService {
|
||||
}
|
||||
|
||||
public void publishBundlesIfNeeded() {
|
||||
if (!pepBroken) {
|
||||
if (pepBroken) {
|
||||
Log.d(Config.LOGTAG, getLogprefix(account) + "publishBundlesIfNeeded called, but PEP is broken. Ignoring... ");
|
||||
return;
|
||||
}
|
||||
@ -374,92 +374,88 @@ public class AxolotlService {
|
||||
mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
|
||||
@Override
|
||||
public void onIqPacketReceived(Account account, IqPacket packet) {
|
||||
if (packet.getType() == IqPacket.TYPE.RESULT) {
|
||||
PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
|
||||
Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
|
||||
boolean flush = false;
|
||||
if (bundle == null) {
|
||||
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
|
||||
bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
|
||||
flush = true;
|
||||
PreKeyBundle bundle = mXmppConnectionService.getIqParser().bundle(packet);
|
||||
Map<Integer, ECPublicKey> keys = mXmppConnectionService.getIqParser().preKeyPublics(packet);
|
||||
boolean flush = false;
|
||||
if (bundle == null) {
|
||||
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid bundle:" + packet);
|
||||
bundle = new PreKeyBundle(-1, -1, -1, null, -1, null, null, null);
|
||||
flush = true;
|
||||
}
|
||||
if (keys == null) {
|
||||
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
|
||||
}
|
||||
try {
|
||||
boolean changed = false;
|
||||
// Validate IdentityKey
|
||||
IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
|
||||
if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
|
||||
changed = true;
|
||||
}
|
||||
if (keys == null) {
|
||||
Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
|
||||
}
|
||||
try {
|
||||
boolean changed = false;
|
||||
// Validate IdentityKey
|
||||
IdentityKeyPair identityKeyPair = axolotlStore.getIdentityKeyPair();
|
||||
if (flush || !identityKeyPair.getPublicKey().equals(bundle.getIdentityKey())) {
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding own IdentityKey " + identityKeyPair.getPublicKey() + " to PEP.");
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// Validate signedPreKeyRecord + ID
|
||||
SignedPreKeyRecord signedPreKeyRecord;
|
||||
int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
|
||||
try {
|
||||
signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
|
||||
if (flush
|
||||
|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
|
||||
|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
|
||||
signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
|
||||
axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
|
||||
changed = true;
|
||||
}
|
||||
} catch (InvalidKeyIdException e) {
|
||||
// Validate signedPreKeyRecord + ID
|
||||
SignedPreKeyRecord signedPreKeyRecord;
|
||||
int numSignedPreKeys = axolotlStore.loadSignedPreKeys().size();
|
||||
try {
|
||||
signedPreKeyRecord = axolotlStore.loadSignedPreKey(bundle.getSignedPreKeyId());
|
||||
if (flush
|
||||
|| !bundle.getSignedPreKey().equals(signedPreKeyRecord.getKeyPair().getPublicKey())
|
||||
|| !Arrays.equals(bundle.getSignedPreKeySignature(), signedPreKeyRecord.getSignature())) {
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
|
||||
signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
|
||||
axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// Validate PreKeys
|
||||
Set<PreKeyRecord> preKeyRecords = new HashSet<>();
|
||||
if (keys != null) {
|
||||
for (Integer id : keys.keySet()) {
|
||||
try {
|
||||
PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
|
||||
if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
|
||||
preKeyRecords.add(preKeyRecord);
|
||||
}
|
||||
} catch (InvalidKeyIdException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
|
||||
if (newKeys > 0) {
|
||||
List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
|
||||
axolotlStore.getCurrentPreKeyId() + 1, newKeys);
|
||||
preKeyRecords.addAll(newRecords);
|
||||
for (PreKeyRecord record : newRecords) {
|
||||
axolotlStore.storePreKey(record.getId(), record);
|
||||
}
|
||||
changed = true;
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
|
||||
}
|
||||
|
||||
|
||||
if (changed) {
|
||||
IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
|
||||
signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
|
||||
preKeyRecords, getOwnDeviceId());
|
||||
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
|
||||
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
|
||||
@Override
|
||||
public void onIqPacketReceived(Account account, IqPacket packet) {
|
||||
// TODO: implement this!
|
||||
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Published bundle, got: " + packet);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (InvalidKeyException e) {
|
||||
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
|
||||
return;
|
||||
} catch (InvalidKeyIdException e) {
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding new signedPreKey with ID " + (numSignedPreKeys + 1) + " to PEP.");
|
||||
signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKeyPair, numSignedPreKeys + 1);
|
||||
axolotlStore.storeSignedPreKey(signedPreKeyRecord.getId(), signedPreKeyRecord);
|
||||
changed = true;
|
||||
}
|
||||
} else {
|
||||
Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing Bundle:" + packet.findChild("error"));
|
||||
|
||||
// Validate PreKeys
|
||||
Set<PreKeyRecord> preKeyRecords = new HashSet<>();
|
||||
if (keys != null) {
|
||||
for (Integer id : keys.keySet()) {
|
||||
try {
|
||||
PreKeyRecord preKeyRecord = axolotlStore.loadPreKey(id);
|
||||
if (preKeyRecord.getKeyPair().getPublicKey().equals(keys.get(id))) {
|
||||
preKeyRecords.add(preKeyRecord);
|
||||
}
|
||||
} catch (InvalidKeyIdException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
int newKeys = NUM_KEYS_TO_PUBLISH - preKeyRecords.size();
|
||||
if (newKeys > 0) {
|
||||
List<PreKeyRecord> newRecords = KeyHelper.generatePreKeys(
|
||||
axolotlStore.getCurrentPreKeyId() + 1, newKeys);
|
||||
preKeyRecords.addAll(newRecords);
|
||||
for (PreKeyRecord record : newRecords) {
|
||||
axolotlStore.storePreKey(record.getId(), record);
|
||||
}
|
||||
changed = true;
|
||||
Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Adding " + newKeys + " new preKeys to PEP.");
|
||||
}
|
||||
|
||||
|
||||
if (changed) {
|
||||
IqPacket publish = mXmppConnectionService.getIqGenerator().publishBundles(
|
||||
signedPreKeyRecord, axolotlStore.getIdentityKeyPair().getPublicKey(),
|
||||
preKeyRecords, getOwnDeviceId());
|
||||
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + ": Bundle " + getOwnDeviceId() + " in PEP not current. Publishing: " + publish);
|
||||
mXmppConnectionService.sendIqPacket(account, publish, new OnIqPacketReceived() {
|
||||
@Override
|
||||
public void onIqPacketReceived(Account account, IqPacket packet) {
|
||||
// TODO: implement this!
|
||||
Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Published bundle, got: " + packet);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (InvalidKeyException e) {
|
||||
Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user