Skip to content

Commit

Permalink
verifier-az-cvm-vtpm: Fix tests to updated library
Browse files Browse the repository at this point in the history
This commit updates the test fixtures, way to load quote and way to mess
with quote for negative tests.

Signed-off-by: Suraj Deshmukh <[email protected]>
  • Loading branch information
surajssd committed Jan 31, 2024
1 parent a37469f commit b2e412c
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 73 deletions.
68 changes: 38 additions & 30 deletions attestation-service/verifier/src/az_snp_vtpm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ fn verify_snp_report(
#[cfg(test)]
mod tests {
use super::*;
use az_snp_vtpm::vtpm::VerifyError;

const REPORT: &[u8; 2048] = include_bytes!("../../test_data/az-snp-vtpm/hcl-report.bin");
const SIGNATURE: &[u8; 256] = include_bytes!("../../test_data/az-snp-vtpm/tpm-quote.sig");
const MESSAGE: &[u8; 122] = include_bytes!("../../test_data/az-snp-vtpm/tpm-quote.msg");
const REPORT: &[u8; 2600] = include_bytes!("../../test_data/az-snp-vtpm/hcl-report.bin");
const QUOTE: &[u8; 1362] = include_bytes!("../../test_data/az-snp-vtpm/quote.bin");
const REPORT_DATA: &[u8] = "challenge".as_bytes();

#[test]
Expand All @@ -152,12 +152,17 @@ mod tests {
fn test_verify_snp_report_failure() {
let mut wrong_report = REPORT.clone();
// messing with snp report
wrong_report[0x00b0] = 0;
wrong_report[0x01a6] = 0;
let hcl_report = HclReport::new(wrong_report.to_vec()).unwrap();
let snp_report = hcl_report.try_into().unwrap();
let vcek = Vcek::from_pem(include_str!("../../test_data/az-snp-vtpm/vcek.pem")).unwrap();
let vendor_certs = load_milan_cert_chain().as_ref().unwrap();
verify_snp_report(&snp_report, &vcek, vendor_certs).unwrap_err();
assert_eq!(
verify_snp_report(&snp_report, &vcek, vendor_certs)
.unwrap_err()
.to_string(),
"SNP version mismatch",
);
}

#[test]
Expand All @@ -175,59 +180,62 @@ mod tests {
let hcl_report = HclReport::new(wrong_report.to_vec()).unwrap();
let var_data_hash = hcl_report.var_data_sha256();
let snp_report = hcl_report.try_into().unwrap();
verify_report_data(&var_data_hash, &snp_report).unwrap_err();
assert_eq!(
verify_report_data(&var_data_hash, &snp_report)
.unwrap_err()
.to_string(),
"SNP report report_data mismatch"
);
}

#[test]
fn test_verify_signature() {
let quote = Quote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
verify_signature(&quote, &hcl_report).unwrap();
}

#[test]
fn test_verify_quote_signature_failure() {
let mut wrong_message = MESSAGE.clone();
wrong_message.reverse();
let wrong_quote = Quote {
signature: SIGNATURE.to_vec(),
message: wrong_message.to_vec(),
};
let mut quote = QUOTE.clone();
quote[0x030] = 0;
let wrong_quote: Quote = bincode::deserialize(&quote).unwrap();

let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
verify_signature(&wrong_quote, &hcl_report).unwrap_err();
assert_eq!(
verify_signature(&wrong_quote, &hcl_report)
.unwrap_err()
.downcast_ref::<VerifyError>()
.unwrap()
.to_string(),
VerifyError::SignatureMismatch.to_string()
);
}

#[test]
fn test_verify_akpub_failure() {
let quote = Quote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let mut wrong_report = REPORT.clone();
// messing with AKpub in var data
wrong_report[0x0540] = 0;
let wrong_hcl_report = HclReport::new(wrong_report.to_vec()).unwrap();
verify_signature(&quote, &wrong_hcl_report).unwrap_err();
assert_eq!(
verify_signature(&quote, &wrong_hcl_report)
.unwrap_err()
.to_string(),
"Failed to get AKpub",
);
}

#[test]
fn test_verify_quote_nonce() {
let quote = Quote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
verify_nonce(&quote, &REPORT_DATA).unwrap();
}

#[test]
fn test_verify_quote_nonce_failure() {
let quote = Quote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let mut wrong_report_data = REPORT_DATA.to_vec();
wrong_report_data.reverse();
verify_nonce(&quote, &wrong_report_data).unwrap_err();
Expand Down
48 changes: 25 additions & 23 deletions attestation-service/verifier/src/az_tdx_vtpm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ fn verify_tpm_nonce(quote: &TpmQuote, report_data: &[u8]) -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use az_tdx_vtpm::vtpm::Quote;
use az_tdx_vtpm::vtpm::VerifyError;

const REPORT: &[u8; 2600] = include_bytes!("../../test_data/az-tdx-vtpm/hcl-report.bin");
const SIGNATURE: &[u8; 256] = include_bytes!("../../test_data/az-tdx-vtpm/tpm-quote.sig");
const MESSAGE: &[u8; 126] = include_bytes!("../../test_data/az-tdx-vtpm/tpm-quote.msg");
const QUOTE: &[u8; 1362] = include_bytes!("../../test_data/az-tdx-vtpm/quote.bin");
const TD_QUOTE: &[u8; 5006] = include_bytes!("../../test_data/az-tdx-vtpm/td-quote.bin");

#[test]
Expand All @@ -115,47 +116,48 @@ mod tests {
wrong_report[0x0880] += 1;
let hcl_report = HclReport::new(wrong_report.to_vec()).unwrap();
let td_quote = parse_tdx_quote(TD_QUOTE).unwrap();
verify_hcl_var_data(&hcl_report, &td_quote).unwrap_err();
assert_eq!(
verify_hcl_var_data(&hcl_report, &td_quote)
.unwrap_err()
.to_string(),
"TDX Quote report data mismatch"
);
}

#[test]
fn test_verify_tpm_signature() {
let quote = TpmQuote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
verify_tpm_signature(&quote, &hcl_report).unwrap();
}

#[test]
fn test_verify_tpm_signature_failure() {
let mut wrong_message = MESSAGE.clone();
wrong_message.reverse();
let wrong_quote = TpmQuote {
signature: SIGNATURE.to_vec(),
message: wrong_message.to_vec(),
};
let mut quote = QUOTE.clone();
quote[0x020] = 0;
let wrong_quote: Quote = bincode::deserialize(&quote).unwrap();

let hcl_report = HclReport::new(REPORT.to_vec()).unwrap();
verify_tpm_signature(&wrong_quote, &hcl_report).unwrap_err();
assert_eq!(
verify_tpm_signature(&wrong_quote, &hcl_report)
.unwrap_err()
.downcast_ref::<VerifyError>()
.unwrap()
.to_string(),
VerifyError::SignatureMismatch.to_string()
);
}

#[test]
fn test_verify_tpm_nonce() {
let quote = TpmQuote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let nonce = "tdx challenge".as_bytes();
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let nonce = "challenge".as_bytes();
verify_tpm_nonce(&quote, nonce).unwrap();
}

#[test]
fn test_verify_tpm_nonce_failure() {
let quote = TpmQuote {
signature: SIGNATURE.to_vec(),
message: MESSAGE.to_vec(),
};
let quote: Quote = bincode::deserialize(QUOTE).unwrap();
let wrong_nonce = "wrong".as_bytes();
verify_tpm_nonce(&quote, wrong_nonce).unwrap_err();
}
Expand Down
Binary file modified attestation-service/verifier/test_data/az-snp-vtpm/hcl-report.bin
Binary file not shown.
Binary file not shown.
40 changes: 20 additions & 20 deletions attestation-service/verifier/test_data/az-snp-vtpm/vcek.pem
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ MIIFTDCCAvugAwIBAgIBADBGBgkqhkiG9w0BAQowOaAPMA0GCWCGSAFlAwQCAgUA
oRwwGgYJKoZIhvcNAQEIMA0GCWCGSAFlAwQCAgUAogMCATCjAwIBATB7MRQwEgYD
VQQLDAtFbmdpbmVlcmluZzELMAkGA1UEBhMCVVMxFDASBgNVBAcMC1NhbnRhIENs
YXJhMQswCQYDVQQIDAJDQTEfMB0GA1UECgwWQWR2YW5jZWQgTWljcm8gRGV2aWNl
czESMBAGA1UEAwwJU0VWLU1pbGFuMB4XDTIzMDEyNDE3NTgyNloXDTMwMDEyNDE3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
nQNL1O7/n8M3K6OYa1SYFubU1tXJjRL3Bzx/rcL1w6sGQIeXwDXNjxyPO433YbIW
oQ6VuUj5AeTLlp3hChfrQ0Nkj3DbfsmDmSL4F0vsSq629YxQ6XTT+tIT897iMMVI
IMUeJ4O7wxcMnoKoNbM+dTmKrkDm4/FdqYpsT2ziPD9FcTcMud0qZGy8YmUCa9cV
/g7xKrDfRe8A8FDe1iFAVJ62xhS7GiOx/F2Gd/oEHto9dwE4/OVRmZlFy1njlxZt
3Dd6W8z0TLmiyHngApx8VMJHNuf4+UeiDYkWo14ZzGlsMKHweGQhy/bRbkIpjNB/
fUZsn404ZxV8+mbTdeMftIUAofDHryPq2/K4FZB6IucEil9S/dejUerlAlDn+uq/
TT/FatsAv8gMnTHNB2/GRDRuiLf5bqRGXFRelV8K0edfoxxvW8JjiK2h6bsQf4aA
JPH53P1GFWvEu9zOsGTldzOIvcPdykMFxhssRryI4YTTwAg6BPpXM83kdnGCEPgU
pNO5zaEImuqtrxdmOZt8qGxyQ2DYgwS0QC8srEIxBtM9eBSJAodvgUiovwACF4+w
5CU3rQt0CzyWLJop7k8GMw==
-----END CERTIFICATE-----
Binary file not shown.
Binary file not shown.
Binary file modified attestation-service/verifier/test_data/az-tdx-vtpm/td-quote.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit b2e412c

Please sign in to comment.