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);
pipeStringToStdin(msgOut.toString());
ByteArrayOutputStream verificationsOut = pipeStdoutToStream();
assertSuccess(executeCommand("verify",
assertSuccess(executeCommand("verify", "--stacktrace",
sigFile.getAbsolutePath(),
cert.getAbsolutePath()));

View File

@ -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<EncryptedData> getEncryptionLayers() {
return new LayerIterator<EncryptedData>(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<CompressedData> getCompressionLayers() {
return new LayerIterator<CompressedData>(message) {
@Override
boolean matches(Layer layer) {
boolean matches(Packet layer) {
return layer instanceof CompressedData;
}
@ -242,15 +241,10 @@ public class MessageMetadata {
public @Nonnull Iterator<List<SignatureVerification>> getVerifiedInlineSignaturesByLayer() {
return new LayerIterator<List<SignatureVerification>>(message) {
@Override
boolean matches(Nested layer) {
boolean matches(Packet layer) {
return layer instanceof Layer;
}
@Override
boolean matches(Layer layer) {
return true;
}
@Override
List<SignatureVerification> getProperty(Layer last) {
List<SignatureVerification> list = new ArrayList<>();
@ -284,15 +278,10 @@ public class MessageMetadata {
public @Nonnull Iterator<List<SignatureVerification.Failure>> getRejectedInlineSignaturesByLayer() {
return new LayerIterator<List<SignatureVerification.Failure>>(message) {
@Override
boolean matches(Nested layer) {
boolean matches(Packet layer) {
return layer instanceof Layer;
}
@Override
boolean matches(Layer layer) {
return true;
}
@Override
List<SignatureVerification.Failure> getProperty(Layer last) {
List<SignatureVerification.Failure> list = new ArrayList<>();
@ -334,10 +323,10 @@ public class MessageMetadata {
* @return filename
* @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();
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 <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();
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 <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();
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<SignatureVerification> 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<A, B> {
B apply(A item);
}
private static @Nullable <A> A firstOrNull(Iterator<A> iterator) {
if (iterator.hasNext()) {
return iterator.next();

View File

@ -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;
}

View File

@ -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<Verification> verificationList = new ArrayList<>();
for (SignatureVerification signatureVerification : metadata.getVerifiedDetachedSignatures()) {