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
//! Encrypt → cross-implementation decrypt matrix.
//!
//! Direction **(a) flpdf encrypt → qpdf decrypt**: flpdf encrypts each Standard
//! security-handler variant the writer supports, and qpdf must authenticate
//! and decrypt the result to clean plaintext. This is the writer-side half of
//! the cross-implementation matrix.
//!
//! Directions **(b) qpdf encrypt → flpdf decrypt** and **(c) flpdf --decrypt
//! round-trip** are already covered, for the full qpdf-generated fixture
//! matrix (v1-rc4-40 / v2-rc4-128 / v4-rc4-128 / v4-aes-128 / v5-aes-256-r5 /
//! v5-aes-256-r6), by `encrypted_rewrite_tests.rs`
//! (`encrypted_fixtures_rewrite_to_plaintext_matching_qpdf_decrypt_objects`),
//! so they are intentionally not duplicated here.
//!
//! The empty-user-password row is covered below. Option-specific permission
//! and metadata cases are covered by the writer-side CLI tests.
use assert_cmd::Command;
use std::path::{Path, PathBuf};
use std::process::Command as ShellCommand;
const FIXTURE: &str = "../../tests/fixtures/compat/one-page.pdf";
fn fixture() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join(FIXTURE)
}
fn ensure_qpdf_or_skip() -> bool {
let available = ShellCommand::new("qpdf")
.arg("--version")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if available {
return true;
}
if std::env::var_os("CI").is_some() {
panic!("qpdf required for the encrypt/decrypt matrix tests on CI");
}
eprintln!("skipping: qpdf not available");
false
}
/// (label, `--encrypt` arg vector incl. `--allow-weak-crypto`/`--` where
/// needed, expected qpdf `R =` line).
const WRITE_MATRIX: &[(&str, &[&str], &str)] = &[
(
"v1-rc4-40",
&["--allow-weak-crypto", "--encrypt", "u", "o", "40", "--"],
"R = 2",
),
(
"v2-rc4-128",
&["--allow-weak-crypto", "--encrypt", "u", "o", "128", "--"],
"R = 3",
),
(
"v4-rc4-128",
&[
"--allow-weak-crypto",
"--encrypt",
"u",
"o",
"128",
"--force-V4",
"--",
],
"R = 4",
),
(
"v4-aes-128",
&["--encrypt", "u", "o", "128", "--use-aes=y", "--"],
"R = 4",
),
("v5-aes-256", &["--encrypt", "u", "o", "256", "--"], "R = 6"),
];
/// (a) For every writer-supported handler, flpdf-encrypted output must (1)
/// authenticate under qpdf with the expected revision and (2) decrypt cleanly
/// via `qpdf --decrypt` to plaintext that no longer carries `/Encrypt`.
#[test]
fn flpdf_encrypt_matrix_decrypts_cleanly_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
for (label, enc_args, expected_r) in WRITE_MATRIX {
let enc = tmp.path().join(format!("{label}.pdf"));
let mut cmd = Command::cargo_bin("flpdf").unwrap();
for a in *enc_args {
cmd.arg(a);
}
cmd.arg(fixture()).arg(&enc).assert().success();
// qpdf authenticates the user password and reports the expected R.
let show = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--show-encryption")
.arg(&enc)
.output()
.unwrap();
assert!(
show.status.success(),
"{label}: qpdf --show-encryption failed: {}",
String::from_utf8_lossy(&show.stderr)
);
let s = String::from_utf8_lossy(&show.stdout);
assert!(
s.contains(expected_r) && s.contains("Supplied password is user password"),
"{label}: expected {expected_r:?} + user-password match, got: {s}"
);
// qpdf --decrypt produces clean plaintext (full content round-trip).
let dec = tmp.path().join(format!("{label}-dec.pdf"));
let d = ShellCommand::new("qpdf")
.arg("--password=u")
.arg("--decrypt")
.arg(&enc)
.arg(&dec)
.output()
.unwrap();
assert!(
d.status.success(),
"{label}: qpdf --decrypt failed: {}",
String::from_utf8_lossy(&d.stderr)
);
// Structurally confirm the decrypted output is plaintext (a byte-search
// for "/Encrypt" could false-positive on content/metadata): qpdf
// reports it as unencrypted.
let show_dec = ShellCommand::new("qpdf")
.arg("--show-encryption")
.arg(&dec)
.output()
.unwrap();
assert!(
show_dec.status.success(),
"{label}: qpdf --show-encryption on decrypted output failed: {}",
String::from_utf8_lossy(&show_dec.stderr)
);
assert!(
String::from_utf8_lossy(&show_dec.stdout).contains("File is not encrypted"),
"{label}: qpdf-decrypted output must be plaintext, got: {}",
String::from_utf8_lossy(&show_dec.stdout)
);
}
}
/// Edge case (empty user password): a 256-bit file with an EMPTY user password
/// and a non-empty owner password opens without a password. qpdf must decrypt
/// it with no `--password`. (The insecure gate is for non-empty-user +
/// empty-owner, so it does not fire here.)
#[test]
fn flpdf_encrypt_empty_user_password_decrypts_via_qpdf() {
if !ensure_qpdf_or_skip() {
return;
}
let tmp = tempfile::tempdir().unwrap();
let enc = tmp.path().join("empty-user.pdf");
Command::cargo_bin("flpdf")
.unwrap()
.args(["--encrypt", "", "owner-pw", "256", "--"])
.arg(fixture())
.arg(&enc)
.assert()
.success();
let dec = tmp.path().join("empty-user-dec.pdf");
let d = ShellCommand::new("qpdf")
.arg("--decrypt")
.arg(&enc)
.arg(&dec)
.output()
.unwrap();
assert!(
d.status.success(),
"qpdf --decrypt with an empty user password failed: {}",
String::from_utf8_lossy(&d.stderr)
);
let show_dec = ShellCommand::new("qpdf")
.arg("--show-encryption")
.arg(&dec)
.output()
.unwrap();
assert!(
show_dec.status.success(),
"qpdf --show-encryption on decrypted empty-user output failed: {}",
String::from_utf8_lossy(&show_dec.stderr)
);
assert!(
String::from_utf8_lossy(&show_dec.stdout).contains("File is not encrypted"),
"decrypted empty-user output must be plaintext, got: {}",
String::from_utf8_lossy(&show_dec.stdout)
);
}