% Step 5.
Trailer /ID knownoget { 0 oget } {
()
( **** Error: ID key in the trailer is required for encrypted files.\n) pdfformaterror
( File may not be possible to decrypt.\n) pdfformaterror
} ifelse
3 { concatstrings } repeat
% We will finish step 5 after possibly including step 6.
% The following only executed for /R equal to 3 or more
Trailer /Encrypt oget dup /R oget dup 3 ge {
% Step 6. If EncryptMetadata is false, pass 0xFFFFFFFF to md5 function
% The PDF 1.5 Spec says that EncryptMetadata is an undocumented
% feature of PDF 1.4. That implies that this piece of logic should
% be executed if R >= 3. However testing with Acrobat 5.0 and 6.0 shows
% that this step is not executed if R equal to 3. Thus we have a test for
% R being >= 4.
4 ge {
/EncryptMetadata knownoget % Get EncryptMetadata (if present)
not { //true } if % Default is true
not { % If EncryptMetadata is false
concatstrings % Add 0xFFFFFFFF to working string
} if
} {
pop % Remove Encrypt dict
} ifelse
md5_trunk % Finish step 5 and 6.
% Step 7. Executed as part of step 6
% Step 8. (This step is defintely a part of PDF 1.4.)
50 { md5_trunk } repeat
} {
pop pop md5_trunk % Remove R, Encrypt dict, finish step 5
} ifelse
% Step 9 - Done in md5_trunk.
} bind executeonly def
% Algorithm 3.4
/pdf_gen_user_password_R2 { % pdf_gen_user_password_R2
% Step 2.
pdf_padding_string exch arc4decode
} bind executeonly def
% Algorithm 3.5
/pdf_gen_user_password_R3 { % pdf_gen_user_password_R3
% Step 2.
pdf_padding_string
% Step 3.
Trailer /ID knownoget { 0 oget } {
()
( **** Error: ID key in the trailer is required for encrypted files.\n) pdfformaterror
( File may not be possible to decrypt.\n) pdfformaterror
} ifelse
concatstrings md5
% Step 4.
1 index arc4decode
% Step 5.
1 1 19 {
2 index pdf_xorbytes arc4decode
} for
exch pop
} bind executeonly def
/pdf_gen_user_password { % pdf_gen_user_password
% common Step 1 of Algorithms 3.4 and 3.5.
pdf_compute_encryption_key dup
Trailer /Encrypt oget
/R oget dup 2 eq {
pop pdf_gen_user_password_R2
} {
dup 3 eq {
pop pdf_gen_user_password_R3
} {
dup 4 eq { % 4 uses the algorithm as 3
pop pdf_gen_user_password_R3
} {
% This procedure is only used if R is between 2 and 4,
% so we should never get here.
/pdf_gen_user_password cvx /undefined signalerror
} ifelse
} ifelse
} ifelse
} bind executeonly def
% Algorithm 3.6
% pdf_check_pre_r5_user_password true
% pdf_check_pre_r5_user_password false
/pdf_check_pre_r5_user_password {
pdf_gen_user_password
Trailer /Encrypt oget /U oget
0 2 index length getinterval eq {
//true
} {
pop //false
} ifelse
} bind executeonly def
% Compute an owner key, ie the result of step 4 of Algorithm 3.3
/pdf_owner_key % pdf_owner_key
{
% Step 1.
pdf_pad_key
% Step 2.
md5_trunk
% 3.3 Step 3. Only executed for /R equal to 3 or more
Trailer /Encrypt oget /R oget 3 ge {
50 { md5_trunk } repeat
} if
% Step 4 - Done in md5_trunk.
} bind executeonly def
% Algorithm 3.7
% pdf_check_pre_r5_owner_password true
% pdf_check_pre_r5_owner_password false
/pdf_check_pre_r5_owner_password {
% Step 1.
pdf_owner_key
% Step 2.
Trailer /Encrypt oget dup /O oget 2 index arc4decode
%
% Step 3. Only executed for /R equal to 3 or more
exch /R oget 3 ge {
1 1 19 {
2 index pdf_xorbytes arc4decode
} for
} if
exch pop
%
pdf_check_pre_r5_user_password
} bind executeonly def
% Algorithm 3.2a
% pdf_check_r5_password true
% pdf_check_r5_password false
/pdf_check_r5_password {
10 dict begin % temporary dict for local variables
% Step 1.
% If the .saslprep operator isn't available (because ghostscript
% wasn't built with libidn support), just skip this step. ASCII
% passwords will still work fine, and even most non-ASCII passwords
% will be okay; any non-ASCII passwords that fail will produce a
% warning from pdf_process_Encrypt.
/.saslprep where { pop .saslprep } if
% Step 2.
dup length 127 gt { 0 127 getinterval } if
/Password exch def
% Step 3.
/O Trailer /Encrypt oget /O oget def
/U Trailer /Encrypt oget /U oget def
Password O 32 8 getinterval concatstrings
U 0 48 getinterval concatstrings sha256
O 0 32 getinterval eq {
% Step 3, second paragraph.
Password O 40 8 getinterval concatstrings
U 0 48 getinterval concatstrings sha256
16 string Trailer /Encrypt oget /OE oget concatstrings
<< /Key 4 -1 roll /Padding //false >> aesdecode //true
} {
% Step 4.
Password U 32 8 getinterval concatstrings sha256
U 0 32 getinterval eq {
% Step 4, second paragraph.
Password U 40 8 getinterval concatstrings sha256
16 string Trailer /Encrypt oget /UE oget concatstrings
<< /Key 4 -1 roll /Padding //false >> aesdecode //true
} {
//false
} ifelse
} ifelse
% Step 5.
dup {
% Adobe says to decrypt the Perms string using "ECB mode with
% an initialization vector of zero", which must be a mistake --
% ECB mode doesn't use initialization vectors. It looks like
% they meant "ECB mode, or CBC mode with an initialization
% vector of zero", since the two are equivalent for a single-
% block message. We use the latter.
16 string Trailer /Encrypt oget /Perms oget concatstrings
<< /Key 4 index /Padding //false >> aesdecode
9 3 getinterval (adb) eq not {
( **** Error: Failed to decrypt Perms string.\n) pdfformaterror
( Cannot decrypt PDF file.\n) pdfformaterror
printProducer
/pdf_check_r5_password cvx /rangecheck signalerror
} if
} if
end
} bind executeonly def
% pdf_check_password true
% pdf_check_password false
/pdf_check_password {
% If R is 2, 3, or 4, use Algorithms 3.6 and 3.7 to see if this is
% a valid user or owner password. Following historical practice,
% we treat the password as an arbitrary string of bytes and don't
% interpret it in any way. (If the password fails, it would be
% nice to try some plausible character set conversions, but this
% gets complicated. Even Adobe products don't seem to handle it
% consistently.)
%
% If R is 5, use Algorithm 3.2a. The password should be text, in
% either UTF-8 or the current locale's charset.
Trailer /Encrypt oget /R oget
dup dup 2 ge exch 4 le and {
pop
dup pdf_check_pre_r5_user_password {
exch pop //true
} {
pdf_check_pre_r5_owner_password
} ifelse
} {
dup 5 eq {
pop
% First, try the password as UTF-8.
dup pdf_check_r5_password {
exch pop //true
} {
% The password didn't work as UTF-8, so maybe it's in the
% locale character set instead. If possible, convert it to
% UTF-8 and try again.
/.locale_to_utf8 where {
pop .locale_to_utf8 pdf_check_r5_password
} {
pop //false
} ifelse
} ifelse
} {
dup 6 eq {
pop
% First, try the password as UTF-8.
dup Trailer /Encrypt oget check_r6_password {
exch pop //true
} {
% The password didn't work as UTF-8, so maybe it's in the
% locale character set instead. If possible, convert it to
% UTF-8 and try again.
/.locale_to_utf8 where {
pop .locale_to_utf8 Trailer /Encrypt oget check_r6_password
} {
pop //false
} ifelse
} ifelse
} {
( **** Warning: This file uses an unknown standard security handler revision: )
exch =string cvs concatstrings (\n) concatstrings pdfformatwarning
( Cannot decrypt PDF file.\n) pdfformaterror
printProducer
/pdf_check_password cvx /undefined signalerror
} ifelse
} ifelse
} ifelse
} bind executeonly def
% Process the encryption information in the Trailer.
/pdf_process_Encrypt {
Trailer /Encrypt oget
/Filter oget /Standard eq not {
( **** Warning: This file uses an unknown security handler.\n) pdfformatwarning
( Cannot decrypt PDF file.\n) pdfformaterror
printProducer
/pdf_process_Encrypt cvx /undefined signalerror
} if
() pdf_check_password
{
/FileKey exch def
} {
/PDFPassword where {
pop PDFPassword pdf_check_password
{
/FileKey exch def
} {
( **** Error: Password did not work.\n) pdfformaterror
( Cannot decrypt PDF file.\n) pdfformaterror
% If ghostscript was built without libidn, it's missing the
% .saslprep operator and thus can't do proper Unicode password
% normalization. Similarly, if the system provides neither
% iconv nor the Windows MultiByteToWideChar function, then we
% won't have the .locale_to_utf8 operator to convert passwords
% from the locale character set to UTF-8.
%
% It's not a huge problem if you're missing either or both of
% these. ASCII passwords will work fine regardless, and even
% Unicode passwords will often be okay.
%
% However, if .saslprep or .locale_to_utf8 is missing, and the
% user enters a non-ASCII password that doesn't work, we give
% a warning message.
PDFPassword contains_non_ascii {
/.saslprep where not {
( **** WARNING: Ghostscript was configured without libidn,\n)
( **** so non-ASCII passwords aren't supported!\n)
concatstrings pdfformaterror
} {
pop
/.locale_to_utf8 where not {
( **** WARNING: Ghostscript was configured without iconv,\n)
( **** so non-ASCII passwords aren't supported!\n)
concatstrings pdfformaterror
} {
pop
} ifelse
} ifelse
} if
/pdf_process_Encrypt cvx /invalidfileaccess signalerror
} ifelse
} {
( **** This file requires a password for access.\n) pdfformaterror
/pdf_process_Encrypt cvx /invalidfileaccess signalerror
} ifelse
} ifelse
% Trailer /Encrypt oget /P oget 4 and 0 eq #? and
% { ( ****This owner of this file has requested you do not print it.\n)
% pdfformaterror printProducer
% /pdf_process_Encrypt cvx /invalidfileaccess signalerror
% }
% if
} bind executeonly def
% Calculate the key used to decrypt an object (to pass to .decpdfrun or
% put into a stream dictionary).
/computeobjkey %