From cb64f8865c407543c01dbc3646e9eb1667996dae Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 29 Jun 2014 12:18:16 +0100 Subject: [PATCH 01/11] work in progress --- .../keychain/testsupport/KeyringBuilder.java | 168 +++++++++++ .../keychain/testsupport/TestDataUtil.java | 66 ++++- .../UncachedKeyringTestingHelper.java | 278 ++++++++++++++++++ .../test/java/tests/UncachedKeyringTest.java | 37 +++ 4 files changed, 548 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 create mode 100644 OpenKeychain/src/test/java/tests/UncachedKeyringTest.java 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 + "]"); + } + } + +} diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java new file mode 100644 index 000000000..05a9c23ef --- /dev/null +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -0,0 +1,37 @@ +package tests; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.*; +import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.testsupport.*; +import org.sufficientlysecure.keychain.testsupport.KeyringBuilder; +import org.sufficientlysecure.keychain.testsupport.TestDataUtil; + +@RunWith(RobolectricTestRunner.class) +@org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 +public class UncachedKeyringTest { + + @Test + public void testVerifySuccess() throws Exception { + UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2(); +// Uncomment to prove it's working - the createdDate will then be different +// Thread.sleep(1500); + UncachedKeyRing inputKeyRing = KeyringBuilder.ring1(); + new UncachedKeyringTestingHelper().doTestCanonicalize( + inputKeyRing, expectedKeyRing); + } + + /** + * Just testing my own test code. Should really be using a library for this. + */ + @Test + public void testConcat() throws Exception { + byte[] actual = TestDataUtil.concatAll(new byte[]{1}, new byte[]{2,-2}, new byte[]{5},new byte[]{3}); + byte[] expected = new byte[]{1,2,-2,5,3}; + Assert.assertEquals(java.util.Arrays.toString(expected), java.util.Arrays.toString(actual)); + } + + +} From 5479eafd4b295c0d83955133c3150652bb325578 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:05:20 +0100 Subject: [PATCH 02/11] actually canonicalize --- .../testsupport/UncachedKeyringTestingHelper.java | 11 ++++++++++- .../src/test/java/tests/UncachedKeyringTest.java | 5 +---- 2 files changed, 11 insertions(+), 5 deletions(-) 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) { diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index 05a9c23ef..e4e98cc5c 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -16,11 +16,8 @@ public class UncachedKeyringTest { @Test public void testVerifySuccess() throws Exception { UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2(); -// Uncomment to prove it's working - the createdDate will then be different -// Thread.sleep(1500); UncachedKeyRing inputKeyRing = KeyringBuilder.ring1(); - new UncachedKeyringTestingHelper().doTestCanonicalize( - inputKeyRing, expectedKeyRing); + new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); } /** From 9032e032ff7583ddd09008446ff16c90c6a190b2 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 15:48:47 +0100 Subject: [PATCH 03/11] add toString for test ease --- .../keychain/service/OperationResultParcel.java | 10 ++++++++++ 1 file changed, 10 insertions(+) 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. From c05fd0798627ea2444e9f06fc611bff3e7ee44f3 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 17:09:23 +0100 Subject: [PATCH 04/11] data fixes --- .../keychain/testsupport/KeyringBuilder.java | 27 ++++++++++--------- .../test/java/tests/UncachedKeyringTest.java | 4 +++ 2 files changed, 18 insertions(+), 13 deletions(-) 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()); } diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index e4e98cc5c..cb44d5d8f 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -9,6 +9,8 @@ import org.sufficientlysecure.keychain.testsupport.*; import org.sufficientlysecure.keychain.testsupport.KeyringBuilder; import org.sufficientlysecure.keychain.testsupport.TestDataUtil; +import java.io.*; + @RunWith(RobolectricTestRunner.class) @org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 public class UncachedKeyringTest { @@ -17,6 +19,8 @@ public class UncachedKeyringTest { public void testVerifySuccess() throws Exception { UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2(); UncachedKeyRing inputKeyRing = KeyringBuilder.ring1(); + // Uncomment to dump the encoded key for manual inspection +// inputKeyRing.getPublicKey().getPublicKey().encode(new FileOutputStream(new File("/tmp/key-encoded"))); new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); } From e906fe53870660bd7231e45acce2a7c525edeb91 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Sun, 6 Jul 2014 17:35:07 +0100 Subject: [PATCH 05/11] add the GPG version --- .../src/test/java/tests/UncachedKeyringTest.java | 11 ++++++++++- .../test/resources/public-key-canonicalize.blob | Bin 0 -> 1224 bytes 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 OpenKeychain/src/test/resources/public-key-canonicalize.blob diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index cb44d5d8f..b14bc2301 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -10,6 +10,7 @@ import org.sufficientlysecure.keychain.testsupport.KeyringBuilder; import org.sufficientlysecure.keychain.testsupport.TestDataUtil; import java.io.*; +import java.util.Collections; @RunWith(RobolectricTestRunner.class) @org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 @@ -19,11 +20,19 @@ public class UncachedKeyringTest { public void testVerifySuccess() throws Exception { UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2(); UncachedKeyRing inputKeyRing = KeyringBuilder.ring1(); - // Uncomment to dump the encoded key for manual inspection +// Uncomment to dump the encoded key for manual inspection // inputKeyRing.getPublicKey().getPublicKey().encode(new FileOutputStream(new File("/tmp/key-encoded"))); new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); } + @Test + public void testVerifyFromGpg() throws Exception { + byte[] data = TestDataUtil.readAllFully(Collections.singleton( "/public-key-canonicalize.blob")); + UncachedKeyRing inputKeyRing = UncachedKeyRing.decodeFromData(data); + new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, KeyringBuilder.ring2()); + } + + /** * Just testing my own test code. Should really be using a library for this. */ diff --git a/OpenKeychain/src/test/resources/public-key-canonicalize.blob b/OpenKeychain/src/test/resources/public-key-canonicalize.blob new file mode 100644 index 0000000000000000000000000000000000000000..3450824c1f0197bd526dafc2b9d5118bf3090ccc GIT binary patch literal 1224 zcmV;(1ULJc0SyFGxBTM)2ms5gc-akKEWpK0)5Dc81)=7(A@TpEMp_UiNWXiL>~i_R zfOV4rSa^8K>vJ<9Rv@2&%3C{>FrAhW4{37G#cDVGVtMu*aq*jvWU`3kI4PaZ6Ez_9 zxwIudZ%d;bqiX$!!U!7`@UnyHH4Zlp7#btf6L}CH7os}p86r^IWk4>Rt12g3#fEib zB6}HpixX^vDwHx?Xbu!k*kt^TSMkFg$%}k zv3HHuv9%ah^K3OB@>uZ%+b@>08dKeL_Xy1V@65hJ3XmK(@$`45q_JOK%fHz(5vE7H z!}a(aS>NX+ELwWx#RML!DL- z>muc}-=0G4c(}<=NOOtg{FvycS_D57zgN|ffgX;1veOx70xd4)Lvn(VgX~ez;tWF9 zq2(Fr`XbGpxepK52~W_Iw~OMy+hPJClTma8*GUyDaqY(aNUt;&1C5A6V2;h+yRjTt zBcw7NASt++Jg^t~j#}4>nqh`=&>?(FGypFeN_His6t<{jb>(x*hsI5CX=RRLe-**vGQTG0)?@d8ohLa(^_v24G4>)T)F=w~uoMCF{V36aYqn;eChg5vA2 zLAVZF+&e(+C7OyS);MW(ObCI@+_et{Wd6;@mTTmsxOz1QsoB1{0SyFGxBTM)2mt8f z;-fJFWS<%Mpmc`&2Wk`s%7~3}t_9MM4)b!gK#oNdENS-T%W8SMitU7BL6&n9lc+6I z;N8?ODETbGSWt}Px5^rrFiS6ABGxoRGnjvFAp}5>XGGU Date: Sun, 6 Jul 2014 17:46:43 +0100 Subject: [PATCH 06/11] Actually test canonicalize --- .../keychain/testsupport/KeyringBuilder.java | 218 ++++++++++++------ .../keychain/testsupport/TestDataUtil.java | 19 +- .../test/java/tests/UncachedKeyringTest.java | 33 ++- 3 files changed, 182 insertions(+), 88 deletions(-) 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); + private static final BigInteger PUBLIC_SUBKEY_MODULUS = new BigInteger( + "e8e2e2a33102649f19f8a07486fb076a1406ca888d72ae05d28f0ef372b5408e" + + "45132c69f6e5cb6a79bb8aed84634196731393a82d53e0ddd42f28f92cc15850" + + "8ce3b7ca1a9830502745aee774f86987993df984781f47c4a2910f95cf4c950c" + + "c4c6cccdc134ad408a0c5418b5e360c9781a8434d366053ea6338b975fae88f9" + + "383a10a90e7b2caa9ddb95708aa9d8a90246e29b04dbd6136613085c9a287315" + + "c6e9c7ff4012defc1713875e3ff6073333a1c93d7cd75ebeaaf16b8b853d96ba" + + "7003258779e8d2f70f1bc0bcd3ef91d7a9ccd8e225579b2d6fcae32799b0a6c0" + + "e7305fc65dc4edc849c6130a0d669c90c193b1e746c812510f9d600a208be4a5" + , 16 + ); - public static UncachedKeyRing ring1() { - return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); + private static final Date SIGNATURE_DATE = new Date(1404566755000L); + + private static final BigInteger EXPONENT = BigInteger.valueOf(0x010001); + + private static final String USER_ID_STRING = "OpenKeychain User (NOT A REAL KEY) "; + + 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); + + public static UncachedKeyRing correctRing() { + return convertToKeyring(correctKeyringPackets()); } - public static UncachedKeyRing ring2() { - return ringForModulus(new Date(1404566755000L), "OpenKeychain User (NOT A REAL KEY) "); + 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 ringForModulus(Date date, String userIdString) { - + private static UncachedKeyRing convertToKeyring(List packets) { 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()); - + 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]; diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index b14bc2301..1f5bb82bd 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -6,30 +6,39 @@ import org.junit.runner.RunWith; import org.robolectric.*; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; import org.sufficientlysecure.keychain.testsupport.*; -import org.sufficientlysecure.keychain.testsupport.KeyringBuilder; -import org.sufficientlysecure.keychain.testsupport.TestDataUtil; +import java.util.*; import java.io.*; -import java.util.Collections; @RunWith(RobolectricTestRunner.class) @org.robolectric.annotation.Config(emulateSdk = 18) // Robolectric doesn't yet support 19 public class UncachedKeyringTest { @Test - public void testVerifySuccess() throws Exception { - UncachedKeyRing expectedKeyRing = KeyringBuilder.ring2(); - UncachedKeyRing inputKeyRing = KeyringBuilder.ring1(); + public void testCanonicalizeNoChanges() throws Exception { + UncachedKeyRing expectedKeyRing = KeyringBuilder.correctRing(); + UncachedKeyRing inputKeyRing = KeyringBuilder.correctRing(); // Uncomment to dump the encoded key for manual inspection -// inputKeyRing.getPublicKey().getPublicKey().encode(new FileOutputStream(new File("/tmp/key-encoded"))); +// TestDataUtil.appendToOutput(new ByteArrayInputStream(inputKeyRing.getEncoded()), new FileOutputStream(new File("/tmp/key-encoded"))); new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); } + @Test - public void testVerifyFromGpg() throws Exception { - byte[] data = TestDataUtil.readAllFully(Collections.singleton( "/public-key-canonicalize.blob")); + public void testCanonicalizeExtraIncorrectSignature() throws Exception { + UncachedKeyRing expectedKeyRing = KeyringBuilder.correctRing(); + UncachedKeyRing inputKeyRing = KeyringBuilder.ringWithExtraIncorrectSignature(); + new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); + } + + /** + * Check the original GnuPG-generated public key is OK + */ + @Test + public void testCanonicalizeOriginalGpg() throws Exception { + byte[] data = TestDataUtil.readAllFully(Collections.singleton("/public-key-canonicalize.blob")); UncachedKeyRing inputKeyRing = UncachedKeyRing.decodeFromData(data); - new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, KeyringBuilder.ring2()); + new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, KeyringBuilder.correctRing()); } @@ -38,8 +47,8 @@ public class UncachedKeyringTest { */ @Test public void testConcat() throws Exception { - byte[] actual = TestDataUtil.concatAll(new byte[]{1}, new byte[]{2,-2}, new byte[]{5},new byte[]{3}); - byte[] expected = new byte[]{1,2,-2,5,3}; + byte[] actual = TestDataUtil.concatAll(new byte[]{1}, new byte[]{2, -2}, new byte[]{5}, new byte[]{3}); + byte[] expected = new byte[]{1, 2, -2, 5, 3}; Assert.assertEquals(java.util.Arrays.toString(expected), java.util.Arrays.toString(actual)); } From 83e5a3d341ef35c37e39ac9102eef5f9d2a3106f Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:30:08 +0200 Subject: [PATCH 07/11] add diffKeyrings method --- .../testsupport/KeyringTestingHelper.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) 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 } } + } From 9971f9ad4cf0c06bb6ab22f9cee16f1a91370365 Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Mon, 7 Jul 2014 18:53:41 +0200 Subject: [PATCH 08/11] use KeyringTestHelper.diffKeyrings method for unit test Conflicts: OpenKeychain/src/test/java/tests/UncachedKeyringTest.java --- .../testsupport/KeyringTestingHelper.java | 2 +- .../test/java/tests/UncachedKeyringTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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); } } diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index 1f5bb82bd..c0f7bf17e 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -5,7 +5,11 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.*; import org.sufficientlysecure.keychain.pgp.UncachedKeyRing; +import org.sufficientlysecure.keychain.service.OperationResultParcel; import org.sufficientlysecure.keychain.testsupport.*; +import org.sufficientlysecure.keychain.testsupport.KeyringBuilder; +import org.sufficientlysecure.keychain.testsupport.KeyringTestingHelper; +import org.sufficientlysecure.keychain.testsupport.TestDataUtil; import java.util.*; import java.io.*; @@ -21,6 +25,20 @@ public class UncachedKeyringTest { // Uncomment to dump the encoded key for manual inspection // TestDataUtil.appendToOutput(new ByteArrayInputStream(inputKeyRing.getEncoded()), new FileOutputStream(new File("/tmp/key-encoded"))); new UncachedKeyringTestingHelper().doTestCanonicalize(inputKeyRing, expectedKeyRing); + + OperationResultParcel.OperationLog log = new OperationResultParcel.OperationLog(); + UncachedKeyRing canonicalizedRing = inputKeyRing.canonicalize(log, 0); + + if (canonicalizedRing == null) { + throw new AssertionError("Canonicalization failed; messages: [" + log.toString() + "]"); + } + + HashSet onlyA = new HashSet(); + HashSet onlyB = new HashSet(); + Assert.assertTrue(KeyringTestingHelper.diffKeyrings( + canonicalizedRing.getEncoded(), expectedKeyRing.getEncoded(), onlyA, onlyB)); + + } From 51bedc2e73da3fe0022ee3339723c3b351ccd5b9 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:31:32 +0100 Subject: [PATCH 09/11] (c) headers, tidy imports --- .../keychain/testsupport/KeyringBuilder.java | 17 +++++++++++++ .../testsupport/KeyringTestingHelper.java | 16 +++++++++++++ .../testsupport/PgpVerifyTestingHelper.java | 16 +++++++++++++ .../testsupport/ProviderHelperStub.java | 17 +++++++++++++ .../keychain/testsupport/TestDataUtil.java | 17 +++++++++++++ .../UncachedKeyringTestingHelper.java | 24 +++++++++++++------ .../test/java/tests/PgpDecryptVerifyTest.java | 17 +++++++++++++ .../java/tests/ProviderHelperKeyringTest.java | 17 +++++++++++++ .../test/java/tests/UncachedKeyringTest.java | 17 +++++++++++++ 9 files changed, 151 insertions(+), 7 deletions(-) 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. diff --git a/OpenKeychain/src/test/java/tests/PgpDecryptVerifyTest.java b/OpenKeychain/src/test/java/tests/PgpDecryptVerifyTest.java index d759bce05..0353e03f5 100644 --- a/OpenKeychain/src/test/java/tests/PgpDecryptVerifyTest.java +++ b/OpenKeychain/src/test/java/tests/PgpDecryptVerifyTest.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 tests; import org.junit.Assert; diff --git a/OpenKeychain/src/test/java/tests/ProviderHelperKeyringTest.java b/OpenKeychain/src/test/java/tests/ProviderHelperKeyringTest.java index 3d48c2f97..830ec1266 100644 --- a/OpenKeychain/src/test/java/tests/ProviderHelperKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/ProviderHelperKeyringTest.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 tests; import java.util.Collections; diff --git a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java index c0f7bf17e..b3f78f22d 100644 --- a/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java +++ b/OpenKeychain/src/test/java/tests/UncachedKeyringTest.java @@ -1,3 +1,20 @@ +/* + * 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 tests; import org.junit.Assert; From 37433bd2823b42aa4b5047b605b517cb9d7f4932 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:37:48 +0100 Subject: [PATCH 10/11] prevent odd ambiguous method toString error --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 78b0c5e74a13aa33ccbb7cbfff86f7a02d977429 Mon Sep 17 00:00:00 2001 From: Art O Cathain Date: Mon, 7 Jul 2014 21:38:49 +0100 Subject: [PATCH 11/11] actually provide a tostring --- .../keychain/testsupport/UncachedKeyringTestingHelper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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