Closed robinbryce closed 1 week ago
Idealy, there would be a two step decode, so that the payload can be produced from contenent in the unprotected header and other content supplied out of band
There is a similar need for detached payloads in draft-ietf-dtn-bpsec-cose.
In my use of pycose
I manually manipulate the CBOR-decoded array before using from_cose_obj()
but it would be convenient to not need to go through those extra steps and possible failure modes.
Yep, I ended up doing this
def decode_sign1_detached(message: bytes, payload=None) -> Sign1Message:
# decode the cbor encoded cose sign1 message, per the CoseBase implementation
try:
cbor_msg = cbor2.loads(message)
cose_obj = cbor_msg.value
except AttributeError as e:
raise AttributeError("Message was not tagged.") from e
except ValueError as e:
raise ValueError("Decode accepts only bytes as input.") from e
if payload is None:
payload = b""
cose_obj[2] = (
payload # force replace with b'' if payload is detached, will not verify until replaced with correct content
)
return Sign1Message.from_cose_obj(cose_obj, True)
Of course that ignores the possibility that the provided message actually has a payload. but its fine if the caller knows this a-priori
decoding requires that the payload is attached (not None)
I believe the following is a valid (hex encoded) Sign1Message with a detached payload