Skip to content

Commit

Permalink
allow non flattened representation for jwe
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Mollard authored and simo5 committed Apr 4, 2024
1 parent ecde4ef commit 1169eca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
27 changes: 16 additions & 11 deletions jwcrypto/jwe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class JWE:

def __init__(self, plaintext=None, protected=None, unprotected=None,
aad=None, algs=None, recipient=None, header=None,
header_registry=None):
header_registry=None, flattened=True):
"""Creates a JWE token.
:param plaintext(bytes): An arbitrary plaintext to be encrypted.
Expand All @@ -93,11 +93,13 @@ def __init__(self, plaintext=None, protected=None, unprotected=None,
:param recipient: An optional, default recipient key
:param header: An optional header for the default recipient
:param header_registry: Optional additions to the header registry
:param flattened: Use flattened serialization syntax (default True)
"""
self._allowed_algs = None
self.objects = {}
self.plaintext = None
self.header_registry = JWSEHeaderRegistry(JWEHeaderRegistry)
self.flattened = flattened
if header_registry:
self.header_registry.update(header_registry)
if plaintext is not None:
Expand Down Expand Up @@ -253,17 +255,20 @@ def add_recipient(self, key, header=None):

if 'recipients' in self.objects:
self.objects['recipients'].append(rec)
elif 'encrypted_key' in self.objects or 'header' in self.objects:
self.objects['recipients'] = []
n = {}
if 'encrypted_key' in self.objects:
n['encrypted_key'] = self.objects.pop('encrypted_key')
if 'header' in self.objects:
n['header'] = self.objects.pop('header')
self.objects['recipients'].append(n)
self.objects['recipients'].append(rec)
elif self.flattened:
if 'encrypted_key' in self.objects or 'header' in self.objects:
self.objects['recipients'] = []
n = {}
if 'encrypted_key' in self.objects:
n['encrypted_key'] = self.objects.pop('encrypted_key')
if 'header' in self.objects:
n['header'] = self.objects.pop('header')
self.objects['recipients'].append(n)
self.objects['recipients'].append(rec)
else:
self.objects.update(rec)
else:
self.objects.update(rec)
self.objects['recipients'] = [rec]

def serialize(self, compact=False):
"""Serializes the object into a JWE token.
Expand Down
12 changes: 12 additions & 0 deletions jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,18 @@ def test_decrypt_keyset(self):
with self.assertRaises(JWKeyNotFound):
e4.deserialize(e3.serialize(), ks)

def test_serialize_not_flattened(self):
# JWE with flattened=False adds recipients in objects and in serialized
e = jwe.JWE(E_A1_ex['plaintext'], flattened=False)
e.add_recipient(E_A1_ex['key'], E_A1_ex['protected'])
self.assertIn('recipients', e.objects)
self.assertIn('recipients', e.serialize())

e = jwe.JWE(E_A1_ex['plaintext'])
e.add_recipient(E_A1_ex['key'], E_A1_ex['protected'])
self.assertNotIn('recipients', e.objects)
self.assertNotIn('recipients', e.serialize())


MMA_vector_key = jwk.JWK(**E_A2_key)
MMA_vector_ok_cek = \
Expand Down

0 comments on commit 1169eca

Please sign in to comment.