forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwe.go
244 lines (215 loc) · 7.46 KB
/
jwe.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
//go:generate go run internal/cmd/genheader/main.go
// Package jwe implements JWE as described in https://tools.ietf.org/html/rfc7516
package jwe
import (
"bytes"
"crypto/ecdsa"
"crypto/rsa"
"github.com/lestrrat-go/jwx/internal/json"
"github.com/lestrrat-go/jwx/internal/keyconv"
"github.com/lestrrat-go/jwx/buffer"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwe/internal/content_crypt"
"github.com/lestrrat-go/jwx/jwe/internal/keyenc"
"github.com/lestrrat-go/jwx/jwe/internal/keygen"
"github.com/lestrrat-go/jwx/x25519"
"github.com/lestrrat-go/pdebug/v3"
"github.com/pkg/errors"
)
// Encrypt takes the plaintext payload and encrypts it in JWE compact format.
func Encrypt(payload []byte, keyalg jwa.KeyEncryptionAlgorithm, key interface{}, contentalg jwa.ContentEncryptionAlgorithm, compressalg jwa.CompressionAlgorithm) ([]byte, error) {
if pdebug.Enabled {
g := pdebug.FuncMarker()
defer g.End()
}
contentcrypt, err := content_crypt.NewGeneric(contentalg)
if err != nil {
return nil, errors.Wrap(err, `failed to create AES encrypter`)
}
var enc keyenc.Encrypter
switch keyalg {
case jwa.RSA1_5:
var pubkey rsa.PublicKey
if err := keyconv.RSAPublicKey(&pubkey, key); err != nil {
return nil, errors.Errorf("failed to build %s key encrypter", keyalg)
}
enc, err = keyenc.NewRSAPKCSEncrypt(keyalg, &pubkey)
if err != nil {
return nil, errors.Wrap(err, "failed to create RSA PKCS encrypter")
}
case jwa.RSA_OAEP, jwa.RSA_OAEP_256:
var pubkey rsa.PublicKey
if err := keyconv.RSAPublicKey(&pubkey, key); err != nil {
return nil, errors.Errorf("failed to build %s key encrypter", keyalg)
}
enc, err = keyenc.NewRSAOAEPEncrypt(keyalg, &pubkey)
if err != nil {
return nil, errors.Wrap(err, "failed to create RSA OAEP encrypter")
}
case jwa.A128KW, jwa.A192KW, jwa.A256KW,
jwa.A128GCMKW, jwa.A192GCMKW, jwa.A256GCMKW,
jwa.PBES2_HS256_A128KW, jwa.PBES2_HS384_A192KW, jwa.PBES2_HS512_A256KW:
sharedkey, ok := key.([]byte)
if !ok {
return nil, errors.New("invalid key: []byte required")
}
switch keyalg {
case jwa.A128KW, jwa.A192KW, jwa.A256KW:
enc, err = keyenc.NewAES(keyalg, sharedkey)
case jwa.PBES2_HS256_A128KW, jwa.PBES2_HS384_A192KW, jwa.PBES2_HS512_A256KW:
enc, err = keyenc.NewPBES2Encrypt(keyalg, sharedkey)
default:
enc, err = keyenc.NewAESGCMEncrypt(keyalg, sharedkey)
}
if err != nil {
return nil, errors.Wrap(err, "failed to create key wrap encrypter")
}
// NOTE: there was formerly a restriction, introduced
// in PR #26, which disallowed certain key/content
// algorithm combinations. This seemed bogus, and
// interop with the jose tool demonstrates it.
case jwa.ECDH_ES, jwa.ECDH_ES_A128KW, jwa.ECDH_ES_A192KW, jwa.ECDH_ES_A256KW:
var keysize int
switch keyalg {
case jwa.ECDH_ES:
// https://tools.ietf.org/html/rfc7518#page-15
// In Direct Key Agreement mode, the output of the Concat KDF MUST be a
// key of the same length as that used by the "enc" algorithm.
keysize = contentcrypt.KeySize()
case jwa.ECDH_ES_A128KW:
keysize = 16
case jwa.ECDH_ES_A192KW:
keysize = 24
case jwa.ECDH_ES_A256KW:
keysize = 32
}
switch key := key.(type) {
case x25519.PublicKey:
enc, err = keyenc.NewECDHESEncrypt(keyalg, contentalg, keysize, key)
default:
var pubkey ecdsa.PublicKey
if err := keyconv.ECDSAPublicKey(&pubkey, key); err != nil {
return nil, errors.Errorf("failed to build %s key encrypter", keyalg)
}
enc, err = keyenc.NewECDHESEncrypt(keyalg, contentalg, keysize, &pubkey)
}
if err != nil {
return nil, errors.Wrap(err, "failed to create ECDHS key wrap encrypter")
}
case jwa.DIRECT:
sharedkey, ok := key.([]byte)
if !ok {
return nil, errors.New("invalid key: []byte required")
}
enc, _ = keyenc.NewNoop(keyalg, sharedkey)
default:
if pdebug.Enabled {
pdebug.Printf("Encrypt: unknown key encryption algorithm: %s", keyalg)
}
return nil, errors.Errorf(`invalid key encryption algorithm (%s)`, keyalg)
}
keysize := contentcrypt.KeySize()
if pdebug.Enabled {
pdebug.Printf("Encrypt: keysize = %d", keysize)
}
encctx := getEncryptCtx()
defer releaseEncryptCtx(encctx)
encctx.contentEncrypter = contentcrypt
encctx.generator = keygen.NewRandom(keysize)
encctx.keyEncrypters = []keyenc.Encrypter{enc}
encctx.compress = compressalg
msg, err := encctx.Encrypt(payload)
if err != nil {
if pdebug.Enabled {
pdebug.Printf("Encrypt: failed to encrypt: %s", err)
}
return nil, errors.Wrap(err, "failed to encrypt payload")
}
return Compact(msg)
}
// Decrypt takes the key encryption algorithm and the corresponding
// key to decrypt the JWE message, and returns the decrypted payload.
// The JWE message can be either compact or full JSON format.
func Decrypt(buf []byte, alg jwa.KeyEncryptionAlgorithm, key interface{}) ([]byte, error) {
msg, err := Parse(buf)
if err != nil {
return nil, errors.Wrap(err, "failed to parse buffer for Decrypt")
}
return msg.Decrypt(alg, key)
}
// Parse parses the JWE message into a Message object. The JWE message
// can be either compact or full JSON format.
func Parse(buf []byte) (*Message, error) {
buf = bytes.TrimSpace(buf)
if len(buf) == 0 {
return nil, errors.New("empty buffer")
}
if buf[0] == '{' {
return parseJSON(buf)
}
return parseCompact(buf)
}
// ParseString is the same as Parse, but takes a string.
func ParseString(s string) (*Message, error) {
return Parse([]byte(s))
}
func parseJSON(buf []byte) (*Message, error) {
m := NewMessage()
if err := json.Unmarshal(buf, &m); err != nil {
return nil, errors.Wrap(err, "failed to parse JSON")
}
return m, nil
}
func parseCompact(buf []byte) (*Message, error) {
if pdebug.Enabled {
pdebug.Printf("Parse(Compact): buf = '%s'", buf)
}
parts := bytes.Split(buf, []byte{'.'})
if len(parts) != 5 {
return nil, errors.Errorf(`compact JWE format must have five parts (%d)`, len(parts))
}
hdrbuf := buffer.Buffer{}
if err := hdrbuf.Base64Decode(parts[0]); err != nil {
return nil, errors.Wrap(err, `failed to parse first part of compact form`)
}
if pdebug.Enabled {
pdebug.Printf("hdrbuf = %s", hdrbuf)
}
protected := NewHeaders()
if err := json.Unmarshal(hdrbuf, protected); err != nil {
return nil, errors.Wrap(err, "failed to parse header JSON")
}
var enckeybuf buffer.Buffer
if err := enckeybuf.Base64Decode(parts[1]); err != nil {
return nil, errors.Wrap(err, "failed to base64 decode encryption key")
}
var ivbuf buffer.Buffer
if err := ivbuf.Base64Decode(parts[2]); err != nil {
return nil, errors.Wrap(err, "failed to base64 decode iv")
}
var ctbuf buffer.Buffer
if err := ctbuf.Base64Decode(parts[3]); err != nil {
return nil, errors.Wrap(err, "failed to base64 decode content")
}
var tagbuf buffer.Buffer
if err := tagbuf.Base64Decode(parts[4]); err != nil {
return nil, errors.Wrap(err, "failed to base64 decode tag")
}
m := NewMessage()
if err := m.Set(CipherTextKey, ctbuf); err != nil {
return nil, errors.Wrapf(err, `failed to set %s`, CipherTextKey)
}
if err := m.Set(InitializationVectorKey, ivbuf); err != nil {
return nil, errors.Wrapf(err, `failed to set %s`, InitializationVectorKey)
}
if err := m.Set(ProtectedHeadersKey, protected); err != nil {
return nil, errors.Wrapf(err, `failed to set %s`, ProtectedHeadersKey)
}
if err := m.makeDummyRecipient(enckeybuf, protected); err != nil {
return nil, errors.Wrap(err, `failed to setup recipient`)
}
if err := m.Set(TagKey, tagbuf); err != nil {
return nil, errors.Wrapf(err, `failed to set %s`, TagKey)
}
return m, nil
}