Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 50 additions & 9 deletions pkgs/fleet/crates/fleet-cert/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Groups and verbs (spec §6.1 tree; status as built):
ocsp sign | inspect | test
chain build | test
bundle inspect
profile list | show
profile list | show | validate | scaffold
algorithms list | describe
explain [topic]
doctor
Expand Down Expand Up @@ -433,7 +433,7 @@ fn leaf_cmd(args: &[String], json_out: bool) -> Result<i32> {
let issuer = p.require("issuer", "leaf issue")?;
let signer = signer_source(&p, "issuer-key-dir", "leaf issue")?;
let profile_name = p.require("profile", "leaf issue")?;
let prof = profile::leaf_profile(&profile_name)?;
let prof = profile::resolve_profile(&profile_name)?;
let name = p
.get("name")
.map(|s| s.to_string())
Expand Down Expand Up @@ -771,8 +771,53 @@ fn profile_cmd(args: &[String], json_out: bool) -> Result<i32> {
});
Ok(emit(json_out, text, value))
}
"scaffold" => {
let p = parse_args(rest, &["name", "out"], &["json"])?;
let name = p
.get("name")
.or_else(|| p.positional(0))
.ok_or_else(|| CertError::usage("profile scaffold needs --name <name>"))?;
let out = p.require("out", "profile scaffold")?;
let scaffold = profile::CustomProfile::scaffold(name);
scaffold.validate()?;
let bytes = serde_json::to_vec_pretty(&scaffold)
.map_err(|e| CertError::engine(e.to_string()))?;
use std::io::Write;
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&out)
.map_err(|e| CertError::engine(format!("cannot create profile {out}: {e}")))?;
file.write_all(&bytes)
.map_err(|e| CertError::engine(format!("cannot write profile {out}: {e}")))?;
Ok(emit(
json_out,
format!("Scaffolded custom profile {name} at {out}\n"),
json!({"name": name, "path": out, "profile": scaffold}),
))
}
"validate" => {
let p = parse_args(rest, &["file"], &["json"])?;
let path = p
.get("file")
.or_else(|| p.positional(0))
.ok_or_else(|| CertError::usage("profile validate needs <file>"))?;
let (custom, resolved) = profile::load_custom_profile(&PathBuf::from(path))?;
let text = format!(
"Valid custom profile {} (key={}, validity={}d, ceiling={}d)\n",
custom.name,
resolved.key_algorithm.name(),
custom.default_validity_days,
custom.max_validity_days
);
Ok(emit(
json_out,
text,
json!({"valid": true, "profile": custom}),
))
}
other => Err(CertError::usage(format!(
"unknown profile verb '{other}' (list | show)"
"unknown profile verb '{other}' (list | show | validate | scaffold)"
))),
}
}
Expand Down Expand Up @@ -865,10 +910,6 @@ const NOT_BUILT: &[(&str, &str)] = &[
"key inspect/import/export/convert-public (spec §6.1)",
),
("format", "format list/describe/export/convert (spec §7.5)"),
(
"profile validate/scaffold",
"profile validation + scaffolding (spec §6.1)",
),
(
"algorithms benchmark/doctor",
"algorithm benchmarks (spec §6.1)",
Expand Down Expand Up @@ -984,15 +1025,15 @@ fn doctor_cmd(_args: &[String], json_out: bool) -> Result<i32> {
" openssl (independent validator): {}\n",
openssl.as_deref().unwrap_or("not found")
));
text.push_str(" built: root, issuer, leaf, chain, bundle (inspect), profile (list/show), algorithms (list/describe), explain, doctor\n");
text.push_str(" built: root, issuer, leaf, chain, bundle (inspect), profile (list/show/validate/scaffold), algorithms (list/describe), explain, doctor\n");
text.push_str(" not built yet (tracked as child issues under #9157):\n");
for (name, what) in NOT_BUILT {
text.push_str(&format!(" {name} — {what}\n"));
}
let value = json!({
"engine_self_test": algo_results.iter().map(|(i, ok)| json!({"algorithm": i.name, "ok": ok})).collect::<Vec<_>>(),
"openssl": openssl,
"built": ["root", "issuer", "leaf", "chain", "bundle-inspect", "profile-list/show", "algorithms-list/describe", "explain", "doctor"],
"built": ["root", "issuer", "leaf", "chain", "bundle-inspect", "profile-list/show/validate/scaffold", "algorithms-list/describe", "explain", "doctor"],
"not_built": NOT_BUILT.iter().map(|(n, w)| json!({"surface": n, "scope": w})).collect::<Vec<_>>(),
});
let code = emit(json_out, text, value);
Expand Down
25 changes: 21 additions & 4 deletions pkgs/fleet/crates/fleet-cert/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::model::{
ParentRef, RootCreateRequest,
};
use crate::naming::{GenerationPartition, slugify};
use crate::profile::leaf_profile;
use crate::timeparse::Rfc3339;
use rcgen::{
BasicConstraints, CertificateParams, DnType, DnValue, IsCa, Issuer, KeyPair, KeyUsagePurpose,
Expand Down Expand Up @@ -850,7 +849,7 @@ fn clamp_window(
/// `cert leaf issue`: issue one leaf from an issuer bundle, signed
/// by the selected issuer key loaded from `req.issuer_signer`.
pub fn issue_leaf(req: &LeafIssueRequest, out_dir: &Path) -> Result<BundleManifest> {
let profile = leaf_profile(&req.profile)?;
let profile = crate::profile::resolve_profile(&req.profile)?;
let issuer_bundle = load_bundle(&req.issuer_bundle)?;
if issuer_bundle.manifest.bundle_type != BundleType::IssuerSet {
return Err(CertError::usage(format!(
Expand All @@ -859,6 +858,18 @@ pub fn issue_leaf(req: &LeafIssueRequest, out_dir: &Path) -> Result<BundleManife
issuer_bundle.manifest.bundle_type.as_str()
)));
}
if profile.is_custom {
if let Some(requested) = req.key_algorithm {
if requested != profile.key_algorithm {
return Err(CertError::policy(format!(
"profile '{}' fixes key algorithm {}, not {}",
profile.name,
profile.key_algorithm.name(),
requested.name()
)));
}
}
}
let key_algo = req.key_algorithm.unwrap_or(profile.key_algorithm);
let issuer_partition = req
.issuer_partition
Expand Down Expand Up @@ -924,6 +935,12 @@ pub fn issue_leaf(req: &LeafIssueRequest, out_dir: &Path) -> Result<BundleManife
&req.name,
&mut warnings,
)?;
if na.0 - nb.0 > time::Duration::days(i64::from(profile.max_validity_days)) {
return Err(CertError::policy(format!(
"profile '{}' validity exceeds its {} day ceiling",
profile.name, profile.max_validity_days
)));
}

// Subject + SANs.
let mut sans: Vec<String> = req.dns_names.clone();
Expand All @@ -945,8 +962,8 @@ pub fn issue_leaf(req: &LeafIssueRequest, out_dir: &Path) -> Result<BundleManife
&cn,
);
params.is_ca = IsCa::ExplicitNoCa;
params.key_usages = profile.key_usages.to_vec();
params.extended_key_usages = profile.extended_key_usages.to_vec();
params.key_usages = profile.key_usages;
params.extended_key_usages = profile.extended_key_usages;
params.not_before = nb.0;
params.not_after = na.0;
params.serial_number = Some(random_serial());
Expand Down
Loading