Skip to content

Commit

Permalink
handle the JSON decoding path
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 10, 2024
1 parent dc007e7 commit 0d63631
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 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
20 changes: 15 additions & 5 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 @@ -61,7 +62,12 @@ func DecodeClaimsFromCBOR(buf []byte) (IClaims, error) {
return nil, err
}

return i.(IClaims), 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 @@ -81,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 0d63631

Please sign in to comment.