aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java
diff options
context:
space:
mode:
authorVincent Breitmoser <valodim@mugenguild.com>2014-09-02 01:24:16 +0200
committerVincent Breitmoser <valodim@mugenguild.com>2014-09-02 01:24:16 +0200
commite0905a3afbba7f96822becb378b7e8fd9c51e85a (patch)
tree72f463174e322bea66c7702c236739a196b6514d /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java
parenta97ebc1ec9afa4951f676b4483c6db871c4704ab (diff)
downloadopen-keychain-e0905a3afbba7f96822becb378b7e8fd9c51e85a.tar.gz
open-keychain-e0905a3afbba7f96822becb378b7e8fd9c51e85a.tar.bz2
open-keychain-e0905a3afbba7f96822becb378b7e8fd9c51e85a.zip
cache key s2k type in database, for later use
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java53
1 files changed, 53 insertions, 0 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java
index 51770f930..833e1ad3d 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java
@@ -80,6 +80,59 @@ public class CanonicalizedSecretKey extends CanonicalizedPublicKey {
return (CanonicalizedSecretKeyRing) mRing;
}
+ public enum SecretKeyType {
+ UNAVAILABLE(0), GNU_DUMMY (1), PASSPHRASE (2), PASSPHRASE_EMPTY (3), DIVERT_TO_CARD (4);
+
+ final int mNum;
+ SecretKeyType(int num) {
+ mNum = num;
+ }
+
+ public static SecretKeyType fromNum(int num) {
+ switch (num) {
+ case 1: return GNU_DUMMY;
+ case 2: return PASSPHRASE;
+ case 3: return PASSPHRASE_EMPTY;
+ case 4: return DIVERT_TO_CARD;
+ // if this case happens, it's probably a check from a database value
+ default: return UNAVAILABLE;
+ }
+ }
+
+ public int getNum() {
+ return mNum;
+ }
+
+ public boolean isUsable() {
+ return this != UNAVAILABLE && this != GNU_DUMMY;
+ }
+
+ }
+
+ public SecretKeyType getSecretKeyType() {
+ if (mSecretKey.getS2K().getType() == S2K.GNU_DUMMY_S2K) {
+ // divert to card is special
+ if (mSecretKey.getS2K().getProtectionMode() == S2K.GNU_PROTECTION_MODE_DIVERT_TO_CARD) {
+ return SecretKeyType.DIVERT_TO_CARD;
+ }
+ // no matter the exact protection mode, it's some kind of dummy key
+ return SecretKeyType.GNU_DUMMY;
+ }
+
+ try {
+ PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider(
+ Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray());
+ // If this doesn't throw
+ mSecretKey.extractPrivateKey(keyDecryptor);
+ // It means the passphrase is empty
+ return SecretKeyType.PASSPHRASE_EMPTY;
+ } catch (PGPException e) {
+ // Otherwise, it's just a regular ol' passphrase
+ return SecretKeyType.PASSPHRASE;
+ }
+
+ }
+
/**
* Returns true on right passphrase
*/