Skip to content

Commit 14125c5

Browse files
authored
Merge pull request #399 from oasisprotocol/kostko/feature/rofl-build-identity
feat(cmd/rofl): Populate component identity in the generated bundle
2 parents ac8bd37 + a97ac78 commit 14125c5

File tree

6 files changed

+62
-25
lines changed

6 files changed

+62
-25
lines changed

build/measurement/tdx_qemu.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func computeEnclaveIdentity(
453453
//
454454
// It may return multiple identities because there may be differences between QEMU versions that can
455455
// cause differences in measurements (e.g. with MRTD).
456-
func MeasureTdxQemu(bnd *bundle.Bundle, comp *bundle.Component) ([]sgx.EnclaveIdentity, error) {
456+
func MeasureTdxQemu(bnd *bundle.Bundle, comp *bundle.Component) ([]bundle.Identity, error) {
457457
if comp.TDX == nil {
458458
return nil, fmt.Errorf("component does not support TDX")
459459
}
@@ -526,14 +526,18 @@ func MeasureTdxQemu(bnd *bundle.Bundle, comp *bundle.Component) ([]sgx.EnclaveId
526526

527527
// Compute MRTD for all known QEMU variants as there are unfortunately different
528528
// implementations.
529-
eids := make([]sgx.EnclaveIdentity, 0, 2)
529+
ids := make([]bundle.Identity, 0, 2)
530530
for _, variant := range []int{
531531
mrtdVariantTwoPass,
532532
mrtdVariantSinglePass,
533533
} {
534534
mrtd := tdvfMeta.computeMrtd(fw, variant)
535+
eid := computeEnclaveIdentity(mrtd, rtmr0, rtmr1, rtmr2, rtmr3[:])
535536

536-
eids = append(eids, computeEnclaveIdentity(mrtd, rtmr0, rtmr1, rtmr2, rtmr3[:]))
537+
ids = append(ids, bundle.Identity{
538+
Hypervisor: fmt.Sprintf("qemu/v%d", variant),
539+
Enclave: eid,
540+
})
537541
}
538-
return eids, nil
542+
return ids, nil
539543
}

cmd/rofl/build/build.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,24 @@ var (
143143

144144
fmt.Println("Computing enclave identity...")
145145

146-
eids, err := roflCommon.ComputeEnclaveIdentity(bnd, "")
146+
ids, err := roflCommon.ComputeEnclaveIdentity(bnd, "")
147147
if err != nil {
148148
fmt.Printf("%s\n", err)
149149
return
150150
}
151151

152152
// Setup some post-bundle environment variables.
153153
os.Setenv("ROFL_BUNDLE", outFn)
154-
for idx, enclaveID := range eids {
155-
data, _ := enclaveID.MarshalText()
154+
for idx, id := range ids {
155+
data, _ := id.Enclave.MarshalText()
156156
os.Setenv(fmt.Sprintf("ROFL_ENCLAVE_ID_%d", idx), string(data))
157157
}
158158

159159
runScript(manifest, buildRofl.ScriptBundlePost)
160160

161161
buildEnclaves := make(map[sgx.EnclaveIdentity]struct{})
162-
for _, eid := range eids {
163-
buildEnclaves[eid] = struct{}{}
162+
for _, id := range ids {
163+
buildEnclaves[id.Enclave] = struct{}{}
164164
}
165165

166166
manifestEnclaves := make(map[sgx.EnclaveIdentity]struct{})
@@ -241,15 +241,18 @@ var (
241241
fmt.Printf(" %s:\n", deploymentName)
242242
fmt.Printf(" policy:\n")
243243
fmt.Printf(" enclaves:\n")
244-
for _, enclaveID := range eids {
245-
data, _ := enclaveID.MarshalText()
244+
for _, id := range ids {
245+
data, _ := id.Enclave.MarshalText()
246246
fmt.Printf(" - \"%s\"\n", string(data))
247247
}
248248
fmt.Println()
249249
fmt.Println("Next time you can also use the --update-manifest flag to apply changes.")
250250
case true:
251251
// Update the manifest with the given enclave identities, overwriting existing ones.
252-
deployment.Policy.Enclaves = eids
252+
deployment.Policy.Enclaves = make([]sgx.EnclaveIdentity, len(ids))
253+
for _, id := range ids {
254+
deployment.Policy.Enclaves = append(deployment.Policy.Enclaves, id.Enclave)
255+
}
253256

254257
if err = manifest.Save(); err != nil {
255258
cobra.CheckErr(fmt.Errorf("failed to update manifest: %w", err))

cmd/rofl/build/sgx.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
buildRofl "github.com/oasisprotocol/cli/build/rofl"
2222
"github.com/oasisprotocol/cli/build/sgxs"
2323
"github.com/oasisprotocol/cli/cmd/common"
24+
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
2425
)
2526

2627
// sgxBuild builds an SGX-based "raw" ROFL app.
@@ -132,6 +133,13 @@ func sgxBuild(
132133
_ = bnd.Add(dst, bundle.NewBytesData(b))
133134
}
134135
_ = bnd.Add(sigName, bundle.NewBytesData(sigData))
136+
137+
// Compute expected component identity and include it in the manifest.
138+
ids, err := roflCommon.ComputeComponentIdentity(bnd, &comp)
139+
if err != nil {
140+
cobra.CheckErr(fmt.Errorf("failed to compute component identity: %w", err))
141+
}
142+
comp.Identities = ids
135143
}
136144

137145
// sgxGenerateKey generates a 3072-bit RSA key with public exponent 3 as required for SGX.

cmd/rofl/build/tdx.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/oasisprotocol/cli/build/cargo"
1515
buildRofl "github.com/oasisprotocol/cli/build/rofl"
1616
"github.com/oasisprotocol/cli/cmd/common"
17+
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
1718
)
1819

1920
// Artifact kinds.
@@ -283,6 +284,13 @@ func tdxBundleComponent(
283284
_ = bnd.Add(dst, bundle.NewFileData(src))
284285
}
285286

287+
// Compute expected component identity and include it in the manifest.
288+
ids, err := roflCommon.ComputeComponentIdentity(bnd, &comp)
289+
if err != nil {
290+
return fmt.Errorf("failed to compute component identity: %w", err)
291+
}
292+
comp.Identities = ids
293+
286294
return nil
287295
}
288296

cmd/rofl/common/identity.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package common
33
import (
44
"fmt"
55

6-
"github.com/oasisprotocol/oasis-core/go/common/sgx"
76
"github.com/oasisprotocol/oasis-core/go/runtime/bundle"
87
"github.com/oasisprotocol/oasis-core/go/runtime/bundle/component"
98

@@ -12,7 +11,7 @@ import (
1211

1312
// ComputeEnclaveIdentity computes the enclave identity of the given ROFL components. If no specific
1413
// component ID is passed, it uses the first ROFL component.
15-
func ComputeEnclaveIdentity(bnd *bundle.Bundle, compID string) ([]sgx.EnclaveIdentity, error) {
14+
func ComputeEnclaveIdentity(bnd *bundle.Bundle, compID string) ([]bundle.Identity, error) {
1615
var cid component.ID
1716
if compID != "" {
1817
if err := cid.UnmarshalText([]byte(compID)); err != nil {
@@ -33,14 +32,7 @@ func ComputeEnclaveIdentity(bnd *bundle.Bundle, compID string) ([]sgx.EnclaveIde
3332
}
3433
}
3534

36-
switch teeKind := comp.TEEKind(); teeKind {
37-
case component.TEEKindSGX:
38-
return bnd.EnclaveIdentities(comp.ID())
39-
case component.TEEKindTDX:
40-
return measurement.MeasureTdxQemu(bnd, comp)
41-
default:
42-
return nil, fmt.Errorf("identity computation for TEE kind '%s' not supported", teeKind)
43-
}
35+
return ComputeComponentIdentity(bnd, comp)
4436
}
4537

4638
switch compID {
@@ -50,3 +42,25 @@ func ComputeEnclaveIdentity(bnd *bundle.Bundle, compID string) ([]sgx.EnclaveIde
5042
return nil, fmt.Errorf("ROFL app '%s' not found in bundle", compID)
5143
}
5244
}
45+
46+
// ComputeComponentIdentity computes the enclave identity of the given component.
47+
func ComputeComponentIdentity(bnd *bundle.Bundle, comp *bundle.Component) ([]bundle.Identity, error) {
48+
switch teeKind := comp.TEEKind(); teeKind {
49+
case component.TEEKindSGX:
50+
eids, err := bnd.EnclaveIdentities(comp.ID())
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
ids := make([]bundle.Identity, len(eids))
56+
for _, eid := range eids {
57+
// For SGX enclaves, there is no additional metadata (e.g. hypervisor).
58+
ids = append(ids, bundle.Identity{Enclave: eid})
59+
}
60+
return ids, nil
61+
case component.TEEKindTDX:
62+
return measurement.MeasureTdxQemu(bnd, comp)
63+
default:
64+
return nil, fmt.Errorf("identity computation for TEE kind '%s' not supported", teeKind)
65+
}
66+
}

cmd/rofl/identity.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ var (
2727
cobra.CheckErr(fmt.Errorf("failed to open bundle: %w", err))
2828
}
2929

30-
eids, err := roflCommon.ComputeEnclaveIdentity(bnd, compID)
30+
ids, err := roflCommon.ComputeEnclaveIdentity(bnd, compID)
3131
cobra.CheckErr(err)
3232

33-
for _, enclaveID := range eids {
34-
data, _ := enclaveID.MarshalText()
33+
for _, id := range ids {
34+
data, _ := id.Enclave.MarshalText()
3535
fmt.Println(string(data))
3636
}
3737
},

0 commit comments

Comments
 (0)