mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-02-07 18:40:19 -05:00
db-overhaul: minor stuff, mostly ProviderHelper
This commit is contained in:
parent
2620e0bfc8
commit
59f4b4e3e7
@ -232,7 +232,7 @@ public class PgpDecryptVerify {
|
|||||||
updateProgress(R.string.progress_finding_key, currentProgress, 100);
|
updateProgress(R.string.progress_finding_key, currentProgress, 100);
|
||||||
|
|
||||||
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj;
|
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj;
|
||||||
secretKey = ProviderHelper.getPGPSecretKeyByKeyId(mContext, encData.getKeyID());
|
secretKey = ProviderHelper.getPGPSecretKeyRing(mContext, encData.getKeyID()).getSecretKey();
|
||||||
if (secretKey != null) {
|
if (secretKey != null) {
|
||||||
// secret key exists in database
|
// secret key exists in database
|
||||||
|
|
||||||
@ -362,7 +362,7 @@ public class PgpDecryptVerify {
|
|||||||
for (int i = 0; i < sigList.size(); ++i) {
|
for (int i = 0; i < sigList.size(); ++i) {
|
||||||
signature = sigList.get(i);
|
signature = sigList.get(i);
|
||||||
signatureKey = ProviderHelper
|
signatureKey = ProviderHelper
|
||||||
.getPGPPublicKeyByKeyId(mContext, signature.getKeyID());
|
.getPGPPublicKeyRing(mContext, signature.getKeyID()).getPublicKey();
|
||||||
if (signatureKeyId == 0) {
|
if (signatureKeyId == 0) {
|
||||||
signatureKeyId = signature.getKeyID();
|
signatureKeyId = signature.getKeyID();
|
||||||
}
|
}
|
||||||
@ -546,7 +546,7 @@ public class PgpDecryptVerify {
|
|||||||
PGPPublicKey signatureKey = null;
|
PGPPublicKey signatureKey = null;
|
||||||
for (int i = 0; i < sigList.size(); ++i) {
|
for (int i = 0; i < sigList.size(); ++i) {
|
||||||
signature = sigList.get(i);
|
signature = sigList.get(i);
|
||||||
signatureKey = ProviderHelper.getPGPPublicKeyByKeyId(mContext, signature.getKeyID());
|
signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, signature.getKeyID()).getPublicKey();
|
||||||
if (signatureKeyId == 0) {
|
if (signatureKeyId == 0) {
|
||||||
signatureKeyId = signature.getKeyID();
|
signatureKeyId = signature.getKeyID();
|
||||||
}
|
}
|
||||||
|
@ -61,44 +61,10 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class ProviderHelper {
|
public class ProviderHelper {
|
||||||
|
|
||||||
/**
|
public static Object getGenericData(Context context, Uri uri, String column) {
|
||||||
* Private helper method to get PGPKeyRing from database
|
return getGenericData(context, uri, new String[] { column }).get(column);
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
public static HashMap<String,Object> getGenericData(Context context, Uri uri, String[] proj) {
|
||||||
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
return keyRing;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
|
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
|
||||||
|
|
||||||
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
|
HashMap<String, Object> result = new HashMap<String, Object>(proj.length);
|
||||||
@ -135,20 +101,65 @@ public class ProviderHelper {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object getUnifiedData(Context context, long masterKeyId, String column) {
|
||||||
public static PGPPublicKey getPGPPublicKeyByKeyId(Context context, long keyId) {
|
return getUnifiedData(context, masterKeyId, new String[] { column }).get(column);
|
||||||
return getPGPPublicKeyRingWithKeyId(context, keyId).getPublicKey(keyId);
|
|
||||||
}
|
}
|
||||||
|
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) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PGPSecretKey getPGPSecretKeyByKeyId(Context context, long keyId) {
|
|
||||||
return getPGPSecretKeyRingWithKeyId(context, keyId).getSecretKey(keyId);
|
|
||||||
}
|
|
||||||
public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long 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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -332,24 +343,6 @@ public class ProviderHelper {
|
|||||||
return getMasterKeyId(context, queryUri) == masterKeyId;
|
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) {
|
public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds) {
|
||||||
ArrayList<String> output = new ArrayList<String>();
|
ArrayList<String> output = new ArrayList<String>();
|
||||||
|
|
||||||
|
@ -35,7 +35,8 @@ import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify;
|
|||||||
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult;
|
import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult;
|
||||||
import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt;
|
import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt;
|
||||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
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.provider.ProviderHelper;
|
||||||
import org.sufficientlysecure.keychain.remote.ui.RemoteServiceActivity;
|
import org.sufficientlysecure.keychain.remote.ui.RemoteServiceActivity;
|
||||||
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
|
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
|
||||||
@ -351,7 +352,7 @@ public class OpenPgpService extends RemoteService {
|
|||||||
try {
|
try {
|
||||||
long keyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0);
|
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();
|
Intent result = new Intent();
|
||||||
|
|
||||||
// If keys are not in db we return an additional PendingIntent
|
// If keys are not in db we return an additional PendingIntent
|
||||||
@ -460,7 +461,7 @@ public class OpenPgpService extends RemoteService {
|
|||||||
String currentPkg = getCurrentCallingPackage();
|
String currentPkg = getCurrentCallingPackage();
|
||||||
Set<Long> allowedKeyIds =
|
Set<Long> allowedKeyIds =
|
||||||
ProviderHelper.getAllKeyIdsForApp(mContext,
|
ProviderHelper.getAllKeyIdsForApp(mContext,
|
||||||
KeychainContract.ApiAccounts.buildBaseUri(currentPkg));
|
ApiAccounts.buildBaseUri(currentPkg));
|
||||||
return decryptAndVerifyImpl(data, input, output, allowedKeyIds);
|
return decryptAndVerifyImpl(data, input, output, allowedKeyIds);
|
||||||
} else if (OpenPgpApi.ACTION_GET_KEY.equals(action)) {
|
} else if (OpenPgpApi.ACTION_GET_KEY.equals(action)) {
|
||||||
return getKeyImpl(data);
|
return getKeyImpl(data);
|
||||||
|
@ -37,7 +37,7 @@ import org.spongycastle.openpgp.PGPSecretKeyRing;
|
|||||||
import org.sufficientlysecure.keychain.Id;
|
import org.sufficientlysecure.keychain.Id;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
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 org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -206,7 +206,7 @@ public class EncryptAsymmetricFragment extends Fragment {
|
|||||||
String uidExtra = "";
|
String uidExtra = "";
|
||||||
// See if we can get a user_id from a unified query
|
// See if we can get a user_id from a unified query
|
||||||
Object data = ProviderHelper.getUnifiedData(
|
Object data = ProviderHelper.getUnifiedData(
|
||||||
getActivity(), mSecretKeyId, KeychainContract.UserIds.USER_ID);
|
getActivity(), mSecretKeyId, KeyRings.USER_ID);
|
||||||
if(data instanceof String) {
|
if(data instanceof String) {
|
||||||
String chunks[] = ((String) data).split(" <", 2);
|
String chunks[] = ((String) data).split(" <", 2);
|
||||||
uid = chunks[0];
|
uid = chunks[0];
|
||||||
|
@ -266,9 +266,6 @@ public class KeyListFragment extends Fragment
|
|||||||
static final int INDEX_IS_REVOKED = 3;
|
static final int INDEX_IS_REVOKED = 3;
|
||||||
static final int INDEX_HAS_SECRET = 4;
|
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
|
@Override
|
||||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||||
// This is called when a new Loader needs to be created. This
|
// 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
|
// Now create and return a CursorLoader that will take care of
|
||||||
// creating a Cursor for the data being displayed.
|
// 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
|
@Override
|
||||||
|
@ -173,7 +173,9 @@ public class ViewKeyActivity extends ActionBarActivity {
|
|||||||
private void shareKey(Uri dataUri, boolean fingerprintOnly) {
|
private void shareKey(Uri dataUri, boolean fingerprintOnly) {
|
||||||
String content;
|
String content;
|
||||||
if (fingerprintOnly) {
|
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[]) {
|
if(blob instanceof byte[]) {
|
||||||
String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob);
|
String fingerprint = PgpKeyHelper.convertFingerprintToHex((byte[]) blob);
|
||||||
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint;
|
||||||
|
@ -116,7 +116,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
|||||||
secretKey = null;
|
secretKey = null;
|
||||||
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
|
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
|
||||||
} else {
|
} else {
|
||||||
secretKey = ProviderHelper.getPGPSecretKeyByKeyId(activity, secretKeyId);
|
secretKey = ProviderHelper.getPGPSecretKeyRing(activity, secretKeyId).getSecretKey();
|
||||||
|
|
||||||
if (secretKey == null) {
|
if (secretKey == null) {
|
||||||
alert.setTitle(R.string.title_key_not_found);
|
alert.setTitle(R.string.title_key_not_found);
|
||||||
|
@ -90,7 +90,9 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
|
|||||||
if (mFingerprintOnly) {
|
if (mFingerprintOnly) {
|
||||||
alert.setPositiveButton(R.string.btn_okay, null);
|
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[])) {
|
if(!(blob instanceof byte[])) {
|
||||||
// TODO error handling?!
|
// TODO error handling?!
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user