From 9df7897471359f96db6e31395c77f9466d33d75c Mon Sep 17 00:00:00 2001 From: arvindskumar99 Date: Mon, 29 Apr 2024 16:10:58 +0000 Subject: [PATCH] pushing measure --- Cargo.lock | 10 +++ Cargo.toml | 2 +- src/main.rs | 10 ++- src/measure.rs | 179 +++++++++++++++++++------------------------------ 4 files changed, 83 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5eef1a4..5c5ceea 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,6 +225,15 @@ dependencies = [ "clap_derive", ] +[[package]] +name = "clap-num" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e063d263364859dc54fb064cedb7c122740cd4733644b14b176c097f51e8ab7" +dependencies = [ + "num-traits", +] + [[package]] name = "clap_builder" version = "4.4.18" @@ -1363,6 +1372,7 @@ dependencies = [ "bincode", "bitfield 0.13.2", "clap", + "clap-num", "colorful", "env_logger", "hex", diff --git a/Cargo.toml b/Cargo.toml index ec2928a..63de674 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ hyperv = ["tss-esapi"] [dependencies] clap = { version = "<4.5", features = [ "derive" ] } +clap-num = "=1.1.1" env_logger = "0.10.0" anyhow = "1.0.69" sev = { version = "^3.1.1", default-features = false, features = ['openssl','snp']} @@ -37,4 +38,3 @@ tss-esapi = { version = "7.2", optional=true } msru = "0.2.0" colorful = "0.2.2" bitfield = "0.13.2" -clap = { version = "4.5.4", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index d73ce16..3951d91 100755 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,10 @@ mod certs; mod display; mod fetch; mod key; +mod measure; mod ok; mod report; mod verify; -mod measure; #[cfg(feature = "hyperv")] mod hyperv; @@ -17,9 +17,9 @@ use certs::CertificatesArgs; use display::DisplayCmd; use fetch::FetchCmd; use key::KeyArgs; +use measure::MeasureArgs; use report::ReportArgs; use verify::VerifyCmd; -use measure::MeasureArgs; use anyhow::{Context, Result}; use clap::{arg, Parser, Subcommand, ValueEnum}; @@ -64,10 +64,8 @@ enum SnpGuestCmd { #[command(subcommand)] Ok, - #[structopt(about = "Use the guest's attributes to create a pre calculated measurement.")] + /// Key command to create a pre calculated measurement. Measure(MeasureArgs), - - } fn main() -> Result<()> { @@ -89,7 +87,7 @@ fn main() -> Result<()> { SnpGuestCmd::Display(subcmd) => display::cmd(subcmd, snpguest.quiet), SnpGuestCmd::Key(args) => key::get_derived_key(args), SnpGuestCmd::Ok => ok::cmd(snpguest.quiet), - SnpGuestCmd::Measure(args) => measure::cmd(args), + SnpGuestCmd::Measure(args) => measure::get_measurement(args), }; if let Err(ref e) = status { diff --git a/src/measure.rs b/src/measure.rs index 204dc47..068d2e4 100755 --- a/src/measure.rs +++ b/src/measure.rs @@ -1,121 +1,78 @@ use super::*; -use sev::measurement::{self, gctx, ovmf, sev_hashes, snp::{self, SnpMeasurementArgs}, vcpu_types, vmsa}; -use std::{fmt, str::FromStr}; use anyhow::{anyhow, Result}; - - -use serde::{Deserialize, Serialize}; -use structopt::StructOpt; -use std::path::{Path, PathBuf}; -use clap::{Args, Parser, builder, Subcommand}; - - -#[derive(Parser, Debug, , Subcommand)] -#[command(author, version, about, long_about = None)] -pub struct MeasureArgs{ - -#[arg(short, long, help = "Kernel command line to calculate hash from (use with --kernel)")] -pub append: u64, - - +use sev::measurement::{self, snp::SnpMeasurementArgs, vcpu_types, vmsa}; +use std::str::FromStr; + +use clap::{builder, Parser}; +use clap_num::maybe_hex; +use std::path::PathBuf; + +#[derive(Parser, Debug)] +pub struct MeasureArgs { + ///Kernel command line to calculate hash from (use with --kernel) + #[arg(short, long, value_name = "append")] + pub append: String, + + /// {snp,snp:ovmf-hash,snp:svsm} + #[arg(short, long, value_name = "mode")] + pub mode: String, + + /// Number of guest vcpus + #[arg(long)] + pub vcpus: u32, + + ///Type of guest vcpu (EPYC, EPYC-v1, EPYC-v2, EPYC-IBPB, EPYC-v3, EPYC-v4, + /// EPYC-Rome, EPYC-Rome-v1, EPYC-Rome-v2, EPYC-Rome-v3, EPYC-Milan, EPYC- + /// Milan-v1, EPYC-Milan-v2, EPYC-Genoa, EPYC-Genoa-v1) + #[arg(short, long, value_name = "vcpu-type")] + pub vcpu_type: String, + + /// Type of guest vmm (1 = QEMU, 2 = ec2, 3 = KRUN) + #[arg(long, value_name = "vmm-type")] + pub vmm_type: u8, + + /// OVMF file to calculate hash from + #[arg(short, long, value_name = "ovmf-path")] + pub ovmf_path: PathBuf, + + /// Precalculated hash of the OVMF binary (hex string) + #[arg(short, long, value_name = "snp-ovmf-hash")] + pub snp_ovmf_hash: String, + + /// Initrd file to calculate hash from (use with --kernel) + #[arg(short, long, value_name = "initrd")] + pub initrd: Option, + + /// Kernel file to calculate hash from + #[arg(short, long, requires = "append", requires = "initrd", value_name = "kernel")] + pub kernel: Option, + + /// Hex representation of the guest kernel features expected to be included + #[arg(short, long, value_name = "guest-features", default_value_t = 1u64, value_parser=maybe_hex::)] + pub guest_features: u64, } +pub fn get_measurement(args: MeasureArgs) -> Result<()> { + // println!("{:?}", args); + let vcpu_type = vcpu_types::CpuType::from_str(args.vcpu_type.as_str())?; -pub fn cmd(args: MeasureArgs) -> Result<()>{ - let mut args = MeasureArgs::parse(); - println!("{:?}", args); - - // let vcpu_type = vcpu_types::CpuType::from_str(args.vcpu_type.as_str())?; - - - - // let collected_args:SnpMeasurementArgs = measurement::snp+:SnpMeasurementArgs { - // vcpus: args.vcpus, - // vcpu_type: args.vcpu_type, - // ovmf_file: args.ovmf_path, - // guest_features: vmsa::GuestFeatures(args.guest_features), - // kernel_file: args.kernel, - // initrd_file: args.initrd, - // append: Some(&*args.append), - // ovmf_hash_str: Some(args.snp_ovmf_hash.as_str()), - // vmm_type: Some(measurement::vmsa::VMMType::try_from(args.vmm_type)?) - // }; - + let collected_args: SnpMeasurementArgs = measurement::snp::SnpMeasurementArgs { + vcpus: args.vcpus, + vcpu_type: vcpu_type.to_string(), + ovmf_file: args.ovmf_path, + guest_features: vmsa::GuestFeatures(args.guest_features), + kernel_file: args.kernel, + initrd_file: args.initrd, + append: Some(&*args.append), + ovmf_hash_str: Some(args.snp_ovmf_hash.as_str()), + vmm_type: Some(measurement::vmsa::VMMType::try_from(args.vmm_type)?), + }; - // match sev::measurement::snp::snp_calc_launch_digest(collected_args) { - // Ok(ld) => println!("{:?}", ld), - // Err(_) => return Err(anyhow!("Error calculating the measurement.")) - // }; + match sev::measurement::snp::snp_calc_launch_digest(collected_args) { + Ok(ld) => println!("{:?}", ld), + Err(_) => return Err(anyhow!("Error calculating the measurement.")), + }; Ok(()) - - - - } - - - - - - - -// #[clap(long, help = "Show program's version number and exit")] -// pub version: String, - -// #[arg(long, help = "{snp,snp:ovmf-hash,snp:svsm}", value_parser = builder::PossibleValuesParser::new(["snp", "snp:ovmf-hash", "snp:svsm"]) )] -// pub mode: String, - -// #[arg(long, help = "Number of guest vcpus")] -// pub vcpus: u32, - -// #[arg(long, help = "Type of guest vcpu (EPYC, EPYC-v1, EPYC-v2, EPYC-IBPB, EPYC-v3, EPYC-v4, -// EPYC-Rome, EPYC-Rome-v1, EPYC-Rome-v2, EPYC-Rome-v3, EPYC-Milan, EPYC- -// Milan-v1, EPYC-Milan-v2, EPYC-Genoa, EPYC-Genoa-v1)")] -// pub vcpu_type: String, - -// // #[arg(long, help = "Guest vcpu signature value")] -// // pub vcpu_sig: String, - -// // #[arg(long, help = "Guest vcpu family")] -// // pub vcpu_family: String, - -// // #[arg(long, help = "Guest vcpu model")] -// // pub vcpu_model: String, - -// #[arg(long, help = "Type of guest vmm (1 = QEMU, 2 = ec2, 3 = KRUN)", value_parser = builder::PossibleValuesParser::new(["1", "2", "3"]))] -// pub vmm_type: u8, - -// #[arg(long, help = "OVMF file to calculate hash from")] -// pub ovmf_path: PathBuf, - -// #[arg(long, help = "Hex representation of the guest kernel features expected to be included -// (defaults to 0x1); see README.md for possible values", default_value = "0x1")] -// pub guest_features: u64, - -// // #[arg(long, help = "Measurement output format {hex, base64}")] -// // pub output_format: String, - -// #[arg(long, help = "Precalculated hash of the OVMF binary (hex string)", value_parser)] -// pub snp_ovmf_hash: String, - -// // #[arg(long, help = "Write measured VMSAs to +vmsa.bin (seves, snp, and snp:svsm modes only)")] -// // pub dump_vmsa: String, - -// // #[arg(long, help = "SVSM binary", required_if = ("mode", "snp:svsm"))] -// // pub svsm_path: Option, - -// // #[arg(long, help = "Size of the OVMF_VARS file in bytes (conflicts with --vars-file)", -// // required_if = ("mode", "snp:svsm"))] -// // pub vars_size: u64, - -// // #[arg(long, help = "OVMF_VARS file (conflicts with --vars-size)", required_if = ("mode", "snp:svsm"))] -// // pub vars_file: Option -// #[arg(long, help = "Initrd file to calculate hash from (use with --kernel)")] -// pub initrd: Option, - - -// #[arg(long, help = "Kernel file to calculate hash from", requires = ("append"), requires = ("initrd"))] -// pub kernel: Option, -