@@ -5,9 +5,9 @@ use dcap_types::cert::{SgxExtensionTcbLevel, SgxExtensions};
55use dcap_types:: tcbinfo:: { TcbComponent , TcbInfoV3 } ;
66use dcap_types:: TcbInfoV3TcbStatus ;
77use dcap_types:: { SGX_TEE_TYPE , TDX_TEE_TYPE } ;
8- use x509_parser:: oid_registry:: OID_X509_EXT_CRL_DISTRIBUTION_POINTS ;
98use x509_parser:: prelude:: * ;
109
10+ /// Parse a PEM-encoded certificate chain into a vector of `X509Certificate`.
1111pub fn parse_certchain ( pem_certs : & [ Pem ] ) -> crate :: Result < Vec < X509Certificate > > {
1212 Ok ( pem_certs
1313 . iter ( )
@@ -44,7 +44,7 @@ pub fn verify_crl_signature(
4444 verify_p256_signature_der ( data, signature, public_key)
4545}
4646
47- // verify_certchain_signature just verify that the certchain signature matches, any other checks will be done by the caller
47+ /// verify_certchain_signature just verify that the certchain signature matches, any other checks will be done by the caller
4848pub fn verify_certchain_signature (
4949 certs : & [ & X509Certificate ] ,
5050 root_cert : & X509Certificate ,
@@ -61,67 +61,52 @@ pub fn verify_certchain_signature(
6161 verify_certificate ( prev_cert, root_cert)
6262}
6363
64+ /// Get the Subject Common Name (CN) from a certificate.
6465pub fn get_x509_subject_cn ( cert : & X509Certificate ) -> String {
6566 let subject = cert. subject ( ) ;
6667 let cn = subject. iter_common_name ( ) . next ( ) . unwrap ( ) ;
6768 cn. as_str ( ) . unwrap ( ) . to_string ( )
6869}
6970
71+ /// Get the Issuer Common Name (CN) from a certificate.
7072pub fn get_x509_issuer_cn ( cert : & X509Certificate ) -> String {
7173 let issuer = cert. issuer ( ) ;
7274 let cn = issuer. iter_common_name ( ) . next ( ) . unwrap ( ) ;
7375 cn. as_str ( ) . unwrap ( ) . to_string ( )
7476}
7577
76- pub fn get_crl_uri ( cert : & X509Certificate ) -> Option < String > {
77- let crl_ext = cert
78- . get_extension_unique ( & OID_X509_EXT_CRL_DISTRIBUTION_POINTS )
79- . unwrap ( )
80- . unwrap ( ) ;
81- let crl_uri = match crl_ext. parsed_extension ( ) {
82- ParsedExtension :: CRLDistributionPoints ( crls) => {
83- match & crls. iter ( ) . next ( ) . unwrap ( ) . distribution_point {
84- Some ( DistributionPointName :: FullName ( uri) ) => {
85- let uri = & uri[ 0 ] ;
86- match uri {
87- GeneralName :: URI ( uri) => Some ( uri. to_string ( ) ) ,
88- _ => None ,
89- }
90- }
91- _ => None ,
92- }
93- }
94- _ => {
95- unreachable ! ( ) ;
96- }
97- } ;
98- crl_uri
99- }
100-
101- /// https://github.com/intel/SGX-TDX-DCAP-QuoteVerificationLibrary/blob/7e5b2a13ca5472de8d97dd7d7024c2ea5af9a6ba/Src/AttestationLibrary/src/Verifiers/Checks/TcbLevelCheck.cpp#L129-L181
102- pub fn get_sgx_tdx_fmspc_tcbstatus_v3 (
78+ /// Get the TCB status of the SGX and TDX corresponding to the given SVN from the TCB Info V3.
79+ /// This function returns the TCB status of the SGX and TDX, and the advisory IDs.
80+ /// ref. <https://github.com/intel/SGX-TDX-DCAP-QuoteVerificationLibrary/blob/7e5b2a13ca5472de8d97dd7d7024c2ea5af9a6ba/Src/AttestationLibrary/src/Verifiers/Checks/TcbLevelCheck.cpp#L129-L181>
81+ ///
82+ /// # Arguments
83+ /// * `tee_type` - The type of TEE (SGX or TDX)
84+ /// * `tee_tcb_svn` - The TCB SVN of the TEE (only for TDX)
85+ /// * `sgx_extensions` - The SGX Extensions from the PCK Certificate
86+ /// * `tcbinfov3` - The TCB Info V3
87+ /// # Returns
88+ /// * `(sgx_tcb_status, tdx_tcb_status, advisory_ids)` - The TCB status of the SGX and TDX, and the advisory IDs
89+ pub fn get_sgx_tdx_tcb_status_v3 (
10390 tee_type : u32 ,
10491 tee_tcb_svn : Option < [ u8 ; 16 ] > ,
105- // SGX Extensions from the PCK Certificate
10692 sgx_extensions : & SgxExtensions ,
10793 tcbinfov3 : & TcbInfoV3 ,
10894) -> crate :: Result < ( TcbInfoV3TcbStatus , Option < TcbInfoV3TcbStatus > , Vec < String > ) > {
109- let is_tdx = tee_type == TDX_TEE_TYPE && tcbinfov3. tcb_info . id == "TDX" ;
110- if !is_tdx {
111- // check if tee_type and tcb_info.id are consistent
112- assert ! ( tee_type == SGX_TEE_TYPE && tcbinfov3. tcb_info. id == "SGX" ) ;
113- }
114-
115- let is_tdx = if tee_type == SGX_TEE_TYPE {
116- false
95+ if tee_type == SGX_TEE_TYPE {
96+ if tcbinfov3. tcb_info . id != "SGX" {
97+ bail ! ( "Invalid TCB Info ID for SGX TEE Type" ) ;
98+ } else if tee_tcb_svn. is_some ( ) {
99+ bail ! ( "SGX TCB SVN is not needed" ) ;
100+ }
117101 } else if tee_type == TDX_TEE_TYPE {
118- if tee_tcb_svn. is_none ( ) {
102+ if tcbinfov3. tcb_info . id != "TDX" {
103+ bail ! ( "Invalid TCB Info ID for TDX TEE Type" ) ;
104+ } else if tee_tcb_svn. is_none ( ) {
119105 bail ! ( "TDX TCB SVN is missing" ) ;
120106 }
121- true
122107 } else {
123108 bail ! ( "Unsupported TEE type: {}" , tee_type) ;
124- } ;
109+ }
125110
126111 // ref. https://github.com/intel/SGX-TDX-DCAP-QuoteVerificationLibrary/blob/7e5b2a13ca5472de8d97dd7d7024c2ea5af9a6ba/Src/AttestationLibrary/src/Verifiers/QuoteVerifier.cpp#L117
127112 if sgx_extensions. fmspc != tcbinfov3. tcb_info . fmspc ( ) ? {
@@ -148,15 +133,16 @@ pub fn get_sgx_tdx_fmspc_tcbstatus_v3(
148133 && extension_pcesvn >= tcb_level. tcb . pcesvn
149134 {
150135 sgx_tcb_status = Some ( TcbInfoV3TcbStatus :: from_str ( tcb_level. tcb_status . as_str ( ) ) ?) ;
151- if !is_tdx {
136+ if tee_type == SGX_TEE_TYPE {
152137 return Ok ( (
153138 sgx_tcb_status. unwrap ( ) ,
154139 None ,
155140 tcb_level. advisory_ids . clone ( ) . unwrap_or_default ( ) ,
156141 ) ) ;
157142 }
158143 }
159- if is_tdx && sgx_tcb_status. is_some ( ) {
144+
145+ if tee_type == TDX_TEE_TYPE && sgx_tcb_status. is_some ( ) {
160146 let tdxtcbcomponents = match & tcb_level. tcb . tdxtcbcomponents {
161147 Some ( cmps) => cmps,
162148 None => bail ! ( "TDX TCB Components are missing" ) ,
@@ -178,6 +164,18 @@ pub fn get_sgx_tdx_fmspc_tcbstatus_v3(
178164 }
179165}
180166
167+ /// Merge two vectors of advisory ids into one vector
168+ /// This function will remove any duplicates
169+ pub fn merge_advisory_ids ( advisory_ids : Vec < String > , advisory_ids2 : Vec < String > ) -> Vec < String > {
170+ let mut ids = advisory_ids
171+ . into_iter ( )
172+ . chain ( advisory_ids2)
173+ . collect :: < Vec < _ > > ( ) ;
174+ ids. sort ( ) ;
175+ ids. dedup ( ) ;
176+ ids
177+ }
178+
181179fn match_sgxtcbcomp ( tcb : & SgxExtensionTcbLevel , sgxtcbcomponents : & [ TcbComponent ; 16 ] ) -> bool {
182180 // Compare all of the SGX TCB Comp SVNs retrieved from the SGX PCK Certificate (from 01 to 16) with the corresponding values of SVNs in sgxtcbcomponents array of TCB Level.
183181 // If all SGX TCB Comp SVNs in the certificate are greater or equal to the corresponding values in TCB Level, then return true.
@@ -197,15 +195,3 @@ fn match_tdxtcbcomp(tee_tcb_svn: &[u8; 16], tdxtcbcomponents: &[TcbComponent; 16
197195 . zip ( tdxtcbcomponents. iter ( ) )
198196 . all ( |( tee, tcb) | * tee >= tcb. svn )
199197}
200-
201- /// Merge two vectors of advisory ids into one vector
202- /// This function will remove any duplicates
203- pub fn merge_advisory_ids ( advisory_ids : Vec < String > , advisory_ids2 : Vec < String > ) -> Vec < String > {
204- let mut ids = advisory_ids
205- . into_iter ( )
206- . chain ( advisory_ids2)
207- . collect :: < Vec < _ > > ( ) ;
208- ids. sort ( ) ;
209- ids. dedup ( ) ;
210- ids
211- }
0 commit comments