Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
87 changes: 86 additions & 1 deletion corim/signedcorim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,92 @@ func TestSignedCorim_TaggedFromCOSE_ok(t *testing.T) {
var actual SignedCorim
err := actual.FromCOSE(tv)

assert.Nil(t, err)
// With enhanced tag validation, this should now fail due to outdated schema
// The error indicates the payload has an incorrect schema for PSA impl-id vs refval-id
assert.NotNil(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is supposed to test correct unmarshalling of a valid CoRIM. Rather than changing the expected error to be non-nil, the input needs to be amended with a valide signed CoRIM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIX

assert.Contains(t, err.Error(), "tag validation failed")
}

// TestSignedCorim_TaggedFromCOSE_enhanced_validation tests that our enhanced
// tag validation correctly identifies schema problems with outdated payloads
func TestSignedCorim_TaggedFromCOSE_enhanced_validation(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestSignedCorim_TaggedFromCOSE_nok or TestSignedCorim_TaggedFromCOSE_bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

// This is the same outdated payload as above but with explicit expectation of failure
tv := []byte{0xd9, 0x01, 0xf4, 0xd9, 0x01, 0xf6, 0xd2,
0x84, 0x58, 0x59, 0xa4, 0x01, 0x26, 0x03, 0x74,
0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2f, 0x72, 0x69, 0x6d, 0x2b,
0x63, 0x62, 0x6f, 0x72, 0x04, 0x58, 0x24, 0x6d,
0x65, 0x72, 0x69, 0x61, 0x64, 0x6f, 0x63, 0x2e,
0x62, 0x72, 0x61, 0x6e, 0x64, 0x79, 0x62, 0x75,
0x63, 0x6b, 0x40, 0x62, 0x75, 0x63, 0x6b, 0x6c,
0x61, 0x6e, 0x64, 0x2e, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x08, 0x57, 0xa2, 0x00, 0xa1,
0x00, 0x69, 0x41, 0x43, 0x4d, 0x45, 0x20, 0x4c,
0x74, 0x64, 0x2e, 0x01, 0xa1, 0x01, 0xc1, 0x1a,
0x5f, 0xad, 0x20, 0x56, 0xa0, 0x59, 0x01, 0xbb,
0xd9, 0x01, 0xf5, 0xa2, 0x00, 0x6d, 0x74, 0x65,
0x73, 0x74, 0x20, 0x63, 0x6f, 0x72, 0x69, 0x6d,
0x20, 0x69, 0x64, 0x01, 0x81, 0xd9, 0x01, 0xfa,
0x59, 0x01, 0xa0, 0xa4, 0x00, 0x65, 0x65, 0x6e,
0x2d, 0x47, 0x42, 0x01, 0xa1, 0x00, 0x50, 0x43,
0xbb, 0xe3, 0x7f, 0x2e, 0x61, 0x4b, 0x33, 0xae,
0xd3, 0x53, 0xcf, 0xf1, 0x42, 0x8b, 0x16, 0x02,
0x81, 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, 0x83, 0x00,
0x01, 0x02, 0x04, 0xa1, 0x00, 0x81, 0x82, 0xa1,
0x00, 0xa3, 0x00, 0xd9, 0x02, 0x58, 0x58, 0x20,
0x61, 0x63, 0x6d, 0x65, 0x2d, 0x69, 0x6d, 0x70,
0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x61, 0x74,
0x69, 0x6f, 0x6e, 0x2d, 0x69, 0x64, 0x2d, 0x30,
0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x31,
0x01, 0x64, 0x41, 0x43, 0x4d, 0x45, 0x02, 0x6a,
0x52, 0x6f, 0x61, 0x64, 0x52, 0x75, 0x6e, 0x6e,
0x65, 0x72, 0x83, 0xa2, 0x00, 0xd9, 0x02, 0x58,
0xa3, 0x01, 0x62, 0x42, 0x4c, 0x04, 0x65, 0x32,
0x2e, 0x31, 0x2e, 0x30, 0x05, 0x58, 0x20, 0xac,
0xbb, 0x11, 0xc7, 0xe4, 0xda, 0x21, 0x72, 0x05,
0x52, 0x3c, 0xe4, 0xce, 0x1a, 0x24, 0x5a, 0xe1,
0xa2, 0x39, 0xae, 0x3c, 0x6b, 0xfd, 0x9e, 0x78,
0x71, 0xf7, 0xe5, 0xd8, 0xba, 0xe8, 0x6b, 0x01,
0xa1, 0x02, 0x81, 0x82, 0x01, 0x58, 0x20, 0x87,
0x42, 0x8f, 0xc5, 0x22, 0x80, 0x3d, 0x31, 0x06,
0x5e, 0x7b, 0xce, 0x3c, 0xf0, 0x3f, 0xe4, 0x75,
0x09, 0x66, 0x31, 0xe5, 0xe0, 0x7b, 0xbd, 0x7a,
0x0f, 0xde, 0x60, 0xc4, 0xcf, 0x25, 0xc7, 0xa2,
0x00, 0xd9, 0x02, 0x58, 0xa3, 0x01, 0x64, 0x50,
0x52, 0x6f, 0x54, 0x04, 0x65, 0x31, 0x2e, 0x33,
0x2e, 0x35, 0x05, 0x58, 0x20, 0xac, 0xbb, 0x11,
0xc7, 0xe4, 0xda, 0x21, 0x72, 0x05, 0x52, 0x3c,
0xe4, 0xce, 0x1a, 0x24, 0x5a, 0xe1, 0xa2, 0x39,
0xae, 0x3c, 0x6b, 0xfd, 0x9e, 0x78, 0x71, 0xf7,
0xe5, 0xd8, 0xba, 0xe8, 0x6b, 0x01, 0xa1, 0x02,
0x81, 0x82, 0x01, 0x58, 0x20, 0x02, 0x63, 0x82,
0x99, 0x89, 0xb6, 0xfd, 0x95, 0x4f, 0x72, 0xba,
0xaf, 0x2f, 0xc6, 0x4b, 0xc2, 0xe2, 0xf0, 0x1d,
0x69, 0x2d, 0x4d, 0xe7, 0x29, 0x86, 0xea, 0x80,
0x8f, 0x6e, 0x99, 0x81, 0x3f, 0xa2, 0x00, 0xd9,
0x02, 0x58, 0xa3, 0x01, 0x64, 0x41, 0x52, 0x6f,
0x54, 0x04, 0x65, 0x30, 0x2e, 0x31, 0x2e, 0x34,
0x05, 0x58, 0x20, 0xac, 0xbb, 0x11, 0xc7, 0xe4,
0xda, 0x21, 0x72, 0x05, 0x52, 0x3c, 0xe4, 0xce,
0x1a, 0x24, 0x5a, 0xe1, 0xa2, 0x39, 0xae, 0x3c,
0x6b, 0xfd, 0x9e, 0x78, 0x71, 0xf7, 0xe5, 0xd8,
0xba, 0xe8, 0x6b, 0x01, 0xa1, 0x02, 0x81, 0x82,
0x01, 0x58, 0x20, 0xa3, 0xa5, 0xe7, 0x15, 0xf0,
0xcc, 0x57, 0x4a, 0x73, 0xc3, 0xf9, 0xbe, 0xbb,
0x6b, 0xc2, 0x4f, 0x32, 0xff, 0xd5, 0xb6, 0x7b,
0x38, 0x72, 0x44, 0xc2, 0xc9, 0x09, 0xda, 0x77,
0x9a, 0x14, 0x78, 0x44, 0xde, 0xad, 0xbe, 0xef}

var actual SignedCorim
err := actual.FromCOSE(tv)

// Enhanced validation should detect the outdated schema and return an error
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "tag validation failed")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "tag validation failed")
assert.ErrorContains(t, err, "tag validation failed")

}

func TestSignedCorim_FromCOSE_fail_no_tag(t *testing.T) {
Expand Down
48 changes: 46 additions & 2 deletions corim/unsignedcorim.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,55 @@ type Tag struct {
}

func (o Tag) Valid() error {
// there is no much we can check here, except making sure that the tag is
// not zero-length
// Check that the tag is not zero-length
if len(o.Content) == 0 {
return errors.New("empty tag")
}

// Validate the tag content based on the tag number
switch o.Number {
case ComidTag: // 506 - CoMID tag
return o.validateComidTag()
case CoswidTag: // 505 - CoSWID tag
return o.validateCoswidTag()
default:
// For unknown tags, just ensure the content is valid CBOR
return o.validateGenericCBOR()
}
}

// validateComidTag validates the content of a CoMID tag (506)
func (o Tag) validateComidTag() error {
var c comid.Comid
if err := dm.Unmarshal(o.Content, &c); err != nil {
return fmt.Errorf("invalid CoMID content: %w", err)
}

if err := c.Valid(); err != nil {
return fmt.Errorf("CoMID validation failed: %w", err)
}

return nil
}

// validateCoswidTag validates the content of a CoSWID tag (505)
func (o Tag) validateCoswidTag() error {
var s swid.SoftwareIdentity
if err := dm.Unmarshal(o.Content, &s); err != nil {
return fmt.Errorf("invalid CoSWID content: %w", err)
}

// Basic validation - if unmarshaling succeeded, the structure is valid
// Additional validation could be added here if needed
return nil
}

// validateGenericCBOR validates that content is valid CBOR
func (o Tag) validateGenericCBOR() error {
var raw interface{}
if err := dm.Unmarshal(o.Content, &raw); err != nil {
return fmt.Errorf("invalid CBOR content: %w", err)
}
return nil
}

Expand Down