1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/urfave/cli/v2"
)
func init() {
topLevelCommands = append(topLevelCommands, makeJwsCmd())
}
func jwsAlgorithmFlag(use string) cli.Flag {
return &cli.StringFlag{
Name: "alg",
Aliases: []string{"a"},
Usage: "algorithm `ALG` to use to " + use + " the message with",
}
}
func makeJwsCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "jws"
cmd.Usage = "Work with JWS messages"
cmd.Subcommands = []*cli.Command{
makeJwsParseCmd(),
makeJwsSignCmd(),
makeJwsVerifyCmd(),
}
return &cmd
}
func makeJwsParseCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "parse"
cmd.Usage = "Parse JWS message"
cmd.UsageText = `jwx jws parse [command options] FILE
Parse FILE and display information about a JWS message.
Use "-" as FILE to read from STDIN.
`
// jwx jws parse <file>
cmd.Action = func(c *cli.Context) error {
src, err := getSource(c.Args().Get(0))
if err != nil {
return err
}
defer src.Close()
buf, err := io.ReadAll(src)
if err != nil {
return fmt.Errorf(`failed to read data from source: %w`, err)
if err != nil {
return fmt.Errorf(`failed to read message: %w`, err)
}
}
msg, err := jws.Parse(buf)
if err != nil {
return fmt.Errorf(`failed to parse message: %w`, err)
}
jsbuf, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return fmt.Errorf(`failed to marshal message: %w`, err)
}
fmt.Fprintf(os.Stdout, "%s\n\n", jsbuf)
for i, sig := range msg.Signatures() {
sigbuf, err := json.MarshalIndent(sig.ProtectedHeaders(), "", " ")
if err != nil {
return fmt.Errorf(`failed to marshal signature %d: %w`, 1, err)
}
fmt.Fprintf(os.Stdout, "Signature %d: %s\n", i, sigbuf)
}
return nil
}
return &cmd
}
func makeJwsVerifyCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "verify"
cmd.Aliases = []string{"ver"}
cmd.Usage = "Verify JWS messages."
cmd.UsageText = `jwx jws verify [command options] FILE
Parses a JWS message in FILE, and verifies using the specified method.
Use "-" as FILE to read from STDIN.
By default the user is responsible for providing the algorithm to
use to verify the signature. This is because we can not safely rely
on the "alg" field of the JWS message to deduce which key to use.
See https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
The alternative is to match a key based on explicitly specified
key ID ("kid"). In this case the following conditions must be met
for a successful verification:
(1) JWS message must list the key ID that it expects
(2) At least one of the provided JWK must contain the same key ID
(3) The same key must also contain the "alg" field
Therefore, the following key may be able to successfully verify
a JWS message using "--match-kid":
{ "typ": "oct", "alg": "H256", "kid": "mykey", .... }
But the following two will never succeed because they lack
either "alg" or "kid"
{ "typ": "oct", "kid": "mykey", .... }
{ "typ": "oct", "alg": "H256", .... }
`
cmd.Flags = []cli.Flag{
jwsAlgorithmFlag("verify"),
keyFlag("verify"),
keyFormatFlag(),
&cli.BoolFlag{
Name: "match-kid",
Value: false,
Usage: "instead of using alg, attempt to verify only if the key ID (kid) matches",
},
outputFlag(),
}
// jwx jws verify <file>
cmd.Action = func(c *cli.Context) error {
keyset, err := getKeyFile(c.String("key"), c.String("key-format"))
if err != nil {
return err
}
keyset, err = jwk.PublicSetOf(keyset)
if err != nil {
return fmt.Errorf(`failed to retrieve public key: %w`, err)
}
src, err := getSource(c.Args().Get(0))
if err != nil {
return err
}
defer src.Close()
buf, err := io.ReadAll(src)
if err != nil {
return fmt.Errorf(`failed to read data from source: %w`, err)
if err != nil {
return fmt.Errorf(`failed to verify message: %w`, err)
}
}
output, err := getOutput(c.String("output"))
if err != nil {
return err
}
defer output.Close()
if c.Bool("match-kid") {
payload, err := jws.Verify(buf, jws.WithKeySet(keyset))
if err == nil {
fmt.Fprintf(output, "%s", payload)
return nil
}
} else {
var alg jwa.SignatureAlgorithm
givenalg := c.String("alg")
if givenalg == "" {
return fmt.Errorf(`option --alg must be given`)
}
if err := alg.Accept(givenalg); err != nil {
return fmt.Errorf(`invalid alg %s`, givenalg)
}
ctx := context.Background()
for iter := keyset.Keys(ctx); iter.Next(ctx); {
pair := iter.Pair()
key := pair.Value.(jwk.Key)
payload, err := jws.Verify(buf, jws.WithKey(alg, key))
if err == nil {
fmt.Fprintf(output, "%s", payload)
return nil
}
}
}
return fmt.Errorf(`could not verify with any of the keys`)
}
return &cmd
}
func makeJwsSignCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "sign"
cmd.Aliases = []string{"sig"}
cmd.Usage = "Verify JWS message"
cmd.UsageText = `jwx jws sign [command options] FILE
Signs the payload in FILE and generates a JWS message in compact format.
Use "-" as FILE to read from STDIN.
Currently only single key signature mode is supported.
`
cmd.Flags = []cli.Flag{
jwsAlgorithmFlag("sign"),
keyFlag("sign"),
keyFormatFlag(),
&cli.StringFlag{
Name: "header",
Usage: "header object to inject into JWS message protected header",
},
outputFlag(),
}
// jwx jws verify <file>
cmd.Action = func(c *cli.Context) error {
keyset, err := getKeyFile(c.String("key"), c.String("key-format"))
if err != nil {
return err
}
if keyset.Len() != 1 {
return fmt.Errorf(`jwk file must contain exactly one key`)
}
key, _ := keyset.Key(0)
src, err := getSource(c.Args().Get(0))
if err != nil {
return err
}
defer src.Close()
buf, err := io.ReadAll(src)
if err != nil {
return fmt.Errorf(`failed to read data from source: %w`, err)
if err != nil {
return fmt.Errorf(`failed to sign message: %w`, err)
}
}
var alg jwa.SignatureAlgorithm
givenalg := c.String("alg")
if givenalg == "" {
return fmt.Errorf(`option --alg must be given`)
}
if err := alg.Accept(givenalg); err != nil {
return fmt.Errorf(`invalid alg %s`, givenalg)
}
// headers must go to WithKeySuboptions
var suboptions []jws.WithKeySuboption
if hdrbuf := c.String("header"); hdrbuf != "" {
h := jws.NewHeaders()
if err := json.Unmarshal([]byte(hdrbuf), h); err != nil {
return fmt.Errorf(`failed to parse header: %w`, err)
}
suboptions = append(suboptions, jws.WithProtectedHeaders(h))
}
var options []jws.SignOption
options = append(options, jws.WithKey(alg, key, suboptions...))
signed, err := jws.Sign(buf, options...)
if err != nil {
return fmt.Errorf(`failed to sign payload: %w`, err)
}
output, err := getOutput(c.String("output"))
if err != nil {
return err
}
defer output.Close()
fmt.Fprintf(output, "%s", signed)
return nil
}
return &cmd
}
|