aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java')
-rw-r--r--OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java156
1 files changed, 136 insertions, 20 deletions
diff --git a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java
index cc5ef8038..be233d0b3 100644
--- a/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java
+++ b/OpenKeychain/src/test/java/org/sufficientlysecure/keychain/pgp/PgpEncryptDecryptTest.java
@@ -27,6 +27,7 @@ import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
+import org.apache.tools.ant.util.StringUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
@@ -77,7 +78,7 @@ public class PgpEncryptDecryptTest {
static UncachedKeyRing mStaticRing1, mStaticRing2, mStaticRingInsecure;
static Passphrase mKeyPhrase1 = TestingUtils.genPassphrase(true);
static Passphrase mKeyPhrase2 = TestingUtils.genPassphrase(true);
- static Passphrase mKeyPhraseInsecure = TestingUtils.genPassphrase(true);
+// static Passphrase mKeyPhraseInsecure = TestingUtils.genPassphrase(true);
static PrintStream oldShadowStream;
@@ -127,24 +128,24 @@ public class PgpEncryptDecryptTest {
mStaticRing2 = result.getRing();
}
- {
- // insecure (1024 bit) RSA key
- SaveKeyringParcel parcel = new SaveKeyringParcel();
- parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
- Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L));
- parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
- Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L));
- parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
- Algorithm.RSA, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L));
- parcel.mAddUserIds.add("eve");
- parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhraseInsecure);
-
- PgpEditKeyResult result = op.createSecretKeyRing(parcel);
- Assert.assertTrue("initial test key creation must succeed", result.success());
- Assert.assertNotNull("initial test key creation must succeed", result.getRing());
-
- mStaticRingInsecure = result.getRing();
- }
+// {
+// // insecure (1024 bit) RSA key
+// SaveKeyringParcel parcel = new SaveKeyringParcel();
+// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
+// Algorithm.RSA, 1024, null, KeyFlags.CERTIFY_OTHER, 0L));
+// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
+// Algorithm.RSA, 1024, null, KeyFlags.SIGN_DATA, 0L));
+// parcel.mAddSubKeys.add(new SaveKeyringParcel.SubkeyAdd(
+// Algorithm.RSA, 1024, null, KeyFlags.ENCRYPT_COMMS, 0L));
+// parcel.mAddUserIds.add("eve");
+// parcel.mNewUnlock = new ChangeUnlockParcel(mKeyPhraseInsecure);
+//
+// PgpEditKeyResult result = op.createSecretKeyRing(parcel);
+// Assert.assertTrue("initial test key creation must succeed", result.success());
+// Assert.assertNotNull("initial test key creation must succeed", result.getRing());
+//
+// mStaticRingInsecure = result.getRing();
+// }
}
@@ -287,7 +288,7 @@ public class PgpEncryptDecryptTest {
}
@Test
- public void testAsymmetricSign() {
+ public void testAsymmetricSignLiteral() {
String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true);
byte[] ciphertext;
@@ -341,6 +342,121 @@ public class PgpEncryptDecryptTest {
}
@Test
+ public void testAsymmetricSignCleartext() {
+
+ String plaintext = "dies ist ein\r\nplaintext\n ☭" + TestingUtils.genPassphrase(true);
+ byte[] ciphertext;
+
+ { // encrypt data with key
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes());
+
+ PgpSignEncryptOperation op = new PgpSignEncryptOperation(RuntimeEnvironment.application,
+ new ProviderHelper(RuntimeEnvironment.application), null);
+
+ InputData data = new InputData(in, in.available());
+ PgpSignEncryptInputParcel input = new PgpSignEncryptInputParcel();
+
+ // only sign, as cleartext
+ input.setSignatureMasterKeyId(mStaticRing1.getMasterKeyId());
+ input.setSignatureSubKeyId(KeyringTestingHelper.getSubkeyId(mStaticRing1, 1));
+ input.setCleartextSignature(true);
+ input.setEnableAsciiArmorOutput(true);
+ input.setDetachedSignature(false);
+
+ PgpSignEncryptResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1), data, out);
+ Assert.assertTrue("signing must succeed", result.success());
+
+ ciphertext = out.toByteArray();
+ }
+
+ Assert.assertTrue("clearsigned text must contain plaintext (ignoring newlines)",
+ new String(ciphertext).replace("\r\n", "").contains(plaintext.replace("\r", "").replace("\n", "")));
+
+ { // verification should succeed
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = new ByteArrayInputStream(ciphertext);
+ InputData data = new InputData(in, in.available());
+
+ PgpDecryptVerifyOperation op = operationWithFakePassphraseCache(null, null, null);
+ PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel();
+ DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(), data, out);
+
+ Assert.assertTrue("verification must succeed", result.success());
+
+ Assert.assertTrue("verification text should equal plaintext (ignoring newlines)",
+ new String(out.toByteArray()).replace(StringUtils.LINE_SEP, "")
+ .equals(plaintext.replace("\r", "").replace("\n", "")));
+ Assert.assertEquals("decryptionResult should be RESULT_NOT_ENCRYPTED",
+ OpenPgpDecryptionResult.RESULT_NOT_ENCRYPTED, result.getDecryptionResult().getResult());
+ Assert.assertEquals("signatureResult should be RESULT_VALID_CONFIRMED",
+ OpenPgpSignatureResult.RESULT_VALID_CONFIRMED, result.getSignatureResult().getResult());
+
+ OpenPgpMetadata metadata = result.getDecryptionMetadata();
+ Assert.assertEquals("filesize must be correct",
+ out.toByteArray().length, metadata.getOriginalSize());
+
+ }
+
+ }
+
+ @Test
+ public void testAsymmetricSignDetached() {
+
+ String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true);
+ byte[] detachedSignature;
+
+ { // encrypt data with key
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes());
+
+ PgpSignEncryptOperation op = new PgpSignEncryptOperation(RuntimeEnvironment.application,
+ new ProviderHelper(RuntimeEnvironment.application), null);
+
+ InputData data = new InputData(in, in.available());
+ PgpSignEncryptInputParcel input = new PgpSignEncryptInputParcel();
+
+ // only sign, as cleartext
+ input.setSignatureMasterKeyId(mStaticRing1.getMasterKeyId());
+ input.setSignatureSubKeyId(KeyringTestingHelper.getSubkeyId(mStaticRing1, 1));
+ input.setDetachedSignature(true);
+
+ PgpSignEncryptResult result = op.execute(input, new CryptoInputParcel(mKeyPhrase1), data, out);
+ Assert.assertTrue("signing must succeed", result.success());
+
+ detachedSignature = result.getDetachedSignature();
+ }
+
+ { // verification should succeed
+
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ByteArrayInputStream in = new ByteArrayInputStream(plaintext.getBytes());
+ InputData data = new InputData(in, in.available());
+
+ PgpDecryptVerifyOperation op = operationWithFakePassphraseCache(null, null, null);
+ PgpDecryptVerifyInputParcel input = new PgpDecryptVerifyInputParcel();
+ input.setDetachedSignature(detachedSignature);
+ DecryptVerifyResult result = op.execute(input, new CryptoInputParcel(), data, out);
+
+ Assert.assertTrue("verification must succeed", result.success());
+ Assert.assertArrayEquals("verification text should equal plaintext (save for a newline)",
+ plaintext.getBytes(), out.toByteArray());
+ Assert.assertEquals("decryptionResult should be RESULT_NOT_ENCRYPTED",
+ OpenPgpDecryptionResult.RESULT_NOT_ENCRYPTED, result.getDecryptionResult().getResult());
+ Assert.assertEquals("signatureResult should be RESULT_VALID_CONFIRMED",
+ OpenPgpSignatureResult.RESULT_VALID_CONFIRMED, result.getSignatureResult().getResult());
+
+ // TODO should detached verify return any metadata?
+ // OpenPgpMetadata metadata = result.getDecryptionMetadata();
+ // Assert.assertEquals("filesize must be correct",
+ // out.toByteArray().length, metadata.getOriginalSize());
+
+ }
+
+ }
+
+ @Test
public void testAsymmetricEncryptDecrypt() {
String plaintext = "dies ist ein plaintext ☭" + TestingUtils.genPassphrase(true);