Skip to content

Commit 29f29ec

Browse files
committed
chore: update GWX packages to V3
This change includes: - go mod tidy - go fmt - update tests with new error messages - code changes for "Change JWT accessors from returning T to returning (T, bool)" - code changes for "Remove iterators from JWX" and as a result removing Token.PrivateClaims() method. Signed-off-by: Anton Antonov <Anton.Antonov@arm.com>
1 parent b3d0080 commit 29f29ec

10 files changed

Lines changed: 150 additions & 147 deletions

File tree

arc/cmd/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ package cmd
55
import (
66
"strings"
77

8-
"github.com/lestrrat-go/jwx/v2/jwa"
8+
"github.com/lestrrat-go/jwx/v3/jwa"
99
)
1010

1111
func algList() string {
1212
var l []string // nolint: prealloc
1313

1414
for _, a := range jwa.SignatureAlgorithms() {
15-
l = append(l, string(a))
15+
l = append(l, a.String())
1616
}
1717

1818
return strings.Join(l, ", ")

arc/cmd/create.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"errors"
77
"fmt"
88

9-
"github.com/lestrrat-go/jwx/v2/jwa"
10-
"github.com/lestrrat-go/jwx/v2/jwk"
9+
"github.com/lestrrat-go/jwx/v3/jwa"
10+
"github.com/lestrrat-go/jwx/v3/jwk"
1111
"github.com/spf13/afero"
1212
"github.com/spf13/cobra"
1313
"github.com/veraison/ear"
@@ -64,7 +64,11 @@ the key in the default key file "skey.json", and save the result to "my-ear.jwt"
6464
return fmt.Errorf("parsing signing key from %q: %w", createSKey, err)
6565
}
6666

67-
if arBytes, err = ar.Sign(jwa.KeyAlgorithmFrom(createAlg), sigK); err != nil {
67+
alg, err := jwa.KeyAlgorithmFrom(createAlg); if err != nil {
68+
return fmt.Errorf("parsing algorithm from %q: %w", createAlg, err)
69+
}
70+
71+
if arBytes, err = ar.Sign(alg, sigK); err != nil {
6872
return fmt.Errorf("signing EAR: %w", err)
6973
}
7074

arc/cmd/create_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func Test_CreateCmd_skey_file_bad_format(t *testing.T) {
6666
}
6767
cmd.SetArgs(args)
6868

69-
expectedErr := `parsing signing key from "empty-skey.json": failed to unmarshal JSON into key hint: EOF`
69+
expectedErr := `parsing signing key from "empty-skey.json": jwk.Parse: failed to probe data: probe: failed to unmarshal data: EOF`
7070

7171
err := cmd.Execute()
7272
assert.EqualError(t, err, expectedErr)
@@ -89,7 +89,7 @@ func Test_CreateCmd_skey_not_ok_for_signing(t *testing.T) {
8989
}
9090
cmd.SetArgs(args)
9191

92-
expectedErr := `failed to generate signature for signer #0 (alg=ES256): failed to sign payload: failed to retrieve ecdsa.PrivateKey out of *jwk.ecdsaPublicKey: failed to produce ecdsa.PrivateKey from *jwk.ecdsaPublicKey: argument to AssignIfCompatible() must be compatible with *ecdsa.PublicKey (was *ecdsa.PrivateKey)`
92+
expectedErr := `invalid key type *jwk.ecdsaPublicKey. ecdsa.PrivateKey is required: keyconv: failed to produce ecdsa.PrivateKey from *jwk.ecdsaPublicKey`
9393

9494
err := cmd.Execute()
9595
assert.ErrorContains(t, err, expectedErr)
@@ -154,7 +154,7 @@ func Test_CreateCmd_unknown_signing_alg(t *testing.T) {
154154
}
155155
cmd.SetArgs(args)
156156

157-
expectedErr := `expected algorithm to be of type jwa.SignatureAlgorithm but got ("XYZ", jwa.InvalidKeyAlgorithm)`
157+
expectedErr := `invalid key value: "XYZ": invalid key algorithm`
158158

159159
err := cmd.Execute()
160160
assert.ErrorContains(t, err, expectedErr)

arc/cmd/verify.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"errors"
77
"fmt"
88

9-
"github.com/lestrrat-go/jwx/v2/jwa"
10-
"github.com/lestrrat-go/jwx/v2/jwk"
9+
"github.com/lestrrat-go/jwx/v3/jwa"
10+
"github.com/lestrrat-go/jwx/v3/jwk"
1111
"github.com/spf13/afero"
1212
"github.com/spf13/cobra"
1313
"github.com/veraison/ear"
@@ -62,7 +62,11 @@ embedded EAR claims-set and present a report of the trustworthiness vector.
6262
return fmt.Errorf("parsing verification key from %q: %w", verifyPKey, err)
6363
}
6464

65-
if err = ar.Verify(arBytes, jwa.KeyAlgorithmFrom(verifyAlg), vfyK); err != nil {
65+
alg, err := jwa.KeyAlgorithmFrom(verifyAlg); if err != nil {
66+
return fmt.Errorf("parsing algorithm from %q: %w", verifyAlg, err)
67+
}
68+
69+
if err = ar.Verify(arBytes, alg, vfyK); err != nil {
6670
return fmt.Errorf("verifying signed EAR from %s: %w", verifyInput, err)
6771
}
6872

arc/cmd/verify_test.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,29 +64,7 @@ func Test_VerifyCmd_pkey_file_bad_format(t *testing.T) {
6464
}
6565
cmd.SetArgs(args)
6666

67-
expectedErr := `parsing verification key from "empty-pkey.json": failed to unmarshal JSON into key hint: EOF`
68-
69-
err := cmd.Execute()
70-
assert.EqualError(t, err, expectedErr)
71-
}
72-
73-
func Test_VerifyCmd_skey_not_ok_for_verifying(t *testing.T) {
74-
cmd := NewVerifyCmd()
75-
76-
files := []fileEntry{
77-
{"ear.jwt", testJWT},
78-
{"skey.json", testSKey},
79-
}
80-
makeFS(t, files)
81-
82-
args := []string{
83-
"--pkey=skey.json",
84-
"--alg=ES256",
85-
"ear.jwt",
86-
}
87-
cmd.SetArgs(args)
88-
89-
expectedErr := `verifying signed EAR from ear.jwt: failed verifying JWT message: could not verify message using any of the signatures or keys`
67+
expectedErr := `parsing verification key from "empty-pkey.json": jwk.Parse: failed to probe data: probe: failed to unmarshal data: EOF`
9068

9169
err := cmd.Execute()
9270
assert.EqualError(t, err, expectedErr)
@@ -129,7 +107,7 @@ func Test_VerifyCmd_input_file_bad_format(t *testing.T) {
129107
}
130108
cmd.SetArgs(args)
131109

132-
expectedErr := `verifying signed EAR from ear.jwt: failed verifying JWT message: failed to parse jws: invalid byte sequence`
110+
expectedErr := `verifying signed EAR from ear.jwt: failed verifying JWT message: jwt.Parse: failed to parse token: jwt.verifyFast: failed to split compact: jwsbb: invalid number of segments`
133111

134112
err := cmd.Execute()
135113
assert.EqualError(t, err, expectedErr)
@@ -151,7 +129,7 @@ func Test_VerifyCmd_unknown_verification_alg(t *testing.T) {
151129
}
152130
cmd.SetArgs(args)
153131

154-
expectedErr := `verifying signed EAR from ear.jwt: failed verifying JWT message: WithKey() option must be specified using jwa.SignatureAlgorithm (got jwa.InvalidKeyAlgorithm)`
132+
expectedErr := `parsing algorithm from "XYZ": invalid key value: "XYZ": invalid key algorithm`
155133

156134
err := cmd.Execute()
157135
assert.EqualError(t, err, expectedErr)

doc.go

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,60 @@ The mandatory attributes are: status, issued_at, profile, submods, and verifier_
1313
For example, a simple AttestationResult payload with only the bare minimum
1414
claims could be created as follows:
1515
16-
myStatus := TrustTierAffirming
17-
myTimestamp := time.Now().Unix()
18-
myPolicyID := `https://veraison.example/policy/1A4DF345-B512-4F3B-8461-967DE7F60ECA`
19-
myProfile := EatProfile
20-
verifierBuild := "verifier-build"
21-
verifierDeveloper := "verifier-developer"
22-
23-
ar := AttestationResult{
24-
Profile: &myProfile,
25-
IssuedAt: &myTimestamp,
26-
Submods: map[string]*Appraisal{
27-
"submodName": {
28-
TrustVector: &TrustVector{},
29-
Status: &myStatus,
30-
AppraisalPolicyID: &myPolicyID,
31-
},
32-
},
33-
VerifierID: &VerifierIdentity{
34-
Build: &verifierBuild,
35-
Developer: &verifierDeveloper,
36-
},
37-
}
16+
myStatus := TrustTierAffirming
17+
myTimestamp := time.Now().Unix()
18+
myPolicyID := `https://veraison.example/policy/1A4DF345-B512-4F3B-8461-967DE7F60ECA`
19+
myProfile := EatProfile
20+
verifierBuild := "verifier-build"
21+
verifierDeveloper := "verifier-developer"
22+
23+
ar := AttestationResult{
24+
Profile: &myProfile,
25+
IssuedAt: &myTimestamp,
26+
Submods: map[string]*Appraisal{
27+
"submodName": {
28+
TrustVector: &TrustVector{},
29+
Status: &myStatus,
30+
AppraisalPolicyID: &myPolicyID,
31+
},
32+
},
33+
VerifierID: &VerifierIdentity{
34+
Build: &verifierBuild,
35+
Developer: &verifierDeveloper,
36+
},
37+
}
3838
3939
A richer one would normally include the Trustworthiness Vector, which provides
4040
details about the appraised attester components. In the example below, the
4141
attester has been assessed as genuine, i.e., all claims are in the "affirming"
4242
range. (See §2.3 of draft-ietf-rats-ar4si-03 for details about the allowed values
4343
and their meaning.)
4444
45-
tv := TrustVector{
46-
InstanceIdentity: 2,
47-
Configuration: 2,
48-
Executables: 2,
49-
Hardware: 2,
50-
}
45+
tv := TrustVector{
46+
InstanceIdentity: 2,
47+
Configuration: 2,
48+
Executables: 2,
49+
Hardware: 2,
50+
}
5151
52-
ar.Submods["submodName"].TrustVector = &tv
52+
ar.Submods["submodName"].TrustVector = &tv
5353
5454
# Signing and Serializing
5555
5656
Once the AttestationResult is populated, it can be signed (i.e., wrapped in a
5757
JWT) by invoking the Sign method:
5858
59-
myECDSAPrivateKey := `{
60-
"kty": "EC",
61-
"crv": "P-256",
62-
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
63-
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
64-
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
65-
}`
59+
myECDSAPrivateKey := `{
60+
"kty": "EC",
61+
"crv": "P-256",
62+
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
63+
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4",
64+
"d": "V8kgd2ZBRuh2dgyVINBUqpPDr7BOMGcF22CQMIUHtNM"
65+
}`
6666
67-
sigK, _ := jwk.ParseKey([]byte(myECDSAPrivateKey))
67+
sigK, _ := jwk.ParseKey([]byte(myECDSAPrivateKey))
6868
69-
buf, _ := ar.Sign(jwa.ES256, sigK)
69+
buf, _ := ar.Sign(jwa.ES256, sigK)
7070
7171
In this case, the returned buf contains a signed ES256 JWT with the JSON
7272
serialization of the AttestationResult object as its payload. This is the usual
@@ -78,38 +78,38 @@ On the consumer end of the protocol, when the EAT containing the attestation
7878
result is received from a veraison verifier, the relying party needs to first
7979
parse it and verify the signature using the Verify method:
8080
81-
myECDSAPublicKey := `{
82-
"kty": "EC",
83-
"crv": "P-256",
84-
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
85-
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4"
86-
}`
81+
myECDSAPublicKey := `{
82+
"kty": "EC",
83+
"crv": "P-256",
84+
"x": "usWxHK2PmfnHKwXPS54m0kTcGJ90UiglWiGahtagnv8",
85+
"y": "IBOL-C3BttVivg-lSreASjpkttcsz-1rb7btKLv8EX4"
86+
}`
8787
88-
vfyK, _ := jwk.ParseKey([]byte(myECDSAPublicKey))
88+
vfyK, _ := jwk.ParseKey([]byte(myECDSAPublicKey))
8989
90-
var ar AttestationResult
90+
var ar AttestationResult
9191
92-
err := ar.Verify(token, jwa.ES256, vfyK)
93-
if err != nil {
94-
// handle verification error
95-
}
92+
err := ar.Verify(token, jwa.ES256, vfyK)
93+
if err != nil {
94+
// handle verification error
95+
}
9696
9797
If there are no errors, the relying party can trust the attestation result and
9898
inspect the relevant fields to decide about the trustworthiness of the attested
9999
entity.
100100
101-
if *ar.Submods["submodName"].Status != TrustTierAffirming {
102-
// handle troubles with appraisal
103-
}
101+
if *ar.Submods["submodName"].Status != TrustTierAffirming {
102+
// handle troubles with appraisal
103+
}
104104
105105
# Pretty printing
106106
107107
The package provides a Report method that allows pretty printing of the
108108
Trustworthiness Vector. The caller can request a short summary or a detailed
109109
printout, as well as using colors when displaying the claims' values.
110110
111-
short, color := true, true
111+
short, color := true, true
112112
113-
fmt.Print(ar.Submods["submodName"].TrustVector.Report(short, color))
113+
fmt.Print(ar.Submods["submodName"].TrustVector.Report(short, color))
114114
*/
115115
package ear

ear.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"strings"
1212
"time"
1313

14-
"github.com/lestrrat-go/jwx/v2/jwa"
15-
"github.com/lestrrat-go/jwx/v2/jwt"
14+
"github.com/lestrrat-go/jwx/v3/jwa"
15+
"github.com/lestrrat-go/jwx/v3/jwt"
1616
)
1717

1818
// EatProfile is the EAT profile implemented by this package
@@ -195,8 +195,16 @@ func (o *AttestationResult) Verify(data []byte, alg jwa.KeyAlgorithm, key interf
195195
return fmt.Errorf("failed verifying JWT message: %w", err)
196196
}
197197

198-
claims := token.PrivateClaims()
199-
claims["iat"] = token.IssuedAt().Unix()
198+
claims := make(map[string]any)
199+
for _, k := range token.Keys() {
200+
var v any
201+
if err := token.Get(k, &v); err != nil {
202+
return fmt.Errorf(`failed to get claim %s: %w`, k, err)
203+
}
204+
claims[k] = v
205+
}
206+
iat, _ := token.IssuedAt()
207+
claims["iat"] = iat.Unix()
200208

201209
return o.populateFromMap(claims)
202210
}

0 commit comments

Comments
 (0)