Return result upon encryption and check if messages are signed/encrypted with specified keys

This commit is contained in:
Paul Schaub 2018-06-27 15:09:39 +02:00
parent fb5d351de7
commit a8e51a47d5
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
5 changed files with 44 additions and 8 deletions

View File

@ -264,18 +264,18 @@ public class EncryptionBuilder implements EncryptionBuilderInterface {
class ArmorImpl implements Armor { class ArmorImpl implements Armor {
@Override @Override
public OutputStream asciiArmor() throws IOException, PGPException { public EncryptionStream asciiArmor() throws IOException, PGPException {
EncryptionBuilder.this.asciiArmor = true; EncryptionBuilder.this.asciiArmor = true;
return build(); return build();
} }
@Override @Override
public OutputStream noArmor() throws IOException, PGPException { public EncryptionStream noArmor() throws IOException, PGPException {
EncryptionBuilder.this.asciiArmor = false; EncryptionBuilder.this.asciiArmor = false;
return build(); return build();
} }
private OutputStream build() throws IOException, PGPException { private EncryptionStream build() throws IOException, PGPException {
Set<PGPPrivateKey> privateKeys = new HashSet<>(); Set<PGPPrivateKey> privateKeys = new HashSet<>();
for (PGPSecretKey secretKey : signingKeys) { for (PGPSecretKey secretKey : signingKeys) {

View File

@ -87,9 +87,9 @@ public interface EncryptionBuilderInterface {
interface Armor { interface Armor {
OutputStream asciiArmor() throws IOException, PGPException; EncryptionStream asciiArmor() throws IOException, PGPException;
OutputStream noArmor() throws IOException, PGPException; EncryptionStream noArmor() throws IOException, PGPException;
} }

View File

@ -18,6 +18,7 @@ package de.vanitasvitae.crypto.pgpainless.encryption_signing;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -160,7 +161,7 @@ public class EncryptionStream extends OutputStream {
this.result = new PainlessResult(recipientKeyIds, this.result = new PainlessResult(recipientKeyIds,
null, symmetricKeyAlgorithm, null, symmetricKeyAlgorithm,
compressionAlgorithm, true, compressionAlgorithm, true,
signingKeyIds, null); signingKeyIds, Collections.emptySet());
} }
static EncryptionStream create(OutputStream outputStream, static EncryptionStream create(OutputStream outputStream,

View File

@ -211,4 +211,22 @@ public class BCUtil {
} }
return ids; return ids;
} }
public static boolean keyRingContainsKeyWithId(PGPPublicKeyRing ring, long keyId) {
Iterator<PGPPublicKey> 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<PGPPublicKey> keys = ring.getPublicKeys();
while (keys.hasNext()) {
PGPPublicKey key = keys.next();
if (key.getKeyID() == keyId) return true;
}
return false;
}
} }

View File

@ -15,12 +15,13 @@
*/ */
package de.vanitasvitae.crypto.pgpainless; package de.vanitasvitae.crypto.pgpainless;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue; import static junit.framework.TestCase.assertTrue;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.security.InvalidAlgorithmParameterException; import java.security.InvalidAlgorithmParameterException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -31,8 +32,10 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import de.vanitasvitae.crypto.pgpainless.algorithm.PublicKeyAlgorithm; 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.DecryptionStream;
import de.vanitasvitae.crypto.pgpainless.decryption_verification.PainlessResult; 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.SecretKeyRingProtector;
import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector; import de.vanitasvitae.crypto.pgpainless.key.UnprotectedKeysProtector;
import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength; import de.vanitasvitae.crypto.pgpainless.key.generation.type.length.RsaLength;
@ -112,13 +115,27 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
ByteArrayOutputStream envelope = new ByteArrayOutputStream(); ByteArrayOutputStream envelope = new ByteArrayOutputStream();
OutputStream encryptor = PGPainless.createEncryptor() EncryptionStream encryptor = PGPainless.createEncryptor()
.onOutputStream(envelope) .onOutputStream(envelope)
.toRecipients(recipientPub) .toRecipients(recipientPub)
.usingSecureAlgorithms() .usingSecureAlgorithms()
.signWith(keyDecryptor, sender) .signWith(keyDecryptor, sender)
.noArmor(); .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); Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
encryptor.close(); encryptor.close();
byte[] encryptedSecretMessage = envelope.toByteArray(); byte[] encryptedSecretMessage = envelope.toByteArray();