Skip to content

Commit 60fc059

Browse files
Add PCE test case
Signed-off-by: Yogesh Deshpande <[email protected]>
1 parent e74d471 commit 60fc059

File tree

4 files changed

+155
-6
lines changed

4 files changed

+155
-6
lines changed

comid/tdx-profile/example_pce_refval_test.go

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44
package 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

814
func 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("\ninstanceid: %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("\npceID: %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+
}

comid/tdx-profile/example_qe_refval_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/veraison/eat"
1212
)
1313

14-
// Example_decode_JSON decodes the TDX Measurement Extensions from the given JSON Template
14+
// Example_decode_QE_JSON decodes the TDX Quoting Enclave Measurement Extensions from the given JSON Template
1515
func Example_decode_QE_JSON() {
1616
profileID, err := eat.NewProfile("http://intel.com/tdx-profile")
1717
if err != nil {

comid/tdx-profile/mval_extensions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type MvalExtensions struct {
1515
// a string field extension
1616
TcbDate *tdate `cbor:"-72,keyasint,omitempty" json:"tcbdate,omitempty"`
1717
IsvSVN *teeSVN `cbor:"-73,keyasint,omitempty" json:"isvsvn,omitempty"`
18+
InstanceID *teeInstanceID `cbor:"-77,keyasint,omitempty" json:"instanceid,omitempty"`
1819
PCEID *pceID `cbor:"-80,keyasint,omitempty" json:"pceid,omitempty"`
1920
MiscSelect *teeMiscSelect `cbor:"-81,keyasint,omitempty" json:"miscselect,omitempty"`
2021
Attributes *teeAttributes `cbor:"-82,keyasint,omitempty" json:"attributes,omitempty"`
@@ -26,7 +27,7 @@ type MvalExtensions struct {
2627
Epoch *epochSeconds `cbor:"-90, keyasint,omitempty" json:"epoch,omitempty"`
2728

2829
TeeCryptoKeys *[]teeCryptoKey `cbor:"-91, keyasint,omitempty" json:"teecryptokeys,omitempty"`
29-
TeeTCBCompSvn *teeTcbCompSvn `cbor:"-125, keyasint,omitempty" json:"teetcbcompsvn,omitempty"`
30+
TCBCompSvn *teeTcbCompSvn `cbor:"-125, keyasint,omitempty" json:"tcbcompsvn,omitempty"`
3031
}
3132

3233
// Registering the profile inside init() in the same file where it is defined

comid/tdx-profile/test_vars.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
"class": {
2929
"id": {
3030
"type": "oid",
31-
"value": "2.16.840.1.113741.1.2.3.4.4"
31+
"value": "2.16.840.1.113741.1.2.3.4.6"
3232
},
3333
"vendor": "Intel Corporation",
3434
"model": "0123456789ABCDEF"
@@ -37,8 +37,8 @@ var (
3737
"measurements": [
3838
{
3939
"value": {
40-
"attributes": "AwM=",
41-
"tcbevalnum": 5,
40+
"instanceid": 0,
41+
"tcbcompsvn": [10, 10, 2, 2, 2, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0],
4242
"pceid": "0000"
4343
},
4444
"authorized-by": {

0 commit comments

Comments
 (0)