-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathexample_psa_refval_test.go
151 lines (115 loc) · 3.27 KB
/
example_psa_refval_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import "fmt"
func Example_psa_refval() {
comid := Comid{}
if err := comid.FromJSON([]byte(PSARefValJSONTemplate)); err != nil {
panic(err)
}
if err := comid.Valid(); err != nil {
panic(err)
}
if err := extractRefVals(&comid); err != nil {
panic(err)
}
// output:
// ImplementationID: 61636d652d696d706c656d656e746174696f6e2d69642d303030303030303031
// SignerID: acbb11c7e4da217205523ce4ce1a245ae1a239ae3c6bfd9e7871f7e5d8bae86b
// Label: BL
// Version: 2.1.0
// Digest: 87428fc522803d31065e7bce3cf03fe475096631e5e07bbd7a0fde60c4cf25c7
// SignerID: acbb11c7e4da217205523ce4ce1a245ae1a239ae3c6bfd9e7871f7e5d8bae86b
// Label: PRoT
// Version: 1.3.5
// Digest: 0263829989b6fd954f72baaf2fc64bc2e2f01d692d4de72986ea808f6e99813f
// SignerID: acbb11c7e4da217205523ce4ce1a245ae1a239ae3c6bfd9e7871f7e5d8bae86b
// Label: ARoT
// Version: 0.1.4
// Digest: a3a5e715f0cc574a73c3f9bebb6bc24f32ffd5b67b387244c2c909da779a1478
}
func extractRefVals(c *Comid) error {
if c.Triples.ReferenceValues == nil {
return fmt.Errorf("no reference values triples")
}
for i, rv := range c.Triples.ReferenceValues.Values {
if err := extractPSARefVal(rv); err != nil {
return fmt.Errorf("bad PSA reference value at index %d: %w", i, err)
}
}
return nil
}
func extractPSARefVal(rv ValueTriple) error {
class := rv.Environment.Class
if err := extractImplementationID(class); err != nil {
return fmt.Errorf("extracting impl-id: %w", err)
}
measurements := rv.Measurements
if err := extractSwMeasurements(measurements); err != nil {
return fmt.Errorf("extracting measurements: %w", err)
}
return nil
}
func extractSwMeasurements(m Measurements) error {
if len(m.Values) == 0 {
return fmt.Errorf("no measurements")
}
for i := range m.Values {
meas := &m.Values[i]
if err := extractSwMeasurement(meas); err != nil {
return fmt.Errorf("extracting measurement at index %d: %w", i, err)
}
}
return nil
}
func extractSwMeasurement(m *Measurement) error {
if err := extractPSARefValID(m.Key); err != nil {
return fmt.Errorf("extracting PSA refval id: %w", err)
}
if err := extractDigest(m.Val.Digests); err != nil {
return fmt.Errorf("extracting digest: %w", err)
}
return nil
}
func extractDigest(d *Digests) error {
if d == nil {
return fmt.Errorf("no digest")
}
if len(*d) != 1 {
return fmt.Errorf("more than one digest")
}
fmt.Printf("Digest: %x\n", (*d)[0].HashValue)
return nil
}
func extractPSARefValID(k *Mkey) error {
if k == nil {
return fmt.Errorf("no measurement key")
}
id, ok := k.Value.(*TaggedPSARefValID)
if !ok {
return fmt.Errorf("expected PSA refval id, found: %T", k.Value)
}
fmt.Printf("SignerID: %x\n", id.SignerID)
if id.Label != nil {
fmt.Printf("Label: %s\n", *id.Label)
}
if id.Version != nil {
fmt.Printf("Version: %s\n", *id.Version)
}
// ignore alg-id
return nil
}
func extractImplementationID(c *Class) error {
if c == nil {
return fmt.Errorf("no class")
}
classID := c.ClassID
if classID == nil {
return fmt.Errorf("no class-id")
}
if classID.Type() != ImplIDType {
return fmt.Errorf("class id is not a psa.impl-id")
}
fmt.Printf("ImplementationID: %x\n", classID.Bytes())
return nil
}