Debian Codegraph @

command/crypto/jwe/jwe.go

raw symbols SCIP
1 package jwe
2
3 import "github.com/urfave/cli"
4
5 // Command returns the jwe subcommand.
6 func Command() cli.Command {
7 return cli.Command{
8 Name: "jwe",
9 Usage: "encrypt and decrypt data and keys using JSON Web Encryption (JWE)",
10 UsageText: "step crypto jwe <subcommand> [arguments] [global-flags] [subcommand-flags]",
11 Description: `The **step crypto jwe** command group provides facilities for encrypting and
12 decrypting content and representing encrypted content using JSON-based data
13 structures as defined by the JSON Web Encryption (JWE) specification in
14 RFC7516, using algorithms defined in the JSON Web Algorithms (JWA)
15 specification in RFC7518. A JWE is a data structure representing an encrypted
16 and integrity-protected message.
17
18 There are two JWE serializations: the compact serialization is a small, URL-
19 safe representation that base64 encodes the JWE components. The compact
20 serialization is a URL-safe string, suitable for space-constrained
21 environments such as HTTP headers and URI query parameters. The JSON
22 serialization represents JWEs as JSON objects and allows the same content to
23 be encrypted to multiple parties (using multiple keys).
24
25 A typical JWE in compact serialization is a dot-separated string with five
26 parts:
27
28 * Header: metadata describing how the plaintext payload was processed to
29 produce ciphertext (e.g., which algorithms were used to encrypt the
30 content encryption key and the plaintext payload)
31
32 * Encrypted Key: the "content encryption key" that was used to encrypt the
33 plaintext payload, encrypted for the JWE recipient(s) (see: "what's with
34 the encrypted key" below)
35
36 * Initialization Vector: an initialization vector for use with the specified
37 encryption algorithm, if applicable
38
39 * Ciphertext: the ciphertext value resulting produced from authenticated
40 encryption of the plaintext with additional authenticated data
41
42 * Authentication Tag: value resulting fromthe authenticated encryption of
43 the plaintext with additional authenticated data
44
45 ## What's with encrypted key?
46
47 This is somewhat confusing. Instead of directly encrypting the plaintext
48 payload, JWE typically generates a new "content encryption key" then encrypts
49 *that key* for the intended recipient(s).
50
51 While versatile, JWE is easy to use incorrectly. Therefore, any use of this
52 subcommand requires the use of the '--subtle' flag as a misuse prevention
53 mechanism. You should only use this subcommand if you know what you're doing.
54 If possible, you're better off using the higher level 'crypto nacl' command
55 group.
56
57 ## EXAMPLES
58
59 This example demonstrates how to produce a JWE for a recipient using the
60 RSA-OAEP algorithm to encrypt the content encryption key (producing the
61 encrypted key), and the A256GCM (AES GCM with 256-bit key) algorithm to
62 produce the ciphertext and authentication tag.
63
64 1. Encode the JWE header with the desired "alg" and "enc" members then
65 encode it producing the *header*
66 '''raw
67 BASE64URL(UTF8({"alg":"RSA-OAEP","enc":"A256GCM"}))
68 => eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ
69 '''
70
71 2. Generate a random content encryption key (CEK), encrypt it using
72 RSA-OAEP, producing the *encrypted key*
73
74 3. Generate a random initialization vector
75
76 4. Perform authenticated encryption over the plaintext using the content
77 encryption key and A256GCM algorithm with the base64-encoded JWE headers
78 provided as additional authenticated data producing the *ciphertext* and
79 *authentication tag*
80
81 5. Assemble the final result (compact serialization) to produce the string:
82 '''raw
83 BASE64URL(UTF8(header)) || '.'
84 || BASE64URL(encrypted key) || '.'
85 || BASE64URL(initialization vector) || '.'
86 || BASE64URL(ciphertext) || '.'
87 || BASE64URL(authentication tag)
88 '''
89 Producing a result like (line breaks for display purposes only):
90 '''raw
91 eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkEyNTZHQ00ifQ.
92 OKOawDo13gRp2ojaHV7LFpZcgV7T6DVZKTyKOMTYUmKoTCVJRgckCL9kiMT03JGe
93 ipsEdY3mx_etLbbWSrFr05kLzcSr4qKAq7YN7e9jwQRb23nfa6c9d-StnImGyFDb
94 Sv04uVuxIp5Zms1gNxKKK2Da14B8S4rzVRltdYwam_lDp5XnZAYpQdb76FdIKLaV
95 mqgfwX7XWRxv2322i-vDxRfqNzo_tETKzpVLzfiwQyeyPGLBIO56YJ7eObdv0je8
96 1860ppamavo35UgoRdbYaBcoh9QcfylQr66oc6vFWXRcZ_ZT2LawVCWTIy3brGPi
97 6UklfCpIMfIjf7iGdXKHzg.
98 48V1_ALb6US04U3b.
99 5eym8TW_c8SuK0ltJ3rpYIzOeDQz7TALvtu6UG9oMo4vpzs9tX_EFShS8iB7j6ji
100 SdiwkIr3ajwQzaBtQD_A.
101 XFBoMYUZodetZdvTiFvSkQ
102 '''
103
104 Create a JWK for encryption use:
105 '''
106 $ step crypto jwk create --use enc p256.enc.pub p256.enc.priv
107 '''
108
109 Encrypt a message using the previous public key (output indented for display purposes):
110 '''
111 $ echo The message | step crypto jwe encrypt --key p256.enc.pub
112 {
113 "protected":"eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6Ii1hakZFVlZaSWNRa0RxbkhpZ0NOWU5fa29nZkhxZnRGX1N3c2ZQeXlSRUUiLCJ5IjoicGpjVnJJZHRHSVpka05HS1FETEpIdG5SLUxudUI2V3k4bHpuX3REdm9BUSJ9LCJraWQiOiJHd0tSTUdXY1pWNFE2dGZZblpjZm90N090N2hjQ0t2cUJPVWljX0JoZ0gwIn0",
114 "iv":"-10PlAIteHLVABtt",
115 "ciphertext":"_xnGoE7vPCrXRRlK",
116 "tag":"wcvj4sXXMc9qII_ySYNYGA"
117 }
118 '''
119
120 Decrypt the previous message using the private key:
121 '''
122 $ step crypto jwe decrypt --key p256.enc.priv \< message.json
123 Please enter the password to decrypt p256.enc.priv: ********
124 The message
125 '''
126
127 Encrypt a message using a shared password:
128 '''
129 $ echo The message | step crypto jwe encrypt --alg PBES2-HS256+A128KW
130 Please enter the password to encrypt the content encryption key: ********
131 {
132 "protected":"eyJhbGciOiJQQkVTMi1IUzI1NitBMTI4S1ciLCJlbmMiOiJBMjU2R0NNIiwicDJjIjoxMDAwMDAsInAycyI6ImpKMnJpejJGZnhoSXVOS3JSYUJqc2cifQ",
133 "encrypted_key":"p4xazaWvaAYC7NbHoAQTC4DxCX-rEjs7wvRF-OvaVliYzhdRtEdgzA",
134 "iv":"Jw4JCCr-lLrE0irT",
135 "ciphertext":"jcb3wKopsHmClh7s",
136 "tag":"7ttDDDfuqA45puDu7KbVkA"
137 }
138 '''
139
140 Decrypt a message protected with shared password:
141 '''
142 $ step crypto jwe decrypt \< message.json
143 Please enter the password to decrypt the content encryption key: ********
144 The message
145 '''`,
146 Subcommands: cli.Commands{
147 encryptCommand(),
148 decryptCommand(),
149 },
150 }
151 }