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 4% not removed. 5 6% pdf_sec.ps (version 1.0.3) 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/.setlanguagelevel where { pop 2 .setlanguagelevel } if 15.currentglobal true .setglobal 16/pdfdict where { pop } { /pdfdict 100 dict def } ifelse 17pdfdict begin 18 19% Older ghostscript versions do not have .pdftoken, so we use 'token' instead. 20/.pdftoken where { pop } { /.pdftoken /token load def } ifelse 21 22% An implementation of an algorithm compatible with the RSA Data Security 23% Inc. RC4 stream encryption algorithm. 24 25% <string> rc4setkey <dict> 26/rc4setkey 27{ 28 6 dict begin 29 /k exch def 30 /a 256 string def 31 0 1 255 { a exch dup put } for 32 /l k length def 33 /j 0 def 34 0 1 255 35 { 36 /i exch def 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 39 } for 40 3 dict dup begin 41 /a a def 42 /x 0 def 43 /y 0 def 44 end 45 end 46} bind def 47 48% <rc4key> <string> rc4 <string> <rc4key> 49/rc4 50{ 51 1 index begin 52 dup dup length 1 sub 0 exch 1 exch 53 { 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 58 2 copy get 59 a dup x get a y get add 255 and get 60 xor put dup 61 } for 62 pop 63 end 64} bind def 65 66% take a stream and rc4 decrypt it. 67% <stream> <key> rc4decodefilter <stream> 68/rc4decodefilter { 69 currentglobal exch true setglobal 70 dup length string copy rc4setkey 71 exch setglobal 72 exch 512 string 73 % stack: <key> <stream> <string> 74 { readstring pop rc4 exch pop } aload pop 75 8 array astore cvx 0 () /SubFileDecode filter 76} bind def 77 78% MD5 derived from RFC 1321, "The MD5 Message-Digest Algorithm", 79% R. Rivest, MIT, RSADSI; implemented in PostScript by Geoffrey Keating. 80 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 85% wide integers. 86% This will not run on interpreters with 16-bit wide integers, if such 87% things exist. 8820 dict begin 89 90/T [ 9116#d76aa478 16#e8c7b756 16#242070db 16#c1bdceee 9216#f57c0faf 16#4787c62a 16#a8304613 16#fd469501 9316#698098d8 16#8b44f7af 16#ffff5bb1 16#895cd7be 9416#6b901122 16#fd987193 16#a679438e 16#49b40821 9516#f61e2562 16#c040b340 16#265e5a51 16#e9b6c7aa 9616#d62f105d 16#02441453 16#d8a1e681 16#e7d3fbc8 9716#21e1cde6 16#c33707d6 16#f4d50d87 16#455a14ed 9816#a9e3e905 16#fcefa3f8 16#676f02d9 16#8d2a4c8a 9916#fffa3942 16#8771f681 16#6d9d6122 16#fde5380c 10016#a4beea44 16#4bdecfa9 16#f6bb4b60 16#bebfbc70 10116#289b7ec6 16#eaa127fa 16#d4ef3085 16#04881d05 10216#d9d4d039 16#e6db99e5 16#1fa27cf8 16#c4ac5665 10316#f4292244 16#432aff97 16#ab9423a7 16#fc93a039 10416#655b59c3 16#8f0ccc92 16#ffeff47d 16#85845dd1 10516#6fa87e4f 16#fe2ce6e0 16#a3014314 16#4e0811a1 10616#f7537e82 16#bd3af235 16#2ad7d2bb 16#eb86d391 107] def 108/F [ 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 } 111] def 112/R [ 11316#0007 16#010c 16#0211 16#0316 16#0407 16#050c 16#0611 16#0716 11416#0807 16#090c 16#0a11 16#0b16 16#0c07 16#0d0c 16#0e11 16#0f16 11516#0105 16#0609 16#0b0e 16#0014 16#0505 16#0a09 16#0f0e 16#0414 11616#0905 16#0e09 16#030e 16#0814 16#0d05 16#0209 16#070e 16#0c14 11716#0504 16#080b 16#0b10 16#0e17 16#0104 16#040b 16#0710 16#0a17 11816#0d04 16#000b 16#0310 16#0617 16#0904 16#0c0b 16#0f10 16#0217 11916#0006 16#070a 16#0e0f 16#0515 16#0c06 16#030a 16#0a0f 16#0115 12016#0806 16#0f0a 16#060f 16#0d15 16#0406 16#0b0a 16#020f 16#0915 121] def 122 123/W 1 31 bitshift 0 gt def 124/A W { /add } { /md5add } ifelse def 125/t W { 1744 } { 1616 } ifelse array def 126/C 0 def 127 1280 1 63 { 129 /i exch def 130 /r R i get def 131 /a/b/c/d 4 i 3 and roll [ /d/c/b/a ] { exch def } forall 132 133 t C [ 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 138 b A 139 /def 140 ] dup length C add /C exch def putinterval 141} for 142 1431 1 C 1 sub { 144 dup 1 sub t exch get /def cvx eq 145 {pop} 146 {t exch 2 copy get cvx put} 147 ifelse 148} for 149 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. 155 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 162% trick. 163 164W /md5transform t end cvx bind def 165 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 170% machines. 171not { 172 /md5add { 173 2 copy xor 0 lt 174 % if one is positive and one is negative, can't overflow 175 { add } 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 179 % adding) it back. 180 ifelse 181 } bind def 182} { 183 /md5add { 184 add 16#0FFFFFFFF and 185 } bind def 186} ifelse 187 188/md5 { 189 20 dict begin 190 191 % initialise a,b,c,d,x 192 /a 16#67452301 def 193 /b 16#efcdab89 def 194 /c 16#98badcfe def 195 /d 16#10325476 def 196 /x 16 array def 197 198 % parameters 199 /origs exch def 200 /oslen origs length def 201 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 205 s oslen 16#80 put 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 209 210 0 64 slen 64 sub { 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 213 a b c d 214 md5transform 215 d md5add /d exch def 216 c md5add /c exch def 217 b md5add /b exch def 218 a md5add /a exch def 219 } for 220 221 16 string 222 [ [ a b c d ] { 3 { dup -8 bitshift } repeat } forall ] 223 0 1 15 { 224 3 copy dup 3 1 roll get 255 and put pop 225 } for 226 pop 227 228 end 229} bind def 230 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 237 concatstrings 238 } bind def 239 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 244 dup /O oget exch 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 252 md5 0 5 getinterval 253 dup 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 259 } bind def 260 261% Process the encryption information in the Trailer. 262/pdf_process_Encrypt 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 267 } 268 if 269 () pdf_pad_key pdf_try_key 270 { /FileKey exch def } 271 { /PDFPassword where 272 { PDFPassword pdf_pad_key pdf_try_key 273 { true } 274 { PDFPassword pdf_pad_key md5 0 5 getinterval rc4setkey 275 Trailer /Encrypt oget /O oget dup length string copy 276 rc4 exch pop 277 pdf_try_key 278 } 279 ifelse 280 { /FileKey exch def } 281 { (****Password did not work.\n) print flush 282 /pdf_process_Encrypt cvx /invalidfileaccess signalerror 283 } 284 ifelse 285 } 286 { (****This file has a user password set.\n) print flush 287 /pdf_process_Encrypt cvx /invalidfileaccess signalerror 288 } 289 ifelse 290 } 291 ifelse 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) 294 print flush 295 /pdf_process_Encrypt cvx /invalidfileaccess signalerror 296 } 297 if 298 } bind def 299 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> 303{ 304 exch 305 10 string 306 dup 0 FileKey putinterval 307 exch 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 312 pop exch 313 2 copy 255 and 8 exch put 314 2 copy -8 bitshift 255 and 9 exch put 315 pop md5 0 10 getinterval 316} bind def 317 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 323 dup xcheck 324 { DEBUG { dup == flush } if 325 3 -1 roll pop 326 2 copy .knownget 327 { exch pop exch pop exec } 328 { (%stderr) (w) file 329 dup (****************Unknown operator: ) writestring 330 dup 3 -1 roll .writecvs dup (\n) writestring flushfile 331 pop 332 } 333 ifelse 334 } 335 { exch pop DEBUG { dup ==only ( ) print flush } if 336 dup type /stringtype eq 337 { exch rc4setkey exch rc4 } 338 if 339 exch pop 340 } 341 ifelse 342 } 343 aload pop .packtomark cvx 344 /loop cvx 2 packedarray cvx 345 { stopped /PDFsource } aload pop 346 PDFsource 347 { store { stop } if } aload pop .packtomark cvx 348 /PDFsource 3 -1 roll store exec 349 } bind def 350 351% Run the code to resolve an object reference. 352/pdf_run_resolve 353 { /FileKey where 354 { pop 355 2 copy computeobjkey dup 4 1 roll 356 PDFfile exch resolveopdict .decpdfrun 357 dup dup dup 5 2 roll 358 % stack: object object key object object 359 xcheck exch type /dicttype eq and 360 { /StreamKey exch put } 361 { pop pop } 362 ifelse 363 } 364 { PDFfile resolveopdict .pdfrun } 365 ifelse 366 } bind def 367 368% Prefix a decryption filter to a stream if needed. 369% Stack: readdata? dict parms file/string filternames 370% (both before and after). 371/pdf_decrypt_stream 372 { 3 index /StreamKey known 373 { 374 exch 375 % Stack: readdata? dict parms filternames file/string 376 3 index /Length oget () /SubFileDecode filter 377 3 index /StreamKey get rc4decodefilter 378 exch 379 } if 380 } bind def 381 382end % pdfdict 383.setglobal 384