store identity key in XmppAxolotlSession instead of the fingerprint

This commit is contained in:
Daniel Gultsch 2015-12-19 15:44:11 +01:00
parent ade89beb96
commit 534013fd0c
2 changed files with 27 additions and 23 deletions

View File

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

View File

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