Put PgpMain methods in separate opbject classes, handle passphrase dialog in EditKey not in SecretKeyList

This commit is contained in:
Dominik Schürmann
2013-09-15 16:42:08 +02:00
parent 121f8aaca0
commit 5aebd115d4
33 changed files with 2774 additions and 2603 deletions

View File

@@ -26,7 +26,7 @@ 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.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.SelectSecretKeyActivity;
import org.sufficientlysecure.keychain.util.KeyValueSpinnerAdapter;
@@ -259,9 +259,9 @@ public class AppSettingsFragment extends Fragment {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(
getActivity(), secretKeyId);
if (keyRing != null) {
PGPSecretKey key = PgpHelper.getMasterKey(keyRing);
PGPSecretKey key = PgpKeyHelper.getMasterKey(keyRing);
if (key != null) {
String userId = PgpHelper.getMainUserIdSafe(getActivity(), key);
String userId = PgpKeyHelper.getMainUserIdSafe(getActivity(), key);
String chunks[] = userId.split(" <", 2);
uid = chunks[0];
if (chunks.length > 1) {

View File

@@ -29,8 +29,7 @@ import org.spongycastle.openpgp.PGPPrivateKey;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openssl.PEMWriter;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpMain;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.pgp.PgpToX509;
import org.sufficientlysecure.keychain.util.Log;
@@ -53,13 +52,13 @@ public class ExtendedApiService extends RemoteService {
try {
long keyId = appSettings.getKeyId();
PGPSecretKey pgpSecretKey = PgpHelper.getSigningKey(this, keyId);
PGPSecretKey pgpSecretKey = PgpKeyHelper.getSigningKey(this, keyId);
PasswordCallback pgpSecKeyPasswordCallBack = new PasswordCallback("pgp passphrase?",
false);
pgpPwdCallbackHandler.handle(new Callback[] { pgpSecKeyPasswordCallBack });
PGPPrivateKey pgpPrivKey = pgpSecretKey.extractPrivateKey(
pgpSecKeyPasswordCallBack.getPassword(), PgpMain.BOUNCY_CASTLE_PROVIDER_NAME);
pgpSecKeyPasswordCallBack.getPassword(), Constants.BOUNCY_CASTLE_PROVIDER_NAME);
pgpSecKeyPasswordCallBack.clearPassword();
X509Certificate selfSignedCert = PgpToX509.createSelfSignedCert(pgpSecretKey,

View File

@@ -32,7 +32,10 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.Preferences;
import org.sufficientlysecure.keychain.pgp.PgpMain;
import org.sufficientlysecure.keychain.pgp.PgpHelper;
import org.sufficientlysecure.keychain.pgp.PgpOperation;
import org.sufficientlysecure.keychain.pgp.exception.NoAsymmetricEncryptionException;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.service.KeychainIntentService;
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
@@ -230,6 +233,7 @@ public class OpenPgpService extends RemoteService {
return;
}
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
if (sign) {
String passphrase = getCachedPassphrase(appSettings.getKeyId());
if (passphrase == null) {
@@ -238,13 +242,11 @@ public class OpenPgpService extends RemoteService {
return;
}
PgpMain.encryptAndSign(getContext(), null, inputData, outputStream, asciiArmor,
appSettings.getCompression(), keyIds, null,
operation.encryptAndSign(asciiArmor, appSettings.getCompression(), keyIds, null,
appSettings.getEncryptionAlgorithm(), appSettings.getKeyId(),
appSettings.getHashAlgorithm(), true, passphrase);
} else {
PgpMain.encryptAndSign(getContext(), null, inputData, outputStream, asciiArmor,
appSettings.getCompression(), keyIds, null,
operation.encryptAndSign(asciiArmor, appSettings.getCompression(), keyIds, null,
appSettings.getEncryptionAlgorithm(), Id.key.none,
appSettings.getHashAlgorithm(), true, null);
}
@@ -286,9 +288,9 @@ public class OpenPgpService extends RemoteService {
return;
}
PgpMain.signText(this, null, inputData, outputStream, appSettings.getKeyId(),
passphrase, appSettings.getHashAlgorithm(), Preferences.getPreferences(this)
.getForceV3Signatures());
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
operation.signText(appSettings.getKeyId(), passphrase, appSettings.getHashAlgorithm(),
Preferences.getPreferences(this).getForceV3Signatures());
outputStream.close();
@@ -315,7 +317,7 @@ public class OpenPgpService extends RemoteService {
String message = new String(inputBytes);
Log.d(Constants.TAG, "in: " + message);
boolean signedOnly = false;
Matcher matcher = PgpMain.PGP_MESSAGE.matcher(message);
Matcher matcher = PgpHelper.PGP_MESSAGE.matcher(message);
if (matcher.matches()) {
Log.d(Constants.TAG, "PGP_MESSAGE matched");
message = matcher.group(1);
@@ -325,7 +327,7 @@ public class OpenPgpService extends RemoteService {
// overwrite inputBytes
inputBytes = message.getBytes();
} else {
matcher = PgpMain.PGP_SIGNED_MESSAGE.matcher(message);
matcher = PgpHelper.PGP_SIGNED_MESSAGE.matcher(message);
if (matcher.matches()) {
signedOnly = true;
Log.d(Constants.TAG, "PGP_SIGNED_MESSAGE matched");
@@ -368,19 +370,18 @@ public class OpenPgpService extends RemoteService {
// than 0.
inputStream2.mark(200);
}
secretKeyId = PgpMain.getDecryptionKeyId(this, inputStream2);
secretKeyId = PgpHelper.getDecryptionKeyId(this, inputStream2);
if (secretKeyId == Id.key.none) {
throw new PgpMain.PgpGeneralException(
getString(R.string.error_noSecretKeyFound));
throw new PgpGeneralException(getString(R.string.error_noSecretKeyFound));
}
assumeSymmetricEncryption = false;
} catch (PgpMain.NoAsymmetricEncryptionException e) {
} catch (NoAsymmetricEncryptionException e) {
if (inputStream2.markSupported()) {
inputStream2.reset();
}
secretKeyId = Id.key.symmetric;
if (!PgpMain.hasSymmetricEncryption(this, inputStream2)) {
throw new PgpMain.PgpGeneralException(
if (!PgpOperation.hasSymmetricEncryption(this, inputStream2)) {
throw new PgpGeneralException(
getString(R.string.error_noKnownEncryptionFound));
}
assumeSymmetricEncryption = true;
@@ -404,13 +405,13 @@ public class OpenPgpService extends RemoteService {
OutputStream outputStream = new ByteArrayOutputStream();
Bundle outputBundle;
PgpOperation operation = new PgpOperation(getContext(), null, inputData, outputStream);
if (signedOnly) {
// TODO: download missing keys from keyserver?
outputBundle = PgpMain.verifyText(this, null, inputData, outputStream, false);
outputBundle = operation.verifyText(false);
} else {
// TODO: assume symmetric: callback to enter symmetric pass
outputBundle = PgpMain.decryptAndVerify(this, null, inputData, outputStream,
passphrase, assumeSymmetricEncryption);
outputBundle = operation.decryptAndVerify(passphrase, assumeSymmetricEncryption);
}
outputStream.close();

View File

@@ -24,7 +24,7 @@ import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.helper.ActionBarHelper;
import org.sufficientlysecure.keychain.pgp.PgpMain;
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.ui.SelectPublicKeyFragment;
import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment;
@@ -316,7 +316,7 @@ public class RemoteServiceActivity extends SherlockFragmentActivity {
messenger, secretKeyId);
passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog");
} catch (PgpMain.PgpGeneralException e) {
} catch (PgpGeneralException e) {
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
// send message to handler to start encryption directly
returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY);