pub struct EncryptionConfig {
pub version: i64,
pub revision: u8,
pub key_len: usize,
pub o: Vec<u8>,
pub u: Vec<u8>,
pub p: u32,
pub encrypt_metadata: bool,
pub stream_cipher: Cipher,
pub string_cipher: Cipher,
pub aes256: Option<Aes256Keys>,
}Expand description
A parsed, supported /Encrypt dictionary.
Constructing one is the whole of “can pdfcer open this?”; it holds no key
and proves no password. Self::authenticate is the next step.
Fields§
§version: i64/V — the algorithm family.
revision: u8/R — the handler revision, which selects between Algorithms 4 and 5,
decides whether Algorithm 2’s 50-round loop runs, and decides whether
bits 9–12 of /P mean anything.
key_len: usizeFile encryption key length in bytes (n in the algorithms).
5 at /R 2; /Length ÷ 8 above that; a fixed 32 at /R 5, where
/AESV3 mandates a 256-bit key and /Length is not consulted.
o: Vec<u8>/O. 32 bytes at /R ≤ 4 and 48 at /R 5 (Table 3.19).
The length change is not flagged by anything in the file: a 32-byte
/O means /R ≤ 4 and a 48-byte one means /R ≥ 5, and length is
the only discriminator the format provides. Self::parse resolves
it once, against /R, and refuses a document whose lengths and
revision disagree — the standard states no recovery for that case
(N6).
Opaque either way; never re-derived on save (R33, T15).
u: Vec<u8>/U. 32 bytes at /R ≤ 4 (only the first 16 are ever compared, T15)
and 48 at /R 5, where all 48 are compared — the tail is two salts,
not padding. See Self::o.
p: u32/P as an unsigned bit field (T10, A5).
encrypt_metadata: bool/EncryptMetadata. Default true; false adds four 0xFF bytes to
Algorithm 2’s hash at /R ≥ 4 (T11).
stream_cipher: CipherThe cipher for stream data (/StmF’s filter).
string_cipher: CipherThe cipher for strings (/StrF’s filter). Independent of the stream
cipher — a document may leave one in the clear via /Identity.
aes256: Option<Aes256Keys>The three key strings ExtensionLevel 3 adds at /R 5, or None at
/R ≤ 4 where they do not exist.
Implementations§
Source§impl EncryptionConfig
impl EncryptionConfig
Sourcepub fn to_encrypt_dict(&self) -> Option<Object>
pub fn to_encrypt_dict(&self) -> Option<Object>
Serialise this configuration back into a /Encrypt dictionary value
(Pass 5.4, the write-side inverse of EncryptionConfig::parse).
Emits exactly the entries parse reads and
nothing decorative: /Filter /Standard, /V, /R, /Length (bits),
signed /P, the byte-string /O//U//OE//UE//Perms, a /CF
with a single /StdCF naming /CFM /AESV3, and /StmF//StrF both
/StdCF. /EncryptMetadata is written whenever it is false (its
default is true, §7.6.2 Table 21), so a true document stays
minimal.
None for a config with no Aes256Keys (anything parsed at
/R <= 4); BuiltEncryption::encrypt_dict is the infallible form
for a config the encryptor built.
Sourcepub fn parse(
dict: &Dict,
resolve: &dyn Fn(ObjId) -> Option<Object>,
) -> Result<Self, EncryptionUnsupported>
pub fn parse( dict: &Dict, resolve: &dyn Fn(ObjId) -> Option<Object>, ) -> Result<Self, EncryptionUnsupported>
Parse an /Encrypt dictionary, refusing anything not implemented.
resolve looks up an indirect reference, because /O, /U and the
crypt-filter dictionaries may be indirect even though the
/Encrypt dictionary itself must be direct in the trailer. It returns
None for a dangling reference, matching §7.3.10’s “shall not be
considered an error”.
§Errors
EncryptionUnsupported naming the specific configuration.
Slicing is in bounds by construction: /O and /U are sliced to 32
bytes only inside a s.len() >= 32 guard, and the shorter case is a
named refusal rather than a truncation.
Sourcepub fn permissions(&self) -> Permissions
pub fn permissions(&self) -> Permissions
The permissions the document’s author declared.
Sourcepub fn password_may_need_normalisation(&self, password: &[u8]) -> bool
pub fn password_may_need_normalisation(&self, password: &[u8]) -> bool
Whether a failed authentication with password is ambiguous because
pdfcer could not apply the password preprocessing the revision asks for.
true only for a /R 5 document whose supplied password contains a
byte RFC 4013’s SASLprep step could have changed — see
PreparedPassword’s docs for why pdfcer attempts such a password
rather than refusing it, and why the disclosure belongs on the failure
path rather than on the attempt.
false for every /R ≤ 4 document. Password encoding below /R 5 is
PDFDocEncoding rather than SASLprep’d UTF-8 (T8), which is a
separate unimplemented question with a separate answer, and reporting
this one there would be wrong rather than merely imprecise.
Sourcepub fn check_perms(&self, key: &FileKey) -> PermsCheck
pub fn check_perms(&self, key: &FileKey) -> PermsCheck
Algorithm 3.13 — validate /Perms against this dictionary’s /P and
/EncryptMetadata.
key must be the FileKey this configuration authenticated, because
/Perms is encrypted under the file encryption key itself; that is
exactly what makes it the only value in clause 7.6 an attacker cannot
edit without the password.
Returns PermsCheck::NotApplicable for every /R ≤ 4 document —
/Perms was introduced by ExtensionLevel 3 and simply does not exist
below /R 5. That is not a failed check and must not be rendered as
one.
The result is a report. pdfcer never prefers the encrypted copy over
the dictionary’s, never refuses a document over a mismatch, and never
treats one as damage. See PermsCheck’s docs for the full argument
(T27).
Sourcepub fn authenticate(
&self,
password: Option<&[u8]>,
id0: &[u8],
) -> Option<(FileKey, AuthKind)>
pub fn authenticate( &self, password: Option<&[u8]>, id0: &[u8], ) -> Option<(FileKey, AuthKind)>
Authenticate and derive the file encryption key.
password is None to mean “try the default (empty) user password”,
which §7.6.3.1 requires a reader to do first and silently. That is
deliberately distinct from Some(b""), which is the operator typing an
empty box — the two produce the same key here, but the returned
AuthKind differs, and the shells use it to decide whether they ever
showed a prompt.
Order follows the clause: empty user password, then the supplied
password as a user password, then as an owner password. Trying owner
first would work, but would report AuthKind::Owner for a document
whose two passwords happen to be equal, overstating the access granted.