Skip to content

Commit

Permalink
Add jwe.WithCEK
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Oct 30, 2023
1 parent 16acb8b commit b8636c0
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
9 changes: 9 additions & 0 deletions jwe/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type decrypter struct {
aad []byte
apu []byte
apv []byte
cek *[]byte
computedAad []byte
iv []byte
keyiv []byte
Expand Down Expand Up @@ -120,6 +121,11 @@ func (d *decrypter) Tag(tag []byte) *decrypter {
return d
}

func (d *decrypter) CEK(ptr *[]byte) *decrypter {
d.cek = ptr
return d
}

func (d *decrypter) ContentCipher() (content_crypt.Cipher, error) {
if d.cipher == nil {
switch d.ctalg {
Expand Down Expand Up @@ -161,6 +167,9 @@ func (d *decrypter) Decrypt(recipient Recipient, ciphertext []byte, msg *Message
return
}

if d.cek != nil {
*d.cek = cek
}
return plaintext, nil
}

Expand Down
9 changes: 7 additions & 2 deletions jwe/jwe.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ func Encrypt(payload []byte, options ...EncryptOption) ([]byte, error) {
type decryptCtx struct {
msg *Message
aad []byte
cek *[]byte
computedAad []byte
keyProviders []KeyProvider
protectedHeaders Headers
Expand All @@ -438,7 +439,7 @@ type decryptCtx struct {
func Decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
var keyProviders []KeyProvider
var keyUsed interface{}

var cek *[]byte
var dst *Message
//nolint:forcetypeassert
for _, option := range options {
Expand All @@ -459,6 +460,8 @@ func Decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
alg: alg,
key: pair.key,
})
case identCEK{}:
cek = option.Value().(*[]byte)
}
}

Expand Down Expand Up @@ -517,6 +520,7 @@ func Decrypt(buf []byte, options ...DecryptOption) ([]byte, error) {
dctx.msg = msg
dctx.keyProviders = keyProviders
dctx.protectedHeaders = h
dctx.cek = cek

var lastError error
for _, recipient := range recipients {
Expand Down Expand Up @@ -583,7 +587,8 @@ func (dctx *decryptCtx) decryptContent(ctx context.Context, alg jwa.KeyEncryptio
AuthenticatedData(dctx.aad).
ComputedAuthenticatedData(dctx.computedAad).
InitializationVector(dctx.msg.initializationVector).
Tag(dctx.msg.tag)
Tag(dctx.msg.tag).
CEK(dctx.cek)

if recipient.Headers().Algorithm() != alg {
// algorithms don't match
Expand Down
15 changes: 15 additions & 0 deletions jwe/jwe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,18 @@ func TestGH924(t *testing.T) {
require.NoError(t, err, `jwe.Decrypt should succeed`)
require.Equal(t, payload, decrypted, `decrypt messages match`)
}

func TestGH1001(t *testing.T) {
rawKey, err := jwxtest.GenerateRsaKey()
require.NoError(t, err, `jwxtest.GenerateRsaKey should succeed`)

encrypted, err := jwe.Encrypt([]byte("Lorem Ipsum"), jwe.WithKey(jwa.RSA_OAEP, rawKey.PublicKey))
require.NoError(t, err, `jwe.Encrypt should succeed`)
var cek []byte
decrypted, err := jwe.Decrypt(encrypted, jwe.WithKey(jwa.RSA_OAEP, rawKey), jwe.WithCEK(&cek))
require.NoError(t, err, `jwe.Decrypt should succeed`)

require.Equal(t, "Lorem Ipsum", string(decrypted), `decrypted message should match`)

require.NotNil(t, cek, `cek should not be nil`)
}
8 changes: 7 additions & 1 deletion jwe/options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,10 @@ options:
`jwk.Key` here unless you are 100% sure that all keys that you
have provided are instances of `jwk.Key` (remember that the
jwx API allows users to specify a raw key such as *rsa.PublicKey)
- ident: CEK
interface: DecryptOption
argument_type: '*[]byte'
comment: |
WithCEK allows users to specify a variable to store the CEK used in the
message upon successful decryption. The variable must be a pointer to
a byte slice, and it will only be populated if the decryption is successful.
12 changes: 12 additions & 0 deletions jwe/options_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions jwe/options_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8636c0

Please sign in to comment.