vti-secrets 0.19.0

Pluggable secret-store backends + integration onboarding flow for Verifiable Trust Infrastructure
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
//! Encrypted-at-rest file backend (`backend = "encrypted_file"`, #1820).
//!
//! The secret is kept in one file under AES-256-GCM, keyed from a
//! key-encryption key (KEK) the operator chose explicitly:
//!
//! | `kek.source`    | file key                                   | unattended boot |
//! |-----------------|--------------------------------------------|-----------------|
//! | `passphrase`    | Argon2id(passphrase, salt), m=64 MiB, t=3  | if the passphrase file / credential is |
//! | `systemd_creds` | HKDF-SHA256(KEK from `$CREDENTIALS_DIRECTORY`) | yes (systemd unseals) |
//! | `tpm`           | HKDF-SHA256(KEK unsealed by the TPM 2.0)   | yes             |
//!
//! The envelope format is in [`envelope`]. Every write draws a fresh salt and
//! nonce, so each write has its own file key; with the TPM source each write
//! also seals a fresh random KEK.
//!
//! # Threat model
//!
//! Protects the secret in a copy of the file taken away from the running host
//! — a stolen disk or disk image, a backup, a snapshot, a misdirected `rsync` —
//! provided the KEK did not travel with it (a passphrase file on the same disk
//! does; a systemd credential sealed to the TPM, or a TPM-sealed KEK, does not).
//!
//! Does **not** protect against root, or the service user, on the running
//! host: whoever can read the passphrase file or credential, use the TPM as
//! this host, or read the process's memory can decrypt it. It is a local
//! at-rest control, not an isolation boundary — use a remote backend or a TEE
//! for that.
//!
//! # Failure semantics
//!
//! Fails closed. A missing file is "no secret yet" (`Ok(None)`), as for every
//! other backend; anything else — an unreadable KEK, a wrong passphrase, a bad
//! tag, an unknown version, a malformed header, a header written by a
//! different KEK source than the one configured — is an error.

mod envelope;
#[cfg(all(feature = "tpm", target_os = "linux"))]
mod tpm;

use std::fs::File;
use std::future::Future;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::pin::Pin;

use zeroize::Zeroizing;

use vti_common::error::AppError;

use crate::config::{EncryptedFileConfig, KekSource};
use envelope::{Argon2Params, Header, KekKind, NONCE_LEN, SALT_LEN, TpmDescriptor};

/// The default TCTI for the TPM source: the kernel's resource manager.
const DEFAULT_TCTI: &str = "device:/dev/tpmrm0";
/// Upper bound on a passphrase or credential file; nothing legitimate is close.
const MAX_INPUT_LEN: u64 = 64 * 1024;

/// Encrypted-at-rest file seed store. See the [module docs](self).
#[derive(Clone)]
pub struct EncryptedFileSeedStore {
    path: PathBuf,
    kek: Kek,
    /// Overrides `$CREDENTIALS_DIRECTORY` (tests, non-systemd supervisors).
    credentials_dir: Option<PathBuf>,
}

/// The resolved KEK source. Holds locations only, never key material.
#[derive(Clone)]
enum Kek {
    PassphraseFile(PathBuf),
    PassphraseCredential(String),
    SystemdCreds(String),
    #[cfg_attr(not(all(feature = "tpm", target_os = "linux")), allow(dead_code))]
    Tpm(TpmSettings),
}

#[derive(Clone)]
#[cfg_attr(not(all(feature = "tpm", target_os = "linux")), allow(dead_code))]
pub(crate) struct TpmSettings {
    pub(crate) pcrs: Vec<u8>,
    pub(crate) tcti: String,
}

impl Kek {
    fn kind(&self) -> KekKind {
        match self {
            Self::PassphraseFile(_) | Self::PassphraseCredential(_) => KekKind::Passphrase,
            Self::SystemdCreds(_) => KekKind::SystemdCreds,
            Self::Tpm(_) => KekKind::Tpm,
        }
    }
}

impl std::fmt::Debug for EncryptedFileSeedStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EncryptedFileSeedStore")
            .field("path", &self.path)
            .field("kek", &self.kek.kind().label())
            .finish()
    }
}

impl EncryptedFileSeedStore {
    /// Build from `[secrets.encrypted_file]`. The file is
    /// `config.path`, or `<data_dir>/<default_filename>` when unset.
    ///
    /// Validates the configuration only; the KEK is not read until the first
    /// [`get`](vti_common::seed_store::SeedStore::get) or `set`, so it is held
    /// in memory only while it is being used.
    pub fn from_config(
        config: &EncryptedFileConfig,
        data_dir: &Path,
        default_filename: &str,
    ) -> Result<Self, AppError> {
        let kek = match &config.kek {
            KekSource::Passphrase { file, credential } => match (file, credential) {
                (Some(path), None) => Kek::PassphraseFile(path.clone()),
                (None, Some(name)) => {
                    validate_credential_name(name)?;
                    Kek::PassphraseCredential(name.clone())
                }
                _ => {
                    return Err(AppError::Config(
                        "secrets.encrypted_file.kek (source = \"passphrase\") needs exactly one \
                         of `file` (a path to a file holding the passphrase) or `credential` (a \
                         systemd credential name). A passphrase is never taken from the config \
                         or an environment variable."
                            .into(),
                    ));
                }
            },
            KekSource::SystemdCreds { credential } => {
                validate_credential_name(credential)?;
                Kek::SystemdCreds(credential.clone())
            }
            KekSource::Tpm { pcrs, tcti } => {
                if !cfg!(all(feature = "tpm", target_os = "linux")) {
                    return Err(AppError::Config(
                        "secrets.encrypted_file.kek source \"tpm\" needs a Linux build with the \
                         vti-secrets `tpm` feature; this binary has no TPM support. Use \
                         source = \"systemd_creds\" (systemd can seal the credential to the TPM) \
                         or \"passphrase\"."
                            .into(),
                    ));
                }
                let mut pcrs = pcrs.clone();
                if let Some(bad) = pcrs.iter().find(|p| **p > envelope::MAX_PCR) {
                    return Err(AppError::Config(format!(
                        "secrets.encrypted_file.kek.pcrs: PCR {bad} is out of range (0-23)"
                    )));
                }
                pcrs.sort_unstable();
                pcrs.dedup();
                Kek::Tpm(TpmSettings {
                    pcrs,
                    tcti: tcti.clone().unwrap_or_else(|| DEFAULT_TCTI.to_string()),
                })
            }
        };
        let path = config
            .path
            .clone()
            .unwrap_or_else(|| data_dir.join(default_filename));
        Ok(Self {
            path,
            kek,
            credentials_dir: None,
        })
    }

    /// Read systemd credentials from `dir` instead of `$CREDENTIALS_DIRECTORY`.
    /// For tests, and for supervisors other than systemd that deliver
    /// credentials the same way.
    pub fn with_credentials_directory(mut self, dir: impl Into<PathBuf>) -> Self {
        self.credentials_dir = Some(dir.into());
        self
    }

    /// The envelope's location.
    pub fn path(&self) -> &Path {
        &self.path
    }

    fn credential_path(&self, name: &str) -> Result<PathBuf, AppError> {
        let dir = match &self.credentials_dir {
            Some(d) => d.clone(),
            None => std::env::var_os("CREDENTIALS_DIRECTORY")
                .map(PathBuf::from)
                .ok_or_else(|| {
                    AppError::SecretStore(format!(
                        "systemd credential '{name}' is configured but CREDENTIALS_DIRECTORY is \
                         not set: run the service under systemd with LoadCredential= or \
                         LoadCredentialEncrypted= naming '{name}'"
                    ))
                })?,
        };
        Ok(dir.join(name))
    }

    fn read_passphrase(&self) -> Result<Zeroizing<Vec<u8>>, AppError> {
        let path = match &self.kek {
            Kek::PassphraseFile(p) => p.clone(),
            Kek::PassphraseCredential(name) => self.credential_path(name)?,
            Kek::SystemdCreds(_) | Kek::Tpm(_) => {
                return Err(AppError::SecretStore(
                    "internal: passphrase requested for a non-passphrase KEK source".into(),
                ));
            }
        };
        let mut raw = read_secret_input(&path, "passphrase")?;
        strip_one_newline(&mut raw);
        if raw.is_empty() {
            return Err(AppError::SecretStore(format!(
                "the passphrase in {} is empty",
                path.display()
            )));
        }
        Ok(raw)
    }

    fn read_credential_kek(&self, name: &str) -> Result<Zeroizing<[u8; 32]>, AppError> {
        let path = self.credential_path(name)?;
        let mut raw = read_secret_input(&path, "systemd credential")?;
        decode_kek(&mut raw).ok_or_else(|| {
            AppError::SecretStore(format!(
                "systemd credential '{name}' must hold a 32-byte key: 32 raw bytes or 64 hex \
                 characters (e.g. `openssl rand 32 | systemd-creds encrypt --name={name} - …`)"
            ))
        })
    }

    /// The file key for `header`, deriving or unsealing the KEK as configured.
    fn file_key(&self, header: &Header) -> Result<Zeroizing<[u8; 32]>, AppError> {
        match &self.kek {
            Kek::PassphraseFile(_) | Kek::PassphraseCredential(_) => {
                let params = Argon2Params::decode(&header.descriptor)?;
                let passphrase = self.read_passphrase()?;
                params.derive(&passphrase, &header.salt)
            }
            Kek::SystemdCreds(name) => {
                if !header.descriptor.is_empty() {
                    return Err(AppError::SecretStore(
                        "encrypted secret file is malformed: unexpected systemd-creds descriptor"
                            .into(),
                    ));
                }
                let kek = self.read_credential_kek(name)?;
                envelope::derive_from_kek(&kek, &header.salt, KekKind::SystemdCreds)
            }
            Kek::Tpm(settings) => {
                let descriptor = TpmDescriptor::decode(&header.descriptor)?;
                let kek = unseal_tpm(settings, &descriptor)?;
                envelope::derive_from_kek(&kek, &header.salt, KekKind::Tpm)
            }
        }
    }

    fn get_blocking(&self) -> Result<Option<Vec<u8>>, AppError> {
        let file = match read_envelope(&self.path)? {
            Some(f) => f,
            None => return Ok(None),
        };
        let (header, header_len) = Header::parse(&file)?;
        let configured = self.kek.kind();
        if header.kek != configured {
            return Err(AppError::SecretStore(format!(
                "{} was written with the '{}' key-encryption key source, but the config says \
                 '{}'; refusing to guess",
                self.path.display(),
                header.kek.label(),
                configured.label()
            )));
        }
        let key = self.file_key(&header)?;
        envelope::open(&file, &header, header_len, &key).map(Some)
    }

    fn set_blocking(&self, secret: &[u8]) -> Result<(), AppError> {
        let mut salt = [0u8; SALT_LEN];
        rand::fill(&mut salt);
        let mut nonce = [0u8; NONCE_LEN];
        rand::fill(&mut nonce);

        let (descriptor, key) = match &self.kek {
            Kek::PassphraseFile(_) | Kek::PassphraseCredential(_) => {
                let params = Argon2Params::WRITE;
                let passphrase = self.read_passphrase()?;
                (params.encode(), params.derive(&passphrase, &salt)?)
            }
            Kek::SystemdCreds(name) => {
                let kek = self.read_credential_kek(name)?;
                (
                    Vec::new(),
                    envelope::derive_from_kek(&kek, &salt, KekKind::SystemdCreds)?,
                )
            }
            Kek::Tpm(settings) => {
                // A fresh KEK per write: the old sealed object is simply
                // replaced along with the file.
                let mut kek = Zeroizing::new([0u8; 32]);
                rand::fill(kek.as_mut());
                let descriptor = seal_tpm(settings, &kek)?;
                (
                    descriptor.encode()?,
                    envelope::derive_from_kek(&kek, &salt, KekKind::Tpm)?,
                )
            }
        };
        let header = Header {
            kek: self.kek.kind(),
            salt,
            nonce,
            descriptor,
        };
        let file = envelope::seal(&header, &key, secret)?;
        write_atomic(&self.path, &file)
            .map_err(|e| AppError::SecretStore(format!("writing {}: {e}", self.path.display())))
    }
}

impl super::SeedStore for EncryptedFileSeedStore {
    fn get(&self) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>, AppError>> + Send + '_>> {
        let this = self.clone();
        Box::pin(async move {
            // Argon2id at 64 MiB and TPM round-trips are slow, blocking work.
            tokio::task::spawn_blocking(move || this.get_blocking())
                .await
                .map_err(|e| AppError::SecretStore(format!("encrypted_file read task: {e}")))?
        })
    }

    fn set(
        &self,
        secret: &[u8],
    ) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + '_>> {
        let this = self.clone();
        let secret = Zeroizing::new(secret.to_vec());
        Box::pin(async move {
            tokio::task::spawn_blocking(move || this.set_blocking(&secret))
                .await
                .map_err(|e| AppError::SecretStore(format!("encrypted_file write task: {e}")))?
        })
    }

    fn delete(&self) -> Pin<Box<dyn Future<Output = Result<(), AppError>> + Send + '_>> {
        Box::pin(async move {
            match std::fs::remove_file(&self.path) {
                Ok(()) => Ok(()),
                Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
                Err(e) => Err(AppError::SecretStore(format!(
                    "removing {}: {e}",
                    self.path.display()
                ))),
            }
        })
    }
}

#[cfg(all(feature = "tpm", target_os = "linux"))]
fn seal_tpm(settings: &TpmSettings, kek: &[u8; 32]) -> Result<TpmDescriptor, AppError> {
    tpm::seal(settings, kek)
}

#[cfg(all(feature = "tpm", target_os = "linux"))]
fn unseal_tpm(
    settings: &TpmSettings,
    descriptor: &TpmDescriptor,
) -> Result<Zeroizing<[u8; 32]>, AppError> {
    tpm::unseal(settings, descriptor)
}

// `from_config` refuses a TPM source on these builds, so these are reachable
// only through a bug; they still fail closed.
#[cfg(not(all(feature = "tpm", target_os = "linux")))]
fn seal_tpm(_: &TpmSettings, _: &[u8; 32]) -> Result<TpmDescriptor, AppError> {
    Err(no_tpm())
}

#[cfg(not(all(feature = "tpm", target_os = "linux")))]
fn unseal_tpm(_: &TpmSettings, _: &TpmDescriptor) -> Result<Zeroizing<[u8; 32]>, AppError> {
    Err(no_tpm())
}

#[cfg(not(all(feature = "tpm", target_os = "linux")))]
fn no_tpm() -> AppError {
    AppError::SecretStore("this build has no TPM support (Linux + `tpm` feature)".into())
}

/// A systemd credential name is a single path component.
fn validate_credential_name(name: &str) -> Result<(), AppError> {
    if name.is_empty() || name == "." || name == ".." || name.contains(['/', '\\', '\0']) {
        return Err(AppError::Config(format!(
            "secrets.encrypted_file: '{name}' is not a valid systemd credential name"
        )));
    }
    Ok(())
}

/// Read the envelope, or `None` when it does not exist yet.
fn read_envelope(path: &Path) -> Result<Option<Vec<u8>>, AppError> {
    let file = match File::open(path) {
        Ok(f) => f,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => {
            return Err(AppError::SecretStore(format!(
                "reading {}: {e}",
                path.display()
            )));
        }
    };
    warn_if_not_owner_only(path, &file);
    let mut bytes = Vec::new();
    file.take(envelope::MAX_FILE_LEN + 1)
        .read_to_end(&mut bytes)
        .map_err(|e| AppError::SecretStore(format!("reading {}: {e}", path.display())))?;
    if bytes.len() as u64 > envelope::MAX_FILE_LEN {
        return Err(AppError::SecretStore(format!(
            "{} is larger than any encrypted secret file",
            path.display()
        )));
    }
    Ok(Some(bytes))
}

/// Read a passphrase or KEK file into zeroizing memory, sized up front so a
/// reallocation never leaves a copy behind.
fn read_secret_input(path: &Path, what: &str) -> Result<Zeroizing<Vec<u8>>, AppError> {
    let file = File::open(path)
        .map_err(|e| AppError::SecretStore(format!("reading {what} {}: {e}", path.display())))?;
    warn_if_not_owner_only(path, &file);
    let len = file.metadata().map(|m| m.len()).unwrap_or(0);
    if len > MAX_INPUT_LEN {
        return Err(AppError::SecretStore(format!(
            "{what} {} is implausibly large",
            path.display()
        )));
    }
    let mut buf = Zeroizing::new(Vec::with_capacity(len as usize + 1));
    file.take(MAX_INPUT_LEN + 1)
        .read_to_end(&mut buf)
        .map_err(|e| AppError::SecretStore(format!("reading {what} {}: {e}", path.display())))?;
    if buf.len() as u64 > MAX_INPUT_LEN {
        return Err(AppError::SecretStore(format!(
            "{what} {} is implausibly large",
            path.display()
        )));
    }
    Ok(buf)
}

fn strip_one_newline(buf: &mut Vec<u8>) {
    if buf.last() == Some(&b'\n') {
        buf.pop();
        if buf.last() == Some(&b'\r') {
            buf.pop();
        }
    }
}

/// 32 raw bytes, or 64 hex characters with an optional trailing newline.
fn decode_kek(raw: &mut Vec<u8>) -> Option<Zeroizing<[u8; 32]>> {
    let mut kek = Zeroizing::new([0u8; 32]);
    if raw.len() == 32 {
        kek.copy_from_slice(raw);
        return Some(kek);
    }
    strip_one_newline(raw);
    if raw.len() == 64 && hex::decode_to_slice(&raw[..], kek.as_mut()).is_ok() {
        return Some(kek);
    }
    None
}

#[cfg(unix)]
fn warn_if_not_owner_only(path: &Path, file: &File) {
    use std::os::unix::fs::PermissionsExt;
    if let Ok(meta) = file.metadata()
        && meta.permissions().mode() & 0o077 != 0
    {
        tracing::warn!(
            path = %path.display(),
            mode = format_args!("{:o}", meta.permissions().mode() & 0o777),
            "secret-bearing file is readable by other users; restrict it to the service user (chmod 600)"
        );
    }
}

#[cfg(not(unix))]
fn warn_if_not_owner_only(_: &Path, _: &File) {}

/// Write `bytes` to `path` atomically at owner-only permissions: a new file
/// beside it (0600 from creation, fsynced, via
/// [`vti_common::secure_file::write_secret_file`]) renamed over it, then the
/// directory fsynced. A crash leaves the old file or the new one, never a torn
/// one.
fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
    let dir = match path.parent() {
        Some(d) if !d.as_os_str().is_empty() => d.to_path_buf(),
        _ => PathBuf::from("."),
    };
    if !dir.exists() {
        std::fs::create_dir_all(&dir)?;
        vti_common::secure_file::restrict_dir_to_owner(&dir)?;
    }
    let name = path
        .file_name()
        .ok_or_else(|| std::io::Error::other("the secret file path has no file name"))?;
    let mut suffix = [0u8; 8];
    rand::fill(&mut suffix);
    let tmp = dir.join(format!(
        ".{}.tmp-{}",
        name.to_string_lossy(),
        hex::encode(suffix)
    ));
    vti_common::secure_file::write_secret_file(&tmp, bytes)?;
    if let Err(e) = std::fs::rename(&tmp, path) {
        let _ = std::fs::remove_file(&tmp);
        return Err(e);
    }
    #[cfg(unix)]
    if let Ok(d) = File::open(&dir) {
        let _ = d.sync_all();
    }
    Ok(())
}

#[cfg(test)]
mod tests;