Debian Codegraph @

command/crypto/jwe/decrypt.go

raw symbols SCIP
1 package jwe
2
3 import (
4 "fmt"
5 "os"
6
7 "github.com/smallstep/cli/errs"
8 "github.com/smallstep/cli/ui"
9
10 "github.com/pkg/errors"
11 "github.com/smallstep/cli/jose"
12 "github.com/smallstep/cli/utils"
13 "github.com/urfave/cli"
14 )
15
16 func decryptCommand() cli.Command {
17 return cli.Command{
18 Name: "decrypt",
19 Action: cli.ActionFunc(decryptAction),
20 Usage: "verify a JWE and decrypt ciphertext",
21 UsageText: `**step crypto jwe decrypt**
22 [**--key**=<path>] [**--jwks**=<jwks>] [**--kid**=<kid>]`,
23 Description: `**step crypto jwe decrypt** verifies a JWE read from STDIN and decrypts the
24 ciphertext printing it to STDOUT. If verification fails a non-zero failure
25 code is returned. If verification succeeds the command returns 0.
26
27 For examples, see **step help crypto jwe**.`,
28 Flags: []cli.Flag{
29 cli.StringFlag{
30 Name: "key",
31 Usage: `The <path> to the JWE recipient's private key. The argument should be the name of a file
32 containing a private JWK (or a JWK encrypted as a JWE payload) or a PEM encoded
33 private key (or a private key encrypted using the modes described on RFC 1423 or
34 with PBES2+PBKDF2 described in RFC 2898).`,
35 },
36 cli.StringFlag{
37 Name: "jwks",
38 Usage: `The JWK Set containing the recipient's private key. The <jwks> argument should
39 be the name of a file. The file contents should be a JWK Set or a JWE with a
40 JWK Set payload. The **--jwks** flag requires the use of the **--kid** flag to
41 specify which key to use.`,
42 },
43 cli.StringFlag{
44 Name: "kid",
45 Usage: `The ID of the recipient's private key. <kid> is a case-sensitive string. When
46 used with **--key** the <kid> value must match the **"kid"** member of the JWK. When
47 used with **--jwks** (a JWK Set) the KID value must match the **"kid"** member of
48 one of the JWKs in the JWK Set.`,
49 },
50 },
51 }
52 }
53
54 func decryptAction(ctx *cli.Context) error {
55 if err := errs.NumberOfArguments(ctx, 0); err != nil {
56 return err
57 }
58
59 data, err := utils.ReadAll(os.Stdin)
60 if err != nil {
61 return err
62 }
63
64 key := ctx.String("key")
65 jwks := ctx.String("jwks")
66 kid := ctx.String("kid")
67
68 obj, err := jose.ParseEncrypted(string(data))
69 if err != nil {
70 return errors.Wrap(err, "error parsing data")
71 }
72
73 alg := jose.KeyAlgorithm(obj.Header.Algorithm)
74
75 var isPBES2 bool
76 switch alg {
77 case jose.PBES2_HS256_A128KW, jose.PBES2_HS384_A192KW, jose.PBES2_HS512_A256KW:
78 isPBES2 = true
79 }
80
81 switch {
82 case isPBES2 && key != "":
83 return errors.Errorf("flag '--key' cannot be used with JWE algorithm '%s'", alg)
84 case isPBES2 && jwks != "":
85 return errors.Errorf("flag '--jwks' cannot be used with JWE algorithm '%s'", alg)
86 case !isPBES2 && key == "" && jwks == "":
87 return errs.RequiredOrFlag(ctx, "key", "jwk")
88 case key != "" && jwks != "":
89 return errs.MutuallyExclusiveFlags(ctx, "key", "jwks")
90 case jwks != "" && kid == "":
91 return errs.RequiredWithFlag(ctx, "kid", "jwks")
92 }
93
94 // Add parse options
95 var options []jose.Option
96 options = append(options, jose.WithUse("enc"))
97 if len(kid) > 0 {
98 options = append(options, jose.WithKid(kid))
99 }
100
101 // Read key from --key or --jwks
102 var pbes2Key []byte
103 var jwk *jose.JSONWebKey
104 switch {
105 case key != "":
106 jwk, err = jose.ParseKey(key, options...)
107 case jwks != "":
108 jwk, err = jose.ParseKeySet(jwks, options...)
109 case isPBES2:
110 pbes2Key, err = ui.PromptPassword("Please enter the password to decrypt the content encryption key")
111 default:
112 return errs.RequiredOrFlag(ctx, "key", "jwk")
113 }
114 if err != nil {
115 return err
116 }
117
118 var decryptKey interface{}
119 if isPBES2 {
120 decryptKey = pbes2Key
121 } else {
122 // Private keys are used for decryption
123 if jwk.IsPublic() {
124 return errors.New("cannot use a public key for decryption")
125 }
126
127 if jwk.Use == "sig" {
128 return errors.New("invalid jwk use: found 'sig' (signature), expecting 'enc' (encryption)")
129 }
130
131 // Validate jwk
132 if err = jose.ValidateJWK(jwk); err != nil {
133 return err
134 }
135
136 decryptKey = jwk.Key
137 }
138
139 decrypted, err := obj.Decrypt(decryptKey)
140 if err != nil {
141 return errors.Wrap(err, "error decrypting data")
142 }
143
144 fmt.Print(string(decrypted))
145
146 return nil
147 }