Repository navigation
Expand file tree
/
Copy pathdecrypt.go
More file actions
313 lines (278 loc) · 11.4 KB
/
Copy pathdecrypt.go
File metadata and controls
313 lines (278 loc) · 11.4 KB
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
package jwe
import (
"crypto/ecdsa"
stdjson "encoding/json"
"fmt"
"math"
"github.com/lestrrat-go/jwx/v4/internal/base64"
"github.com/lestrrat-go/jwx/v4/jwa"
"github.com/lestrrat-go/jwx/v4/jwe/internal/content_crypt"
"github.com/lestrrat-go/jwx/v4/jwe/jwebb"
"github.com/lestrrat-go/jwx/v4/jwk"
)
// decryptCEKContext holds algorithm-agnostic context needed during CEK decryption.
type decryptCEKContext struct {
maxPBES2Count int
minPBES2Count int
ctalg jwa.ContentEncryptionAlgorithm
contentCipher content_crypt.Cipher
}
// decryptCEK dispatches key decryption to the appropriate per-family
// function based on the algorithm. Each function extracts its own
// algorithm-specific parameters from the merged headers.
func decryptCEK(alg jwa.KeyEncryptionAlgorithm, key any, msg *Message, recipient Recipient, headers Headers, ctx *decryptCEKContext) ([]byte, error) {
algStr := alg.String()
recipientKey := recipient.EncryptedKey()
// Direct-mode key management (RFC 7518 §4.5 "dir", §4.6 bare ECDH-ES, and
// direct ML-KEM) derives the CEK without an encrypted key, so the JWE
// Encrypted Key must be the empty octet sequence. Enforce this here, before
// the KeyDecrypter branch, so a tampered message carrying a stray
// encrypted_key is rejected on every path -- including caller-supplied
// custom decrypters. IsMLKEMDirect consults a runtime registry that is
// empty unless the ML-KEM companion module is imported, so this clause is a
// no-op in builds without ML-KEM.
directMode := jwebb.IsDirect(algStr) || jwebb.IsMLKEMDirect(algStr)
if !directMode && jwebb.IsECDHES(algStr) {
// ECDH-ES+A*KW (keywrap == true) legitimately carries an encrypted_key;
// only bare ECDH-ES is direct. Reuse the same helper the ECDH-ES path
// uses to determine keywrap.
if _, _, keywrap, err := jwebb.KeyEncryptionECDHESKeySize(algStr, ctx.ctalg.String()); err == nil {
directMode = !keywrap
}
}
if directMode && len(recipientKey) != 0 {
return nil, fmt.Errorf(`jwe: decrypt key: %q requires an empty encrypted_key`, algStr)
}
if kd, ok := key.(KeyDecrypter); ok {
return kd.DecryptKey(alg, recipientKey, recipient, msg)
}
switch {
case jwebb.IsDirect(algStr):
return decryptKeyDirect(recipientKey, algStr, key)
case jwebb.IsPBES2(algStr):
return decryptKeyPBES2(recipientKey, algStr, key, headers, ctx.maxPBES2Count, ctx.minPBES2Count)
case jwebb.IsAESGCMKW(algStr):
return decryptKeyAESGCMKW(recipientKey, algStr, key, headers)
case jwebb.IsECDHES(algStr):
return decryptKeyECDHES(recipientKey, algStr, ctx.ctalg, key, headers)
case jwebb.IsMLKEM(algStr):
return decryptKeyMLKEM(recipientKey, algStr, ctx.ctalg, key, headers)
case jwebb.IsHPKE(algStr):
return decryptKeyHPKE(recipientKey, algStr, ctx.ctalg, key, headers)
case jwebb.IsRSA15(algStr):
return decryptKeyRSA15(recipientKey, algStr, key, ctx.contentCipher)
case jwebb.IsRSAOAEP(algStr):
return decryptKeyRSAOAEP(recipientKey, algStr, key)
case jwebb.IsAESKW(algStr):
return decryptKeyAESKW(recipientKey, algStr, key)
default:
return nil, fmt.Errorf(`jwe: decrypt key: unsupported algorithm (%s)`, algStr)
}
}
func decryptKeyDirect(recipientKey []byte, alg string, key any) ([]byte, error) {
// The empty-encrypted_key invariant for "dir" (RFC 7518 §4.5) is enforced
// in decryptCEK ahead of the KeyDecrypter branch, so it covers every path.
cek, err := requireByteKey(key, alg)
if err != nil {
return nil, err
}
return jwebb.KeyDecryptDirect(recipientKey, recipientKey, alg, cek)
}
func decryptKeyPBES2(recipientKey []byte, alg string, key any, headers Headers, maxCount, minCount int) ([]byte, error) {
password, err := requireByteKey(key, alg)
if err != nil {
return nil, err
}
saltV, ok := headers.Field(SaltKey)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: missing %q field for PBES2`, SaltKey)
}
saltB64, ok := saltV.(string)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: %q field is not a string`, SaltKey)
}
countV, ok := headers.Field(CountKey)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: missing %q field for PBES2`, CountKey)
}
// Parse p2c into int64 directly. Float64 cannot represent integers
// above 2^53 exactly; comparing a parsed value against a high
// MaxPBES2Count cap in float-space and then casting via int(...) lets
// out-of-range values silently round into the accepted range, which
// would defeat the cap when callers raise it past 2^53. int64 keeps
// the bound check exact.
var count int64
switch v := countV.(type) {
case float64:
if math.IsNaN(v) || math.IsInf(v, 0) || math.Trunc(v) != v {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2c' value: not a positive integer (got %v)`, v)
}
// Reject values outside int64 range before casting; the cast
// of an out-of-range float to int is implementation-defined.
// Use explicit float-domain bounds (2^63 / -2^63) instead of
// math.MaxInt64 / MinInt64 so the comparison is independent
// of the platform's int width and the constants do not need
// implicit conversion.
const (
int64MaxAsFloat = float64(1 << 63) // 2^63, the smallest float > MaxInt64
int64MinAsFloat = -int64MaxAsFloat // -2^63, exact float = MinInt64
)
if v >= int64MaxAsFloat || v < int64MinAsFloat {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2c' value: not representable as int64 (got %v)`, v)
}
count = int64(v)
case stdjson.Number:
c, err := v.Int64()
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2c' value: %q is not a valid integer: %w`, v.String(), err)
}
count = c
default:
return nil, fmt.Errorf(`jwe: decrypt key: %q field is not a number`, CountKey)
}
if count < int64(minCount) {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2c' value: %d is below WithMinPBES2Count=%d (RFC 7518 §4.8.1.2 floor; loosen via jwe.WithMinPBES2Count)`, count, minCount)
}
if count > int64(maxCount) {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2c' value: %d exceeds WithMaxPBES2Count=%d (DoS amplification cap; raise via jwe.WithMaxPBES2Count)`, count, maxCount)
}
saltBytes, err := base64.DecodeString(saltB64)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to decode 'p2s': %w`, err)
}
// RFC 7518 §4.8.1.1 requires the salt input to be at least 8 octets.
// This is a hard floor and is not loosenable via an option.
if len(saltBytes) < 8 {
return nil, fmt.Errorf(`jwe: decrypt key: invalid 'p2s' value: salt is %d octets, RFC 7518 §4.8.1.1 requires at least 8`, len(saltBytes))
}
salt := []byte(alg)
salt = append(salt, byte(0))
salt = append(salt, saltBytes...)
return jwebb.KeyDecryptPBES2(recipientKey, recipientKey, alg, password, salt, int(count))
}
func decryptKeyAESGCMKW(recipientKey []byte, alg string, key any, headers Headers) ([]byte, error) {
sharedkey, err := requireByteKey(key, alg)
if err != nil {
return nil, err
}
var keyiv, keytag []byte
if ivV, ok := headers.Field(InitializationVectorKey); ok {
ivB64, ok := ivV.(string)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: %q is not a string`, InitializationVectorKey)
}
keyiv, err = base64.DecodeString(ivB64)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to decode 'iv': %w`, err)
}
}
if tagV, ok := headers.Field(TagKey); ok {
tagB64, ok := tagV.(string)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: %q is not a string`, TagKey)
}
keytag, err = base64.DecodeString(tagB64)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to decode 'tag': %w`, err)
}
}
return jwebb.KeyDecryptAESGCMKW(recipientKey, recipientKey, alg, sharedkey, keyiv, keytag)
}
func decryptKeyECDHES(recipientKey []byte, alg string, ctalg jwa.ContentEncryptionAlgorithm, key any, headers Headers) ([]byte, error) {
ctalgStr := ctalg.String()
derivedAlg, keysize, keywrap, err := jwebb.KeyEncryptionECDHESKeySize(alg, ctalgStr)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to determine ECDH-ES key size: %w`, err)
}
// RFC 7518 §4.6: for bare ECDH-ES (keywrap == false) the CEK is derived
// directly and the JWE Encrypted Key must be the empty octet sequence.
// That invariant is enforced in decryptCEK ahead of the KeyDecrypter
// branch so it covers every path; ECDH-ES+A*KW legitimately carries an
// encrypted_key.
// Extract ephemeral public key from headers
epkV, ok := headers.Field(EphemeralPublicKeyKey)
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: missing 'epk' field for ECDH-ES`)
}
var pubkey any
switch epk := epkV.(type) {
case jwk.ECDSAPublicKey:
pubkey, err = jwk.Export[*ecdsa.PublicKey](epk)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to export ECDSA public key: %w`, err)
}
case jwk.OKPPublicKey:
pubkey, err = jwk.Export[any](epk)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: failed to export OKP public key: %w`, err)
}
default:
return nil, fmt.Errorf("jwe: decrypt key: unexpected 'epk' type %T for %s", epk, alg)
}
var apu, apv []byte
if v, ok := headers.AgreementPartyUInfo(); ok && len(v) > 0 {
apu = v
}
if v, ok := headers.AgreementPartyVInfo(); ok && len(v) > 0 {
apv = v
}
deriver, err := jwebb.NewECDHESKeyDeriver(key)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: %w`, err)
}
return jwebb.KeyDecryptECDHESCustom(recipientKey, derivedAlg, apu, apv, deriver, pubkey, keysize, keywrap)
}
func decryptKeyHPKE(recipientKey []byte, alg string, ctalg jwa.ContentEncryptionAlgorithm, key any, headers Headers) ([]byte, error) {
ctalgStr := ctalg.String()
ek, ok := headers.EncapsulatedKey()
if !ok {
return nil, makeHPKEError(`decrypt key (HPKE): missing 'ek' field`)
}
cek, err := jwebb.KeyDecryptHPKEKE(recipientKey, alg, ctalgStr, key, ek)
if err != nil {
return nil, makeHPKEError(`decrypt key (HPKE): %w`, err)
}
return cek, nil
}
func decryptKeyMLKEM(recipientKey []byte, alg string, ctalg jwa.ContentEncryptionAlgorithm, key any, headers Headers) ([]byte, error) {
ctalgStr := ctalg.String()
ek, ok := headers.EncapsulatedKey()
if !ok {
return nil, fmt.Errorf(`jwe: decrypt key: missing 'ek' field for ML-KEM`)
}
dec, err := mlkemDecrypterFromKey(key)
if err != nil {
return nil, fmt.Errorf(`jwe: decrypt key: %w`, err)
}
return jwebb.KeyDecryptMLKEMCustom(recipientKey, alg, ctalgStr, dec, ek)
}
// mlkemDecrypterFromKey is the decrypt-side counterpart to
// mlkemEncrypterFromKey. See its doc for the conversion strategy.
func mlkemDecrypterFromKey(key any) (jwebb.MLKEMKeyDecrypter, error) {
if d, ok := key.(jwebb.MLKEMKeyDecrypter); ok {
return d, nil
}
jkey, ok := key.(jwk.Key)
if !ok {
imported, err := jwk.Import[jwk.Key](key)
if err != nil {
return nil, fmt.Errorf(`ML-KEM: cannot convert %T (import github.com/jwx-go/mlkem/v4 to enable ML-KEM): %w`, key, err)
}
jkey = imported
}
return jwk.Export[jwebb.MLKEMKeyDecrypter](jkey)
}
func decryptKeyRSA15(recipientKey []byte, _ string, key any, contentCipher content_crypt.Cipher) ([]byte, error) {
keysize := contentCipher.KeySize() / 2
return jwebb.KeyDecryptRSA15(recipientKey, recipientKey, key, keysize)
}
func decryptKeyRSAOAEP(recipientKey []byte, alg string, key any) ([]byte, error) {
return jwebb.KeyDecryptRSAOAEP(recipientKey, recipientKey, alg, key)
}
func decryptKeyAESKW(recipientKey []byte, alg string, key any) ([]byte, error) {
sharedkey, err := requireByteKey(key, alg)
if err != nil {
return nil, err
}
return jwebb.KeyDecryptAESKW(recipientKey, recipientKey, alg, sharedkey)
}