1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//! # The object-encoder seam (ISO 32000-1 §7.6, decision 007 R37)
//!
//! A one-trait indirection between "the serializer has produced the
//! bytes of a string or a stream" and "those bytes go into the file".
//! In Pass 3.0 the only implementation is [`IdentityEncoder`], which
//! returns its input untouched. **This module exists entirely so that
//! Pass 5 (encryption) is a plug-in rather than a rewrite.**
//!
//! ## Why a seam now, when nothing uses it
//!
//! Decision 007 W8, verbatim: *"The crypt stage touches EVERY string
//! and EVERY stream, and incremental save of an encrypted document must
//! encrypt newly-appended objects with the existing key. Bolting that
//! onto a completed serializer is a cross-cutting rewrite."* The two
//! call sites that would have to be found and changed later are
//! [`crate::writer::serialize`]'s string arm and its stream arm. Naming
//! them now, as a trait boundary, costs one indirection and removes an
//! entire class of Pass-5 archaeology.
//!
//! ## What the seam deliberately does NOT abstract
//!
//! Three categories of bytes are **never** routed through an encoder,
//! and the type system should not make it look as though they could be:
//!
//! 1. **Verbatim re-emission of a `Provenance::File` object.** Those
//! bytes are copied from the retained source buffer without ever
//! being decomposed into strings and streams — in an encrypted
//! document they are *already* ciphertext, and re-encrypting them
//! would be catastrophic. The writer's verbatim path does not call
//! into this module at all, which is the correct structural answer.
//! 2. **Cross-reference streams.** §7.5.8.2: *"The cross-reference
//! stream shall not be encrypted and strings appearing in the
//! cross-reference stream dictionary shall not be encrypted. It
//! shall not have a `Filter` entry that specifies a `Crypt`
//! filter."* [`crate::writer::xref_out`] therefore serializes with
//! [`IdentityEncoder`] unconditionally, in every Pass, forever.
//! 3. **The two `/ID` strings when `/Encrypt` is present.** Table 15:
//! both *"shall be direct and unencrypted"* — they are an input to
//! key derivation, so encrypting them is circular.
//!
//! Each of those is a `shall`/`shall not` that Pass 5 will otherwise
//! have to rediscover; they are recorded here because here is where a
//! Pass-5 engineer will look.
use Cow;
use crateObjId;
/// Transforms a serialized string or stream payload on its way into the
/// file — the §7.6 encryption hook, in identity form for Pass 3.0.
///
/// Implementations receive the **containing indirect object's**
/// identifier because §7.6.2's key derivation is per-object: the
/// encryption key for object `N G` mixes the document key with `N` and
/// `G`. A serializer that did not thread the id through would be
/// unable to host a conforming standard security handler at all, which
/// is the whole reason this parameter exists in Pass 3.0 where it is
/// unused.
///
/// # Examples
///
/// ```
/// use pdfcer_model::object::ObjId;
/// use pdfcer_model::writer::{IdentityEncoder, ObjectEncoder};
///
/// let enc = IdentityEncoder;
/// let id = ObjId::new(4, 0);
/// assert_eq!(&*enc.encode_string(id, b"hello"), b"hello");
/// assert_eq!(&*enc.encode_stream(id, b"data"), b"data");
/// ```
/// The Pass 3.0 encoder: returns every payload unchanged.
///
/// This is not a placeholder to be deleted — it stays as the encoder
/// for unencrypted documents (the overwhelming majority) and as the
/// mandatory encoder for cross-reference streams even in encrypted ones
/// (module docs, item 2).
///
/// Deliberately **not** `#[non_exhaustive]`: a unit struct marked so
/// cannot be constructed outside its defining crate at all, and this one
/// exists precisely to be constructed by `pdfcer`, `pdfcer-render`
/// and the round-trip harness.
;
/// The AES-256 `/R` 6 encrypting encoder (`Pass 5.4`) — the seam's first
/// non-identity implementation, and the reason it exists (decision 007 W8).
///
/// Encrypts every string and stream through **Algorithm 1.A** (§7.6.3.3): a
/// fresh random 16-byte IV prefixed to AES-256-CBC ciphertext, PKCS#7 always
/// padded. At `/V` 5 the object key IS the file key (T24), so no per-object
/// derivation happens here.
///
/// # What it does NOT encrypt, and why the writer decides by OWNER id
///
/// The `ObjectEncoder` trait hands this only the bytes and the containing
/// object's id — not the dictionary — so the exemptions that depend on an
/// object's ROLE (the `/Encrypt` dict itself, cross-reference streams,
/// `/Metadata` when `/EncryptMetadata` is false, external streams, an
/// `/Identity` crypt filter) are resolved by the caller into two id sets and
/// consulted here. A signed document is refused UPSTREAM (`EditError`), so the
/// per-string `/Contents` exemption (N13) never has to be expressed at this
/// granularity in this cut.
/// Encrypts appended objects under a document's **existing** handler and file
/// key — the encoder an incremental save of an encrypted document uses.
///
/// Every cipher the read side decrypts is written back the same way
/// (Algorithm 1 per-object keys below `/V` 5, a fresh IV per AES payload).
/// Objects in `clear` — those [`crate::crypto::apply::skip_value`] exempts —
/// are written in clear, strings and stream alike, mirroring the read side.
pub