pdf/src/encrypt.rs#
Source: lib/pdf/src/encrypt.rs
encrypt -- how a document is encrypted when it is written (Document::set_encryption) or
written again in full (edit::Update::save_full): the standard security handler of ISO 32000-2
(7.6.4) with AES-256 (revision 6, PDF 2.0; in a PDF 1.7 file with Adobe's extension level 8,
as qpdf writes it) or AES-128 (revision 4, for readers older than Acrobat X). RC4 is not
written (pdf::read still opens it).
Encryption::aes256(user, owner) the user password opens the file, the owner password
Encryption::aes128(user, owner) also lifts the permissions; an empty owner password is
the user password (as qpdf has it)
.permissions(Permissions::PRINT | Permissions::COPY) what the user may do (all)
.encrypt_metadata(false) XMP metadata stays readable (encrypted)
.with_seed(seed) the 32 bytes the key, the salts and the IDs come from
Permissions PRINT, MODIFY, COPY, ANNOTATE, FILL_FORMS, ACCESSIBILITY, ASSEMBLE, PRINT_HIGH,
ALL, NONE; from_p / p: the /P value of a file (read::Document::permissions)
Passwords of AES-256 are UTF-8, truncated to 127 bytes as ISO 32000-2 says (SASLprep is not
applied, as pdf::read does not); those of AES-128 are PDFDocEncoding (as qpdf and Acrobat take
them: "€" yes, a no-break space no; other characters are refused) and only their first 32 bytes
count. User and owner passwords that are the same after the cut are refused unless they are
the same (an empty owner password): the user's would open the file as the owner's.
The key, the salts and the file ID come from the system's random numbers (plat::fill_random),
or from the seed given: the same document, the same options and the same seed give the same
bytes, and the file key of AES-256 is the seed's, so a seed must be as secret and as random as
a key: one seed for two documents gives both the same key, and a seed used again when the
passwords change keeps the old key (who knew it still decrypts). Leave the seed out for real
encryption on the systems that have random numbers; fixed seeds are for output that must be
reproducible, as in tests. The browser has no random numbers of plat, so there with_seed is needed
(from the page's crypto.getRandomValues). Permissions are the file's request to the viewer;
nothing in the format enforces them.
enum Cipherthe cipher of an encrypted documentAes256-- AES-256 (security handler revision 6): what readers since Acrobat X openAes128-- AES-128 (revision 4): for older readers; its password hash (MD5) is weaker
struct Permissions(u32)what a user who opened the document with the user password may do (ISO 32000-1, Table 22)
impl Permissions
const PRINT: Permissions = Permissions(1 << 2)print (in low quality without PRINT_HIGH)const MODIFY: Permissions = Permissions(1 << 3)change the document otherwise than ANNOTATE, FILL_FORMS and ASSEMBLE allowconst COPY: Permissions = Permissions(1 << 4)copy or extract text and graphicsconst ANNOTATE: Permissions = Permissions(1 << 5)add or change annotations, fill in forms (with MODIFY: create fields)const FILL_FORMS: Permissions = Permissions(1 << 8)fill in forms and sign, also without ANNOTATEconst ACCESSIBILITY: Permissions = Permissions(1 << 9)extract text and graphics for accessibilityconst ASSEMBLE: Permissions = Permissions(1 << 10)insert, rotate, delete pages, bookmarks and thumbnailsconst PRINT_HIGH: Permissions = Permissions(1 << 11)print in full qualityconst NONE: Permissions = Permissions(0)nothingconst ALL: Permissions = Permissions(0xF3C)everythingfn from_p(p: u32) -> Permissionsthe permissions of a /P value (the bits of the eight above)fn p(self) -> u32the /P value: these bits, bits 7 and 8 and 13 to 32 set as revisions 3 and later wantfn contains(self, other: Permissions) -> boolstruct Encryptionhow a document is encrypted: the cipher, the passwords, the permissions
impl Encryption
fn aes256(user: &str, owner: &str) -> EncryptionAES-256 with these passwords, every permission, metadata encryptedfn aes128(user: &str, owner: &str) -> EncryptionAES-128 with these passwords (PDFDocEncoding, the first 32 bytes count), every permission, metadata encryptedfn permissions(mut self, p: Permissions) -> Encryptionwhat the user password allowsfn encrypt_metadata(mut self, on: bool) -> Encryptionwhether the XMP metadata is encrypted too (true)fn with_seed(mut self, seed: [u8; 32]) -> Encryptionthe bytes the key, the salts and the file ID come from (instead of the system's random numbers): the same document and the same seed give the same file. Keep it as secret as a key, random, and fresh for each encryption (a seed used again when the passwords change keeps the old key): who knows the seed can decrypt without a password. Fixed seeds are for reproducible output such as testsfn cipher(&self) -> Cipher