home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 September / PCWorld_2001-09_cd.bin / Software / Vyzkuste / rychlokurz / gs700w32.exe / gs7.00 / lib / pdf_sec.ps < prev    next >
Text File  |  2001-03-24  |  12KB  |  407 lines

  1. %    Copyright (C) 1996-1998 Geoffrey Keating. 
  2. % This file may be freely distributed with or without modifications,
  3. % so long as modified versions are marked as such and copyright notices are
  4. % not removed.
  5.  
  6. % $Id: pdf_sec.ps,v 1.3 2001/03/25 02:03:44 alexcher Exp $
  7. % Implementation of security hooks for PDF reader.
  8.  
  9. % This file contains the procedures that have to take encryption into
  10. % account when reading a PDF file. It replaces the stub version of this
  11. % file that is shipped with GhostScript. It requires GhostScript version 4.02
  12. % or later.
  13.  
  14. % Documentation for using this file is available at
  15. % http://www.ozemail.com.au/%7Egeoffk/pdfencrypt/
  16.  
  17. % Modified by Alex Cherepanov to work with GS 6.60 and higher.
  18. % New versions of GS require explicit checks for /true , /false, and /null
  19. % in .decpdfrun . This fix is backward-compatible.
  20.  
  21. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  22. .currentglobal true .setglobal
  23. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  24. pdfdict begin
  25.  
  26. % Older ghostscript versions do not have .pdftoken, so we use 'token' instead.
  27. /.pdftoken where { pop } { /.pdftoken /token load def } ifelse
  28.  
  29. % An implementation of an algorithm compatible with the RSA Data Security 
  30. % Inc. RC4 stream encryption algorithm.
  31.  
  32. % <string> rc4setkey <dict>
  33. /rc4setkey
  34. {
  35.   6 dict begin
  36.     /k exch def
  37.     /a 256 string def
  38.     0 1 255 { a exch dup put } for
  39.     /l k length def
  40.     /j 0 def
  41.     0 1 255
  42.     {
  43.       /i exch def
  44.       /j a i get k i l mod get add j add 255 and def
  45.       a i a j get a j a i get put put
  46.     } for
  47.     3 dict dup begin
  48.       /a a def
  49.       /x 0 def
  50.       /y 0 def
  51.     end
  52.   end
  53. } bind def
  54.  
  55. % <rc4key> <string> rc4 <string> <rc4key>
  56. /rc4
  57. {
  58.   1 index begin
  59.     dup dup length 1 sub 0 exch 1 exch
  60.     {
  61.       /x x 1 add 255 and def
  62.       /y a x get y add 255 and def
  63.       a x a y get a y a x get put put
  64. % stack: string string index
  65.       2 copy get
  66.       a dup x get a y get add 255 and get
  67.       xor put dup
  68.     } for
  69.     pop
  70.   end
  71. } bind def
  72.  
  73. % take a stream and rc4 decrypt it.
  74. % <stream> <key> rc4decodefilter <stream>
  75. /rc4decodefilter {
  76.   currentglobal exch true setglobal
  77.   dup length string copy rc4setkey
  78.   exch setglobal
  79.   exch 512 string
  80.    % stack: <key> <stream> <string>
  81.   { readstring pop rc4 exch pop } aload pop
  82.    8 array astore cvx 0 () /SubFileDecode filter 
  83. } bind def
  84.  
  85. % MD5 derived from RFC 1321, "The MD5 Message-Digest Algorithm",
  86. % R. Rivest, MIT, RSADSI; implemented in PostScript by Geoffrey Keating.
  87.  
  88. % We construct the MD5 transform by a sort of inline expansion.
  89. % this takes up quite a bit of memory (around 17k), but gives a
  90. % factor-of-two speed increase.
  91. % This also allows us to take advantage of interpreters with 64-bit
  92. % wide integers.
  93. % This will not run on interpreters with 16-bit wide integers, if such
  94. % things exist.
  95. 20 dict begin
  96.  
  97. /T [
  98. 16#d76aa478 16#e8c7b756 16#242070db 16#c1bdceee
  99. 16#f57c0faf 16#4787c62a 16#a8304613 16#fd469501
  100. 16#698098d8 16#8b44f7af 16#ffff5bb1 16#895cd7be
  101. 16#6b901122 16#fd987193 16#a679438e 16#49b40821
  102. 16#f61e2562 16#c040b340 16#265e5a51 16#e9b6c7aa
  103. 16#d62f105d 16#02441453 16#d8a1e681 16#e7d3fbc8
  104. 16#21e1cde6 16#c33707d6 16#f4d50d87 16#455a14ed
  105. 16#a9e3e905 16#fcefa3f8 16#676f02d9 16#8d2a4c8a
  106. 16#fffa3942 16#8771f681 16#6d9d6122 16#fde5380c
  107. 16#a4beea44 16#4bdecfa9 16#f6bb4b60 16#bebfbc70
  108. 16#289b7ec6 16#eaa127fa 16#d4ef3085 16#04881d05
  109. 16#d9d4d039 16#e6db99e5 16#1fa27cf8 16#c4ac5665
  110. 16#f4292244 16#432aff97 16#ab9423a7 16#fc93a039
  111. 16#655b59c3 16#8f0ccc92 16#ffeff47d 16#85845dd1
  112. 16#6fa87e4f 16#fe2ce6e0 16#a3014314 16#4e0811a1
  113. 16#f7537e82 16#bd3af235 16#2ad7d2bb 16#eb86d391
  114. ] def
  115. /F [
  116. { c d /xor b /and d /xor } { b c /xor d /and c /xor }
  117. { b c /xor d /xor } { d /not b /or c /xor }
  118. ] def
  119. /R [
  120. 16#0007 16#010c 16#0211 16#0316 16#0407 16#050c 16#0611 16#0716
  121. 16#0807 16#090c 16#0a11 16#0b16 16#0c07 16#0d0c 16#0e11 16#0f16
  122. 16#0105 16#0609 16#0b0e 16#0014 16#0505 16#0a09 16#0f0e 16#0414
  123. 16#0905 16#0e09 16#030e 16#0814 16#0d05 16#0209 16#070e 16#0c14
  124. 16#0504 16#080b 16#0b10 16#0e17 16#0104 16#040b 16#0710 16#0a17
  125. 16#0d04 16#000b 16#0310 16#0617 16#0904 16#0c0b 16#0f10 16#0217
  126. 16#0006 16#070a 16#0e0f 16#0515 16#0c06 16#030a 16#0a0f 16#0115
  127. 16#0806 16#0f0a 16#060f 16#0d15 16#0406 16#0b0a 16#020f 16#0915
  128. ] def
  129.  
  130. /W 1 31 bitshift 0 gt def
  131. /A W { /add } { /md5add } ifelse def
  132. /t W { 1744 } { 1616 } ifelse array def
  133. /C 0 def
  134.  
  135. 0 1 63 {
  136.   /i exch def
  137.   /r R i get def
  138.   /a/b/c/d 4 i 3 and roll [ /d/c/b/a ] { exch def } forall
  139.  
  140.   t C [
  141.     a F i -4 bitshift get exec 
  142.     a A /x r -8 bitshift /get A T i get A
  143.     W { 1 32 bitshift 1 sub /and } if
  144.     /dup r 31 and /bitshift /exch r 31 and 32 sub /bitshift /or
  145.     b A
  146.     /def
  147.   ] dup length C add /C exch def putinterval
  148. } for
  149.  
  150. 1 1 C 1 sub {
  151.   dup 1 sub t exch get /def cvx eq
  152.     {pop}
  153.     {t exch 2 copy get cvx put}
  154.   ifelse
  155. } for
  156.  
  157. % If we could put t into a _packed_ array, its memory requirements
  158. % would go from about 13k to about 4k. Unfortunately, we'd need around
  159. % 1600 stack positions, around 3 times what we can expect to have 
  160. % available---and if that kind of memory is available, we don't really
  161. % need to pack t. Sigh.
  162.  
  163. % In fact, it's worse than that. You can't even determine what t will 
  164. % be and write it in directly (something like
  165. % { /a c d xor b and d xor a md5add x 0 get md5add -680876936 md5add dup 7 
  166. %   bitshift exch -25 bitshift or b md5add def /d b c xor a ...
  167. % ) because the scanner uses the operand stack to accumulate procedures.
  168. % So the only way to have md5transform as a single procedure is the above 
  169. % trick.
  170.  
  171. W /md5transform t end cvx bind def
  172.  
  173. % Unfortunately, PostScript & its imitators convert large
  174. % integers to floating-point. Worse, the fp representation probably
  175. % won't have 32 significant bits.
  176. % This procedure accounts for about 35% of the total time on 32-bit 
  177. % machines.
  178. not {
  179.   /md5add {
  180.     2 copy xor 0 lt
  181.       % if one is positive and one is negative, can't overflow
  182.       { add }
  183.       % if both are positive or negative
  184.       { 16#80000000 xor add 16#80000000 xor }
  185.       % same as subtracting (or adding) 2^31 and then subtracting (or 
  186.       % adding) it back.
  187.     ifelse
  188.   } bind def
  189. } {
  190.   /md5add {
  191.     add 16#0FFFFFFFF and
  192.   } bind def
  193. } ifelse
  194.  
  195. /md5 {
  196.   20 dict begin
  197.  
  198.   % initialise a,b,c,d,x
  199.   /a 16#67452301 def
  200.   /b 16#efcdab89 def
  201.   /c 16#98badcfe def
  202.   /d 16#10325476 def
  203.   /x 16 array def
  204.  
  205.   % parameters
  206.   /origs exch def
  207.   /oslen origs length def
  208.  
  209.   % pad string to multiple of 512 bits
  210.   /s oslen 72 add 64 idiv 64 mul dup /slen exch def string def
  211.   s 0 origs putinterval
  212.   s oslen 16#80 put
  213.   s slen 8 sub oslen 31 and 3 bitshift put
  214.   s slen 7 sub oslen -5 bitshift 255 and put
  215.   s slen 6 sub oslen -13 bitshift 255 and put
  216.  
  217.   0 64 slen 64 sub {
  218.     dup 1 exch 63 add { s exch get } for
  219.     15 -1 0 { x exch 6 2 roll 3 { 8 bitshift or } repeat put } for
  220.     a b c d
  221.     md5transform
  222.     d md5add /d exch def
  223.     c md5add /c exch def
  224.     b md5add /b exch def
  225.     a md5add /a exch def
  226.   } for
  227.   
  228.   16 string
  229.   [ [ a b c d ] { 3 { dup -8 bitshift } repeat } forall ]
  230.   0 1 15 {
  231.     3 copy dup 3 1 roll get 255 and put pop
  232.   } for
  233.   pop
  234.  
  235.   end
  236. } bind def
  237.  
  238. % Pad a key out to 32 bytes.
  239. /pdf_pad_key        % <key> pdf_pad_key <padded key>
  240.  { dup length 32 gt { 0 32 getinterval } if
  241.    <28bf4e5e4e758a41 64004e56fffa0108
  242.     2e2e00b6d0683e80 2f0ca9fe6453697a>
  243.    0 32 3 index length sub getinterval
  244.    concatstrings
  245.  } bind def
  246.  
  247. % Try a user key for a PDF file.
  248. /pdf_try_key        % <key> pdf_try_key <filekey> true
  249.             % <key> pdf_try_key false
  250.  { Trailer /Encrypt oget
  251.    dup /O oget exch
  252.    /P oget 4 string exch
  253.      2 copy 255 and 0 exch put
  254.      2 copy -8 bitshift 255 and 1 exch put
  255.      2 copy -16 bitshift 255 and 2 exch put
  256.      2 copy -24 bitshift 255 and 3 exch put pop
  257.    Trailer /ID oget 0 oget
  258.    3 { concatstrings } repeat
  259.    md5 0 5 getinterval
  260.    dup
  261.    Trailer /Encrypt oget /U oget dup length string copy exch
  262.    rc4setkey exch rc4 exch pop
  263.    <28bf4e5e4e758a41 64004e56fffa0108
  264.     2e2e00b6d0683e80 2f0ca9fe6453697a> eq
  265.    dup not { exch pop } if
  266.  } bind def
  267.  
  268. % Process the encryption information in the Trailer.
  269. /pdf_process_Encrypt
  270.  { Trailer /Encrypt oget
  271.    /Filter oget /Standard eq not
  272.     { (****This file uses an unknown security handler.\n) print flush
  273.     /pdf_process_Encrypt cvx /undefined signalerror
  274.     }
  275.    if
  276.    () pdf_pad_key pdf_try_key
  277.     { /FileKey exch def } 
  278.     { /PDFPassword where
  279.        { pop PDFPassword pdf_pad_key pdf_try_key 
  280.       { true }
  281.       { PDFPassword pdf_pad_key md5 0 5 getinterval rc4setkey
  282.         Trailer /Encrypt oget /O oget dup length string copy
  283.            rc4 exch pop
  284.         pdf_try_key
  285.       }
  286.      ifelse
  287.       { /FileKey exch def }
  288.       { (****Password did not work.\n) print flush
  289.         /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  290.       }
  291.      ifelse 
  292.        }
  293.        { (****This file has a user password set.\n) print flush
  294.      /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  295.        }
  296.       ifelse
  297.     }
  298.    ifelse
  299.    Trailer /Encrypt oget /P oget 4 and 0 eq #? and
  300.     { (****This owner of this file has requested you do not print it.\n)
  301.     print flush
  302.       /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  303.     }
  304.    if
  305.  } bind def
  306.  
  307. % Calculate the key used to decrypt an object (to pass to .decpdfrun or
  308. % put into a stream dictionary).
  309. /computeobjkey    % <object#> <generation#> computeobjkey <keystring>
  310. {
  311.   exch
  312.   10 string
  313.   dup 0 FileKey putinterval
  314.   exch
  315.         % stack:  gen# string obj#
  316.     2 copy 255 and 5 exch put
  317.     2 copy -8 bitshift 255 and 6 exch put
  318.     2 copy -16 bitshift 255 and 7 exch put
  319.   pop exch
  320.     2 copy 255 and 8 exch put
  321.     2 copy -8 bitshift 255 and 9 exch put
  322.   pop md5 0 10 getinterval
  323. } bind def
  324.  
  325. % As .pdfrun, but decrypt strings with key <key>.
  326. /.decpdfrun            % <file> <keystring> <opdict> .decpdfrun -
  327.  {    % Construct a procedure with the file, opdict and key bound into it.
  328.    2 index cvlit mark mark 5 2 roll
  329.     { .pdftoken not { (%%EOF) cvn cvx } if
  330.       dup xcheck
  331.        { DEBUG { dup == flush } if
  332.      3 -1 roll pop
  333.      2 copy .knownget
  334.       { exch pop exch pop exec
  335.           }
  336.       { exch pop
  337.             dup /true eq
  338.               { pop //true
  339.               }
  340.               { dup /false eq
  341.                   { pop //false 
  342.                   }
  343.                   { dup /null eq
  344.                       { pop //null
  345.                       }
  346.                       { (%stderr) (w) file
  347.                     dup (****************Unknown operator: ) writestring
  348.                     dup 3 -1 roll .writecvs dup (\n) writestring flushfile
  349.                       }
  350.                     ifelse
  351.                   }
  352.                 ifelse
  353.               }
  354.             ifelse
  355.       }
  356.      ifelse
  357.        }
  358.        { exch pop DEBUG { dup ==only ( ) print flush } if
  359.      dup type /stringtype eq
  360.       { exch rc4setkey exch rc4 }
  361.      if
  362.      exch pop
  363.        }
  364.       ifelse
  365.     }
  366.    aload pop .packtomark cvx
  367.    /loop cvx 2 packedarray cvx
  368.     { stopped /PDFsource } aload pop
  369.    PDFsource
  370.     { store { stop } if } aload pop .packtomark cvx 
  371.    /PDFsource 3 -1 roll store exec
  372.  } bind def
  373.  
  374. % Run the code to resolve an object reference.
  375. /pdf_run_resolve
  376.  { /FileKey where
  377.     { pop
  378.       2 copy computeobjkey dup 4 1 roll
  379.       PDFfile exch resolveopdict .decpdfrun
  380.       dup dup dup 5 2 roll
  381.     % stack: object object key object object
  382.       xcheck exch type /dicttype eq and
  383.        { /StreamKey exch put }
  384.        { pop pop }
  385.       ifelse
  386.     }
  387.     { PDFfile resolveopdict .pdfrun }
  388.    ifelse
  389.  } bind def
  390.  
  391. % Prefix a decryption filter to a stream if needed.
  392. % Stack: readdata? dict parms file/string filternames
  393. % (both before and after).
  394. /pdf_decrypt_stream
  395.  { 3 index /StreamKey known
  396.    {
  397.       exch 
  398.     % Stack: readdata? dict parms filternames file/string
  399.       3 index /Length oget () /SubFileDecode filter
  400.       3 index /StreamKey get rc4decodefilter
  401.       exch
  402.    } if
  403.  } bind def
  404.  
  405. end            % pdfdict
  406. .setglobal
  407.