|
| 1 | +// Copyright 2026 Contributors to the Veraison project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package psa |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "github.com/veraison/corim/comid" |
| 15 | + "github.com/veraison/corim/corim" |
| 16 | + "github.com/veraison/eat" |
| 17 | +) |
| 18 | + |
| 19 | +// getTestcasePath returns the absolute path to a testcase file |
| 20 | +func getTestcasePath(filename string) string { |
| 21 | + _, thisFile, _, ok := runtime.Caller(0) |
| 22 | + if !ok { |
| 23 | + panic("failed to get current file path") |
| 24 | + } |
| 25 | + testcasesDir := filepath.Join(filepath.Dir(thisFile), "testcases") |
| 26 | + return filepath.Join(testcasesDir, filename) |
| 27 | +} |
| 28 | + |
| 29 | +// loadTestcase loads a CBOR testcase file |
| 30 | +func loadTestcase(t *testing.T, filename string) []byte { |
| 31 | + path := getTestcasePath(filename) |
| 32 | + data, err := os.ReadFile(path) |
| 33 | + require.NoError(t, err, "failed to load testcase %s", filename) |
| 34 | + return data |
| 35 | +} |
| 36 | + |
| 37 | +// getComidFromCorim extracts and decodes the first CoMID from a CoRIM |
| 38 | +// using the PSA profile extensions |
| 39 | +func getComidFromCorim(t *testing.T, corimData []byte) *comid.Comid { |
| 40 | + // First, parse the CoRIM to extract the tags |
| 41 | + var c corim.UnsignedCorim |
| 42 | + err := c.FromCBOR(corimData) |
| 43 | + require.NoError(t, err, "failed to parse CoRIM") |
| 44 | + require.Greater(t, len(c.Tags), 0, "CoRIM must have at least one tag") |
| 45 | + |
| 46 | + // Get a CoMID with PSA profile extensions registered |
| 47 | + profileID, err := eat.NewProfile(ProfileURI) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + manifest, found := corim.GetProfileManifest(profileID) |
| 51 | + require.True(t, found, "PSA profile should be registered") |
| 52 | + |
| 53 | + // Create a Comid with extensions |
| 54 | + m := manifest.GetComid() |
| 55 | + |
| 56 | + // Decode the first tag (which should be a CoMID, tag 506) |
| 57 | + require.Equal(t, uint64(506), c.Tags[0].Number, "first tag should be a CoMID (506)") |
| 58 | + err = m.FromCBOR(c.Tags[0].Content) |
| 59 | + require.NoError(t, err, "failed to decode CoMID from tag") |
| 60 | + |
| 61 | + return m |
| 62 | +} |
| 63 | + |
| 64 | +// TestCoRIMIntegration_ValidPSA tests that a valid PSA CoRIM: |
| 65 | +// - Parses successfully |
| 66 | +// - The PSA profile is recognized and loaded |
| 67 | +// - CoMID validation passes with PSA constraints |
| 68 | +func TestCoRIMIntegration_ValidPSA(t *testing.T) { |
| 69 | + data := loadTestcase(t, "psa-valid.cbor") |
| 70 | + |
| 71 | + // Extract and decode CoMID with PSA extensions |
| 72 | + m := getComidFromCorim(t, data) |
| 73 | + |
| 74 | + // Validate the CoMID (should pass all PSA constraints) |
| 75 | + err := m.Valid() |
| 76 | + assert.NoError(t, err, "valid PSA CoMID should pass validation") |
| 77 | +} |
| 78 | + |
| 79 | +// TestCoRIMIntegration_InvalidImplementationID tests that a CoRIM with invalid |
| 80 | +// Implementation ID (wrong length) is rejected by PSA profile validation. |
| 81 | +// The CoRIM structure itself is valid, but fails PSA profile constraints. |
| 82 | +func TestCoRIMIntegration_InvalidImplementationID(t *testing.T) { |
| 83 | + data := loadTestcase(t, "psa-invalid-impl-id.cbor") |
| 84 | + |
| 85 | + // Extract and decode CoMID with PSA extensions |
| 86 | + m := getComidFromCorim(t, data) |
| 87 | + |
| 88 | + // Validation should FAIL due to invalid Implementation ID |
| 89 | + err := m.Valid() |
| 90 | + require.Error(t, err, "PSA validation should fail for invalid implementation-id") |
| 91 | + assert.Contains(t, err.Error(), "implementation-id", |
| 92 | + "error should mention implementation-id") |
| 93 | + assert.Contains(t, err.Error(), "32 bytes", |
| 94 | + "error should mention expected length") |
| 95 | +} |
| 96 | + |
| 97 | +// TestCoRIMIntegration_InvalidAttestVerifKey tests that a CoRIM with invalid |
| 98 | +// AttestVerifKeys (multiple keys instead of one) is rejected by PSA profile validation. |
| 99 | +// The CoRIM structure itself is valid, but fails PSA profile constraints. |
| 100 | +func TestCoRIMIntegration_InvalidAttestVerifKey(t *testing.T) { |
| 101 | + data := loadTestcase(t, "psa-invalid-attest-key.cbor") |
| 102 | + |
| 103 | + // Extract and decode CoMID with PSA extensions |
| 104 | + m := getComidFromCorim(t, data) |
| 105 | + |
| 106 | + // Validation should FAIL due to multiple attestation keys |
| 107 | + err := m.Valid() |
| 108 | + require.Error(t, err, "PSA validation should fail for multiple attest keys") |
| 109 | + assert.Contains(t, err.Error(), "verification-keys", |
| 110 | + "error should mention verification-keys") |
| 111 | + assert.Contains(t, err.Error(), "exactly one", |
| 112 | + "error should mention exactly one key required") |
| 113 | +} |
| 114 | + |
| 115 | +// TestCoRIMIntegration_NoProfile tests that a CoRIM without a profile: |
| 116 | +// - Parses successfully |
| 117 | +// - Validation passes without PSA extensions (base validation only) |
| 118 | +// This serves as a control case - the same CoMID that fails with PSA profile |
| 119 | +// should pass without it. |
| 120 | +func TestCoRIMIntegration_NoProfile(t *testing.T) { |
| 121 | + data := loadTestcase(t, "no-profile.cbor") |
| 122 | + |
| 123 | + // Parse the CoRIM |
| 124 | + var c corim.UnsignedCorim |
| 125 | + err := c.FromCBOR(data) |
| 126 | + require.NoError(t, err, "profile-less CoRIM should parse without error") |
| 127 | + |
| 128 | + // Verify no profile is set |
| 129 | + assert.Nil(t, c.Profile, "profile should not be present") |
| 130 | + |
| 131 | + // Decode CoMID WITHOUT PSA extensions (plain CoMID) |
| 132 | + require.Greater(t, len(c.Tags), 0, "CoRIM must have at least one tag") |
| 133 | + m := comid.NewComid() |
| 134 | + err = m.FromCBOR(c.Tags[0].Content) |
| 135 | + require.NoError(t, err, "failed to decode CoMID") |
| 136 | + |
| 137 | + // Validation should pass (no PSA profile constraints applied) |
| 138 | + err = m.Valid() |
| 139 | + assert.NoError(t, err, "profile-less CoMID should pass base validation") |
| 140 | +} |
0 commit comments