Fix key publishing

Andreas Straub created

Remove invalid check for result code, which prevented publishing if the
node was empty to begin with.

Fix pepBroken check

Change summary

src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java | 140 
1 file changed, 68 insertions(+), 72 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java 🔗

@@ -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;
-					}
-					if (keys == null) {
-						Log.w(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Received invalid prekeys:" + packet);
+				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;
 					}
-					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;
 						}
+					} 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;
+					}
 
-						// 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) {
+					// 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.");
+					}
+					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;
+					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);
+							}
+						});
 					}
-				} else {
-					Log.d(Config.LOGTAG, getLogprefix(account) + "Error received while publishing Bundle:" + packet.findChild("error"));
+				} catch (InvalidKeyException e) {
+					Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Failed to publish bundle " + getOwnDeviceId() + ", reason: " + e.getMessage());
+					return;
 				}
 			}
 		});