From 1c3d0a58eaaf7bb56d8b274f869d99b318e3daac Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 28 Sep 2014 03:34:25 +0200 Subject: Reject subkeys with no key flags and no primary key binding certificate Closes #899 --- .../keychain/pgp/UncachedKeyRing.java | 62 ++++++++++++---------- 1 file changed, 35 insertions(+), 27 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index 7bf16791d..99a0ef94e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -616,43 +616,51 @@ public class UncachedKeyRing { continue; } + boolean needsPrimaryBinding = false; + // if this certificate says it allows signing for the key if (zert.getHashedSubPackets() != null && zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { - int flags = ((KeyFlags) zert.getHashedSubPackets() .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); if ((flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { - boolean ok = false; - // it MUST have an embedded primary key binding signature - try { - PGPSignatureList list = zert.getUnhashedSubPackets().getEmbeddedSignatures(); - for (int i = 0; i < list.size(); i++) { - WrappedSignature subsig = new WrappedSignature(list.get(i)); - if (subsig.getSignatureType() == PGPSignature.PRIMARYKEY_BINDING) { - subsig.init(key); - if (subsig.verifySignature(masterKey, key)) { - ok = true; - } else { - log.add(LogType.MSG_KC_SUB_PRIMARY_BAD, indent); - badCerts += 1; - continue uids; - } + needsPrimaryBinding = true; + } + } else { + // If there are no key flags, we STILL require this because the key can sign! + needsPrimaryBinding = true; + } + + // If this key can sign, it MUST have a primary key binding certificate + if (needsPrimaryBinding) { + boolean ok = false; + if (zert.getUnhashedSubPackets() != null) try { + // Check all embedded signatures, if any of them fits + PGPSignatureList list = zert.getUnhashedSubPackets().getEmbeddedSignatures(); + for (int i = 0; i < list.size(); i++) { + WrappedSignature subsig = new WrappedSignature(list.get(i)); + if (subsig.getSignatureType() == PGPSignature.PRIMARYKEY_BINDING) { + subsig.init(key); + if (subsig.verifySignature(masterKey, key)) { + ok = true; + } else { + log.add(LogType.MSG_KC_SUB_PRIMARY_BAD, indent); + badCerts += 1; + continue uids; } } - } catch (Exception e) { - log.add(LogType.MSG_KC_SUB_PRIMARY_BAD_ERR, indent); - badCerts += 1; - continue; - } - // if it doesn't, get rid of this! - if (!ok) { - log.add(LogType.MSG_KC_SUB_PRIMARY_NONE, indent); - badCerts += 1; - continue; } + } catch (Exception e) { + log.add(LogType.MSG_KC_SUB_PRIMARY_BAD_ERR, indent); + badCerts += 1; + continue; + } + // if it doesn't, get rid of this! + if (!ok) { + log.add(LogType.MSG_KC_SUB_PRIMARY_NONE, indent); + badCerts += 1; + continue; } - } // if we already have a cert, and this one is older: skip it -- cgit v1.2.3 From c0abae5cc366eb4fc0ed89cbf44e4d2ab8aa7893 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 28 Sep 2014 03:53:14 +0200 Subject: only check for primary binding certificate if key algorithm even supports signing --- .../keychain/pgp/UncachedKeyRing.java | 24 ++++++++++++++-------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index 99a0ef94e..0e9377890 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -618,17 +618,23 @@ public class UncachedKeyRing { boolean needsPrimaryBinding = false; - // if this certificate says it allows signing for the key - if (zert.getHashedSubPackets() != null && - zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { - int flags = ((KeyFlags) zert.getHashedSubPackets() - .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); - if ((flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { + // If the algorithm is even suitable for signing + if (key.getAlgorithm() != PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT + && key.getAlgorithm() != PublicKeyAlgorithmTags.RSA_ENCRYPT) { + + // If this certificate says it allows signing for the key + if (zert.getHashedSubPackets() != null && + zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { + int flags = ((KeyFlags) zert.getHashedSubPackets() + .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); + if ((flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { + needsPrimaryBinding = true; + } + } else { + // If there are no key flags, we STILL require this because the key can sign! needsPrimaryBinding = true; } - } else { - // If there are no key flags, we STILL require this because the key can sign! - needsPrimaryBinding = true; + } // If this key can sign, it MUST have a primary key binding certificate -- cgit v1.2.3 From 706e60474d15f833ad5ead519518775812978bac Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 28 Sep 2014 14:25:54 +0200 Subject: be more mindful of algorithm and usage flag interaction Fixes #895 --- .../keychain/pgp/UncachedKeyRing.java | 40 ++++++++++++++++++++-- .../keychain/pgp/UncachedPublicKey.java | 16 ++------- .../keychain/service/results/OperationResult.java | 2 ++ OpenKeychain/src/main/res/values/strings.xml | 2 ++ 4 files changed, 44 insertions(+), 16 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index 0e9377890..b4842b0a5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -619,8 +619,7 @@ public class UncachedKeyRing { boolean needsPrimaryBinding = false; // If the algorithm is even suitable for signing - if (key.getAlgorithm() != PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT - && key.getAlgorithm() != PublicKeyAlgorithmTags.RSA_ENCRYPT) { + if (isSigningAlgo(key.getAlgorithm())) { // If this certificate says it allows signing for the key if (zert.getHashedSubPackets() != null && @@ -722,6 +721,24 @@ public class UncachedKeyRing { continue; } + // If we have flags, check if the algorithm supports all of them + if (selfCert.getHashedSubPackets() == null + && selfCert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { + int flags = ((KeyFlags) selfCert.getHashedSubPackets().getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); + int algo = key.getAlgorithm(); + // If this is a signing key, but not a signing algorithm, warn the user + if (!isSigningAlgo(algo) && (flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { + log.add(LogType.MSG_KC_SUB_ALGO_BAD_SIGN, indent); + } + // If this is an encryption key, but not an encryption algorithm, warn the user + if (!isEncryptionAlgo(algo) && ( + (flags & PGPKeyFlags.CAN_ENCRYPT_COMMS) == PGPKeyFlags.CAN_ENCRYPT_COMMS + || (flags & PGPKeyFlags.CAN_ENCRYPT_STORAGE) == PGPKeyFlags.CAN_ENCRYPT_STORAGE + )) { + log.add(LogType.MSG_KC_SUB_ALGO_BAD_ENCRYPT, indent); + } + } + // re-add certification modified = PGPPublicKey.addCertification(modified, selfCert); // add revocation, if any @@ -953,4 +970,23 @@ public class UncachedKeyRing { } } + + /** Returns true if the algorithm is of a type which is suitable for signing. */ + static boolean isSigningAlgo(int algorithm) { + return algorithm == PGPPublicKey.RSA_GENERAL + || algorithm == PGPPublicKey.RSA_SIGN + || algorithm == PGPPublicKey.DSA + || algorithm == PGPPublicKey.ELGAMAL_GENERAL + || algorithm == PGPPublicKey.ECDSA; + } + + /** Returns true if the algorithm is of a type which is suitable for encryption. */ + static boolean isEncryptionAlgo(int algorithm) { + return algorithm == PGPPublicKey.RSA_GENERAL + || algorithm == PGPPublicKey.RSA_ENCRYPT + || algorithm == PGPPublicKey.ELGAMAL_ENCRYPT + || algorithm == PGPPublicKey.ELGAMAL_GENERAL + || algorithm == PGPPublicKey.ECDH; + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java index 7f08d121e..74af9fc97 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -18,9 +18,6 @@ package org.sufficientlysecure.keychain.pgp; -import org.spongycastle.asn1.ASN1ObjectIdentifier; -import org.spongycastle.asn1.nist.NISTNamedCurves; -import org.spongycastle.asn1.teletrust.TeleTrusTNamedCurves; import org.spongycastle.bcpg.ECPublicBCPGKey; import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.sig.KeyFlags; @@ -28,7 +25,6 @@ import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; -import org.spongycastle.util.Strings; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; @@ -264,12 +260,6 @@ public class UncachedPublicKey { return (getKeyUsage() & KeyFlags.CERTIFY_OTHER) != 0; } - if (mPublicKey.getAlgorithm() == PGPPublicKey.RSA_GENERAL - || mPublicKey.getAlgorithm() == PGPPublicKey.RSA_SIGN - || mPublicKey.getAlgorithm() == PGPPublicKey.ECDSA) { - return true; - } - return false; } @@ -279,9 +269,7 @@ public class UncachedPublicKey { return (getKeyUsage() & KeyFlags.SIGN_DATA) != 0; } - if (mPublicKey.getAlgorithm() == PGPPublicKey.RSA_GENERAL - || mPublicKey.getAlgorithm() == PGPPublicKey.RSA_SIGN - || mPublicKey.getAlgorithm() == PGPPublicKey.ECDSA) { + if (UncachedKeyRing.isSigningAlgo(mPublicKey.getAlgorithm())) { return true; } @@ -295,7 +283,7 @@ public class UncachedPublicKey { } // RSA_GENERAL, RSA_ENCRYPT, ELGAMAL_ENCRYPT, ELGAMAL_GENERAL, ECDH - if (mPublicKey.isEncryptionKey()) { + if (UncachedKeyRing.isEncryptionAlgo(mPublicKey.getAlgorithm())) { return true; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java index b0a255162..0a4d9649f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/results/OperationResult.java @@ -341,6 +341,8 @@ public abstract class OperationResult implements Parcelable { MSG_KC_SUB_REVOKE_BAD (LogLevel.WARN, R.string.msg_kc_sub_revoke_bad), MSG_KC_SUB_REVOKE_DUP (LogLevel.DEBUG, R.string.msg_kc_sub_revoke_dup), MSG_KC_SUB_UNKNOWN_ALGO (LogLevel.WARN, R.string.msg_kc_sub_unknown_algo), + MSG_KC_SUB_ALGO_BAD_ENCRYPT (LogLevel.WARN, R.string.msg_kc_sub_algo_bad_encrpyt), + MSG_KC_SUB_ALGO_BAD_SIGN (LogLevel.WARN, R.string.msg_kc_sub_algo_bad_sign), MSG_KC_SUCCESS_BAD (LogLevel.OK, R.plurals.msg_kc_success_bad), MSG_KC_SUCCESS_BAD_AND_RED (LogLevel.OK, R.string.msg_kc_success_bad_and_red), MSG_KC_SUCCESS_REDUNDANT (LogLevel.OK, R.plurals.msg_kc_success_redundant), diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 14d0eba9b..f9ae6f029 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -684,6 +684,8 @@ "Removing bad subkey revocation certificate" "Removing redundant subkey revocation certificate" "Subkey uses an unknown algorithm, not importing…" + "Subkey has encryption usage flag, but algorithm is not suitable for encryption." + "Subkey has signing usage flag, but algorithm is not suitable for signing." "Keyring canonicalization successful, no changes" "Keyring canonicalization successful, removed one erroneous certificate" -- cgit v1.2.3 From 8131daa6380ed752c4d31cd6a40650d9ac5b9817 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 28 Sep 2014 14:40:49 +0200 Subject: move can.*() methods to CanonicalizedPublicKey, where they belong --- .../keychain/pgp/CanonicalizedPublicKey.java | 83 ++++++++++++++++++++-- .../keychain/pgp/UncachedKeyRing.java | 9 ++- .../keychain/pgp/UncachedPublicKey.java | 72 ------------------- 3 files changed, 83 insertions(+), 81 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java index 7a63a7a42..d0808d446 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java @@ -18,7 +18,11 @@ package org.sufficientlysecure.keychain.pgp; +import org.spongycastle.bcpg.SignatureSubpacketTags; +import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator; import org.sufficientlysecure.keychain.util.IterableIterator; @@ -36,6 +40,7 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { // this is the parent key ring final KeyRing mRing; + private Integer mCacheUsage = null; CanonicalizedPublicKey(KeyRing ring, PGPPublicKey key) { super(key); @@ -46,12 +51,82 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { return new IterableIterator(mPublicKey.getUserIDs()); } - public KeyRing getKeyRing() { - return mRing; - } - JcePublicKeyKeyEncryptionMethodGenerator getPubKeyEncryptionGenerator() { return new JcePublicKeyKeyEncryptionMethodGenerator(mPublicKey); } + public boolean canSign() { + // if key flags subpacket is available, honor it! + if (getKeyUsage() != null) { + return (getKeyUsage() & KeyFlags.SIGN_DATA) != 0; + } + + if (UncachedKeyRing.isSigningAlgo(mPublicKey.getAlgorithm())) { + return true; + } + + return false; + } + + /** + * Get all key usage flags. + * If at least one key flag subpacket is present return these. + * If no subpacket is present it returns null. + */ + @SuppressWarnings("unchecked") + public Integer getKeyUsage() { + if (mCacheUsage == null) { + for (PGPSignature sig : new IterableIterator(mPublicKey.getSignatures())) { + if (mPublicKey.isMasterKey() && sig.getKeyID() != mPublicKey.getKeyID()) { + continue; + } + + PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); + if (hashed != null && hashed.getSubpacket(SignatureSubpacketTags.KEY_FLAGS) != null) { + // init if at least one key flag subpacket has been found + if (mCacheUsage == null) { + mCacheUsage = 0; + } + mCacheUsage |= hashed.getKeyFlags(); + } + } + } + return mCacheUsage; + } + + public boolean canCertify() { + // if key flags subpacket is available, honor it! + if (getKeyUsage() != null) { + return (getKeyUsage() & KeyFlags.CERTIFY_OTHER) != 0; + } + + if (UncachedKeyRing.isSigningAlgo(mPublicKey.getAlgorithm())) { + return true; + } + + return false; + } + + public boolean canEncrypt() { + // if key flags subpacket is available, honor it! + if (getKeyUsage() != null) { + return (getKeyUsage() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0; + } + + // RSA_GENERAL, RSA_ENCRYPT, ELGAMAL_ENCRYPT, ELGAMAL_GENERAL, ECDH + if (UncachedKeyRing.isEncryptionAlgo(mPublicKey.getAlgorithm())) { + return true; + } + + return false; + } + + public boolean canAuthenticate() { + // if key flags subpacket is available, honor it! + if (getKeyUsage() != null) { + return (getKeyUsage() & KeyFlags.AUTHENTICATION) != 0; + } + + return false; + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java index b4842b0a5..8651760c0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -22,7 +22,6 @@ import org.spongycastle.bcpg.ArmoredOutputStream; import org.spongycastle.bcpg.PublicKeyAlgorithmTags; import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.sig.KeyFlags; -import org.spongycastle.openpgp.PGPKeyFlags; import org.spongycastle.openpgp.PGPKeyRing; import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKey; @@ -626,7 +625,7 @@ public class UncachedKeyRing { zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { int flags = ((KeyFlags) zert.getHashedSubPackets() .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); - if ((flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { + if ((flags & KeyFlags.SIGN_DATA) == KeyFlags.SIGN_DATA) { needsPrimaryBinding = true; } } else { @@ -727,13 +726,13 @@ public class UncachedKeyRing { int flags = ((KeyFlags) selfCert.getHashedSubPackets().getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); int algo = key.getAlgorithm(); // If this is a signing key, but not a signing algorithm, warn the user - if (!isSigningAlgo(algo) && (flags & PGPKeyFlags.CAN_SIGN) == PGPKeyFlags.CAN_SIGN) { + if (!isSigningAlgo(algo) && (flags & KeyFlags.SIGN_DATA) == KeyFlags.SIGN_DATA) { log.add(LogType.MSG_KC_SUB_ALGO_BAD_SIGN, indent); } // If this is an encryption key, but not an encryption algorithm, warn the user if (!isEncryptionAlgo(algo) && ( - (flags & PGPKeyFlags.CAN_ENCRYPT_COMMS) == PGPKeyFlags.CAN_ENCRYPT_COMMS - || (flags & PGPKeyFlags.CAN_ENCRYPT_STORAGE) == PGPKeyFlags.CAN_ENCRYPT_STORAGE + (flags & KeyFlags.ENCRYPT_STORAGE) == KeyFlags.ENCRYPT_STORAGE + || (flags & KeyFlags.ENCRYPT_COMMS) == KeyFlags.ENCRYPT_COMMS )) { log.add(LogType.MSG_KC_SUB_ALGO_BAD_ENCRYPT, indent); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java index 74af9fc97..345c00579 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -39,7 +39,6 @@ import java.util.Iterator; public class UncachedPublicKey { protected final PGPPublicKey mPublicKey; - private Integer mCacheUsage = null; public UncachedPublicKey(PGPPublicKey key) { mPublicKey = key; @@ -228,77 +227,6 @@ public class UncachedPublicKey { return getAlgorithm() == PGPPublicKey.ECDH || getAlgorithm() == PGPPublicKey.ECDSA; } - /** - * Get all key usage flags. - * If at least one key flag subpacket is present return these. - * If no subpacket is present it returns null. - */ - @SuppressWarnings("unchecked") - public Integer getKeyUsage() { - if (mCacheUsage == null) { - for (PGPSignature sig : new IterableIterator(mPublicKey.getSignatures())) { - if (mPublicKey.isMasterKey() && sig.getKeyID() != mPublicKey.getKeyID()) { - continue; - } - - PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); - if (hashed != null && hashed.getSubpacket(SignatureSubpacketTags.KEY_FLAGS) != null) { - // init if at least one key flag subpacket has been found - if (mCacheUsage == null) { - mCacheUsage = 0; - } - mCacheUsage |= hashed.getKeyFlags(); - } - } - } - return mCacheUsage; - } - - public boolean canCertify() { - // if key flags subpacket is available, honor it! - if (getKeyUsage() != null) { - return (getKeyUsage() & KeyFlags.CERTIFY_OTHER) != 0; - } - - return false; - } - - public boolean canSign() { - // if key flags subpacket is available, honor it! - if (getKeyUsage() != null) { - return (getKeyUsage() & KeyFlags.SIGN_DATA) != 0; - } - - if (UncachedKeyRing.isSigningAlgo(mPublicKey.getAlgorithm())) { - return true; - } - - return false; - } - - public boolean canEncrypt() { - // if key flags subpacket is available, honor it! - if (getKeyUsage() != null) { - return (getKeyUsage() & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) != 0; - } - - // RSA_GENERAL, RSA_ENCRYPT, ELGAMAL_ENCRYPT, ELGAMAL_GENERAL, ECDH - if (UncachedKeyRing.isEncryptionAlgo(mPublicKey.getAlgorithm())) { - return true; - } - - return false; - } - - public boolean canAuthenticate() { - // if key flags subpacket is available, honor it! - if (getKeyUsage() != null) { - return (getKeyUsage() & KeyFlags.AUTHENTICATION) != 0; - } - - return false; - } - public byte[] getFingerprint() { return mPublicKey.getFingerprint(); } -- cgit v1.2.3 From c34a159cae783178b692022a584e1f937cc652ca Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 28 Sep 2014 15:17:09 +0200 Subject: fix method visibility for tests --- .../keychain/pgp/CanonicalizedPublicKey.java | 37 ++++------------------ .../keychain/pgp/UncachedPublicKey.java | 33 +++++++++++++++++-- 2 files changed, 37 insertions(+), 33 deletions(-) (limited to 'OpenKeychain/src') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java index d0808d446..8fb3402b2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java @@ -18,11 +18,8 @@ package org.sufficientlysecure.keychain.pgp; -import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPSignature; -import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator; import org.sufficientlysecure.keychain.util.IterableIterator; @@ -40,7 +37,6 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { // this is the parent key ring final KeyRing mRing; - private Integer mCacheUsage = null; CanonicalizedPublicKey(KeyRing ring, PGPPublicKey key) { super(key); @@ -52,7 +48,7 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { } JcePublicKeyKeyEncryptionMethodGenerator getPubKeyEncryptionGenerator() { - return new JcePublicKeyKeyEncryptionMethodGenerator(mPublicKey); + return new JcePublicKeyKeyEncryptionMethodGenerator(mPublicKey); } public boolean canSign() { @@ -68,32 +64,6 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { return false; } - /** - * Get all key usage flags. - * If at least one key flag subpacket is present return these. - * If no subpacket is present it returns null. - */ - @SuppressWarnings("unchecked") - public Integer getKeyUsage() { - if (mCacheUsage == null) { - for (PGPSignature sig : new IterableIterator(mPublicKey.getSignatures())) { - if (mPublicKey.isMasterKey() && sig.getKeyID() != mPublicKey.getKeyID()) { - continue; - } - - PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); - if (hashed != null && hashed.getSubpacket(SignatureSubpacketTags.KEY_FLAGS) != null) { - // init if at least one key flag subpacket has been found - if (mCacheUsage == null) { - mCacheUsage = 0; - } - mCacheUsage |= hashed.getKeyFlags(); - } - } - } - return mCacheUsage; - } - public boolean canCertify() { // if key flags subpacket is available, honor it! if (getKeyUsage() != null) { @@ -129,4 +99,9 @@ public class CanonicalizedPublicKey extends UncachedPublicKey { return false; } + + /** Same method as superclass, but we make it public. */ + public Integer getKeyUsage() { + return super.getKeyUsage(); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java index 345c00579..bb9c7d51c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -39,6 +39,7 @@ import java.util.Iterator; public class UncachedPublicKey { protected final PGPPublicKey mPublicKey; + private Integer mCacheUsage = null; public UncachedPublicKey(PGPPublicKey key) { mPublicKey = key; @@ -231,9 +232,8 @@ public class UncachedPublicKey { return mPublicKey.getFingerprint(); } - // TODO This method should have package visibility - no access outside the pgp package! // (It's still used in ProviderHelper at this point) - public PGPPublicKey getPublicKey() { + PGPPublicKey getPublicKey() { return mPublicKey; } @@ -271,4 +271,33 @@ public class UncachedPublicKey { } } + /** Get all key usage flags. + * If at least one key flag subpacket is present return these. If no + * subpacket is present it returns null. + * + * Note that this method has package visiblity because it is used in test + * cases. Certificates of UncachedPublicKey instances can NOT be assumed to + * be verified, so the result of this method should not be used in other + * places! + */ + @SuppressWarnings("unchecked") + Integer getKeyUsage() { + if (mCacheUsage == null) { + for (PGPSignature sig : new IterableIterator(mPublicKey.getSignatures())) { + if (mPublicKey.isMasterKey() && sig.getKeyID() != mPublicKey.getKeyID()) { + continue; + } + + PGPSignatureSubpacketVector hashed = sig.getHashedSubPackets(); + if (hashed != null && hashed.getSubpacket(SignatureSubpacketTags.KEY_FLAGS) != null) { + // init if at least one key flag subpacket has been found + if (mCacheUsage == null) { + mCacheUsage = 0; + } + mCacheUsage |= hashed.getKeyFlags(); + } + } + } + return mCacheUsage; + } } -- cgit v1.2.3