Skip to content

Commit

Permalink
fix: use generic psatoken decoding
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Fossati <thomas.fossati@linaro.org>
  • Loading branch information
thomas-fossati committed Sep 11, 2024
1 parent bd95775 commit 32ff59a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
4 changes: 2 additions & 2 deletions platform/claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ func Test_CCAPlatform_UnmarshalJSON_negatives(t *testing.T) {
/* 12 */ "testvectors/json/test-instance-id-invalid.json",
/* 13 */ "testvectors/json/test-config-missing.json",
/* 14 */ "testvectors/json/test-hash-algid-missing.json",
/* 15 */ "testvectors/json/test-invalid-psa-claims.json",
}

for i, fn := range tvs {
Expand Down Expand Up @@ -385,7 +386,6 @@ func Test_DecodeUnvalidatedJSONCCAClaims(t *testing.T) {

// invalid
{"testvectors/json/test-no-sw-components.json", &Claims{}},
{"testvectors/json/test-invalid-profile.json", &Claims{}},
{"testvectors/json/test-invalid-psa-claims.json", &Claims{}},
}

Expand All @@ -395,7 +395,7 @@ func Test_DecodeUnvalidatedJSONCCAClaims(t *testing.T) {

v, err := DecodeClaimsFromJSON(buf)

assert.NoError(t, err)
require.NoError(t, err)
assert.IsType(t, tv.Expected, v)
}
}
Expand Down
25 changes: 17 additions & 8 deletions platform/iclaims.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package platform

import (
"encoding/json"
"errors"
"fmt"

"github.com/veraison/psatoken"
Expand Down Expand Up @@ -56,13 +57,17 @@ func DecodeAndValidateClaimsFromCBOR(buf []byte) (IClaims, error) {

// DecodeClaimsFromCBOR unmarshals CCA platform claims from provided CBOR buf.
func DecodeClaimsFromCBOR(buf []byte) (IClaims, error) {
cl := NewClaims()

if err := dm.Unmarshal(buf, cl); err != nil {
i, err := psatoken.DecodeClaimsFromCBOR(buf)
if err != nil {
return nil, err
}

return cl, nil
ic, ok := i.(IClaims)
if !ok {
return nil, errors.New("not a CCA platform token")
}

return ic, nil
}

// DecodeAndValidateClaimsFromJSON unmarshals and validates CCA platform claims
Expand All @@ -82,13 +87,17 @@ func DecodeAndValidateClaimsFromJSON(buf []byte) (IClaims, error) {

// DecodeClaimsFromJSON unmarshals CCA platform claims from provided JSON buf.
func DecodeClaimsFromJSON(buf []byte) (IClaims, error) {
cl := NewClaims()

if err := json.Unmarshal(buf, cl); err != nil {
i, err := psatoken.DecodeClaimsFromJSON(buf)
if err != nil {
return nil, err
}

return cl, nil
ic, ok := i.(IClaims)
if !ok {
return nil, errors.New("not a (JSON-encoded) CCA platform token")
}

return ic, nil
}

// ValidateAndEncodeClaimsToCBOR validates and then marshals CCA platform claims
Expand Down
2 changes: 1 addition & 1 deletion platform/testvectors/json/test-invalid-psa-claims.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"cca-platform-profile": "http://arm.com/PSA-SSD/1.0.0",
"cca-platform-profile": "http://arm.com/CCA-SSD/1.0.0",
"cca-platform-challenge": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=",
"cca-platform-instance-id": "AQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC",
"psa-implementation-id": "AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyA=",
Expand Down

0 comments on commit 32ff59a

Please sign in to comment.