You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The COSE spec mandates that the TBS (ie. the Sig_structure) be canonically encoded, eg. all the lengths must be encoded as short as possible. For example, a 3-byte protected header { alg: ES256 }, within the TBS, will be encoded as follows:
[...]
43 # Byte string of length 3
A1 01 26 # Contents of the protected header
[...]
(The actual contents don't need to be canonically encoded, only the length does. I'm also omitting the rest of the TBS here)
However, there is no such requirement on the overall envelope. This means that when it comes to serializing this message, using a non-optimal encoding for the length is allowed, for example:
[...]
58 # Byte string, with a uint8 length
03 # Length of the byte string
A1 01 26 # Contents of the protected header
[...]
If a message with this non-optimal encoding is received, the verifier should make sure to re-encode the length to be as short as possible.
This is true of all the fields of the TBS, ie. both the payload and the protected header. While go-cose correctly processes the payload, by decoding it into a []byte and discarding the original encoding of the length, the entire encoding of the protected header is preserved, as a cbor.RawMessage. The encoding of the length of the header leaks into the TBS, which may violate the requirement that the TBS use canonical encoding.
The following is a small reproduction of the problem. The decanonicalize function will modify any encoded message to switch to a non-optimal encoding (assuming the length of the protected header would have been short enough to fit in the initial byte).
The call the Verify() at the end should succeed, but returns a "verification error".
This is an interesting finding that CBOR Encoding Restrictions only applies to the Sig_structure, the Enc_structure, and the MAC_structure. The COSE_Sign and COSE_Sign1 structures have no restrictions.
The COSE spec mandates that the TBS (ie. the Sig_structure) be canonically encoded, eg. all the lengths must be encoded as short as possible. For example, a 3-byte protected header
{ alg: ES256 }
, within the TBS, will be encoded as follows:(The actual contents don't need to be canonically encoded, only the length does. I'm also omitting the rest of the TBS here)
However, there is no such requirement on the overall envelope. This means that when it comes to serializing this message, using a non-optimal encoding for the length is allowed, for example:
If a message with this non-optimal encoding is received, the verifier should make sure to re-encode the length to be as short as possible.
This is true of all the fields of the TBS, ie. both the payload and the protected header. While go-cose correctly processes the payload, by decoding it into a
[]byte
and discarding the original encoding of the length, the entire encoding of the protected header is preserved, as acbor.RawMessage
. The encoding of the length of the header leaks into the TBS, which may violate the requirement that the TBS use canonical encoding.The following is a small reproduction of the problem. The
decanonicalize
function will modify any encoded message to switch to a non-optimal encoding (assuming the length of the protected header would have been short enough to fit in the initial byte).The call the
Verify()
at the end should succeed, but returns a "verification error".The text was updated successfully, but these errors were encountered: