1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Do not throw PGPException in OpenPgpV4Fingerprint

This commit is contained in:
Paul Schaub 2018-07-09 16:40:02 +02:00
parent 6fa7c33b7a
commit 260ecaaea3
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 18 additions and 28 deletions

View file

@ -76,12 +76,7 @@ public class DecryptionBuilder implements DecryptionBuilderInterface {
Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>(); Set<PGPPublicKeyRing> publicKeyRings = new HashSet<>();
for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) { for (Iterator<PGPPublicKeyRing> i = publicKeyRingCollection.getKeyRings(); i.hasNext(); ) {
PGPPublicKeyRing p = i.next(); PGPPublicKeyRing p = i.next();
OpenPgpV4Fingerprint fingerprint; OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(p);
try {
fingerprint = new OpenPgpV4Fingerprint(p);
} catch (PGPException e) {
throw new IllegalArgumentException(e);
}
if (trustedKeyIds.contains(fingerprint)) { if (trustedKeyIds.contains(fingerprint)) {
publicKeyRings.add(p); publicKeyRings.add(p);
} }

View file

@ -19,7 +19,6 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm; import org.pgpainless.pgpainless.algorithm.CompressionAlgorithm;
@ -94,7 +93,7 @@ public class PainlessResult {
return !verifiedSignaturesFingerprints.isEmpty(); return !verifiedSignaturesFingerprints.isEmpty();
} }
public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) throws PGPException { public boolean containsVerifiedSignatureFrom(PGPPublicKeyRing publicKeys) {
for (PGPPublicKey key : publicKeys) { for (PGPPublicKey key : publicKeys) {
OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key); OpenPgpV4Fingerprint fingerprint = new OpenPgpV4Fingerprint(key);
if (verifiedSignaturesFingerprints.contains(fingerprint)) { if (verifiedSignaturesFingerprints.contains(fingerprint)) {

View file

@ -20,7 +20,6 @@ import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.PGPSecretKey;
@ -40,38 +39,38 @@ public class OpenPgpV4Fingerprint implements CharSequence, Comparable<OpenPgpV4F
* XEP-0373 §4.1: The OpenPGP Public-Key Data Node about how to obtain the fingerprint</a> * XEP-0373 §4.1: The OpenPGP Public-Key Data Node about how to obtain the fingerprint</a>
* @param fingerprint hexadecimal representation of the fingerprint. * @param fingerprint hexadecimal representation of the fingerprint.
*/ */
public OpenPgpV4Fingerprint(String fingerprint) throws PGPException { public OpenPgpV4Fingerprint(String fingerprint) {
if (fingerprint == null) { if (fingerprint == null) {
throw new NullPointerException("Fingerprint MUST NOT be null."); throw new NullPointerException("Fingerprint MUST NOT be null.");
} }
String fp = fingerprint.trim().toUpperCase(); String fp = fingerprint.trim().toUpperCase();
if (!isValid(fp)) { if (!isValid(fp)) {
throw new PGPException("Fingerprint " + fingerprint + throw new IllegalArgumentException("Fingerprint " + fingerprint +
" does not appear to be a valid OpenPGP v4 fingerprint."); " does not appear to be a valid OpenPGP v4 fingerprint.");
} }
this.fingerprint = fp; this.fingerprint = fp;
} }
public OpenPgpV4Fingerprint(byte[] bytes) throws PGPException { public OpenPgpV4Fingerprint(byte[] bytes) {
this(new String(bytes, Charset.forName("UTF-8"))); this(new String(bytes, Charset.forName("UTF-8")));
} }
public OpenPgpV4Fingerprint(PGPPublicKey key) throws PGPException { public OpenPgpV4Fingerprint(PGPPublicKey key) {
this(Hex.encode(key.getFingerprint())); this(Hex.encode(key.getFingerprint()));
if (key.getVersion() != 4) { if (key.getVersion() != 4) {
throw new PGPException("Key is not a v4 OpenPgp key."); throw new IllegalArgumentException("Key is not a v4 OpenPgp key.");
} }
} }
public OpenPgpV4Fingerprint(PGPSecretKey key) throws PGPException { public OpenPgpV4Fingerprint(PGPSecretKey key) {
this(key.getPublicKey()); this(key.getPublicKey());
} }
public OpenPgpV4Fingerprint(PGPPublicKeyRing ring) throws PGPException { public OpenPgpV4Fingerprint(PGPPublicKeyRing ring) {
this(ring.getPublicKey()); this(ring.getPublicKey());
} }
public OpenPgpV4Fingerprint(PGPSecretKeyRing ring) throws PGPException { public OpenPgpV4Fingerprint(PGPSecretKeyRing ring) {
this(ring.getPublicKey()); this(ring.getPublicKey());
} }

View file

@ -19,8 +19,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;

View file

@ -19,47 +19,46 @@ import static junit.framework.TestCase.assertEquals;
import java.io.IOException; import java.io.IOException;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.junit.Test; import org.junit.Test;
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint; import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
public class OpenPgpV4FingerprintTest { public class OpenPgpV4FingerprintTest {
@Test(expected = PGPException.class) @Test(expected = IllegalArgumentException.class)
public void fpTooShort() throws PGPException { public void fpTooShort() {
String fp = "484f57414c495645"; // Asking Mark String fp = "484f57414c495645"; // Asking Mark
new OpenPgpV4Fingerprint(fp); new OpenPgpV4Fingerprint(fp);
} }
@Test(expected = PGPException.class) @Test(expected = IllegalArgumentException.class)
public void invalidHexTest() throws PGPException { public void invalidHexTest() {
String fp = "UNFORTUNATELYTHISISNOVALIDHEXADECIMALDOH"; String fp = "UNFORTUNATELYTHISISNOVALIDHEXADECIMALDOH";
new OpenPgpV4Fingerprint(fp); new OpenPgpV4Fingerprint(fp);
} }
@Test @Test
public void validFingerprintTest() throws PGPException { public void validFingerprintTest() {
String fp = "4A4F48414E4E53454E2049532041204E45524421"; String fp = "4A4F48414E4E53454E2049532041204E45524421";
OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp); OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp);
assertEquals(fp, finger.toString()); assertEquals(fp, finger.toString());
} }
@Test @Test
public void convertsToUpperCaseTest() throws PGPException { public void convertsToUpperCaseTest() {
String fp = "444f4e5420552048415645204120484f4242593f"; String fp = "444f4e5420552048415645204120484f4242593f";
OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp); OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint(fp);
assertEquals("444F4E5420552048415645204120484F4242593F", finger.toString()); assertEquals("444F4E5420552048415645204120484F4242593F", finger.toString());
} }
@Test @Test
public void equalsOtherFingerprintTest() throws PGPException { public void equalsOtherFingerprintTest() {
OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint("5448452043414b452049532041204c4945212121"); OpenPgpV4Fingerprint finger = new OpenPgpV4Fingerprint("5448452043414b452049532041204c4945212121");
assertEquals(finger, new OpenPgpV4Fingerprint("5448452043414B452049532041204C4945212121")); assertEquals(finger, new OpenPgpV4Fingerprint("5448452043414B452049532041204C4945212121"));
} }
@Test @Test
public void keyIdTest() throws IOException, PGPException { public void keyIdTest() throws IOException {
PGPPublicKey key = TestKeys.getJulietPublicKeyRing().getPublicKey(); PGPPublicKey key = TestKeys.getJulietPublicKeyRing().getPublicKey();
long keyId = key.getKeyID(); long keyId = key.getKeyID();