use std::path::Path;
use std::process::Command;
const CA_KEY: &str = "ca.key";
const CURVE: &str = "ec_paramgen_curve:P-256";
const DAYS: &str = "1";
pub(crate) const SEAT_NAME: &str = "lernie-test-seat";
pub(crate) const SERVER: &str = "server";
pub(crate) fn material(dir: &Path) {
std::fs::create_dir_all(dir).expect("the scratch directory");
ca(dir);
leaf(
dir,
SERVER,
&format!("/CN={SERVER}"),
"IP:127.0.0.1",
"serverAuth",
);
leaf(
dir,
"client",
&format!("/CN={SEAT_NAME}"),
&format!("DNS:{SEAT_NAME}"),
"clientAuth",
);
}
pub(crate) fn provisioned(dir: &Path, address: &str) -> crate::channel::material::Material {
material(dir);
std::fs::write(dir.join(crate::channel::material::ADDRESS), address).expect("the address");
crate::channel::material::read_dir(dir, crate::channel::material::Whose::Own)
.expect("readable")
.expect("provisioned")
}
fn ca(dir: &Path) {
tool(&[
"req",
"-x509",
"-newkey",
"ec",
"-pkeyopt",
CURVE,
"-nodes",
"-sha256",
"-days",
DAYS,
"-subj",
"/CN=lernie-test-ca",
"-keyout",
&path(dir, CA_KEY),
"-out",
&path(dir, crate::channel::material::ANCHORS),
]);
}
fn leaf(dir: &Path, name: &str, subject: &str, san: &str, eku: &str) {
issue(dir, name, subject, san, eku, None);
}
pub(crate) fn signed(dir: &Path, name: &str, public_key: &str) -> String {
let device = dir.join(format!("{name}.pub"));
std::fs::write(&device, public_key).expect("the device's public key");
let subject = format!("/CN={name}");
let san = format!("DNS:{name}");
issue(dir, name, &subject, &san, "clientAuth", Some(&device));
std::fs::read_to_string(dir.join(format!("{name}.pem"))).expect("the signed leaf")
}
fn issue(dir: &Path, name: &str, subject: &str, san: &str, eku: &str, forced: Option<&Path>) {
let ext = dir.join(format!("{name}.ext"));
std::fs::write(
&ext,
format!("[leaf]\nsubjectAltName={san}\nextendedKeyUsage={eku}\n"),
)
.expect("the extension file");
tool(&[
"req",
"-new",
"-newkey",
"ec",
"-pkeyopt",
CURVE,
"-nodes",
"-sha256",
"-subj",
subject,
"-keyout",
&path(dir, &format!("{name}.key")),
"-out",
&path(dir, &format!("{name}.csr")),
]);
let (ext, csr, pem) = (
ext.to_string_lossy().into_owned(),
path(dir, &format!("{name}.csr")),
path(dir, &format!("{name}.pem")),
);
let (ca, ca_key) = (
path(dir, crate::channel::material::ANCHORS),
path(dir, CA_KEY),
);
let forced = forced.map(|key| key.to_string_lossy().into_owned());
let mut signing = vec![
"x509",
"-req",
"-sha256",
"-days",
DAYS,
"-extfile",
&ext,
"-extensions",
"leaf",
"-in",
&csr,
"-CA",
&ca,
"-CAkey",
&ca_key,
"-CAcreateserial",
"-out",
&pem,
];
if let Some(key) = &forced {
signing.extend(["-force_pubkey", key]);
}
tool(&signing);
}
pub(crate) fn foot(dir: &Path, named: bool) {
let subject = if named {
format!("/CN={SEAT_NAME}/OU=foot")
} else {
"/OU=foot".to_owned()
};
leaf(
dir,
"client",
&subject,
&format!("DNS:{SEAT_NAME}"),
"clientAuth",
);
}
fn path(dir: &Path, leaf: &str) -> String {
dir.join(leaf).to_string_lossy().into_owned()
}
fn tool(args: &[&str]) {
let said = Command::new("openssl")
.args(args)
.output()
.expect("openssl is installed");
assert!(
said.status.success(),
"openssl {args:?}: {}",
String::from_utf8_lossy(&said.stderr)
);
}