From 6926cedf615dc0f079f7b96394328f64a6f92fe9 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 22 Nov 2022 15:41:58 +0100 Subject: [PATCH] Fix compilation errors and simplify LayerIterator by introducing Packet interface --- ...oundTripInlineSignInlineVerifyCmdTest.java | 2 +- .../MessageMetadata.java | 53 +++++++------------ .../OpenPgpMetadata.java | 2 +- .../pgpainless/sop/DetachedVerifyImpl.java | 4 +- 4 files changed, 24 insertions(+), 37 deletions(-) diff --git a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripInlineSignInlineVerifyCmdTest.java b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripInlineSignInlineVerifyCmdTest.java index 1e5310f5..d36ee58f 100644 --- a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripInlineSignInlineVerifyCmdTest.java +++ b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripInlineSignInlineVerifyCmdTest.java @@ -287,7 +287,7 @@ public class RoundTripInlineSignInlineVerifyCmdTest extends CLITest { File cert = writeFile("cert.asc", CERT_1); pipeStringToStdin(msgOut.toString()); ByteArrayOutputStream verificationsOut = pipeStdoutToStream(); - assertSuccess(executeCommand("verify", + assertSuccess(executeCommand("verify", "--stacktrace", sigFile.getAbsolutePath(), cert.getAbsolutePath())); diff --git a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MessageMetadata.java b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MessageMetadata.java index 5318fcb1..da6902c5 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MessageMetadata.java +++ b/pgpainless-core/src/main/java/org/pgpainless/decryption_verification/MessageMetadata.java @@ -9,7 +9,6 @@ import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.function.Function; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -92,7 +91,7 @@ public class MessageMetadata { public @Nonnull Iterator getEncryptionLayers() { return new LayerIterator(message) { @Override - public boolean matches(Nested layer) { + public boolean matches(Packet layer) { return layer instanceof EncryptedData; } @@ -128,7 +127,7 @@ public class MessageMetadata { public @Nonnull Iterator getCompressionLayers() { return new LayerIterator(message) { @Override - boolean matches(Layer layer) { + boolean matches(Packet layer) { return layer instanceof CompressedData; } @@ -242,15 +241,10 @@ public class MessageMetadata { public @Nonnull Iterator> getVerifiedInlineSignaturesByLayer() { return new LayerIterator>(message) { @Override - boolean matches(Nested layer) { + boolean matches(Packet layer) { return layer instanceof Layer; } - @Override - boolean matches(Layer layer) { - return true; - } - @Override List getProperty(Layer last) { List list = new ArrayList<>(); @@ -284,15 +278,10 @@ public class MessageMetadata { public @Nonnull Iterator> getRejectedInlineSignaturesByLayer() { return new LayerIterator>(message) { @Override - boolean matches(Nested layer) { + boolean matches(Packet layer) { return layer instanceof Layer; } - @Override - boolean matches(Layer layer) { - return true; - } - @Override List getProperty(Layer last) { List list = new ArrayList<>(); @@ -334,10 +323,10 @@ public class MessageMetadata { * @return filename * @see RFC4880 §5.9. Literal Data Packet */ - public @Nonnull String getFilename() { + public @Nullable String getFilename() { LiteralData literalData = findLiteralData(); if (literalData == null) { - throw new NoSuchElementException("No Literal Data Packet found."); + return null; } return literalData.getFileName(); } @@ -359,10 +348,10 @@ public class MessageMetadata { * @return modification date * @see RFC4880 §5.9. Literal Data Packet */ - public @Nonnull Date getModificationDate() { + public @Nullable Date getModificationDate() { LiteralData literalData = findLiteralData(); if (literalData == null) { - throw new NoSuchElementException("No Literal Data Packet found."); + return null; } return literalData.getModificationDate(); } @@ -375,10 +364,10 @@ public class MessageMetadata { * @return format * @see RFC4880 §5.9. Literal Data Packet */ - public @Nonnull StreamEncoding getLiteralDataEncoding() { + public @Nullable StreamEncoding getLiteralDataEncoding() { LiteralData literalData = findLiteralData(); if (literalData == null) { - throw new NoSuchElementException("No Literal Data Packet found."); + return null; } return literalData.getFormat(); } @@ -414,7 +403,10 @@ public class MessageMetadata { return firstOrNull(map(getEncryptionLayers(), encryptedData -> encryptedData.decryptionKey)); } - public abstract static class Layer { + public interface Packet { + + } + public abstract static class Layer implements Packet { public static final int MAX_LAYER_DEPTH = 16; protected final int depth; protected final List verifiedDetachedSignatures = new ArrayList<>(); @@ -562,7 +554,7 @@ public class MessageMetadata { } - public interface Nested { + public interface Nested extends Packet { boolean hasNestedChild(); } @@ -760,16 +752,7 @@ public class MessageMetadata { } } - boolean matches(Nested layer) { - return false; - } - - boolean matches(Layer layer) { - if (layer instanceof Nested) { - return matches((Nested) layer); - } - return false; - } + abstract boolean matches(Packet layer); abstract O getProperty(Layer last); } @@ -788,6 +771,10 @@ public class MessageMetadata { }; } + public interface Function { + B apply(A item); + } + private static @Nullable A firstOrNull(Iterator iterator) { if (iterator.hasNext()) { return iterator.next(); 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 e26fb804..be9cf2b1 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 @@ -328,7 +328,7 @@ public class OpenPgpMetadata { return this; } - public Builder setFileName(@Nonnull String fileName) { + public Builder setFileName(@Nullable String fileName) { this.fileName = fileName; return this; } diff --git a/pgpainless-sop/src/main/java/org/pgpainless/sop/DetachedVerifyImpl.java b/pgpainless-sop/src/main/java/org/pgpainless/sop/DetachedVerifyImpl.java index 3a5b3b6a..1ed43941 100644 --- a/pgpainless-sop/src/main/java/org/pgpainless/sop/DetachedVerifyImpl.java +++ b/pgpainless-sop/src/main/java/org/pgpainless/sop/DetachedVerifyImpl.java @@ -16,7 +16,7 @@ import org.bouncycastle.util.io.Streams; import org.pgpainless.PGPainless; import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.DecryptionStream; -import org.pgpainless.decryption_verification.OpenPgpMetadata; +import org.pgpainless.decryption_verification.MessageMetadata; import org.pgpainless.decryption_verification.SignatureVerification; import org.pgpainless.exception.MalformedOpenPgpMessageException; import sop.Verification; @@ -69,7 +69,7 @@ public class DetachedVerifyImpl implements DetachedVerify { Streams.drain(decryptionStream); decryptionStream.close(); - OpenPgpMetadata metadata = decryptionStream.getResult(); + MessageMetadata metadata = decryptionStream.getMetadata(); List verificationList = new ArrayList<>(); for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) {