Skip to content

Commit aae97c1

Browse files
committed
Add test for user data in HCL report
1 parent 1220af5 commit aae97c1

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

Cargo.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ az-tdx-vtpm = "0.7.4"
3030
serde = "1.0.228"
3131
base64 = "0.22.1"
3232
reqwest = { version = "0.12.23", default-features = false, features = ["rustls-tls-webpki-roots-no-provider"] }
33+
josekit = "0.10.3"
34+
# jwt-simple = "0.12.13"
3335

3436
[dev-dependencies]
3537
rcgen = "0.14.5"

src/attestation/azure.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use az_tdx_vtpm::{hcl, imds, report, vtpm};
22
use tokio_rustls::rustls::pki_types::CertificateDer;
33
// use openssl::pkey::{PKey, Public};
4-
use base64::prelude::*;
4+
use base64::{engine::general_purpose::URL_SAFE as BASE64_URL_SAFE, Engine as _};
55
use reqwest::Client;
66
use serde::Serialize;
77

@@ -58,8 +58,8 @@ impl QuoteGenerator for MaaGenerator {
5858
// let pub_key = PKey::public_key_from_der(&der).unwrap();
5959
// tpm_quote.verify(&pub_key, nonce).unwrap();
6060

61-
let quote_b64 = BASE64_STANDARD.encode(&td_quote_bytes);
62-
let runtime_b64 = BASE64_STANDARD.encode(hcl_var_data);
61+
let quote_b64 = BASE64_URL_SAFE.encode(&td_quote_bytes);
62+
let runtime_b64 = BASE64_URL_SAFE.encode(hcl_var_data);
6363

6464
let body = TdxVmRequest {
6565
quote: quote_b64,
@@ -123,11 +123,28 @@ impl QuoteVerifier for MaaVerifier {
123123
cert_chain: &[CertificateDer<'_>],
124124
exporter: [u8; 32],
125125
) -> Result<Option<super::measurements::Measurements>, AttestationError> {
126-
let input_data = compute_report_input(cert_chain, exporter)?;
126+
let _input_data = compute_report_input(cert_chain, exporter)?;
127+
let token = String::from_utf8(input).unwrap();
128+
129+
self.decode_jwt(&token).await.unwrap();
130+
127131
todo!()
128132
}
129133
}
130134

135+
impl MaaVerifier {
136+
async fn decode_jwt(&self, token: &str) -> Result<(), AttestationError> {
137+
// Parse payload (claims) without verification (TODO this will be swapped out once we have the
138+
// key-getting logic)
139+
let parts: Vec<&str> = token.split('.').collect();
140+
let claims_json = BASE64_URL_SAFE.decode(parts[1]).unwrap();
141+
142+
let claims: serde_json::Value = serde_json::from_slice(&claims_json).unwrap();
143+
println!("{claims}");
144+
Ok(())
145+
}
146+
}
147+
131148
#[derive(Serialize)]
132149
struct RuntimeData<'a> {
133150
data: String, // base64url of VarData bytes
@@ -143,3 +160,26 @@ struct TdxVmRequest<'a> {
143160
#[serde(skip_serializing_if = "Option::is_none")]
144161
nonce: Option<String>,
145162
}
163+
164+
#[cfg(test)]
165+
mod tests {
166+
use super::*;
167+
168+
#[tokio::test]
169+
async fn test_decode_hcl() {
170+
// from cvm-reverse-proxy/internal/attestation/azure/tdx/testdata/hclreport.bin
171+
let hcl_bytes: &'static [u8] = include_bytes!("../../test-assets/hclreport.bin");
172+
173+
let hcl_report = hcl::HclReport::new(hcl_bytes.to_vec()).unwrap();
174+
let hcl_var_data = hcl_report.var_data();
175+
let var_data_values: serde_json::Value = serde_json::from_slice(&hcl_var_data).unwrap();
176+
177+
// Check that it contains 64 byte user data
178+
assert_eq!(
179+
hex::decode(var_data_values["user-data"].as_str().unwrap())
180+
.unwrap()
181+
.len(),
182+
64
183+
);
184+
}
185+
}

0 commit comments

Comments
 (0)