Introduce ProviderHelper.NotFoundException, remove dead code

This commit is contained in:
Dominik Schürmann 2014-04-08 22:25:42 +02:00
parent 946c1e115c
commit a1efb24228
12 changed files with 150 additions and 126 deletions

View File

@ -50,13 +50,17 @@ public class ExportHelper {
}
public void deleteKey(Uri dataUri, Handler deleteHandler) {
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(deleteHandler);
long masterKeyId = ProviderHelper.getMasterKeyId(mActivity, dataUri);
try {
long masterKeyId = ProviderHelper.getMasterKeyId(mActivity, dataUri);
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
new long[]{ masterKeyId });
deleteKeyDialog.show(mActivity.getSupportFragmentManager(), "deleteKeyDialog");
// Create a new Messenger for the communication back
Messenger messenger = new Messenger(deleteHandler);
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
new long[]{ masterKeyId });
deleteKeyDialog.show(mActivity.getSupportFragmentManager(), "deleteKeyDialog");
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
/**

View File

@ -54,6 +54,7 @@ import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactory
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
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.ProviderHelper;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
@ -235,10 +236,19 @@ public class PgpDecryptVerify {
updateProgress(R.string.progress_finding_key, currentProgress, 100);
PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj;
long masterKeyId = ProviderHelper.getMasterKeyId(mContext,
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(encData.getKeyID()))
);
// get master key id for this encryption key id
long masterKeyId = 0;
try {
masterKeyId = ProviderHelper.getMasterKeyId(mContext,
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(encData.getKeyID()))
);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
// get actual keyring object based on master key id
PGPSecretKeyRing secretKeyRing = ProviderHelper.getPGPSecretKeyRing(mContext, masterKeyId);
if (secretKeyRing == null) {
throw new PgpGeneralException(mContext.getString(R.string.error_no_secret_key_found));
}

View File

@ -24,17 +24,12 @@ import android.content.pm.PackageManager.NameNotFoundException;
import org.spongycastle.openpgp.PGPEncryptedDataList;
import org.spongycastle.openpgp.PGPObjectFactory;
import org.spongycastle.openpgp.PGPPublicKeyEncryptedData;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.spongycastle.openpgp.PGPUtil;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.exception.NoAsymmetricEncryptionException;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.ProgressDialogUpdater;
@ -43,7 +38,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.security.SecureRandom;
import java.util.Iterator;
import java.util.regex.Pattern;
public class PgpHelper {
@ -76,52 +70,6 @@ public class PgpHelper {
return "OpenPGP Keychain v" + getVersion(context);
}
public static long getDecryptionKeyId(Context context, InputStream inputStream)
throws PgpGeneralException, NoAsymmetricEncryptionException, IOException {
InputStream in = PGPUtil.getDecoderStream(inputStream);
PGPObjectFactory pgpF = new PGPObjectFactory(in);
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
// the first object might be a PGP marker packet.
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
if (enc == null) {
throw new PgpGeneralException(context.getString(R.string.error_invalid_data));
}
// TODO: currently we always only look at the first known key
// find the secret key
PGPSecretKey secretKey = null;
Iterator<?> it = enc.getEncryptedDataObjects();
boolean gotAsymmetricEncryption = false;
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof PGPPublicKeyEncryptedData) {
gotAsymmetricEncryption = true;
PGPPublicKeyEncryptedData pbe = (PGPPublicKeyEncryptedData) obj;
secretKey = ProviderHelper.getPGPSecretKeyRing(context, pbe.getKeyID()).getSecretKey();
if (secretKey != null) {
break;
}
}
}
if (!gotAsymmetricEncryption) {
throw new NoAsymmetricEncryptionException();
}
if (secretKey == null) {
return Id.key.none;
}
return secretKey.getKeyID();
}
public static int getStreamContent(Context context, InputStream inStream) throws IOException {
InputStream in = PGPUtil.getDecoderStream(inStream);
PGPObjectFactory pgpF = new PGPObjectFactory(in);

View File

@ -64,6 +64,15 @@ import java.util.Set;
public class ProviderHelper {
public static class NotFoundException extends Exception {
public NotFoundException() {
}
public NotFoundException(String name) {
super(name);
}
}
// If we ever switch to api level 11, we can ditch this whole mess!
public static final int FIELD_TYPE_NULL = 1;
// this is called integer to stay coherent with the constants in Cursor (api level 11)
@ -113,7 +122,7 @@ public class ProviderHelper {
* 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) {
public static long getMasterKeyId(Context context, Uri queryUri) throws NotFoundException {
// try extracting from the uri first
String firstSegment = queryUri.getPathSegments().get(1);
if(!firstSegment.equals("find")) try {
@ -123,10 +132,11 @@ public class ProviderHelper {
Log.d(Constants.TAG, "Couldn't get masterKeyId from URI, querying...");
}
Object data = getGenericData(context, queryUri, KeyRings.MASTER_KEY_ID, FIELD_TYPE_INTEGER);
if(data != null)
if(data != null) {
return (Long) data;
// TODO better error handling?
return 0L;
} else {
throw new NotFoundException();
}
}
public static Map<Long, PGPKeyRing> getPGPKeyRings(Context context, Uri queryUri) {
@ -158,17 +168,23 @@ public class ProviderHelper {
public static PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(Context context, long keyId) {
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
long masterKeyId = getMasterKeyId(context, uri);
if(masterKeyId != 0)
long masterKeyId;
try {
masterKeyId = getMasterKeyId(context, uri);
return getPGPPublicKeyRing(context, masterKeyId);
return null;
} catch (NotFoundException e) {
return null;
}
}
public static PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(Context context, long keyId) {
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
long masterKeyId = getMasterKeyId(context, uri);
if(masterKeyId != 0)
long masterKeyId;
try {
masterKeyId = getMasterKeyId(context, uri);
return getPGPSecretKeyRing(context, masterKeyId);
return null;
} catch (NotFoundException notFound) {
return null;
}
}
/**

View File

@ -32,6 +32,7 @@ import android.widget.TextView;
import com.beardedhen.androidbootstrap.BootstrapButton;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
@ -40,6 +41,7 @@ import org.sufficientlysecure.keychain.ui.EditKeyActivity;
import org.sufficientlysecure.keychain.ui.SelectSecretKeyLayoutFragment;
import org.sufficientlysecure.keychain.ui.adapter.KeyValueSpinnerAdapter;
import org.sufficientlysecure.keychain.util.AlgorithmNames;
import org.sufficientlysecure.keychain.util.Log;
public class AccountSettingsFragment extends Fragment implements
SelectSecretKeyLayoutFragment.SelectSecretKeyCallback {
@ -177,8 +179,12 @@ public class AccountSettingsFragment extends Fragment implements
case REQUEST_CODE_CREATE_KEY: {
if (resultCode == Activity.RESULT_OK) {
// select newly created key
long masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), data.getData());
mSelectKeyFragment.selectKey(masterKeyId);
try {
long masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), data.getData());
mSelectKeyFragment.selectKey(masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
break;
}

View File

@ -171,11 +171,12 @@ public class PassphraseCacheService extends Service {
// try to get master key id which is used as an identifier for cached passphrases
long masterKeyId = keyId;
if (masterKeyId != Id.key.symmetric) {
masterKeyId = ProviderHelper.getMasterKeyId(this,
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId)));
// Failure
if(masterKeyId == 0)
try {
masterKeyId = ProviderHelper.getMasterKeyId(this,
KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId)));
} catch (ProviderHelper.NotFoundException e) {
return null;
}
}
Log.d(TAG, "getCachedPassphraseImpl() for masterKeyId " + masterKeyId);

View File

@ -286,9 +286,13 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
} else {
Log.d(Constants.TAG, "uri: " + mDataUri);
// get master key id using row id
long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri);
finallyEdit(masterKeyId);
try {
// get master key id using row id
long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri);
finallyEdit(masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
}

View File

@ -34,11 +34,14 @@ import org.spongycastle.openpgp.PGPPublicKey;
import org.spongycastle.openpgp.PGPPublicKeyRing;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.sufficientlysecure.keychain.Constants;
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 org.sufficientlysecure.keychain.util.Log;
import java.util.HashMap;
import java.util.Vector;
@ -160,11 +163,15 @@ public class EncryptAsymmetricFragment extends Fragment {
if (preselectedEncryptionKeyIds != null) {
Vector<Long> goodIds = new Vector<Long>();
for (int i = 0; i < preselectedEncryptionKeyIds.length; ++i) {
long id = ProviderHelper.getMasterKeyId(getActivity(),
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(preselectedEncryptionKeyIds[i]))
);
// TODO check for available encrypt keys... is this even relevant?
goodIds.add(id);
try {
long id = ProviderHelper.getMasterKeyId(getActivity(),
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(preselectedEncryptionKeyIds[i]))
);
goodIds.add(id);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
if (goodIds.size() > 0) {
long[] keyIds = new long[goodIds.size()];

View File

@ -234,17 +234,20 @@ public class ViewCertActivity extends ActionBarActivity
} else {
viewIntent = new Intent(this, ViewKeyActivityJB.class);
}
//
long signerMasterKeyId = ProviderHelper.getMasterKeyId(this,
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(mSignerKeyId))
);
// TODO notify user of this, maybe offer download?
if (mSignerKeyId == 0L)
return true;
viewIntent.setData(KeyRings.buildGenericKeyRingUri(
Long.toString(signerMasterKeyId))
);
startActivity(viewIntent);
try {
long signerMasterKeyId = ProviderHelper.getMasterKeyId(this,
KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(mSignerKeyId))
);
viewIntent.setData(KeyRings.buildGenericKeyRingUri(
Long.toString(signerMasterKeyId))
);
startActivity(viewIntent);
} catch (ProviderHelper.NotFoundException e) {
// TODO notify user of this, maybe offer download?
Log.e(Constants.TAG, "key not found!", e);
}
return true;
}
return super.onOptionsItemSelected(item);

View File

@ -43,6 +43,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.adapter.TabsAdapter;
import org.sufficientlysecure.keychain.ui.dialog.ShareNfcDialogFragment;
import org.sufficientlysecure.keychain.ui.dialog.ShareQrCodeDialogFragment;
import org.sufficientlysecure.keychain.util.Log;
import java.util.ArrayList;
import java.util.HashMap;
@ -184,7 +185,7 @@ public class ViewKeyActivity extends ActionBarActivity {
}
private void shareKey(Uri dataUri, boolean fingerprintOnly) {
String content;
String content = null;
if (fingerprintOnly) {
byte[] data = (byte[]) ProviderHelper.getGenericData(
this, KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri),
@ -199,27 +200,36 @@ public class ViewKeyActivity extends ActionBarActivity {
}
} else {
// get public keyring as ascii armored string
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
this, new long[]{masterKeyId});
try {
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
content = keyringArmored.get(0);
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
this, new long[]{masterKeyId});
// Android will fail with android.os.TransactionTooLargeException if key is too big
// see http://www.lonestarprod.com/?p=34
if (content.length() >= 86389) {
Toast.makeText(getApplicationContext(), R.string.key_too_big_for_sharing,
Toast.LENGTH_LONG).show();
return;
content = keyringArmored.get(0);
// Android will fail with android.os.TransactionTooLargeException if key is too big
// see http://www.lonestarprod.com/?p=34
if (content.length() >= 86389) {
Toast.makeText(getApplicationContext(), R.string.key_too_big_for_sharing,
Toast.LENGTH_LONG).show();
return;
}
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
// let user choose application
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent,
getResources().getText(R.string.action_share_key_with)));
if (content != null) {
// let user choose application
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, content);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent,
getResources().getText(R.string.action_share_key_with)));
} else {
Log.e(Constants.TAG, "content is null!");
}
}
private void shareKeyQrCode(Uri dataUri, boolean fingerprintOnly) {
@ -230,13 +240,18 @@ public class ViewKeyActivity extends ActionBarActivity {
private void copyToClipboard(Uri dataUri) {
// get public keyring as ascii armored string
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
this, new long[]{masterKeyId});
try {
long masterKeyId = ProviderHelper.getMasterKeyId(this, dataUri);
ClipboardReflection.copyToClipboard(this, keyringArmored.get(0));
Toast.makeText(getApplicationContext(), R.string.key_copied_to_clipboard, Toast.LENGTH_LONG)
.show();
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
this, new long[]{masterKeyId});
ClipboardReflection.copyToClipboard(this, keyringArmored.get(0));
Toast.makeText(getApplicationContext(), R.string.key_copied_to_clipboard, Toast.LENGTH_LONG)
.show();
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
private void shareNfc() {

View File

@ -328,14 +328,18 @@ public class ViewKeyMainFragment extends Fragment implements
private void encryptToContact(Uri dataUri) {
// TODO preselect from uri? should be feasible without trivial query
long keyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);
try {
long keyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);
long[] encryptionKeyIds = new long[]{ keyId };
Intent intent = new Intent(getActivity(), EncryptActivity.class);
intent.setAction(EncryptActivity.ACTION_ENCRYPT);
intent.putExtra(EncryptActivity.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
// used instead of startActivity set actionbar based on callingPackage
startActivityForResult(intent, 0);
long[] encryptionKeyIds = new long[]{ keyId };
Intent intent = new Intent(getActivity(), EncryptActivity.class);
intent.setAction(EncryptActivity.ACTION_ENCRYPT);
intent.putExtra(EncryptActivity.EXTRA_ENCRYPTION_KEY_IDS, encryptionKeyIds);
// used instead of startActivity set actionbar based on callingPackage
startActivityForResult(intent, 0);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
private void certifyKey(Uri dataUri) {

View File

@ -33,6 +33,7 @@ import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.Log;
import org.sufficientlysecure.keychain.util.QrCodeUtils;
import java.util.ArrayList;
@ -106,7 +107,12 @@ public class ShareQrCodeDialogFragment extends DialogFragment {
mText.setText(R.string.share_qr_code_dialog_start);
// TODO works, but
long masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);
long masterKeyId = 0;
try {
masterKeyId = ProviderHelper.getMasterKeyId(getActivity(), dataUri);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
// get public keyring as ascii armored string
ArrayList<String> keyringArmored = ProviderHelper.getKeyRingsAsArmoredString(
getActivity(), new long[] { masterKeyId });