1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-18 02:12:06 +01:00

Fix compilation errors and simplify LayerIterator by introducing Packet interface

This commit is contained in:
Paul Schaub 2022-11-22 15:41:58 +01:00
parent 8f6227c14b
commit 6926cedf61
4 changed files with 24 additions and 37 deletions

View file

@ -287,7 +287,7 @@ public class RoundTripInlineSignInlineVerifyCmdTest extends CLITest {
File cert = writeFile("cert.asc", CERT_1); File cert = writeFile("cert.asc", CERT_1);
pipeStringToStdin(msgOut.toString()); pipeStringToStdin(msgOut.toString());
ByteArrayOutputStream verificationsOut = pipeStdoutToStream(); ByteArrayOutputStream verificationsOut = pipeStdoutToStream();
assertSuccess(executeCommand("verify", assertSuccess(executeCommand("verify", "--stacktrace",
sigFile.getAbsolutePath(), sigFile.getAbsolutePath(),
cert.getAbsolutePath())); cert.getAbsolutePath()));

View file

@ -9,7 +9,6 @@ import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.Function;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -92,7 +91,7 @@ public class MessageMetadata {
public @Nonnull Iterator<EncryptedData> getEncryptionLayers() { public @Nonnull Iterator<EncryptedData> getEncryptionLayers() {
return new LayerIterator<EncryptedData>(message) { return new LayerIterator<EncryptedData>(message) {
@Override @Override
public boolean matches(Nested layer) { public boolean matches(Packet layer) {
return layer instanceof EncryptedData; return layer instanceof EncryptedData;
} }
@ -128,7 +127,7 @@ public class MessageMetadata {
public @Nonnull Iterator<CompressedData> getCompressionLayers() { public @Nonnull Iterator<CompressedData> getCompressionLayers() {
return new LayerIterator<CompressedData>(message) { return new LayerIterator<CompressedData>(message) {
@Override @Override
boolean matches(Layer layer) { boolean matches(Packet layer) {
return layer instanceof CompressedData; return layer instanceof CompressedData;
} }
@ -242,15 +241,10 @@ public class MessageMetadata {
public @Nonnull Iterator<List<SignatureVerification>> getVerifiedInlineSignaturesByLayer() { public @Nonnull Iterator<List<SignatureVerification>> getVerifiedInlineSignaturesByLayer() {
return new LayerIterator<List<SignatureVerification>>(message) { return new LayerIterator<List<SignatureVerification>>(message) {
@Override @Override
boolean matches(Nested layer) { boolean matches(Packet layer) {
return layer instanceof Layer; return layer instanceof Layer;
} }
@Override
boolean matches(Layer layer) {
return true;
}
@Override @Override
List<SignatureVerification> getProperty(Layer last) { List<SignatureVerification> getProperty(Layer last) {
List<SignatureVerification> list = new ArrayList<>(); List<SignatureVerification> list = new ArrayList<>();
@ -284,15 +278,10 @@ public class MessageMetadata {
public @Nonnull Iterator<List<SignatureVerification.Failure>> getRejectedInlineSignaturesByLayer() { public @Nonnull Iterator<List<SignatureVerification.Failure>> getRejectedInlineSignaturesByLayer() {
return new LayerIterator<List<SignatureVerification.Failure>>(message) { return new LayerIterator<List<SignatureVerification.Failure>>(message) {
@Override @Override
boolean matches(Nested layer) { boolean matches(Packet layer) {
return layer instanceof Layer; return layer instanceof Layer;
} }
@Override
boolean matches(Layer layer) {
return true;
}
@Override @Override
List<SignatureVerification.Failure> getProperty(Layer last) { List<SignatureVerification.Failure> getProperty(Layer last) {
List<SignatureVerification.Failure> list = new ArrayList<>(); List<SignatureVerification.Failure> list = new ArrayList<>();
@ -334,10 +323,10 @@ public class MessageMetadata {
* @return filename * @return filename
* @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a> * @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a>
*/ */
public @Nonnull String getFilename() { public @Nullable String getFilename() {
LiteralData literalData = findLiteralData(); LiteralData literalData = findLiteralData();
if (literalData == null) { if (literalData == null) {
throw new NoSuchElementException("No Literal Data Packet found."); return null;
} }
return literalData.getFileName(); return literalData.getFileName();
} }
@ -359,10 +348,10 @@ public class MessageMetadata {
* @return modification date * @return modification date
* @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a> * @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a>
*/ */
public @Nonnull Date getModificationDate() { public @Nullable Date getModificationDate() {
LiteralData literalData = findLiteralData(); LiteralData literalData = findLiteralData();
if (literalData == null) { if (literalData == null) {
throw new NoSuchElementException("No Literal Data Packet found."); return null;
} }
return literalData.getModificationDate(); return literalData.getModificationDate();
} }
@ -375,10 +364,10 @@ public class MessageMetadata {
* @return format * @return format
* @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a> * @see <a href="https://www.rfc-editor.org/rfc/rfc4880#section-5.9">RFC4880 §5.9. Literal Data Packet</a>
*/ */
public @Nonnull StreamEncoding getLiteralDataEncoding() { public @Nullable StreamEncoding getLiteralDataEncoding() {
LiteralData literalData = findLiteralData(); LiteralData literalData = findLiteralData();
if (literalData == null) { if (literalData == null) {
throw new NoSuchElementException("No Literal Data Packet found."); return null;
} }
return literalData.getFormat(); return literalData.getFormat();
} }
@ -414,7 +403,10 @@ public class MessageMetadata {
return firstOrNull(map(getEncryptionLayers(), encryptedData -> encryptedData.decryptionKey)); 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; public static final int MAX_LAYER_DEPTH = 16;
protected final int depth; protected final int depth;
protected final List<SignatureVerification> verifiedDetachedSignatures = new ArrayList<>(); protected final List<SignatureVerification> verifiedDetachedSignatures = new ArrayList<>();
@ -562,7 +554,7 @@ public class MessageMetadata {
} }
public interface Nested { public interface Nested extends Packet {
boolean hasNestedChild(); boolean hasNestedChild();
} }
@ -760,16 +752,7 @@ public class MessageMetadata {
} }
} }
boolean matches(Nested layer) { abstract boolean matches(Packet layer);
return false;
}
boolean matches(Layer layer) {
if (layer instanceof Nested) {
return matches((Nested) layer);
}
return false;
}
abstract O getProperty(Layer last); abstract O getProperty(Layer last);
} }
@ -788,6 +771,10 @@ public class MessageMetadata {
}; };
} }
public interface Function<A, B> {
B apply(A item);
}
private static @Nullable <A> A firstOrNull(Iterator<A> iterator) { private static @Nullable <A> A firstOrNull(Iterator<A> iterator) {
if (iterator.hasNext()) { if (iterator.hasNext()) {
return iterator.next(); return iterator.next();

View file

@ -328,7 +328,7 @@ public class OpenPgpMetadata {
return this; return this;
} }
public Builder setFileName(@Nonnull String fileName) { public Builder setFileName(@Nullable String fileName) {
this.fileName = fileName; this.fileName = fileName;
return this; return this;
} }

View file

@ -16,7 +16,7 @@ import org.bouncycastle.util.io.Streams;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.ConsumerOptions; import org.pgpainless.decryption_verification.ConsumerOptions;
import org.pgpainless.decryption_verification.DecryptionStream; 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.decryption_verification.SignatureVerification;
import org.pgpainless.exception.MalformedOpenPgpMessageException; import org.pgpainless.exception.MalformedOpenPgpMessageException;
import sop.Verification; import sop.Verification;
@ -69,7 +69,7 @@ public class DetachedVerifyImpl implements DetachedVerify {
Streams.drain(decryptionStream); Streams.drain(decryptionStream);
decryptionStream.close(); decryptionStream.close();
OpenPgpMetadata metadata = decryptionStream.getResult(); MessageMetadata metadata = decryptionStream.getMetadata();
List<Verification> verificationList = new ArrayList<>(); List<Verification> verificationList = new ArrayList<>();
for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) { for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) {