store identity key in XmppAxolotlSession instead of the fingerprint

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/crypto/axolotl/AxolotlService.java     | 25 
src/main/java/eu/siacs/conversations/crypto/axolotl/XmppAxolotlSession.java | 25 
2 files changed, 27 insertions(+), 23 deletions(-)

Detailed changes

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

@@ -146,8 +146,8 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 			for (Integer deviceId : deviceIds) {
 				AxolotlAddress axolotlAddress = new AxolotlAddress(bareJid, deviceId);
 				Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Building session for remote address: " + axolotlAddress.toString());
-				String fingerprint = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey().getFingerprint().replaceAll("\\s", "");
-				this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, fingerprint));
+				IdentityKey identityKey = store.loadSession(axolotlAddress).getSessionState().getRemoteIdentityKey();
+				this.put(axolotlAddress, new XmppAxolotlSession(account, store, axolotlAddress, identityKey));
 			}
 		}
 
@@ -579,9 +579,10 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 		axolotlStore.setFingerprintTrust(fingerprint, trust);
 	}
 
-	private void verifySessionWithPEP(final XmppAxolotlSession session, final IdentityKey identityKey) {
-		Log.d(Config.LOGTAG,"trying to verify fresh session ("+session.getRemoteAddress().getName()+") with pep");
+	private void verifySessionWithPEP(final XmppAxolotlSession session) {
+		Log.d(Config.LOGTAG, "trying to verify fresh session (" + session.getRemoteAddress().getName() + ") with pep");
 		final AxolotlAddress address = session.getRemoteAddress();
+		final IdentityKey identityKey = session.getIdentityKey();
 		try {
 			IqPacket packet = mXmppConnectionService.getIqGenerator().retrieveVerificationForDevice(Jid.fromString(address.getName()), address.getDeviceId());
 			mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
@@ -681,10 +682,10 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 						try {
 							SessionBuilder builder = new SessionBuilder(axolotlStore, address);
 							builder.process(preKeyBundle);
-							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey().getFingerprint().replaceAll("\\s", ""));
+							XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, bundle.getIdentityKey());
 							sessions.put(address, session);
 							if (Config.X509_VERIFICATION) {
-								verifySessionWithPEP(session, bundle.getIdentityKey());
+								verifySessionWithPEP(session);
 							} else {
 								fetchStatusMap.put(address, FetchStatus.SUCCESS);
 								finishBuildingSessionsFromPEP(address);
@@ -721,7 +722,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
 					if (identityKey != null) {
 						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
-						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
+						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
 						sessions.put(address, session);
 					} else {
 						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + foreignId);
@@ -743,7 +744,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 					IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
 					if (identityKey != null) {
 						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Already have session for " + address.toString() + ", adding to cache...");
-						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey.getFingerprint().replaceAll("\\s", ""));
+						XmppAxolotlSession session = new XmppAxolotlSession(account, axolotlStore, address, identityKey);
 						sessions.put(address, session);
 					} else {
 						Log.d(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Found device " + account.getJid().toBareJid() + ":" + ownId);
@@ -892,8 +893,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 	private XmppAxolotlSession recreateUncachedSession(AxolotlAddress address) {
 		IdentityKey identityKey = axolotlStore.loadSession(address).getSessionState().getRemoteIdentityKey();
 		return (identityKey != null)
-				? new XmppAxolotlSession(account, axolotlStore, address,
-						identityKey.getFingerprint().replaceAll("\\s", ""))
+				? new XmppAxolotlSession(account, axolotlStore, address, identityKey)
 				: null;
 	}
 
@@ -950,9 +950,8 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded {
 		Log.d(Config.LOGTAG,"put fresh session");
 		sessions.put(session);
 		if (Config.X509_VERIFICATION) {
-			IdentityKey identityKey = axolotlStore.loadSession(session.getRemoteAddress()).getSessionState().getRemoteIdentityKey();
-			if (identityKey != null) {
-				verifySessionWithPEP(session, identityKey);
+			if (session.getIdentityKey() != null) {
+				verifySessionWithPEP(session);
 			} else {
 				Log.e(Config.LOGTAG,account.getJid().toBareJid()+": identity key was empty after reloading for x509 verification");
 			}

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

@@ -6,6 +6,7 @@ import android.util.Log;
 
 import org.whispersystems.libaxolotl.AxolotlAddress;
 import org.whispersystems.libaxolotl.DuplicateMessageException;
+import org.whispersystems.libaxolotl.IdentityKey;
 import org.whispersystems.libaxolotl.InvalidKeyException;
 import org.whispersystems.libaxolotl.InvalidKeyIdException;
 import org.whispersystems.libaxolotl.InvalidMessageException;
@@ -29,7 +30,7 @@ public class XmppAxolotlSession {
 	private final SQLiteAxolotlStore sqLiteAxolotlStore;
 	private final AxolotlAddress remoteAddress;
 	private final Account account;
-	private String fingerprint = null;
+	private IdentityKey identityKey;
 	private Integer preKeyId = null;
 	private boolean fresh = true;
 
@@ -103,9 +104,9 @@ public class XmppAxolotlSession {
 		}
 	}
 
-	public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, String fingerprint) {
+	public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress, IdentityKey identityKey) {
 		this(account, store, remoteAddress);
-		this.fingerprint = fingerprint.replaceAll("\\s","");
+		this.identityKey = identityKey;
 	}
 
 	public XmppAxolotlSession(Account account, SQLiteAxolotlStore store, AxolotlAddress remoteAddress) {
@@ -125,7 +126,11 @@ public class XmppAxolotlSession {
 	}
 
 	public String getFingerprint() {
-		return fingerprint;
+		return identityKey == null ? null : identityKey.getFingerprint().replaceAll("\\s", "");
+	}
+
+	public IdentityKey getIdentityKey() {
+		return identityKey;
 	}
 
 	public AxolotlAddress getRemoteAddress() {
@@ -141,11 +146,11 @@ public class XmppAxolotlSession {
 	}
 
 	protected void setTrust(Trust trust) {
-		sqLiteAxolotlStore.setFingerprintTrust(fingerprint, trust);
+		sqLiteAxolotlStore.setFingerprintTrust(getFingerprint(), trust);
 	}
 
 	protected Trust getTrust() {
-		Trust trust = sqLiteAxolotlStore.getFingerprintTrust(fingerprint);
+		Trust trust = sqLiteAxolotlStore.getFingerprintTrust(getFingerprint());
 		return (trust == null) ? Trust.UNDECIDED : trust;
 	}
 
@@ -164,11 +169,11 @@ public class XmppAxolotlSession {
 					try {
 						PreKeyWhisperMessage message = new PreKeyWhisperMessage(encryptedKey);
 						Log.i(Config.LOGTAG, AxolotlService.getLogprefix(account) + "PreKeyWhisperMessage received, new session ID:" + message.getSignedPreKeyId() + "/" + message.getPreKeyId());
-						String fingerprint = message.getIdentityKey().getFingerprint().replaceAll("\\s", "");
-						if (this.fingerprint != null && !this.fingerprint.equals(fingerprint)) {
-							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.fingerprint + ", received message with fingerprint " + fingerprint);
+						IdentityKey msgIdentityKey = message.getIdentityKey();
+						if (this.identityKey != null && !this.identityKey.equals(msgIdentityKey)) {
+							Log.e(Config.LOGTAG, AxolotlService.getLogprefix(account) + "Had session with fingerprint " + this.getFingerprint() + ", received message with fingerprint " + msgIdentityKey.getFingerprint());
 						} else {
-							this.fingerprint = fingerprint;
+							this.identityKey = msgIdentityKey;
 							plaintext = cipher.decrypt(message);
 							if (message.getPreKeyId().isPresent()) {
 								preKeyId = message.getPreKeyId().get();