1//! OneNote 2010's password-protected sections: an Office Agile password wrapper
2//! (MS-OFFCRYPTO; SHA-1, 100,000 rounds) around an AES-128 key that encrypts every
3//! property object (CBC, an IV of its own) and payload (CBC, one IV from the key data's
4//! salt). Unknown wrappers stay opaque. CBC authenticates nothing; read-only hashes and
5//! model checks catch structural damage, not every change to content.
6
7mod crypto;
8
9use crate::{
10 Chunk, ExGuid, FileDataReference, ObjectData, Reference, RevisionIndex, Store,
11 document::Document,
12};
13use bumpalo::Bump;
14use std::{
15 cell::RefCell,
16 collections::{BTreeMap, BTreeSet},
17 fmt,
18 sync::Arc,
19};
20use zeroize::Zeroizing;
21
22type Result<T> = std::result::Result<T, Error>;
23type Format<T> = std::result::Result<T, crate::Error>;
24
25#[derive(Debug)]
26pub enum Error {
27 PasswordMismatch,
28 Unsupported,
29 Limit,
30 Invalid(crate::Error),
31}
32
33impl From<crate::Error> for Error {
34 fn from(value: crate::Error) -> Self {
35 Self::Invalid(value)
36 }
37}
38
39impl Error {
40 fn message(&self) -> &'static str {
41 match self {
42 Self::PasswordMismatch => "The password did not match the section verifier",
43 Self::Unsupported => "This protection format is not supported",
44 Self::Limit => "Opening the protected section exceeded its work limit",
45 Self::Invalid(error) => error.message,
46 }
47 }
48}
49
50impl From<Error> for crate::Error {
51 fn from(error: Error) -> Self {
52 match error {
53 Error::Invalid(error) => error,
54 other => crate::Error {
55 offset: 0,
56 message: other.message(),
57 },
58 }
59 }
60}
61
62impl fmt::Display for Error {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 match self {
65 Self::Invalid(error) => error.fmt(f),
66 other => f.write_str(other.message()),
67 }
68 }
69}
70impl std::error::Error for Error {
71 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
72 match self {
73 Self::Invalid(error) => Some(error),
74 _ => None,
75 }
76 }
77}
78
79fn invalid(message: &'static str) -> Error {
80 Error::Invalid(crate::Error { offset: 0, message })
81}
82
83#[derive(Debug, Clone, Copy)]
84pub struct Limits {
85 /// Total password iterations across distinct encryption metadata containers.
86 pub kdf_rounds: u64,
87 /// Total ciphertext/reference bytes materialized; temporary copies can double this.
88 pub decoded_bytes: usize,
89 /// Sum of object counts in distinct labeled revisions.
90 pub object_visits: usize,
91}
92
93impl Default for Limits {
94 fn default() -> Self {
95 Self {
96 kdf_rounds: 1_000_000,
97 decoded_bytes: 256 * 1024 * 1024,
98 object_visits: 1_000_000,
99 }
100 }
101}
102
103/// A protected section's labelled revisions decoded for reading as a `Document`, which
104/// cannot outlive it. Holds no password; dropping it clears its keys and decoded buffers,
105/// while strings and exports made from the `Document` are the caller's to dispose of.
106/// Edits go through `Section::unlock`.
107///
108/// ```compile_fail
109/// # use onestore::{RevisionIndex, protected::{Limits, UnlockedSection}};
110/// fn outlive<'a>(index: &'a RevisionIndex<'a>, password: &str) {
111/// let unlocked = UnlockedSection::open(index, password, Limits::default()).unwrap();
112/// let document = unlocked.document().unwrap();
113/// drop(unlocked);
114/// document.pages().unwrap();
115/// }
116/// ```
117pub struct UnlockedSection<'a> {
118 index: &'a RevisionIndex<'a>,
119 /// By object space and stored address.
120 objects: BTreeMap<(ExGuid, usize), Zeroizing<Vec<u8>>>,
121 files: BTreeMap<[u8; 16], Zeroizing<Vec<u8>>>,
122}
123
124impl<'a> UnlockedSection<'a> {
125 /// Verifies the password and every labeled revision before exposing a view.
126 /// Metadata and password input are each bounded to 64 KiB.
127 pub fn open(index: &'a RevisionIndex<'a>, password: &str, limits: Limits) -> Result<Self> {
128 Self::open_with(index, limits, |metadata, rounds| {
129 crypto::Key::open(metadata, password, rounds)
130 })
131 }
132
133 /// `open` with the key `Key::open` gave, which every space's encryption data must name.
134 pub fn unlock(index: &'a RevisionIndex<'a>, key: &Key, limits: Limits) -> Result<Self> {
135 Self::open_with(index, limits, |metadata, _| {
136 match metadata == &key.metadata[..] {
137 true => Ok(key.key.clone()),
138 false => Err(Error::PasswordMismatch),
139 }
140 })
141 }
142
143 fn open_with(
144 index: &'a RevisionIndex<'a>,
145 mut limits: Limits,
146 mut key: impl FnMut(&[u8], &mut u64) -> Result<crypto::Key>,
147 ) -> Result<Self> {
148 if index.store.header.file_type != crate::FileType::Section {
149 return Err(Error::Unsupported);
150 }
151 if !index.store.checksum_mismatches.is_empty() {
152 return Err(invalid("Protected document transaction checksum mismatch"));
153 }
154 let mut result = Self {
155 index,
156 objects: BTreeMap::new(),
157 files: BTreeMap::new(),
158 };
159 let mut keys = BTreeMap::new();
160 let mut file_keys = BTreeMap::new();
161 let hashes = hashes(index.store)?;
162 for (space_id, space) in &index.spaces {
163 let mut metadata = None;
164 for revision in space.revisions.values() {
165 if !revision.encrypted {
166 return Err(Error::Unsupported);
167 }
168 let Some(node) = revision.nodes.first().filter(|node| node.id == 0x7c) else {
169 continue;
170 };
171 let Some(Reference::Data(chunk)) = node.reference else {
172 return Err(invalid("Missing encryption key reference"));
173 };
174 let data = index.store.encryption_key(chunk)?;
175 if metadata.replace(data).is_some_and(|old| old != data) {
176 return Err(invalid("Object space changes its encryption metadata"));
177 }
178 }
179 let metadata =
180 metadata.ok_or_else(|| invalid("Protected object space has no revision"))?;
181 if !keys.contains_key(metadata) {
182 keys.insert(metadata, key(metadata, &mut limits.kdf_rounds)?);
183 }
184 let key = &keys[metadata];
185 for rid in space.labels.values().copied().collect::<BTreeSet<_>>() {
186 let revision = index.resolve(*space_id, rid)?;
187 limits.object_visits = limits
188 .object_visits
189 .checked_sub(revision.objects.len())
190 .ok_or(Error::Limit)?;
191 for object in revision.objects.values() {
192 match object.data {
193 ObjectData::Encrypted(bytes) => {
194 let identity = (*space_id, bytes.as_ptr().addr());
195 let expected = hashes.get(&identity.1);
196 if object.jcid & 0x100000 != 0 && expected.is_none() {
197 return Err(invalid("Read-only encrypted object has no hash"));
198 }
199 if result.objects.contains_key(&identity) {
200 continue;
201 }
202 limits.decoded_bytes = limits
203 .decoded_bytes
204 .checked_sub(bytes.len())
205 .ok_or(Error::Limit)?;
206 let decoded = key.property(bytes)?;
207 if expected.is_some_and(|expected| digest(&decoded) != *expected) {
208 return Err(invalid("Decrypted read-only object hash mismatch"));
209 }
210 result.objects.insert(identity, decoded);
211 }
212 ObjectData::File { .. } => {
213 if let Some(FileDataReference::Internal(guid)) =
214 object.file_reference()?
215 {
216 if file_keys
217 .insert(guid, metadata)
218 .is_some_and(|old| old != metadata)
219 {
220 return Err(invalid(
221 "File payload uses inconsistent encryption metadata",
222 ));
223 }
224 if result.files.contains_key(&guid) {
225 continue;
226 }
227 let bytes = index.store.file_data(guid)?;
228 limits.decoded_bytes = limits
229 .decoded_bytes
230 .checked_sub(bytes.len())
231 .ok_or(Error::Limit)?;
232 result.files.insert(guid, key.file(bytes)?);
233 }
234 }
235 ObjectData::Properties(_) => {
236 return Err(invalid("Protected revision contains clear properties"));
237 }
238 }
239 }
240 }
241 }
242 for (space, info) in &index.spaces {
243 for rid in info.labels.values().copied().collect::<BTreeSet<_>>() {
244 result.resolve(*space, rid)?.reachable()?;
245 }
246 }
247 result.document()?.pages()?;
248 Ok(result)
249 }
250
251 fn resolve(
252 &self,
253 space: ExGuid,
254 rid: ExGuid,
255 ) -> std::result::Result<crate::ResolvedRevision<'_>, crate::Error> {
256 let mut revision = self.index.resolve(space, rid)?;
257 for object in revision.objects.values_mut() {
258 if let ObjectData::Encrypted(bytes) = object.data {
259 let decoded =
260 self.objects
261 .get(&(space, bytes.as_ptr().addr()))
262 .ok_or(crate::Error {
263 offset: 0,
264 message: "Protected object was not decoded",
265 })?;
266 object.data = ObjectData::Properties(decoded);
267 }
268 }
269 Ok(revision)
270 }
271
272 pub fn document(&self) -> std::result::Result<Document<'_>, crate::Error> {
273 Document::parse_with(
274 self.index,
275 |space, rid| self.resolve(space, rid),
276 |id| {
277 self.files
278 .get(&id)
279 .map(|bytes| bytes.as_slice())
280 .ok_or(crate::Error {
281 offset: 0,
282 message: "Protected file payload was not decoded",
283 })
284 },
285 )
286 }
287}
288
289/// A protected section's key, opened with its password or made for a new one. Holds no
290/// password; the key is cleared when its last clone drops.
291#[derive(Clone)]
292pub struct Key {
293 key: crypto::Key,
294 /// The encryption data (MS-ONESTORE 2.5.19) that opens the key.
295 metadata: Arc<[u8]>,
296}
297
298impl fmt::Debug for Key {
299 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
300 f.write_str("Key")
301 }
302}
303
304impl Key {
305 /// Opens the key of the protected section `image` with `password`.
306 pub fn open(image: &[u8], password: &str) -> Result<Self> {
307 let store = Store::parse(image)?;
308 let metadata = metadata(&store)?.ok_or(Error::Unsupported)?;
309 Ok(Self {
310 key: crypto::Key::open(metadata, password, &mut Limits::default().kdf_rounds)?,
311 metadata: Arc::from(metadata),
312 })
313 }
314
315 /// A fresh key for `password`, with OneNote 2010's encryption data.
316 pub fn new(password: &str) -> Result<Self> {
317 let (key, metadata) = crypto::Key::create(password)?;
318 Ok(Self {
319 key,
320 metadata: Arc::from(metadata),
321 })
322 }
323
324 /// The AES key the section's content is encrypted under, for keys derived from it.
325 pub fn secret(&self) -> &[u8; 16] {
326 self.key.value()
327 }
328
329 pub(crate) fn metadata(&self) -> &[u8] {
330 &self.metadata
331 }
332}
333
334/// The section `image` written anew, under `new` or in the clear, as OneNote 2010 rewrites
335/// a section whose password it sets, changes or removes, or that it moves or copies into
336/// another notebook: the file and its object spaces
337/// take new identities, each space keeps its labelled revisions, every one a checkpoint,
338/// and the payloads come along. A protected `image` opens under `key`.
339pub fn rekey(image: &[u8], key: Option<&Key>, new: Option<&Key>) -> Result<Vec<u8>> {
340 let store = Store::parse(image)?;
341 if !store.checksum_mismatches.is_empty() {
342 return Err(invalid(
343 "Cannot write a file with transaction checksum damage",
344 ));
345 }
346 let index = RevisionIndex::parse(&store)?;
347 let opened = match (key, metadata(&store)?) {
348 (Some(key), Some(_)) => Some(Opened::new(&store, key)?),
349 (None, None) => None,
350 (None, Some(_)) => return Err(Error::PasswordMismatch),
351 (Some(_), None) => return Err(invalid("The section is not protected")),
352 };
353 let arena = Bump::new();
354 let mut fresh = BTreeMap::new();
355 for space in index.spaces.keys() {
356 if let std::collections::btree_map::Entry::Vacant(entry) = fresh.entry(space.guid) {
357 entry.insert(crate::write::fresh_guid()?);
358 }
359 }
360 // A page's metadata is named for its object space, so it takes the new space's name.
361 let derived: Vec<([u8; 16], [u8; 16])> = (fresh.iter())
362 .map(|(old, new)| {
363 let named = |guid: &[u8; 16]| crate::pages::metadata_id(ExGuid { guid: *guid, n: 1 });
364 (named(old).guid, named(new).guid)
365 })
366 .collect();
367 fresh.extend(derived);
368 // OneNote gives the payloads new identities too.
369 let mut files = BTreeMap::new();
370 for guid in crate::write::declared_payloads(&store) {
371 files.insert(guid, crate::write::fresh_guid()?);
372 }
373 let rename = |id: ExGuid| ExGuid {
374 guid: fresh.get(&id.guid).copied().unwrap_or(id.guid),
375 n: id.n,
376 };
377 let mut spaces = Vec::new();
378 for (space, info) in &index.spaces {
379 let mut labelled: Vec<(ExGuid, Vec<(ExGuid, u32)>)> = Vec::new();
380 // The current revision first, as OneNote writes it.
381 let mut labels: Vec<_> = info.labels.iter().collect();
382 labels.sort_by_key(|(label, _)| **label != (ExGuid::default(), 1));
383 for (label, rid) in labels {
384 match labelled.iter_mut().find(|(known, _)| known == rid) {
385 Some((_, names)) => names.push(*label),
386 None => labelled.push((*rid, vec![*label])),
387 }
388 }
389 let mut revisions = Vec::new();
390 for (rid, labels) in labelled {
391 let mut resolved = index.resolve(*space, rid)?;
392 if let Some(opened) = &opened {
393 for object in resolved.objects.values_mut() {
394 if let ObjectData::Encrypted(stored) = object.data {
395 object.data = ObjectData::Properties(opened.property(&arena, stored)?);
396 }
397 }
398 }
399 let mut objects = BTreeMap::new();
400 for id in resolved.reachable()? {
401 let mut object = resolved.objects[&id].clone();
402 if let (Some(FileDataReference::Internal(guid)), ObjectData::File { extension, .. }) =
403 (object.file_reference()?, object.data)
404 && let Some(renamed) = files.get(&guid)
405 {
406 let reference: Vec<u8> = crate::op::content::payload_reference(*renamed)
407 .encode_utf16()
408 .flat_map(u16::to_le_bytes)
409 .collect();
410 object.data = ObjectData::File {
411 reference: arena.alloc_slice_copy(&reference),
412 extension,
413 };
414 }
415 object.global_ids = Arc::new(
416 object
417 .global_ids
418 .iter()
419 .map(|(entry, guid)| (*entry, fresh.get(guid).copied().unwrap_or(*guid)))
420 .collect(),
421 );
422 objects.insert(rename(id), object);
423 }
424 let roots = resolved
425 .roots
426 .iter()
427 .map(|(role, id)| (*role, rename(*id)))
428 .collect();
429 revisions.push(crate::write::Labelled {
430 labels,
431 revision: crate::ResolvedRevision { roots, objects },
432 });
433 }
434 spaces.push((rename(*space), revisions));
435 }
436 let mut payloads = Vec::new();
437 for (guid, renamed) in &files {
438 let stored = store.file_data(*guid)?;
439 let clear = match &opened {
440 Some(opened) => opened.file(&arena, stored)?,
441 None => stored,
442 };
443 payloads.push((*renamed, clear));
444 }
445 let placement = image[128..148].try_into().unwrap();
446 let skeleton = crate::create::skeleton(rename(index.root), placement)?;
447 let written = crate::write::rewrite(skeleton, &spaces, &payloads, new)?;
448 let arena = crate::Arena::default();
449 match new {
450 Some(new) => drop(crate::Section::unlock(&arena, written.clone(), new)?),
451 None => drop(crate::Section::open(&arena, written.clone())?),
452 }
453 Ok(written)
454}
455
456/// The encryption data every revision of `store` names; none in a section without any.
457fn metadata<'a>(store: &Store<'a>) -> Result<Option<&'a [u8]>> {
458 let mut found = None;
459 for node in store.lists.values().flat_map(|list| &list.nodes) {
460 if node.id != 0x7c {
461 continue;
462 }
463 let Some(Reference::Data(chunk)) = node.reference else {
464 return Err(invalid("Missing encryption key reference"));
465 };
466 let data = store.encryption_key(chunk)?;
467 if found.replace(data).is_some_and(|old| old != data) {
468 return Err(Error::Unsupported);
469 }
470 }
471 Ok(found)
472}
473
474/// The hashes read-only declarations give their stored bytes, by address.
475fn hashes(store: &Store<'_>) -> Result<BTreeMap<usize, [u8; 16]>> {
476 let mut hashes = BTreeMap::new();
477 for node in store.lists.values().flat_map(|list| &list.nodes) {
478 if !matches!(node.id, 0xc4 | 0xc5) {
479 continue;
480 }
481 let Some(Reference::Data(chunk)) = node.reference else {
482 return Err(invalid("Read-only object has no data reference"));
483 };
484 let bytes = store.chunk_data(chunk)?;
485 let expected: [u8; 16] = node
486 .payload
487 .last_chunk::<16>()
488 .copied()
489 .ok_or_else(|| invalid("Missing read-only object hash"))?;
490 if hashes
491 .insert(bytes.as_ptr().addr(), expected)
492 .is_some_and(|old| old != expected)
493 {
494 return Err(invalid("Inconsistent read-only hashes for one payload"));
495 }
496 }
497 Ok(hashes)
498}
499
500/// A protected read-only declaration's hash: MD5 of the clear bytes zero-padded to 8.
501pub(crate) fn digest(clear: &[u8]) -> [u8; 16] {
502 let mut hash = md5::Context::new();
503 hash.consume(clear);
504 hash.consume(&[0; 7][..(8 - clear.len() % 8) % 8]);
505 hash.finalize().0
506}
507
508/// A `Section`'s key, with what it decoded under it: clear bytes by the address of the
509/// stored bytes they decode, and stored bytes by the address of their clear bytes.
510pub(crate) struct Opened<'a> {
511 key: Key,
512 /// The encryption data each revision names.
513 chunk: Chunk,
514 /// Read-only declarations' hashes by the address of their stored bytes.
515 hashes: BTreeMap<usize, [u8; 16]>,
516 clear: RefCell<BTreeMap<usize, &'a [u8]>>,
517 stored: RefCell<BTreeMap<usize, &'a [u8]>>,
518}
519
520impl<'a> Opened<'a> {
521 /// `key` for the section `store` holds, every object space of which it must encrypt.
522 pub(crate) fn new(store: &Store<'_>, key: &Key) -> Result<Self> {
523 if metadata(store)?.ok_or(Error::Unsupported)? != &key.metadata[..] {
524 return Err(Error::PasswordMismatch);
525 }
526 let index = RevisionIndex::parse(store)?;
527 if index
528 .spaces
529 .values()
530 .flat_map(|space| space.revisions.values())
531 .any(|revision| !revision.encrypted)
532 {
533 return Err(invalid(
534 "A protected section stores a revision in the clear",
535 ));
536 }
537 let chunk = store
538 .lists
539 .values()
540 .flat_map(|list| &list.nodes)
541 .find_map(|node| match node.reference {
542 Some(Reference::Data(chunk)) if node.id == 0x7c => Some(chunk),
543 _ => None,
544 })
545 .ok_or(Error::Unsupported)?;
546 Ok(Self {
547 key: key.clone(),
548 chunk,
549 hashes: hashes(store)?,
550 clear: RefCell::default(),
551 stored: RefCell::default(),
552 })
553 }
554
555 /// `key` for writing a new image whose encryption data lies at `chunk`.
556 pub(crate) fn sealing(key: &Key, chunk: Chunk) -> Self {
557 Self {
558 key: key.clone(),
559 chunk,
560 hashes: BTreeMap::new(),
561 clear: RefCell::default(),
562 stored: RefCell::default(),
563 }
564 }
565
566 /// The clear bytes of a stored object, decoded once into `arena`.
567 pub(crate) fn property(&self, arena: &'a Bump, stored: &'a [u8]) -> Format<&'a [u8]> {
568 if let Some(clear) = self.clear.borrow().get(&stored.as_ptr().addr()) {
569 return Ok(clear);
570 }
571 let decoded = self.key.key.property(stored).map_err(crate::Error::from)?;
572 if let Some(expected) = self.hashes.get(&stored.as_ptr().addr())
573 && digest(&decoded) != *expected
574 {
575 return Err(crate::Error {
576 offset: 0,
577 message: "Decrypted read-only object hash mismatch",
578 });
579 }
580 let clear: &'a [u8] = arena.alloc_slice_copy(&decoded);
581 self.sealed(clear, stored);
582 Ok(clear)
583 }
584
585 /// The clear bytes of a stored payload, in `arena`.
586 pub(crate) fn file(&self, arena: &'a Bump, stored: &[u8]) -> Format<&'a [u8]> {
587 Ok(arena.alloc_slice_copy(&self.key.key.file(stored).map_err(crate::Error::from)?))
588 }
589
590 pub(crate) fn key(&self) -> &Key {
591 &self.key
592 }
593
594 /// Records that `clear` is stored as `stored`.
595 pub(crate) fn sealed(&self, clear: &'a [u8], stored: &'a [u8]) {
596 self.clear
597 .borrow_mut()
598 .insert(stored.as_ptr().addr(), clear);
599 self.stored
600 .borrow_mut()
601 .insert(clear.as_ptr().addr(), stored);
602 }
603}
604
605impl crate::write::Protection for Opened<'_> {
606 fn stored(&self, clear: &[u8]) -> Option<&[u8]> {
607 self.stored.borrow().get(&clear.as_ptr().addr()).copied()
608 }
609
610 fn seal_property(&self, clear: &[u8]) -> Format<Vec<u8>> {
611 seal_property(&self.key, clear)
612 }
613
614 fn seal_file(&self, clear: &[u8]) -> Format<Vec<u8>> {
615 self.key.key.seal_file(clear).map_err(crate::Error::from)
616 }
617
618 fn open_property(&self, stored: &[u8]) -> Format<Zeroizing<Vec<u8>>> {
619 self.key.key.property(stored).map_err(crate::Error::from)
620 }
621
622 fn open_file(&self, stored: &[u8]) -> Format<Zeroizing<Vec<u8>>> {
623 self.key.key.file(stored).map_err(crate::Error::from)
624 }
625
626 fn metadata(&self) -> Chunk {
627 self.chunk
628 }
629}
630
631/// A property object's stored form under `key`, with a fresh IV.
632fn seal_property(key: &Key, clear: &[u8]) -> Format<Vec<u8>> {
633 let mut iv = [0; 16];
634 getrandom::fill(&mut iv).map_err(|_| crate::Error {
635 offset: 0,
636 message: "System random source failed",
637 })?;
638 key.key.seal_property(clear, iv).map_err(crate::Error::from)
639}