~ubuntu-branches/ubuntu/oneiric/ghostscript/oneiric

« back to all changes in this revision

Viewing changes to Resource/Init/pdf_sec.ps

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2011-07-15 16:49:55 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20110715164955-uga6qibao6kez05c
Tags: 9.04~dfsg~20110715-0ubuntu1
* New upstream release
   - GIT snapshot from Jult, 12 2011.
* debian/patches/020110406~a54df2d.patch,
  debian/patches/020110408~0791cc8.patch,
  debian/patches/020110408~507cbee.patch,
  debian/patches/020110411~4509a49.patch,
  debian/patches/020110412~78bb9a6.patch,
  debian/patches/020110418~a05ab8a.patch,
  debian/patches/020110420~20b6c78.patch,
  debian/patches/020110420~4ddefa2.patch: Removed upstream patches.
* debian/rules: Generate ABI version number (variable "abi") correctly,
  cutting off repackaging and pre-release parts.
* debian/rules: Added ./lcms2/ directory to DEB_UPSTREAM_REPACKAGE_EXCLUDES.
* debian/copyright: Added lcms2/* to the list of excluded files.
* debian/symbols.common: Updated for new upstream source. Applied patch
  which dpkg-gensymbols generated for debian/libgs9.symbols to this file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
% so long as modified versions are marked as such and copyright notices are
5
5
% not removed.
6
6
 
7
 
% $Id: pdf_sec.ps 12268 2011-03-10 06:19:17Z alexcher $
 
7
% $Id$
8
8
% Implementation of security hooks for PDF reader.
9
9
 
10
10
% This file contains the procedures that have to take encryption into
15
15
% New versions of GS require explicit checks for /true, /false, and /null
16
16
% in .decpdfrun. This fix is backward-compatible.
17
17
 
18
 
% Modified by Raph Levien and Ralph Giles to use the new C 
19
 
% implementations of md5 and arcfour in ghostscript 7.01, and to 
 
18
% Modified by Raph Levien and Ralph Giles to use the new C
 
19
% implementations of md5 and arcfour in ghostscript 7.01, and to
20
20
% be compatible with PDF 1.4 128-bit encryption.
21
21
 
22
22
% Modified by Ralph Giles for PDF 1.6 AES encryption.
98
98
  //false exch { 128 ge { pop //true exit } if } forall
99
99
} bind def
100
100
 
101
 
 
102
101
/pdf_padding_string
103
102
   <28bf4e5e4e758a41 64004e56fffa0108
104
103
    2e2e00b6d0683e80 2f0ca9fe6453697a>
154
153
    ()
155
154
    (   **** ID key in the trailer is required for encrypted files.\n) pdfformaterror
156
155
  } ifelse
157
 
  3 { concatstrings } repeat 
 
156
  3 { concatstrings } repeat
158
157
  % We will finish step 5 after possibly including step 6.
159
158
 
160
159
  % The following only executed for /R equal to 3 or more
437
436
         % iconv nor the Windows MultiByteToWideChar function, then we
438
437
         % won't have the .locale_to_utf8 operator to convert passwords
439
438
         % from the locale character set to UTF-8.
440
 
         % 
 
439
         %
441
440
         % It's not a huge problem if you're missing either or both of
442
441
         % these.  ASCII passwords will work fine regardless, and even
443
442
         % Unicode passwords will often be okay.
493
492
    FileKey length 5 add string
494
493
    dup 0 FileKey putinterval
495
494
    exch
496
 
                  % stack:  gen# string obj#
 
495
                  % stack:  gen# string obj#
497
496
      2 copy 255 and FileKey length exch put
498
497
      2 copy -8 bitshift 255 and FileKey length 1 add exch put
499
498
      2 copy -16 bitshift 255 and FileKey length 2 add exch put
533
532
    { .pdftoken not { (%%EOF) cvn cvx } if
534
533
      dup xcheck
535
534
       { PDFDEBUG { dup //== exec flush } if
536
 
         3 -1 roll pop
537
 
         2 copy .knownget
538
 
          { exch pop exch pop exec
 
535
         3 -1 roll pop
 
536
         2 copy .knownget
 
537
          { exch pop exch pop exec
539
538
          }
540
 
          { exch pop
 
539
          { exch pop
541
540
            dup /true eq
542
541
              { pop //true
543
542
              }
544
543
              { dup /false eq
545
 
                  { pop //false 
 
544
                  { pop //false
546
545
                  }
547
546
                  { dup /null eq
548
547
                      { pop //null
549
548
                      }
550
 
                      { (   **** Unknown operator: ) 
551
 
                        exch =string cvs concatstrings (\n) concatstrings
552
 
                        pdfformaterror
 
549
                      { (   **** Unknown operator: )
 
550
                        exch =string cvs concatstrings (\n) concatstrings
 
551
                        pdfformaterror
553
552
                      }
554
553
                    ifelse
555
554
                  }
556
555
                ifelse
557
556
              }
558
557
            ifelse
559
 
          }
560
 
         ifelse
 
558
          }
 
559
         ifelse
561
560
       }
562
561
       { exch pop PDFDEBUG { dup ==only ( ) print flush } if
563
 
         dup type /stringtype eq
 
562
         dup type /stringtype eq
564
563
          {
565
 
        % Check if we have encrypted strings R>=4 allows for
566
 
        % selection of encryption on streams and strings
 
564
        % Check if we have encrypted strings R>=4 allows for
 
565
        % selection of encryption on streams and strings
567
566
            Trailer /Encrypt oget       % Get encryption dictionary
568
567
            dup /R oget 4 lt            % only >=4 has selectable
569
568
             {                          % R < 4 --> arc4 strings
570
 
               pop 1 index arc4decode   % Decrypt string
571
 
               PDFDEBUG { (%Decrypted: ) print dup //== exec flush } if
 
569
               pop 1 index arc4decode   % Decrypt string
 
570
               PDFDEBUG { (%Decrypted: ) print dup //== exec flush } if
572
571
             } {                        % Else R >= 4
573
 
               /StrF knownoget          % Get StrF (if present)
574
 
                {                       % If StrF is present ...
575
 
                  dup /Identity eq not  % Check if StrF != Identity
576
 
                   { /StdCF eq
577
 
                       { Trailer /Encrypt oget /CF knownoget {
 
572
               /StrF knownoget          % Get StrF (if present)
 
573
                {                       % If StrF is present ...
 
574
                  dup /Identity eq not  % Check if StrF != Identity
 
575
                   { /StdCF eq
 
576
                       { Trailer /Encrypt oget /CF knownoget {
578
577
                           /StdCF oget /CFM oget
579
578
                           dup /AESV2 eq exch /AESV3 eq or
580
579
                         } {
581
580
                           //false
582
581
                         } ifelse {     % Decrypt string
583
 
                           1 index aesdecode
 
582
                           1 index aesdecode
584
583
                         } {
585
584
                           1 index arc4decode
586
585
                         } ifelse
587
 
                       }
588
 
                       { 1 index arc4decode }
589
 
                     ifelse             % If StrF != StdCF
590
 
                     PDFDEBUG { (%Decrypted: ) print dup //== exec flush } if
591
 
                   }
592
 
                   { pop }
593
 
                  ifelse                % If StrF != identity
594
 
                }
595
 
               if                       % If StrF is known
596
 
             }
597
 
            ifelse                      % Ifelse R < 4
598
 
          } {
 
586
                       }
 
587
                       { 1 index arc4decode }
 
588
                     ifelse             % If StrF != StdCF
 
589
                     PDFDEBUG { (%Decrypted: ) print dup //== exec flush } if
 
590
                   }
 
591
                   { pop }
 
592
                  ifelse                % If StrF != identity
 
593
                }
 
594
               if                       % If StrF is known
 
595
             }
 
596
            ifelse                      % Ifelse R < 4
 
597
          } {
599
598
            dup type /nametype eq {
600
599
              .pdffixname
601
600
            } if
602
601
          }
603
602
         ifelse
604
 
         exch pop
 
603
         exch pop
605
604
       }
606
605
      ifelse
607
606
    }
612
611
     //PDFScanRules_null { setuserparams } 0 get % reset PDF scannig mode if it was off
613
612
   } if
614
613
   /PDFsource PDFsource
615
 
    { store { stop } if } aload pop .packtomark cvx 
 
614
    { store { stop } if } aload pop .packtomark cvx
616
615
   /PDFsource 3 -1 roll store exec
617
616
 } bind def
618
617
currentdict /PDFScanRules_true undef
625
624
    2 copy computeobjkey dup 4 1 roll
626
625
    PDFfile exch resolveopdict .decpdfrun
627
626
    dup dup dup 5 2 roll
628
 
        % stack: object object key object object
 
627
        % stack: object object key object object
629
628
    {   % Use loop to provide an exitable context.
630
629
      xcheck exch type /dicttype eq and % Check if executable dictionary
631
630
      not {                             % If object is not ...
632
631
        pop pop                         % ignore object
633
632
        exit                            % Exit 'loop' context
634
633
      } if                              % If not possible stream
635
 
        % Starting with PDF 1.4 (R = 3), there are some extra features
636
 
        % which control encryption of streams.  The EncryptMetadata entry
637
 
        % in the Encrypt dict controls the encryption of metadata streams.
 
634
        % Starting with PDF 1.4 (R = 3), there are some extra features
 
635
        % which control encryption of streams.  The EncryptMetadata entry
 
636
        % in the Encrypt dict controls the encryption of metadata streams.
638
637
      Trailer /Encrypt oget             % Get encryption dictionary
639
638
      dup /R oget dup 3 lt              % Only PDF 1.4 and higher has options
640
639
      {                                 % R < 3 --> all streams encrypted
641
640
        pop pop /StreamKey exch put     % Insert StreamKey in dictionary
642
 
        exit                            % Exit 'loop' context
 
641
        exit                            % Exit 'loop' context
643
642
      } if
644
 
        % Check EncryptMeta.  stack: object object key Encrypt R
 
643
        % Check EncryptMeta.  stack: object object key Encrypt R
645
644
      exch dup /EncryptMetadata knownoget % Get EncryptMetadata (if present)
646
645
      not { //true } if                 % If not present default = true
647
646
      not                               % Check if EncryptMetadata = false
648
647
      {                                 % if false we need to check the stream type
649
 
        3 index /Type knownoget         % Get stream type (if present)
650
 
        not { //null } if               % If type not present use fake name
651
 
        /Metadata eq                    % Check if the type is Metadata
 
648
        3 index /Type knownoget         % Get stream type (if present)
 
649
        not { //null } if               % If type not present use fake name
 
650
        /Metadata eq                    % Check if the type is Metadata
652
651
        { pop pop pop pop               % Type == Metadata --> no encryption
653
 
          exit                          % Exit 'loop' context
 
652
          exit                          % Exit 'loop' context
654
653
        } if
655
654
      } if
656
 
        % PDF 1.5 encryption (R == 4) has selectable encryption handlers.  If
657
 
        % this is not PDF 1.5 encryption (R < 4) then we are done checking and
658
 
        % we need to decrypt the stream.  stack: object object key R Encrypt
 
655
        % PDF 1.5 encryption (R == 4) has selectable encryption handlers.  If
 
656
        % this is not PDF 1.5 encryption (R < 4) then we are done checking and
 
657
        % we need to decrypt the stream.  stack: object object key R Encrypt
659
658
      exch 4 lt                         % Check for less than PDF 1.5
660
659
      { pop /StreamKey exch put         % Insert StreamKey in dictionary
661
 
        exit                            % Exit 'loop' context
 
660
        exit                            % Exit 'loop' context
662
661
      } if
663
 
        % Check if the stream encryption handler (StmF) == Identity.
 
662
        % Check if the stream encryption handler (StmF) == Identity.
664
663
      PDFDEBUG {
665
 
        Trailer /Encrypt oget /CF knownoget {
 
664
        Trailer /Encrypt oget /CF knownoget {
666
665
          /StdCF oget /CFM oget
667
 
          (Encrypt StmF is StdCF with CFM ) print =
 
666
          (Encrypt StmF is StdCF with CFM ) print =
668
667
        } if
669
668
      } if
670
669
      /StmF knownoget                   % Get StmF (if present)
671
670
      not { /Identity } if              % If StmF not present default = Identity
672
671
      /Identity eq                      % Check if StmF == Identity
673
672
      { pop pop                         % Identity --> no encryption
674
 
        exit                            % Exit 'loop' context
 
673
        exit                            % Exit 'loop' context
675
674
      } if
676
 
        % If we get here then we need to decrypt the stream.
 
675
        % If we get here then we need to decrypt the stream.
677
676
      /StreamKey exch put               % Insert StreamKey into dictionary
678
677
      exit                              % Exit 'loop' context, never loop
679
678
    } loop                              % End of loop exitable context
688
687
/pdf_decrypt_stream
689
688
 { 3 index /StreamKey known     % Check if the file is encrypted
690
689
   {
691
 
      exch 
692
 
        % Stack: readdata? dict parms filternames file/string
 
690
      exch
 
691
        % Stack: readdata? dict parms filternames file/string
693
692
      3 index /StreamKey get
694
693
      Trailer /Encrypt oget
695
694
      dup /StmF knownoget
696
695
       {                        % stack: key Encrypt StmF
697
 
        exch /CF knownoget {
698
 
          exch oget /CFM oget   % stack: key StmF-CFM
699
 
          dup /AESV2 eq exch /AESV3 eq or
 
696
        exch /CF knownoget {
 
697
          exch oget /CFM oget   % stack: key StmF-CFM
 
698
          dup /AESV2 eq exch /AESV3 eq or
700
699
        } { pop //false } ifelse
701
 
         { aesdecodefilter }    % install the requested filter
702
 
         { arc4decodefilter }
703
 
        ifelse
704
 
       } 
 
700
         { aesdecodefilter }    % install the requested filter
 
701
         { arc4decodefilter }
 
702
        ifelse
 
703
       }
705
704
       { pop arc4decodefilter } % fallback for no StmF
706
705
      ifelse
707
706
      exch