mirror of
https://github.com/moparisthebest/open-keychain
synced 2025-01-11 21:48:17 -05:00
Refactor ProviderHelper to be non-static using a constructor based on context (first commit to get context out of pgp classes)
This commit is contained in:
parent
bbd97cf800
commit
094fb698de
@ -51,7 +51,7 @@ public class ExportHelper {
|
||||
|
||||
public void deleteKey(Uri dataUri, Handler deleteHandler) {
|
||||
try {
|
||||
long masterKeyId = ProviderHelper.extractOrGetMasterKeyId(mActivity, dataUri);
|
||||
long masterKeyId = new ProviderHelper(mActivity).extractOrGetMasterKeyId(dataUri);
|
||||
|
||||
// Create a new Messenger for the communication back
|
||||
Messenger messenger = new Messenger(deleteHandler);
|
||||
|
@ -79,6 +79,7 @@ import java.util.Set;
|
||||
*/
|
||||
public class PgpDecryptVerify {
|
||||
private Context mContext;
|
||||
private ProviderHelper mProviderHelper;
|
||||
private InputData mData;
|
||||
private OutputStream mOutStream;
|
||||
|
||||
@ -90,6 +91,7 @@ public class PgpDecryptVerify {
|
||||
private PgpDecryptVerify(Builder builder) {
|
||||
// private Constructor can only be called from Builder
|
||||
this.mContext = builder.mContext;
|
||||
this.mProviderHelper = new ProviderHelper(mContext);
|
||||
this.mData = builder.mData;
|
||||
this.mOutStream = builder.mOutStream;
|
||||
|
||||
@ -243,11 +245,11 @@ public class PgpDecryptVerify {
|
||||
PGPSecretKeyRing secretKeyRing = null;
|
||||
try {
|
||||
// get master key id for this encryption key id
|
||||
masterKeyId = ProviderHelper.getMasterKeyId(mContext,
|
||||
masterKeyId = mProviderHelper.getMasterKeyId(
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(encData.getKeyID()))
|
||||
);
|
||||
// get actual keyring object based on master key id
|
||||
secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, masterKeyId);
|
||||
secretKeyRing = mProviderHelper.getPGPSecretKeyRing(masterKeyId);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
// continue with the next packet in the while loop
|
||||
continue;
|
||||
@ -393,17 +395,17 @@ public class PgpDecryptVerify {
|
||||
try {
|
||||
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
|
||||
Long.toString(sigList.get(i).getKeyID()));
|
||||
masterKeyId = ProviderHelper.getMasterKeyId(mContext, uri);
|
||||
masterKeyId = mProviderHelper.getMasterKeyId(uri);
|
||||
signatureIndex = i;
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.d(Constants.TAG, "key not found!");
|
||||
}
|
||||
}
|
||||
|
||||
if(masterKeyId == null) {
|
||||
if (masterKeyId == null) {
|
||||
try {
|
||||
signatureKey = ProviderHelper
|
||||
.getPGPPublicKeyRing(mContext, masterKeyId).getPublicKey();
|
||||
signatureKey = mProviderHelper
|
||||
.getPGPPublicKeyRing(masterKeyId).getPublicKey();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
// can't happen
|
||||
}
|
||||
@ -417,7 +419,7 @@ public class PgpDecryptVerify {
|
||||
signature.init(contentVerifierBuilderProvider, signatureKey);
|
||||
|
||||
} else {
|
||||
if(!sigList.isEmpty()) {
|
||||
if (!sigList.isEmpty()) {
|
||||
signatureResult.setKeyId(sigList.get(0).getKeyID());
|
||||
}
|
||||
|
||||
@ -489,7 +491,7 @@ public class PgpDecryptVerify {
|
||||
signatureResult.setSignatureOnly(false);
|
||||
|
||||
//Now check binding signatures
|
||||
boolean validKeyBinding = verifyKeyBinding(mContext, messageSignature, signatureKey);
|
||||
boolean validKeyBinding = verifyKeyBinding(messageSignature, signatureKey);
|
||||
boolean validSignature = signature.verify(messageSignature);
|
||||
|
||||
// TODO: implement CERTIFIED!
|
||||
@ -587,7 +589,7 @@ public class PgpDecryptVerify {
|
||||
signatureKeyId = signature.getKeyID();
|
||||
|
||||
// find data about this subkey
|
||||
HashMap<String, Object> data = ProviderHelper.getGenericData(mContext,
|
||||
HashMap<String, Object> data = mProviderHelper.getGenericData(
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(signature.getKeyID())),
|
||||
new String[]{KeyRings.MASTER_KEY_ID, KeyRings.USER_ID},
|
||||
new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_STRING});
|
||||
@ -600,7 +602,7 @@ public class PgpDecryptVerify {
|
||||
|
||||
// this one can't fail now (yay database constraints)
|
||||
try {
|
||||
signatureKey = ProviderHelper.getPGPPublicKeyRing(mContext, (Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
|
||||
signatureKey = mProviderHelper.getPGPPublicKeyRing((Long) data.get(KeyRings.MASTER_KEY_ID)).getPublicKey();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
}
|
||||
@ -644,7 +646,7 @@ public class PgpDecryptVerify {
|
||||
}
|
||||
|
||||
//Now check binding signatures
|
||||
boolean validKeyBinding = verifyKeyBinding(mContext, signature, signatureKey);
|
||||
boolean validKeyBinding = verifyKeyBinding(signature, signatureKey);
|
||||
boolean validSignature = signature.verify();
|
||||
|
||||
if (validKeyBinding && validSignature) {
|
||||
@ -664,14 +666,13 @@ public class PgpDecryptVerify {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean verifyKeyBinding(Context context,
|
||||
PGPSignature signature, PGPPublicKey signatureKey) {
|
||||
private boolean verifyKeyBinding(PGPSignature signature, PGPPublicKey signatureKey) {
|
||||
long signatureKeyId = signature.getKeyID();
|
||||
boolean validKeyBinding = false;
|
||||
|
||||
PGPPublicKey mKey = null;
|
||||
try {
|
||||
PGPPublicKeyRing signKeyRing = ProviderHelper.getPGPPublicKeyRingWithKeyId(context,
|
||||
PGPPublicKeyRing signKeyRing = mProviderHelper.getPGPPublicKeyRingWithKeyId(
|
||||
signatureKeyId);
|
||||
mKey = signKeyRing.getPublicKey();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
@ -686,7 +687,7 @@ public class PgpDecryptVerify {
|
||||
return validKeyBinding;
|
||||
}
|
||||
|
||||
private static boolean verifyKeyBinding(PGPPublicKey masterPublicKey, PGPPublicKey signingPublicKey) {
|
||||
private boolean verifyKeyBinding(PGPPublicKey masterPublicKey, PGPPublicKey signingPublicKey) {
|
||||
boolean validSubkeyBinding = false;
|
||||
boolean validTempSubkeyBinding = false;
|
||||
boolean validPrimaryKeyBinding = false;
|
||||
@ -734,9 +735,9 @@ public class PgpDecryptVerify {
|
||||
return (validSubkeyBinding & validPrimaryKeyBinding);
|
||||
}
|
||||
|
||||
private static boolean verifyPrimaryKeyBinding(PGPSignatureSubpacketVector pkts,
|
||||
PGPPublicKey masterPublicKey,
|
||||
PGPPublicKey signingPublicKey) {
|
||||
private boolean verifyPrimaryKeyBinding(PGPSignatureSubpacketVector pkts,
|
||||
PGPPublicKey masterPublicKey,
|
||||
PGPPublicKey signingPublicKey) {
|
||||
boolean validPrimaryKeyBinding = false;
|
||||
JcaPGPContentVerifierBuilderProvider contentVerifierBuilderProvider =
|
||||
new JcaPGPContentVerifierBuilderProvider()
|
||||
|
@ -57,10 +57,13 @@ public class PgpImportExport {
|
||||
|
||||
private KeychainServiceListener mKeychainServiceListener;
|
||||
|
||||
private ProviderHelper mProviderHelper;
|
||||
|
||||
public PgpImportExport(Context context, ProgressDialogUpdater progress) {
|
||||
super();
|
||||
this.mContext = context;
|
||||
this.mProgress = progress;
|
||||
this.mProviderHelper = new ProviderHelper(context);
|
||||
}
|
||||
|
||||
public PgpImportExport(Context context,
|
||||
@ -68,6 +71,7 @@ public class PgpImportExport {
|
||||
super();
|
||||
this.mContext = context;
|
||||
this.mProgress = progress;
|
||||
this.mProviderHelper = new ProviderHelper(context);
|
||||
this.mKeychainServiceListener = keychainListener;
|
||||
}
|
||||
|
||||
@ -196,7 +200,7 @@ public class PgpImportExport {
|
||||
updateProgress(progress * 100 / masterKeyIdsSize, 100);
|
||||
|
||||
try {
|
||||
PGPPublicKeyRing publicKeyRing = ProviderHelper.getPGPPublicKeyRing(mContext, pubKeyMasterId);
|
||||
PGPPublicKeyRing publicKeyRing = mProviderHelper.getPGPPublicKeyRing(pubKeyMasterId);
|
||||
|
||||
publicKeyRing.encode(arOutStream);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
@ -222,7 +226,7 @@ public class PgpImportExport {
|
||||
updateProgress(progress * 100 / masterKeyIdsSize, 100);
|
||||
|
||||
try {
|
||||
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, secretKeyMasterId);
|
||||
PGPSecretKeyRing secretKeyRing = mProviderHelper.getPGPSecretKeyRing(secretKeyMasterId);
|
||||
secretKeyRing.encode(arOutStream);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
@ -279,15 +283,15 @@ public class PgpImportExport {
|
||||
newPubRing = PGPPublicKeyRing.insertPublicKey(newPubRing, key);
|
||||
}
|
||||
if (newPubRing != null) {
|
||||
ProviderHelper.saveKeyRing(mContext, newPubRing);
|
||||
mProviderHelper.saveKeyRing(newPubRing);
|
||||
}
|
||||
ProviderHelper.saveKeyRing(mContext, secretKeyRing);
|
||||
mProviderHelper.saveKeyRing(secretKeyRing);
|
||||
// TODO: remove status returns, use exceptions!
|
||||
status = Id.return_value.ok;
|
||||
}
|
||||
} else if (keyring instanceof PGPPublicKeyRing) {
|
||||
PGPPublicKeyRing publicKeyRing = (PGPPublicKeyRing) keyring;
|
||||
ProviderHelper.saveKeyRing(mContext, publicKeyRing);
|
||||
mProviderHelper.saveKeyRing(publicKeyRing);
|
||||
// TODO: remove status returns, use exceptions!
|
||||
status = Id.return_value.ok;
|
||||
}
|
||||
|
@ -67,6 +67,7 @@ import java.util.Date;
|
||||
*/
|
||||
public class PgpSignEncrypt {
|
||||
private Context mContext;
|
||||
private ProviderHelper mProviderHelper;
|
||||
private InputData mData;
|
||||
private OutputStream mOutStream;
|
||||
|
||||
@ -85,6 +86,7 @@ public class PgpSignEncrypt {
|
||||
private PgpSignEncrypt(Builder builder) {
|
||||
// private Constructor can only be called from Builder
|
||||
this.mContext = builder.mContext;
|
||||
this.mProviderHelper = new ProviderHelper(mContext);
|
||||
this.mData = builder.mData;
|
||||
this.mOutStream = builder.mOutStream;
|
||||
|
||||
@ -252,7 +254,7 @@ public class PgpSignEncrypt {
|
||||
PGPPrivateKey signaturePrivateKey = null;
|
||||
if (enableSignature) {
|
||||
try {
|
||||
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureMasterKeyId);
|
||||
signingKeyRing = mProviderHelper.getPGPSecretKeyRingWithKeyId(mSignatureMasterKeyId);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
|
||||
}
|
||||
@ -300,7 +302,7 @@ public class PgpSignEncrypt {
|
||||
// Asymmetric encryption
|
||||
for (long id : mEncryptionMasterKeyIds) {
|
||||
try {
|
||||
PGPPublicKeyRing keyRing = ProviderHelper.getPGPPublicKeyRing(mContext, id);
|
||||
PGPPublicKeyRing keyRing = mProviderHelper.getPGPPublicKeyRing(id);
|
||||
PGPPublicKey key = PgpKeyHelper.getEncryptPublicKey(keyRing);
|
||||
if (key != null) {
|
||||
JcePublicKeyKeyEncryptionMethodGenerator pubKeyEncryptionGenerator =
|
||||
@ -491,7 +493,7 @@ public class PgpSignEncrypt {
|
||||
|
||||
PGPSecretKeyRing signingKeyRing;
|
||||
try {
|
||||
signingKeyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(mContext, mSignatureMasterKeyId);
|
||||
signingKeyRing = mProviderHelper.getPGPSecretKeyRingWithKeyId(mSignatureMasterKeyId);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
throw new PgpGeneralException(mContext.getString(R.string.error_signature_failed));
|
||||
}
|
||||
|
@ -250,10 +250,11 @@ public class KeychainDatabase extends SQLiteOpenHelper {
|
||||
c.moveToPosition(i);
|
||||
byte[] data = c.getBlob(0);
|
||||
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||
ProviderHelper providerHelper = new ProviderHelper(context);
|
||||
if(ring instanceof PGPPublicKeyRing)
|
||||
ProviderHelper.saveKeyRing(context, (PGPPublicKeyRing) ring);
|
||||
providerHelper.saveKeyRing((PGPPublicKeyRing) ring);
|
||||
else if(ring instanceof PGPSecretKeyRing)
|
||||
ProviderHelper.saveKeyRing(context, (PGPSecretKeyRing) ring);
|
||||
providerHelper.saveKeyRing((PGPSecretKeyRing) ring);
|
||||
else {
|
||||
Log.e(Constants.TAG, "Unknown blob data type!");
|
||||
}
|
||||
@ -271,10 +272,11 @@ public class KeychainDatabase extends SQLiteOpenHelper {
|
||||
c.moveToPosition(i);
|
||||
byte[] data = c.getBlob(0);
|
||||
PGPKeyRing ring = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||
ProviderHelper providerHelper = new ProviderHelper(context);
|
||||
if(ring instanceof PGPPublicKeyRing)
|
||||
ProviderHelper.saveKeyRing(context, (PGPPublicKeyRing) ring);
|
||||
providerHelper.saveKeyRing((PGPPublicKeyRing) ring);
|
||||
else if(ring instanceof PGPSecretKeyRing)
|
||||
ProviderHelper.saveKeyRing(context, (PGPSecretKeyRing) ring);
|
||||
providerHelper.saveKeyRing((PGPSecretKeyRing) ring);
|
||||
else {
|
||||
Log.e(Constants.TAG, "Unknown blob data type!");
|
||||
}
|
||||
|
@ -63,6 +63,13 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ProviderHelper {
|
||||
private Context mContext;
|
||||
private ContentResolver mContentResolver;
|
||||
|
||||
public ProviderHelper(Context context) {
|
||||
this.mContext = context;
|
||||
this.mContentResolver = context.getContentResolver();
|
||||
}
|
||||
|
||||
public static class NotFoundException extends Exception {
|
||||
public NotFoundException() {
|
||||
@ -81,23 +88,33 @@ public class ProviderHelper {
|
||||
public static final int FIELD_TYPE_STRING = 4;
|
||||
public static final int FIELD_TYPE_BLOB = 5;
|
||||
|
||||
public static Object getGenericData(Context context, Uri uri, String column, int type) {
|
||||
return getGenericData(context, uri, new String[] { column }, new int[] { type }).get(column);
|
||||
public Object getGenericData(Uri uri, String column, int type) {
|
||||
return getGenericData(uri, new String[]{column}, new int[]{type}).get(column);
|
||||
}
|
||||
|
||||
public static HashMap<String,Object> getGenericData(Context context, Uri uri, String[] proj, int[] types) {
|
||||
Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
|
||||
public HashMap<String, Object> getGenericData(Uri uri, String[] proj, int[] types) {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
@ -110,43 +127,43 @@ public class ProviderHelper {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Object getUnifiedData(Context context, long masterKeyId, String column, int type) {
|
||||
return getUnifiedData(context, masterKeyId, new String[] { column }, new int[] { type }).get(column);
|
||||
public Object getUnifiedData(long masterKeyId, String column, int type) {
|
||||
return getUnifiedData(masterKeyId, new String[]{column}, new int[]{type}).get(column);
|
||||
}
|
||||
|
||||
public static HashMap<String,Object> getUnifiedData(Context context, long masterKeyId, String[] proj, int[] types) {
|
||||
return getGenericData(context, KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj, types);
|
||||
public HashMap<String, Object> getUnifiedData(long masterKeyId, String[] proj, int[] types) {
|
||||
return getGenericData(KeyRings.buildUnifiedKeyRingUri(Long.toString(masterKeyId)), proj, types);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 extractOrGetMasterKeyId(Context context, Uri queryUri)
|
||||
public long extractOrGetMasterKeyId(Uri queryUri)
|
||||
throws NotFoundException {
|
||||
// try extracting from the uri first
|
||||
String firstSegment = queryUri.getPathSegments().get(1);
|
||||
if(!firstSegment.equals("find")) try {
|
||||
if (!firstSegment.equals("find")) try {
|
||||
return Long.parseLong(firstSegment);
|
||||
} catch(NumberFormatException e) {
|
||||
} catch (NumberFormatException e) {
|
||||
// didn't work? oh well.
|
||||
Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying...");
|
||||
}
|
||||
return getMasterKeyId(context, queryUri);
|
||||
return getMasterKeyId(queryUri);
|
||||
}
|
||||
|
||||
public static long getMasterKeyId(Context context, Uri queryUri) throws NotFoundException {
|
||||
Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
|
||||
if(data != null) {
|
||||
public long getMasterKeyId(Uri queryUri) throws NotFoundException {
|
||||
Object data = getGenericData(queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
|
||||
if (data != null) {
|
||||
return (Long) data;
|
||||
} else {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Long, PGPKeyRing> getPGPKeyRings(Context context, Uri queryUri) {
|
||||
Cursor cursor = context.getContentResolver().query(queryUri,
|
||||
new String[]{KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA },
|
||||
public Map<Long, 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());
|
||||
@ -156,7 +173,7 @@ public class ProviderHelper {
|
||||
if (data != null) {
|
||||
result.put(masterKeyId, PgpConversionHelper.BytesToPGPKeyRing(data));
|
||||
}
|
||||
} while(cursor.moveToNext());
|
||||
} while (cursor.moveToNext());
|
||||
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
@ -165,66 +182,64 @@ public class ProviderHelper {
|
||||
return result;
|
||||
}
|
||||
|
||||
public static PGPKeyRing getPGPKeyRing(Context context, Uri queryUri) throws NotFoundException {
|
||||
Map<Long, PGPKeyRing> result = getPGPKeyRings(context, queryUri);
|
||||
if(result.isEmpty()) {
|
||||
public PGPKeyRing getPGPKeyRing(Uri queryUri) throws NotFoundException {
|
||||
Map<Long, PGPKeyRing> result = getPGPKeyRings(queryUri);
|
||||
if (result.isEmpty()) {
|
||||
throw new NotFoundException("PGPKeyRing object not found!");
|
||||
} else {
|
||||
return result.values().iterator().next();
|
||||
}
|
||||
}
|
||||
|
||||
public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId)
|
||||
public PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(long keyId)
|
||||
throws NotFoundException {
|
||||
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
|
||||
long masterKeyId = getMasterKeyId(context, uri);
|
||||
return getPGPPublicKeyRing(context, masterKeyId);
|
||||
long masterKeyId = getMasterKeyId(uri);
|
||||
return getPGPPublicKeyRing(masterKeyId);
|
||||
}
|
||||
|
||||
public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long keyId)
|
||||
public PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(long keyId)
|
||||
throws NotFoundException {
|
||||
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
|
||||
long masterKeyId = getMasterKeyId(context, uri);
|
||||
return getPGPSecretKeyRing(context, masterKeyId);
|
||||
long masterKeyId = getMasterKeyId(uri);
|
||||
return getPGPSecretKeyRing(masterKeyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the actual PGPPublicKeyRing object from the database blob based on the masterKeyId
|
||||
*/
|
||||
public static PGPPublicKeyRing getPGPPublicKeyRing(Context context,
|
||||
long masterKeyId) throws NotFoundException {
|
||||
public PGPPublicKeyRing getPGPPublicKeyRing(long masterKeyId) throws NotFoundException {
|
||||
Uri queryUri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId));
|
||||
return (PGPPublicKeyRing) getPGPKeyRing(context, queryUri);
|
||||
return (PGPPublicKeyRing) getPGPKeyRing(queryUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the actual PGPSecretKeyRing object from the database blob based on the maserKeyId
|
||||
*/
|
||||
public static PGPSecretKeyRing getPGPSecretKeyRing(Context context,
|
||||
long masterKeyId) throws NotFoundException {
|
||||
public PGPSecretKeyRing getPGPSecretKeyRing(long masterKeyId) throws NotFoundException {
|
||||
Uri queryUri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId));
|
||||
return (PGPSecretKeyRing) getPGPKeyRing(context, queryUri);
|
||||
return (PGPSecretKeyRing) getPGPKeyRing(queryUri);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves PGPPublicKeyRing with its keys and userIds in DB
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void saveKeyRing(Context context, PGPPublicKeyRing keyRing) throws IOException {
|
||||
public void saveKeyRing(PGPPublicKeyRing keyRing) throws IOException {
|
||||
PGPPublicKey masterKey = keyRing.getPublicKey();
|
||||
long masterKeyId = masterKey.getKeyID();
|
||||
|
||||
// IF there is a secret key, preserve it!
|
||||
PGPSecretKeyRing secretRing = null;
|
||||
try {
|
||||
secretRing = ProviderHelper.getPGPSecretKeyRing(context, masterKeyId);
|
||||
secretRing = getPGPSecretKeyRing(masterKeyId);
|
||||
} catch (NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!");
|
||||
}
|
||||
|
||||
// delete old version of this keyRing, which also deletes all keys and userIds on cascade
|
||||
try {
|
||||
context.getContentResolver().delete(KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null);
|
||||
mContentResolver.delete(KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId)), null, null);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
Log.e(Constants.TAG, "Key could not be deleted! Maybe we are creating a new one!", e);
|
||||
}
|
||||
@ -234,21 +249,21 @@ public class ProviderHelper {
|
||||
values.put(KeyRingData.MASTER_KEY_ID, masterKeyId);
|
||||
values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded());
|
||||
Uri uri = KeyRingData.buildPublicKeyRingUri(Long.toString(masterKeyId));
|
||||
context.getContentResolver().insert(uri, values);
|
||||
mContentResolver.insert(uri, values);
|
||||
|
||||
// save all keys and userIds included in keyRing object in database
|
||||
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
|
||||
|
||||
int rank = 0;
|
||||
for (PGPPublicKey key : new IterableIterator<PGPPublicKey>(keyRing.getPublicKeys())) {
|
||||
operations.add(buildPublicKeyOperations(context, masterKeyId, key, rank));
|
||||
operations.add(buildPublicKeyOperations(masterKeyId, key, rank));
|
||||
++rank;
|
||||
}
|
||||
|
||||
// get a list of owned secret keys, for verification filtering
|
||||
Map<Long, PGPKeyRing> allKeyRings = getPGPKeyRings(context, KeyRingData.buildSecretKeyRingUri());
|
||||
Map<Long, PGPKeyRing> allKeyRings = getPGPKeyRings(KeyRingData.buildSecretKeyRingUri());
|
||||
// special case: available secret keys verify themselves!
|
||||
if(secretRing != null)
|
||||
if (secretRing != null)
|
||||
allKeyRings.put(secretRing.getSecretKey().getKeyID(), secretRing);
|
||||
|
||||
// classify and order user ids. primary are moved to the front, revoked to the back,
|
||||
@ -266,16 +281,16 @@ public class ProviderHelper {
|
||||
long certId = cert.getKeyID();
|
||||
try {
|
||||
// self signature
|
||||
if(certId == masterKeyId) {
|
||||
if (certId == masterKeyId) {
|
||||
cert.init(new JcaPGPContentVerifierBuilderProvider().setProvider(
|
||||
Constants.BOUNCY_CASTLE_PROVIDER_NAME), masterKey);
|
||||
if(!cert.verifyCertification(userId, masterKey)) {
|
||||
Constants.BOUNCY_CASTLE_PROVIDER_NAME), masterKey);
|
||||
if (!cert.verifyCertification(userId, masterKey)) {
|
||||
// not verified?! dang! TODO notify user? this is kinda serious...
|
||||
Log.e(Constants.TAG, "Could not verify self signature for " + userId + "!");
|
||||
continue;
|
||||
}
|
||||
// is this the first, or a more recent certificate?
|
||||
if(item.selfCert == null ||
|
||||
if (item.selfCert == null ||
|
||||
item.selfCert.getCreationTime().before(cert.getCreationTime())) {
|
||||
item.selfCert = cert;
|
||||
item.isPrimary = cert.getHashedSubPackets().isPrimaryUserID();
|
||||
@ -284,21 +299,21 @@ public class ProviderHelper {
|
||||
}
|
||||
}
|
||||
// verify signatures from known private keys
|
||||
if(allKeyRings.containsKey(certId)) {
|
||||
if (allKeyRings.containsKey(certId)) {
|
||||
// mark them as verified
|
||||
cert.init(new JcaPGPContentVerifierBuilderProvider().setProvider(
|
||||
Constants.BOUNCY_CASTLE_PROVIDER_NAME),
|
||||
Constants.BOUNCY_CASTLE_PROVIDER_NAME),
|
||||
allKeyRings.get(certId).getPublicKey());
|
||||
if(cert.verifyCertification(userId, masterKey)) {
|
||||
if (cert.verifyCertification(userId, masterKey)) {
|
||||
item.trustedCerts.add(cert);
|
||||
}
|
||||
}
|
||||
} catch(SignatureException e) {
|
||||
} catch (SignatureException e) {
|
||||
Log.e(Constants.TAG, "Signature verification failed! "
|
||||
+ PgpKeyHelper.convertKeyIdToHex(masterKey.getKeyID())
|
||||
+ " from "
|
||||
+ PgpKeyHelper.convertKeyIdToHex(cert.getKeyID()), e);
|
||||
} catch(PGPException e) {
|
||||
} catch (PGPException e) {
|
||||
Log.e(Constants.TAG, "Signature verification failed! "
|
||||
+ PgpKeyHelper.convertKeyIdToHex(masterKey.getKeyID())
|
||||
+ " from "
|
||||
@ -311,26 +326,26 @@ public class ProviderHelper {
|
||||
// this is a stable sort, so the order of keys is otherwise preserved.
|
||||
Collections.sort(uids);
|
||||
// iterate and put into db
|
||||
for(int userIdRank = 0; userIdRank < uids.size(); userIdRank++) {
|
||||
for (int userIdRank = 0; userIdRank < uids.size(); userIdRank++) {
|
||||
UserIdItem item = uids.get(userIdRank);
|
||||
operations.add(buildUserIdOperations(masterKeyId, item, userIdRank));
|
||||
// no self cert is bad, but allowed by the rfc...
|
||||
if(item.selfCert != null) {
|
||||
if (item.selfCert != null) {
|
||||
operations.add(buildCertOperations(
|
||||
masterKeyId, userIdRank, item.selfCert, Certs.VERIFIED_SELF));
|
||||
}
|
||||
// don't bother with trusted certs if the uid is revoked, anyways
|
||||
if(item.isRevoked) {
|
||||
if (item.isRevoked) {
|
||||
continue;
|
||||
}
|
||||
for(int i = 0; i < item.trustedCerts.size(); i++) {
|
||||
for (int i = 0; i < item.trustedCerts.size(); i++) {
|
||||
operations.add(buildCertOperations(
|
||||
masterKeyId, userIdRank, item.trustedCerts.get(i), Certs.VERIFIED_SECRET));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
context.getContentResolver().applyBatch(KeychainContract.CONTENT_AUTHORITY, operations);
|
||||
mContentResolver.applyBatch(KeychainContract.CONTENT_AUTHORITY, operations);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(Constants.TAG, "applyBatch failed!", e);
|
||||
} catch (OperationApplicationException e) {
|
||||
@ -338,8 +353,8 @@ public class ProviderHelper {
|
||||
}
|
||||
|
||||
// Save the saved keyring (if any)
|
||||
if(secretRing != null) {
|
||||
saveKeyRing(context, secretRing);
|
||||
if (secretRing != null) {
|
||||
saveKeyRing(secretRing);
|
||||
}
|
||||
|
||||
}
|
||||
@ -354,11 +369,13 @@ public class ProviderHelper {
|
||||
@Override
|
||||
public int compareTo(UserIdItem o) {
|
||||
// if one key is primary but the other isn't, the primary one always comes first
|
||||
if(isPrimary != o.isPrimary)
|
||||
if (isPrimary != o.isPrimary) {
|
||||
return isPrimary ? -1 : 1;
|
||||
}
|
||||
// revoked keys always come last!
|
||||
if(isRevoked != o.isRevoked)
|
||||
if (isRevoked != o.isRevoked) {
|
||||
return isRevoked ? 1 : -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@ -368,7 +385,7 @@ public class ProviderHelper {
|
||||
* is already in the database!
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void saveKeyRing(Context context, PGPSecretKeyRing keyRing) throws IOException {
|
||||
public void saveKeyRing(PGPSecretKeyRing keyRing) throws IOException {
|
||||
long masterKeyId = keyRing.getPublicKey().getKeyID();
|
||||
|
||||
// save secret keyring
|
||||
@ -377,30 +394,29 @@ public class ProviderHelper {
|
||||
values.put(KeyRingData.KEY_RING_DATA, keyRing.getEncoded());
|
||||
// insert new version of this keyRing
|
||||
Uri uri = KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId));
|
||||
context.getContentResolver().insert(uri, values);
|
||||
|
||||
mContentResolver.insert(uri, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves (or updates) a pair of public and secret KeyRings in the database
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void saveKeyRing(Context context, PGPPublicKeyRing pubRing, PGPSecretKeyRing privRing) throws IOException {
|
||||
public void saveKeyRing(PGPPublicKeyRing pubRing, PGPSecretKeyRing privRing) throws IOException {
|
||||
long masterKeyId = pubRing.getPublicKey().getKeyID();
|
||||
|
||||
// delete secret keyring (so it isn't unnecessarily saved by public-saveKeyRing below)
|
||||
context.getContentResolver().delete(KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId)), null, null);
|
||||
mContentResolver.delete(KeyRingData.buildSecretKeyRingUri(Long.toString(masterKeyId)), null, null);
|
||||
|
||||
// save public keyring
|
||||
saveKeyRing(context, pubRing);
|
||||
saveKeyRing(context, privRing);
|
||||
saveKeyRing(pubRing);
|
||||
saveKeyRing(privRing);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing
|
||||
*/
|
||||
private static ContentProviderOperation buildPublicKeyOperations(Context context,
|
||||
long masterKeyId, PGPPublicKey key, int rank) throws IOException {
|
||||
private ContentProviderOperation
|
||||
buildPublicKeyOperations(long masterKeyId, PGPPublicKey key, int rank) throws IOException {
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(Keys.MASTER_KEY_ID, masterKeyId);
|
||||
@ -430,11 +446,8 @@ public class ProviderHelper {
|
||||
/**
|
||||
* Build ContentProviderOperation to add PGPPublicKey to database corresponding to a keyRing
|
||||
*/
|
||||
private static ContentProviderOperation buildCertOperations(long masterKeyId,
|
||||
int rank,
|
||||
PGPSignature cert,
|
||||
int verified)
|
||||
throws IOException {
|
||||
private ContentProviderOperation
|
||||
buildCertOperations(long masterKeyId, int rank, PGPSignature cert, int verified) throws IOException {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(Certs.MASTER_KEY_ID, masterKeyId);
|
||||
values.put(Certs.RANK, rank);
|
||||
@ -452,8 +465,8 @@ public class ProviderHelper {
|
||||
/**
|
||||
* Build ContentProviderOperation to add PublicUserIds to database corresponding to a keyRing
|
||||
*/
|
||||
private static ContentProviderOperation buildUserIdOperations(long masterKeyId, UserIdItem item,
|
||||
int rank) {
|
||||
private ContentProviderOperation
|
||||
buildUserIdOperations(long masterKeyId, UserIdItem item, int rank) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(UserIds.MASTER_KEY_ID, masterKeyId);
|
||||
values.put(UserIds.USER_ID, item.userId);
|
||||
@ -466,7 +479,7 @@ public class ProviderHelper {
|
||||
return ContentProviderOperation.newInsert(uri).withValues(values).build();
|
||||
}
|
||||
|
||||
private static String getKeyRingAsArmoredString(Context context, byte[] data) throws IOException {
|
||||
private String getKeyRingAsArmoredString(byte[] data) throws IOException {
|
||||
Object keyRing = null;
|
||||
if (data != null) {
|
||||
keyRing = PgpConversionHelper.BytesToPGPKeyRing(data);
|
||||
@ -474,7 +487,7 @@ public class ProviderHelper {
|
||||
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ArmoredOutputStream aos = new ArmoredOutputStream(bos);
|
||||
aos.setHeader("Version", PgpHelper.getFullVersion(context));
|
||||
aos.setHeader("Version", PgpHelper.getFullVersion(mContext));
|
||||
|
||||
if (keyRing instanceof PGPSecretKeyRing) {
|
||||
aos.write(((PGPSecretKeyRing) keyRing).getEncoded());
|
||||
@ -490,15 +503,15 @@ public class ProviderHelper {
|
||||
return armoredKey;
|
||||
}
|
||||
|
||||
public static String getKeyRingAsArmoredString(Context context, Uri uri)
|
||||
public String getKeyRingAsArmoredString(Uri uri)
|
||||
throws NotFoundException, IOException {
|
||||
byte[] data = (byte[]) ProviderHelper.getGenericData(
|
||||
context, uri, KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
return getKeyRingAsArmoredString(context, data);
|
||||
byte[] data = (byte[]) getGenericData(
|
||||
uri, KeyRingData.KEY_RING_DATA, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
return getKeyRingAsArmoredString(data);
|
||||
}
|
||||
|
||||
// TODO This method is NOT ACTUALLY USED. Is this preparation for something, or just dead code?
|
||||
public static ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds)
|
||||
public ArrayList<String> getKeyRingsAsArmoredString(Context context, long[] masterKeyIds)
|
||||
throws IOException {
|
||||
ArrayList<String> output = new ArrayList<String>();
|
||||
|
||||
@ -508,7 +521,8 @@ public class ProviderHelper {
|
||||
}
|
||||
|
||||
// Build a cursor for the selected masterKeyIds
|
||||
Cursor cursor = null; {
|
||||
Cursor cursor = null;
|
||||
{
|
||||
String inMasterKeyList = KeyRingData.MASTER_KEY_ID + " IN (";
|
||||
for (int i = 0; i < masterKeyIds.length; ++i) {
|
||||
if (i != 0) {
|
||||
@ -518,7 +532,7 @@ public class ProviderHelper {
|
||||
}
|
||||
inMasterKeyList += ")";
|
||||
|
||||
cursor = context.getContentResolver().query(KeyRingData.buildPublicKeyRingUri(), new String[] {
|
||||
cursor = context.getContentResolver().query(KeyRingData.buildPublicKeyRingUri(), new String[]{
|
||||
KeyRingData._ID, KeyRingData.MASTER_KEY_ID, KeyRingData.KEY_RING_DATA
|
||||
}, inMasterKeyList, null, null);
|
||||
}
|
||||
@ -534,7 +548,7 @@ public class ProviderHelper {
|
||||
|
||||
// get actual keyring data blob and write it to ByteArrayOutputStream
|
||||
try {
|
||||
output.add(getKeyRingAsArmoredString(context, data));
|
||||
output.add(getKeyRingAsArmoredString(data));
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "IOException", e);
|
||||
}
|
||||
@ -553,9 +567,8 @@ public class ProviderHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static ArrayList<String> getRegisteredApiApps(Context context) {
|
||||
Cursor cursor = context.getContentResolver().query(ApiApps.CONTENT_URI, null, null, null,
|
||||
null);
|
||||
public ArrayList<String> getRegisteredApiApps() {
|
||||
Cursor cursor = mContentResolver.query(ApiApps.CONTENT_URI, null, null, null, null);
|
||||
|
||||
ArrayList<String> packageNames = new ArrayList<String>();
|
||||
if (cursor != null) {
|
||||
@ -574,14 +587,14 @@ public class ProviderHelper {
|
||||
return packageNames;
|
||||
}
|
||||
|
||||
private static ContentValues contentValueForApiApps(AppSettings appSettings) {
|
||||
private ContentValues contentValueForApiApps(AppSettings appSettings) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(ApiApps.PACKAGE_NAME, appSettings.getPackageName());
|
||||
values.put(ApiApps.PACKAGE_SIGNATURE, appSettings.getPackageSignature());
|
||||
return values;
|
||||
}
|
||||
|
||||
private static ContentValues contentValueForApiAccounts(AccountSettings accSettings) {
|
||||
private ContentValues contentValueForApiAccounts(AccountSettings accSettings) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KeychainContract.ApiAccounts.ACCOUNT_NAME, accSettings.getAccountName());
|
||||
values.put(KeychainContract.ApiAccounts.KEY_ID, accSettings.getKeyId());
|
||||
@ -591,24 +604,24 @@ public class ProviderHelper {
|
||||
return values;
|
||||
}
|
||||
|
||||
public static void insertApiApp(Context context, AppSettings appSettings) {
|
||||
context.getContentResolver().insert(KeychainContract.ApiApps.CONTENT_URI,
|
||||
public void insertApiApp(AppSettings appSettings) {
|
||||
mContentResolver.insert(KeychainContract.ApiApps.CONTENT_URI,
|
||||
contentValueForApiApps(appSettings));
|
||||
}
|
||||
|
||||
public static void insertApiAccount(Context context, Uri uri, AccountSettings accSettings) {
|
||||
context.getContentResolver().insert(uri, contentValueForApiAccounts(accSettings));
|
||||
public void insertApiAccount(Uri uri, AccountSettings accSettings) {
|
||||
mContentResolver.insert(uri, contentValueForApiAccounts(accSettings));
|
||||
}
|
||||
|
||||
public static void updateApiApp(Context context, AppSettings appSettings, Uri uri) {
|
||||
if (context.getContentResolver().update(uri, contentValueForApiApps(appSettings), null,
|
||||
public void updateApiApp(AppSettings appSettings, Uri uri) {
|
||||
if (mContentResolver.update(uri, contentValueForApiApps(appSettings), null,
|
||||
null) <= 0) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateApiAccount(Context context, AccountSettings accSettings, Uri uri) {
|
||||
if (context.getContentResolver().update(uri, contentValueForApiAccounts(accSettings), null,
|
||||
public void updateApiAccount(AccountSettings accSettings, Uri uri) {
|
||||
if (mContentResolver.update(uri, contentValueForApiAccounts(accSettings), null,
|
||||
null) <= 0) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
@ -617,14 +630,13 @@ public class ProviderHelper {
|
||||
/**
|
||||
* Must be an uri pointing to an account
|
||||
*
|
||||
* @param context
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
public static AppSettings getApiAppSettings(Context context, Uri uri) {
|
||||
public AppSettings getApiAppSettings(Uri uri) {
|
||||
AppSettings settings = null;
|
||||
|
||||
Cursor cur = context.getContentResolver().query(uri, null, null, null, null);
|
||||
Cursor cur = mContentResolver.query(uri, null, null, null, null);
|
||||
if (cur != null && cur.moveToFirst()) {
|
||||
settings = new AppSettings();
|
||||
settings.setPackageName(cur.getString(
|
||||
@ -636,10 +648,10 @@ public class ProviderHelper {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static AccountSettings getApiAccountSettings(Context context, Uri accountUri) {
|
||||
public AccountSettings getApiAccountSettings(Uri accountUri) {
|
||||
AccountSettings settings = null;
|
||||
|
||||
Cursor cur = context.getContentResolver().query(accountUri, null, null, null, null);
|
||||
Cursor cur = mContentResolver.query(accountUri, null, null, null, null);
|
||||
if (cur != null && cur.moveToFirst()) {
|
||||
settings = new AccountSettings();
|
||||
|
||||
@ -658,10 +670,10 @@ public class ProviderHelper {
|
||||
return settings;
|
||||
}
|
||||
|
||||
public static Set<Long> getAllKeyIdsForApp(Context context, Uri uri) {
|
||||
public Set<Long> getAllKeyIdsForApp(Uri uri) {
|
||||
Set<Long> keyIds = new HashSet<Long>();
|
||||
|
||||
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
|
||||
Cursor cursor = mContentResolver.query(uri, null, null, null, null);
|
||||
if (cursor != null) {
|
||||
int keyIdColumn = cursor.getColumnIndex(KeychainContract.ApiAccounts.KEY_ID);
|
||||
while (cursor.moveToNext()) {
|
||||
@ -672,13 +684,12 @@ public class ProviderHelper {
|
||||
return keyIds;
|
||||
}
|
||||
|
||||
public static byte[] getApiAppSignature(Context context, String packageName) {
|
||||
public byte[] getApiAppSignature(String packageName) {
|
||||
Uri queryUri = ApiApps.buildByPackageNameUri(packageName);
|
||||
|
||||
String[] projection = new String[]{ApiApps.PACKAGE_SIGNATURE};
|
||||
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
Cursor cursor = cr.query(queryUri, projection, null, null, null);
|
||||
Cursor cursor = mContentResolver.query(queryUri, projection, null, null, null);
|
||||
|
||||
byte[] signature = null;
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
|
@ -296,7 +296,7 @@ public class OpenPgpService extends RemoteService {
|
||||
PgpDecryptVerify.Builder builder = new PgpDecryptVerify.Builder(this, inputData, os);
|
||||
builder.allowSymmetricDecryption(false) // no support for symmetric encryption
|
||||
.allowedKeyIds(allowedKeyIds) // allow only private keys associated with
|
||||
// accounts of this app
|
||||
// accounts of this app
|
||||
.passphrase(passphrase);
|
||||
|
||||
// TODO: currently does not support binary signed-only content
|
||||
@ -305,10 +305,10 @@ public class OpenPgpService extends RemoteService {
|
||||
if (PgpDecryptVerifyResult.KEY_PASSHRASE_NEEDED == decryptVerifyResult.getStatus()) {
|
||||
// get PendingIntent for passphrase input, add it to given params and return to client
|
||||
Intent passphraseBundle =
|
||||
getPassphraseBundleIntent(data, decryptVerifyResult.getKeyIdPassphraseNeeded());
|
||||
getPassphraseBundleIntent(data, decryptVerifyResult.getKeyIdPassphraseNeeded());
|
||||
return passphraseBundle;
|
||||
} else if (PgpDecryptVerifyResult.SYMMETRIC_PASSHRASE_NEEDED ==
|
||||
decryptVerifyResult.getStatus()) {
|
||||
decryptVerifyResult.getStatus()) {
|
||||
throw new PgpGeneralException("Decryption of symmetric content not supported by API!");
|
||||
}
|
||||
|
||||
@ -352,7 +352,7 @@ public class OpenPgpService extends RemoteService {
|
||||
try {
|
||||
long keyId = data.getLongExtra(OpenPgpApi.EXTRA_KEY_ID, 0);
|
||||
|
||||
if (ProviderHelper.getPGPPublicKeyRing(this, keyId) == null) {
|
||||
if (mProviderHelper.getPGPPublicKeyRing(keyId) == null) {
|
||||
Intent result = new Intent();
|
||||
|
||||
// If keys are not in db we return an additional PendingIntent
|
||||
@ -462,8 +462,8 @@ public class OpenPgpService extends RemoteService {
|
||||
} else if (OpenPgpApi.ACTION_DECRYPT_VERIFY.equals(action)) {
|
||||
String currentPkg = getCurrentCallingPackage();
|
||||
Set<Long> allowedKeyIds =
|
||||
ProviderHelper.getAllKeyIdsForApp(mContext,
|
||||
ApiAccounts.buildBaseUri(currentPkg));
|
||||
mProviderHelper.getAllKeyIdsForApp(
|
||||
ApiAccounts.buildBaseUri(currentPkg));
|
||||
return decryptAndVerifyImpl(data, input, output, allowedKeyIds);
|
||||
} else if (OpenPgpApi.ACTION_GET_KEY.equals(action)) {
|
||||
return getKeyImpl(data);
|
||||
|
@ -45,6 +45,7 @@ import java.util.Arrays;
|
||||
*/
|
||||
public abstract class RemoteService extends Service {
|
||||
Context mContext;
|
||||
ProviderHelper mProviderHelper;
|
||||
|
||||
public Context getContext() {
|
||||
return mContext;
|
||||
@ -148,7 +149,7 @@ public abstract class RemoteService extends Service {
|
||||
|
||||
Uri uri = KeychainContract.ApiAccounts.buildByPackageAndAccountUri(currentPkg, accountName);
|
||||
|
||||
AccountSettings settings = ProviderHelper.getApiAccountSettings(this, uri);
|
||||
AccountSettings settings = mProviderHelper.getApiAccountSettings(uri);
|
||||
|
||||
return settings; // can be null!
|
||||
}
|
||||
@ -221,7 +222,7 @@ public abstract class RemoteService extends Service {
|
||||
private boolean isPackageAllowed(String packageName) throws WrongPackageSignatureException {
|
||||
Log.d(Constants.TAG, "isPackageAllowed packageName: " + packageName);
|
||||
|
||||
ArrayList<String> allowedPkgs = ProviderHelper.getRegisteredApiApps(this);
|
||||
ArrayList<String> allowedPkgs = mProviderHelper.getRegisteredApiApps();
|
||||
Log.d(Constants.TAG, "allowed: " + allowedPkgs);
|
||||
|
||||
// check if package is allowed to use our service
|
||||
@ -236,7 +237,7 @@ public abstract class RemoteService extends Service {
|
||||
throw new WrongPackageSignatureException(e.getMessage());
|
||||
}
|
||||
|
||||
byte[] storedSig = ProviderHelper.getApiAppSignature(this, packageName);
|
||||
byte[] storedSig = mProviderHelper.getApiAppSignature(packageName);
|
||||
if (Arrays.equals(currentSig, storedSig)) {
|
||||
Log.d(Constants.TAG,
|
||||
"Package signature is correct! (equals signature from database)");
|
||||
@ -244,7 +245,7 @@ public abstract class RemoteService extends Service {
|
||||
} else {
|
||||
throw new WrongPackageSignatureException(
|
||||
"PACKAGE NOT ALLOWED! Signature wrong! (Signature not " +
|
||||
"equals signature from database)");
|
||||
"equals signature from database)");
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,6 +257,7 @@ public abstract class RemoteService extends Service {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
mContext = this;
|
||||
mProviderHelper = new ProviderHelper(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ public class AccountSettingsActivity extends ActionBarActivity {
|
||||
}
|
||||
|
||||
private void loadData(Uri accountUri) {
|
||||
AccountSettings settings = ProviderHelper.getApiAccountSettings(this, accountUri);
|
||||
AccountSettings settings = new ProviderHelper(this).getApiAccountSettings(accountUri);
|
||||
mAccountSettingsFragment.setAccSettings(settings);
|
||||
}
|
||||
|
||||
@ -102,7 +102,7 @@ public class AccountSettingsActivity extends ActionBarActivity {
|
||||
}
|
||||
|
||||
private void save() {
|
||||
ProviderHelper.updateApiAccount(this, mAccountSettingsFragment.getAccSettings(), mAccountUri);
|
||||
new ProviderHelper(this).updateApiAccount(mAccountSettingsFragment.getAccSettings(), mAccountUri);
|
||||
finish();
|
||||
}
|
||||
|
||||
|
@ -180,8 +180,8 @@ public class AccountSettingsFragment extends Fragment implements
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
// select newly created key
|
||||
try {
|
||||
long masterKeyId = ProviderHelper.extractOrGetMasterKeyId(
|
||||
getActivity(), data.getData());
|
||||
long masterKeyId = new ProviderHelper(getActivity())
|
||||
.extractOrGetMasterKeyId(data.getData());
|
||||
mSelectKeyFragment.selectKey(masterKeyId);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
|
@ -85,7 +85,7 @@ public class AppSettingsActivity extends ActionBarActivity {
|
||||
}
|
||||
|
||||
private void loadData(Bundle savedInstanceState, Uri appUri) {
|
||||
AppSettings settings = ProviderHelper.getApiAppSettings(this, appUri);
|
||||
AppSettings settings = new ProviderHelper(this).getApiAppSettings(appUri);
|
||||
mSettingsFragment.setAppSettings(settings);
|
||||
|
||||
String appName;
|
||||
|
@ -103,7 +103,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
||||
public void onClick(View v) {
|
||||
// Allow
|
||||
|
||||
ProviderHelper.insertApiApp(RemoteServiceActivity.this,
|
||||
new ProviderHelper(RemoteServiceActivity.this).insertApiApp(
|
||||
mAppSettingsFragment.getAppSettings());
|
||||
|
||||
// give data through for new service call
|
||||
@ -146,7 +146,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
||||
mAccSettingsFragment.setErrorOnSelectKeyFragment(
|
||||
getString(R.string.api_register_error_select_key));
|
||||
} else {
|
||||
ProviderHelper.insertApiAccount(RemoteServiceActivity.this,
|
||||
new ProviderHelper(RemoteServiceActivity.this).insertApiAccount(
|
||||
KeychainContract.ApiAccounts.buildBaseUri(packageName),
|
||||
mAccSettingsFragment.getAccSettings());
|
||||
|
||||
@ -179,19 +179,19 @@ public class RemoteServiceActivity extends ActionBarActivity {
|
||||
final Intent resultData = extras.getParcelable(EXTRA_DATA);
|
||||
|
||||
PassphraseDialogFragment.show(this, secretKeyId,
|
||||
new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
|
||||
// return given params again, for calling the service method again
|
||||
RemoteServiceActivity.this.setResult(RESULT_OK, resultData);
|
||||
} else {
|
||||
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
|
||||
}
|
||||
new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message message) {
|
||||
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
|
||||
// return given params again, for calling the service method again
|
||||
RemoteServiceActivity.this.setResult(RESULT_OK, resultData);
|
||||
} else {
|
||||
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
|
||||
}
|
||||
|
||||
RemoteServiceActivity.this.finish();
|
||||
}
|
||||
});
|
||||
RemoteServiceActivity.this.finish();
|
||||
}
|
||||
});
|
||||
|
||||
} else if (ACTION_SELECT_PUB_KEYS.equals(action)) {
|
||||
long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS);
|
||||
|
@ -496,20 +496,21 @@ public class KeychainIntentService extends IntentService
|
||||
long masterKeyId = saveParcel.keys.get(0).getKeyID();
|
||||
|
||||
/* Operation */
|
||||
ProviderHelper providerHelper = new ProviderHelper(this);
|
||||
if (!canSign) {
|
||||
PgpKeyOperation keyOperations = new PgpKeyOperation(new ProgressScaler(this, 0, 50, 100));
|
||||
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
|
||||
PGPSecretKeyRing keyRing = providerHelper.getPGPSecretKeyRing(masterKeyId);
|
||||
keyRing = keyOperations.changeSecretKeyPassphrase(keyRing,
|
||||
oldPassphrase, newPassphrase);
|
||||
setProgress(R.string.progress_saving_key_ring, 50, 100);
|
||||
ProviderHelper.saveKeyRing(this, keyRing);
|
||||
providerHelper.saveKeyRing(keyRing);
|
||||
setProgress(R.string.progress_done, 100, 100);
|
||||
} else {
|
||||
PgpKeyOperation keyOperations = new PgpKeyOperation(new ProgressScaler(this, 0, 90, 100));
|
||||
PgpKeyOperation.Pair<PGPSecretKeyRing, PGPPublicKeyRing> pair;
|
||||
try {
|
||||
PGPSecretKeyRing privkey = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
|
||||
PGPPublicKeyRing pubkey = ProviderHelper.getPGPPublicKeyRing(this, masterKeyId);
|
||||
PGPSecretKeyRing privkey = providerHelper.getPGPSecretKeyRing(masterKeyId);
|
||||
PGPPublicKeyRing pubkey = providerHelper.getPGPPublicKeyRing(masterKeyId);
|
||||
|
||||
pair = keyOperations.buildSecretKey(privkey, pubkey, saveParcel); // edit existing
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
@ -518,7 +519,7 @@ public class KeychainIntentService extends IntentService
|
||||
|
||||
setProgress(R.string.progress_saving_key_ring, 90, 100);
|
||||
// save the pair
|
||||
ProviderHelper.saveKeyRing(this, pair.second, pair.first);
|
||||
providerHelper.saveKeyRing(pair.second, pair.first);
|
||||
setProgress(R.string.progress_done, 100, 100);
|
||||
}
|
||||
PassphraseCacheService.addCachedPassphrase(this, masterKeyId, newPassphrase);
|
||||
@ -707,7 +708,8 @@ public class KeychainIntentService extends IntentService
|
||||
/* Operation */
|
||||
HkpKeyServer server = new HkpKeyServer(keyServer);
|
||||
|
||||
PGPPublicKeyRing keyring = (PGPPublicKeyRing) ProviderHelper.getPGPKeyRing(this, dataUri);
|
||||
ProviderHelper providerHelper = new ProviderHelper(this);
|
||||
PGPPublicKeyRing keyring = (PGPPublicKeyRing) providerHelper.getPGPKeyRing(dataUri);
|
||||
if (keyring != null) {
|
||||
PgpImportExport pgpImportExport = new PgpImportExport(this, null);
|
||||
|
||||
@ -808,12 +810,13 @@ public class KeychainIntentService extends IntentService
|
||||
throw new PgpGeneralException("Unable to obtain passphrase");
|
||||
}
|
||||
|
||||
ProviderHelper providerHelper = new ProviderHelper(this);
|
||||
PgpKeyOperation keyOperation = new PgpKeyOperation(new ProgressScaler(this, 0, 100, 100));
|
||||
PGPPublicKeyRing publicRing = ProviderHelper.getPGPPublicKeyRing(this, pubKeyId);
|
||||
PGPPublicKeyRing publicRing = providerHelper.getPGPPublicKeyRing(pubKeyId);
|
||||
PGPPublicKey publicKey = publicRing.getPublicKey(pubKeyId);
|
||||
PGPSecretKeyRing secretKeyRing = null;
|
||||
try {
|
||||
secretKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
|
||||
secretKeyRing = providerHelper.getPGPSecretKeyRing(masterKeyId);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
// TODO: throw exception here!
|
||||
|
@ -172,7 +172,7 @@ public class PassphraseCacheService extends Service {
|
||||
long masterKeyId = keyId;
|
||||
if (masterKeyId != Id.key.symmetric) {
|
||||
try {
|
||||
masterKeyId = ProviderHelper.getMasterKeyId(this,
|
||||
masterKeyId = new ProviderHelper(this).getMasterKeyId(
|
||||
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId)));
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
return null;
|
||||
@ -234,7 +234,7 @@ public class PassphraseCacheService extends Service {
|
||||
public static boolean hasPassphrase(Context context, long secretKeyId) {
|
||||
// check if the key has no passphrase
|
||||
try {
|
||||
PGPSecretKeyRing secRing = ProviderHelper.getPGPSecretKeyRing(context, secretKeyId);
|
||||
PGPSecretKeyRing secRing = new ProviderHelper(context).getPGPSecretKeyRing(secretKeyId);
|
||||
return hasPassphrase(secRing);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
|
@ -39,6 +39,7 @@ import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.beardedhen.androidbootstrap.BootstrapButton;
|
||||
import com.devspark.appmsg.AppMsg;
|
||||
|
||||
@ -159,7 +160,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
static final String USER_IDS_SELECTION = UserIds.IS_REVOKED + " = 0";
|
||||
|
||||
static final String[] KEYRING_PROJECTION =
|
||||
new String[] {
|
||||
new String[]{
|
||||
KeyRings._ID,
|
||||
KeyRings.MASTER_KEY_ID,
|
||||
KeyRings.FINGERPRINT,
|
||||
@ -171,7 +172,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
|
||||
@Override
|
||||
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
|
||||
switch(id) {
|
||||
switch (id) {
|
||||
case LOADER_ID_KEYRING: {
|
||||
Uri uri = KeyRings.buildUnifiedKeyRingUri(mDataUri);
|
||||
return new CursorLoader(this, uri, KEYRING_PROJECTION, null, null, null);
|
||||
@ -187,7 +188,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
|
||||
switch(loader.getId()) {
|
||||
switch (loader.getId()) {
|
||||
case LOADER_ID_KEYRING:
|
||||
// the first key here is our master key
|
||||
if (data.moveToFirst()) {
|
||||
@ -202,7 +203,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
byte[] fingerprintBlob = data.getBlob(INDEX_FINGERPRINT);
|
||||
String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob);
|
||||
((TextView) findViewById(R.id.fingerprint))
|
||||
.setText(PgpKeyHelper.colorizeFingerprint(fingerprint));
|
||||
.setText(PgpKeyHelper.colorizeFingerprint(fingerprint));
|
||||
}
|
||||
break;
|
||||
case LOADER_ID_USER_IDS:
|
||||
@ -213,7 +214,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<Cursor> loader) {
|
||||
switch(loader.getId()) {
|
||||
switch (loader.getId()) {
|
||||
case LOADER_ID_USER_IDS:
|
||||
mUserIdsAdapter.swapCursor(null);
|
||||
break;
|
||||
@ -225,7 +226,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements
|
||||
*/
|
||||
private void initiateSigning() {
|
||||
try {
|
||||
PGPPublicKeyRing pubring = ProviderHelper.getPGPPublicKeyRing(this, mPubKeyId);
|
||||
PGPPublicKeyRing pubring = new ProviderHelper(this).getPGPPublicKeyRing(mPubKeyId);
|
||||
|
||||
// if we have already signed this key, dont bother doing it again
|
||||
boolean alreadySigned = false;
|
||||
|
@ -290,7 +290,7 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
|
||||
|
||||
try {
|
||||
Uri secretUri = KeychainContract.KeyRingData.buildSecretKeyRingUri(mDataUri);
|
||||
mKeyRing = (PGPSecretKeyRing) ProviderHelper.getPGPKeyRing(this, secretUri);
|
||||
mKeyRing = (PGPSecretKeyRing) new ProviderHelper(this).getPGPKeyRing(secretUri);
|
||||
|
||||
PGPSecretKey masterKey = mKeyRing.getSecretKey();
|
||||
mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey());
|
||||
|
@ -53,6 +53,8 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
public static final int RESULT_CODE_PUBLIC_KEYS = 0x00007001;
|
||||
public static final int RESULT_CODE_SECRET_KEYS = 0x00007002;
|
||||
|
||||
ProviderHelper mProviderHelper;
|
||||
|
||||
OnAsymmetricKeySelection mKeySelectionListener;
|
||||
|
||||
// view
|
||||
@ -133,8 +135,10 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
long signatureKeyId = getArguments().getLong(ARG_SIGNATURE_KEY_ID);
|
||||
long[] encryptionKeyIds = getArguments().getLongArray(ARG_ENCRYPTION_KEY_IDS);
|
||||
|
||||
mProviderHelper = new ProviderHelper(getActivity());
|
||||
|
||||
// preselect keys given by arguments (given by Intent to EncryptActivity)
|
||||
preselectKeys(signatureKeyId, encryptionKeyIds);
|
||||
preselectKeys(signatureKeyId, encryptionKeyIds, mProviderHelper);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,11 +147,12 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
* @param preselectedSignatureKeyId
|
||||
* @param preselectedEncryptionKeyIds
|
||||
*/
|
||||
private void preselectKeys(long preselectedSignatureKeyId, long[] preselectedEncryptionKeyIds) {
|
||||
private void preselectKeys(long preselectedSignatureKeyId, long[] preselectedEncryptionKeyIds,
|
||||
ProviderHelper providerHelper) {
|
||||
if (preselectedSignatureKeyId != 0) {
|
||||
// TODO: don't use bouncy castle objects!
|
||||
try {
|
||||
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingWithKeyId(getActivity(),
|
||||
PGPSecretKeyRing keyRing = providerHelper.getPGPSecretKeyRingWithKeyId(
|
||||
preselectedSignatureKeyId);
|
||||
|
||||
PGPSecretKey masterKey = keyRing.getSecretKey();
|
||||
@ -167,7 +172,7 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
for (int i = 0; i < preselectedEncryptionKeyIds.length; ++i) {
|
||||
// TODO One query per selected key?! wtf
|
||||
try {
|
||||
long id = ProviderHelper.getMasterKeyId(getActivity(),
|
||||
long id = providerHelper.getMasterKeyId(
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(
|
||||
Long.toString(preselectedEncryptionKeyIds[i]))
|
||||
);
|
||||
@ -201,8 +206,8 @@ public class EncryptAsymmetricFragment extends Fragment {
|
||||
mMainUserIdRest.setText("");
|
||||
} else {
|
||||
// See if we can get a user_id from a unified query
|
||||
String userIdResult = (String) ProviderHelper.getUnifiedData(
|
||||
getActivity(), mSecretKeyId, KeyRings.USER_ID, ProviderHelper.FIELD_TYPE_STRING);
|
||||
String userIdResult = (String) mProviderHelper.getUnifiedData(
|
||||
mSecretKeyId, KeyRings.USER_ID, ProviderHelper.FIELD_TYPE_STRING);
|
||||
String[] userId = PgpKeyHelper.splitUserId(userIdResult);
|
||||
if (userId[0] != null) {
|
||||
mMainUserId.setText(userId[0]);
|
||||
|
@ -145,10 +145,11 @@ public class ViewCertActivity extends ActionBarActivity
|
||||
|
||||
PGPSignature sig = PgpConversionHelper.BytesToPGPSignature(data.getBlob(INDEX_DATA));
|
||||
try {
|
||||
PGPKeyRing signeeRing = ProviderHelper.getPGPKeyRing(this,
|
||||
ProviderHelper providerHelper = new ProviderHelper(this);
|
||||
PGPKeyRing signeeRing = providerHelper.getPGPKeyRing(
|
||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(
|
||||
Long.toString(data.getLong(INDEX_MASTER_KEY_ID))));
|
||||
PGPKeyRing signerRing = ProviderHelper.getPGPKeyRing(this,
|
||||
PGPKeyRing signerRing = providerHelper.getPGPKeyRing(
|
||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(
|
||||
Long.toString(sig.getKeyID())));
|
||||
|
||||
@ -230,7 +231,8 @@ public class ViewCertActivity extends ActionBarActivity
|
||||
Intent viewIntent = new Intent(this, ViewKeyActivity.class);
|
||||
|
||||
try {
|
||||
long signerMasterKeyId = ProviderHelper.getMasterKeyId(this,
|
||||
ProviderHelper providerHelper = new ProviderHelper(this);
|
||||
long signerMasterKeyId = providerHelper.getMasterKeyId(
|
||||
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(mSignerKeyId))
|
||||
);
|
||||
viewIntent.setData(KeyRings.buildGenericKeyRingUri(
|
||||
|
@ -54,12 +54,14 @@ import org.sufficientlysecure.keychain.ui.dialog.ShareQrCodeDialogFragment;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.Provider;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class ViewKeyActivity extends ActionBarActivity {
|
||||
|
||||
ExportHelper mExportHelper;
|
||||
ProviderHelper mProviderHelper;
|
||||
|
||||
protected Uri mDataUri;
|
||||
|
||||
@ -83,6 +85,7 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
mExportHelper = new ExportHelper(this);
|
||||
mProviderHelper = new ProviderHelper(this);
|
||||
|
||||
// let the actionbar look like Android's contact app
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
@ -134,19 +137,19 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
startActivity(homeIntent);
|
||||
return true;
|
||||
case R.id.menu_key_view_update:
|
||||
updateFromKeyserver(mDataUri);
|
||||
updateFromKeyserver(mDataUri, mProviderHelper);
|
||||
return true;
|
||||
case R.id.menu_key_view_export_keyserver:
|
||||
uploadToKeyserver(mDataUri);
|
||||
return true;
|
||||
case R.id.menu_key_view_export_file:
|
||||
exportToFile(mDataUri, mExportHelper);
|
||||
exportToFile(mDataUri, mExportHelper, mProviderHelper);
|
||||
return true;
|
||||
case R.id.menu_key_view_share_default_fingerprint:
|
||||
shareKey(mDataUri, true);
|
||||
shareKey(mDataUri, true, mProviderHelper);
|
||||
return true;
|
||||
case R.id.menu_key_view_share_default:
|
||||
shareKey(mDataUri, false);
|
||||
shareKey(mDataUri, false, mProviderHelper);
|
||||
return true;
|
||||
case R.id.menu_key_view_share_qr_code_fingerprint:
|
||||
shareKeyQrCode(mDataUri, true);
|
||||
@ -158,7 +161,7 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
shareNfc();
|
||||
return true;
|
||||
case R.id.menu_key_view_share_clipboard:
|
||||
copyToClipboard(mDataUri);
|
||||
copyToClipboard(mDataUri, mProviderHelper);
|
||||
return true;
|
||||
case R.id.menu_key_view_delete: {
|
||||
deleteKey(mDataUri, mExportHelper);
|
||||
@ -168,10 +171,10 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
private void exportToFile(Uri dataUri, ExportHelper exportHelper) {
|
||||
private void exportToFile(Uri dataUri, ExportHelper exportHelper, ProviderHelper providerHelper) {
|
||||
Uri baseUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri);
|
||||
|
||||
HashMap<String, Object> data = ProviderHelper.getGenericData(this,
|
||||
HashMap<String, Object> data = providerHelper.getGenericData(
|
||||
baseUri,
|
||||
new String[]{KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET},
|
||||
new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER});
|
||||
@ -189,9 +192,9 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
startActivityForResult(uploadIntent, Id.request.export_to_server);
|
||||
}
|
||||
|
||||
private void updateFromKeyserver(Uri dataUri) {
|
||||
byte[] blob = (byte[]) ProviderHelper.getGenericData(
|
||||
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
private void updateFromKeyserver(Uri dataUri, ProviderHelper providerHelper) {
|
||||
byte[] blob = (byte[]) providerHelper.getGenericData(
|
||||
KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob);
|
||||
|
||||
@ -202,11 +205,11 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
startActivityForResult(queryIntent, RESULT_CODE_LOOKUP_KEY);
|
||||
}
|
||||
|
||||
private void shareKey(Uri dataUri, boolean fingerprintOnly) {
|
||||
private void shareKey(Uri dataUri, boolean fingerprintOnly, ProviderHelper providerHelper) {
|
||||
String content = null;
|
||||
if (fingerprintOnly) {
|
||||
byte[] data = (byte[]) ProviderHelper.getGenericData(
|
||||
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
byte[] data = (byte[]) providerHelper.getGenericData(
|
||||
KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
KeychainContract.Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
if (data != null) {
|
||||
String fingerprint = PgpKeyHelper.convertFingerprintToHex(data);
|
||||
@ -220,7 +223,7 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
// get public keyring as ascii armored string
|
||||
try {
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
content = ProviderHelper.getKeyRingAsArmoredString(this, uri);
|
||||
content = providerHelper.getKeyRingAsArmoredString(uri);
|
||||
|
||||
// Android will fail with android.os.TransactionTooLargeException if key is too big
|
||||
// see http://www.lonestarprod.com/?p=34
|
||||
@ -256,11 +259,11 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
dialog.show(getSupportFragmentManager(), "shareQrCodeDialog");
|
||||
}
|
||||
|
||||
private void copyToClipboard(Uri dataUri) {
|
||||
private void copyToClipboard(Uri dataUri, ProviderHelper providerHelper) {
|
||||
// get public keyring as ascii armored string
|
||||
try {
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
String keyringArmored = ProviderHelper.getKeyRingAsArmoredString(this, uri);
|
||||
String keyringArmored = providerHelper.getKeyRingAsArmoredString(uri);
|
||||
|
||||
ClipboardReflection.copyToClipboard(this, keyringArmored);
|
||||
AppMsg.makeText(this, R.string.key_copied_to_clipboard, AppMsg.STYLE_INFO)
|
||||
@ -359,8 +362,8 @@ public class ViewKeyActivity extends ActionBarActivity {
|
||||
try {
|
||||
Uri blobUri =
|
||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
mNfcKeyringBytes = ProviderHelper.getPGPKeyRing(
|
||||
ViewKeyActivity.this, blobUri).getEncoded();
|
||||
mNfcKeyringBytes = mProviderHelper.getPGPKeyRing(
|
||||
blobUri).getEncoded();
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "Error parsing keyring", e);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
|
@ -332,7 +332,7 @@ public class ViewKeyMainFragment extends Fragment implements
|
||||
|
||||
private void encryptToContact(Uri dataUri) {
|
||||
try {
|
||||
long keyId = ProviderHelper.extractOrGetMasterKeyId(getActivity(), dataUri);
|
||||
long keyId = new ProviderHelper(getActivity()).extractOrGetMasterKeyId(dataUri);
|
||||
long[] encryptionKeyIds = new long[]{ keyId };
|
||||
Intent intent = new Intent(getActivity(), EncryptActivity.class);
|
||||
intent.setAction(EncryptActivity.ACTION_ENCRYPT);
|
||||
|
@ -102,7 +102,7 @@ public class DeleteKeyDialogFragment extends DialogFragment {
|
||||
|
||||
long masterKeyId = masterKeyIds[0];
|
||||
|
||||
HashMap<String, Object> data = ProviderHelper.getUnifiedData(activity, masterKeyId, new String[]{
|
||||
HashMap<String, Object> data = new ProviderHelper(activity).getUnifiedData(masterKeyId, new String[]{
|
||||
KeyRings.USER_ID,
|
||||
KeyRings.HAS_SECRET
|
||||
}, new int[] { ProviderHelper.FIELD_TYPE_STRING, ProviderHelper.FIELD_TYPE_INTEGER });
|
||||
|
@ -140,7 +140,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
|
||||
} else {
|
||||
try {
|
||||
secretKey = ProviderHelper.getPGPSecretKeyRing(activity, secretKeyId).getSecretKey();
|
||||
secretKey = new ProviderHelper(activity).getPGPSecretKeyRing(secretKeyId).getSecretKey();
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
alert.setTitle(R.string.title_key_not_found);
|
||||
alert.setMessage(getString(R.string.key_not_found, secretKeyId));
|
||||
@ -196,8 +196,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
return;
|
||||
} else {
|
||||
try {
|
||||
clickSecretKey = PgpKeyHelper.getKeyNum(ProviderHelper
|
||||
.getPGPSecretKeyRingWithKeyId(activity, secretKeyId),
|
||||
clickSecretKey = PgpKeyHelper.getKeyNum(new ProviderHelper(activity)
|
||||
.getPGPSecretKeyRingWithKeyId(secretKeyId),
|
||||
curKeyIndex);
|
||||
} catch (ProviderHelper.NotFoundException e) {
|
||||
Log.e(Constants.TAG, "key not found!", e);
|
||||
|
@ -41,6 +41,7 @@ import org.sufficientlysecure.keychain.util.Log;
|
||||
import org.sufficientlysecure.keychain.util.QrCodeUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.Provider;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ShareQrCodeDialogFragment extends DialogFragment {
|
||||
@ -91,14 +92,15 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
|
||||
mImage = (ImageView) view.findViewById(R.id.share_qr_code_dialog_image);
|
||||
mText = (TextView) view.findViewById(R.id.share_qr_code_dialog_text);
|
||||
|
||||
ProviderHelper providerHelper = new ProviderHelper(getActivity());
|
||||
String content = null;
|
||||
if (mFingerprintOnly) {
|
||||
alert.setPositiveButton(R.string.btn_okay, null);
|
||||
|
||||
byte[] blob = (byte[]) ProviderHelper.getGenericData(
|
||||
getActivity(), KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
byte[] blob = (byte[]) providerHelper.getGenericData(
|
||||
KeyRings.buildUnifiedKeyRingUri(dataUri),
|
||||
KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB);
|
||||
if(blob == null) {
|
||||
if (blob == null) {
|
||||
Log.e(Constants.TAG, "key not found!");
|
||||
AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show();
|
||||
return null;
|
||||
@ -113,7 +115,7 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
|
||||
|
||||
try {
|
||||
Uri uri = KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||
content = ProviderHelper.getKeyRingAsArmoredString(getActivity(), uri);
|
||||
content = providerHelper.getKeyRingAsArmoredString(uri);
|
||||
} catch (IOException e) {
|
||||
Log.e(Constants.TAG, "error processing key!", e);
|
||||
AppMsg.makeText(getActivity(), R.string.error_invalid_data, AppMsg.STYLE_ALERT).show();
|
||||
|
Loading…
Reference in New Issue
Block a user