enprot 0.5.20

Engyon Protected Text (EPT) — confidentiality processor and capability ledger
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Core transform pipeline — `encrypt` / `decrypt` / `store` / `fetch` /
//! `encrypt-store` / `passthrough`.
//!
//! This module owns the orchestration that turns a typed [`RunConfig`]
//! into actual file transformations. `app_main`'s dispatch arms for
//! the six core ops each build a `RunConfig` and call [`run`]. The
//! parallel-processing path (`--jobs > 1`) builds one `ParseOps` per
//! scoped thread via [`RunConfig::build_paops`].

use std::fs;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::path::Path;

use crate::error::{Error, Result};
use crate::etree::{self, ParseOps};
use crate::{capability, cappolicy, consts};

use super::{
    CommonArgs, EncryptOpts, Operation, OutputArgs, build_anchor_config, make_policy,
    resolve_separators, walk_for_chains,
};

/// Typed configuration for the four core transform operations
/// (`encrypt`, `decrypt`, `store`, `fetch`, `encrypt-store`,
/// `passthrough`). Grouping these previously-separate parameters
/// into a struct gives the FFI, library consumers, and tests a
/// single dispatch surface that doesn't depend on clap.
///
/// Model-driven: the configuration IS the model. `app_main` parses
/// argv into `RunConfig`; `run()` consumes `RunConfig`. The FFI
/// can build a `RunConfig` directly from JSON, bypassing clap
/// entirely.
#[derive(Clone, Debug)]
pub struct RunConfig {
    pub common: CommonArgs,
    pub output: OutputArgs,
    /// `None` means Passthrough (no transform).
    pub op: Option<(EncryptOpts, Operation)>,
    pub recipient_pubs: Vec<String>,
    pub recipient_privs: Vec<String>,
}

impl RunConfig {
    /// Build a fresh `ParseOps` from this configuration. Each call
    /// produces an independent instance with its own RNG, PBKDF cache,
    /// and mutable state. Safe to call from multiple threads — no
    /// shared mutable state between instances.
    ///
    /// `policy_name` must be pre-resolved (FIPS detection, explicit
    /// policy, defaults) by the caller. This method does NOT redo
    /// that resolution.
    pub(super) fn build_paops(&self, policy_name: &str) -> Result<ParseOps> {
        let policy = make_policy(policy_name);
        let mut paops = if let Some(defaults) = self.common.defaults.as_deref() {
            let mut p = ParseOps::new(make_policy(defaults))?;
            p.crypto.policy = policy;
            p
        } else {
            ParseOps::new(policy)?
        };

        if let Some(dir) = self.common.casdir.clone() {
            paops.io.set_local_casdir(dir);
        } else if Path::new("cas").is_dir() {
            paops.io.set_local_casdir(Path::new("cas").to_path_buf());
        } else {
            paops.io.set_local_casdir(Path::new(".").to_path_buf());
        }

        paops.io.verbose = self.common.verbose && !self.common.quiet;
        paops.io.inline_data = self.common.inline || self.common.casdir.is_none();
        paops.max_depth = self.common.max_depth;
        let (left, right) = resolve_separators(&self.common);
        paops.separators.left = left;
        paops.separators.right = right;
        paops.passwords.extend(self.common.password.clone());
        paops.crypto.recipient_pubs = self.recipient_pubs.clone();
        for (i, w) in self.output.word.iter().enumerate() {
            if let Some(priv_pem) = self
                .recipient_privs
                .get(i)
                .or_else(|| self.recipient_privs.first())
            {
                paops
                    .crypto
                    .recipient_privkeys
                    .insert(w.clone(), priv_pem.clone());
            }
        }
        if self.common.pbkdf_disable_cache {
            paops.crypto.pbkdf_cache = None;
        }

        if let Some((enc_opts, op_kind)) = self.op.as_ref() {
            for w in &self.output.word {
                match op_kind {
                    Operation::Encrypt => {
                        paops.transforms.encrypt.insert(w.clone());
                    }
                    Operation::Decrypt => {
                        paops.transforms.decrypt.insert(w.clone());
                    }
                    Operation::Store => {
                        paops.transforms.store.insert(w.clone());
                    }
                    Operation::Fetch => {
                        paops.transforms.fetch.insert(w.clone());
                    }
                    Operation::EncryptStore => {
                        paops.transforms.encrypt.insert(w.clone());
                        paops.transforms.store.insert(w.clone());
                    }
                }
            }

            if matches!(op_kind, Operation::Encrypt | Operation::EncryptStore) {
                if let Some(alg) = enc_opts.pbkdf.as_deref() {
                    paops.crypto.pbkdfopts.alg = alg.to_string();
                }
                if let Some(saltlen) = enc_opts.pbkdf_salt_len {
                    paops.crypto.pbkdfopts.saltlen = saltlen;
                }
                if let Some(msec) = enc_opts.pbkdf_msec {
                    paops.crypto.pbkdfopts.msec = Some(msec);
                }
                if let Some(raw) = enc_opts.pbkdf_params.as_deref() {
                    paops.crypto.pbkdfopts.msec = None;
                    let params: std::collections::BTreeMap<String, usize> = raw
                        .split(',')
                        .map(|kv| {
                            let (k, v) = kv.split_once('=').unwrap_or(("", "0"));
                            (k.to_string(), v.parse().unwrap_or(0))
                        })
                        .collect();
                    paops.crypto.pbkdfopts.params = Some(params);
                }
                if let Some(salt_hex) = enc_opts.pbkdf_salt.as_deref() {
                    paops.crypto.pbkdfopts.salt = Some(hex::decode(salt_hex).map_err(Error::from)?);
                }
                if let Some(c) = enc_opts.cipher.as_deref() {
                    paops.crypto.cipheropts.alg = c.to_string();
                }
                if let Some(iv_hex) = enc_opts.cipher_iv.as_deref() {
                    paops.crypto.cipheropts.iv = Some(hex::decode(iv_hex).map_err(Error::from)?);
                }
                paops.crypto.cipheropts.compress = enc_opts.compress;
            }
        }

        paops.anchor = build_anchor_config(
            self.common.anchor,
            self.common.signer.as_deref(),
            self.op.as_ref().map(|(_, k)| *k),
            &self.output.word,
        )?;

        Ok(paops)
    }
}

pub fn run(cfg: RunConfig) -> Result<()> {
    let cfg_parallel = cfg.clone();
    let RunConfig {
        common,
        output,
        op,
        recipient_pubs,
        recipient_privs,
    } = cfg;
    let explicit_policy = common.policy.clone();
    let mut policy_name = explicit_policy
        .clone()
        .unwrap_or_else(|| consts::DEFAULT_POLICY.to_string());

    let fips = common.fips
        || (cfg!(unix)
            && match fs::read_to_string("/proc/sys/crypto/fips_enabled") {
                Ok(s) => s.starts_with('1'),
                Err(_) => false,
            });
    if fips {
        if let Some(p) = explicit_policy.as_deref()
            && p != "nist"
        {
            return Err(Error::InvalidArg {
                arg: "--policy",
                reason: format!("Policy setting of '{p}' conflicts with --fips"),
            });
        }
        policy_name = "nist".to_string();
    }

    let policy = make_policy(&policy_name);
    let mut paops = if let Some(defaults) = common.defaults.as_deref() {
        let mut p = ParseOps::new(make_policy(defaults))?;
        p.crypto.policy = policy;
        p
    } else {
        ParseOps::new(policy)?
    };

    if let Some(dir) = common.casdir.clone() {
        paops.io.set_local_casdir(dir);
    } else if Path::new("cas").is_dir() {
        paops.io.set_local_casdir(Path::new("cas").to_path_buf());
    } else {
        paops.io.set_local_casdir(Path::new(".").to_path_buf());
    }

    paops.io.verbose = common.verbose && !common.quiet;
    paops.io.inline_data = common.inline || common.casdir.is_none();
    paops.max_depth = common.max_depth;
    let (left, right) = resolve_separators(&common);
    paops.separators.left = left;
    paops.separators.right = right;
    paops.passwords.extend(common.password);
    paops.crypto.recipient_pubs = recipient_pubs;
    for (i, w) in output.word.iter().enumerate() {
        if let Some(priv_pem) = recipient_privs.get(i).or_else(|| recipient_privs.first()) {
            paops
                .crypto
                .recipient_privkeys
                .insert(w.clone(), priv_pem.clone());
        }
    }
    if common.pbkdf_disable_cache {
        paops.crypto.pbkdf_cache = None;
    }

    // Apply the operation: populate the transform sets on paops. `op == None`
    // means Passthrough — leave the sets empty.
    if let Some((enc_opts, op_kind)) = op.as_ref() {
        // Capability policy check (TODO.roadmap/46): when encrypting,
        // refuse to write blocks for a WORD whose required capability
        // the caller doesn't hold. Decrypt/store/fetch don't gate on
        // per-WORD capability — they're not capability-changing ops.
        if matches!(op_kind, Operation::Encrypt | Operation::EncryptStore)
            && let Some(p) = common
                .policy_file
                .as_ref()
                .map(|p| cappolicy::CapPolicy::load_file(p))
                .transpose()?
        {
            let held = capability::CapabilitySet::from_paops(&paops);
            for w in &output.word {
                p.check_word_capability(w, &held)?;
            }
        }
        for w in &output.word {
            match op_kind {
                Operation::Encrypt => {
                    paops.transforms.encrypt.insert(w.clone());
                }
                Operation::Decrypt => {
                    paops.transforms.decrypt.insert(w.clone());
                }
                Operation::Store => {
                    paops.transforms.store.insert(w.clone());
                }
                Operation::Fetch => {
                    paops.transforms.fetch.insert(w.clone());
                }
                Operation::EncryptStore => {
                    paops.transforms.encrypt.insert(w.clone());
                    paops.transforms.store.insert(w.clone());
                }
            }
        }

        // PBKDF + cipher options only meaningful for encrypt / encrypt-store.
        if matches!(op_kind, Operation::Encrypt | Operation::EncryptStore) {
            if let Some(alg) = enc_opts.pbkdf.as_deref() {
                paops.crypto.pbkdfopts.alg = alg.to_string();
            }
            if let Some(saltlen) = enc_opts.pbkdf_salt_len {
                paops.crypto.pbkdfopts.saltlen = saltlen;
            }
            if let Some(msec) = enc_opts.pbkdf_msec {
                paops.crypto.pbkdfopts.msec = Some(msec);
            }
            if let Some(raw) = enc_opts.pbkdf_params.as_deref() {
                paops.crypto.pbkdfopts.msec = None;
                let params: std::collections::BTreeMap<String, usize> = raw
                    .split(',')
                    .map(|kv| {
                        let (k, v) = kv.split_once('=').unwrap_or(("", "0"));
                        (k.to_string(), v.parse().unwrap_or(0))
                    })
                    .collect();
                paops.crypto.pbkdfopts.params = Some(params);
            }
            if let Some(salt_hex) = enc_opts.pbkdf_salt.as_deref() {
                paops.crypto.pbkdfopts.salt = Some(hex::decode(salt_hex).map_err(Error::from)?);
            }
            if let Some(c) = enc_opts.cipher.as_deref() {
                paops.crypto.cipheropts.alg = c.to_string();
            }
            if let Some(iv_hex) = enc_opts.cipher_iv.as_deref() {
                paops.crypto.cipheropts.iv = Some(hex::decode(iv_hex).map_err(Error::from)?);
            }
        }
    }

    if paops.io.verbose {
        eprintln!(
            "LEFT_SEP='{}' RIGHT_SEP='{}' casdir = '{}'",
            paops.separators.left,
            paops.separators.right,
            paops.io.casdir.display(),
        );
    }

    let files = pair_inputs_to_outputs(
        &output.files,
        &output.output,
        &output.prefix,
        output.output_dir.as_deref(),
    );

    // Populate the anchor context once. `passthrough` (op == None) is
    // excluded because it performs no transformation and therefore
    // has nothing meaningful to anchor.
    paops.anchor = build_anchor_config(
        common.anchor,
        common.signer.as_deref(),
        op.as_ref().map(|(_, k)| *k),
        &output.word,
    )?;

    // --- File processing ---
    // When --jobs > 1 and we have multiple files, process them in
    // parallel using scoped threads. Each thread builds its own
    // ParseOps from the same RunConfig — no shared mutable state.
    if common.jobs > 1 && files.len() > 1 {
        tracing::info!(
            files = files.len(),
            jobs = common.jobs,
            "processing files in parallel"
        );
        let jobs = common.jobs.min(files.len());
        let chunk_size = files.len().div_ceil(jobs);
        let policy = &policy_name;
        let cfg_ref = &cfg_parallel;
        let chunks: Vec<&[(String, String)]> = files.chunks(chunk_size).collect();
        std::thread::scope(|s| {
            let handles: Vec<_> = chunks
                .iter()
                .map(|chunk| {
                    s.spawn(move || -> Result<()> {
                        let mut local_paops = cfg_ref.build_paops(policy)?;
                        for (path_in, path_out) in chunk.iter() {
                            process_one_file(path_in, path_out, &mut local_paops)?;
                        }
                        Ok(())
                    })
                })
                .collect();
            for h in handles {
                h.join()
                    .map_err(|_| Error::Io(std::io::Error::other("worker thread panicked")))??;
            }
            Ok(())
        })
    } else {
        for (path_in, path_out) in &files {
            process_one_file(path_in, path_out, &mut paops)?;
        }
        Ok(())
    }
}

/// Pair each input with its output, following the rules in
/// `TODO.issues/18-output-directory-mode.md`:
///
/// 1. `--output <FILE>` paired with this input → use it as-is.
/// 2. `--output-dir <DIR>` given → `DIR + "/" + basename(input)`.
/// 3. `--prefix <PREFIX>` where PREFIX ends in `/` or is an existing
///    directory → same as `--output-dir` behaviour.
/// 4. `--prefix <PREFIX>` (other) → PREFIX prepended to input verbatim
///    (the legacy flat-CLI behavior).
/// 5. Input is `-` (stdin) → output is `-` (stdout passthrough).
/// 6. Otherwise → output = input (in-place).
pub(super) fn pair_inputs_to_outputs(
    inputs: &[String],
    outputs: &[String],
    prefix: &str,
    output_dir: Option<&Path>,
) -> Vec<(String, String)> {
    let mut result = Vec::with_capacity(inputs.len());
    let mut out_iter = outputs.iter();
    let prefix_is_dir = !prefix.is_empty() && (prefix.ends_with('/') || Path::new(prefix).is_dir());

    for input in inputs {
        if let Some(output) = out_iter.next() {
            result.push((input.clone(), output.clone()));
            continue;
        }
        let output = if input == "-" {
            "-".to_string()
        } else if let Some(dir) = output_dir {
            join_with_basename(dir, input)
        } else if prefix_is_dir {
            let dir_str = prefix.trim_end_matches('/');
            join_with_basename(Path::new(dir_str), input)
        } else if prefix.is_empty() {
            input.clone()
        } else {
            format!("{}{}", prefix, input)
        };
        result.push((input.clone(), output));
    }
    result
}

fn join_with_basename(dir: &Path, input: &str) -> String {
    let base = Path::new(input)
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| input.to_string());
    dir.join(base).to_string_lossy().into_owned()
}

#[tracing::instrument(skip(paops), fields(path = %path_in))]
fn process_one_file(path_in: &str, path_out: &str, paops: &mut ParseOps) -> Result<()> {
    tracing::debug!(path_in, path_out, "processing file");

    let reader_in: Box<dyn BufRead> = if path_in == "-" {
        Box::new(BufReader::new(std::io::stdin()))
    } else {
        match std::fs::File::open(path_in) {
            Ok(f) => Box::new(BufReader::new(f)),
            Err(e) => {
                return Err(Error::Io(std::io::Error::other(format!(
                    "Failed to open {path_in} for reading: {e}"
                ))));
            }
        }
    };

    paops.runtime.fname = if path_in == "-" {
        "<stdin>".to_string()
    } else {
        path_in.to_string()
    };

    let tree_in = etree::parse(reader_in, paops)
        .map_err(|e| Error::Io(std::io::Error::other(format!("{e} in {path_in}, aborting"))))?;

    if paops.io.verbose {
        eprintln!("Transforming {}", path_in);
    }
    let mut tree_out = etree::transform(&tree_in, paops)
        .map_err(|e| Error::Io(std::io::Error::other(format!("{e} in {path_in}, aborting"))))?;

    // Optionally append a CHAIN block signing the new file state.
    // Must happen BEFORE tree_write so the anchor lands in the output.
    if paops.anchor.enabled {
        let chain_node = build_chain_anchor_node(&tree_out, paops)?;
        tree_out.push(chain_node);
    }

    if paops.io.verbose {
        eprintln!("Writing {}", path_out);
    }

    if paops.io.dry_run {
        eprintln!(
            "dry-run: {}{} ({} nodes in, {} nodes out, would write)",
            path_in,
            path_out,
            tree_in.len(),
            tree_out.len()
        );
        return Ok(());
    }

    let mut writer_out: Box<dyn Write> = if path_out == "-" {
        Box::new(BufWriter::new(std::io::stdout()))
    } else {
        match std::fs::File::create(path_out) {
            Ok(f) => Box::new(BufWriter::new(f)),
            Err(e) => {
                return Err(Error::Io(std::io::Error::other(format!(
                    "Failed to open {path_out} for writing: {e}"
                ))));
            }
        }
    };

    etree::tree_write(&mut writer_out, &tree_out, paops).map_err(|e| {
        Error::Io(std::io::Error::other(format!(
            "Write to {path_out} failed: {e}"
        )))
    })?;
    Ok(())
}

/// Build a [`TextNode::Chain`] node that signs the post-transform
/// `tree_out` state. The payload hash commits to the file content
/// EXCLUDING any chain anchors — that way the anchor isn't
/// self-referential and the hash is stable across re-anchoring.
///
/// parents: hashes of every CHAIN block already present in tree_out,
/// in file order. This makes the new anchor a linear descendant of
/// the most-recent prior anchor (TODO.finalize/17 DAG semantics;
/// multiple-parents / merge anchors are a future extension).
fn build_chain_anchor_node(
    tree_out: &etree::TextTree,
    paops: &mut ParseOps,
) -> Result<etree::TextNode> {
    use crate::ledger::{Anchor, PayloadHash, SignerId};
    use crate::pki::SigAlgKind;
    use std::collections::BTreeMap;

    let priv_pem = paops
        .anchor
        .signer_priv_pem
        .clone()
        .ok_or_else(|| Error::InvalidArg {
            arg: "anchor",
            reason: "anchor config missing signer_priv_pem".to_string(),
        })?;

    // Derive pubkey from privkey; compute fingerprint.
    let botan_priv = botan::Privkey::load_pem(&priv_pem).map_err(Error::botan)?;
    let botan_pub = botan_priv.pubkey().map_err(Error::botan)?;
    let pub_pem = botan_pub.pem_encode().map_err(Error::botan)?;
    let fp = capability::KeyFp::from_pem(&pub_pem)?;

    // payload_hash: SHA3-256 over the ENTIRE post-transform tree
    // (including any prior CHAIN blocks). This gives end-to-end
    // tamper detection: changing any earlier content invalidates
    // every subsequent anchor's payload. The new anchor itself
    // isn't in `tree_out` yet, so there's no self-reference.
    let blob = etree::tree_to_blob(tree_out, paops)?;
    let policy = crate::crypto::CryptoPolicyDefault {};
    let payload_hex = crate::crypto::hexdigest("sha3-256", &blob, &policy)?;
    let mut payload_arr = [0u8; 32];
    payload_arr.copy_from_slice(&hex::decode(payload_hex)?);
    let payload_hash = PayloadHash(payload_arr);

    // parents: latest existing CHAIN anchor only (linear chain).
    let parents = latest_existing_anchors(tree_out, paops)?;

    // mutations: e.g., "encrypt+Agent_007,GEHEIM". URL-encoded space.
    let words_joined = paops.anchor.words.join(",");
    let mutations = if words_joined.is_empty() {
        paops.anchor.operation.clone()
    } else {
        format!("{}+{}", paops.anchor.operation, words_joined)
    };

    let signer = SignerId::new(SigAlgKind::Ed25519, fp);
    let anchor = Anchor::builder(signer, payload_hash)
        .with_parents(parents)
        .with_mutations(mutations)
        .build();
    let signed = anchor.sign(&priv_pem, &pub_pem, SigAlgKind::Ed25519)?;
    let extfields: BTreeMap<String, String> = signed.to_extfields();
    Ok(etree::TextNode::Chain { extfields })
}

/// Return at most one parent: the [`AnchorHash`] of the LAST
/// [`TextNode::Chain`] in document order, or `None` if the tree has
/// no anchors. Linear-chain semantic — new anchors build on the tip.
fn latest_existing_anchors(
    tree: &etree::TextTree,
    _paops: &ParseOps,
) -> Result<Vec<crate::ledger::AnchorHash>> {
    let mut all = Vec::new();
    walk_for_chains(tree, &mut all)?;
    Ok(all.pop().into_iter().collect())
}