Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor verifier #128

Merged
merged 2 commits into from
Nov 30, 2023
Merged
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
124 changes: 42 additions & 82 deletions verifier/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use env_logger::Builder;
use log::{debug, LevelFilter};
use std::{
fmt::{self, Debug, Formatter},
process::Command,
process::{Command, Output},
};

/// Execute HIF operations exposed by the RoT Attest task.
Expand Down Expand Up @@ -48,11 +48,7 @@ enum AttestCommand {
index: u32,
},
/// Get the log of measurements recorded by the RoT.
Log {
/// Output format for Log structure.
#[clap(long, env)]
form: Form,
},
Log,
/// Get the length in bytes of the Log.
LogLen,
/// Get an attestation.
Expand Down Expand Up @@ -82,20 +78,6 @@ impl fmt::Display for Interface {
}
}

/// An enum of the supported output format for commands that return complex
/// types.
#[derive(Clone, Debug, ValueEnum)]
enum Form {
Bin,
Text,
}

impl fmt::Display for Form {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Debug::fmt(self, f)
}
}

/// A type to simplify the execution of the HIF operations exposed by the RoT
/// Attest task.
struct AttestHiffy {
Expand All @@ -116,27 +98,41 @@ impl AttestHiffy {
/// to be decimal. Currently this function ignores the interface and
/// operation names from the string. Future work may check that these are
/// consistent with the operation executed.
fn u32_from_stdout(output: &[u8]) -> Result<u32> {
// check interface & operation name?
let output = String::from_utf8_lossy(output);
let output: Vec<&str> = output.trim().split(' ').collect();
let output = output[output.len() - 1];
debug!("output: {}", output);

let (output, radix) = match output.strip_prefix("0x") {
Some(s) => {
debug!("prefix stripped: \"{}\"", s);
(s, 16)
}
None => (output, 10),
};
let log_len = u32::from_str_radix(output, 16).with_context(|| {
format!("Failed to parse \"{}\" as base {} u32", output, radix)
})?;

debug!("output u32: {}", log_len);

Ok(log_len)
fn u32_from_cmd_output(output: Output) -> Result<u32> {
if output.status.success() {
// check interface & operation name?
let output = String::from_utf8_lossy(&output.stdout);
let output: Vec<&str> = output.trim().split(' ').collect();
let output = output[output.len() - 1];
debug!("output: {}", output);

let (output, radix) = match output.strip_prefix("0x") {
Some(s) => {
debug!("prefix stripped: \"{}\"", s);
(s, 16)
}
None => (output, 10),
};

let log_len =
u32::from_str_radix(output, 16).with_context(|| {
format!(
"Failed to parse \"{}\" as base {} u32",
output, radix
)
})?;

debug!("output u32: {}", log_len);

Ok(log_len)
} else {
Err(anyhow!(
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
))
}
}

/// Get length of the certificate chain from the Attest task. This cert
Expand All @@ -153,16 +149,7 @@ impl AttestHiffy {
debug!("executing command: {:?}", cmd);

let output = cmd.output()?;
if output.status.success() {
Self::u32_from_stdout(&output.stdout)
} else {
Err(anyhow!(
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
))
}
Self::u32_from_cmd_output(output)
}

/// Get length of the certificate at the provided index in bytes.
Expand All @@ -177,16 +164,7 @@ impl AttestHiffy {
debug!("executing command: {:?}", cmd);

let output = cmd.output()?;
if output.status.success() {
Self::u32_from_stdout(&output.stdout)
} else {
Err(anyhow!(
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
))
}
Self::u32_from_cmd_output(output)
}

/// Get length of the measurement log in bytes.
Expand All @@ -199,16 +177,7 @@ impl AttestHiffy {
debug!("executing command: {:?}", cmd);

let output = cmd.output()?;
if output.status.success() {
Self::u32_from_stdout(&output.stdout)
} else {
Err(anyhow!(
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
))
}
Self::u32_from_cmd_output(output)
}

/// Get length of the measurement log in bytes.
Expand All @@ -221,16 +190,7 @@ impl AttestHiffy {
debug!("executing command: {:?}", cmd);

let output = cmd.output()?;
if output.status.success() {
Self::u32_from_stdout(&output.stdout)
} else {
Err(anyhow!(
"command failed with status: {}\nstdout: \"{}\"\nstderr: \"{}\"",
output.status,
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
))
}
Self::u32_from_cmd_output(output)
}
}

Expand All @@ -256,7 +216,7 @@ fn main() -> Result<()> {
AttestCommand::CertLen { index } => {
println!("{}", attest.cert_len(index)?)
}
AttestCommand::Log { form } => todo!("AttestCommand::Log: {}", form),
AttestCommand::Log => todo!("AttestCommand::Log"),
AttestCommand::LogLen => println!("{}", attest.log_len()?),
AttestCommand::Quote => todo!("AttestCommand::Quote"),
AttestCommand::QuoteLen => println!("{}", attest.quote_len()?),
Expand Down
Loading