question on stream closing via CMSEnvelopedDataStreamGenerator

View: New views
2 Messages — Rating Filter:   Alert me  

question on stream closing via CMSEnvelopedDataStreamGenerator

by toadie D :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

i have a question on the object CMSEnvelopedDataStreamGenerator.  When
CMSEnvelopedDataStreamGenerator returns an OutputStream and the caller
calls close() on the returned stream, is the original stream (the
plain text one) supposed to be closed automatically ?

CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
OutputStream out = edGen.open( bOut, EnvelopedDataGenerator.AES128_CBC, "BC");

I am running BC 1.40.  Most of the time things just work when I am
using either ByterArrayOutputStream or FileOutputStream.  We recently
had a case where the OutputStream is really a blob column and found
that the data wasn't correctly encrypted.  I did a quick test using a
simple FilterOutputStream found that it does not close the original
OutputStream.  Should it?

Thanks

=======================


CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
BugFilterOutputStream  filterStream = new BugFilterOutputStream ( bOut );
OutputStream out = edGen.open( filterStream ,
EnvelopedDataGenerator.AES128_CBC, "BC");
for (int i = 0; i != 2000; i++) { out.write(data[i]); }
out.close();
               
assertTrue( "ByteArrayOutputStream is also closed automatically ",
filterStream.streamIsClosed());     // The assertion failed!


class BugFilterOutputStream extends FilterOutputStream {

        boolean _closeMethodCalled = false;
        public BugFilterOutputStream(OutputStream arg0) {
                super(arg0);
        }
        public void close() throws IOException {
                _closeMethodCalled = true;
                super.close();
        }
        public boolean streamIsClosed() {
                return _closeMethodCalled;
        }
}


Re: question on stream closing via CMSEnvelopedDataStreamGenerator

by David Hook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


The original stream passed in to the generator is left open. It's a bit
weird, but there's nothing to stop people from writing multiple of these
objects so the close() on the returned stream is really just closing off
the stream that represents the encapsulated content, as opposed to the
stream that is carrying the object that contains the encapsulated
content.

Regards,

David

On Mon, 2008-09-15 at 23:31 -0700, Toadie wrote:

> i have a question on the object CMSEnvelopedDataStreamGenerator.  When
> CMSEnvelopedDataStreamGenerator returns an OutputStream and the caller
> calls close() on the returned stream, is the original stream (the
> plain text one) supposed to be closed automatically ?
>
> CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
> ByteArrayOutputStream bOut = new ByteArrayOutputStream();
> OutputStream out = edGen.open( bOut, EnvelopedDataGenerator.AES128_CBC, "BC");
>
> I am running BC 1.40.  Most of the time things just work when I am
> using either ByterArrayOutputStream or FileOutputStream.  We recently
> had a case where the OutputStream is really a blob column and found
> that the data wasn't correctly encrypted.  I did a quick test using a
> simple FilterOutputStream found that it does not close the original
> OutputStream.  Should it?
>
> Thanks
>
> =======================
>
>
> CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();
> ByteArrayOutputStream bOut = new ByteArrayOutputStream();
> BugFilterOutputStream  filterStream = new BugFilterOutputStream ( bOut );
> OutputStream out = edGen.open( filterStream ,
> EnvelopedDataGenerator.AES128_CBC, "BC");
> for (int i = 0; i != 2000; i++) { out.write(data[i]); }
> out.close();
>
> assertTrue( "ByteArrayOutputStream is also closed automatically ",
> filterStream.streamIsClosed());     // The assertion failed!
>
>
> class BugFilterOutputStream extends FilterOutputStream {
>
> boolean _closeMethodCalled = false;
> public BugFilterOutputStream(OutputStream arg0) {
> super(arg0);
> }
> public void close() throws IOException {
> _closeMethodCalled = true;
> super.close();
> }
> public boolean streamIsClosed() {
> return _closeMethodCalled;
> }
> }
>


LightInTheBox - Buy quality products at wholesale price!