From be4962c531734f503cde58fd3033b28fdc963837 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sat, 10 Jul 2021 11:07:13 +0200 Subject: [PATCH] Fix KeyRingReader methods not ignoring marker packets --- .../pgpainless/key/parsing/KeyRingReader.java | 104 ++++++++++++++++-- 1 file changed, 93 insertions(+), 11 deletions(-) diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java index 6678cac2..1c7fb902 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java @@ -19,9 +19,14 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import javax.annotation.Nonnull; +import org.bouncycastle.bcpg.MarkerPacket; import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPObjectFactory; import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRing; @@ -98,34 +103,111 @@ public class KeyRingReader { return keyRingCollection(asciiArmored.getBytes(UTF8), isSilent); } - /* - STATIC METHODS - */ - public static PGPPublicKeyRing readPublicKeyRing(@Nonnull InputStream inputStream) throws IOException { - return new PGPPublicKeyRing( - PGPUtil.getDecoderStream(inputStream), + PGPObjectFactory objectFactory = new PGPObjectFactory( + getDecoderStream(inputStream), ImplementationFactory.getInstance().getKeyFingerprintCalculator()); + Object next; + do { + next = objectFactory.nextObject(); + if (next == null) { + break; + } + if (next instanceof MarkerPacket) { + continue; + } + if (next instanceof PGPPublicKeyRing) { + return (PGPPublicKeyRing) next; + } + } while (true); + + return null; } public static PGPPublicKeyRingCollection readPublicKeyRingCollection(@Nonnull InputStream inputStream) throws IOException, PGPException { - return new PGPPublicKeyRingCollection( + PGPObjectFactory objectFactory = new PGPObjectFactory( getDecoderStream(inputStream), ImplementationFactory.getInstance().getKeyFingerprintCalculator()); + + List rings = new ArrayList<>(); + + Object next; + do { + next = objectFactory.nextObject(); + if (next == null) { + break; + } + if (next instanceof MarkerPacket) { + continue; + } + if (next instanceof PGPPublicKeyRing) { + rings.add((PGPPublicKeyRing) next); + } + if (next instanceof PGPPublicKeyRingCollection) { + PGPPublicKeyRingCollection collection = (PGPPublicKeyRingCollection) next; + Iterator iterator = collection.getKeyRings(); + while (iterator.hasNext()) { + rings.add(iterator.next()); + } + } + } while (true); + + return new PGPPublicKeyRingCollection(rings); } - public static PGPSecretKeyRing readSecretKeyRing(@Nonnull InputStream inputStream) throws IOException, PGPException { - return new PGPSecretKeyRing( - PGPUtil.getDecoderStream(inputStream), + public static PGPSecretKeyRing readSecretKeyRing(@Nonnull InputStream inputStream) throws IOException { + PGPObjectFactory objectFactory = new PGPObjectFactory( + getDecoderStream(inputStream), ImplementationFactory.getInstance().getKeyFingerprintCalculator()); + + Object next; + do { + next = objectFactory.nextObject(); + if (next == null) { + break; + } + if (next instanceof MarkerPacket) { + continue; + } + if (next instanceof PGPSecretKeyRing) { + return (PGPSecretKeyRing) next; + } + } while (true); + + return null; } public static PGPSecretKeyRingCollection readSecretKeyRingCollection(@Nonnull InputStream inputStream) throws IOException, PGPException { - return new PGPSecretKeyRingCollection( + PGPObjectFactory objectFactory = new PGPObjectFactory( getDecoderStream(inputStream), ImplementationFactory.getInstance().getKeyFingerprintCalculator()); + + List rings = new ArrayList<>(); + + Object next; + do { + next = objectFactory.nextObject(); + if (next == null) { + break; + } + if (next instanceof MarkerPacket) { + continue; + } + if (next instanceof PGPSecretKeyRing) { + rings.add((PGPSecretKeyRing) next); + } + if (next instanceof PGPSecretKeyRingCollection) { + PGPSecretKeyRingCollection collection = (PGPSecretKeyRingCollection) next; + Iterator iterator = collection.getKeyRings(); + while (iterator.hasNext()) { + rings.add(iterator.next()); + } + } + } while (true); + + return new PGPSecretKeyRingCollection(rings); } public static PGPKeyRingCollection readKeyRingCollection(@Nonnull InputStream inputStream, boolean isSilent)