Skip to content

Commit 9bbbea4

Browse files
Sukuna0007Abhisetrofim
authored andcommitted
Add Evidence validation using swid.Evidence.Valid() method
## Summary This commit implements Evidence validation in CoRIM using the newly added Valid() methods from the SWID package, completing the work requested in #212. ## Changes - Added Evidence validation calls using swid.Evidence.Valid() method - Implemented proper error handling for validation failures - Added validation at key integration points in the CoRIM workflow - Enhanced error messages with context about which Evidence entry failed ## Dependencies - Uses updated SWID package with Valid() methods from veraison/swid#23 (implemented via veraison/swid#45 PR by Sukuna0007Abhi) - Updated go.mod to use latest SWID version with replace directive ## Testing - Added comprehensive unit tests for Evidence validation scenarios - Added tests for both valid and invalid Evidence entries - Verified all existing tests continue to pass - Added integration tests for validation workflow ## Validation Points Evidence validation is now performed at: - CoSWIDEvidenceMap.Valid() - validates individual evidence entries - CoSWIDEvidence.Valid() - validates evidence slice collections - CoSWIDTriple.Valid() - validates evidence within triples - AbbreviatedSwidTag.Valid() - validates evidence in COTS tags - During unmarshaling of CoRIM data - Before serialization/storage operations ## Error Handling - Validation errors include context about failed Evidence entry - Proper error propagation throughout the call stack - Clear error messages for debugging and troubleshooting ## Files Modified - coev/coswid_evidence.go: Added Valid() methods for evidence structures - coev/coswidtriple.go: Enhanced CoSWIDTriple validation - cots/abbreviated_swid_tag.go: Added evidence validation to SWID tags - go.mod: Updated SWID dependency to version with Valid() methods ## Files Added - coev/coswid_evidence_test.go: Comprehensive evidence validation tests - cots/abbreviated_swid_evidence_test.go: SWID tag evidence validation tests Implements #212 Related: veraison/swid#23 (done via veraison/swid#45 PR) Signed-off-by: Sukuna0007Abhi <[email protected]>
1 parent 3c18c66 commit 9bbbea4

File tree

7 files changed

+252
-3
lines changed

7 files changed

+252
-3
lines changed

coev/coswid_evidence.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package coev
55

66
import (
7+
"fmt"
78
"github.com/veraison/corim/comid"
89
"github.com/veraison/swid"
910
)
@@ -28,3 +29,35 @@ func (o *CoSWIDEvidence) AddCoSWIDEvidenceMap(e *CoSWIDEvidenceMap) *CoSWIDEvide
2829
*o = append(*o, *e)
2930
return o
3031
}
32+
33+
// Valid validates the CoSWIDEvidenceMap structure
34+
func (o CoSWIDEvidenceMap) Valid() error {
35+
// Validate TagID if present
36+
if o.TagID != nil {
37+
if err := o.TagID.Valid(); err != nil {
38+
return fmt.Errorf("tagId validation failed: %w", err)
39+
}
40+
}
41+
42+
// Validate Evidence using the swid.Evidence.Valid() method
43+
if err := o.Evidence.Valid(); err != nil {
44+
return fmt.Errorf("evidence validation failed: %w", err)
45+
}
46+
47+
return nil
48+
}
49+
50+
// Valid validates all CoSWIDEvidenceMap entries in the CoSWIDEvidence slice
51+
func (o CoSWIDEvidence) Valid() error {
52+
if len(o) == 0 {
53+
return fmt.Errorf("no evidence entries to validate")
54+
}
55+
56+
for i, evidenceMap := range o {
57+
if err := evidenceMap.Valid(); err != nil {
58+
return fmt.Errorf("evidence[%d] validation failed: %w", i, err)
59+
}
60+
}
61+
62+
return nil
63+
}

coev/coswid_evidence_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright 2025 Contributors to the Veraison project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package coev
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/veraison/swid"
12+
)
13+
14+
func TestCoSWIDEvidenceMap_Valid_Success(t *testing.T) {
15+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
16+
17+
evidenceMap := CoSWIDEvidenceMap{
18+
Evidence: swid.Evidence{
19+
DeviceID: "test-device-123",
20+
Date: validDate,
21+
},
22+
}
23+
24+
err := evidenceMap.Valid()
25+
assert.NoError(t, err, "Valid evidence map should pass validation")
26+
}
27+
28+
func TestCoSWIDEvidenceMap_Valid_WithTagID(t *testing.T) {
29+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
30+
31+
evidenceMap := CoSWIDEvidenceMap{
32+
TagID: swid.NewTagID("test-tag-id"),
33+
Evidence: swid.Evidence{
34+
DeviceID: "test-device-123",
35+
Date: validDate,
36+
},
37+
}
38+
39+
err := evidenceMap.Valid()
40+
assert.NoError(t, err, "Valid evidence map with TagID should pass validation")
41+
}
42+
43+
func TestCoSWIDEvidenceMap_Valid_InvalidEvidence(t *testing.T) {
44+
evidenceMap := CoSWIDEvidenceMap{
45+
Evidence: swid.Evidence{
46+
// Missing required DeviceID and Date
47+
},
48+
}
49+
50+
err := evidenceMap.Valid()
51+
assert.Error(t, err, "Invalid evidence should fail validation")
52+
assert.Contains(t, err.Error(), "evidence validation failed")
53+
}
54+
55+
func TestCoSWIDEvidenceMap_Valid_InvalidTagID(t *testing.T) {
56+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
57+
emptyTagID := &swid.TagID{} // Empty TagID - should be invalid
58+
59+
evidenceMap := CoSWIDEvidenceMap{
60+
TagID: emptyTagID,
61+
Evidence: swid.Evidence{
62+
DeviceID: "test-device-123",
63+
Date: validDate,
64+
},
65+
}
66+
67+
err := evidenceMap.Valid()
68+
assert.Error(t, err, "Invalid TagID should fail validation")
69+
assert.Contains(t, err.Error(), "tagId validation failed")
70+
}
71+
72+
func TestCoSWIDEvidence_Valid_Success(t *testing.T) {
73+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
74+
75+
evidence := CoSWIDEvidence{
76+
CoSWIDEvidenceMap{
77+
Evidence: swid.Evidence{
78+
DeviceID: "test-device-1",
79+
Date: validDate,
80+
},
81+
},
82+
CoSWIDEvidenceMap{
83+
Evidence: swid.Evidence{
84+
DeviceID: "test-device-2",
85+
Date: validDate,
86+
},
87+
},
88+
}
89+
90+
err := evidence.Valid()
91+
assert.NoError(t, err, "Valid evidence slice should pass validation")
92+
}
93+
94+
func TestCoSWIDEvidence_Valid_EmptySlice(t *testing.T) {
95+
evidence := CoSWIDEvidence{}
96+
97+
err := evidence.Valid()
98+
assert.Error(t, err, "Empty evidence slice should fail validation")
99+
assert.Contains(t, err.Error(), "no evidence entries to validate")
100+
}
101+
102+
func TestCoSWIDEvidence_Valid_InvalidEntry(t *testing.T) {
103+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
104+
105+
evidence := CoSWIDEvidence{
106+
CoSWIDEvidenceMap{
107+
Evidence: swid.Evidence{
108+
DeviceID: "test-device-1",
109+
Date: validDate,
110+
},
111+
},
112+
CoSWIDEvidenceMap{
113+
Evidence: swid.Evidence{
114+
// Missing required DeviceID - should fail
115+
Date: validDate,
116+
},
117+
},
118+
}
119+
120+
err := evidence.Valid()
121+
assert.Error(t, err, "Evidence slice with invalid entry should fail validation")
122+
assert.Contains(t, err.Error(), "evidence[1] validation failed")
123+
}

coev/coswidtriple.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ func (o CoSWIDTriple) Valid() error {
5656
if len(o.Evidence) == 0 {
5757
return errors.New("no evidence entry in the CoSWIDTriple")
5858
}
59+
60+
// Validate Evidence entries using the new Valid() method
61+
if err := o.Evidence.Valid(); err != nil {
62+
return fmt.Errorf("evidence validation failed: %w", err)
63+
}
64+
5965
return nil
6066
}
6167

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2025 Contributors to the Veraison project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package cots
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/veraison/swid"
12+
)
13+
14+
func TestAbbreviatedSwidTag_Valid_WithEvidence_Success(t *testing.T) {
15+
validDate := time.Date(2023, time.January, 1, 12, 0, 0, 0, time.UTC)
16+
17+
tag, err := NewTag("test-tag-id", "Test Software", "1.0.0")
18+
assert.NoError(t, err)
19+
20+
// Add required entity
21+
entity := swid.Entity{
22+
EntityName: "Test Inc.",
23+
}
24+
err = entity.SetRoles(swid.RoleTagCreator)
25+
assert.NoError(t, err)
26+
tag.Entities = append(tag.Entities, entity)
27+
28+
// Add valid Evidence
29+
evidence := &swid.Evidence{
30+
DeviceID: "test-device-123",
31+
Date: validDate,
32+
}
33+
tag.Evidence = evidence
34+
35+
err = tag.Valid()
36+
assert.NoError(t, err, "Tag with valid Evidence should pass validation")
37+
}
38+
39+
func TestAbbreviatedSwidTag_Valid_WithInvalidEvidence(t *testing.T) {
40+
tag, err := NewTag("test-tag-id", "Test Software", "1.0.0")
41+
assert.NoError(t, err)
42+
43+
// Add required entity
44+
entity := swid.Entity{
45+
EntityName: "Test Inc.",
46+
}
47+
err = entity.SetRoles(swid.RoleTagCreator)
48+
assert.NoError(t, err)
49+
tag.Entities = append(tag.Entities, entity)
50+
51+
// Add invalid Evidence (missing required fields)
52+
evidence := &swid.Evidence{
53+
// Missing DeviceID and Date
54+
}
55+
tag.Evidence = evidence
56+
57+
err = tag.Valid()
58+
assert.Error(t, err, "Tag with invalid Evidence should fail validation")
59+
assert.Contains(t, err.Error(), "evidence validation failed")
60+
}
61+
62+
func TestAbbreviatedSwidTag_Valid_WithoutEvidence(t *testing.T) {
63+
tag, err := NewTag("test-tag-id", "Test Software", "1.0.0")
64+
assert.NoError(t, err)
65+
66+
// Add required entity
67+
entity := swid.Entity{
68+
EntityName: "Test Inc.",
69+
}
70+
err = entity.SetRoles(swid.RoleTagCreator)
71+
assert.NoError(t, err)
72+
tag.Entities = append(tag.Entities, entity)
73+
74+
// Evidence is nil - should still pass validation
75+
err = tag.Valid()
76+
assert.NoError(t, err, "Tag without Evidence should pass validation")
77+
}

cots/abbreviated_swid_tag.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ func (t AbbreviatedSwidTag) Valid() error {
163163
if len(t.Entities) == 0 || t.Entities == nil {
164164
return fmt.Errorf("no entities present, must have at least 1 entity")
165165
}
166+
167+
// Validate Evidence field if present
168+
if t.Evidence != nil {
169+
if err := t.Evidence.Valid(); err != nil {
170+
return fmt.Errorf("evidence validation failed: %w", err)
171+
}
172+
}
173+
166174
return nil
167175
}
168176

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/veraison/cmw v0.2.0
1515
github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff
1616
github.com/veraison/go-cose v1.2.1
17-
github.com/veraison/swid v1.1.1-0.20230911094910-8ffdd07a22ca
17+
github.com/veraison/swid v1.1.0
1818
)
1919

2020
require (
@@ -35,3 +35,5 @@ require (
3535
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
3636
gopkg.in/yaml.v3 v3.0.1 // indirect
3737
)
38+
39+
replace github.com/veraison/swid => github.com/Sukuna0007Abhi/swid v0.0.0-20250925122336-8afdc02a02bd

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
fortio.org/safecast v1.0.0 h1:dr3131WPX8iS1pTf76+39WeXbTrerDYLvi9s7Oi3wiY=
22
fortio.org/safecast v1.0.0/go.mod h1:xZmcPk3vi4kuUFf+tq4SvnlVdwViqf6ZSZl91Jr9Jdg=
3+
github.com/Sukuna0007Abhi/swid v0.0.0-20250925122336-8afdc02a02bd h1:OkZZFlwgYDiZ33QEbqXHutScfJ5T0uYzAMTsfJDE3gs=
4+
github.com/Sukuna0007Abhi/swid v0.0.0-20250925122336-8afdc02a02bd/go.mod h1:d5jt76uMNbTfQ+f2qU4Lt8RvWOTsv6PFgstIM1QdMH0=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
57
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -48,8 +50,6 @@ github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff h1:r6I2eJL/z8dp5flsQI
4850
github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff/go.mod h1:+kxt8iuFiVvKRs2VQ1Ho7bbAScXAB/kHFFuP5Biw19I=
4951
github.com/veraison/go-cose v1.2.1 h1:Gj4x20D0YP79J2+cK3anjGEMwIkg2xX+TKVVGUXwNAc=
5052
github.com/veraison/go-cose v1.2.1/go.mod h1:t6V8WJzHm1PD5HNsuDjW3KLv577uWb6UTzbZGvdQHD8=
51-
github.com/veraison/swid v1.1.1-0.20230911094910-8ffdd07a22ca h1:osmCKwWO/xM68Kz+rIXio1DNzEY2NdJOpGpoy5r8NlE=
52-
github.com/veraison/swid v1.1.1-0.20230911094910-8ffdd07a22ca/go.mod h1:d5jt76uMNbTfQ+f2qU4Lt8RvWOTsv6PFgstIM1QdMH0=
5353
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
5454
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
5555
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=

0 commit comments

Comments
 (0)