Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions comid/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ type Mval struct {
UEID *eat.UEID `cbor:"9,keyasint,omitempty" json:"ueid,omitempty"`
UUID *UUID `cbor:"10,keyasint,omitempty" json:"uuid,omitempty"`
Name *string `cbor:"11,keyasint,omitempty" json:"name,omitempty"`
CryptoKeys *CryptoKeys `cbor:"13,keyasint,omitempty" json:"cryptokeys,omitempty"`
IntegrityRegisters *IntegrityRegisters `cbor:"14,keyasint,omitempty" json:"integrity-registers,omitempty"`
Extensions
}
Expand Down Expand Up @@ -510,6 +511,7 @@ func (o Mval) Valid() error {
o.UEID == nil &&
o.UUID == nil &&
o.Name == nil &&
o.CryptoKeys == nil &&
o.IntegrityRegisters == nil &&
o.IsEmpty() {

Expand All @@ -530,6 +532,13 @@ func (o Mval) Valid() error {
}
}

// Validate CryptoKeys
if o.CryptoKeys != nil {
if err := o.CryptoKeys.Valid(); err != nil {
return err
}
}

// Validate Flags
if o.Flags != nil {
if err := o.Flags.Valid(); err != nil {
Expand Down Expand Up @@ -733,6 +742,19 @@ func (o *Measurement) AddDigest(algID uint64, digest []byte) *Measurement {
return o
}

// AddCryptoKey adds the supplied CryptoKey to the measurement-values-map of the
// target measurement
func (o *Measurement) AddCryptoKey(key *CryptoKey) *Measurement {
if o != nil {
ck := o.Val.CryptoKeys
if ck == nil {
ck = NewCryptoKeys()
}
o.Val.CryptoKeys = ck.Add(key)
}
return o
}

// SetFlagsTrue sets the supplied operational flags to true in the
// measurement-values-map of the target measurement
func (o *Measurement) SetFlagsTrue(flags ...Flag) *Measurement {
Expand Down
33 changes: 33 additions & 0 deletions comid/measurement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,3 +798,36 @@ func TestMval_Valid(t *testing.T) {
assert.NoError(t, err)
})
}

// Test Marshal and Unmarshal of Cryptokeys(tag 13)
func TestMeasurement_CryptoKeys_RoundTrip(t *testing.T) {
// Create a new measurement with a valid key
m := MustNewMeasurement("31fb5abf-023e-4992-aa4e-95f9c1503bfa", UUIDType)

// Create a CryptoKey (using a dummy PKIX base64 key for testing)
pkText := `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=
-----END PUBLIC KEY-----`
ck := MustNewPKIXBase64Key(pkText)

// Add CryptoKey to Measurement
m.AddCryptoKey(ck)

// Marshal to CBOR
// Use em (from cbor.go)
data, err := em.Marshal(m)
require.NoError(t, err)

// Check if tag 13 is present in the CBOR output hex
// We can try to decode it back to verify.

// Unmarshal back
var m2 Measurement
err = dm.Unmarshal(data, &m2)
require.NoError(t, err)

// Verify CryptoKeys are present and correct
require.NotNil(t, m2.Val.CryptoKeys)
require.Len(t, *m2.Val.CryptoKeys, 1)
assert.Equal(t, pkText, (*m2.Val.CryptoKeys)[0].String())
}