Skip to main content

EncryptionConfig

Struct EncryptionConfig 

Source
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: usize

File 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: Cipher

The cipher for stream data (/StmF’s filter).

§string_cipher: Cipher

The 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

Source

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.

Source

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.

Source

pub fn permissions(&self) -> Permissions

The permissions the document’s author declared.

Source

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.

Source

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).

Source

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.

Trait Implementations§

Source§

impl Clone for EncryptionConfig

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EncryptionConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.