1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-25 04:17:59 +01:00

Fix checkstyle issues

This commit is contained in:
Paul Schaub 2022-09-27 16:11:55 +02:00
parent 714e424eac
commit 7cb22f1530
8 changed files with 75 additions and 41 deletions

View file

@ -90,7 +90,7 @@ public class MessageMetadata {
return (LiteralData) nested; return (LiteralData) nested;
} }
public static abstract class Layer { public abstract static class Layer {
protected final List<SignatureVerification> verifiedSignatures = new ArrayList<>(); protected final List<SignatureVerification> verifiedSignatures = new ArrayList<>();
protected final List<SignatureVerification.Failure> failedSignatures = new ArrayList<>(); protected final List<SignatureVerification.Failure> failedSignatures = new ArrayList<>();
protected Nested child; protected Nested child;
@ -198,11 +198,11 @@ public class MessageMetadata {
} }
private static abstract class LayerIterator<O> implements Iterator<O> { private abstract static class LayerIterator<O> implements Iterator<O> {
private Nested current; private Nested current;
Layer last = null; Layer last = null;
public LayerIterator(Message message) { LayerIterator(Message message) {
super(); super();
this.current = message.child; this.current = message.child;
if (matches(current)) { if (matches(current)) {

View file

@ -50,6 +50,8 @@ import org.pgpainless.key.protection.UnlockSecretKey;
import org.pgpainless.signature.SignatureUtils; import org.pgpainless.signature.SignatureUtils;
import org.pgpainless.util.Passphrase; import org.pgpainless.util.Passphrase;
import org.pgpainless.util.Tuple; import org.pgpainless.util.Tuple;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
@ -60,6 +62,8 @@ import java.util.List;
public class OpenPgpMessageInputStream extends InputStream { public class OpenPgpMessageInputStream extends InputStream {
private static final Logger LOGGER = LoggerFactory.getLogger(OpenPgpMessageInputStream.class);
protected final PDA automaton = new PDA(); protected final PDA automaton = new PDA();
protected final ConsumerOptions options; protected final ConsumerOptions options;
protected final OpenPgpMetadata.Builder resultBuilder; protected final OpenPgpMetadata.Builder resultBuilder;
@ -519,7 +523,7 @@ public class OpenPgpMessageInputStream extends InputStream {
private final PGPOnePassSignatureList list; private final PGPOnePassSignatureList list;
private final List<Boolean> encapsulating; private final List<Boolean> encapsulating;
public PGPOnePassSignatureListWrapper(PGPOnePassSignatureList signatures, List<Boolean> encapsulating) { PGPOnePassSignatureListWrapper(PGPOnePassSignatureList signatures, List<Boolean> encapsulating) {
this.list = signatures; this.list = signatures;
this.encapsulating = encapsulating; this.encapsulating = encapsulating;
} }
@ -529,7 +533,7 @@ public class OpenPgpMessageInputStream extends InputStream {
} }
} }
private static class Signatures { private static final class Signatures {
final ConsumerOptions options; final ConsumerOptions options;
List<PGPSignature> detachedSignatures = new ArrayList<>(); List<PGPSignature> detachedSignatures = new ArrayList<>();
List<PGPSignature> prependedSignatures = new ArrayList<>(); List<PGPSignature> prependedSignatures = new ArrayList<>();
@ -551,7 +555,6 @@ public class OpenPgpMessageInputStream extends InputStream {
} }
void addPrependedSignatures(PGPSignatureList signatures) { void addPrependedSignatures(PGPSignatureList signatures) {
System.out.println("Adding " + signatures.size() + " prepended Signatures");
for (PGPSignature signature : signatures) { for (PGPSignature signature : signatures) {
long keyId = SignatureUtils.determineIssuerKeyId(signature); long keyId = SignatureUtils.determineIssuerKeyId(signature);
PGPPublicKeyRing certificate = findCertificate(keyId); PGPPublicKeyRing certificate = findCertificate(keyId);
@ -561,7 +564,6 @@ public class OpenPgpMessageInputStream extends InputStream {
} }
void addOnePassSignatures(PGPOnePassSignatureListWrapper signatures) { void addOnePassSignatures(PGPOnePassSignatureListWrapper signatures) {
System.out.println("Adding " + signatures.size() + " OPSs");
for (PGPOnePassSignature ops : signatures.list) { for (PGPOnePassSignature ops : signatures.list) {
PGPPublicKeyRing certificate = findCertificate(ops.getKeyID()); PGPPublicKeyRing certificate = findCertificate(ops.getKeyID());
initialize(ops, certificate); initialize(ops, certificate);
@ -570,7 +572,6 @@ public class OpenPgpMessageInputStream extends InputStream {
} }
void addOnePassCorrespondingSignatures(PGPSignatureList signatures) { void addOnePassCorrespondingSignatures(PGPSignatureList signatures) {
System.out.println("Adding " + signatures.size() + " Corresponding Signatures");
for (PGPSignature signature : signatures) { for (PGPSignature signature : signatures) {
correspondingSignatures.add(signature); correspondingSignatures.add(signature);
} }
@ -631,9 +632,9 @@ public class OpenPgpMessageInputStream extends InputStream {
try { try {
verified = detached.verify(); verified = detached.verify();
} catch (PGPException e) { } catch (PGPException e) {
System.out.println(e.getMessage()); LOGGER.debug("Cannot verify detached signature.", e);
} }
System.out.println("Detached Signature by " + Long.toHexString(detached.getKeyID()) + " is " + (verified ? "verified" : "unverified")); LOGGER.debug("Detached Signature by " + Long.toHexString(detached.getKeyID()) + " is " + (verified ? "verified" : "unverified"));
} }
for (PGPSignature prepended : prependedSignatures) { for (PGPSignature prepended : prependedSignatures) {
@ -641,9 +642,9 @@ public class OpenPgpMessageInputStream extends InputStream {
try { try {
verified = prepended.verify(); verified = prepended.verify();
} catch (PGPException e) { } catch (PGPException e) {
System.out.println(e.getMessage()); LOGGER.debug("Cannot verify prepended signature.", e);
} }
System.out.println("Prepended Signature by " + Long.toHexString(prepended.getKeyID()) + " is " + (verified ? "verified" : "unverified")); LOGGER.debug("Prepended Signature by " + Long.toHexString(prepended.getKeyID()) + " is " + (verified ? "verified" : "unverified"));
} }
@ -654,9 +655,9 @@ public class OpenPgpMessageInputStream extends InputStream {
try { try {
verified = ops.verify(signature); verified = ops.verify(signature);
} catch (PGPException e) { } catch (PGPException e) {
System.out.println(e.getMessage()); LOGGER.debug("Cannot verify OPS signature.", e);
} }
System.out.println("One-Pass-Signature by " + Long.toHexString(ops.getKeyID()) + " is " + (verified ? "verified" : "unverified")); LOGGER.debug("One-Pass-Signature by " + Long.toHexString(ops.getKeyID()) + " is " + (verified ? "verified" : "unverified"));
} }
} }
} }

View file

@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification.automaton; package org.pgpainless.decryption_verification.automaton;
import org.bouncycastle.openpgp.PGPCompressedData; import org.bouncycastle.openpgp.PGPCompressedData;

View file

@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification.automaton; package org.pgpainless.decryption_verification.automaton;
import org.pgpainless.exception.MalformedOpenPgpMessageException; import org.pgpainless.exception.MalformedOpenPgpMessageException;

View file

@ -1,3 +1,7 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification.automaton; package org.pgpainless.decryption_verification.automaton;
public enum StackAlphabet { public enum StackAlphabet {

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Pushdown Automaton to verify validity of packet sequences according to the OpenPGP Message format.
*/
package org.pgpainless.decryption_verification.automaton;

View file

@ -35,6 +35,7 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.StreamEncoding; import org.pgpainless.algorithm.StreamEncoding;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.encryption_signing.EncryptionOptions; import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult; import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream; import org.pgpainless.encryption_signing.EncryptionStream;
@ -118,7 +119,7 @@ public class OpenPgpMessageInputStreamTest {
"=K9Zl\n" + "=K9Zl\n" +
"-----END PGP MESSAGE-----"; "-----END PGP MESSAGE-----";
public static final String SIG_LIT = "" + public static final String SIG_COMP_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" + "-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" + "Version: BCPG v1.71\n" +
"\n" + "\n" +
@ -235,9 +236,9 @@ public class OpenPgpMessageInputStreamTest {
} }
public static void genKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { public static void genKey() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException {
System.out.println(PGPainless.asciiArmor( PGPainless.asciiArmor(
PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>") PGPainless.generateKeyRing().modernKeyRing("Alice <alice@pgpainless.org>"),
)); System.out);
} }
public static void genSIG_LIT() throws PGPException, IOException { public static void genSIG_LIT() throws PGPException, IOException {
@ -265,54 +266,46 @@ public class OpenPgpMessageInputStreamTest {
armorOut.close(); armorOut.close();
String armored = out.toString(); String armored = out.toString();
// CHECKSTYLE:OFF
System.out.println(armored System.out.println(armored
.replace("-----BEGIN PGP SIGNATURE-----\n", "-----BEGIN PGP MESSAGE-----\n") .replace("-----BEGIN PGP SIGNATURE-----\n", "-----BEGIN PGP MESSAGE-----\n")
.replace("-----END PGP SIGNATURE-----", "-----END PGP MESSAGE-----")); .replace("-----END PGP SIGNATURE-----", "-----END PGP MESSAGE-----"));
// CHECKSTYLE:ON
} }
public static void genSENC_LIT() throws PGPException, IOException { public static void genSENC_LIT() throws PGPException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream enc = PGPainless.encryptAndOrSign() EncryptionStream enc = PGPainless.encryptAndOrSign()
.onOutputStream(out) .onOutputStream(System.out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get() .withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.addPassphrase(Passphrase.fromPassword(PASSPHRASE))) .addPassphrase(Passphrase.fromPassword(PASSPHRASE)))
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED)); .overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));
enc.write(PLAINTEXT.getBytes(StandardCharsets.UTF_8)); enc.write(PLAINTEXT.getBytes(StandardCharsets.UTF_8));
enc.close(); enc.close();
System.out.println(out);
} }
public static void genPENC_COMP_LIT() throws IOException, PGPException { public static void genPENC_COMP_LIT() throws IOException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY); PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKeys); PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKeys);
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream enc = PGPainless.encryptAndOrSign() EncryptionStream enc = PGPainless.encryptAndOrSign()
.onOutputStream(out) .onOutputStream(System.out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get() .withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.addRecipient(cert)) .addRecipient(cert))
.overrideCompressionAlgorithm(CompressionAlgorithm.ZLIB)); .overrideCompressionAlgorithm(CompressionAlgorithm.ZLIB));
Streams.pipeAll(new ByteArrayInputStream(PLAINTEXT.getBytes(StandardCharsets.UTF_8)), enc); Streams.pipeAll(new ByteArrayInputStream(PLAINTEXT.getBytes(StandardCharsets.UTF_8)), enc);
enc.close(); enc.close();
System.out.println(out);
} }
public static void genOPS_LIT_SIG() throws PGPException, IOException { public static void genOPS_LIT_SIG() throws PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY); PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream enc = PGPainless.encryptAndOrSign() EncryptionStream enc = PGPainless.encryptAndOrSign()
.onOutputStream(out) .onOutputStream(System.out)
.withOptions(ProducerOptions.sign(SigningOptions.get() .withOptions(ProducerOptions.sign(SigningOptions.get()
.addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys)) .addSignature(SecretKeyRingProtector.unprotectedKeys(), secretKeys))
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED)); .overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));
Streams.pipeAll(new ByteArrayInputStream(PLAINTEXT.getBytes(StandardCharsets.UTF_8)), enc); Streams.pipeAll(new ByteArrayInputStream(PLAINTEXT.getBytes(StandardCharsets.UTF_8)), enc);
enc.close(); enc.close();
System.out.println(out);
} }
interface Processor { interface Processor {
@ -322,7 +315,8 @@ public class OpenPgpMessageInputStreamTest {
private static Stream<Arguments> provideMessageProcessors() { private static Stream<Arguments> provideMessageProcessors() {
return Stream.of( return Stream.of(
Arguments.of(Named.of("read(buf,off,len)", (Processor) OpenPgpMessageInputStreamTest::processReadBuffered)), Arguments.of(Named.of("read(buf,off,len)", (Processor) OpenPgpMessageInputStreamTest::processReadBuffered)),
Arguments.of(Named.of("read()", (Processor) OpenPgpMessageInputStreamTest::processReadSequential))); Arguments.of(Named.of("read()", (Processor) OpenPgpMessageInputStreamTest::processReadSequential))
);
} }
@ParameterizedTest(name = "Process LIT using {0}") @ParameterizedTest(name = "Process LIT using {0}")
@ -349,7 +343,8 @@ public class OpenPgpMessageInputStreamTest {
@ParameterizedTest(name = "Process COMP(LIT) using {0}") @ParameterizedTest(name = "Process COMP(LIT) using {0}")
@MethodSource("provideMessageProcessors") @MethodSource("provideMessageProcessors")
public void testProcessCOMP_LIT(Processor processor) throws PGPException, IOException { public void testProcessCOMP_LIT(Processor processor)
throws PGPException, IOException {
Tuple<String, MessageMetadata> result = processor.process(COMP_LIT, ConsumerOptions.get()); Tuple<String, MessageMetadata> result = processor.process(COMP_LIT, ConsumerOptions.get());
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
@ -366,7 +361,8 @@ public class OpenPgpMessageInputStreamTest {
@ParameterizedTest(name = "Process COMP(COMP(LIT)) using {0}") @ParameterizedTest(name = "Process COMP(COMP(LIT)) using {0}")
@MethodSource("provideMessageProcessors") @MethodSource("provideMessageProcessors")
public void testProcessCOMP_COMP_LIT(Processor processor) throws PGPException, IOException { public void testProcessCOMP_COMP_LIT(Processor processor)
throws PGPException, IOException {
Tuple<String, MessageMetadata> result = processor.process(COMP_COMP_LIT, ConsumerOptions.get()); Tuple<String, MessageMetadata> result = processor.process(COMP_COMP_LIT, ConsumerOptions.get());
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
@ -376,29 +372,37 @@ public class OpenPgpMessageInputStreamTest {
assertEquals(CompressionAlgorithm.ZIP, compressionAlgorithms.next()); assertEquals(CompressionAlgorithm.ZIP, compressionAlgorithms.next());
assertEquals(CompressionAlgorithm.BZIP2, compressionAlgorithms.next()); assertEquals(CompressionAlgorithm.BZIP2, compressionAlgorithms.next());
assertFalse(compressionAlgorithms.hasNext()); assertFalse(compressionAlgorithms.hasNext());
assertNull(metadata.getEncryptionAlgorithm());
} }
@ParameterizedTest(name = "Process SIG LIT using {0}") @ParameterizedTest(name = "Process SIG COMP(LIT) using {0}")
@MethodSource("provideMessageProcessors") @MethodSource("provideMessageProcessors")
public void testProcessSIG_LIT(Processor processor) throws PGPException, IOException { public void testProcessSIG_COMP_LIT(Processor processor) throws PGPException, IOException {
PGPPublicKeyRing cert = PGPainless.extractCertificate( PGPPublicKeyRing cert = PGPainless.extractCertificate(
PGPainless.readKeyRing().secretKeyRing(KEY)); PGPainless.readKeyRing().secretKeyRing(KEY));
Tuple<String, MessageMetadata> result = processor.process(SIG_LIT, ConsumerOptions.get() Tuple<String, MessageMetadata> result = processor.process(SIG_COMP_LIT, ConsumerOptions.get()
.addVerificationCert(cert)); .addVerificationCert(cert));
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
MessageMetadata metadata = result.getB();
assertEquals(CompressionAlgorithm.ZIP, metadata.getCompressionAlgorithm());
assertNull(metadata.getEncryptionAlgorithm());
} }
@ParameterizedTest(name = "Process SENC(LIT) using {0}") @ParameterizedTest(name = "Process SENC(LIT) using {0}")
@MethodSource("provideMessageProcessors") @MethodSource("provideMessageProcessors")
public void testProcessSENC_LIT(Processor processor) throws PGPException, IOException { public void testProcessSENC_LIT(Processor processor) throws PGPException, IOException {
Tuple<String, MessageMetadata> result = processor.process(SENC_LIT, ConsumerOptions.get().addDecryptionPassphrase(Passphrase.fromPassword(PASSPHRASE))); Tuple<String, MessageMetadata> result = processor.process(SENC_LIT, ConsumerOptions.get()
.addDecryptionPassphrase(Passphrase.fromPassword(PASSPHRASE)));
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
MessageMetadata metadata = result.getB();
assertNull(metadata.getCompressionAlgorithm());
assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getEncryptionAlgorithm());
} }
@ParameterizedTest(name = "Process PENC(LIT) using {0}") @ParameterizedTest(name = "Process PENC(COMP(LIT)) using {0}")
@MethodSource("provideMessageProcessors") @MethodSource("provideMessageProcessors")
public void testProcessPENC_COMP_LIT(Processor processor) throws IOException, PGPException { public void testProcessPENC_COMP_LIT(Processor processor) throws IOException, PGPException {
PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY); PGPSecretKeyRing secretKeys = PGPainless.readKeyRing().secretKeyRing(KEY);
@ -406,6 +410,9 @@ public class OpenPgpMessageInputStreamTest {
.addDecryptionKey(secretKeys)); .addDecryptionKey(secretKeys));
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
MessageMetadata metadata = result.getB();
assertEquals(CompressionAlgorithm.ZLIB, metadata.getCompressionAlgorithm());
assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getEncryptionAlgorithm());
} }
@ParameterizedTest(name = "Process OPS LIT SIG using {0}") @ParameterizedTest(name = "Process OPS LIT SIG using {0}")
@ -416,6 +423,9 @@ public class OpenPgpMessageInputStreamTest {
.addVerificationCert(cert)); .addVerificationCert(cert));
String plain = result.getA(); String plain = result.getA();
assertEquals(PLAINTEXT, plain); assertEquals(PLAINTEXT, plain);
MessageMetadata metadata = result.getB();
assertNull(metadata.getEncryptionAlgorithm());
assertNull(metadata.getCompressionAlgorithm());
} }
private static Tuple<String, MessageMetadata> processReadBuffered(String armoredMessage, ConsumerOptions options) throws PGPException, IOException { private static Tuple<String, MessageMetadata> processReadBuffered(String armoredMessage, ConsumerOptions options) throws PGPException, IOException {

View file

@ -9,6 +9,8 @@ import org.bouncycastle.openpgp.PGPException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.algorithm.OpenPgpPacket; import org.pgpainless.algorithm.OpenPgpPacket;
import org.pgpainless.util.ArmoredInputStreamFactory; import org.pgpainless.util.ArmoredInputStreamFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -18,6 +20,7 @@ import java.nio.charset.StandardCharsets;
public class TeeBCPGInputStreamTest { public class TeeBCPGInputStreamTest {
private static final Logger LOGGER = LoggerFactory.getLogger(TeeBCPGInputStreamTest.class);
private static final String INBAND_SIGNED = "-----BEGIN PGP MESSAGE-----\n" + private static final String INBAND_SIGNED = "-----BEGIN PGP MESSAGE-----\n" +
"Version: PGPainless\n" + "Version: PGPainless\n" +
"\n" + "\n" +
@ -48,11 +51,11 @@ public class TeeBCPGInputStreamTest {
int tag; int tag;
while ((tag = nestedTeeIn.nextPacketTag()) != -1) { while ((tag = nestedTeeIn.nextPacketTag()) != -1) {
System.out.println(OpenPgpPacket.requireFromTag(tag)); LOGGER.debug(OpenPgpPacket.requireFromTag(tag).toString());
Packet packet = nestedTeeIn.readPacket(); Packet packet = nestedTeeIn.readPacket();
} }
nestedArmorOut.close(); nestedArmorOut.close();
System.out.println(nestedOut); LOGGER.debug(nestedOut.toString());
} }
} }