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
//! `enprot verify` subcommand — structural integrity check.
//!
//! Distinct from `verify-chain` (which checks cryptographic chain
//! anchor signatures against trust roots): this checks that parsed
//! EPT files have resolvable CAS pointers, well-formed cipher/pbkdf
//! extfields, and that IMMUTABLE/MUTED content hashes match their
//! declared values.
use std::fs::File;
use std::io::{BufRead, BufReader};
use crate::error::{Error, Result};
use crate::etree::{self, ParseOps};
use crate::{cas, cipher, crypto, pbkdf};
use super::pipeline::pair_inputs_to_outputs;
use super::{CommonArgs, OutputArgs, common::apply_common, common::resolve_policy};
/// `verify` entry point: parse each input file, walk the tree, and
/// report any structural issue (missing CAS blob, malformed extfield,
/// IMMUTABLE/MUTED hash mismatch). Returns Err if any issue found.
pub fn run(common: CommonArgs, output: OutputArgs) -> Result<()> {
let policy = resolve_policy(&common)?;
let mut paops = ParseOps::new(policy)?;
apply_common(&common, &mut paops);
let files = pair_inputs_to_outputs(
&output.files,
&output.output,
&output.prefix,
output.output_dir.as_deref(),
);
let mut issues = 0usize;
for (path_in, _) in &files {
if paops.io.verbose {
eprintln!("Verifying {}", path_in);
}
let reader: Box<dyn BufRead> = if path_in == "-" {
Box::new(BufReader::new(std::io::stdin()))
} else {
Box::new(BufReader::new(File::open(path_in).map_err(|e| {
Error::Io(std::io::Error::other(format!(
"Failed to open {path_in}: {e}"
)))
})?))
};
paops.runtime.fname = path_in.clone();
let tree = match etree::parse(reader, &mut paops) {
Ok(t) => t,
Err(e) => {
eprintln!("FAIL {}: parse error: {}", path_in, e);
issues += 1;
continue;
}
};
for node in &tree {
let node_issues = verify_node(node, &mut paops);
issues += node_issues;
}
if issues == 0 {
eprintln!("OK {}", path_in);
}
}
if issues > 0 {
return Err(Error::VerifyFailed { issues });
}
Ok(())
}
/// Recursively verify a tree node. Returns the number of issues found.
fn verify_node(node: &etree::TextNode, paops: &mut ParseOps) -> usize {
match node {
etree::TextNode::Stored { keyw, cas } => match cas::load(cas, paops) {
Ok(_) => 0,
Err(e) => {
eprintln!("FAIL: CAS pointer '{}' for WORD '{}': {}", cas, keyw, e);
1
}
},
etree::TextNode::Encrypted { txt, extfields, .. } => {
let mut n = 0;
for child in txt {
n += verify_node(child, paops);
}
if let Some(cipher_str) =
crate::extfield::EncryptedExtFields::from_map(extfields).cipher()
&& let Err(e) = cipher::parse_cipher_extfield(cipher_str)
{
eprintln!("FAIL: cipher extfield '{}': {}", cipher_str, e);
n += 1;
}
if let Some(phc_str) = crate::extfield::EncryptedExtFields::from_map(extfields).pbkdf()
&& let Err(e) = pbkdf::parse_phc(phc_str)
{
eprintln!("FAIL: pbkdf extfield '{}': {}", phc_str, e);
n += 1;
}
n
}
etree::TextNode::BeginEnd { txt, .. } => {
let mut n = 0;
for child in txt {
n += verify_node(child, paops);
}
n
}
etree::TextNode::Immutable {
name,
hashalg,
hash,
txt,
} => {
// RSD spec: verify that the declared hash matches the
// actual content hash.
let mut n = 0;
let blob = crate::etree::tree_to_blob(txt, paops);
match blob {
Ok(b) => {
let policy: &dyn crypto::CryptoPolicy = &*paops.crypto.policy;
match crate::crypto::hexdigest(hashalg, &b, policy) {
Ok(computed) if computed == *hash => {
// Hash matches — pass
}
Ok(computed) => {
eprintln!(
"FAIL: IMMUTABLE {} hash mismatch (declared={}, computed={})",
name, hash, computed
);
n += 1;
}
Err(e) => {
eprintln!(
"FAIL: IMMUTABLE {} hash algorithm '{}': {}",
name, hashalg, e
);
n += 1;
}
}
}
Err(e) => {
eprintln!("FAIL: IMMUTABLE {} internal serialization: {}", name, e);
n += 1;
}
}
for child in txt {
n += verify_node(child, paops);
}
n
}
etree::TextNode::Muted {
name,
hashalg,
hash,
} => {
// MUTED is the sanitized form — content lives in CAS.
// Verify the CAS blob exists and its hash matches.
match cas::load(hash, paops) {
Ok(_) => 0,
Err(e) => {
eprintln!(
"FAIL: MUTED {} CAS blob ({}={}): {}",
name, hashalg, hash, e
);
1
}
}
}
_ => 0,
}
}