diff options
author | Vincent Breitmoser <valodim@mugenguild.com> | 2014-04-04 01:04:28 +0200 |
---|---|---|
committer | Vincent Breitmoser <valodim@mugenguild.com> | 2014-04-04 01:05:53 +0200 |
commit | 59f4b4e3e7cc27981e66f140030bef9a3fce3d25 (patch) | |
tree | 5afefe6b60e47fabfebbfbae1a02535547bdef00 /OpenPGP-Keychain | |
parent | 2620e0bfc8cbbdfb304629dbcf8d3a9d2d0b35ae (diff) | |
download | open-keychain-59f4b4e3e7cc27981e66f140030bef9a3fce3d25.tar.gz open-keychain-59f4b4e3e7cc27981e66f140030bef9a3fce3d25.tar.bz2 open-keychain-59f4b4e3e7cc27981e66f140030bef9a3fce3d25.zip |
db-overhaul: minor stuff, mostly ProviderHelper
Diffstat (limited to 'OpenPGP-Keychain')
8 files changed, 73 insertions, 78 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 77edd353c..4b0a07d9b 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 @@ -232,7 +232,7 @@ public class PgpDecryptVerify { updateProgress(R.string.progress_finding_key, currentProgress, 100); PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj; - secretKey = ProviderHelper.getPGPSecretKeyByKeyId(mContext, encData.getKeyID()); + secretKey = ProviderHelper.getPGPSecretKeyRing(mContext, encData.getKeyID()).getSecretKey(); if (secretKey != null) { // secret key exists in database @@ -362,7 +362,7 @@ public class PgpDecryptVerify { for (int i = 0; i < sigList.size(); ++i) { signature = sigList.get(i); signatureKey = ProviderHelper - .getPGPPublicKeyByKeyId(mContext, signature.getKeyID()); + .getPGPPublicKeyRing(mContext, signature.getKeyID()).getPublicKey(); if (signatureKeyId == 0) { signatureKeyId = signature.getKeyID(); } @@ -546,7 +546,7 @@ public class PgpDecryptVerify { PGPPublicKey signatureKey = null; for (int i = 0; i < sigList.size(); ++i) { signature = sigList.get(i); - signatureKey = ProviderHelper.getPGPPublicKeyByKeyId(mContext, signature.getKeyID()); + signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, signature.getKeyID()).getPublicKey(); if (signatureKeyId == 0) { signatureKeyId = signature.getKeyID(); } 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 339e6a069..a81368988 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 @@ -61,44 +61,10 @@ import java.util.Set; public class ProviderHelper { - /** - * Private helper method to get PGPKeyRing from database - */ - public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) { - Cursor cursor = context.getContentResolver().query(queryUri, - new String[]{KeyRings._ID, KeyRingData.KEY_RING_DATA}, null, null, null); - - PGPKeyRing keyRing = null; - if (cursor != null && cursor.moveToFirst()) { - int keyRingDataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); - - byte[] data = cursor.getBlob(keyRingDataCol); - if (data != null) { - keyRing = PgpConversionHelper.BytesToPGPKeyRing(data); - } - } - - if (cursor != null) { - cursor.close(); - } - - return keyRing; + public static Object getGenericData(Context context, Uri uri, String column) { + return getGenericData(context, uri, new String[] { column }).get(column); } - - public static Object getUnifiedData(Context context, long masterKeyId, String column) { - return getUnifiedData(context, masterKeyId, new String[] { column }).get(column); - } - public static Object getUnifiedData(Context context, Uri uri, String column) { - return getUnifiedData(context, uri, new String[] { column }).get(column); - } - - public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj) { - return getUnifiedData(context, KeyRings.buildGenericKeyRingUri(Long.toString(masterKeyId)), proj); - } - - public static HashMap<String,Object> getUnifiedData(Context context, Uri uri, String[] proj) { - uri = KeyRings.buildUnifiedKeyRingUri(uri); - + public static HashMap<String,Object> getGenericData(Context context, Uri uri, String[] proj) { Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null); HashMap<String, Object> result = new HashMap<String, Object>(proj.length); @@ -135,20 +101,65 @@ public class ProviderHelper { return result; } + public static Object getUnifiedData(Context context, long masterKeyId, String column) { + return getUnifiedData(context, masterKeyId, new String[] { column }).get(column); + } + public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj) { + return getGenericData(context, KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj); + } - public static PGPPublicKey getPGPPublicKeyByKeyId(Context context, long keyId) { - return getPGPPublicKeyRingWithKeyId(context, keyId).getPublicKey(keyId); + /** Find the master key id related to a given query. The id will either be extracted from the + * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. + */ + public static long getMasterKeyId(Context context, Uri queryUri) { + // try extracting from the uri first + String firstSegment = queryUri.getPathSegments().get(1); + if(!firstSegment.equals("find")) try { + return Long.parseLong(firstSegment); + } catch(NumberFormatException e) { + // didn't work? oh well. + Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying..."); + } + Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID); + if(data instanceof Long) + return (Long) data; + // TODO better error handling? + return 0L; } - public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId) { - // todo do - return null; + + public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) { + Cursor cursor = context.getContentResolver().query(queryUri, + new String[]{KeyRings._ID, KeyRingData.KEY_RING_DATA}, null, null, null); + + PGPKeyRing keyRing = null; + if (cursor != null && cursor.moveToFirst()) { + int keyRingDataCol = cursor.getColumnIndex(KeyRingData.KEY_RING_DATA); + + byte[] data = cursor.getBlob(keyRingDataCol); + if (data != null) { + keyRing = PgpConversionHelper.BytesToPGPKeyRing(data); + } + } + + if (cursor != null) { + cursor.close(); + } + + return keyRing; } - public static PGPSecretKey getPGPSecretKeyByKeyId(Context context, long keyId) { - return getPGPSecretKeyRingWithKeyId(context, keyId).getSecretKey(keyId); + public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId) { + Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId)); + long masterKeyId = getMasterKeyId(context, uri); + if(masterKeyId != 0) + return getPGPPublicKeyRing(context, masterKeyId); + return null; } public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long keyId) { - // todo do + Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId)); + long masterKeyId = getMasterKeyId(context, uri); + if(masterKeyId != 0) + return getPGPSecretKeyRing(context, masterKeyId); return null; } @@ -332,24 +343,6 @@ public class ProviderHelper { return getMasterKeyId(context, queryUri) == masterKeyId; } - /** Find the master key id related to a given query. The id will either be extracted from the - * query, which should work for all specific /key_rings/ queries, or will be queried if it can't. - */ - public static long getMasterKeyId(Context context, Uri queryUri) { - // try extracting from the uri first - try { - return Long.parseLong(queryUri.getPathSegments().get(1)); - } catch(NumberFormatException e) { - // didn't work? oh well. - Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying..."); - } - Object data = getUnifiedData(context, queryUri, KeyRings.MASTER_KEY_ID); - if(data instanceof Long) - return (Long) data; - // TODO better error handling? - return 0L; - } - public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds) { ArrayList<String> output = new ArrayList<String>(); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 007fbc99b..ccae6b2f2 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -35,7 +35,8 @@ import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; +import org.sufficientlysecure.keychain.provider.KeychainContract.ApiAccounts; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.remote.ui.RemoteServiceActivity; import org.sufficientlysecure.keychain.service.PassphraseCacheService; @@ -351,7 +352,7 @@ public class OpenPgpService extends RemoteService { try { long keyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0); - if (ProviderHelper.getPGPPublicKeyByKeyId(this, keyId) == null) { + if (ProviderHelper.getPGPPublicKeyRing(this, keyId) == null) { Intent result = new Intent(); // If keys are not in db we return an additional PendingIntent @@ -460,7 +461,7 @@ public class OpenPgpService extends RemoteService { String currentPkg = getCurrentCallingPackage(); Set<Long> allowedKeyIds = ProviderHelper.getAllKeyIdsForApp(mContext, - KeychainContract.ApiAccounts.buildBaseUri(currentPkg)); + ApiAccounts.buildBaseUri(currentPkg)); return decryptAndVerifyImpl(data, input, output, allowedKeyIds); } else if (OpenPgpApi.ACTION_GET_KEY.equals(action)) { return getKeyImpl(data); 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 0b724d52c..f749a4c1c 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 @@ -37,7 +37,7 @@ import org.spongycastle.openpgp.PGPSecretKeyRing; import org.sufficientlysecure.keychain.Id; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; import java.util.HashMap; @@ -206,7 +206,7 @@ public class EncryptAsymmetricFragment extends Fragment { String uidExtra = ""; // See if we can get a user_id from a unified query Object data = ProviderHelper.getUnifiedData( - getActivity(), mSecretKeyId, KeychainContract.UserIds.USER_ID); + getActivity(), mSecretKeyId, KeyRings.USER_ID); if(data instanceof String) { String chunks[] = ((String) data).split(" <", 2); uid = chunks[0]; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index ecd742896..b688dec51 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -266,9 +266,6 @@ public class KeyListFragment extends Fragment static final int INDEX_IS_REVOKED = 3; static final int INDEX_HAS_SECRET = 4; - // show secret before public key, sort by user id otherwise - static final String SORT_ORDER = KeyRings.HAS_SECRET + " DESC, " + UserIds.USER_ID + " ASC"; - @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { // This is called when a new Loader needs to be created. This @@ -282,7 +279,7 @@ public class KeyListFragment extends Fragment } // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. - return new CursorLoader(getActivity(), baseUri, PROJECTION, where, whereArgs, SORT_ORDER); + return new CursorLoader(getActivity(), baseUri, PROJECTION, where, whereArgs, null); } @Override diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index b60e75fa9..5760f7d8c 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -173,7 +173,9 @@ public class ViewKeyActivity extends ActionBarActivity { private void shareKey(Uri dataUri, boolean fingerprintOnly) { String content; if (fingerprintOnly) { - Object blob = ProviderHelper.getUnifiedData(this, dataUri, KeychainContract.Keys.FINGERPRINT); + Object blob = ProviderHelper.getGenericData( + this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri), + KeychainContract.Keys.FINGERPRINT); if(blob instanceof byte[]) { String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob); content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index b8db470b4..05b571362 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -116,7 +116,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor secretKey = null; alert.setMessage(R.string.passphrase_for_symmetric_encryption); } else { - secretKey = ProviderHelper.getPGPSecretKeyByKeyId(activity, secretKeyId); + secretKey = ProviderHelper.getPGPSecretKeyRing(activity, secretKeyId).getSecretKey(); if (secretKey == null) { alert.setTitle(R.string.title_key_not_found); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java index dc9fa6235..b55645732 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java @@ -90,7 +90,9 @@ public class ShareQrCodeDialogFragment extends DialogFragment { if (mFingerprintOnly) { alert.setPositiveButton(R.string.btn_okay, null); - Object blob = ProviderHelper.getUnifiedData(getActivity(), dataUri, KeychainContract.Keys.FINGERPRINT); + Object blob = ProviderHelper.getGenericData( + getActivity(), KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri), + KeychainContract.Keys.FINGERPRINT); if(!(blob instanceof byte[])) { // TODO error handling?! return null; |