Skip to content

Commit 714d328

Browse files
committed
fix: genlimiting o-length protected header for non AEAD
1 parent 4ed5e3e commit 714d328

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

cwt/cose.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .cbor_processor import CBORProcessor
77
from .const import (
88
COSE_ALGORITHMS_CEK,
9+
COSE_ALGORITHMS_CEK_NON_AEAD,
910
COSE_ALGORITHMS_CKDM,
1011
COSE_ALGORITHMS_CKDM_KEY_AGREEMENT,
1112
COSE_ALGORITHMS_CKDM_KEY_AGREEMENT_DIRECT,
@@ -551,6 +552,12 @@ def _encode_headers(
551552
p[1] = key.alg
552553
if self._kid_auto_inclusion and key.kid:
553554
u[4] = key.kid
555+
556+
# Check the protected header is empty if the algorithm is non AEAD (AES-CBC or AES-CTR)
557+
# because section 4 of RFC9459 says "The 'protected' header MUST be a zero-length byte string."
558+
alg = p[1] if 1 in p else u.get(1, 0)
559+
if alg in COSE_ALGORITHMS_CEK_NON_AEAD.values() and len(p) > 0:
560+
raise ValueError("protected header MUST be zero-length")
554561
return p, u
555562

556563
def _decode_headers(self, protected: Any, unprotected: Any) -> Tuple[Dict[int, Any], Dict[int, Any]]:

0 commit comments

Comments
 (0)