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 289 290 291 292
|
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"io"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/x25519"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ed25519"
)
func init() {
topLevelCommands = append(topLevelCommands, makeJwkCmd())
}
func jwkSetFlag() cli.Flag {
return &cli.BoolFlag{
Name: "set",
Usage: "generate as a JWK set",
}
}
func jwkOutputFormatFlag() cli.Flag {
return &cli.StringFlag{
Name: "output-format",
Aliases: []string{"O"},
Value: "json",
Usage: "Output format `OUTPUT` (json/pem)",
}
}
func publicKeyFlag() cli.Flag {
return &cli.BoolFlag{
Name: "public-key",
Aliases: []string{"p"},
Usage: "Display public key version of the key",
}
}
func makeJwkCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "jwk"
cmd.Usage = "Work with JWK and JWK sets"
cmd.Subcommands = []*cli.Command{
makeJwkGenerateCmd(),
makeJwkFormatCmd(),
}
return &cmd
}
func dumpJWKSet(dst io.Writer, keyset jwk.Set, format string, preserve bool) error {
if format == "pem" {
buf, err := jwk.Pem(keyset)
if err != nil {
return fmt.Errorf(`failed to format key in PEM format: %w`, err)
}
if _, err := dst.Write(buf); err != nil {
return fmt.Errorf(`failed to write to destination: %w`, err)
}
return nil
}
if format == "json" {
if preserve || keyset.Len() != 1 {
if err := dumpJSON(dst, keyset); err != nil {
return fmt.Errorf(`failed to marshal keyset into JSON format: %w`, err)
}
} else {
key, _ := keyset.Key(0)
if err := dumpJSON(dst, key); err != nil {
return fmt.Errorf(`failed to marshal key into JSON format: %w`, err)
}
}
return nil
}
return fmt.Errorf(`invalid JWK format "%s"`, format)
}
func makeJwkGenerateCmd() *cli.Command {
var crvnames bytes.Buffer
for i, crv := range jwk.AvailableCurves() {
if i > 0 {
crvnames.WriteByte('/')
}
crvnames.WriteString(crv.Params().Name)
}
var cmd cli.Command
cmd.Name = "generate"
cmd.Aliases = []string{"gen"}
cmd.Usage = "Generate a new JWK private key"
cmd.Flags = []cli.Flag{
&cli.StringFlag{
Name: "type",
Aliases: []string{"t"},
Usage: "JWK type `TYPE` (RSA/EC/OKP/oct)",
Required: true,
},
&cli.StringFlag{
Name: "curve",
Aliases: []string{"c"},
Usage: "Elliptic curve name `CURVE` (" + crvnames.String() + ") for ECDSA and OKP keys",
},
&cli.StringFlag{
Name: "template",
Usage: `Extra values in the JWK as JSON object`,
},
&cli.IntFlag{
Name: "keysize",
Aliases: []string{"s"},
Usage: "Integer `SIZE` for RSA and oct key sizes",
Value: 2048,
},
publicKeyFlag(),
outputFlag(),
jwkOutputFormatFlag(),
jwkSetFlag(),
}
cmd.Action = func(c *cli.Context) error {
var rawkey interface{}
switch typ := jwa.KeyType(c.String("type")); typ {
case jwa.RSA:
v, err := rsa.GenerateKey(rand.Reader, c.Int("keysize"))
if err != nil {
return fmt.Errorf(`failed to generate rsa private key: %w`, err)
}
rawkey = v
case jwa.EC:
var crv elliptic.Curve
var crvalg jwa.EllipticCurveAlgorithm
if err := crvalg.Accept(c.String("curve")); err != nil {
return fmt.Errorf(`invalid elliptic curve name %s: %w`, c.String("curve"), err)
}
crv, ok := jwk.CurveForAlgorithm(crvalg)
if !ok {
return fmt.Errorf(`invalid elliptic curve for ECDSA: %s (expected %s)`, crvalg, crvnames.String())
}
v, err := ecdsa.GenerateKey(crv, rand.Reader)
if err != nil {
return fmt.Errorf(`failed to generate ECDSA private key: %w`, err)
}
rawkey = v
case jwa.OctetSeq:
octets := make([]byte, c.Int("keysize"))
io.ReadFull(rand.Reader, octets)
rawkey = octets
case jwa.OKP:
var crvalg jwa.EllipticCurveAlgorithm
if err := crvalg.Accept(c.String("curve")); err != nil {
return fmt.Errorf(`invalid elliptic curve name: %w`, err)
}
switch crvalg {
case jwa.Ed25519:
_, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return fmt.Errorf(`failed to generate ed25519 private key: %w`, err)
}
rawkey = priv
case jwa.X25519:
_, priv, err := x25519.GenerateKey(rand.Reader)
if err != nil {
return fmt.Errorf(`failed to generate x25519 private key: %w`, err)
}
rawkey = priv
default:
return fmt.Errorf(`invalid elliptic curve for OKP: %s (expected %s/%s)`, crvalg, jwa.Ed25519, jwa.X25519)
}
default:
return fmt.Errorf(`invalid key type %s`, typ)
}
var attrs map[string]interface{}
if tmpl := c.String("template"); tmpl != "" {
if err := json.Unmarshal([]byte(tmpl), &attrs); err != nil {
return fmt.Errorf(`failed to unmarshal template: %w`, err)
}
}
key, err := jwk.FromRaw(rawkey)
if err != nil {
return fmt.Errorf(`failed to create new JWK from raw key: %w`, err)
}
for k, v := range attrs {
if err := key.Set(k, v); err != nil {
return fmt.Errorf(`failed to set field %s: %w`, k, err)
}
}
keyset := jwk.NewSet()
keyset.AddKey(key)
if c.Bool("public-key") {
pubks, err := jwk.PublicSetOf(keyset)
if err != nil {
return fmt.Errorf(`failed to generate public keys: %w`, err)
}
keyset = pubks
}
output, err := getOutput(c.String("output"))
if err != nil {
return err
}
defer output.Close()
return dumpJWKSet(output, keyset, c.String("output-format"), c.Bool("set"))
}
return &cmd
}
func makeJwkFormatCmd() *cli.Command {
var cmd cli.Command
cmd.Name = "format"
cmd.Aliases = []string{"fmt"}
cmd.Usage = "Format JWK"
cmd.Flags = []cli.Flag{
publicKeyFlag(),
&cli.StringFlag{
Name: "input-format",
Aliases: []string{"I"},
Value: "json",
Usage: "Input format `INPUT` (json/pem)",
},
jwkOutputFormatFlag(),
jwkSetFlag(),
outputFlag(),
}
// jwx jwk format <file>
cmd.Action = func(c *cli.Context) error {
if c.Args().Get(0) == "" {
cli.ShowCommandHelpAndExit(c, "format", 1)
}
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)
}
var options []jwk.ParseOption
switch format := c.String("input-format"); format {
case "json":
case "pem":
options = append(options, jwk.WithPEM(true))
default:
return fmt.Errorf(`invalid input format %s`, format)
}
keyset, err := jwk.Parse(buf, options...)
if err != nil {
return fmt.Errorf(`failed to parse keyset: %w`, err)
}
output, err := getOutput(c.String("output"))
if err != nil {
return err
}
defer output.Close()
if c.Bool("public-key") {
pubks, err := jwk.PublicSetOf(keyset)
if err != nil {
return fmt.Errorf(`failed to generate public keys: %w`, err)
}
keyset = pubks
}
return dumpJWKSet(output, keyset, c.String("output-format"), c.Bool("set"))
}
return &cmd
}
|