Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set MKey as uint #34

Merged
merged 5 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions comid/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,31 @@ func Example_decode_CBOR_4() {

// Output: OK
}

func Example_decode_CBOR_5() {
yogeshbdeshpande marked this conversation as resolved.
Show resolved Hide resolved
// Taken from https://github.com/ietf-corim-cddl/blob/main/examples/comid-3.diag
in := []byte{
0xa3, 0x01, 0xa1, 0x00, 0x78, 0x20, 0x6d, 0x79, 0x2d, 0x6e, 0x73, 0x3a,
0x61, 0x63, 0x6d, 0x65, 0x2d, 0x72, 0x6f, 0x61, 0x64, 0x72, 0x75, 0x6e,
0x6e, 0x65, 0x72, 0x2d, 0x73, 0x75, 0x70, 0x70, 0x6c, 0x65, 0x6d, 0x65,
0x6e, 0x74, 0x02, 0x81, 0xa3, 0x00, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20,
0x49, 0x6e, 0x63, 0x2e, 0x01, 0xd8, 0x20, 0x74, 0x68, 0x74, 0x74, 0x70,
0x73, 0x3a, 0x2f, 0x2f, 0x61, 0x63, 0x6d, 0x65, 0x2e, 0x65, 0x78, 0x61,
0x6d, 0x70, 0x6c, 0x65, 0x02, 0x83, 0x01, 0x00, 0x02, 0x04, 0xa1, 0x00,
0x81, 0x82, 0xa1, 0x00, 0xa3, 0x00, 0xd8, 0x6f, 0x44, 0x55, 0x02, 0xc0,
0x00, 0x01, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x49, 0x6e, 0x63, 0x2e,
0x02, 0x78, 0x18, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x52, 0x6f, 0x61, 0x64,
0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x20, 0x46, 0x69, 0x72, 0x6d, 0x77,
0x61, 0x72, 0x65, 0x81, 0xa2, 0x00, 0x19, 0x02, 0xbc, 0x01, 0xa1, 0x02,
0x81, 0x82, 0x06, 0x44, 0xab, 0xcd, 0xef, 0x00,
}
comid := Comid{}
err := comid.FromCBOR(in)
if err != nil {
fmt.Printf("FAIL: %v", err)
} else {
fmt.Println("OK")
}

// Output: OK
}
60 changes: 57 additions & 3 deletions comid/measurement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@ import (
"github.com/veraison/swid"
)

const MaxUint64 = ^uint64(0)

// Measurement stores a measurement-map with CBOR and JSON serializations.
type Measurement struct {
Key *Mkey `cbor:"0,keyasint,omitempty" json:"key,omitempty"`
Val Mval `cbor:"1,keyasint" json:"value"`
}

// Mkey stores a $measured-element-type-choice.
// The supported types are UUID and PSA refval-id.
// The supported types are UUID, PSA refval-id and unsigned integer
yogeshbdeshpande marked this conversation as resolved.
Show resolved Hide resolved
// TO DO Add tagged OID: see https://github.com/veraison/corim/issues/35
type Mkey struct {
val interface{}
}
Expand All @@ -37,8 +40,13 @@ func (o Mkey) Valid() error {
return nil
case TaggedPSARefValID:
return PSARefValID(t).Valid()
case uint64:
if o.val == nil {
return fmt.Errorf("empty uint Mkey")
}
return nil
default:
return fmt.Errorf("unknown measurement key type %T", t)
return fmt.Errorf("unknown measurement key type: %T", t)
}
}

Expand All @@ -51,6 +59,15 @@ func (o Mkey) GetPSARefValID() (PSARefValID, error) {
}
}

func (o Mkey) GetKeyUint() (uint64, error) {
switch t := o.val.(type) {
case uint64:
return t, nil
default:
return MaxUint64, fmt.Errorf("measurement-key type is: %T", t)
}
}

// UnmarshalJSON deserializes the type'n'value JSON object into the target Mkey
func (o *Mkey) UnmarshalJSON(data []byte) error {
var v tnv
Expand Down Expand Up @@ -84,6 +101,15 @@ func (o *Mkey) UnmarshalJSON(data []byte) error {
)
}
o.val = TaggedPSARefValID(x)
case "uint":
var x uint64
if err := json.Unmarshal(v.Value, &x); err != nil {
return fmt.Errorf(
"cannot unmarshal $measured-element-type-choice of type uint: %w",
err,
)
}
o.val = x
default:
return fmt.Errorf("unknown type %s for $measured-element-type-choice", v.Type)
}
Expand All @@ -92,7 +118,7 @@ func (o *Mkey) UnmarshalJSON(data []byte) error {
}

// MarshalJSON serializes the target Mkey into the type'n'value JSON object
// uuid, psa.refval-id
// Supported types are: uuid, psa.refval-id and unsigned integer
func (o Mkey) MarshalJSON() ([]byte, error) {
var (
v tnv
Expand All @@ -114,6 +140,13 @@ func (o Mkey) MarshalJSON() ([]byte, error) {
return nil, err
}
v = tnv{Type: "psa.refval-id", Value: b}
case uint64:
b, err = json.Marshal(t)
if err != nil {
return nil, err
}
v = tnv{Type: "uint", Value: b}

default:
return nil, fmt.Errorf("unknown type %T for mkey", t)
}
Expand All @@ -122,6 +155,9 @@ func (o Mkey) MarshalJSON() ([]byte, error) {
}

func (o Mkey) MarshalCBOR() ([]byte, error) {
if err := o.Valid(); err != nil {
return nil, err
}
yogeshbdeshpande marked this conversation as resolved.
Show resolved Hide resolved
return em.Marshal(o.val)
}

Expand Down Expand Up @@ -255,6 +291,17 @@ func (o *Measurement) SetKeyUUID(u UUID) *Measurement {
return o
}

// SetKeyUint sets the key of the target measurement-map to the supplied
// unsigned integer
func (o *Measurement) SetKeyUint(u uint64) *Measurement {
if o != nil {
o.Key = &Mkey{
val: u,
}
}
return o
}

// NewPSAMeasurement instantiates a new measurement-map with the key set to the
// supplied PSA refval-id
func NewPSAMeasurement(psaRefValID PSARefValID) *Measurement {
Expand All @@ -269,6 +316,13 @@ func NewUUIDMeasurement(uuid UUID) *Measurement {
return m.SetKeyUUID(uuid)
}

// NewUintMeasurement instantiates a new measurement-map with the key set to the
// supplied Uint
func NewUintMeasurement(mkey uint64) *Measurement {
m := &Measurement{}
return m.SetKeyUint(mkey)
}

func (o *Measurement) SetVersion(ver string, scheme uint64) *Measurement {
if o != nil {
v := NewVersion().SetVersion(ver).SetScheme(scheme)
Expand Down
Loading