Skip to main content

pdfcer_model/document/
encryption_refusal.rs

1//! Why an encrypted document refuses an edit, for a shell to word in its own terms.
2
3use super::DocumentEncryption;
4use crate::crypto::PermissionBit;
5
6/// The cause of an editing verb's encryption refusal (every verb's
7/// `DocumentEncrypted` / `Encrypted` error variant).
8///
9/// Each cause has one remedy: [`Rc4NotAllowed`](Self::Rc4NotAllowed) lifts
10/// when the session allows edits under RC4
11/// ([`Document::set_rc4_append`](crate::document::Document::set_rc4_append)
12/// with [`Rc4Append::Preserve`](crate::writer::Rc4Append::Preserve));
13/// [`PermissionDenied`](Self::PermissionDenied) lifts only when the document
14/// is opened with its owner password.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16#[non_exhaustive]
17pub enum EncryptedRefusal {
18    /// The password grants the edit, but the document uses RC4 and the
19    /// session has not allowed saving under it (decision 190).
20    Rc4NotAllowed,
21    /// The password that opened the document does not grant the permission
22    /// the edit needs (ISO 32000-2 Table 22).
23    /// [`DocumentEncryption::edit_refusal`] reports it in preference to
24    /// [`Rc4NotAllowed`](Self::Rc4NotAllowed) when both apply, because
25    /// allowing RC4 alone would not let the edit through.
26    PermissionDenied,
27}
28
29impl DocumentEncryption {
30    /// Why an edit governed by any of `bits` would be refused, or `None` when
31    /// it is allowed. This is the test every editing verb applies, so a shell
32    /// can ask before calling one.
33    ///
34    /// Editing verbs and the bits they need: annotation verbs need
35    /// [`Annotate`](PermissionBit::Annotate); fills and form-data import need
36    /// [`FillForms`](PermissionBit::FillForms) or `Annotate`; page insert,
37    /// delete, rotate, reorder, labels, merge and outline items need
38    /// [`Assemble`](PermissionBit::Assemble); everything else needs
39    /// [`ModifyContents`](PermissionBit::ModifyContents).
40    #[must_use]
41    pub fn edit_refusal(&self, bits: &[PermissionBit]) -> Option<EncryptedRefusal> {
42        if !bits.iter().any(|&b| self.grants(b)) {
43            Some(EncryptedRefusal::PermissionDenied)
44        } else if !self.appendable() {
45            Some(EncryptedRefusal::Rc4NotAllowed)
46        } else {
47            None
48        }
49    }
50
51    /// The cause of an encryption refusal a verb has already returned, when
52    /// the shell does not know which permission that verb needs.
53    ///
54    /// A refusal under an RC4 policy that does not allow saving is reported as
55    /// [`Rc4NotAllowed`](EncryptedRefusal::Rc4NotAllowed) — allowing RC4 is
56    /// then necessary for every edit, though a permission refusal may remain
57    /// after it. Otherwise the refusal can only have come from the password,
58    /// so it is [`PermissionDenied`](EncryptedRefusal::PermissionDenied).
59    /// Call it only after a refusal: it does not test whether an edit is
60    /// allowed (use [`edit_refusal`](Self::edit_refusal) for that).
61    #[must_use]
62    pub fn refusal_cause(&self) -> EncryptedRefusal {
63        if self.appendable() {
64            EncryptedRefusal::PermissionDenied
65        } else {
66            EncryptedRefusal::Rc4NotAllowed
67        }
68    }
69}