Fix trust status for outgoing messages

Tag sent messages with own fingerprint, set own fingerprint as always
trusted, include own fingerprint in database trust search, explicitly
reset trust colorfilter
This commit is contained in:
Andreas Straub 2015-07-15 16:32:42 +02:00
parent e8ec2ee628
commit 4038af2f47
5 changed files with 43 additions and 28 deletions

View file

@ -267,12 +267,12 @@ public class AxolotlService {
return true; return true;
} }
public Trust getFingerprintTrust(String name, String fingerprint) { public Trust getFingerprintTrust(String fingerprint) {
return mXmppConnectionService.databaseBackend.isIdentityKeyTrusted(account, name, fingerprint); return mXmppConnectionService.databaseBackend.isIdentityKeyTrusted(account, fingerprint);
} }
public void setFingerprintTrust(String name, String fingerprint, Trust trust) { public void setFingerprintTrust(String fingerprint, Trust trust) {
mXmppConnectionService.databaseBackend.setIdentityKeyTrust(account, name, fingerprint, trust); mXmppConnectionService.databaseBackend.setIdentityKeyTrust(account, fingerprint, trust);
} }
// -------------------------------------- // --------------------------------------
@ -844,12 +844,12 @@ public class AxolotlService {
return sessions.hasAny(address) || return sessions.hasAny(address) ||
( deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty()); ( deviceIds.containsKey(jid) && !deviceIds.get(jid).isEmpty());
} }
public SQLiteAxolotlStore.Trust getFingerprintTrust(String name, String fingerprint) { public SQLiteAxolotlStore.Trust getFingerprintTrust(String fingerprint) {
return axolotlStore.getFingerprintTrust(name, fingerprint); return axolotlStore.getFingerprintTrust(fingerprint);
} }
public void setFingerprintTrust(String name, String fingerprint, SQLiteAxolotlStore.Trust trust) { public void setFingerprintTrust(String fingerprint, SQLiteAxolotlStore.Trust trust) {
axolotlStore.setFingerprintTrust(name, fingerprint, trust); axolotlStore.setFingerprintTrust(fingerprint, trust);
} }
private void buildSessionFromPEP(final Conversation conversation, final AxolotlAddress address) { private void buildSessionFromPEP(final Conversation conversation, final AxolotlAddress address) {

View file

@ -785,21 +785,29 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return getIdentityKeyCursor(account, name, own, null); return getIdentityKeyCursor(account, name, own, null);
} }
private Cursor getIdentityKeyCursor(Account account, String name, boolean own, String fingerprint) { private Cursor getIdentityKeyCursor(Account account, String fingerprint) {
return getIdentityKeyCursor(account, null, null, fingerprint);
}
private Cursor getIdentityKeyCursor(Account account, String name, Boolean own, String fingerprint) {
final SQLiteDatabase db = this.getReadableDatabase(); final SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {AxolotlService.SQLiteAxolotlStore.TRUSTED, String[] columns = {AxolotlService.SQLiteAxolotlStore.TRUSTED,
AxolotlService.SQLiteAxolotlStore.KEY}; AxolotlService.SQLiteAxolotlStore.KEY};
ArrayList<String> selectionArgs = new ArrayList<>(4); ArrayList<String> selectionArgs = new ArrayList<>(4);
selectionArgs.add(account.getUuid()); selectionArgs.add(account.getUuid());
String selectionString = AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ?";
if (name != null){
selectionArgs.add(name); selectionArgs.add(name);
selectionArgs.add(own?"1":"0"); selectionString += " AND " +AxolotlService.SQLiteAxolotlStore.NAME + " = ?";
String selectionString = AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ? AND " }
+ AxolotlService.SQLiteAxolotlStore.NAME + " = ? AND "
+ AxolotlService.SQLiteAxolotlStore.OWN + " = ? ";
if (fingerprint != null){ if (fingerprint != null){
selectionArgs.add(fingerprint); selectionArgs.add(fingerprint);
selectionString += " AND " +AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ?"; selectionString += " AND " +AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ?";
} }
if (own != null){
selectionArgs.add(own?"1":"0");
selectionString += " AND " +AxolotlService.SQLiteAxolotlStore.OWN + " = ?";
}
Cursor cursor = db.query(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, Cursor cursor = db.query(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME,
columns, columns,
selectionString, selectionString,
@ -842,6 +850,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized) { private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized) {
storeIdentityKey(account, name, own, fingerprint, base64Serialized, AxolotlService.SQLiteAxolotlStore.Trust.UNDECIDED);
}
private void storeIdentityKey(Account account, String name, boolean own, String fingerprint, String base64Serialized, AxolotlService.SQLiteAxolotlStore.Trust trusted) {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(AxolotlService.SQLiteAxolotlStore.ACCOUNT, account.getUuid()); values.put(AxolotlService.SQLiteAxolotlStore.ACCOUNT, account.getUuid());
@ -849,11 +861,12 @@ public class DatabaseBackend extends SQLiteOpenHelper {
values.put(AxolotlService.SQLiteAxolotlStore.OWN, own ? 1 : 0); values.put(AxolotlService.SQLiteAxolotlStore.OWN, own ? 1 : 0);
values.put(AxolotlService.SQLiteAxolotlStore.FINGERPRINT, fingerprint); values.put(AxolotlService.SQLiteAxolotlStore.FINGERPRINT, fingerprint);
values.put(AxolotlService.SQLiteAxolotlStore.KEY, base64Serialized); values.put(AxolotlService.SQLiteAxolotlStore.KEY, base64Serialized);
values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trusted.ordinal());
db.insert(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values); db.insert(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, null, values);
} }
public AxolotlService.SQLiteAxolotlStore.Trust isIdentityKeyTrusted(Account account, String name, String fingerprint) { public AxolotlService.SQLiteAxolotlStore.Trust isIdentityKeyTrusted(Account account, String fingerprint) {
Cursor cursor = getIdentityKeyCursor(account, name, false, fingerprint); Cursor cursor = getIdentityKeyCursor(account, fingerprint);
AxolotlService.SQLiteAxolotlStore.Trust trust = null; AxolotlService.SQLiteAxolotlStore.Trust trust = null;
if (cursor.getCount() > 0) { if (cursor.getCount() > 0) {
cursor.moveToFirst(); cursor.moveToFirst();
@ -864,18 +877,16 @@ public class DatabaseBackend extends SQLiteOpenHelper {
return trust; return trust;
} }
public boolean setIdentityKeyTrust(Account account, String name, String fingerprint, AxolotlService.SQLiteAxolotlStore.Trust trust) { public boolean setIdentityKeyTrust(Account account, String fingerprint, AxolotlService.SQLiteAxolotlStore.Trust trust) {
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
String[] selectionArgs = { String[] selectionArgs = {
account.getUuid(), account.getUuid(),
name,
fingerprint fingerprint
}; };
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trust.ordinal()); values.put(AxolotlService.SQLiteAxolotlStore.TRUSTED, trust.ordinal());
int rows = db.update(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, values, int rows = db.update(AxolotlService.SQLiteAxolotlStore.IDENTITIES_TABLENAME, values,
AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ? AND " AxolotlService.SQLiteAxolotlStore.ACCOUNT + " = ? AND "
+ AxolotlService.SQLiteAxolotlStore.NAME + " = ? AND "
+ AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ? ", + AxolotlService.SQLiteAxolotlStore.FINGERPRINT + " = ? ",
selectionArgs); selectionArgs);
return rows == 1; return rows == 1;
@ -886,7 +897,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
} }
public void storeOwnIdentityKeyPair(Account account, String name, IdentityKeyPair identityKeyPair) { public void storeOwnIdentityKeyPair(Account account, String name, IdentityKeyPair identityKeyPair) {
storeIdentityKey(account, name, true, identityKeyPair.getPublicKey().getFingerprint().replaceAll("\\s", ""), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT)); storeIdentityKey(account, name, true, identityKeyPair.getPublicKey().getFingerprint().replaceAll("\\s", ""), Base64.encodeToString(identityKeyPair.serialize(), Base64.DEFAULT), AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED);
} }
public void recreateAxolotlDb() { public void recreateAxolotlDb() {

View file

@ -759,6 +759,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
packet = account.getAxolotlService().fetchPacketFromCache(message); packet = account.getAxolotlService().fetchPacketFromCache(message);
if (packet == null && account.isOnlineAndConnected()) { if (packet == null && account.isOnlineAndConnected()) {
account.getAxolotlService().prepareMessage(message); account.getAxolotlService().prepareMessage(message);
message.setAxolotlFingerprint(account.getAxolotlService().getOwnPublicKey().getFingerprint().replaceAll("\\s", ""));
} }
break; break;
@ -789,6 +790,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
conversation.startOtrSession(message.getCounterpart().getResourcepart(), false); conversation.startOtrSession(message.getCounterpart().getResourcepart(), false);
} }
break; break;
case Message.ENCRYPTION_AXOLOTL:
message.setAxolotlFingerprint(account.getAxolotlService().getOwnPublicKey().getFingerprint().replaceAll("\\s", ""));
break;
} }
} }

View file

@ -393,7 +393,7 @@ public class ContactDetailsActivity extends XmppActivity implements OnAccountUpd
final String fingerprint = identityKey.getFingerprint().replaceAll("\\s", ""); final String fingerprint = identityKey.getFingerprint().replaceAll("\\s", "");
final Jid bareJid = contactJid.toBareJid(); final Jid bareJid = contactJid.toBareJid();
AxolotlService.SQLiteAxolotlStore.Trust trust = contact.getAccount().getAxolotlService() AxolotlService.SQLiteAxolotlStore.Trust trust = contact.getAccount().getAxolotlService()
.getFingerprintTrust(bareJid.toString(), fingerprint); .getFingerprintTrust(fingerprint);
switch (trust) { switch (trust) {
case TRUSTED: case TRUSTED:
removeButton.setVisibility(View.VISIBLE); removeButton.setVisibility(View.VISIBLE);
@ -413,7 +413,7 @@ public class ContactDetailsActivity extends XmppActivity implements OnAccountUpd
removeButton.setOnClickListener(new OnClickListener() { removeButton.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
axolotlService.setFingerprintTrust(bareJid.toString(), fingerprint, axolotlService.setFingerprintTrust(fingerprint,
AxolotlService.SQLiteAxolotlStore.Trust.UNTRUSTED); AxolotlService.SQLiteAxolotlStore.Trust.UNTRUSTED);
refreshUi(); refreshUi();
xmppConnectionService.updateConversationUi(); xmppConnectionService.updateConversationUi();
@ -422,7 +422,7 @@ public class ContactDetailsActivity extends XmppActivity implements OnAccountUpd
trustButton.setOnClickListener(new OnClickListener() { trustButton.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
axolotlService.setFingerprintTrust(bareJid.toString(), fingerprint, axolotlService.setFingerprintTrust(fingerprint,
AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED); AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED);
refreshUi(); refreshUi();
xmppConnectionService.updateConversationUi(); xmppConnectionService.updateConversationUi();

View file

@ -156,15 +156,15 @@ public class MessageAdapter extends ArrayAdapter<Message> {
viewHolder.indicator.setVisibility(View.GONE); viewHolder.indicator.setVisibility(View.GONE);
} else { } else {
viewHolder.indicator.setVisibility(View.VISIBLE); viewHolder.indicator.setVisibility(View.VISIBLE);
if (message.getMergedStatus() == Message.STATUS_RECEIVED if (message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
&& message.getEncryption() == Message.ENCRYPTION_AXOLOTL) {
AxolotlService.SQLiteAxolotlStore.Trust trust = message.getConversation() AxolotlService.SQLiteAxolotlStore.Trust trust = message.getConversation()
.getAccount().getAxolotlService().getFingerprintTrust( .getAccount().getAxolotlService().getFingerprintTrust(
message.getContact().getJid().toBareJid().toString(),
message.getAxolotlFingerprint()); message.getAxolotlFingerprint());
if(trust == null || trust != AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED) { if(trust == null || trust != AxolotlService.SQLiteAxolotlStore.Trust.TRUSTED) {
viewHolder.indicator.setColorFilter(Color.RED); viewHolder.indicator.setColorFilter(Color.RED);
} else {
viewHolder.indicator.clearColorFilter();
} }
} }
} }