From 428e94bb57764729efa72887110194300e63b28e Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 3 Apr 2014 17:08:04 +0200 Subject: [PATCH] ditch useless PgpKeyHelper.getMasterKey methods --- .../keychain/pgp/PgpDecryptVerify.java | 8 +-- .../keychain/pgp/PgpKeyHelper.java | 28 -------- .../keychain/pgp/PgpSignEncrypt.java | 4 +- .../keychain/provider/ProviderHelper.java | 68 +++---------------- .../service/PassphraseCacheService.java | 2 +- .../keychain/ui/EditKeyActivity.java | 9 ++- .../ui/EncryptAsymmetricFragment.java | 4 +- 7 files changed, 23 insertions(+), 100 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java index 11b87fc97..77edd353c 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java @@ -241,7 +241,7 @@ public class PgpDecryptVerify { // TODO: improve this code! get master key directly! PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, encData.getKeyID()); - long masterKeyId = PgpKeyHelper.getMasterKey(secretKeyRing).getKeyID(); + long masterKeyId = secretKeyRing.getSecretKey().getKeyID(); Log.d(Constants.TAG, "encData.getKeyID():" + encData.getKeyID()); Log.d(Constants.TAG, "allowedKeyIds: " + mAllowedKeyIds); Log.d(Constants.TAG, "masterKeyId: " + masterKeyId); @@ -375,7 +375,7 @@ public class PgpDecryptVerify { PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId( mContext, signatureKeyId); if (signKeyRing != null) { - userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signKeyRing)); + userId = PgpKeyHelper.getMainUserId(signKeyRing.getPublicKey()); } signatureResult.setUserId(userId); break; @@ -559,7 +559,7 @@ public class PgpDecryptVerify { PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(mContext, signatureKeyId); if (signKeyRing != null) { - userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signKeyRing)); + userId = PgpKeyHelper.getMainUserId(signKeyRing.getPublicKey()); } signatureResult.setUserId(userId); break; @@ -624,7 +624,7 @@ public class PgpDecryptVerify { signatureKeyId); PGPPublicKey mKey = null; if (signKeyRing != null) { - mKey = PgpKeyHelper.getMasterKey(signKeyRing); + mKey = signKeyRing.getPublicKey(); } if (signature.getKeyID() != mKey.getKeyID()) { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java index 290c8870b..9a97bc717 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java @@ -60,34 +60,6 @@ public class PgpKeyHelper { return key.getPublicKey().getCreationTime(); } - @SuppressWarnings("unchecked") - public static PGPPublicKey getMasterKey(PGPPublicKeyRing keyRing) { - if (keyRing == null) { - return null; - } - for (PGPPublicKey key : new IterableIterator(keyRing.getPublicKeys())) { - if (key.isMasterKey()) { - return key; - } - } - - return null; - } - - @SuppressWarnings("unchecked") - public static PGPSecretKey getMasterKey(PGPSecretKeyRing keyRing) { - if (keyRing == null) { - return null; - } - for (PGPSecretKey key : new IterableIterator(keyRing.getSecretKeys())) { - if (key.isMasterKey()) { - return key; - } - } - - return null; - } - @SuppressWarnings("unchecked") public static PGPSecretKey getKeyNum(PGPSecretKeyRing keyRing, long num) { long cnt = 0; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java index c1baed402..a864a165d 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -306,7 +306,7 @@ public class PgpSignEncrypt { signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); signatureGenerator.init(signatureType, signaturePrivateKey); - String userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signingKeyRing)); + String userId = PgpKeyHelper.getMainUserId(signingKeyRing.getSecretKey()); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, userId); signatureGenerator.setHashedSubpackets(spGen.generate()); @@ -505,7 +505,7 @@ public class PgpSignEncrypt { signatureGenerator.init(type, signaturePrivateKey); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - String userId = PgpKeyHelper.getMainUserId(PgpKeyHelper.getMasterKey(signingKeyRing)); + String userId = PgpKeyHelper.getMainUserId(signingKeyRing.getSecretKey()); spGen.setSignerUserID(false, userId); signatureGenerator.setHashedSubpackets(spGen.generate()); } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 3f96a51ef..3a0d910c5 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -260,30 +260,6 @@ public class ProviderHelper { return ContentProviderOperation.newInsert(uri).withValues(values).build(); } - /** - * Private helper method - */ - private static ArrayList getKeyRingsMasterKeyIds(Context context, Uri queryUri) { - Cursor cursor = context.getContentResolver().query(queryUri, - new String[]{KeyRings.MASTER_KEY_ID}, null, null, null); - - ArrayList masterKeyIds = new ArrayList(); - if (cursor != null) { - int masterKeyIdCol = cursor.getColumnIndex(KeyRings.MASTER_KEY_ID); - if (cursor.moveToFirst()) { - do { - masterKeyIds.add(cursor.getLong(masterKeyIdCol)); - } while (cursor.moveToNext()); - } - } - - if (cursor != null) { - cursor.close(); - } - - return masterKeyIds; - } - public static void deletePublicKeyRing(Context context, long masterKeyId) { ContentResolver cr = context.getContentResolver(); cr.delete(KeyRings.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null); @@ -294,38 +270,6 @@ public class ProviderHelper { cr.delete(KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId)), null, null); } - public static boolean getMasterKeyCanCertify(Context context, Uri queryUri) { - // TODO redo - - return false; - - /* - String[] projection = new String[]{ - KeyRings.MASTER_KEY_ID, - "(SELECT COUNT(sign_keys." + Keys._ID + ") FROM " + Tables.KEYS - + " AS sign_keys WHERE sign_keys." + Keys.KEY_RING_ROW_ID + " = " - + KeychainDatabase.Tables.KEY_RINGS + "." + KeyRings._ID - + " AND sign_keys." + Keys.CAN_CERTIFY + " = '1' AND " + Keys.IS_MASTER_KEY - + " = 1) AS sign",}; - - ContentResolver cr = context.getContentResolver(); - Cursor cursor = cr.query(queryUri, projection, null, null, null); - - long masterKeyId = -1; - if (cursor != null && cursor.moveToFirst()) { - int masterKeyIdCol = cursor.getColumnIndex("sign"); - - masterKeyId = cursor.getLong(masterKeyIdCol); - } - - if (cursor != null) { - cursor.close(); - } - - return (masterKeyId > 0); - */ - } - public static boolean hasSecretKeyByMasterKeyId(Context context, long masterKeyId) { Uri queryUri = KeyRings.buildSecretKeyRingUri(Long.toString(masterKeyId)); // see if we can get our master key id back from the uri @@ -336,8 +280,16 @@ public class ProviderHelper { * Get master key id of key */ public static long getMasterKeyId(Context context, Uri queryUri) { - String[] projection = new String[]{KeyRings.MASTER_KEY_ID}; - Cursor cursor = context.getContentResolver().query(queryUri, projection, null, null, null); + // try extracting from the uri first + try { + return Long.parseLong(queryUri.getPathSegments().get(1)); + } catch(NumberFormatException e) { + // didn't work? oh well. + } + + Cursor cursor = context.getContentResolver().query(queryUri, new String[] { + KeyRings.MASTER_KEY_ID + }, null, null, null); long masterKeyId = 0; try { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 63703a02b..0ec4c1239 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -174,7 +174,7 @@ public class PassphraseCacheService extends Service { if (keyRing == null) { return null; } - PGPSecretKey masterKey = PgpKeyHelper.getMasterKey(keyRing); + PGPSecretKey masterKey = keyRing.getSecretKey(); if (masterKey == null) { return null; } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index d7ff8c207..0caf60aa1 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -276,8 +276,6 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener // get master key id using row id long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri); - - mMasterCanSign = ProviderHelper.getMasterKeyCanCertify(this, mDataUri); finallyEdit(masterKeyId); } } @@ -347,8 +345,7 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener } return true; case R.id.menu_key_edit_delete: - long rowId= ProviderHelper.getRowId(this,mDataUri); - Uri convertUri = KeychainContract.KeyRings.buildSecretKeyRingUri(Long.toString(rowId)); + Uri convertUri = KeychainContract.KeyRings.buildSecretKeyRingUri(mDataUri); // Message is received after key is deleted Handler returnHandler = new Handler() { @Override @@ -374,7 +371,8 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener PGPSecretKey masterKey = null; mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId); if (mKeyRing != null) { - masterKey = PgpKeyHelper.getMasterKey(mKeyRing); + masterKey = mKeyRing.getSecretKey(); + mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey()); for (PGPSecretKey key : new IterableIterator(mKeyRing.getSecretKeys())) { mKeys.add(key); mKeysUsages.add(-1); // get usage when view is created @@ -382,6 +380,7 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener } else { Log.e(Constants.TAG, "Keyring not found with masterKeyId: " + masterKeyId); Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_LONG).show(); + // TODO } if (masterKey != null) { boolean isSet = false; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index 6b0e1d9f1..a8de40c70 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -145,7 +145,7 @@ public class EncryptAsymmetricFragment extends Fragment { preselectedSignatureKeyId); PGPSecretKey masterKey; if (keyRing != null) { - masterKey = PgpKeyHelper.getMasterKey(keyRing); + masterKey = keyRing.getSecretKey(); if (masterKey != null) { Vector signKeys = PgpKeyHelper.getUsableSigningKeys(keyRing); if (signKeys.size() > 0) { @@ -166,7 +166,7 @@ public class EncryptAsymmetricFragment extends Fragment { if (keyRing == null) { continue; } - masterKey = PgpKeyHelper.getMasterKey(keyRing); + masterKey = keyRing.getPublicKey(); if (masterKey == null) { continue; }