Debian Codegraph @

command/crypto/jws/verify.go

raw symbols SCIP
1 package jws
2
3 import (
4 "os"
5 "strings"
6
7 "github.com/pkg/errors"
8 "github.com/smallstep/cli/errs"
9 "github.com/smallstep/cli/jose"
10 "github.com/smallstep/cli/utils"
11 "github.com/urfave/cli"
12 )
13
14 func verifyCommand() cli.Command {
15 return cli.Command{
16 Name: "verify",
17 Action: cli.ActionFunc(verifyAction),
18 Usage: "verify a signed JWS data structure and return the payload",
19 UsageText: `**step crypto jws verify**
20 [**--alg**=<algorithm>] [**--key**=<path>] [**--jwks**=<jwks>] [**--kid**=<kid>]`,
21 Description: `**step crypto jws verify** reads a JWS data structure from STDIN; checks that
22 the algorithm are in agreement with expectations; verifies the digital
23 signature or message authentication code as appropriate; and outputs the
24 decoded payload of the JWS on STDOUT. If verification fails a non-zero failure
25 code is returned. If verification succeeds the command returns 0.
26
27 For a JWS to be verified successfully:
28
29 * The JWS must be well formed (no errors during deserialization)
30 * The <algorithm> must match the **"alg"** member in the JWS header
31 * The <kid> must match the **"kid"** member in the JWS header (if both are
32 present) and must match the **"kid"** in the JWK or the **"kid"** of one of the
33 JWKs in JWKS
34 * The JWS signature must be successfully verified
35
36 For examples, see **step help crypto jws**.`,
37 Flags: []cli.Flag{
38 cli.StringFlag{
39 Name: "alg, algorithm",
40 Usage: `The signature or MAC <algorithm> to use. Algorithms are case-sensitive strings
41 defined in RFC7518. If the key used do verify the JWS is not a JWK, or if it
42 is a JWK but does not have an **"alg"** member indicating its the intended
43 algorithm for use with the key, then the **--alg** flag is required to prevent
44 algorithm downgrade attacks. To disable this protection you can pass the
45 **--insecure** flag and omit the **--alg** flag.`,
46 },
47 cli.StringFlag{
48 Name: "key",
49 Usage: `The <path> to the key with which to verify the JWS.
50 The contents of the file can be a public or private JWK (or a JWK
51 encrypted as a JWE payload) or a public or private PEM (or a private key
52 encrypted using the modes described on RFC 1423 or with PBES2+PBKDF2 described
53 in RFC 2898).`,
54 },
55 cli.StringFlag{
56 Name: "jwks",
57 Usage: `The JWK Set containing the key to use to verify the JWS. The <jwks> argument
58 should be the name of a file. The file contents should be a JWK Set or a JWE
59 with a JWK Set payload. The JWS being verified should have a "kid" member that
60 matches the "kid" of one of the JWKs in the JWK Set. If the JWS does not have
61 a "kid" member the '--kid' flag can be used.`,
62 },
63 cli.StringFlag{
64 Name: "kid",
65 Usage: `The ID of the key used to sign the JWK, used to select a JWK from a JWK Set.
66 The KID argument is a case-sensitive string. If the input JWS has a "kid"
67 member its value must match <kid> or verification will fail.`,
68 },
69 cli.BoolFlag{
70 Name: "json",
71 Usage: `Displays the header, payload and signature as a JSON object. The payload will
72 be encoded using Base64.`,
73 },
74 cli.BoolFlag{
75 Name: "subtle",
76 Hidden: true,
77 },
78 cli.BoolFlag{
79 Name: "insecure",
80 Hidden: true,
81 },
82 },
83 }
84 }
85
86 // Get the public key for a JWK.
87 func publicKey(jwk *jose.JSONWebKey) interface{} {
88 if jose.IsSymmetric(jwk) {
89 return jwk.Key
90 }
91 return jwk.Public().Key
92 }
93
94 func verifyAction(ctx *cli.Context) error {
95 token, err := utils.ReadString(os.Stdin)
96 if err != nil {
97 return errors.Wrap(err, "error reading token")
98 }
99
100 tok, err := jose.ParseJWS(token)
101 if err != nil {
102 return errors.Errorf("error parsing token: %s", strings.TrimPrefix(err.Error(), "square/go-jose: "))
103 }
104
105 // We don't support multiple signatures
106 if len(tok.Signatures) > 1 {
107 return errors.New("validation failed: multiple signatures are not supported")
108 }
109
110 // Validate key, jwks and kid
111 key := ctx.String("key")
112 jwks := ctx.String("jwks")
113 kid := ctx.String("kid")
114 alg := ctx.String("alg")
115 switch {
116 case key == "" && jwks == "":
117 return errs.RequiredOrFlag(ctx, "key", "jwks")
118 case key != "" && jwks != "":
119 return errs.MutuallyExclusiveFlags(ctx, "key", "jwks")
120 case jwks != "" && kid == "":
121 if tok.Signatures[0].Header.KeyID == "" {
122 return errs.RequiredWithFlag(ctx, "kid", "jwks")
123 }
124 kid = tok.Signatures[0].Header.KeyID
125 }
126
127 // Add parse options
128 var options []jose.Option
129 options = append(options, jose.WithUse("sig"))
130 if len(alg) > 0 {
131 options = append(options, jose.WithAlg(alg))
132 }
133 if len(kid) > 0 {
134 options = append(options, jose.WithKid(kid))
135 }
136 if !ctx.Bool("insecure") {
137 options = append(options, jose.WithNoDefaults(true))
138 }
139
140 // Read key from --key or --jwks
141 var jwk *jose.JSONWebKey
142 switch {
143 case key != "":
144 jwk, err = jose.ParseKey(key, options...)
145 case jwks != "":
146 jwk, err = jose.ParseKeySet(jwks, options...)
147 default:
148 return errs.RequiredOrFlag(ctx, "key", "jwks")
149 }
150 if err != nil {
151 return err
152 }
153
154 // At this moment jwk.Algorithm should have an alg from:
155 // * alg parameter
156 // * jwk or jwkset
157 // * guessed for ecdsa and ed25519 keys
158 if jwk.Algorithm == "" {
159 return errors.New("flag '--alg' is required with the given key")
160 }
161 if err = jose.ValidateJWK(jwk); err != nil {
162 return err
163 }
164
165 // We don't support any critical headers
166 if _, ok := tok.Signatures[0].Header.ExtraHeaders["crit"]; ok {
167 return errors.New("validation failed: unrecognized critical headers (crit)")
168 }
169 if alg != "" && tok.Signatures[0].Header.Algorithm != "" && alg != tok.Signatures[0].Header.Algorithm {
170 return errors.Errorf("alg %s does not match the alg on JWS (%s)", alg, tok.Signatures[0].Header.Algorithm)
171 }
172
173 payload, err := tok.Verify(publicKey(jwk))
174 if err != nil {
175 return errors.New("validation failed: invalid signature")
176 }
177
178 if ctx.Bool("json") {
179 return printToken(tok)
180 }
181
182 os.Stdout.Write(payload)
183 return nil
184 }