| 1 |
package jwe |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
|
| 7 |
"github.com/pkg/errors" |
| 8 |
"github.com/smallstep/cli/errs" |
| 9 |
"github.com/smallstep/cli/jose" |
| 10 |
"github.com/smallstep/cli/ui" |
| 11 |
"github.com/smallstep/cli/utils" |
| 12 |
"github.com/urfave/cli" |
| 13 |
) |
| 14 |
|
| 15 |
func encryptCommand() cli.Command { |
| 16 |
return cli.Command{ |
| 17 |
Name: "encrypt", |
| 18 |
Action: cli.ActionFunc(encryptAction), |
| 19 |
Usage: "encrypt a payload using JSON Web Encryption (JWE)", |
| 20 |
UsageText: `**step crypto jwe encrypt** |
| 21 |
[**--alg**=<key-enc-algorithm>] [**--enc**=<content-enc-algorithm>] |
| 22 |
[**--key**=<path>] [**--jwks**=<jwks>] [**--kid**=<kid>]`, |
| 23 |
Description: `**step crypto jwe encrypt** encrypts a payload using JSON Web Encryption |
| 24 |
(JWE). By default, the payload to encrypt is read from STDIN and the JWE data |
| 25 |
structure will be written to STDOUT. |
| 26 |
|
| 27 |
For examples, see **step help crypto jwe**.`, |
| 28 |
Flags: []cli.Flag{ |
| 29 |
cli.StringFlag{ |
| 30 |
Name: "alg, algorithm", |
| 31 |
Usage: `The cryptographic algorithm used to encrypt or determine the value of the |
| 32 |
content encryption key (CEK). Algorithms are case-sensitive strings defined in |
| 33 |
RFC7518. The selected algorithm must be compatible with the key type. This |
| 34 |
flag is optional. If not specified, the **"alg"** member of the JWK is used. If |
| 35 |
the JWK has no **"alg"** member then a default is selected depending on the JWK |
| 36 |
key type. If the JWK has an **"alg"** member and the **--alg** flag is passed the two |
| 37 |
options must match unless the **--subtle** flag is also passed. |
| 38 |
|
| 39 |
: <key-enc-algorithm> is a case-sensitive string and must be one of: |
| 40 |
|
| 41 |
**RSA1_5** |
| 42 |
: RSAES-PKCS1-v1_5 |
| 43 |
|
| 44 |
**RSA-OAEP** |
| 45 |
: RSAES OAEP using default parameters |
| 46 |
|
| 47 |
**RSA-OAEP-256** (default for RSA keys) |
| 48 |
: RSAES OAEP using SHA-256 and MGF1 with SHA-256 |
| 49 |
|
| 50 |
**A128KW** |
| 51 |
: AES Key Wrap with default initial value using 128-bit key |
| 52 |
|
| 53 |
**A192KW** |
| 54 |
: AES Key Wrap with default initial value using 192-bit key |
| 55 |
|
| 56 |
**A256KW** |
| 57 |
: AES Key Wrap with default initial value using 256-bit key |
| 58 |
|
| 59 |
**dir** |
| 60 |
: Direct use of a shared symmetric key as the content encryption key (CEK) |
| 61 |
|
| 62 |
**ECDH-ES** (default for EC keys) |
| 63 |
: Elliptic Curve Diffie-Hellman Ephemeral Static key agreement |
| 64 |
|
| 65 |
**ECDH-ES+A128KW** |
| 66 |
: ECDH-ES using Concat KDF and CEK wrapped with "A128KW |
| 67 |
|
| 68 |
**ECDH-ES+A192KW** |
| 69 |
: ECDH-ES using Concat KDF and CEK wrapped with "A192KW |
| 70 |
|
| 71 |
**ECDH-ES+A256KW** |
| 72 |
: ECDH-ES using Concat KDF and CEK wrapped with "A256KW |
| 73 |
|
| 74 |
**A128GCMKW** |
| 75 |
: Key wrappiung with AES GCM using 128-bit key |
| 76 |
|
| 77 |
**A192GCMKW** |
| 78 |
: Key wrappiung with AES GCM using 192-bit key |
| 79 |
|
| 80 |
**A256GCMKW** (default for oct keys) |
| 81 |
: Key wrappiung with AES GCM using 256-bit key |
| 82 |
|
| 83 |
**PBES2-HS256+A128KW** |
| 84 |
: PBES2 with HMAC SHA-256 and "A128KW" wrapping |
| 85 |
|
| 86 |
**PBES2-HS384+A192KW** |
| 87 |
: PBES2 with HMAC SHA-256 and "A192KW" wrapping |
| 88 |
|
| 89 |
**PBES2-HS512+A256KW** |
| 90 |
: PBES2 with HMAC SHA-256 and "A256KW" wrapping`, |
| 91 |
}, |
| 92 |
cli.StringFlag{ |
| 93 |
Name: "enc, encryption-algorithm", |
| 94 |
Value: "A256GCM", |
| 95 |
Usage: `The cryptographic content encryption algorithm used to perform authenticated |
| 96 |
encryption on the plaintext payload (the content) to produce ciphertext and |
| 97 |
the authentication tag. |
| 98 |
|
| 99 |
: <content-enc-algorithm> is a case-sensitive string and must be one of: |
| 100 |
|
| 101 |
**A128CBC-HS256** |
| 102 |
: AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm |
| 103 |
|
| 104 |
**A192CBC-HS384** |
| 105 |
: AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm |
| 106 |
|
| 107 |
**A256CBC-HS512** |
| 108 |
: AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm |
| 109 |
|
| 110 |
**A128GCM** |
| 111 |
: AES GCM using 128-bit key |
| 112 |
|
| 113 |
**A192GCM** |
| 114 |
: AES GCM using 192-bit key |
| 115 |
|
| 116 |
**A256GCM** (default) |
| 117 |
: AES GCM using 256-bit key`, |
| 118 |
}, |
| 119 |
cli.StringFlag{ |
| 120 |
Name: "key", |
| 121 |
Usage: `The <path> to the JWE recipient's public key. |
| 122 |
JWEs can be encrypted for a recipient using a public JWK or a PEM encoded public key.`, |
| 123 |
}, |
| 124 |
cli.StringFlag{ |
| 125 |
Name: "jwks", |
| 126 |
Usage: `The JWK Set containing the recipient's public key. The <jwks> argument should |
| 127 |
be the name of a file. The file contents should be a JWK Set. The **--jwks** |
| 128 |
flag requires the use of the **--kid** flag to specify which key to use.`, |
| 129 |
}, |
| 130 |
cli.StringFlag{ |
| 131 |
Name: "kid", |
| 132 |
Usage: `The ID of the recipient's public key. <kid> is a case-sensitive string. When |
| 133 |
used with **--key** the <kid> value must match the **"kid"** member of the JWK. When |
| 134 |
used with **--jwks** (a JWK Set) the <kid> value must match the **"kid"** member of |
| 135 |
one of the JWKs in the JWK Set.`, |
| 136 |
}, |
| 137 |
cli.StringFlag{ |
| 138 |
Name: "typ, type", |
| 139 |
Usage: `The media type of the JWE, used for disambiguation in applications where |
| 140 |
more than one type of JWE may be processed. While this parameter might be |
| 141 |
useful to applications, it is ignored by JWE implementations.`, |
| 142 |
}, |
| 143 |
cli.StringFlag{ |
| 144 |
Name: "cty, content-type", |
| 145 |
Usage: `The media type of the JWE payload, used for disambiguation of JWE objects in |
| 146 |
applications where more than one JWE payload type may be present. This |
| 147 |
parameter is ignored by JWE implementations, but may be processed by |
| 148 |
applications that use JWE.`, |
| 149 |
}, |
| 150 |
cli.BoolFlag{ |
| 151 |
Name: "subtle", |
| 152 |
Hidden: true, |
| 153 |
}, |
| 154 |
}, |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
func encryptAction(ctx *cli.Context) error { |
| 159 |
if err := errs.NumberOfArguments(ctx, 0); err != nil { |
| 160 |
return err |
| 161 |
} |
| 162 |
|
| 163 |
data, err := utils.ReadAll(os.Stdin) |
| 164 |
if err != nil { |
| 165 |
return err |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
alg, err := getRecipientAlg(ctx, ctx.String("alg")) |
| 170 |
if err != nil { |
| 171 |
return err |
| 172 |
} |
| 173 |
|
| 174 |
enc, err := getContentEncryptionAlg(ctx, ctx.String("enc")) |
| 175 |
if err != nil { |
| 176 |
return err |
| 177 |
} |
| 178 |
|
| 179 |
var isPBES2 bool |
| 180 |
switch alg { |
| 181 |
case jose.PBES2_HS256_A128KW, jose.PBES2_HS384_A192KW, jose.PBES2_HS512_A256KW: |
| 182 |
isPBES2 = true |
| 183 |
} |
| 184 |
|
| 185 |
key := ctx.String("key") |
| 186 |
jwks := ctx.String("jwks") |
| 187 |
kid := ctx.String("kid") |
| 188 |
typ := ctx.String("typ") |
| 189 |
cty := ctx.String("cty") |
| 190 |
isSubtle := ctx.Bool("subtle") |
| 191 |
|
| 192 |
switch { |
| 193 |
case isPBES2 && key != "": |
| 194 |
return errs.MutuallyExclusiveFlags(ctx, "alg "+ctx.String("alg"), "key") |
| 195 |
case isPBES2 && jwks != "": |
| 196 |
return errs.MutuallyExclusiveFlags(ctx, "alg "+ctx.String("alg"), "jwks") |
| 197 |
case !isPBES2 && key == "" && jwks == "": |
| 198 |
return errs.RequiredOrFlag(ctx, "key", "jwk") |
| 199 |
case key != "" && jwks != "": |
| 200 |
return errs.MutuallyExclusiveFlags(ctx, "key", "jwks") |
| 201 |
case jwks != "" && kid == "": |
| 202 |
return errs.RequiredWithFlag(ctx, "kid", "jwks") |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
var options []jose.Option |
| 207 |
options = append(options, jose.WithUse("enc")) |
| 208 |
if len(alg) > 0 { |
| 209 |
options = append(options, jose.WithAlg(string(alg))) |
| 210 |
} |
| 211 |
if len(kid) > 0 { |
| 212 |
options = append(options, jose.WithKid(kid)) |
| 213 |
} |
| 214 |
if isSubtle { |
| 215 |
options = append(options, jose.WithSubtle(true)) |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
var pbes2Key []byte |
| 220 |
var jwk *jose.JSONWebKey |
| 221 |
switch { |
| 222 |
case key != "": |
| 223 |
jwk, err = jose.ParseKey(key, options...) |
| 224 |
case jwks != "": |
| 225 |
jwk, err = jose.ParseKeySet(jwks, options...) |
| 226 |
case isPBES2: |
| 227 |
pbes2Key, err = ui.PromptPassword("Please enter the password to encrypt the content encryption key") |
| 228 |
default: |
| 229 |
return errs.RequiredOrFlag(ctx, "key", "jwks") |
| 230 |
} |
| 231 |
if err != nil { |
| 232 |
return err |
| 233 |
} |
| 234 |
|
| 235 |
var recipient jose.Recipient |
| 236 |
if isPBES2 { |
| 237 |
recipient = jose.Recipient{ |
| 238 |
Algorithm: alg, |
| 239 |
Key: pbes2Key, |
| 240 |
KeyID: kid, |
| 241 |
} |
| 242 |
} else { |
| 243 |
|
| 244 |
jwkPub := jwk.Public() |
| 245 |
jwk = &jwkPub |
| 246 |
|
| 247 |
if jwk.Use == "sig" { |
| 248 |
return errors.New("invalid jwk use: found 'sig' (signature), expecting 'enc' (encryption)") |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
if err = jose.ValidateJWK(jwk); err != nil { |
| 253 |
return err |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
if alg == "" { |
| 258 |
alg, err = getRecipientAlg(ctx, jwk.Algorithm) |
| 259 |
if err != nil { |
| 260 |
return err |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
recipient = jose.Recipient{ |
| 265 |
Algorithm: alg, |
| 266 |
Key: jwk, |
| 267 |
KeyID: kid, |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
opts := new(jose.EncrypterOptions) |
| 273 |
if typ != "" { |
| 274 |
opts.WithType(jose.ContentType(typ)) |
| 275 |
} |
| 276 |
if cty != "" { |
| 277 |
opts.WithContentType(jose.ContentType(cty)) |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
encrypter, err := jose.NewEncrypter(enc, recipient, opts) |
| 282 |
if err != nil { |
| 283 |
return errs.Wrap(err, "error creating cipher") |
| 284 |
} |
| 285 |
|
| 286 |
obj, err := encrypter.Encrypt(data) |
| 287 |
if err != nil { |
| 288 |
return errs.Wrap(err, "error encrypting data") |
| 289 |
} |
| 290 |
|
| 291 |
fmt.Println(obj.FullSerialize()) |
| 292 |
return nil |
| 293 |
} |
| 294 |
|
| 295 |
func getContentEncryptionAlg(ctx *cli.Context, enc string) (jose.ContentEncryption, error) { |
| 296 |
switch enc { |
| 297 |
case "": |
| 298 |
return jose.A256GCM, nil |
| 299 |
case "A128GCM": |
| 300 |
return jose.A128GCM, nil |
| 301 |
case "A192GCM": |
| 302 |
return jose.A192GCM, nil |
| 303 |
case "A256GCM": |
| 304 |
return jose.A256GCM, nil |
| 305 |
case "A128CBC-HS256": |
| 306 |
return jose.A128CBC_HS256, nil |
| 307 |
case "A192CBC-HS384": |
| 308 |
return jose.A192CBC_HS384, nil |
| 309 |
case "A256CBC-HS512": |
| 310 |
return jose.A256CBC_HS512, nil |
| 311 |
default: |
| 312 |
return "", errs.InvalidFlagValue(ctx, "enc", enc, "") |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
func getRecipientAlg(ctx *cli.Context, alg string) (jose.KeyAlgorithm, error) { |
| 317 |
switch alg { |
| 318 |
case "": |
| 319 |
return "", nil |
| 320 |
case "RSA1_5": |
| 321 |
return jose.RSA1_5, nil |
| 322 |
case "RSA-OAEP": |
| 323 |
return jose.RSA_OAEP, nil |
| 324 |
case "RSA-OAEP-256": |
| 325 |
return jose.RSA_OAEP_256, nil |
| 326 |
case "A128KW": |
| 327 |
return jose.A128KW, nil |
| 328 |
case "A192KW": |
| 329 |
return jose.A192KW, nil |
| 330 |
case "A256KW": |
| 331 |
return jose.A256KW, nil |
| 332 |
case "dir": |
| 333 |
return jose.DIRECT, nil |
| 334 |
case "ECDH-ES": |
| 335 |
return jose.ECDH_ES, nil |
| 336 |
case "ECDH-ES+A128KW": |
| 337 |
return jose.ECDH_ES_A128KW, nil |
| 338 |
case "ECDH-ES+A192KW": |
| 339 |
return jose.ECDH_ES_A192KW, nil |
| 340 |
case "ECDH-ES+A256KW": |
| 341 |
return jose.ECDH_ES_A256KW, nil |
| 342 |
case "A128GCMKW": |
| 343 |
return jose.A128GCMKW, nil |
| 344 |
case "A192GCMKW": |
| 345 |
return jose.A192GCMKW, nil |
| 346 |
case "A256GCMKW": |
| 347 |
return jose.A256GCMKW, nil |
| 348 |
case "PBES2-HS256+A128KW": |
| 349 |
return jose.PBES2_HS256_A128KW, nil |
| 350 |
case "PBES2-HS384+A192KW": |
| 351 |
return jose.PBES2_HS384_A192KW, nil |
| 352 |
case "PBES2-HS512+A256KW": |
| 353 |
return jose.PBES2_HS512_A256KW, nil |
| 354 |
default: |
| 355 |
return "", errs.InvalidFlagValue(ctx, "alg", alg, "") |
| 356 |
} |
| 357 |
} |