Skip to content

Commit 7c4fb9c

Browse files
committed
cca/psa create: add flag to bypass validation
Add -I/--allow-invalid flag to bypass validation during token creation. This allows creating in some way invalid, but otherwise well-constructed tokens for testing. Signed-off-by: Sergei Trofimov <[email protected]>
1 parent 758bd1f commit 7c4fb9c

File tree

6 files changed

+119
-19
lines changed

6 files changed

+119
-19
lines changed

cmd/cca/common.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ import (
1212
"github.com/veraison/psatoken"
1313
)
1414

15-
func loadCCAClaimsFromFile(fs afero.Fs, fn string) (*ccatoken.Evidence, error) {
15+
func loadCCAClaimsFromFile(fs afero.Fs, fn string, validate bool) (*ccatoken.Evidence, error) {
1616
buf, err := afero.ReadFile(fs, fn)
1717
if err != nil {
1818
return nil, err
1919
}
2020

2121
var e ccatoken.Evidence
22-
if err := e.UnmarshalJSON(buf); err != nil {
23-
return nil, err
22+
if validate {
23+
if err := e.UnmarshalJSON(buf); err != nil {
24+
return nil, err
25+
}
26+
} else {
27+
if err := e.UnmarshalUnvalidatedJSON(buf); err != nil {
28+
return nil, err
29+
}
2430
}
2531

2632
return &e, nil

cmd/cca/create.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
)
1313

1414
var (
15-
createClaimsFile *string
16-
createRAKFile *string
17-
createPAKFile *string
18-
createTokenFile *string
15+
createClaimsFile *string
16+
createRAKFile *string
17+
createPAKFile *string
18+
createTokenFile *string
19+
allowInvalidClaims *bool
1920
)
2021

2122
var createCmd = NewCreateCmd(common.Fs)
@@ -26,14 +27,16 @@ func NewCreateCmd(fs afero.Fs) *cobra.Command {
2627
Short: "create a CCA attestation token from the supplied claims and keys",
2728
Long: `Create a CCA attestation token from the JSON-encoded claims and
2829
keys (PAK and RAK)
29-
30+
3031
Create a CCA attestation token from claims contained in claims.json, sign
3132
with pak.jwk and rak.jwk and save the result to my.cbor:
32-
33+
3334
evcli cca create --claims=claims.json --pak=pak.jwk --rak=rak.jwk --token=my.cbor
3435
`,
3536
RunE: func(cmd *cobra.Command, args []string) error {
36-
evidence, err := loadCCAClaimsFromFile(fs, *createClaimsFile)
37+
validate := !*allowInvalidClaims
38+
39+
evidence, err := loadCCAClaimsFromFile(fs, *createClaimsFile, validate)
3740
if err != nil {
3841
return fmt.Errorf(
3942
"error loading CCA claims from %s: %w",
@@ -73,7 +76,14 @@ with pak.jwk and rak.jwk and save the result to my.cbor:
7376
)
7477
}
7578

76-
b, err := evidence.Sign(pSigner, rSigner)
79+
var b []byte
80+
if validate {
81+
b, err = evidence.Sign(pSigner, rSigner)
82+
83+
} else {
84+
b, err = evidence.SignUnvalidated(pSigner, rSigner)
85+
}
86+
7787
if err != nil {
7888
return fmt.Errorf("error signing evidence: %w", err)
7989
}
@@ -110,6 +120,12 @@ with pak.jwk and rak.jwk and save the result to my.cbor:
110120
"token", "t", "", "name of the file where the produced CCA attestation token will be stored",
111121
)
112122

123+
allowInvalidClaims = cmd.Flags().BoolP(
124+
"allow-invalid", "I", false,
125+
"Do not validate provided claims, allowing invalid tokens to be generated. "+
126+
"This is intended for testing.",
127+
)
128+
113129
return cmd
114130
}
115131

cmd/psa/create.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
createKeyFile *string
1919
createTokenFile *string
2020
createTokenProfile *string
21+
allowInvalidClaims *bool
2122
)
2223

2324
var createCmd = NewCreateCmd(common.Fs)
@@ -28,12 +29,12 @@ func NewCreateCmd(fs afero.Fs) *cobra.Command {
2829
Short: "create a PSA attestation token from the supplied claims and IAK",
2930
Long: `Create a PSA attestation token from the JSON-encoded claims and
3031
Initial Attestation Key, optionally specifying the wanted profile
31-
32+
3233
Create a PSA attestation token from claims contained in claims.json, sign
3334
with es256.jwk and save the result to my.cbor:
34-
35+
3536
evcli psa create --claims=claims.json --key=es256.jwk --token=my.cbor
36-
37+
3738
Or, equivalently:
3839
3940
evcli psa create -c claims.json -k es256.jwk -t my.cbor
@@ -47,12 +48,13 @@ te-profile1.cbor:
4748
Note that the default profile is http://arm.com/psa/2.0.0.
4849
`,
4950
RunE: func(cmd *cobra.Command, args []string) error {
51+
validate := !*allowInvalidClaims
52+
5053
if err := checkProfile(createTokenProfile); err != nil {
5154
return err
5255
}
5356

54-
validateClaims := true
55-
claims, err := loadClaimsFromFile(fs, *createClaimsFile, validateClaims)
57+
claims, err := loadClaimsFromFile(fs, *createClaimsFile, validate)
5658
if err != nil {
5759
return err
5860
}
@@ -68,8 +70,12 @@ Note that the default profile is http://arm.com/psa/2.0.0.
6870

6971
evidence := psatoken.Evidence{}
7072

71-
if err = evidence.SetClaims(claims); err != nil {
72-
return err
73+
if validate {
74+
if err = evidence.SetClaims(claims); err != nil {
75+
return err
76+
}
77+
} else {
78+
evidence.Claims = claims
7379
}
7480

7581
key, err := afero.ReadFile(fs, *createKeyFile)
@@ -82,7 +88,12 @@ Note that the default profile is http://arm.com/psa/2.0.0.
8288
return fmt.Errorf("error decoding signing key from %s: %w", *createKeyFile, err)
8389
}
8490

85-
cwt, err := evidence.Sign(signer)
91+
var cwt []byte
92+
if validate {
93+
cwt, err = evidence.Sign(signer)
94+
} else {
95+
cwt, err = evidence.SignUnvalidated(signer)
96+
}
8697
if err != nil {
8798
return fmt.Errorf("signature failed: %w", err)
8899
}
@@ -116,6 +127,12 @@ Note that the default profile is http://arm.com/psa/2.0.0.
116127
"profile", "p", psatoken.PsaProfile2, "name of the PSA profile to use",
117128
)
118129

130+
allowInvalidClaims = cmd.Flags().BoolP(
131+
"allow-invalid", "I", false,
132+
"Do not validate provided claims, allowing invalid tokens to be generated. "+
133+
"This is intended for testing.",
134+
)
135+
119136
return cmd
120137
}
121138

cmd/psa/create_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ func Test_CreateCmd_WithP1Claims_ok(t *testing.T) {
5454
assert.NoError(t, err)
5555
}
5656

57+
func Test_CreateCmd_allowInivalid_ok(t *testing.T) {
58+
fs := afero.NewMemMapFs()
59+
60+
err := afero.WriteFile(fs, "es256.jwk", testValidKey, 0644)
61+
require.NoError(t, err)
62+
63+
err = afero.WriteFile(fs, "claims.json", testValidP2PSAClaimsWithNonce, 0644)
64+
require.NoError(t, err)
65+
66+
cmd := NewCreateCmd(fs)
67+
cmd.SetArgs(
68+
[]string{
69+
"--claims=claims.json",
70+
"--key=es256.jwk",
71+
"--allow-invalid",
72+
},
73+
)
74+
75+
err = cmd.Execute()
76+
assert.NoError(t, err)
77+
}
78+
5779
func Test_CreateCmd_claims_not_found(t *testing.T) {
5880
fs := afero.NewMemMapFs()
5981

misc/cca-claims-bad-norealm.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"cca-platform-token": {
3+
"cca-platform-profile": "http://arm.com/CCA-SSD/1.0.0",
4+
"cca-platform-implementation-id": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
5+
"cca-platform-instance-id": "AQICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC",
6+
"cca-platform-config": "AQID",
7+
"cca-platform-lifecycle": 12288,
8+
"cca-platform-sw-components": [
9+
{
10+
"measurement-value": "AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM=",
11+
"signer-id": "BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ="
12+
}
13+
],
14+
"cca-platform-service-indicator": "https://veraison.example/v1/challenge-response",
15+
"cca-platform-hash-algo-id": "sha-256"
16+
}
17+
}

misc/psa-claims-bad-no-implid.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"eat-profile": "http://arm.com/psa/2.0.0",
3+
"psa-client-id": 1,
4+
"psa-security-lifecycle": 12288,
5+
"psa-boot-seed": "3q2+796tvu/erb7v3q2+796tvu/erb7v3q2+796tvu8=",
6+
"psa-hardware-version": "1234567890123",
7+
"psa-software-components": [
8+
{
9+
"measurement-type": "BL",
10+
"measurement-value": "AAECBAABAgQAAQIEAAECBAABAgQAAQIEAAECBAABAgQ=",
11+
"signer-id": "UZIA/1GSAP9RkgD/UZIA/1GSAP9RkgD/UZIA/1GSAP8="
12+
},
13+
{
14+
"measurement-type": "PRoT",
15+
"measurement-value": "BQYHCAUGBwgFBgcIBQYHCAUGBwgFBgcIBQYHCAUGBwg=",
16+
"signer-id": "UZIA/1GSAP9RkgD/UZIA/1GSAP9RkgD/UZIA/1GSAP8="
17+
}
18+
],
19+
"psa-instance-id": "AaChoqOgoaKjoKGio6ChoqOgoaKjoKGio6ChoqOgoaKj",
20+
"psa-verification-service-indicator": "https://psa-verifier.org",
21+
"psa-nonce": "QUp8F0FBs9DpodKK8xUg8NQimf6sQAfe2J1ormzZLxk="
22+
}

0 commit comments

Comments
 (0)