Merge branch 'master' of github.com:open-keychain/open-keychain
Conflicts: OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptActivity.java
This commit is contained in:
@@ -78,7 +78,7 @@ public abstract class CanonicalizedKeyRing extends KeyRing {
|
||||
|
||||
public long getEncryptId() throws PgpGeneralException {
|
||||
for(CanonicalizedPublicKey key : publicKeyIterator()) {
|
||||
if(key.canEncrypt()) {
|
||||
if (key.canEncrypt() && key.isValid()) {
|
||||
return key.getKeyId();
|
||||
}
|
||||
}
|
||||
@@ -94,24 +94,6 @@ public abstract class CanonicalizedKeyRing extends KeyRing {
|
||||
}
|
||||
}
|
||||
|
||||
public long getSignId() throws PgpGeneralException {
|
||||
for(CanonicalizedPublicKey key : publicKeyIterator()) {
|
||||
if(key.canSign()) {
|
||||
return key.getKeyId();
|
||||
}
|
||||
}
|
||||
throw new PgpGeneralException("No valid signing key found!");
|
||||
}
|
||||
|
||||
public boolean hasSign() throws PgpGeneralException {
|
||||
try {
|
||||
getSignId();
|
||||
return true;
|
||||
} catch (PgpGeneralException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void encode(OutputStream stream) throws IOException {
|
||||
getRing().encode(stream);
|
||||
}
|
||||
|
||||
@@ -104,4 +104,10 @@ public class CanonicalizedPublicKey extends UncachedPublicKey {
|
||||
public Integer getKeyUsage() {
|
||||
return super.getKeyUsage();
|
||||
}
|
||||
|
||||
/** Returns whether this key is valid, ie not expired or revoked. */
|
||||
public boolean isValid() {
|
||||
return !isRevoked() && !isExpired();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor;
|
||||
import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder;
|
||||
import org.sufficientlysecure.keychain.Constants;
|
||||
import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException;
|
||||
import org.sufficientlysecure.keychain.provider.KeychainContract;
|
||||
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
||||
import org.sufficientlysecure.keychain.util.IterableIterator;
|
||||
import org.sufficientlysecure.keychain.util.Log;
|
||||
|
||||
@@ -74,43 +76,18 @@ public class CanonicalizedSecretKeyRing extends CanonicalizedKeyRing {
|
||||
return new CanonicalizedSecretKey(this, mRing.getSecretKey(id));
|
||||
}
|
||||
|
||||
/** Getter that returns the subkey that should be used for signing. */
|
||||
CanonicalizedSecretKey getSigningSubKey() throws PgpGeneralException {
|
||||
PGPSecretKey key = mRing.getSecretKey(getSignId());
|
||||
if(key != null) {
|
||||
CanonicalizedSecretKey cKey = new CanonicalizedSecretKey(this, key);
|
||||
if(!cKey.canSign()) {
|
||||
throw new PgpGeneralException("key error");
|
||||
}
|
||||
return cKey;
|
||||
}
|
||||
// TODO handle with proper exception
|
||||
throw new PgpGeneralException("no signing key available");
|
||||
}
|
||||
|
||||
public boolean hasPassphrase() {
|
||||
PGPSecretKey secretKey = null;
|
||||
boolean foundValidKey = false;
|
||||
for (Iterator keys = mRing.getSecretKeys(); keys.hasNext(); ) {
|
||||
secretKey = (PGPSecretKey) keys.next();
|
||||
if (!secretKey.isPrivateKeyEmpty()) {
|
||||
foundValidKey = true;
|
||||
break;
|
||||
/** Returns the key id which should be used for signing.
|
||||
*
|
||||
* This method returns keys which are actually available (ie. secret available, and not stripped,
|
||||
* revoked, or expired), hence only works on keyrings where a secret key is available!
|
||||
*/
|
||||
public long getSecretSignId() throws PgpGeneralException {
|
||||
for(CanonicalizedSecretKey key : secretKeyIterator()) {
|
||||
if (key.canSign() && key.isValid() && key.getSecretKeyType().isUsable()) {
|
||||
return key.getKeyId();
|
||||
}
|
||||
}
|
||||
if(!foundValidKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder()
|
||||
.setProvider("SC").build("".toCharArray());
|
||||
PGPPrivateKey testKey = secretKey.extractPrivateKey(keyDecryptor);
|
||||
return testKey == null;
|
||||
} catch(PGPException e) {
|
||||
// this means the crc check failed -> passphrase required
|
||||
return true;
|
||||
}
|
||||
throw new PgpGeneralException("no valid signing key available");
|
||||
}
|
||||
|
||||
public IterableIterator<CanonicalizedSecretKey> secretKeyIterator() {
|
||||
|
||||
@@ -56,10 +56,6 @@ public abstract class KeyRing {
|
||||
|
||||
abstract public boolean hasEncrypt() throws PgpGeneralException;
|
||||
|
||||
abstract public long getSignId() throws PgpGeneralException;
|
||||
|
||||
abstract public boolean hasSign() throws PgpGeneralException;
|
||||
|
||||
abstract public int getVerified() throws PgpGeneralException;
|
||||
|
||||
private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
|
||||
|
||||
@@ -51,12 +51,9 @@ public class UncachedPublicKey {
|
||||
|
||||
/** The revocation signature is NOT checked here, so this may be false! */
|
||||
public boolean isRevoked() {
|
||||
for (PGPSignature sig : new IterableIterator<PGPSignature>(
|
||||
mPublicKey.getSignaturesOfType(isMasterKey() ? PGPSignature.KEY_REVOCATION
|
||||
: PGPSignature.SUBKEY_REVOCATION))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return mPublicKey.getSignaturesOfType(isMasterKey()
|
||||
? PGPSignature.KEY_REVOCATION
|
||||
: PGPSignature.SUBKEY_REVOCATION).hasNext();
|
||||
}
|
||||
|
||||
public Date getCreationTime() {
|
||||
|
||||
@@ -135,65 +135,33 @@ public class CachedPublicKeyRing extends KeyRing {
|
||||
|
||||
@Override
|
||||
public long getEncryptId() throws PgpGeneralException {
|
||||
try {
|
||||
Cursor subkeys = getSubkeys();
|
||||
if (subkeys != null) {
|
||||
try {
|
||||
while (subkeys.moveToNext()) {
|
||||
if (subkeys.getInt(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.CAN_ENCRYPT)) != 0) {
|
||||
return subkeys.getLong(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.KEY_ID));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
subkeys.close();
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
throw new PgpGeneralException(e);
|
||||
}
|
||||
throw new PgpGeneralException("No encrypt key found");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasEncrypt() throws PgpGeneralException {
|
||||
try {
|
||||
Object data = mProviderHelper.getGenericData(mUri,
|
||||
KeychainContract.KeyRings.HAS_ENCRYPT,
|
||||
KeyRings.HAS_ENCRYPT,
|
||||
ProviderHelper.FIELD_TYPE_INTEGER);
|
||||
return (Long) data > 0;
|
||||
return (Long) data;
|
||||
} catch(ProviderHelper.NotFoundException e) {
|
||||
throw new PgpGeneralException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getSignId() throws PgpGeneralException {
|
||||
try {
|
||||
Cursor subkeys = getSubkeys();
|
||||
if (subkeys != null) {
|
||||
try {
|
||||
while (subkeys.moveToNext()) {
|
||||
if (subkeys.getInt(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.CAN_SIGN)) != 0) {
|
||||
return subkeys.getLong(subkeys.getColumnIndexOrThrow(KeychainContract.Keys.KEY_ID));
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
subkeys.close();
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
throw new PgpGeneralException(e);
|
||||
}
|
||||
throw new PgpGeneralException("No sign key found");
|
||||
public boolean hasEncrypt() throws PgpGeneralException {
|
||||
return getEncryptId() != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasSign() throws PgpGeneralException {
|
||||
/** Returns the key id which should be used for signing.
|
||||
*
|
||||
* This method returns keys which are actually available (ie. secret available, and not stripped,
|
||||
* revoked, or expired), hence only works on keyrings where a secret key is available!
|
||||
*
|
||||
*/
|
||||
public long getSecretSignId() throws PgpGeneralException {
|
||||
try {
|
||||
Object data = mProviderHelper.getGenericData(mUri,
|
||||
KeychainContract.KeyRings.HAS_SIGN,
|
||||
KeyRings.HAS_SIGN,
|
||||
ProviderHelper.FIELD_TYPE_INTEGER);
|
||||
return (Long) data > 0;
|
||||
return (Long) data;
|
||||
} catch(ProviderHelper.NotFoundException e) {
|
||||
throw new PgpGeneralException(e);
|
||||
}
|
||||
|
||||
@@ -293,7 +293,7 @@ public class OpenPgpService extends RemoteService {
|
||||
// Find the appropriate subkey to sign with
|
||||
CachedPublicKeyRing signingRing =
|
||||
new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId());
|
||||
final long sigSubKeyId = signingRing.getSignId();
|
||||
final long sigSubKeyId = signingRing.getSecretSignId();
|
||||
|
||||
// sign-only
|
||||
PgpSignEncrypt.Builder builder = new PgpSignEncrypt.Builder(
|
||||
@@ -405,7 +405,7 @@ public class OpenPgpService extends RemoteService {
|
||||
// Find the appropriate subkey to sign with
|
||||
CachedPublicKeyRing signingRing =
|
||||
new ProviderHelper(this).getCachedPublicKeyRing(accSettings.getKeyId());
|
||||
final long sigSubKeyId = signingRing.getSignId();
|
||||
final long sigSubKeyId = signingRing.getSecretSignId();
|
||||
|
||||
String passphrase;
|
||||
if (data.hasExtra(OpenPgpApi.EXTRA_PASSPHRASE)) {
|
||||
|
||||
@@ -294,7 +294,7 @@ public class KeychainIntentService extends IntentService implements Progressable
|
||||
// Find the appropriate subkey to sign with
|
||||
CachedPublicKeyRing signingRing =
|
||||
new ProviderHelper(this).getCachedPublicKeyRing(sigMasterKeyId);
|
||||
long sigSubKeyId = signingRing.getSignId();
|
||||
long sigSubKeyId = signingRing.getSecretSignId();
|
||||
|
||||
// Set signature settings
|
||||
builder.setSignatureMasterKeyId(sigMasterKeyId)
|
||||
|
||||
@@ -166,6 +166,9 @@ public class CreateKeyInputFragment extends Fragment {
|
||||
}
|
||||
|
||||
private void hideKeyboard() {
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager inputManager = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
|
||||
|
||||
@@ -76,9 +76,15 @@ public class DecryptActivity extends DrawerActivity {
|
||||
if (Build.VERSION.SDK_INT >= VERSION_CODES.ICE_CREAM_SANDWICH) {
|
||||
|
||||
// get text from clipboard
|
||||
final CharSequence clipboardText = ClipboardReflection.getClipboardText(DecryptActivity.this);
|
||||
final CharSequence clipboardText =
|
||||
ClipboardReflection.getClipboardText(DecryptActivity.this);
|
||||
|
||||
AsyncTask<String, Void, Boolean> tadaTask = new AsyncTask<String, Void, Boolean>() {
|
||||
// if it's null, nothing to do here /o/
|
||||
if (clipboardText == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
new AsyncTask<String, Void, Boolean>() {
|
||||
@Override
|
||||
protected Boolean doInBackground(String... clipboardText) {
|
||||
|
||||
@@ -103,11 +109,7 @@ public class DecryptActivity extends DrawerActivity {
|
||||
SubtleAttentionSeeker.tada(findViewById(R.id.clipboard_icon), 1.5f).start();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (clipboardText != null) {
|
||||
tadaTask.execute(clipboardText.toString());
|
||||
}
|
||||
}.execute(clipboardText.toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -159,6 +159,9 @@ public class ImportKeysCloudFragment extends Fragment {
|
||||
}
|
||||
|
||||
private void hideKeyboard() {
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager inputManager = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
|
||||
|
||||
@@ -157,8 +157,8 @@ public class PassphraseDialogActivity extends FragmentActivity {
|
||||
|
||||
/* Get key type for message */
|
||||
// find a master key id for our key
|
||||
long masterKeyId = new ProviderHelper(getActivity()).getMasterKeyId(mSubKeyId);
|
||||
CachedPublicKeyRing keyRing = new ProviderHelper(getActivity()).getCachedPublicKeyRing(masterKeyId);
|
||||
long masterKeyId = new ProviderHelper(activity).getMasterKeyId(mSubKeyId);
|
||||
CachedPublicKeyRing keyRing = new ProviderHelper(activity).getCachedPublicKeyRing(masterKeyId);
|
||||
// get the type of key (from the database)
|
||||
CanonicalizedSecretKey.SecretKeyType keyType = keyRing.getSecretKeyType(mSubKeyId);
|
||||
switch (keyType) {
|
||||
@@ -324,6 +324,11 @@ public class PassphraseDialogActivity extends FragmentActivity {
|
||||
}
|
||||
|
||||
private void finishCaching(String passphrase) {
|
||||
// any indication this isn't needed anymore, don't do it.
|
||||
if (mIsCancelled || getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (mServiceIntent != null) {
|
||||
// TODO: Not routing passphrase through OpenPGP API currently
|
||||
// due to security concerns...
|
||||
@@ -352,6 +357,10 @@ public class PassphraseDialogActivity extends FragmentActivity {
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
super.onDismiss(dialog);
|
||||
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
hideKeyboard();
|
||||
|
||||
getActivity().setResult(RESULT_CANCELED);
|
||||
|
||||
@@ -206,6 +206,9 @@ public class AddUserIdDialogFragment extends DialogFragment implements OnEditorA
|
||||
}
|
||||
|
||||
private void hideKeyboard() {
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager inputManager = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
|
||||
|
||||
@@ -209,6 +209,10 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
mPassphraseEditText.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// The activity might already be gone! Nvm in that case.
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager imm = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.showSoftInput(mPassphraseEditText, InputMethodManager.SHOW_IMPLICIT);
|
||||
@@ -342,13 +346,18 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor
|
||||
}
|
||||
|
||||
private void hideKeyboard() {
|
||||
// The activity which called the dialog might no longer exist. Nvm in that case...
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager inputManager = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
|
||||
//check if no view has focus:
|
||||
View v = getActivity().getCurrentFocus();
|
||||
if (v == null)
|
||||
if (v == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
|
||||
}
|
||||
|
||||
@@ -200,6 +200,9 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi
|
||||
}
|
||||
|
||||
private void hideKeyboard() {
|
||||
if (getActivity() == null) {
|
||||
return;
|
||||
}
|
||||
InputMethodManager inputManager = (InputMethodManager) getActivity()
|
||||
.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user