Skip to content

Commit d6a3d15

Browse files
committed
fix(jwe): when decrypt, use the original aad base64 value
#101
1 parent 3c9efed commit d6a3d15

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/joserfc/_rfc7516/json.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ def __extract_segments(
119119
"tag": urlsafe_b64decode(base64_segments["tag"]),
120120
}
121121
if "aad" in data:
122-
aad = urlsafe_b64decode(to_bytes(data["aad"]))
122+
base64_segments["aad"] = to_bytes(data["aad"])
123+
aad = urlsafe_b64decode(base64_segments["aad"])
124+
bytes_segments["aad"] = aad
123125
else:
124126
aad = None
125127
return base64_segments, bytes_segments, aad

src/joserfc/_rfc7516/message.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ def _perform_decrypt(obj: EncryptionData, registry: JWERegistry) -> None:
124124
if len(cek) * 8 != enc.cek_size: # pragma: no cover
125125
cek = secrets.token_bytes(enc.cek_size // 8)
126126

127-
aad = json_b64encode(obj.protected)
128-
if isinstance(obj, BaseJSONEncryption) and obj.aad:
129-
aad = aad + b"." + urlsafe_b64encode(obj.aad)
127+
if isinstance(obj, BaseJSONEncryption):
128+
aad = json_b64encode(obj.protected)
129+
if obj.aad:
130+
aad = aad + b"." + obj.base64_segments["aad"]
131+
else:
132+
aad = obj.base64_segments["aad"]
130133

131134
msg = enc.decrypt(ciphertext, tag, cek, iv, aad)
132135
if "zip" in obj.protected:

tests/jwe/test_compact.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,13 @@ def test_compact_encryption(self):
313313
self.assertEqual(obj.recipients, [])
314314
obj.attach_recipient(key, {"kid": "foo"})
315315
self.assertEqual(obj.protected["kid"], "foo")
316+
317+
def test_decrypt_from_other_libary(self):
318+
token = (
319+
"eyJhbGciOiAiZGlyIiwgImVuYyI6ICJBMjU2R0NNIiwgImtpZCI6ICJrLTEifQ."
320+
".UlOr6A5mRw3K1Wxx.8lI1604.2ubm__cUeWUB5Q7E8fpkyw"
321+
)
322+
key = OctKey.import_key(b"\x01" * 32)
323+
registry = JWERegistry(algorithms=["dir", "A256GCM"])
324+
obj = decrypt_compact(token, key, registry=registry)
325+
self.assertEqual(obj.plaintext, b"hello")

0 commit comments

Comments
 (0)