Skip to content

Commit a8d871c

Browse files
committed
Add OpenPGPKeyReader .readKeys(), readCertificates() methods
1 parent 44f5bc3 commit a8d871c

File tree

1 file changed

+86
-16
lines changed

1 file changed

+86
-16
lines changed

pg/src/main/java/org/bouncycastle/openpgp/api/OpenPGPKeyReader.java

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import org.bouncycastle.openpgp.PGPMarker;
55
import org.bouncycastle.openpgp.PGPObjectFactory;
66
import org.bouncycastle.openpgp.PGPPublicKeyRing;
7-
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
87
import org.bouncycastle.openpgp.PGPSecretKeyRing;
9-
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
108
import org.bouncycastle.openpgp.PGPUtil;
119
import org.bouncycastle.util.io.Streams;
1210

@@ -250,27 +248,99 @@ else if (object instanceof PGPPublicKeyRing)
250248
{
251249
certsOrKeys.add(new OpenPGPCertificate((PGPPublicKeyRing) object, implementation, policy));
252250
}
253-
else if (object instanceof PGPSecretKeyRingCollection)
251+
else
252+
{
253+
throw new IOException("Neither a certificate, nor secret key.");
254+
}
255+
}
256+
return certsOrKeys;
257+
}
258+
259+
public List<OpenPGPCertificate> parseCertificates(String armored)
260+
throws IOException
261+
{
262+
return parseCertificates(armored.getBytes(StandardCharsets.UTF_8));
263+
}
264+
265+
public List<OpenPGPCertificate> parseCertificates(InputStream inputStream)
266+
throws IOException
267+
{
268+
return parseCertificates(Streams.readAll(inputStream));
269+
}
270+
271+
public List<OpenPGPCertificate> parseCertificates(byte[] bytes)
272+
throws IOException
273+
{
274+
List<OpenPGPCertificate> certs = new ArrayList<>();
275+
276+
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
277+
InputStream decoderStream = PGPUtil.getDecoderStream(bIn);
278+
// Call getDecoderStream() twice, to make sure the stream is a BufferedInputStreamExt.
279+
// This is necessary, so that for streams containing multiple concatenated armored blocks of certs,
280+
// we parse all of them and do not quit after reading the first one.
281+
decoderStream = PGPUtil.getDecoderStream(decoderStream);
282+
PGPObjectFactory objectFactory = implementation.pgpObjectFactory(decoderStream);
283+
Object object;
284+
285+
while ((object = objectFactory.nextObject()) != null)
286+
{
287+
if (object instanceof PGPMarker)
254288
{
255-
PGPSecretKeyRingCollection collection = (PGPSecretKeyRingCollection) object;
256-
for (PGPSecretKeyRing k : collection)
257-
{
258-
certsOrKeys.add(new OpenPGPKey(k, implementation, policy));
259-
}
289+
continue;
260290
}
261-
else if (object instanceof PGPPublicKeyRingCollection)
291+
else if (object instanceof PGPPublicKeyRing)
262292
{
263-
PGPPublicKeyRingCollection collection = (PGPPublicKeyRingCollection) object;
264-
for (PGPPublicKeyRing k : collection)
265-
{
266-
certsOrKeys.add(new OpenPGPCertificate(k, implementation, policy));
267-
}
293+
certs.add(new OpenPGPCertificate((PGPPublicKeyRing) object, implementation, policy));
268294
}
269295
else
270296
{
271-
throw new IOException("Neither a certificate, nor secret key.");
297+
throw new IOException("Encountered unexpected packet: " + object.getClass().getName());
272298
}
273299
}
274-
return certsOrKeys;
300+
return certs;
301+
}
302+
303+
public List<OpenPGPKey> parseKeys(String armored)
304+
throws IOException
305+
{
306+
return parseKeys(armored.getBytes(StandardCharsets.UTF_8));
307+
}
308+
309+
public List<OpenPGPKey> parseKeys(InputStream inputStream)
310+
throws IOException
311+
{
312+
return parseKeys(Streams.readAll(inputStream));
313+
}
314+
315+
public List<OpenPGPKey> parseKeys(byte[] bytes)
316+
throws IOException
317+
{
318+
List<OpenPGPKey> keys = new ArrayList<>();
319+
320+
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
321+
InputStream decoderStream = PGPUtil.getDecoderStream(bIn);
322+
// Call getDecoderStream() twice, to make sure the stream is a BufferedInputStreamExt.
323+
// This is necessary, so that for streams containing multiple concatenated armored blocks of keys,
324+
// we parse all of them and do not quit after reading the first one.
325+
decoderStream = PGPUtil.getDecoderStream(decoderStream);
326+
PGPObjectFactory objectFactory = implementation.pgpObjectFactory(decoderStream);
327+
Object object;
328+
329+
while ((object = objectFactory.nextObject()) != null)
330+
{
331+
if (object instanceof PGPMarker)
332+
{
333+
continue;
334+
}
335+
else if (object instanceof PGPSecretKeyRing)
336+
{
337+
keys.add(new OpenPGPKey((PGPSecretKeyRing) object, implementation, policy));
338+
}
339+
else
340+
{
341+
throw new IOException("Encountered unexpected packet: " + object.getClass().getName());
342+
}
343+
}
344+
return keys;
275345
}
276346
}

0 commit comments

Comments
 (0)