|
4 | 4 | import org.bouncycastle.openpgp.PGPMarker;
|
5 | 5 | import org.bouncycastle.openpgp.PGPObjectFactory;
|
6 | 6 | import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
7 |
| -import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; |
8 | 7 | import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
9 |
| -import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; |
10 | 8 | import org.bouncycastle.openpgp.PGPUtil;
|
11 | 9 | import org.bouncycastle.util.io.Streams;
|
12 | 10 |
|
@@ -250,27 +248,99 @@ else if (object instanceof PGPPublicKeyRing)
|
250 | 248 | {
|
251 | 249 | certsOrKeys.add(new OpenPGPCertificate((PGPPublicKeyRing) object, implementation, policy));
|
252 | 250 | }
|
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) |
254 | 288 | {
|
255 |
| - PGPSecretKeyRingCollection collection = (PGPSecretKeyRingCollection) object; |
256 |
| - for (PGPSecretKeyRing k : collection) |
257 |
| - { |
258 |
| - certsOrKeys.add(new OpenPGPKey(k, implementation, policy)); |
259 |
| - } |
| 289 | + continue; |
260 | 290 | }
|
261 |
| - else if (object instanceof PGPPublicKeyRingCollection) |
| 291 | + else if (object instanceof PGPPublicKeyRing) |
262 | 292 | {
|
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)); |
268 | 294 | }
|
269 | 295 | else
|
270 | 296 | {
|
271 |
| - throw new IOException("Neither a certificate, nor secret key."); |
| 297 | + throw new IOException("Encountered unexpected packet: " + object.getClass().getName()); |
272 | 298 | }
|
273 | 299 | }
|
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; |
275 | 345 | }
|
276 | 346 | }
|
0 commit comments