[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    Re: [dev-crypto] Last Packet in BCPGInputStream doesn't de-stream properly
From:       David Hook <dgh () autochthonous ! org>
Date:       2013-11-17 6:17:34
Message-ID: 52885F7E.7020507 () autochthonous ! org
[Download RAW message or body]


The OpenPGP API is built around streaming. In practice what this means 
is the BCPGInputStream will return either ContainedPacket objects (which 
are always small enough to be read into memory) and InputStreamPacket 
objects (which may well be far too large).

The first two packets are ContainedPackets. The one causing the issue is 
an InputStreamPacket. With an InputStreamPacket you need to invoke 
getInputStream() and read the data the packet contains before attempting 
another call to readPacket().

Regards,

David

On 13/11/13 20:04, Matthew Shaylor wrote:
> Hi,
>
> I have a simple program that's trying to inspect all the packets in a 
> BCPGInputStream, however the last packet always seems to fail:
>
> ---
> import java.io.File;
> import java.io.FileInputStream;
> import java.io.InputStream;
>
> import org.bouncycastle.bcpg.BCPGInputStream;
> import org.bouncycastle.bcpg.Packet;
> import org.bouncycastle.openpgp.PGPUtil;
>
> public class PGPInspectorTest {
>     public static void main(String argv[]) throws Exception {
>         InputStream in = new FileInputStream(new File("pgp.asc"));
>         in = PGPUtil.getDecoderStream(in);
>         BCPGInputStream bcpgIn = new BCPGInputStream(in);
>         while(true) {
>             if(bcpgIn.nextPacketTag() == -1)
>                 break;
>             Packet p = bcpgIn.readPacket();
>             System.out.println("Packet " + bcpgIn.nextPacketTag() + ": 
> " + p.toString());
>         }
>     }
> }
> ---
>
> Here's the file pgp.asc:
> -----BEGIN PGP MESSAGE-----
> Version: GnuPG v2.0.19 (MingW32)
>
> hQEMA5nkRDbu8wCEAQf8D/qkBBRMQ90NfBX289P0hxqEHPHIBZfSGNE4vb5NJDoa
> cjraJiVBHbCOZsmbQ8ede95gNgKhuGKyNGWIcxx6iepOlGnXl5dH3eu1GbeVXs/0
> gsbw8HZpguKWbm6G4NjLCCjWww0tnxUh19P+KCFUL+9mRHu68+H1p7VdxfAVRryO
> wdeTIB9UanUPrAn0wPOQqsnX8pmZCySi8hCPTYmBJ/2O7JtLSUCmtWSjyz4/cpWZ
> YdzH6ab87+OXcVpOvdULgf2ibAI1AJn8yRJTCriWUBJPRh9zrguHjlh7BNeFz2Rq
> iDo5UyVJYxDxf2gjYql/wh11vA4qcKNmhtZSK6r5t8kiVpxQJQSqf1h7IGkdV8Hx
> vOv7M8qBvtmwZwOcjULnWDCujQ==
> =M7XF
> -----END PGP MESSAGE-----
>
>
> Generates the output:
>
> Packet 9: org.bouncycastle.bcpg.PublicKeyEncSessionPacket@457471e0
> Packet 22: org.bouncycastle.bcpg.SymmetricEncDataPacket@5fe04cbf
> Exception in thread "main" java.io.IOException: invalid header encountered
>     at 
> org.bouncycastle.bcpg.BCPGInputStream.readPacket(BCPGInputStream.java:151)
>     at com.anonpgp.PGPInspectorTest.main(PGPInspectorTest.java:19)
>
>
> On one level I don't suppose it matters. RFC4880 says an PGP message 
> can be a publicKeyEncSessionPacket followed by a 
> SymmetricEncDataPacket, so I should know there can be no valid packet 
> afterwards. Still it seems odd that I get errors when I try and decode 
> more from the stream. I get a similar issue no matter how I build up 
> the PGP Message.
>
> Any ideas?
>
> Many thanks,
> Matthew


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic