-
Notifications
You must be signed in to change notification settings - Fork 243
/
encrypt.go
355 lines (306 loc) 路 9.55 KB
/
encrypt.go
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package jwe
import (
"fmt"
"os"
"github.com/pkg/errors"
"github.com/smallstep/cli/flags"
"github.com/smallstep/cli/utils"
"github.com/urfave/cli"
"go.step.sm/cli-utils/errs"
"go.step.sm/cli-utils/ui"
"go.step.sm/crypto/jose"
)
func encryptCommand() cli.Command {
return cli.Command{
Name: "encrypt",
Action: cli.ActionFunc(encryptAction),
Usage: "encrypt a payload using JSON Web Encryption (JWE)",
UsageText: `**step crypto jwe encrypt**
[**--alg**=<key-enc-algorithm>] [**--enc**=<content-enc-algorithm>]
[**--key**=<file>] [**--jwks**=<jwks>] [**--kid**=<kid>]`,
Description: `**step crypto jwe encrypt** encrypts a payload using JSON Web Encryption
(JWE). By default, the payload to encrypt is read from STDIN and the JWE data
structure will be written to STDOUT.
For examples, see **step help crypto jwe**.`,
Flags: []cli.Flag{
cli.StringFlag{
Name: "alg, algorithm",
Usage: `The cryptographic algorithm used to encrypt or determine the value of the
content encryption key (CEK). Algorithms are case-sensitive strings defined in
RFC7518. The selected algorithm must be compatible with the key type. This
flag is optional. If not specified, the **"alg"** member of the JWK is used. If
the JWK has no **"alg"** member then a default is selected depending on the JWK
key type. If the JWK has an **"alg"** member and the **--alg** flag is passed the two
options must match unless the **--subtle** flag is also passed.
: <key-enc-algorithm> is a case-sensitive string and must be one of:
**RSA1_5**
: RSAES-PKCS1-v1_5
**RSA-OAEP**
: RSAES OAEP using default parameters
**RSA-OAEP-256** (default for RSA keys)
: RSAES OAEP using SHA-256 and MGF1 with SHA-256
**A128KW**
: AES Key Wrap with default initial value using 128-bit key
**A192KW**
: AES Key Wrap with default initial value using 192-bit key
**A256KW**
: AES Key Wrap with default initial value using 256-bit key
**dir**
: Direct use of a shared symmetric key as the content encryption key (CEK)
**ECDH-ES** (default for EC keys)
: Elliptic Curve Diffie-Hellman Ephemeral Static key agreement
**ECDH-ES+A128KW**
: ECDH-ES using Concat KDF and CEK wrapped with "A128KW
**ECDH-ES+A192KW**
: ECDH-ES using Concat KDF and CEK wrapped with "A192KW
**ECDH-ES+A256KW**
: ECDH-ES using Concat KDF and CEK wrapped with "A256KW
**A128GCMKW**
: Key wrapping with AES GCM using 128-bit key
**A192GCMKW**
: Key wrapping with AES GCM using 192-bit key
**A256GCMKW** (default for oct keys)
: Key wrapping with AES GCM using 256-bit key
**PBES2-HS256+A128KW**
: PBES2 with HMAC SHA-256 and "A128KW" wrapping
**PBES2-HS384+A192KW**
: PBES2 with HMAC SHA-256 and "A192KW" wrapping
**PBES2-HS512+A256KW**
: PBES2 with HMAC SHA-256 and "A256KW" wrapping`,
},
cli.StringFlag{
Name: "enc, encryption-algorithm",
Value: "A256GCM",
Usage: `The cryptographic content encryption algorithm used to perform authenticated
encryption on the plaintext payload (the content) to produce ciphertext and
the authentication tag.
: <content-enc-algorithm> is a case-sensitive string and must be one of:
**A128CBC-HS256**
: AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm
**A192CBC-HS384**
: AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm
**A256CBC-HS512**
: AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm
**A128GCM**
: AES GCM using 128-bit key
**A192GCM**
: AES GCM using 192-bit key
**A256GCM** (default)
: AES GCM using 256-bit key`,
},
cli.StringFlag{
Name: "key",
Usage: `The <file> containing the JWE recipient's public key.
JWEs can be encrypted for a recipient using a public JWK or a PEM encoded public key.`,
},
cli.StringFlag{
Name: "jwks",
Usage: `The JWK Set containing the recipient's public key. The <jwks> argument should
be the name of a file. The file contents should be a JWK Set. The **--jwks**
flag requires the use of the **--kid** flag to specify which key to use.`,
},
cli.StringFlag{
Name: "kid",
Usage: `The ID of the recipient's public key. <kid> is a case-sensitive string. When
used with **--key** the <kid> value must match the **"kid"** member of the JWK. When
used with **--jwks** (a JWK Set) the <kid> value must match the **"kid"** member of
one of the JWKs in the JWK Set.`,
},
cli.StringFlag{
Name: "typ, type",
Usage: `The media type of the JWE, used for disambiguation in applications where
more than one type of JWE may be processed. While this parameter might be
useful to applications, it is ignored by JWE implementations.`,
},
cli.StringFlag{
Name: "cty, content-type",
Usage: `The media type of the JWE payload, used for disambiguation of JWE objects in
applications where more than one JWE payload type may be present. This
parameter is ignored by JWE implementations, but may be processed by
applications that use JWE.`,
},
flags.SubtleHidden,
},
}
}
func encryptAction(ctx *cli.Context) error {
if err := errs.NumberOfArguments(ctx, 0); err != nil {
return err
}
data, err := utils.ReadAll(os.Stdin)
if err != nil {
return err
}
// Validate parameters
alg, err := getRecipientAlg(ctx, ctx.String("alg"))
if err != nil {
return err
}
enc, err := getContentEncryptionAlg(ctx, ctx.String("enc"))
if err != nil {
return err
}
var isPBES2 bool
switch alg {
case jose.PBES2_HS256_A128KW, jose.PBES2_HS384_A192KW, jose.PBES2_HS512_A256KW:
isPBES2 = true
}
key := ctx.String("key")
jwks := ctx.String("jwks")
kid := ctx.String("kid")
typ := ctx.String("typ")
cty := ctx.String("cty")
isSubtle := ctx.Bool("subtle")
switch {
case isPBES2 && key != "":
return errs.MutuallyExclusiveFlags(ctx, "alg "+ctx.String("alg"), "key")
case isPBES2 && jwks != "":
return errs.MutuallyExclusiveFlags(ctx, "alg "+ctx.String("alg"), "jwks")
case !isPBES2 && key == "" && jwks == "":
return errs.RequiredOrFlag(ctx, "key", "jwk")
case key != "" && jwks != "":
return errs.MutuallyExclusiveFlags(ctx, "key", "jwks")
case jwks != "" && kid == "":
return errs.RequiredWithFlag(ctx, "kid", "jwks")
}
// Add parse options
var options []jose.Option
options = append(options, jose.WithUse("enc"))
if len(alg) > 0 {
options = append(options, jose.WithAlg(string(alg)))
}
if kid != "" {
options = append(options, jose.WithKid(kid))
}
if isSubtle {
options = append(options, jose.WithSubtle(true))
}
// Read key from --key, --jwks, or a user provided
var pbes2Key []byte
var jwk *jose.JSONWebKey
switch {
case key != "":
jwk, err = jose.ReadKey(key, options...)
case jwks != "":
jwk, err = jose.ReadKeySet(jwks, options...)
case isPBES2:
pbes2Key, err = ui.PromptPassword("Please enter the password to encrypt the content encryption key")
default:
return errs.RequiredOrFlag(ctx, "key", "jwks")
}
if err != nil {
return err
}
var recipient jose.Recipient
if isPBES2 {
recipient = jose.Recipient{
Algorithm: alg,
Key: pbes2Key,
KeyID: kid,
}
} else {
// Public keys are used for encryption
jwkPub := jwk.Public()
jwk = &jwkPub
if jwk.Use == "sig" {
return errors.New("invalid jwk use: found 'sig' (signature), expecting 'enc' (encryption)")
}
// Validate jwk
if err := jose.ValidateJWK(jwk); err != nil {
return err
}
// Prepare recipient
if alg == "" {
alg, err = getRecipientAlg(ctx, jwk.Algorithm)
if err != nil {
return err
}
}
recipient = jose.Recipient{
Algorithm: alg,
Key: jwk,
KeyID: kid,
}
}
// Add extra headers
opts := new(jose.EncrypterOptions)
if typ != "" {
opts.WithType(jose.ContentType(typ))
}
if cty != "" {
opts.WithContentType(jose.ContentType(cty))
}
// Encrypt
encrypter, err := jose.NewEncrypter(enc, recipient, opts)
if err != nil {
return errs.Wrap(err, "error creating cipher")
}
obj, err := encrypter.Encrypt(data)
if err != nil {
return errs.Wrap(err, "error encrypting data")
}
fmt.Println(obj.FullSerialize())
return nil
}
func getContentEncryptionAlg(ctx *cli.Context, enc string) (jose.ContentEncryption, error) {
switch enc {
case "":
return jose.A256GCM, nil
case "A128GCM":
return jose.A128GCM, nil
case "A192GCM":
return jose.A192GCM, nil
case "A256GCM":
return jose.A256GCM, nil
case "A128CBC-HS256":
return jose.A128CBC_HS256, nil
case "A192CBC-HS384":
return jose.A192CBC_HS384, nil
case "A256CBC-HS512":
return jose.A256CBC_HS512, nil
default:
return "", errs.InvalidFlagValue(ctx, "enc", enc, "")
}
}
func getRecipientAlg(ctx *cli.Context, alg string) (jose.KeyAlgorithm, error) {
switch alg {
case "":
return "", nil
case "RSA1_5":
return jose.RSA1_5, nil
case "RSA-OAEP":
return jose.RSA_OAEP, nil
case "RSA-OAEP-256":
return jose.RSA_OAEP_256, nil
case "A128KW":
return jose.A128KW, nil
case "A192KW":
return jose.A192KW, nil
case "A256KW":
return jose.A256KW, nil
case "dir":
return jose.DIRECT, nil
case "ECDH-ES":
return jose.ECDH_ES, nil
case "ECDH-ES+A128KW":
return jose.ECDH_ES_A128KW, nil
case "ECDH-ES+A192KW":
return jose.ECDH_ES_A192KW, nil
case "ECDH-ES+A256KW":
return jose.ECDH_ES_A256KW, nil
case "A128GCMKW":
return jose.A128GCMKW, nil
case "A192GCMKW":
return jose.A192GCMKW, nil
case "A256GCMKW":
return jose.A256GCMKW, nil
case "PBES2-HS256+A128KW":
return jose.PBES2_HS256_A128KW, nil
case "PBES2-HS384+A192KW":
return jose.PBES2_HS384_A192KW, nil
case "PBES2-HS512+A256KW":
return jose.PBES2_HS512_A256KW, nil
default:
return "", errs.InvalidFlagValue(ctx, "alg", alg, "")
}
}