From 530a22ba0ed81c1e10911a22f76dbb58bb76409d Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 10 Jan 2020 15:12:04 +0100 Subject: [PATCH] Improve tests and add signatures to result --- .../DecryptionBuilder.java | 15 +++-- .../DecryptionStreamFactory.java | 8 ++- .../OpenPgpMetadata.java | 60 ++++++++++++++----- .../SignatureVerifyingInputStream.java | 2 + .../encryption_signing/EncryptionStream.java | 28 ++++----- .../org/pgpainless/EncryptDecryptTest.java | 20 +++---- .../test/java/org/pgpainless/TestKeys.java | 52 +++++++--------- .../java/org/pgpainless/TestKeysTest.java | 47 ++++++++++++--- 8 files changed, 146 insertions(+), 86 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java index 5e9cf9ea..39441bc4 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionBuilder.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import java.util.function.Predicate; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; @@ -74,15 +75,17 @@ public class DecryptionBuilder implements DecryptionBuilderInterface { @Override public HandleMissingPublicKeys verifyWith(@Nonnull Set trustedKeyIds, @Nonnull PGPPublicKeyRingCollection publicKeyRingCollection) { + Set publicKeyRings = keyRingCollectionToSet(publicKeyRingCollection); + publicKeyRings.removeIf(p -> !trustedKeyIds.contains(new OpenPgpV4Fingerprint(p))); + return verifyWith(publicKeyRings); + } + + private Set keyRingCollectionToSet(PGPPublicKeyRingCollection publicKeyRingCollection) { Set publicKeyRings = new HashSet<>(); for (Iterator i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { - PGPPublicKeyRing p = i.next(); - OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(p); - if (trustedKeyIds.contains(fingerprint)) { - publicKeyRings.add(p); - } + publicKeyRings.add(i.next()); } - return verifyWith(publicKeyRings); + return publicKeyRings; } @Override diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java index 67e34b43..dcd6f0ae 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/DecryptionStreamFactory.java @@ -198,8 +198,12 @@ public final class DecryptionStreamFactory { throw new PGPException("Verification failed - No OnePassSignatures found"); } - while (iterator.hasNext()) { - PGPOnePassSignature signature = iterator.next(); + processOnePassSignatures(iterator); + } + + private void processOnePassSignatures(Iterator signatures) throws PGPException { + while (signatures.hasNext()) { + PGPOnePassSignature signature = signatures.next(); final long keyId = signature.getKeyID(); resultBuilder.addUnverifiedSignatureKeyId(keyId); diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/OpenPgpMetadata.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/OpenPgpMetadata.java index 5ae994fe..7f9033e7 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/OpenPgpMetadata.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/OpenPgpMetadata.java @@ -17,10 +17,13 @@ package org.pgpainless.decryption_verification; import java.util.Collections; import java.util.HashSet; +import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSignature; import org.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.key.OpenPgpV4Fingerprint; @@ -29,7 +32,9 @@ public class OpenPgpMetadata { private final Set recipientKeyIds; private final OpenPgpV4Fingerprint decryptionFingerprint; - private final Set unverifiedSignatureKeyIds; + private final Set signatures; + private final Set signatureKeyIds; + private final Map verifiedSignatures; private final Set verifiedSignaturesFingerprints; private final SymmetricKeyAlgorithm symmetricKeyAlgorithm; @@ -41,7 +46,9 @@ public class OpenPgpMetadata { SymmetricKeyAlgorithm symmetricKeyAlgorithm, CompressionAlgorithm algorithm, boolean integrityProtected, - Set unverifiedSignatureKeyIds, + Set signatures, + Set signatureKeyIds, + Map verifiedSignatures, Set verifiedSignaturesFingerprints) { this.recipientKeyIds = Collections.unmodifiableSet(recipientKeyIds); @@ -49,7 +56,9 @@ public class OpenPgpMetadata { this.symmetricKeyAlgorithm = symmetricKeyAlgorithm; this.compressionAlgorithm = algorithm; this.integrityProtected = integrityProtected; - this.unverifiedSignatureKeyIds = Collections.unmodifiableSet(unverifiedSignatureKeyIds); + this.signatures = Collections.unmodifiableSet(signatures); + this.signatureKeyIds = Collections.unmodifiableSet(signatureKeyIds); + this.verifiedSignatures = Collections.unmodifiableMap(verifiedSignatures); this.verifiedSignaturesFingerprints = Collections.unmodifiableSet(verifiedSignaturesFingerprints); } @@ -77,15 +86,23 @@ public class OpenPgpMetadata { return integrityProtected; } - public Set getAllSignatureKeyFingerprints() { - return unverifiedSignatureKeyIds; + public Set getSignatures() { + return signatures; + } + + public Set getSignatureKeyIDs() { + return signatureKeyIds; } public boolean isSigned() { - return !unverifiedSignatureKeyIds.isEmpty(); + return !signatureKeyIds.isEmpty(); } - public Set getVerifiedSignaturesFingerprints() { + public Map getVerifiedSignatures() { + return verifiedSignatures; + } + + public Set getVerifiedSignatureKeyFingerprints() { return verifiedSignaturesFingerprints; } @@ -107,16 +124,18 @@ public class OpenPgpMetadata { return verifiedSignaturesFingerprints.contains(fingerprint); } - static Builder getBuilder() { + public static Builder getBuilder() { return new Builder(); } - static class Builder { + public static class Builder { private final Set recipientFingerprints = new HashSet<>(); private OpenPgpV4Fingerprint decryptionFingerprint; - private final Set unverifiedSignatureKeyIds = new HashSet<>(); - private final Set verifiedSignatureFingerprints = new HashSet<>(); + private final Set signatures = new HashSet<>(); + private final Set signatureKeyIds = new HashSet<>(); + private final Map verifiedSignatures = new ConcurrentHashMap<>(); + private final Set verifiedSignatureKeyFingerprints = new HashSet<>(); private SymmetricKeyAlgorithm symmetricKeyAlgorithm = SymmetricKeyAlgorithm.NULL; private CompressionAlgorithm compressionAlgorithm = CompressionAlgorithm.UNCOMPRESSED; private boolean integrityProtected = false; @@ -136,13 +155,23 @@ public class OpenPgpMetadata { return this; } + public Builder addSignature(PGPSignature signature) { + signatures.add(signature); + return this; + } + public Builder addUnverifiedSignatureKeyId(Long keyId) { - this.unverifiedSignatureKeyIds.add(keyId); + this.signatureKeyIds.add(keyId); + return this; + } + + public Builder putVerifiedSignature(OpenPgpV4Fingerprint fingerprint, PGPSignature verifiedSignature) { + verifiedSignatures.put(fingerprint, verifiedSignature); return this; } public Builder addVerifiedSignatureFingerprint(OpenPgpV4Fingerprint fingerprint) { - this.verifiedSignatureFingerprints.add(fingerprint); + this.verifiedSignatureKeyFingerprints.add(fingerprint); return this; } @@ -157,7 +186,10 @@ public class OpenPgpMetadata { } public OpenPgpMetadata build() { - return new OpenPgpMetadata(recipientFingerprints, decryptionFingerprint, symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected, unverifiedSignatureKeyIds, verifiedSignatureFingerprints); + return new OpenPgpMetadata(recipientFingerprints, decryptionFingerprint, + symmetricKeyAlgorithm, compressionAlgorithm, integrityProtected, + signatures, signatureKeyIds, + verifiedSignatures, verifiedSignatureKeyFingerprints); } } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java index 58bb8e22..fd0f3217 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/SignatureVerifyingInputStream.java @@ -96,6 +96,7 @@ public class SignatureVerifyingInputStream extends FilterInputStream { } for (PGPSignature signature : signatureList) { + resultBuilder.addSignature(signature); OpenPgpV4Fingerprint fingerprint = null; for (OpenPgpV4Fingerprint f : onePassSignatures.keySet()) { if (f.getKeyId() == signature.getKeyID()) { @@ -114,6 +115,7 @@ public class SignatureVerifyingInputStream extends FilterInputStream { throw new SignatureException("Bad Signature of key " + signature.getKeyID()); } else { LOGGER.log(LEVEL, "Verified signature of key " + Long.toHexString(signature.getKeyID())); + resultBuilder.putVerifiedSignature(fingerprint, signature); resultBuilder.addVerifiedSignatureFingerprint(fingerprint); } } diff --git a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java index d2c7b1d1..70f01c28 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java +++ b/pgpainless-core/src/main/java/org/pgpainless/encryption_signing/EncryptionStream.java @@ -57,7 +57,7 @@ public final class EncryptionStream extends OutputStream { private static final int BUFFER_SIZE = 1 << 8; - private final OpenPgpMetadata result; + private final OpenPgpMetadata.Builder resultBuilder = OpenPgpMetadata.getBuilder(); private List signatureGenerators = new ArrayList<>(); private boolean closed = false; @@ -147,21 +147,11 @@ public final class EncryptionStream extends OutputStream { PGPLiteralData.BINARY, PGPLiteralData.CONSOLE, new Date(), new byte[BUFFER_SIZE]); // Prepare result - Set recipientKeyIds = new HashSet<>(); for (PGPPublicKey recipient : encryptionKeys) { - recipientKeyIds.add(recipient.getKeyID()); + resultBuilder.addRecipientKeyId(recipient.getKeyID()); } - - Set signingKeyIds = new HashSet<>(); - for (PGPPrivateKey signer : signingKeys) { - signingKeyIds.add(signer.getKeyID()); - } - - - this.result = new OpenPgpMetadata(recipientKeyIds, - null, symmetricKeyAlgorithm, - compressionAlgorithm, true, - signingKeyIds, Collections.emptySet()); + resultBuilder.setSymmetricKeyAlgorithm(symmetricKeyAlgorithm); + resultBuilder.setCompressionAlgorithm(compressionAlgorithm); } @Override @@ -205,7 +195,10 @@ public final class EncryptionStream extends OutputStream { // Signing for (PGPSignatureGenerator signatureGenerator : signatureGenerators) { try { - signatureGenerator.generate().encode(basicCompressionStream); + PGPSignature signature = signatureGenerator.generate(); + signature.encode(basicCompressionStream); + resultBuilder.addSignature(signature); + resultBuilder.addUnverifiedSignatureKeyId(signature.getKeyID()); } catch (PGPException e) { throw new IOException(e); } @@ -230,6 +223,9 @@ public final class EncryptionStream extends OutputStream { } public OpenPgpMetadata getResult() { - return result; + if (!closed) { + throw new IllegalStateException("EncryptionStream must be closed before accessing the Result."); + } + return resultBuilder.build(); } } diff --git a/pgpainless-core/src/test/java/org/pgpainless/EncryptDecryptTest.java b/pgpainless-core/src/test/java/org/pgpainless/EncryptDecryptTest.java index f8c294d5..4070c1fc 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/EncryptDecryptTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/EncryptDecryptTest.java @@ -142,10 +142,18 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest { .signWith(keyDecryptor, senderSec) .noArmor(); + Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor); + encryptor.close(); + byte[] encryptedSecretMessage = envelope.toByteArray(); + + LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) + + " Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) + + " Encrypted Length: " + encryptedSecretMessage.length); + OpenPgpMetadata encryptionResult = encryptor.getResult(); - assertFalse(encryptionResult.getAllSignatureKeyFingerprints().isEmpty()); - for (long keyId : encryptionResult.getAllSignatureKeyFingerprints()) { + assertFalse(encryptionResult.getSignatureKeyIDs().isEmpty()); + for (long keyId : encryptionResult.getSignatureKeyIDs()) { assertTrue(BCUtil.keyRingContainsKeyWithId(senderPub, keyId)); } @@ -156,14 +164,6 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest { assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getSymmetricKeyAlgorithm()); - Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor); - encryptor.close(); - byte[] encryptedSecretMessage = envelope.toByteArray(); - - LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) + - " Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) + - " Encrypted Length: " + encryptedSecretMessage.length); - // Juliet trieth to comprehend Romeos words ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage); diff --git a/pgpainless-core/src/test/java/org/pgpainless/TestKeys.java b/pgpainless-core/src/test/java/org/pgpainless/TestKeys.java index fbc4585e..4dea45d2 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/TestKeys.java +++ b/pgpainless-core/src/test/java/org/pgpainless/TestKeys.java @@ -26,6 +26,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.openpgp.PGPUtil; import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.pgpainless.key.OpenPgpV4Fingerprint; public class TestKeys extends AbstractPGPainlessTest { @@ -42,6 +43,8 @@ public class TestKeys extends AbstractPGPainlessTest { public static final String JULIET_UID = "xmpp:juliet@capulet.lit"; public static final long JULIET_KEY_ID = -5425419407118114754L; + public static final String JULIET_FINGERPRINT_STRING = "1D018C772DF8C5EF86A1DCC9B4B509CB5936E03E"; + public static final OpenPgpV4Fingerprint JULIET_FINGERPRINT = new OpenPgpV4Fingerprint(JULIET_FINGERPRINT_STRING); /** * Public key of xmpp:juliet@capulet.lit. @@ -104,6 +107,8 @@ public class TestKeys extends AbstractPGPainlessTest { public static final String ROMEO_UID = "xmpp:romeo@montague.lit"; public static final long ROMEO_KEY_ID = 334147643349279223L; + public static final String ROMEO_FINGERPRINT_STRING = "35D299D08A2F7D80230B095D04A32182E05E21F7"; + public static final OpenPgpV4Fingerprint ROMEO_FINGERPRINT = new OpenPgpV4Fingerprint(ROMEO_FINGERPRINT_STRING); /** * Public key of xmpp:romeo@montague.lit. @@ -233,35 +238,24 @@ public class TestKeys extends AbstractPGPainlessTest { /** * Test Message signed with {@link #JULIET_SEC} and encrypted for {@link #JULIET_PUB}. */ - public static final String TEST_MESSAGE_01 = "-----BEGIN PGP MESSAGE-----\n" + + public static final String MSG_SIGN_CRYPT_JULIET_JULIET = + "-----BEGIN PGP MESSAGE-----\n" + "\n" + - "hQGMAwAAAAAAAAAAAQwAoJtfpcBPCwhUzzHuVIcBzBLyfIWT/EJ527neb46lN56S\n" + - "B05BTIRudIeCsPYz81jwiFi/k0MBecRfozZ1xCPByo8ohSvRgzEHEkCNgObQ1bz0\n" + - "iB+Xb76OEzFOCPUebTaVscLNf8ak/GSzaW7jDc+5vnvDf7cV0x26pe4odpS/U5Tr\n" + - "cO3wb/47K+sJ1cxJmPtcD41O02xu3QisQKPrimM0Kue6ziGeKyw1RkSowv9U47TK\n" + - "wppPCHOTli2Nf+gZizF1oyQZzPGst4fjujygcIoajplfW9nZvxsbmYRSLSdmV9m6\n" + - "k1jQbPDUhVs0gstH92C6hPpoBWxoxkHcwz8gy36nCyB6cYGyq3oN1UnGU4afPyD5\n" + - "SmmEjELBd2i2Ll/DYk2x06SnKZMQuWrSCZzWgl/9HsPo5ydVb97OjuEpWtW9xDMA\n" + - "KlYPNWEq+b+akOEstNraC3pfVKvypz6ZzaMAS1gWWNYg8dlwBJOUVMSo7iLaUQkK\n" + - "yp4uH1DlsyVu1atCUc8thQIMAwAAAAAAAAAAAQ/5AdiZ/sG859Y/rGR7U/8MzGg0\n" + - "j3f2vrgDF/0NRRk5aqd1lb4CaZvrztcYqW3cEK7iF9rKwImZZiWIptjJ9Mz6f1Zl\n" + - "FbODObSVRZAcZqYGswEEfsQvpQFlwG6Qx48OaQaDPr147raFI3C3kEU9Nb2VBg8+\n" + - "MevJaXJft5PXwUTG2Qvfxqr/3hfGAwB4/zHwA8vFd1np3spryfrC9Dq8UXUoRXIS\n" + - "xaFPiLEYt8rLef8f11OypEpmknIibu9jjJtuVZo+SjP6jgLHDwM7rqCZFITM2Qra\n" + - "2iBCt8YVcIiTK137t+EfsdVN/KHiRbc++e9zUbGMEextbtNbdoFOU4dnKBm6Su8l\n" + - "Z5UerNbR8D7+xJKfAEabdi0qI7QFmhTZ/4H/22yrvoD9jMFSBXUTE9ENIX9Hfqom\n" + - "UdsHfuE+5PC0JjkZkhchDO1M7XBX++lBCFsq2abfdpmaX+roVX0iTGboxr5Ag1Cf\n" + - "T2zWyRX/XKnvmdeGICV5qjy/ThuSWvAclazyFxWLamMztJq5BRpfAzKNQRDqlmKw\n" + - "eePtKW2EWUIjFQ5/UAM6Edu/K34ksFxb0w6YGLzQSskGr7gGAipLmpek6vcUSUA1\n" + - "oc9XJGdpx93GDRcqDjKDt/ej06VxG33/pW65ntf5QM/+LScGqaLhAHyEOsBzVIXY\n" + - "BONcadSgzkTrlbSMGAmFAQwDtLUJy1k24D4BB/0brqR0UN1LtO+Lc/vN6X/Um2CZ\n" + - "CM6MRhPnXP63Q9HHkGJ2S8zGWvQLwWL9Y14CFCgm6rACLBSIyPbihhC2OC8afhSy\n" + - "apGkdHtdghS2egs2U8qlJ2Y32IAG9CcUtNkRjxp+/RWSrmZeuL4l7DXCyH5lUadx\n" + - "5bPZhAHqW9408q2rQd9dBg2o7ciGXTJSKVahjuiB/O0gchOnbqnlYJbKbCkntXUo\n" + - "c7h4w1e8MutisSJorh7kbxgxUJSboZzEkiUfnoacPTz6bL+re9tmnpvlee70sIyM\n" + - "BiYRCyPw7Ice4R3XyWtsMTjT/wjZ//whMpWdy2drcJSyhh+GQMbekTVsNWod0lQB\n" + - "JTPUfti2VU7PMB3LjJA+l/T9iWPPx8lirnLhXOOerWKH9I5Wo4Kqv/47aJhfMO6+\n" + - "jmLekAOylq+9DizrslW/EUgQyjIbcWfmyMiV6E2RwbI93tE=\n" + - "=GAhR\n" + + "hQEMA7S1CctZNuA+AQf/SMX7NTOaAynogTVKE9BMWSj5fgK+7sFrCKiLYbungJEu\n" + + "RA/fYqaJNfZN3GARqsHcGaGihQDXr0thnx71+37NhV2cHVeFkeMsHmJf/74lRrHk\n" + + "QBXDv2ez0LxUwhkE15/d/NTlT/fm8Vzce6rsm7/ZvzQIaWYyDCnpHXyftJplKd+Y\n" + + "PW0PaoFRq1wlZKcNUp/1a3xxpbSpvsYkiAxpdGIwvgUIb85KpFN0EWD3aH8C65it\n" + + "Iphuv8CEaKqcO0hchQr7kYclEM0qcmm1ukw8+niTV8TFqAzNZh7DF/IWaMeamgfA\n" + + "P6pAB1oy7YoWUPQgy7mczD76WzPgJjy8y0hxFd9/f9LA2gEZZ/ClAiX0gHglc4oa\n" + + "j5iKIICvtTQzKYL29mW66BUistqMavz6eqHRggoADCBzfgOwuoAQxZMyj33bmrWm\n" + + "831LMu+4sZyx6ihLvZ0YcDKMd7C7pQJ3Ucxt+DJUlTmo6KxzGdwGhq7cUcXwCuer\n" + + "3MoPIV5YQwXBMbYN9fXV+yQagquz0z7r5igE7AQ1d9SyLJoQ3IHXnsa0xcUVZrIs\n" + + "A59LdIXEeRk/Ctjqp34UdTsuUPzervPexY+kNQVSQ2VODhwM5IowzPZFGviPNJYa\n" + + "nGt27c4rsQ3sSC/WkdUxdaVY2+m7JktfnklUyVyC5wE1Nw+bO3sni6FeoP/fVSVi\n" + + "HmPy7vMj23cQcvcAnuUEd4Qua0lwVrN1MTUggfZOzcH4+9rgMn/uYRAwPH9hdLWQ\n" + + "vziQMH5qtJMyWy08m9hIxleoI3+zIGSbra15R+hdWwEaD9+Pak//0Q0thFMeNww7\n" + + "Y8gK8CSbUHbUjefUIx0s+JjrDGtXG8xfl63MLBbU7yLLB4Vcx77Sxxi3yt5DTi0n\n" + + "GmPGRU4LsOYbpPFy\n" + + "=caif\n" + "-----END PGP MESSAGE-----"; } diff --git a/pgpainless-core/src/test/java/org/pgpainless/TestKeysTest.java b/pgpainless-core/src/test/java/org/pgpainless/TestKeysTest.java index d9bb0e9e..4c5cbffe 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/TestKeysTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/TestKeysTest.java @@ -15,23 +15,27 @@ */ package org.pgpainless; +import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertArrayEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.charset.Charset; -import java.util.Arrays; +import java.nio.charset.StandardCharsets; import java.util.Collections; -import junit.framework.TestCase; import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; import org.bouncycastle.util.io.Streams; import org.junit.Test; +import org.pgpainless.algorithm.CompressionAlgorithm; +import org.pgpainless.algorithm.SymmetricKeyAlgorithm; import org.pgpainless.decryption_verification.DecryptionStream; +import org.pgpainless.decryption_verification.OpenPgpMetadata; +import org.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.key.protection.UnprotectedKeysProtector; public class TestKeysTest extends AbstractPGPainlessTest { @@ -45,14 +49,26 @@ public class TestKeysTest extends AbstractPGPainlessTest { } @Test - public void keyIdTest() { - TestCase.assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID()); - TestCase.assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID()); + public void julietKeyTest() { + assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID()); + assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet)); + assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getPublicKey())); + assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getSecretKey())); + assertEquals(TestKeys.JULIET_KEY_ID, TestKeys.JULIET_FINGERPRINT.getKeyId()); + } + + @Test + public void romeoKeyTest() { + assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID()); + assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo)); + assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getPublicKey())); + assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getSecretKey())); + assertEquals(TestKeys.ROMEO_KEY_ID, TestKeys.ROMEO_FINGERPRINT.getKeyId()); } @Test public void decryptVerifyTest() throws Exception { - String encryptedMessage = TestKeys.TEST_MESSAGE_01; + String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET; DecryptionStream decryptor = PGPainless.createDecryptor() .onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes())) @@ -65,10 +81,23 @@ public class TestKeysTest extends AbstractPGPainlessTest { Streams.pipeAll(decryptor, toPlain); decryptor.close(); toPlain.close(); + OpenPgpMetadata metadata = decryptor.getResult(); - byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(Charset.forName("UTF-8")); + byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(StandardCharsets.UTF_8); byte[] actual = toPlain.toByteArray(); - assertTrue(Arrays.equals(expected, actual)); + assertArrayEquals(expected, actual); + + assertTrue(metadata.isIntegrityProtected()); + assertTrue(metadata.isEncrypted()); + assertTrue(metadata.isSigned()); + assertTrue(metadata.isVerified()); + assertEquals(CompressionAlgorithm.ZLIB, metadata.getCompressionAlgorithm()); + assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getSymmetricKeyAlgorithm()); + assertEquals(1, metadata.getSignatureKeyIDs().size()); + assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size()); + assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT)); + assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint()); + } }