fix EditKeyActivity

This commit is contained in:
Vincent Breitmoser
2014-04-11 01:48:08 +02:00
parent 0f06b8a1d6
commit 9af532880c
2 changed files with 46 additions and 50 deletions

View File

@@ -200,6 +200,26 @@ public class PassphraseCacheService extends Service {
return cachedPassphrase; return cachedPassphrase;
} }
public static boolean hasPassphrase(PGPSecretKeyRing secretKeyRing) throws PGPException {
PGPSecretKey secretKey = null;
boolean foundValidKey = false;
for (Iterator keys = secretKeyRing.getSecretKeys(); keys.hasNext(); ) {
secretKey = (PGPSecretKey) keys.next();
if (!secretKey.isPrivateKeyEmpty()) {
foundValidKey = true;
break;
}
}
if(!foundValidKey) {
return false;
}
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
.setProvider("SC").build("".toCharArray());
PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
return testKey == null;
}
/** /**
* Checks if key has a passphrase. * Checks if key has a passphrase.
* *
@@ -210,25 +230,7 @@ public class PassphraseCacheService extends Service {
// check if the key has no passphrase // check if the key has no passphrase
try { try {
PGPSecretKeyRing secRing = ProviderHelper.getPGPSecretKeyRing(context, secretKeyId); PGPSecretKeyRing secRing = ProviderHelper.getPGPSecretKeyRing(context, secretKeyId);
PGPSecretKey secretKey = null; return hasPassphrase(secRing);
boolean foundValidKey = false;
for (Iterator keys = secRing.getSecretKeys(); keys.hasNext(); ) {
secretKey = (PGPSecretKey) keys.next();
if (!secretKey.isPrivateKeyEmpty()) {
foundValidKey = true;
break;
}
}
if (!foundValidKey) {
return false;
}
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider(
"SC").build("".toCharArray());
PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
if (testKey != null) {
return false;
}
} catch (PGPException e) { } catch (PGPException e) {
// silently catch // silently catch
} catch (ProviderHelper.NotFoundException e) { } catch (ProviderHelper.NotFoundException e) {

View File

@@ -39,10 +39,12 @@ import android.widget.CheckBox;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.Toast;
import com.beardedhen.androidbootstrap.BootstrapButton; import com.beardedhen.androidbootstrap.BootstrapButton;
import com.devspark.appmsg.AppMsg; import com.devspark.appmsg.AppMsg;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPSecretKey; import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants;
@@ -287,34 +289,16 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
Log.d(Constants.TAG, "uri: " + mDataUri); Log.d(Constants.TAG, "uri: " + mDataUri);
try { try {
// get master key id using row id Uri secretUri = KeychainContract.KeyRingData.buildSecretKeyRingUri(mDataUri);
long masterKeyId = ProviderHelper.getMasterKeyId(this, mDataUri); mKeyRing = (PGPSecretKeyRing) ProviderHelper.getPGPKeyRing(this, secretUri);
finallyEdit(masterKeyId);
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "key not found!", e);
}
}
}
@SuppressWarnings("unchecked") PGPSecretKey masterKey = mKeyRing.getSecretKey();
private void finallyEdit(final long masterKeyId) {
if (masterKeyId != 0) {
PGPSecretKey masterKey = null;
try {
mKeyRing = ProviderHelper.getPGPSecretKeyRing(this, masterKeyId);
masterKey = mKeyRing.getSecretKey();
mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey()); mMasterCanSign = PgpKeyHelper.isCertificationKey(mKeyRing.getSecretKey());
for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) { for (PGPSecretKey key : new IterableIterator<PGPSecretKey>(mKeyRing.getSecretKeys())) {
mKeys.add(key); mKeys.add(key);
mKeysUsages.add(-1); // get usage when view is created mKeysUsages.add(-1); // get usage when view is created
} }
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "Keyring not found with masterKeyId: " + masterKeyId);
AppMsg.makeText(this, R.string.error_no_secret_key_found, AppMsg.STYLE_ALERT).show();
// TODO
}
if (masterKey != null) {
boolean isSet = false; boolean isSet = false;
for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) { for (String userId : new IterableIterator<String>(masterKey.getUserIDs())) {
Log.d(Constants.TAG, "Added userId " + userId); Log.d(Constants.TAG, "Added userId " + userId);
@@ -327,17 +311,27 @@ public class EditKeyActivity extends ActionBarActivity implements EditorListener
} }
mUserIds.add(userId); mUserIds.add(userId);
} }
buildLayout(false);
mCurrentPassphrase = "";
mIsPassphraseSet = PassphraseCacheService.hasPassphrase(mKeyRing);
if (!mIsPassphraseSet) {
// check "no passphrase" checkbox and remove button
mNoPassphrase.setChecked(true);
mChangePassphrase.setVisibility(View.GONE);
}
} catch (ProviderHelper.NotFoundException e) {
Log.e(Constants.TAG, "Keyring not found: " + e.getMessage(), e);
Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_SHORT).show();
finish();
} catch (PGPException e) {
Log.e(Constants.TAG, "Error extracting key: " + e.getMessage(), e);
Toast.makeText(this, R.string.error_no_secret_key_found, Toast.LENGTH_LONG).show();
finish();
} }
}
mCurrentPassphrase = "";
buildLayout(false);
mIsPassphraseSet = PassphraseCacheService.hasPassphrase(this, masterKeyId);
if (!mIsPassphraseSet) {
// check "no passphrase" checkbox and remove button
mNoPassphrase.setChecked(true);
mChangePassphrase.setVisibility(View.GONE);
} }
} }