odf-crypto 0.1.1

LibreOffice-faithful encryption for ODF packages (.odt, .ods, .odp): detect it, decrypt it, write it
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! S6 (#92): `reencrypt_to`, and the plan's ยง4 refusals.

use super::*;

use std::io::Cursor;

use zip::write::SimpleFileOptions;

use crate::classify::classify;
use crate::decrypt::decrypt;
use crate::decrypt_stream::decrypt_to;
use crate::test_support::{
    append_stored_member, load_golden, load_ladder_fixture, unlatched_package, NONASCII_PASSWORD,
    PASSWORD,
};

const NEW: &str = "a new password";

fn cheap() -> Argon2Params {
    Argon2Params::new(1, 8, 1).unwrap()
}

fn rekey(src: &[u8], old: &str) -> (Result<DecryptOutcome, ReencryptError>, Vec<u8>) {
    let mut out = Cursor::new(Vec::new());
    let r = reencrypt_to(
        Cursor::new(src),
        &mut out,
        old,
        NEW,
        DecryptLimits::default(),
        cheap(),
    );
    (r, out.into_inner())
}

fn artifact(name: &str) -> Vec<u8> {
    std::fs::read(
        crate::test_support::goldens_dir()
            .with_file_name("artifacts")
            .join(name),
    )
    .unwrap()
}

/// A wholesome source is re-keyed without recompressing: the new package
/// decrypts under the new password to **exactly** the inner package the old
/// one held, and no longer opens under the old password.
#[test]
fn a_wholesome_source_rekeys_byte_for_byte() {
    for (name, src, old) in [
        (
            "lo-wholesome-gcm-argon2",
            load_golden("lo-wholesome-gcm-argon2.odt"),
            PASSWORD,
        ),
        (
            "wholesome-nonascii-password",
            artifact("wholesome-nonascii-password.odt"),
            NONASCII_PASSWORD,
        ),
        (
            "argon2-floor-1-8-1",
            artifact("argon2-floor-1-8-1.odt"),
            PASSWORD,
        ),
    ] {
        let (r, out) = rekey(&src, old);
        assert_eq!(r.unwrap().integrity, IntegrityOutcome::Verified, "{name}");
        assert_eq!(classify(&out).unwrap().mode, Mode::Wholesome, "{name}");
        assert!(
            decrypt(&out, NEW).unwrap() == decrypt(&src, old).unwrap(),
            "{name}"
        );
        assert!(
            matches!(decrypt(&out, old), Err(DecryptError::WrongPassword)),
            "{name}"
        );
    }
}

/// A per-entry source becomes a wholesome package whose inner package is
/// **exactly** what `decrypt_to` writes for it -- the same rebuild, now
/// encrypted -- and the outcome reports what the source was checked with.
#[test]
fn a_per_entry_source_becomes_wholesome() {
    for (name, src, old) in [
        (
            "lo-legacy-aes-cbc",
            load_golden("lo-legacy-aes-cbc.odt"),
            PASSWORD,
        ),
        (
            "aoo-blowfish-pbkdf2",
            load_golden("aoo-blowfish-pbkdf2.odt"),
            PASSWORD,
        ),
        (
            "ladder-staroffice-utf8",
            load_ladder_fixture("ladder-staroffice-utf8.odt"),
            NONASCII_PASSWORD,
        ),
    ] {
        let (r, out) = rekey(&src, old);
        assert_eq!(
            r.unwrap().integrity,
            IntegrityOutcome::PasswordCheckOnly,
            "{name}"
        );
        assert_eq!(classify(&out).unwrap().mode, Mode::Wholesome, "{name}");
        let mut streamed = Vec::new();
        decrypt_to(Cursor::new(&src), &mut streamed, old).unwrap();
        assert!(decrypt(&out, NEW).unwrap() == streamed, "{name}");
        let mut again = Vec::new();
        decrypt_to(Cursor::new(&out), &mut again, NEW).unwrap();
        assert!(again == streamed, "{name}: through decrypt_to");
    }
}

#[test]
fn passwords_are_checked_before_anything_else() {
    let src = load_golden("lo-wholesome-gcm-argon2.odt");
    let (r, out) = rekey(&src, "wrong");
    assert!(
        matches!(r, Err(ReencryptError::Decrypt(DecryptError::WrongPassword))),
        "{r:?}"
    );
    assert!(
        out.is_empty(),
        "a wrong old password wrote {} bytes",
        out.len()
    );

    let mut out = Cursor::new(Vec::new());
    let r = reencrypt_to(
        Cursor::new(&src),
        &mut out,
        "",
        NEW,
        DecryptLimits::default(),
        cheap(),
    );
    assert!(
        matches!(r, Err(ReencryptError::Decrypt(DecryptError::EmptyPassword))),
        "{r:?}"
    );
    let r = reencrypt_to(
        Cursor::new(&src),
        &mut out,
        PASSWORD,
        "",
        DecryptLimits::default(),
        cheap(),
    );
    assert!(
        matches!(r, Err(ReencryptError::Encrypt(EncryptError::EmptyPassword))),
        "{r:?}"
    );
}

/// `lo-legacy-aes-cbc.odt` plus a member the manifest lists with no
/// `encryption-data`: plaintext slipped into an old encrypted file.
fn with_plain_listed_row() -> Vec<u8> {
    let golden = load_golden("lo-legacy-aes-cbc.odt");
    let mut src = ZipArchive::new(Cursor::new(&golden)).unwrap();
    let mut out = ZipWriter::new(Cursor::new(Vec::new()));
    for i in 0..src.len() {
        let name = src.by_index(i).unwrap().name().unwrap().to_string();
        if name == MANIFEST_PATH {
            let mut xml = String::new();
            src.by_index(i).unwrap().read_to_string(&mut xml).unwrap();
            let xml = xml.replace(
                "</manifest:manifest>",
                r#" <manifest:file-entry manifest:full-path="extra.xml" manifest:media-type="text/xml"/>
</manifest:manifest>"#,
            );
            let opt = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
            out.start_file(MANIFEST_PATH, opt).unwrap();
            out.write_all(xml.as_bytes()).unwrap();
        } else {
            out.raw_copy_file(src.by_index_raw(i).unwrap()).unwrap();
        }
    }
    let opt = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
    out.start_file("extra.xml", opt).unwrap();
    out.write_all(b"<slipped-in/>").unwrap();
    out.finish().unwrap().into_inner()
}

/// An ODF 1.1 per-entry package with a member its manifest does not list. Not
/// fatal to LibreOffice below 1.2, which is why the refusal is ours.
fn with_unlisted_member() -> Vec<u8> {
    append_stored_member(
        &load_golden("aoo-blowfish-pbkdf2.odt"),
        "unlisted.xml",
        b"<x/>",
    )
}

/// Refusal (a): a plain listed row.
#[test]
fn a_plain_listed_row_is_refused() {
    let pkg = with_plain_listed_row();
    assert!(classify(&pkg).unwrap().has_non_encrypted_entries);
    let (r, out) = rekey(&pkg, PASSWORD);
    assert!(
        matches!(r, Err(ReencryptError::PartiallyEncrypted)),
        "{r:?}"
    );
    assert!(out.is_empty(), "refused before writing");
}

/// Refusal (b): an unlisted member, at an ODF version where LibreOffice does
/// not refuse it.
#[test]
fn an_unlisted_member_is_refused_below_odf_12() {
    let pkg = with_unlisted_member();
    let class = classify(&pkg).unwrap();
    assert!(
        class.has_unexpected_streams && !class.odf12_fatal,
        "{class:?}"
    );
    let (r, out) = rekey(&pkg, PASSWORD);
    assert!(matches!(r, Err(ReencryptError::UnexpectedStreams)), "{r:?}");
    assert!(out.is_empty());
}

/// `lo-legacy-aes-cbc.odt` with `content.xml` and its manifest row both
/// removed: encrypted rows remain, none of them latches the package, and
/// there is no plain row and no unlisted member -- so refusal (c) alone
/// catches it. `unlatched_package()` also carries a plain row, which (a)
/// would catch first.
fn without_content() -> Vec<u8> {
    let golden = load_golden("lo-legacy-aes-cbc.odt");
    let mut src = ZipArchive::new(Cursor::new(&golden)).unwrap();
    let mut out = ZipWriter::new(Cursor::new(Vec::new()));
    for i in 0..src.len() {
        let name = src.by_index(i).unwrap().name().unwrap().to_string();
        if name == "content.xml" {
            continue;
        }
        if name == MANIFEST_PATH {
            let mut xml = String::new();
            src.by_index(i).unwrap().read_to_string(&mut xml).unwrap();
            let start = xml
                .find(r#"<manifest:file-entry manifest:full-path="content.xml""#)
                .unwrap();
            let end = start
                + xml[start..].find("</manifest:file-entry>").unwrap()
                + "</manifest:file-entry>".len();
            let xml = format!("{}{}", &xml[..start], &xml[end..]);
            let opt = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
            out.start_file(MANIFEST_PATH, opt).unwrap();
            out.write_all(xml.as_bytes()).unwrap();
        } else {
            out.raw_copy_file(src.by_index_raw(i).unwrap()).unwrap();
        }
    }
    out.finish().unwrap().into_inner()
}

/// Refusal (c): the unlatched shape -- encrypted rows, but none on
/// `content.xml`, so LibreOffice opens it without asking. Both fixtures: the
/// one only (c) can catch, and the one built for rc.9's decision.
#[test]
fn the_unlatched_shape_is_refused() {
    let only_c = without_content();
    let class = classify(&only_c).unwrap();
    assert!(
        !class.package_encrypted && !class.encrypted_entries.is_empty(),
        "{class:?}"
    );
    assert!(
        !class.has_non_encrypted_entries && !class.has_unexpected_streams,
        "{class:?}"
    );
    for pkg in [only_c, unlatched_package()] {
        let (r, _) = rekey(&pkg, PASSWORD);
        assert!(
            matches!(r, Err(ReencryptError::PartiallyEncrypted)),
            "{r:?}"
        );
    }
}

/// The refusals are `reencrypt_to`'s policy, not fidelity: every refused
/// package still decrypts, as LibreOffice still opens it.
#[test]
fn every_refused_package_still_decrypts() {
    for (name, pkg) in [
        ("plain listed row", with_plain_listed_row()),
        ("unlisted member", with_unlisted_member()),
        ("unlatched", unlatched_package()),
        ("no content.xml", without_content()),
    ] {
        let mut out = Vec::new();
        decrypt_to(Cursor::new(&pkg), &mut out, PASSWORD)
            .unwrap_or_else(|e| panic!("{name}: {e:?}"));
    }
}

/// A wholesome source is not screened that way: its tag covered every byte
/// it holds. An extra unlisted member under `META-INF/` -- where LibreOffice
/// keeps signatures -- does not stop the re-key, and is not carried over.
#[test]
fn a_wholesome_source_with_an_extra_meta_inf_member_still_rekeys() {
    let src = append_stored_member(
        &load_golden("lo-wholesome-gcm-argon2.odt"),
        "META-INF/documentsignatures.xml",
        b"<x/>",
    );
    assert_eq!(classify(&src).unwrap().mode, Mode::Wholesome);
    let (r, out) = rekey(&src, PASSWORD);
    r.unwrap();
    let names = crate::test_support::zip_namelist(&out);
    assert_eq!(names, ["mimetype", "encrypted-package", MANIFEST_PATH]);
}

#[derive(Debug)]
struct Cancelled;
impl std::fmt::Display for Cancelled {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("cancelled")
    }
}
impl std::error::Error for Cancelled {}

struct CancelsAt {
    inner: Cursor<Vec<u8>>,
    calls: usize,
    at: usize,
}
impl CancelsAt {
    fn tick(&mut self) -> io::Result<()> {
        self.calls += 1;
        if self.calls >= self.at {
            return Err(io::Error::other(Cancelled));
        }
        Ok(())
    }
}
impl Read for CancelsAt {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.tick()?;
        self.inner.read(buf)
    }
}
impl Seek for CancelsAt {
    fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
        self.tick()?;
        self.inner.seek(pos)
    }
}

/// A cancelling reader is `Io`, carrying the caller's error, at every call --
/// for both source shapes.
#[test]
fn a_cancelling_reader_is_io_at_every_call() {
    for src in [
        artifact("argon2-floor-1-8-1.odt"),
        load_golden("lo-legacy-aes-cbc.odt"),
    ] {
        let mut at = 1;
        loop {
            let reader = CancelsAt {
                inner: Cursor::new(src.clone()),
                calls: 0,
                at,
            };
            let r = reencrypt_to(
                reader,
                Cursor::new(Vec::new()),
                PASSWORD,
                NEW,
                DecryptLimits::default(),
                cheap(),
            );
            match r {
                Err(ReencryptError::Io(e)) => {
                    let inner = e.get_ref().expect("the caller's error");
                    assert!(inner.is::<Cancelled>(), "call {at}: {e}");
                }
                Ok(_) => break,
                Err(other) => panic!("call {at}: expected Io, got {other:?}"),
            }
            at += 1;
        }
    }
}

/// A failing writer is `Io` too, from either shape -- including the per-entry
/// one, where the failure surfaces deep inside the rebuild.
#[test]
fn a_failing_writer_is_io() {
    struct Full;
    impl Write for Full {
        fn write(&mut self, _: &[u8]) -> io::Result<usize> {
            Err(io::Error::new(io::ErrorKind::StorageFull, "disk full"))
        }
        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }
    impl Seek for Full {
        fn seek(&mut self, _: SeekFrom) -> io::Result<u64> {
            Ok(0)
        }
    }
    for name in ["lo-wholesome-gcm-argon2.odt", "lo-legacy-aes-cbc.odt"] {
        let r = reencrypt_to(
            Cursor::new(load_golden(name)),
            Full,
            PASSWORD,
            NEW,
            DecryptLimits::default(),
            cheap(),
        );
        assert!(
            matches!(&r, Err(ReencryptError::Io(e)) if e.kind() == io::ErrorKind::StorageFull),
            "{name}: {r:?}"
        );
    }
}

/// Writes `reencrypt_to` outputs for the LibreOffice gate into the directory
/// named by `ODF_CRYPTO_LO_GATE_DIR`; they open there with `NEW`.
#[test]
#[ignore = "writes files for the LibreOffice gate"]
fn write_lo_gate_outputs() {
    let dir = std::path::PathBuf::from(std::env::var("ODF_CRYPTO_LO_GATE_DIR").unwrap());
    std::fs::create_dir_all(&dir).unwrap();
    for name in [
        "lo-wholesome-gcm-argon2.odt",
        "lo-legacy-aes-cbc.odt",
        "aoo-blowfish-pbkdf2.odt",
    ] {
        let mut out = Cursor::new(Vec::new());
        reencrypt_to(
            Cursor::new(load_golden(name)),
            &mut out,
            PASSWORD,
            NEW,
            DecryptLimits::default(),
            Argon2Params::LIBREOFFICE_DEFAULT,
        )
        .unwrap();
        std::fs::write(dir.join(format!("rekeyed-{name}")), out.into_inner()).unwrap();
    }
}

/// LibreOffice's own per-entry package with a picture: every row it lists --
/// the `Pictures/` member included -- carries complete `encryption-data`, so
/// none of the refusals fires and it re-keys like any other.
/// `make_goldens.py lo-legacy-aes-cbc-image` wrote it.
#[test]
fn a_per_entry_golden_with_a_picture_rekeys() {
    let src = load_golden("lo-legacy-aes-cbc-image.odt");
    let class = classify(&src).unwrap();
    assert!(
        !class.has_non_encrypted_entries && !class.has_unexpected_streams,
        "{class:?}"
    );
    assert!(
        class
            .encrypted_entries
            .iter()
            .any(|e| e.path.starts_with("Pictures/")),
        "no encrypted Pictures/ row: {:?}",
        class
            .encrypted_entries
            .iter()
            .map(|e| &e.path)
            .collect::<Vec<_>>()
    );
    let (r, out) = rekey(&src, PASSWORD);
    assert_eq!(r.unwrap().integrity, IntegrityOutcome::PasswordCheckOnly);
    let mut streamed = Vec::new();
    decrypt_to(Cursor::new(&src), &mut streamed, PASSWORD).unwrap();
    assert!(decrypt(&out, NEW).unwrap() == streamed);
}

/// The library half of the CLI exit-code guard (CLAUDE.md, "The CLI"): no `_`
/// arm, so a new `ReencryptError` variant stops this compiling.
///
/// **If this stopped compiling, you added a variant.** Give it an exit code in
/// `reencrypt_exit`, add it to `the_reencrypt_exit_map_is_the_documented_one`
/// in `src/bin/odf-crypto_tests.rs`, then name it here.
#[test]
fn every_reencrypt_error_variant_is_accounted_for_in_the_cli_exit_map() {
    fn accounted_for(e: &ReencryptError) {
        match e {
            ReencryptError::Decrypt(_) => (),
            ReencryptError::Encrypt(_) => (),
            ReencryptError::PartiallyEncrypted => (),
            ReencryptError::UnexpectedStreams => (),
            ReencryptError::Io(_) => (),
        }
    }
    accounted_for(&ReencryptError::PartiallyEncrypted);
}