33
44package tdx
55
6- import "github.com/veraison/corim/comid"
6+ import (
7+ "fmt"
8+
9+ "github.com/veraison/corim/comid"
10+ "github.com/veraison/corim/corim"
11+ "github.com/veraison/eat"
12+ )
713
814func Example_tdx_pce_refval () {
915 comid := comid.Comid {}
@@ -17,3 +23,145 @@ func Example_tdx_pce_refval() {
1723 }
1824
1925}
26+
27+ // Example_decode_PCE_JSON decodes the TDX Provisioning Certification Enclave Measurement Extensions from the given JSON Template
28+ func Example_decode_PCE_JSON () {
29+ profileID , err := eat .NewProfile ("http://intel.com/tdx-profile" )
30+ if err != nil {
31+ panic (err ) // will not error, as the hard-coded string above is valid
32+ }
33+ profile , found := corim .GetProfile (profileID )
34+ if ! found {
35+ fmt .Printf ("CoRIM Profile NOT FOUND" )
36+ return
37+ }
38+
39+ coMID := profile .GetComid ()
40+ if err := coMID .FromJSON ([]byte (TDXPCERefValTemplate )); err != nil {
41+ panic (err )
42+ }
43+
44+ if err := coMID .Valid (); err != nil {
45+ panic (err )
46+ }
47+
48+ if err := extractPCERefVals (coMID ); err != nil {
49+ panic (err )
50+ }
51+
52+ //output
53+ // OID: 2.16.840.1.113741.1.2.3.4.1
54+ // Vendor: Intel Corporation
55+ // Model: TDX QE TCB
56+ // miscselect: c0000000fbff0000
57+ // tcbEvalNum: 11
58+ // IsvProdID: 0303
59+ // CryptoKey Type: pkix-base64-key
60+ // CryptoKey Value: -----BEGIN PUBLIC KEY-----
61+ // MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFn0taoAwR3PmrKkYLtAsD9o05KSM6mbgfNCgpuL0g6VpTHkZl73wk5BDxoV7n+Oeee0iIqkW3HMZT3ETiniJdg==
62+ // -----END PUBLIC KEY-----
63+ }
64+
65+ func extractPCERefVals (c * comid.Comid ) error {
66+ if c .Triples .ReferenceValues == nil {
67+ return fmt .Errorf ("no reference values triples" )
68+ }
69+
70+ for i , rv := range c .Triples .ReferenceValues .Values {
71+ if err := extractPCERefVal (rv ); err != nil {
72+ return fmt .Errorf ("bad PSA reference value at index %d: %w" , i , err )
73+ }
74+ }
75+
76+ return nil
77+ }
78+
79+ func extractPCERefVal (rv comid.ValueTriple ) error {
80+ class := rv .Environment .Class
81+
82+ if err := extractClassElements (class ); err != nil {
83+ return fmt .Errorf ("extracting class: %w" , err )
84+ }
85+
86+ measurements := rv .Measurements
87+ if err := extractPCEMeasurements (measurements ); err != nil {
88+ return fmt .Errorf ("extracting measurements: %w" , err )
89+ }
90+
91+ return nil
92+ }
93+
94+ func extractPCEMeasurements (m comid.Measurements ) error {
95+ if len (m .Values ) == 0 {
96+ return fmt .Errorf ("no measurements" )
97+ }
98+ for i , m := range m .Values {
99+ if err := decodePCEMValExtensions (m ); err != nil {
100+ return fmt .Errorf ("extracting measurement at index %d: %w" , i , err )
101+ }
102+
103+ if m .AuthorizedBy != nil {
104+ err := decodeAuthorisedBy (m )
105+ if err != nil {
106+ return fmt .Errorf ("extracting measurement at index %d: %w" , i , err )
107+ }
108+ }
109+ }
110+ return nil
111+ }
112+
113+ func decodePCEMValExtensions (m comid.Measurement ) error {
114+ val , err := m .Val .Extensions .Get ("instanceid" )
115+ if err != nil {
116+ return fmt .Errorf ("failed to decode instanceid from measurement extensions" )
117+ }
118+ i , ok := val .(* teeInstanceID )
119+ if ! ok {
120+ fmt .Printf ("val was not pointer to teeInstanceID" )
121+ }
122+ instanceID := * i
123+ fmt .Printf ("\n instanceid: %d" , instanceID )
124+
125+ val , err = m .Val .Extensions .Get ("tcbcompsvn" )
126+ if err != nil {
127+ return fmt .Errorf ("failed to decode tcbcompsvn from measurement extensions" )
128+ }
129+
130+ tD , ok := val .(* teeTcbCompSvn )
131+ if ! ok {
132+ fmt .Printf ("val was not pointer to tcbcompsvn" )
133+ }
134+
135+ val , err = m .Val .Extensions .Get ("pceid" )
136+ if err != nil {
137+ return fmt .Errorf ("failed to decode tcbevalnum from measurement extensions" )
138+ }
139+ t , ok := val .(* pceID )
140+ if ! ok {
141+ fmt .Printf ("val was not pointer to teeTcbEvalNum" )
142+ }
143+ pceID := * t
144+ fmt .Printf ("\n pceID: %s" , pceID )
145+
146+ err = extractSVN (tD )
147+ if err != nil {
148+ return fmt .Errorf ("unable to extract TEE Digest: %w" , err )
149+ }
150+ return nil
151+ }
152+
153+ func extractSVN (s * teeTcbCompSvn ) error {
154+ if s == nil {
155+ return fmt .Errorf ("no TEE TCB Comp SVN" )
156+ }
157+
158+ if len (* s ) > 16 {
159+ return fmt .Errorf ("computed SVN cannot be greater than 16" )
160+ }
161+
162+ for i , svn := range * s {
163+ fmt .Printf ("\n SVN[%d]: %d" , i , svn )
164+ }
165+
166+ return nil
167+ }
0 commit comments