139 lines
4.1 KiB
Rust
139 lines
4.1 KiB
Rust
//! LUKS2 encryption backend.
|
|
//!
|
|
//! Passphrases are passed only over stdin and never appear in a command line
|
|
//! or a log line. TPM2 enrollment is not implemented.
|
|
|
|
use phoebus_core::{InstallConfig, SecretString};
|
|
|
|
use crate::auth::DestructiveAuthorization;
|
|
use crate::command::CommandSpec;
|
|
use crate::error::{ExecError, Result};
|
|
use crate::exec::Exec;
|
|
use crate::probe;
|
|
|
|
pub fn require_tools(exec: &Exec) -> Result<()> {
|
|
exec.require_tools(&["cryptsetup", "blkid"])
|
|
}
|
|
|
|
pub fn mapper_path(config: &InstallConfig) -> String {
|
|
format!("/dev/mapper/{}", config.encryption.name)
|
|
}
|
|
|
|
pub fn luks_format(
|
|
exec: &Exec,
|
|
_auth: &DestructiveAuthorization,
|
|
config: &InstallConfig,
|
|
partition: &str,
|
|
passphrase: &SecretString,
|
|
) -> Result<()> {
|
|
if passphrase.is_empty() {
|
|
return Err(ExecError::operation(
|
|
"storage_prepare",
|
|
"luksFormat",
|
|
"no passphrase configured",
|
|
));
|
|
}
|
|
let spec = CommandSpec::new("storage_prepare", "format LUKS2", "cryptsetup")
|
|
.args([
|
|
"luksFormat",
|
|
"--batch-mode",
|
|
"--type",
|
|
"luks2",
|
|
"--cipher",
|
|
&config.encryption.cipher,
|
|
"--key-size",
|
|
&config.encryption.key_size.to_string(),
|
|
"--pbkdf",
|
|
&config.encryption.pbkdf,
|
|
"--key-file",
|
|
"-",
|
|
partition,
|
|
])
|
|
.stdin(passphrase.expose().as_bytes().to_vec());
|
|
exec.run(spec)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn luks_open(
|
|
exec: &Exec,
|
|
config: &InstallConfig,
|
|
partition: &str,
|
|
passphrase: &SecretString,
|
|
) -> Result<()> {
|
|
let spec = CommandSpec::new("storage_prepare", "open LUKS2", "cryptsetup")
|
|
.args([
|
|
"open",
|
|
"--type",
|
|
"luks",
|
|
"--key-file",
|
|
"-",
|
|
partition,
|
|
&config.encryption.name,
|
|
])
|
|
.stdin(passphrase.expose().as_bytes().to_vec());
|
|
exec.run(spec)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn luks_close(
|
|
exec: &Exec,
|
|
_auth: &DestructiveAuthorization,
|
|
config: &InstallConfig,
|
|
) -> Result<()> {
|
|
let spec = CommandSpec::new("finalize", "close LUKS device", "cryptsetup")
|
|
.arg("close")
|
|
.arg(&config.encryption.name);
|
|
exec.run(spec)?;
|
|
Ok(())
|
|
}
|
|
|
|
/// A crypttab line for the root partition, using its stable UUID.
|
|
pub fn crypttab_line(exec: &Exec, config: &InstallConfig, partition: &str) -> Result<String> {
|
|
let uuid = probe::blkid_uuid(exec.runner.as_ref(), partition).ok_or_else(|| {
|
|
ExecError::operation("fstab_generate", "crypttab", "cannot determine LUKS UUID")
|
|
})?;
|
|
Ok(phoebus_core::system::crypttab(
|
|
&config.encryption.name,
|
|
&uuid,
|
|
))
|
|
}
|
|
|
|
pub fn tpm2_available() -> bool {
|
|
std::path::Path::new("/sys/class/tpm/tpm0").exists()
|
|
&& crate::exec::find_in_path("systemd-cryptenroll").is_some()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::command::RecordingRunner;
|
|
use crate::log::Logger;
|
|
use phoebus_core::Redactor;
|
|
|
|
fn make_exec(runner: RecordingRunner) -> Exec {
|
|
let path = tempfile::tempdir().unwrap().keep();
|
|
let logger = Logger::open(path.join("l.log"), Redactor::new(), false);
|
|
Exec::new(Box::new(runner), logger, false, path.join("root"), path)
|
|
}
|
|
|
|
#[test]
|
|
fn passphrase_goes_over_stdin_not_args() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner.clone());
|
|
let cfg = InstallConfig::default();
|
|
let auth = crate::auth::authorize(&crate::auth::AutoConfirm, "p", "f").unwrap();
|
|
let secret = SecretString::new("topsecret");
|
|
luks_format(&exec, &auth, &cfg, "/dev/vda2", &secret).unwrap();
|
|
let recorded = runner.recorded();
|
|
let spec = &recorded[0];
|
|
assert_eq!(spec.program, "cryptsetup");
|
|
assert!(!spec.args.iter().any(|a| a.contains("topsecret")));
|
|
assert_eq!(spec.stdin.as_deref(), Some(b"topsecret".as_slice()));
|
|
}
|
|
|
|
#[test]
|
|
fn mapper_path_uses_configured_name() {
|
|
let cfg = InstallConfig::default();
|
|
assert_eq!(mapper_path(&cfg), "/dev/mapper/cryptroot");
|
|
}
|
|
}
|