| 1 |
package jws |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/smallstep/cli/flags" |
| 10 |
"github.com/smallstep/cli/utils" |
| 11 |
|
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/smallstep/cli/errs" |
| 14 |
"github.com/smallstep/cli/jose" |
| 15 |
"github.com/urfave/cli" |
| 16 |
) |
| 17 |
|
| 18 |
func signCommand() cli.Command { |
| 19 |
return cli.Command{ |
| 20 |
Name: "sign", |
| 21 |
Action: cli.ActionFunc(signAction), |
| 22 |
Usage: "create a signed JWS data structure", |
| 23 |
UsageText: `**step crypto jws sign** [- | <filename>] |
| 24 |
[**--alg**=<algorithm>] [**--jku**=<jwk-url>] [**--jwk**] [**--typ**=<type>] |
| 25 |
[**--cty**=<content-type>] [**--key**=<path>] [**--jwks**=<jwks>] [**--kid**=<kid>] |
| 26 |
[**--password-file**=<path>] [**--x5c-cert**=<path>] [**--x5c-key**=<path>] |
| 27 |
[**--x5t-cert**=<path>] [**--x5t-key**=<path>]`, |
| 28 |
|
| 29 |
Description: `**step crypto jws sign** generates a signed JSON Web Signature (JWS) by |
| 30 |
computing a digital signature or message authentication code for an arbitrary |
| 31 |
payload. By default, the payload to sign is read from STDIN and the JWS will |
| 32 |
be written to STDOUT. |
| 33 |
|
| 34 |
For examples, see **step help crypto jws**.`, |
| 35 |
Flags: []cli.Flag{ |
| 36 |
cli.StringFlag{ |
| 37 |
Name: "alg, algorithm", |
| 38 |
Usage: `The signature or MAC algorithm to use. Algorithms are case-sensitive strings |
| 39 |
defined in RFC7518. The selected algorithm must be compatible with the key |
| 40 |
type. This flag is optional. If not specified, the "alg" member of the JWK is |
| 41 |
used. If the JWK has no "alg" member then a default is selected depending on |
| 42 |
the JWK key type. If the JWK has an "alg" member and the "alg" flag is passed |
| 43 |
the two options must match unless the '--subtle' flag is also passed. |
| 44 |
|
| 45 |
: <algorithm> is a case-sensitive string and must be one of: |
| 46 |
|
| 47 |
**HS256** |
| 48 |
: HMAC using SHA-256 (default for "oct" key type) |
| 49 |
|
| 50 |
**HS384** |
| 51 |
: HMAC using SHA-384 |
| 52 |
|
| 53 |
**HS512** |
| 54 |
: HMAC using SHA-512 |
| 55 |
|
| 56 |
**RS256** |
| 57 |
: RSASSA-PKCS1-v1_5 using SHA-256 (default for "RSA" key type) |
| 58 |
|
| 59 |
**RS384** |
| 60 |
: RSASSA-PKCS1-v1_5 using SHA-384 |
| 61 |
|
| 62 |
**RS512** |
| 63 |
: RSASSA-PKCS1-v1_5 using SHA-512 |
| 64 |
|
| 65 |
**ES256** |
| 66 |
: ECDSA using P-256 and SHA-256 (default for "EC" key type) |
| 67 |
|
| 68 |
**ES384** |
| 69 |
: ECDSA using P-384 and SHA-384 |
| 70 |
|
| 71 |
**ES512** |
| 72 |
: ECDSA using P-521 and SHA-512 |
| 73 |
|
| 74 |
**PS256** |
| 75 |
: RSASSA-PSS using SHA-256 and MGF1 with SHA-256 |
| 76 |
|
| 77 |
**PS384** |
| 78 |
: RSASSA-PSS using SHA-384 and MGF1 with SHA-384 |
| 79 |
|
| 80 |
**PS512** |
| 81 |
: RSASSA-PSS using SHA-512 and MGF1 with SHA-512 |
| 82 |
|
| 83 |
**EdDSA** |
| 84 |
: EdDSA signature algorithm`, |
| 85 |
}, |
| 86 |
cli.StringFlag{ |
| 87 |
Name: "jku", |
| 88 |
Usage: `The "jku" (JWK Set URL) Header Parameter is a URI that refers to a resource |
| 89 |
for a set of JSON-encoded public keys, one of which corresponds to the key |
| 90 |
used to digitally sign the JWS. The keys MUST be encoded as a JWK Set (JWK). |
| 91 |
The protocol used to acquire the resource MUST provide integrity protection; |
| 92 |
an HTTP GET request to retrieve the JWK Set MUST use Transport Layer Security |
| 93 |
(TLS); and the identity of the server MUST be validated. Use of <jwk-url> is |
| 94 |
optional.`, |
| 95 |
}, |
| 96 |
cli.BoolFlag{ |
| 97 |
Name: "jwk", |
| 98 |
Usage: `The "jwk" (JSON Web Key) Header Parameter is the public key that corresponds |
| 99 |
to the key used to digitally sign the JWS. This key is represented as a JSON |
| 100 |
Web Key (JWK). Use of <jwk> is optional.`, |
| 101 |
}, |
| 102 |
cli.StringFlag{ |
| 103 |
Name: "typ, type", |
| 104 |
Usage: `The "typ" (type) Header Parameter is used by JWS applications to declare the |
| 105 |
media type of this complete JWS. This is intended for use by the application |
| 106 |
when more than one kind of object could be present in an application data |
| 107 |
structure that can contain a JWS; the application can use this value to |
| 108 |
disambiguate among the different kinds of objects that might be present. It |
| 109 |
will typically not be used by applications when the kind of object is already |
| 110 |
known. This parameter is ignored by JWS implementations; any processing of |
| 111 |
this parameter is performed by the JWS application. Use of <type> is |
| 112 |
optional. |
| 113 |
|
| 114 |
The "typ" value "JOSE" can be used by applications to indicate that this |
| 115 |
object is a JWS or JWE using the JWS Compact Serialization or the JWE Compact |
| 116 |
Serialization. The "typ" value "JOSE+JSON" can be used by applications to |
| 117 |
indicate that this object is a JWS or JWE using the JWS JSON Serialization or |
| 118 |
the JWE JSON Serialization. Other type values can also be used by |
| 119 |
applications.`, |
| 120 |
}, |
| 121 |
cli.StringFlag{ |
| 122 |
Name: "cty", |
| 123 |
Usage: `The "cty" (content type) Header Parameter is used by JWS applications to |
| 124 |
declare the media type of the secured content (the payload). This is intended |
| 125 |
for use by the application when more than one kind of object could be present |
| 126 |
in the JWS Payload; the application can use this value to disambiguate among |
| 127 |
the different kinds of objects that might be present. It will typically not be |
| 128 |
used by applications when the kind of object is already known. This parameter |
| 129 |
is ignored by JWS implementations; any processing of this parameter is |
| 130 |
performed by the JWS application. Use of <content-type> is optional.`, |
| 131 |
}, |
| 132 |
cli.StringFlag{ |
| 133 |
Name: "key, x5c-key, x5t-key", |
| 134 |
Usage: `The <path> to the key with which to sign the JWS. |
| 135 |
JWSs can be signed using a private JWK (or a JWK encrypted as a JWE payload) or |
| 136 |
a PEM encoded private key (or a private key encrypted using the modes described |
| 137 |
on RFC 1423 or with PBES2+PBKDF2 described in RFC 2898).`, |
| 138 |
}, |
| 139 |
cli.StringFlag{ |
| 140 |
Name: "jwks", |
| 141 |
Usage: `The JWK Set containing the key to use to sign the JWS. The <jwks> argument |
| 142 |
should be the name of a file. The file contents should be a JWK Set or a JWE |
| 143 |
with a JWK Set payload. The **--jwks** flag requires the use of the **--kid** |
| 144 |
flag to specify which key to use.`, |
| 145 |
}, |
| 146 |
cli.StringFlag{ |
| 147 |
Name: "kid", |
| 148 |
Usage: `The ID of the key used to sign the JWS. The <kid> argument is a case-sensitive |
| 149 |
string. When used with '--jwk' the <kid> value must match the **"kid"** member |
| 150 |
of the JWK. When used with **--jwks** (a JWK Set) the <kid> value must match |
| 151 |
the **"kid"** member of one of the JWKs in the JWK Set.`, |
| 152 |
}, |
| 153 |
cli.BoolFlag{ |
| 154 |
Name: "subtle", |
| 155 |
Hidden: true, |
| 156 |
}, |
| 157 |
cli.BoolFlag{ |
| 158 |
Name: "no-kid", |
| 159 |
Hidden: true, |
| 160 |
}, |
| 161 |
flags.PasswordFile, |
| 162 |
flags.X5cCert, |
| 163 |
flags.X5tCert, |
| 164 |
}, |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func signAction(ctx *cli.Context) error { |
| 169 |
var err error |
| 170 |
var payload []byte |
| 171 |
|
| 172 |
|
| 173 |
args := ctx.Args() |
| 174 |
switch len(args) { |
| 175 |
case 0: |
| 176 |
if payload, err = readPayload(""); err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
case 1: |
| 180 |
if payload, err = readPayload(args[0]); err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
default: |
| 184 |
return errs.TooManyArguments(ctx) |
| 185 |
} |
| 186 |
|
| 187 |
isSubtle := ctx.Bool("subtle") |
| 188 |
alg := ctx.String("alg") |
| 189 |
|
| 190 |
x5cCertFile, x5cKeyFile := ctx.String("x5c-cert"), ctx.String("x5c-key") |
| 191 |
x5tCertFile, x5tKeyFile := ctx.String("x5t-cert"), ctx.String("x5t-key") |
| 192 |
key := ctx.String("key") |
| 193 |
jwks := ctx.String("jwks") |
| 194 |
kid := ctx.String("kid") |
| 195 |
var isX5C bool |
| 196 |
if len(x5cCertFile) > 0 { |
| 197 |
if len(x5cKeyFile) == 0 { |
| 198 |
return errs.RequiredWithOrFlag(ctx, "x5c-cert", "key", "x5c-key") |
| 199 |
} |
| 200 |
if len(x5tCertFile) > 0 { |
| 201 |
return errs.MutuallyExclusiveFlags(ctx, "x5c-cert", "x5t-cert") |
| 202 |
} |
| 203 |
if ctx.IsSet("jwk") { |
| 204 |
return errs.MutuallyExclusiveFlags(ctx, "x5c-cert", "jwk") |
| 205 |
} |
| 206 |
if len(jwks) > 0 { |
| 207 |
return errs.MutuallyExclusiveFlags(ctx, "x5c-cert", "jwks") |
| 208 |
} |
| 209 |
isX5C = true |
| 210 |
} |
| 211 |
|
| 212 |
var isX5T bool |
| 213 |
if len(x5tCertFile) > 0 { |
| 214 |
if len(x5tKeyFile) == 0 { |
| 215 |
return errs.RequiredWithOrFlag(ctx, "x5t-cert", "key", "x5t-key") |
| 216 |
} |
| 217 |
if len(x5cCertFile) > 0 { |
| 218 |
return errs.MutuallyExclusiveFlags(ctx, "x5t-cert", "x5c-cert") |
| 219 |
} |
| 220 |
if ctx.IsSet("jwk") { |
| 221 |
return errs.MutuallyExclusiveFlags(ctx, "x5t-cert", "jwk") |
| 222 |
} |
| 223 |
if len(jwks) > 0 { |
| 224 |
return errs.MutuallyExclusiveFlags(ctx, "x5t-cert", "jwks") |
| 225 |
} |
| 226 |
isX5T = true |
| 227 |
} |
| 228 |
|
| 229 |
if !isX5C && !isX5T { |
| 230 |
|
| 231 |
switch { |
| 232 |
case key == "" && jwks == "": |
| 233 |
return errs.RequiredOrFlag(ctx, "key", "jwks") |
| 234 |
case key != "" && jwks != "": |
| 235 |
return errs.MutuallyExclusiveFlags(ctx, "key", "jwks") |
| 236 |
case jwks != "" && kid == "": |
| 237 |
return errs.RequiredWithFlag(ctx, "kid", "jwks") |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
var options []jose.Option |
| 243 |
options = append(options, jose.WithUse("sig")) |
| 244 |
if len(alg) > 0 { |
| 245 |
options = append(options, jose.WithAlg(alg)) |
| 246 |
} |
| 247 |
if len(kid) > 0 { |
| 248 |
options = append(options, jose.WithKid(kid)) |
| 249 |
} |
| 250 |
if isSubtle { |
| 251 |
options = append(options, jose.WithSubtle(true)) |
| 252 |
} |
| 253 |
if passwordFile := ctx.String("password-file"); len(passwordFile) != 0 { |
| 254 |
options = append(options, jose.WithPasswordFile(passwordFile)) |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
var jwk *jose.JSONWebKey |
| 259 |
switch { |
| 260 |
case isX5T: |
| 261 |
jwk, err = jose.ParseKey(x5tKeyFile, options...) |
| 262 |
case isX5C: |
| 263 |
jwk, err = jose.ParseKey(x5cKeyFile, options...) |
| 264 |
case key != "": |
| 265 |
jwk, err = jose.ParseKey(key, options...) |
| 266 |
case jwks != "": |
| 267 |
jwk, err = jose.ParseKeySet(jwks, options...) |
| 268 |
default: |
| 269 |
return errs.RequiredOrFlag(ctx, "key", "jwks") |
| 270 |
} |
| 271 |
if err != nil { |
| 272 |
return err |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
if jwk.IsPublic() { |
| 277 |
return errors.New("cannot use a public key for signing") |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if jwk.Use != "sig" && jwk.Use != "" { |
| 282 |
return errors.Errorf("invalid jwk use: found '%s', expecting 'sig' (signature)", jwk.Use) |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if jwk.Algorithm == "" { |
| 290 |
return errors.New("flag '--alg' is required with the given key") |
| 291 |
} |
| 292 |
if err = jose.ValidateJWK(jwk); err != nil { |
| 293 |
return err |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
so := new(jose.SignerOptions) |
| 298 |
if ctx.IsSet("typ") { |
| 299 |
so.WithType(jose.ContentType(ctx.String("typ"))) |
| 300 |
} |
| 301 |
if ctx.IsSet("cty") { |
| 302 |
so.WithContentType(jose.ContentType(ctx.String("cty"))) |
| 303 |
} |
| 304 |
if !ctx.Bool("no-kid") && jwk.KeyID != "" { |
| 305 |
so.WithHeader("kid", jwk.KeyID) |
| 306 |
} |
| 307 |
if ctx.IsSet("jku") { |
| 308 |
so.WithHeader("jku", ctx.String("jku")) |
| 309 |
} |
| 310 |
if ctx.Bool("jwk") { |
| 311 |
so.WithHeader("jwk", jwk.Public()) |
| 312 |
} |
| 313 |
if isX5C { |
| 314 |
certStrs, err := jose.ValidateX5C(x5cCertFile, jwk.Key) |
| 315 |
if err != nil { |
| 316 |
return errors.Wrap(err, "error validating x5c certificate chain and key for use in x5c header") |
| 317 |
} |
| 318 |
so.WithHeader("x5c", certStrs) |
| 319 |
} |
| 320 |
if isX5T { |
| 321 |
fingerprint, err := jose.ValidateX5T(x5tCertFile, jwk.Key) |
| 322 |
if err != nil { |
| 323 |
return errors.Wrap(err, "error validating x5t certificate and key for use in x5t header") |
| 324 |
} |
| 325 |
so.WithHeader("x5t", fingerprint) |
| 326 |
} |
| 327 |
|
| 328 |
signer, err := jose.NewSigner(jose.SigningKey{ |
| 329 |
Algorithm: jose.SignatureAlgorithm(jwk.Algorithm), |
| 330 |
Key: jwk.Key, |
| 331 |
}, so) |
| 332 |
if err != nil { |
| 333 |
return errors.Wrap(err, "error creating JWS signer") |
| 334 |
} |
| 335 |
|
| 336 |
signed, err := signer.Sign(payload) |
| 337 |
if err != nil { |
| 338 |
return errors.Errorf("error signing payload: %s", strings.TrimPrefix(err.Error(), "square/go-jose: ")) |
| 339 |
} |
| 340 |
|
| 341 |
raw, err := signed.CompactSerialize() |
| 342 |
if err != nil { |
| 343 |
return errors.Wrapf(err, "error serializing JWS") |
| 344 |
} |
| 345 |
|
| 346 |
fmt.Println(raw) |
| 347 |
return nil |
| 348 |
} |
| 349 |
|
| 350 |
func readPayload(filename string) ([]byte, error) { |
| 351 |
switch filename { |
| 352 |
case "": |
| 353 |
st, err := os.Stdin.Stat() |
| 354 |
if err != nil { |
| 355 |
return nil, errors.Wrap(err, "error reading data") |
| 356 |
} |
| 357 |
if st.Size() == 0 && st.Mode()&os.ModeNamedPipe == 0 { |
| 358 |
return []byte{}, nil |
| 359 |
} |
| 360 |
return utils.ReadAll(os.Stdin) |
| 361 |
case "-": |
| 362 |
return utils.ReadAll(os.Stdin) |
| 363 |
default: |
| 364 |
b, err := ioutil.ReadFile(filename) |
| 365 |
if err != nil { |
| 366 |
return nil, errs.FileError(err, filename) |
| 367 |
} |
| 368 |
return b, nil |
| 369 |
} |
| 370 |
} |