1 % Copyright (C) 1996, 1997 Geoffrey Keating.
2 % This file may be freely distributed with or without modifications,
3 % so long as proper attribution is given and copyright notices are
6 % pdf_sec.ps (version 1.0.3)
7 % Implementation of security hooks for PDF reader.
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
14 /.setlanguagelevel where { pop 2 .setlanguagelevel } if
15 .currentglobal true .setglobal
16 /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
19 % Older ghostscript versions do not have .pdftoken, so we use 'token' instead.
20 /.pdftoken where { pop } { /.pdftoken /token load def } ifelse
22 % An implementation of an algorithm compatible with the RSA Data Security
23 % Inc. RC4 stream encryption algorithm.
25 % <string> rc4setkey <dict>
31 0 1 255 { a exch dup put } for
37 /j a i get k i l mod get add j add 255 and def
38 a i a j get a j a i get put put
48 % <rc4key> <string> rc4 <string> <rc4key>
52 dup dup length 1 sub 0 exch 1 exch
54 /x x 1 add 255 and def
55 /y a x get y add 255 and def
56 a x a y get a y a x get put put
57 % stack: string string index
59 a dup x get a y get add 255 and get
66 % take a stream and rc4 decrypt it.
67 % <stream> <key> rc4decodefilter <stream>
69 currentglobal exch true setglobal
70 dup length string copy rc4setkey
73 % stack: <key> <stream> <string>
74 { readstring pop rc4 exch pop } aload pop
75 8 array astore cvx 0 () /SubFileDecode filter
78 % MD5 derived from RFC 1321, "The MD5 Message-Digest Algorithm",
79 % R. Rivest, MIT, RSADSI; implemented in PostScript by Geoffrey Keating.
81 % We construct the MD5 transform by a sort of inline expansion.
82 % this takes up quite a bit of memory (around 17k), but gives a
83 % factor-of-two speed increase.
84 % This also allows us to take advantage of interpreters with 64-bit
86 % This will not run on interpreters with 16-bit wide integers, if such
91 16#d76aa478 16#e8c7b756 16#242070db 16#c1bdceee
92 16#f57c0faf 16#4787c62a 16#a8304613 16#fd469501
93 16#698098d8 16#8b44f7af 16#ffff5bb1 16#895cd7be
94 16#6b901122 16#fd987193 16#a679438e 16#49b40821
95 16#f61e2562 16#c040b340 16#265e5a51 16#e9b6c7aa
96 16#d62f105d 16#02441453 16#d8a1e681 16#e7d3fbc8
97 16#21e1cde6 16#c33707d6 16#f4d50d87 16#455a14ed
98 16#a9e3e905 16#fcefa3f8 16#676f02d9 16#8d2a4c8a
99 16#fffa3942 16#8771f681 16#6d9d6122 16#fde5380c
100 16#a4beea44 16#4bdecfa9 16#f6bb4b60 16#bebfbc70
101 16#289b7ec6 16#eaa127fa 16#d4ef3085 16#04881d05
102 16#d9d4d039 16#e6db99e5 16#1fa27cf8 16#c4ac5665
103 16#f4292244 16#432aff97 16#ab9423a7 16#fc93a039
104 16#655b59c3 16#8f0ccc92 16#ffeff47d 16#85845dd1
105 16#6fa87e4f 16#fe2ce6e0 16#a3014314 16#4e0811a1
106 16#f7537e82 16#bd3af235 16#2ad7d2bb 16#eb86d391
109 { c d /xor b /and d /xor } { b c /xor d /and c /xor }
110 { b c /xor d /xor } { d /not b /or c /xor }
113 16#0007 16#010c 16#0211 16#0316 16#0407 16#050c 16#0611 16#0716
114 16#0807 16#090c 16#0a11 16#0b16 16#0c07 16#0d0c 16#0e11 16#0f16
115 16#0105 16#0609 16#0b0e 16#0014 16#0505 16#0a09 16#0f0e 16#0414
116 16#0905 16#0e09 16#030e 16#0814 16#0d05 16#0209 16#070e 16#0c14
117 16#0504 16#080b 16#0b10 16#0e17 16#0104 16#040b 16#0710 16#0a17
118 16#0d04 16#000b 16#0310 16#0617 16#0904 16#0c0b 16#0f10 16#0217
119 16#0006 16#070a 16#0e0f 16#0515 16#0c06 16#030a 16#0a0f 16#0115
120 16#0806 16#0f0a 16#060f 16#0d15 16#0406 16#0b0a 16#020f 16#0915
123 /W 1 31 bitshift 0 gt def
124 /A W { /add } { /md5add } ifelse def
125 /t W { 1744 } { 1616 } ifelse array def
131 /a/b/c/d 4 i 3 and roll [ /d/c/b/a ] { exch def } forall
134 a F i -4 bitshift get exec
135 a A /x r -8 bitshift /get A T i get A
136 W { 1 32 bitshift 1 sub /and } if
137 /dup r 31 and /bitshift /exch r 31 and 32 sub /bitshift /or
140 ] dup length C add /C exch def putinterval
144 dup 1 sub t exch get /def cvx eq
146 {t exch 2 copy get cvx put}
150 % If we could put t into a _packed_ array, its memory requirements
151 % would go from about 13k to about 4k. Unfortunately, we'd need around
152 % 1600 stack positions, around 3 times what we can expect to have
153 % available---and if that kind of memory is available, we don't really
154 % need to pack t. Sigh.
156 % In fact, it's worse than that. You can't even determine what t will
157 % be and write it in directly (something like
158 % { /a c d xor b and d xor a md5add x 0 get md5add -680876936 md5add dup 7
159 % bitshift exch -25 bitshift or b md5add def /d b c xor a ...
160 % ) because the scanner uses the operand stack to accumulate procedures.
161 % So the only way to have md5transform as a single procedure is the above
164 W /md5transform t end cvx bind def
166 % Unfortunately, PostScript & its imitators convert large
167 % integers to floating-point. Worse, the fp representation probably
168 % won't have 32 significant bits.
169 % This procedure accounts for about 35% of the total time on 32-bit
174 % if one is positive and one is negative, can't overflow
176 % if both are positive or negative
177 { 16#80000000 xor add 16#80000000 xor }
178 % same as subtracting (or adding) 2^31 and then subtracting (or
191 % initialise a,b,c,d,x
200 /oslen origs length def
202 % pad string to multiple of 512 bits
203 /s oslen 72 add 64 idiv 64 mul dup /slen exch def string def
204 s 0 origs putinterval
206 s slen 8 sub oslen 31 and 3 bitshift put
207 s slen 7 sub oslen -5 bitshift 255 and put
208 s slen 6 sub oslen -13 bitshift 255 and put
211 dup 1 exch 63 add { s exch get } for
212 15 -1 0 { x exch 6 2 roll 3 { 8 bitshift or } repeat put } for
222 [ [ a b c d ] { 3 { dup -8 bitshift } repeat } forall ]
224 3 copy dup 3 1 roll get 255 and put pop
231 % Pad a key out to 32 bytes.
232 /pdf_pad_key % <key> pdf_pad_key <padded key>
233 { dup length 32 gt { 0 32 getinterval } if
234 <28bf4e5e4e758a41 64004e56fffa0108
235 2e2e00b6d0683e80 2f0ca9fe6453697a>
236 0 32 3 index length sub getinterval
240 % Try a user key for a PDF file.
241 /pdf_try_key % <key> pdf_try_key <filekey> true
242 % <key> pdf_try_key false
243 { Trailer /Encrypt oget
245 /P oget 4 string exch
246 2 copy 255 and 0 exch put
247 2 copy -8 bitshift 255 and 1 exch put
248 2 copy -16 bitshift 255 and 2 exch put
249 2 copy -24 bitshift 255 and 3 exch put pop
250 Trailer /ID oget 0 oget
251 3 { concatstrings } repeat
254 Trailer /Encrypt oget /U oget dup length string copy exch
255 rc4setkey exch rc4 exch pop
256 <28bf4e5e4e758a41 64004e56fffa0108
257 2e2e00b6d0683e80 2f0ca9fe6453697a> eq
258 dup not { exch pop } if
261 % Process the encryption information in the Trailer.
263 { Trailer /Encrypt oget
264 /Filter oget /Standard eq not
265 { (****This file uses an unknown security handler.\n) print flush
266 /pdf_process_Encrypt cvx /undefined signalerror
269 () pdf_pad_key pdf_try_key
270 { /FileKey exch def }
272 { PDFPassword pdf_pad_key pdf_try_key
274 { PDFPassword pdf_pad_key md5 0 5 getinterval rc4setkey
275 Trailer /Encrypt oget /O oget dup length string copy
280 { /FileKey exch def }
281 { (****Password did not work.\n) print flush
282 /pdf_process_Encrypt cvx /invalidfileaccess signalerror
286 { (****This file has a user password set.\n) print flush
287 /pdf_process_Encrypt cvx /invalidfileaccess signalerror
292 Trailer /Encrypt oget /P oget 4 and 0 eq #? and
293 { (****This owner of this file has requested you do not print it.\n)
295 /pdf_process_Encrypt cvx /invalidfileaccess signalerror
300 % Calculate the key used to decrypt an object (to pass to .decpdfrun or
301 % put into a stream dictionary).
302 /computeobjkey % <object#> <generation#> computeobjkey <keystring>
306 dup 0 FileKey putinterval
308 % stack: gen# string obj#
309 2 copy 255 and 5 exch put
310 2 copy -8 bitshift 255 and 6 exch put
311 2 copy -16 bitshift 255 and 7 exch put
313 2 copy 255 and 8 exch put
314 2 copy -8 bitshift 255 and 9 exch put
315 pop md5 0 10 getinterval
318 % As .pdfrun, but decrypt strings with key <key>.
319 /.decpdfrun % <file> <keystring> <opdict> .decpdfrun -
320 { % Construct a procedure with the file, opdict and key bound into it.
321 2 index cvlit mark mark 5 2 roll
322 { .pdftoken not { (%%EOF) cvn cvx } if
324 { DEBUG { dup == flush } if
327 { exch pop exch pop exec }
329 dup (****************Unknown operator: ) writestring
330 dup 3 -1 roll .writecvs dup (\n) writestring flushfile
335 { exch pop DEBUG { dup ==only ( ) print flush } if
336 dup type /stringtype eq
337 { exch rc4setkey exch rc4 }
343 aload pop .packtomark cvx
344 /loop cvx 2 packedarray cvx
345 { stopped /PDFsource } aload pop
347 { store { stop } if } aload pop .packtomark cvx
348 /PDFsource 3 -1 roll store exec
351 % Run the code to resolve an object reference.
355 2 copy computeobjkey dup 4 1 roll
356 PDFfile exch resolveopdict .decpdfrun
358 % stack: object object key object object
359 xcheck exch type /dicttype eq and
360 { /StreamKey exch put }
364 { PDFfile resolveopdict .pdfrun }
368 % Prefix a decryption filter to a stream if needed.
369 % Stack: readdata? dict parms file/string filternames
370 % (both before and after).
372 { 3 index /StreamKey known
375 % Stack: readdata? dict parms filternames file/string
376 3 index /Length oget () /SubFileDecode filter
377 3 index /StreamKey get rc4decodefilter