Skip to content

Commit

Permalink
Set MKey as uint
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshbdeshpande committed Dec 1, 2021
1 parent 640b00b commit 360edfb
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 11 deletions.
76 changes: 76 additions & 0 deletions comid/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,79 @@ func Example_decode_CBOR_4() {

// Output: OK
}

func Example_decode_CBOR_5() {
// Taken from https://github.com/ietf-corim-cddl/blob/main/examples/comid-2.diag
in := []byte{
0xa5, 0x00, 0x65, 0x65, 0x6e, 0x2d, 0x47, 0x42, 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, 0x82, 0xa3,
0x00, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c, 0x74, 0x64, 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,
0x82, 0x01, 0x00, 0xa2, 0x00, 0x69, 0x45, 0x4d, 0x43, 0x41, 0x20, 0x4c,
0x74, 0x64, 0x2e, 0x02, 0x81, 0x02, 0x03, 0x82, 0xa2, 0x00, 0x78, 0x1a,
0x6d, 0x79, 0x2d, 0x6e, 0x73, 0x3a, 0x61, 0x63, 0x6d, 0x65, 0x2d, 0x72,
0x6f, 0x61, 0x64, 0x72, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x2d, 0x62, 0x61,
0x73, 0x65, 0x01, 0x00, 0xa2, 0x00, 0x78, 0x19, 0x6d, 0x79, 0x2d, 0x6e,
0x73, 0x3a, 0x61, 0x63, 0x6d, 0x65, 0x2d, 0x72, 0x6f, 0x61, 0x64, 0x72,
0x75, 0x6e, 0x6e, 0x65, 0x72, 0x2d, 0x6f, 0x6c, 0x64, 0x01, 0x01, 0x04,
0xa4, 0x00, 0x81, 0x82, 0xa3, 0x00, 0xa5, 0x00, 0xd8, 0x6f, 0x44, 0x55,
0x02, 0xc0, 0x00, 0x01, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c, 0x74,
0x64, 0x2e, 0x02, 0x6a, 0x52, 0x6f, 0x61, 0x64, 0x52, 0x75, 0x6e, 0x6e,
0x65, 0x72, 0x03, 0x00, 0x04, 0x01, 0x01, 0xd9, 0x02, 0x26, 0x47, 0x02,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0x02, 0xd8, 0x25, 0x50, 0x31, 0xfb,
0x5a, 0xbf, 0x02, 0x3e, 0x49, 0x92, 0xaa, 0x4e, 0x95, 0xf9, 0xc1, 0x50,
0x3b, 0xfa, 0x81, 0xa2, 0x00, 0x18, 0x2d, 0x01, 0xaa, 0x01, 0xd9, 0x02,
0x28, 0x02, 0x02, 0x82, 0x82, 0x06, 0x44, 0xab, 0xcd, 0xef, 0x00, 0x82,
0x06, 0x44, 0xff, 0xff, 0xff, 0xff, 0x03, 0x0a, 0x04, 0xd9, 0x02, 0x30,
0x44, 0x01, 0x02, 0x03, 0x04, 0x05, 0x44, 0xff, 0xff, 0xff, 0xff, 0x06,
0x48, 0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01, 0x07, 0x50, 0x20,
0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x68, 0x08, 0x6c, 0x43, 0x30, 0x32, 0x58, 0x37, 0x30, 0x56,
0x48, 0x4a, 0x48, 0x44, 0x35, 0x09, 0x47, 0x02, 0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0x0a, 0x50, 0x31, 0xfb, 0x5a, 0xbf, 0x02, 0x3e, 0x49, 0x92,
0xaa, 0x4e, 0x95, 0xf9, 0xc1, 0x50, 0x3b, 0xfa, 0x01, 0x81, 0x82, 0xa3,
0x00, 0xa5, 0x00, 0xd8, 0x25, 0x50, 0x31, 0xfb, 0x5a, 0xbf, 0x02, 0x3e,
0x49, 0x92, 0xaa, 0x4e, 0x95, 0xf9, 0xc1, 0x50, 0x3b, 0xfa, 0x01, 0x69,
0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c, 0x74, 0x64, 0x2e, 0x02, 0x6a, 0x52,
0x6f, 0x61, 0x64, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x03, 0x00, 0x04,
0x01, 0x01, 0xd9, 0x02, 0x26, 0x47, 0x02, 0xde, 0xad, 0xbe, 0xef, 0xde,
0xad, 0x02, 0xd8, 0x25, 0x50, 0x31, 0xfb, 0x5a, 0xbf, 0x02, 0x3e, 0x49,
0x92, 0xaa, 0x4e, 0x95, 0xf9, 0xc1, 0x50, 0x3b, 0xfa, 0x81, 0xa2, 0x00,
0x18, 0x2e, 0x01, 0xaa, 0x01, 0xd9, 0x02, 0x29, 0x02, 0x02, 0x82, 0x82,
0x06, 0x44, 0xab, 0xcd, 0xef, 0x00, 0x82, 0x06, 0x44, 0xff, 0xff, 0xff,
0xff, 0x03, 0x0b, 0x04, 0xd9, 0x02, 0x30, 0x44, 0x01, 0x02, 0x03, 0x04,
0x05, 0x44, 0xff, 0xff, 0xff, 0xff, 0x06, 0x48, 0x02, 0x00, 0x5e, 0x10,
0x00, 0x00, 0x00, 0x01, 0x07, 0x50, 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x08, 0x6c,
0x43, 0x30, 0x32, 0x58, 0x37, 0x30, 0x56, 0x48, 0x4a, 0x48, 0x44, 0x35,
0x09, 0x47, 0x02, 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0x0a, 0x50, 0x31,
0xfb, 0x5a, 0xbf, 0x02, 0x3e, 0x49, 0x92, 0xaa, 0x4e, 0x95, 0xf9, 0xc1,
0x50, 0x3b, 0xfa, 0x02, 0x81, 0x82, 0xa1, 0x01, 0xd8, 0x25, 0x50, 0x31,
0xfb, 0x5a, 0xbf, 0x02, 0x3e, 0x49, 0x92, 0xaa, 0x4e, 0x95, 0xf9, 0xc1,
0x50, 0x3b, 0xfa, 0x81, 0xa2, 0x00, 0x75, 0x46, 0x47, 0x48, 0x49, 0x6a,
0x6b, 0x69, 0x73, 0x6c, 0x64, 0x6e, 0x41, 0x53, 0x44, 0x78, 0x76, 0x57,
0x59, 0x2e, 0x2e, 0x2e, 0x01, 0x82, 0x74, 0x4d, 0x45, 0x4e, 0x47, 0x73,
0x64, 0x68, 0x66, 0x75, 0x67, 0x6a, 0x51, 0x57, 0x45, 0x74, 0x79, 0x58,
0x2e, 0x2e, 0x2e, 0x74, 0x4d, 0x49, 0x49, 0x45, 0x6e, 0x6a, 0x43, 0x43,
0x41, 0x34, 0x61, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41, 0x2e, 0x2e, 0x2e,
0x03, 0x81, 0x82, 0xa1, 0x01, 0xd9, 0x02, 0x26, 0x47, 0x02, 0xde, 0xad,
0xbe, 0xef, 0xde, 0xad, 0x81, 0xa2, 0x00, 0x74, 0x4d, 0x49, 0x47, 0x6b,
0x41, 0x67, 0x45, 0x42, 0x42, 0x44, 0x43, 0x6b, 0x35, 0x51, 0x62, 0x6f,
0x42, 0x2e, 0x2e, 0x2e, 0x01, 0x82, 0x74, 0x4d, 0x49, 0x49, 0x44, 0x6b,
0x6a, 0x43, 0x43, 0x41, 0x78, 0x69, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41,
0x2e, 0x2e, 0x2e, 0x74, 0x4d, 0x49, 0x49, 0x45, 0x6e, 0x6a, 0x43, 0x43,
0x41, 0x34, 0x61, 0x67, 0x41, 0x77, 0x49, 0x42, 0x41, 0x2e, 0x2e, 0x2e,
}
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
// 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
}
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

0 comments on commit 360edfb

Please sign in to comment.