From ccb157986434f6c0fafb31d906cda0e0f80caf88 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:23:39 +0100 Subject: Prefer composition to inheritance is the mantra these das --- .../keychain/service/OperationResultParcel.java | 30 ++++++++++++++++++---- .../keychain/ui/LogDisplayFragment.java | 3 ++- 2 files changed, 27 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 6bf6b655d..8db48ae3b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -9,6 +9,8 @@ import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** Represent the result of an operation. * @@ -288,7 +290,7 @@ public class OperationResultParcel implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { dest.writeInt(mResult); - dest.writeTypedList(mLog); + dest.writeTypedList(mLog.toList()); } public static final Creator CREATOR = new Creator() { @@ -301,20 +303,22 @@ public class OperationResultParcel implements Parcelable { } }; - public static class OperationLog extends ArrayList { + public static class OperationLog implements Iterable { + + private final List parcels = new ArrayList(); /// Simple convenience method public void add(LogLevel level, LogType type, int indent, Object... parameters) { Log.d(Constants.TAG, type.toString()); - add(new OperationResultParcel.LogEntryParcel(level, type, indent, parameters)); + parcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, parameters)); } public void add(LogLevel level, LogType type, int indent) { - add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); + parcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } public boolean containsWarnings() { - for(LogEntryParcel entry : new IterableIterator(iterator())) { + for(LogEntryParcel entry : new IterableIterator(parcels.iterator())) { if (entry.mLevel == LogLevel.WARN || entry.mLevel == LogLevel.ERROR) { return true; } @@ -322,6 +326,22 @@ public class OperationResultParcel implements Parcelable { return false; } + public void addAll(List parcels) { + parcels.addAll(parcels); + } + + public List toList() { + return parcels; + } + + public boolean isEmpty() { + return parcels.isEmpty(); + } + + @Override + public Iterator iterator() { + return parcels.iterator(); + } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 67317de6e..75c967c60 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -43,6 +43,7 @@ import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; public class LogDisplayFragment extends ListFragment implements OnTouchListener { @@ -135,7 +136,7 @@ public class LogDisplayFragment extends ListFragment implements OnTouchListener private LayoutInflater mInflater; private int dipFactor; - public LogAdapter(Context context, ArrayList log, LogLevel level) { + public LogAdapter(Context context, OperationResultParcel.OperationLog log, LogLevel level) { super(context, R.layout.log_display_item); mInflater = LayoutInflater.from(getContext()); dipFactor = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -- cgit v1.2.3 From 80e09bd05e69c2517fbb7a1573bdd6f515a36fc8 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 29 Jun 2014 12:18:16 +0100 Subject: work in progress --- .../keychain/testsupport/KeyringBuilder.java | 168 +++++++++++++ .../keychain/testsupport/TestDataUtil.java | 66 ++++- .../testsupport/UncachedKeyringTestingHelper.java | 278 +++++++++++++++++++++ 3 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java new file mode 100644 index 000000000..bbbe45ba2 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java @@ -0,0 +1,168 @@ +package org.sufficientlysecure.keychain.testsupport; + +import org.spongycastle.bcpg.CompressionAlgorithmTags; +import org.spongycastle.bcpg.HashAlgorithmTags; +import org.spongycastle.bcpg.MPInteger; +import org.spongycastle.bcpg.PublicKeyAlgorithmTags; +import org.spongycastle.bcpg.PublicKeyPacket; +import org.spongycastle.bcpg.RSAPublicBCPGKey; +import org.spongycastle.bcpg.SignaturePacket; +import org.spongycastle.bcpg.SignatureSubpacket; +import org.spongycastle.bcpg.SignatureSubpacketTags; +import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; +import org.spongycastle.bcpg.UserIDPacket; +import org.spongycastle.bcpg.sig.Features; +import org.spongycastle.bcpg.sig.IssuerKeyID; +import org.spongycastle.bcpg.sig.KeyFlags; +import org.spongycastle.bcpg.sig.PreferredAlgorithms; +import org.spongycastle.bcpg.sig.SignatureCreationTime; +import org.spongycastle.bcpg.sig.SignatureExpirationTime; +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; + +import java.math.BigInteger; +import java.util.Date; + +/** + * Created by art on 05/07/14. + */ +public class KeyringBuilder { + + + private static final BigInteger modulus = new BigInteger( + "cbab78d90d5f2cc0c54dd3c3953005a1" + + "e6b521f1ffa5465a102648bf7b91ec72" + + "f9c180759301587878caeb7333215620" + + "9f81ca5b3b94309d96110f6972cfc56a" + + "37fd6279f61d71f19b8f64b288e33829" + + "9dce133520f5b9b4253e6f4ba31ca36a" + + "fd87c2081b15f0b283e9350e370e181a" + + "23d31379101f17a23ae9192250db6540" + + "2e9cab2a275bc5867563227b197c8b13" + + "6c832a94325b680e144ed864fb00b9b8" + + "b07e13f37b40d5ac27dae63cd6a470a7" + + "b40fa3c7479b5b43e634850cc680b177" + + "8dd6b1b51856f36c3520f258f104db2f" + + "96b31a53dd74f708ccfcefccbe420a90" + + "1c37f1f477a6a4b15f5ecbbfd93311a6" + + "47bcc3f5f81c59dfe7252e3cd3be6e27" + , 16 + ); + + private static final BigInteger exponent = new BigInteger("010001", 16); + + public static UncachedKeyRing ring1() { + return ringForModulus(new Date(1404566755), "user1@example.com"); + } + + public static UncachedKeyRing ring2() { + return ringForModulus(new Date(1404566755), "user1@example.com"); + } + + private static UncachedKeyRing ringForModulus(Date date, String userIdString) { + + try { + PGPPublicKey publicKey = createPgpPublicKey(modulus, date); + UserIDPacket userId = createUserId(userIdString); + SignaturePacket signaturePacket = createSignaturePacket(date); + + byte[] publicKeyEncoded = publicKey.getEncoded(); + byte[] userIdEncoded = userId.getEncoded(); + byte[] signaturePacketEncoded = signaturePacket.getEncoded(); + byte[] encodedRing = TestDataUtil.concatAll( + publicKeyEncoded, + userIdEncoded, + signaturePacketEncoded + ); + + PGPPublicKeyRing pgpPublicKeyRing = new PGPPublicKeyRing( + encodedRing, new BcKeyFingerprintCalculator()); + + return UncachedKeyRing.decodeFromData(pgpPublicKeyRing.getEncoded()); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static SignaturePacket createSignaturePacket(Date date) { + int signatureType = PGPSignature.POSITIVE_CERTIFICATION; + long keyID = 1; + int keyAlgorithm = SignaturePacket.RSA_GENERAL; + int hashAlgorithm = HashAlgorithmTags.SHA1; + + SignatureSubpacket[] hashedData = new SignatureSubpacket[]{ + new SignatureCreationTime(true, date), + new KeyFlags(true, KeyFlags.SIGN_DATA & KeyFlags.CERTIFY_OTHER), + new SignatureExpirationTime(true, date.getTime() + 24 * 60 * 60 * 2), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, true, new int[]{ + SymmetricKeyAlgorithmTags.AES_256, + SymmetricKeyAlgorithmTags.AES_192, + SymmetricKeyAlgorithmTags.AES_128, + SymmetricKeyAlgorithmTags.CAST5, + SymmetricKeyAlgorithmTags.TRIPLE_DES + }), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_HASH_ALGS, true, new int[]{ + HashAlgorithmTags.SHA256, + HashAlgorithmTags.SHA1, + HashAlgorithmTags.SHA384, + HashAlgorithmTags.SHA512, + HashAlgorithmTags.SHA224 + }), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_COMP_ALGS, true, new int[]{ + CompressionAlgorithmTags.ZLIB, + CompressionAlgorithmTags.BZIP2, + CompressionAlgorithmTags.ZLIB + }), + new Features(false, Features.FEATURE_MODIFICATION_DETECTION), + // can't do keyserver prefs + }; + SignatureSubpacket[] unhashedData = new SignatureSubpacket[]{ + new IssuerKeyID(true, new BigInteger("15130BCF071AE6BF", 16).toByteArray()) + }; + byte[] fingerPrint = new BigInteger("522c", 16).toByteArray(); + MPInteger[] signature = new MPInteger[]{ + new MPInteger(new BigInteger( + "b065c071d3439d5610eb22e5b4df9e42" + + "ed78b8c94f487389e4fc98e8a75a043f" + + "14bf57d591811e8e7db2d31967022d2e" + + "e64372829183ec51d0e20c42d7a1e519" + + "e9fa22cd9db90f0fd7094fd093b78be2" + + "c0db62022193517404d749152c71edc6" + + "fd48af3416038d8842608ecddebbb11c" + + "5823a4321d2029b8993cb017fa8e5ad7" + + "8a9a618672d0217c4b34002f1a4a7625" + + "a514b6a86475e573cb87c64d7069658e" + + "627f2617874007a28d525e0f87d93ca7" + + "b15ad10dbdf10251e542afb8f9b16cbf" + + "7bebdb5fe7e867325a44e59cad0991cb" + + "239b1c859882e2ebb041b80e5cdc3b40" + + "ed259a8a27d63869754c0881ccdcb50f" + + "0564fecdc6966be4a4b87a3507a9d9be", 16 + )) + }; + return new SignaturePacket(signatureType, + keyID, + keyAlgorithm, + hashAlgorithm, + hashedData, + unhashedData, + fingerPrint, + signature); + } + + private static PGPPublicKey createPgpPublicKey(BigInteger modulus, Date date) throws PGPException { + PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_SIGN, date, new RSAPublicBCPGKey(modulus, exponent)); + return new PGPPublicKey( + publicKeyPacket, new BcKeyFingerprintCalculator()); + } + + private static UserIDPacket createUserId(String userId) { + return new UserIDPacket(userId); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java index 9e6ede761..338488e1f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.Iterator; /** * Misc support functions. Would just use Guava / Apache Commons but @@ -37,8 +38,71 @@ public class TestDataUtil { return output.toByteArray(); } - public static InputStream getResourceAsStream(String resourceName) { return TestDataUtil.class.getResourceAsStream(resourceName); } + + /** + * Null-safe equivalent of {@code a.equals(b)}. + */ + public static boolean equals(Object a, Object b) { + return (a == null) ? (b == null) : a.equals(b); + } + + public static boolean iterEquals(Iterator a, Iterator b, EqualityChecker comparator) { + while (a.hasNext()) { + T aObject = a.next(); + if (!b.hasNext()) { + return false; + } + T bObject = b.next(); + if (!comparator.areEquals(aObject, bObject)) { + return false; + } + } + + if (b.hasNext()) { + return false; + } + + return true; + } + + + public static boolean iterEquals(Iterator a, Iterator b) { + return iterEquals(a, b, new EqualityChecker() { + @Override + public boolean areEquals(T lhs, T rhs) { + return TestDataUtil.equals(lhs, rhs); + } + }); + } + + public static interface EqualityChecker { + public boolean areEquals(T lhs, T rhs); + } + + public static byte[] concatAll(byte[]... byteArrays) { + if (byteArrays.length == 1) { + return byteArrays[0]; + } else if (byteArrays.length == 2) { + return concat(byteArrays[0], byteArrays[1]); + } else { + byte[] first = concat(byteArrays[0], byteArrays[1]); + byte[][] remainingArrays = new byte[byteArrays.length - 1][]; + remainingArrays[0] = first; + System.arraycopy(byteArrays, 2, remainingArrays, 1, byteArrays.length - 2); + return concatAll(remainingArrays); + } + } + + private static byte[] concat(byte[] a, byte[] b) { + int aLen = a.length; + int bLen = b.length; + byte[] c = new byte[aLen + bLen]; + System.arraycopy(a, 0, c, 0, aLen); + System.arraycopy(b, 0, c, aLen, bLen); + return c; + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java new file mode 100644 index 000000000..e0580a86a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -0,0 +1,278 @@ +package org.sufficientlysecure.keychain.testsupport; + +import org.spongycastle.bcpg.BCPGKey; +import org.spongycastle.bcpg.PublicKeyAlgorithmTags; +import org.spongycastle.bcpg.PublicKeyPacket; +import org.spongycastle.bcpg.RSAPublicBCPGKey; +import org.spongycastle.bcpg.SignatureSubpacket; +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; +import org.spongycastle.openpgp.PGPUserAttributeSubpacketVector; +import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Date; + +/** + * Created by art on 28/06/14. + */ +public class UncachedKeyringTestingHelper { + + public static boolean compareRing(UncachedKeyRing keyRing1, UncachedKeyRing keyRing2) { + return TestDataUtil.iterEquals(keyRing1.getPublicKeys(), keyRing2.getPublicKeys(), new + TestDataUtil.EqualityChecker() { + @Override + public boolean areEquals(UncachedPublicKey lhs, UncachedPublicKey rhs) { + return comparePublicKey(lhs, rhs); + } + }); + } + + public static boolean comparePublicKey(UncachedPublicKey key1, UncachedPublicKey key2) { + boolean equal = true; + + if (key1.canAuthenticate() != key2.canAuthenticate()) { + return false; + } + if (key1.canCertify() != key2.canCertify()) { + return false; + } + if (key1.canEncrypt() != key2.canEncrypt()) { + return false; + } + if (key1.canSign() != key2.canSign()) { + return false; + } + if (key1.getAlgorithm() != key2.getAlgorithm()) { + return false; + } + if (key1.getBitStrength() != key2.getBitStrength()) { + return false; + } + if (!TestDataUtil.equals(key1.getCreationTime(), key2.getCreationTime())) { + return false; + } + if (!TestDataUtil.equals(key1.getExpiryTime(), key2.getExpiryTime())) { + return false; + } + if (!Arrays.equals(key1.getFingerprint(), key2.getFingerprint())) { + return false; + } + if (key1.getKeyId() != key2.getKeyId()) { + return false; + } + if (key1.getKeyUsage() != key2.getKeyUsage()) { + return false; + } + if (!TestDataUtil.equals(key1.getPrimaryUserId(), key2.getPrimaryUserId())) { + return false; + } + + // Ooops, getPublicKey is due to disappear. But then how to compare? + if (!keysAreEqual(key1.getPublicKey(), key2.getPublicKey())) { + return false; + } + + return equal; + } + + public static boolean keysAreEqual(PGPPublicKey a, PGPPublicKey b) { + + if (a.getAlgorithm() != b.getAlgorithm()) { + return false; + } + + if (a.getBitStrength() != b.getBitStrength()) { + return false; + } + + if (!TestDataUtil.equals(a.getCreationTime(), b.getCreationTime())) { + return false; + } + + if (!Arrays.equals(a.getFingerprint(), b.getFingerprint())) { + return false; + } + + if (a.getKeyID() != b.getKeyID()) { + return false; + } + + if (!pubKeyPacketsAreEqual(a.getPublicKeyPacket(), b.getPublicKeyPacket())) { + return false; + } + + if (a.getVersion() != b.getVersion()) { + return false; + } + + if (a.getValidDays() != b.getValidDays()) { + return false; + } + + if (a.getValidSeconds() != b.getValidSeconds()) { + return false; + } + + if (!Arrays.equals(a.getTrustData(), b.getTrustData())) { + return false; + } + + if (!TestDataUtil.iterEquals(a.getUserIDs(), b.getUserIDs())) { + return false; + } + + if (!TestDataUtil.iterEquals(a.getUserAttributes(), b.getUserAttributes(), + new TestDataUtil.EqualityChecker() { + public boolean areEquals(PGPUserAttributeSubpacketVector lhs, PGPUserAttributeSubpacketVector rhs) { + // For once, BC defines equals, so we use it implicitly. + return TestDataUtil.equals(lhs, rhs); + } + } + )) { + return false; + } + + + if (!TestDataUtil.iterEquals(a.getSignatures(), b.getSignatures(), + new TestDataUtil.EqualityChecker() { + public boolean areEquals(PGPSignature lhs, PGPSignature rhs) { + return signaturesAreEqual(lhs, rhs); + } + } + )) { + return false; + } + + return true; + } + + public static boolean signaturesAreEqual(PGPSignature a, PGPSignature b) { + + if (a.getVersion() != b.getVersion()) { + return false; + } + + if (a.getKeyAlgorithm() != b.getKeyAlgorithm()) { + return false; + } + + if (a.getHashAlgorithm() != b.getHashAlgorithm()) { + return false; + } + + if (a.getSignatureType() != b.getSignatureType()) { + return false; + } + + try { + if (!Arrays.equals(a.getSignature(), b.getSignature())) { + return false; + } + } catch (PGPException ex) { + throw new RuntimeException(ex); + } + + if (a.getKeyID() != b.getKeyID()) { + return false; + } + + if (!TestDataUtil.equals(a.getCreationTime(), b.getCreationTime())) { + return false; + } + + if (!Arrays.equals(a.getSignatureTrailer(), b.getSignatureTrailer())) { + return false; + } + + if (!subPacketVectorsAreEqual(a.getHashedSubPackets(), b.getHashedSubPackets())) { + return false; + } + + if (!subPacketVectorsAreEqual(a.getUnhashedSubPackets(), b.getUnhashedSubPackets())) { + return false; + } + + return true; + } + + private static boolean subPacketVectorsAreEqual(PGPSignatureSubpacketVector aHashedSubPackets, PGPSignatureSubpacketVector bHashedSubPackets) { + for (int i = 0; i < Byte.MAX_VALUE; i++) { + if (!TestDataUtil.iterEquals(Arrays.asList(aHashedSubPackets.getSubpackets(i)).iterator(), + Arrays.asList(bHashedSubPackets.getSubpackets(i)).iterator(), + new TestDataUtil.EqualityChecker() { + @Override + public boolean areEquals(SignatureSubpacket lhs, SignatureSubpacket rhs) { + return signatureSubpacketsAreEqual(lhs, rhs); + } + } + )) { + return false; + } + + } + return true; + } + + private static boolean signatureSubpacketsAreEqual(SignatureSubpacket lhs, SignatureSubpacket rhs) { + if (lhs.getType() != rhs.getType()) { + return false; + } + if (!Arrays.equals(lhs.getData(), rhs.getData())) { + return false; + } + return true; + } + + public static boolean pubKeyPacketsAreEqual(PublicKeyPacket a, PublicKeyPacket b) { + + if (a.getAlgorithm() != b.getAlgorithm()) { + return false; + } + + if (!bcpgKeysAreEqual(a.getKey(), b.getKey())) { + return false; + } + + if (!TestDataUtil.equals(a.getTime(), b.getTime())) { + return false; + } + + if (a.getValidDays() != b.getValidDays()) { + return false; + } + + if (a.getVersion() != b.getVersion()) { + return false; + } + + return true; + } + + public static boolean bcpgKeysAreEqual(BCPGKey a, BCPGKey b) { + + if (!TestDataUtil.equals(a.getFormat(), b.getFormat())) { + return false; + } + + if (!Arrays.equals(a.getEncoded(), b.getEncoded())) { + return false; + } + + return true; + } + + + public void doTestCanonicalize(UncachedKeyRing inputKeyRing, UncachedKeyRing expectedKeyRing) { + if (!compareRing(inputKeyRing, expectedKeyRing)) { + throw new AssertionError("Expected [" + inputKeyRing + "] to match [" + expectedKeyRing + "]"); + } + } + +} -- cgit v1.2.3 From 22108cf4e2ddd74be0d02e6560ac3db0cd547532 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:05:20 +0100 Subject: actually canonicalize --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java index e0580a86a..ac4955715 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -14,10 +14,12 @@ import org.spongycastle.openpgp.PGPUserAttributeSubpacketVector; import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import java.math.BigInteger; import java.util.Arrays; import java.util.Date; +import java.util.Objects; /** * Created by art on 28/06/14. @@ -25,7 +27,14 @@ import java.util.Date; public class UncachedKeyringTestingHelper { public static boolean compareRing(UncachedKeyRing keyRing1, UncachedKeyRing keyRing2) { - return TestDataUtil.iterEquals(keyRing1.getPublicKeys(), keyRing2.getPublicKeys(), new + OperationResultParcel.OperationLog operationLog = new OperationResultParcel.OperationLog(); + UncachedKeyRing canonicalized = keyRing1.canonicalize(operationLog, 0); + + if (canonicalized == null) { + throw new AssertionError("Canonicalization failed; messages: [" + operationLog.toString() + "]"); + } + + return TestDataUtil.iterEquals(canonicalized.getPublicKeys(), keyRing2.getPublicKeys(), new TestDataUtil.EqualityChecker() { @Override public boolean areEquals(UncachedPublicKey lhs, UncachedPublicKey rhs) { -- cgit v1.2.3 From b02519ce253664951aa2307e764b1833d2b88b52 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:48:47 +0100 Subject: add toString for test ease --- .../keychain/service/OperationResultParcel.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 6bf6b655d..2adfe8840 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -9,6 +9,7 @@ import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Arrays; /** Represent the result of an operation. * @@ -102,6 +103,15 @@ public class OperationResultParcel implements Parcelable { } }; + @Override + public String toString() { + return "LogEntryParcel{" + + "mLevel=" + mLevel + + ", mType=" + mType + + ", mParameters=" + Arrays.toString(mParameters) + + ", mIndent=" + mIndent + + '}'; + } } /** This is an enum of all possible log events. -- cgit v1.2.3 From cb64f8865c407543c01dbc3646e9eb1667996dae Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 29 Jun 2014 12:18:16 +0100 Subject: work in progress --- .../keychain/testsupport/KeyringBuilder.java | 168 +++++++++++++ .../keychain/testsupport/TestDataUtil.java | 66 ++++- .../testsupport/UncachedKeyringTestingHelper.java | 278 +++++++++++++++++++++ 3 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java new file mode 100644 index 000000000..bbbe45ba2 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java @@ -0,0 +1,168 @@ +package org.sufficientlysecure.keychain.testsupport; + +import org.spongycastle.bcpg.CompressionAlgorithmTags; +import org.spongycastle.bcpg.HashAlgorithmTags; +import org.spongycastle.bcpg.MPInteger; +import org.spongycastle.bcpg.PublicKeyAlgorithmTags; +import org.spongycastle.bcpg.PublicKeyPacket; +import org.spongycastle.bcpg.RSAPublicBCPGKey; +import org.spongycastle.bcpg.SignaturePacket; +import org.spongycastle.bcpg.SignatureSubpacket; +import org.spongycastle.bcpg.SignatureSubpacketTags; +import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; +import org.spongycastle.bcpg.UserIDPacket; +import org.spongycastle.bcpg.sig.Features; +import org.spongycastle.bcpg.sig.IssuerKeyID; +import org.spongycastle.bcpg.sig.KeyFlags; +import org.spongycastle.bcpg.sig.PreferredAlgorithms; +import org.spongycastle.bcpg.sig.SignatureCreationTime; +import org.spongycastle.bcpg.sig.SignatureExpirationTime; +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; + +import java.math.BigInteger; +import java.util.Date; + +/** + * Created by art on 05/07/14. + */ +public class KeyringBuilder { + + + private static final BigInteger modulus = new BigInteger( + "cbab78d90d5f2cc0c54dd3c3953005a1" + + "e6b521f1ffa5465a102648bf7b91ec72" + + "f9c180759301587878caeb7333215620" + + "9f81ca5b3b94309d96110f6972cfc56a" + + "37fd6279f61d71f19b8f64b288e33829" + + "9dce133520f5b9b4253e6f4ba31ca36a" + + "fd87c2081b15f0b283e9350e370e181a" + + "23d31379101f17a23ae9192250db6540" + + "2e9cab2a275bc5867563227b197c8b13" + + "6c832a94325b680e144ed864fb00b9b8" + + "b07e13f37b40d5ac27dae63cd6a470a7" + + "b40fa3c7479b5b43e634850cc680b177" + + "8dd6b1b51856f36c3520f258f104db2f" + + "96b31a53dd74f708ccfcefccbe420a90" + + "1c37f1f477a6a4b15f5ecbbfd93311a6" + + "47bcc3f5f81c59dfe7252e3cd3be6e27" + , 16 + ); + + private static final BigInteger exponent = new BigInteger("010001", 16); + + public static UncachedKeyRing ring1() { + return ringForModulus(new Date(1404566755), "user1@example.com"); + } + + public static UncachedKeyRing ring2() { + return ringForModulus(new Date(1404566755), "user1@example.com"); + } + + private static UncachedKeyRing ringForModulus(Date date, String userIdString) { + + try { + PGPPublicKey publicKey = createPgpPublicKey(modulus, date); + UserIDPacket userId = createUserId(userIdString); + SignaturePacket signaturePacket = createSignaturePacket(date); + + byte[] publicKeyEncoded = publicKey.getEncoded(); + byte[] userIdEncoded = userId.getEncoded(); + byte[] signaturePacketEncoded = signaturePacket.getEncoded(); + byte[] encodedRing = TestDataUtil.concatAll( + publicKeyEncoded, + userIdEncoded, + signaturePacketEncoded + ); + + PGPPublicKeyRing pgpPublicKeyRing = new PGPPublicKeyRing( + encodedRing, new BcKeyFingerprintCalculator()); + + return UncachedKeyRing.decodeFromData(pgpPublicKeyRing.getEncoded()); + + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private static SignaturePacket createSignaturePacket(Date date) { + int signatureType = PGPSignature.POSITIVE_CERTIFICATION; + long keyID = 1; + int keyAlgorithm = SignaturePacket.RSA_GENERAL; + int hashAlgorithm = HashAlgorithmTags.SHA1; + + SignatureSubpacket[] hashedData = new SignatureSubpacket[]{ + new SignatureCreationTime(true, date), + new KeyFlags(true, KeyFlags.SIGN_DATA & KeyFlags.CERTIFY_OTHER), + new SignatureExpirationTime(true, date.getTime() + 24 * 60 * 60 * 2), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, true, new int[]{ + SymmetricKeyAlgorithmTags.AES_256, + SymmetricKeyAlgorithmTags.AES_192, + SymmetricKeyAlgorithmTags.AES_128, + SymmetricKeyAlgorithmTags.CAST5, + SymmetricKeyAlgorithmTags.TRIPLE_DES + }), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_HASH_ALGS, true, new int[]{ + HashAlgorithmTags.SHA256, + HashAlgorithmTags.SHA1, + HashAlgorithmTags.SHA384, + HashAlgorithmTags.SHA512, + HashAlgorithmTags.SHA224 + }), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_COMP_ALGS, true, new int[]{ + CompressionAlgorithmTags.ZLIB, + CompressionAlgorithmTags.BZIP2, + CompressionAlgorithmTags.ZLIB + }), + new Features(false, Features.FEATURE_MODIFICATION_DETECTION), + // can't do keyserver prefs + }; + SignatureSubpacket[] unhashedData = new SignatureSubpacket[]{ + new IssuerKeyID(true, new BigInteger("15130BCF071AE6BF", 16).toByteArray()) + }; + byte[] fingerPrint = new BigInteger("522c", 16).toByteArray(); + MPInteger[] signature = new MPInteger[]{ + new MPInteger(new BigInteger( + "b065c071d3439d5610eb22e5b4df9e42" + + "ed78b8c94f487389e4fc98e8a75a043f" + + "14bf57d591811e8e7db2d31967022d2e" + + "e64372829183ec51d0e20c42d7a1e519" + + "e9fa22cd9db90f0fd7094fd093b78be2" + + "c0db62022193517404d749152c71edc6" + + "fd48af3416038d8842608ecddebbb11c" + + "5823a4321d2029b8993cb017fa8e5ad7" + + "8a9a618672d0217c4b34002f1a4a7625" + + "a514b6a86475e573cb87c64d7069658e" + + "627f2617874007a28d525e0f87d93ca7" + + "b15ad10dbdf10251e542afb8f9b16cbf" + + "7bebdb5fe7e867325a44e59cad0991cb" + + "239b1c859882e2ebb041b80e5cdc3b40" + + "ed259a8a27d63869754c0881ccdcb50f" + + "0564fecdc6966be4a4b87a3507a9d9be", 16 + )) + }; + return new SignaturePacket(signatureType, + keyID, + keyAlgorithm, + hashAlgorithm, + hashedData, + unhashedData, + fingerPrint, + signature); + } + + private static PGPPublicKey createPgpPublicKey(BigInteger modulus, Date date) throws PGPException { + PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_SIGN, date, new RSAPublicBCPGKey(modulus, exponent)); + return new PGPPublicKey( + publicKeyPacket, new BcKeyFingerprintCalculator()); + } + + private static UserIDPacket createUserId(String userId) { + return new UserIDPacket(userId); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java index 9e6ede761..338488e1f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Collection; +import java.util.Iterator; /** * Misc support functions. Would just use Guava / Apache Commons but @@ -37,8 +38,71 @@ public class TestDataUtil { return output.toByteArray(); } - public static InputStream getResourceAsStream(String resourceName) { return TestDataUtil.class.getResourceAsStream(resourceName); } + + /** + * Null-safe equivalent of {@code a.equals(b)}. + */ + public static boolean equals(Object a, Object b) { + return (a == null) ? (b == null) : a.equals(b); + } + + public static boolean iterEquals(Iterator a, Iterator b, EqualityChecker comparator) { + while (a.hasNext()) { + T aObject = a.next(); + if (!b.hasNext()) { + return false; + } + T bObject = b.next(); + if (!comparator.areEquals(aObject, bObject)) { + return false; + } + } + + if (b.hasNext()) { + return false; + } + + return true; + } + + + public static boolean iterEquals(Iterator a, Iterator b) { + return iterEquals(a, b, new EqualityChecker() { + @Override + public boolean areEquals(T lhs, T rhs) { + return TestDataUtil.equals(lhs, rhs); + } + }); + } + + public static interface EqualityChecker { + public boolean areEquals(T lhs, T rhs); + } + + public static byte[] concatAll(byte[]... byteArrays) { + if (byteArrays.length == 1) { + return byteArrays[0]; + } else if (byteArrays.length == 2) { + return concat(byteArrays[0], byteArrays[1]); + } else { + byte[] first = concat(byteArrays[0], byteArrays[1]); + byte[][] remainingArrays = new byte[byteArrays.length - 1][]; + remainingArrays[0] = first; + System.arraycopy(byteArrays, 2, remainingArrays, 1, byteArrays.length - 2); + return concatAll(remainingArrays); + } + } + + private static byte[] concat(byte[] a, byte[] b) { + int aLen = a.length; + int bLen = b.length; + byte[] c = new byte[aLen + bLen]; + System.arraycopy(a, 0, c, 0, aLen); + System.arraycopy(b, 0, c, aLen, bLen); + return c; + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java new file mode 100644 index 000000000..e0580a86a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -0,0 +1,278 @@ +package org.sufficientlysecure.keychain.testsupport; + +import org.spongycastle.bcpg.BCPGKey; +import org.spongycastle.bcpg.PublicKeyAlgorithmTags; +import org.spongycastle.bcpg.PublicKeyPacket; +import org.spongycastle.bcpg.RSAPublicBCPGKey; +import org.spongycastle.bcpg.SignatureSubpacket; +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; +import org.spongycastle.openpgp.PGPUserAttributeSubpacketVector; +import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.Date; + +/** + * Created by art on 28/06/14. + */ +public class UncachedKeyringTestingHelper { + + public static boolean compareRing(UncachedKeyRing keyRing1, UncachedKeyRing keyRing2) { + return TestDataUtil.iterEquals(keyRing1.getPublicKeys(), keyRing2.getPublicKeys(), new + TestDataUtil.EqualityChecker() { + @Override + public boolean areEquals(UncachedPublicKey lhs, UncachedPublicKey rhs) { + return comparePublicKey(lhs, rhs); + } + }); + } + + public static boolean comparePublicKey(UncachedPublicKey key1, UncachedPublicKey key2) { + boolean equal = true; + + if (key1.canAuthenticate() != key2.canAuthenticate()) { + return false; + } + if (key1.canCertify() != key2.canCertify()) { + return false; + } + if (key1.canEncrypt() != key2.canEncrypt()) { + return false; + } + if (key1.canSign() != key2.canSign()) { + return false; + } + if (key1.getAlgorithm() != key2.getAlgorithm()) { + return false; + } + if (key1.getBitStrength() != key2.getBitStrength()) { + return false; + } + if (!TestDataUtil.equals(key1.getCreationTime(), key2.getCreationTime())) { + return false; + } + if (!TestDataUtil.equals(key1.getExpiryTime(), key2.getExpiryTime())) { + return false; + } + if (!Arrays.equals(key1.getFingerprint(), key2.getFingerprint())) { + return false; + } + if (key1.getKeyId() != key2.getKeyId()) { + return false; + } + if (key1.getKeyUsage() != key2.getKeyUsage()) { + return false; + } + if (!TestDataUtil.equals(key1.getPrimaryUserId(), key2.getPrimaryUserId())) { + return false; + } + + // Ooops, getPublicKey is due to disappear. But then how to compare? + if (!keysAreEqual(key1.getPublicKey(), key2.getPublicKey())) { + return false; + } + + return equal; + } + + public static boolean keysAreEqual(PGPPublicKey a, PGPPublicKey b) { + + if (a.getAlgorithm() != b.getAlgorithm()) { + return false; + } + + if (a.getBitStrength() != b.getBitStrength()) { + return false; + } + + if (!TestDataUtil.equals(a.getCreationTime(), b.getCreationTime())) { + return false; + } + + if (!Arrays.equals(a.getFingerprint(), b.getFingerprint())) { + return false; + } + + if (a.getKeyID() != b.getKeyID()) { + return false; + } + + if (!pubKeyPacketsAreEqual(a.getPublicKeyPacket(), b.getPublicKeyPacket())) { + return false; + } + + if (a.getVersion() != b.getVersion()) { + return false; + } + + if (a.getValidDays() != b.getValidDays()) { + return false; + } + + if (a.getValidSeconds() != b.getValidSeconds()) { + return false; + } + + if (!Arrays.equals(a.getTrustData(), b.getTrustData())) { + return false; + } + + if (!TestDataUtil.iterEquals(a.getUserIDs(), b.getUserIDs())) { + return false; + } + + if (!TestDataUtil.iterEquals(a.getUserAttributes(), b.getUserAttributes(), + new TestDataUtil.EqualityChecker() { + public boolean areEquals(PGPUserAttributeSubpacketVector lhs, PGPUserAttributeSubpacketVector rhs) { + // For once, BC defines equals, so we use it implicitly. + return TestDataUtil.equals(lhs, rhs); + } + } + )) { + return false; + } + + + if (!TestDataUtil.iterEquals(a.getSignatures(), b.getSignatures(), + new TestDataUtil.EqualityChecker() { + public boolean areEquals(PGPSignature lhs, PGPSignature rhs) { + return signaturesAreEqual(lhs, rhs); + } + } + )) { + return false; + } + + return true; + } + + public static boolean signaturesAreEqual(PGPSignature a, PGPSignature b) { + + if (a.getVersion() != b.getVersion()) { + return false; + } + + if (a.getKeyAlgorithm() != b.getKeyAlgorithm()) { + return false; + } + + if (a.getHashAlgorithm() != b.getHashAlgorithm()) { + return false; + } + + if (a.getSignatureType() != b.getSignatureType()) { + return false; + } + + try { + if (!Arrays.equals(a.getSignature(), b.getSignature())) { + return false; + } + } catch (PGPException ex) { + throw new RuntimeException(ex); + } + + if (a.getKeyID() != b.getKeyID()) { + return false; + } + + if (!TestDataUtil.equals(a.getCreationTime(), b.getCreationTime())) { + return false; + } + + if (!Arrays.equals(a.getSignatureTrailer(), b.getSignatureTrailer())) { + return false; + } + + if (!subPacketVectorsAreEqual(a.getHashedSubPackets(), b.getHashedSubPackets())) { + return false; + } + + if (!subPacketVectorsAreEqual(a.getUnhashedSubPackets(), b.getUnhashedSubPackets())) { + return false; + } + + return true; + } + + private static boolean subPacketVectorsAreEqual(PGPSignatureSubpacketVector aHashedSubPackets, PGPSignatureSubpacketVector bHashedSubPackets) { + for (int i = 0; i < Byte.MAX_VALUE; i++) { + if (!TestDataUtil.iterEquals(Arrays.asList(aHashedSubPackets.getSubpackets(i)).iterator(), + Arrays.asList(bHashedSubPackets.getSubpackets(i)).iterator(), + new TestDataUtil.EqualityChecker() { + @Override + public boolean areEquals(SignatureSubpacket lhs, SignatureSubpacket rhs) { + return signatureSubpacketsAreEqual(lhs, rhs); + } + } + )) { + return false; + } + + } + return true; + } + + private static boolean signatureSubpacketsAreEqual(SignatureSubpacket lhs, SignatureSubpacket rhs) { + if (lhs.getType() != rhs.getType()) { + return false; + } + if (!Arrays.equals(lhs.getData(), rhs.getData())) { + return false; + } + return true; + } + + public static boolean pubKeyPacketsAreEqual(PublicKeyPacket a, PublicKeyPacket b) { + + if (a.getAlgorithm() != b.getAlgorithm()) { + return false; + } + + if (!bcpgKeysAreEqual(a.getKey(), b.getKey())) { + return false; + } + + if (!TestDataUtil.equals(a.getTime(), b.getTime())) { + return false; + } + + if (a.getValidDays() != b.getValidDays()) { + return false; + } + + if (a.getVersion() != b.getVersion()) { + return false; + } + + return true; + } + + public static boolean bcpgKeysAreEqual(BCPGKey a, BCPGKey b) { + + if (!TestDataUtil.equals(a.getFormat(), b.getFormat())) { + return false; + } + + if (!Arrays.equals(a.getEncoded(), b.getEncoded())) { + return false; + } + + return true; + } + + + public void doTestCanonicalize(UncachedKeyRing inputKeyRing, UncachedKeyRing expectedKeyRing) { + if (!compareRing(inputKeyRing, expectedKeyRing)) { + throw new AssertionError("Expected [" + inputKeyRing + "] to match [" + expectedKeyRing + "]"); + } + } + +} -- cgit v1.2.3 From 5479eafd4b295c0d83955133c3150652bb325578 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:05:20 +0100 Subject: actually canonicalize --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java index e0580a86a..ac4955715 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -14,10 +14,12 @@ import org.spongycastle.openpgp.PGPUserAttributeSubpacketVector; import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import java.math.BigInteger; import java.util.Arrays; import java.util.Date; +import java.util.Objects; /** * Created by art on 28/06/14. @@ -25,7 +27,14 @@ import java.util.Date; public class UncachedKeyringTestingHelper { public static boolean compareRing(UncachedKeyRing keyRing1, UncachedKeyRing keyRing2) { - return TestDataUtil.iterEquals(keyRing1.getPublicKeys(), keyRing2.getPublicKeys(), new + OperationResultParcel.OperationLog operationLog = new OperationResultParcel.OperationLog(); + UncachedKeyRing canonicalized = keyRing1.canonicalize(operationLog, 0); + + if (canonicalized == null) { + throw new AssertionError("Canonicalization failed; messages: [" + operationLog.toString() + "]"); + } + + return TestDataUtil.iterEquals(canonicalized.getPublicKeys(), keyRing2.getPublicKeys(), new TestDataUtil.EqualityChecker() { @Override public boolean areEquals(UncachedPublicKey lhs, UncachedPublicKey rhs) { -- cgit v1.2.3 From 9032e032ff7583ddd09008446ff16c90c6a190b2 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:48:47 +0100 Subject: add toString for test ease --- .../keychain/service/OperationResultParcel.java | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 8db48ae3b..babd251c4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -9,6 +9,7 @@ import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -104,6 +105,15 @@ public class OperationResultParcel implements Parcelable { } }; + @Override + public String toString() { + return "LogEntryParcel{" + + "mLevel=" + mLevel + + ", mType=" + mType + + ", mParameters=" + Arrays.toString(mParameters) + + ", mIndent=" + mIndent + + '}'; + } } /** This is an enum of all possible log events. -- cgit v1.2.3 From c05fd0798627ea2444e9f06fc611bff3e7ee44f3 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 17:09:23 +0100 Subject: data fixes --- .../keychain/testsupport/KeyringBuilder.java | 27 +++++++++++----------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java index bbbe45ba2..dfa70d506 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java @@ -13,10 +13,10 @@ import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; import org.spongycastle.bcpg.UserIDPacket; import org.spongycastle.bcpg.sig.Features; import org.spongycastle.bcpg.sig.IssuerKeyID; +import org.spongycastle.bcpg.sig.KeyExpirationTime; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.bcpg.sig.PreferredAlgorithms; import org.spongycastle.bcpg.sig.SignatureCreationTime; -import org.spongycastle.bcpg.sig.SignatureExpirationTime; import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; @@ -26,6 +26,7 @@ import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import java.math.BigInteger; import java.util.Date; +import java.util.concurrent.TimeUnit; /** * Created by art on 05/07/14. @@ -56,11 +57,11 @@ public class KeyringBuilder { private static final BigInteger exponent = new BigInteger("010001", 16); public static UncachedKeyRing ring1() { - return ringForModulus(new Date(1404566755), "user1@example.com"); + return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); } public static UncachedKeyRing ring2() { - return ringForModulus(new Date(1404566755), "user1@example.com"); + return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); } private static UncachedKeyRing ringForModulus(Date date, String userIdString) { @@ -91,38 +92,38 @@ public class KeyringBuilder { private static SignaturePacket createSignaturePacket(Date date) { int signatureType = PGPSignature.POSITIVE_CERTIFICATION; - long keyID = 1; + long keyID = 0x15130BCF071AE6BFL; int keyAlgorithm = SignaturePacket.RSA_GENERAL; int hashAlgorithm = HashAlgorithmTags.SHA1; SignatureSubpacket[] hashedData = new SignatureSubpacket[]{ - new SignatureCreationTime(true, date), - new KeyFlags(true, KeyFlags.SIGN_DATA & KeyFlags.CERTIFY_OTHER), - new SignatureExpirationTime(true, date.getTime() + 24 * 60 * 60 * 2), - new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, true, new int[]{ + new SignatureCreationTime(false, date), + new KeyFlags(false, KeyFlags.CERTIFY_OTHER + KeyFlags.SIGN_DATA), + new KeyExpirationTime(false, TimeUnit.DAYS.toSeconds(2)), + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, false, new int[]{ SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, SymmetricKeyAlgorithmTags.AES_128, SymmetricKeyAlgorithmTags.CAST5, SymmetricKeyAlgorithmTags.TRIPLE_DES }), - new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_HASH_ALGS, true, new int[]{ + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_HASH_ALGS, false, new int[]{ HashAlgorithmTags.SHA256, HashAlgorithmTags.SHA1, HashAlgorithmTags.SHA384, HashAlgorithmTags.SHA512, HashAlgorithmTags.SHA224 }), - new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_COMP_ALGS, true, new int[]{ + new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_COMP_ALGS, false, new int[]{ CompressionAlgorithmTags.ZLIB, CompressionAlgorithmTags.BZIP2, - CompressionAlgorithmTags.ZLIB + CompressionAlgorithmTags.ZIP }), new Features(false, Features.FEATURE_MODIFICATION_DETECTION), // can't do keyserver prefs }; SignatureSubpacket[] unhashedData = new SignatureSubpacket[]{ - new IssuerKeyID(true, new BigInteger("15130BCF071AE6BF", 16).toByteArray()) + new IssuerKeyID(false, new BigInteger("15130BCF071AE6BF", 16).toByteArray()) }; byte[] fingerPrint = new BigInteger("522c", 16).toByteArray(); MPInteger[] signature = new MPInteger[]{ @@ -156,7 +157,7 @@ public class KeyringBuilder { } private static PGPPublicKey createPgpPublicKey(BigInteger modulus, Date date) throws PGPException { - PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_SIGN, date, new RSAPublicBCPGKey(modulus, exponent)); + PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_GENERAL, date, new RSAPublicBCPGKey(modulus, exponent)); return new PGPPublicKey( publicKeyPacket, new BcKeyFingerprintCalculator()); } -- cgit v1.2.3 From 4fbffd7bb481a44a57a94c1af5e9be80094d815f Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 17:46:43 +0100 Subject: Actually test canonicalize --- .../keychain/testsupport/KeyringBuilder.java | 216 ++++++++++++++------- .../keychain/testsupport/TestDataUtil.java | 19 +- 2 files changed, 160 insertions(+), 75 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java index dfa70d506..f618d8de9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java @@ -1,13 +1,16 @@ package org.sufficientlysecure.keychain.testsupport; import org.spongycastle.bcpg.CompressionAlgorithmTags; +import org.spongycastle.bcpg.ContainedPacket; import org.spongycastle.bcpg.HashAlgorithmTags; import org.spongycastle.bcpg.MPInteger; import org.spongycastle.bcpg.PublicKeyAlgorithmTags; import org.spongycastle.bcpg.PublicKeyPacket; +import org.spongycastle.bcpg.PublicSubkeyPacket; import org.spongycastle.bcpg.RSAPublicBCPGKey; import org.spongycastle.bcpg.SignaturePacket; import org.spongycastle.bcpg.SignatureSubpacket; +import org.spongycastle.bcpg.SignatureSubpacketInputStream; import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.SymmetricKeyAlgorithmTags; import org.spongycastle.bcpg.UserIDPacket; @@ -17,87 +20,126 @@ import org.spongycastle.bcpg.sig.KeyExpirationTime; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.bcpg.sig.PreferredAlgorithms; import org.spongycastle.bcpg.sig.SignatureCreationTime; -import org.spongycastle.openpgp.PGPException; -import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPSignature; -import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.concurrent.TimeUnit; /** - * Created by art on 05/07/14. + * Helps create correct and incorrect keyrings for tests. + * + * The original "correct" keyring was generated by GnuPG. */ public class KeyringBuilder { - private static final BigInteger modulus = new BigInteger( - "cbab78d90d5f2cc0c54dd3c3953005a1" + - "e6b521f1ffa5465a102648bf7b91ec72" + - "f9c180759301587878caeb7333215620" + - "9f81ca5b3b94309d96110f6972cfc56a" + - "37fd6279f61d71f19b8f64b288e33829" + - "9dce133520f5b9b4253e6f4ba31ca36a" + - "fd87c2081b15f0b283e9350e370e181a" + - "23d31379101f17a23ae9192250db6540" + - "2e9cab2a275bc5867563227b197c8b13" + - "6c832a94325b680e144ed864fb00b9b8" + - "b07e13f37b40d5ac27dae63cd6a470a7" + - "b40fa3c7479b5b43e634850cc680b177" + - "8dd6b1b51856f36c3520f258f104db2f" + - "96b31a53dd74f708ccfcefccbe420a90" + - "1c37f1f477a6a4b15f5ecbbfd93311a6" + - "47bcc3f5f81c59dfe7252e3cd3be6e27" + private static final BigInteger PUBLIC_KEY_MODULUS = new BigInteger( + "cbab78d90d5f2cc0c54dd3c3953005a1e6b521f1ffa5465a102648bf7b91ec72" + + "f9c180759301587878caeb73332156209f81ca5b3b94309d96110f6972cfc56a" + + "37fd6279f61d71f19b8f64b288e338299dce133520f5b9b4253e6f4ba31ca36a" + + "fd87c2081b15f0b283e9350e370e181a23d31379101f17a23ae9192250db6540" + + "2e9cab2a275bc5867563227b197c8b136c832a94325b680e144ed864fb00b9b8" + + "b07e13f37b40d5ac27dae63cd6a470a7b40fa3c7479b5b43e634850cc680b177" + + "8dd6b1b51856f36c3520f258f104db2f96b31a53dd74f708ccfcefccbe420a90" + + "1c37f1f477a6a4b15f5ecbbfd93311a647bcc3f5f81c59dfe7252e3cd3be6e27" , 16 ); - private static final BigInteger exponent = new BigInteger("010001", 16); - - public static UncachedKeyRing ring1() { - return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); - } + private static final BigInteger PUBLIC_SUBKEY_MODULUS = new BigInteger( + "e8e2e2a33102649f19f8a07486fb076a1406ca888d72ae05d28f0ef372b5408e" + + "45132c69f6e5cb6a79bb8aed84634196731393a82d53e0ddd42f28f92cc15850" + + "8ce3b7ca1a9830502745aee774f86987993df984781f47c4a2910f95cf4c950c" + + "c4c6cccdc134ad408a0c5418b5e360c9781a8434d366053ea6338b975fae88f9" + + "383a10a90e7b2caa9ddb95708aa9d8a90246e29b04dbd6136613085c9a287315" + + "c6e9c7ff4012defc1713875e3ff6073333a1c93d7cd75ebeaaf16b8b853d96ba" + + "7003258779e8d2f70f1bc0bcd3ef91d7a9ccd8e225579b2d6fcae32799b0a6c0" + + "e7305fc65dc4edc849c6130a0d669c90c193b1e746c812510f9d600a208be4a5" + , 16 + ); - public static UncachedKeyRing ring2() { - return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); - } + private static final Date SIGNATURE_DATE = new Date(1404566755000L); - private static UncachedKeyRing ringForModulus(Date date, String userIdString) { + private static final BigInteger EXPONENT = BigInteger.valueOf(0x010001); - try { - PGPPublicKey publicKey = createPgpPublicKey(modulus, date); - UserIDPacket userId = createUserId(userIdString); - SignaturePacket signaturePacket = createSignaturePacket(date); + private static final String USER_ID_STRING = "OpenKeychain User (NOT A REAL KEY) "; - byte[] publicKeyEncoded = publicKey.getEncoded(); - byte[] userIdEncoded = userId.getEncoded(); - byte[] signaturePacketEncoded = signaturePacket.getEncoded(); - byte[] encodedRing = TestDataUtil.concatAll( - publicKeyEncoded, - userIdEncoded, - signaturePacketEncoded - ); + public static final BigInteger CORRECT_SIGNATURE = new BigInteger( + "b065c071d3439d5610eb22e5b4df9e42ed78b8c94f487389e4fc98e8a75a043f" + + "14bf57d591811e8e7db2d31967022d2ee64372829183ec51d0e20c42d7a1e519" + + "e9fa22cd9db90f0fd7094fd093b78be2c0db62022193517404d749152c71edc6" + + "fd48af3416038d8842608ecddebbb11c5823a4321d2029b8993cb017fa8e5ad7" + + "8a9a618672d0217c4b34002f1a4a7625a514b6a86475e573cb87c64d7069658e" + + "627f2617874007a28d525e0f87d93ca7b15ad10dbdf10251e542afb8f9b16cbf" + + "7bebdb5fe7e867325a44e59cad0991cb239b1c859882e2ebb041b80e5cdc3b40" + + "ed259a8a27d63869754c0881ccdcb50f0564fecdc6966be4a4b87a3507a9d9be" + , 16 + ); + public static final BigInteger CORRECT_SUBKEY_SIGNATURE = new BigInteger( + "9c40543e646cfa6d3d1863d91a4e8f1421d0616ddb3187505df75fbbb6c59dd5" + + "3136b866f246a0320e793cb142c55c8e0e521d1e8d9ab864650f10690f5f1429" + + "2eb8402a3b1f82c01079d12f5c57c43fce524a530e6f49f6f87d984e26db67a2" + + "d469386dac87553c50147ebb6c2edd9248325405f737b815253beedaaba4f5c9" + + "3acd5d07fe6522ceda1027932d849e3ec4d316422cd43ea6e506f643936ab0be" + + "8246e546bb90d9a83613185047566864ffe894946477e939725171e0e15710b2" + + "089f78752a9cb572f5907323f1b62f14cb07671aeb02e6d7178f185467624ec5" + + "74e4a73c439a12edba200a4832106767366a1e6f63da0a42d593fa3914deee2b" + , 16 + ); + public static final BigInteger KEY_ID = BigInteger.valueOf(0x15130BCF071AE6BFL); - PGPPublicKeyRing pgpPublicKeyRing = new PGPPublicKeyRing( - encodedRing, new BcKeyFingerprintCalculator()); + public static UncachedKeyRing correctRing() { + return convertToKeyring(correctKeyringPackets()); + } - return UncachedKeyRing.decodeFromData(pgpPublicKeyRing.getEncoded()); + public static UncachedKeyRing ringWithExtraIncorrectSignature() { + List packets = correctKeyringPackets(); + SignaturePacket incorrectSignaturePacket = createSignaturePacket(CORRECT_SIGNATURE.subtract(BigInteger.ONE)); + packets.add(2, incorrectSignaturePacket); + return convertToKeyring(packets); + } + private static UncachedKeyRing convertToKeyring(List packets) { + try { + return UncachedKeyRing.decodeFromData(TestDataUtil.concatAll(packets)); } catch (Exception e) { throw new RuntimeException(e); } } - private static SignaturePacket createSignaturePacket(Date date) { + private static List correctKeyringPackets() { + PublicKeyPacket publicKey = createPgpPublicKey(PUBLIC_KEY_MODULUS); + UserIDPacket userId = createUserId(USER_ID_STRING); + SignaturePacket signaturePacket = createSignaturePacket(CORRECT_SIGNATURE); + PublicKeyPacket subKey = createPgpPublicSubKey(PUBLIC_SUBKEY_MODULUS); + SignaturePacket subKeySignaturePacket = createSubkeySignaturePacket(); + + return new ArrayList(Arrays.asList( + publicKey, + userId, + signaturePacket, + subKey, + subKeySignaturePacket + )); + } + + private static SignaturePacket createSignaturePacket(BigInteger signature) { + MPInteger[] signatureArray = new MPInteger[]{ + new MPInteger(signature) + }; + int signatureType = PGPSignature.POSITIVE_CERTIFICATION; - long keyID = 0x15130BCF071AE6BFL; int keyAlgorithm = SignaturePacket.RSA_GENERAL; int hashAlgorithm = HashAlgorithmTags.SHA1; SignatureSubpacket[] hashedData = new SignatureSubpacket[]{ - new SignatureCreationTime(false, date), + new SignatureCreationTime(false, SIGNATURE_DATE), new KeyFlags(false, KeyFlags.CERTIFY_OTHER + KeyFlags.SIGN_DATA), new KeyExpirationTime(false, TimeUnit.DAYS.toSeconds(2)), new PreferredAlgorithms(SignatureSubpacketTags.PREFERRED_SYM_ALGS, false, new int[]{ @@ -120,34 +162,58 @@ public class KeyringBuilder { CompressionAlgorithmTags.ZIP }), new Features(false, Features.FEATURE_MODIFICATION_DETECTION), - // can't do keyserver prefs + createPreferencesSignatureSubpacket() }; SignatureSubpacket[] unhashedData = new SignatureSubpacket[]{ - new IssuerKeyID(false, new BigInteger("15130BCF071AE6BF", 16).toByteArray()) + new IssuerKeyID(false, KEY_ID.toByteArray()) }; byte[] fingerPrint = new BigInteger("522c", 16).toByteArray(); + + return new SignaturePacket(signatureType, + KEY_ID.longValue(), + keyAlgorithm, + hashAlgorithm, + hashedData, + unhashedData, + fingerPrint, + signatureArray); + } + + /** + * There is no Preferences subpacket in BouncyCastle, so we have + * to create one manually. + */ + private static SignatureSubpacket createPreferencesSignatureSubpacket() { + SignatureSubpacket prefs; + try { + prefs = new SignatureSubpacketInputStream(new ByteArrayInputStream( + new byte[]{2, SignatureSubpacketTags.KEY_SERVER_PREFS, (byte) 0x80}) + ).readPacket(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + return prefs; + } + + private static SignaturePacket createSubkeySignaturePacket() { + int signatureType = PGPSignature.SUBKEY_BINDING; + int keyAlgorithm = SignaturePacket.RSA_GENERAL; + int hashAlgorithm = HashAlgorithmTags.SHA1; + + SignatureSubpacket[] hashedData = new SignatureSubpacket[]{ + new SignatureCreationTime(false, SIGNATURE_DATE), + new KeyFlags(false, KeyFlags.ENCRYPT_COMMS + KeyFlags.ENCRYPT_STORAGE), + new KeyExpirationTime(false, TimeUnit.DAYS.toSeconds(2)), + }; + SignatureSubpacket[] unhashedData = new SignatureSubpacket[]{ + new IssuerKeyID(false, KEY_ID.toByteArray()) + }; + byte[] fingerPrint = new BigInteger("234a", 16).toByteArray(); MPInteger[] signature = new MPInteger[]{ - new MPInteger(new BigInteger( - "b065c071d3439d5610eb22e5b4df9e42" + - "ed78b8c94f487389e4fc98e8a75a043f" + - "14bf57d591811e8e7db2d31967022d2e" + - "e64372829183ec51d0e20c42d7a1e519" + - "e9fa22cd9db90f0fd7094fd093b78be2" + - "c0db62022193517404d749152c71edc6" + - "fd48af3416038d8842608ecddebbb11c" + - "5823a4321d2029b8993cb017fa8e5ad7" + - "8a9a618672d0217c4b34002f1a4a7625" + - "a514b6a86475e573cb87c64d7069658e" + - "627f2617874007a28d525e0f87d93ca7" + - "b15ad10dbdf10251e542afb8f9b16cbf" + - "7bebdb5fe7e867325a44e59cad0991cb" + - "239b1c859882e2ebb041b80e5cdc3b40" + - "ed259a8a27d63869754c0881ccdcb50f" + - "0564fecdc6966be4a4b87a3507a9d9be", 16 - )) + new MPInteger(CORRECT_SUBKEY_SIGNATURE) }; return new SignaturePacket(signatureType, - keyID, + KEY_ID.longValue(), keyAlgorithm, hashAlgorithm, hashedData, @@ -156,10 +222,12 @@ public class KeyringBuilder { signature); } - private static PGPPublicKey createPgpPublicKey(BigInteger modulus, Date date) throws PGPException { - PublicKeyPacket publicKeyPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_GENERAL, date, new RSAPublicBCPGKey(modulus, exponent)); - return new PGPPublicKey( - publicKeyPacket, new BcKeyFingerprintCalculator()); + private static PublicKeyPacket createPgpPublicKey(BigInteger modulus) { + return new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_GENERAL, SIGNATURE_DATE, new RSAPublicBCPGKey(modulus, EXPONENT)); + } + + private static PublicKeyPacket createPgpPublicSubKey(BigInteger modulus) { + return new PublicSubkeyPacket(PublicKeyAlgorithmTags.RSA_GENERAL, SIGNATURE_DATE, new RSAPublicBCPGKey(modulus, EXPONENT)); } private static UserIDPacket createUserId(String userId) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java index 338488e1f..c08b60d1a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java @@ -1,8 +1,11 @@ package org.sufficientlysecure.keychain.testsupport; +import org.spongycastle.bcpg.ContainedPacket; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.util.Collection; import java.util.Iterator; @@ -17,7 +20,7 @@ public class TestDataUtil { return output.toByteArray(); } - private static void appendToOutput(InputStream input, ByteArrayOutputStream output) { + public static void appendToOutput(InputStream input, OutputStream output) { byte[] buffer = new byte[8192]; int bytesRead; try { @@ -82,6 +85,20 @@ public class TestDataUtil { public boolean areEquals(T lhs, T rhs); } + + public static byte[] concatAll(java.util.List packets) { + byte[][] byteArrays = new byte[packets.size()][]; + try { + for (int i = 0; i < packets.size(); i++) { + byteArrays[i] = packets.get(i).getEncoded(); + } + } catch (IOException ex) { + throw new RuntimeException(ex); + } + + return concatAll(byteArrays); + } + public static byte[] concatAll(byte[]... byteArrays) { if (byteArrays.length == 1) { return byteArrays[0]; -- cgit v1.2.3 From d044daeedd9ae42e72ce3d3b2ed191778b173943 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 17:32:16 +0200 Subject: get rid of AppMsg library --- .../keychain/ui/EditKeyActivityOld.java | 744 --------------------- 1 file changed, 744 deletions(-) delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java deleted file mode 100644 index 51457cd45..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java +++ /dev/null @@ -1,744 +0,0 @@ -/* - * Copyright (C) 2012-2014 Dominik Schürmann - * Copyright (C) 2010-2014 Thialfihar - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui; - -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.os.Messenger; -import android.support.v4.app.ActivityCompat; -import android.support.v7.app.ActionBarActivity; -import android.view.LayoutInflater; -import android.view.View; -import android.view.View.OnClickListener; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.CompoundButton; -import android.widget.CompoundButton.OnCheckedChangeListener; -import android.widget.LinearLayout; -import android.widget.Toast; - -import com.devspark.appmsg.AppMsg; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.helper.ActionBarHelper; -import org.sufficientlysecure.keychain.helper.ExportHelper; -import org.sufficientlysecure.keychain.pgp.KeyRing; -import org.sufficientlysecure.keychain.pgp.UncachedSecretKey; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; -import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.service.PassphraseCacheService; -import org.sufficientlysecure.keychain.ui.dialog.CustomAlertDialogBuilder; -import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; -import org.sufficientlysecure.keychain.ui.widget.Editor; -import org.sufficientlysecure.keychain.ui.widget.Editor.EditorListener; -import org.sufficientlysecure.keychain.ui.widget.KeyEditor; -import org.sufficientlysecure.keychain.ui.widget.SectionView; -import org.sufficientlysecure.keychain.ui.widget.UserIdEditor; -import org.sufficientlysecure.keychain.util.Log; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; -import java.util.Vector; - -public class EditKeyActivityOld extends ActionBarActivity implements EditorListener { - - // Actions for internal use only: - public static final String ACTION_CREATE_KEY = Constants.INTENT_PREFIX + "CREATE_KEY"; - public static final String ACTION_EDIT_KEY = Constants.INTENT_PREFIX + "EDIT_KEY"; - - // possible extra keys - public static final String EXTRA_USER_IDS = "user_ids"; - public static final String EXTRA_NO_PASSPHRASE = "no_passphrase"; - public static final String EXTRA_GENERATE_DEFAULT_KEYS = "generate_default_keys"; - - // EDIT - private Uri mDataUri; - - private SectionView mUserIdsView; - private SectionView mKeysView; - - private String mCurrentPassphrase = null; - private String mNewPassphrase = null; - private String mSavedNewPassphrase = null; - private boolean mIsPassphraseSet; - private boolean mNeedsSaving; - private boolean mIsBrandNewKeyring = false; - - private Button mChangePassphrase; - - private CheckBox mNoPassphrase; - - Vector mUserIds; - Vector mKeys; - Vector mKeysUsages; - boolean mMasterCanSign = true; - - ExportHelper mExportHelper; - - public boolean needsSaving() { - mNeedsSaving = (mUserIdsView == null) ? false : mUserIdsView.needsSaving(); - mNeedsSaving |= (mKeysView == null) ? false : mKeysView.needsSaving(); - mNeedsSaving |= hasPassphraseChanged(); - mNeedsSaving |= mIsBrandNewKeyring; - return mNeedsSaving; - } - - - public void somethingChanged() { - ActivityCompat.invalidateOptionsMenu(this); - } - - public void onDeleted(Editor e, boolean wasNewItem) { - somethingChanged(); - } - - public void onEdited() { - somethingChanged(); - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - mExportHelper = new ExportHelper(this); - - // Inflate a "Done"/"Cancel" custom action bar view - ActionBarHelper.setTwoButtonView(getSupportActionBar(), - R.string.btn_save, R.drawable.ic_action_save, - new View.OnClickListener() { - @Override - public void onClick(View v) { - // Save - saveClicked(); - } - }, R.string.menu_key_edit_cancel, R.drawable.ic_action_cancel, - new View.OnClickListener() { - @Override - public void onClick(View v) { - // Cancel - cancelClicked(); - } - } - ); - - mUserIds = new Vector(); - mKeys = new Vector(); - mKeysUsages = new Vector(); - - // Catch Intents opened from other apps - Intent intent = getIntent(); - String action = intent.getAction(); - if (ACTION_CREATE_KEY.equals(action)) { - handleActionCreateKey(intent); - } else if (ACTION_EDIT_KEY.equals(action)) { - handleActionEditKey(intent); - } - } - - /** - * Handle intent action to create new key - * - * @param intent - */ - private void handleActionCreateKey(Intent intent) { - Bundle extras = intent.getExtras(); - - mCurrentPassphrase = ""; - mIsBrandNewKeyring = true; - - if (extras != null) { - // if userId is given, prefill the fields - if (extras.containsKey(EXTRA_USER_IDS)) { - Log.d(Constants.TAG, "UserIds are given!"); - mUserIds.add(extras.getString(EXTRA_USER_IDS)); - } - - // if no passphrase is given - if (extras.containsKey(EXTRA_NO_PASSPHRASE)) { - boolean noPassphrase = extras.getBoolean(EXTRA_NO_PASSPHRASE); - if (noPassphrase) { - // check "no passphrase" checkbox and remove button - mNoPassphrase.setChecked(true); - mChangePassphrase.setVisibility(View.GONE); - } - } - - // generate key - if (extras.containsKey(EXTRA_GENERATE_DEFAULT_KEYS)) { - /* - boolean generateDefaultKeys = extras.getBoolean(EXTRA_GENERATE_DEFAULT_KEYS); - if (generateDefaultKeys) { - - // fill values for this action - Bundle data = new Bundle(); - data.putString(KeychainIntentService.GENERATE_KEY_SYMMETRIC_PASSPHRASE, - mCurrentPassphrase); - - serviceIntent.putExtra(KeychainIntentService.EXTRA_DATA, data); - - // Message is received after generating is done in KeychainIntentService - KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, getResources().getQuantityString(R.plurals.progress_generating, 1), - ProgressDialog.STYLE_HORIZONTAL, true, - - new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - // Stop key generation on cancel - stopService(serviceIntent); - EditKeyActivity.this.setResult(Activity.RESULT_CANCELED); - EditKeyActivity.this.finish(); - } - }) { - - @Override - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - // get new key from data bundle returned from service - Bundle data = message.getDataAsStringList(); - - ArrayList newKeys = - PgpConversionHelper.BytesToPGPSecretKeyList(data - .getByteArray(KeychainIntentService.RESULT_NEW_KEY)); - - ArrayList keyUsageFlags = data.getIntegerArrayList( - KeychainIntentService.RESULT_KEY_USAGES); - - if (newKeys.size() == keyUsageFlags.size()) { - for (int i = 0; i < newKeys.size(); ++i) { - mKeys.add(newKeys.get(i)); - mKeysUsages.add(keyUsageFlags.get(i)); - } - } - - buildLayout(true); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - serviceIntent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - - saveHandler.showProgressDialog(this); - - // start service with intent - startService(serviceIntent); - } - */ - } - } else { - buildLayout(false); - } - } - - /** - * Handle intent action to edit existing key - * - * @param intent - */ - private void handleActionEditKey(Intent intent) { - mDataUri = intent.getData(); - if (mDataUri == null) { - Log.e(Constants.TAG, "Intent data missing. Should be Uri of key!"); - finish(); - } else { - Log.d(Constants.TAG, "uri: " + mDataUri); - - try { - Uri secretUri = KeyRings.buildUnifiedKeyRingUri(mDataUri); - WrappedSecretKeyRing keyRing = new ProviderHelper(this).getWrappedSecretKeyRing(secretUri); - - mMasterCanSign = keyRing.getSubKey().canCertify(); - for (WrappedSecretKey key : keyRing.secretKeyIterator()) { - // Turn into uncached instance - mKeys.add(key.getUncached()); - mKeysUsages.add(key.getKeyUsage()); // get usage when view is created - } - - boolean isSet = false; - for (String userId : keyRing.getSubKey().getUserIds()) { - Log.d(Constants.TAG, "Added userId " + userId); - if (!isSet) { - isSet = true; - String[] parts = KeyRing.splitUserId(userId); - if (parts[0] != null) { - setTitle(parts[0]); - } - } - mUserIds.add(userId); - } - - buildLayout(false); - - mCurrentPassphrase = ""; - mIsPassphraseSet = keyRing.hasPassphrase(); - 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(); - } - - } - } - - /** - * Shows the dialog to set a new passphrase - */ - private void showSetPassphraseDialog() { - // Message is received after passphrase is cached - Handler returnHandler = new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == SetPassphraseDialogFragment.MESSAGE_OKAY) { - Bundle data = message.getData(); - - // set new returned passphrase! - mNewPassphrase = data - .getString(SetPassphraseDialogFragment.MESSAGE_NEW_PASSPHRASE); - - updatePassphraseButtonText(); - somethingChanged(); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(returnHandler); - - // set title based on isPassphraseSet() - int title; - if (isPassphraseSet()) { - title = R.string.title_change_passphrase; - } else { - title = R.string.title_set_passphrase; - } - - SetPassphraseDialogFragment setPassphraseDialog = SetPassphraseDialogFragment.newInstance( - messenger, null, title); - - setPassphraseDialog.show(getSupportFragmentManager(), "setPassphraseDialog"); - } - - /** - * Build layout based on mUserId, mKeys and mKeysUsages Vectors. It creates Views for every user - * id and key. - * - * @param newKeys - */ - private void buildLayout(boolean newKeys) { - setContentView(R.layout.edit_key_activity); - - // find views - mChangePassphrase = (Button) findViewById(R.id.edit_key_btn_change_passphrase); - mNoPassphrase = (CheckBox) findViewById(R.id.edit_key_no_passphrase); - // Build layout based on given userIds and keys - - LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - - LinearLayout container = (LinearLayout) findViewById(R.id.edit_key_container); - if (mIsPassphraseSet) { - mChangePassphrase.setText(getString(R.string.btn_change_passphrase)); - } - mUserIdsView = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false); - mUserIdsView.setType(SectionView.TYPE_USER_ID); - mUserIdsView.setCanBeEdited(mMasterCanSign); - mUserIdsView.setUserIds(mUserIds); - mUserIdsView.setEditorListener(this); - container.addView(mUserIdsView); - mKeysView = (SectionView) inflater.inflate(R.layout.edit_key_section, container, false); - mKeysView.setType(SectionView.TYPE_KEY); - mKeysView.setCanBeEdited(mMasterCanSign); - mKeysView.setKeys(mKeys, mKeysUsages, newKeys); - mKeysView.setEditorListener(this); - container.addView(mKeysView); - - updatePassphraseButtonText(); - - mChangePassphrase.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - showSetPassphraseDialog(); - } - }); - - // disable passphrase when no passphrase checkbox is checked! - mNoPassphrase.setOnCheckedChangeListener(new OnCheckedChangeListener() { - - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (isChecked) { - // remove passphrase - mSavedNewPassphrase = mNewPassphrase; - mNewPassphrase = ""; - mChangePassphrase.setVisibility(View.GONE); - } else { - mNewPassphrase = mSavedNewPassphrase; - mChangePassphrase.setVisibility(View.VISIBLE); - } - somethingChanged(); - } - }); - } - - private long getMasterKeyId() { - if (mKeysView.getEditors().getChildCount() == 0) { - return 0; - } - return ((KeyEditor) mKeysView.getEditors().getChildAt(0)).getValue().getKeyId(); - } - - public boolean isPassphraseSet() { - if (mNoPassphrase.isChecked()) { - return true; - } else if ((mIsPassphraseSet) - || (mNewPassphrase != null && !mNewPassphrase.equals(""))) { - return true; - } else { - return false; - } - } - - public boolean hasPassphraseChanged() { - if (mNoPassphrase != null) { - if (mNoPassphrase.isChecked()) { - return mIsPassphraseSet; - } else { - return (mNewPassphrase != null && !mNewPassphrase.equals("")); - } - } else { - return false; - } - } - - private void saveClicked() { - final long masterKeyId = getMasterKeyId(); - if (needsSaving()) { //make sure, as some versions don't support invalidateOptionsMenu - try { - if (!isPassphraseSet()) { - throw new PgpGeneralException(this.getString(R.string.set_a_passphrase)); - } - - String passphrase; - if (mIsPassphraseSet) { - passphrase = PassphraseCacheService.getCachedPassphrase(this, masterKeyId); - } else { - passphrase = ""; - } - if (passphrase == null) { - PassphraseDialogFragment.show(this, masterKeyId, - new Handler() { - @Override - public void handleMessage(Message message) { - if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) { - mCurrentPassphrase = PassphraseCacheService.getCachedPassphrase( - EditKeyActivityOld.this, masterKeyId); - checkEmptyIDsWanted(); - } - } - }); - } else { - mCurrentPassphrase = passphrase; - checkEmptyIDsWanted(); - } - } catch (PgpGeneralException e) { - AppMsg.makeText(this, getString(R.string.error_message, e.getMessage()), - AppMsg.STYLE_ALERT).show(); - } - } else { - AppMsg.makeText(this, R.string.error_change_something_first, AppMsg.STYLE_ALERT).show(); - } - } - - private void checkEmptyIDsWanted() { - try { - ArrayList userIDs = getUserIds(mUserIdsView); - List newIDs = mUserIdsView.getNewIDFlags(); - ArrayList originalIDs = mUserIdsView.getOriginalIDs(); - int curID = 0; - for (String userID : userIDs) { - if (userID.equals("") && (!userID.equals(originalIDs.get(curID)) || newIDs.get(curID))) { - CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder( - EditKeyActivityOld.this); - - alert.setIcon(R.drawable.ic_dialog_alert_holo_light); - alert.setTitle(R.string.warning); - alert.setMessage(EditKeyActivityOld.this.getString(R.string.ask_empty_id_ok)); - - alert.setPositiveButton(EditKeyActivityOld.this.getString(android.R.string.yes), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - finallySaveClicked(); - } - } - ); - alert.setNegativeButton(this.getString(android.R.string.no), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - } - } - ); - alert.setCancelable(false); - alert.show(); - return; - } - curID++; - } - } catch (PgpGeneralException e) { - Log.e(Constants.TAG, getString(R.string.error_message, e.getMessage())); - AppMsg.makeText(this, getString(R.string.error_message, e.getMessage()), AppMsg.STYLE_ALERT).show(); - } - finallySaveClicked(); - } - - private boolean[] toPrimitiveArray(final List booleanList) { - final boolean[] primitives = new boolean[booleanList.size()]; - int index = 0; - for (Boolean object : booleanList) { - primitives[index++] = object; - } - return primitives; - } - - private void finallySaveClicked() { - /* - try { - // Send all information needed to service to edit key in other thread - Intent intent = new Intent(this, KeychainIntentService.class); - - intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); - - OldSaveKeyringParcel saveParams = new OldSaveKeyringParcel(); - saveParams.userIds = getUserIds(mUserIdsView); - saveParams.originalIDs = mUserIdsView.getOriginalIDs(); - saveParams.deletedIDs = mUserIdsView.getDeletedIDs(); - saveParams.newIDs = toPrimitiveArray(mUserIdsView.getNewIDFlags()); - saveParams.primaryIDChanged = mUserIdsView.primaryChanged(); - saveParams.moddedKeys = toPrimitiveArray(mKeysView.getNeedsSavingArray()); - saveParams.deletedKeys = mKeysView.getDeletedKeys(); - saveParams.keysExpiryDates = getKeysExpiryDates(mKeysView); - saveParams.keysUsages = getKeysUsages(mKeysView); - saveParams.newPassphrase = mNewPassphrase; - saveParams.oldPassphrase = mCurrentPassphrase; - saveParams.newKeys = toPrimitiveArray(mKeysView.getNewKeysArray()); - saveParams.keys = getKeys(mKeysView); - saveParams.originalPrimaryID = mUserIdsView.getOriginalPrimaryID(); - - // fill values for this action - Bundle data = new Bundle(); - data.putBoolean(KeychainIntentService.SAVE_KEYRING_CAN_SIGN, mMasterCanSign); - data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, saveParams); - - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - - // Message is received after saving is done in KeychainIntentService - KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this, - getString(R.string.progress_saving), ProgressDialog.STYLE_HORIZONTAL) { - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - Intent data = new Intent(); - - // return uri pointing to new created key - Uri uri = KeyRings.buildGenericKeyRingUri(getMasterKeyId()); - data.setData(uri); - - setResult(RESULT_OK, data); - finish(); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - - saveHandler.showProgressDialog(this); - - // start service with intent - startService(intent); - } catch (PgpGeneralException e) { - Log.e(Constants.TAG, getString(R.string.error_message, e.getMessage())); - AppMsg.makeText(this, getString(R.string.error_message, e.getMessage()), - AppMsg.STYLE_ALERT).show(); - } - */ - } - - private void cancelClicked() { - if (needsSaving()) { //ask if we want to save - CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder( - EditKeyActivityOld.this); - - alert.setIcon(R.drawable.ic_dialog_alert_holo_light); - alert.setTitle(R.string.warning); - alert.setMessage(EditKeyActivityOld.this.getString(R.string.ask_save_changed_key)); - - alert.setPositiveButton(EditKeyActivityOld.this.getString(android.R.string.yes), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - saveClicked(); - } - }); - alert.setNegativeButton(this.getString(android.R.string.no), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - dialog.dismiss(); - setResult(RESULT_CANCELED); - finish(); - } - }); - alert.setCancelable(false); - alert.show(); - } else { - setResult(RESULT_CANCELED); - finish(); - } - } - - /** - * Returns user ids from the SectionView - * - * @param userIdsView - * @return - */ - private ArrayList getUserIds(SectionView userIdsView) throws PgpGeneralException { - ArrayList userIds = new ArrayList(); - - ViewGroup userIdEditors = userIdsView.getEditors(); - - boolean gotMainUserId = false; - for (int i = 0; i < userIdEditors.getChildCount(); ++i) { - UserIdEditor editor = (UserIdEditor) userIdEditors.getChildAt(i); - String userId; - userId = editor.getValue(); - - if (editor.isMainUserId()) { - userIds.add(0, userId); - gotMainUserId = true; - } else { - userIds.add(userId); - } - } - - if (userIds.size() == 0) { - throw new PgpGeneralException(getString(R.string.error_key_needs_a_user_id)); - } - - if (!gotMainUserId) { - throw new PgpGeneralException(getString(R.string.error_main_user_id_must_not_be_empty)); - } - - return userIds; - } - - /** - * Returns keys from the SectionView - * - * @param keysView - * @return - */ - private ArrayList getKeys(SectionView keysView) throws PgpGeneralException { - ArrayList keys = new ArrayList(); - - ViewGroup keyEditors = keysView.getEditors(); - - if (keyEditors.getChildCount() == 0) { - throw new PgpGeneralException(getString(R.string.error_key_needs_master_key)); - } - - for (int i = 0; i < keyEditors.getChildCount(); ++i) { - KeyEditor editor = (KeyEditor) keyEditors.getChildAt(i); - keys.add(editor.getValue()); - } - - return keys; - } - - /** - * Returns usage selections of keys from the SectionView - * - * @param keysView - * @return - */ - private ArrayList getKeysUsages(SectionView keysView) throws PgpGeneralException { - ArrayList keysUsages = new ArrayList(); - - ViewGroup keyEditors = keysView.getEditors(); - - if (keyEditors.getChildCount() == 0) { - throw new PgpGeneralException(getString(R.string.error_key_needs_master_key)); - } - - for (int i = 0; i < keyEditors.getChildCount(); ++i) { - KeyEditor editor = (KeyEditor) keyEditors.getChildAt(i); - keysUsages.add(editor.getUsage()); - } - - return keysUsages; - } - - private ArrayList getKeysExpiryDates(SectionView keysView) throws PgpGeneralException { - ArrayList keysExpiryDates = new ArrayList(); - - ViewGroup keyEditors = keysView.getEditors(); - - if (keyEditors.getChildCount() == 0) { - throw new PgpGeneralException(getString(R.string.error_key_needs_master_key)); - } - - for (int i = 0; i < keyEditors.getChildCount(); ++i) { - KeyEditor editor = (KeyEditor) keyEditors.getChildAt(i); - keysExpiryDates.add(editor.getExpiryDate()); - } - - return keysExpiryDates; - } - - private void updatePassphraseButtonText() { - mChangePassphrase.setText(isPassphraseSet() ? getString(R.string.btn_change_passphrase) - : getString(R.string.btn_set_passphrase)); - } - -} -- cgit v1.2.3 From 2c62aa90c0560c975d34b39df4379b5e1fcd6884 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 17:34:41 +0200 Subject: use SuperToast instead of AppMsg in code, part 1 --- .../service/KeychainIntentServiceHandler.java | 7 +++-- .../keychain/ui/CertifyKeyActivity.java | 18 +++++++------ .../keychain/ui/DecryptFileFragment.java | 10 ++----- .../keychain/ui/DecryptMessageFragment.java | 9 +++---- .../keychain/ui/EncryptFileFragment.java | 27 +++++++++---------- .../keychain/ui/EncryptMessageFragment.java | 17 +++++------- .../keychain/ui/KeyListActivity.java | 16 +++++------ .../keychain/ui/KeyListFragment.java | 15 +++++------ .../keychain/ui/ViewKeyActivity.java | 31 +++++++--------------- .../keychain/ui/ViewKeyMainFragment.java | 7 +++-- .../keychain/ui/ViewKeyShareFragment.java | 15 +++++------ .../ui/dialog/ShareQrCodeDialogFragment.java | 7 +++-- .../sufficientlysecure/keychain/util/Notify.java | 2 +- 13 files changed, 75 insertions(+), 106 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java index 755827482..0cdbe708e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentServiceHandler.java @@ -25,12 +25,11 @@ import android.os.Message; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.ui.dialog.ProgressDialogFragment; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; public class KeychainIntentServiceHandler extends Handler { @@ -102,9 +101,9 @@ public class KeychainIntentServiceHandler extends Handler { // show error from service if (data.containsKey(DATA_ERROR)) { - AppMsg.makeText(mActivity, + Notify.showNotify(mActivity, mActivity.getString(R.string.error_message, data.getString(DATA_ERROR)), - AppMsg.STYLE_ALERT).show(); + Notify.Style.ERROR); } break; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java index 7ac229b91..301b4ad40 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java @@ -41,8 +41,6 @@ import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; @@ -52,10 +50,12 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.util.ArrayList; @@ -246,8 +246,8 @@ public class CertifyKeyActivity extends ActionBarActivity implements // Bail out if there is not at least one user id selected ArrayList userIds = mUserIdsAdapter.getSelectedUserIds(); if (userIds.isEmpty()) { - AppMsg.makeText(CertifyKeyActivity.this, "No User IDs to sign selected!", - AppMsg.STYLE_ALERT).show(); + Notify.showNotify(CertifyKeyActivity.this, "No Notify.Style IDs to sign selected!", + Notify.Style.ERROR); return; } @@ -274,8 +274,8 @@ public class CertifyKeyActivity extends ActionBarActivity implements if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - AppMsg.makeText(CertifyKeyActivity.this, R.string.key_certify_success, - AppMsg.STYLE_INFO).show(); + Notify.showNotify(CertifyKeyActivity.this, R.string.key_certify_success, + Notify.Style.INFO); // check if we need to send the key to the server or not if (mUploadKeyCheckbox.isChecked()) { @@ -327,8 +327,10 @@ public class CertifyKeyActivity extends ActionBarActivity implements super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - AppMsg.makeText(CertifyKeyActivity.this, R.string.key_send_success, - AppMsg.STYLE_INFO).show(); + Intent intent = new Intent(); + intent.putExtra(OperationResultParcel.EXTRA_RESULT, message.getData()); + Notify.showNotify(CertifyKeyActivity.this, R.string.key_send_success, + Notify.Style.INFO); setResult(RESULT_OK); finish(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFileFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFileFragment.java index c12b5b7be..56dfdbd95 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFileFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFileFragment.java @@ -34,8 +34,6 @@ import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageButton; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; @@ -129,7 +127,6 @@ public class DecryptFileFragment extends DecryptFragment { } if (mInputFilename.equals("")) { - //AppMsg.makeText(getActivity(), R.string.no_file_selected, AppMsg.STYLE_ALERT).show(); Notify.showNotify(getActivity(), R.string.no_file_selected, Notify.Style.ERROR); return; } @@ -137,11 +134,8 @@ public class DecryptFileFragment extends DecryptFragment { if (mInputUri == null && mInputFilename.startsWith("file")) { File file = new File(mInputFilename); if (!file.exists() || !file.isFile()) { - AppMsg.makeText( - getActivity(), - getString(R.string.error_message, - getString(R.string.error_file_not_found)), AppMsg.STYLE_ALERT) - .show(); + Notify.showNotify(getActivity(), getString(R.string.error_message, + getString(R.string.error_file_not_found)), Notify.Style.ERROR); return; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptMessageFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptMessageFragment.java index 46462f924..cf7a0b4b8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptMessageFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptMessageFragment.java @@ -28,8 +28,6 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.EditText; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; @@ -38,6 +36,7 @@ import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.util.regex.Matcher; @@ -107,12 +106,10 @@ public class DecryptMessageFragment extends DecryptFragment { mCiphertext = matcher.group(1); decryptStart(null); } else { - AppMsg.makeText(getActivity(), R.string.error_invalid_data, AppMsg.STYLE_ALERT) - .show(); + Notify.showNotify(getActivity(), R.string.error_invalid_data, Notify.Style.ERROR); } } else { - AppMsg.makeText(getActivity(), R.string.error_invalid_data, AppMsg.STYLE_ALERT) - .show(); + Notify.showNotify(getActivity(), R.string.error_invalid_data, Notify.Style.ERROR); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java index f5d89d186..6ff8d421b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java @@ -37,8 +37,6 @@ import android.widget.EditText; import android.widget.ImageButton; import android.widget.Spinner; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; @@ -51,6 +49,7 @@ import org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Choice; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.io.File; @@ -218,18 +217,18 @@ public class EncryptFileFragment extends Fragment { } if (mInputFilename.equals("")) { - AppMsg.makeText(getActivity(), R.string.no_file_selected, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.no_file_selected, Notify.Style.ERROR); return; } if (mInputUri == null && !mInputFilename.startsWith("content")) { File file = new File(mInputFilename); if (!file.exists() || !file.isFile()) { - AppMsg.makeText( + Notify.showNotify( getActivity(), getString(R.string.error_message, - getString(R.string.error_file_not_found)), AppMsg.STYLE_ALERT) - .show(); + getString(R.string.error_file_not_found)), Notify.Style.ERROR + ); return; } } @@ -240,13 +239,13 @@ public class EncryptFileFragment extends Fragment { boolean gotPassphrase = (mEncryptInterface.getPassphrase() != null && mEncryptInterface.getPassphrase().length() != 0); if (!gotPassphrase) { - AppMsg.makeText(getActivity(), R.string.passphrase_must_not_be_empty, AppMsg.STYLE_ALERT) - .show(); + Notify.showNotify(getActivity(), R.string.passphrase_must_not_be_empty, Notify.Style.ERROR) + ; return; } if (!mEncryptInterface.getPassphrase().equals(mEncryptInterface.getPassphraseAgain())) { - AppMsg.makeText(getActivity(), R.string.passphrases_do_not_match, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.passphrases_do_not_match, Notify.Style.ERROR); return; } } else { @@ -256,13 +255,13 @@ public class EncryptFileFragment extends Fragment { && mEncryptInterface.getEncryptionKeys().length > 0); if (!gotEncryptionKeys) { - AppMsg.makeText(getActivity(), R.string.select_encryption_key, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.select_encryption_key, Notify.Style.ERROR); return; } if (!gotEncryptionKeys && mEncryptInterface.getSignatureKey() == 0) { - AppMsg.makeText(getActivity(), R.string.select_encryption_or_signature_key, - AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.select_encryption_or_signature_key, + Notify.Style.ERROR); return; } @@ -345,8 +344,8 @@ public class EncryptFileFragment extends Fragment { super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - AppMsg.makeText(getActivity(), R.string.encrypt_sign_successful, - AppMsg.STYLE_INFO).show(); + Notify.showNotify(getActivity(), R.string.encrypt_sign_successful, + Notify.Style.INFO); if (mDeleteAfter.isChecked()) { // Create and show dialog to delete original file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptMessageFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptMessageFragment.java index 8a6103b16..e1760b4ed 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptMessageFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptMessageFragment.java @@ -30,8 +30,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; @@ -41,6 +39,7 @@ import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; public class EncryptMessageFragment extends Fragment { public static final String ARG_TEXT = "text"; @@ -126,13 +125,12 @@ public class EncryptMessageFragment extends Fragment { boolean gotPassphrase = (mEncryptInterface.getPassphrase() != null && mEncryptInterface.getPassphrase().length() != 0); if (!gotPassphrase) { - AppMsg.makeText(getActivity(), R.string.passphrase_must_not_be_empty, AppMsg.STYLE_ALERT) - .show(); + Notify.showNotify(getActivity(), R.string.passphrase_must_not_be_empty, Notify.Style.ERROR); return; } if (!mEncryptInterface.getPassphrase().equals(mEncryptInterface.getPassphraseAgain())) { - AppMsg.makeText(getActivity(), R.string.passphrases_do_not_match, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.passphrases_do_not_match, Notify.Style.ERROR); return; } @@ -143,8 +141,8 @@ public class EncryptMessageFragment extends Fragment { && mEncryptInterface.getEncryptionKeys().length > 0); if (!gotEncryptionKeys && mEncryptInterface.getSignatureKey() == 0) { - AppMsg.makeText(getActivity(), R.string.select_encryption_or_signature_key, - AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.select_encryption_or_signature_key, + Notify.Style.ERROR); return; } @@ -226,9 +224,8 @@ public class EncryptMessageFragment extends Fragment { if (toClipboard) { ClipboardReflection.copyToClipboard(getActivity(), output); - AppMsg.makeText(getActivity(), - R.string.encrypt_sign_clipboard_successful, AppMsg.STYLE_INFO) - .show(); + Notify.showNotify(getActivity(), + R.string.encrypt_sign_clipboard_successful, Notify.Style.INFO); } else { Intent sendIntent = new Intent(Intent.ACTION_SEND); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 849576284..c219fb98c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -25,15 +25,11 @@ import android.os.Messenger; import android.view.Menu; import android.view.MenuItem; -import com.devspark.appmsg.AppMsg; - import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.choice.algorithm; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; -import org.sufficientlysecure.keychain.helper.OtherHelper; -import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.service.KeychainIntentService; @@ -41,9 +37,9 @@ import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.io.IOException; -import java.util.ArrayList; public class KeyListActivity extends DrawerActivity { @@ -96,21 +92,21 @@ public class KeyListActivity extends DrawerActivity { case R.id.menu_key_list_debug_read: try { KeychainDatabase.debugRead(this); - AppMsg.makeText(this, "Restored from backup", AppMsg.STYLE_CONFIRM).show(); + Notify.showNotify(this, "Restored Notify.Style backup", Notify.Style.INFO); getContentResolver().notifyChange(KeychainContract.KeyRings.CONTENT_URI, null); } catch(IOException e) { Log.e(Constants.TAG, "IO Error", e); - AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); + Notify.showNotify(this, "IO Notify.Style: " + e.getMessage(), Notify.Style.ERROR); } return true; case R.id.menu_key_list_debug_write: try { KeychainDatabase.debugWrite(this); - AppMsg.makeText(this, "Backup successful", AppMsg.STYLE_CONFIRM).show(); + Notify.showNotify(this, "Backup Notify.Style", Notify.Style.INFO); } catch(IOException e) { Log.e(Constants.TAG, "IO Error", e); - AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); + Notify.showNotify(this, "IO Notify.Style: " + e.getMessage(), Notify.Style.ERROR); } return true; @@ -171,4 +167,4 @@ public class KeyListActivity extends DrawerActivity { startService(intent); } -} \ No newline at end of file +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 9d0a80406..c8f6b7b1e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -52,8 +52,6 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Button; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; @@ -62,6 +60,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.util.Highlighter; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.util.Date; import java.util.HashMap; @@ -104,10 +103,10 @@ public class KeyListFragment extends LoaderFragment @Override public void onClick(View v) { - Intent intent = new Intent(getActivity(), EditKeyActivityOld.class); - intent.setAction(EditKeyActivityOld.ACTION_CREATE_KEY); - intent.putExtra(EditKeyActivityOld.EXTRA_GENERATE_DEFAULT_KEYS, true); - intent.putExtra(EditKeyActivityOld.EXTRA_USER_IDS, ""); // show user id view + Intent intent = new Intent(getActivity(), EditKeyActivity.class); + intent.setAction(EditKeyActivity.ACTION_CREATE_KEY); + intent.putExtra(EditKeyActivity.EXTRA_GENERATE_DEFAULT_KEYS, true); + intent.putExtra(EditKeyActivity.EXTRA_USER_IDS, ""); // show user id view startActivityForResult(intent, 0); } }); @@ -339,8 +338,8 @@ public class KeyListFragment extends LoaderFragment public void showDeleteKeyDialog(final ActionMode mode, long[] masterKeyIds, boolean hasSecret) { // Can only work on singular secret keys if(hasSecret && masterKeyIds.length > 1) { - AppMsg.makeText(getActivity(), R.string.secret_cannot_multiple, - AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.secret_cannot_multiple, + Notify.Style.ERROR); return; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index a9cd0976b..b8b2542ce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -44,8 +44,6 @@ import android.view.MenuItem; import android.view.View; import android.view.Window; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ContactHelper; @@ -54,11 +52,13 @@ import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter; import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout.TabColorizer; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout; +import org.sufficientlysecure.keychain.util.Notify; import java.util.Date; import java.util.HashMap; @@ -295,7 +295,7 @@ public class ViewKeyActivity extends ActionBarActivity implements } } } catch (ProviderHelper.NotFoundException e) { - AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(this, R.string.error_key_not_found, Notify.Style.ERROR); Log.e(Constants.TAG, "Key not found", e); } return super.onOptionsItemSelected(item); @@ -352,22 +352,11 @@ public class ViewKeyActivity extends ActionBarActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - switch (requestCode) { - case REQUEST_CODE_LOOKUP_KEY: { - if (resultCode == Activity.RESULT_OK) { - ImportResult result = data.getParcelableExtra(ImportKeysActivity.EXTRA_RESULT); - if (result != null) { - result.displayNotify(this); - } - } - break; - } - - default: { - super.onActivityResult(requestCode, resultCode, data); - - break; - } + if (data.hasExtra(OperationResultParcel.EXTRA_RESULT)) { + OperationResultParcel result = data.getParcelableExtra(OperationResultParcel.EXTRA_RESULT); + result.createNotify(this).show(); + } else { + super.onActivityResult(requestCode, resultCode, data); } } @@ -455,8 +444,8 @@ public class ViewKeyActivity extends ActionBarActivity implements public void handleMessage(Message msg) { switch (msg.what) { case NFC_SENT: - AppMsg.makeText(ViewKeyActivity.this, R.string.nfc_successful, - AppMsg.STYLE_INFO).show(); + Notify.showNotify( + ViewKeyActivity.this, R.string.nfc_successful, Notify.Style.INFO); break; } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index f0636cf2c..2a0f518d8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -29,8 +29,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ListView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; @@ -40,6 +38,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import java.util.Date; @@ -225,7 +224,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements private void encrypt(Uri dataUri) { // If there is no encryption key, don't bother. if (!mHasEncrypt) { - AppMsg.makeText(getActivity(), R.string.error_no_encrypt_subkey, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_no_encrypt_subkey, Notify.Style.ERROR); return; } try { @@ -246,7 +245,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements private void certify(Uri dataUri) { Intent signIntent = new Intent(getActivity(), CertifyKeyActivity.class); signIntent.setData(dataUri); - startActivity(signIntent); + startActivityForResult(signIntent, 0); } private void editKey(Uri dataUri) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java index 52b573f47..c2339c25f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java @@ -33,8 +33,6 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.ClipboardReflection; @@ -47,6 +45,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.dialog.ShareNfcDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.ShareQrCodeDialogFragment; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import org.sufficientlysecure.keychain.util.QrCodeUtils; import java.io.IOException; @@ -171,13 +170,13 @@ public class ViewKeyShareFragment extends LoaderFragment implements } else { message = getResources().getString(R.string.key_copied_to_clipboard); } - AppMsg.makeText(getActivity(), message, AppMsg.STYLE_INFO).show(); + Notify.showNotify(getActivity(), message, Notify.Style.OK); } else { // Android will fail with android.os.TransactionTooLargeException if key is too big // see http://www.lonestarprod.com/?p=34 if (content.length() >= 86389) { - AppMsg.makeText(getActivity(), R.string.key_too_big_for_sharing, - AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.key_too_big_for_sharing, + Notify.Style.ERROR); return; } @@ -195,13 +194,13 @@ public class ViewKeyShareFragment extends LoaderFragment implements } } catch (PgpGeneralException e) { Log.e(Constants.TAG, "error processing key!", e); - AppMsg.makeText(getActivity(), R.string.error_key_processing, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_key_processing, Notify.Style.ERROR); } catch (IOException e) { Log.e(Constants.TAG, "error processing key!", e); - AppMsg.makeText(getActivity(), R.string.error_key_processing, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_key_processing, Notify.Style.ERROR); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); - AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_key_not_found, Notify.Style.ERROR); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java index 24608784b..eb779f401 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java @@ -27,14 +27,13 @@ import android.view.View; import android.widget.ImageView; import android.widget.TextView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; import org.sufficientlysecure.keychain.util.QrCodeUtils; public class ShareQrCodeDialogFragment extends DialogFragment { @@ -87,7 +86,7 @@ public class ShareQrCodeDialogFragment extends DialogFragment { KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); if (blob == null) { Log.e(Constants.TAG, "key not found!"); - AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_key_not_found, Notify.Style.ERROR); return null; } @@ -97,7 +96,7 @@ public class ShareQrCodeDialogFragment extends DialogFragment { setQrCode(content); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); - AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(getActivity(), R.string.error_key_not_found, Notify.Style.ERROR); return null; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Notify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Notify.java index 67f81fb24..22e3f5d66 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Notify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/Notify.java @@ -29,7 +29,7 @@ import com.github.johnpersano.supertoasts.SuperToast; */ public class Notify { - public static enum Style {OK, WARN, ERROR} + public static enum Style {OK, WARN, INFO, ERROR} /** * Shows a simple in-layout notification with the CharSequence given as parameter -- cgit v1.2.3 From d8f278229352a58dbdcf49bb65538249f8c4ccc0 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 17:35:23 +0200 Subject: use SuperToast instead of AppMsg in code, part 2 --- .../keychain/service/OperationResultParcel.java | 60 ++++++++++++++++++++++ .../keychain/service/OperationResults.java | 7 +-- 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 6bf6b655d..1df2a38f1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -1,10 +1,20 @@ package org.sufficientlysecure.keychain.service; +import android.app.Activity; +import android.content.Intent; import android.os.Parcel; import android.os.Parcelable; +import android.view.View; + +import com.github.johnpersano.supertoasts.SuperCardToast; +import com.github.johnpersano.supertoasts.SuperToast; +import com.github.johnpersano.supertoasts.util.OnClickWrapper; +import com.github.johnpersano.supertoasts.util.Style; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.ui.LogDisplayActivity; +import org.sufficientlysecure.keychain.ui.LogDisplayFragment; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; @@ -21,6 +31,9 @@ import java.util.ArrayList; * */ public class OperationResultParcel implements Parcelable { + + public static final String EXTRA_RESULT = "operation_result"; + /** Holds the overall result, the number specifying varying degrees of success. The first bit * is 0 on overall success, 1 on overall failure. All other bits may be used for more specific * conditions. */ @@ -104,6 +117,43 @@ public class OperationResultParcel implements Parcelable { } + public SuperCardToast createNotify(final Activity activity) { + + int resultType = getResult(); + + String str; + int duration, color; + + // Not an overall failure + if ((resultType & OperationResultParcel.RESULT_ERROR) == 0) { + + if (getLog().containsWarnings()) { + duration = 0; + color = Style.ORANGE; + } else { + duration = SuperToast.Duration.LONG; + color = Style.GREEN; + } + + str = activity.getString(R.string.import_error); + + } else { + duration = 0; + color = Style.RED; + str = activity.getString(R.string.import_error); + } + + SuperCardToast toast = new SuperCardToast(activity, + SuperToast.Type.STANDARD, Style.getStyle(color, SuperToast.Animations.POPUP)); + toast.setText(str); + toast.setDuration(duration); + toast.setIndeterminate(duration == 0); + toast.setSwipeToDismiss(true); + + return toast; + + } + /** This is an enum of all possible log events. * * Element names should generally be prefixed with MSG_XX_ where XX is an @@ -123,6 +173,8 @@ public class OperationResultParcel implements Parcelable { */ public static enum LogType { + INTERNAL_ERROR (R.string.internal_error), + // import public MSG_IP(R.string.msg_ip), MSG_IP_APPLY_BATCH (R.string.msg_ip_apply_batch), @@ -313,6 +365,14 @@ public class OperationResultParcel implements Parcelable { add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } + public LogEntryParcel getResultId() { + LogEntryParcel entry = get(size()-1); + if (entry.mLevel != LogLevel.OK && entry.mLevel != LogLevel.ERROR) { + return new LogEntryParcel(LogLevel.ERROR, LogType.INTERNAL_ERROR, 0); + } + return entry; + } + public boolean containsWarnings() { for(LogEntryParcel entry : new IterableIterator(iterator())) { if (entry.mLevel == LogLevel.WARN || entry.mLevel == LogLevel.ERROR) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index fd3d4ed00..39c11a855 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -80,7 +80,7 @@ public abstract class OperationResults { } }; - public void displayNotify(final Activity activity) { + public SuperCardToast createNotify(final Activity activity) { int resultType = getResult(); @@ -88,7 +88,7 @@ public abstract class OperationResults { int duration, color; // Not an overall failure - if ((resultType & ImportResult.RESULT_ERROR) == 0) { + if ((resultType & OperationResultParcel.RESULT_ERROR) == 0) { String withWarnings; // Any warnings? @@ -157,7 +157,8 @@ public abstract class OperationResults { } )); } - toast.show(); + + return toast; } -- cgit v1.2.3 From 23524af81d6297f7b5a182feba093172653b0045 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:30:08 +0200 Subject: add diffKeyrings method --- .../keychain/testsupport/KeyringTestingHelper.java | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index a565f0707..da0f47e99 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -2,12 +2,18 @@ package org.sufficientlysecure.keychain.testsupport; import android.content.Context; +import org.spongycastle.util.Arrays; import org.sufficientlysecure.keychain.pgp.NullProgressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.OperationResults; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; /** * Helper for tests of the Keyring import in ProviderHelper. @@ -44,6 +50,132 @@ public class KeyringTestingHelper { return saveSuccess; } + public static class Packet { + int tag; + int length; + byte[] buf; + + public boolean equals(Object other) { + return other instanceof Packet && Arrays.areEqual(this.buf, ((Packet) other).buf); + } + + public int hashCode() { + System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); + return Arrays.hashCode(buf); + } + } + + public static boolean diffKeyrings(byte[] ringA, byte[] ringB, Set onlyA, Set onlyB) + throws IOException { + InputStream streamA = new ByteArrayInputStream(ringA); + InputStream streamB = new ByteArrayInputStream(ringB); + + HashSet a = new HashSet(), b = new HashSet(); + + Packet p; + while(true) { + p = readPacket(streamA); + if (p == null) { + break; + } + a.add(p); + } + while(true) { + p = readPacket(streamB); + if (p == null) { + break; + } + b.add(p); + } + + onlyA.addAll(a); + onlyA.removeAll(b); + onlyB.addAll(b); + onlyB.removeAll(a); + + return onlyA.isEmpty() && onlyB.isEmpty(); + } + + private static Packet readPacket(InputStream in) throws IOException { + + // save here. this is tag + length, max 6 bytes + in.mark(6); + + int hdr = in.read(); + int headerLength = 1; + + if (hdr < 0) { + return null; + } + + if ((hdr & 0x80) == 0) { + throw new IOException("invalid header encountered"); + } + + boolean newPacket = (hdr & 0x40) != 0; + int tag = 0; + int bodyLen = 0; + + if (newPacket) { + tag = hdr & 0x3f; + + int l = in.read(); + headerLength += 1; + + if (l < 192) { + bodyLen = l; + } else if (l <= 223) { + int b = in.read(); + headerLength += 1; + + bodyLen = ((l - 192) << 8) + (b) + 192; + } else if (l == 255) { + bodyLen = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read(); + headerLength += 4; + } else { + // bodyLen = 1 << (l & 0x1f); + throw new IOException("no support for partial bodies in test classes"); + } + } else { + int lengthType = hdr & 0x3; + + tag = (hdr & 0x3f) >> 2; + + switch (lengthType) { + case 0: + bodyLen = in.read(); + headerLength += 1; + break; + case 1: + bodyLen = (in.read() << 8) | in.read(); + headerLength += 2; + break; + case 2: + bodyLen = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read(); + headerLength += 4; + break; + case 3: + // bodyLen = 1 << (l & 0x1f); + throw new IOException("no support for partial bodies in test classes"); + default: + throw new IOException("unknown length type encountered"); + } + } + + in.reset(); + + // read the entire packet INCLUDING the header here + byte[] buf = new byte[headerLength+bodyLen]; + if (in.read(buf) != headerLength+bodyLen) { + throw new IOException("read length mismatch!"); + } + Packet p = new Packet(); + p.tag = tag; + p.length = bodyLen; + p.buf = buf; + return p; + + } private void retrieveKeyAndExpectNotFound(ProviderHelper providerHelper, long masterKeyId) { try { @@ -53,4 +185,5 @@ public class KeyringTestingHelper { // good } } + } -- cgit v1.2.3 From 9320d2d8a204496ace8f973a59594ccd698a2170 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:53:41 +0200 Subject: use KeyringTestHelper.diffKeyrings method for unit test --- .../sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index da0f47e99..768f2f6c4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -60,7 +60,7 @@ public class KeyringTestingHelper { } public int hashCode() { - System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); + // System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); return Arrays.hashCode(buf); } } -- cgit v1.2.3 From 83e5a3d341ef35c37e39ac9102eef5f9d2a3106f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:30:08 +0200 Subject: add diffKeyrings method --- .../keychain/testsupport/KeyringTestingHelper.java | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index a565f0707..da0f47e99 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -2,12 +2,18 @@ package org.sufficientlysecure.keychain.testsupport; import android.content.Context; +import org.spongycastle.util.Arrays; import org.sufficientlysecure.keychain.pgp.NullProgressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.OperationResults; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; import java.util.Collection; +import java.util.HashSet; +import java.util.Set; /** * Helper for tests of the Keyring import in ProviderHelper. @@ -44,6 +50,132 @@ public class KeyringTestingHelper { return saveSuccess; } + public static class Packet { + int tag; + int length; + byte[] buf; + + public boolean equals(Object other) { + return other instanceof Packet && Arrays.areEqual(this.buf, ((Packet) other).buf); + } + + public int hashCode() { + System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); + return Arrays.hashCode(buf); + } + } + + public static boolean diffKeyrings(byte[] ringA, byte[] ringB, Set onlyA, Set onlyB) + throws IOException { + InputStream streamA = new ByteArrayInputStream(ringA); + InputStream streamB = new ByteArrayInputStream(ringB); + + HashSet a = new HashSet(), b = new HashSet(); + + Packet p; + while(true) { + p = readPacket(streamA); + if (p == null) { + break; + } + a.add(p); + } + while(true) { + p = readPacket(streamB); + if (p == null) { + break; + } + b.add(p); + } + + onlyA.addAll(a); + onlyA.removeAll(b); + onlyB.addAll(b); + onlyB.removeAll(a); + + return onlyA.isEmpty() && onlyB.isEmpty(); + } + + private static Packet readPacket(InputStream in) throws IOException { + + // save here. this is tag + length, max 6 bytes + in.mark(6); + + int hdr = in.read(); + int headerLength = 1; + + if (hdr < 0) { + return null; + } + + if ((hdr & 0x80) == 0) { + throw new IOException("invalid header encountered"); + } + + boolean newPacket = (hdr & 0x40) != 0; + int tag = 0; + int bodyLen = 0; + + if (newPacket) { + tag = hdr & 0x3f; + + int l = in.read(); + headerLength += 1; + + if (l < 192) { + bodyLen = l; + } else if (l <= 223) { + int b = in.read(); + headerLength += 1; + + bodyLen = ((l - 192) << 8) + (b) + 192; + } else if (l == 255) { + bodyLen = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read(); + headerLength += 4; + } else { + // bodyLen = 1 << (l & 0x1f); + throw new IOException("no support for partial bodies in test classes"); + } + } else { + int lengthType = hdr & 0x3; + + tag = (hdr & 0x3f) >> 2; + + switch (lengthType) { + case 0: + bodyLen = in.read(); + headerLength += 1; + break; + case 1: + bodyLen = (in.read() << 8) | in.read(); + headerLength += 2; + break; + case 2: + bodyLen = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read(); + headerLength += 4; + break; + case 3: + // bodyLen = 1 << (l & 0x1f); + throw new IOException("no support for partial bodies in test classes"); + default: + throw new IOException("unknown length type encountered"); + } + } + + in.reset(); + + // read the entire packet INCLUDING the header here + byte[] buf = new byte[headerLength+bodyLen]; + if (in.read(buf) != headerLength+bodyLen) { + throw new IOException("read length mismatch!"); + } + Packet p = new Packet(); + p.tag = tag; + p.length = bodyLen; + p.buf = buf; + return p; + + } private void retrieveKeyAndExpectNotFound(ProviderHelper providerHelper, long masterKeyId) { try { @@ -53,4 +185,5 @@ public class KeyringTestingHelper { // good } } + } -- cgit v1.2.3 From 9971f9ad4cf0c06bb6ab22f9cee16f1a91370365 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:53:41 +0200 Subject: use KeyringTestHelper.diffKeyrings method for unit test Conflicts: OpenKeychain/src/test/java/tests/UncachedKeyringTest.java --- .../sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index da0f47e99..768f2f6c4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -60,7 +60,7 @@ public class KeyringTestingHelper { } public int hashCode() { - System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); + // System.out.println("tag: " + tag + ", code: " + Arrays.hashCode(buf)); return Arrays.hashCode(buf); } } -- cgit v1.2.3 From 51bedc2e73da3fe0022ee3339723c3b351ccd5b9 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:31:32 +0100 Subject: (c) headers, tidy imports --- .../keychain/testsupport/KeyringBuilder.java | 17 +++++++++++++++ .../keychain/testsupport/KeyringTestingHelper.java | 16 +++++++++++++++ .../testsupport/PgpVerifyTestingHelper.java | 16 +++++++++++++++ .../keychain/testsupport/ProviderHelperStub.java | 17 +++++++++++++++ .../keychain/testsupport/TestDataUtil.java | 17 +++++++++++++++ .../testsupport/UncachedKeyringTestingHelper.java | 24 +++++++++++++++------- 6 files changed, 100 insertions(+), 7 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java index f618d8de9..1672e9c81 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringBuilder.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) Art O Cathain + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.testsupport; import org.spongycastle.bcpg.CompressionAlgorithmTags; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java index 768f2f6c4..d2a945185 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/KeyringTestingHelper.java @@ -1,3 +1,19 @@ +/* + * Copyright (C) Art O Cathain, Vincent Breitmoser + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package org.sufficientlysecure.keychain.testsupport; import android.content.Context; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java index 1ab5878cc..19c125319 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/PgpVerifyTestingHelper.java @@ -1,4 +1,20 @@ package org.sufficientlysecure.keychain.testsupport; +/* + * Copyright (C) Art O Cathain + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ import android.content.Context; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/ProviderHelperStub.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/ProviderHelperStub.java index c6d834bf9..0f73d4264 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/ProviderHelperStub.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/ProviderHelperStub.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) Art O Cathain + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.testsupport; import android.content.Context; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java index c08b60d1a..e823c10fd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/TestDataUtil.java @@ -1,3 +1,20 @@ +/* + * Copyright (C) Art O Cathain + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.testsupport; import org.spongycastle.bcpg.ContainedPacket; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java index ac4955715..8e4a7b98a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -1,25 +1,35 @@ +/* + * Copyright (C) Art O Cathain + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package org.sufficientlysecure.keychain.testsupport; import org.spongycastle.bcpg.BCPGKey; -import org.spongycastle.bcpg.PublicKeyAlgorithmTags; import org.spongycastle.bcpg.PublicKeyPacket; -import org.spongycastle.bcpg.RSAPublicBCPGKey; import org.spongycastle.bcpg.SignatureSubpacket; import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPPublicKeyRing; import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.PGPUserAttributeSubpacketVector; -import org.spongycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; import org.sufficientlysecure.keychain.service.OperationResultParcel; -import java.math.BigInteger; import java.util.Arrays; -import java.util.Date; -import java.util.Objects; /** * Created by art on 28/06/14. -- cgit v1.2.3 From 37433bd2823b42aa4b5047b605b517cb9d7f4932 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:37:48 +0100 Subject: prevent odd ambiguous method toString error --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java index 8e4a7b98a..bb1afaf4b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -41,7 +41,7 @@ public class UncachedKeyringTestingHelper { UncachedKeyRing canonicalized = keyRing1.canonicalize(operationLog, 0); if (canonicalized == null) { - throw new AssertionError("Canonicalization failed; messages: [" + operationLog.toString() + "]"); + throw new AssertionError("Canonicalization failed; messages: [" + operationLog + "]"); } return TestDataUtil.iterEquals(canonicalized.getPublicKeys(), keyRing2.getPublicKeys(), new -- cgit v1.2.3 From 78b0c5e74a13aa33ccbb7cbfff86f7a02d977429 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:38:49 +0100 Subject: actually provide a tostring --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java index bb1afaf4b..7a493ecf6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/testsupport/UncachedKeyringTestingHelper.java @@ -41,7 +41,7 @@ public class UncachedKeyringTestingHelper { UncachedKeyRing canonicalized = keyRing1.canonicalize(operationLog, 0); if (canonicalized == null) { - throw new AssertionError("Canonicalization failed; messages: [" + operationLog + "]"); + throw new AssertionError("Canonicalization failed; messages: [" + operationLog.toList() + "]"); } return TestDataUtil.iterEquals(canonicalized.getPublicKeys(), keyRing2.getPublicKeys(), new -- cgit v1.2.3 From 5adcb7885cf9629b1ef60b26e76a4c9a8b1f7845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 8 Jul 2014 03:34:27 +0200 Subject: Work on subkeys adapter --- .../keychain/ui/EditKeyFragment.java | 51 ++++++++- .../keychain/ui/adapter/SubkeysAdapter.java | 127 +++++++++++++-------- .../keychain/ui/adapter/UserIdsAdapter.java | 10 +- .../ui/dialog/EditSubkeyDialogFragment.java | 110 ++++++++++++++++++ 4 files changed, 246 insertions(+), 52 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/EditSubkeyDialogFragment.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index c76dc0164..78ccafcbc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -54,6 +54,7 @@ import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter; +import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditUserIdDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; @@ -214,9 +215,17 @@ public class EditKeyFragment extends LoaderFragment implements mUserIdsAddedAdapter = new UserIdsAddedAdapter(getActivity(), mUserIdsAddedData); mUserIdsAddedList.setAdapter(mUserIdsAddedAdapter); - mSubkeysAdapter = new SubkeysAdapter(getActivity(), null, 0); + mSubkeysAdapter = new SubkeysAdapter(getActivity(), null, 0, mSaveKeyringParcel); mSubkeysList.setAdapter(mSubkeysAdapter); + mSubkeysList.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + long keyId = mSubkeysAdapter.getKeyId(position); + editSubkey(keyId); + } + }); + mSubkeysAddedAdapter = new SubkeysAddedAdapter(getActivity(), mSaveKeyringParcel.addSubKeys); mSubkeysAddedList.setAdapter(mSubkeysAddedAdapter); @@ -342,6 +351,46 @@ public class EditKeyFragment extends LoaderFragment implements }); } + private void editSubkey(final long keyId) { + Handler returnHandler = new Handler() { + @Override + public void handleMessage(Message message) { + switch (message.what) { + case EditSubkeyDialogFragment.MESSAGE_CHANGE_EXPIRY: + // toggle +// if (mSaveKeyringParcel.changePrimaryUserId != null +// && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { +// mSaveKeyringParcel.changePrimaryUserId = null; +// } else { +// mSaveKeyringParcel.changePrimaryUserId = userId; +// } + break; + case EditSubkeyDialogFragment.MESSAGE_REVOKE: + // toggle + if (mSaveKeyringParcel.revokeSubKeys.contains(keyId)) { + mSaveKeyringParcel.revokeSubKeys.remove(keyId); + } else { + mSaveKeyringParcel.revokeSubKeys.add(keyId); + } + break; + } + getLoaderManager().getLoader(LOADER_ID_SUBKEYS).forceLoad(); + } + }; + + // Create a new Messenger for the communication back + final Messenger messenger = new Messenger(returnHandler); + + DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() { + public void run() { + EditSubkeyDialogFragment dialogFragment = + EditSubkeyDialogFragment.newInstance(messenger); + + dialogFragment.show(getActivity().getSupportFragmentManager(), "editSubkeyDialog"); + } + }); + } + private void addUserId() { mUserIdsAddedAdapter.add(new UserIdsAddedAdapter.UserIdModel()); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java index 02b1f31e2..ccc5960c8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SubkeysAdapter.java @@ -32,14 +32,15 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import java.util.Date; public class SubkeysAdapter extends CursorAdapter { private LayoutInflater mInflater; + private SaveKeyringParcel mSaveKeyringParcel; private boolean hasAnySecret; - private ColorStateList mDefaultTextColor; public static final String[] SUBKEYS_PROJECTION = new String[]{ @@ -71,10 +72,21 @@ public class SubkeysAdapter extends CursorAdapter { private static final int INDEX_EXPIRY = 11; private static final int INDEX_FINGERPRINT = 12; - public SubkeysAdapter(Context context, Cursor c, int flags) { + public SubkeysAdapter(Context context, Cursor c, int flags, + SaveKeyringParcel saveKeyringParcel) { super(context, c, flags); mInflater = LayoutInflater.from(context); + mSaveKeyringParcel = saveKeyringParcel; + } + + public SubkeysAdapter(Context context, Cursor c, int flags) { + this(context, c, flags, null); + } + + public long getKeyId(int position) { + mCursor.moveToPosition(position); + return mCursor.getLong(INDEX_KEY_ID); } @Override @@ -94,79 +106,94 @@ public class SubkeysAdapter extends CursorAdapter { @Override public void bindView(View view, Context context, Cursor cursor) { - TextView keyId = (TextView) view.findViewById(R.id.keyId); - TextView keyDetails = (TextView) view.findViewById(R.id.keyDetails); - TextView keyExpiry = (TextView) view.findViewById(R.id.keyExpiry); - ImageView masterKeyIcon = (ImageView) view.findViewById(R.id.ic_masterKey); - ImageView certifyIcon = (ImageView) view.findViewById(R.id.ic_certifyKey); - ImageView encryptIcon = (ImageView) view.findViewById(R.id.ic_encryptKey); - ImageView signIcon = (ImageView) view.findViewById(R.id.ic_signKey); - ImageView revokedKeyIcon = (ImageView) view.findViewById(R.id.ic_revokedKey); - - String keyIdStr = PgpKeyHelper.convertKeyIdToHex(cursor.getLong(INDEX_KEY_ID)); + TextView vKeyId = (TextView) view.findViewById(R.id.keyId); + TextView vKeyDetails = (TextView) view.findViewById(R.id.keyDetails); + TextView vKeyExpiry = (TextView) view.findViewById(R.id.keyExpiry); + ImageView vMasterKeyIcon = (ImageView) view.findViewById(R.id.ic_masterKey); + ImageView vCertifyIcon = (ImageView) view.findViewById(R.id.ic_certifyKey); + ImageView vEncryptIcon = (ImageView) view.findViewById(R.id.ic_encryptKey); + ImageView vSignIcon = (ImageView) view.findViewById(R.id.ic_signKey); + ImageView vRevokedKeyIcon = (ImageView) view.findViewById(R.id.ic_revokedKey); + ImageView vEditImage = (ImageView) view.findViewById(R.id.edit_image); + + long keyId = cursor.getLong(INDEX_KEY_ID); + String keyIdStr = PgpKeyHelper.convertKeyIdToHex(keyId); String algorithmStr = PgpKeyHelper.getAlgorithmInfo( context, cursor.getInt(INDEX_ALGORITHM), cursor.getInt(INDEX_KEY_SIZE) ); - keyId.setText(keyIdStr); + vKeyId.setText(keyIdStr); // may be set with additional "stripped" later on if (hasAnySecret && cursor.getInt(INDEX_HAS_SECRET) == 0) { - keyDetails.setText(algorithmStr + ", " + + vKeyDetails.setText(algorithmStr + ", " + context.getString(R.string.key_stripped)); } else { - keyDetails.setText(algorithmStr); + vKeyDetails.setText(algorithmStr); } // Set icons according to properties - masterKeyIcon.setVisibility(cursor.getInt(INDEX_RANK) == 0 ? View.VISIBLE : View.INVISIBLE); - certifyIcon.setVisibility(cursor.getInt(INDEX_CAN_CERTIFY) != 0 ? View.VISIBLE : View.GONE); - encryptIcon.setVisibility(cursor.getInt(INDEX_CAN_ENCRYPT) != 0 ? View.VISIBLE : View.GONE); - signIcon.setVisibility(cursor.getInt(INDEX_CAN_SIGN) != 0 ? View.VISIBLE : View.GONE); + vMasterKeyIcon.setVisibility(cursor.getInt(INDEX_RANK) == 0 ? View.VISIBLE : View.INVISIBLE); + vCertifyIcon.setVisibility(cursor.getInt(INDEX_CAN_CERTIFY) != 0 ? View.VISIBLE : View.GONE); + vEncryptIcon.setVisibility(cursor.getInt(INDEX_CAN_ENCRYPT) != 0 ? View.VISIBLE : View.GONE); + vSignIcon.setVisibility(cursor.getInt(INDEX_CAN_SIGN) != 0 ? View.VISIBLE : View.GONE); + + boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; + + // for edit key + if (mSaveKeyringParcel != null) { + boolean revokeThisSubkey = (mSaveKeyringParcel.revokeSubKeys.contains(keyId)); - boolean valid = true; - if (cursor.getInt(INDEX_IS_REVOKED) > 0) { - revokedKeyIcon.setVisibility(View.VISIBLE); + if (revokeThisSubkey) { + if (!isRevoked) { + isRevoked = true; + } + } - valid = false; + vEditImage.setVisibility(View.VISIBLE); } else { - keyId.setTextColor(mDefaultTextColor); - keyDetails.setTextColor(mDefaultTextColor); - keyExpiry.setTextColor(mDefaultTextColor); + vEditImage.setVisibility(View.GONE); + } - revokedKeyIcon.setVisibility(View.GONE); + if (isRevoked) { + vRevokedKeyIcon.setVisibility(View.VISIBLE); + } else { + vKeyId.setTextColor(mDefaultTextColor); + vKeyDetails.setTextColor(mDefaultTextColor); + vKeyExpiry.setTextColor(mDefaultTextColor); + + vRevokedKeyIcon.setVisibility(View.GONE); } + boolean isExpired; if (!cursor.isNull(INDEX_EXPIRY)) { Date expiryDate = new Date(cursor.getLong(INDEX_EXPIRY) * 1000); + isExpired = expiryDate.before(new Date()); - valid = valid && expiryDate.after(new Date()); - keyExpiry.setText( - context.getString(R.string.label_expiry) + ": " + - DateFormat.getDateFormat(context).format(expiryDate) - ); + vKeyExpiry.setText(context.getString(R.string.label_expiry) + ": " + + DateFormat.getDateFormat(context).format(expiryDate)); } else { - keyExpiry.setText( - context.getString(R.string.label_expiry) + ": " + - context.getString(R.string.none) - ); + isExpired = false; + + vKeyExpiry.setText(context.getString(R.string.label_expiry) + ": " + context.getString(R.string.none)); } // if key is expired or revoked, strike through text - if (!valid) { - keyId.setText(OtherHelper.strikeOutText(keyId.getText())); - keyDetails.setText(OtherHelper.strikeOutText(keyDetails.getText())); - keyExpiry.setText(OtherHelper.strikeOutText(keyExpiry.getText())); + boolean isInvalid = isRevoked || isExpired; + if (isInvalid) { + vKeyId.setText(OtherHelper.strikeOutText(vKeyId.getText())); + vKeyDetails.setText(OtherHelper.strikeOutText(vKeyDetails.getText())); + vKeyExpiry.setText(OtherHelper.strikeOutText(vKeyExpiry.getText())); } - keyId.setEnabled(valid); - keyDetails.setEnabled(valid); - keyExpiry.setEnabled(valid); + vKeyId.setEnabled(!isInvalid); + vKeyDetails.setEnabled(!isInvalid); + vKeyExpiry.setEnabled(!isInvalid); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { - View view = mInflater.inflate(R.layout.view_key_keys_item, null); + View view = mInflater.inflate(R.layout.view_key_subkey_item, null); if (mDefaultTextColor == null) { TextView keyId = (TextView) view.findViewById(R.id.keyId); mDefaultTextColor = keyId.getTextColors(); @@ -177,13 +204,21 @@ public class SubkeysAdapter extends CursorAdapter { // Disable selection of items, http://stackoverflow.com/a/4075045 @Override public boolean areAllItemsEnabled() { - return false; + if (mSaveKeyringParcel == null) { + return false; + } else { + return super.areAllItemsEnabled(); + } } // Disable selection of items, http://stackoverflow.com/a/4075045 @Override public boolean isEnabled(int position) { - return false; + if (mSaveKeyringParcel == null) { + return false; + } else { + return super.isEnabled(position); + } } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index d729648e5..10e299ae3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -40,9 +40,7 @@ import java.util.ArrayList; public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemClickListener { private LayoutInflater mInflater; - private final ArrayList mCheckStates; - private SaveKeyringParcel mSaveKeyringParcel; public static final String[] USER_IDS_PROJECTION = new String[]{ @@ -60,7 +58,6 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC private static final int INDEX_IS_PRIMARY = 4; private static final int INDEX_IS_REVOKED = 5; - public UserIdsAdapter(Context context, Cursor c, int flags, boolean showCheckBoxes, SaveKeyringParcel saveKeyringParcel) { super(context, c, flags); @@ -139,10 +136,13 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC && mSaveKeyringParcel.changePrimaryUserId.equals(userId)); boolean revokeThisUserId = (mSaveKeyringParcel.revokeUserIds.contains(userId)); + // only if primary user id will be changed + // (this is not triggered if the user id is currently the primary one) if (changeAnyPrimaryUserId) { - // change all user ids, only this one should be primary + // change _all_ primary user ids and set new one to true isPrimary = changeThisPrimaryUserId; } + if (revokeThisUserId) { if (!isRevoked) { isRevoked = true; @@ -233,7 +233,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { - View view = mInflater.inflate(R.layout.view_key_userids_item, null); + View view = mInflater.inflate(R.layout.view_key_user_id_item, null); // only need to do this once ever, since mShowCheckBoxes is final view.findViewById(R.id.checkBox).setVisibility(mCheckStates != null ? View.VISIBLE : View.GONE); return view; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/EditSubkeyDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/EditSubkeyDialogFragment.java new file mode 100644 index 000000000..9fef88a78 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/EditSubkeyDialogFragment.java @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +public class EditSubkeyDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + + public static final int MESSAGE_CHANGE_EXPIRY = 1; + public static final int MESSAGE_REVOKE = 2; + + private Messenger mMessenger; + + /** + * Creates new instance of this dialog fragment + */ + public static EditSubkeyDialogFragment newInstance(Messenger messenger) { + EditSubkeyDialogFragment frag = new EditSubkeyDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + + frag.setArguments(args); + + return frag; + } + + /** + * Creates dialog + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + mMessenger = getArguments().getParcelable(ARG_MESSENGER); + + CustomAlertDialogBuilder builder = new CustomAlertDialogBuilder(getActivity()); + CharSequence[] array = getResources().getStringArray(R.array.edit_key_edit_subkey); + + builder.setTitle(R.string.edit_key_edit_subkey_title); + builder.setItems(array, new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case 0: + sendMessageToHandler(MESSAGE_CHANGE_EXPIRY, null); + break; + case 1: + sendMessageToHandler(MESSAGE_REVOKE, null); + break; + default: + break; + } + } + }); + builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + dismiss(); + } + }); + + return builder.show(); + } + + /** + * Send message back to handler which is initialized in a activity + * + * @param what Message integer you want to send + */ + private void sendMessageToHandler(Integer what, Bundle data) { + Message msg = Message.obtain(); + msg.what = what; + if (data != null) { + msg.setData(data); + } + + try { + mMessenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } +} -- cgit v1.2.3 From 16498be4a2c4b08b9763f21de4bc5670c89db4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 8 Jul 2014 04:24:27 +0200 Subject: Fix nullpointer in API, fix #693 --- .../org/sufficientlysecure/keychain/remote/OpenPgpService.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 17c277026..62d6b5ad6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -53,6 +53,12 @@ import java.util.Set; public class OpenPgpService extends RemoteService { + static final String[] KEYRING_PROJECTION = + new String[]{ + KeyRings._ID, + KeyRings.MASTER_KEY_ID, + }; + /** * Search database for key ids based on emails. * @@ -70,7 +76,7 @@ public class OpenPgpService extends RemoteService { for (String email : encryptionUserIds) { Uri uri = KeyRings.buildUnifiedKeyRingsFindByEmailUri(email); - Cursor cursor = getContentResolver().query(uri, null, null, null, null); + Cursor cursor = getContentResolver().query(uri, KEYRING_PROJECTION, null, null, null); try { if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID)); -- cgit v1.2.3 From 5185492b049926511d727a510fc2dffcc0947c88 Mon Sep 17 00:00:00 2001 From: mar-v-in Date: Wed, 9 Jul 2014 00:10:09 +0200 Subject: Fix OperationResultParcel Naming conventions save lives... or atleast make addAll() work --- .../keychain/service/OperationResultParcel.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 8db48ae3b..f868d931c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -305,20 +305,20 @@ public class OperationResultParcel implements Parcelable { public static class OperationLog implements Iterable { - private final List parcels = new ArrayList(); + private final List mParcels = new ArrayList(); /// Simple convenience method public void add(LogLevel level, LogType type, int indent, Object... parameters) { Log.d(Constants.TAG, type.toString()); - parcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, parameters)); + mParcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, parameters)); } public void add(LogLevel level, LogType type, int indent) { - parcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); + mParcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } public boolean containsWarnings() { - for(LogEntryParcel entry : new IterableIterator(parcels.iterator())) { + for(LogEntryParcel entry : new IterableIterator(mParcels.iterator())) { if (entry.mLevel == LogLevel.WARN || entry.mLevel == LogLevel.ERROR) { return true; } @@ -327,20 +327,20 @@ public class OperationResultParcel implements Parcelable { } public void addAll(List parcels) { - parcels.addAll(parcels); + mParcels.addAll(parcels); } public List toList() { - return parcels; + return mParcels; } public boolean isEmpty() { - return parcels.isEmpty(); + return mParcels.isEmpty(); } @Override public Iterator iterator() { - return parcels.iterator(); + return mParcels.iterator(); } } -- cgit v1.2.3 From 90f546a4e877972d6686745aabfac1fca1178b8c Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 10 Jul 2014 01:38:57 +0200 Subject: tests: add testSubkeyAdd --- .../main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java | 4 ++++ .../sufficientlysecure/keychain/service/OperationResultParcel.java | 1 + 2 files changed, 5 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java index 6f3068261..73fd92a3a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java @@ -101,6 +101,10 @@ public abstract class WrappedKeyRing extends KeyRing { abstract public IterableIterator publicKeyIterator(); + public byte[] getEncoded() throws IOException { + return getRing().getEncoded(); + } + public UncachedKeyRing getUncached() { return new UncachedKeyRing(getRing()); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 5a778a19d..e0d11dafa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -325,6 +325,7 @@ public class OperationResultParcel implements Parcelable { } public void add(LogLevel level, LogType type, int indent) { + Log.d(Constants.TAG, type.toString()); parcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } -- cgit v1.2.3 From dce2df41132cc11facde85bcac00c55563aead5f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 02:48:54 +0200 Subject: add come createKey strings --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 7 ++++++- .../sufficientlysecure/keychain/service/OperationResultParcel.java | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 9a08290e4..797a3cc3e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -166,7 +166,7 @@ public class PgpKeyOperation { try { - log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_KEYID, indent); + log.add(LogLevel.START, LogType.MSG_CR, indent); indent += 1; updateProgress(R.string.progress_building_key, 0, 100); @@ -176,6 +176,11 @@ public class PgpKeyOperation { } SubkeyAdd add = saveParcel.addSubKeys.remove(0); + if ((add.mFlags & KeyFlags.CERTIFY_OTHER) != KeyFlags.CERTIFY_OTHER) { + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_CERTIFY, indent); + return null; + } + PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize); if (add.mAlgorithm == Constants.choice.algorithm.elgamal) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index e0d11dafa..ffe640d90 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -248,7 +248,9 @@ public class OperationResultParcel implements Parcelable { MSG_MG_FOUND_NEW (R.string.msg_mg_found_new), // secret key create - MSG_CR_ERROR_NO_MASTER (R.string.msg_mr), + MSG_CR (R.string.msg_cr), + MSG_CR_ERROR_NO_MASTER (R.string.msg_cr_error_no_master), + MSG_CR_ERROR_NO_CERTIFY (R.string.msg_cr_error_no_certify), // secret key modify MSG_MF (R.string.msg_mr), @@ -260,6 +262,8 @@ public class OperationResultParcel implements Parcelable { MSG_MF_ERROR_PGP (R.string.msg_mf_error_pgp), MSG_MF_ERROR_SIG (R.string.msg_mf_error_sig), MSG_MF_PASSPHRASE (R.string.msg_mf_passphrase), + MSG_MF_PRIMARY_REPLACE_OLD (R.string.msg_mf_primary_replace_old), + MSG_MF_PRIMARY_NEW (R.string.msg_mf_primary_new), MSG_MF_SUBKEY_CHANGE (R.string.msg_mf_subkey_change), MSG_MF_SUBKEY_MISSING (R.string.msg_mf_subkey_missing), MSG_MF_SUBKEY_NEW_ID (R.string.msg_mf_subkey_new_id), -- cgit v1.2.3 From 38ee6203ad1dd784a37e705735398955cc7ec9f5 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 02:49:51 +0200 Subject: modifyKey: preserve master key flags --- .../keychain/pgp/PgpKeyOperation.java | 27 ++++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 797a3cc3e..77a51c061 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -200,7 +200,7 @@ public class PgpKeyOperation { PGPSecretKeyRing sKR = new PGPSecretKeyRing( masterSecretKey.getEncoded(), new JcaKeyFingerprintCalculator()); - return internal(sKR, masterSecretKey, saveParcel, "", log, indent); + return internal(sKR, masterSecretKey, add.mFlags, saveParcel, "", log, indent); } catch (PGPException e) { Log.e(Constants.TAG, "pgp error encoding key", e); @@ -262,11 +262,16 @@ public class PgpKeyOperation { return null; } - return internal(sKR, masterSecretKey, saveParcel, passphrase, log, indent); + // read masterKeyFlags, and use the same as before. + // since this is the master key, this contains at least CERTIFY_OTHER + int masterKeyFlags = readMasterKeyFlags(masterSecretKey.getPublicKey()); + + return internal(sKR, masterSecretKey, masterKeyFlags, saveParcel, passphrase, log, indent); } private UncachedKeyRing internal(PGPSecretKeyRing sKR, PGPSecretKey masterSecretKey, + int masterKeyFlags, SaveKeyringParcel saveParcel, String passphrase, OperationLog log, int indent) { @@ -323,8 +328,8 @@ public class PgpKeyOperation { && userId.equals(saveParcel.changePrimaryUserId); // generate and add new certificate PGPSignature cert = generateUserIdSignature(masterPrivateKey, - masterPublicKey, userId, isPrimary); - modifiedPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, cert); + masterPublicKey, userId, isPrimary, masterKeyFlags); + modifiedPublicKey = PGPPublicKey.addCertification(modifiedPublicKey, userId, cert); } // 2b. Add revocations for revoked user ids @@ -551,7 +556,7 @@ public class PgpKeyOperation { } private static PGPSignature generateUserIdSignature( - PGPPrivateKey masterPrivateKey, PGPPublicKey pKey, String userId, boolean primary) + PGPPrivateKey masterPrivateKey, PGPPublicKey pKey, String userId, boolean primary, int flags) throws IOException, PGPException, SignatureException { PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder( pKey.getAlgorithm(), PGPUtil.SHA1) @@ -563,6 +568,7 @@ public class PgpKeyOperation { subHashedPacketsGen.setPreferredHashAlgorithms(true, PREFERRED_HASH_ALGORITHMS); subHashedPacketsGen.setPreferredCompressionAlgorithms(true, PREFERRED_COMPRESSION_ALGORITHMS); subHashedPacketsGen.setPrimaryUserID(false, primary); + subHashedPacketsGen.setKeyFlags(false, flags); sGen.setHashedSubpackets(subHashedPacketsGen.generate()); sGen.init(PGPSignature.POSITIVE_CERTIFICATION, masterPrivateKey); return sGen.generateCertification(userId, pKey); @@ -670,4 +676,15 @@ public class PgpKeyOperation { } + private static int readMasterKeyFlags(PGPPublicKey masterKey) { + int flags = KeyFlags.CERTIFY_OTHER; + for(PGPSignature sig : new IterableIterator(masterKey.getSignatures())) { + if (!sig.hasSubpackets()) { + continue; + } + flags |= sig.getHashedSubPackets().getKeyFlags(); + } + return flags; + } + } -- cgit v1.2.3 From e477577c55cf9c30b749b467ab01fa2ff2ce8254 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 02:50:35 +0200 Subject: some UncachedKeyRing fixes, primary user id mostly --- .../keychain/pgp/UncachedPublicKey.java | 70 +++++++++++++++++++--- .../keychain/pgp/WrappedKeyRing.java | 4 -- .../keychain/provider/ProviderHelper.java | 11 ++-- 3 files changed, 67 insertions(+), 18 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 33db7771b..3171d0565 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -77,26 +77,65 @@ public class UncachedPublicKey { return mPublicKey.getBitStrength(); } + /** Returns the primary user id, as indicated by the public key's self certificates. + * + * This is an expensive operation, since potentially a lot of certificates (and revocations) + * have to be checked, and even then the result is NOT guaranteed to be constant through a + * canonicalization operation. + * + * Returns null if there is no primary user id (as indicated by certificates) + * + */ public String getPrimaryUserId() { + String found = null; + PGPSignature foundSig = null; for (String userId : new IterableIterator(mPublicKey.getUserIDs())) { + PGPSignature revocation = null; + for (PGPSignature sig : new IterableIterator(mPublicKey.getSignaturesForID(userId))) { - if (sig.getHashedSubPackets() != null - && sig.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.PRIMARY_USER_ID)) { - try { + try { + + // if this is a revocation, this is not the user id + if (sig.getSignatureType() == PGPSignature.CERTIFICATION_REVOCATION) { + // make sure it's actually valid + sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider( + Constants.BOUNCY_CASTLE_PROVIDER_NAME), mPublicKey); + if (!sig.verifyCertification(userId, mPublicKey)) { + continue; + } + if (found != null && found.equals(userId)) { + found = null; + } + revocation = sig; + // this revocation may still be overridden by a newer cert + continue; + } + + if (sig.getHashedSubPackets() != null && sig.getHashedSubPackets().isPrimaryUserID()) { + if (foundSig != null && sig.getCreationTime().before(foundSig.getCreationTime())) { + continue; + } + // ignore if there is a newer revocation for this user id + if (revocation != null && sig.getCreationTime().before(revocation.getCreationTime())) { + continue; + } // make sure it's actually valid sig.init(new JcaPGPContentVerifierBuilderProvider().setProvider( Constants.BOUNCY_CASTLE_PROVIDER_NAME), mPublicKey); if (sig.verifyCertification(userId, mPublicKey)) { - return userId; + found = userId; + foundSig = sig; + // this one can't be relevant anymore at this point + revocation = null; } - } catch (Exception e) { - // nothing bad happens, the key is just not considered the primary key id } - } + } catch (Exception e) { + // nothing bad happens, the key is just not considered the primary key id + } } } - return null; + return found; } public ArrayList getUnorderedUserIds() { @@ -186,6 +225,21 @@ public class UncachedPublicKey { return mPublicKey; } + public Iterator getSignatures() { + final Iterator it = mPublicKey.getSignatures(); + return new Iterator() { + public void remove() { + it.remove(); + } + public WrappedSignature next() { + return new WrappedSignature(it.next()); + } + public boolean hasNext() { + return it.hasNext(); + } + }; + } + public Iterator getSignaturesForId(String userId) { final Iterator it = mPublicKey.getSignaturesForID(userId); return new Iterator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java index 73fd92a3a..2fcd05204 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java @@ -105,8 +105,4 @@ public abstract class WrappedKeyRing extends KeyRing { return getRing().getEncoded(); } - public UncachedKeyRing getUncached() { - return new UncachedKeyRing(getRing()); - } - } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index b5609a327..c338c9d95 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -450,8 +450,7 @@ public class ProviderHelper { if (cert.verifySignature(masterKey, userId)) { item.trustedCerts.add(cert); log(LogLevel.INFO, LogType.MSG_IP_UID_CERT_GOOD, - PgpKeyHelper.convertKeyIdToHexShort(trustedKey.getKeyId()), - trustedKey.getPrimaryUserId() + PgpKeyHelper.convertKeyIdToHexShort(trustedKey.getKeyId()) ); } else { log(LogLevel.WARN, LogType.MSG_IP_UID_CERT_BAD); @@ -670,7 +669,7 @@ public class ProviderHelper { // If there is an old keyring, merge it try { - UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncached(); + UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new public ring into the old one publicRing = oldPublicRing.merge(publicRing, mLog, mIndent); @@ -706,7 +705,7 @@ public class ProviderHelper { // If there is a secret key, merge new data (if any) and save the key for later UncachedKeyRing secretRing; try { - secretRing = getWrappedSecretKeyRing(publicRing.getMasterKeyId()).getUncached(); + secretRing = getWrappedSecretKeyRing(publicRing.getMasterKeyId()).getUncachedKeyRing(); // Merge data from new public ring into secret one secretRing = secretRing.merge(publicRing, mLog, mIndent); @@ -754,7 +753,7 @@ public class ProviderHelper { // If there is an old secret key, merge it. try { - UncachedKeyRing oldSecretRing = getWrappedSecretKeyRing(masterKeyId).getUncached(); + UncachedKeyRing oldSecretRing = getWrappedSecretKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new secret ring into old one secretRing = oldSecretRing.merge(secretRing, mLog, mIndent); @@ -791,7 +790,7 @@ public class ProviderHelper { // Merge new data into public keyring as well, if there is any UncachedKeyRing publicRing; try { - UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncached(); + UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new public ring into secret one publicRing = oldPublicRing.merge(secretRing, mLog, mIndent); -- cgit v1.2.3 From f6e39b0a9702beda90b6c6e7f32ca986a8a1f3fb Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 02:51:13 +0200 Subject: modifyKey: couple more fixes from tests --- .../keychain/pgp/PgpKeyOperation.java | 12 ++++++---- .../keychain/pgp/WrappedSignature.java | 26 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 77a51c061..21527159b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -339,12 +339,13 @@ public class PgpKeyOperation { // take care of that here. PGPSignature cert = generateRevocationSignature(masterPrivateKey, masterPublicKey, userId); - modifiedPublicKey = PGPPublicKey.addCertification(masterPublicKey, userId, cert); + modifiedPublicKey = PGPPublicKey.addCertification(modifiedPublicKey, userId, cert); } // 3. If primary user id changed, generate new certificates for both old and new if (saveParcel.changePrimaryUserId != null) { log.add(LogLevel.INFO, LogType.MSG_MF_UID_PRIMARY, indent); + indent += 1; // we work on the modifiedPublicKey here, to respect new or newly revoked uids // noinspection unchecked @@ -353,7 +354,7 @@ public class PgpKeyOperation { PGPSignature currentCert = null; // noinspection unchecked for (PGPSignature cert : new IterableIterator( - masterPublicKey.getSignaturesForID(userId))) { + modifiedPublicKey.getSignaturesForID(userId))) { // if it's not a self cert, never mind if (cert.getKeyID() != masterPublicKey.getKeyID()) { continue; @@ -397,10 +398,11 @@ public class PgpKeyOperation { continue; } // otherwise, generate new non-primary certification + log.add(LogLevel.DEBUG, LogType.MSG_MF_PRIMARY_REPLACE_OLD, indent); modifiedPublicKey = PGPPublicKey.removeCertification( modifiedPublicKey, userId, currentCert); PGPSignature newCert = generateUserIdSignature( - masterPrivateKey, masterPublicKey, userId, false); + masterPrivateKey, masterPublicKey, userId, false, masterKeyFlags); modifiedPublicKey = PGPPublicKey.addCertification( modifiedPublicKey, userId, newCert); continue; @@ -411,10 +413,11 @@ public class PgpKeyOperation { // if it should be if (userId.equals(saveParcel.changePrimaryUserId)) { // add shiny new primary user id certificate + log.add(LogLevel.DEBUG, LogType.MSG_MF_PRIMARY_NEW, indent); modifiedPublicKey = PGPPublicKey.removeCertification( modifiedPublicKey, userId, currentCert); PGPSignature newCert = generateUserIdSignature( - masterPrivateKey, masterPublicKey, userId, true); + masterPrivateKey, masterPublicKey, userId, true, masterKeyFlags); modifiedPublicKey = PGPPublicKey.addCertification( modifiedPublicKey, userId, newCert); } @@ -423,6 +426,7 @@ public class PgpKeyOperation { } + indent -= 1; } // Update the secret key ring diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java index 196ac1dee..c87b23222 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java @@ -15,6 +15,7 @@ import org.sufficientlysecure.keychain.util.Log; import java.io.IOException; import java.security.SignatureException; +import java.util.ArrayList; import java.util.Date; /** OpenKeychain wrapper around PGPSignature objects. @@ -55,6 +56,31 @@ public class WrappedSignature { return mSig.getCreationTime(); } + public ArrayList getEmbeddedSignatures() { + ArrayList sigs = new ArrayList(); + if (!mSig.hasSubpackets()) { + return sigs; + } + try { + PGPSignatureList list; + list = mSig.getHashedSubPackets().getEmbeddedSignatures(); + for(int i = 0; i < list.size(); i++) { + sigs.add(new WrappedSignature(list.get(i))); + } + list = mSig.getUnhashedSubPackets().getEmbeddedSignatures(); + for(int i = 0; i < list.size(); i++) { + sigs.add(new WrappedSignature(list.get(i))); + } + } catch (PGPException e) { + // no matter + Log.e(Constants.TAG, "exception reading embedded signatures", e); + } catch (IOException e) { + // no matter + Log.e(Constants.TAG, "exception reading embedded signatures", e); + } + return sigs; + } + public byte[] getEncoded() throws IOException { return mSig.getEncoded(); } -- cgit v1.2.3 From d6f3b4b8792b88e627ded3df1d69fcdb6821452a Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 03:27:44 +0200 Subject: fix bug in canonicalization regarding subkey revocation --- .../main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') 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 441e2762a..5ed395f99 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -558,7 +558,7 @@ public class UncachedKeyRing { // make sure the certificate checks out try { cert.init(masterKey); - if (!cert.verifySignature(key)) { + if (!cert.verifySignature(masterKey, key)) { log.add(LogLevel.WARN, LogType.MSG_KC_SUB_REVOKE_BAD, indent); badCerts += 1; continue; -- cgit v1.2.3 From 4da273ac16fc93524bc6212e2b0477904155a4f5 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 13:35:48 +0200 Subject: modifyKey: error out on nonexisting new primary user id --- .../org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 10 ++++++++++ .../keychain/service/OperationResultParcel.java | 1 + 2 files changed, 11 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 21527159b..3c29d361a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -344,6 +344,9 @@ public class PgpKeyOperation { // 3. If primary user id changed, generate new certificates for both old and new if (saveParcel.changePrimaryUserId != null) { + + // keep track if we actually changed one + boolean ok = false; log.add(LogLevel.INFO, LogType.MSG_MF_UID_PRIMARY, indent); indent += 1; @@ -395,6 +398,7 @@ public class PgpKeyOperation { if (currentCert.hasSubpackets() && currentCert.getHashedSubPackets().isPrimaryUserID()) { // if it's the one we want, just leave it as is if (userId.equals(saveParcel.changePrimaryUserId)) { + ok = true; continue; } // otherwise, generate new non-primary certification @@ -420,6 +424,7 @@ public class PgpKeyOperation { masterPrivateKey, masterPublicKey, userId, true, masterKeyFlags); modifiedPublicKey = PGPPublicKey.addCertification( modifiedPublicKey, userId, newCert); + ok = true; } // user id is not primary and is not supposed to be - nothing to do here. @@ -427,6 +432,11 @@ public class PgpKeyOperation { } indent -= 1; + + if (!ok) { + log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_NOEXIST_PRIMARY, indent); + return null; + } } // Update the secret key ring diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index ffe640d90..3b62656ef 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -258,6 +258,7 @@ public class OperationResultParcel implements Parcelable { MSG_MF_ERROR_FINGERPRINT (R.string.msg_mf_error_fingerprint), MSG_MF_ERROR_KEYID (R.string.msg_mf_error_keyid), MSG_MF_ERROR_INTEGRITY (R.string.msg_mf_error_integrity), + MSG_MF_ERROR_NOEXIST_PRIMARY (R.string.msg_mf_error_noexist_primary), MSG_MF_ERROR_REVOKED_PRIMARY (R.string.msg_mf_error_revoked_primary), MSG_MF_ERROR_PGP (R.string.msg_mf_error_pgp), MSG_MF_ERROR_SIG (R.string.msg_mf_error_sig), -- cgit v1.2.3 From 26f6d58284d7665aa810e0d6a219e1191166e349 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 13:37:31 +0200 Subject: get rid of some inspection warnings --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 1 + .../sufficientlysecure/keychain/service/OperationResultParcel.java | 4 ---- 2 files changed, 1 insertion(+), 4 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 3c29d361a..b0d458102 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -692,6 +692,7 @@ public class PgpKeyOperation { private static int readMasterKeyFlags(PGPPublicKey masterKey) { int flags = KeyFlags.CERTIFY_OTHER; + //noinspection unchecked for(PGPSignature sig : new IterableIterator(masterKey.getSignatures())) { if (!sig.hasSubpackets()) { continue; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 3b62656ef..1cf2a60ec 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -12,7 +12,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.Arrays; /** Represent the result of an operation. * @@ -72,9 +71,6 @@ public class OperationResultParcel implements Parcelable { mParameters = parameters; mIndent = indent; } - public LogEntryParcel(LogLevel level, LogType type, Object... parameters) { - this(level, type, 0, parameters); - } public LogEntryParcel(Parcel source) { mLevel = LogLevel.values()[source.readInt()]; -- cgit v1.2.3 From bb92fe2804beed31d8f06a92732e9b1f12dd3aec Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 13:49:17 +0200 Subject: test: get rid of some SaveKeyringParcel boilerplate --- .../keychain/service/SaveKeyringParcel.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java index a56095767..9f29c15dc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java @@ -39,11 +39,7 @@ public class SaveKeyringParcel implements Parcelable { public ArrayList revokeSubKeys; public SaveKeyringParcel() { - addUserIds = new ArrayList(); - addSubKeys = new ArrayList(); - changeSubKeys = new ArrayList(); - revokeUserIds = new ArrayList(); - revokeSubKeys = new ArrayList(); + reset(); } public SaveKeyringParcel(long masterKeyId, byte[] fingerprint) { @@ -52,6 +48,16 @@ public class SaveKeyringParcel implements Parcelable { mFingerprint = fingerprint; } + public void reset() { + newPassphrase = null; + addUserIds = new ArrayList(); + addSubKeys = new ArrayList(); + changePrimaryUserId = null; + changeSubKeys = new ArrayList(); + revokeUserIds = new ArrayList(); + revokeSubKeys = new ArrayList(); + } + // performance gain for using Parcelable here would probably be negligible, // use Serializable instead. public static class SubkeyAdd implements Serializable { -- cgit v1.2.3 From 1436ab8d90853bfe2ba52d628a38a78368508fe8 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 13:51:36 +0200 Subject: SaveKeyringParcel: follow attribute m prefix coding guideline --- .../keychain/pgp/PgpKeyOperation.java | 30 ++++++------ .../keychain/service/KeychainIntentService.java | 4 +- .../keychain/service/SaveKeyringParcel.java | 56 +++++++++++----------- .../keychain/ui/EditKeyActivityOld.java | 2 +- .../keychain/ui/EditKeyFragment.java | 22 ++++----- .../keychain/ui/KeyListActivity.java | 11 ++--- .../keychain/ui/WizardActivity.java | 2 +- .../keychain/ui/adapter/UserIdsAdapter.java | 8 ++-- 8 files changed, 66 insertions(+), 69 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index b0d458102..748707384 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -170,12 +170,12 @@ public class PgpKeyOperation { indent += 1; updateProgress(R.string.progress_building_key, 0, 100); - if (saveParcel.addSubKeys == null || saveParcel.addSubKeys.isEmpty()) { + if (saveParcel.mAddSubKeys == null || saveParcel.mAddSubKeys.isEmpty()) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_MASTER, indent); return null; } - SubkeyAdd add = saveParcel.addSubKeys.remove(0); + SubkeyAdd add = saveParcel.mAddSubKeys.remove(0); if ((add.mFlags & KeyFlags.CERTIFY_OTHER) != KeyFlags.CERTIFY_OTHER) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_CERTIFY, indent); return null; @@ -299,7 +299,7 @@ public class PgpKeyOperation { PGPPublicKey modifiedPublicKey = masterPublicKey; // 2a. Add certificates for new user ids - for (String userId : saveParcel.addUserIds) { + for (String userId : saveParcel.mAddUserIds) { log.add(LogLevel.INFO, LogType.MSG_MF_UID_ADD, indent); // this operation supersedes all previous binding and revocation certificates, @@ -324,8 +324,8 @@ public class PgpKeyOperation { } // if it's supposed to be primary, we can do that here as well - boolean isPrimary = saveParcel.changePrimaryUserId != null - && userId.equals(saveParcel.changePrimaryUserId); + boolean isPrimary = saveParcel.mChangePrimaryUserId != null + && userId.equals(saveParcel.mChangePrimaryUserId); // generate and add new certificate PGPSignature cert = generateUserIdSignature(masterPrivateKey, masterPublicKey, userId, isPrimary, masterKeyFlags); @@ -333,7 +333,7 @@ public class PgpKeyOperation { } // 2b. Add revocations for revoked user ids - for (String userId : saveParcel.revokeUserIds) { + for (String userId : saveParcel.mRevokeUserIds) { log.add(LogLevel.INFO, LogType.MSG_MF_UID_REVOKE, indent); // a duplicate revocatin will be removed during canonicalization, so no need to // take care of that here. @@ -343,7 +343,7 @@ public class PgpKeyOperation { } // 3. If primary user id changed, generate new certificates for both old and new - if (saveParcel.changePrimaryUserId != null) { + if (saveParcel.mChangePrimaryUserId != null) { // keep track if we actually changed one boolean ok = false; @@ -387,7 +387,7 @@ public class PgpKeyOperation { // we definitely should not update certifications of revoked keys, so just leave it. if (isRevoked) { // revoked user ids cannot be primary! - if (userId.equals(saveParcel.changePrimaryUserId)) { + if (userId.equals(saveParcel.mChangePrimaryUserId)) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_REVOKED_PRIMARY, indent); return null; } @@ -397,7 +397,7 @@ public class PgpKeyOperation { // if this is~ the/a primary user id if (currentCert.hasSubpackets() && currentCert.getHashedSubPackets().isPrimaryUserID()) { // if it's the one we want, just leave it as is - if (userId.equals(saveParcel.changePrimaryUserId)) { + if (userId.equals(saveParcel.mChangePrimaryUserId)) { ok = true; continue; } @@ -415,7 +415,7 @@ public class PgpKeyOperation { // if we are here, this is not currently a primary user id // if it should be - if (userId.equals(saveParcel.changePrimaryUserId)) { + if (userId.equals(saveParcel.mChangePrimaryUserId)) { // add shiny new primary user id certificate log.add(LogLevel.DEBUG, LogType.MSG_MF_PRIMARY_NEW, indent); modifiedPublicKey = PGPPublicKey.removeCertification( @@ -447,7 +447,7 @@ public class PgpKeyOperation { } // 4a. For each subkey change, generate new subkey binding certificate - for (SaveKeyringParcel.SubkeyChange change : saveParcel.changeSubKeys) { + for (SaveKeyringParcel.SubkeyChange change : saveParcel.mChangeSubKeys) { log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_CHANGE, indent, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); PGPSecretKey sKey = sKR.getSecretKey(change.mKeyId); @@ -473,7 +473,7 @@ public class PgpKeyOperation { } // 4b. For each subkey revocation, generate new subkey revocation certificate - for (long revocation : saveParcel.revokeSubKeys) { + for (long revocation : saveParcel.mRevokeSubKeys) { log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_REVOKE, indent, PgpKeyHelper.convertKeyIdToHex(revocation)); PGPSecretKey sKey = sKR.getSecretKey(revocation); @@ -492,7 +492,7 @@ public class PgpKeyOperation { } // 5. Generate and add new subkeys - for (SaveKeyringParcel.SubkeyAdd add : saveParcel.addSubKeys) { + for (SaveKeyringParcel.SubkeyAdd add : saveParcel.mAddSubKeys) { try { if (add.mExpiry != null && new Date(add.mExpiry).before(new Date())) { @@ -537,7 +537,7 @@ public class PgpKeyOperation { } // 6. If requested, change passphrase - if (saveParcel.newPassphrase != null) { + if (saveParcel.mNewPassphrase != null) { log.add(LogLevel.INFO, LogType.MSG_MF_PASSPHRASE, indent); PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build() .get(HashAlgorithmTags.SHA1); @@ -547,7 +547,7 @@ public class PgpKeyOperation { PBESecretKeyEncryptor keyEncryptorNew = new JcePBESecretKeyEncryptorBuilder( PGPEncryptedData.CAST5, sha1Calc) .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( - saveParcel.newPassphrase.toCharArray()); + saveParcel.mNewPassphrase.toCharArray()); sKR = PGPSecretKeyRing.copyWithNewPassword(sKR, keyDecryptor, keyEncryptorNew); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index c4c31bdad..172e1418f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -349,9 +349,9 @@ public class KeychainIntentService extends IntentService providerHelper.saveSecretKeyRing(ring, new ProgressScaler(this, 10, 95, 100)); // cache new passphrase - if (saveParcel.newPassphrase != null) { + if (saveParcel.mNewPassphrase != null) { PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), - saveParcel.newPassphrase); + saveParcel.mNewPassphrase); } } catch (ProviderHelper.NotFoundException e) { sendErrorToHandler(e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java index 9f29c15dc..a0dbf2b0b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java @@ -27,16 +27,16 @@ public class SaveKeyringParcel implements Parcelable { // the key fingerprint, for safety. MUST be null for a new key. public byte[] mFingerprint; - public String newPassphrase; + public String mNewPassphrase; - public ArrayList addUserIds; - public ArrayList addSubKeys; + public ArrayList mAddUserIds; + public ArrayList mAddSubKeys; - public ArrayList changeSubKeys; - public String changePrimaryUserId; + public ArrayList mChangeSubKeys; + public String mChangePrimaryUserId; - public ArrayList revokeUserIds; - public ArrayList revokeSubKeys; + public ArrayList mRevokeUserIds; + public ArrayList mRevokeSubKeys; public SaveKeyringParcel() { reset(); @@ -49,13 +49,13 @@ public class SaveKeyringParcel implements Parcelable { } public void reset() { - newPassphrase = null; - addUserIds = new ArrayList(); - addSubKeys = new ArrayList(); - changePrimaryUserId = null; - changeSubKeys = new ArrayList(); - revokeUserIds = new ArrayList(); - revokeSubKeys = new ArrayList(); + mNewPassphrase = null; + mAddUserIds = new ArrayList(); + mAddSubKeys = new ArrayList(); + mChangePrimaryUserId = null; + mChangeSubKeys = new ArrayList(); + mRevokeUserIds = new ArrayList(); + mRevokeSubKeys = new ArrayList(); } // performance gain for using Parcelable here would probably be negligible, @@ -88,16 +88,16 @@ public class SaveKeyringParcel implements Parcelable { mMasterKeyId = source.readInt() != 0 ? source.readLong() : null; mFingerprint = source.createByteArray(); - newPassphrase = source.readString(); + mNewPassphrase = source.readString(); - addUserIds = source.createStringArrayList(); - addSubKeys = (ArrayList) source.readSerializable(); + mAddUserIds = source.createStringArrayList(); + mAddSubKeys = (ArrayList) source.readSerializable(); - changeSubKeys = (ArrayList) source.readSerializable(); - changePrimaryUserId = source.readString(); + mChangeSubKeys = (ArrayList) source.readSerializable(); + mChangePrimaryUserId = source.readString(); - revokeUserIds = source.createStringArrayList(); - revokeSubKeys = (ArrayList) source.readSerializable(); + mRevokeUserIds = source.createStringArrayList(); + mRevokeSubKeys = (ArrayList) source.readSerializable(); } @Override @@ -108,16 +108,16 @@ public class SaveKeyringParcel implements Parcelable { } destination.writeByteArray(mFingerprint); - destination.writeString(newPassphrase); + destination.writeString(mNewPassphrase); - destination.writeStringList(addUserIds); - destination.writeSerializable(addSubKeys); + destination.writeStringList(mAddUserIds); + destination.writeSerializable(mAddSubKeys); - destination.writeSerializable(changeSubKeys); - destination.writeString(changePrimaryUserId); + destination.writeSerializable(mChangeSubKeys); + destination.writeString(mChangePrimaryUserId); - destination.writeStringList(revokeUserIds); - destination.writeSerializable(revokeSubKeys); + destination.writeStringList(mRevokeUserIds); + destination.writeSerializable(mRevokeSubKeys); } public static final Creator CREATOR = new Creator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java index 51457cd45..d9deb802c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java @@ -556,7 +556,7 @@ public class EditKeyActivityOld extends ActionBarActivity implements EditorListe saveParams.deletedKeys = mKeysView.getDeletedKeys(); saveParams.keysExpiryDates = getKeysExpiryDates(mKeysView); saveParams.keysUsages = getKeysUsages(mKeysView); - saveParams.newPassphrase = mNewPassphrase; + saveParams.mNewPassphrase = mNewPassphrase; saveParams.oldPassphrase = mCurrentPassphrase; saveParams.newKeys = toPrimitiveArray(mKeysView.getNewKeysArray()); saveParams.keys = getKeys(mKeysView); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index c76dc0164..ae7cf02cf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -217,7 +217,7 @@ public class EditKeyFragment extends LoaderFragment implements mSubkeysAdapter = new SubkeysAdapter(getActivity(), null, 0); mSubkeysList.setAdapter(mSubkeysAdapter); - mSubkeysAddedAdapter = new SubkeysAddedAdapter(getActivity(), mSaveKeyringParcel.addSubKeys); + mSubkeysAddedAdapter = new SubkeysAddedAdapter(getActivity(), mSaveKeyringParcel.mAddSubKeys); mSubkeysAddedList.setAdapter(mSubkeysAddedAdapter); // Prepare the loaders. Either re-connect with an existing ones, @@ -287,7 +287,7 @@ public class EditKeyFragment extends LoaderFragment implements Bundle data = message.getData(); // cache new returned passphrase! - mSaveKeyringParcel.newPassphrase = data + mSaveKeyringParcel.mNewPassphrase = data .getString(SetPassphraseDialogFragment.MESSAGE_NEW_PASSPHRASE); } } @@ -309,19 +309,19 @@ public class EditKeyFragment extends LoaderFragment implements switch (message.what) { case EditUserIdDialogFragment.MESSAGE_CHANGE_PRIMARY_USER_ID: // toggle - if (mSaveKeyringParcel.changePrimaryUserId != null - && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { - mSaveKeyringParcel.changePrimaryUserId = null; + if (mSaveKeyringParcel.mChangePrimaryUserId != null + && mSaveKeyringParcel.mChangePrimaryUserId.equals(userId)) { + mSaveKeyringParcel.mChangePrimaryUserId = null; } else { - mSaveKeyringParcel.changePrimaryUserId = userId; + mSaveKeyringParcel.mChangePrimaryUserId = userId; } break; case EditUserIdDialogFragment.MESSAGE_REVOKE: // toggle - if (mSaveKeyringParcel.revokeUserIds.contains(userId)) { - mSaveKeyringParcel.revokeUserIds.remove(userId); + if (mSaveKeyringParcel.mRevokeUserIds.contains(userId)) { + mSaveKeyringParcel.mRevokeUserIds.remove(userId); } else { - mSaveKeyringParcel.revokeUserIds.add(userId); + mSaveKeyringParcel.mRevokeUserIds.add(userId); } break; } @@ -374,9 +374,9 @@ public class EditKeyFragment extends LoaderFragment implements private void save(String passphrase) { Log.d(Constants.TAG, "add userids to parcel: " + mUserIdsAddedAdapter.getDataAsStringList()); - Log.d(Constants.TAG, "mSaveKeyringParcel.newPassphrase: " + mSaveKeyringParcel.newPassphrase); + Log.d(Constants.TAG, "mSaveKeyringParcel.mNewPassphrase: " + mSaveKeyringParcel.mNewPassphrase); - mSaveKeyringParcel.addUserIds = mUserIdsAddedAdapter.getDataAsStringList(); + mSaveKeyringParcel.mAddUserIds = mUserIdsAddedAdapter.getDataAsStringList(); // Message is received after importing is done in KeychainIntentService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 849576284..da9986890 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -32,8 +32,6 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.choice.algorithm; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; -import org.sufficientlysecure.keychain.helper.OtherHelper; -import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.service.KeychainIntentService; @@ -43,7 +41,6 @@ import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.util.Log; import java.io.IOException; -import java.util.ArrayList; public class KeyListActivity extends DrawerActivity { @@ -153,10 +150,10 @@ public class KeyListActivity extends DrawerActivity { Bundle data = new Bundle(); SaveKeyringParcel parcel = new SaveKeyringParcel(); - parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, null)); - parcel.addSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); - parcel.addUserIds.add("swagerinho"); - parcel.newPassphrase = "swag"; + parcel.mAddSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, null)); + parcel.mAddSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); + parcel.mAddUserIds.add("swagerinho"); + parcel.mNewPassphrase = "swag"; // get selected key entries data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java index 601fc09f9..7a2233524 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java @@ -328,7 +328,7 @@ public class WizardActivity extends ActionBarActivity { && isEditTextNotEmpty(this, passphraseEdit)) { // SaveKeyringParcel newKey = new SaveKeyringParcel(); -// newKey.addUserIds.add(nameEdit.getText().toString() + " <" +// newKey.mAddUserIds.add(nameEdit.getText().toString() + " <" // + emailEdit.getText().toString() + ">"); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index d729648e5..92b652018 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -134,10 +134,10 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC // for edit key if (mSaveKeyringParcel != null) { - boolean changeAnyPrimaryUserId = (mSaveKeyringParcel.changePrimaryUserId != null); - boolean changeThisPrimaryUserId = (mSaveKeyringParcel.changePrimaryUserId != null - && mSaveKeyringParcel.changePrimaryUserId.equals(userId)); - boolean revokeThisUserId = (mSaveKeyringParcel.revokeUserIds.contains(userId)); + boolean changeAnyPrimaryUserId = (mSaveKeyringParcel.mChangePrimaryUserId != null); + boolean changeThisPrimaryUserId = (mSaveKeyringParcel.mChangePrimaryUserId != null + && mSaveKeyringParcel.mChangePrimaryUserId.equals(userId)); + boolean revokeThisUserId = (mSaveKeyringParcel.mRevokeUserIds.contains(userId)); if (changeAnyPrimaryUserId) { // change all user ids, only this one should be primary -- cgit v1.2.3 From 7b195ac2e37cd8223dd788e8978f3bfd3d9596cb Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 15:20:16 +0200 Subject: modifyKey: make SubkeyChange operations work --- .../keychain/pgp/PgpKeyOperation.java | 74 +++++++++++++++------- .../keychain/pgp/UncachedKeyRing.java | 4 ++ .../keychain/pgp/UncachedPublicKey.java | 12 +++- .../keychain/service/SaveKeyringParcel.java | 1 + 4 files changed, 64 insertions(+), 27 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 748707384..e68c871ed 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -65,10 +65,8 @@ import java.security.NoSuchProviderException; import java.security.SecureRandom; import java.security.SignatureException; import java.util.Arrays; -import java.util.Calendar; import java.util.Date; import java.util.Iterator; -import java.util.TimeZone; /** * This class is the single place where ALL operations that actually modify a PGP public or secret @@ -264,7 +262,7 @@ public class PgpKeyOperation { // read masterKeyFlags, and use the same as before. // since this is the master key, this contains at least CERTIFY_OTHER - int masterKeyFlags = readMasterKeyFlags(masterSecretKey.getPublicKey()); + int masterKeyFlags = readKeyFlags(masterSecretKey.getPublicKey()) | KeyFlags.CERTIFY_OTHER; return internal(sKR, masterSecretKey, masterKeyFlags, saveParcel, passphrase, log, indent); @@ -450,6 +448,13 @@ public class PgpKeyOperation { for (SaveKeyringParcel.SubkeyChange change : saveParcel.mChangeSubKeys) { log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_CHANGE, indent, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); + + // TODO allow changes in master key? this implies generating new user id certs... + if (change.mKeyId == masterPublicKey.getKeyID()) { + Log.e(Constants.TAG, "changing the master key not supported"); + return null; + } + PGPSecretKey sKey = sKR.getSecretKey(change.mKeyId); if (sKey == null) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_MISSING, @@ -458,16 +463,39 @@ public class PgpKeyOperation { } PGPPublicKey pKey = sKey.getPublicKey(); - if (change.mExpiry != null && new Date(change.mExpiry).before(new Date())) { + // expiry must not be in the past + if (change.mExpiry != null && new Date(change.mExpiry*1000).before(new Date())) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent + 1, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); return null; } - // generate and add new signature. we can be sloppy here and just leave the old one, - // it will be removed during canonicalization + // keep old flags, or replace with new ones + int flags = change.mFlags == null ? readKeyFlags(pKey) : change.mFlags; + long expiry; + if (change.mExpiry == null) { + long valid = pKey.getValidSeconds(); + expiry = valid == 0 + ? 0 + : pKey.getCreationTime().getTime() / 1000 + pKey.getValidSeconds(); + } else { + expiry = change.mExpiry; + } + + // drop all old signatures, they will be superseded by the new one + //noinspection unchecked + for (PGPSignature sig : new IterableIterator(pKey.getSignatures())) { + // special case: if there is a revocation, don't use expiry from before + if (change.mExpiry == null + && sig.getSignatureType() == PGPSignature.SUBKEY_REVOCATION) { + expiry = 0; + } + pKey = PGPPublicKey.removeCertification(pKey, sig); + } + + // generate and add new signature PGPSignature sig = generateSubkeyBindingSignature(masterPublicKey, masterPrivateKey, - sKey, pKey, change.mFlags, change.mExpiry, passphrase); + sKey, pKey, flags, expiry, passphrase); pKey = PGPPublicKey.addCertification(pKey, sig); sKR = PGPSecretKeyRing.insertSecretKey(sKR, PGPSecretKey.replacePublicKey(sKey, pKey)); } @@ -509,7 +537,7 @@ public class PgpKeyOperation { PGPPublicKey pKey = keyPair.getPublicKey(); PGPSignature cert = generateSubkeyBindingSignature( masterPublicKey, masterPrivateKey, keyPair.getPrivateKey(), pKey, - add.mFlags, add.mExpiry); + add.mFlags, add.mExpiry == null ? 0 : add.mExpiry); pKey = PGPPublicKey.addSubkeyBindingCertification(pKey, cert); PGPSecretKey sKey; { @@ -624,7 +652,7 @@ public class PgpKeyOperation { private static PGPSignature generateSubkeyBindingSignature( PGPPublicKey masterPublicKey, PGPPrivateKey masterPrivateKey, - PGPSecretKey sKey, PGPPublicKey pKey, int flags, Long expiry, String passphrase) + PGPSecretKey sKey, PGPPublicKey pKey, int flags, long expiry, String passphrase) throws IOException, PGPException, SignatureException { PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder() .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build( @@ -636,7 +664,7 @@ public class PgpKeyOperation { private static PGPSignature generateSubkeyBindingSignature( PGPPublicKey masterPublicKey, PGPPrivateKey masterPrivateKey, - PGPPrivateKey subPrivateKey, PGPPublicKey pKey, int flags, Long expiry) + PGPPrivateKey subPrivateKey, PGPPublicKey pKey, int flags, long expiry) throws IOException, PGPException, SignatureException { // date for signing @@ -665,17 +693,9 @@ public class PgpKeyOperation { hashedPacketsGen.setKeyFlags(false, flags); } - if (expiry != null) { - Calendar creationDate = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - creationDate.setTime(pKey.getCreationTime()); - - // (Just making sure there's no programming error here, this MUST have been checked above!) - if (new Date(expiry).before(todayDate)) { - throw new RuntimeException("Bad subkey creation date, this is a bug!"); - } - hashedPacketsGen.setKeyExpirationTime(false, expiry - creationDate.getTimeInMillis()); - } else { - hashedPacketsGen.setKeyExpirationTime(false, 0); + if (expiry > 0) { + long creationTime = pKey.getCreationTime().getTime() / 1000; + hashedPacketsGen.setKeyExpirationTime(false, expiry - creationTime); } PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder( @@ -690,10 +710,16 @@ public class PgpKeyOperation { } - private static int readMasterKeyFlags(PGPPublicKey masterKey) { - int flags = KeyFlags.CERTIFY_OTHER; + /** Returns all flags valid for this key. + * + * This method does not do any validity checks on the signature, so it should not be used on + * a non-canonicalized key! + * + */ + private static int readKeyFlags(PGPPublicKey key) { + int flags = 0; //noinspection unchecked - for(PGPSignature sig : new IterableIterator(masterKey.getSignatures())) { + for(PGPSignature sig : new IterableIterator(key.getSignatures())) { if (!sig.hasSubpackets()) { continue; } 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 5ed395f99..691e867b2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -81,6 +81,10 @@ public class UncachedKeyRing { return new UncachedPublicKey(mRing.getPublicKey()); } + public UncachedPublicKey getPublicKey(long keyId) { + return new UncachedPublicKey(mRing.getPublicKey(keyId)); + } + public Iterator getPublicKeys() { final Iterator it = mRing.getPublicKeys(); return new Iterator() { 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 3171d0565..0bd2c2c02 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -9,6 +9,7 @@ import org.spongycastle.openpgp.PGPSignatureSubpacketVector; import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentVerifierBuilderProvider; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.IterableIterator; +import org.sufficientlysecure.keychain.util.Log; import java.security.SignatureException; import java.util.ArrayList; @@ -44,14 +45,19 @@ public class UncachedPublicKey { } public Date getExpiryTime() { - Date creationDate = getCreationTime(); - if (mPublicKey.getValidDays() == 0) { + long seconds = mPublicKey.getValidSeconds(); + if (seconds > Integer.MAX_VALUE) { + Log.e(Constants.TAG, "error, expiry time too large"); + return null; + } + if (seconds == 0) { // no expiry return null; } + Date creationDate = getCreationTime(); Calendar calendar = GregorianCalendar.getInstance(); calendar.setTime(creationDate); - calendar.add(Calendar.DATE, mPublicKey.getValidDays()); + calendar.add(Calendar.SECOND, (int) seconds); return calendar.getTime(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java index a0dbf2b0b..5e90b396e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/SaveKeyringParcel.java @@ -76,6 +76,7 @@ public class SaveKeyringParcel implements Parcelable { public static class SubkeyChange implements Serializable { public long mKeyId; public Integer mFlags; + // this is a long unix timestamp, in seconds (NOT MILLISECONDS!) public Long mExpiry; public SubkeyChange(long keyId, Integer flags, Long expiry) { mKeyId = keyId; -- cgit v1.2.3 From 20b28b52073df0dacff96f5ade26e5dc079ed248 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Fri, 11 Jul 2014 15:42:02 +0200 Subject: modifyKey: proper expiry check during SubkeyAdd --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index e68c871ed..8dbc2208c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -333,7 +333,7 @@ public class PgpKeyOperation { // 2b. Add revocations for revoked user ids for (String userId : saveParcel.mRevokeUserIds) { log.add(LogLevel.INFO, LogType.MSG_MF_UID_REVOKE, indent); - // a duplicate revocatin will be removed during canonicalization, so no need to + // a duplicate revocation will be removed during canonicalization, so no need to // take care of that here. PGPSignature cert = generateRevocationSignature(masterPrivateKey, masterPublicKey, userId); @@ -523,7 +523,7 @@ public class PgpKeyOperation { for (SaveKeyringParcel.SubkeyAdd add : saveParcel.mAddSubKeys) { try { - if (add.mExpiry != null && new Date(add.mExpiry).before(new Date())) { + if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent +1); return null; } -- cgit v1.2.3 From 0e3327c65c670a0d910abd6760ed0ece298dcfbb Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 12 Jul 2014 01:29:06 +0200 Subject: createKey: better logging, handle empty user id case --- .../keychain/pgp/PgpKeyOperation.java | 100 ++++++++++++--------- .../keychain/service/OperationResultParcel.java | 5 ++ 2 files changed, 61 insertions(+), 44 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 8dbc2208c..3e7e9d98e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -102,11 +102,12 @@ public class PgpKeyOperation { } /** Creates new secret key. */ - private PGPKeyPair createKey(int algorithmChoice, int keySize) throws PgpGeneralMsgIdException { + private PGPKeyPair createKey(int algorithmChoice, int keySize, OperationLog log, int indent) { try { if (keySize < 512) { - throw new PgpGeneralMsgIdException(R.string.error_key_size_minimum512bit); + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_KEYSIZE_512, indent); + return null; } int algorithm; @@ -141,7 +142,8 @@ public class PgpKeyOperation { } default: { - throw new PgpGeneralMsgIdException(R.string.error_unknown_algorithm_choice); + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_UNKNOWN_ALGO, indent); + return null; } } @@ -155,7 +157,9 @@ public class PgpKeyOperation { } catch(InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } catch(PGPException e) { - throw new PgpGeneralMsgIdException(R.string.msg_mf_error_pgp, e); + Log.e(Constants.TAG, "internal pgp error", e); + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_INTERNAL_PGP, indent); + return null; } } @@ -168,21 +172,32 @@ public class PgpKeyOperation { indent += 1; updateProgress(R.string.progress_building_key, 0, 100); - if (saveParcel.mAddSubKeys == null || saveParcel.mAddSubKeys.isEmpty()) { + if (saveParcel.mAddSubKeys.isEmpty()) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_MASTER, indent); return null; } + if (saveParcel.mAddUserIds.isEmpty()) { + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_USER_ID, indent); + return null; + } + SubkeyAdd add = saveParcel.mAddSubKeys.remove(0); if ((add.mFlags & KeyFlags.CERTIFY_OTHER) != KeyFlags.CERTIFY_OTHER) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_CERTIFY, indent); return null; } - PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize); + PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); if (add.mAlgorithm == Constants.choice.algorithm.elgamal) { - throw new PgpGeneralMsgIdException(R.string.error_master_key_must_not_be_el_gamal); + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_MASTER_ELGAMAL, indent); + return null; + } + + // return null if this failed (it will already have been logged by createKey) + if (keyPair == null) { + return null; } // define hashing and signing algos @@ -201,14 +216,12 @@ public class PgpKeyOperation { return internal(sKR, masterSecretKey, add.mFlags, saveParcel, "", log, indent); } catch (PGPException e) { + log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_INTERNAL_PGP, indent); Log.e(Constants.TAG, "pgp error encoding key", e); return null; } catch (IOException e) { Log.e(Constants.TAG, "io error encoding key", e); return null; - } catch (PgpGeneralMsgIdException e) { - Log.e(Constants.TAG, "pgp msg id error", e); - return null; } } @@ -521,47 +534,46 @@ public class PgpKeyOperation { // 5. Generate and add new subkeys for (SaveKeyringParcel.SubkeyAdd add : saveParcel.mAddSubKeys) { - try { - - if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) { - log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent +1); - return null; - } - - log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent); - // generate a new secret key (privkey only for now) - PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize); - - // add subkey binding signature (making this a sub rather than master key) - PGPPublicKey pKey = keyPair.getPublicKey(); - PGPSignature cert = generateSubkeyBindingSignature( - masterPublicKey, masterPrivateKey, keyPair.getPrivateKey(), pKey, - add.mFlags, add.mExpiry == null ? 0 : add.mExpiry); - pKey = PGPPublicKey.addSubkeyBindingCertification(pKey, cert); + if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) { + log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent +1); + return null; + } - PGPSecretKey sKey; { - // define hashing and signing algos - PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder() - .build().get(HashAlgorithmTags.SHA1); + log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent); - // Build key encrypter and decrypter based on passphrase - PBESecretKeyEncryptor keyEncryptor = new JcePBESecretKeyEncryptorBuilder( - PGPEncryptedData.CAST5, sha1Calc) - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray()); + // generate a new secret key (privkey only for now) + PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); + if(keyPair == null) { + return null; + } - sKey = new PGPSecretKey(keyPair.getPrivateKey(), pKey, - sha1Calc, false, keyEncryptor); - } + // add subkey binding signature (making this a sub rather than master key) + PGPPublicKey pKey = keyPair.getPublicKey(); + PGPSignature cert = generateSubkeyBindingSignature( + masterPublicKey, masterPrivateKey, keyPair.getPrivateKey(), pKey, + add.mFlags, add.mExpiry == null ? 0 : add.mExpiry); + pKey = PGPPublicKey.addSubkeyBindingCertification(pKey, cert); + + PGPSecretKey sKey; { + // define hashing and signing algos + PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder() + .build().get(HashAlgorithmTags.SHA1); + + // Build key encrypter and decrypter based on passphrase + PBESecretKeyEncryptor keyEncryptor = new JcePBESecretKeyEncryptorBuilder( + PGPEncryptedData.CAST5, sha1Calc) + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("".toCharArray()); + + sKey = new PGPSecretKey(keyPair.getPrivateKey(), pKey, + sha1Calc, false, keyEncryptor); + } - log.add(LogLevel.DEBUG, LogType.MSG_MF_SUBKEY_NEW_ID, - indent+1, PgpKeyHelper.convertKeyIdToHex(sKey.getKeyID())); + log.add(LogLevel.DEBUG, LogType.MSG_MF_SUBKEY_NEW_ID, + indent+1, PgpKeyHelper.convertKeyIdToHex(sKey.getKeyID())); - sKR = PGPSecretKeyRing.insertSecretKey(sKR, sKey); + sKR = PGPSecretKeyRing.insertSecretKey(sKR, sKey); - } catch (PgpGeneralMsgIdException e) { - return null; - } } // 6. If requested, change passphrase diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index c160da483..67386da06 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -246,7 +246,12 @@ public class OperationResultParcel implements Parcelable { // secret key create MSG_CR (R.string.msg_cr), MSG_CR_ERROR_NO_MASTER (R.string.msg_cr_error_no_master), + MSG_CR_ERROR_NO_USER_ID (R.string.msg_cr_error_no_user_id), MSG_CR_ERROR_NO_CERTIFY (R.string.msg_cr_error_no_certify), + MSG_CR_ERROR_KEYSIZE_512 (R.string.msg_cr_error_keysize_512), + MSG_CR_ERROR_UNKNOWN_ALGO (R.string.msg_cr_error_unknown_algo), + MSG_CR_ERROR_INTERNAL_PGP (R.string.msg_cr_error_internal_pgp), + MSG_CR_ERROR_MASTER_ELGAMAL (R.string.msg_cr_error_master_elgamal), // secret key modify MSG_MF (R.string.msg_mr), -- cgit v1.2.3 From f82093c666a443cda0985a017f907b6c25977565 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 12 Jul 2014 02:02:37 +0200 Subject: modifyKey: error out on integrity check fails --- .../sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 16 +++++++++------- .../sufficientlysecure/keychain/util/ProgressScaler.java | 12 +++++++++--- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 3e7e9d98e..bd8a9201e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -188,14 +188,14 @@ public class PgpKeyOperation { return null; } - PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); - if (add.mAlgorithm == Constants.choice.algorithm.elgamal) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_MASTER_ELGAMAL, indent); return null; } - // return null if this failed (it will already have been logged by createKey) + PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); + + // return null if this failed (an error will already have been logged by createKey) if (keyPair == null) { return null; } @@ -319,9 +319,10 @@ public class PgpKeyOperation { Iterator it = modifiedPublicKey.getSignaturesForID(userId); if (it != null) { for (PGPSignature cert : new IterableIterator(it)) { - // if it's not a self cert, never mind if (cert.getKeyID() != masterPublicKey.getKeyID()) { - continue; + // foreign certificate?! error error error + log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_INTEGRITY, indent); + return null; } if (cert.getSignatureType() == PGPSignature.CERTIFICATION_REVOCATION || cert.getSignatureType() == PGPSignature.NO_CERTIFICATION @@ -369,9 +370,10 @@ public class PgpKeyOperation { // noinspection unchecked for (PGPSignature cert : new IterableIterator( modifiedPublicKey.getSignaturesForID(userId))) { - // if it's not a self cert, never mind if (cert.getKeyID() != masterPublicKey.getKeyID()) { - continue; + // foreign certificate?! error error error + log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_INTEGRITY, indent); + return null; } // we know from canonicalization that if there is any revocation here, it // is valid and not superseded by a newer certification. diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java index 5553ea5d2..869eea03f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java @@ -39,15 +39,21 @@ public class ProgressScaler implements Progressable { * Set progress of ProgressDialog by sending message to handler on UI thread */ public void setProgress(String message, int progress, int max) { - mWrapped.setProgress(message, mFrom + progress * (mTo - mFrom) / max, mMax); + if (mWrapped != null) { + mWrapped.setProgress(message, mFrom + progress * (mTo - mFrom) / max, mMax); + } } public void setProgress(int resourceId, int progress, int max) { - mWrapped.setProgress(resourceId, progress, mMax); + if (mWrapped != null) { + mWrapped.setProgress(resourceId, progress, mMax); + } } public void setProgress(int progress, int max) { - mWrapped.setProgress(progress, max); + if (mWrapped != null) { + mWrapped.setProgress(progress, max); + } } } -- cgit v1.2.3 From af90db96a5d05bb37985afa7d01246fe963674f0 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 10:51:12 +0200 Subject: new PassphraseCache, storing UserIDs as well --- .../keychain/service/PassphraseCacheService.java | 58 ++++++++++++++++++---- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 28f230f71..9a68b8367 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -37,6 +37,7 @@ import android.support.v4.util.LongSparseArray; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; @@ -54,6 +55,8 @@ public class PassphraseCacheService extends Service { + "PASSPHRASE_CACHE_ADD"; public static final String ACTION_PASSPHRASE_CACHE_GET = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_GET"; + public static final String ACTION_PASSPHRASE_CACHE_PURGE = Constants.INTENT_PREFIX + + "PASSPHRASE_CACHE_PURGE"; public static final String BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_BROADCAST"; @@ -62,13 +65,14 @@ public class PassphraseCacheService extends Service { public static final String EXTRA_KEY_ID = "key_id"; public static final String EXTRA_PASSPHRASE = "passphrase"; public static final String EXTRA_MESSENGER = "messenger"; + public static final String EXTRA_USERID = "userid"; private static final int REQUEST_ID = 0; private static final long DEFAULT_TTL = 15; private BroadcastReceiver mIntentReceiver; - private LongSparseArray mPassphraseCache = new LongSparseArray(); + private LongSparseArray mPassphraseCache = new LongSparseArray(); Context mContext; @@ -81,14 +85,17 @@ public class PassphraseCacheService extends Service { * @param keyId * @param passphrase */ - public static void addCachedPassphrase(Context context, long keyId, String passphrase) { + public static void addCachedPassphrase(Context context, long keyId, String passphrase, + String primaryUserId) { Log.d(Constants.TAG, "PassphraseCacheService.cacheNewPassphrase() for " + keyId); Intent intent = new Intent(context, PassphraseCacheService.class); intent.setAction(ACTION_PASSPHRASE_CACHE_ADD); + intent.putExtra(EXTRA_TTL, Preferences.getPreferences(context).getPassphraseCacheTtl()); intent.putExtra(EXTRA_PASSPHRASE, passphrase); intent.putExtra(EXTRA_KEY_ID, keyId); + intent.putExtra(EXTRA_USERID, primaryUserId); context.startService(intent); } @@ -159,11 +166,11 @@ public class PassphraseCacheService extends Service { // passphrase for symmetric encryption? if (keyId == Constants.key.symmetric) { Log.d(Constants.TAG, "PassphraseCacheService.getCachedPassphraseImpl() for symmetric encryption"); - String cachedPassphrase = mPassphraseCache.get(Constants.key.symmetric); + String cachedPassphrase = mPassphraseCache.get(Constants.key.symmetric).getPassphrase(); if (cachedPassphrase == null) { return null; } - addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase); + addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, "Password"); return cachedPassphrase; } @@ -176,13 +183,17 @@ public class PassphraseCacheService extends Service { if (!key.hasPassphrase()) { Log.d(Constants.TAG, "Key has no passphrase! Caches and returns empty passphrase!"); - addCachedPassphrase(this, keyId, ""); + try { + addCachedPassphrase(this, keyId, "", key.getPrimaryUserId()); + } catch (PgpGeneralException e) { + Log.d(Constants.TAG, "PgpGeneralException occured"); + } return ""; } // get cached passphrase - String cachedPassphrase = mPassphraseCache.get(keyId); - if (cachedPassphrase == null) { + CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId); + if (cachedPassphrase == null && cachedPassphrase.getPassphrase() != null) { Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null"); // not really an error, just means the passphrase is not cached but not empty either return null; @@ -190,8 +201,8 @@ public class PassphraseCacheService extends Service { // set it again to reset the cache life cycle Log.d(Constants.TAG, "PassphraseCacheService Cache passphrase again when getting it!"); - addCachedPassphrase(this, keyId, cachedPassphrase); - return cachedPassphrase; + addCachedPassphrase(this, keyId, cachedPassphrase.getPassphrase(), cachedPassphrase.getPrimaryUserID()); + return cachedPassphrase.getPassphrase(); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "PassphraseCacheService Passphrase for unknown key was requested!"); @@ -256,14 +267,16 @@ public class PassphraseCacheService extends Service { if (ACTION_PASSPHRASE_CACHE_ADD.equals(intent.getAction())) { long ttl = intent.getLongExtra(EXTRA_TTL, DEFAULT_TTL); long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); + String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE); + String primaryUserID = intent.getStringExtra(EXTRA_USERID); Log.d(Constants.TAG, "PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " + keyId + ", ttl: " + ttl); // add keyId and passphrase to memory - mPassphraseCache.put(keyId, passphrase); + mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID)); if (ttl > 0) { // register new alarm with keyId for this passphrase @@ -341,4 +354,27 @@ public class PassphraseCacheService extends Service { private final IBinder mBinder = new PassphraseCacheBinder(); -} + public class CachedPassphrase { + private String primaryUserID = ""; + private String passphrase = ""; + + public CachedPassphrase(String passphrase, String primaryUserID) { + setPassphrase(passphrase); + setPrimaryUserID(primaryUserID); + } + + public String getPrimaryUserID() { + return primaryUserID; + } + public String getPassphrase() { + return passphrase; + } + + public void setPrimaryUserID(String primaryUserID) { + this.primaryUserID = primaryUserID; + } + public void setPassphrase(String passphrase) { + this.passphrase = passphrase; + } + } +} \ No newline at end of file -- cgit v1.2.3 From cf40517eaca2c852a0a0022b93c46e7a41766d80 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:27:19 +0200 Subject: Implemented Notification, no fallback yet --- .../keychain/service/KeychainIntentService.java | 3 +- .../keychain/service/PassphraseCacheService.java | 72 +++++++++++++++++++++- .../ui/dialog/PassphraseDialogFragment.java | 15 ++++- 3 files changed, 83 insertions(+), 7 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index c4c31bdad..10faa8786 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -44,6 +44,7 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.pgp.WrappedPublicKey; import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; @@ -351,7 +352,7 @@ public class KeychainIntentService extends IntentService // cache new passphrase if (saveParcel.newPassphrase != null) { PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), - saveParcel.newPassphrase); + saveParcel.newPassphrase, ring.getPublicKey().getPrimaryUserId()); } } catch (ProviderHelper.NotFoundException e) { sendErrorToHandler(e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9a68b8367..9d998f18f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.service; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.Service; +import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -32,9 +33,12 @@ import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.os.RemoteException; + import android.support.v4.util.LongSparseArray; +import android.support.v4.app.NotificationCompat; import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; @@ -70,6 +74,8 @@ public class PassphraseCacheService extends Service { private static final int REQUEST_ID = 0; private static final long DEFAULT_TTL = 15; + private static final int NOTIFICATION_ID = 1; + private BroadcastReceiver mIntentReceiver; private LongSparseArray mPassphraseCache = new LongSparseArray(); @@ -193,7 +199,7 @@ public class PassphraseCacheService extends Service { // get cached passphrase CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId); - if (cachedPassphrase == null && cachedPassphrase.getPassphrase() != null) { + if (cachedPassphrase == null) { Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null"); // not really an error, just means the passphrase is not cached but not empty either return null; @@ -273,9 +279,9 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " - + keyId + ", ttl: " + ttl); + + keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID); - // add keyId and passphrase to memory + // add keyId, passphrase and primary user id to memory mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID)); if (ttl > 0) { @@ -284,6 +290,9 @@ public class PassphraseCacheService extends Service { AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId)); } + + updateNotifications(); + } else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) { long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER); @@ -299,6 +308,17 @@ public class PassphraseCacheService extends Service { } catch (RemoteException e) { Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e); } + } else if (ACTION_PASSPHRASE_CACHE_PURGE.equals(intent.getAction())) { + AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); + + // Stop all ttl alarms + for(int i = 0; i < mPassphraseCache.size(); i++) { + am.cancel(buildIntent(this, mPassphraseCache.keyAt(i))); + } + + mPassphraseCache.clear(); + + updateNotifications(); } else { Log.e(Constants.TAG, "PassphraseCacheService Intent or Intent Action not supported!"); } @@ -324,6 +344,52 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "PassphraseCacheServic No passphrases remaining in memory, stopping service!"); stopSelf(); } + + updateNotifications(); + } + + private void updateNotifications() { + NotificationManager notificationManager = + (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + if(mPassphraseCache.size() > 0) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain") + .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + + inboxStyle.setBigContentTitle("Cached Passphrases:"); + + // Moves events into the big view + for (int i = 0; i < mPassphraseCache.size(); i++) { + inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); + } + + // Moves the big view style object into the notification object. + builder.setStyle(inboxStyle); + + // Add purging action + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + builder.addAction( + R.drawable.abc_ic_clear_normal, + "Purge Cache", + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + + } else { + notificationManager.cancel(NOTIFICATION_ID); + } } @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 59e4d8dee..5bbbf20a7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -189,7 +189,8 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // Early breakout if we are dealing with a symmetric key if (secretRing == null) { - PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, passphrase); + PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, + passphrase, "Password"); // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); @@ -228,10 +229,18 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // cache the new passphrase Log.d(Constants.TAG, "Everything okay! Caching entered passphrase"); - PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase); + + try { + PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, + secretRing.getPrimaryUserId()); + } catch(PgpGeneralException e) { + Log.e(Constants.TAG, e.getMessage()); + } + if (unlockedSecretKey.getKeyId() != masterKeyId) { PassphraseCacheService.addCachedPassphrase( - activity, unlockedSecretKey.getKeyId(), passphrase); + activity, unlockedSecretKey.getKeyId(), passphrase, + unlockedSecretKey.getPrimaryUserId()); } // also return passphrase back to activity -- cgit v1.2.3 From 2568ea4b2edaa1a13b15395d1951e7f941fba073 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:42:48 +0200 Subject: Added Purging for Android < 4.1 --- .../keychain/service/PassphraseCacheService.java | 79 ++++++++++++++-------- 1 file changed, 51 insertions(+), 28 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9d998f18f..84ad441cb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -26,6 +26,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.HandlerThread; @@ -353,39 +354,61 @@ public class PassphraseCacheService extends Service { (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if(mPassphraseCache.size() > 0) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain") - .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain") + .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); - NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - inboxStyle.setBigContentTitle("Cached Passphrases:"); + inboxStyle.setBigContentTitle("Cached Passphrases:"); - // Moves events into the big view - for (int i = 0; i < mPassphraseCache.size(); i++) { - inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); - } + // Moves events into the big view + for (int i = 0; i < mPassphraseCache.size(); i++) { + inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); + } - // Moves the big view style object into the notification object. - builder.setStyle(inboxStyle); - - // Add purging action - Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); - builder.addAction( - R.drawable.abc_ic_clear_normal, - "Purge Cache", - PendingIntent.getService( - getApplicationContext(), - 0, - intent, - PendingIntent.FLAG_UPDATE_CURRENT - ) - ); - - notificationManager.notify(NOTIFICATION_ID, builder.build()); + // Moves the big view style object into the notification object. + builder.setStyle(inboxStyle); + + // Add purging action + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + builder.addAction( + R.drawable.abc_ic_clear_normal, + "Purge Cache", + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } else { // Fallback, since expandable notifications weren't available back then + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle("Open Keychain has cached " + mPassphraseCache.size() + " keys.") + .setContentText("Click to purge the cache"); + + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + + builder.setContentIntent( + PendingIntent.getService( + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT + ) + ); + + notificationManager.notify(NOTIFICATION_ID, builder.build()); + } } else { notificationManager.cancel(NOTIFICATION_ID); -- cgit v1.2.3 From 7a3fe61a1f497ba431ed5fe3c68b11ed9578eaba Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 12:52:44 +0200 Subject: Put text into strings.xml, for internationalization --- .../keychain/service/PassphraseCacheService.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 84ad441cb..9b868c859 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -358,12 +358,12 @@ public class PassphraseCacheService extends Service { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain") - .setContentText("Open Keychain has cached " + mPassphraseCache.size() + " keys."); + .setContentTitle(getString(R.string.app_name)) + .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - inboxStyle.setBigContentTitle("Cached Passphrases:"); + inboxStyle.setBigContentTitle(getString(R.string.passp_cache_notif_keys)); // Moves events into the big view for (int i = 0; i < mPassphraseCache.size(); i++) { @@ -378,7 +378,7 @@ public class PassphraseCacheService extends Service { intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); builder.addAction( R.drawable.abc_ic_clear_normal, - "Purge Cache", + getString(R.string.passp_cache_notif_purge), PendingIntent.getService( getApplicationContext(), 0, @@ -392,8 +392,8 @@ public class PassphraseCacheService extends Service { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("Open Keychain has cached " + mPassphraseCache.size() + " keys.") - .setContentText("Click to purge the cache"); + .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))) + .setContentText(getString(R.string.passp_cache_notif_click_to_purge)); Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); -- cgit v1.2.3 From 079194abe5b48c2b6a5cf943b45a67b956937435 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 18:12:03 +0200 Subject: Fixed issues discussed in #713 --- .../keychain/service/PassphraseCacheService.java | 20 ++++++++++---------- .../keychain/ui/dialog/PassphraseDialogFragment.java | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 9b868c859..fb1da28fc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -60,8 +60,8 @@ public class PassphraseCacheService extends Service { + "PASSPHRASE_CACHE_ADD"; public static final String ACTION_PASSPHRASE_CACHE_GET = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_GET"; - public static final String ACTION_PASSPHRASE_CACHE_PURGE = Constants.INTENT_PREFIX - + "PASSPHRASE_CACHE_PURGE"; + public static final String ACTION_PASSPHRASE_CACHE_CLEAR = Constants.INTENT_PREFIX + + "PASSPHRASE_CACHE_CLEAR"; public static final String BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE = Constants.INTENT_PREFIX + "PASSPHRASE_CACHE_BROADCAST"; @@ -177,7 +177,7 @@ public class PassphraseCacheService extends Service { if (cachedPassphrase == null) { return null; } - addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, "Password"); + addCachedPassphrase(this, Constants.key.symmetric, cachedPassphrase, getString(R.string.passp_cache_notif_pwd)); return cachedPassphrase; } @@ -309,7 +309,7 @@ public class PassphraseCacheService extends Service { } catch (RemoteException e) { Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e); } - } else if (ACTION_PASSPHRASE_CACHE_PURGE.equals(intent.getAction())) { + } else if (ACTION_PASSPHRASE_CACHE_CLEAR.equals(intent.getAction())) { AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // Stop all ttl alarms @@ -375,10 +375,10 @@ public class PassphraseCacheService extends Service { // Add purging action Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); builder.addAction( R.drawable.abc_ic_clear_normal, - getString(R.string.passp_cache_notif_purge), + getString(R.string.passp_cache_notif_clear), PendingIntent.getService( getApplicationContext(), 0, @@ -393,10 +393,10 @@ public class PassphraseCacheService extends Service { builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))) - .setContentText(getString(R.string.passp_cache_notif_click_to_purge)); + .setContentText(getString(R.string.passp_cache_notif_click_to_clear)); Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_PURGE); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); builder.setContentIntent( PendingIntent.getService( @@ -444,8 +444,8 @@ public class PassphraseCacheService extends Service { private final IBinder mBinder = new PassphraseCacheBinder(); public class CachedPassphrase { - private String primaryUserID = ""; - private String passphrase = ""; + private String primaryUserID; + private String passphrase; public CachedPassphrase(String passphrase, String primaryUserID) { setPassphrase(passphrase); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 5bbbf20a7..4d0b73d30 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -190,7 +190,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // Early breakout if we are dealing with a symmetric key if (secretRing == null) { PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, - passphrase, "Password"); + passphrase, getString(R.string.passp_cache_notif_pwd)); // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); @@ -234,7 +234,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, secretRing.getPrimaryUserId()); } catch(PgpGeneralException e) { - Log.e(Constants.TAG, e.getMessage()); + Log.e(Constants.TAG, "adding of a passhrase failed", e); } if (unlockedSecretKey.getKeyId() != masterKeyId) { -- cgit v1.2.3 From 92c66743e07b77da21af6236689a276eb23a9b1c Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 15:12:35 +0200 Subject: Added Preference for concealing the PgpApplication --- .../java/org/sufficientlysecure/keychain/Constants.java | 1 + .../sufficientlysecure/keychain/helper/Preferences.java | 10 ++++++++++ .../keychain/ui/PreferencesActivity.java | 17 +++++++++++++++++ 3 files changed, 28 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index e1bf1afa4..86bc3fa5b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -68,6 +68,7 @@ public final class Constants { public static final String FORCE_V3_SIGNATURES = "forceV3Signatures"; public static final String KEY_SERVERS = "keyServers"; public static final String KEY_SERVERS_DEFAULT_VERSION = "keyServersDefaultVersion"; + public static final String CONCEAL_PGP_APPLICATION = "concealPgpApplication"; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java index 6f3d38ccd..fd8267a59 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java @@ -187,4 +187,14 @@ public class Preferences { .commit(); } } + + public void setConcealPgpApplication(boolean conceal) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(Constants.Pref.CONCEAL_PGP_APPLICATION, conceal); + editor.commit(); + } + + public boolean getConcealPgpApplication() { + return mSharedPreferences.getBoolean(Constants.Pref.CONCEAL_PGP_APPLICATION, false); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java index 448d29156..dcacdbc9d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/PreferencesActivity.java @@ -121,6 +121,9 @@ public class PreferencesActivity extends PreferenceActivity { initializeForceV3Signatures( (CheckBoxPreference) findPreference(Constants.Pref.FORCE_V3_SIGNATURES)); + initializeConcealPgpApplication( + (CheckBoxPreference) findPreference(Constants.Pref.CONCEAL_PGP_APPLICATION)); + } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // Load the legacy preferences headers addPreferencesFromResource(R.xml.preference_headers_legacy); @@ -264,6 +267,9 @@ public class PreferencesActivity extends PreferenceActivity { initializeForceV3Signatures( (CheckBoxPreference) findPreference(Constants.Pref.FORCE_V3_SIGNATURES)); + + initializeConcealPgpApplication( + (CheckBoxPreference) findPreference(Constants.Pref.CONCEAL_PGP_APPLICATION)); } } @@ -396,4 +402,15 @@ public class PreferencesActivity extends PreferenceActivity { } }); } + + private static void initializeConcealPgpApplication(final CheckBoxPreference mConcealPgpApplication) { + mConcealPgpApplication.setChecked(sPreferences.getConcealPgpApplication()); + mConcealPgpApplication.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + public boolean onPreferenceChange(Preference preference, Object newValue) { + mConcealPgpApplication.setChecked((Boolean) newValue); + sPreferences.setConcealPgpApplication((Boolean) newValue); + return false; + } + }); + } } -- cgit v1.2.3 From bd909375c2cac5f1b50838a9591eebbddc007b61 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Sat, 12 Jul 2014 19:24:51 +0200 Subject: Fixed misplaced bracket --- .../org/sufficientlysecure/keychain/service/PassphraseCacheService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index fb1da28fc..72c1a8f67 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -359,7 +359,7 @@ public class PassphraseCacheService extends Service { builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.app_name)) - .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))); + .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys), mPassphraseCache.size())); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); -- cgit v1.2.3 From 45dfb3974908d953ff656fb69696f69c0af017c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sat, 12 Jul 2014 20:39:23 +0200 Subject: more work on edit key --- .../keychain/ui/EditKeyFragment.java | 42 ++++- .../ui/dialog/ChangeExpiryDialogFragment.java | 187 +++++++++++++++++++++ 2 files changed, 222 insertions(+), 7 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 78ccafcbc..94ca883d6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -54,6 +54,7 @@ import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAddedAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAddedAdapter; +import org.sufficientlysecure.keychain.ui.dialog.ChangeExpiryDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditSubkeyDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.EditUserIdDialogFragment; import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment; @@ -61,6 +62,7 @@ import org.sufficientlysecure.keychain.ui.dialog.SetPassphraseDialogFragment; import org.sufficientlysecure.keychain.util.Log; import java.util.ArrayList; +import java.util.Date; public class EditKeyFragment extends LoaderFragment implements LoaderManager.LoaderCallbacks { @@ -357,13 +359,7 @@ public class EditKeyFragment extends LoaderFragment implements public void handleMessage(Message message) { switch (message.what) { case EditSubkeyDialogFragment.MESSAGE_CHANGE_EXPIRY: - // toggle -// if (mSaveKeyringParcel.changePrimaryUserId != null -// && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { -// mSaveKeyringParcel.changePrimaryUserId = null; -// } else { -// mSaveKeyringParcel.changePrimaryUserId = userId; -// } + editSubkeyExpiry(keyId); break; case EditSubkeyDialogFragment.MESSAGE_REVOKE: // toggle @@ -391,6 +387,38 @@ public class EditKeyFragment extends LoaderFragment implements }); } + private void editSubkeyExpiry(final long keyId) { + Handler returnHandler = new Handler() { + @Override + public void handleMessage(Message message) { + switch (message.what) { + case ChangeExpiryDialogFragment.MESSAGE_NEW_EXPIRY_DATE: + // toggle +// if (mSaveKeyringParcel.changePrimaryUserId != null +// && mSaveKeyringParcel.changePrimaryUserId.equals(userId)) { +// mSaveKeyringParcel.changePrimaryUserId = null; +// } else { +// mSaveKeyringParcel.changePrimaryUserId = userId; +// } + break; + } + getLoaderManager().getLoader(LOADER_ID_SUBKEYS).forceLoad(); + } + }; + + // Create a new Messenger for the communication back + final Messenger messenger = new Messenger(returnHandler); + + DialogFragmentWorkaround.INTERFACE.runnableRunDelayed(new Runnable() { + public void run() { + ChangeExpiryDialogFragment dialogFragment = + ChangeExpiryDialogFragment.newInstance(messenger, new Date(), new Date()); + + dialogFragment.show(getActivity().getSupportFragmentManager(), "editSubkeyExpiryDialog"); + } + }); + } + private void addUserId() { mUserIdsAddedAdapter.add(new UserIdsAddedAdapter.UserIdModel()); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java new file mode 100644 index 000000000..d5354a9f6 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ChangeExpiryDialogFragment.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui.dialog; + +import android.app.DatePickerDialog; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.os.RemoteException; +import android.support.v4.app.DialogFragment; +import android.text.format.DateUtils; +import android.widget.DatePicker; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.util.Log; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +public class ChangeExpiryDialogFragment extends DialogFragment { + private static final String ARG_MESSENGER = "messenger"; + private static final String ARG_CREATION_DATE = "creation_date"; + private static final String ARG_EXPIRY_DATE = "expiry_date"; + + public static final int MESSAGE_NEW_EXPIRY_DATE = 1; + public static final String MESSAGE_DATA_EXPIRY_DATE = "expiry_date"; + + private Messenger mMessenger; + private Calendar mCreationCal; + private Calendar mExpiryCal; + + private int mDatePickerResultCount = 0; + private DatePickerDialog.OnDateSetListener mExpiryDateSetListener = + new DatePickerDialog.OnDateSetListener() { + public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { + // Note: Ignore results after the first one - android sends multiples. + if (mDatePickerResultCount++ == 0) { + Calendar selectedCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + selectedCal.set(year, monthOfYear, dayOfMonth); + if (mExpiryCal != null) { + long numDays = (selectedCal.getTimeInMillis() / 86400000) + - (mExpiryCal.getTimeInMillis() / 86400000); + if (numDays > 0) { + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, selectedCal.getTime()); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } else { + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, selectedCal.getTime()); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } + } + }; + + public class ExpiryDatePickerDialog extends DatePickerDialog { + + public ExpiryDatePickerDialog(Context context, OnDateSetListener callBack, + int year, int monthOfYear, int dayOfMonth) { + super(context, callBack, year, monthOfYear, dayOfMonth); + } + + // set permanent title + public void setTitle(CharSequence title) { + super.setTitle(getContext().getString(R.string.expiry_date_dialog_title)); + } + } + + /** + * Creates new instance of this dialog fragment + */ + public static ChangeExpiryDialogFragment newInstance(Messenger messenger, + Date creationDate, Date expiryDate) { + ChangeExpiryDialogFragment frag = new ChangeExpiryDialogFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_MESSENGER, messenger); + args.putSerializable(ARG_CREATION_DATE, creationDate); + args.putSerializable(ARG_EXPIRY_DATE, expiryDate); + + frag.setArguments(args); + + return frag; + } + + /** + * Creates dialog + */ + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + mMessenger = getArguments().getParcelable(ARG_MESSENGER); + Date creationDate = (Date) getArguments().getSerializable(ARG_CREATION_DATE); + Date expiryDate = (Date) getArguments().getSerializable(ARG_EXPIRY_DATE); + + mCreationCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + mCreationCal.setTime(creationDate); + mExpiryCal = Calendar.getInstance(TimeZone.getTimeZone("UTC")); + mExpiryCal.setTime(expiryDate); + + /* + * Using custom DatePickerDialog which overrides the setTitle because + * the DatePickerDialog title is buggy (unix warparound bug). + * See: https://code.google.com/p/android/issues/detail?id=49066 + */ + DatePickerDialog dialog = new ExpiryDatePickerDialog(getActivity(), + mExpiryDateSetListener, mExpiryCal.get(Calendar.YEAR), mExpiryCal.get(Calendar.MONTH), + mExpiryCal.get(Calendar.DAY_OF_MONTH)); + mDatePickerResultCount = 0; + dialog.setCancelable(true); + dialog.setButton(Dialog.BUTTON_NEGATIVE, + getActivity().getString(R.string.btn_no_date), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + // Note: Ignore results after the first one - android sends multiples. + if (mDatePickerResultCount++ == 0) { + // none expiry dates corresponds to a null message + Bundle data = new Bundle(); + data.putSerializable(MESSAGE_DATA_EXPIRY_DATE, null); + sendMessageToHandler(MESSAGE_NEW_EXPIRY_DATE, data); + } + } + } + ); + + // setCalendarViewShown() is supported from API 11 onwards. + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + // Hide calendarView in tablets because of the unix warparound bug. + dialog.getDatePicker().setCalendarViewShown(false); + } + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { + // will crash with IllegalArgumentException if we set a min date + // that is not before expiry + if (mCreationCal != null && mCreationCal.before(mExpiryCal)) { + dialog.getDatePicker().setMinDate(mCreationCal.getTime().getTime() + + DateUtils.DAY_IN_MILLIS); + } else { + // When created date isn't available + dialog.getDatePicker().setMinDate(mExpiryCal.getTime().getTime() + + DateUtils.DAY_IN_MILLIS); + } + } + + return dialog; + } + + /** + * Send message back to handler which is initialized in a activity + * + * @param what Message integer you want to send + */ + private void sendMessageToHandler(Integer what, Bundle data) { + Message msg = Message.obtain(); + msg.what = what; + if (data != null) { + msg.setData(data); + } + + try { + mMessenger.send(msg); + } catch (RemoteException e) { + Log.w(Constants.TAG, "Exception sending message, Is handler present?", e); + } catch (NullPointerException e) { + Log.w(Constants.TAG, "Messenger is null!", e); + } + } +} -- cgit v1.2.3 From 29145e49c96998f220485a8e46cd2b62665b00de Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 15 Jul 2014 19:17:08 +0200 Subject: merge: different msg if nothing was merged --- .../java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java | 8 ++++++-- .../org/sufficientlysecure/keychain/provider/ProviderHelper.java | 2 +- .../keychain/service/OperationResultParcel.java | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 691e867b2..9ddfd3405 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -748,8 +748,12 @@ public class UncachedKeyRing { } - log.add(LogLevel.DEBUG, LogType.MSG_MG_FOUND_NEW, - indent, Integer.toString(newCerts)); + if (newCerts > 0) { + log.add(LogLevel.DEBUG, LogType.MSG_MG_FOUND_NEW, indent, + Integer.toString(newCerts)); + } else { + log.add(LogLevel.DEBUG, LogType.MSG_MG_UNCHANGED, indent); + } return new UncachedKeyRing(result); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index c338c9d95..4c09bad45 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -792,7 +792,7 @@ public class ProviderHelper { try { UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncachedKeyRing(); - // Merge data from new public ring into secret one + // Merge data from new secret ring into public one publicRing = oldPublicRing.merge(secretRing, mLog, mIndent); if (publicRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 67386da06..705d6afaf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -242,6 +242,7 @@ public class OperationResultParcel implements Parcelable { MSG_MG_HETEROGENEOUS (R.string.msg_mg_heterogeneous), MSG_MG_NEW_SUBKEY (R.string.msg_mg_new_subkey), MSG_MG_FOUND_NEW (R.string.msg_mg_found_new), + MSG_MG_UNCHANGED (R.string.msg_mg_unchanged), // secret key create MSG_CR (R.string.msg_cr), -- cgit v1.2.3 From 72237a08924e60f0ee5d57f5b1e6735ece9172c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Tue, 15 Jul 2014 19:22:40 +0200 Subject: some fixes for edit --- .../org/sufficientlysecure/keychain/provider/ProviderHelper.java | 2 +- .../java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index c338c9d95..ea0d2ea39 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -756,7 +756,7 @@ public class ProviderHelper { UncachedKeyRing oldSecretRing = getWrappedSecretKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new secret ring into old one - secretRing = oldSecretRing.merge(secretRing, mLog, mIndent); + secretRing = secretRing.merge(oldSecretRing, mLog, mIndent); // If this is null, there is an error in the log so we can just return if (secretRing == null) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 10729ef30..b41871a39 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -450,11 +450,12 @@ public class EditKeyFragment extends LoaderFragment implements } private void save(String passphrase) { - Log.d(Constants.TAG, "add userids to parcel: " + mUserIdsAddedAdapter.getDataAsStringList()); - Log.d(Constants.TAG, "mSaveKeyringParcel.mNewPassphrase: " + mSaveKeyringParcel.mNewPassphrase); - mSaveKeyringParcel.mAddUserIds = mUserIdsAddedAdapter.getDataAsStringList(); + Log.d(Constants.TAG, "mSaveKeyringParcel.mAddUserIds: " + mSaveKeyringParcel.mAddUserIds); + Log.d(Constants.TAG, "mSaveKeyringParcel.mNewPassphrase: " + mSaveKeyringParcel.mNewPassphrase); + Log.d(Constants.TAG, "mSaveKeyringParcel.mRevokeUserIds: " + mSaveKeyringParcel.mRevokeUserIds); + // Message is received after importing is done in KeychainIntentService KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( getActivity(), -- cgit v1.2.3 From 501d4b887a59ed4f5dea76013ece4294fe794b28 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 15 Jul 2014 19:31:27 +0200 Subject: signatures: a revocation reason does NOT determine if a cert is a revocation type --- .../main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java index c87b23222..df19930c4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java @@ -86,7 +86,7 @@ public class WrappedSignature { } public boolean isRevocation() { - return mSig.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.REVOCATION_REASON); + return mSig.getSignatureType() == PGPSignature.CERTIFICATION_REVOCATION; } public boolean isPrimaryUserId() { -- cgit v1.2.3 From 64b87f75be6e9d47ef2e799c8899cc00b751f528 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 15 Jul 2014 19:47:40 +0200 Subject: move getPublicKey into abstract WrappedKeyRing (also, fix getPrimaryUserId) --- .../org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java | 6 +++--- .../org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java | 10 +++++++++- .../sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java | 8 -------- .../org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java | 2 +- .../sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java | 4 ++-- .../sufficientlysecure/keychain/provider/ProviderHelper.java | 2 +- .../keychain/service/KeychainIntentService.java | 3 +-- .../org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java | 4 ++-- .../org/sufficientlysecure/keychain/ui/ViewCertActivity.java | 4 ++-- 9 files changed, 21 insertions(+), 22 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java index a5ccfbd3b..c279b7a9b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java @@ -258,7 +258,7 @@ public class PgpDecryptVerify { continue; } // get subkey which has been used for this encryption packet - secretEncryptionKey = secretKeyRing.getSubKey(encData.getKeyID()); + secretEncryptionKey = secretKeyRing.getSecretKey(encData.getKeyID()); if (secretEncryptionKey == null) { // continue with the next packet in the while loop continue; @@ -393,7 +393,7 @@ public class PgpDecryptVerify { signingRing = mProviderHelper.getWrappedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) ); - signingKey = signingRing.getSubkey(sigKeyId); + signingKey = signingRing.getPublicKey(sigKeyId); signatureIndex = i; } catch (ProviderHelper.NotFoundException e) { Log.d(Constants.TAG, "key not found!"); @@ -578,7 +578,7 @@ public class PgpDecryptVerify { signingRing = mProviderHelper.getWrappedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) ); - signingKey = signingRing.getSubkey(sigKeyId); + signingKey = signingRing.getPublicKey(sigKeyId); signatureIndex = i; } catch (ProviderHelper.NotFoundException e) { Log.d(Constants.TAG, "key not found!"); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java index 2fcd05204..ba5ebea4a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java @@ -39,7 +39,7 @@ public abstract class WrappedKeyRing extends KeyRing { } public String getPrimaryUserId() throws PgpGeneralException { - return (String) getRing().getPublicKey().getUserIDs().next(); + return getPublicKey().getPrimaryUserId(); }; public boolean isRevoked() throws PgpGeneralException { @@ -101,6 +101,14 @@ public abstract class WrappedKeyRing extends KeyRing { abstract public IterableIterator publicKeyIterator(); + public WrappedPublicKey getPublicKey() { + return new WrappedPublicKey(this, getRing().getPublicKey()); + } + + public WrappedPublicKey getPublicKey(long id) { + return new WrappedPublicKey(this, getRing().getPublicKey(id)); + } + public byte[] getEncoded() throws IOException { return getRing().getEncoded(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java index b2abf15a4..57d84072a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java @@ -44,14 +44,6 @@ public class WrappedPublicKeyRing extends WrappedKeyRing { getRing().encode(stream); } - public WrappedPublicKey getSubkey() { - return new WrappedPublicKey(this, getRing().getPublicKey()); - } - - public WrappedPublicKey getSubkey(long id) { - return new WrappedPublicKey(this, getRing().getPublicKey(id)); - } - /** Getter that returns the subkey that should be used for signing. */ WrappedPublicKey getEncryptionSubKey() throws PgpGeneralException { PGPPublicKey key = getRing().getPublicKey(getEncryptId()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java index ef8044a9b..067aaeda3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java @@ -175,7 +175,7 @@ public class WrappedSecretKey extends WrappedPublicKey { } // get the master subkey (which we certify for) - PGPPublicKey publicKey = publicKeyRing.getSubkey().getPublicKey(); + PGPPublicKey publicKey = publicKeyRing.getPublicKey().getPublicKey(); // fetch public key ring, add the certification and return it for (String userId : new IterableIterator(userIds.iterator())) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java index c737b7c46..5cb24cf88 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java @@ -41,11 +41,11 @@ public class WrappedSecretKeyRing extends WrappedKeyRing { return mRing; } - public WrappedSecretKey getSubKey() { + public WrappedSecretKey getSecretKey() { return new WrappedSecretKey(this, mRing.getSecretKey()); } - public WrappedSecretKey getSubKey(long id) { + public WrappedSecretKey getSecretKey(long id) { return new WrappedSecretKey(this, mRing.getSecretKey(id)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 991c9f3a8..2d524f5b0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -199,7 +199,7 @@ public class ProviderHelper { byte[] blob = cursor.getBlob(3); if (blob != null) { result.put(masterKeyId, - new WrappedPublicKeyRing(blob, hasAnySecret, verified).getSubkey()); + new WrappedPublicKeyRing(blob, hasAnySecret, verified).getPublicKey()); } } while (cursor.moveToNext()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 4e435253a..1e4e926e9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -44,7 +44,6 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.pgp.WrappedPublicKey; import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; @@ -546,7 +545,7 @@ public class KeychainIntentService extends IntentService ProviderHelper providerHelper = new ProviderHelper(this); WrappedPublicKeyRing publicRing = providerHelper.getWrappedPublicKeyRing(pubKeyId); WrappedSecretKeyRing secretKeyRing = providerHelper.getWrappedSecretKeyRing(masterKeyId); - WrappedSecretKey certificationKey = secretKeyRing.getSubKey(); + WrappedSecretKey certificationKey = secretKeyRing.getSecretKey(); if(!certificationKey.unlock(signaturePassphrase)) { throw new PgpGeneralException("Error extracting key (bad passphrase?)"); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java index d9deb802c..70ccb8800 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivityOld.java @@ -280,7 +280,7 @@ public class EditKeyActivityOld extends ActionBarActivity implements EditorListe Uri secretUri = KeyRings.buildUnifiedKeyRingUri(mDataUri); WrappedSecretKeyRing keyRing = new ProviderHelper(this).getWrappedSecretKeyRing(secretUri); - mMasterCanSign = keyRing.getSubKey().canCertify(); + mMasterCanSign = keyRing.getSecretKey().canCertify(); for (WrappedSecretKey key : keyRing.secretKeyIterator()) { // Turn into uncached instance mKeys.add(key.getUncached()); @@ -288,7 +288,7 @@ public class EditKeyActivityOld extends ActionBarActivity implements EditorListe } boolean isSet = false; - for (String userId : keyRing.getSubKey().getUserIds()) { + for (String userId : keyRing.getSecretKey().getUserIds()) { Log.d(Constants.TAG, "Added userId " + userId); if (!isSet) { isSet = true; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index c7fffe263..cfdea0611 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -149,8 +149,8 @@ public class ViewCertActivity extends ActionBarActivity providerHelper.getWrappedPublicKeyRing(sig.getKeyId()); try { - sig.init(signerRing.getSubkey()); - if (sig.verifySignature(signeeRing.getSubkey(), signeeUid)) { + sig.init(signerRing.getPublicKey()); + if (sig.verifySignature(signeeRing.getPublicKey(), signeeUid)) { mStatus.setText(R.string.cert_verify_ok); mStatus.setTextColor(getResources().getColor(R.color.result_green)); } else { -- cgit v1.2.3 From d3c54d5f129ca24cbfa08208fc5c79c626897d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 16 Jul 2014 00:22:45 +0200 Subject: Fallback if no primary user id exists --- .../keychain/keyimport/ImportKeysListEntry.java | 2 +- .../java/org/sufficientlysecure/keychain/pgp/KeyRing.java | 6 ++++-- .../org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java | 4 ++-- .../sufficientlysecure/keychain/pgp/UncachedPublicKey.java | 11 +++++++++++ .../org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java | 6 +++++- .../org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java | 2 +- .../keychain/provider/CachedPublicKeyRing.java | 4 ++++ .../keychain/service/KeychainIntentService.java | 2 +- .../keychain/service/PassphraseCacheService.java | 2 +- .../keychain/ui/EncryptAsymmetricFragment.java | 2 +- .../keychain/ui/dialog/PassphraseDialogFragment.java | 6 +++--- 11 files changed, 34 insertions(+), 13 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java index 0a49cb629..30e93f957 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/ImportKeysListEntry.java @@ -250,7 +250,7 @@ public class ImportKeysListEntry implements Serializable, Parcelable { mHashCode = key.hashCode(); - mPrimaryUserId = key.getPrimaryUserId(); + mPrimaryUserId = key.getPrimaryUserIdWithFallback(); mUserIds = key.getUnorderedUserIds(); // if there was no user id flagged as primary, use the first one diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index 47b827677..129ffba3e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -22,8 +22,10 @@ public abstract class KeyRing { abstract public String getPrimaryUserId() throws PgpGeneralException; - public String[] getSplitPrimaryUserId() throws PgpGeneralException { - return splitUserId(getPrimaryUserId()); + abstract public String getPrimaryUserIdWithFallback() throws PgpGeneralException; + + public String[] getSplitPrimaryUserIdWithFallback() throws PgpGeneralException { + return splitUserId(getPrimaryUserIdWithFallback()); } abstract public boolean isRevoked() throws PgpGeneralException; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java index c279b7a9b..db9e2c6c6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java @@ -409,7 +409,7 @@ public class PgpDecryptVerify { signatureResultBuilder.knownKey(true); signatureResultBuilder.keyId(signingRing.getMasterKeyId()); try { - signatureResultBuilder.userId(signingRing.getPrimaryUserId()); + signatureResultBuilder.userId(signingRing.getPrimaryUserIdWithFallback()); } catch(PgpGeneralException e) { Log.d(Constants.TAG, "No primary user id in key " + signingRing.getMasterKeyId()); } @@ -596,7 +596,7 @@ public class PgpDecryptVerify { signatureResultBuilder.knownKey(true); signatureResultBuilder.keyId(signingRing.getMasterKeyId()); try { - signatureResultBuilder.userId(signingRing.getPrimaryUserId()); + signatureResultBuilder.userId(signingRing.getPrimaryUserIdWithFallback()); } catch(PgpGeneralException e) { Log.d(Constants.TAG, "No primary user id in key " + signingRing.getMasterKeyId()); } 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 0bd2c2c02..ef5aa8e86 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -144,6 +144,17 @@ public class UncachedPublicKey { return found; } + /** + * Returns primary user id if existing. If not, return first encountered user id. + */ + public String getPrimaryUserIdWithFallback() { + String userId = getPrimaryUserId(); + if (userId == null) { + userId = (String) mPublicKey.getUserIDs().next(); + } + return userId; + } + public ArrayList getUnorderedUserIds() { ArrayList userIds = new ArrayList(); for (String userId : new IterableIterator(mPublicKey.getUserIDs())) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java index ba5ebea4a..a054255dc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java @@ -40,7 +40,11 @@ public abstract class WrappedKeyRing extends KeyRing { public String getPrimaryUserId() throws PgpGeneralException { return getPublicKey().getPrimaryUserId(); - }; + } + + public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + return getPublicKey().getPrimaryUserIdWithFallback(); + } public boolean isRevoked() throws PgpGeneralException { // Is the master key revoked? diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java index 067aaeda3..98ad2b706 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java @@ -97,7 +97,7 @@ public class WrappedSecretKey extends WrappedPublicKey { signatureGenerator.init(signatureType, mPrivateKey); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - spGen.setSignerUserID(false, mRing.getPrimaryUserId()); + spGen.setSignerUserID(false, mRing.getPrimaryUserIdWithFallback()); signatureGenerator.setHashedSubpackets(spGen.generate()); return signatureGenerator; } catch(PGPException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java index 48d40430a..34de0024d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/CachedPublicKeyRing.java @@ -70,6 +70,10 @@ public class CachedPublicKeyRing extends KeyRing { } } + public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + return getPrimaryUserId(); + } + public boolean isRevoked() throws PgpGeneralException { try { Object data = mProviderHelper.getGenericData(mUri, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 1e4e926e9..9a4cef2f1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -351,7 +351,7 @@ public class KeychainIntentService extends IntentService // cache new passphrase if (saveParcel.mNewPassphrase != null) { PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), - saveParcel.mNewPassphrase, ring.getPublicKey().getPrimaryUserId()); + saveParcel.mNewPassphrase, ring.getPublicKey().getPrimaryUserIdWithFallback()); } } catch (ProviderHelper.NotFoundException e) { sendErrorToHandler(e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 72c1a8f67..13d9b497f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -191,7 +191,7 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "Key has no passphrase! Caches and returns empty passphrase!"); try { - addCachedPassphrase(this, keyId, "", key.getPrimaryUserId()); + addCachedPassphrase(this, keyId, "", key.getPrimaryUserIdWithFallback()); } catch (PgpGeneralException e) { Log.d(Constants.TAG, "PgpGeneralException occured"); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java index 51963e963..dc0510189 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptAsymmetricFragment.java @@ -198,7 +198,7 @@ public class EncryptAsymmetricFragment extends Fragment { String[] userId; try { userId = mProviderHelper.getCachedPublicKeyRing( - KeyRings.buildUnifiedKeyRingUri(mSecretKeyId)).getSplitPrimaryUserId(); + KeyRings.buildUnifiedKeyRingUri(mSecretKeyId)).getSplitPrimaryUserIdWithFallback(); } catch (PgpGeneralException e) { userId = null; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 4d0b73d30..d723f88af 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -152,7 +152,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // above can't be statically verified to have been set in all cases because // the catch clause doesn't return. try { - userId = secretRing.getPrimaryUserId(); + userId = secretRing.getPrimaryUserIdWithFallback(); } catch (PgpGeneralException e) { userId = null; } @@ -232,7 +232,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor try { PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, - secretRing.getPrimaryUserId()); + secretRing.getPrimaryUserIdWithFallback()); } catch(PgpGeneralException e) { Log.e(Constants.TAG, "adding of a passhrase failed", e); } @@ -240,7 +240,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor if (unlockedSecretKey.getKeyId() != masterKeyId) { PassphraseCacheService.addCachedPassphrase( activity, unlockedSecretKey.getKeyId(), passphrase, - unlockedSecretKey.getPrimaryUserId()); + unlockedSecretKey.getPrimaryUserIdWithFallback()); } // also return passphrase back to activity -- cgit v1.2.3 From c1c831e52b11fc976b06b0d850f62e7934f581f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 16 Jul 2014 09:49:37 +0200 Subject: New first time screen --- .../org/sufficientlysecure/keychain/Constants.java | 1 + .../keychain/helper/Preferences.java | 10 + .../keychain/ui/CreateKeyActivity.java | 134 ++++++ .../keychain/ui/FirstTimeActivity.java | 74 ++++ .../keychain/ui/KeyListActivity.java | 30 +- .../keychain/ui/WizardActivity.java | 466 --------------------- 6 files changed, 241 insertions(+), 474 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java index e1bf1afa4..686a53f5b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/Constants.java @@ -68,6 +68,7 @@ public final class Constants { public static final String FORCE_V3_SIGNATURES = "forceV3Signatures"; public static final String KEY_SERVERS = "keyServers"; public static final String KEY_SERVERS_DEFAULT_VERSION = "keyServersDefaultVersion"; + public static final String FIRST_TIME = "firstTime"; } public static final class Defaults { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java index 6f3d38ccd..d870b89f5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java @@ -139,6 +139,16 @@ public class Preferences { editor.commit(); } + public boolean getFirstTime() { + return mSharedPreferences.getBoolean(Constants.Pref.FIRST_TIME, true); + } + + public void setFirstTime(boolean value) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putBoolean(Constants.Pref.FIRST_TIME, value); + editor.commit(); + } + public String[] getKeyServers() { String rawData = mSharedPreferences.getString(Constants.Pref.KEY_SERVERS, Constants.Defaults.KEY_SERVERS); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java new file mode 100644 index 000000000..3094c0e19 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.content.Context; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Patterns; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; +import android.widget.Button; +import android.widget.EditText; + +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.ContactHelper; + +import java.util.regex.Matcher; + +public class CreateKeyActivity extends ActionBarActivity { + + AutoCompleteTextView nameEdit; + AutoCompleteTextView emailEdit; + EditText passphraseEdit; + Button createButton; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.create_key_activity); + + nameEdit = (AutoCompleteTextView) findViewById(R.id.name); + emailEdit = (AutoCompleteTextView) findViewById(R.id.email); + passphraseEdit = (EditText) findViewById(R.id.passphrase); + createButton = (Button) findViewById(R.id.create_key_button); + + emailEdit.setThreshold(1); // Start working from first character + emailEdit.setAdapter( + new ArrayAdapter + (this, android.R.layout.simple_spinner_dropdown_item, + ContactHelper.getPossibleUserEmails(this) + ) + ); + emailEdit.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + } + + @Override + public void afterTextChanged(Editable editable) { + String email = editable.toString(); + if (email.length() > 0) { + Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); + if (emailMatcher.matches()) { + emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + R.drawable.uid_mail_ok, 0); + } else { + emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + R.drawable.uid_mail_bad, 0); + } + } else { + // remove drawable if email is empty + emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } + } + }); + nameEdit.setThreshold(1); // Start working from first character + nameEdit.setAdapter( + new ArrayAdapter + (this, android.R.layout.simple_spinner_dropdown_item, + ContactHelper.getPossibleUserNames(this) + ) + ); + + createButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createKey(); + } + }); + + } + + private void createKey() { + if (isEditTextNotEmpty(this, nameEdit) + && isEditTextNotEmpty(this, emailEdit) + && isEditTextNotEmpty(this, passphraseEdit)) { + + } + } + + /** + * Checks if text of given EditText is not empty. If it is empty an error is + * set and the EditText gets the focus. + * + * @param context + * @param editText + * @return true if EditText is not empty + */ + private static boolean isEditTextNotEmpty(Context context, EditText editText) { + boolean output = true; + if (editText.getText().toString().length() == 0) { + editText.setError("empty!"); + editText.requestFocus(); + output = false; + } else { + editText.setError(null); + } + + return output; + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java new file mode 100644 index 000000000..7de5e16b0 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.content.Intent; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.View; +import android.view.Window; +import android.widget.Button; + +import org.sufficientlysecure.keychain.R; + +public class FirstTimeActivity extends ActionBarActivity { + + Button mCreateKey; + Button mImportKey; + Button mSkipSetup; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + supportRequestWindowFeature(Window.FEATURE_NO_TITLE); + + setContentView(R.layout.first_time_activity); + + mCreateKey = (Button) findViewById(R.id.first_time_create_key); + mImportKey = (Button) findViewById(R.id.first_time_import_key); + mSkipSetup = (Button) findViewById(R.id.first_time_cancel); + + mSkipSetup.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FirstTimeActivity.this, KeyListActivity.class); + startActivity(intent); + } + }); + + mImportKey.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FirstTimeActivity.this, ImportKeysActivity.class); + intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); + startActivity(intent); + } + }); + + mCreateKey.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent(FirstTimeActivity.this, CreateKeyActivity.class); + startActivity(intent); + } + }); + + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index da9986890..b4daf1822 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -32,6 +32,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.Constants.choice.algorithm; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; +import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.service.KeychainIntentService; @@ -50,6 +51,15 @@ public class KeyListActivity extends DrawerActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Preferences prefs = Preferences.getPreferences(this); + if (prefs.getFirstTime()) { + prefs.setFirstTime(false); + Intent intent = new Intent(this, FirstTimeActivity.class); + startActivity(intent); + finish(); + return; + } + mExportHelper = new ExportHelper(this); setContentView(R.layout.key_list_activity); @@ -63,9 +73,10 @@ public class KeyListActivity extends DrawerActivity { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.key_list, menu); - if(Constants.DEBUG) { + if (Constants.DEBUG) { menu.findItem(R.id.menu_key_list_debug_read).setVisible(true); menu.findItem(R.id.menu_key_list_debug_write).setVisible(true); + menu.findItem(R.id.menu_key_list_debug_first_time).setVisible(true); } return true; @@ -95,7 +106,7 @@ public class KeyListActivity extends DrawerActivity { KeychainDatabase.debugRead(this); AppMsg.makeText(this, "Restored from backup", AppMsg.STYLE_CONFIRM).show(); getContentResolver().notifyChange(KeychainContract.KeyRings.CONTENT_URI, null); - } catch(IOException e) { + } catch (IOException e) { Log.e(Constants.TAG, "IO Error", e); AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); } @@ -105,12 +116,18 @@ public class KeyListActivity extends DrawerActivity { try { KeychainDatabase.debugWrite(this); AppMsg.makeText(this, "Backup successful", AppMsg.STYLE_CONFIRM).show(); - } catch(IOException e) { + } catch (IOException e) { Log.e(Constants.TAG, "IO Error", e); AppMsg.makeText(this, "IO Error: " + e.getMessage(), AppMsg.STYLE_ALERT).show(); } return true; + case R.id.menu_key_list_debug_first_time: + Intent intent = new Intent(this, FirstTimeActivity.class); + startActivity(intent); + finish(); + return true; + default: return super.onOptionsItemSelected(item); } @@ -122,11 +139,8 @@ public class KeyListActivity extends DrawerActivity { } private void createKey() { - Intent intent = new Intent(this, WizardActivity.class); -// intent.setAction(EditKeyActivity.ACTION_CREATE_KEY); -// intent.putExtra(EditKeyActivity.EXTRA_GENERATE_DEFAULT_KEYS, true); -// intent.putExtra(EditKeyActivity.EXTRA_USER_IDS, ""); // show user id view - startActivityForResult(intent, 0); + Intent intent = new Intent(this, CreateKeyActivity.class); + startActivity(intent); } private void createKeyExpert() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java deleted file mode 100644 index 7a2233524..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/WizardActivity.java +++ /dev/null @@ -1,466 +0,0 @@ -/* - * Copyright (C) 2014 Dominik Schürmann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui; - -import android.app.Activity; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.Bundle; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentTransaction; -import android.support.v7.app.ActionBarActivity; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.Patterns; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; -import android.widget.ArrayAdapter; -import android.widget.AutoCompleteTextView; -import android.widget.Button; -import android.widget.EditText; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.ProgressBar; -import android.widget.RadioGroup; -import android.widget.TextView; - -import org.sufficientlysecure.htmltextview.HtmlTextView; -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.helper.ContactHelper; -import org.sufficientlysecure.keychain.util.Log; - -import java.util.regex.Matcher; - - -public class WizardActivity extends ActionBarActivity { - - private State mCurrentState; - - // values for mCurrentScreen - private enum State { - START, CREATE_KEY, IMPORT_KEY, K9 - } - - public static final int REQUEST_CODE_IMPORT = 0x00007703; - - Button mBackButton; - Button mNextButton; - StartFragment mStartFragment; - CreateKeyFragment mCreateKeyFragment; - K9Fragment mK9Fragment; - - private static final String K9_PACKAGE = "com.fsck.k9"; - // private static final String K9_MARKET_INTENT_URI_BASE = "market://details?id=%s"; -// private static final Intent K9_MARKET_INTENT = new Intent(Intent.ACTION_VIEW, Uri.parse( -// String.format(K9_MARKET_INTENT_URI_BASE, K9_PACKAGE))); - private static final Intent K9_MARKET_INTENT = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/k9mail/k-9/releases/tag/4.904")); - - LinearLayout mProgressLayout; - View mProgressLine; - ProgressBar mProgressBar; - ImageView mProgressImage; - TextView mProgressText; - - /** - * Checks if text of given EditText is not empty. If it is empty an error is - * set and the EditText gets the focus. - * - * @param context - * @param editText - * @return true if EditText is not empty - */ - private static boolean isEditTextNotEmpty(Context context, EditText editText) { - boolean output = true; - if (editText.getText().toString().length() == 0) { - editText.setError("empty!"); - editText.requestFocus(); - output = false; - } else { - editText.setError(null); - } - - return output; - } - - public static class StartFragment extends Fragment { - public static StartFragment newInstance() { - StartFragment myFragment = new StartFragment(); - - Bundle args = new Bundle(); - myFragment.setArguments(args); - - return myFragment; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - return inflater.inflate(R.layout.wizard_start_fragment, - container, false); - } - } - - public static class CreateKeyFragment extends Fragment { - public static CreateKeyFragment newInstance() { - CreateKeyFragment myFragment = new CreateKeyFragment(); - - Bundle args = new Bundle(); - myFragment.setArguments(args); - - return myFragment; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.wizard_create_key_fragment, - container, false); - - final AutoCompleteTextView emailView = (AutoCompleteTextView) view.findViewById(R.id.email); - emailView.setThreshold(1); // Start working from first character - emailView.setAdapter( - new ArrayAdapter - (getActivity(), android.R.layout.simple_spinner_dropdown_item, - ContactHelper.getPossibleUserEmails(getActivity()) - ) - ); - emailView.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { - } - - @Override - public void afterTextChanged(Editable editable) { - String email = editable.toString(); - if (email.length() > 0) { - Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); - if (emailMatcher.matches()) { - emailView.setCompoundDrawablesWithIntrinsicBounds(0, 0, - R.drawable.uid_mail_ok, 0); - } else { - emailView.setCompoundDrawablesWithIntrinsicBounds(0, 0, - R.drawable.uid_mail_bad, 0); - } - } else { - // remove drawable if email is empty - emailView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); - } - } - }); - final AutoCompleteTextView nameView = (AutoCompleteTextView) view.findViewById(R.id.name); - nameView.setThreshold(1); // Start working from first character - nameView.setAdapter( - new ArrayAdapter - (getActivity(), android.R.layout.simple_spinner_dropdown_item, - ContactHelper.getPossibleUserNames(getActivity()) - ) - ); - return view; - } - } - - public static class K9Fragment extends Fragment { - public static K9Fragment newInstance() { - K9Fragment myFragment = new K9Fragment(); - - Bundle args = new Bundle(); - myFragment.setArguments(args); - - return myFragment; - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.wizard_k9_fragment, - container, false); - - HtmlTextView text = (HtmlTextView) v - .findViewById(R.id.wizard_k9_text); - text.setHtmlFromString("Install K9. It's good for you! Here is a screenhot how to enable OK in K9: (TODO)", true); - - return v; - } - - } - - /** - * Loads new fragment - * - * @param fragment - */ - private void loadFragment(Fragment fragment) { - FragmentManager fragmentManager = getSupportFragmentManager(); - FragmentTransaction fragmentTransaction = fragmentManager - .beginTransaction(); - fragmentTransaction.replace(R.id.wizard_container, - fragment); - fragmentTransaction.commit(); - } - - /** - * Instantiate View and initialize fragments for this Activity - */ - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.wizard_activity); - mBackButton = (Button) findViewById(R.id.wizard_back); - mNextButton = (Button) findViewById(R.id.wizard_next); - - // progress layout - mProgressLayout = (LinearLayout) findViewById(R.id.wizard_progress); - mProgressLine = findViewById(R.id.wizard_progress_line); - mProgressBar = (ProgressBar) findViewById(R.id.wizard_progress_progressbar); - mProgressImage = (ImageView) findViewById(R.id.wizard_progress_image); - mProgressText = (TextView) findViewById(R.id.wizard_progress_text); - - changeToState(State.START); - } - - private enum ProgressState { - WORKING, ENABLED, DISABLED, ERROR - } - - private void showProgress(ProgressState state, String text) { - switch (state) { - case WORKING: - mProgressBar.setVisibility(View.VISIBLE); - mProgressImage.setVisibility(View.GONE); - break; - case ENABLED: - mProgressBar.setVisibility(View.GONE); - mProgressImage.setVisibility(View.VISIBLE); -// mProgressImage.setImageDrawable(getResources().getDrawable( -// R.drawable.status_enabled)); - break; - case DISABLED: - mProgressBar.setVisibility(View.GONE); - mProgressImage.setVisibility(View.VISIBLE); -// mProgressImage.setImageDrawable(getResources().getDrawable( -// R.drawable.status_disabled)); - break; - case ERROR: - mProgressBar.setVisibility(View.GONE); - mProgressImage.setVisibility(View.VISIBLE); -// mProgressImage.setImageDrawable(getResources().getDrawable( -// R.drawable.status_fail)); - break; - - default: - break; - } - mProgressText.setText(text); - - mProgressLine.setVisibility(View.VISIBLE); - mProgressLayout.setVisibility(View.VISIBLE); - } - - private void hideProgress() { - mProgressLine.setVisibility(View.GONE); - mProgressLayout.setVisibility(View.GONE); - } - - public void nextOnClick(View view) { - // close keyboard - if (getCurrentFocus() != null) { - InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - inputManager.hideSoftInputFromWindow(getCurrentFocus() - .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); - } - - switch (mCurrentState) { - case START: { - RadioGroup radioGroup = (RadioGroup) findViewById(R.id.wizard_start_radio_group); - int selectedId = radioGroup.getCheckedRadioButtonId(); - switch (selectedId) { - case R.id.wizard_start_new_key: { - changeToState(State.CREATE_KEY); - break; - } - case R.id.wizard_start_import: { - changeToState(State.IMPORT_KEY); - break; - } - case R.id.wizard_start_skip: { - finish(); - break; - } - } - - mBackButton.setText(R.string.btn_back); - break; - } - case CREATE_KEY: - EditText nameEdit = (EditText) findViewById(R.id.name); - EditText emailEdit = (EditText) findViewById(R.id.email); - EditText passphraseEdit = (EditText) findViewById(R.id.passphrase); - - if (isEditTextNotEmpty(this, nameEdit) - && isEditTextNotEmpty(this, emailEdit) - && isEditTextNotEmpty(this, passphraseEdit)) { - -// SaveKeyringParcel newKey = new SaveKeyringParcel(); -// newKey.mAddUserIds.add(nameEdit.getText().toString() + " <" -// + emailEdit.getText().toString() + ">"); - - - AsyncTask generateTask = new AsyncTask() { - - @Override - protected void onPreExecute() { - super.onPreExecute(); - - showProgress(ProgressState.WORKING, "generating key..."); - } - - @Override - protected Boolean doInBackground(String... params) { - return true; - } - - @Override - protected void onPostExecute(Boolean result) { - super.onPostExecute(result); - - if (result) { - showProgress(ProgressState.ENABLED, "key generated successfully!"); - - changeToState(State.K9); - } else { - showProgress(ProgressState.ERROR, "error in key gen"); - } - } - - }; - - generateTask.execute(""); - } - break; - case K9: { - RadioGroup radioGroup = (RadioGroup) findViewById(R.id.wizard_k9_radio_group); - int selectedId = radioGroup.getCheckedRadioButtonId(); - switch (selectedId) { - case R.id.wizard_k9_install: { - try { - startActivity(K9_MARKET_INTENT); - } catch (ActivityNotFoundException e) { - Log.e(Constants.TAG, "Activity not found for: " + K9_MARKET_INTENT); - } - break; - } - case R.id.wizard_k9_skip: { - finish(); - break; - } - } - - finish(); - break; - } - default: - break; - } - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - super.onActivityResult(requestCode, resultCode, data); - switch (requestCode) { - case REQUEST_CODE_IMPORT: { - if (resultCode == Activity.RESULT_OK) { - // imported now... - changeToState(State.K9); - } else { - // back to start - changeToState(State.START); - } - break; - } - - default: { - super.onActivityResult(requestCode, resultCode, data); - - break; - } - } - } - - public void backOnClick(View view) { - switch (mCurrentState) { - case START: - finish(); - break; - case CREATE_KEY: - changeToState(State.START); - break; - case IMPORT_KEY: - changeToState(State.START); - break; - default: - changeToState(State.START); - break; - } - } - - private void changeToState(State state) { - switch (state) { - case START: { - mCurrentState = State.START; - mStartFragment = StartFragment.newInstance(); - loadFragment(mStartFragment); - mBackButton.setText(android.R.string.cancel); - mNextButton.setText(R.string.btn_next); - break; - } - case CREATE_KEY: { - mCurrentState = State.CREATE_KEY; - mCreateKeyFragment = CreateKeyFragment.newInstance(); - loadFragment(mCreateKeyFragment); - break; - } - case IMPORT_KEY: { - mCurrentState = State.IMPORT_KEY; - Intent intent = new Intent(this, ImportKeysActivity.class); - intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); - startActivityForResult(intent, REQUEST_CODE_IMPORT); - break; - } - case K9: { - mCurrentState = State.K9; - mBackButton.setEnabled(false); // don't go back to import/create key - mK9Fragment = K9Fragment.newInstance(); - loadFragment(mK9Fragment); - break; - } - } - } - -} -- cgit v1.2.3 From 57f5a788fd1b80828ada00b25b26400c85758d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 16 Jul 2014 10:04:48 +0200 Subject: Simple create key --- .../keychain/ui/CreateKeyActivity.java | 60 ++++++++++++++++++++-- .../keychain/ui/KeyListActivity.java | 43 ---------------- 2 files changed, 57 insertions(+), 46 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index 3094c0e19..14b1f3064 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -17,8 +17,12 @@ package org.sufficientlysecure.keychain.ui; +import android.app.ProgressDialog; import android.content.Context; +import android.content.Intent; import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; import android.support.v7.app.ActionBarActivity; import android.text.Editable; import android.text.TextWatcher; @@ -29,8 +33,13 @@ import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ContactHelper; +import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import java.util.regex.Matcher; @@ -97,20 +106,65 @@ public class CreateKeyActivity extends ActionBarActivity { createButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - createKey(); + createKeyCheck(); } }); } - private void createKey() { + private void createKeyCheck() { if (isEditTextNotEmpty(this, nameEdit) && isEditTextNotEmpty(this, emailEdit) && isEditTextNotEmpty(this, passphraseEdit)) { - + createKey(); } } + private void createKey() { + Intent intent = new Intent(this, KeychainIntentService.class); + intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); + + // Message is received after importing is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( + this, + getString(R.string.progress_importing), + ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + CreateKeyActivity.this.finish(); + } + } + }; + + // fill values for this action + Bundle data = new Bundle(); + + SaveKeyringParcel parcel = new SaveKeyringParcel(); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.CERTIFY_OTHER, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); + String userId = nameEdit.getText().toString() + " <" + emailEdit.getText().toString() + ">"; + parcel.mAddUserIds.add(userId); + parcel. + parcel.mNewPassphrase = passphraseEdit.getText().toString(); + + // get selected key entries + data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + saveHandler.showProgressDialog(this); + + startService(intent); + } + /** * Checks if text of given EditText is not empty. If it is empty an error is * set and the EditText gets the focus. diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index b4daf1822..9cc623c83 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -93,10 +93,6 @@ public class KeyListActivity extends DrawerActivity { createKey(); return true; - case R.id.menu_key_list_create_expert: - createKeyExpert(); - return true; - case R.id.menu_key_list_export: mExportHelper.showExportKeysDialog(null, Constants.Path.APP_DIR_FILE, true); return true; @@ -143,43 +139,4 @@ public class KeyListActivity extends DrawerActivity { startActivity(intent); } - private void createKeyExpert() { - Intent intent = new Intent(this, KeychainIntentService.class); - intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); - - // Message is received after importing is done in KeychainIntentService - KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL) { - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - Bundle data = message.getData(); - // OtherHelper.logDebugBundle(data, "message reply"); - } - }; - - // fill values for this action - Bundle data = new Bundle(); - - SaveKeyringParcel parcel = new SaveKeyringParcel(); - parcel.mAddSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.CERTIFY_OTHER, null)); - parcel.mAddSubKeys.add(new SubkeyAdd(algorithm.rsa, 1024, KeyFlags.SIGN_DATA, null)); - parcel.mAddUserIds.add("swagerinho"); - parcel.mNewPassphrase = "swag"; - - // get selected key entries - data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); - - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - - saveHandler.showProgressDialog(this); - - startService(intent); - } } \ No newline at end of file -- cgit v1.2.3 From 77d04a915b7875a97070ce2087eb80cac073ef0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 16 Jul 2014 10:05:00 +0200 Subject: Simple create key --- .../main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java | 1 - 1 file changed, 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index 14b1f3064..d2073d9a7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -148,7 +148,6 @@ public class CreateKeyActivity extends ActionBarActivity { parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); String userId = nameEdit.getText().toString() + " <" + emailEdit.getText().toString() + ">"; parcel.mAddUserIds.add(userId); - parcel. parcel.mNewPassphrase = passphraseEdit.getText().toString(); // get selected key entries -- cgit v1.2.3 From 82af9672fdc7f548eb801c29d123c824ea286cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 16 Jul 2014 10:07:50 +0200 Subject: Temporary program flow fixes --- .../java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java | 3 +++ 1 file changed, 3 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java index 7de5e16b0..aa4120d2c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java @@ -49,6 +49,7 @@ public class FirstTimeActivity extends ActionBarActivity { public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, KeyListActivity.class); startActivity(intent); + finish(); } }); @@ -58,6 +59,7 @@ public class FirstTimeActivity extends ActionBarActivity { Intent intent = new Intent(FirstTimeActivity.this, ImportKeysActivity.class); intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); startActivity(intent); + finish(); } }); @@ -66,6 +68,7 @@ public class FirstTimeActivity extends ActionBarActivity { public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, CreateKeyActivity.class); startActivity(intent); + finish(); } }); -- cgit v1.2.3 From e375cde7e1a09df33c65a44ba57f3f75f2f98b15 Mon Sep 17 00:00:00 2001 From: Daniel Albert Date: Wed, 16 Jul 2014 18:17:46 +0200 Subject: Final Commit for #662 --- .../main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java index 969ff1de8..0ceefc4d9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpHelper.java @@ -24,6 +24,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.util.Log; import java.io.File; @@ -60,7 +61,11 @@ public class PgpHelper { } public static String getFullVersion(Context context) { - return "OpenKeychain v" + getVersion(context); + if(Preferences.getPreferences(context).getConcealPgpApplication()){ + return ""; + } else { + return "OpenKeychain v" + getVersion(context); + } } // public static final class content { -- cgit v1.2.3 From cd1511a4e6d346d177be7d828faa82f99e46685c Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 19 Jul 2014 02:19:15 +0200 Subject: canonicalize: fix for tests --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 1 + .../java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java | 8 ++++++-- .../org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java | 3 --- .../keychain/service/OperationResultParcel.java | 2 ++ 4 files changed, 9 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index bd8a9201e..00f73a5b0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -599,6 +599,7 @@ public class PgpKeyOperation { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_ENCODE, indent+1); return null; } catch (PGPException e) { + Log.e(Constants.TAG, "encountered pgp error while modifying key", e); log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_PGP, indent+1); return null; } catch (SignatureException e) { 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 9ddfd3405..6020fc084 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -229,7 +229,7 @@ public class UncachedKeyRing { PGPPublicKey modified = masterKey; PGPSignature revocation = null; - for (PGPSignature zert : new IterableIterator(masterKey.getSignatures())) { + for (PGPSignature zert : new IterableIterator(masterKey.getKeySignatures())) { int type = zert.getSignatureType(); // Disregard certifications on user ids, we will deal with those later @@ -238,6 +238,10 @@ public class UncachedKeyRing { || type == PGPSignature.CASUAL_CERTIFICATION || type == PGPSignature.POSITIVE_CERTIFICATION || type == PGPSignature.CERTIFICATION_REVOCATION) { + // These should not be here... + log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TYPE_UID, indent); + modified = PGPPublicKey.removeCertification(modified, zert); + badCerts += 1; continue; } WrappedSignature cert = new WrappedSignature(zert); @@ -429,7 +433,7 @@ public class UncachedKeyRing { // If no valid certificate (if only a revocation) remains, drop it if (selfCert == null && revocation == null) { modified = PGPPublicKey.removeCertification(modified, userId); - log.add(LogLevel.ERROR, LogType.MSG_KC_UID_REVOKE_DUP, + log.add(LogLevel.ERROR, LogType.MSG_KC_UID_REMOVE, indent, userId); } } 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 ef5aa8e86..358b1c552 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedPublicKey.java @@ -1,8 +1,6 @@ package org.sufficientlysecure.keychain.pgp; -import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.sig.KeyFlags; -import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureSubpacketVector; @@ -11,7 +9,6 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; -import java.security.SignatureException; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 705d6afaf..89d534df6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -202,6 +202,7 @@ public class OperationResultParcel implements Parcelable { MSG_KC_REVOKE_BAD_LOCAL (R.string.msg_kc_revoke_bad_local), MSG_KC_REVOKE_BAD_TIME (R.string.msg_kc_revoke_bad_time), MSG_KC_REVOKE_BAD_TYPE (R.string.msg_kc_revoke_bad_type), + MSG_KC_REVOKE_BAD_TYPE_UID (R.string.msg_kc_revoke_bad_type_uid), MSG_KC_REVOKE_BAD (R.string.msg_kc_revoke_bad), MSG_KC_REVOKE_DUP (R.string.msg_kc_revoke_dup), MSG_KC_SUB (R.string.msg_kc_sub), @@ -233,6 +234,7 @@ public class OperationResultParcel implements Parcelable { MSG_KC_UID_NO_CERT (R.string.msg_kc_uid_no_cert), MSG_KC_UID_REVOKE_DUP (R.string.msg_kc_uid_revoke_dup), MSG_KC_UID_REVOKE_OLD (R.string.msg_kc_uid_revoke_old), + MSG_KC_UID_REMOVE (R.string.msg_kc_uid_remove), // keyring consolidation -- cgit v1.2.3 From 5eb414a22b28c93e47fe7f62ee786b898d9a21d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Fri, 25 Jul 2014 01:34:29 +0200 Subject: Program flow fixes --- .../keychain/helper/Preferences.java | 2 +- .../keychain/ui/CreateKeyActivity.java | 46 +++++++++++----------- .../keychain/ui/FirstTimeActivity.java | 27 +++++++++---- .../keychain/ui/KeyListActivity.java | 16 ++------ 4 files changed, 48 insertions(+), 43 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java index e55c14a2a..5d765b663 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/helper/Preferences.java @@ -139,7 +139,7 @@ public class Preferences { editor.commit(); } - public boolean getFirstTime() { + public boolean isFirstTime() { return mSharedPreferences.getBoolean(Constants.Pref.FIRST_TIME, true); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index d2073d9a7..fe1b7e688 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -45,10 +45,10 @@ import java.util.regex.Matcher; public class CreateKeyActivity extends ActionBarActivity { - AutoCompleteTextView nameEdit; - AutoCompleteTextView emailEdit; - EditText passphraseEdit; - Button createButton; + AutoCompleteTextView mNameEdit; + AutoCompleteTextView mEmailEdit; + EditText mPassphraseEdit; + Button mCreateButton; @Override protected void onCreate(Bundle savedInstanceState) { @@ -56,19 +56,19 @@ public class CreateKeyActivity extends ActionBarActivity { setContentView(R.layout.create_key_activity); - nameEdit = (AutoCompleteTextView) findViewById(R.id.name); - emailEdit = (AutoCompleteTextView) findViewById(R.id.email); - passphraseEdit = (EditText) findViewById(R.id.passphrase); - createButton = (Button) findViewById(R.id.create_key_button); + mNameEdit = (AutoCompleteTextView) findViewById(R.id.name); + mEmailEdit = (AutoCompleteTextView) findViewById(R.id.email); + mPassphraseEdit = (EditText) findViewById(R.id.passphrase); + mCreateButton = (Button) findViewById(R.id.create_key_button); - emailEdit.setThreshold(1); // Start working from first character - emailEdit.setAdapter( + mEmailEdit.setThreshold(1); // Start working from first character + mEmailEdit.setAdapter( new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item, ContactHelper.getPossibleUserEmails(this) ) ); - emailEdit.addTextChangedListener(new TextWatcher() { + mEmailEdit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { } @@ -83,27 +83,28 @@ public class CreateKeyActivity extends ActionBarActivity { if (email.length() > 0) { Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); if (emailMatcher.matches()) { - emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.uid_mail_ok, 0); } else { - emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.uid_mail_bad, 0); } } else { // remove drawable if email is empty - emailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); } } }); - nameEdit.setThreshold(1); // Start working from first character - nameEdit.setAdapter( + + mNameEdit.setThreshold(1); // Start working from first character + mNameEdit.setAdapter( new ArrayAdapter (this, android.R.layout.simple_spinner_dropdown_item, ContactHelper.getPossibleUserNames(this) ) ); - createButton.setOnClickListener(new View.OnClickListener() { + mCreateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { createKeyCheck(); @@ -113,9 +114,9 @@ public class CreateKeyActivity extends ActionBarActivity { } private void createKeyCheck() { - if (isEditTextNotEmpty(this, nameEdit) - && isEditTextNotEmpty(this, emailEdit) - && isEditTextNotEmpty(this, passphraseEdit)) { + if (isEditTextNotEmpty(this, mNameEdit) + && isEditTextNotEmpty(this, mEmailEdit) + && isEditTextNotEmpty(this, mPassphraseEdit)) { createKey(); } } @@ -134,6 +135,7 @@ public class CreateKeyActivity extends ActionBarActivity { super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + CreateKeyActivity.this.setResult(RESULT_OK); CreateKeyActivity.this.finish(); } } @@ -146,9 +148,9 @@ public class CreateKeyActivity extends ActionBarActivity { parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.CERTIFY_OTHER, null)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); - String userId = nameEdit.getText().toString() + " <" + emailEdit.getText().toString() + ">"; + String userId = mNameEdit.getText().toString() + " <" + mEmailEdit.getText().toString() + ">"; parcel.mAddUserIds.add(userId); - parcel.mNewPassphrase = passphraseEdit.getText().toString(); + parcel.mNewPassphrase = mPassphraseEdit.getText().toString(); // get selected key entries data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java index aa4120d2c..4271b2d5a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java @@ -25,6 +25,7 @@ import android.view.Window; import android.widget.Button; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.Preferences; public class FirstTimeActivity extends ActionBarActivity { @@ -47,9 +48,7 @@ public class FirstTimeActivity extends ActionBarActivity { mSkipSetup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Intent intent = new Intent(FirstTimeActivity.this, KeyListActivity.class); - startActivity(intent); - finish(); + finishSetup(); } }); @@ -58,8 +57,7 @@ public class FirstTimeActivity extends ActionBarActivity { public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, ImportKeysActivity.class); intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); - startActivity(intent); - finish(); + startActivityForResult(intent, 1); } }); @@ -67,11 +65,26 @@ public class FirstTimeActivity extends ActionBarActivity { @Override public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, CreateKeyActivity.class); - startActivity(intent); - finish(); + startActivityForResult(intent, 1); } }); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (resultCode == RESULT_OK) { + finishSetup(); + } + } + + private void finishSetup() { + Preferences prefs = Preferences.getPreferences(this); + prefs.setFirstTime(false); + Intent intent = new Intent(FirstTimeActivity.this, KeyListActivity.class); + startActivity(intent); + finish(); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 9cc623c83..095762283 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -17,28 +17,19 @@ package org.sufficientlysecure.keychain.ui; -import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; -import android.os.Message; -import android.os.Messenger; import android.view.Menu; import android.view.MenuItem; import com.devspark.appmsg.AppMsg; -import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.Constants.choice.algorithm; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ExportHelper; import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainDatabase; -import org.sufficientlysecure.keychain.service.KeychainIntentService; -import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; -import org.sufficientlysecure.keychain.service.SaveKeyringParcel; -import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.util.Log; import java.io.IOException; @@ -51,11 +42,10 @@ public class KeyListActivity extends DrawerActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // if this is the first time show first time activity Preferences prefs = Preferences.getPreferences(this); - if (prefs.getFirstTime()) { - prefs.setFirstTime(false); - Intent intent = new Intent(this, FirstTimeActivity.class); - startActivity(intent); + if (prefs.isFirstTime()) { + startActivity(new Intent(this, FirstTimeActivity.class)); finish(); return; } -- cgit v1.2.3 From ab2b90342e805a0a625e059dcfe2db11157a5680 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Jul 2014 03:47:29 +0200 Subject: test and fix: adding an empty user id should fail --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 5 +++++ .../sufficientlysecure/keychain/service/OperationResultParcel.java | 1 + 2 files changed, 6 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 00f73a5b0..69244ca14 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -313,6 +313,11 @@ public class PgpKeyOperation { for (String userId : saveParcel.mAddUserIds) { log.add(LogLevel.INFO, LogType.MSG_MF_UID_ADD, indent); + if (userId.equals("")) { + log.add(LogLevel.ERROR, LogType.MSG_MF_UID_ERROR_EMPTY, indent+1); + return null; + } + // this operation supersedes all previous binding and revocation certificates, // so remove those to retain assertions from canonicalization for later operations @SuppressWarnings("unchecked") diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 89d534df6..e55ec5568 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -279,6 +279,7 @@ public class OperationResultParcel implements Parcelable { MSG_MF_UID_ADD (R.string.msg_mf_uid_add), MSG_MF_UID_PRIMARY (R.string.msg_mf_uid_primary), MSG_MF_UID_REVOKE (R.string.msg_mf_uid_revoke), + MSG_MF_UID_ERROR_EMPTY (R.string.msg_mf_uid_error_empty), MSG_MF_UNLOCK_ERROR (R.string.msg_mf_unlock_error), MSG_MF_UNLOCK (R.string.msg_mf_unlock), ; -- cgit v1.2.3 From 7fe1b00080ad7bcdbda890e9c2c60ad4a1225a9f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Jul 2014 03:56:28 +0200 Subject: fixes for extractPublicKeyRing, update SpongyCastle --- .../keychain/pgp/UncachedKeyRing.java | 15 +++++++++------ .../keychain/pgp/WrappedPublicKeyRing.java | 18 +++++++++--------- 2 files changed, 18 insertions(+), 15 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 6020fc084..3b39eb201 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -14,6 +14,7 @@ import org.spongycastle.openpgp.PGPSecretKeyRing; import org.spongycastle.openpgp.PGPSignature; import org.spongycastle.openpgp.PGPSignatureList; import org.spongycastle.openpgp.PGPUtil; +import org.spongycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; @@ -24,6 +25,7 @@ import org.sufficientlysecure.keychain.util.Log; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -193,7 +195,7 @@ public class UncachedKeyRing { * - Remove all certificates flagged as "local" * - Remove all certificates which are superseded by a newer one on the same target, * including revocations with later re-certifications. - * - Remove all certificates of unknown type: + * - Remove all certificates in other positions if not of known type: * - key revocation signatures on the master key * - subkey binding signatures for subkeys * - certifications and certification revocations for user ids @@ -658,7 +660,7 @@ public class UncachedKeyRing { return left.length - right.length; } // compare byte-by-byte - for (int i = 0; i < left.length && i < right.length; i++) { + for (int i = 0; i < left.length; i++) { if (left[i] != right[i]) { return (left[i] & 0xff) - (right[i] & 0xff); } @@ -768,19 +770,20 @@ public class UncachedKeyRing { } - public UncachedKeyRing extractPublicKeyRing() { + public UncachedKeyRing extractPublicKeyRing() throws IOException { if(!isSecret()) { throw new RuntimeException("Tried to extract public keyring from non-secret keyring. " + "This is a programming error and should never happen!"); } - ArrayList keys = new ArrayList(); Iterator it = mRing.getPublicKeys(); + ByteArrayOutputStream stream = new ByteArrayOutputStream(2048); while (it.hasNext()) { - keys.add(it.next()); + stream.write(it.next().getEncoded()); } - return new UncachedKeyRing(new PGPPublicKeyRing(keys)); + return new UncachedKeyRing( + new PGPPublicKeyRing(stream.toByteArray(), new JcaKeyFingerprintCalculator())); } /** This method replaces a public key in a keyring. diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java index 57d84072a..bb1f7d778 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java @@ -1,14 +1,11 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.bcpg.ArmoredOutputStream; -import org.spongycastle.openpgp.PGPKeyRing; import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPPublicKey; import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.util.IterableIterator; -import org.sufficientlysecure.keychain.util.Log; import java.io.IOException; import java.util.Iterator; @@ -25,17 +22,20 @@ public class WrappedPublicKeyRing extends WrappedKeyRing { PGPPublicKeyRing getRing() { if(mRing == null) { + // get first object in block PGPObjectFactory factory = new PGPObjectFactory(mPubKey); - PGPKeyRing keyRing = null; try { - if ((keyRing = (PGPKeyRing) factory.nextObject()) == null) { - Log.e(Constants.TAG, "No keys given!"); + Object obj = factory.nextObject(); + if (! (obj instanceof PGPPublicKeyRing)) { + throw new RuntimeException("Error constructing WrappedPublicKeyRing, should never happen!"); + } + mRing = (PGPPublicKeyRing) obj; + if (factory.nextObject() != null) { + throw new RuntimeException("Encountered trailing data after keyring, should never happen!"); } } catch (IOException e) { - Log.e(Constants.TAG, "Error while converting to PGPKeyRing!", e); + throw new RuntimeException("IO Error constructing WrappedPublicKeyRing, should never happen!"); } - - mRing = (PGPPublicKeyRing) keyRing; } return mRing; } -- cgit v1.2.3 From 7296ac484914bbe5cec90b37cc1319db49f7d9b8 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Jul 2014 03:57:54 +0200 Subject: UncachedKeyRing.merge: copy over new secret subkeys into secret keyrings --- .../keychain/pgp/UncachedKeyRing.java | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 3b39eb201..91b06324a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -688,7 +688,14 @@ public class UncachedKeyRing { final PGPPublicKey resultKey = result.getPublicKey(key.getKeyID()); if (resultKey == null) { log.add(LogLevel.DEBUG, LogType.MSG_MG_NEW_SUBKEY, indent); - result = replacePublicKey(result, key); + // special case: if both rings are secret, copy over the secret key + if (isSecret() && other.isSecret()) { + PGPSecretKey sKey = ((PGPSecretKeyRing) candidate).getSecretKey(key.getKeyID()); + result = PGPSecretKeyRing.insertSecretKey((PGPSecretKeyRing) result, sKey); + } else { + // otherwise, just insert the public key + result = replacePublicKey(result, key); + } continue; } @@ -696,17 +703,7 @@ public class UncachedKeyRing { PGPPublicKey modified = resultKey; // Iterate certifications - for (PGPSignature cert : new IterableIterator(key.getSignatures())) { - int type = cert.getSignatureType(); - // Disregard certifications on user ids, we will deal with those later - if (type == PGPSignature.NO_CERTIFICATION - || type == PGPSignature.DEFAULT_CERTIFICATION - || type == PGPSignature.CASUAL_CERTIFICATION - || type == PGPSignature.POSITIVE_CERTIFICATION - || type == PGPSignature.CERTIFICATION_REVOCATION) { - continue; - } - + for (PGPSignature cert : new IterableIterator(key.getKeySignatures())) { // Don't merge foreign stuff into secret keys if (cert.getKeyID() != masterKeyId && isSecret()) { continue; -- cgit v1.2.3 From 236a502ea75d0cd6f999d0059db7d15f685c2ff2 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Jul 2014 15:51:35 +0200 Subject: generic UncachedKeyRing fixes --- .../keychain/pgp/UncachedKeyRing.java | 49 +++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 91b06324a..b8f582d6c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -91,7 +91,7 @@ public class UncachedKeyRing { final Iterator it = mRing.getPublicKeys(); return new Iterator() { public void remove() { - it.remove(); + throw new UnsupportedOperationException(); } public UncachedPublicKey next() { return new UncachedPublicKey(it.next()); @@ -121,42 +121,41 @@ public class UncachedKeyRing { public static UncachedKeyRing decodeFromData(byte[] data) throws PgpGeneralException, IOException { - BufferedInputStream bufferedInput = - new BufferedInputStream(new ByteArrayInputStream(data)); - if (bufferedInput.available() > 0) { - InputStream in = PGPUtil.getDecoderStream(bufferedInput); - PGPObjectFactory objectFactory = new PGPObjectFactory(in); - // get first object in block - Object obj; - if ((obj = objectFactory.nextObject()) != null && obj instanceof PGPKeyRing) { - return new UncachedKeyRing((PGPKeyRing) obj); - } else { - throw new PgpGeneralException("Object not recognized as PGPKeyRing!"); - } - } else { + List parsed = fromStream(new ByteArrayInputStream(data)); + + if (parsed.isEmpty()) { throw new PgpGeneralException("Object not recognized as PGPKeyRing!"); } + if (parsed.size() > 1) { + throw new PgpGeneralException( + "Expected single keyring in stream, found " + parsed.size()); + } + + return parsed.get(0); + } public static List fromStream(InputStream stream) throws PgpGeneralException, IOException { - PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); - List result = new Vector(); - // go through all objects in this block - Object obj; - while ((obj = objectFactory.nextObject()) != null) { - Log.d(Constants.TAG, "Found class: " + obj.getClass()); + while(stream.available() > 0) { + PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); - if (obj instanceof PGPKeyRing) { + // go through all objects in this block + Object obj; + while ((obj = objectFactory.nextObject()) != null) { + Log.d(Constants.TAG, "Found class: " + obj.getClass()); + if (!(obj instanceof PGPKeyRing)) { + throw new PgpGeneralException( + "Bad object of type " + obj.getClass().getName() + " in stream!"); + } result.add(new UncachedKeyRing((PGPKeyRing) obj)); - } else { - Log.e(Constants.TAG, "Object not recognized as PGPKeyRing!"); } } + return result; } @@ -327,7 +326,7 @@ public class UncachedKeyRing { if (cert.getCreationTime().after(now)) { // Creation date in the future? No way! - log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_TIME, indent); + log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_TIME, indent); modified = PGPPublicKey.removeCertification(modified, zert); badCerts += 1; continue; @@ -335,7 +334,7 @@ public class UncachedKeyRing { if (cert.isLocal()) { // Creation date in the future? No way! - log.add(LogLevel.WARN, LogType.MSG_KC_REVOKE_BAD_LOCAL, indent); + log.add(LogLevel.WARN, LogType.MSG_KC_UID_BAD_LOCAL, indent); modified = PGPPublicKey.removeCertification(modified, zert); badCerts += 1; continue; -- cgit v1.2.3 From 45722d7cfbb2996ff994b3f2143e5871c2e564ba Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sat, 26 Jul 2014 22:16:33 +0200 Subject: canonicalize: couple of fixes --- .../keychain/pgp/UncachedKeyRing.java | 40 ++++++++++++++++------ .../keychain/pgp/WrappedSecretKey.java | 8 ----- 2 files changed, 30 insertions(+), 18 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 b8f582d6c..8838075cd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -23,13 +23,11 @@ import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; -import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.HashSet; @@ -514,14 +512,16 @@ public class UncachedKeyRing { continue; } + // if this certificate says it allows signing for the key if (zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { + int flags = ((KeyFlags) zert.getHashedSubPackets() .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); - // If this subkey is allowed to sign data, 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(); - boolean ok = false; for (int i = 0; i < list.size(); i++) { WrappedSignature subsig = new WrappedSignature(list.get(i)); if (subsig.getSignatureType() == PGPSignature.PRIMARYKEY_BINDING) { @@ -535,17 +535,19 @@ public class UncachedKeyRing { } } } - if (!ok) { - log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_NONE, indent); - badCerts += 1; - continue; - } } catch (Exception e) { log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_BAD_ERR, indent); badCerts += 1; continue; } + // if it doesn't, get rid of this! + if (!ok) { + log.add(LogLevel.WARN, LogType.MSG_KC_SUB_PRIMARY_NONE, indent); + badCerts += 1; + continue; + } } + } // if we already have a cert, and this one is not newer: skip it @@ -558,6 +560,8 @@ public class UncachedKeyRing { selfCert = zert; // if this is newer than a possibly existing revocation, drop that one if (revocation != null && selfCert.getCreationTime().after(revocation.getCreationTime())) { + log.add(LogLevel.DEBUG, LogType.MSG_KC_SUB_REVOKE_DUP, indent); + redundantCerts += 1; revocation = null; } @@ -591,7 +595,7 @@ public class UncachedKeyRing { // it is not properly bound? error! if (selfCert == null) { - ring = replacePublicKey(ring, modified); + ring = removeSubKey(ring, key); log.add(LogLevel.ERROR, LogType.MSG_KC_SUB_NO_CERT, indent, PgpKeyHelper.convertKeyIdToHex(key.getKeyID())); @@ -803,4 +807,20 @@ public class UncachedKeyRing { return PGPSecretKeyRing.insertSecretKey(secRing, sKey); } + /** This method removes a subkey in a keyring. + * + * This method essentially wraps PGP*KeyRing.remove*Key, where the keyring may be of either + * the secret or public subclass. + * + * @return the resulting PGPKeyRing of the same type as the input + */ + private static PGPKeyRing removeSubKey(PGPKeyRing ring, PGPPublicKey key) { + if (ring instanceof PGPPublicKeyRing) { + return PGPPublicKeyRing.removePublicKey((PGPPublicKeyRing) ring, key); + } else { + PGPSecretKey sKey = ((PGPSecretKeyRing) ring).getSecretKey(key.getKeyID()); + return PGPSecretKeyRing.removeSecretKey((PGPSecretKeyRing) ring, sKey); + } + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java index 98ad2b706..f0485d801 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java @@ -51,14 +51,6 @@ public class WrappedSecretKey extends WrappedPublicKey { return (WrappedSecretKeyRing) mRing; } - /** Returns the wrapped PGPSecretKeyRing. - * This function is for compatibility only, should not be used anymore and will be removed - */ - @Deprecated - public PGPSecretKey getKeyExternal() { - return mSecretKey; - } - public boolean unlock(String passphrase) throws PgpGeneralException { try { PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( -- cgit v1.2.3 From a8782272b3db20ba6e88acab1d035d4699aa7166 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 27 Jul 2014 00:46:38 +0200 Subject: some more work on supertoast and result parcel stuff --- .../keychain/pgp/PgpImportExport.java | 22 ++++++------ .../remote/ui/AccountSettingsFragment.java | 11 +++--- .../keychain/service/KeychainIntentService.java | 3 +- .../keychain/service/OperationResultParcel.java | 8 ----- .../keychain/service/OperationResults.java | 42 ++++++++++++++++------ .../keychain/ui/EditKeyActivity.java | 2 +- .../keychain/ui/EditKeyFragment.java | 14 ++++++-- .../keychain/ui/ImportKeysActivity.java | 8 ++--- .../keychain/ui/KeyListFragment.java | 7 ++-- .../keychain/ui/ViewKeyActivity.java | 2 -- 10 files changed, 66 insertions(+), 53 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java index 6fc55cfb8..9d334d9bd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java @@ -34,7 +34,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; -import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; +import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.service.OperationResults.SaveKeyringResult; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ProgressScaler; @@ -123,14 +123,14 @@ public class PgpImportExport { } /** Imports keys from given data. If keyIds is given only those are imported */ - public ImportResult importKeyRings(List entries) { + public ImportKeyResult importKeyRings(List entries) { updateProgress(R.string.progress_importing, 0, 100); // If there aren't even any keys, do nothing here. if (entries == null || entries.size() == 0) { - return new ImportResult( - ImportResult.RESULT_FAIL_NOTHING, mProviderHelper.getLog(), 0, 0, 0); + return new ImportKeyResult( + ImportKeyResult.RESULT_FAIL_NOTHING, mProviderHelper.getLog(), 0, 0, 0); } int newKeys = 0, oldKeys = 0, badKeys = 0; @@ -185,26 +185,26 @@ public class PgpImportExport { int resultType = 0; // special return case: no new keys at all if (badKeys == 0 && newKeys == 0 && oldKeys == 0) { - resultType = ImportResult.RESULT_FAIL_NOTHING; + resultType = ImportKeyResult.RESULT_FAIL_NOTHING; } else { if (newKeys > 0) { - resultType |= ImportResult.RESULT_OK_NEWKEYS; + resultType |= ImportKeyResult.RESULT_OK_NEWKEYS; } if (oldKeys > 0) { - resultType |= ImportResult.RESULT_OK_UPDATED; + resultType |= ImportKeyResult.RESULT_OK_UPDATED; } if (badKeys > 0) { - resultType |= ImportResult.RESULT_WITH_ERRORS; + resultType |= ImportKeyResult.RESULT_WITH_ERRORS; if (newKeys == 0 && oldKeys == 0) { - resultType |= ImportResult.RESULT_ERROR; + resultType |= ImportKeyResult.RESULT_ERROR; } } if (log.containsWarnings()) { - resultType |= ImportResult.RESULT_WITH_WARNINGS; + resultType |= ImportKeyResult.RESULT_WITH_WARNINGS; } } - return new ImportResult(resultType, log, newKeys, oldKeys, badKeys); + return new ImportKeyResult(resultType, log, newKeys, oldKeys, badKeys); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index 0b1d521ad..86460c45d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -35,7 +35,7 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.remote.AccountSettings; -import org.sufficientlysecure.keychain.ui.EditKeyActivityOld; +import org.sufficientlysecure.keychain.ui.CreateKeyActivity; import org.sufficientlysecure.keychain.ui.SelectSecretKeyLayoutFragment; import org.sufficientlysecure.keychain.ui.adapter.KeyValueSpinnerAdapter; import org.sufficientlysecure.keychain.util.AlgorithmNames; @@ -163,12 +163,9 @@ public class AccountSettingsFragment extends Fragment implements } private void createKey() { - Intent intent = new Intent(getActivity(), EditKeyActivityOld.class); - intent.setAction(EditKeyActivityOld.ACTION_CREATE_KEY); - intent.putExtra(EditKeyActivityOld.EXTRA_GENERATE_DEFAULT_KEYS, true); - // set default user id to account name - intent.putExtra(EditKeyActivityOld.EXTRA_USER_IDS, mAccSettings.getAccountName()); - startActivityForResult(intent, REQUEST_CODE_CREATE_KEY); + Intent intent = new Intent(getActivity(), CreateKeyActivity.class); + // startActivityForResult(intent, REQUEST_CODE_CREATE_KEY); + startActivity(intent); } @Override diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 9a4cef2f1..f6a269e24 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -53,6 +53,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; +import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ProgressScaler; @@ -390,7 +391,7 @@ public class KeychainIntentService extends IntentService List entries = data.getParcelableArrayList(IMPORT_KEY_LIST); PgpImportExport pgpImportExport = new PgpImportExport(this, this); - OperationResults.ImportResult result = pgpImportExport.importKeyRings(entries); + ImportKeyResult result = pgpImportExport.importKeyRings(entries); Bundle resultData = new Bundle(); resultData.putParcelable(RESULT, result); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index d80c508c3..2c6c29f8d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -391,14 +391,6 @@ public class OperationResultParcel implements Parcelable { mParcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } - public LogEntryParcel getResultId() { - LogEntryParcel entry = get(size()-1); - if (entry.mLevel != LogLevel.OK && entry.mLevel != LogLevel.ERROR) { - return new LogEntryParcel(LogLevel.ERROR, LogType.INTERNAL_ERROR, 0); - } - return entry; - } - public boolean containsWarnings() { for(LogEntryParcel entry : new IterableIterator(mParcels.iterator())) { if (entry.mLevel == LogLevel.WARN || entry.mLevel == LogLevel.ERROR) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index 39c11a855..fcb6c027d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -12,12 +12,13 @@ import com.github.johnpersano.supertoasts.util.OnClickWrapper; import com.github.johnpersano.supertoasts.util.Style; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.ui.LogDisplayActivity; import org.sufficientlysecure.keychain.ui.LogDisplayFragment; public abstract class OperationResults { - public static class ImportResult extends OperationResultParcel { + public static class ImportKeyResult extends OperationResultParcel { public final int mNewKeys, mUpdatedKeys, mBadKeys; @@ -47,15 +48,15 @@ public abstract class OperationResults { return (mResult & RESULT_FAIL_NOTHING) == RESULT_FAIL_NOTHING; } - public ImportResult(Parcel source) { + public ImportKeyResult(Parcel source) { super(source); mNewKeys = source.readInt(); mUpdatedKeys = source.readInt(); mBadKeys = source.readInt(); } - public ImportResult(int result, OperationLog log, - int newKeys, int updatedKeys, int badKeys) { + public ImportKeyResult(int result, OperationLog log, + int newKeys, int updatedKeys, int badKeys) { super(result, log); mNewKeys = newKeys; mUpdatedKeys = updatedKeys; @@ -70,13 +71,13 @@ public abstract class OperationResults { dest.writeInt(mBadKeys); } - public static Creator CREATOR = new Creator() { - public ImportResult createFromParcel(final Parcel source) { - return new ImportResult(source); + public static Creator CREATOR = new Creator() { + public ImportKeyResult createFromParcel(final Parcel source) { + return new ImportKeyResult(source); } - public ImportResult[] newArray(final int size) { - return new ImportResult[size]; + public ImportKeyResult[] newArray(final int size) { + return new ImportKeyResult[size]; } }; @@ -92,7 +93,7 @@ public abstract class OperationResults { String withWarnings; // Any warnings? - if ((resultType & ImportResult.RESULT_WITH_WARNINGS) > 0) { + if ((resultType & ImportKeyResult.RESULT_WITH_WARNINGS) > 0) { duration = 0; color = Style.ORANGE; withWarnings = activity.getResources().getString(R.string.import_with_warnings); @@ -151,7 +152,7 @@ public abstract class OperationResults { public void onClick(View view, Parcelable token) { Intent intent = new Intent( activity, LogDisplayActivity.class); - intent.putExtra(LogDisplayFragment.EXTRA_RESULT, ImportResult.this); + intent.putExtra(LogDisplayFragment.EXTRA_RESULT, ImportKeyResult.this); activity.startActivity(intent); } } @@ -164,6 +165,25 @@ public abstract class OperationResults { } + public static class EditKeyResult extends OperationResultParcel { + + public EditKeyResult(Parcel source) { + super(source); + } + + public static Creator CREATOR = new Creator() { + public EditKeyResult createFromParcel(final Parcel source) { + return new EditKeyResult(source); + } + + public EditKeyResult[] newArray(final int size) { + return new EditKeyResult[size]; + } + }; + + } + + public static class SaveKeyringResult extends OperationResultParcel { public SaveKeyringResult(int result, OperationLog log) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java index d80425c3c..6ddaec17f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyActivity.java @@ -33,7 +33,7 @@ public class EditKeyActivity extends ActionBarActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.edit_key_activity_new); + setContentView(R.layout.edit_key_activity); Uri dataUri = getIntent().getData(); if (dataUri == null) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index b41871a39..8fc4cffb8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -48,6 +48,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.OperationResults; +import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; @@ -480,12 +481,19 @@ public class EditKeyFragment extends LoaderFragment implements return; } - // if good -> finish, return result to showkey and display there! + // if bad -> display here! + if (!result.success()) { + result.createNotify(getActivity()).show(); + return; + } -// result.displayNotify(ImportKeysActivity.this); + // if good -> finish, return result to showkey and display there! + Intent intent = new Intent(); + intent.putExtra(ImportKeyResult.EXTRA_RESULT, result); + getActivity().setResult(EditKeyActivity.RESULT_OK, intent); + getActivity().finish(); -// getActivity().finish(); } } }; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 3ff3b56bf..5ab4d69f1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -45,7 +45,7 @@ import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; -import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; +import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter; import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout; import org.sufficientlysecure.keychain.util.Log; @@ -427,7 +427,7 @@ public class ImportKeysActivity extends ActionBarActivity { if (returnData == null) { return; } - final ImportResult result = + final ImportKeyResult result = returnData.getParcelable(KeychainIntentService.RESULT); if (result == null) { return; @@ -435,7 +435,7 @@ public class ImportKeysActivity extends ActionBarActivity { if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction())) { Intent intent = new Intent(); - intent.putExtra(EXTRA_RESULT, result); + intent.putExtra(ImportKeyResult.EXTRA_RESULT, result); ImportKeysActivity.this.setResult(RESULT_OK, intent); ImportKeysActivity.this.finish(); return; @@ -451,7 +451,7 @@ public class ImportKeysActivity extends ActionBarActivity { return; } - result.displayNotify(ImportKeysActivity.this); + result.createNotify(ImportKeysActivity.this).show(); } } }; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index c8f6b7b1e..5e2e4cb43 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -103,11 +103,8 @@ public class KeyListFragment extends LoaderFragment @Override public void onClick(View v) { - Intent intent = new Intent(getActivity(), EditKeyActivity.class); - intent.setAction(EditKeyActivity.ACTION_CREATE_KEY); - intent.putExtra(EditKeyActivity.EXTRA_GENERATE_DEFAULT_KEYS, true); - intent.putExtra(EditKeyActivity.EXTRA_USER_IDS, ""); // show user id view - startActivityForResult(intent, 0); + Intent intent = new Intent(getActivity(), CreateKeyActivity.class); + startActivity(intent); } }); mButtonEmptyImport = (Button) view.findViewById(R.id.key_list_empty_button_import); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index b8b2542ce..256526dd9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain.ui; import android.annotation.TargetApi; -import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; @@ -53,7 +52,6 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.OperationResultParcel; -import org.sufficientlysecure.keychain.service.OperationResults.ImportResult; import org.sufficientlysecure.keychain.ui.adapter.PagerTabStripAdapter; import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout.TabColorizer; import org.sufficientlysecure.keychain.util.Log; -- cgit v1.2.3 From f4ee71e3ef0dcacf691def97a94880b277a99496 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Sun, 27 Jul 2014 01:22:10 +0200 Subject: introduce EditKeyResult with transient UncachedKeyRing (half-baked!) --- .../keychain/pgp/PgpKeyOperation.java | 27 ++++++++---- .../keychain/service/KeychainIntentService.java | 50 ++++++++++++---------- .../keychain/service/OperationResultParcel.java | 31 ++++++++++++-- .../keychain/service/OperationResults.java | 14 +++++- .../keychain/ui/EditKeyFragment.java | 10 ++--- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/ViewKeyMainFragment.java | 4 +- 7 files changed, 93 insertions(+), 45 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 69244ca14..c498bad0b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -47,9 +47,11 @@ import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; +import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.util.IterableIterator; @@ -163,8 +165,10 @@ public class PgpKeyOperation { } } - public UncachedKeyRing createSecretKeyRing(SaveKeyringParcel saveParcel, OperationLog log, - int indent) { + public EditKeyResult createSecretKeyRing(SaveKeyringParcel saveParcel) { + + OperationLog log = new OperationLog(); + int indent = 0; try { @@ -213,7 +217,7 @@ public class PgpKeyOperation { PGPSecretKeyRing sKR = new PGPSecretKeyRing( masterSecretKey.getEncoded(), new JcaKeyFingerprintCalculator()); - return internal(sKR, masterSecretKey, add.mFlags, saveParcel, "", log, indent); + return internal(sKR, masterSecretKey, add.mFlags, saveParcel, "", log); } catch (PGPException e) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_INTERNAL_PGP, indent); @@ -237,8 +241,11 @@ public class PgpKeyOperation { * are changed by adding new certificates, which implicitly override older certificates. * */ - public UncachedKeyRing modifySecretKeyRing(WrappedSecretKeyRing wsKR, SaveKeyringParcel saveParcel, - String passphrase, OperationLog log, int indent) { + public EditKeyResult modifySecretKeyRing(WrappedSecretKeyRing wsKR, SaveKeyringParcel saveParcel, + String passphrase) { + + OperationLog log = new OperationLog(); + int indent = 0; /* * 1. Unlock private key @@ -277,14 +284,16 @@ public class PgpKeyOperation { // since this is the master key, this contains at least CERTIFY_OTHER int masterKeyFlags = readKeyFlags(masterSecretKey.getPublicKey()) | KeyFlags.CERTIFY_OTHER; - return internal(sKR, masterSecretKey, masterKeyFlags, saveParcel, passphrase, log, indent); + return internal(sKR, masterSecretKey, masterKeyFlags, saveParcel, passphrase, log); } - private UncachedKeyRing internal(PGPSecretKeyRing sKR, PGPSecretKey masterSecretKey, + private EditKeyResult internal(PGPSecretKeyRing sKR, PGPSecretKey masterSecretKey, int masterKeyFlags, SaveKeyringParcel saveParcel, String passphrase, - OperationLog log, int indent) { + OperationLog log) { + + int indent = 1; updateProgress(R.string.progress_certifying_master_key, 20, 100); @@ -613,7 +622,7 @@ public class PgpKeyOperation { } log.add(LogLevel.OK, LogType.MSG_MF_SUCCESS, indent); - return new UncachedKeyRing(sKR); + return new EditKeyResult(OperationResultParcel.RESULT_OK, log, new UncachedKeyRing(sKR)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index f6a269e24..0e38a1c47 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -24,6 +24,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; +import android.os.Parcelable; import android.os.RemoteException; import org.sufficientlysecure.keychain.Constants; @@ -53,6 +54,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; +import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult; import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.util.InputData; import org.sufficientlysecure.keychain.util.Log; @@ -333,38 +335,36 @@ public class KeychainIntentService extends IntentService /* Operation */ ProviderHelper providerHelper = new ProviderHelper(this); PgpKeyOperation keyOperations = new PgpKeyOperation(new ProgressScaler(this, 10, 50, 100)); - try { - OperationLog log = new OperationLog(); - UncachedKeyRing ring; - if (saveParcel.mMasterKeyId != null) { - String passphrase = data.getString(SAVE_KEYRING_PASSPHRASE); - WrappedSecretKeyRing secRing = - providerHelper.getWrappedSecretKeyRing(saveParcel.mMasterKeyId); - - ring = keyOperations.modifySecretKeyRing(secRing, saveParcel, - passphrase, log, 0); - } else { - ring = keyOperations.createSecretKeyRing(saveParcel, log, 0); - } + EditKeyResult result; - providerHelper.saveSecretKeyRing(ring, new ProgressScaler(this, 10, 95, 100)); + if (saveParcel.mMasterKeyId != null) { + String passphrase = data.getString(SAVE_KEYRING_PASSPHRASE); + WrappedSecretKeyRing secRing = + providerHelper.getWrappedSecretKeyRing(saveParcel.mMasterKeyId); - // cache new passphrase - if (saveParcel.mNewPassphrase != null) { - PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), - saveParcel.mNewPassphrase, ring.getPublicKey().getPrimaryUserIdWithFallback()); - } - } catch (ProviderHelper.NotFoundException e) { - sendErrorToHandler(e); + result = keyOperations.modifySecretKeyRing(secRing, saveParcel, passphrase); + } else { + result = keyOperations.createSecretKeyRing(saveParcel); + } + + UncachedKeyRing ring = result.getRing(); + + providerHelper.saveSecretKeyRing(ring, new ProgressScaler(this, 10, 95, 100)); + + // cache new passphrase + if (saveParcel.mNewPassphrase != null) { + PassphraseCacheService.addCachedPassphrase(this, ring.getMasterKeyId(), + saveParcel.mNewPassphrase, ring.getPublicKey().getPrimaryUserIdWithFallback()); } setProgress(R.string.progress_done, 100, 100); /* Output */ - sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY); + sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, result); } catch (Exception e) { sendErrorToHandler(e); } + } else if (ACTION_DELETE_FILE_SECURELY.equals(action)) { try { /* Input */ @@ -624,6 +624,12 @@ public class KeychainIntentService extends IntentService } } + private void sendMessageToHandler(Integer arg1, OperationResultParcel data) { + Bundle bundle = new Bundle(); + bundle.putParcelable(OperationResultParcel.EXTRA_RESULT, data); + sendMessageToHandler(arg1, null, bundle); + } + private void sendMessageToHandler(Integer arg1, Bundle data) { sendMessageToHandler(arg1, null, data); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 2c6c29f8d..99cafd3f9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -144,20 +144,45 @@ public class OperationResultParcel implements Parcelable { color = Style.GREEN; } - str = activity.getString(R.string.import_error); + str = "operation succeeded!"; + // str = activity.getString(R.string.import_error); } else { + duration = 0; color = Style.RED; - str = activity.getString(R.string.import_error); + + str = "operation failed"; + // str = activity.getString(R.string.import_error); + } + boolean button = getLog() != null && !getLog().isEmpty(); SuperCardToast toast = new SuperCardToast(activity, - SuperToast.Type.STANDARD, Style.getStyle(color, SuperToast.Animations.POPUP)); + button ? SuperToast.Type.BUTTON : SuperToast.Type.STANDARD, + Style.getStyle(color, SuperToast.Animations.POPUP)); toast.setText(str); toast.setDuration(duration); toast.setIndeterminate(duration == 0); toast.setSwipeToDismiss(true); + // If we have a log and it's non-empty, show a View Log button + if (button) { + toast.setButtonIcon(R.drawable.ic_action_view_as_list, + activity.getResources().getString(R.string.view_log)); + toast.setButtonTextColor(activity.getResources().getColor(R.color.black)); + toast.setTextColor(activity.getResources().getColor(R.color.black)); + toast.setOnClickWrapper(new OnClickWrapper("supercardtoast", + new SuperToast.OnClickListener() { + @Override + public void onClick(View view, Parcelable token) { + Intent intent = new Intent( + activity, LogDisplayActivity.class); + intent.putExtra(LogDisplayFragment.EXTRA_RESULT, OperationResultParcel.this); + activity.startActivity(intent); + } + } + )); + } return toast; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index fcb6c027d..6fb0db0e5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -143,7 +143,7 @@ public abstract class OperationResults { // If we have a log and it's non-empty, show a View Log button if (button) { toast.setButtonIcon(R.drawable.ic_action_view_as_list, - activity.getResources().getString(R.string.import_view_log)); + activity.getResources().getString(R.string.view_log)); toast.setButtonTextColor(activity.getResources().getColor(R.color.black)); toast.setTextColor(activity.getResources().getColor(R.color.black)); toast.setOnClickWrapper(new OnClickWrapper("supercardtoast", @@ -167,6 +167,18 @@ public abstract class OperationResults { public static class EditKeyResult extends OperationResultParcel { + private transient UncachedKeyRing mRing; + + public EditKeyResult(int result, OperationLog log, + UncachedKeyRing ring) { + super(result, log); + mRing = ring; + } + + public UncachedKeyRing getRing() { + return mRing; + } + public EditKeyResult(Parcel source) { super(source); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 8fc4cffb8..5ff864547 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -48,6 +48,7 @@ import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.OperationResults; +import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult; import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; @@ -467,21 +468,18 @@ public class EditKeyFragment extends LoaderFragment implements super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - getActivity().finish(); - // TODO below // get returned data bundle Bundle returnData = message.getData(); if (returnData == null) { return; } - final OperationResults.SaveKeyringResult result = - returnData.getParcelable(KeychainIntentService.RESULT); + final OperationResults.EditKeyResult result = + returnData.getParcelable(EditKeyResult.EXTRA_RESULT); if (result == null) { return; } - // if bad -> display here! if (!result.success()) { result.createNotify(getActivity()).show(); @@ -490,7 +488,7 @@ public class EditKeyFragment extends LoaderFragment implements // if good -> finish, return result to showkey and display there! Intent intent = new Intent(); - intent.putExtra(ImportKeyResult.EXTRA_RESULT, result); + intent.putExtra(EditKeyResult.EXTRA_RESULT, result); getActivity().setResult(EditKeyActivity.RESULT_OK, intent); getActivity().finish(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 256526dd9..f92977dbe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -350,7 +350,7 @@ public class ViewKeyActivity extends ActionBarActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (data.hasExtra(OperationResultParcel.EXTRA_RESULT)) { + if (data != null && data.hasExtra(OperationResultParcel.EXTRA_RESULT)) { OperationResultParcel result = data.getParcelableExtra(OperationResultParcel.EXTRA_RESULT); result.createNotify(this).show(); } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index 2a0f518d8..ade618840 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -251,9 +251,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements private void editKey(Uri dataUri) { Intent editIntent = new Intent(getActivity(), EditKeyActivity.class); editIntent.setData(KeychainContract.KeyRingData.buildSecretKeyRingUri(dataUri)); -// editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY); -// startActivityForResult(editIntent, 0); - startActivity(editIntent); + startActivityForResult(editIntent, 0); } } -- cgit v1.2.3 From dab540e121d622236dd723e31b611624ffb976ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 17:56:52 +0200 Subject: Work on first time wizard design --- .../keychain/ui/CreateKeyActivity.java | 74 ++++++++++++++++++++-- .../keychain/ui/EncryptFileFragment.java | 8 +-- .../keychain/ui/FirstTimeActivity.java | 16 +++-- .../keychain/ui/KeyListActivity.java | 2 + .../keychain/ui/KeyListFragment.java | 12 ++-- 5 files changed, 90 insertions(+), 22 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index fe1b7e688..3cf89f6a4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; @@ -31,7 +32,11 @@ import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; +import android.widget.CheckBox; import android.widget.EditText; +import android.widget.Spinner; + +import com.devspark.appmsg.AppMsg; import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; @@ -48,7 +53,8 @@ public class CreateKeyActivity extends ActionBarActivity { AutoCompleteTextView mNameEdit; AutoCompleteTextView mEmailEdit; EditText mPassphraseEdit; - Button mCreateButton; + View mCreateButton; + CheckBox mUploadCheckbox; @Override protected void onCreate(Bundle savedInstanceState) { @@ -59,7 +65,8 @@ public class CreateKeyActivity extends ActionBarActivity { mNameEdit = (AutoCompleteTextView) findViewById(R.id.name); mEmailEdit = (AutoCompleteTextView) findViewById(R.id.email); mPassphraseEdit = (EditText) findViewById(R.id.passphrase); - mCreateButton = (Button) findViewById(R.id.create_key_button); + mCreateButton = findViewById(R.id.create_key_button); + mUploadCheckbox = (CheckBox) findViewById(R.id.create_key_upload); mEmailEdit.setThreshold(1); // Start working from first character mEmailEdit.setAdapter( @@ -134,10 +141,15 @@ public class CreateKeyActivity extends ActionBarActivity { // handle messages by standard KeychainIntentServiceHandler first super.handleMessage(message); - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - CreateKeyActivity.this.setResult(RESULT_OK); - CreateKeyActivity.this.finish(); - } + // TODO +// if (mUploadCheckbox.isChecked()) { +// uploadKey(); +// } else { + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + CreateKeyActivity.this.setResult(RESULT_OK); + CreateKeyActivity.this.finish(); + } +// } } }; @@ -166,6 +178,54 @@ public class CreateKeyActivity extends ActionBarActivity { startService(intent); } + private void uploadKey() { + // Send all information needed to service to upload key in other thread + Intent intent = new Intent(this, KeychainIntentService.class); + + intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); + + // set data uri as path to keyring + // TODO +// Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); +// intent.setData(blobUri); + + // fill values for this action + Bundle data = new Bundle(); + + Spinner keyServer = (Spinner) findViewById(R.id.upload_key_keyserver); + String server = (String) keyServer.getSelectedItem(); + data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Message is received after uploading is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this, + getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + AppMsg.makeText(CreateKeyActivity.this, R.string.key_send_success, + AppMsg.STYLE_INFO).show(); + + CreateKeyActivity.this.setResult(RESULT_OK); + CreateKeyActivity.this.finish(); + } + } + }; + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + // show progress dialog + saveHandler.showProgressDialog(this); + + // start service with intent + startService(intent); + } + /** * Checks if text of given EditText is not empty. If it is empty an error is * set and the EditText gets the focus. @@ -177,7 +237,7 @@ public class CreateKeyActivity extends ActionBarActivity { private static boolean isEditTextNotEmpty(Context context, EditText editText) { boolean output = true; if (editText.getText().toString().length() == 0) { - editText.setError("empty!"); + editText.setError(context.getString(R.string.create_key_empty)); editText.requestFocus(); output = false; } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java index f5d89d186..fa6d6fab5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EncryptFileFragment.java @@ -58,7 +58,7 @@ public class EncryptFileFragment extends Fragment { public static final String ARG_FILENAME = "filename"; public static final String ARG_ASCII_ARMOR = "ascii_armor"; - private static final int RESULT_CODE_FILE = 0x00007003; + private static final int REQUEST_CODE_FILE = 0x00007003; private EncryptActivityInterface mEncryptInterface; @@ -109,10 +109,10 @@ public class EncryptFileFragment extends Fragment { mBrowse.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (Constants.KITKAT) { - FileHelper.openDocument(EncryptFileFragment.this, mInputUri, "*/*", RESULT_CODE_FILE); + FileHelper.openDocument(EncryptFileFragment.this, mInputUri, "*/*", REQUEST_CODE_FILE); } else { FileHelper.openFile(EncryptFileFragment.this, mFilename.getText().toString(), "*/*", - RESULT_CODE_FILE); + REQUEST_CODE_FILE); } } }); @@ -390,7 +390,7 @@ public class EncryptFileFragment extends Fragment { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { - case RESULT_CODE_FILE: { + case REQUEST_CODE_FILE: { if (resultCode == Activity.RESULT_OK && data != null) { if (Constants.KITKAT) { mInputUri = data.getData(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java index 4271b2d5a..a764d78c2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java @@ -24,8 +24,10 @@ import android.view.View; import android.view.Window; import android.widget.Button; +import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.util.Log; public class FirstTimeActivity extends ActionBarActivity { @@ -33,6 +35,8 @@ public class FirstTimeActivity extends ActionBarActivity { Button mImportKey; Button mSkipSetup; + public static final int REQUEST_CODE_CREATE_OR_IMPORT_KEY = 0x00007012; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -57,7 +61,7 @@ public class FirstTimeActivity extends ActionBarActivity { public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, ImportKeysActivity.class); intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); - startActivityForResult(intent, 1); + startActivityForResult(intent, REQUEST_CODE_CREATE_OR_IMPORT_KEY); } }); @@ -65,7 +69,7 @@ public class FirstTimeActivity extends ActionBarActivity { @Override public void onClick(View v) { Intent intent = new Intent(FirstTimeActivity.this, CreateKeyActivity.class); - startActivityForResult(intent, 1); + startActivityForResult(intent, REQUEST_CODE_CREATE_OR_IMPORT_KEY); } }); @@ -75,8 +79,12 @@ public class FirstTimeActivity extends ActionBarActivity { protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - if (resultCode == RESULT_OK) { - finishSetup(); + if (requestCode == REQUEST_CODE_CREATE_OR_IMPORT_KEY) { + if (resultCode == RESULT_OK) { + finishSetup(); + } + } else { + Log.e(Constants.TAG, "No valid request code!"); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java index 095762283..588d9ead6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListActivity.java @@ -109,6 +109,8 @@ public class KeyListActivity extends DrawerActivity { return true; case R.id.menu_key_list_debug_first_time: + Preferences prefs = Preferences.getPreferences(this); + prefs.setFirstTime(true); Intent intent = new Intent(this, FirstTimeActivity.class); startActivity(intent); finish(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 9d0a80406..5ace66f79 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -86,6 +86,7 @@ public class KeyListFragment extends LoaderFragment private Button mButtonEmptyCreate; private Button mButtonEmptyImport; + public static final int REQUEST_CODE_CREATE_OR_IMPORT_KEY = 0x00007012; /** * Load custom layout with StickyListView from library @@ -104,11 +105,8 @@ public class KeyListFragment extends LoaderFragment @Override public void onClick(View v) { - Intent intent = new Intent(getActivity(), EditKeyActivityOld.class); - intent.setAction(EditKeyActivityOld.ACTION_CREATE_KEY); - intent.putExtra(EditKeyActivityOld.EXTRA_GENERATE_DEFAULT_KEYS, true); - intent.putExtra(EditKeyActivityOld.EXTRA_USER_IDS, ""); // show user id view - startActivityForResult(intent, 0); + Intent intent = new Intent(getActivity(), CreateKeyActivity.class); + startActivityForResult(intent, REQUEST_CODE_CREATE_OR_IMPORT_KEY); } }); mButtonEmptyImport = (Button) view.findViewById(R.id.key_list_empty_button_import); @@ -117,8 +115,8 @@ public class KeyListFragment extends LoaderFragment @Override public void onClick(View v) { Intent intent = new Intent(getActivity(), ImportKeysActivity.class); - intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE); - startActivityForResult(intent, 0); + intent.setAction(ImportKeysActivity.ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN); + startActivityForResult(intent, REQUEST_CODE_CREATE_OR_IMPORT_KEY); } }); -- cgit v1.2.3 From 0403cbf11ac0b5ada7624578b7a0c398c3fa6904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 18:12:14 +0200 Subject: Prettify buttons --- .../sufficientlysecure/keychain/ui/FirstTimeActivity.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java index a764d78c2..5f3f170a1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/FirstTimeActivity.java @@ -22,7 +22,6 @@ import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.Window; -import android.widget.Button; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -31,9 +30,9 @@ import org.sufficientlysecure.keychain.util.Log; public class FirstTimeActivity extends ActionBarActivity { - Button mCreateKey; - Button mImportKey; - Button mSkipSetup; + View mCreateKey; + View mImportKey; + View mSkipSetup; public static final int REQUEST_CODE_CREATE_OR_IMPORT_KEY = 0x00007012; @@ -45,9 +44,9 @@ public class FirstTimeActivity extends ActionBarActivity { setContentView(R.layout.first_time_activity); - mCreateKey = (Button) findViewById(R.id.first_time_create_key); - mImportKey = (Button) findViewById(R.id.first_time_import_key); - mSkipSetup = (Button) findViewById(R.id.first_time_cancel); + mCreateKey = findViewById(R.id.first_time_create_key); + mImportKey = findViewById(R.id.first_time_import_key); + mSkipSetup = findViewById(R.id.first_time_cancel); mSkipSetup.setOnClickListener(new View.OnClickListener() { @Override -- cgit v1.2.3 From 97e8faa1dd833169accba5aab65602a9c9fd310a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 18:56:23 +0200 Subject: Make PassphraseCacheService a foreground service --- .../keychain/service/PassphraseCacheService.java | 139 ++++++++++----------- 1 file changed, 67 insertions(+), 72 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index 13d9b497f..cfb3d50f4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -18,9 +18,9 @@ package org.sufficientlysecure.keychain.service; import android.app.AlarmManager; +import android.app.Notification; import android.app.PendingIntent; import android.app.Service; -import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -70,7 +70,7 @@ public class PassphraseCacheService extends Service { public static final String EXTRA_KEY_ID = "key_id"; public static final String EXTRA_PASSPHRASE = "passphrase"; public static final String EXTRA_MESSENGER = "messenger"; - public static final String EXTRA_USERID = "userid"; + public static final String EXTRA_USER_ID = "user_id"; private static final int REQUEST_ID = 0; private static final long DEFAULT_TTL = 15; @@ -102,7 +102,7 @@ public class PassphraseCacheService extends Service { intent.putExtra(EXTRA_TTL, Preferences.getPreferences(context).getPassphraseCacheTtl()); intent.putExtra(EXTRA_PASSPHRASE, passphrase); intent.putExtra(EXTRA_KEY_ID, keyId); - intent.putExtra(EXTRA_USERID, primaryUserId); + intent.putExtra(EXTRA_USER_ID, primaryUserId); context.startService(intent); } @@ -201,18 +201,18 @@ public class PassphraseCacheService extends Service { // get cached passphrase CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId); if (cachedPassphrase == null) { - Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null"); + Log.d(Constants.TAG, "PassphraseCacheService: Passphrase not (yet) cached, returning null"); // not really an error, just means the passphrase is not cached but not empty either return null; } // set it again to reset the cache life cycle - Log.d(Constants.TAG, "PassphraseCacheService Cache passphrase again when getting it!"); + Log.d(Constants.TAG, "PassphraseCacheService: Cache passphrase again when getting it!"); addCachedPassphrase(this, keyId, cachedPassphrase.getPassphrase(), cachedPassphrase.getPrimaryUserID()); return cachedPassphrase.getPassphrase(); } catch (ProviderHelper.NotFoundException e) { - Log.e(Constants.TAG, "PassphraseCacheService Passphrase for unknown key was requested!"); + Log.e(Constants.TAG, "PassphraseCacheService: Passphrase for unknown key was requested!"); return null; } } @@ -229,7 +229,7 @@ public class PassphraseCacheService extends Service { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); - Log.d(Constants.TAG, "PassphraseCacheService Received broadcast..."); + Log.d(Constants.TAG, "PassphraseCacheService: Received broadcast..."); if (action.equals(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE)) { long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); @@ -254,10 +254,8 @@ public class PassphraseCacheService extends Service { private static PendingIntent buildIntent(Context context, long keyId) { Intent intent = new Intent(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE); intent.putExtra(EXTRA_KEY_ID, keyId); - PendingIntent sender = PendingIntent.getBroadcast(context, REQUEST_ID, intent, + return PendingIntent.getBroadcast(context, REQUEST_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT); - - return sender; } /** @@ -276,11 +274,12 @@ public class PassphraseCacheService extends Service { long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE); - String primaryUserID = intent.getStringExtra(EXTRA_USERID); + String primaryUserID = intent.getStringExtra(EXTRA_USER_ID); Log.d(Constants.TAG, - "PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " - + keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID); + "PassphraseCacheService: Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " + + keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID + ); // add keyId, passphrase and primary user id to memory mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID)); @@ -292,8 +291,7 @@ public class PassphraseCacheService extends Service { am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId)); } - updateNotifications(); - + updateService(); } else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) { long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1); Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER); @@ -307,21 +305,21 @@ public class PassphraseCacheService extends Service { try { messenger.send(msg); } catch (RemoteException e) { - Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e); + Log.e(Constants.TAG, "PassphraseCacheService: Sending message failed", e); } } else if (ACTION_PASSPHRASE_CACHE_CLEAR.equals(intent.getAction())) { AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); // Stop all ttl alarms - for(int i = 0; i < mPassphraseCache.size(); i++) { + for (int i = 0; i < mPassphraseCache.size(); i++) { am.cancel(buildIntent(this, mPassphraseCache.keyAt(i))); } mPassphraseCache.clear(); - updateNotifications(); + updateService(); } else { - Log.e(Constants.TAG, "PassphraseCacheService Intent or Intent Action not supported!"); + Log.e(Constants.TAG, "PassphraseCacheService: Intent or Intent Action not supported!"); } } @@ -340,79 +338,74 @@ public class PassphraseCacheService extends Service { Log.d(Constants.TAG, "PassphraseCacheService Timeout of keyId " + keyId + ", removed from memory!"); - // stop whole service if no cached passphrases remaining - if (mPassphraseCache.size() == 0) { - Log.d(Constants.TAG, "PassphraseCacheServic No passphrases remaining in memory, stopping service!"); - stopSelf(); - } - - updateNotifications(); + updateService(); } - private void updateNotifications() { - NotificationManager notificationManager = - (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + private void updateService() { + if (mPassphraseCache.size() > 0) { + startForeground(NOTIFICATION_ID, getNotification()); + } else { + // stop whole service if no cached passphrases remaining + Log.d(Constants.TAG, "PassphraseCacheService: No passphrases remaining in memory, stopping service!"); + stopForeground(true); + } + } - if(mPassphraseCache.size() > 0) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); + private Notification getNotification() { + NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - builder.setSmallIcon(R.drawable.ic_launcher) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + builder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle(getString(R.string.app_name)) - .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys), mPassphraseCache.size())); + .setContentText(String.format(getString(R.string.passp_cache_notif_n_keys), + mPassphraseCache.size())); - NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); + NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); - inboxStyle.setBigContentTitle(getString(R.string.passp_cache_notif_keys)); + inboxStyle.setBigContentTitle(getString(R.string.passp_cache_notif_keys)); - // Moves events into the big view - for (int i = 0; i < mPassphraseCache.size(); i++) { - inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); - } + // Moves events into the big view + for (int i = 0; i < mPassphraseCache.size(); i++) { + inboxStyle.addLine(mPassphraseCache.valueAt(i).getPrimaryUserID()); + } - // Moves the big view style object into the notification object. - builder.setStyle(inboxStyle); + // Moves the big view style object into the notification object. + builder.setStyle(inboxStyle); - // Add purging action - Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); - builder.addAction( + // Add purging action + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); + builder.addAction( R.drawable.abc_ic_clear_normal, getString(R.string.passp_cache_notif_clear), PendingIntent.getService( - getApplicationContext(), - 0, - intent, - PendingIntent.FLAG_UPDATE_CURRENT + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT ) - ); - - notificationManager.notify(NOTIFICATION_ID, builder.build()); - } else { // Fallback, since expandable notifications weren't available back then - NotificationCompat.Builder builder = new NotificationCompat.Builder(this); - - builder.setSmallIcon(R.drawable.ic_launcher) - .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size()))) + ); + } else { + // Fallback, since expandable notifications weren't available back then + builder.setSmallIcon(R.drawable.ic_launcher) + .setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, + mPassphraseCache.size()))) .setContentText(getString(R.string.passp_cache_notif_click_to_clear)); - Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); - intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); + Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class); + intent.setAction(ACTION_PASSPHRASE_CACHE_CLEAR); - builder.setContentIntent( + builder.setContentIntent( PendingIntent.getService( - getApplicationContext(), - 0, - intent, - PendingIntent.FLAG_UPDATE_CURRENT + getApplicationContext(), + 0, + intent, + PendingIntent.FLAG_UPDATE_CURRENT ) - ); - - notificationManager.notify(NOTIFICATION_ID, builder.build()); - } - - } else { - notificationManager.cancel(NOTIFICATION_ID); + ); } + + return builder.build(); } @Override @@ -455,6 +448,7 @@ public class PassphraseCacheService extends Service { public String getPrimaryUserID() { return primaryUserID; } + public String getPassphrase() { return passphrase; } @@ -462,6 +456,7 @@ public class PassphraseCacheService extends Service { public void setPrimaryUserID(String primaryUserID) { this.primaryUserID = primaryUserID; } + public void setPassphrase(String passphrase) { this.passphrase = passphrase; } -- cgit v1.2.3 From 57d9c7a013cefa6641bff7167486f7fe12247265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 20:42:39 +0200 Subject: Open keyboard on passphrase dialog, this should now work on all Android versions... hopefully --- .../ui/dialog/PassphraseDialogFragment.java | 73 +++++++++++++++------- .../ui/dialog/SetPassphraseDialogFragment.java | 46 ++++++++++++-- 2 files changed, 92 insertions(+), 27 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index d723f88af..7981b717e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -33,8 +33,10 @@ import android.support.v4.app.FragmentActivity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.view.WindowManager.LayoutParams; import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; @@ -62,7 +64,6 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor private Messenger mMessenger; private EditText mPassphraseEditText; - private boolean mCanKB; /** * Shows passphrase dialog to cache a new passphrase the user enters for using it later for @@ -105,7 +106,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor if (!new ProviderHelper(context).getWrappedSecretKeyRing(secretKeyId).hasPassphrase()) { throw new PgpGeneralException("No passphrase! No passphrase dialog needed!"); } - } catch(ProviderHelper.NotFoundException e) { + } catch (ProviderHelper.NotFoundException e) { throw new PgpGeneralException("Error: Key not found!", e); } } @@ -165,7 +166,6 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor } }); alert.setCancelable(false); - mCanKB = false; return alert.create(); } @@ -190,7 +190,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // Early breakout if we are dealing with a symmetric key if (secretRing == null) { PassphraseCacheService.addCachedPassphrase(activity, Constants.key.symmetric, - passphrase, getString(R.string.passp_cache_notif_pwd)); + passphrase, getString(R.string.passp_cache_notif_pwd)); // also return passphrase back to activity Bundle data = new Bundle(); data.putString(MESSAGE_DATA_PASSPHRASE, passphrase); @@ -200,7 +200,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor WrappedSecretKey unlockedSecretKey = null; - for(WrappedSecretKey clickSecretKey : secretRing.secretKeyIterator()) { + for (WrappedSecretKey clickSecretKey : secretRing.secretKeyIterator()) { try { boolean unlocked = clickSecretKey.unlock(passphrase); if (unlocked) { @@ -232,9 +232,9 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor try { PassphraseCacheService.addCachedPassphrase(activity, masterKeyId, passphrase, - secretRing.getPrimaryUserIdWithFallback()); - } catch(PgpGeneralException e) { - Log.e(Constants.TAG, "adding of a passhrase failed", e); + secretRing.getPrimaryUserIdWithFallback()); + } catch (PgpGeneralException e) { + Log.e(Constants.TAG, "adding of a passphrase failed", e); } if (unlockedSecretKey.getKeyId() != masterKeyId) { @@ -258,20 +258,30 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor } }); - mCanKB = true; - return alert.show(); - } + // Hack to open keyboard. + // This is the only method that I found to work across all Android versions + // http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/ + // Notes: * onCreateView can't be used because we want to add buttons to the dialog + // * opening in onActivityCreated does not work on Android 4.4 + mPassphraseEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + mPassphraseEditText.post(new Runnable() { + @Override + public void run() { + InputMethodManager imm = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(mPassphraseEditText, InputMethodManager.SHOW_IMPLICIT); + } + }); + } + }); + mPassphraseEditText.requestFocus(); - @Override - public void onActivityCreated(Bundle arg0) { - super.onActivityCreated(arg0); - if (mCanKB) { - // request focus and open soft keyboard - mPassphraseEditText.requestFocus(); - getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); - - mPassphraseEditText.setOnEditorActionListener(this); - } + mPassphraseEditText.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); + mPassphraseEditText.setOnEditorActionListener(this); + + return alert.show(); } @Override @@ -282,6 +292,27 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor sendMessageToHandler(MESSAGE_CANCEL); } + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + Log.d(Constants.TAG, "onDismiss"); + + // hide keyboard on dismiss + hideKeyboard(); + } + + private void hideKeyboard() { + InputMethodManager inputManager = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + + //check if no view has focus: + View v = getActivity().getCurrentFocus(); + if (v == null) + return; + + inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); + } + /** * Associate the "done" button on the soft keyboard with the okay button in the view */ diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SetPassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SetPassphraseDialogFragment.java index 93da48b75..1386ed098 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SetPassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/SetPassphraseDialogFragment.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.os.Message; @@ -32,6 +33,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.WindowManager.LayoutParams; import android.view.inputmethod.EditorInfo; +import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; @@ -164,18 +166,50 @@ public class SetPassphraseDialogFragment extends DialogFragment implements OnEdi } }); + // Hack to open keyboard. + // This is the only method that I found to work across all Android versions + // http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/ + // Notes: * onCreateView can't be used because we want to add buttons to the dialog + // * opening in onActivityCreated does not work on Android 4.4 + mPassphraseEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { + @Override + public void onFocusChange(View v, boolean hasFocus) { + mPassphraseEditText.post(new Runnable() { + @Override + public void run() { + InputMethodManager imm = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(mPassphraseEditText, InputMethodManager.SHOW_IMPLICIT); + } + }); + } + }); + mPassphraseEditText.requestFocus(); + + mPassphraseAgainEditText.setImeActionLabel(getString(android.R.string.ok), EditorInfo.IME_ACTION_DONE); + mPassphraseAgainEditText.setOnEditorActionListener(this); + return alert.show(); } @Override - public void onActivityCreated(Bundle arg0) { - super.onActivityCreated(arg0); + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); - // request focus and open soft keyboard - mPassphraseEditText.requestFocus(); - getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); + // hide keyboard on dismiss + hideKeyboard(); + } - mPassphraseAgainEditText.setOnEditorActionListener(this); + private void hideKeyboard() { + InputMethodManager inputManager = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + + //check if no view has focus: + View v = getActivity().getCurrentFocus(); + if (v == null) + return; + + inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); } /** -- cgit v1.2.3 From 0f87b81158d0de0bd4e1af54a0bbc2cd6c10527e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 20:58:25 +0200 Subject: Load QR Code asynchronously and with a fade in animation --- .../keychain/ui/ViewKeyShareFragment.java | 46 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java index 52b573f47..46cd7e25b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java @@ -19,8 +19,16 @@ package org.sufficientlysecure.keychain.ui; import android.annotation.TargetApi; import android.content.Intent; +import android.content.res.Resources; import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.Color; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.ColorDrawable; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.TransitionDrawable; import android.net.Uri; +import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.Settings; @@ -152,7 +160,7 @@ public class ViewKeyShareFragment extends LoaderFragment implements KeyRings.buildUnifiedKeyRingUri(dataUri), Keys.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); String fingerprint = PgpKeyHelper.convertFingerprintToHex(data); - if(!toClipboard){ + if (!toClipboard) { content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; } else { content = fingerprint; @@ -292,10 +300,7 @@ public class ViewKeyShareFragment extends LoaderFragment implements String fingerprint = PgpKeyHelper.convertFingerprintToHex(fingerprintBlob); mFingerprint.setText(PgpKeyHelper.colorizeFingerprint(fingerprint)); - String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; - mFingerprintQrCode.setImageBitmap( - QrCodeUtils.getQRCodeBitmap(qrCodeContent, QR_CODE_SIZE) - ); + loadQrCode(fingerprint); break; } @@ -311,4 +316,35 @@ public class ViewKeyShareFragment extends LoaderFragment implements */ public void onLoaderReset(Loader loader) { } + + /** + * Load QR Code asynchronously and with a fade in animation + * + * @param fingerprint + */ + private void loadQrCode(final String fingerprint) { + AsyncTask loadTask = + new AsyncTask() { + protected Bitmap doInBackground(Void... unused) { + String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; + return QrCodeUtils.getQRCodeBitmap(qrCodeContent, QR_CODE_SIZE); + } + + protected void onPostExecute(Bitmap qrCode) { + mFingerprintQrCode.setImageBitmap(qrCode); + + // Transition drawable with a transparent drawable and the final bitmap + final TransitionDrawable td = + new TransitionDrawable(new Drawable[]{ + new ColorDrawable(Color.TRANSPARENT), + new BitmapDrawable(getResources(), qrCode) + }); + + mFingerprintQrCode.setImageDrawable(td); + td.startTransition(200); + } + }; + + loadTask.execute(); + } } -- cgit v1.2.3 From 5974dccbeaf672db76cb3be7ee639f2bd6638a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 21:40:43 +0200 Subject: Redesigned QR Code view --- .../keychain/ui/QrCodeActivity.java | 114 +++++++++++++++++++++ .../keychain/ui/ViewKeyShareFragment.java | 9 +- .../ui/dialog/ShareQrCodeDialogFragment.java | 111 -------------------- 3 files changed, 119 insertions(+), 115 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java new file mode 100644 index 000000000..f4d395ae6 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.net.Uri; +import android.os.Bundle; +import android.support.v7.app.ActionBarActivity; +import android.view.View; +import android.widget.ImageView; + +import com.devspark.appmsg.AppMsg; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.ActionBarHelper; +import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; +import org.sufficientlysecure.keychain.provider.KeychainContract; +import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.QrCodeUtils; + +public class QrCodeActivity extends ActionBarActivity { + + private ImageView mFingerprintQrCode; + + private static final int QR_CODE_SIZE = 1000; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Inflate a "Done" custom action bar + ActionBarHelper.setOneButtonView(getSupportActionBar(), + R.string.btn_okay, R.drawable.ic_action_done, + new View.OnClickListener() { + @Override + public void onClick(View v) { + // "Done" + finish(); + } + } + ); + + setContentView(R.layout.qr_code_activity); + + Uri dataUri = getIntent().getData(); + if (dataUri == null) { + Log.e(Constants.TAG, "Data missing. Should be Uri of key!"); + finish(); + return; + } + + mFingerprintQrCode = (ImageView) findViewById(R.id.qr_code_image); + + mFingerprintQrCode.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + finish(); + } + }); + + ProviderHelper providerHelper = new ProviderHelper(this); + try { + byte[] blob = (byte[]) providerHelper.getGenericData( + KeychainContract.KeyRings.buildUnifiedKeyRingUri(dataUri), + KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); + if (blob == null) { + Log.e(Constants.TAG, "key not found!"); + AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + finish(); + } + + String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob); + String qrCodeContent = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; + mFingerprintQrCode.setImageBitmap(QrCodeUtils.getQRCodeBitmap(qrCodeContent, QR_CODE_SIZE)); + } catch (ProviderHelper.NotFoundException e) { + Log.e(Constants.TAG, "key not found!", e); + AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + finish(); + } + } + + @Override + protected void onResume() { + super.onResume(); + + // custom activity transition to get zoom in effect + this.overridePendingTransition(R.anim.zoom_enter, android.R.anim.fade_out); + } + + @Override + protected void onPause() { + super.onPause(); + + // custom activity transition to get zoom out effect + this.overridePendingTransition(0, R.anim.zoom_exit); + } + +} \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java index 46cd7e25b..0027ebbfe 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java @@ -19,7 +19,6 @@ package org.sufficientlysecure.keychain.ui; import android.annotation.TargetApi; import android.content.Intent; -import android.content.res.Resources; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Color; @@ -53,7 +52,6 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainContract.Keys; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.ui.dialog.ShareNfcDialogFragment; -import org.sufficientlysecure.keychain.ui.dialog.ShareQrCodeDialogFragment; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.QrCodeUtils; @@ -214,8 +212,11 @@ public class ViewKeyShareFragment extends LoaderFragment implements } private void showQrCodeDialog() { - ShareQrCodeDialogFragment dialog = ShareQrCodeDialogFragment.newInstance(mDataUri); - dialog.show(ViewKeyShareFragment.this.getActivity().getSupportFragmentManager(), "shareQrCodeDialog"); +// ShareQrCodeDialogFragment dialog = ShareQrCodeDialogFragment.newInstance(mDataUri); +// dialog.show(ViewKeyShareFragment.this.getActivity().getSupportFragmentManager(), "shareQrCodeDialog"); + Intent qrCodeIntent = new Intent(getActivity(), QrCodeActivity.class); + qrCodeIntent.setData(mDataUri); + startActivity(qrCodeIntent); } private void showNfcHelpDialog() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java deleted file mode 100644 index 24608784b..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/ShareQrCodeDialogFragment.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2012-2014 Dominik Schürmann - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.sufficientlysecure.keychain.ui.dialog; - -import android.app.Activity; -import android.app.Dialog; -import android.net.Uri; -import android.os.Bundle; -import android.support.v4.app.DialogFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.ImageView; -import android.widget.TextView; - -import com.devspark.appmsg.AppMsg; - -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; -import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.util.Log; -import org.sufficientlysecure.keychain.util.QrCodeUtils; - -public class ShareQrCodeDialogFragment extends DialogFragment { - private static final String ARG_KEY_URI = "uri"; - - private ImageView mImage; - private TextView mText; - - private static final int QR_CODE_SIZE = 1000; - - /** - * Creates new instance of this dialog fragment - */ - public static ShareQrCodeDialogFragment newInstance(Uri dataUri) { - ShareQrCodeDialogFragment frag = new ShareQrCodeDialogFragment(); - Bundle args = new Bundle(); - args.putParcelable(ARG_KEY_URI, dataUri); - - frag.setArguments(args); - - return frag; - } - - /** - * Creates dialog - */ - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final Activity activity = getActivity(); - - Uri dataUri = getArguments().getParcelable(ARG_KEY_URI); - - CustomAlertDialogBuilder alert = new CustomAlertDialogBuilder(getActivity()); - alert.setTitle(R.string.share_qr_code_dialog_title); - - LayoutInflater inflater = activity.getLayoutInflater(); - View view = inflater.inflate(R.layout.share_qr_code_dialog, null); - alert.setView(view); - - mImage = (ImageView) view.findViewById(R.id.share_qr_code_dialog_image); - mText = (TextView) view.findViewById(R.id.share_qr_code_dialog_text); - - ProviderHelper providerHelper = new ProviderHelper(getActivity()); - String content; - try { - alert.setPositiveButton(R.string.btn_okay, null); - - byte[] blob = (byte[]) providerHelper.getGenericData( - KeyRings.buildUnifiedKeyRingUri(dataUri), - KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); - if (blob == null) { - Log.e(Constants.TAG, "key not found!"); - AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); - return null; - } - - String fingerprint = PgpKeyHelper.convertFingerprintToHex(blob); - mText.setText(getString(R.string.share_qr_code_dialog_fingerprint_text) + " " + fingerprint); - content = Constants.FINGERPRINT_SCHEME + ":" + fingerprint; - setQrCode(content); - } catch (ProviderHelper.NotFoundException e) { - Log.e(Constants.TAG, "key not found!", e); - AppMsg.makeText(getActivity(), R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); - return null; - } - - return alert.show(); - } - - private void setQrCode(String data) { - mImage.setImageBitmap(QrCodeUtils.getQRCodeBitmap(data, QR_CODE_SIZE)); - } - -} -- cgit v1.2.3 From 7e8b05657409c398f171f89bfada24604646231b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Sun, 27 Jul 2014 21:44:00 +0200 Subject: cleanup left-overs --- .../java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java | 2 -- 1 file changed, 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java index 0027ebbfe..053907cc8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyShareFragment.java @@ -212,8 +212,6 @@ public class ViewKeyShareFragment extends LoaderFragment implements } private void showQrCodeDialog() { -// ShareQrCodeDialogFragment dialog = ShareQrCodeDialogFragment.newInstance(mDataUri); -// dialog.show(ViewKeyShareFragment.this.getActivity().getSupportFragmentManager(), "shareQrCodeDialog"); Intent qrCodeIntent = new Intent(getActivity(), QrCodeActivity.class); qrCodeIntent.setData(mDataUri); startActivity(qrCodeIntent); -- cgit v1.2.3 From b2f7e839e1fab5bb36116f60c3c2ab8ecaf31e15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 01:23:38 +0200 Subject: Fix resource names, add small test --- .../sufficientlysecure/keychain/ui/KeyListFragment.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 5ace66f79..e44708c42 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Color; +import android.graphics.PorterDuff; import android.net.Uri; import android.os.Build; import android.os.Bundle; @@ -437,7 +438,7 @@ public class KeyListFragment extends LoaderFragment TextView mMainUserIdRest; FrameLayout mStatusLayout; TextView mRevoked; - ImageView mVerified; + ImageView mStatus; } @Override @@ -448,7 +449,7 @@ public class KeyListFragment extends LoaderFragment holder.mMainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); holder.mStatusLayout = (FrameLayout) view.findViewById(R.id.status_layout); holder.mRevoked = (TextView) view.findViewById(R.id.revoked); - holder.mVerified = (ImageView) view.findViewById(R.id.verified); + holder.mStatus = (ImageView) view.findViewById(R.id.status_image); view.setTag(holder); return view; } @@ -486,7 +487,7 @@ public class KeyListFragment extends LoaderFragment // this is a secret key h.mStatusLayout.setVisibility(View.VISIBLE); h.mRevoked.setVisibility(View.GONE); - h.mVerified.setVisibility(View.GONE); + h.mStatus.setVisibility(View.GONE); } else { // this is a public key - show if it's revoked @@ -496,13 +497,17 @@ public class KeyListFragment extends LoaderFragment if(isRevoked || isExpired) { h.mStatusLayout.setVisibility(View.VISIBLE); h.mRevoked.setVisibility(View.VISIBLE); - h.mVerified.setVisibility(View.GONE); + h.mStatus.setVisibility(View.GONE); h.mRevoked.setText(isRevoked ? R.string.revoked : R.string.expired); } else { boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; h.mStatusLayout.setVisibility(isVerified ? View.VISIBLE : View.GONE); h.mRevoked.setVisibility(View.GONE); - h.mVerified.setVisibility(isVerified ? View.VISIBLE : View.GONE); + h.mStatus.setVisibility(isVerified ? View.VISIBLE : View.GONE); + if (isVerified) { + h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), + PorterDuff.Mode.MULTIPLY); + } } } } -- cgit v1.2.3 From 0c3b2a6ed4c0ffb3022c064e28254f6567f4ba12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 09:10:45 +0200 Subject: Smaller status images, green test --- .../main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index e44708c42..60efed9b4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -506,7 +506,7 @@ public class KeyListFragment extends LoaderFragment h.mStatus.setVisibility(isVerified ? View.VISIBLE : View.GONE); if (isVerified) { h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), - PorterDuff.Mode.MULTIPLY); + PorterDuff.Mode.SRC_ATOP); } } } -- cgit v1.2.3 From a3045c710e44a189f3166c5d56b94117a1318731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 09:34:57 +0200 Subject: Use new status icons in key list --- .../keychain/ui/KeyListFragment.java | 43 ++++++++++++---------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 60efed9b4..ef3c0a1c1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -436,8 +436,6 @@ public class KeyListFragment extends LoaderFragment private class ItemViewHolder { TextView mMainUserId; TextView mMainUserIdRest; - FrameLayout mStatusLayout; - TextView mRevoked; ImageView mStatus; } @@ -447,8 +445,6 @@ public class KeyListFragment extends LoaderFragment ItemViewHolder holder = new ItemViewHolder(); holder.mMainUserId = (TextView) view.findViewById(R.id.mainUserId); holder.mMainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest); - holder.mStatusLayout = (FrameLayout) view.findViewById(R.id.status_layout); - holder.mRevoked = (TextView) view.findViewById(R.id.revoked); holder.mStatus = (ImageView) view.findViewById(R.id.status_image); view.setTag(holder); return view; @@ -485,29 +481,36 @@ public class KeyListFragment extends LoaderFragment if (cursor.getInt(KeyListFragment.INDEX_HAS_ANY_SECRET) != 0) { // this is a secret key - h.mStatusLayout.setVisibility(View.VISIBLE); - h.mRevoked.setVisibility(View.GONE); h.mStatus.setVisibility(View.GONE); } else { - // this is a public key - show if it's revoked + // this is a public key - show if it's revoked, expired, or verified boolean isRevoked = cursor.getInt(INDEX_IS_REVOKED) > 0; boolean isExpired = !cursor.isNull(INDEX_EXPIRY) && new Date(cursor.getLong(INDEX_EXPIRY)*1000).before(new Date()); - if(isRevoked || isExpired) { - h.mStatusLayout.setVisibility(View.VISIBLE); - h.mRevoked.setVisibility(View.VISIBLE); - h.mStatus.setVisibility(View.GONE); - h.mRevoked.setText(isRevoked ? R.string.revoked : R.string.expired); + boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; + + // Note: order is important! + if (isRevoked) { + h.mStatus.setVisibility(View.VISIBLE); + h.mStatus.setImageDrawable( + getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); + h.mStatus.setColorFilter(getResources().getColor(R.color.result_red), + PorterDuff.Mode.SRC_ATOP); + } else if (isExpired) { + h.mStatus.setVisibility(View.VISIBLE); + h.mStatus.setImageDrawable( + getResources().getDrawable(R.drawable.status_signature_expired_cutout)); + h.mStatus.setColorFilter(getResources().getColor(R.color.result_orange), + PorterDuff.Mode.SRC_ATOP); + } else if (isVerified) { + h.mStatus.setVisibility(View.VISIBLE); + h.mStatus.setImageDrawable( + getResources().getDrawable(R.drawable.status_signature_verified_cutout)); + h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), + PorterDuff.Mode.SRC_ATOP); } else { - boolean isVerified = cursor.getInt(INDEX_VERIFIED) > 0; - h.mStatusLayout.setVisibility(isVerified ? View.VISIBLE : View.GONE); - h.mRevoked.setVisibility(View.GONE); - h.mStatus.setVisibility(isVerified ? View.VISIBLE : View.GONE); - if (isVerified) { - h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), - PorterDuff.Mode.SRC_ATOP); - } + h.mStatus.setVisibility(View.GONE); } } } -- cgit v1.2.3 From 3d6edd11903898b8dc95a52d26b8dc3716d85e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 11:11:23 +0200 Subject: Use icons in key view --- .../keychain/ui/KeyListFragment.java | 6 +-- .../keychain/ui/ViewKeyActivity.java | 55 ++++++++++++++-------- 2 files changed, 39 insertions(+), 22 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index ef3c0a1c1..ee05a5fe4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -494,19 +494,19 @@ public class KeyListFragment extends LoaderFragment if (isRevoked) { h.mStatus.setVisibility(View.VISIBLE); h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); + getResources().getDrawable(R.drawable.status_signature_revoked)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_red), PorterDuff.Mode.SRC_ATOP); } else if (isExpired) { h.mStatus.setVisibility(View.VISIBLE); h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_expired_cutout)); + getResources().getDrawable(R.drawable.status_signature_expired)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_orange), PorterDuff.Mode.SRC_ATOP); } else if (isVerified) { h.mStatus.setVisibility(View.VISIBLE); h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_verified_cutout)); + getResources().getDrawable(R.drawable.status_signature_verified)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), PorterDuff.Mode.SRC_ATOP); } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index a9cd0976b..8407ab66d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -22,6 +22,7 @@ import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.database.Cursor; +import android.graphics.PorterDuff; import android.net.Uri; import android.nfc.NdefMessage; import android.nfc.NdefRecord; @@ -43,6 +44,9 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.Window; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.TextView; import com.devspark.appmsg.AppMsg; @@ -81,9 +85,11 @@ public class ViewKeyActivity extends ActionBarActivity implements private ViewPager mViewPager; private SlidingTabLayout mSlidingTabLayout; private PagerTabStripAdapter mTabsAdapter; + + private LinearLayout mStatusLayout; + private TextView mStatusText; + private ImageView mStatusImage; private View mStatusDivider; - private View mStatusRevoked; - private View mStatusExpired; public static final int REQUEST_CODE_LOOKUP_KEY = 0x00007006; @@ -115,9 +121,10 @@ public class ViewKeyActivity extends ActionBarActivity implements setContentView(R.layout.view_key_activity); - mStatusDivider = findViewById(R.id.status_divider); - mStatusRevoked = findViewById(R.id.view_key_revoked); - mStatusExpired = findViewById(R.id.view_key_expired); + mStatusLayout = (LinearLayout) findViewById(R.id.view_key_status_layout); + mStatusText = (TextView) findViewById(R.id.view_key_status_text); + mStatusImage = (ImageView) findViewById(R.id.view_key_status_image); + mStatusDivider = findViewById(R.id.view_key_status_divider); mViewPager = (ViewPager) findViewById(R.id.view_key_pager); mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.view_key_sliding_tab_layout); @@ -515,22 +522,32 @@ public class ViewKeyActivity extends ActionBarActivity implements String keyIdStr = PgpKeyHelper.convertKeyIdToHex(masterKeyId); getSupportActionBar().setSubtitle(keyIdStr); - // If this key is revoked, it cannot be used for anything! - if (data.getInt(INDEX_UNIFIED_IS_REVOKED) != 0) { + boolean isRevoked = data.getInt(INDEX_UNIFIED_IS_REVOKED) > 0; + boolean isExpired = !data.isNull(INDEX_UNIFIED_EXPIRY) + && new Date(data.getLong(INDEX_UNIFIED_EXPIRY) * 1000).before(new Date()); + + // Note: order is important + if (isRevoked) { + mStatusDivider.setVisibility(View.VISIBLE); + mStatusLayout.setVisibility(View.VISIBLE); + mStatusText.setText(R.string.view_key_revoked); + mStatusText.setTextColor(getResources().getColor(R.color.result_red)); + mStatusImage.setImageDrawable( + getResources().getDrawable(R.drawable.status_signature_revoked)); + mStatusImage.setColorFilter(getResources().getColor(R.color.result_red), + PorterDuff.Mode.SRC_ATOP); + } else if (isExpired) { mStatusDivider.setVisibility(View.VISIBLE); - mStatusRevoked.setVisibility(View.VISIBLE); - mStatusExpired.setVisibility(View.GONE); + mStatusLayout.setVisibility(View.VISIBLE); + mStatusText.setText(R.string.view_key_expired); + mStatusText.setTextColor(getResources().getColor(R.color.result_orange)); + mStatusImage.setImageDrawable( + getResources().getDrawable(R.drawable.status_signature_expired_cutout)); + mStatusImage.setColorFilter(getResources().getColor(R.color.result_orange), + PorterDuff.Mode.SRC_ATOP); } else { - mStatusRevoked.setVisibility(View.GONE); - - Date expiryDate = new Date(data.getLong(INDEX_UNIFIED_EXPIRY) * 1000); - if (!data.isNull(INDEX_UNIFIED_EXPIRY) && expiryDate.before(new Date())) { - mStatusDivider.setVisibility(View.VISIBLE); - mStatusExpired.setVisibility(View.VISIBLE); - } else { - mStatusDivider.setVisibility(View.GONE); - mStatusExpired.setVisibility(View.GONE); - } + mStatusDivider.setVisibility(View.GONE); + mStatusLayout.setVisibility(View.GONE); } break; -- cgit v1.2.3 From 730f820c8c6edbf5a86bb7a1389b5d52ca07bba3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 11:17:12 +0200 Subject: Set visibility at last --- .../java/org/sufficientlysecure/keychain/ui/KeyListFragment.java | 6 +++--- .../java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index ee05a5fe4..5c74b6602 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -492,23 +492,23 @@ public class KeyListFragment extends LoaderFragment // Note: order is important! if (isRevoked) { - h.mStatus.setVisibility(View.VISIBLE); h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_revoked)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_red), PorterDuff.Mode.SRC_ATOP); - } else if (isExpired) { h.mStatus.setVisibility(View.VISIBLE); + } else if (isExpired) { h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_expired)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_orange), PorterDuff.Mode.SRC_ATOP); - } else if (isVerified) { h.mStatus.setVisibility(View.VISIBLE); + } else if (isVerified) { h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_verified)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), PorterDuff.Mode.SRC_ATOP); + h.mStatus.setVisibility(View.VISIBLE); } else { h.mStatus.setVisibility(View.GONE); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 8407ab66d..d18a63948 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -528,23 +528,23 @@ public class ViewKeyActivity extends ActionBarActivity implements // Note: order is important if (isRevoked) { - mStatusDivider.setVisibility(View.VISIBLE); - mStatusLayout.setVisibility(View.VISIBLE); mStatusText.setText(R.string.view_key_revoked); mStatusText.setTextColor(getResources().getColor(R.color.result_red)); mStatusImage.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_revoked)); mStatusImage.setColorFilter(getResources().getColor(R.color.result_red), PorterDuff.Mode.SRC_ATOP); - } else if (isExpired) { mStatusDivider.setVisibility(View.VISIBLE); mStatusLayout.setVisibility(View.VISIBLE); + } else if (isExpired) { mStatusText.setText(R.string.view_key_expired); mStatusText.setTextColor(getResources().getColor(R.color.result_orange)); mStatusImage.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_expired_cutout)); mStatusImage.setColorFilter(getResources().getColor(R.color.result_orange), PorterDuff.Mode.SRC_ATOP); + mStatusDivider.setVisibility(View.VISIBLE); + mStatusLayout.setVisibility(View.VISIBLE); } else { mStatusDivider.setVisibility(View.GONE); mStatusLayout.setVisibility(View.GONE); -- cgit v1.2.3 From 23689da56db1c4f5e53d6e028a619f357a73586c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 12:24:40 +0200 Subject: Use verified icon for certify action --- .../keychain/ui/ViewKeyMainFragment.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java index f0636cf2c..50e1c6ad1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyMainFragment.java @@ -19,6 +19,7 @@ package org.sufficientlysecure.keychain.ui; import android.content.Intent; import android.database.Cursor; +import android.graphics.PorterDuff; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.LoaderManager; @@ -27,6 +28,7 @@ import android.support.v4.content.Loader; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ImageView; import android.widget.ListView; import com.devspark.appmsg.AppMsg; @@ -52,6 +54,8 @@ public class ViewKeyMainFragment extends LoaderFragment implements private View mActionEditDivider; private View mActionEncrypt; private View mActionCertify; + private View mActionCertifyText; + private ImageView mActionCertifyImage; private View mActionCertifyDivider; private ListView mUserIds; @@ -76,6 +80,11 @@ public class ViewKeyMainFragment extends LoaderFragment implements mActionEditDivider = view.findViewById(R.id.view_key_action_edit_divider); mActionEncrypt = view.findViewById(R.id.view_key_action_encrypt); mActionCertify = view.findViewById(R.id.view_key_action_certify); + mActionCertifyText = view.findViewById(R.id.view_key_action_certify_text); + mActionCertifyImage = (ImageView) view.findViewById(R.id.view_key_action_certify_image); + // make certify image gray, like action icons + mActionCertifyImage.setColorFilter(getResources().getColor(R.color.tertiary_text_light), + PorterDuff.Mode.SRC_IN); mActionCertifyDivider = view.findViewById(R.id.view_key_action_certify_divider); return root; @@ -182,6 +191,7 @@ public class ViewKeyMainFragment extends LoaderFragment implements if (data.getInt(INDEX_UNIFIED_IS_REVOKED) != 0) { mActionEdit.setEnabled(false); mActionCertify.setEnabled(false); + mActionCertifyText.setEnabled(false); mActionEncrypt.setEnabled(false); } else { mActionEdit.setEnabled(true); @@ -189,9 +199,11 @@ public class ViewKeyMainFragment extends LoaderFragment implements Date expiryDate = new Date(data.getLong(INDEX_UNIFIED_EXPIRY) * 1000); if (!data.isNull(INDEX_UNIFIED_EXPIRY) && expiryDate.before(new Date())) { mActionCertify.setEnabled(false); + mActionCertifyText.setEnabled(false); mActionEncrypt.setEnabled(false); } else { mActionCertify.setEnabled(true); + mActionCertifyText.setEnabled(true); mActionEncrypt.setEnabled(true); } } @@ -252,8 +264,6 @@ public class ViewKeyMainFragment extends LoaderFragment implements private void editKey(Uri dataUri) { Intent editIntent = new Intent(getActivity(), EditKeyActivity.class); editIntent.setData(KeychainContract.KeyRingData.buildSecretKeyRingUri(dataUri)); -// editIntent.setAction(EditKeyActivity.ACTION_EDIT_KEY); -// startActivityForResult(editIntent, 0); startActivity(editIntent); } -- cgit v1.2.3 From ed6a89f35c21e92108036ec40722c1a3f9d8641c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Jul 2014 16:10:36 +0200 Subject: Use new icons in other key related activites, delete old icons --- .../keychain/ui/CertifyKeyActivity.java | 24 +++++++++---- .../keychain/ui/KeyListFragment.java | 6 ++-- .../keychain/ui/ViewKeyActivity.java | 2 +- .../keychain/ui/adapter/UserIdsAdapter.java | 40 +++++++++++++++------- 4 files changed, 49 insertions(+), 23 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java index 7ac229b91..7b888eccb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui; import android.app.ProgressDialog; import android.content.Intent; import android.database.Cursor; +import android.graphics.PorterDuff; import android.net.Uri; import android.os.Bundle; import android.os.Handler; @@ -37,6 +38,7 @@ import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.ImageView; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; @@ -64,7 +66,8 @@ import java.util.ArrayList; */ public class CertifyKeyActivity extends ActionBarActivity implements SelectSecretKeyLayoutFragment.SelectSecretKeyCallback, LoaderManager.LoaderCallbacks { - private View mSignButton; + private View mCertifyButton; + private ImageView mActionCertifyImage; private CheckBox mUploadKeyCheckbox; private Spinner mSelectKeyserverSpinner; @@ -88,10 +91,19 @@ public class CertifyKeyActivity extends ActionBarActivity implements mSelectKeyFragment = (SelectSecretKeyLayoutFragment) getSupportFragmentManager() .findFragmentById(R.id.sign_key_select_key_fragment); + mSelectKeyserverSpinner = (Spinner) findViewById(R.id.upload_key_keyserver); + mUploadKeyCheckbox = (CheckBox) findViewById(R.id.sign_key_upload_checkbox); + mCertifyButton = findViewById(R.id.certify_key_certify_button); + mActionCertifyImage = (ImageView) findViewById(R.id.certify_key_action_certify_image); + mUserIds = (ListView) findViewById(R.id.view_key_user_ids); + + // make certify image gray, like action icons + mActionCertifyImage.setColorFilter(getResources().getColor(R.color.tertiary_text_light), + PorterDuff.Mode.SRC_IN); + mSelectKeyFragment.setCallback(this); mSelectKeyFragment.setFilterCertify(true); - mSelectKeyserverSpinner = (Spinner) findViewById(R.id.upload_key_keyserver); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, Preferences.getPreferences(this) .getKeyServers() @@ -99,7 +111,6 @@ public class CertifyKeyActivity extends ActionBarActivity implements adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSelectKeyserverSpinner.setAdapter(adapter); - mUploadKeyCheckbox = (CheckBox) findViewById(R.id.sign_key_upload_checkbox); if (!mUploadKeyCheckbox.isChecked()) { mSelectKeyserverSpinner.setEnabled(false); } else { @@ -118,8 +129,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements } }); - mSignButton = findViewById(R.id.sign_key_sign_button); - mSignButton.setOnClickListener(new OnClickListener() { + mCertifyButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { @@ -141,7 +151,6 @@ public class CertifyKeyActivity extends ActionBarActivity implements } Log.e(Constants.TAG, "uri: " + mDataUri); - mUserIds = (ListView) findViewById(R.id.view_key_user_ids); mUserIdsAdapter = new UserIdsAdapter(this, null, 0, true); mUserIds.setAdapter(mUserIdsAdapter); @@ -230,7 +239,8 @@ public class CertifyKeyActivity extends ActionBarActivity implements startSigning(); } } - }); + } + ); // bail out; need to wait until the user has entered the passphrase before trying again return; } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 5c74b6602..e2321ccf2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -493,19 +493,19 @@ public class KeyListFragment extends LoaderFragment // Note: order is important! if (isRevoked) { h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_revoked)); + getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_red), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); } else if (isExpired) { h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_expired)); + getResources().getDrawable(R.drawable.status_signature_expired_cutout)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_orange), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); } else if (isVerified) { h.mStatus.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_verified)); + getResources().getDrawable(R.drawable.status_signature_verified_cutout)); h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index d18a63948..c64cfe417 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -531,7 +531,7 @@ public class ViewKeyActivity extends ActionBarActivity implements mStatusText.setText(R.string.view_key_revoked); mStatusText.setTextColor(getResources().getColor(R.color.result_red)); mStatusImage.setImageDrawable( - getResources().getDrawable(R.drawable.status_signature_revoked)); + getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); mStatusImage.setColorFilter(getResources().getColor(R.color.result_red), PorterDuff.Mode.SRC_ATOP); mStatusDivider.setVisibility(View.VISIBLE); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index 18312660a..6519915fa 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -19,6 +19,8 @@ package org.sufficientlysecure.keychain.ui.adapter; import android.content.Context; import android.database.Cursor; +import android.graphics.PorterDuff; +import android.graphics.Typeface; import android.support.v4.widget.CursorAdapter; import android.view.LayoutInflater; import android.view.View; @@ -156,7 +158,10 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC if (isRevoked) { // set revocation icon (can this even be primary?) - vVerified.setImageResource(R.drawable.key_certify_revoke); + vVerified.setImageResource(R.drawable.status_signature_revoked_cutout); + vVerified.setColorFilter( + mContext.getResources().getColor(R.color.bg_gray), + PorterDuff.Mode.SRC_IN); // disable and strike through text for revoked user ids vName.setEnabled(false); @@ -170,22 +175,33 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC vAddress.setEnabled(true); vComment.setEnabled(true); - // verified: has been verified - // isPrimary: show small star icon for primary user ids - int verified = cursor.getInt(INDEX_VERIFIED); - switch (verified) { + if (isPrimary) { + vName.setTypeface(null, Typeface.BOLD); + vAddress.setTypeface(null, Typeface.BOLD); + } else { + vName.setTypeface(null, Typeface.NORMAL); + vAddress.setTypeface(null, Typeface.NORMAL); + } + + int isVerified = cursor.getInt(INDEX_VERIFIED); + switch (isVerified) { case Certs.VERIFIED_SECRET: - vVerified.setImageResource(isPrimary - ? R.drawable.key_certify_primary_ok_depth0 - : R.drawable.key_certify_ok_depth0); + vVerified.setImageResource(R.drawable.status_signature_verified_cutout); + vVerified.setColorFilter( + mContext.getResources().getColor(R.color.result_green), + PorterDuff.Mode.SRC_IN); break; case Certs.VERIFIED_SELF: - vVerified.setImageResource(isPrimary - ? R.drawable.key_certify_primary_ok_self - : R.drawable.key_certify_ok_self); + vVerified.setImageResource(R.drawable.status_signature_unverified_cutout); + vVerified.setColorFilter( + mContext.getResources().getColor(R.color.bg_gray), + PorterDuff.Mode.SRC_IN); break; default: - vVerified.setImageResource(R.drawable.key_certify_error); + vVerified.setImageResource(R.drawable.status_signature_invalid_cutout); + vVerified.setColorFilter( + mContext.getResources().getColor(R.color.result_red), + PorterDuff.Mode.SRC_IN); break; } } -- cgit v1.2.3 From da131220aac727865b9e33e8d34743939e8acbf0 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 28 Jul 2014 17:04:25 +0200 Subject: watch out for nullpointers from get(Un|)HashedSubpackets fixes #721 --- .../keychain/pgp/PgpKeyOperation.java | 5 +++-- .../keychain/pgp/UncachedKeyRing.java | 3 ++- .../keychain/pgp/WrappedSignature.java | 21 ++++++++++++++------- 3 files changed, 19 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index 69244ca14..6bce07824 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -413,7 +413,8 @@ public class PgpKeyOperation { } // if this is~ the/a primary user id - if (currentCert.hasSubpackets() && currentCert.getHashedSubPackets().isPrimaryUserID()) { + if (currentCert.getHashedSubPackets() != null + && currentCert.getHashedSubPackets().isPrimaryUserID()) { // if it's the one we want, just leave it as is if (userId.equals(saveParcel.mChangePrimaryUserId)) { ok = true; @@ -740,7 +741,7 @@ public class PgpKeyOperation { int flags = 0; //noinspection unchecked for(PGPSignature sig : new IterableIterator(key.getSignatures())) { - if (!sig.hasSubpackets()) { + if (sig.getHashedSubPackets() == null) { continue; } flags |= sig.getHashedSubPackets().getKeyFlags(); 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 8838075cd..de8c683ff 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -513,7 +513,8 @@ public class UncachedKeyRing { } // if this certificate says it allows signing for the key - if (zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { + if (zert.getHashedSubPackets() != null && + zert.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.KEY_FLAGS)) { int flags = ((KeyFlags) zert.getHashedSubPackets() .getSubpacket(SignatureSubpacketTags.KEY_FLAGS)).getFlags(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java index df19930c4..28e4c51d6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java @@ -63,13 +63,17 @@ public class WrappedSignature { } try { PGPSignatureList list; - list = mSig.getHashedSubPackets().getEmbeddedSignatures(); - for(int i = 0; i < list.size(); i++) { - sigs.add(new WrappedSignature(list.get(i))); + if (mSig.getHashedSubPackets() != null) { + list = mSig.getHashedSubPackets().getEmbeddedSignatures(); + for (int i = 0; i < list.size(); i++) { + sigs.add(new WrappedSignature(list.get(i))); + } } - list = mSig.getUnhashedSubPackets().getEmbeddedSignatures(); - for(int i = 0; i < list.size(); i++) { - sigs.add(new WrappedSignature(list.get(i))); + if (mSig.getUnhashedSubPackets() != null) { + list = mSig.getUnhashedSubPackets().getEmbeddedSignatures(); + for (int i = 0; i < list.size(); i++) { + sigs.add(new WrappedSignature(list.get(i))); + } } } catch (PGPException e) { // no matter @@ -97,6 +101,9 @@ public class WrappedSignature { if(!isRevocation()) { throw new PgpGeneralException("Not a revocation signature."); } + if (mSig.getHashedSubPackets() == null) { + return null; + } SignatureSubpacket p = mSig.getHashedSubPackets().getSubpacket( SignatureSubpacketTags.REVOCATION_REASON); // For some reason, this is missing in SignatureSubpacketInputStream:146 @@ -205,7 +212,7 @@ public class WrappedSignature { } public boolean isLocal() { - if (!mSig.hasSubpackets() + if (mSig.getHashedSubPackets() == null || !mSig.getHashedSubPackets().hasSubpacket(SignatureSubpacketTags.EXPORTABLE)) { return false; } -- cgit v1.2.3 From 4457b4a24c4078723c94f949235e040de0328c63 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Tue, 29 Jul 2014 14:00:36 +0200 Subject: fix a couple remaining AppMsgs --- .../org/sufficientlysecure/keychain/ui/CreateKeyActivity.java | 9 +++------ .../java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java | 8 ++++---- 2 files changed, 7 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index 3cf89f6a4..57390ec4d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -20,7 +20,6 @@ package org.sufficientlysecure.keychain.ui; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; -import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; @@ -31,13 +30,10 @@ import android.util.Patterns; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; -import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Spinner; -import com.devspark.appmsg.AppMsg; - import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; @@ -45,6 +41,7 @@ import org.sufficientlysecure.keychain.helper.ContactHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; +import org.sufficientlysecure.keychain.util.Notify; import java.util.regex.Matcher; @@ -206,8 +203,8 @@ public class CreateKeyActivity extends ActionBarActivity { super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - AppMsg.makeText(CreateKeyActivity.this, R.string.key_send_success, - AppMsg.STYLE_INFO).show(); + Notify.showNotify(CreateKeyActivity.this, R.string.key_send_success, + Notify.Style.INFO); CreateKeyActivity.this.setResult(RESULT_OK); CreateKeyActivity.this.finish(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java index f4d395ae6..99400056f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java @@ -23,8 +23,6 @@ import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.ImageView; -import com.devspark.appmsg.AppMsg; - import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.ActionBarHelper; @@ -32,6 +30,8 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; +import org.sufficientlysecure.keychain.util.Notify; +import org.sufficientlysecure.keychain.util.Notify.Style; import org.sufficientlysecure.keychain.util.QrCodeUtils; public class QrCodeActivity extends ActionBarActivity { @@ -81,7 +81,7 @@ public class QrCodeActivity extends ActionBarActivity { KeychainContract.KeyRings.FINGERPRINT, ProviderHelper.FIELD_TYPE_BLOB); if (blob == null) { Log.e(Constants.TAG, "key not found!"); - AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(this, R.string.error_key_not_found, Style.ERROR); finish(); } @@ -90,7 +90,7 @@ public class QrCodeActivity extends ActionBarActivity { mFingerprintQrCode.setImageBitmap(QrCodeUtils.getQRCodeBitmap(qrCodeContent, QR_CODE_SIZE)); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); - AppMsg.makeText(this, R.string.error_key_not_found, AppMsg.STYLE_ALERT).show(); + Notify.showNotify(this, R.string.error_key_not_found, Style.ERROR); finish(); } } -- cgit v1.2.3 From 6448de8f6ac84a8dcc91908af37c8826e32aa785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 00:50:53 +0200 Subject: Create key with fragments --- .../remote/ui/AccountSettingsFragment.java | 13 +- .../keychain/ui/CreateKeyActivity.java | 252 ++++----------------- .../keychain/ui/CreateKeyFinalFragment.java | 220 ++++++++++++++++++ .../keychain/ui/CreateKeyInputFragment.java | 200 ++++++++++++++++ .../keychain/ui/QrCodeActivity.java | 4 +- 5 files changed, 472 insertions(+), 217 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java index 0b1d521ad..0a1822798 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/ui/AccountSettingsFragment.java @@ -32,9 +32,12 @@ import android.widget.Button; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.pgp.KeyRing; +import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.remote.AccountSettings; +import org.sufficientlysecure.keychain.ui.CreateKeyActivity; import org.sufficientlysecure.keychain.ui.EditKeyActivityOld; import org.sufficientlysecure.keychain.ui.SelectSecretKeyLayoutFragment; import org.sufficientlysecure.keychain.ui.adapter.KeyValueSpinnerAdapter; @@ -163,11 +166,11 @@ public class AccountSettingsFragment extends Fragment implements } private void createKey() { - Intent intent = new Intent(getActivity(), EditKeyActivityOld.class); - intent.setAction(EditKeyActivityOld.ACTION_CREATE_KEY); - intent.putExtra(EditKeyActivityOld.EXTRA_GENERATE_DEFAULT_KEYS, true); - // set default user id to account name - intent.putExtra(EditKeyActivityOld.EXTRA_USER_IDS, mAccSettings.getAccountName()); + String[] userId = KeyRing.splitUserId(mAccSettings.getAccountName()); + + Intent intent = new Intent(getActivity(), CreateKeyActivity.class); + intent.putExtra(CreateKeyActivity.EXTRA_NAME, userId[0]); + intent.putExtra(CreateKeyActivity.EXTRA_EMAIL, userId[1]); startActivityForResult(intent, REQUEST_CODE_CREATE_KEY); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index 3cf89f6a4..40e8af72d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -17,233 +17,65 @@ package org.sufficientlysecure.keychain.ui; -import android.app.ProgressDialog; -import android.content.Context; -import android.content.Intent; -import android.net.Uri; import android.os.Bundle; -import android.os.Message; -import android.os.Messenger; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBarActivity; -import android.text.Editable; -import android.text.TextWatcher; -import android.util.Patterns; -import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.AutoCompleteTextView; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.Spinner; -import com.devspark.appmsg.AppMsg; - -import org.spongycastle.bcpg.sig.KeyFlags; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.helper.ContactHelper; -import org.sufficientlysecure.keychain.service.KeychainIntentService; -import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; -import org.sufficientlysecure.keychain.service.SaveKeyringParcel; - -import java.util.regex.Matcher; public class CreateKeyActivity extends ActionBarActivity { - AutoCompleteTextView mNameEdit; - AutoCompleteTextView mEmailEdit; - EditText mPassphraseEdit; - View mCreateButton; - CheckBox mUploadCheckbox; + public static final String EXTRA_NAME = "name"; + public static final String EXTRA_EMAIL = "email"; + + public static final int ANIM_NO = 0; + public static final int ANIM_TO_RIGHT = 1; + public static final int ANIM_TO_LEFT = 2; @Override - protected void onCreate(Bundle savedInstanceState) { + public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.create_key_activity); - mNameEdit = (AutoCompleteTextView) findViewById(R.id.name); - mEmailEdit = (AutoCompleteTextView) findViewById(R.id.email); - mPassphraseEdit = (EditText) findViewById(R.id.passphrase); - mCreateButton = findViewById(R.id.create_key_button); - mUploadCheckbox = (CheckBox) findViewById(R.id.create_key_upload); - - mEmailEdit.setThreshold(1); // Start working from first character - mEmailEdit.setAdapter( - new ArrayAdapter - (this, android.R.layout.simple_spinner_dropdown_item, - ContactHelper.getPossibleUserEmails(this) - ) - ); - mEmailEdit.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { - } - - @Override - public void afterTextChanged(Editable editable) { - String email = editable.toString(); - if (email.length() > 0) { - Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); - if (emailMatcher.matches()) { - mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, - R.drawable.uid_mail_ok, 0); - } else { - mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, - R.drawable.uid_mail_bad, 0); - } - } else { - // remove drawable if email is empty - mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); - } - } - }); - - mNameEdit.setThreshold(1); // Start working from first character - mNameEdit.setAdapter( - new ArrayAdapter - (this, android.R.layout.simple_spinner_dropdown_item, - ContactHelper.getPossibleUserNames(this) - ) - ); - - mCreateButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - createKeyCheck(); - } - }); - + // pass extras into fragment + CreateKeyInputFragment frag = + CreateKeyInputFragment.newInstance( + getIntent().getStringExtra(EXTRA_NAME), + getIntent().getStringExtra(EXTRA_EMAIL) + ); + loadFragment(null, frag, ANIM_NO); } - private void createKeyCheck() { - if (isEditTextNotEmpty(this, mNameEdit) - && isEditTextNotEmpty(this, mEmailEdit) - && isEditTextNotEmpty(this, mPassphraseEdit)) { - createKey(); + public void loadFragment(Bundle savedInstanceState, Fragment fragment, int animation) { + // However, if we're being restored from a previous state, + // then we don't need to do anything and should return or else + // we could end up with overlapping fragments. + if (savedInstanceState != null) { + return; } - } - - private void createKey() { - Intent intent = new Intent(this, KeychainIntentService.class); - intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); - - // Message is received after importing is done in KeychainIntentService - KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( - this, - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL) { - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - // TODO -// if (mUploadCheckbox.isChecked()) { -// uploadKey(); -// } else { - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - CreateKeyActivity.this.setResult(RESULT_OK); - CreateKeyActivity.this.finish(); - } -// } - } - }; - - // fill values for this action - Bundle data = new Bundle(); - - SaveKeyringParcel parcel = new SaveKeyringParcel(); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.CERTIFY_OTHER, null)); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); - parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); - String userId = mNameEdit.getText().toString() + " <" + mEmailEdit.getText().toString() + ">"; - parcel.mAddUserIds.add(userId); - parcel.mNewPassphrase = mPassphraseEdit.getText().toString(); - - // get selected key entries - data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); - - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - saveHandler.showProgressDialog(this); - - startService(intent); - } - - private void uploadKey() { - // Send all information needed to service to upload key in other thread - Intent intent = new Intent(this, KeychainIntentService.class); - - intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); - - // set data uri as path to keyring - // TODO -// Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); -// intent.setData(blobUri); - - // fill values for this action - Bundle data = new Bundle(); - - Spinner keyServer = (Spinner) findViewById(R.id.upload_key_keyserver); - String server = (String) keyServer.getSelectedItem(); - data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server); - - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - - // Message is received after uploading is done in KeychainIntentService - KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this, - getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) { - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { - AppMsg.makeText(CreateKeyActivity.this, R.string.key_send_success, - AppMsg.STYLE_INFO).show(); - - CreateKeyActivity.this.setResult(RESULT_OK); - CreateKeyActivity.this.finish(); - } - } - }; - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - - // show progress dialog - saveHandler.showProgressDialog(this); - - // start service with intent - startService(intent); - } - - /** - * Checks if text of given EditText is not empty. If it is empty an error is - * set and the EditText gets the focus. - * - * @param context - * @param editText - * @return true if EditText is not empty - */ - private static boolean isEditTextNotEmpty(Context context, EditText editText) { - boolean output = true; - if (editText.getText().toString().length() == 0) { - editText.setError(context.getString(R.string.create_key_empty)); - editText.requestFocus(); - output = false; - } else { - editText.setError(null); + // Add the fragment to the 'fragment_container' FrameLayout + // NOTE: We use commitAllowingStateLoss() to prevent weird crashes! + FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); + + switch (animation) { + case ANIM_NO: + transaction.setCustomAnimations(0, 0); + break; + case ANIM_TO_LEFT: + transaction.setCustomAnimations(R.anim.frag_slide_in_from_left, R.anim.frag_slide_out_to_right); + break; + case ANIM_TO_RIGHT: + transaction.setCustomAnimations(R.anim.frag_slide_out_to_left, R.anim.frag_slide_in_from_right); + transaction.addToBackStack("back"); + break; } - - return output; + transaction.replace(R.id.create_key_fragment_container, fragment) + .commitAllowingStateLoss(); + // do it immediately! + getSupportFragmentManager().executePendingTransactions(); } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java new file mode 100644 index 000000000..dffdfe448 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -0,0 +1,220 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Intent; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.Spinner; +import android.widget.TextView; + +import com.devspark.appmsg.AppMsg; + +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; + +public class CreateKeyFinalFragment extends Fragment { + + CreateKeyActivity mCreateKeyActivity; + + TextView mNameEdit; + TextView mEmailEdit; + CheckBox mUploadCheckbox; + View mBackButton; + View mCreateButton; + + public static final String ARG_NAME = "name"; + public static final String ARG_EMAIL = "email"; + public static final String ARG_PASSPHRASE = "passphrase"; + + String mName; + String mEmail; + String mPassphrase; + + /** + * Creates new instance of this fragment + */ + public static CreateKeyFinalFragment newInstance(String name, String email, String passphrase) { + CreateKeyFinalFragment frag = new CreateKeyFinalFragment(); + + Bundle args = new Bundle(); + args.putString(ARG_NAME, name); + args.putString(ARG_EMAIL, email); + args.putString(ARG_PASSPHRASE, passphrase); + + frag.setArguments(args); + + return frag; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.create_key_final_fragment, container, false); + + mNameEdit = (TextView) view.findViewById(R.id.name); + mEmailEdit = (TextView) view.findViewById(R.id.email); + mUploadCheckbox = (CheckBox) view.findViewById(R.id.create_key_upload); + mBackButton = view.findViewById(R.id.create_key_back_button); + mCreateButton = view.findViewById(R.id.create_key_create_button); + + // get args + mName = getArguments().getString(ARG_NAME); + mEmail = getArguments().getString(ARG_EMAIL); + mPassphrase = getArguments().getString(ARG_PASSPHRASE); + + // set values + mNameEdit.setText(mName); + mEmailEdit.setText(mEmail); + + mCreateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createKey(); + } + }); + + mBackButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + CreateKeyInputFragment frag = + CreateKeyInputFragment.newInstance(mName, mEmail); + mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_LEFT); + } + }); + + return view; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + mCreateKeyActivity = (CreateKeyActivity) getActivity(); + } + + private void createKey() { + Intent intent = new Intent(getActivity(), KeychainIntentService.class); + intent.setAction(KeychainIntentService.ACTION_SAVE_KEYRING); + + // Message is received after importing is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler( + getActivity(), + getString(R.string.progress_importing), + ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + // TODO +// if (mUploadCheckbox.isChecked()) { +// uploadKey(); +// } else { + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + getActivity().setResult(Activity.RESULT_OK); + getActivity().finish(); + } +// } + } + }; + + // fill values for this action + Bundle data = new Bundle(); + + SaveKeyringParcel parcel = new SaveKeyringParcel(); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.CERTIFY_OTHER, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.SIGN_DATA, null)); + parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(Constants.choice.algorithm.rsa, 4096, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE, null)); + String userId = mName + " <" + mEmail + ">"; + parcel.mAddUserIds.add(userId); + parcel.mNewPassphrase = mPassphrase; + + // get selected key entries + data.putParcelable(KeychainIntentService.SAVE_KEYRING_PARCEL, parcel); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + saveHandler.showProgressDialog(getActivity()); + + getActivity().startService(intent); + } + + private void uploadKey() { + // Send all information needed to service to upload key in other thread + Intent intent = new Intent(getActivity(), KeychainIntentService.class); + + intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); + + // set data uri as path to keyring + // TODO +// Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); +// intent.setData(blobUri); + + // fill values for this action + Bundle data = new Bundle(); + + Spinner keyServer = (Spinner) getActivity().findViewById(R.id.upload_key_keyserver); + String server = (String) keyServer.getSelectedItem(); + data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server); + + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + + // Message is received after uploading is done in KeychainIntentService + KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(getActivity(), + getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + AppMsg.makeText(getActivity(), R.string.key_send_success, + AppMsg.STYLE_INFO).show(); + + getActivity().setResult(Activity.RESULT_OK); + getActivity().finish(); + } + } + }; + + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + + // show progress dialog + saveHandler.showProgressDialog(getActivity()); + + // start service with intent + getActivity().startService(intent); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java new file mode 100644 index 000000000..78e8af14e --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java @@ -0,0 +1,200 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.ui; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.Bundle; +import android.os.Message; +import android.os.Messenger; +import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.TextUtils; +import android.text.TextWatcher; +import android.util.Patterns; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; +import android.widget.EditText; +import android.widget.Spinner; + +import com.devspark.appmsg.AppMsg; + +import org.spongycastle.bcpg.sig.KeyFlags; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.ContactHelper; +import org.sufficientlysecure.keychain.service.KeychainIntentService; +import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.SaveKeyringParcel; + +import java.util.regex.Matcher; + +public class CreateKeyInputFragment extends Fragment { + + CreateKeyActivity mCreateKeyActivity; + + AutoCompleteTextView mNameEdit; + AutoCompleteTextView mEmailEdit; + EditText mPassphraseEdit; + View mCreateButton; + + public static final String ARG_NAME = "name"; + public static final String ARG_EMAIL = "email"; + + /** + * Creates new instance of this fragment + */ + public static CreateKeyInputFragment newInstance(String name, String email) { + CreateKeyInputFragment frag = new CreateKeyInputFragment(); + + Bundle args = new Bundle(); + args.putString(ARG_NAME, name); + args.putString(ARG_EMAIL, email); + + frag.setArguments(args); + + return frag; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.create_key_input_fragment, container, false); + + mNameEdit = (AutoCompleteTextView) view.findViewById(R.id.name); + mEmailEdit = (AutoCompleteTextView) view.findViewById(R.id.email); + mPassphraseEdit = (EditText) view.findViewById(R.id.passphrase); + // TODO: second passphrase field + mCreateButton = view.findViewById(R.id.create_key_button); + + // initial values + String name = getArguments().getString(ARG_NAME); + String email = getArguments().getString(ARG_EMAIL); + mNameEdit.setText(name); + mEmailEdit.setText(email); + + // focus non-empty edit fields + if (name != null && email != null) { + mPassphraseEdit.requestFocus(); + } else if (name != null) { + mEmailEdit.requestFocus(); + } + + mEmailEdit.setThreshold(1); // Start working from first character + mEmailEdit.setAdapter( + new ArrayAdapter + (getActivity(), android.R.layout.simple_spinner_dropdown_item, + ContactHelper.getPossibleUserEmails(getActivity()) + ) + ); + mEmailEdit.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { + } + + @Override + public void afterTextChanged(Editable editable) { + String email = editable.toString(); + if (email.length() > 0) { + Matcher emailMatcher = Patterns.EMAIL_ADDRESS.matcher(email); + if (emailMatcher.matches()) { + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + R.drawable.uid_mail_ok, 0); + } else { + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, + R.drawable.uid_mail_bad, 0); + } + } else { + // remove drawable if email is empty + mEmailEdit.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); + } + } + }); + + mNameEdit.setThreshold(1); // Start working from first character + mNameEdit.setAdapter( + new ArrayAdapter + (getActivity(), android.R.layout.simple_spinner_dropdown_item, + ContactHelper.getPossibleUserNames(getActivity()) + ) + ); + + mCreateButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + createKeyCheck(); + } + }); + + return view; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + mCreateKeyActivity = (CreateKeyActivity) getActivity(); + } + + + private void createKeyCheck() { + if (isEditTextNotEmpty(getActivity(), mNameEdit) + && isEditTextNotEmpty(getActivity(), mEmailEdit) + && isEditTextNotEmpty(getActivity(), mPassphraseEdit)) { + + CreateKeyFinalFragment frag = + CreateKeyFinalFragment.newInstance( + mNameEdit.getText().toString(), + mEmailEdit.getText().toString(), + mPassphraseEdit.getText().toString() + ); + mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_RIGHT); + } + } + + /** + * Checks if text of given EditText is not empty. If it is empty an error is + * set and the EditText gets the focus. + * + * @param context + * @param editText + * @return true if EditText is not empty + */ + private static boolean isEditTextNotEmpty(Context context, EditText editText) { + boolean output = true; + if (editText.getText().toString().length() == 0) { + editText.setError(context.getString(R.string.create_key_empty)); + editText.requestFocus(); + output = false; + } else { + editText.setError(null); + } + + return output; + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java index f4d395ae6..44416a25a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/QrCodeActivity.java @@ -100,7 +100,7 @@ public class QrCodeActivity extends ActionBarActivity { super.onResume(); // custom activity transition to get zoom in effect - this.overridePendingTransition(R.anim.zoom_enter, android.R.anim.fade_out); + this.overridePendingTransition(R.anim.qr_code_zoom_enter, android.R.anim.fade_out); } @Override @@ -108,7 +108,7 @@ public class QrCodeActivity extends ActionBarActivity { super.onPause(); // custom activity transition to get zoom out effect - this.overridePendingTransition(0, R.anim.zoom_exit); + this.overridePendingTransition(0, R.anim.qr_code_zoom_exit); } } \ No newline at end of file -- cgit v1.2.3 From 9f9aa79066c48f2d4678c2d13893ea94b194032d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 01:07:10 +0200 Subject: Fix conflicts --- .../java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index b49731739..569ae82f0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -68,7 +68,8 @@ public class CreateKeyActivity extends ActionBarActivity { transaction.setCustomAnimations(R.anim.frag_slide_in_from_left, R.anim.frag_slide_out_to_right); break; case ANIM_TO_RIGHT: - transaction.setCustomAnimations(R.anim.frag_slide_out_to_left, R.anim.frag_slide_in_from_right); + transaction.setCustomAnimations(R.anim.frag_slide_out_to_left, R.anim.frag_slide_in_from_right, + R.anim.frag_slide_in_from_left, R.anim.frag_slide_out_to_right); transaction.addToBackStack("back"); break; -- cgit v1.2.3 From 70bfb5c586b3e2923da75d4fa517a785133b537a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 14:57:59 +0200 Subject: return values for create key --- .../keychain/service/KeychainIntentService.java | 6 ++---- .../keychain/ui/CreateKeyFinalFragment.java | 22 +++++++++++++++++++--- .../keychain/ui/ImportKeysActivity.java | 2 +- 3 files changed, 22 insertions(+), 8 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 0e38a1c47..a764c323c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -24,7 +24,6 @@ import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; -import android.os.Parcelable; import android.os.RemoteException; import org.sufficientlysecure.keychain.Constants; @@ -53,7 +52,6 @@ import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.ProviderHelper; -import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult; import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.util.InputData; @@ -172,7 +170,7 @@ public class KeychainIntentService extends IntentService // export public static final String RESULT_EXPORT = "exported"; - public static final String RESULT = "result"; + public static final String RESULT_IMPORT = "result"; Messenger mMessenger; @@ -394,7 +392,7 @@ public class KeychainIntentService extends IntentService ImportKeyResult result = pgpImportExport.importKeyRings(entries); Bundle resultData = new Bundle(); - resultData.putParcelable(RESULT, result); + resultData.putParcelable(RESULT_IMPORT, result); sendMessageToHandler(KeychainIntentServiceHandler.MESSAGE_OKAY, resultData); } catch (Exception e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index 7e6f81cc2..c60200bce 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -36,6 +36,8 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; +import org.sufficientlysecure.keychain.service.OperationResultParcel; +import org.sufficientlysecure.keychain.service.OperationResults; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.util.Notify; @@ -131,15 +133,29 @@ public class CreateKeyFinalFragment extends Fragment { // handle messages by standard KeychainIntentServiceHandler first super.handleMessage(message); - // TODO + if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + // get returned data bundle + Bundle returnData = message.getData(); + if (returnData == null) { + return; + } + final OperationResults.EditKeyResult result = + returnData.getParcelable(OperationResultParcel.EXTRA_RESULT); + if (result == null) { + return; + } + + result.createNotify(getActivity()); + + // TODO // if (mUploadCheckbox.isChecked()) { // uploadKey(); // } else { - if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { getActivity().setResult(Activity.RESULT_OK); getActivity().finish(); + // } + } -// } } }; diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 5ab4d69f1..cb59afa93 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -428,7 +428,7 @@ public class ImportKeysActivity extends ActionBarActivity { return; } final ImportKeyResult result = - returnData.getParcelable(KeychainIntentService.RESULT); + returnData.getParcelable(KeychainIntentService.RESULT_IMPORT); if (result == null) { return; } -- cgit v1.2.3 From 052cdfa392f4d6377903b5a982e6acb74bc6433a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 15:19:33 +0200 Subject: Create key: upload --- .../keychain/ui/CreateKeyFinalFragment.java | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index c60200bce..7bccb8995 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -20,6 +20,7 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.os.Messenger; @@ -34,6 +35,7 @@ import android.widget.TextView; import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.OperationResultParcel; @@ -145,16 +147,16 @@ public class CreateKeyFinalFragment extends Fragment { return; } - result.createNotify(getActivity()); - - // TODO -// if (mUploadCheckbox.isChecked()) { -// uploadKey(); -// } else { - getActivity().setResult(Activity.RESULT_OK); - getActivity().finish(); - // } + if (mUploadCheckbox.isChecked()) { + // result will be displayed after upload + uploadKey(result); + } else { + // TODO: return result + result.createNotify(getActivity()); + getActivity().setResult(Activity.RESULT_OK); + getActivity().finish(); + } } } }; @@ -184,16 +186,17 @@ public class CreateKeyFinalFragment extends Fragment { getActivity().startService(intent); } - private void uploadKey() { + private void uploadKey(final OperationResults.EditKeyResult editKeyResult) { // Send all information needed to service to upload key in other thread Intent intent = new Intent(getActivity(), KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); // set data uri as path to keyring - // TODO -// Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); -// intent.setData(blobUri); + Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri( + Long.toString(editKeyResult.getRing().getMasterKeyId()) + ); + intent.setData(blobUri); // fill values for this action Bundle data = new Bundle(); @@ -212,6 +215,10 @@ public class CreateKeyFinalFragment extends Fragment { super.handleMessage(message); if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) { + // TODO: return results + + editKeyResult.createNotify(getActivity()); + Notify.showNotify(getActivity(), R.string.key_send_success, Notify.Style.INFO); -- cgit v1.2.3 From fcc535a57381009b173bbe455e67e6825be6b684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 15:29:01 +0200 Subject: Create key: repeat passphrase --- .../keychain/ui/CreateKeyInputFragment.java | 34 ++++++++++++++++++++-- .../ui/dialog/PassphraseDialogFragment.java | 5 ---- 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java index d82bf28a7..30a48e63e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java @@ -26,6 +26,7 @@ import android.util.Patterns; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.view.inputmethod.InputMethodManager; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.EditText; @@ -42,6 +43,7 @@ public class CreateKeyInputFragment extends Fragment { AutoCompleteTextView mNameEdit; AutoCompleteTextView mEmailEdit; EditText mPassphraseEdit; + EditText mPassphraseEditAgain; View mCreateButton; public static final String ARG_NAME = "name"; @@ -69,7 +71,7 @@ public class CreateKeyInputFragment extends Fragment { mNameEdit = (AutoCompleteTextView) view.findViewById(R.id.name); mEmailEdit = (AutoCompleteTextView) view.findViewById(R.id.email); mPassphraseEdit = (EditText) view.findViewById(R.id.passphrase); - // TODO: second passphrase field + mPassphraseEditAgain = (EditText) view.findViewById(R.id.passphrase_again); mCreateButton = view.findViewById(R.id.create_key_button); // initial values @@ -149,7 +151,8 @@ public class CreateKeyInputFragment extends Fragment { private void createKeyCheck() { if (isEditTextNotEmpty(getActivity(), mNameEdit) && isEditTextNotEmpty(getActivity(), mEmailEdit) - && isEditTextNotEmpty(getActivity(), mPassphraseEdit)) { + && isEditTextNotEmpty(getActivity(), mPassphraseEdit) + && areEditTextsEqual(getActivity(), mPassphraseEdit, mPassphraseEditAgain)) { CreateKeyFinalFragment frag = CreateKeyFinalFragment.newInstance( @@ -157,10 +160,24 @@ public class CreateKeyInputFragment extends Fragment { mEmailEdit.getText().toString(), mPassphraseEdit.getText().toString() ); + + hideKeyboard(); mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_RIGHT); } } + private void hideKeyboard() { + InputMethodManager inputManager = (InputMethodManager) getActivity() + .getSystemService(Context.INPUT_METHOD_SERVICE); + + //check if no view has focus: + View v = getActivity().getCurrentFocus(); + if (v == null) + return; + + inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); + } + /** * Checks if text of given EditText is not empty. If it is empty an error is * set and the EditText gets the focus. @@ -182,4 +199,17 @@ public class CreateKeyInputFragment extends Fragment { return output; } + private static boolean areEditTextsEqual(Context context, EditText editText1, EditText editText2) { + boolean output = true; + if (!editText1.getText().toString().equals(editText2.getText().toString())) { + editText2.setError(context.getString(R.string.create_key_passphrases_not_equal)); + editText2.requestFocus(); + output = false; + } else { + editText2.setError(null); + } + + return output; + } + } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 7981b717e..0a7fd6aee 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -121,11 +121,6 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor return frag; } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - } - /** * Creates dialog */ -- cgit v1.2.3 From 64aac8023ef9c9b08d17346e41d06a667480a118 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Wed, 30 Jul 2014 16:22:55 +0200 Subject: couple of fixes in save*KeyRing methods --- .../keychain/provider/ProviderHelper.java | 36 ++++++++-------------- .../keychain/service/OperationResultParcel.java | 1 + 2 files changed, 14 insertions(+), 23 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 2d524f5b0..998cf25c3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -567,20 +567,15 @@ public class ProviderHelper { } if (!keyRing.isCanonicalized()) { - log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_PUBLIC); + log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_UNCANON); return SaveKeyringResult.RESULT_ERROR; } long masterKeyId = keyRing.getMasterKeyId(); log(LogLevel.START, LogType.MSG_IS, PgpKeyHelper.convertKeyIdToHex(masterKeyId)); mIndent += 1; - try { - // Canonicalize this key, to assert a number of assumptions made about it. - keyRing = keyRing.canonicalize(mLog, mIndent); - if (keyRing == null) { - return SaveKeyringResult.RESULT_ERROR; - } + try { // IF this is successful, it's a secret key int result = SaveKeyringResult.SAVED_SECRET; @@ -798,31 +793,26 @@ public class ProviderHelper { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } - // If nothing changed, never mind - if (Arrays.hashCode(publicRing.getEncoded()) - == Arrays.hashCode(oldPublicRing.getEncoded())) { - publicRing = null; - } - } catch (NotFoundException e) { log(LogLevel.DEBUG, LogType.MSG_IS_PUBRING_GENERATE); publicRing = secretRing.extractPublicKeyRing(); } - if (publicRing != null) { - publicRing = publicRing.canonicalize(mLog, mIndent); - if (publicRing == null) { - return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); - } + publicRing = publicRing.canonicalize(mLog, mIndent); + if (publicRing == null) { + return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); + } - int result = internalSavePublicKeyRing(publicRing, progress, true); - if ((result & SaveKeyringResult.RESULT_ERROR) == SaveKeyringResult.RESULT_ERROR) { - return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); - } + int result; + + result = internalSavePublicKeyRing(publicRing, progress, true); + if ((result & SaveKeyringResult.RESULT_ERROR) == SaveKeyringResult.RESULT_ERROR) { + return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } progress.setProgress(LogType.MSG_IP_REINSERT_SECRET.getMsgId(), 90, 100); - int result = internalSaveSecretKeyRing(secretRing); + result = internalSaveSecretKeyRing(secretRing); + return new SaveKeyringResult(result, mLog); } catch (IOException e) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 99cafd3f9..bfe06dca7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -260,6 +260,7 @@ public class OperationResultParcel implements Parcelable { // import secret MSG_IS(R.string.msg_is), MSG_IS_BAD_TYPE_PUBLIC (R.string.msg_is_bad_type_public), + MSG_IS_BAD_TYPE_UNCANON (R.string.msg_is_bad_type_uncanon), MSG_IS_DB_EXCEPTION (R.string.msg_is_db_exception), MSG_IS_FAIL_IO_EXC (R.string.msg_is_io_exc), MSG_IS_IMPORTING_SUBKEYS (R.string.msg_is_importing_subkeys), -- cgit v1.2.3 From 65488cbf05b0c90ac84d9b1895c68455217e3f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 30 Jul 2014 17:47:17 +0200 Subject: Create key: work on upload --- .../keychain/service/OperationResults.java | 9 +++++++++ .../sufficientlysecure/keychain/ui/CertifyKeyActivity.java | 4 +++- .../keychain/ui/CreateKeyFinalFragment.java | 14 +++++++++----- .../keychain/ui/CreateKeyInputFragment.java | 1 - .../sufficientlysecure/keychain/ui/ImportKeysActivity.java | 5 ++--- 5 files changed, 23 insertions(+), 10 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index 6fb0db0e5..a05b77ea5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -168,11 +168,13 @@ public abstract class OperationResults { public static class EditKeyResult extends OperationResultParcel { private transient UncachedKeyRing mRing; + public final long mRingMasterKeyId; public EditKeyResult(int result, OperationLog log, UncachedKeyRing ring) { super(result, log); mRing = ring; + mRingMasterKeyId = ring.getMasterKeyId(); } public UncachedKeyRing getRing() { @@ -181,6 +183,13 @@ public abstract class OperationResults { public EditKeyResult(Parcel source) { super(source); + mRingMasterKeyId = source.readLong(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeLong(mRingMasterKeyId); } public static Creator CREATOR = new Creator() { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java index 5e20b24b5..3c69fb071 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CertifyKeyActivity.java @@ -136,6 +136,8 @@ public class CertifyKeyActivity extends ActionBarActivity implements if (mPubKeyId != 0) { if (mMasterKeyId == 0) { mSelectKeyFragment.setError(getString(R.string.select_key_to_certify)); + Notify.showNotify(CertifyKeyActivity.this, getString(R.string.select_key_to_certify), + Notify.Style.ERROR); } else { initiateSigning(); } @@ -256,7 +258,7 @@ public class CertifyKeyActivity extends ActionBarActivity implements // Bail out if there is not at least one user id selected ArrayList userIds = mUserIdsAdapter.getSelectedUserIds(); if (userIds.isEmpty()) { - Notify.showNotify(CertifyKeyActivity.this, "No Notify.Style IDs to sign selected!", + Notify.showNotify(CertifyKeyActivity.this, "No identities selected!", Notify.Style.ERROR); return; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index 7bccb8995..c083539da 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -20,8 +20,11 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; +import android.database.ContentObserver; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.support.v4.app.Fragment; @@ -35,6 +38,7 @@ import android.widget.TextView; import org.spongycastle.bcpg.sig.KeyFlags; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; @@ -188,22 +192,22 @@ public class CreateKeyFinalFragment extends Fragment { private void uploadKey(final OperationResults.EditKeyResult editKeyResult) { // Send all information needed to service to upload key in other thread - Intent intent = new Intent(getActivity(), KeychainIntentService.class); + final Intent intent = new Intent(getActivity(), KeychainIntentService.class); intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING); // set data uri as path to keyring Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri( - Long.toString(editKeyResult.getRing().getMasterKeyId()) + Long.toString(editKeyResult.mRingMasterKeyId) ); intent.setData(blobUri); // fill values for this action Bundle data = new Bundle(); - Spinner keyServer = (Spinner) getActivity().findViewById(R.id.upload_key_keyserver); - String server = (String) keyServer.getSelectedItem(); - data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server); + // upload to favorite keyserver + String keyserver = Preferences.getPreferences(getActivity()).getKeyServers()[0]; + data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, keyserver); intent.putExtra(KeychainIntentService.EXTRA_DATA, data); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java index 30a48e63e..a1fdc09cc 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java @@ -147,7 +147,6 @@ public class CreateKeyInputFragment extends Fragment { mCreateKeyActivity = (CreateKeyActivity) getActivity(); } - private void createKeyCheck() { if (isEditTextNotEmpty(getActivity(), mNameEdit) && isEditTextNotEmpty(getActivity(), mEmailEdit) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index cb59afa93..5f340019f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -354,9 +354,8 @@ public class ImportKeysActivity extends ActionBarActivity { ImportKeysServerFragment f = (ImportKeysServerFragment) getActiveFragment(mViewPager, TAB_KEYSERVER); - // TODO: Currently it simply uses keyserver nr 0 - String keyserver = Preferences.getPreferences(ImportKeysActivity.this) - .getKeyServers()[0]; + // ask favorite keyserver + String keyserver = Preferences.getPreferences(ImportKeysActivity.this).getKeyServers()[0]; // set fields of ImportKeysServerFragment f.setQueryAndKeyserver(query, keyserver); -- cgit v1.2.3 From 1d2c93ca8ae4c75e92b5da74e262b9476f22f172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 16:15:36 +0200 Subject: More file association hacks for AndroidManifest and key import --- .../keychain/ui/ImportKeysListFragment.java | 9 +++---- .../keychain/ui/adapter/ImportKeysListLoader.java | 29 +++++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 9a39b6cc3..469601a54 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -35,7 +35,6 @@ import org.sufficientlysecure.keychain.helper.Preferences; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; -import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.ui.adapter.AsyncTaskResultWrapper; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysAdapter; import org.sufficientlysecure.keychain.ui.adapter.ImportKeysListKeybaseLoader; @@ -288,13 +287,13 @@ public class ImportKeysListFragment extends ListFragment implements if (error == null) { // No error mCachedKeyData = ((ImportKeysListLoader) loader).getParcelableRings(); - } else if (error instanceof ImportKeysListLoader.FileHasNoContent) { + } else if (error instanceof ImportKeysListLoader.FileHasNoContentException) { Notify.showNotify(getActivity(), R.string.error_import_file_no_content, Notify.Style.ERROR); - } else if (error instanceof ImportKeysListLoader.NonPgpPart) { + } else if (error instanceof ImportKeysListLoader.NonPgpPartException) { Notify.showNotify(getActivity(), - ((ImportKeysListLoader.NonPgpPart) error).getCount() + " " + getResources(). + ((ImportKeysListLoader.NonPgpPartException) error).getCount() + " " + getResources(). getQuantityString(R.plurals.error_import_non_pgp_part, - ((ImportKeysListLoader.NonPgpPart) error).getCount()), + ((ImportKeysListLoader.NonPgpPartException) error).getCount()), Notify.Style.OK ); } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 99f959035..4a8eb0cb1 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -30,20 +30,20 @@ import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.PositionAwareInputStream; import java.io.BufferedInputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.List; public class ImportKeysListLoader extends AsyncTaskLoader>> { - public static class FileHasNoContent extends Exception { - + public static class FileHasNoContentException extends Exception { } - public static class NonPgpPart extends Exception { + public static class NonPgpPartException extends Exception { private int mCount; - public NonPgpPart(int count) { + public NonPgpPartException(int count) { this.mCount = count; } @@ -67,7 +67,6 @@ public class ImportKeysListLoader @Override public AsyncTaskResultWrapper> loadInBackground() { - // This has already been loaded! nvm any further, just return if (mEntryListWrapper != null) { return mEntryListWrapper; @@ -119,7 +118,6 @@ public class ImportKeysListLoader * @return */ private void generateListOfKeyrings(InputData inputData) { - boolean isEmpty = true; PositionAwareInputStream progressIn = new PositionAwareInputStream( @@ -129,13 +127,14 @@ public class ImportKeysListLoader // PGPObject chunks after the first one, e.g. files with several consecutive ASCII // armor blocks BufferedInputStream bufferedInput = new BufferedInputStream(progressIn); + bufferedInput.mark(1024); try { // read all available blocks... (asc files can contain many blocks with BEGIN END) while (bufferedInput.available() > 0) { // TODO: deal with non-keyring objects? List rings = UncachedKeyRing.fromStream(bufferedInput); - for(UncachedKeyRing key : rings) { + for (UncachedKeyRing key : rings) { ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); mData.add(item); mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); @@ -144,14 +143,26 @@ public class ImportKeysListLoader } } catch (Exception e) { Log.e(Constants.TAG, "Exception on parsing key file!", e); + + try { + bufferedInput.reset(); + } catch (IOException e1) { + } + Log.d(Constants.TAG, "Last 1024 byte input data: " + convertStreamToString(bufferedInput)); mEntryListWrapper = new AsyncTaskResultWrapper>(mData, e); } if (isEmpty) { - Log.e(Constants.TAG, "File has no content!", new FileHasNoContent()); + FileHasNoContentException e = new FileHasNoContentException(); + Log.e(Constants.TAG, "File has no content!", e); mEntryListWrapper = new AsyncTaskResultWrapper> - (mData, new FileHasNoContent()); + (mData, e); } } + static String convertStreamToString(java.io.InputStream is) { + java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); + return s.hasNext() ? s.next() : ""; + } + } -- cgit v1.2.3 From 9475285013accafd24d5bc14da9ba01ca218cbe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 17:11:06 +0200 Subject: Better exception handling for import of keys --- .../keychain/pgp/UncachedKeyRing.java | 10 ++--- .../keychain/ui/ImportKeysListFragment.java | 4 +- .../keychain/ui/adapter/ImportKeysListLoader.java | 48 +++++++--------------- 3 files changed, 21 insertions(+), 41 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 de8c683ff..0e59b7fdb 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -134,9 +134,7 @@ public class UncachedKeyRing { } - public static List fromStream(InputStream stream) - throws PgpGeneralException, IOException { - + public static List fromStream(InputStream stream) throws IOException { List result = new Vector(); while(stream.available() > 0) { @@ -147,8 +145,10 @@ public class UncachedKeyRing { while ((obj = objectFactory.nextObject()) != null) { Log.d(Constants.TAG, "Found class: " + obj.getClass()); if (!(obj instanceof PGPKeyRing)) { - throw new PgpGeneralException( - "Bad object of type " + obj.getClass().getName() + " in stream!"); + Log.d(Constants.TAG, + "Bad object of type " + obj.getClass().getName() + " in stream, proceed with next object..."); + // skip object + continue; } result.add(new UncachedKeyRing((PGPKeyRing) obj)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 469601a54..1617a84e4 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -287,8 +287,8 @@ public class ImportKeysListFragment extends ListFragment implements if (error == null) { // No error mCachedKeyData = ((ImportKeysListLoader) loader).getParcelableRings(); - } else if (error instanceof ImportKeysListLoader.FileHasNoContentException) { - Notify.showNotify(getActivity(), R.string.error_import_file_no_content, Notify.Style.ERROR); + } else if (error instanceof ImportKeysListLoader.NoValidKeysException) { + Notify.showNotify(getActivity(), R.string.error_import_no_valid_keys, Notify.Style.ERROR); } else if (error instanceof ImportKeysListLoader.NonPgpPartException) { Notify.showNotify(getActivity(), ((ImportKeysListLoader.NonPgpPartException) error).getCount() + " " + getResources(). diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 4a8eb0cb1..9996e0381 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -37,7 +37,7 @@ import java.util.List; public class ImportKeysListLoader extends AsyncTaskLoader>> { - public static class FileHasNoContentException extends Exception { + public static class NoValidKeysException extends Exception { } public static class NonPgpPartException extends Exception { @@ -118,8 +118,6 @@ public class ImportKeysListLoader * @return */ private void generateListOfKeyrings(InputData inputData) { - boolean isEmpty = true; - PositionAwareInputStream progressIn = new PositionAwareInputStream( inputData.getInputStream()); @@ -127,42 +125,24 @@ public class ImportKeysListLoader // PGPObject chunks after the first one, e.g. files with several consecutive ASCII // armor blocks BufferedInputStream bufferedInput = new BufferedInputStream(progressIn); - bufferedInput.mark(1024); try { - - // read all available blocks... (asc files can contain many blocks with BEGIN END) - while (bufferedInput.available() > 0) { - // TODO: deal with non-keyring objects? - List rings = UncachedKeyRing.fromStream(bufferedInput); - for (UncachedKeyRing key : rings) { - ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); - mData.add(item); - mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); - isEmpty = false; - } + // parse all keyrings + List rings = UncachedKeyRing.fromStream(bufferedInput); + for (UncachedKeyRing key : rings) { + ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); + mData.add(item); + mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); } - } catch (Exception e) { - Log.e(Constants.TAG, "Exception on parsing key file!", e); + } catch (IOException e) { + Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e); - try { - bufferedInput.reset(); - } catch (IOException e1) { - } - Log.d(Constants.TAG, "Last 1024 byte input data: " + convertStreamToString(bufferedInput)); - mEntryListWrapper = new AsyncTaskResultWrapper>(mData, e); - } - - if (isEmpty) { - FileHasNoContentException e = new FileHasNoContentException(); - Log.e(Constants.TAG, "File has no content!", e); + NoValidKeysException e1 = new NoValidKeysException(); mEntryListWrapper = new AsyncTaskResultWrapper> - (mData, e); + (mData, e1); + } catch (Exception e) { + Log.e(Constants.TAG, "Other Exception on parsing key file!", e); + mEntryListWrapper = new AsyncTaskResultWrapper>(mData, e); } } - static String convertStreamToString(java.io.InputStream is) { - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } - } -- cgit v1.2.3 From b156a057e8c5b715f515725ab051087a86ecd547 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 17:08:33 +0200 Subject: rename Wrapped*Key* to Canonicalized*Key* --- .../keychain/pgp/CanonicalizedKeyRing.java | 114 ++++++++++++ .../keychain/pgp/CanonicalizedPublicKey.java | 39 +++++ .../keychain/pgp/CanonicalizedPublicKeyRing.java | 84 +++++++++ .../keychain/pgp/CanonicalizedSecretKey.java | 192 +++++++++++++++++++++ .../keychain/pgp/CanonicalizedSecretKeyRing.java | 152 ++++++++++++++++ .../sufficientlysecure/keychain/pgp/KeyRing.java | 2 +- .../keychain/pgp/PgpDecryptVerify.java | 18 +- .../keychain/pgp/PgpImportExport.java | 8 +- .../keychain/pgp/PgpKeyOperation.java | 3 +- .../keychain/pgp/PgpSignEncrypt.java | 10 +- .../keychain/pgp/UncachedKeyRing.java | 38 +--- .../keychain/pgp/WrappedKeyRing.java | 120 ------------- .../keychain/pgp/WrappedPublicKey.java | 39 ----- .../keychain/pgp/WrappedPublicKeyRing.java | 81 --------- .../keychain/pgp/WrappedSecretKey.java | 192 --------------------- .../keychain/pgp/WrappedSecretKeyRing.java | 131 -------------- .../keychain/pgp/WrappedSignature.java | 4 +- .../keychain/provider/ProviderHelper.java | 131 +++++++------- .../keychain/remote/OpenPgpService.java | 2 +- .../keychain/service/KeychainIntentService.java | 18 +- .../keychain/service/OperationResultParcel.java | 1 - .../keychain/service/PassphraseCacheService.java | 4 +- .../keychain/ui/EditKeyFragment.java | 7 +- .../keychain/ui/LogDisplayFragment.java | 6 +- .../keychain/ui/ViewCertActivity.java | 10 +- .../ui/dialog/PassphraseDialogFragment.java | 14 +- 26 files changed, 702 insertions(+), 718 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKeyRing.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKey.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java delete mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java new file mode 100644 index 000000000..ee0dfefa4 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedKeyRing.java @@ -0,0 +1,114 @@ +package org.sufficientlysecure.keychain.pgp; + +import org.spongycastle.openpgp.PGPKeyRing; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.util.IterableIterator; + +import java.io.IOException; +import java.io.OutputStream; + +/** A generic wrapped PGPKeyRing object. + * + * This class provides implementations for all basic getters which both + * PublicKeyRing and SecretKeyRing have in common. To make the wrapped keyring + * class typesafe in implementing subclasses, the field is stored in the + * implementing class, providing properly typed access through the getRing + * getter method. + * + */ +public abstract class CanonicalizedKeyRing extends KeyRing { + + private final int mVerified; + + CanonicalizedKeyRing(int verified) { + mVerified = verified; + } + + public long getMasterKeyId() { + return getRing().getPublicKey().getKeyID(); + } + + public int getVerified() { + return mVerified; + } + + public String getPrimaryUserId() throws PgpGeneralException { + return getPublicKey().getPrimaryUserId(); + } + + public String getPrimaryUserIdWithFallback() throws PgpGeneralException { + return getPublicKey().getPrimaryUserIdWithFallback(); + } + + public boolean isRevoked() throws PgpGeneralException { + // Is the master key revoked? + return getRing().getPublicKey().isRevoked(); + } + + public boolean canCertify() throws PgpGeneralException { + return getRing().getPublicKey().isEncryptionKey(); + } + + public long getEncryptId() throws PgpGeneralException { + for(CanonicalizedPublicKey key : publicKeyIterator()) { + if(key.canEncrypt()) { + return key.getKeyId(); + } + } + throw new PgpGeneralException("No valid encryption key found!"); + } + + public boolean hasEncrypt() throws PgpGeneralException { + try { + getEncryptId(); + return true; + } catch(PgpGeneralException e) { + return false; + } + } + + 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); + } + + /** Returns an UncachedKeyRing which wraps the same data as this ring. This method should + * only be used */ + public UncachedKeyRing getUncachedKeyRing() { + return new UncachedKeyRing(getRing()); + } + + abstract PGPKeyRing getRing(); + + abstract public IterableIterator publicKeyIterator(); + + public CanonicalizedPublicKey getPublicKey() { + return new CanonicalizedPublicKey(this, getRing().getPublicKey()); + } + + public CanonicalizedPublicKey getPublicKey(long id) { + return new CanonicalizedPublicKey(this, getRing().getPublicKey(id)); + } + + public byte[] getEncoded() throws IOException { + return getRing().getEncoded(); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java new file mode 100644 index 000000000..981caad49 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKey.java @@ -0,0 +1,39 @@ +package org.sufficientlysecure.keychain.pgp; + +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator; +import org.sufficientlysecure.keychain.util.IterableIterator; + +/** Wrapper for a PGPPublicKey. + * + * The methods implemented in this class are a thin layer over + * UncachedPublicKey. The difference between the two classes is that objects of + * this class can only be obtained from a WrappedKeyRing, and that it stores a + * back reference to its parent as well. A method which works with + * WrappedPublicKey is therefore guaranteed to work on a KeyRing which is + * stored in the database. + * + */ +public class CanonicalizedPublicKey extends UncachedPublicKey { + + // this is the parent key ring + final KeyRing mRing; + + CanonicalizedPublicKey(KeyRing ring, PGPPublicKey key) { + super(key); + mRing = ring; + } + + public IterableIterator getUserIds() { + return new IterableIterator(mPublicKey.getUserIDs()); + } + + public KeyRing getKeyRing() { + return mRing; + } + + JcePublicKeyKeyEncryptionMethodGenerator getPubKeyEncryptionGenerator() { + return new JcePublicKeyKeyEncryptionMethodGenerator(mPublicKey); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java new file mode 100644 index 000000000..70288dceb --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedPublicKeyRing.java @@ -0,0 +1,84 @@ +package org.sufficientlysecure.keychain.pgp; + +import org.spongycastle.bcpg.ArmoredOutputStream; +import org.spongycastle.openpgp.PGPObjectFactory; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.util.IterableIterator; + +import java.io.IOException; +import java.util.Iterator; + +public class CanonicalizedPublicKeyRing extends CanonicalizedKeyRing { + + private PGPPublicKeyRing mRing; + + CanonicalizedPublicKeyRing(PGPPublicKeyRing ring, int verified) { + super(verified); + mRing = ring; + } + + public CanonicalizedPublicKeyRing(byte[] blob, int verified) { + super(verified); + if(mRing == null) { + // get first object in block + PGPObjectFactory factory = new PGPObjectFactory(blob); + try { + Object obj = factory.nextObject(); + if (! (obj instanceof PGPPublicKeyRing)) { + throw new RuntimeException("Error constructing CanonicalizedPublicKeyRing, should never happen!"); + } + mRing = (PGPPublicKeyRing) obj; + if (factory.nextObject() != null) { + throw new RuntimeException("Encountered trailing data after keyring, should never happen!"); + } + } catch (IOException e) { + throw new RuntimeException("IO Error constructing CanonicalizedPublicKeyRing, should never happen!"); + } + } + } + + PGPPublicKeyRing getRing() { + return mRing; + } + + public void encode(ArmoredOutputStream stream) throws IOException { + getRing().encode(stream); + } + + /** Getter that returns the subkey that should be used for signing. */ + CanonicalizedPublicKey getEncryptionSubKey() throws PgpGeneralException { + PGPPublicKey key = getRing().getPublicKey(getEncryptId()); + if(key != null) { + CanonicalizedPublicKey cKey = new CanonicalizedPublicKey(this, key); + if(!cKey.canEncrypt()) { + throw new PgpGeneralException("key error"); + } + return cKey; + } + throw new PgpGeneralException("no encryption key available"); + } + + public IterableIterator publicKeyIterator() { + @SuppressWarnings("unchecked") + final Iterator it = getRing().getPublicKeys(); + return new IterableIterator(new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public CanonicalizedPublicKey next() { + return new CanonicalizedPublicKey(CanonicalizedPublicKeyRing.this, it.next()); + } + + @Override + public void remove() { + it.remove(); + } + }); + } + +} \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java new file mode 100644 index 000000000..2eb517697 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKey.java @@ -0,0 +1,192 @@ +package org.sufficientlysecure.keychain.pgp; + +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPPrivateKey; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSecretKey; +import org.spongycastle.openpgp.PGPSignature; +import org.spongycastle.openpgp.PGPSignatureGenerator; +import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator; +import org.spongycastle.openpgp.PGPSignatureSubpacketVector; +import org.spongycastle.openpgp.PGPUtil; +import org.spongycastle.openpgp.PGPV3SignatureGenerator; +import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor; +import org.spongycastle.openpgp.operator.PublicKeyDataDecryptorFactory; +import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; +import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; +import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder; +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; +import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; +import org.sufficientlysecure.keychain.util.IterableIterator; + +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SignatureException; +import java.util.List; + +/** Wrapper for a PGPSecretKey. + * + * This object can only be obtained from a WrappedSecretKeyRing, and stores a + * back reference to its parent. + * + * This class represents known secret keys which are stored in the database. + * All "crypto operations using a known secret key" should be implemented in + * this class, to ensure on type level that these operations are performed on + * properly imported secret keys only. + * + */ +public class CanonicalizedSecretKey extends CanonicalizedPublicKey { + + private final PGPSecretKey mSecretKey; + private PGPPrivateKey mPrivateKey = null; + + CanonicalizedSecretKey(CanonicalizedSecretKeyRing ring, PGPSecretKey key) { + super(ring, key.getPublicKey()); + mSecretKey = key; + } + + public CanonicalizedSecretKeyRing getRing() { + return (CanonicalizedSecretKeyRing) mRing; + } + + public boolean unlock(String passphrase) throws PgpGeneralException { + try { + PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( + Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray()); + mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor); + } catch (PGPException e) { + return false; + } + if(mPrivateKey == null) { + throw new PgpGeneralException("error extracting key"); + } + return true; + } + + public PGPSignatureGenerator getSignatureGenerator(int hashAlgo, boolean cleartext) + throws PgpGeneralException { + if(mPrivateKey == null) { + throw new PrivateKeyNotUnlockedException(); + } + + // content signer based on signing key algorithm and chosen hash algorithm + JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( + mSecretKey.getPublicKey().getAlgorithm(), hashAlgo) + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); + + int signatureType; + if (cleartext) { + // for sign-only ascii text + signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT; + } else { + signatureType = PGPSignature.BINARY_DOCUMENT; + } + + try { + PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); + signatureGenerator.init(signatureType, mPrivateKey); + + PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); + spGen.setSignerUserID(false, mRing.getPrimaryUserIdWithFallback()); + signatureGenerator.setHashedSubpackets(spGen.generate()); + return signatureGenerator; + } catch(PGPException e) { + throw new PgpGeneralException("Error initializing signature!", e); + } + } + + public PGPV3SignatureGenerator getV3SignatureGenerator(int hashAlgo, boolean cleartext) + throws PgpGeneralException { + if(mPrivateKey == null) { + throw new PrivateKeyNotUnlockedException(); + } + + // content signer based on signing key algorithm and chosen hash algorithm + JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( + mSecretKey.getPublicKey().getAlgorithm(), hashAlgo) + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); + + int signatureType; + if (cleartext) { + // for sign-only ascii text + signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT; + } else { + signatureType = PGPSignature.BINARY_DOCUMENT; + } + + try { + PGPV3SignatureGenerator signatureV3Generator = new PGPV3SignatureGenerator(contentSignerBuilder); + signatureV3Generator.init(signatureType, mPrivateKey); + return signatureV3Generator; + } catch(PGPException e) { + throw new PgpGeneralException("Error initializing signature!", e); + } + } + + public PublicKeyDataDecryptorFactory getDecryptorFactory() { + if(mPrivateKey == null) { + throw new PrivateKeyNotUnlockedException(); + } + return new JcePublicKeyDataDecryptorFactoryBuilder() + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(mPrivateKey); + } + + /** + * Certify the given pubkeyid with the given masterkeyid. + * + * @param publicKeyRing Keyring to add certification to. + * @param userIds User IDs to certify, must not be null or empty + * @return A keyring with added certifications + */ + public UncachedKeyRing certifyUserIds(CanonicalizedPublicKeyRing publicKeyRing, List userIds) + throws PgpGeneralMsgIdException, NoSuchAlgorithmException, NoSuchProviderException, + PGPException, SignatureException { + + if(mPrivateKey == null) { + throw new PrivateKeyNotUnlockedException(); + } + + // create a signatureGenerator from the supplied masterKeyId and passphrase + PGPSignatureGenerator signatureGenerator; + { + // TODO: SHA256 fixed? + JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( + mSecretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256) + .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); + + signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); + signatureGenerator.init(PGPSignature.DEFAULT_CERTIFICATION, mPrivateKey); + } + + { // supply signatureGenerator with a SubpacketVector + PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); + PGPSignatureSubpacketVector packetVector = spGen.generate(); + signatureGenerator.setHashedSubpackets(packetVector); + } + + // get the master subkey (which we certify for) + PGPPublicKey publicKey = publicKeyRing.getPublicKey().getPublicKey(); + + // fetch public key ring, add the certification and return it + for (String userId : new IterableIterator(userIds.iterator())) { + PGPSignature sig = signatureGenerator.generateCertification(userId, publicKey); + publicKey = PGPPublicKey.addCertification(publicKey, userId, sig); + } + + PGPPublicKeyRing ring = PGPPublicKeyRing.insertPublicKey(publicKeyRing.getRing(), publicKey); + + return new UncachedKeyRing(ring); + } + + static class PrivateKeyNotUnlockedException extends RuntimeException { + // this exception is a programming error which happens when an operation which requires + // the private key is called without a previous call to unlock() + } + + public UncachedSecretKey getUncached() { + return new UncachedSecretKey(mSecretKey); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKeyRing.java new file mode 100644 index 000000000..e48fe5020 --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/CanonicalizedSecretKeyRing.java @@ -0,0 +1,152 @@ +package org.sufficientlysecure.keychain.pgp; + +import org.spongycastle.bcpg.S2K; +import org.spongycastle.openpgp.PGPException; +import org.spongycastle.openpgp.PGPKeyRing; +import org.spongycastle.openpgp.PGPObjectFactory; +import org.spongycastle.openpgp.PGPPrivateKey; +import org.spongycastle.openpgp.PGPPublicKey; +import org.spongycastle.openpgp.PGPPublicKeyRing; +import org.spongycastle.openpgp.PGPSecretKey; +import org.spongycastle.openpgp.PGPSecretKeyRing; +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.util.IterableIterator; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Iterator; + +public class CanonicalizedSecretKeyRing extends CanonicalizedKeyRing { + + private PGPSecretKeyRing mRing; + + CanonicalizedSecretKeyRing(PGPSecretKeyRing ring, int verified) { + super(verified); + mRing = ring; + } + + public CanonicalizedSecretKeyRing(byte[] blob, boolean isRevoked, int verified) + { + super(verified); + PGPObjectFactory factory = new PGPObjectFactory(blob); + PGPKeyRing keyRing = null; + try { + if ((keyRing = (PGPKeyRing) factory.nextObject()) == null) { + Log.e(Constants.TAG, "No keys given!"); + } + } catch (IOException e) { + Log.e(Constants.TAG, "Error while converting to PGPKeyRing!", e); + } + + mRing = (PGPSecretKeyRing) keyRing; + } + + PGPSecretKeyRing getRing() { + return mRing; + } + + public CanonicalizedSecretKey getSecretKey() { + return new CanonicalizedSecretKey(this, mRing.getSecretKey()); + } + + public CanonicalizedSecretKey getSecretKey(long id) { + return new CanonicalizedSecretKey(this, mRing.getSecretKey(id)); + } + + public HashSet getAvailableSubkeys() { + HashSet result = new HashSet(); + // then, mark exactly the keys we have available + for (PGPSecretKey sub : new IterableIterator(getRing().getSecretKeys())) { + S2K s2k = sub.getS2K(); + // Set to 1, except if the encryption type is GNU_DUMMY_S2K + if(s2k == null || s2k.getType() != S2K.GNU_DUMMY_S2K) { + result.add(sub.getKeyID()); + } + } + return result; + } + + /** 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; + } + } + 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; + } + } + + public IterableIterator secretKeyIterator() { + final Iterator it = mRing.getSecretKeys(); + return new IterableIterator(new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public CanonicalizedSecretKey next() { + return new CanonicalizedSecretKey(CanonicalizedSecretKeyRing.this, it.next()); + } + + @Override + public void remove() { + it.remove(); + } + }); + } + + public IterableIterator publicKeyIterator() { + final Iterator it = getRing().getPublicKeys(); + return new IterableIterator(new Iterator() { + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public CanonicalizedPublicKey next() { + return new CanonicalizedPublicKey(CanonicalizedSecretKeyRing.this, it.next()); + } + + @Override + public void remove() { + it.remove(); + } + }); + } + +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java index 129ffba3e..ebc49ab05 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java @@ -12,7 +12,7 @@ import java.util.regex.Pattern; * keyring should in all cases agree on the output of all methods described * here. * - * @see org.sufficientlysecure.keychain.pgp.WrappedKeyRing + * @see CanonicalizedKeyRing * @see org.sufficientlysecure.keychain.provider.CachedPublicKeyRing * */ diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java index db9e2c6c6..7f2d971ed 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpDecryptVerify.java @@ -231,7 +231,7 @@ public class PgpDecryptVerify { PGPPublicKeyEncryptedData encryptedDataAsymmetric = null; PGPPBEEncryptedData encryptedDataSymmetric = null; - WrappedSecretKey secretEncryptionKey = null; + CanonicalizedSecretKey secretEncryptionKey = null; Iterator it = enc.getEncryptedDataObjects(); boolean asymmetricPacketFound = false; boolean symmetricPacketFound = false; @@ -243,10 +243,10 @@ public class PgpDecryptVerify { PGPPublicKeyEncryptedData encData = (PGPPublicKeyEncryptedData) obj; - WrappedSecretKeyRing secretKeyRing; + CanonicalizedSecretKeyRing secretKeyRing; try { // get actual keyring object based on master key id - secretKeyRing = mProviderHelper.getWrappedSecretKeyRing( + secretKeyRing = mProviderHelper.getCanonicalizedSecretKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(encData.getKeyID()) ); } catch (ProviderHelper.NotFoundException e) { @@ -365,8 +365,8 @@ public class PgpDecryptVerify { Object dataChunk = plainFact.nextObject(); OpenPgpSignatureResultBuilder signatureResultBuilder = new OpenPgpSignatureResultBuilder(); int signatureIndex = -1; - WrappedPublicKeyRing signingRing = null; - WrappedPublicKey signingKey = null; + CanonicalizedPublicKeyRing signingRing = null; + CanonicalizedPublicKey signingKey = null; if (dataChunk instanceof PGPCompressedData) { updateProgress(R.string.progress_decompressing_data, currentProgress, 100); @@ -390,7 +390,7 @@ public class PgpDecryptVerify { for (int i = 0; i < sigList.size(); ++i) { try { long sigKeyId = sigList.get(i).getKeyID(); - signingRing = mProviderHelper.getWrappedPublicKeyRing( + signingRing = mProviderHelper.getCanonicalizedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) ); signingKey = signingRing.getPublicKey(sigKeyId); @@ -566,8 +566,8 @@ public class PgpDecryptVerify { throw new InvalidDataException(); } - WrappedPublicKeyRing signingRing = null; - WrappedPublicKey signingKey = null; + CanonicalizedPublicKeyRing signingRing = null; + CanonicalizedPublicKey signingKey = null; int signatureIndex = -1; // go through all signatures @@ -575,7 +575,7 @@ public class PgpDecryptVerify { for (int i = 0; i < sigList.size(); ++i) { try { long sigKeyId = sigList.get(i).getKeyID(); - signingRing = mProviderHelper.getWrappedPublicKeyRing( + signingRing = mProviderHelper.getCanonicalizedPublicKeyRing( KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(sigKeyId) ); signingKey = signingRing.getPublicKey(sigKeyId); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java index 9d334d9bd..846b00ef2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpImportExport.java @@ -93,7 +93,7 @@ public class PgpImportExport { } } - public boolean uploadKeyRingToServer(HkpKeyserver server, WrappedPublicKeyRing keyring) { + public boolean uploadKeyRingToServer(HkpKeyserver server, CanonicalizedPublicKeyRing keyring) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ArmoredOutputStream aos = null; try { @@ -235,7 +235,7 @@ public class PgpImportExport { updateProgress(progress * 100 / masterKeyIdsSize, 100); try { - WrappedPublicKeyRing ring = mProviderHelper.getWrappedPublicKeyRing( + CanonicalizedPublicKeyRing ring = mProviderHelper.getCanonicalizedPublicKeyRing( KeychainContract.KeyRings.buildUnifiedKeyRingUri(pubKeyMasterId) ); @@ -263,8 +263,8 @@ public class PgpImportExport { updateProgress(progress * 100 / masterKeyIdsSize, 100); try { - WrappedSecretKeyRing secretKeyRing = - mProviderHelper.getWrappedSecretKeyRing(secretKeyMasterId); + CanonicalizedSecretKeyRing secretKeyRing = + mProviderHelper.getCanonicalizedSecretKeyRing(secretKeyMasterId); secretKeyRing.encode(arOutStream); } catch (ProviderHelper.NotFoundException e) { Log.e(Constants.TAG, "key not found!", e); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index e9d7e5958..fef15db1e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -46,7 +46,6 @@ import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.service.OperationResultParcel; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType; @@ -241,7 +240,7 @@ public class PgpKeyOperation { * are changed by adding new certificates, which implicitly override older certificates. * */ - public EditKeyResult modifySecretKeyRing(WrappedSecretKeyRing wsKR, SaveKeyringParcel saveParcel, + public EditKeyResult modifySecretKeyRing(CanonicalizedSecretKeyRing wsKR, SaveKeyringParcel saveParcel, String passphrase) { OperationLog log = new OperationLog(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java index 4cb92c368..f0403e625 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpSignEncrypt.java @@ -266,11 +266,11 @@ public class PgpSignEncrypt { } /* Get keys for signature generation for later usage */ - WrappedSecretKey signingKey = null; + CanonicalizedSecretKey signingKey = null; if (enableSignature) { - WrappedSecretKeyRing signingKeyRing; + CanonicalizedSecretKeyRing signingKeyRing; try { - signingKeyRing = mProviderHelper.getWrappedSecretKeyRing(mSignatureMasterKeyId); + signingKeyRing = mProviderHelper.getCanonicalizedSecretKeyRing(mSignatureMasterKeyId); } catch (ProviderHelper.NotFoundException e) { throw new NoSigningKeyException(); } @@ -316,9 +316,9 @@ public class PgpSignEncrypt { // Asymmetric encryption for (long id : mEncryptionMasterKeyIds) { try { - WrappedPublicKeyRing keyRing = mProviderHelper.getWrappedPublicKeyRing( + CanonicalizedPublicKeyRing keyRing = mProviderHelper.getCanonicalizedPublicKeyRing( KeyRings.buildUnifiedKeyRingUri(id)); - WrappedPublicKey key = keyRing.getEncryptionSubKey(); + CanonicalizedPublicKey key = keyRing.getEncryptionSubKey(); cPk.addMethod(key.getPubKeyEncryptionGenerator()); } catch (PgpGeneralException e) { Log.e(Constants.TAG, "key not found!", e); 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 0e59b7fdb..d8a2532f3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -49,7 +49,7 @@ import java.util.Vector; * treated equally for most purposes in UI code. It is up to the programmer to * take care of the differences. * - * @see org.sufficientlysecure.keychain.pgp.WrappedKeyRing + * @see CanonicalizedKeyRing * @see org.sufficientlysecure.keychain.pgp.UncachedPublicKey * @see org.sufficientlysecure.keychain.pgp.UncachedSecretKey * @@ -59,18 +59,10 @@ public class UncachedKeyRing { final PGPKeyRing mRing; final boolean mIsSecret; - final boolean mIsCanonicalized; UncachedKeyRing(PGPKeyRing ring) { mRing = ring; mIsSecret = ring instanceof PGPSecretKeyRing; - mIsCanonicalized = false; - } - - private UncachedKeyRing(PGPKeyRing ring, boolean canonicalized) { - mRing = ring; - mIsSecret = ring instanceof PGPSecretKeyRing; - mIsCanonicalized = canonicalized; } public long getMasterKeyId() { @@ -105,10 +97,6 @@ public class UncachedKeyRing { return mIsSecret; } - public boolean isCanonicalized() { - return mIsCanonicalized; - } - public byte[] getEncoded() throws IOException { return mRing.getEncoded(); } @@ -164,25 +152,6 @@ public class UncachedKeyRing { aos.close(); } - public HashSet getAvailableSubkeys() { - if(!isSecret()) { - throw new RuntimeException("Tried to find available subkeys from non-secret keys. " + - "This is a programming error and should never happen!"); - } - - HashSet result = new HashSet(); - // then, mark exactly the keys we have available - for (PGPSecretKey sub : new IterableIterator( - ((PGPSecretKeyRing) mRing).getSecretKeys())) { - S2K s2k = sub.getS2K(); - // Set to 1, except if the encryption type is GNU_DUMMY_S2K - if(s2k == null || s2k.getType() != S2K.GNU_DUMMY_S2K) { - result.add(sub.getKeyID()); - } - } - return result; - } - /** "Canonicalizes" a public key, removing inconsistencies in the process. This variant can be * applied to public keyrings only. * @@ -207,7 +176,7 @@ public class UncachedKeyRing { * */ @SuppressWarnings("ConstantConditions") - public UncachedKeyRing canonicalize(OperationLog log, int indent) { + public CanonicalizedKeyRing canonicalize(OperationLog log, int indent) { log.add(LogLevel.START, isSecret() ? LogType.MSG_KC_SECRET : LogType.MSG_KC_PUBLIC, indent, PgpKeyHelper.convertKeyIdToHex(getMasterKeyId())); @@ -629,7 +598,8 @@ public class UncachedKeyRing { log.add(LogLevel.OK, LogType.MSG_KC_SUCCESS, indent); } - return new UncachedKeyRing(ring, true); + return isSecret() ? new CanonicalizedSecretKeyRing((PGPSecretKeyRing) ring, 1) + : new CanonicalizedPublicKeyRing((PGPPublicKeyRing) ring, 0); } /** This operation merges information from a different keyring, returning a combined diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java deleted file mode 100644 index a054255dc..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedKeyRing.java +++ /dev/null @@ -1,120 +0,0 @@ -package org.sufficientlysecure.keychain.pgp; - -import org.spongycastle.openpgp.PGPKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.util.IterableIterator; - -import java.io.IOException; -import java.io.OutputStream; - -/** A generic wrapped PGPKeyRing object. - * - * This class provides implementations for all basic getters which both - * PublicKeyRing and SecretKeyRing have in common. To make the wrapped keyring - * class typesafe in implementing subclasses, the field is stored in the - * implementing class, providing properly typed access through the getRing - * getter method. - * - */ -public abstract class WrappedKeyRing extends KeyRing { - - private final boolean mHasAnySecret; - private final int mVerified; - - WrappedKeyRing(boolean hasAnySecret, int verified) { - mHasAnySecret = hasAnySecret; - mVerified = verified; - } - - public long getMasterKeyId() { - return getRing().getPublicKey().getKeyID(); - } - - public boolean hasAnySecret() { - return mHasAnySecret; - } - - public int getVerified() { - return mVerified; - } - - public String getPrimaryUserId() throws PgpGeneralException { - return getPublicKey().getPrimaryUserId(); - } - - public String getPrimaryUserIdWithFallback() throws PgpGeneralException { - return getPublicKey().getPrimaryUserIdWithFallback(); - } - - public boolean isRevoked() throws PgpGeneralException { - // Is the master key revoked? - return getRing().getPublicKey().isRevoked(); - } - - public boolean canCertify() throws PgpGeneralException { - return getRing().getPublicKey().isEncryptionKey(); - } - - public long getEncryptId() throws PgpGeneralException { - for(WrappedPublicKey key : publicKeyIterator()) { - if(key.canEncrypt()) { - return key.getKeyId(); - } - } - throw new PgpGeneralException("No valid encryption key found!"); - } - - public boolean hasEncrypt() throws PgpGeneralException { - try { - getEncryptId(); - return true; - } catch(PgpGeneralException e) { - return false; - } - } - - public long getSignId() throws PgpGeneralException { - for(WrappedPublicKey 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); - } - - /** Returns an UncachedKeyRing which wraps the same data as this ring. This method should - * only be used */ - public UncachedKeyRing getUncachedKeyRing() { - return new UncachedKeyRing(getRing()); - } - - abstract PGPKeyRing getRing(); - - abstract public IterableIterator publicKeyIterator(); - - public WrappedPublicKey getPublicKey() { - return new WrappedPublicKey(this, getRing().getPublicKey()); - } - - public WrappedPublicKey getPublicKey(long id) { - return new WrappedPublicKey(this, getRing().getPublicKey(id)); - } - - public byte[] getEncoded() throws IOException { - return getRing().getEncoded(); - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKey.java deleted file mode 100644 index 69a4fbdee..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKey.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.sufficientlysecure.keychain.pgp; - -import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyKeyEncryptionMethodGenerator; -import org.sufficientlysecure.keychain.util.IterableIterator; - -/** Wrapper for a PGPPublicKey. - * - * The methods implemented in this class are a thin layer over - * UncachedPublicKey. The difference between the two classes is that objects of - * this class can only be obtained from a WrappedKeyRing, and that it stores a - * back reference to its parent as well. A method which works with - * WrappedPublicKey is therefore guaranteed to work on a KeyRing which is - * stored in the database. - * - */ -public class WrappedPublicKey extends UncachedPublicKey { - - // this is the parent key ring - final KeyRing mRing; - - WrappedPublicKey(KeyRing ring, PGPPublicKey key) { - super(key); - mRing = ring; - } - - public IterableIterator getUserIds() { - return new IterableIterator(mPublicKey.getUserIDs()); - } - - public KeyRing getKeyRing() { - return mRing; - } - - JcePublicKeyKeyEncryptionMethodGenerator getPubKeyEncryptionGenerator() { - return new JcePublicKeyKeyEncryptionMethodGenerator(mPublicKey); - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java deleted file mode 100644 index bb1f7d778..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedPublicKeyRing.java +++ /dev/null @@ -1,81 +0,0 @@ -package org.sufficientlysecure.keychain.pgp; - -import org.spongycastle.bcpg.ArmoredOutputStream; -import org.spongycastle.openpgp.PGPObjectFactory; -import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.util.IterableIterator; - -import java.io.IOException; -import java.util.Iterator; - -public class WrappedPublicKeyRing extends WrappedKeyRing { - - private PGPPublicKeyRing mRing; - private final byte[] mPubKey; - - public WrappedPublicKeyRing(byte[] blob, boolean hasAnySecret, int verified) { - super(hasAnySecret, verified); - mPubKey = blob; - } - - PGPPublicKeyRing getRing() { - if(mRing == null) { - // get first object in block - PGPObjectFactory factory = new PGPObjectFactory(mPubKey); - try { - Object obj = factory.nextObject(); - if (! (obj instanceof PGPPublicKeyRing)) { - throw new RuntimeException("Error constructing WrappedPublicKeyRing, should never happen!"); - } - mRing = (PGPPublicKeyRing) obj; - if (factory.nextObject() != null) { - throw new RuntimeException("Encountered trailing data after keyring, should never happen!"); - } - } catch (IOException e) { - throw new RuntimeException("IO Error constructing WrappedPublicKeyRing, should never happen!"); - } - } - return mRing; - } - - public void encode(ArmoredOutputStream stream) throws IOException { - getRing().encode(stream); - } - - /** Getter that returns the subkey that should be used for signing. */ - WrappedPublicKey getEncryptionSubKey() throws PgpGeneralException { - PGPPublicKey key = getRing().getPublicKey(getEncryptId()); - if(key != null) { - WrappedPublicKey cKey = new WrappedPublicKey(this, key); - if(!cKey.canEncrypt()) { - throw new PgpGeneralException("key error"); - } - return cKey; - } - throw new PgpGeneralException("no encryption key available"); - } - - public IterableIterator publicKeyIterator() { - @SuppressWarnings("unchecked") - final Iterator it = getRing().getPublicKeys(); - return new IterableIterator(new Iterator() { - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public WrappedPublicKey next() { - return new WrappedPublicKey(WrappedPublicKeyRing.this, it.next()); - } - - @Override - public void remove() { - it.remove(); - } - }); - } - -} \ No newline at end of file diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java deleted file mode 100644 index f0485d801..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKey.java +++ /dev/null @@ -1,192 +0,0 @@ -package org.sufficientlysecure.keychain.pgp; - -import org.spongycastle.openpgp.PGPException; -import org.spongycastle.openpgp.PGPPrivateKey; -import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPPublicKeyRing; -import org.spongycastle.openpgp.PGPSecretKey; -import org.spongycastle.openpgp.PGPSignature; -import org.spongycastle.openpgp.PGPSignatureGenerator; -import org.spongycastle.openpgp.PGPSignatureSubpacketGenerator; -import org.spongycastle.openpgp.PGPSignatureSubpacketVector; -import org.spongycastle.openpgp.PGPUtil; -import org.spongycastle.openpgp.PGPV3SignatureGenerator; -import org.spongycastle.openpgp.operator.PBESecretKeyDecryptor; -import org.spongycastle.openpgp.operator.PublicKeyDataDecryptorFactory; -import org.spongycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; -import org.spongycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; -import org.spongycastle.openpgp.operator.jcajce.JcePublicKeyDataDecryptorFactoryBuilder; -import org.sufficientlysecure.keychain.Constants; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; -import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; -import org.sufficientlysecure.keychain.util.IterableIterator; - -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.SignatureException; -import java.util.List; - -/** Wrapper for a PGPSecretKey. - * - * This object can only be obtained from a WrappedSecretKeyRing, and stores a - * back reference to its parent. - * - * This class represents known secret keys which are stored in the database. - * All "crypto operations using a known secret key" should be implemented in - * this class, to ensure on type level that these operations are performed on - * properly imported secret keys only. - * - */ -public class WrappedSecretKey extends WrappedPublicKey { - - private final PGPSecretKey mSecretKey; - private PGPPrivateKey mPrivateKey = null; - - WrappedSecretKey(WrappedSecretKeyRing ring, PGPSecretKey key) { - super(ring, key.getPublicKey()); - mSecretKey = key; - } - - public WrappedSecretKeyRing getRing() { - return (WrappedSecretKeyRing) mRing; - } - - public boolean unlock(String passphrase) throws PgpGeneralException { - try { - PBESecretKeyDecryptor keyDecryptor = new JcePBESecretKeyDecryptorBuilder().setProvider( - Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(passphrase.toCharArray()); - mPrivateKey = mSecretKey.extractPrivateKey(keyDecryptor); - } catch (PGPException e) { - return false; - } - if(mPrivateKey == null) { - throw new PgpGeneralException("error extracting key"); - } - return true; - } - - public PGPSignatureGenerator getSignatureGenerator(int hashAlgo, boolean cleartext) - throws PgpGeneralException { - if(mPrivateKey == null) { - throw new PrivateKeyNotUnlockedException(); - } - - // content signer based on signing key algorithm and chosen hash algorithm - JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( - mSecretKey.getPublicKey().getAlgorithm(), hashAlgo) - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); - - int signatureType; - if (cleartext) { - // for sign-only ascii text - signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT; - } else { - signatureType = PGPSignature.BINARY_DOCUMENT; - } - - try { - PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); - signatureGenerator.init(signatureType, mPrivateKey); - - PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - spGen.setSignerUserID(false, mRing.getPrimaryUserIdWithFallback()); - signatureGenerator.setHashedSubpackets(spGen.generate()); - return signatureGenerator; - } catch(PGPException e) { - throw new PgpGeneralException("Error initializing signature!", e); - } - } - - public PGPV3SignatureGenerator getV3SignatureGenerator(int hashAlgo, boolean cleartext) - throws PgpGeneralException { - if(mPrivateKey == null) { - throw new PrivateKeyNotUnlockedException(); - } - - // content signer based on signing key algorithm and chosen hash algorithm - JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( - mSecretKey.getPublicKey().getAlgorithm(), hashAlgo) - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); - - int signatureType; - if (cleartext) { - // for sign-only ascii text - signatureType = PGPSignature.CANONICAL_TEXT_DOCUMENT; - } else { - signatureType = PGPSignature.BINARY_DOCUMENT; - } - - try { - PGPV3SignatureGenerator signatureV3Generator = new PGPV3SignatureGenerator(contentSignerBuilder); - signatureV3Generator.init(signatureType, mPrivateKey); - return signatureV3Generator; - } catch(PGPException e) { - throw new PgpGeneralException("Error initializing signature!", e); - } - } - - public PublicKeyDataDecryptorFactory getDecryptorFactory() { - if(mPrivateKey == null) { - throw new PrivateKeyNotUnlockedException(); - } - return new JcePublicKeyDataDecryptorFactoryBuilder() - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(mPrivateKey); - } - - /** - * Certify the given pubkeyid with the given masterkeyid. - * - * @param publicKeyRing Keyring to add certification to. - * @param userIds User IDs to certify, must not be null or empty - * @return A keyring with added certifications - */ - public UncachedKeyRing certifyUserIds(WrappedPublicKeyRing publicKeyRing, List userIds) - throws PgpGeneralMsgIdException, NoSuchAlgorithmException, NoSuchProviderException, - PGPException, SignatureException { - - if(mPrivateKey == null) { - throw new PrivateKeyNotUnlockedException(); - } - - // create a signatureGenerator from the supplied masterKeyId and passphrase - PGPSignatureGenerator signatureGenerator; - { - // TODO: SHA256 fixed? - JcaPGPContentSignerBuilder contentSignerBuilder = new JcaPGPContentSignerBuilder( - mSecretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256) - .setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME); - - signatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); - signatureGenerator.init(PGPSignature.DEFAULT_CERTIFICATION, mPrivateKey); - } - - { // supply signatureGenerator with a SubpacketVector - PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); - PGPSignatureSubpacketVector packetVector = spGen.generate(); - signatureGenerator.setHashedSubpackets(packetVector); - } - - // get the master subkey (which we certify for) - PGPPublicKey publicKey = publicKeyRing.getPublicKey().getPublicKey(); - - // fetch public key ring, add the certification and return it - for (String userId : new IterableIterator(userIds.iterator())) { - PGPSignature sig = signatureGenerator.generateCertification(userId, publicKey); - publicKey = PGPPublicKey.addCertification(publicKey, userId, sig); - } - - PGPPublicKeyRing ring = PGPPublicKeyRing.insertPublicKey(publicKeyRing.getRing(), publicKey); - - return new UncachedKeyRing(ring); - } - - static class PrivateKeyNotUnlockedException extends RuntimeException { - // this exception is a programming error which happens when an operation which requires - // the private key is called without a previous call to unlock() - } - - public UncachedSecretKey getUncached() { - return new UncachedSecretKey(mSecretKey); - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java deleted file mode 100644 index 5cb24cf88..000000000 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSecretKeyRing.java +++ /dev/null @@ -1,131 +0,0 @@ -package org.sufficientlysecure.keychain.pgp; - -import org.spongycastle.openpgp.PGPException; -import org.spongycastle.openpgp.PGPKeyRing; -import org.spongycastle.openpgp.PGPObjectFactory; -import org.spongycastle.openpgp.PGPPrivateKey; -import org.spongycastle.openpgp.PGPPublicKey; -import org.spongycastle.openpgp.PGPSecretKey; -import org.spongycastle.openpgp.PGPSecretKeyRing; -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.util.IterableIterator; -import org.sufficientlysecure.keychain.util.Log; - -import java.io.IOException; -import java.util.Iterator; - -public class WrappedSecretKeyRing extends WrappedKeyRing { - - private PGPSecretKeyRing mRing; - - public WrappedSecretKeyRing(byte[] blob, boolean isRevoked, int verified) - { - super(isRevoked, verified); - PGPObjectFactory factory = new PGPObjectFactory(blob); - PGPKeyRing keyRing = null; - try { - if ((keyRing = (PGPKeyRing) factory.nextObject()) == null) { - Log.e(Constants.TAG, "No keys given!"); - } - } catch (IOException e) { - Log.e(Constants.TAG, "Error while converting to PGPKeyRing!", e); - } - - mRing = (PGPSecretKeyRing) keyRing; - } - - PGPSecretKeyRing getRing() { - return mRing; - } - - public WrappedSecretKey getSecretKey() { - return new WrappedSecretKey(this, mRing.getSecretKey()); - } - - public WrappedSecretKey getSecretKey(long id) { - return new WrappedSecretKey(this, mRing.getSecretKey(id)); - } - - /** Getter that returns the subkey that should be used for signing. */ - WrappedSecretKey getSigningSubKey() throws PgpGeneralException { - PGPSecretKey key = mRing.getSecretKey(getSignId()); - if(key != null) { - WrappedSecretKey cKey = new WrappedSecretKey(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; - } - } - 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; - } - } - - public IterableIterator secretKeyIterator() { - final Iterator it = mRing.getSecretKeys(); - return new IterableIterator(new Iterator() { - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public WrappedSecretKey next() { - return new WrappedSecretKey(WrappedSecretKeyRing.this, it.next()); - } - - @Override - public void remove() { - it.remove(); - } - }); - } - - public IterableIterator publicKeyIterator() { - final Iterator it = getRing().getPublicKeys(); - return new IterableIterator(new Iterator() { - @Override - public boolean hasNext() { - return it.hasNext(); - } - - @Override - public WrappedPublicKey next() { - return new WrappedPublicKey(WrappedSecretKeyRing.this, it.next()); - } - - @Override - public void remove() { - it.remove(); - } - }); - } - -} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java index 28e4c51d6..07fb4fb9e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/WrappedSignature.java @@ -113,7 +113,7 @@ public class WrappedSignature { return ((RevocationReason) p).getRevocationDescription(); } - public void init(WrappedPublicKey key) throws PgpGeneralException { + public void init(CanonicalizedPublicKey key) throws PgpGeneralException { init(key.getPublicKey()); } @@ -191,7 +191,7 @@ public class WrappedSignature { public boolean verifySignature(UncachedPublicKey key, String uid) throws PgpGeneralException { return verifySignature(key.getPublicKey(), uid); } - public boolean verifySignature(WrappedPublicKey key, String uid) throws PgpGeneralException { + public boolean verifySignature(CanonicalizedPublicKey key, String uid) throws PgpGeneralException { return verifySignature(key.getPublicKey(), uid); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java index 998cf25c3..9b35903f6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/provider/ProviderHelper.java @@ -28,10 +28,12 @@ import android.os.RemoteException; import android.support.v4.util.LongSparseArray; import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.pgp.NullProgressable; import org.sufficientlysecure.keychain.pgp.Progressable; -import org.sufficientlysecure.keychain.pgp.WrappedPublicKey; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKey; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogType; import org.sufficientlysecure.keychain.service.OperationResultParcel.LogLevel; import org.sufficientlysecure.keychain.service.OperationResultParcel.OperationLog; @@ -39,8 +41,6 @@ import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.pgp.UncachedPublicKey; -import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.WrappedSignature; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract.ApiApps; @@ -180,7 +180,7 @@ public class ProviderHelper { return getGenericData(KeyRings.buildUnifiedKeyRingUri(masterKeyId), proj, types); } - private LongSparseArray getTrustedMasterKeys() { + private LongSparseArray getTrustedMasterKeys() { Cursor cursor = mContentResolver.query(KeyRings.buildUnifiedKeyRingsUri(), new String[] { KeyRings.MASTER_KEY_ID, // we pick from cache only information that is not easily available from keyrings @@ -190,16 +190,15 @@ public class ProviderHelper { }, KeyRings.HAS_ANY_SECRET + " = 1", null, null); try { - LongSparseArray result = new LongSparseArray(); + LongSparseArray result = new LongSparseArray(); if (cursor != null && cursor.moveToFirst()) do { long masterKeyId = cursor.getLong(0); - boolean hasAnySecret = cursor.getInt(1) > 0; int verified = cursor.getInt(2); byte[] blob = cursor.getBlob(3); if (blob != null) { result.put(masterKeyId, - new WrappedPublicKeyRing(blob, hasAnySecret, verified).getPublicKey()); + new CanonicalizedPublicKeyRing(blob, verified).getPublicKey()); } } while (cursor.moveToNext()); @@ -217,23 +216,23 @@ public class ProviderHelper { return new CachedPublicKeyRing(this, queryUri); } - public WrappedPublicKeyRing getWrappedPublicKeyRing(long id) throws NotFoundException { - return (WrappedPublicKeyRing) getWrappedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), false); + public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(long id) throws NotFoundException { + return (CanonicalizedPublicKeyRing) getCanonicalizedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), false); } - public WrappedPublicKeyRing getWrappedPublicKeyRing(Uri queryUri) throws NotFoundException { - return (WrappedPublicKeyRing) getWrappedKeyRing(queryUri, false); + public CanonicalizedPublicKeyRing getCanonicalizedPublicKeyRing(Uri queryUri) throws NotFoundException { + return (CanonicalizedPublicKeyRing) getCanonicalizedKeyRing(queryUri, false); } - public WrappedSecretKeyRing getWrappedSecretKeyRing(long id) throws NotFoundException { - return (WrappedSecretKeyRing) getWrappedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), true); + public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(long id) throws NotFoundException { + return (CanonicalizedSecretKeyRing) getCanonicalizedKeyRing(KeyRings.buildUnifiedKeyRingUri(id), true); } - public WrappedSecretKeyRing getWrappedSecretKeyRing(Uri queryUri) throws NotFoundException { - return (WrappedSecretKeyRing) getWrappedKeyRing(queryUri, true); + public CanonicalizedSecretKeyRing getCanonicalizedSecretKeyRing(Uri queryUri) throws NotFoundException { + return (CanonicalizedSecretKeyRing) getCanonicalizedKeyRing(queryUri, true); } - private KeyRing getWrappedKeyRing(Uri queryUri, boolean secret) throws NotFoundException { + private KeyRing getCanonicalizedKeyRing(Uri queryUri, boolean secret) throws NotFoundException { Cursor cursor = mContentResolver.query(queryUri, new String[]{ // we pick from cache only information that is not easily available from keyrings @@ -252,8 +251,8 @@ public class ProviderHelper { throw new NotFoundException("Secret key not available!"); } return secret - ? new WrappedSecretKeyRing(blob, true, verified) - : new WrappedPublicKeyRing(blob, hasAnySecret, verified); + ? new CanonicalizedSecretKeyRing(blob, true, verified) + : new CanonicalizedPublicKeyRing(blob, verified); } else { throw new NotFoundException("Key not found!"); } @@ -271,16 +270,8 @@ public class ProviderHelper { * and need to be saved externally to be preserved past the operation. */ @SuppressWarnings("unchecked") - private int internalSavePublicKeyRing(UncachedKeyRing keyRing, - Progressable progress, boolean selfCertsAreTrusted) { - if (keyRing.isSecret()) { - log(LogLevel.ERROR, LogType.MSG_IP_BAD_TYPE_SECRET); - return SaveKeyringResult.RESULT_ERROR; - } - if (!keyRing.isCanonicalized()) { - log(LogLevel.ERROR, LogType.MSG_IP_BAD_TYPE_SECRET); - return SaveKeyringResult.RESULT_ERROR; - } + private int saveCanonicalizedPublicKeyRing(CanonicalizedPublicKeyRing keyRing, + Progressable progress, boolean selfCertsAreTrusted) { // start with ok result int result = SaveKeyringResult.SAVED_PUBLIC; @@ -318,7 +309,7 @@ public class ProviderHelper { { // insert subkeys Uri uri = Keys.buildKeysUri(Long.toString(masterKeyId)); int rank = 0; - for (UncachedPublicKey key : new IterableIterator(keyRing.getPublicKeys())) { + for (CanonicalizedPublicKey key : keyRing.publicKeyIterator()) { long keyId = key.getKeyId(); log(LogLevel.DEBUG, keyId == masterKeyId ? LogType.MSG_IP_MASTER : LogType.MSG_IP_SUBKEY, PgpKeyHelper.convertKeyIdToHex(keyId) @@ -401,7 +392,7 @@ public class ProviderHelper { mIndent -= 1; // get a list of owned secret keys, for verification filtering - LongSparseArray trustedKeys = getTrustedMasterKeys(); + LongSparseArray trustedKeys = getTrustedMasterKeys(); // classify and order user ids. primary are moved to the front, revoked to the back, // otherwise the order in the keyfile is preserved. @@ -445,7 +436,7 @@ public class ProviderHelper { // verify signatures from known private keys if (trustedKeys.indexOfKey(certId) >= 0) { - WrappedPublicKey trustedKey = trustedKeys.get(certId); + CanonicalizedPublicKey trustedKey = trustedKeys.get(certId); cert.init(trustedKey); if (cert.verifySignature(masterKey, userId)) { item.trustedCerts.add(cert); @@ -559,17 +550,7 @@ public class ProviderHelper { /** Saves an UncachedKeyRing of the secret variant into the db. * This method will fail if no corresponding public keyring is in the database! */ - private int internalSaveSecretKeyRing(UncachedKeyRing keyRing) { - - if (!keyRing.isSecret()) { - log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_PUBLIC); - return SaveKeyringResult.RESULT_ERROR; - } - - if (!keyRing.isCanonicalized()) { - log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_UNCANON); - return SaveKeyringResult.RESULT_ERROR; - } + private int saveCanonicalizedSecretKeyRing(CanonicalizedSecretKeyRing keyRing) { long masterKeyId = keyRing.getMasterKeyId(); log(LogLevel.START, LogType.MSG_IS, PgpKeyHelper.convertKeyIdToHex(masterKeyId)); @@ -610,8 +591,7 @@ public class ProviderHelper { log(LogLevel.INFO, LogType.MSG_IS_IMPORTING_SUBKEYS); mIndent += 1; Set available = keyRing.getAvailableSubkeys(); - for (UncachedPublicKey sub : - new IterableIterator(keyRing.getPublicKeys())) { + for (UncachedPublicKey sub : keyRing.publicKeyIterator()) { long id = sub.getKeyId(); if (available.contains(id)) { int upd = mContentResolver.update(uri, values, Keys.KEY_ID + " = ?", @@ -662,9 +642,16 @@ public class ProviderHelper { log(LogLevel.START, LogType.MSG_IP, PgpKeyHelper.convertKeyIdToHex(masterKeyId)); mIndent += 1; + if (publicRing.isSecret()) { + log(LogLevel.ERROR, LogType.MSG_IP_BAD_TYPE_SECRET); + return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); + } + + CanonicalizedPublicKeyRing canPublicRing; + // If there is an old keyring, merge it try { - UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncachedKeyRing(); + UncachedKeyRing oldPublicRing = getCanonicalizedPublicKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new public ring into the old one publicRing = oldPublicRing.merge(publicRing, mLog, mIndent); @@ -675,8 +662,8 @@ public class ProviderHelper { } // Canonicalize this keyring, to assert a number of assumptions made about it. - publicRing = publicRing.canonicalize(mLog, mIndent); - if (publicRing == null) { + canPublicRing = (CanonicalizedPublicKeyRing) publicRing.canonicalize(mLog, mIndent); + if (canPublicRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } @@ -690,39 +677,40 @@ public class ProviderHelper { // Not an issue, just means we are dealing with a new keyring. // Canonicalize this keyring, to assert a number of assumptions made about it. - publicRing = publicRing.canonicalize(mLog, mIndent); - if (publicRing == null) { + canPublicRing = (CanonicalizedPublicKeyRing) publicRing.canonicalize(mLog, mIndent); + if (canPublicRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } } // If there is a secret key, merge new data (if any) and save the key for later - UncachedKeyRing secretRing; + CanonicalizedSecretKeyRing canSecretRing; try { - secretRing = getWrappedSecretKeyRing(publicRing.getMasterKeyId()).getUncachedKeyRing(); + UncachedKeyRing secretRing = getCanonicalizedSecretKeyRing(publicRing.getMasterKeyId()).getUncachedKeyRing(); // Merge data from new public ring into secret one secretRing = secretRing.merge(publicRing, mLog, mIndent); if (secretRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } - secretRing = secretRing.canonicalize(mLog, mIndent); - if (secretRing == null) { + // This has always been a secret key ring, this is a safe cast + canSecretRing = (CanonicalizedSecretKeyRing) secretRing.canonicalize(mLog, mIndent); + if (canSecretRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } } catch (NotFoundException e) { // No secret key available (this is what happens most of the time) - secretRing = null; + canSecretRing = null; } - int result = internalSavePublicKeyRing(publicRing, progress, secretRing != null); + int result = saveCanonicalizedPublicKeyRing(canPublicRing, progress, canSecretRing != null); // Save the saved keyring (if any) - if (secretRing != null) { + if (canSecretRing != null) { progress.setProgress(LogType.MSG_IP_REINSERT_SECRET.getMsgId(), 90, 100); - int secretResult = internalSaveSecretKeyRing(secretRing); + int secretResult = saveCanonicalizedSecretKeyRing(canSecretRing); if ((secretResult & SaveKeyringResult.RESULT_ERROR) != SaveKeyringResult.RESULT_ERROR) { result |= SaveKeyringResult.SAVED_SECRET; } @@ -746,9 +734,16 @@ public class ProviderHelper { log(LogLevel.START, LogType.MSG_IS, PgpKeyHelper.convertKeyIdToHex(masterKeyId)); mIndent += 1; + if ( ! secretRing.isSecret()) { + log(LogLevel.ERROR, LogType.MSG_IS_BAD_TYPE_PUBLIC); + return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); + } + + CanonicalizedSecretKeyRing canSecretRing; + // If there is an old secret key, merge it. try { - UncachedKeyRing oldSecretRing = getWrappedSecretKeyRing(masterKeyId).getUncachedKeyRing(); + UncachedKeyRing oldSecretRing = getCanonicalizedSecretKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new secret ring into old one secretRing = secretRing.merge(oldSecretRing, mLog, mIndent); @@ -759,8 +754,9 @@ public class ProviderHelper { } // Canonicalize this keyring, to assert a number of assumptions made about it. - secretRing = secretRing.canonicalize(mLog, mIndent); - if (secretRing == null) { + // This is a safe cast, because we made sure this is a secret ring above + canSecretRing = (CanonicalizedSecretKeyRing) secretRing.canonicalize(mLog, mIndent); + if (canSecretRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } @@ -775,8 +771,9 @@ public class ProviderHelper { // Not an issue, just means we are dealing with a new keyring // Canonicalize this keyring, to assert a number of assumptions made about it. - secretRing = secretRing.canonicalize(mLog, mIndent); - if (secretRing == null) { + // This is a safe cast, because we made sure this is a secret ring above + canSecretRing = (CanonicalizedSecretKeyRing) secretRing.canonicalize(mLog, mIndent); + if (canSecretRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } @@ -785,7 +782,7 @@ public class ProviderHelper { // Merge new data into public keyring as well, if there is any UncachedKeyRing publicRing; try { - UncachedKeyRing oldPublicRing = getWrappedPublicKeyRing(masterKeyId).getUncachedKeyRing(); + UncachedKeyRing oldPublicRing = getCanonicalizedPublicKeyRing(masterKeyId).getUncachedKeyRing(); // Merge data from new secret ring into public one publicRing = oldPublicRing.merge(secretRing, mLog, mIndent); @@ -798,20 +795,20 @@ public class ProviderHelper { publicRing = secretRing.extractPublicKeyRing(); } - publicRing = publicRing.canonicalize(mLog, mIndent); - if (publicRing == null) { + CanonicalizedPublicKeyRing canPublicRing = (CanonicalizedPublicKeyRing) publicRing.canonicalize(mLog, mIndent); + if (canPublicRing == null) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } int result; - result = internalSavePublicKeyRing(publicRing, progress, true); + result = saveCanonicalizedPublicKeyRing(canPublicRing, progress, true); if ((result & SaveKeyringResult.RESULT_ERROR) == SaveKeyringResult.RESULT_ERROR) { return new SaveKeyringResult(SaveKeyringResult.RESULT_ERROR, mLog); } progress.setProgress(LogType.MSG_IP_REINSERT_SECRET.getMsgId(), 90, 100); - result = internalSaveSecretKeyRing(secretRing); + result = saveCanonicalizedSecretKeyRing(canSecretRing); return new SaveKeyringResult(result, mLog); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java index 62d6b5ad6..5ed95acb3 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/remote/OpenPgpService.java @@ -425,7 +425,7 @@ public class OpenPgpService extends RemoteService { try { // try to find key, throws NotFoundException if not in db! - mProviderHelper.getWrappedPublicKeyRing(masterKeyId); + mProviderHelper.getCanonicalizedPublicKeyRing(masterKeyId); Intent result = new Intent(); result.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index a764c323c..f69961df6 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -36,6 +36,8 @@ import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerifyResult; import org.sufficientlysecure.keychain.pgp.PgpHelper; @@ -44,9 +46,7 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -337,8 +337,8 @@ public class KeychainIntentService extends IntentService if (saveParcel.mMasterKeyId != null) { String passphrase = data.getString(SAVE_KEYRING_PASSPHRASE); - WrappedSecretKeyRing secRing = - providerHelper.getWrappedSecretKeyRing(saveParcel.mMasterKeyId); + CanonicalizedSecretKeyRing secRing = + providerHelper.getCanonicalizedSecretKeyRing(saveParcel.mMasterKeyId); result = keyOperations.modifySecretKeyRing(secRing, saveParcel, passphrase); } else { @@ -466,7 +466,7 @@ public class KeychainIntentService extends IntentService HkpKeyserver server = new HkpKeyserver(keyServer); ProviderHelper providerHelper = new ProviderHelper(this); - WrappedPublicKeyRing keyring = providerHelper.getWrappedPublicKeyRing(dataUri); + CanonicalizedPublicKeyRing keyring = providerHelper.getCanonicalizedPublicKeyRing(dataUri); PgpImportExport pgpImportExport = new PgpImportExport(this, null); boolean uploaded = pgpImportExport.uploadKeyRingToServer(server, keyring); @@ -542,9 +542,9 @@ public class KeychainIntentService extends IntentService } ProviderHelper providerHelper = new ProviderHelper(this); - WrappedPublicKeyRing publicRing = providerHelper.getWrappedPublicKeyRing(pubKeyId); - WrappedSecretKeyRing secretKeyRing = providerHelper.getWrappedSecretKeyRing(masterKeyId); - WrappedSecretKey certificationKey = secretKeyRing.getSecretKey(); + CanonicalizedPublicKeyRing publicRing = providerHelper.getCanonicalizedPublicKeyRing(pubKeyId); + CanonicalizedSecretKeyRing secretKeyRing = providerHelper.getCanonicalizedSecretKeyRing(masterKeyId); + CanonicalizedSecretKey certificationKey = secretKeyRing.getSecretKey(); if(!certificationKey.unlock(signaturePassphrase)) { throw new PgpGeneralException("Error extracting key (bad passphrase?)"); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index bfe06dca7..99cafd3f9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -260,7 +260,6 @@ public class OperationResultParcel implements Parcelable { // import secret MSG_IS(R.string.msg_is), MSG_IS_BAD_TYPE_PUBLIC (R.string.msg_is_bad_type_public), - MSG_IS_BAD_TYPE_UNCANON (R.string.msg_is_bad_type_uncanon), MSG_IS_DB_EXCEPTION (R.string.msg_is_db_exception), MSG_IS_FAIL_IO_EXC (R.string.msg_is_io_exc), MSG_IS_IMPORTING_SUBKEYS (R.string.msg_is_importing_subkeys), diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java index cfb3d50f4..c4ecfdec5 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/PassphraseCacheService.java @@ -41,7 +41,7 @@ import android.support.v4.app.NotificationCompat; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.Preferences; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; @@ -184,7 +184,7 @@ public class PassphraseCacheService extends Service { // try to get master key id which is used as an identifier for cached passphrases try { Log.d(Constants.TAG, "PassphraseCacheService.getCachedPassphraseImpl() for masterKeyId " + keyId); - WrappedSecretKeyRing key = new ProviderHelper(this).getWrappedSecretKeyRing( + CanonicalizedSecretKeyRing key = new ProviderHelper(this).getCanonicalizedSecretKeyRing( KeychainContract.KeyRings.buildUnifiedKeyRingsFindBySubkeyUri(keyId)); // no passphrase needed? just add empty string and return it, then if (!key.hasPassphrase()) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java index 5ff864547..9083d1567 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/EditKeyFragment.java @@ -42,14 +42,13 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; import org.sufficientlysecure.keychain.helper.ActionBarHelper; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.service.KeychainIntentServiceHandler; import org.sufficientlysecure.keychain.service.OperationResults; import org.sufficientlysecure.keychain.service.OperationResults.EditKeyResult; -import org.sufficientlysecure.keychain.service.OperationResults.ImportKeyResult; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.service.SaveKeyringParcel; import org.sufficientlysecure.keychain.ui.adapter.SubkeysAdapter; @@ -169,8 +168,8 @@ public class EditKeyFragment extends LoaderFragment implements try { Uri secretUri = KeychainContract.KeyRings.buildUnifiedKeyRingUri(mDataUri); - WrappedSecretKeyRing keyRing = - new ProviderHelper(getActivity()).getWrappedSecretKeyRing(secretUri); + CanonicalizedSecretKeyRing keyRing = + new ProviderHelper(getActivity()).getCanonicalizedSecretKeyRing(secretUri); mSaveKeyringParcel = new SaveKeyringParcel(keyRing.getMasterKeyId(), keyRing.getUncachedKeyRing().getFingerprint()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java index 75c967c60..43de6774b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/LogDisplayFragment.java @@ -178,9 +178,11 @@ public class LogDisplayFragment extends ListFragment implements OnTouchListener if (entry.mParameters != null && entry.mParameters.length > 0 && entry.mParameters[0] instanceof Integer) { ih.mText.setText(getResources().getQuantityString(entry.mType.getMsgId(), - (Integer) entry.mParameters[0], entry.mParameters)); + (Integer) entry.mParameters[0], + entry.mParameters)); } else { - ih.mText.setText(getResources().getString(entry.mType.getMsgId(), entry.mParameters)); + ih.mText.setText(getResources().getString(entry.mType.getMsgId(), + entry.mParameters)); } ih.mText.setTextColor(entry.mLevel == LogLevel.DEBUG ? Color.GRAY : Color.BLACK); convertView.setPadding((entry.mIndent) * dipFactor, 0, 0, 0); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index cfdea0611..37c583920 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -35,7 +35,7 @@ import android.widget.TextView; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; -import org.sufficientlysecure.keychain.pgp.WrappedPublicKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.WrappedSignature; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.KeychainContract.Certs; @@ -143,10 +143,10 @@ public class ViewCertActivity extends ActionBarActivity try { ProviderHelper providerHelper = new ProviderHelper(this); - WrappedPublicKeyRing signeeRing = - providerHelper.getWrappedPublicKeyRing(data.getLong(INDEX_MASTER_KEY_ID)); - WrappedPublicKeyRing signerRing = - providerHelper.getWrappedPublicKeyRing(sig.getKeyId()); + CanonicalizedPublicKeyRing signeeRing = + providerHelper.getCanonicalizedPublicKeyRing(data.getLong(INDEX_MASTER_KEY_ID)); + CanonicalizedPublicKeyRing signerRing = + providerHelper.getCanonicalizedPublicKeyRing(sig.getKeyId()); try { sig.init(signerRing.getPublicKey()); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index 0a7fd6aee..cce8c9d9f 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -46,8 +46,8 @@ import android.widget.Toast; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKey; -import org.sufficientlysecure.keychain.pgp.WrappedSecretKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey; +import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.service.PassphraseCacheService; @@ -103,7 +103,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor // check if secret key has a passphrase if (!(secretKeyId == Constants.key.symmetric || secretKeyId == Constants.key.none)) { try { - if (!new ProviderHelper(context).getWrappedSecretKeyRing(secretKeyId).hasPassphrase()) { + if (!new ProviderHelper(context).getCanonicalizedSecretKeyRing(secretKeyId).hasPassphrase()) { throw new PgpGeneralException("No passphrase! No passphrase dialog needed!"); } } catch (ProviderHelper.NotFoundException e) { @@ -134,7 +134,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor alert.setTitle(R.string.title_authentication); - final WrappedSecretKeyRing secretRing; + final CanonicalizedSecretKeyRing secretRing; String userId; if (secretKeyId == Constants.key.symmetric || secretKeyId == Constants.key.none) { @@ -143,7 +143,7 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor } else { try { ProviderHelper helper = new ProviderHelper(activity); - secretRing = helper.getWrappedSecretKeyRing(secretKeyId); + secretRing = helper.getCanonicalizedSecretKeyRing(secretKeyId); // yes the inner try/catch block is necessary, otherwise the final variable // above can't be statically verified to have been set in all cases because // the catch clause doesn't return. @@ -193,9 +193,9 @@ public class PassphraseDialogFragment extends DialogFragment implements OnEditor return; } - WrappedSecretKey unlockedSecretKey = null; + CanonicalizedSecretKey unlockedSecretKey = null; - for (WrappedSecretKey clickSecretKey : secretRing.secretKeyIterator()) { + for (CanonicalizedSecretKey clickSecretKey : secretRing.secretKeyIterator()) { try { boolean unlocked = clickSecretKey.unlock(passphrase); if (unlocked) { -- cgit v1.2.3 From 33172d598da62ab1d529facf22660b4e1700c2fa Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 17:09:20 +0200 Subject: couple of logging fixes --- .../java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java | 5 +++-- .../sufficientlysecure/keychain/service/OperationResultParcel.java | 3 +-- .../keychain/ui/dialog/PassphraseDialogFragment.java | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index fef15db1e..ce8bdb38b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -257,7 +257,8 @@ public class PgpKeyOperation { * 6. If requested, change passphrase */ - log.add(LogLevel.START, LogType.MSG_MF, indent); + log.add(LogLevel.START, LogType.MSG_MF, indent, + PgpKeyHelper.convertKeyIdToHex(wsKR.getMasterKeyId())); indent += 1; updateProgress(R.string.progress_building_key, 0, 100); @@ -359,7 +360,7 @@ public class PgpKeyOperation { // 2b. Add revocations for revoked user ids for (String userId : saveParcel.mRevokeUserIds) { - log.add(LogLevel.INFO, LogType.MSG_MF_UID_REVOKE, indent); + log.add(LogLevel.INFO, LogType.MSG_MF_UID_REVOKE, indent, userId); // a duplicate revocation will be removed during canonicalization, so no need to // take care of that here. PGPSignature cert = generateRevocationSignature(masterPrivateKey, diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java index 99cafd3f9..c27b3f6da 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResultParcel.java @@ -83,6 +83,7 @@ public class OperationResultParcel implements Parcelable { mType = type; mParameters = parameters; mIndent = indent; + Log.v(Constants.TAG, "log: " + this.toString()); } public LogEntryParcel(Parcel source) { @@ -407,12 +408,10 @@ public class OperationResultParcel implements Parcelable { /// Simple convenience method public void add(LogLevel level, LogType type, int indent, Object... parameters) { - Log.d(Constants.TAG, type.toString()); mParcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, parameters)); } public void add(LogLevel level, LogType type, int indent) { - Log.d(Constants.TAG, type.toString()); mParcels.add(new OperationResultParcel.LogEntryParcel(level, type, indent, (Object[]) null)); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java index cce8c9d9f..ef2659cf8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/dialog/PassphraseDialogFragment.java @@ -33,8 +33,6 @@ import android.support.v4.app.FragmentActivity; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager.LayoutParams; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.Button; -- cgit v1.2.3 From cc034a09139ba12e70baba5d8eb45aeb1318097b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 17:56:47 +0200 Subject: Exception handling with keyserver queries --- .../keychain/keyimport/HkpKeyserver.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 44679ba18..7f7d10319 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -22,6 +22,7 @@ import de.measite.minidns.Client; import de.measite.minidns.Question; import de.measite.minidns.Record; import de.measite.minidns.record.SRV; + import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.helper.TlsHelper; import org.sufficientlysecure.keychain.pgp.PgpHelper; @@ -102,8 +103,9 @@ public class HkpKeyserver extends Keyserver { */ public static final Pattern PUB_KEY_LINE = Pattern .compile("pub:([0-9a-fA-F]+):([0-9]+):([0-9]+):([0-9]+):([0-9]*):([rde]*)[ \n\r]*" // pub line - + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines - Pattern.CASE_INSENSITIVE); + + "((uid:([^:]*):([0-9]+):([0-9]*):([rde]*)[ \n\r]*)+)", // one or more uid lines + Pattern.CASE_INSENSITIVE + ); /** * uid:%escaped uid string%:%creationdate%:%expirationdate%:%flags% @@ -241,8 +243,10 @@ public class HkpKeyserver extends Keyserver { data = query(request); } catch (HttpError e) { if (e.getCode() == 404) { - return results; - } else { + throw new QueryFailedException("keyserver '" + mHost + "' not found. Error 404"); + } else if (e.getData() != null) { + Log.d(Constants.TAG, "returned error data: " + e.getData().toLowerCase(Locale.US)); + if (e.getData().toLowerCase(Locale.US).contains("no keys found")) { return results; } else if (e.getData().toLowerCase(Locale.US).contains("too many")) { @@ -250,7 +254,11 @@ public class HkpKeyserver extends Keyserver { } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { throw new QueryTooShortException(); } + + // NOTE: some keyserver do not provide a more detailed error response + throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); } + throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); } @@ -291,7 +299,7 @@ public class HkpKeyserver extends Keyserver { while (uidMatcher.find()) { String tmp = uidMatcher.group(1).trim(); if (tmp.contains("%")) { - if(tmp.contains("%%")) { + if (tmp.contains("%%")) { // This is a fix for issue #683 // The server encodes a percent sign as %%, so it is swapped out with its // urlencoded counterpart to prevent errors -- cgit v1.2.3 From aa32c60a0aca4ad35106bb98a214fc724ab090cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 18:07:11 +0200 Subject: Even better Exception handling with keyserver queries --- .../keychain/keyimport/HkpKeyserver.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 7f7d10319..f35ef45a2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -217,7 +217,7 @@ public class HkpKeyserver extends Keyserver { throw new HttpError(response, data); } } catch (IOException e) { - throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); + throw new QueryFailedException("Keyserver '" + mHost + "' is unavailable. Check your Internet connection!"); } } @@ -242,24 +242,26 @@ public class HkpKeyserver extends Keyserver { try { data = query(request); } catch (HttpError e) { - if (e.getCode() == 404) { - throw new QueryFailedException("keyserver '" + mHost + "' not found. Error 404"); - } else if (e.getData() != null) { + if (e.getData() != null) { Log.d(Constants.TAG, "returned error data: " + e.getData().toLowerCase(Locale.US)); if (e.getData().toLowerCase(Locale.US).contains("no keys found")) { + // NOTE: This is also a 404 error for some keyservers! return results; } else if (e.getData().toLowerCase(Locale.US).contains("too many")) { throw new TooManyResponsesException(); } else if (e.getData().toLowerCase(Locale.US).contains("insufficient")) { throw new QueryTooShortException(); + } else if (e.getCode() == 404) { + // NOTE: handle this 404 at last, maybe it was a "no keys found" error + throw new QueryFailedException("Keyserver '" + mHost + "' not found. Error 404"); + } else { + // NOTE: some keyserver do not provide a more detailed error response + throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); } - - // NOTE: some keyserver do not provide a more detailed error response - throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); } - throw new QueryFailedException("querying server(s) for '" + mHost + "' failed"); + throw new QueryFailedException("Querying server(s) for '" + mHost + "' failed."); } final Matcher matcher = PUB_KEY_LINE.matcher(data); -- cgit v1.2.3 From ecb2c2c2b148ec1852cb5c59f2a23449f0488b1a Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 18:25:20 +0200 Subject: reduce memory usage while parsing multiple keyrings from a stream --- .../keychain/pgp/UncachedKeyRing.java | 91 +++++++++++++++------- .../keychain/ui/adapter/ImportKeysListLoader.java | 10 ++- 2 files changed, 71 insertions(+), 30 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 d8a2532f3..e7229ffbd 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -1,7 +1,6 @@ package org.sufficientlysecure.keychain.pgp; import org.spongycastle.bcpg.ArmoredOutputStream; -import org.spongycastle.bcpg.S2K; import org.spongycastle.bcpg.SignatureSubpacketTags; import org.spongycastle.bcpg.sig.KeyFlags; import org.spongycastle.openpgp.PGPKeyFlags; @@ -30,12 +29,9 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.Comparator; import java.util.Date; -import java.util.HashSet; import java.util.Iterator; -import java.util.List; import java.util.Set; import java.util.TreeSet; -import java.util.Vector; /** Wrapper around PGPKeyRing class, to be constructed from bytes. * @@ -108,41 +104,84 @@ public class UncachedKeyRing { public static UncachedKeyRing decodeFromData(byte[] data) throws PgpGeneralException, IOException { - List parsed = fromStream(new ByteArrayInputStream(data)); + Iterator parsed = fromStream(new ByteArrayInputStream(data)); - if (parsed.isEmpty()) { + if ( ! parsed.hasNext()) { throw new PgpGeneralException("Object not recognized as PGPKeyRing!"); } - if (parsed.size() > 1) { - throw new PgpGeneralException( - "Expected single keyring in stream, found " + parsed.size()); + + UncachedKeyRing ring = parsed.next(); + + if (parsed.hasNext()) { + throw new PgpGeneralException("Expected single keyring in stream, found at least two"); } - return parsed.get(0); + return ring; } - public static List fromStream(InputStream stream) throws IOException { - List result = new Vector(); + public static Iterator fromStream(final InputStream stream) throws IOException { - while(stream.available() > 0) { - PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); + return new Iterator() { - // go through all objects in this block - Object obj; - while ((obj = objectFactory.nextObject()) != null) { - Log.d(Constants.TAG, "Found class: " + obj.getClass()); - if (!(obj instanceof PGPKeyRing)) { - Log.d(Constants.TAG, - "Bad object of type " + obj.getClass().getName() + " in stream, proceed with next object..."); - // skip object - continue; + UncachedKeyRing mNext = null; + PGPObjectFactory mObjectFactory = null; + + private void cacheNext() { + if (mNext != null) { + return; + } + + try { + if (mObjectFactory == null) { + if (stream.available() == 0) { + // end of stream. that's fine + return; + } + mObjectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); + } + + // go through all objects in this block + Object obj; + while ((obj = mObjectFactory.nextObject()) != null) { + Log.d(Constants.TAG, "Found class: " + obj.getClass()); + if (!(obj instanceof PGPKeyRing)) { + Log.i(Constants.TAG, + "Skipping object of bad type " + obj.getClass().getName() + " in stream"); + // skip object + continue; + } + mNext = new UncachedKeyRing((PGPKeyRing) obj); + return; + } + } catch (IOException e) { + Log.e(Constants.TAG, "IOException while processing stream", e); } - result.add(new UncachedKeyRing((PGPKeyRing) obj)); + } - } - return result; + @Override + public boolean hasNext() { + cacheNext(); + return mNext != null; + } + + @Override + public UncachedKeyRing next() { + try { + cacheNext(); + return mNext; + } finally { + mNext = null; + } + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } public void encodeArmored(OutputStream out, String version) throws IOException { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java index 9996e0381..4971c535c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysListLoader.java @@ -32,6 +32,7 @@ import org.sufficientlysecure.keychain.util.PositionAwareInputStream; import java.io.BufferedInputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; public class ImportKeysListLoader @@ -127,11 +128,12 @@ public class ImportKeysListLoader BufferedInputStream bufferedInput = new BufferedInputStream(progressIn); try { // parse all keyrings - List rings = UncachedKeyRing.fromStream(bufferedInput); - for (UncachedKeyRing key : rings) { - ImportKeysListEntry item = new ImportKeysListEntry(getContext(), key); + Iterator it = UncachedKeyRing.fromStream(bufferedInput); + while (it.hasNext()) { + UncachedKeyRing ring = it.next(); + ImportKeysListEntry item = new ImportKeysListEntry(getContext(), ring); mData.add(item); - mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(key.getEncoded())); + mParcelableRings.put(item.hashCode(), new ParcelableKeyRing(ring.getEncoded())); } } catch (IOException e) { Log.e(Constants.TAG, "IOException on parsing key file! Return NoValidKeysException!", e); -- cgit v1.2.3 From b052d5e89cb323f528d568373605adaab03cb4a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 18:42:54 +0200 Subject: Smaller notes --- .../org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index f35ef45a2..8ff133164 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -221,6 +221,14 @@ public class HkpKeyserver extends Keyserver { } } + /** + * Results are sorted by creation date of key! + * + * @param query + * @return + * @throws QueryFailedException + * @throws QueryNeedsRepairException + */ @Override public ArrayList search(String query) throws QueryFailedException, QueryNeedsRepairException { -- cgit v1.2.3 From 80f9c769c05485c517036e602a8c83901c63e733 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 19:05:09 +0200 Subject: Keyserver exception translateable --- .../org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java | 2 +- .../java/org/sufficientlysecure/keychain/keyimport/Keyserver.java | 7 +++++++ .../org/sufficientlysecure/keychain/service/OperationResults.java | 2 +- .../org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java | 6 ++++-- 4 files changed, 13 insertions(+), 4 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java index 8ff133164..43bed8397 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/HkpKeyserver.java @@ -265,7 +265,7 @@ public class HkpKeyserver extends Keyserver { throw new QueryFailedException("Keyserver '" + mHost + "' not found. Error 404"); } else { // NOTE: some keyserver do not provide a more detailed error response - throw new QueryFailedException("Either no keys or too many have been found. Please improve your query!"); + throw new QueryTooShortOrTooManyResponsesException(); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java index 842e7d922..b726529f8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/Keyserver.java @@ -44,6 +44,13 @@ public abstract class Keyserver { private static final long serialVersionUID = 2703768928624654514L; } + /** + * query too short _or_ too many responses + */ + public static class QueryTooShortOrTooManyResponsesException extends QueryNeedsRepairException { + private static final long serialVersionUID = 2703768928624654514L; + } + public static class AddKeyException extends Exception { private static final long serialVersionUID = -507574859137295530L; } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index a05b77ea5..d5761b030 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -107,7 +107,7 @@ public abstract class OperationResults { if (this.isOkBoth()) { str = activity.getResources().getQuantityString( R.plurals.import_keys_added_and_updated_1, mNewKeys, mNewKeys); - str += activity.getResources().getQuantityString( + str += " "+ activity.getResources().getQuantityString( R.plurals.import_keys_added_and_updated_2, mUpdatedKeys, mUpdatedKeys, withWarnings); } else if (isOkUpdated()) { str = activity.getResources().getQuantityString( diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java index 1617a84e4..fde0f5f23 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysListFragment.java @@ -307,9 +307,11 @@ public class ImportKeysListFragment extends ListFragment implements if (error == null) { // No error } else if (error instanceof Keyserver.QueryTooShortException) { - Notify.showNotify(getActivity(), R.string.error_keyserver_insufficient_query, Notify.Style.ERROR); + Notify.showNotify(getActivity(), R.string.error_query_too_short, Notify.Style.ERROR); } else if (error instanceof Keyserver.TooManyResponsesException) { - Notify.showNotify(getActivity(), R.string.error_keyserver_too_many_responses, Notify.Style.ERROR); + Notify.showNotify(getActivity(), R.string.error_too_many_responses, Notify.Style.ERROR); + } else if (error instanceof Keyserver.QueryTooShortOrTooManyResponsesException) { + Notify.showNotify(getActivity(), R.string.error_too_short_or_too_many_responses, Notify.Style.ERROR); } else if (error instanceof Keyserver.QueryFailedException) { Log.d(Constants.TAG, "Unrecoverable keyserver query error: " + error.getLocalizedMessage()); -- cgit v1.2.3 From 279ddf7cbeca1d09840d0551b7a50e1e92e056ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 19:19:11 +0200 Subject: Handle result in create key --- .../sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index c083539da..c2c983b95 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -152,8 +152,13 @@ public class CreateKeyFinalFragment extends Fragment { } if (mUploadCheckbox.isChecked()) { - // result will be displayed after upload - uploadKey(result); + if (result.getResult() == OperationResultParcel.RESULT_OK) { + // result will be displayed after upload + uploadKey(result); + } else { + // display result on error without finishing activity + result.createNotify(getActivity()); + } } else { // TODO: return result result.createNotify(getActivity()); -- cgit v1.2.3 From b40081c364210f90adf6ce38948056bbdaba3ea1 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 19:26:15 +0200 Subject: always return an EditKeyResult in modifyKey method --- .../keychain/pgp/PgpKeyOperation.java | 50 +++++++++++----------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index ce8bdb38b..a0b31bed9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -177,30 +177,30 @@ public class PgpKeyOperation { if (saveParcel.mAddSubKeys.isEmpty()) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_MASTER, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } if (saveParcel.mAddUserIds.isEmpty()) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_USER_ID, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } SubkeyAdd add = saveParcel.mAddSubKeys.remove(0); if ((add.mFlags & KeyFlags.CERTIFY_OTHER) != KeyFlags.CERTIFY_OTHER) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_CERTIFY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } if (add.mAlgorithm == Constants.choice.algorithm.elgamal) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_MASTER_ELGAMAL, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); // return null if this failed (an error will already have been logged by createKey) if (keyPair == null) { - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // define hashing and signing algos @@ -221,10 +221,10 @@ public class PgpKeyOperation { } catch (PGPException e) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_INTERNAL_PGP, indent); Log.e(Constants.TAG, "pgp error encoding key", e); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } catch (IOException e) { Log.e(Constants.TAG, "io error encoding key", e); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } } @@ -265,7 +265,7 @@ public class PgpKeyOperation { // Make sure this is called with a proper SaveKeyringParcel if (saveParcel.mMasterKeyId == null || saveParcel.mMasterKeyId != wsKR.getMasterKeyId()) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_KEYID, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // We work on bouncycastle object level here @@ -277,7 +277,7 @@ public class PgpKeyOperation { || !Arrays.equals(saveParcel.mFingerprint, masterSecretKey.getPublicKey().getFingerprint())) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_FINGERPRINT, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // read masterKeyFlags, and use the same as before. @@ -309,7 +309,7 @@ public class PgpKeyOperation { masterPrivateKey = masterSecretKey.extractPrivateKey(keyDecryptor); } catch (PGPException e) { log.add(LogLevel.ERROR, LogType.MSG_MF_UNLOCK_ERROR, indent + 1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } } @@ -324,7 +324,7 @@ public class PgpKeyOperation { if (userId.equals("")) { log.add(LogLevel.ERROR, LogType.MSG_MF_UID_ERROR_EMPTY, indent+1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // this operation supersedes all previous binding and revocation certificates, @@ -336,7 +336,7 @@ public class PgpKeyOperation { if (cert.getKeyID() != masterPublicKey.getKeyID()) { // foreign certificate?! error error error log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_INTEGRITY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } if (cert.getSignatureType() == PGPSignature.CERTIFICATION_REVOCATION || cert.getSignatureType() == PGPSignature.NO_CERTIFICATION @@ -387,7 +387,7 @@ public class PgpKeyOperation { if (cert.getKeyID() != masterPublicKey.getKeyID()) { // foreign certificate?! error error error log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_INTEGRITY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // we know from canonicalization that if there is any revocation here, it // is valid and not superseded by a newer certification. @@ -408,7 +408,7 @@ public class PgpKeyOperation { if (currentCert == null) { // no certificate found?! error error error log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_INTEGRITY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // we definitely should not update certifications of revoked keys, so just leave it. @@ -416,7 +416,7 @@ public class PgpKeyOperation { // revoked user ids cannot be primary! if (userId.equals(saveParcel.mChangePrimaryUserId)) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_REVOKED_PRIMARY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } continue; } @@ -463,7 +463,7 @@ public class PgpKeyOperation { if (!ok) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_NOEXIST_PRIMARY, indent); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } } @@ -482,14 +482,14 @@ public class PgpKeyOperation { // TODO allow changes in master key? this implies generating new user id certs... if (change.mKeyId == masterPublicKey.getKeyID()) { Log.e(Constants.TAG, "changing the master key not supported"); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } PGPSecretKey sKey = sKR.getSecretKey(change.mKeyId); if (sKey == null) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_MISSING, indent + 1, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } PGPPublicKey pKey = sKey.getPublicKey(); @@ -497,7 +497,7 @@ public class PgpKeyOperation { if (change.mExpiry != null && new Date(change.mExpiry*1000).before(new Date())) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent + 1, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // keep old flags, or replace with new ones @@ -538,7 +538,7 @@ public class PgpKeyOperation { if (sKey == null) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_MISSING, indent+1, PgpKeyHelper.convertKeyIdToHex(revocation)); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } PGPPublicKey pKey = sKey.getPublicKey(); @@ -554,7 +554,7 @@ public class PgpKeyOperation { if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent +1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent); @@ -562,7 +562,7 @@ public class PgpKeyOperation { // generate a new secret key (privkey only for now) PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); if(keyPair == null) { - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } // add subkey binding signature (making this a sub rather than master key) @@ -612,14 +612,14 @@ public class PgpKeyOperation { // This one must only be thrown by } catch (IOException e) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_ENCODE, indent+1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } catch (PGPException e) { Log.e(Constants.TAG, "encountered pgp error while modifying key", e); log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_PGP, indent+1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } catch (SignatureException e) { log.add(LogLevel.ERROR, LogType.MSG_MF_ERROR_SIG, indent+1); - return null; + return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } log.add(LogLevel.OK, LogType.MSG_MF_SUCCESS, indent); -- cgit v1.2.3 From acb5a70e445351f245ef3ffa19e69315f8548ec7 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 19:27:01 +0200 Subject: fix fromStream method, properly recognize multiple concatenated streams --- .../keychain/pgp/UncachedKeyRing.java | 37 ++++++++++++---------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'OpenKeychain/src/main/java/org') 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 e7229ffbd..502e3a70c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/UncachedKeyRing.java @@ -27,9 +27,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.Iterator; +import java.util.List; import java.util.Set; import java.util.TreeSet; @@ -133,26 +135,27 @@ public class UncachedKeyRing { } try { - if (mObjectFactory == null) { - if (stream.available() == 0) { - // end of stream. that's fine - return; + while(stream.available() > 0) { + // if there are no objects left from the last factory, create a new one + if (mObjectFactory == null) { + mObjectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); } - mObjectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(stream)); - } - // go through all objects in this block - Object obj; - while ((obj = mObjectFactory.nextObject()) != null) { - Log.d(Constants.TAG, "Found class: " + obj.getClass()); - if (!(obj instanceof PGPKeyRing)) { - Log.i(Constants.TAG, - "Skipping object of bad type " + obj.getClass().getName() + " in stream"); - // skip object - continue; + // go through all objects in this block + Object obj; + while ((obj = mObjectFactory.nextObject()) != null) { + Log.d(Constants.TAG, "Found class: " + obj.getClass()); + if (!(obj instanceof PGPKeyRing)) { + Log.i(Constants.TAG, + "Skipping object of bad type " + obj.getClass().getName() + " in stream"); + // skip object + continue; + } + mNext = new UncachedKeyRing((PGPKeyRing) obj); + return; } - mNext = new UncachedKeyRing((PGPKeyRing) obj); - return; + // if we are past the while loop, that means the objectFactory had no next + mObjectFactory = null; } } catch (IOException e) { Log.e(Constants.TAG, "IOException while processing stream", e); -- cgit v1.2.3 From d48e980946c390e81c6838a68647549605cee922 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 19:27:15 +0200 Subject: avoid nullpointerexception in EditKeyResult --- .../org/sufficientlysecure/keychain/service/OperationResults.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java index d5761b030..11829e7b8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/OperationResults.java @@ -168,13 +168,13 @@ public abstract class OperationResults { public static class EditKeyResult extends OperationResultParcel { private transient UncachedKeyRing mRing; - public final long mRingMasterKeyId; + public final Long mRingMasterKeyId; public EditKeyResult(int result, OperationLog log, UncachedKeyRing ring) { super(result, log); mRing = ring; - mRingMasterKeyId = ring.getMasterKeyId(); + mRingMasterKeyId = ring != null ? ring.getMasterKeyId() : null; } public UncachedKeyRing getRing() { -- cgit v1.2.3 From 7bbe869c88c445b087e32a75572cf18efa2165b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 20:38:06 +0200 Subject: Parcelable data over 1MB can not be send through binder, parcel into a cache file, fix #592 --- .../keychain/keyimport/FileImportCache.java | 101 +++++++++++++++++++++ .../keychain/service/KeychainIntentService.java | 13 ++- .../keychain/ui/ImportKeysActivity.java | 30 ++++-- 3 files changed, 128 insertions(+), 16 deletions(-) create mode 100644 OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java new file mode 100644 index 000000000..ff391af1a --- /dev/null +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2014 Dominik Schürmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sufficientlysecure.keychain.keyimport; + +import android.content.Context; +import android.os.Bundle; +import android.os.Parcel; + +import org.sufficientlysecure.keychain.Constants; +import org.sufficientlysecure.keychain.KeychainApplication; +import org.sufficientlysecure.keychain.util.Log; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * When sending large data (over 1MB) through Androids Binder IPC you get + * JavaBinder E !!! FAILED BINDER TRANSACTION !!! + *

+ * To overcome this problem, we cache large Parcelables into a file in our private cache directory + * instead of sending them through IPC. + */ +public class FileImportCache { + + private Context mContext; + + private static final String FILENAME = "key_import.pcl"; + private static final String BUNDLE_DATA = "data"; + + public FileImportCache(Context context) { + this.mContext = context; + } + + public void writeCache(ArrayList selectedEntries) throws IOException { + Bundle in = new Bundle(); + in.putParcelableArrayList(BUNDLE_DATA, selectedEntries); + File cacheDir = mContext.getCacheDir(); + if (cacheDir == null) { + // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU + throw new IOException("cache dir is null!"); + } + File tempFile = new File(mContext.getCacheDir(), FILENAME); + + FileOutputStream fos = new FileOutputStream(tempFile); + Parcel p = Parcel.obtain(); // creating empty parcel object + in.writeToParcel(p, 0); // saving bundle as parcel + fos.write(p.marshall()); // writing parcel to file + fos.flush(); + fos.close(); + } + + public List readCache() throws IOException { + Parcel parcel = Parcel.obtain(); // creating empty parcel object + Bundle out; + File cacheDir = mContext.getCacheDir(); + if (cacheDir == null) { + // https://groups.google.com/forum/#!topic/android-developers/-694j87eXVU + throw new IOException("cache dir is null!"); + } + + File tempFile = new File(cacheDir, FILENAME); + try { + + FileInputStream fis = new FileInputStream(tempFile); + byte[] array = new byte[(int) fis.getChannel().size()]; + fis.read(array, 0, array.length); + fis.close(); + + parcel.unmarshall(array, 0, array.length); + parcel.setDataPosition(0); + out = parcel.readBundle(KeychainApplication.class.getClassLoader()); + out.putAll(out); + + return out.getParcelableArrayList(BUNDLE_DATA); + } finally { + parcel.recycle(); + // delete temp file + tempFile.delete(); + } + } +} diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index f69961df6..77598e2b9 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -31,11 +31,13 @@ import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.FileHelper; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.keyimport.FileImportCache; import org.sufficientlysecure.keychain.keyimport.HkpKeyserver; -import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.KeybaseKeyserver; +import org.sufficientlysecure.keychain.keyimport.Keyserver; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; +import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKey; import org.sufficientlysecure.keychain.pgp.CanonicalizedSecretKeyRing; import org.sufficientlysecure.keychain.pgp.PgpDecryptVerify; @@ -46,7 +48,6 @@ import org.sufficientlysecure.keychain.pgp.PgpKeyOperation; import org.sufficientlysecure.keychain.pgp.PgpSignEncrypt; import org.sufficientlysecure.keychain.pgp.Progressable; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; -import org.sufficientlysecure.keychain.pgp.CanonicalizedPublicKeyRing; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralException; import org.sufficientlysecure.keychain.pgp.exception.PgpGeneralMsgIdException; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; @@ -134,9 +135,6 @@ public class KeychainIntentService extends IntentService // delete file securely public static final String DELETE_FILE = "deleteFile"; - // import key - public static final String IMPORT_KEY_LIST = "import_key_list"; - // export key public static final String EXPORT_OUTPUT_STREAM = "export_output_stream"; public static final String EXPORT_FILENAME = "export_filename"; @@ -386,7 +384,9 @@ public class KeychainIntentService extends IntentService } } else if (ACTION_IMPORT_KEYRING.equals(action)) { try { - List entries = data.getParcelableArrayList(IMPORT_KEY_LIST); + // get entries from cached file + FileImportCache cache = new FileImportCache(this); + List entries = cache.readCache(); PgpImportExport pgpImportExport = new PgpImportExport(this, this); ImportKeyResult result = pgpImportExport.importKeyRings(entries); @@ -515,7 +515,6 @@ public class KeychainIntentService extends IntentService Intent importIntent = new Intent(this, KeychainIntentService.class); importIntent.setAction(ACTION_IMPORT_KEYRING); Bundle importData = new Bundle(); - importData.putParcelableArrayList(IMPORT_KEY_LIST, keyRings); importIntent.putExtra(EXTRA_DATA, importData); importIntent.putExtra(EXTRA_MESSENGER, mMessenger); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 5f340019f..524fe2ef2 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -40,6 +40,7 @@ import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.helper.OtherHelper; import org.sufficientlysecure.keychain.helper.Preferences; +import org.sufficientlysecure.keychain.keyimport.FileImportCache; import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; @@ -51,6 +52,7 @@ import org.sufficientlysecure.keychain.ui.widget.SlidingTabLayout; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Notify; +import java.io.IOException; import java.util.ArrayList; import java.util.Locale; @@ -469,19 +471,29 @@ public class ImportKeysActivity extends ActionBarActivity { // get DATA from selected key entries ArrayList selectedEntries = mListFragment.getSelectedData(); - data.putParcelableArrayList(KeychainIntentService.IMPORT_KEY_LIST, selectedEntries); - intent.putExtra(KeychainIntentService.EXTRA_DATA, data); + // instead of given the entries by Intent extra, cache them into a file + // to prevent Java Binder problems on heavy imports + // read FileImportCache for more info. + try { + FileImportCache cache = new FileImportCache(this); + cache.writeCache(selectedEntries); - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); - // show progress dialog - saveHandler.showProgressDialog(this); + // Create a new Messenger for the communication back + Messenger messenger = new Messenger(saveHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); - // start service with intent - startService(intent); + // show progress dialog + saveHandler.showProgressDialog(this); + + // start service with intent + startService(intent); + } catch (IOException e) { + Log.e(Constants.TAG, "Problem writing cache file", e); + Notify.showNotify(this, "Problem writing cache file!", Notify.Style.ERROR); + } } else if (ls instanceof ImportKeysListFragment.KeyserverLoaderState) { ImportKeysListFragment.KeyserverLoaderState sls = (ImportKeysListFragment.KeyserverLoaderState) ls; -- cgit v1.2.3 From 58c2ca6eb814fa5384a7342b249fdb48b52af07d Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 31 Jul 2014 20:59:40 +0200 Subject: completely overengineer progress indication in {modify,create}SecretKeyRing methods --- .../keychain/pgp/PgpKeyOperation.java | 96 ++++++++++++++++++---- .../keychain/service/KeychainIntentService.java | 4 +- .../keychain/util/ProgressScaler.java | 4 +- 3 files changed, 85 insertions(+), 19 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java index a0b31bed9..b1bd6a77a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyOperation.java @@ -56,6 +56,7 @@ import org.sufficientlysecure.keychain.service.SaveKeyringParcel.SubkeyAdd; import org.sufficientlysecure.keychain.util.IterableIterator; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Primes; +import org.sufficientlysecure.keychain.util.ProgressScaler; import java.io.IOException; import java.math.BigInteger; @@ -68,6 +69,7 @@ import java.security.SignatureException; import java.util.Arrays; import java.util.Date; import java.util.Iterator; +import java.util.Stack; /** * This class is the single place where ALL operations that actually modify a PGP public or secret @@ -79,7 +81,7 @@ import java.util.Iterator; * This indicator may be null. */ public class PgpKeyOperation { - private Progressable mProgress; + private Stack mProgress; private static final int[] PREFERRED_SYMMETRIC_ALGORITHMS = new int[]{ SymmetricKeyAlgorithmTags.AES_256, SymmetricKeyAlgorithmTags.AES_192, @@ -93,13 +95,34 @@ public class PgpKeyOperation { public PgpKeyOperation(Progressable progress) { super(); - this.mProgress = progress; + if (progress != null) { + mProgress = new Stack(); + mProgress.push(progress); + } } - void updateProgress(int message, int current, int total) { - if (mProgress != null) { - mProgress.setProgress(message, current, total); + private void subProgressPush(int from, int to) { + if (mProgress == null) { + return; } + mProgress.push(new ProgressScaler(mProgress.peek(), from, to, 100)); + } + private void subProgressPop() { + if (mProgress == null) { + return; + } + if (mProgress.size() == 1) { + throw new RuntimeException("Tried to pop progressable without prior push! " + + "This is a programming error, please file a bug report."); + } + mProgress.pop(); + } + + private void progress(int message, int current) { + if (mProgress == null) { + return; + } + mProgress.peek().setProgress(message, current, 100); } /** Creates new secret key. */ @@ -116,6 +139,7 @@ public class PgpKeyOperation { switch (algorithmChoice) { case Constants.choice.algorithm.dsa: { + progress(R.string.progress_generating_dsa, 30); keyGen = KeyPairGenerator.getInstance("DSA", Constants.BOUNCY_CASTLE_PROVIDER_NAME); keyGen.initialize(keySize, new SecureRandom()); algorithm = PGPPublicKey.DSA; @@ -123,6 +147,7 @@ public class PgpKeyOperation { } case Constants.choice.algorithm.elgamal: { + progress(R.string.progress_generating_elgamal, 30); keyGen = KeyPairGenerator.getInstance("ElGamal", Constants.BOUNCY_CASTLE_PROVIDER_NAME); BigInteger p = Primes.getBestPrime(keySize); BigInteger g = new BigInteger("2"); @@ -135,6 +160,7 @@ public class PgpKeyOperation { } case Constants.choice.algorithm.rsa: { + progress(R.string.progress_generating_rsa, 30); keyGen = KeyPairGenerator.getInstance("RSA", Constants.BOUNCY_CASTLE_PROVIDER_NAME); keyGen.initialize(keySize, new SecureRandom()); @@ -172,8 +198,8 @@ public class PgpKeyOperation { try { log.add(LogLevel.START, LogType.MSG_CR, indent); + progress(R.string.progress_building_key, 0); indent += 1; - updateProgress(R.string.progress_building_key, 0, 100); if (saveParcel.mAddSubKeys.isEmpty()) { log.add(LogLevel.ERROR, LogType.MSG_CR_ERROR_NO_MASTER, indent); @@ -196,13 +222,17 @@ public class PgpKeyOperation { return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } + subProgressPush(10, 30); PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); + subProgressPop(); // return null if this failed (an error will already have been logged by createKey) if (keyPair == null) { return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } + progress(R.string.progress_building_master_key, 40); + // define hashing and signing algos PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder() .build().get(HashAlgorithmTags.SHA1); @@ -216,6 +246,7 @@ public class PgpKeyOperation { PGPSecretKeyRing sKR = new PGPSecretKeyRing( masterSecretKey.getEncoded(), new JcaKeyFingerprintCalculator()); + subProgressPush(50, 100); return internal(sKR, masterSecretKey, add.mFlags, saveParcel, "", log); } catch (PGPException e) { @@ -260,7 +291,7 @@ public class PgpKeyOperation { log.add(LogLevel.START, LogType.MSG_MF, indent, PgpKeyHelper.convertKeyIdToHex(wsKR.getMasterKeyId())); indent += 1; - updateProgress(R.string.progress_building_key, 0, 100); + progress(R.string.progress_building_key, 0); // Make sure this is called with a proper SaveKeyringParcel if (saveParcel.mMasterKeyId == null || saveParcel.mMasterKeyId != wsKR.getMasterKeyId()) { @@ -295,11 +326,12 @@ public class PgpKeyOperation { int indent = 1; - updateProgress(R.string.progress_certifying_master_key, 20, 100); + progress(R.string.progress_modify, 0); PGPPublicKey masterPublicKey = masterSecretKey.getPublicKey(); // 1. Unlock private key + progress(R.string.progress_modify_unlock, 10); log.add(LogLevel.DEBUG, LogType.MSG_MF_UNLOCK, indent); PGPPrivateKey masterPrivateKey; { @@ -319,7 +351,11 @@ public class PgpKeyOperation { PGPPublicKey modifiedPublicKey = masterPublicKey; // 2a. Add certificates for new user ids - for (String userId : saveParcel.mAddUserIds) { + subProgressPush(15, 25); + for (int i = 0; i < saveParcel.mAddUserIds.size(); i++) { + + progress(R.string.progress_modify_adduid, (i-1) * (100 / saveParcel.mAddSubKeys.size())); + String userId = saveParcel.mAddUserIds.get(i); log.add(LogLevel.INFO, LogType.MSG_MF_UID_ADD, indent); if (userId.equals("")) { @@ -357,19 +393,27 @@ public class PgpKeyOperation { masterPublicKey, userId, isPrimary, masterKeyFlags); modifiedPublicKey = PGPPublicKey.addCertification(modifiedPublicKey, userId, cert); } + subProgressPop(); // 2b. Add revocations for revoked user ids - for (String userId : saveParcel.mRevokeUserIds) { + subProgressPush(25, 40); + for (int i = 0; i < saveParcel.mRevokeUserIds.size(); i++) { + + progress(R.string.progress_modify_revokeuid, (i-1) * (100 / saveParcel.mAddSubKeys.size())); + String userId = saveParcel.mRevokeUserIds.get(i); log.add(LogLevel.INFO, LogType.MSG_MF_UID_REVOKE, indent, userId); + // a duplicate revocation will be removed during canonicalization, so no need to // take care of that here. PGPSignature cert = generateRevocationSignature(masterPrivateKey, masterPublicKey, userId); modifiedPublicKey = PGPPublicKey.addCertification(modifiedPublicKey, userId, cert); } + subProgressPop(); // 3. If primary user id changed, generate new certificates for both old and new if (saveParcel.mChangePrimaryUserId != null) { + progress(R.string.progress_modify_primaryuid, 40); // keep track if we actually changed one boolean ok = false; @@ -475,7 +519,11 @@ public class PgpKeyOperation { } // 4a. For each subkey change, generate new subkey binding certificate - for (SaveKeyringParcel.SubkeyChange change : saveParcel.mChangeSubKeys) { + subProgressPush(50, 60); + for (int i = 0; i < saveParcel.mChangeSubKeys.size(); i++) { + + progress(R.string.progress_modify_subkeychange, (i-1) * (100 / saveParcel.mAddSubKeys.size())); + SaveKeyringParcel.SubkeyChange change = saveParcel.mChangeSubKeys.get(i); log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_CHANGE, indent, PgpKeyHelper.convertKeyIdToHex(change.mKeyId)); @@ -529,11 +577,17 @@ public class PgpKeyOperation { pKey = PGPPublicKey.addCertification(pKey, sig); sKR = PGPSecretKeyRing.insertSecretKey(sKR, PGPSecretKey.replacePublicKey(sKey, pKey)); } + subProgressPop(); // 4b. For each subkey revocation, generate new subkey revocation certificate - for (long revocation : saveParcel.mRevokeSubKeys) { + subProgressPush(60, 70); + for (int i = 0; i < saveParcel.mRevokeSubKeys.size(); i++) { + + progress(R.string.progress_modify_subkeyrevoke, (i-1) * (100 / saveParcel.mAddSubKeys.size())); + long revocation = saveParcel.mRevokeSubKeys.get(i); log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_REVOKE, indent, PgpKeyHelper.convertKeyIdToHex(revocation)); + PGPSecretKey sKey = sKR.getSecretKey(revocation); if (sKey == null) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_MISSING, @@ -548,19 +602,28 @@ public class PgpKeyOperation { pKey = PGPPublicKey.addCertification(pKey, sig); sKR = PGPSecretKeyRing.insertSecretKey(sKR, PGPSecretKey.replacePublicKey(sKey, pKey)); } + subProgressPop(); // 5. Generate and add new subkeys - for (SaveKeyringParcel.SubkeyAdd add : saveParcel.mAddSubKeys) { + subProgressPush(70, 90); + for (int i = 0; i < saveParcel.mAddSubKeys.size(); i++) { + + progress(R.string.progress_modify_subkeyadd, (i-1) * (100 / saveParcel.mAddSubKeys.size())); + SaveKeyringParcel.SubkeyAdd add = saveParcel.mAddSubKeys.get(0); + log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent); if (add.mExpiry != null && new Date(add.mExpiry*1000).before(new Date())) { log.add(LogLevel.ERROR, LogType.MSG_MF_SUBKEY_PAST_EXPIRY, indent +1); return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } - log.add(LogLevel.INFO, LogType.MSG_MF_SUBKEY_NEW, indent); - // generate a new secret key (privkey only for now) + subProgressPush( + (i-1) * (100 / saveParcel.mAddSubKeys.size()), + i * (100 / saveParcel.mAddSubKeys.size()) + ); PGPKeyPair keyPair = createKey(add.mAlgorithm, add.mKeysize, log, indent); + subProgressPop(); if(keyPair == null) { return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } @@ -592,9 +655,11 @@ public class PgpKeyOperation { sKR = PGPSecretKeyRing.insertSecretKey(sKR, sKey); } + subProgressPop(); // 6. If requested, change passphrase if (saveParcel.mNewPassphrase != null) { + progress(R.string.progress_modify_passphrase, 90); log.add(LogLevel.INFO, LogType.MSG_MF_PASSPHRASE, indent); PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build() .get(HashAlgorithmTags.SHA1); @@ -622,6 +687,7 @@ public class PgpKeyOperation { return new EditKeyResult(EditKeyResult.RESULT_ERROR, log, null); } + progress(R.string.progress_done, 100); log.add(LogLevel.OK, LogType.MSG_MF_SUCCESS, indent); return new EditKeyResult(OperationResultParcel.RESULT_OK, log, new UncachedKeyRing(sKR)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java index 77598e2b9..57152c36d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/KeychainIntentService.java @@ -330,7 +330,7 @@ public class KeychainIntentService extends IntentService /* Operation */ ProviderHelper providerHelper = new ProviderHelper(this); - PgpKeyOperation keyOperations = new PgpKeyOperation(new ProgressScaler(this, 10, 50, 100)); + PgpKeyOperation keyOperations = new PgpKeyOperation(new ProgressScaler(this, 10, 60, 100)); EditKeyResult result; if (saveParcel.mMasterKeyId != null) { @@ -345,7 +345,7 @@ public class KeychainIntentService extends IntentService UncachedKeyRing ring = result.getRing(); - providerHelper.saveSecretKeyRing(ring, new ProgressScaler(this, 10, 95, 100)); + providerHelper.saveSecretKeyRing(ring, new ProgressScaler(this, 60, 95, 100)); // cache new passphrase if (saveParcel.mNewPassphrase != null) { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java index 869eea03f..95a259336 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/ProgressScaler.java @@ -46,13 +46,13 @@ public class ProgressScaler implements Progressable { public void setProgress(int resourceId, int progress, int max) { if (mWrapped != null) { - mWrapped.setProgress(resourceId, progress, mMax); + mWrapped.setProgress(resourceId, mFrom + progress * (mTo - mFrom) / max, mMax); } } public void setProgress(int progress, int max) { if (mWrapped != null) { - mWrapped.setProgress(progress, max); + mWrapped.setProgress(mFrom + progress * (mTo - mFrom) / max, mMax); } } -- cgit v1.2.3 From 138d5a1d9c1bd4b0fa607c8fbbbed988670bd7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 21:18:24 +0200 Subject: Robots like coffee too... --- .../keychain/keyimport/FileImportCache.java | 4 ---- .../org/sufficientlysecure/keychain/ui/DecryptFragment.java | 12 ++++++------ .../org/sufficientlysecure/keychain/ui/KeyListFragment.java | 7 +++---- .../org/sufficientlysecure/keychain/ui/ViewCertActivity.java | 2 +- .../org/sufficientlysecure/keychain/ui/ViewKeyActivity.java | 8 ++++---- .../keychain/ui/adapter/UserIdsAdapter.java | 4 ++-- 6 files changed, 16 insertions(+), 21 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java index ff391af1a..08b8afae7 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/keyimport/FileImportCache.java @@ -21,13 +21,10 @@ import android.content.Context; import android.os.Bundle; import android.os.Parcel; -import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.KeychainApplication; -import org.sufficientlysecure.keychain.util.Log; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -80,7 +77,6 @@ public class FileImportCache { File tempFile = new File(cacheDir, FILENAME); try { - FileInputStream fis = new FileInputStream(tempFile); byte[] array = new byte[(int) fis.getChannel().size()]; fis.read(array, 0, array.length); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java index 6b8358538..d4235b82b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/DecryptFragment.java @@ -132,7 +132,7 @@ public class DecryptFragment extends Fragment { mResultText.setText(R.string.decrypt_result_decrypted_and_signature_certified); } - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_green)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_green_light)); mSignatureStatusImage.setImageResource(R.drawable.overlay_ok); mSignatureLayout.setVisibility(View.VISIBLE); mLookupKey.setVisibility(View.GONE); @@ -146,7 +146,7 @@ public class DecryptFragment extends Fragment { mResultText.setText(R.string.decrypt_result_decrypted_and_signature_uncertified); } - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_orange)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); mSignatureStatusImage.setImageResource(R.drawable.overlay_ok); mSignatureLayout.setVisibility(View.VISIBLE); mLookupKey.setVisibility(View.GONE); @@ -160,7 +160,7 @@ public class DecryptFragment extends Fragment { mResultText.setText(R.string.decrypt_result_decrypted_unknown_pub_key); } - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_orange)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_orange_light)); mSignatureStatusImage.setImageResource(R.drawable.overlay_error); mSignatureLayout.setVisibility(View.VISIBLE); mLookupKey.setVisibility(View.VISIBLE); @@ -170,7 +170,7 @@ public class DecryptFragment extends Fragment { case OpenPgpSignatureResult.SIGNATURE_ERROR: { mResultText.setText(R.string.decrypt_result_invalid_signature); - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_red)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_red_light)); mSignatureStatusImage.setImageResource(R.drawable.overlay_error); mSignatureLayout.setVisibility(View.GONE); mLookupKey.setVisibility(View.GONE); @@ -180,7 +180,7 @@ public class DecryptFragment extends Fragment { default: { mResultText.setText(R.string.error); - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_red)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_red_light)); mSignatureStatusImage.setImageResource(R.drawable.overlay_error); mSignatureLayout.setVisibility(View.GONE); mLookupKey.setVisibility(View.GONE); @@ -192,7 +192,7 @@ public class DecryptFragment extends Fragment { mLookupKey.setVisibility(View.GONE); // successful decryption-only - mResultLayout.setBackgroundColor(getResources().getColor(R.color.result_purple)); + mResultLayout.setBackgroundColor(getResources().getColor(R.color.android_purple_light)); mResultText.setText(R.string.decrypt_result_decrypted); } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 9f1ac0cdd..b0caaa10c 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -47,7 +47,6 @@ import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView; -import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; @@ -493,19 +492,19 @@ public class KeyListFragment extends LoaderFragment if (isRevoked) { h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); - h.mStatus.setColorFilter(getResources().getColor(R.color.result_red), + h.mStatus.setColorFilter(getResources().getColor(R.color.android_red_light), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); } else if (isExpired) { h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_expired_cutout)); - h.mStatus.setColorFilter(getResources().getColor(R.color.result_orange), + h.mStatus.setColorFilter(getResources().getColor(R.color.android_orange_light), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); } else if (isVerified) { h.mStatus.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_verified_cutout)); - h.mStatus.setColorFilter(getResources().getColor(R.color.result_green), + h.mStatus.setColorFilter(getResources().getColor(R.color.android_green_light), PorterDuff.Mode.SRC_ATOP); h.mStatus.setVisibility(View.VISIBLE); } else { diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java index 37c583920..5201b5df8 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewCertActivity.java @@ -152,7 +152,7 @@ public class ViewCertActivity extends ActionBarActivity sig.init(signerRing.getPublicKey()); if (sig.verifySignature(signeeRing.getPublicKey(), signeeUid)) { mStatus.setText(R.string.cert_verify_ok); - mStatus.setTextColor(getResources().getColor(R.color.result_green)); + mStatus.setTextColor(getResources().getColor(R.color.android_green_light)); } else { mStatus.setText(R.string.cert_verify_failed); mStatus.setTextColor(getResources().getColor(R.color.alert)); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 7a833026b..03e446723 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -516,19 +516,19 @@ public class ViewKeyActivity extends ActionBarActivity implements // Note: order is important if (isRevoked) { mStatusText.setText(R.string.view_key_revoked); - mStatusText.setTextColor(getResources().getColor(R.color.result_red)); + mStatusText.setTextColor(getResources().getColor(R.color.android_red_light)); mStatusImage.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_revoked_cutout)); - mStatusImage.setColorFilter(getResources().getColor(R.color.result_red), + mStatusImage.setColorFilter(getResources().getColor(R.color.android_red_light), PorterDuff.Mode.SRC_ATOP); mStatusDivider.setVisibility(View.VISIBLE); mStatusLayout.setVisibility(View.VISIBLE); } else if (isExpired) { mStatusText.setText(R.string.view_key_expired); - mStatusText.setTextColor(getResources().getColor(R.color.result_orange)); + mStatusText.setTextColor(getResources().getColor(R.color.android_orange_light)); mStatusImage.setImageDrawable( getResources().getDrawable(R.drawable.status_signature_expired_cutout)); - mStatusImage.setColorFilter(getResources().getColor(R.color.result_orange), + mStatusImage.setColorFilter(getResources().getColor(R.color.android_orange_light), PorterDuff.Mode.SRC_ATOP); mStatusDivider.setVisibility(View.VISIBLE); mStatusLayout.setVisibility(View.VISIBLE); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java index 6519915fa..c8b74da2e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/UserIdsAdapter.java @@ -188,7 +188,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC case Certs.VERIFIED_SECRET: vVerified.setImageResource(R.drawable.status_signature_verified_cutout); vVerified.setColorFilter( - mContext.getResources().getColor(R.color.result_green), + mContext.getResources().getColor(R.color.android_green_light), PorterDuff.Mode.SRC_IN); break; case Certs.VERIFIED_SELF: @@ -200,7 +200,7 @@ public class UserIdsAdapter extends CursorAdapter implements AdapterView.OnItemC default: vVerified.setImageResource(R.drawable.status_signature_invalid_cutout); vVerified.setColorFilter( - mContext.getResources().getColor(R.color.result_red), + mContext.getResources().getColor(R.color.android_red_light), PorterDuff.Mode.SRC_IN); break; } -- cgit v1.2.3 From 50aea621ba4de844cf6eee077d1f5b14d0247f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Thu, 31 Jul 2014 21:51:35 +0200 Subject: Fix animations in create key --- .../keychain/ui/CreateKeyActivity.java | 30 ++++++++++++---------- .../keychain/ui/CreateKeyFinalFragment.java | 8 +----- .../keychain/ui/CreateKeyInputFragment.java | 2 +- 3 files changed, 18 insertions(+), 22 deletions(-) (limited to 'OpenKeychain/src/main/java/org') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java index 569ae82f0..534ac5811 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyActivity.java @@ -29,9 +29,9 @@ public class CreateKeyActivity extends ActionBarActivity { public static final String EXTRA_NAME = "name"; public static final String EXTRA_EMAIL = "email"; - public static final int ANIM_NO = 0; - public static final int ANIM_TO_RIGHT = 1; - public static final int ANIM_TO_LEFT = 2; + public static final int FRAG_ACTION_START = 0; + public static final int FRAG_ACTION_TO_RIGHT = 1; + public static final int FRAG_ACTION_TO_LEFT = 2; @Override public void onCreate(Bundle savedInstanceState) { @@ -45,10 +45,10 @@ public class CreateKeyActivity extends ActionBarActivity { getIntent().getStringExtra(EXTRA_NAME), getIntent().getStringExtra(EXTRA_EMAIL) ); - loadFragment(null, frag, ANIM_NO); + loadFragment(null, frag, FRAG_ACTION_START); } - public void loadFragment(Bundle savedInstanceState, Fragment fragment, int animation) { + public void loadFragment(Bundle savedInstanceState, Fragment fragment, int action) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. @@ -60,22 +60,24 @@ public class CreateKeyActivity extends ActionBarActivity { // NOTE: We use commitAllowingStateLoss() to prevent weird crashes! FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - switch (animation) { - case ANIM_NO: + switch (action) { + case FRAG_ACTION_START: transaction.setCustomAnimations(0, 0); + transaction.replace(R.id.create_key_fragment_container, fragment) + .commitAllowingStateLoss(); break; - case ANIM_TO_LEFT: - transaction.setCustomAnimations(R.anim.frag_slide_in_from_left, R.anim.frag_slide_out_to_right); + case FRAG_ACTION_TO_LEFT: + getSupportFragmentManager().popBackStackImmediate(); break; - case ANIM_TO_RIGHT: - transaction.setCustomAnimations(R.anim.frag_slide_out_to_left, R.anim.frag_slide_in_from_right, + case FRAG_ACTION_TO_RIGHT: + transaction.setCustomAnimations(R.anim.frag_slide_in_from_right, R.anim.frag_slide_out_to_left, R.anim.frag_slide_in_from_left, R.anim.frag_slide_out_to_right); - transaction.addToBackStack("back"); + transaction.addToBackStack(null); + transaction.replace(R.id.create_key_fragment_container, fragment) + .commitAllowingStateLoss(); break; } - transaction.replace(R.id.create_key_fragment_container, fragment) - .commitAllowingStateLoss(); // do it immediately! getSupportFragmentManager().executePendingTransactions(); } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java index c2c983b95..b64480087 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyFinalFragment.java @@ -20,11 +20,8 @@ package org.sufficientlysecure.keychain.ui; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; -import android.database.ContentObserver; import android.net.Uri; import android.os.Bundle; -import android.os.Handler; -import android.os.Looper; import android.os.Message; import android.os.Messenger; import android.support.v4.app.Fragment; @@ -32,7 +29,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; -import android.widget.Spinner; import android.widget.TextView; import org.spongycastle.bcpg.sig.KeyFlags; @@ -110,9 +106,7 @@ public class CreateKeyFinalFragment extends Fragment { mBackButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - CreateKeyInputFragment frag = - CreateKeyInputFragment.newInstance(mName, mEmail); - mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_LEFT); + mCreateKeyActivity.loadFragment(null, null, CreateKeyActivity.FRAG_ACTION_TO_LEFT); } }); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java index a1fdc09cc..ef5a3b145 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyInputFragment.java @@ -161,7 +161,7 @@ public class CreateKeyInputFragment extends Fragment { ); hideKeyboard(); - mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.ANIM_TO_RIGHT); + mCreateKeyActivity.loadFragment(null, frag, CreateKeyActivity.FRAG_ACTION_TO_RIGHT); } } -- cgit v1.2.3