diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2014-05-14 16:02:28 +0200 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2014-05-14 16:02:28 +0200 |
commit | 2f95100d88954db389cba8e615390795d121c1c8 (patch) | |
tree | 1bf4ffb29bb83fac33ba2ed7c84fb1d6a8af2bd4 /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider | |
parent | 90ac60b6db0637fbefbce5cb2cd80a64f5bb708d (diff) | |
parent | 638554f2560b3c5fd36ca9b4ba205cb5a999b84b (diff) | |
download | open-keychain-2f95100d88954db389cba8e615390795d121c1c8.tar.gz open-keychain-2f95100d88954db389cba8e615390795d121c1c8.tar.bz2 open-keychain-2f95100d88954db389cba8e615390795d121c1c8.zip |
Merge remote-tracking branch 'origin/master' into wrapped-key-ring
Conflicts:
OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java
OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java
OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java
OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java
OpenKeychain/src/main/res/values/strings.xml
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider')
3 files changed, 178 insertions, 143 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java index 061f91176..68726d3e0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainDatabase.java @@ -255,53 +255,60 @@ public class KeychainDatabase extends SQLiteOpenHelper { } }.getReadableDatabase(); - Cursor c = null; + Cursor cursor = null; try { // we insert in two steps: first, all public keys that have secret keys - c = db.rawQuery("SELECT key_ring_data FROM key_rings WHERE type = 1 OR EXISTS (" + cursor = db.rawQuery("SELECT key_ring_data FROM key_rings WHERE type = 1 OR EXISTS (" + " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id" + " AND d2.type = 1) ORDER BY type ASC", null); - Log.d(Constants.TAG, "Importing " + c.getCount() + " secret keyrings from apg.db..."); - for (int i = 0; i < c.getCount(); i++) { - c.moveToPosition(i); - byte[] data = c.getBlob(0); - PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data); - ProviderHelper providerHelper = new ProviderHelper(context); - if (ring instanceof PGPPublicKeyRing) - providerHelper.saveKeyRing((PGPPublicKeyRing) ring); - else if (ring instanceof PGPSecretKeyRing) - providerHelper.saveKeyRing((PGPSecretKeyRing) ring); - else { - Log.e(Constants.TAG, "Unknown blob data type!"); + Log.d(Constants.TAG, "Importing " + cursor.getCount() + " secret keyrings from apg.db..."); + if (cursor != null) { + for (int i = 0; i < cursor.getCount(); i++) { + cursor.moveToPosition(i); + byte[] data = cursor.getBlob(0); + PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data); + ProviderHelper providerHelper = new ProviderHelper(context); + if (ring instanceof PGPPublicKeyRing) + providerHelper.saveKeyRing((PGPPublicKeyRing) ring); + else if (ring instanceof PGPSecretKeyRing) + providerHelper.saveKeyRing((PGPSecretKeyRing) ring); + else { + Log.e(Constants.TAG, "Unknown blob data type!"); + } } } + if (cursor != null) { + cursor.close(); + } // afterwards, insert all keys, starting with public keys that have secret keys, then // secret keys, then all others. this order is necessary to ensure all certifications // are recognized properly. - c = db.rawQuery("SELECT key_ring_data FROM key_rings ORDER BY (type = 0 AND EXISTS (" + cursor = db.rawQuery("SELECT key_ring_data FROM key_rings ORDER BY (type = 0 AND EXISTS (" + " SELECT 1 FROM key_rings d2 WHERE key_rings.master_key_id = d2.master_key_id AND" + " d2.type = 1)) DESC, type DESC", null); // import from old database - Log.d(Constants.TAG, "Importing " + c.getCount() + " keyrings from apg.db..."); - for (int i = 0; i < c.getCount(); i++) { - c.moveToPosition(i); - byte[] data = c.getBlob(0); - PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data); - ProviderHelper providerHelper = new ProviderHelper(context); - if (ring instanceof PGPPublicKeyRing) { - providerHelper.saveKeyRing((PGPPublicKeyRing) ring); - } else if (ring instanceof PGPSecretKeyRing) { - providerHelper.saveKeyRing((PGPSecretKeyRing) ring); - } else { - Log.e(Constants.TAG, "Unknown blob data type!"); + Log.d(Constants.TAG, "Importing " + cursor.getCount() + " keyrings from apg.db..."); + if (cursor != null) { + for (int i = 0; i < cursor.getCount(); i++) { + cursor.moveToPosition(i); + byte[] data = cursor.getBlob(0); + PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data); + ProviderHelper providerHelper = new ProviderHelper(context); + if (ring instanceof PGPPublicKeyRing) { + providerHelper.saveKeyRing((PGPPublicKeyRing) ring); + } else if (ring instanceof PGPSecretKeyRing) { + providerHelper.saveKeyRing((PGPSecretKeyRing) ring); + } else { + Log.e(Constants.TAG, "Unknown blob data type!"); + } } } } catch (IOException e) { Log.e(Constants.TAG, "Error importing apg.db!", e); } finally { - if (c != null) { - c.close(); + if (cursor != null) { + cursor.close(); } if (db != null) { db.close(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java index c67c2eca4..9f6314329 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/KeychainProvider.java @@ -567,20 +567,21 @@ public class KeychainProvider extends ContentProvider { } SQLiteDatabase db = getDb().getReadableDatabase(); - Cursor c = qb.query(db, projection, selection, selectionArgs, groupBy, having, orderBy); - - // Tell the cursor what uri to watch, so it knows when its source data changes - c.setNotificationUri(getContext().getContentResolver(), uri); + Cursor cursor = qb.query(db, projection, selection, selectionArgs, groupBy, having, orderBy); + if (cursor != null) { + // Tell the cursor what uri to watch, so it knows when its source data changes + cursor.setNotificationUri(getContext().getContentResolver(), uri); + } if (Constants.DEBUG) { Log.d(Constants.TAG, "Query: " + qb.buildQuery(projection, selection, selectionArgs, null, null, orderBy, null)); - Log.d(Constants.TAG, "Cursor: " + DatabaseUtils.dumpCursorToString(c)); + Log.d(Constants.TAG, "Cursor: " + DatabaseUtils.dumpCursorToString(cursor)); } - return c; + return cursor; } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 0784a0f33..18035eefe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -26,6 +26,7 @@ import android.database.Cursor; import android.database.DatabaseUtils; import android.net.Uri; import android.os.RemoteException; +import android.support.v4.util.LongSparseArray; import org.spongycastle.bcpg.ArmoredOutputStream; import org.spongycastle.bcpg.S2K; @@ -103,36 +104,38 @@ public class ProviderHelper { throws NotFoundException { Cursor cursor = mContentResolver.query(uri, proj, null, null, null); - HashMap<String, Object> result = new HashMap<String, Object>(proj.length); - if (cursor != null && cursor.moveToFirst()) { - int pos = 0; - for (String p : proj) { - switch (types[pos]) { - case FIELD_TYPE_NULL: - result.put(p, cursor.isNull(pos)); - break; - case FIELD_TYPE_INTEGER: - result.put(p, cursor.getLong(pos)); - break; - case FIELD_TYPE_FLOAT: - result.put(p, cursor.getFloat(pos)); - break; - case FIELD_TYPE_STRING: - result.put(p, cursor.getString(pos)); - break; - case FIELD_TYPE_BLOB: - result.put(p, cursor.getBlob(pos)); - break; + try { + HashMap<String, Object> result = new HashMap<String, Object>(proj.length); + if (cursor != null && cursor.moveToFirst()) { + int pos = 0; + for (String p : proj) { + switch (types[pos]) { + case FIELD_TYPE_NULL: + result.put(p, cursor.isNull(pos)); + break; + case FIELD_TYPE_INTEGER: + result.put(p, cursor.getLong(pos)); + break; + case FIELD_TYPE_FLOAT: + result.put(p, cursor.getFloat(pos)); + break; + case FIELD_TYPE_STRING: + result.put(p, cursor.getString(pos)); + break; + case FIELD_TYPE_BLOB: + result.put(p, cursor.getBlob(pos)); + break; + } + pos += 1; } - pos += 1; } - } - if (cursor != null) { - cursor.close(); + return result; + } finally { + if (cursor != null) { + cursor.close(); + } } - - return result; } public Object getUnifiedData(long masterKeyId, String column, int type) @@ -172,22 +175,24 @@ public class ProviderHelper { } @Deprecated - public Map<Long, PGPKeyRing> getPGPKeyRings(Uri queryUri) { + public LongSparseArray<PGPKeyRing> getPGPKeyRings(Uri queryUri) { Cursor cursor = mContentResolver.query(queryUri, new String[]{KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA}, null, null, null); - Map<Long, PGPKeyRing> result = new HashMap<Long, PGPKeyRing>(cursor.getCount()); - if (cursor != null && cursor.moveToFirst()) do { - long masterKeyId = cursor.getLong(0); - byte[] data = cursor.getBlob(1); - if (data != null) { - result.put(masterKeyId, PgpConversionHelper.BytesToPGPKeyRing(data)); + LongSparseArray<PGPKeyRing> result = new LongSparseArray<PGPKeyRing>(cursor.getCount()); + try { + if (cursor != null && cursor.moveToFirst()) do { + long masterKeyId = cursor.getLong(0); + byte[] data = cursor.getBlob(1); + if (data != null) { + result.put(masterKeyId, PgpConversionHelper.BytesToPGPKeyRing(data)); + } + } while (cursor.moveToNext()); + } finally { + if (cursor != null) { + cursor.close(); } - } while (cursor.moveToNext()); - - if (cursor != null) { - cursor.close(); } return result; @@ -255,11 +260,11 @@ public class ProviderHelper { @Deprecated public PGPKeyRing getPGPKeyRing(Uri queryUri) throws NotFoundException { - Map<Long, PGPKeyRing> result = getPGPKeyRings(queryUri); - if (result.isEmpty()) { + LongSparseArray<PGPKeyRing> result = getPGPKeyRings(queryUri); + if (result.size() == 0) { throw new NotFoundException("PGPKeyRing object not found!"); } else { - return result.values().iterator().next(); + return result.valueAt(0); } } @@ -312,7 +317,7 @@ public class ProviderHelper { } // get a list of owned secret keys, for verification filtering - Map<Long, PGPKeyRing> allKeyRings = getPGPKeyRings(KeyRingData.buildSecretKeyRingUri()); + LongSparseArray<PGPKeyRing> allKeyRings = getPGPKeyRings(KeyRingData.buildSecretKeyRingUri()); // special case: available secret keys verify themselves! if (secretRing != null) allKeyRings.put(secretRing.getSecretKey().getKeyID(), secretRing); @@ -350,7 +355,7 @@ public class ProviderHelper { } } // verify signatures from known private keys - if (allKeyRings.containsKey(certId)) { + if (allKeyRings.indexOfKey(certId) >= 0) { // mark them as verified cert.init(new JcaPGPContentVerifierBuilderProvider().setProvider( Constants.BOUNCY_CASTLE_PROVIDER_NAME), @@ -634,27 +639,29 @@ public class ProviderHelper { }, inMasterKeyList, null, null); } - if (cursor != null) { - int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); - int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); - if (cursor.moveToFirst()) { - do { - Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); - - byte[] data = cursor.getBlob(dataCol); - - // get actual keyring data blob and write it to ByteArrayOutputStream - try { - output.add(getKeyRingAsArmoredString(data)); - } catch (IOException e) { - Log.e(Constants.TAG, "IOException", e); - } - } while (cursor.moveToNext()); + try { + if (cursor != null) { + int masterIdCol = cursor.getColumnIndex(KeyRingData.MASTER_KEY_ID); + int dataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); + if (cursor.moveToFirst()) { + do { + Log.d(Constants.TAG, "masterKeyId: " + cursor.getLong(masterIdCol)); + + byte[] data = cursor.getBlob(dataCol); + + // get actual keyring data blob and write it to ByteArrayOutputStream + try { + output.add(getKeyRingAsArmoredString(data)); + } catch (IOException e) { + Log.e(Constants.TAG, "IOException", e); + } + } while (cursor.moveToNext()); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - } - - if (cursor != null) { - cursor.close(); } if (output.size() > 0) { @@ -668,17 +675,19 @@ public class ProviderHelper { Cursor cursor = mContentResolver.query(ApiApps.CONTENT_URI, null, null, null, null); ArrayList<String> packageNames = new ArrayList<String>(); - if (cursor != null) { - int packageNameCol = cursor.getColumnIndex(ApiApps.PACKAGE_NAME); - if (cursor.moveToFirst()) { - do { - packageNames.add(cursor.getString(packageNameCol)); - } while (cursor.moveToNext()); + try { + if (cursor != null) { + int packageNameCol = cursor.getColumnIndex(ApiApps.PACKAGE_NAME); + if (cursor.moveToFirst()) { + do { + packageNames.add(cursor.getString(packageNameCol)); + } while (cursor.moveToNext()); + } + } + } finally { + if (cursor != null) { + cursor.close(); } - } - - if (cursor != null) { - cursor.close(); } return packageNames; @@ -726,13 +735,19 @@ public class ProviderHelper { public AppSettings getApiAppSettings(Uri uri) { AppSettings settings = null; - Cursor cur = mContentResolver.query(uri, null, null, null, null); - if (cur != null && cur.moveToFirst()) { - settings = new AppSettings(); - settings.setPackageName(cur.getString( - cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME))); - settings.setPackageSignature(cur.getBlob( - cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_SIGNATURE))); + Cursor cursor = mContentResolver.query(uri, null, null, null, null); + try { + if (cursor != null && cursor.moveToFirst()) { + settings = new AppSettings(); + settings.setPackageName(cursor.getString( + cursor.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME))); + settings.setPackageSignature(cursor.getBlob( + cursor.getColumnIndex(KeychainContract.ApiApps.PACKAGE_SIGNATURE))); + } + } finally { + if (cursor != null) { + cursor.close(); + } } return settings; @@ -741,20 +756,26 @@ public class ProviderHelper { public AccountSettings getApiAccountSettings(Uri accountUri) { AccountSettings settings = null; - Cursor cur = mContentResolver.query(accountUri, null, null, null, null); - if (cur != null && cur.moveToFirst()) { - settings = new AccountSettings(); - - settings.setAccountName(cur.getString( - cur.getColumnIndex(KeychainContract.ApiAccounts.ACCOUNT_NAME))); - settings.setKeyId(cur.getLong( - cur.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID))); - settings.setCompression(cur.getInt( - cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.COMPRESSION))); - settings.setHashAlgorithm(cur.getInt( - cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.HASH_ALORITHM))); - settings.setEncryptionAlgorithm(cur.getInt( - cur.getColumnIndexOrThrow(KeychainContract.ApiAccounts.ENCRYPTION_ALGORITHM))); + Cursor cursor = mContentResolver.query(accountUri, null, null, null, null); + try { + if (cursor != null && cursor.moveToFirst()) { + settings = new AccountSettings(); + + settings.setAccountName(cursor.getString( + cursor.getColumnIndex(KeychainContract.ApiAccounts.ACCOUNT_NAME))); + settings.setKeyId(cursor.getLong( + cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID))); + settings.setCompression(cursor.getInt( + cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.COMPRESSION))); + settings.setHashAlgorithm(cursor.getInt( + cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.HASH_ALORITHM))); + settings.setEncryptionAlgorithm(cursor.getInt( + cursor.getColumnIndexOrThrow(KeychainContract.ApiAccounts.ENCRYPTION_ALGORITHM))); + } + } finally { + if (cursor != null) { + cursor.close(); + } } return settings; @@ -764,10 +785,16 @@ public class ProviderHelper { Set<Long> keyIds = new HashSet<Long>(); Cursor cursor = mContentResolver.query(uri, null, null, null, null); - if (cursor != null) { - int keyIdColumn = cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID); - while (cursor.moveToNext()) { - keyIds.add(cursor.getLong(keyIdColumn)); + try { + if (cursor != null) { + int keyIdColumn = cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID); + while (cursor.moveToNext()) { + keyIds.add(cursor.getLong(keyIdColumn)); + } + } + } finally { + if (cursor != null) { + cursor.close(); } } @@ -780,18 +807,18 @@ public class ProviderHelper { String[] projection = new String[]{ApiApps.PACKAGE_SIGNATURE}; Cursor cursor = mContentResolver.query(queryUri, projection, null, null, null); + try { + byte[] signature = null; + if (cursor != null && cursor.moveToFirst()) { + int signatureCol = 0; - byte[] signature = null; - if (cursor != null && cursor.moveToFirst()) { - int signatureCol = 0; - - signature = cursor.getBlob(signatureCol); - } - - if (cursor != null) { - cursor.close(); + signature = cursor.getBlob(signatureCol); + } + return signature; + } finally { + if (cursor != null) { + cursor.close(); + } } - - return signature; } } |