1+ pub mod measurements;
2+
3+ use measurements:: { CvmImageMeasurements , Measurements , PlatformMeasurements } ;
14use std:: {
2- collections:: HashMap ,
35 fmt:: { self , Display , Formatter } ,
46 time:: SystemTimeError ,
57} ;
@@ -9,7 +11,6 @@ use dcap_qvl::{
911 collateral:: get_collateral_for_fmspc,
1012 quote:: { Quote , Report } ,
1113} ;
12- use http:: { header:: InvalidHeaderValue , HeaderValue } ;
1314use sha2:: { Digest , Sha256 } ;
1415use tdx_quote:: QuoteParseError ;
1516use thiserror:: Error ;
@@ -19,66 +20,6 @@ use x509_parser::prelude::*;
1920/// For fetching collateral directly from intel, if no PCCS is specified
2021const PCS_URL : & str = "https://api.trustedservices.intel.com" ;
2122
22- #[ derive( Debug , Clone , PartialEq ) ]
23- pub struct Measurements {
24- pub platform : PlatformMeasurements ,
25- pub cvm_image : CvmImageMeasurements ,
26- }
27-
28- impl Measurements {
29- pub fn to_header_format ( & self ) -> Result < HeaderValue , MeasurementFormatError > {
30- let mut measurements_map = HashMap :: new ( ) ;
31- measurements_map. insert ( 0 , hex:: encode ( self . platform . mrtd ) ) ;
32- measurements_map. insert ( 1 , hex:: encode ( self . platform . rtmr0 ) ) ;
33- measurements_map. insert ( 2 , hex:: encode ( self . cvm_image . rtmr1 ) ) ;
34- measurements_map. insert ( 3 , hex:: encode ( self . cvm_image . rtmr2 ) ) ;
35- measurements_map. insert ( 4 , hex:: encode ( self . cvm_image . rtmr3 ) ) ;
36- Ok ( HeaderValue :: from_str ( & serde_json:: to_string (
37- & measurements_map,
38- ) ?) ?)
39- }
40-
41- pub fn from_header_format ( input : & str ) -> Result < Self , MeasurementFormatError > {
42- let measurements_map: HashMap < u32 , String > = serde_json:: from_str ( input) ?;
43- let measurements_map: HashMap < u32 , [ u8 ; 48 ] > = measurements_map
44- . into_iter ( )
45- . map ( |( k, v) | ( k, hex:: decode ( v) . unwrap ( ) . try_into ( ) . unwrap ( ) ) )
46- . collect ( ) ;
47-
48- Ok ( Self {
49- platform : PlatformMeasurements {
50- mrtd : * measurements_map
51- . get ( & 0 )
52- . ok_or ( MeasurementFormatError :: MissingValue ( "MRTD" . to_string ( ) ) ) ?,
53- rtmr0 : * measurements_map
54- . get ( & 1 )
55- . ok_or ( MeasurementFormatError :: MissingValue ( "RTMR0" . to_string ( ) ) ) ?,
56- } ,
57- cvm_image : CvmImageMeasurements {
58- rtmr1 : * measurements_map
59- . get ( & 2 )
60- . ok_or ( MeasurementFormatError :: MissingValue ( "RTMR1" . to_string ( ) ) ) ?,
61- rtmr2 : * measurements_map
62- . get ( & 3 )
63- . ok_or ( MeasurementFormatError :: MissingValue ( "RTMR2" . to_string ( ) ) ) ?,
64- rtmr3 : * measurements_map
65- . get ( & 4 )
66- . ok_or ( MeasurementFormatError :: MissingValue ( "RTMR3" . to_string ( ) ) ) ?,
67- } ,
68- } )
69- }
70- }
71-
72- #[ derive( Error , Debug ) ]
73- pub enum MeasurementFormatError {
74- #[ error( "JSON: {0}" ) ]
75- Json ( #[ from] serde_json:: Error ) ,
76- #[ error( "Missing value: {0}" ) ]
77- MissingValue ( String ) ,
78- #[ error( "Invalid header value: {0}" ) ]
79- BadHeaderValue ( #[ from] InvalidHeaderValue ) ,
80- }
81-
8223/// Type of attestaion used
8324/// Only supported (or soon-to-be supported) types are given
8425#[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
@@ -164,69 +105,6 @@ impl QuoteGenerator for DcapTdxQuoteGenerator {
164105 }
165106}
166107
167- /// Measurements determined by the CVM platform
168- #[ derive( Clone , PartialEq , Debug ) ]
169- pub struct PlatformMeasurements {
170- pub mrtd : [ u8 ; 48 ] ,
171- pub rtmr0 : [ u8 ; 48 ] ,
172- }
173-
174- impl PlatformMeasurements {
175- fn from_dcap_qvl_quote ( quote : & dcap_qvl:: quote:: Quote ) -> Result < Self , AttestationError > {
176- let report = match quote. report {
177- Report :: TD10 ( report) => report,
178- Report :: TD15 ( report) => report. base ,
179- Report :: SgxEnclave ( _) => {
180- return Err ( AttestationError :: SgxNotSupported ) ;
181- }
182- } ;
183- Ok ( Self {
184- mrtd : report. mr_td ,
185- rtmr0 : report. rt_mr0 ,
186- } )
187- }
188-
189- fn from_tdx_quote ( quote : & tdx_quote:: Quote ) -> Self {
190- Self {
191- mrtd : quote. mrtd ( ) ,
192- rtmr0 : quote. rtmr0 ( ) ,
193- }
194- }
195- }
196-
197- /// Measurements determined by the CVM image
198- #[ derive( Clone , PartialEq , Debug ) ]
199- pub struct CvmImageMeasurements {
200- pub rtmr1 : [ u8 ; 48 ] ,
201- pub rtmr2 : [ u8 ; 48 ] ,
202- pub rtmr3 : [ u8 ; 48 ] ,
203- }
204-
205- impl CvmImageMeasurements {
206- fn from_dcap_qvl_quote ( quote : & dcap_qvl:: quote:: Quote ) -> Result < Self , AttestationError > {
207- let report = match quote. report {
208- Report :: TD10 ( report) => report,
209- Report :: TD15 ( report) => report. base ,
210- Report :: SgxEnclave ( _) => {
211- return Err ( AttestationError :: SgxNotSupported ) ;
212- }
213- } ;
214- Ok ( Self {
215- rtmr1 : report. rt_mr1 ,
216- rtmr2 : report. rt_mr2 ,
217- rtmr3 : report. rt_mr3 ,
218- } )
219- }
220-
221- fn from_tdx_quote ( quote : & tdx_quote:: Quote ) -> Self {
222- Self {
223- rtmr1 : quote. rtmr1 ( ) ,
224- rtmr2 : quote. rtmr2 ( ) ,
225- rtmr3 : quote. rtmr3 ( ) ,
226- }
227- }
228- }
229-
230108/// Verify DCAP TDX quotes, allowing them if they have one of a given set of platform-specific and
231109/// OS image specific measurements
232110#[ derive( Clone ) ]
0 commit comments