Skip to content

Commit ceaca4a

Browse files
committed
verifier-cli: Replace --persist w/ --work-dir in verify command.
Allowing the caller to provide the path makes scripting this much easier.
1 parent 1d784b8 commit ceaca4a

File tree

1 file changed

+80
-74
lines changed

1 file changed

+80
-74
lines changed

verifier-cli/src/main.rs

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,16 @@ enum AttestCommand {
9595
)]
9696
ca_cert: Option<PathBuf>,
9797

98-
/// Preserve temporary / intermediate files. The path to the
99-
/// temp directory will be written to stderr.
100-
#[clap(long, env)]
101-
persist: bool,
102-
10398
/// Verify the final cert in the provided PkiPath against itself.
10499
#[clap(long, env, conflicts_with = "ca_cert")]
105100
self_signed: bool,
101+
102+
/// Caller provided directory where artifacts are stored. If this
103+
/// option is provided it will be used by this tool to store
104+
/// artifacts retrieved from the RoT as part of the attestation
105+
/// process. If omitted a temp directory will be used instead.
106+
#[clap(long, env = "VERIFIER_CLI_WORK_DIR")]
107+
work_dir: Option<PathBuf>,
106108
},
107109
/// Verify signature over Attestation
108110
VerifyAttestation {
@@ -508,78 +510,18 @@ fn main() -> Result<()> {
508510
}
509511
AttestCommand::Verify {
510512
ca_cert,
511-
persist,
512513
self_signed,
514+
work_dir,
513515
} => {
514-
// generate nonce from RNG
515-
info!("getting Nonce from platform RNG");
516-
let nonce = Nonce::from_platform_rng()?;
517-
518-
// make tempdir, write nonce to temp dir
519-
let tmp_dir = tempfile::tempdir()?;
520-
let nonce_path = tmp_dir.path().join("nonce.bin");
521-
info!("writing nonce to: {}", nonce_path.display());
522-
fs::write(&nonce_path, nonce)?;
523-
524-
// get attestation
525-
info!("getting attestation");
526-
let mut attestation = vec![0u8; attest.attest_len()? as usize];
527-
attest.attest(nonce, &mut attestation)?;
528-
let attestation_path = tmp_dir.path().join("attest.bin");
529-
info!("writing attestation to: {}", attestation_path.display());
530-
fs::write(&attestation_path, &attestation)?;
531-
532-
// get log
533-
info!("getting measurement log");
534-
let mut log = vec![0u8; attest.log_len()? as usize];
535-
attest.log(&mut log)?;
536-
let log_path = tmp_dir.path().join("log.bin");
537-
info!("writing measurement log to: {}", log_path.display());
538-
fs::write(&log_path, &log)?;
539-
540-
// get cert chain
541-
info!("getting cert chain");
542-
let cert_chain_path = tmp_dir.path().join("cert-chain.pem");
543-
let mut cert_chain = File::create(&cert_chain_path)?;
544-
let alias_cert_path = tmp_dir.path().join("alias.pem");
545-
for index in 0..attest.cert_chain_len()? {
546-
let encoding = Encoding::Pem;
547-
info!("getting cert[{}] encoded as {}", index, encoding);
548-
let cert = get_cert(&attest, encoding, index)?;
549-
550-
// the first cert in the chain / the leaf cert is the one
551-
// used to sign attestations
552-
if index == 0 {
553-
info!(
554-
"writing alias cert to: {}",
555-
alias_cert_path.display()
556-
);
557-
fs::write(&alias_cert_path, &cert)?;
516+
// Use the directory provided by the caller to hold intermediate
517+
// files, or fall back to a temp dir.
518+
match work_dir {
519+
Some(w) => verify(&attest, &ca_cert, self_signed, w)?,
520+
None => {
521+
let work_dir = tempfile::tempdir()?;
522+
verify(&attest, &ca_cert, self_signed, work_dir)?
558523
}
559-
560-
info!(
561-
"writing cert[{}] to: {}",
562-
index,
563-
cert_chain_path.display()
564-
);
565-
cert_chain.write_all(&cert)?;
566-
}
567-
568-
verify_attestation(
569-
&alias_cert_path,
570-
&attestation_path,
571-
&log_path,
572-
&nonce_path,
573-
)?;
574-
info!("attestation verified");
575-
verify_cert_chain(&ca_cert, &cert_chain_path, self_signed)?;
576-
info!("cert chain verified");
577-
578-
// persist the temp dir and write path to stderr if requested
579-
if persist {
580-
let tmp_path = tmp_dir.into_path();
581-
eprintln!("{}", tmp_path.display());
582-
}
524+
};
583525
}
584526
AttestCommand::VerifyAttestation {
585527
alias_cert,
@@ -601,6 +543,70 @@ fn main() -> Result<()> {
601543
Ok(())
602544
}
603545

546+
fn verify<P: AsRef<Path>>(
547+
attest: &AttestHiffy,
548+
ca_cert: &Option<PathBuf>,
549+
self_signed: bool,
550+
work_dir: P,
551+
) -> Result<()> {
552+
// generate nonce from RNG
553+
info!("getting Nonce from platform RNG");
554+
let nonce = Nonce::from_platform_rng()?;
555+
556+
// write nonce to temp dir
557+
let nonce_path = work_dir.as_ref().join("nonce.bin");
558+
info!("writing nonce to: {}", nonce_path.display());
559+
fs::write(&nonce_path, nonce)?;
560+
561+
// get attestation
562+
info!("getting attestation");
563+
let mut attestation = vec![0u8; attest.attest_len()? as usize];
564+
attest.attest(nonce, &mut attestation)?;
565+
let attestation_path = work_dir.as_ref().join("attest.bin");
566+
info!("writing attestation to: {}", attestation_path.display());
567+
fs::write(&attestation_path, &attestation)?;
568+
569+
// get log
570+
info!("getting measurement log");
571+
let mut log = vec![0u8; attest.log_len()? as usize];
572+
attest.log(&mut log)?;
573+
let log_path = work_dir.as_ref().join("log.bin");
574+
info!("writing measurement log to: {}", log_path.display());
575+
fs::write(&log_path, &log)?;
576+
577+
// get cert chain
578+
info!("getting cert chain");
579+
let cert_chain_path = work_dir.as_ref().join("cert-chain.pem");
580+
let mut cert_chain = File::create(&cert_chain_path)?;
581+
let alias_cert_path = work_dir.as_ref().join("alias.pem");
582+
for index in 0..attest.cert_chain_len()? {
583+
let encoding = Encoding::Pem;
584+
info!("getting cert[{}] encoded as {}", index, encoding);
585+
let cert = get_cert(attest, encoding, index)?;
586+
587+
// the first cert in the chain / the leaf cert is the one
588+
// used to sign attestations
589+
if index == 0 {
590+
info!("writing alias cert to: {}", alias_cert_path.display());
591+
fs::write(&alias_cert_path, &cert)?;
592+
}
593+
594+
info!("writing cert[{}] to: {}", index, cert_chain_path.display());
595+
cert_chain.write_all(&cert)?;
596+
}
597+
598+
verify_attestation(
599+
&alias_cert_path,
600+
&attestation_path,
601+
&log_path,
602+
&nonce_path,
603+
)?;
604+
info!("attestation verified");
605+
verify_cert_chain(ca_cert, &cert_chain_path, self_signed)?;
606+
info!("cert chain verified");
607+
Ok(())
608+
}
609+
604610
fn verify_attestation(
605611
alias_cert: &PathBuf,
606612
attestation: &PathBuf,

0 commit comments

Comments
 (0)