Skip to content

Commit 8aeb191

Browse files
committed
fix(jwe): remove InvalidCEKLengthError
1 parent f06540f commit 8aeb191

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

src/joserfc/_rfc7516/message.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import secrets
12
import typing as t
3+
24
from .models import (
35
CompactEncryption,
46
BaseJSONEncryption,
@@ -16,7 +18,6 @@
1618
from ..errors import (
1719
JoseError,
1820
DecodeError,
19-
InvalidCEKLengthError,
2021
InvalidEncryptedKeyError,
2122
InvalidExchangeKeyError,
2223
ConflictAlgorithmError,
@@ -121,7 +122,7 @@ def _perform_decrypt(obj: EncryptionData, registry: JWERegistry) -> None:
121122

122123
cek = cek_set.pop()
123124
if len(cek) * 8 != enc.cek_size: # pragma: no cover
124-
raise InvalidCEKLengthError(enc.cek_size)
125+
cek = secrets.token_bytes(enc.cek_size // 8)
125126

126127
aad = json_b64encode(obj.protected)
127128
if isinstance(obj, BaseJSONEncryption) and obj.aad:
@@ -187,7 +188,7 @@ def __pre_encrypt_direct_mode(alg: JWEAlgModel, enc: JWEEncModel, recipient: Rec
187188
# let the CEK be the agreed upon key.
188189
cek = alg.encrypt_agreed_upon_key(enc, recipient)
189190
if len(cek) * 8 != enc.cek_size: # pragma: no cover
190-
raise InvalidCEKLengthError(enc.cek_size)
191+
cek = secrets.token_bytes(enc.cek_size // 8)
191192
else:
192193
# 6. When Direct Encryption is employed, let the CEK be the shared
193194
# symmetric key.

src/joserfc/errors.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,6 @@ class InvalidEncryptedKeyError(JoseError):
183183
description = "JWE Encrypted Key value SHOULD be an empty octet sequence"
184184

185185

186-
class InvalidCEKLengthError(JoseError):
187-
error = "invalid_cek_length"
188-
description = "Invalid 'cek' length"
189-
190-
def __init__(self, cek_size: int): # pragma: no cover
191-
description = f"A key of size {cek_size} bits MUST be used"
192-
super(InvalidCEKLengthError, self).__init__(description=description)
193-
194-
195186
# --- JWT related errors --- #
196187

197188

0 commit comments

Comments
 (0)