|
| 1 | +// Copyright 2024 Contributors to the Veraison project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package comid |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func Example_cca_realm_refval() { |
| 12 | + comid := Comid{} |
| 13 | + |
| 14 | + if err := comid.FromJSON([]byte(CCARealmRefValJSONTemplate)); err != nil { |
| 15 | + panic(err) |
| 16 | + } |
| 17 | + |
| 18 | + if err := comid.Valid(); err != nil { |
| 19 | + panic(err) |
| 20 | + } |
| 21 | + |
| 22 | + if err := extractRealmRefVals(&comid); err != nil { |
| 23 | + panic(err) |
| 24 | + } |
| 25 | + // output: |
| 26 | + // ClassID: cd1f0e5526f9460db9d8f7fde171787c |
| 27 | + // Vendor: Workload Client Ltd |
| 28 | + // InstanceID: e45b72f5c0c0b572 |
| 29 | + // Index: rim |
| 30 | + // Alg: sha-384 |
| 31 | + // Digest: 4284b5694ca6c0d2cf4789a0b95ac8025c818de52304364be7cd2981b2d2edc685b322277ec25819962413d8c9b2c1f5 |
| 32 | + // Index: rem |
| 33 | + // Alg: sha-384 |
| 34 | + // Digest: 2107bbe761fca52d95136a1354db7a4dd57b1b26be0d3da71d9eb23986b34ba615abf6514cf35e5a9ea55a032d068a78 |
| 35 | + // Alg: sha-384 |
| 36 | + // Digest: 2507bbe761fca52d95136a1354db7a4dd57b1b26be0d3da71d9eb23986b34ba615abf6514cf35e5a9ea55a032d068a78 |
| 37 | + // Alg: sha-384 |
| 38 | + // Digest: 3107bbe761fca52d95136a1354db7a4dd57b1b26be0d3da71d9eb23986b34ba615abf6514cf35e5a9ea55a032d068a78 |
| 39 | + // Alg: sha-384 |
| 40 | + // Digest: 3507bbe761fca52d95136a1354db7a4dd57b1b26be0d3da71d9eb23986b34ba615abf6514cf35e5a9ea55a032d068a78 |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +func extractRealmRefVals(c *Comid) error { |
| 45 | + if c.Triples.ReferenceValues == nil { |
| 46 | + return fmt.Errorf("no reference values triples") |
| 47 | + } |
| 48 | + |
| 49 | + for i, rv := range *c.Triples.ReferenceValues { |
| 50 | + if err := extractRealmRefVal(rv); err != nil { |
| 51 | + return fmt.Errorf("bad Realm reference value at index %d: %w", i, err) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +func extractRealmRefVal(rv ReferenceValue) error { |
| 59 | + class := rv.Environment.Class |
| 60 | + instance := rv.Environment.Instance |
| 61 | + |
| 62 | + if err := extractRealmClass(class); err != nil { |
| 63 | + return fmt.Errorf("extracting uuid: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + if err := extractRealmInstanceID(instance); err != nil { |
| 67 | + return fmt.Errorf("extracting realm instanceID: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + measurements := rv.Measurements |
| 71 | + |
| 72 | + if err := extractMeasurements(measurements); err != nil { |
| 73 | + return fmt.Errorf("extracting measurements: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func extractMeasurements(m Measurements) error { |
| 80 | + if len(m) == 0 { |
| 81 | + return fmt.Errorf("no measurements") |
| 82 | + } |
| 83 | + |
| 84 | + for i, m := range m { |
| 85 | + if err := extractMeasurement(m); err != nil { |
| 86 | + return fmt.Errorf("extracting measurement at index %d: %w", i, err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +func extractMeasurement(m Measurement) error { |
| 94 | + if err := extractIntegrityRegisters(m.Val.IntegrityRegisters); err != nil { |
| 95 | + return fmt.Errorf("extracting digest: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func extractRealmClass(c *Class) error { |
| 102 | + if c == nil { |
| 103 | + return fmt.Errorf("no class") |
| 104 | + } |
| 105 | + |
| 106 | + classID := c.ClassID |
| 107 | + |
| 108 | + if classID == nil { |
| 109 | + return fmt.Errorf("no class-id") |
| 110 | + } |
| 111 | + |
| 112 | + if classID.Type() != "uuid" { |
| 113 | + return fmt.Errorf("class id is not a uuid") |
| 114 | + } |
| 115 | + |
| 116 | + fmt.Printf("ClassID: %x\n", classID.Bytes()) |
| 117 | + |
| 118 | + if c.Vendor != nil { |
| 119 | + fmt.Printf("Vendor: %s\n", c.GetVendor()) |
| 120 | + } |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func extractRealmInstanceID(i *Instance) error { |
| 125 | + if i == nil { |
| 126 | + return fmt.Errorf("no instance") |
| 127 | + } |
| 128 | + |
| 129 | + if i.Type() != "bytes" { |
| 130 | + return fmt.Errorf("instance id is not bytes") |
| 131 | + } |
| 132 | + |
| 133 | + fmt.Printf("InstanceID: %x\n", i.Bytes()) |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func extractIntegrityRegisters(r *IntegrityRegisters) error { |
| 139 | + if r == nil { |
| 140 | + return fmt.Errorf("no integrity registers") |
| 141 | + } |
| 142 | + |
| 143 | + keys, err := extractRegisterIndex(r) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("unable to extract register index: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + for _, k := range keys { |
| 149 | + d, ok := r.m[k] |
| 150 | + if !ok { |
| 151 | + return fmt.Errorf("unable to locate register index for: %s", k) |
| 152 | + } |
| 153 | + fmt.Printf("Index: %s\n", k) |
| 154 | + if err := extractRealmDigests(d); err != nil { |
| 155 | + return fmt.Errorf("invalid Digests for key: %s, %v", k, err) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func extractRealmDigests(digests Digests) error { |
| 163 | + |
| 164 | + if err := digests.Valid(); err != nil { |
| 165 | + return fmt.Errorf("invalid digest: %v", err) |
| 166 | + } |
| 167 | + for _, d := range digests { |
| 168 | + fmt.Printf("Alg: %s\n", d.AlgIDToString()) |
| 169 | + fmt.Printf("Digest: %x\n", d.HashValue) |
| 170 | + } |
| 171 | + |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func extractRegisterIndex(r *IntegrityRegisters) ([]string, error) { |
| 176 | + var keys [2]string |
| 177 | + for k := range r.m { |
| 178 | + switch t := k.(type) { |
| 179 | + case string: |
| 180 | + key := strings.ToLower(t) |
| 181 | + switch key { |
| 182 | + case "rim": |
| 183 | + keys[0] = key |
| 184 | + case "rem": |
| 185 | + keys[1] = key |
| 186 | + default: |
| 187 | + return nil, fmt.Errorf("unexpected register index: %s", key) |
| 188 | + } |
| 189 | + default: |
| 190 | + return nil, fmt.Errorf("unexpected type for index: %T", t) |
| 191 | + } |
| 192 | + } |
| 193 | + return keys[:], nil |
| 194 | +} |
0 commit comments