From fe3db8f0e68b7490bf98e356474a631dec32530c Mon Sep 17 00:00:00 2001 From: Ashley Hughes Date: Tue, 4 Feb 2014 01:18:54 +0000 Subject: [PATCH] preserve keys that we don\'t use in the app --- .../keychain/pgp/PgpKeyHelper.java | 27 ++++++++++++++++--- .../keychain/ui/widget/KeyEditor.java | 18 ++++++------- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java index bef41ce64..78d42cbf9 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java @@ -209,9 +209,8 @@ public class PgpKeyHelper { Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(creationDate); calendar.add(Calendar.DATE, key.getValidDays()); - Date expiryDate = calendar.getTime(); - return expiryDate; + return calendar.getTime(); } public static Date getExpiryDate(PGPSecretKey key) { @@ -291,6 +290,28 @@ public class PgpKeyHelper { return userId; } + public static int getKeyUsage(PGPSecretKey key) + { + return getKeyUsage(key.getPublicKey()); + } + + @SuppressWarnings("unchecked") + private static int getKeyUsage(PGPPublicKey key) { + int usage = 0; + if (key.getVersion() >= 4) { + for (PGPSignature sig : new IterableIterator(key.getSignatures())) { + if (key.isMasterKey() && sig.getKeyID() != key.getKeyID()) continue; + + PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); + if (hashed != null) usage |= hashed.getKeyFlags(); + + PGPSignatureSubpacketVector unhashed = sig.getUnhashedSubPackets(); + if (unhashed != null) usage |= unhashed.getKeyFlags(); + } + } + return usage; + } + @SuppressWarnings("unchecked") public static boolean isEncryptionKey(PGPPublicKey key) { if (!key.isEncryptionKey()) { @@ -440,7 +461,7 @@ public class PgpKeyHelper { } public static String getAlgorithmInfo(int algorithm, int keySize) { - String algorithmStr = null; + String algorithmStr; switch (algorithm) { case PGPPublicKey.RSA_ENCRYPT: diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java index 2ea044e66..8443895e8 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/widget/KeyEditor.java @@ -66,6 +66,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { CheckBox mChkSign; CheckBox mChkEncrypt; CheckBox mChkAuthenticate; + int mUsage; private int mDatePickerResultCount = 0; private DatePickerDialog.OnDateSetListener mExpiryDateSetListener = new DatePickerDialog.OnDateSetListener() { @@ -189,6 +190,7 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { mChkSign.setChecked(PgpKeyHelper.isSigningKey(key)); mChkEncrypt.setChecked(PgpKeyHelper.isEncryptionKey(key)); mChkAuthenticate.setChecked(PgpKeyHelper.isAuthenticationKey(key)); + mUsage = PgpKeyHelper.getKeyUsage(key); // TODO: use usage argument? GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")); @@ -237,17 +239,13 @@ public class KeyEditor extends LinearLayout implements Editor, OnClickListener { } public int getUsage() { - int result = 0; // TODO: preserve other flags - if (mChkCertify.isChecked()) - result |= KeyFlags.CERTIFY_OTHER; - if (mChkSign.isChecked()) //TODO: fix what happens when we remove sign flag from master - should still be able to certify - result |= KeyFlags.SIGN_DATA; - if (mChkEncrypt.isChecked()) - result |= KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE; - if (mChkAuthenticate.isChecked()) - result |= KeyFlags.AUTHENTICATION; + mUsage = (mUsage & ~KeyFlags.CERTIFY_OTHER) | (mChkCertify.isChecked() ? KeyFlags.CERTIFY_OTHER : 0); + mUsage = (mUsage & ~KeyFlags.SIGN_DATA) | (mChkSign.isChecked() ? KeyFlags.SIGN_DATA : 0); + mUsage = (mUsage & ~KeyFlags.ENCRYPT_COMMS) | (mChkEncrypt.isChecked() ? KeyFlags.ENCRYPT_COMMS : 0); + mUsage = (mUsage & ~KeyFlags.ENCRYPT_STORAGE) | (mChkEncrypt.isChecked() ? KeyFlags.ENCRYPT_STORAGE : 0); + mUsage = (mUsage & ~KeyFlags.AUTHENTICATION) | (mChkAuthenticate.isChecked() ? KeyFlags.AUTHENTICATION : 0); - return result; + return mUsage; } }