Bug 692343 - Error while opening pdf
Summary: Error while opening pdf
Status: NOTIFIED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: PDF Interpreter (show other bugs)
Version: 9.02
Hardware: All Linux
: P2 normal
Assignee: Alex Cherepanov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-14 18:15 UTC by Tom
Modified: 2012-04-12 16:59 UTC (History)
1 user (show)

See Also:
Customer: 700
Word Size: ---


Attachments
The offending PDF file (203.33 KB, application/pdf)
2011-07-14 18:15 UTC, Tom
Details
Working PDF RSA 128bit (144.28 KB, application/pdf)
2011-07-15 10:28 UTC, Tom
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tom 2011-07-14 18:15:36 UTC
Created attachment 7668 [details]
The offending PDF file

While trying to convert a pdf to a jpeg using imagemagick, I get the following error (run directly through gs in this case):

$ gs /tmp/card-pdf-4e1f2e0a0ac64.pdf 
GPL Ghostscript 9.02 (2011-03-30)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1
   **** Warning: stream Length incorrect.
Loading NimbusSanL-Regu font from /usr/share/fonts/type1/gsfonts/n019003l.pfb... 3414728 2046064 4031704 2740916 3 done.
+ ./base/saes.c:152: s_aes_process(): invalid aes padding byte (0xa7)
Error: /ioerror in --.reusablestreamdecode--
Operand stack:
   --dict:16/25(L)--   1   11   170299   --nostringval--   --nostringval--   --dict:5/5(L)--   --dict:5/5(L)--   --nostringval--   --dict:0/0(L)--   --nostringval--   --nostringval--   false   false   false   --nostringval--   --nostringval--   --nostringval--   ($h\364G\351[\216\344P4/\344\0373j~\371y\317wuu>\206L\303H\240Rs\372\364\313\301\350\203o\243\\\324\251\373\021z\320+\310&\206#f\344\216\273\245\214\342w\274\\\002q\232a\303\251\201\264m\263\260\252\362\203\241\307\242\347v\315\350Q\360m\210m1z\002\367=\270\267\367\235+T\325\334\030\300\302\337\355\247\013T\220\336\230\312\235\244u\251~.\0339\227\2228\356\261\356\033kV\210\321\376\312\313\006\020\311\032\317E\f:v\245\350\334\346\376r\201]9X\312$_\370:<\273\210\240X\331q\\\026:\235/\020\327.\200YqqZ\203\223<\215\331\332\346J\207w\334\233@\331\007e\345\353[gw\304Y...)
Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push   1910   1   3   %oparray_pop   1909   1   3   %oparray_pop   1893   1   3   %oparray_pop   --nostringval--   --nostringval--   2   1   1   --nostringval--   %for_pos_int_continue   --nostringval--   --nostringval--   --nostringval--   --nostringval--   %array_continue   --nostringval--   false   1   %stopped_push   --nostringval--   %loop_continue   --nostringval--   946   --nostringval--   1858   4   12   %oparray_pop   --nostringval--   false   1   %stopped_push   1857   4   12   %oparray_pop   --nostringval--   (gstatetype)   --dict:0/0(L)--   --nostringval--   false   1   %stopped_push   --nostringval--   %loop_continue   --nostringval--   --nostringval--   --nostringval--   1781   10   14   %oparray_pop   1884   9   14   %oparray_pop   --nostringval--   --nostringval--   --nostringval--   %loop_continue   --nostringval--
Dictionary stack:
   --dict:1153/1684(ro)(G)--   --dict:1/20(G)--   --dict:82/200(L)--   --dict:82/200(L)--   --dict:108/127(ro)(G)--   --dict:295/300(ro)(G)--   --dict:24/30(L)--   --dict:6/8(L)--   --dict:25/40(L)--   --dict:5/5(L)--   --dict:1/1(ro)(G)--   --dict:1/1(ro)(G)--   --dict:1/1(ro)(G)--   --dict:1/1(ro)(G)--
Current allocation mode is global
GPL Ghostscript 9.02: Unrecoverable error, exit code 1
Comment 1 Tom 2011-07-14 19:50:41 UTC
I can confirm that changing the encryption to RSA 128bit on the program that is generating the pdf (TCPDF on PHP) fixes the problem
Comment 2 Alex Cherepanov 2011-07-15 03:37:47 UTC
The length of the stream is invalid and the padding byte is invalid.
With all the checks disabled, the file renders OK.
Comment 3 Robin Watts 2011-07-15 10:11:15 UTC
Tom: Can you tell us what program you used to generate the pdf file please?
Could you also attach the same thing, saved with a different encryption (ideally no encryption) please?

That would enable us to double check that we really are decoding the streams correctly.
Comment 4 Tom 2011-07-15 10:21:41 UTC
(In reply to comment #3)
> Tom: Can you tell us what program you used to generate the pdf file please?

It's using the PHP library called TCPDF, see http://sourceforge.net/projects/tcpdf/ or http://www.tcpdf.org/

> Could you also attach the same thing, saved with a different encryption
> (ideally no encryption) please?

I can send through the RSA 128bit version, for some reason I can't get it to not encrypt it at the moment.
Regards,
Tom
Comment 5 Tom 2011-07-15 10:28:44 UTC
Created attachment 7670 [details]
Working PDF RSA 128bit
Comment 6 Marcos H. Woehrmann 2011-08-04 18:00:14 UTC
Alex, can you verify that the original attachment can be opened by other software (i.e. Adobe Acrobat)?  If not please close as invalid.
Comment 7 Alex Cherepanov 2011-08-04 20:14:44 UTC
Acrobat 9 can open the sample file. Otherwise I'd closed the bug long ago.
Comment 8 Marcos H. Woehrmann 2012-01-25 16:54:36 UTC
Created attachment 8308 [details]
seattle_coversheet.pdf

A customer has reported the same problem; I've attached their sample file and changed the priority to P2.
Comment 9 Marcos H. Woehrmann 2012-01-25 16:57:13 UTC
Additional information from the customer:

This is a PDF form created by Adobe LifeCycle Designer ES 8.2. It is protected by a password and encrypted 128-bit AES. From what I can gather, encryption is performed in chunks, and all chucks must be the same length (via padding if necessary). It appears there's a problem with one of the padding bytes.  Ghostscript 9.04 reports "invalid aes padding byte".

If you open this file with Acrobat Pro 9.4.7, then "save a copy" (not "save as"), the following dialog appears: "This document enables extended features in Adobe Reader. 'Save A Copy' will create a copy of the document which does not enable extended features in Adobe Reader." The new copy (still AES encrypted) can be processed without error in Ghostscript 9.0
Comment 10 Robin Watts 2012-01-25 20:06:27 UTC
Cluster testing a workaround now:


diff --git a/gs/base/saes.c b/gs/base/saes.c
index d703961..25ceebe 100644
--- a/gs/base/saes.c
+++ b/gs/base/saes.c
@@ -147,9 +147,12 @@ s_aes_process(stream_state * ss, stream_cursor_read * pr,
              plaintext gives the number of bytes to discard */
           pad = temp[15];
           if (pad < 1 || pad > 16) {
-            gs_throw1(gs_error_rangecheck, "invalid aes padding byte (0x%02x)",
-                  (unsigned char)pad);
-            return ERRC;
+            /* Bug 692343 - don't error here, just warn. Take padding to be
+             * zero. This may give us a stream that's too long - preferable
+             * to the alternatives. */
+            gs_warn1("invalid aes padding byte (0x%02x)",
+                     (unsigned char)pad);
+            pad = 0;
           }
         } else {
           /* not using padding */
Comment 11 Robin Watts 2012-01-25 20:40:40 UTC
commit 0fb4785ef74fd22b8c1cced5ec16bd28d5bcc666
Author: Robin Watts <robin.watts@artifex.com>
Date:   Wed Jan 25 19:58:52 2012 +0000

    Fix Bug 692343 - ignore invalid padding length bytes.

    AES streams contain padding at the end to bring them up to 16
    byte chunks. The last byte of the plaintext is supposed to be
    a value between 1 and 16 telling us how many bytes to ignore
    from the last one? (Why 16? 15 would make more sense).

    If the padding byte was out of range we would previously have
    thrown an error. Here we change to just warn and set the
    padding length to 0. This means we'll err on the side of making
    the stream too long, which is better than the alternative.