mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-12-25 08:28:50 -05:00
wrapped-key-ring: no more pgp imports in PassphraseDialogFragment
This commit is contained in:
parent
f524fa692c
commit
b5647b2062
@ -41,17 +41,18 @@ public class CachedSecretKey extends CachedPublicKey {
|
|||||||
return (CachedSecretKeyRing) mRing;
|
return (CachedSecretKeyRing) mRing;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unlock(String passphrase) throws PgpGeneralException {
|
public boolean unlock(String passphrase) throws PgpGeneralException {
|
||||||
try {
|
try {
|
||||||
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider(
|
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider(
|
||||||
Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray());
|
Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray());
|
||||||
mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor);
|
mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor);
|
||||||
} catch (PGPException e) {
|
} catch (PGPException e) {
|
||||||
throw new PgpGeneralException("error extracting key!", e);
|
return false;
|
||||||
}
|
}
|
||||||
if(mPrivateKey == null) {
|
if(mPrivateKey == null) {
|
||||||
throw new PgpGeneralException("error extracting key (bad passphrase?)");
|
throw new PgpGeneralException("error extracting key");
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PGPSignatureGenerator getSignatureGenerator(int hashAlgo, boolean cleartext)
|
public PGPSignatureGenerator getSignatureGenerator(int hashAlgo, boolean cleartext)
|
||||||
|
@ -344,9 +344,11 @@ public class PgpDecryptVerify {
|
|||||||
currentProgress += 5;
|
currentProgress += 5;
|
||||||
updateProgress(R.string.progress_extracting_key, currentProgress, 100);
|
updateProgress(R.string.progress_extracting_key, currentProgress, 100);
|
||||||
try {
|
try {
|
||||||
secretEncryptionKey.unlock(mPassphrase);
|
if (!secretEncryptionKey.unlock(mPassphrase)) {
|
||||||
} catch (PgpGeneralException e) {
|
throw new WrongPassphraseException();
|
||||||
throw new WrongPassphraseException();
|
}
|
||||||
|
} catch(PgpGeneralException e) {
|
||||||
|
throw new KeyExtractionException();
|
||||||
}
|
}
|
||||||
currentProgress += 5;
|
currentProgress += 5;
|
||||||
updateProgress(R.string.progress_preparing_streams, currentProgress, 100);
|
updateProgress(R.string.progress_preparing_streams, currentProgress, 100);
|
||||||
|
@ -338,8 +338,8 @@ public class PgpKeyOperation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UncachedKeyRing buildSecretKey(CachedSecretKeyRing wmKR,
|
public UncachedKeyRing buildSecretKey(CachedSecretKeyRing wmKR,
|
||||||
CachedPublicKeyRing wpKR,
|
CachedPublicKeyRing wpKR,
|
||||||
SaveKeyringParcel saveParcel)
|
SaveKeyringParcel saveParcel)
|
||||||
throws PgpGeneralMsgIdException, PGPException, SignatureException, IOException {
|
throws PgpGeneralMsgIdException, PGPException, SignatureException, IOException {
|
||||||
|
|
||||||
PGPSecretKeyRing mKR = wmKR.getRing();
|
PGPSecretKeyRing mKR = wmKR.getRing();
|
||||||
|
@ -293,14 +293,6 @@ public class ProviderHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public PGPPublicKeyRing getPGPPublicKeyRingWithKeyId(long keyId)
|
|
||||||
throws NotFoundException {
|
|
||||||
Uri uri = KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(Long.toString(keyId));
|
|
||||||
long masterKeyId = getMasterKeyId(uri);
|
|
||||||
return getPGPPublicKeyRing(masterKeyId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(long keyId)
|
public PGPSecretKeyRing getPGPSecretKeyRingWithKeyId(long keyId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
@ -801,7 +801,9 @@ public class KeychainIntentService extends IntentService
|
|||||||
CachedPublicKeyRing publicRing = providerHelper.getCachedPublicKeyRing(pubKeyId);
|
CachedPublicKeyRing publicRing = providerHelper.getCachedPublicKeyRing(pubKeyId);
|
||||||
CachedSecretKeyRing secretKeyRing = providerHelper.getCachedSecretKeyRing(masterKeyId);
|
CachedSecretKeyRing secretKeyRing = providerHelper.getCachedSecretKeyRing(masterKeyId);
|
||||||
CachedSecretKey certificationKey = secretKeyRing.getSubKey();
|
CachedSecretKey certificationKey = secretKeyRing.getSubKey();
|
||||||
certificationKey.unlock(signaturePassphrase);
|
if(!certificationKey.unlock(signaturePassphrase)) {
|
||||||
|
throw new PgpGeneralException("Error extracting key (bad passphrase?)");
|
||||||
|
}
|
||||||
UncachedKeyRing newRing = certificationKey.certifyUserIds(publicRing, userIds);
|
UncachedKeyRing newRing = certificationKey.certifyUserIds(publicRing, userIds);
|
||||||
|
|
||||||
// store the signed key in our local cache
|
// store the signed key in our local cache
|
||||||
|
@ -41,17 +41,12 @@ import android.widget.TextView;
|
|||||||
import android.widget.TextView.OnEditorActionListener;
|
import android.widget.TextView.OnEditorActionListener;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import org.spongycastle.openpgp.PGPException;
|
|
||||||
import org.spongycastle.openpgp.PGPPrivateKey;
|
|
||||||
import org.spongycastle.openpgp.PGPSecretKey;
|
|
||||||
import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor;
|
|
||||||
import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
|
import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround;
|
||||||
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
|
import org.sufficientlysecure.keychain.pgp.CachedSecretKey;
|
||||||
|
import org.sufficientlysecure.keychain.pgp.CachedSecretKeyRing;
|
||||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
|
||||||
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||||
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
|
import org.sufficientlysecure.keychain.service.PassphraseCacheService;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
@ -139,18 +134,17 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
|||||||
|
|
||||||
alert.setTitle(R.string.title_authentication);
|
alert.setTitle(R.string.title_authentication);
|
||||||
|
|
||||||
final PGPSecretKey secretKey;
|
final CachedSecretKeyRing secretRing;
|
||||||
final String userId;
|
final String userId;
|
||||||
|
|
||||||
if (secretKeyId == Constants.key.symmetric || secretKeyId == Constants.key.none) {
|
if (secretKeyId == Constants.key.symmetric || secretKeyId == Constants.key.none) {
|
||||||
secretKey = null;
|
secretRing = null;
|
||||||
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
|
alert.setMessage(R.string.passphrase_for_symmetric_encryption);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
ProviderHelper helper = new ProviderHelper(activity);
|
ProviderHelper helper = new ProviderHelper(activity);
|
||||||
secretKey = helper.getPGPSecretKeyRing(secretKeyId).getSecretKey();
|
secretRing = helper.getCachedSecretKeyRing(secretKeyId);
|
||||||
userId = (String) helper.getUnifiedData(secretKeyId,
|
userId = secretRing.getPrimaryUserId();
|
||||||
KeychainContract.KeyRings.USER_ID, ProviderHelper.FIELD_TYPE_STRING);
|
|
||||||
} catch (ProviderHelper.NotFoundException e) {
|
} catch (ProviderHelper.NotFoundException e) {
|
||||||
alert.setTitle(R.string.title_key_not_found);
|
alert.setTitle(R.string.title_key_not_found);
|
||||||
alert.setMessage(getString(R.string.key_not_found, secretKeyId));
|
alert.setMessage(getString(R.string.key_not_found, secretKeyId));
|
||||||
@ -179,76 +173,59 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int id) {
|
public void onClick(DialogInterface dialog, int id) {
|
||||||
dismiss();
|
dismiss();
|
||||||
long curKeyIndex = 1;
|
|
||||||
boolean keyOK = true;
|
|
||||||
String passphrase = mPassphraseEditText.getText().toString();
|
String passphrase = mPassphraseEditText.getText().toString();
|
||||||
long keyId;
|
|
||||||
PGPSecretKey clickSecretKey = secretKey;
|
|
||||||
|
|
||||||
if (clickSecretKey != null) {
|
// Early breakout if we are dealing with a symmetric key
|
||||||
while (keyOK) {
|
if (secretRing == null) {
|
||||||
if (clickSecretKey != null) { // check again for loop
|
PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, passphrase);
|
||||||
try {
|
// also return passphrase back to activity
|
||||||
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
|
Bundle data = new Bundle();
|
||||||
.setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(
|
data.putString(MESSAGE_DATA_PASSPHRASE, passphrase);
|
||||||
passphrase.toCharArray());
|
sendMessageToHandler(MESSAGE_OKAY, data);
|
||||||
PGPPrivateKey testKey = clickSecretKey
|
return;
|
||||||
.extractPrivateKey(keyDecryptor);
|
|
||||||
if (testKey == null) {
|
|
||||||
if (!clickSecretKey.isMasterKey()) {
|
|
||||||
Toast.makeText(activity,
|
|
||||||
R.string.error_could_not_extract_private_key,
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
sendMessageToHandler(MESSAGE_CANCEL);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
clickSecretKey = PgpKeyHelper.getKeyNum(new ProviderHelper(activity)
|
|
||||||
.getPGPSecretKeyRingWithKeyId(secretKeyId),
|
|
||||||
curKeyIndex
|
|
||||||
);
|
|
||||||
} catch (ProviderHelper.NotFoundException e) {
|
|
||||||
Log.e(Constants.TAG, "key not found!", e);
|
|
||||||
}
|
|
||||||
curKeyIndex++; // does post-increment work like C?
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
keyOK = false;
|
|
||||||
}
|
|
||||||
} catch (PGPException e) {
|
|
||||||
Toast.makeText(activity, R.string.wrong_passphrase,
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
sendMessageToHandler(MESSAGE_CANCEL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Toast.makeText(activity, R.string.error_could_not_extract_private_key,
|
|
||||||
Toast.LENGTH_SHORT).show();
|
|
||||||
|
|
||||||
sendMessageToHandler(MESSAGE_CANCEL);
|
|
||||||
return; // ran out of keys to try
|
|
||||||
}
|
|
||||||
}
|
|
||||||
keyId = secretKey.getKeyID();
|
|
||||||
} else {
|
|
||||||
keyId = Constants.key.symmetric;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CachedSecretKey unlockedSecretKey = null;
|
||||||
|
|
||||||
|
for(CachedSecretKey clickSecretKey : secretRing.iterator()) {
|
||||||
|
try {
|
||||||
|
boolean unlocked = clickSecretKey.unlock(passphrase);
|
||||||
|
if (unlocked) {
|
||||||
|
unlockedSecretKey = clickSecretKey;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (PgpGeneralException e) {
|
||||||
|
Toast.makeText(activity, R.string.error_could_not_extract_private_key,
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
sendMessageToHandler(MESSAGE_CANCEL);
|
||||||
|
return; // ran out of keys to try
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Means we got an exception every time
|
||||||
|
if (unlockedSecretKey == null) {
|
||||||
|
Toast.makeText(activity, R.string.wrong_passphrase,
|
||||||
|
Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
sendMessageToHandler(MESSAGE_CANCEL);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long masterKeyId = secretRing.getMasterKeyId();
|
||||||
|
|
||||||
// cache the new passphrase
|
// cache the new passphrase
|
||||||
Log.d(Constants.TAG, "Everything okay! Caching entered passphrase");
|
Log.d(Constants.TAG, "Everything okay! Caching entered passphrase");
|
||||||
PassphraseCacheService.addCachedPassphrase(activity, keyId, passphrase);
|
PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase);
|
||||||
if (!keyOK && clickSecretKey.getKeyID() != keyId) {
|
if (unlockedSecretKey.getKeyId() != masterKeyId) {
|
||||||
PassphraseCacheService.addCachedPassphrase(activity, clickSecretKey.getKeyID(),
|
PassphraseCacheService.addCachedPassphrase(
|
||||||
passphrase);
|
activity, unlockedSecretKey.getKeyId(), passphrase);
|
||||||
}
|
}
|
||||||
|
|
||||||
// also return passphrase back to activity
|
// also return passphrase back to activity
|
||||||
Bundle data = new Bundle();
|
Bundle data = new Bundle();
|
||||||
data.putString(MESSAGE_DATA_PASSPHRASE, passphrase);
|
data.putString(MESSAGE_DATA_PASSPHRASE, passphrase);
|
||||||
|
|
||||||
sendMessageToHandler(MESSAGE_OKAY, data);
|
sendMessageToHandler(MESSAGE_OKAY, data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user