trust id -> autocrypt peer

This commit is contained in:
Vincent Breitmoser 2017-06-13 18:33:21 +02:00
parent 4e4912bd6d
commit 1e620e01aa
11 changed files with 193 additions and 199 deletions

View file

@ -1,6 +1,5 @@
/*
* Copyright (C) 2012-2014 Dominik Schürmann <dominik@dominikschuermann.de>
* Copyright (C) 2014-2016 Vincent Breitmoser <v.breitmoser@mugenguild.com>
* Copyright (C) 2017 Vincent Breitmoser <v.breitmoser@mugenguild.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -27,15 +26,15 @@ import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentity;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeer;
public class TrustIdentityDataAccessObject {
public class AutocryptPeerDataAccessObject {
private final SimpleContentResolverInterface mQueryInterface;
private final String packageName;
public TrustIdentityDataAccessObject(Context context, String packageName) {
public AutocryptPeerDataAccessObject(Context context, String packageName) {
this.packageName = packageName;
final ContentResolver contentResolver = context.getContentResolver();
@ -63,18 +62,18 @@ public class TrustIdentityDataAccessObject {
};
}
public TrustIdentityDataAccessObject(SimpleContentResolverInterface queryInterface, String packageName) {
public AutocryptPeerDataAccessObject(SimpleContentResolverInterface queryInterface, String packageName) {
mQueryInterface = queryInterface;
this.packageName = packageName;
}
public Long getMasterKeyIdForTrustId(String trustId) {
public Long getMasterKeyIdForAutocryptPeer(String autocryptId) {
Cursor cursor = mQueryInterface.query(
ApiTrustIdentity.buildByPackageNameAndTrustId(packageName, trustId), null, null, null, null);
ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId), null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
int masterKeyIdColumn = cursor.getColumnIndex(ApiTrustIdentity.MASTER_KEY_ID);
int masterKeyIdColumn = cursor.getColumnIndex(ApiAutocryptPeer.MASTER_KEY_ID);
return cursor.getLong(masterKeyIdColumn);
}
} finally {
@ -86,13 +85,13 @@ public class TrustIdentityDataAccessObject {
return null;
}
public Date getLastUpdateForTrustId(String trustId) {
Cursor cursor = mQueryInterface.query(ApiTrustIdentity.buildByPackageNameAndTrustId(packageName, trustId),
public Date getLastUpdateForAutocryptPeer(String autocryptId) {
Cursor cursor = mQueryInterface.query(ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId),
null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
long lastUpdated = cursor.getColumnIndex(ApiTrustIdentity.LAST_UPDATED);
long lastUpdated = cursor.getColumnIndex(ApiAutocryptPeer.LAST_UPDATED);
return new Date(lastUpdated);
}
} finally {
@ -103,15 +102,15 @@ public class TrustIdentityDataAccessObject {
return null;
}
public void setMasterKeyIdForTrustId(String trustId, long masterKeyId, Date date) {
Date lastUpdated = getLastUpdateForTrustId(trustId);
public void setMasterKeyIdForAutocryptPeer(String autocryptId, long masterKeyId, Date date) {
Date lastUpdated = getLastUpdateForAutocryptPeer(autocryptId);
if (lastUpdated != null && lastUpdated.after(date)) {
throw new IllegalArgumentException("Database entry was newer than the one to be inserted! Cannot backdate");
}
ContentValues cv = new ContentValues();
cv.put(ApiTrustIdentity.MASTER_KEY_ID, masterKeyId);
cv.put(ApiTrustIdentity.LAST_UPDATED, date.getTime());
mQueryInterface.update(ApiTrustIdentity.buildByPackageNameAndTrustId(packageName, trustId), cv, null, null);
cv.put(ApiAutocryptPeer.MASTER_KEY_ID, masterKeyId);
cv.put(ApiAutocryptPeer.LAST_UPDATED, date.getTime());
mQueryInterface.update(ApiAutocryptPeer.buildByPackageNameAndAutocryptId(packageName, autocryptId), cv, null, null);
}
}

View file

@ -62,7 +62,7 @@ import org.sufficientlysecure.keychain.pgp.UncachedPublicKey;
import org.sufficientlysecure.keychain.pgp.WrappedSignature;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentity;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeer;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
@ -603,7 +603,7 @@ public class KeyWritableRepository extends KeyRepository {
android.util.Log.e(Constants.TAG, "Could not delete file!", e);
return false;
}
mContentResolver.delete(ApiTrustIdentity.buildByMasterKeyId(masterKeyId),null, null);
mContentResolver.delete(ApiAutocryptPeer.buildByMasterKeyId(masterKeyId),null, null);
int deletedRows = mContentResolver.delete(KeyRingData.buildPublicKeyRingUri(masterKeyId), null, null);
return deletedRows > 0;
}

View file

@ -95,7 +95,7 @@ public class KeychainContract {
String IDENTIFIER = "identifier";
}
interface ApiTrustIdentityColumns {
interface ApiAutocryptPeerColumns {
String PACKAGE_NAME = "package_name";
String IDENTIFIER = "identifier";
String LAST_UPDATED = "last_updated";
@ -131,7 +131,7 @@ public class KeychainContract {
public static final String PATH_BY_PACKAGE_NAME = "by_package_name";
public static final String PATH_BY_KEY_ID = "by_key_id";
public static final String BASE_TRUST_IDENTITIES = "trust_ids";
public static final String BASE_AUTOCRYPT_PEERS = "autocrypt_peers";
public static class KeyRings implements BaseColumns, KeysColumns, UserPacketsColumns {
public static final String MASTER_KEY_ID = KeysColumns.MASTER_KEY_ID;
@ -345,16 +345,16 @@ public class KeychainContract {
}
}
public static class ApiTrustIdentity implements ApiTrustIdentityColumns, BaseColumns {
public static class ApiAutocryptPeer implements ApiAutocryptPeerColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI_INTERNAL.buildUpon()
.appendPath(BASE_TRUST_IDENTITIES).build();
.appendPath(BASE_AUTOCRYPT_PEERS).build();
public static Uri buildByKeyUri(Uri uri) {
return CONTENT_URI.buildUpon().appendPath(PATH_BY_KEY_ID).appendPath(uri.getPathSegments().get(1)).build();
}
public static Uri buildByPackageNameAndTrustId(String packageName, String trustId) {
return CONTENT_URI.buildUpon().appendPath(PATH_BY_PACKAGE_NAME).appendPath(packageName).appendPath(trustId).build();
public static Uri buildByPackageNameAndAutocryptId(String packageName, String autocryptPeer) {
return CONTENT_URI.buildUpon().appendPath(PATH_BY_PACKAGE_NAME).appendPath(packageName).appendPath(autocryptPeer).build();
}
public static Uri buildByMasterKeyId(long masterKeyId) {

View file

@ -33,7 +33,7 @@ import android.provider.BaseColumns;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAppsAllowedKeysColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAppsColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentityColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeerColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.CertsColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingsColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeysColumns;
@ -66,7 +66,7 @@ public class KeychainDatabase extends SQLiteOpenHelper {
String API_APPS = "api_apps";
String API_ALLOWED_KEYS = "api_allowed_keys";
String OVERRIDDEN_WARNINGS = "overridden_warnings";
String API_TRUST_IDENTITIES = "api_trust_identities";
String API_AUTOCRYPT_PEERS = "api_autocrypt_peers";
}
private static final String CREATE_KEYRINGS_PUBLIC =
@ -158,15 +158,15 @@ public class KeychainDatabase extends SQLiteOpenHelper {
+ Tables.KEY_RINGS_PUBLIC + "(" + KeyRingsColumns.MASTER_KEY_ID + ") ON DELETE CASCADE"
+ ")";
private static final String CREATE_API_TRUST_IDENTITIES =
"CREATE TABLE IF NOT EXISTS " + Tables.API_TRUST_IDENTITIES + " ("
+ ApiTrustIdentityColumns.PACKAGE_NAME + " TEXT NOT NULL, "
+ ApiTrustIdentityColumns.IDENTIFIER + " TEXT NOT NULL, "
+ ApiTrustIdentityColumns.LAST_UPDATED + " INTEGER NOT NULL, "
+ ApiTrustIdentityColumns.MASTER_KEY_ID + " INTEGER NOT NULL, "
+ "PRIMARY KEY(" + ApiTrustIdentityColumns.PACKAGE_NAME + ", "
+ ApiTrustIdentityColumns.IDENTIFIER + "), "
+ "FOREIGN KEY(" + ApiTrustIdentityColumns.PACKAGE_NAME + ") REFERENCES "
private static final String CREATE_API_AUTOCRYPT_PEERS =
"CREATE TABLE IF NOT EXISTS " + Tables.API_AUTOCRYPT_PEERS + " ("
+ ApiAutocryptPeerColumns.PACKAGE_NAME + " TEXT NOT NULL, "
+ ApiAutocryptPeerColumns.IDENTIFIER + " TEXT NOT NULL, "
+ ApiAutocryptPeerColumns.LAST_UPDATED + " INTEGER NOT NULL, "
+ ApiAutocryptPeerColumns.MASTER_KEY_ID + " INTEGER NOT NULL, "
+ "PRIMARY KEY(" + ApiAutocryptPeerColumns.PACKAGE_NAME + ", "
+ ApiAutocryptPeerColumns.IDENTIFIER + "), "
+ "FOREIGN KEY(" + ApiAutocryptPeerColumns.PACKAGE_NAME + ") REFERENCES "
+ Tables.API_APPS + "(" + ApiAppsColumns.PACKAGE_NAME + ") ON DELETE CASCADE"
+ ")";
@ -213,7 +213,7 @@ public class KeychainDatabase extends SQLiteOpenHelper {
db.execSQL(CREATE_API_APPS);
db.execSQL(CREATE_API_APPS_ALLOWED_KEYS);
db.execSQL(CREATE_OVERRIDDEN_WARNINGS);
db.execSQL(CREATE_API_TRUST_IDENTITIES);
db.execSQL(CREATE_API_AUTOCRYPT_PEERS);
db.execSQL("CREATE INDEX keys_by_rank ON keys (" + KeysColumns.RANK + ");");
db.execSQL("CREATE INDEX uids_by_rank ON user_packets (" + UserPacketsColumns.RANK + ", "
@ -334,21 +334,16 @@ public class KeychainDatabase extends SQLiteOpenHelper {
db.execSQL("ALTER TABLE updated_keys ADD COLUMN seen_on_keyservers INTEGER;");
case 22:
db.execSQL("CREATE TABLE IF NOT EXISTS " + Tables.API_TRUST_IDENTITIES + " ("
+ ApiTrustIdentityColumns.PACKAGE_NAME + " TEXT NOT NULL, "
+ ApiTrustIdentityColumns.IDENTIFIER + " TEXT NOT NULL, "
+ ApiTrustIdentityColumns.LAST_UPDATED + " INTEGER NOT NULL, "
+ ApiTrustIdentityColumns.MASTER_KEY_ID + " INTEGER NOT NULL, "
+ "PRIMARY KEY(" + ApiTrustIdentityColumns.PACKAGE_NAME + ", "
+ ApiTrustIdentityColumns.IDENTIFIER + "), "
+ "FOREIGN KEY(" + ApiTrustIdentityColumns.MASTER_KEY_ID + ") REFERENCES "
+ Tables.KEY_RINGS_PUBLIC + "(" + KeyRingsColumns.MASTER_KEY_ID + ") ON DELETE CASCADE, "
+ "FOREIGN KEY(" + ApiTrustIdentityColumns.PACKAGE_NAME + ") REFERENCES "
+ Tables.API_APPS + "(" + ApiAppsColumns.PACKAGE_NAME + ") ON DELETE CASCADE"
db.execSQL("CREATE TABLE IF NOT EXISTS api_autocrypt_peers ("
+ "package_name TEXT NOT NULL, "
+ "identifier TEXT NOT NULL, "
+ "last_updated INTEGER NOT NULL, "
+ "master_key_id INTEGER NOT NULL, "
+ "PRIMARY KEY(package_name, identifier), "
+ "FOREIGN KEY(package_name) REFERENCES api_apps(package_name) ON DELETE CASCADE"
+ ")");
if (oldVersion == 18 || oldVersion == 19 || oldVersion == 20 || oldVersion == 21 || oldVersion == 22) {
// no consolidate for now, often crashes!
return;
}
}

View file

@ -22,7 +22,7 @@ import android.net.Uri;
import android.provider.BaseColumns;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentityColumns;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeerColumns;
public class KeychainExternalContract {
@ -35,7 +35,7 @@ public class KeychainExternalContract {
private static final Uri BASE_CONTENT_URI_EXTERNAL = Uri
.parse("content://" + CONTENT_AUTHORITY_EXTERNAL);
public static final String BASE_EMAIL_STATUS = "email_status";
public static final String BASE_TRUST_IDENTITIES = "trust_ids";
public static final String BASE_AUTOCRYPT_PEERS = "autocrypt_peers";
public static class EmailStatus implements BaseColumns {
@ -43,8 +43,8 @@ public class KeychainExternalContract {
public static final String USER_ID = "user_id";
public static final String USER_ID_STATUS = "email_status";
public static final String MASTER_KEY_ID = "master_key_id";
public static final String TRUST_ID_LAST_UPDATE = "trust_id_last_update";
public static final String TRUST_ID_STATUS = "trust_id_status";
public static final String AUTOCRYPT_PEER_LAST_SEEN = "autocrypt_peer_last_seen";
public static final String AUTOCRYPT_PEER_STATE = "autocrypt_peer_state";
public static final Uri CONTENT_URI = BASE_CONTENT_URI_EXTERNAL.buildUpon()
.appendPath(BASE_EMAIL_STATUS).build();
@ -53,16 +53,16 @@ public class KeychainExternalContract {
"vnd.android.cursor.dir/vnd.org.sufficientlysecure.keychain.provider.email_status";
}
public static class ApiTrustIdentity implements ApiTrustIdentityColumns, BaseColumns {
public static class ApiAutocryptPeer implements ApiAutocryptPeerColumns, BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI_EXTERNAL.buildUpon()
.appendPath(BASE_TRUST_IDENTITIES).build();
.appendPath(BASE_AUTOCRYPT_PEERS).build();
public static Uri buildByPackageNameUri(String packageName) {
return CONTENT_URI.buildUpon().appendEncodedPath(packageName).build();
}
public static Uri buildByPackageNameAndTrustIdUri(String packageName, String trustId) {
return CONTENT_URI.buildUpon().appendEncodedPath(packageName).appendEncodedPath(trustId).build();
public static Uri buildByPackageNameAndTrustIdUri(String packageName, String autocryptPeer) {
return CONTENT_URI.buildUpon().appendEncodedPath(packageName).appendEncodedPath(autocryptPeer).build();
}
}

View file

@ -35,7 +35,7 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.WrappedUserAttribute;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAllowedKeys;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiApps;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentity;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeer;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRingData;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
@ -203,11 +203,11 @@ public class KeychainProvider extends ContentProvider {
*
* </pre>
*/
matcher.addURI(authority, KeychainContract.BASE_TRUST_IDENTITIES + "/" +
matcher.addURI(authority, KeychainContract.BASE_AUTOCRYPT_PEERS + "/" +
KeychainContract.PATH_BY_KEY_ID + "/*", TRUST_IDS_BY_MASTER_KEY_ID);
matcher.addURI(authority, KeychainContract.BASE_TRUST_IDENTITIES + "/" +
matcher.addURI(authority, KeychainContract.BASE_AUTOCRYPT_PEERS + "/" +
KeychainContract.PATH_BY_PACKAGE_NAME + "/*", TRUST_IDS_BY_PACKAGE_NAME);
matcher.addURI(authority, KeychainContract.BASE_TRUST_IDENTITIES + "/" +
matcher.addURI(authority, KeychainContract.BASE_AUTOCRYPT_PEERS + "/" +
KeychainContract.PATH_BY_PACKAGE_NAME + "/*/*", TRUST_IDS_BY_PACKAGE_NAME_AND_TRUST_ID);
@ -343,7 +343,7 @@ public class KeychainProvider extends ContentProvider {
"(" + Tables.KEYS + "." + Keys.EXPIRY + " IS NOT NULL AND " + Tables.KEYS + "." + Keys.EXPIRY
+ " < " + new Date().getTime() / 1000 + ") AS " + KeyRings.IS_EXPIRED);
projectionMap.put(KeyRings.API_KNOWN_TO_PACKAGE_NAMES,
"GROUP_CONCAT(aTI." + ApiTrustIdentity.PACKAGE_NAME + ") AS "
"GROUP_CONCAT(aTI." + ApiAutocryptPeer.PACKAGE_NAME + ") AS "
+ KeyRings.API_KNOWN_TO_PACKAGE_NAMES);
qb.setProjectionMap(projectionMap);
@ -414,7 +414,7 @@ public class KeychainProvider extends ContentProvider {
+ " >= " + new Date().getTime() / 1000 + " )"
+ ")" : "")
+ (plist.contains(KeyRings.API_KNOWN_TO_PACKAGE_NAMES) ?
" LEFT JOIN " + Tables.API_TRUST_IDENTITIES + " AS aTI ON ("
" LEFT JOIN " + Tables.API_AUTOCRYPT_PEERS + " AS aTI ON ("
+"aTI." + Keys.MASTER_KEY_ID
+ " = " + Tables.KEYS + "." + Keys.MASTER_KEY_ID
+ ")" : "")
@ -672,32 +672,32 @@ public class KeychainProvider extends ContentProvider {
}
HashMap<String, String> projectionMap = new HashMap<>();
projectionMap.put(ApiTrustIdentity._ID, "oid AS " + ApiTrustIdentity._ID);
projectionMap.put(ApiTrustIdentity.PACKAGE_NAME, ApiTrustIdentity.PACKAGE_NAME);
projectionMap.put(ApiTrustIdentity.IDENTIFIER, ApiTrustIdentity.IDENTIFIER);
projectionMap.put(ApiTrustIdentity.MASTER_KEY_ID, ApiTrustIdentity.MASTER_KEY_ID);
projectionMap.put(ApiTrustIdentity.LAST_UPDATED, ApiTrustIdentity.LAST_UPDATED);
projectionMap.put(ApiAutocryptPeer._ID, "oid AS " + ApiAutocryptPeer._ID);
projectionMap.put(ApiAutocryptPeer.PACKAGE_NAME, ApiAutocryptPeer.PACKAGE_NAME);
projectionMap.put(ApiAutocryptPeer.IDENTIFIER, ApiAutocryptPeer.IDENTIFIER);
projectionMap.put(ApiAutocryptPeer.MASTER_KEY_ID, ApiAutocryptPeer.MASTER_KEY_ID);
projectionMap.put(ApiAutocryptPeer.LAST_UPDATED, ApiAutocryptPeer.LAST_UPDATED);
qb.setProjectionMap(projectionMap);
qb.setTables(Tables.API_TRUST_IDENTITIES);
qb.setTables(Tables.API_AUTOCRYPT_PEERS);
if (match == TRUST_IDS_BY_MASTER_KEY_ID) {
long masterKeyId = Long.parseLong(uri.getLastPathSegment());
selection = Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.MASTER_KEY_ID + " = ?";
selection = Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.MASTER_KEY_ID + " = ?";
selectionArgs = new String[] { Long.toString(masterKeyId) };
} else if (match == TRUST_IDS_BY_PACKAGE_NAME) {
String packageName = uri.getPathSegments().get(2);
selection = Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.PACKAGE_NAME + " = ?";
selection = Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.PACKAGE_NAME + " = ?";
selectionArgs = new String[] { packageName };
} else { // TRUST_IDS_BY_PACKAGE_NAME_AND_TRUST_ID
String packageName = uri.getPathSegments().get(2);
String trustId = uri.getPathSegments().get(3);
String autocryptPeer = uri.getPathSegments().get(3);
selection = Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.PACKAGE_NAME + " = ? AND " +
Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.IDENTIFIER + " = ?";
selectionArgs = new String[] { packageName, trustId };
selection = Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.PACKAGE_NAME + " = ? AND " +
Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.IDENTIFIER + " = ?";
selectionArgs = new String[] { packageName, autocryptPeer };
}
break;
@ -998,21 +998,21 @@ public class KeychainProvider extends ContentProvider {
break;
}
case TRUST_IDS_BY_PACKAGE_NAME_AND_TRUST_ID: {
Long masterKeyId = values.getAsLong(ApiTrustIdentity.MASTER_KEY_ID);
long updateTime = values.getAsLong(KeychainContract.ApiTrustIdentity.LAST_UPDATED);
Long masterKeyId = values.getAsLong(ApiAutocryptPeer.MASTER_KEY_ID);
long updateTime = values.getAsLong(ApiAutocryptPeer.LAST_UPDATED);
if (masterKeyId == null) {
throw new IllegalArgumentException("master_key_id must be a non-null value!");
}
ContentValues actualValues = new ContentValues();
String packageName = uri.getPathSegments().get(2);
actualValues.put(ApiTrustIdentity.PACKAGE_NAME, packageName);
actualValues.put(ApiTrustIdentity.IDENTIFIER, uri.getLastPathSegment());
actualValues.put(ApiTrustIdentity.MASTER_KEY_ID, masterKeyId);
actualValues.put(ApiTrustIdentity.LAST_UPDATED, updateTime);
actualValues.put(ApiAutocryptPeer.PACKAGE_NAME, packageName);
actualValues.put(ApiAutocryptPeer.IDENTIFIER, uri.getLastPathSegment());
actualValues.put(ApiAutocryptPeer.MASTER_KEY_ID, masterKeyId);
actualValues.put(ApiAutocryptPeer.LAST_UPDATED, updateTime);
try {
db.replace(Tables.API_TRUST_IDENTITIES, null, actualValues);
db.replace(Tables.API_AUTOCRYPT_PEERS, null, actualValues);
} finally {
db.close();
}

View file

@ -47,7 +47,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.provider.KeychainDatabase;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.provider.KeychainExternalContract;
import org.sufficientlysecure.keychain.provider.KeychainExternalContract.ApiTrustIdentity;
import org.sufficientlysecure.keychain.provider.KeychainExternalContract.ApiAutocryptPeer;
import org.sufficientlysecure.keychain.provider.KeychainExternalContract.EmailStatus;
import org.sufficientlysecure.keychain.provider.SimpleContentResolverInterface;
import org.sufficientlysecure.keychain.util.Log;
@ -55,7 +55,7 @@ import org.sufficientlysecure.keychain.util.Log;
public class KeychainExternalProvider extends ContentProvider implements SimpleContentResolverInterface {
private static final int EMAIL_STATUS = 101;
private static final int EMAIL_STATUS_INTERNAL = 102;
private static final int TRUST_IDENTITY = 201;
private static final int AUTOCRYPT_PEER = 201;
private static final int API_APPS = 301;
private static final int API_APPS_BY_PACKAGE_NAME = 302;
@ -86,7 +86,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
matcher.addURI(authority, KeychainExternalContract.BASE_EMAIL_STATUS, EMAIL_STATUS);
matcher.addURI(authority, KeychainExternalContract.BASE_EMAIL_STATUS + "/*", EMAIL_STATUS_INTERNAL);
matcher.addURI(authority, KeychainExternalContract.BASE_TRUST_IDENTITIES + "/*", TRUST_IDENTITY);
matcher.addURI(authority, KeychainExternalContract.BASE_AUTOCRYPT_PEERS + "/*", AUTOCRYPT_PEER);
// can only query status of calling app - for internal use only!
matcher.addURI(KeychainContract.CONTENT_AUTHORITY, KeychainContract.BASE_API_APPS + "/*", API_APPS_BY_PACKAGE_NAME);
@ -180,18 +180,18 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
+ " WHEN " + Certs.VERIFIED_SECRET + " THEN " + KeychainExternalContract.KEY_STATUS_VERIFIED
+ " WHEN NULL THEN NULL"
+ " END AS " + EmailStatus.USER_ID_STATUS);
projectionMap.put(EmailStatus.TRUST_ID_STATUS, "CASE ( MIN (certs_trust_id." + Certs.VERIFIED + " ) ) "
projectionMap.put(EmailStatus.AUTOCRYPT_PEER_STATE, "CASE ( MIN (certs_autocrypt_peer." + Certs.VERIFIED + " ) ) "
// remap to keep this provider contract independent from our internal representation
+ " WHEN " + Certs.VERIFIED_SELF + " THEN " + KeychainExternalContract.KEY_STATUS_UNVERIFIED
+ " WHEN " + Certs.VERIFIED_SECRET + " THEN " + KeychainExternalContract.KEY_STATUS_VERIFIED
+ " WHEN NULL THEN NULL"
+ " END AS " + EmailStatus.TRUST_ID_STATUS);
+ " END AS " + EmailStatus.AUTOCRYPT_PEER_STATE);
projectionMap.put(EmailStatus.MASTER_KEY_ID,
Tables.USER_PACKETS + "." + UserPackets.MASTER_KEY_ID + " AS " + EmailStatus.MASTER_KEY_ID);
projectionMap.put(EmailStatus.USER_ID,
Tables.USER_PACKETS + "." + UserPackets.USER_ID + " AS " + EmailStatus.USER_ID);
projectionMap.put(EmailStatus.TRUST_ID_LAST_UPDATE, Tables.API_TRUST_IDENTITIES + "." +
ApiTrustIdentity.LAST_UPDATED + " AS " + EmailStatus.TRUST_ID_LAST_UPDATE);
projectionMap.put(EmailStatus.AUTOCRYPT_PEER_LAST_SEEN, Tables.API_AUTOCRYPT_PEERS + "." +
ApiAutocryptPeer.LAST_UPDATED + " AS " + EmailStatus.AUTOCRYPT_PEER_LAST_SEEN);
qb.setProjectionMap(projectionMap);
if (projection == null) {
@ -208,12 +208,12 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
+ Tables.USER_PACKETS + "." + UserPackets.MASTER_KEY_ID + " = certs_user_id." + Certs.MASTER_KEY_ID
+ " AND " + Tables.USER_PACKETS + "." + UserPackets.RANK + " = certs_user_id." + Certs.RANK
+ ")"
+ " LEFT JOIN " + Tables.API_TRUST_IDENTITIES + " ON ("
+ Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.IDENTIFIER + " LIKE queried_addresses.address"
+ " AND " + Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.PACKAGE_NAME + " = \"" + callingPackageName + "\""
+ " LEFT JOIN " + Tables.API_AUTOCRYPT_PEERS + " ON ("
+ Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.IDENTIFIER + " LIKE queried_addresses.address"
+ " AND " + Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.PACKAGE_NAME + " = \"" + callingPackageName + "\""
+ ")"
+ " LEFT JOIN " + Tables.CERTS + " AS certs_trust_id ON ("
+ Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.MASTER_KEY_ID + " = certs_trust_id." + Certs.MASTER_KEY_ID
+ " LEFT JOIN " + Tables.CERTS + " AS certs_autocrypt_peer ON ("
+ Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.MASTER_KEY_ID + " = certs_autocrypt_peer." + Certs.MASTER_KEY_ID
+ ")"
);
// in case there are multiple verifying certificates
@ -232,7 +232,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
break;
}
case TRUST_IDENTITY: {
case AUTOCRYPT_PEER: {
boolean callerIsAllowed = mApiPermissionHelper.isAllowedIgnoreErrors();
if (!callerIsAllowed) {
throw new AccessControlException("An application must register before use of KeychainExternalProvider!");
@ -243,19 +243,19 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
}
HashMap<String, String> projectionMap = new HashMap<>();
projectionMap.put(ApiTrustIdentity._ID, "oid AS " + ApiTrustIdentity._ID);
projectionMap.put(ApiTrustIdentity.IDENTIFIER, ApiTrustIdentity.IDENTIFIER);
projectionMap.put(ApiTrustIdentity.MASTER_KEY_ID, ApiTrustIdentity.MASTER_KEY_ID);
projectionMap.put(ApiTrustIdentity.LAST_UPDATED, ApiTrustIdentity.LAST_UPDATED);
projectionMap.put(ApiAutocryptPeer._ID, "oid AS " + ApiAutocryptPeer._ID);
projectionMap.put(ApiAutocryptPeer.IDENTIFIER, ApiAutocryptPeer.IDENTIFIER);
projectionMap.put(ApiAutocryptPeer.MASTER_KEY_ID, ApiAutocryptPeer.MASTER_KEY_ID);
projectionMap.put(ApiAutocryptPeer.LAST_UPDATED, ApiAutocryptPeer.LAST_UPDATED);
qb.setProjectionMap(projectionMap);
qb.setTables(Tables.API_TRUST_IDENTITIES);
qb.setTables(Tables.API_AUTOCRYPT_PEERS);
// allow access to columns of the calling package exclusively!
qb.appendWhere(Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.PACKAGE_NAME +
qb.appendWhere(Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.PACKAGE_NAME +
" = " + mApiPermissionHelper.getCurrentCallingPackage());
qb.appendWhere(Tables.API_TRUST_IDENTITIES + "." + ApiTrustIdentity.IDENTIFIER + " = ");
qb.appendWhere(Tables.API_AUTOCRYPT_PEERS + "." + ApiAutocryptPeer.IDENTIFIER + " = ");
qb.appendWhereEscapeString(uri.getLastPathSegment());
break;
@ -325,7 +325,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
Log.v(Constants.TAG, "insert(uri=" + uri + ")");
int match = mUriMatcher.match(uri);
if (match != TRUST_IDENTITY) {
if (match != AUTOCRYPT_PEER) {
throw new UnsupportedOperationException();
}
@ -334,20 +334,20 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
throw new AccessControlException("An application must register before use of KeychainExternalProvider!");
}
Long masterKeyId = values.getAsLong(ApiTrustIdentity.MASTER_KEY_ID);
Long masterKeyId = values.getAsLong(ApiAutocryptPeer.MASTER_KEY_ID);
if (masterKeyId == null) {
throw new IllegalArgumentException("master_key_id must be a non-null value!");
}
ContentValues actualValues = new ContentValues();
actualValues.put(ApiTrustIdentity.PACKAGE_NAME, mApiPermissionHelper.getCurrentCallingPackage());
actualValues.put(ApiTrustIdentity.IDENTIFIER, uri.getLastPathSegment());
actualValues.put(ApiTrustIdentity.MASTER_KEY_ID, masterKeyId);
actualValues.put(ApiTrustIdentity.LAST_UPDATED, new Date().getTime() / 1000);
actualValues.put(ApiAutocryptPeer.PACKAGE_NAME, mApiPermissionHelper.getCurrentCallingPackage());
actualValues.put(ApiAutocryptPeer.IDENTIFIER, uri.getLastPathSegment());
actualValues.put(ApiAutocryptPeer.MASTER_KEY_ID, masterKeyId);
actualValues.put(ApiAutocryptPeer.LAST_UPDATED, new Date().getTime() / 1000);
SQLiteDatabase db = getDb().getWritableDatabase();
try {
db.insert(Tables.API_TRUST_IDENTITIES, null, actualValues);
db.insert(Tables.API_AUTOCRYPT_PEERS, null, actualValues);
return uri;
} finally {
db.close();
@ -359,7 +359,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
Log.v(Constants.TAG, "delete(uri=" + uri + ")");
int match = mUriMatcher.match(uri);
if (match != TRUST_IDENTITY || selection != null || selectionArgs != null) {
if (match != AUTOCRYPT_PEER || selection != null || selectionArgs != null) {
throw new UnsupportedOperationException();
}
@ -368,7 +368,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
throw new AccessControlException("An application must register before use of KeychainExternalProvider!");
}
String actualSelection = ApiTrustIdentity.PACKAGE_NAME + " = ? AND " + ApiTrustIdentity.IDENTIFIER + " = ?";
String actualSelection = ApiAutocryptPeer.PACKAGE_NAME + " = ? AND " + ApiAutocryptPeer.IDENTIFIER + " = ?";
String[] actualSelectionArgs = new String[] {
mApiPermissionHelper.getCurrentCallingPackage(),
uri.getLastPathSegment()
@ -376,7 +376,7 @@ public class KeychainExternalProvider extends ContentProvider implements SimpleC
SQLiteDatabase db = getDb().getWritableDatabase();
try {
return db.delete(Tables.API_TRUST_IDENTITIES, actualSelection, actualSelectionArgs);
return db.delete(Tables.API_AUTOCRYPT_PEERS, actualSelection, actualSelectionArgs);
} finally {
db.close();
}

View file

@ -44,10 +44,10 @@ import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.openintents.openpgp.IOpenPgpService;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpInlineKeyUpdate;
import org.openintents.openpgp.AutocryptPeerUpdate;
import org.openintents.openpgp.OpenPgpMetadata;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.OpenPgpSignatureResult.TrustIdentityResult;
import org.openintents.openpgp.OpenPgpSignatureResult.AutocryptPeerResult;
import org.openintents.openpgp.util.OpenPgpApi;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.operations.BackupOperation;
@ -73,7 +73,7 @@ import org.sufficientlysecure.keychain.provider.KeyWritableRepository;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.OverriddenWarningsRepository;
import org.sufficientlysecure.keychain.provider.TrustIdentityDataAccessObject;
import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject;
import org.sufficientlysecure.keychain.remote.OpenPgpServiceKeyIdExtractor.KeyIdResult;
import org.sufficientlysecure.keychain.remote.OpenPgpServiceKeyIdExtractor.KeyIdResultStatus;
import org.sufficientlysecure.keychain.service.BackupKeyringParcel;
@ -88,7 +88,7 @@ public class OpenPgpService extends Service {
public static final int API_VERSION_WITHOUT_SIGNATURE_ONLY_FLAG = 8;
public static final int API_VERSION_WITH_DECRYPTION_RESULT = 8;
public static final int API_VERSION_WITH_RESULT_NO_SIGNATURE = 8;
public static final int API_VERSION_WITH_TRUST_IDENTITIES = 12;
public static final int API_VERSION_WITH_AUTOCRYPT = 12;
public static final List<Integer> SUPPORTED_VERSIONS =
Collections.unmodifiableList(Arrays.asList(7, 8, 9, 10, 11, 12));
@ -360,9 +360,9 @@ public class OpenPgpService extends Service {
byte[] detachedSignature = data.getByteArrayExtra(OpenPgpApi.EXTRA_DETACHED_SIGNATURE);
String senderAddress = data.getStringExtra(OpenPgpApi.EXTRA_SENDER_ADDRESS);
TrustIdentityDataAccessObject trustIdentityDao = new TrustIdentityDataAccessObject(
AutocryptPeerDataAccessObject autocryptPeerentityDao = new AutocryptPeerDataAccessObject(
getBaseContext(), mApiPermissionHelper.getCurrentCallingPackage());
String senderTrustId = updateTrustIdStateFromIntent(data, trustIdentityDao);
updateAutocryptPeerStateFromIntent(data, autocryptPeerentityDao);
PgpDecryptVerifyOperation op = new PgpDecryptVerifyOperation(this, mKeyRepository, progressable);
@ -449,41 +449,41 @@ public class OpenPgpService extends Service {
mApiPendingIntentFactory.createSecurityProblemIntent(packageName, securityProblem, supportOverride));
}
private String updateTrustIdStateFromIntent(Intent data, TrustIdentityDataAccessObject trustIdentityDao)
private String updateAutocryptPeerStateFromIntent(Intent data, AutocryptPeerDataAccessObject autocryptPeerDao)
throws PgpGeneralException, IOException {
String trustId = data.getStringExtra(OpenPgpApi.EXTRA_TRUST_IDENTITY);
OpenPgpInlineKeyUpdate inlineKeyUpdate = data.getParcelableExtra(OpenPgpApi.EXTRA_INLINE_KEY_DATA);
String autocryptPeerId = data.getStringExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID);
AutocryptPeerUpdate inlineKeyUpdate = data.getParcelableExtra(OpenPgpApi.EXTRA_INLINE_KEY_DATA);
if (inlineKeyUpdate == null) {
return null;
}
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(inlineKeyUpdate.getKeyData());
if (uncachedKeyRing.isSecret()) {
Log.e(Constants.TAG, "Found secret key in trust id! - Ignoring");
Log.e(Constants.TAG, "Found secret key in autocrypt id! - Ignoring");
return null;
}
// this will merge if the key already exists - no worries!
KeyWritableRepository.createDatabaseReadWriteInteractor(this).savePublicKeyRing(uncachedKeyRing);
long inlineMasterKeyId = uncachedKeyRing.getMasterKeyId();
Date lastUpdate = trustIdentityDao.getLastUpdateForTrustId(trustId);
Date lastUpdate = autocryptPeerDao.getLastUpdateForAutocryptPeer(autocryptPeerId);
Date updateTimestamp = inlineKeyUpdate.getTimestamp();
Long trustedMasterKeyId = trustIdentityDao.getMasterKeyIdForTrustId(trustId);
Long autocryptMasterKeyId = autocryptPeerDao.getMasterKeyIdForAutocryptPeer(autocryptPeerId);
if (lastUpdate != null && lastUpdate.after(updateTimestamp)) {
Log.d(Constants.TAG, "Key for trust id is newer, ignoring other");
return trustId;
} else if (trustedMasterKeyId == null) {
Log.d(Constants.TAG, "No binding for trust id, pinning key");
trustIdentityDao.setMasterKeyIdForTrustId(trustId, inlineMasterKeyId, updateTimestamp);
} else if (inlineMasterKeyId == trustedMasterKeyId) {
Log.d(Constants.TAG, "Key for autocrypt peer is newer, ignoring other");
return autocryptPeerId;
} else if (autocryptMasterKeyId == null) {
Log.d(Constants.TAG, "No binding for autocrypt peer, pinning key");
autocryptPeerDao.setMasterKeyIdForAutocryptPeer(autocryptPeerId, inlineMasterKeyId, updateTimestamp);
} else if (inlineMasterKeyId == autocryptMasterKeyId) {
Log.d(Constants.TAG, "Key id is the same - doing nothing");
} else {
// TODO danger in result intent!
trustIdentityDao.setMasterKeyIdForTrustId(trustId, inlineMasterKeyId, updateTimestamp);
autocryptPeerDao.setMasterKeyIdForAutocryptPeer(autocryptPeerId, inlineMasterKeyId, updateTimestamp);
}
return trustId;
return autocryptPeerId;
}
private void processDecryptionResultForResultIntent(int targetApiVersion, Intent result,
@ -568,19 +568,19 @@ public class OpenPgpService extends Service {
}
}
String trustIdentity = data.getStringExtra(OpenPgpApi.EXTRA_TRUST_IDENTITY);
if (trustIdentity != null) {
if (targetApiVersion < API_VERSION_WITH_TRUST_IDENTITIES) {
throw new IllegalStateException("API version conflict, trust identities are supported v12 and up!");
String autocryptPeerentity = data.getStringExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID);
if (autocryptPeerentity != null) {
if (targetApiVersion < API_VERSION_WITH_AUTOCRYPT) {
throw new IllegalStateException("API version conflict, autocrypt is supported v12 and up!");
}
signatureResult = processTrustIdentityInfoToSignatureResult(signatureResult, trustIdentity);
signatureResult = processAutocryptPeerInfoToSignatureResult(signatureResult, autocryptPeerentity);
}
result.putExtra(OpenPgpApi.RESULT_SIGNATURE, signatureResult);
}
private OpenPgpSignatureResult processTrustIdentityInfoToSignatureResult(OpenPgpSignatureResult signatureResult,
String trustIdentity) {
private OpenPgpSignatureResult processAutocryptPeerInfoToSignatureResult(OpenPgpSignatureResult signatureResult,
String autocryptPeerentity) {
boolean hasValidSignature =
signatureResult.getResult() == OpenPgpSignatureResult.RESULT_VALID_KEY_CONFIRMED ||
signatureResult.getResult() == OpenPgpSignatureResult.RESULT_VALID_KEY_UNCONFIRMED;
@ -588,18 +588,18 @@ public class OpenPgpService extends Service {
return signatureResult;
}
TrustIdentityDataAccessObject trustIdentityDao = new TrustIdentityDataAccessObject(getBaseContext(),
AutocryptPeerDataAccessObject autocryptPeerentityDao = new AutocryptPeerDataAccessObject(getBaseContext(),
mApiPermissionHelper.getCurrentCallingPackage());
Long tofuTrustedMasterKeyId = trustIdentityDao.getMasterKeyIdForTrustId(trustIdentity);
Long autocryptPeerMasterKeyId = autocryptPeerentityDao.getMasterKeyIdForAutocryptPeer(autocryptPeerentity);
long masterKeyId = signatureResult.getKeyId();
if (tofuTrustedMasterKeyId == null) {
trustIdentityDao.setMasterKeyIdForTrustId(trustIdentity, masterKeyId, new Date());
return signatureResult.withTrustIdentityResult(TrustIdentityResult.NEW);
} else if (masterKeyId == tofuTrustedMasterKeyId) {
return signatureResult.withTrustIdentityResult(TrustIdentityResult.OK);
if (autocryptPeerMasterKeyId == null) {
autocryptPeerentityDao.setMasterKeyIdForAutocryptPeer(autocryptPeerentity, masterKeyId, new Date());
return signatureResult.withAutocryptPeerResult(AutocryptPeerResult.NEW);
} else if (masterKeyId == autocryptPeerMasterKeyId) {
return signatureResult.withAutocryptPeerResult(AutocryptPeerResult.OK);
} else {
return signatureResult.withTrustIdentityResult(TrustIdentityResult.MISMATCH);
return signatureResult.withAutocryptPeerResult(AutocryptPeerResult.MISMATCH);
}
}
@ -746,14 +746,14 @@ public class OpenPgpService extends Service {
}
}
private Intent updateTrustIdKeyImpl(Intent data) {
private Intent updateAutocryptPeerImpl(Intent data) {
try {
Intent result = new Intent();
String trustId = data.getStringExtra(OpenPgpApi.EXTRA_TRUST_IDENTITY);
OpenPgpInlineKeyUpdate inlineKeyUpdate = data.getParcelableExtra(OpenPgpApi.EXTRA_INLINE_KEY_DATA);
if (inlineKeyUpdate == null || trustId == null) {
throw new IllegalArgumentException("need to specify both trust_id and inline_key_data!");
String autocryptPeer = data.getStringExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID);
AutocryptPeerUpdate inlineKeyUpdate = data.getParcelableExtra(OpenPgpApi.EXTRA_INLINE_KEY_DATA);
if (inlineKeyUpdate == null || autocryptPeer == null) {
throw new IllegalArgumentException("need to specify both autocrypt_peer_id and inline_key_data!");
}
UncachedKeyRing uncachedKeyRing = UncachedKeyRing.decodeFromData(inlineKeyUpdate.getKeyData());
@ -761,10 +761,10 @@ public class OpenPgpService extends Service {
// this will merge if the key already exists - no worries!
KeyWritableRepository.createDatabaseReadWriteInteractor(this).savePublicKeyRing(uncachedKeyRing);
TrustIdentityDataAccessObject trustIdentityDao = new TrustIdentityDataAccessObject(getBaseContext(),
AutocryptPeerDataAccessObject autocryptPeerentityDao = new AutocryptPeerDataAccessObject(getBaseContext(),
mApiPermissionHelper.getCurrentCallingPackage());
Date lastUpdate = trustIdentityDao.getLastUpdateForTrustId(trustId);
Date lastUpdate = autocryptPeerentityDao.getLastUpdateForAutocryptPeer(autocryptPeer);
Date updateTimestamp = inlineKeyUpdate.getTimestamp();
boolean updateIsNewerThanLastUpdate = lastUpdate == null || lastUpdate.before(updateTimestamp);
@ -772,23 +772,23 @@ public class OpenPgpService extends Service {
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
return result;
}
Log.d(Constants.TAG, "Key for trust id is newer");
Log.d(Constants.TAG, "Key for autocrypt peer is newer");
Long trustedMasterKeyId = trustIdentityDao.getMasterKeyIdForTrustId(trustId);
if (trustedMasterKeyId == null) {
Log.d(Constants.TAG, "No binding for trust id, pinning key");
trustIdentityDao.setMasterKeyIdForTrustId(trustId, inlineMasterKeyId, updateTimestamp);
} else if (inlineMasterKeyId == trustedMasterKeyId) {
Long autocryptPeerMasterKeyId = autocryptPeerentityDao.getMasterKeyIdForAutocryptPeer(autocryptPeer);
if (autocryptPeerMasterKeyId == null) {
Log.d(Constants.TAG, "No binding for autocrypt peer, pinning key");
autocryptPeerentityDao.setMasterKeyIdForAutocryptPeer(autocryptPeer, inlineMasterKeyId, updateTimestamp);
} else if (inlineMasterKeyId == autocryptPeerMasterKeyId) {
Log.d(Constants.TAG, "Key id is the same - doing nothing");
} else {
// TODO danger in result intent!
trustIdentityDao.setMasterKeyIdForTrustId(trustId, inlineMasterKeyId, updateTimestamp);
autocryptPeerentityDao.setMasterKeyIdForAutocryptPeer(autocryptPeer, inlineMasterKeyId, updateTimestamp);
}
result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS);
return result;
} catch (Exception e) {
Log.d(Constants.TAG, "exception in updateTrustIdKeyImpl", e);
Log.d(Constants.TAG, "exception in updateAutocryptPeerImpl", e);
return createErrorResultIntent(OpenPgpError.GENERIC_ERROR, e.getMessage());
}
}
@ -964,8 +964,8 @@ public class OpenPgpService extends Service {
case OpenPgpApi.ACTION_BACKUP: {
return backupImpl(data, outputStream);
}
case OpenPgpApi.ACTION_UPDATE_TRUST_ID: {
return updateTrustIdKeyImpl(data);
case OpenPgpApi.ACTION_UPDATE_AUTOCRYPT_PEER: {
return updateAutocryptPeerImpl(data);
}
default: {
return null;

View file

@ -39,7 +39,7 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.linked.LinkedAttribute;
import org.sufficientlysecure.keychain.linked.UriAttribute;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiTrustIdentity;
import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAutocryptPeer;
import org.sufficientlysecure.keychain.provider.KeychainContract.Certs;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserPackets;
import org.sufficientlysecure.keychain.ui.keyview.loader.IdentityLoader.IdentityInfo;
@ -108,15 +108,15 @@ public class IdentityLoader extends AsyncTaskLoader<List<IdentityInfo>> {
}
private static final String[] TRUST_IDS_PROJECTION = new String[] {
ApiTrustIdentity._ID,
ApiTrustIdentity.PACKAGE_NAME,
ApiTrustIdentity.IDENTIFIER,
ApiAutocryptPeer._ID,
ApiAutocryptPeer.PACKAGE_NAME,
ApiAutocryptPeer.IDENTIFIER,
};
private static final int INDEX_PACKAGE_NAME = 1;
private static final int INDEX_TRUST_ID = 2;
private void correlateOrAddTrustIds(ArrayList<IdentityInfo> identities) {
Cursor cursor = contentResolver.query(ApiTrustIdentity.buildByMasterKeyId(masterKeyId),
Cursor cursor = contentResolver.query(ApiAutocryptPeer.buildByMasterKeyId(masterKeyId),
TRUST_IDS_PROJECTION, null, null, null);
if (cursor == null) {
Log.e(Constants.TAG, "Error loading trust ids!");
@ -126,19 +126,19 @@ public class IdentityLoader extends AsyncTaskLoader<List<IdentityInfo>> {
try {
while (cursor.moveToNext()) {
String packageName = cursor.getString(INDEX_PACKAGE_NAME);
String trustId = cursor.getString(INDEX_TRUST_ID);
String autocryptPeer = cursor.getString(INDEX_TRUST_ID);
Drawable drawable = packageIconGetter.getDrawableForPackageName(packageName);
Intent trustIdIntent = getTrustIdActivityIntentIfResolvable(packageName, trustId);
Intent autocryptPeerIntent = getTrustIdActivityIntentIfResolvable(packageName, autocryptPeer);
UserIdInfo associatedUserIdInfo = findUserIdMatchingTrustId(identities, trustId);
UserIdInfo associatedUserIdInfo = findUserIdMatchingTrustId(identities, autocryptPeer);
if (associatedUserIdInfo != null) {
int position = identities.indexOf(associatedUserIdInfo);
TrustIdInfo trustIdInfo = TrustIdInfo.create(associatedUserIdInfo, trustId, drawable, trustIdIntent);
identities.set(position, trustIdInfo);
TrustIdInfo autocryptPeerInfo = TrustIdInfo.create(associatedUserIdInfo, autocryptPeer, drawable, autocryptPeerIntent);
identities.set(position, autocryptPeerInfo);
} else {
TrustIdInfo trustIdInfo = TrustIdInfo.create(trustId, drawable, trustIdIntent);
identities.add(trustIdInfo);
TrustIdInfo autocryptPeerInfo = TrustIdInfo.create(autocryptPeer, drawable, autocryptPeerIntent);
identities.add(autocryptPeerInfo);
}
}
} finally {
@ -146,11 +146,11 @@ public class IdentityLoader extends AsyncTaskLoader<List<IdentityInfo>> {
}
}
private Intent getTrustIdActivityIntentIfResolvable(String packageName, String trustId) {
private Intent getTrustIdActivityIntentIfResolvable(String packageName, String autocryptPeer) {
Intent intent = new Intent();
intent.setAction(packageName + ".AUTOCRYPT_PEER_ACTION");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(OpenPgpApi.EXTRA_TRUST_IDENTITY, trustId);
intent.putExtra(OpenPgpApi.EXTRA_AUTOCRYPT_PEER_ID, autocryptPeer);
List<ResolveInfo> resolveInfos = getContext().getPackageManager().queryIntentActivities(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty()) {
@ -160,11 +160,11 @@ public class IdentityLoader extends AsyncTaskLoader<List<IdentityInfo>> {
}
}
private static UserIdInfo findUserIdMatchingTrustId(List<IdentityInfo> identities, String trustId) {
private static UserIdInfo findUserIdMatchingTrustId(List<IdentityInfo> identities, String autocryptPeer) {
for (IdentityInfo identityInfo : identities) {
if (identityInfo instanceof UserIdInfo) {
UserIdInfo userIdInfo = (UserIdInfo) identityInfo;
if (trustId.equals(userIdInfo.getEmail())) {
if (autocryptPeer.equals(userIdInfo.getEmail())) {
return userIdInfo;
}
}
@ -312,14 +312,14 @@ public class IdentityLoader extends AsyncTaskLoader<List<IdentityInfo>> {
@Nullable
public abstract Intent getTrustIdIntent();
static TrustIdInfo create(UserIdInfo userIdInfo, String trustId, Drawable appIcon, Intent trustIdIntent) {
static TrustIdInfo create(UserIdInfo userIdInfo, String autocryptPeer, Drawable appIcon, Intent autocryptPeerIntent) {
return new AutoValue_IdentityLoader_TrustIdInfo(userIdInfo.getRank(), userIdInfo.getVerified(),
userIdInfo.isPrimary(), trustId, appIcon, userIdInfo, trustIdIntent);
userIdInfo.isPrimary(), autocryptPeer, appIcon, userIdInfo, autocryptPeerIntent);
}
static TrustIdInfo create(String trustId, Drawable appIcon, Intent trustIdIntent) {
static TrustIdInfo create(String autocryptPeer, Drawable appIcon, Intent autocryptPeerIntent) {
return new AutoValue_IdentityLoader_TrustIdInfo(
0, Certs.VERIFIED_SELF, false, trustId, appIcon, null, trustIdIntent);
0, Certs.VERIFIED_SELF, false, autocryptPeer, appIcon, null, autocryptPeerIntent);
}
}

View file

@ -119,8 +119,8 @@ public class IdentitiesPresenter implements LoaderCallbacks<List<IdentityInfo>>
} else if (info instanceof UserIdInfo) {
showUserIdInfo((UserIdInfo) info);
} else if (info instanceof TrustIdInfo) {
Intent trustIdIntent = ((TrustIdInfo) info).getTrustIdIntent();
viewKeyMvpView.startActivity(trustIdIntent);
Intent autocryptPeerIntent = ((TrustIdInfo) info).getTrustIdIntent();
viewKeyMvpView.startActivity(autocryptPeerIntent);
}
}

View file

@ -24,7 +24,7 @@ import org.sufficientlysecure.keychain.provider.ApiDataAccessObject;
import org.sufficientlysecure.keychain.provider.KeyWritableRepository;
import org.sufficientlysecure.keychain.provider.KeychainExternalContract.EmailStatus;
import org.sufficientlysecure.keychain.provider.KeyRepositorySaveTest;
import org.sufficientlysecure.keychain.provider.TrustIdentityDataAccessObject;
import org.sufficientlysecure.keychain.provider.AutocryptPeerDataAccessObject;
import org.sufficientlysecure.keychain.service.CertifyActionsParcel;
import org.sufficientlysecure.keychain.service.CertifyActionsParcel.CertifyAction;
import org.sufficientlysecure.keychain.service.input.CryptoInputParcel;
@ -45,7 +45,7 @@ public class KeychainExternalProviderTest {
static final String USER_ID_SEC_1 = "twi <twi-sec@openkeychain.org>";
static final long KEY_ID_SECRET = 0x5D4DA4423C39122FL;
static final long KEY_ID_PUBLIC = 0x9A282CE2AB44A382L;
public static final String TRUST_ID = "tid";
public static final String AUTOCRYPT_PEER = "tid";
KeyWritableRepository databaseInteractor =
@ -53,7 +53,7 @@ public class KeychainExternalProviderTest {
ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
ApiPermissionHelper apiPermissionHelper;
ApiDataAccessObject apiDao;
private TrustIdentityDataAccessObject trustIdDao;
AutocryptPeerDataAccessObject autocryptPeerDao;
@Before
@ -67,7 +67,7 @@ public class KeychainExternalProviderTest {
apiDao = new ApiDataAccessObject(RuntimeEnvironment.application);
apiPermissionHelper = new ApiPermissionHelper(RuntimeEnvironment.application, apiDao);
trustIdDao = new TrustIdentityDataAccessObject(RuntimeEnvironment.application, PACKAGE_NAME);
autocryptPeerDao = new AutocryptPeerDataAccessObject(RuntimeEnvironment.application, PACKAGE_NAME);
apiDao.insertApiApp(new AppSettings(PACKAGE_NAME, PACKAGE_SIGNATURE));
}
@ -178,17 +178,17 @@ public class KeychainExternalProviderTest {
}
@Test
public void testQuery__trustId__withUnconfirmedKey() throws Exception {
public void testQuery__autocryptPeer__withUnconfirmedKey() throws Exception {
insertSecretKeyringFrom("/test-keys/testring.sec");
insertPublicKeyringFrom("/test-keys/testring.pub");
trustIdDao.setMasterKeyIdForTrustId("tid", KEY_ID_PUBLIC, new Date());
autocryptPeerDao.setMasterKeyIdForAutocryptPeer("tid", KEY_ID_PUBLIC, new Date());
Cursor cursor = contentResolver.query(
EmailStatus.CONTENT_URI, new String[] {
EmailStatus.EMAIL_ADDRESS, EmailStatus.USER_ID_STATUS, EmailStatus.USER_ID,
EmailStatus.TRUST_ID_STATUS },
null, new String [] { TRUST_ID }, null
EmailStatus.AUTOCRYPT_PEER_STATE },
null, new String [] { AUTOCRYPT_PEER }, null
);
assertNotNull(cursor);
@ -201,18 +201,18 @@ public class KeychainExternalProviderTest {
}
@Test
public void testQuery__trustId__withConfirmedKey() throws Exception {
public void testQuery__withAutocryptPeer__withConfirmedKey() throws Exception {
insertSecretKeyringFrom("/test-keys/testring.sec");
insertPublicKeyringFrom("/test-keys/testring.pub");
trustIdDao.setMasterKeyIdForTrustId("tid", KEY_ID_PUBLIC, new Date());
autocryptPeerDao.setMasterKeyIdForAutocryptPeer("tid", KEY_ID_PUBLIC, new Date());
certifyKey(KEY_ID_SECRET, KEY_ID_PUBLIC, USER_ID_1);
Cursor cursor = contentResolver.query(
EmailStatus.CONTENT_URI, new String[] {
EmailStatus.EMAIL_ADDRESS, EmailStatus.USER_ID_STATUS, EmailStatus.USER_ID,
EmailStatus.TRUST_ID_STATUS },
null, new String [] { TRUST_ID }, null
EmailStatus.AUTOCRYPT_PEER_STATE },
null, new String [] { AUTOCRYPT_PEER }, null
);
assertNotNull(cursor);
@ -225,7 +225,7 @@ public class KeychainExternalProviderTest {
}
@Test
public void testQuery__withTrustId() throws Exception {
public void testQuery__withAutocryptPeer() throws Exception {
insertSecretKeyringFrom("/test-keys/testring.sec");
insertPublicKeyringFrom("/test-keys/testring.pub");