db-overhaul: minor stuff, mostly ProviderHelper

This commit is contained in:
Vincent Breitmoser 2014-04-04 01:04:28 +02:00
parent 2620e0bfc8
commit 59f4b4e3e7
8 changed files with 74 additions and 79 deletions

View File

@ -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();
}

View File

@ -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 PGPPublicKey getPGPPublicKeyByKeyId(Context context, long keyId) {
return getPGPPublicKeyRingWithKeyId(context, keyId).getPublicKey(keyId);
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);
}
/** 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 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 PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId) {
// todo do
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
long masterKeyId = getMasterKeyId(context, uri);
if(masterKeyId != 0)
return getPGPPublicKeyRing(context, masterKeyId);
return null;
}
public static PGPSecretKey getPGPSecretKeyByKeyId(Context context, long keyId) {
return getPGPSecretKeyRingWithKeyId(context, keyId).getSecretKey(keyId);
}
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>();

View File

@ -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);

View File

@ -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];

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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;