From a8e51a47d527a13886c40b431139d030c43dfc0b Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 27 Jun 2018 15:09:39 +0200 Subject: [PATCH] Return result upon encryption and check if messages are signed/encrypted with specified keys --- .../encryption_signing/EncryptionBuilder.java | 6 +++--- .../EncryptionBuilderInterface.java | 4 ++-- .../encryption_signing/EncryptionStream.java | 3 ++- .../crypto/pgpainless/util/BCUtil.java | 18 ++++++++++++++++ .../crypto/pgpainless/EncryptDecryptTest.java | 21 +++++++++++++++++-- 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilder.java b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilder.java index 137af08e..84c94b79 100644 --- a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilder.java +++ b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilder.java @@ -264,18 +264,18 @@ public class EncryptionBuilder implements EncryptionBuilderInterface { class ArmorImpl implements Armor { @Override - public OutputStream asciiArmor() throws IOException, PGPException { + public EncryptionStream asciiArmor() throws IOException, PGPException { EncryptionBuilder.this.asciiArmor = true; return build(); } @Override - public OutputStream noArmor() throws IOException, PGPException { + public EncryptionStream noArmor() throws IOException, PGPException { EncryptionBuilder.this.asciiArmor = false; return build(); } - private OutputStream build() throws IOException, PGPException { + private EncryptionStream build() throws IOException, PGPException { Set privateKeys = new HashSet<>(); for (PGPSecretKey secretKey : signingKeys) { diff --git a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilderInterface.java b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilderInterface.java index 48e063ef..e4f7eb9a 100644 --- a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilderInterface.java +++ b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionBuilderInterface.java @@ -87,9 +87,9 @@ public interface EncryptionBuilderInterface { interface Armor { - OutputStream asciiArmor() throws IOException, PGPException; + EncryptionStream asciiArmor() throws IOException, PGPException; - OutputStream noArmor() throws IOException, PGPException; + EncryptionStream noArmor() throws IOException, PGPException; } diff --git a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionStream.java b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionStream.java index 5924bcba..ab2bd621 100644 --- a/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionStream.java +++ b/src/main/java/de/vanitasvitae/crypto/pgpainless/encryption_signing/EncryptionStream.java @@ -18,6 +18,7 @@ package de.vanitasvitae.crypto.pgpainless.encryption_signing; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; @@ -160,7 +161,7 @@ public class EncryptionStream extends OutputStream { this.result = new PainlessResult(recipientKeyIds, null, symmetricKeyAlgorithm, compressionAlgorithm, true, - signingKeyIds, null); + signingKeyIds, Collections.emptySet()); } static EncryptionStream create(OutputStream outputStream, diff --git a/src/main/java/de/vanitasvitae/crypto/pgpainless/util/BCUtil.java b/src/main/java/de/vanitasvitae/crypto/pgpainless/util/BCUtil.java index b8144d72..b4e4e818 100644 --- a/src/main/java/de/vanitasvitae/crypto/pgpainless/util/BCUtil.java +++ b/src/main/java/de/vanitasvitae/crypto/pgpainless/util/BCUtil.java @@ -211,4 +211,22 @@ public class BCUtil { } return ids; } + + public static boolean keyRingContainsKeyWithId(PGPPublicKeyRing ring, long keyId) { + Iterator keys = ring.getPublicKeys(); + while (keys.hasNext()) { + PGPPublicKey key = keys.next(); + if (key.getKeyID() == keyId) return true; + } + return false; + } + + public static boolean keyRingContainsKeyWithId(PGPSecretKeyRing ring, long keyId) { + Iterator keys = ring.getPublicKeys(); + while (keys.hasNext()) { + PGPPublicKey key = keys.next(); + if (key.getKeyID() == keyId) return true; + } + return false; + } } \ No newline at end of file diff --git a/src/test/java/de/vanitasvitae/crypto/pgpainless/EncryptDecryptTest.java b/src/test/java/de/vanitasvitae/crypto/pgpainless/EncryptDecryptTest.java index 853a3ccd..59fa9940 100644 --- a/src/test/java/de/vanitasvitae/crypto/pgpainless/EncryptDecryptTest.java +++ b/src/test/java/de/vanitasvitae/crypto/pgpainless/EncryptDecryptTest.java @@ -15,12 +15,13 @@ */ package de.vanitasvitae.crypto.pgpainless; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertTrue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.OutputStream; import java.nio.charset.Charset; import java.security.InvalidAlgorithmParameterException; import java.security.NoSuchAlgorithmException; @@ -31,8 +32,10 @@ import java.util.logging.Level; import java.util.logging.Logger; import de.vanitasvitae.crypto.pgpainless.algorithm.PublicKeyAlgorithm; +import de.vanitasvitae.crypto.pgpainless.algorithm.SymmetricKeyAlgorithm; import de.vanitasvitae.crypto.pgpainless.decryption_verification.DecryptionStream; import de.vanitasvitae.crypto.pgpainless.decryption_verification.PainlessResult; +import de.vanitasvitae.crypto.pgpainless.encryption_signing.EncryptionStream; import de.vanitasvitae.crypto.pgpainless.key.SecretKeyRingProtector; import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector; import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength; @@ -112,13 +115,27 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest { ByteArrayOutputStream envelope = new ByteArrayOutputStream(); - OutputStream encryptor = PGPainless.createEncryptor() + EncryptionStream encryptor = PGPainless.createEncryptor() .onOutputStream(envelope) .toRecipients(recipientPub) .usingSecureAlgorithms() .signWith(keyDecryptor, sender) .noArmor(); + PainlessResult encryptionResult = encryptor.getResult(); + + assertFalse(encryptionResult.getAllSignatureKeyIds().isEmpty()); + for (long keyId : encryptionResult.getAllSignatureKeyIds()) { + assertTrue(BCUtil.keyRingContainsKeyWithId(sender, keyId)); + } + + assertFalse(encryptionResult.getRecipientKeyIds().isEmpty()); + for (long keyId : encryptionResult.getRecipientKeyIds()) { + assertTrue(BCUtil.keyRingContainsKeyWithId(recipient, keyId)); + } + + assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getSymmetricKeyAlgorithm()); + Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor); encryptor.close(); byte[] encryptedSecretMessage = envelope.toByteArray();