11use az_tdx_vtpm:: { hcl, imds, report, vtpm} ;
22use 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 _ } ;
55use reqwest:: Client ;
66use 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 ) ]
132149struct 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