[ 
https://issues.apache.org/jira/browse/PDFBOX-3230?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Diego Rodrigues Azevedo updated PDFBOX-3230:
--------------------------------------------
    Description: 
Problem:

When using digital signatures, one can add a DSS dictionary, referencing 
Certificates, CRLs, OCSPs... saved as Streams (CosStream) in the PDF.

So, I signed my PDF with a DSS dictionary, and I want to re-sign it, checking 
if any one of the streams is already there (same certificate chain, maybe same 
CRLs (if the first signature wasn't made long ago)...)

How do I do this? I get the latest DSS dictionary and iterate over the 
COSArray, knowing the elements must be CosStreams. But then I get a 
ClassCastException, saying CosObject cannot be cast to CosStream. When I check 
that CosObject in notepad, I can check it is a CosStream

Code (proof of concept): 

{code:java}
private COSStream createStramIfNotExists(byte[] stream, String dssEntry)
                        throws IOException {
                COSArray things = (COSArray) this.dictionary
                                .getDictionaryObject(dssEntry);

                
                ByteArrayOutputStream out = null;
                for (COSBase thing : things) {
                        InputStream input = null;
                        byte[] aux = null;
                        try {
                                System.out.println(thing);
                                out = new ByteArrayOutputStream();
                                input = ((COSStream) 
thing.getCOSObject()).createInputStream();
                                IOUtils.copy(input, out);
                                aux = out.toByteArray();
                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                IOUtils.closeQuietly(input);
                                IOUtils.closeQuietly(out);
                        }
                        if (Arrays.areEqual(stream, aux)) {
                                return (COSStream) thing;
                        }
                }

                COSStream cosStream = new COSStream();

                OutputStream out1 = cosStream.createOutputStream();
                out1.write(stream);
                out1.close();
                
                return cosStream;
        }
{code}

That sysout prints:
COSObject\{170, 0\}
in the PDF attached when my DSS entry is "Certs", and throws the exception on 
line:
input = ((COSStream) thing.getCOSObject()).createInputStream();

But if you open the PDF on notepad++ or similar, you'll see that object 170 
(170 0 obj) is a stream.

  was:
Problem:

When using digital signatures, one can add a DSS dictionary, referencing 
Certificates, CRLs, OCSPs... saved as Streams (CosStream) in the PDF.

So, I signed my PDF with a DSS dictionary, and I want to re-sign it, checking 
if any one of the streams is already there (same certificate chain, maybe same 
CRLs (if the first signature wasn't made long ago)...)

How do I do this? I get the latest DSS dictionary and iterate over the 
COSArray, knowing the elements must be CosStreams. But then I get a 
ClassCastException, saying CosObject cannot be cast to CosStream. When I check 
that CosObject in notepad, I can check it is a CosStream

Code (proof of concept): 

{code:java}
private COSStream createStramIfNotExists(byte[] stream, String dssEntry)
                        throws IOException {
                COSArray things = (COSArray) this.dictionary
                                .getDictionaryObject(dssEntry);

                
                ByteArrayOutputStream out = null;
                for (COSBase thing : things) {
                        InputStream input = null;
                        byte[] aux = null;
                        try {
                                System.out.println(thing);
                                out = new ByteArrayOutputStream();
                                input = ((COSStream) 
thing.getCOSObject()).createInputStream();
                                IOUtils.copy(input, out);
                                aux = out.toByteArray();
                        } catch (IOException e) {
                                e.printStackTrace();
                        } finally {
                                IOUtils.closeQuietly(input);
                                IOUtils.closeQuietly(out);
                        }
                        if (Arrays.areEqual(stream, aux)) {
                                return (COSStream) thing;
                        }
                }

                COSStream cosStream = new COSStream();

                OutputStream out1 = cosStream.createOutputStream();
                out1.write(stream);
                out1.close();
                
                return cosStream;
        }
{code}

That sysout prints "COSObject\{170, 0\} in the PDF attached when my DSS entry 
is "Certs", and throws the exception on line "input = ((COSStream) 
thing.getCOSObject()).createInputStream();"

But if you open the PDF on notepad++ or similar, you'll see that object 170 
(170 0 obj) is a stream.


> Can't cast CosObject to CosStream when it is a Stream
> -----------------------------------------------------
>
>                 Key: PDFBOX-3230
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-3230
>             Project: PDFBox
>          Issue Type: Bug
>    Affects Versions: 2.0.0
>         Environment: Windows 8.1, Java 8 u60, maven 3.2.2, PDFBOX 2.0.0-RC3
>            Reporter: Diego Rodrigues Azevedo
>         Attachments: TestFile-assinado.pdf
>
>
> Problem:
> When using digital signatures, one can add a DSS dictionary, referencing 
> Certificates, CRLs, OCSPs... saved as Streams (CosStream) in the PDF.
> So, I signed my PDF with a DSS dictionary, and I want to re-sign it, checking 
> if any one of the streams is already there (same certificate chain, maybe 
> same CRLs (if the first signature wasn't made long ago)...)
> How do I do this? I get the latest DSS dictionary and iterate over the 
> COSArray, knowing the elements must be CosStreams. But then I get a 
> ClassCastException, saying CosObject cannot be cast to CosStream. When I 
> check that CosObject in notepad, I can check it is a CosStream
> Code (proof of concept): 
> {code:java}
> private COSStream createStramIfNotExists(byte[] stream, String dssEntry)
>                       throws IOException {
>               COSArray things = (COSArray) this.dictionary
>                               .getDictionaryObject(dssEntry);
>               
>               ByteArrayOutputStream out = null;
>               for (COSBase thing : things) {
>                       InputStream input = null;
>                       byte[] aux = null;
>                       try {
>                               System.out.println(thing);
>                               out = new ByteArrayOutputStream();
>                               input = ((COSStream) 
> thing.getCOSObject()).createInputStream();
>                               IOUtils.copy(input, out);
>                               aux = out.toByteArray();
>                       } catch (IOException e) {
>                               e.printStackTrace();
>                       } finally {
>                               IOUtils.closeQuietly(input);
>                               IOUtils.closeQuietly(out);
>                       }
>                       if (Arrays.areEqual(stream, aux)) {
>                               return (COSStream) thing;
>                       }
>               }
>               COSStream cosStream = new COSStream();
>               OutputStream out1 = cosStream.createOutputStream();
>               out1.write(stream);
>               out1.close();
>               
>               return cosStream;
>       }
> {code}
> That sysout prints:
> COSObject\{170, 0\}
> in the PDF attached when my DSS entry is "Certs", and throws the exception on 
> line:
> input = ((COSStream) thing.getCOSObject()).createInputStream();
> But if you open the PDF on notepad++ or similar, you'll see that object 170 
> (170 0 obj) is a stream.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to