Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract load PGPSignature utility. Fix close. #110

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/main/java/org/simplify4u/plugins/PGPVerifyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -48,13 +47,9 @@
import org.apache.maven.settings.Proxy;
import org.apache.maven.settings.Settings;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import org.codehaus.plexus.resource.loader.ResourceNotFoundException;
import org.simplify4u.plugins.ArtifactResolver.Configuration;
Expand Down Expand Up @@ -441,13 +436,10 @@ private boolean verifyPGPSignature(Artifact artifact, Artifact ascArtifact) thro
getLog().debug("Artifact sign: " + signatureFile);

try {
InputStream sigInputStream = PGPUtil.getDecoderStream(new FileInputStream(signatureFile));
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, new BcKeyFingerprintCalculator());
PGPSignatureList sigList = (PGPSignatureList) pgpObjectFactory.nextObject();
if (sigList == null) {
throw new MojoFailureException("Invalid signature file: " + signatureFile);
final PGPSignature pgpSignature;
try (FileInputStream input = new FileInputStream(signatureFile)) {
pgpSignature = PGPSignatureUtils.loadSignature(input);
}
PGPSignature pgpSignature = sigList.get(0);

if (weakSignatures.containsKey(pgpSignature.getHashAlgorithm())) {
final String logMessageWeakSignature = "Weak signature algorithm used: "
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/simplify4u/plugins/utils/PGPSignatureUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

package org.simplify4u.plugins.utils;

import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.PGPSignatureList;
import org.bouncycastle.openpgp.PGPUtil;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;

/**
* Utilities for PGP Signature class.
Expand All @@ -35,6 +40,20 @@ private PGPSignatureUtils() {
// No need to instantiate utility class.
}

public static PGPSignature loadSignature(InputStream input) throws IOException {
InputStream sigInputStream = PGPUtil.getDecoderStream(input);
PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, new BcKeyFingerprintCalculator());
Object object = pgpObjectFactory.nextObject();
if (!(object instanceof PGPSignatureList)) {
throw new ProtocolException("File content is not a PGP signature.");
}
PGPSignatureList siglist = (PGPSignatureList) object;
if (siglist.isEmpty()) {
throw new ProtocolException("PGP signature list is empty.");
}
return siglist.get(0);
}

/**
* Read the content of a file into the PGP signature instance (for verification).
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.simplify4u.plugins.utils;

import org.bouncycastle.openpgp.PGPSignature;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.ProtocolException;

import static org.testng.Assert.assertEquals;

public class PGPSignatureUtilsTest {

@Test(expectedExceptions = NullPointerException.class)
public void testLoadSignatureNull() throws IOException {
PGPSignatureUtils.loadSignature(null);
}

@Test(expectedExceptions = ProtocolException.class)
public void testLoadSignatureNoContent() throws IOException {
PGPSignatureUtils.loadSignature(getClass().getResourceAsStream("/empty.asc"));
}

@Test(expectedExceptions = ProtocolException.class)
public void testLoadSignatureContentNotSignature() throws IOException {
PGPSignatureUtils.loadSignature(getClass().getResourceAsStream("/3D8B00E198E21827.asc"));
}

@Test
public void testLoadSignatureContentIsSignature() throws IOException {
PGPSignature signature = PGPSignatureUtils.loadSignature(
getClass().getResourceAsStream("/helloworld-1.0.jar.asc"));
assertEquals(signature.getKeyID(), 0x9F1A263E15FD0AC9L);
}
}
Empty file added src/test/resources/empty.asc
Empty file.
11 changes: 11 additions & 0 deletions src/test/resources/helloworld-1.0.jar.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN PGP SIGNATURE-----

iQEzBAABCgAdFiEERmWD+UgOviRixGswnxomPhX9CskFAl6+yzwACgkQnxomPhX9
CslwWAf+IsAeoaZC0yo38k1PZ58IAeHQP9iavrfZ4LMCMxFacXmBGZF4SVniBmZ3
o7gaQpp+EYi7LikfBDphX0iNchSn/7jGlDq8eK12JCeoyD7s0rYAYu94itQSPuvE
MZWDD//C0pGNSoK14EZB4TdzE2Ey87+lXqBd2NKNdmSTntL+ijyOPZRMTsLs7o6F
cEwRJQ1T2i26/uC2dpiQ4qelk/bo0eZM/BjJp6DZqjmh4CZDaY/vMTxrM5v7LNVE
4ChAcuu3V8oiNMgWicXFRGHNqyEMrJUM6f7yx325si7ziH3l/CL1iGymVt1DdLzq
3R/QK/dX6YGYEUjJJbxWmx7DTc3HDA==
=DNhd
-----END PGP SIGNATURE-----