2. 基本 COSE 结构
- 基本 COSE 结构
COSE 对象结构的设计目标是, 在解析和处理不同类型的安全消息时可以共享大量通用代码. 所有消息结构都建立在 CBOR array 类型之上. array 的前三个元素始终包含相同信息:
-
protected header parameters, 经编码并封装在 bstr 中.
-
unprotected header parameters, 表示为 map.
-
消息内容. 内容按情况可以是 plaintext 或 ciphertext. 内容可以是 detached 的(即与 COSE 结构分开传输), 但该位置仍然会被使用. 内容存在时封装在 bstr 中; detached 时为 nil 值.
此后的元素取决于具体消息类型.
COSE 消息使用 layer 概念来分离不同类型的密码学概念. 以 COSE_Encrypt 消息(Section 5.1)为例. 此消息类型被分为两层: content layer 和 recipient layer. content layer 包含已加密的 plaintext 以及关于加密消息的信息. recipient layer 针对每个 recipient, 包含已加密的 content encryption key (CEK) 以及它如何被加密的信息. 对于 CEK 已预共享的场景, 提供了单层版本的加密消息 COSE_Encrypt0 (Section 5.2).
识别所呈现消息类型的方法如下:
-
从上下文已知具体消息类型. 这可以由包含结构中的 marker 定义, 也可以由应用协议指定的限制定义.
-
通过 CBOR tag 识别消息类型. 在本规范中, 带有 CBOR tag 的消息称为 tagged messages, 不带 CBOR tag 的消息称为 untagged messages. 本文档为每种消息结构定义一个 CBOR tag. 这些 tag 见 Table 1.
-
当 COSE 对象承载在 "application/cose" media type 中时, 可选参数 "cose-type" 可用于识别嵌入对象. 如果使用结构的 tagged 版本, 该参数为 OPTIONAL. 如果使用结构的 untagged 版本, 该参数为 REQUIRED. 每种结构与该参数一起使用的值见 Table 1.
-
当 COSE 对象作为 CoAP payload 承载时, 可以使用 CoAP Content-Format Option 来识别消息内容. CoAP Content-Format 值见 Table 2. 由于每种安全消息都可被唯一识别, 因此不要求消息结构带有 CBOR tag.
+==========+===============+===============+=======================+ | CBOR Tag | cose-type | Data Item | 语义 | +==========+===============+===============+=======================+ | 98 | cose-sign | COSE_Sign | COSE 签名数据对象 | +----------+---------------+---------------+-----------------------+ | 18 | cose-sign1 | COSE_Sign1 | COSE 单签名者数据对象 | +----------+---------------+---------------+-----------------------+ | 96 | cose-encrypt | COSE_Encrypt | COSE 加密数据对象 | +----------+---------------+---------------+-----------------------+ | 16 | cose-encrypt0 | COSE_Encrypt0 | COSE 单接收者加密 | | | | | 数据对象 | +----------+---------------+---------------+-----------------------+ | 97 | cose-mac | COSE_Mac | COSE MAC 数据对象 | +----------+---------------+---------------+-----------------------+ | 17 | cose-mac0 | COSE_Mac0 | 无 Recipients 的 | | | | | COSE Mac 对象 | +----------+---------------+---------------+-----------------------+
Table 1: COSE 消息标识
+===========================+==========+=====+===========+
| Media Type | Encoding | ID | Reference |
+===========================+==========+=====+===========+
| application/cose; cose- | | 98 | RFC 9052 |
| type="cose-sign" | | | |
+---------------------------+----------+-----+-----------+
| application/cose; cose- | | 18 | RFC 9052 |
| type="cose-sign1" | | | |
+---------------------------+----------+-----+-----------+
| application/cose; cose- | | 96 | RFC 9052 |
| type="cose-encrypt" | | | |
+---------------------------+----------+-----+-----------+
| application/cose; cose- | | 16 | RFC 9052 |
| type="cose-encrypt0" | | | |
+---------------------------+----------+-----+-----------+
| application/cose; cose- | | 97 | RFC 9052 |
| type="cose-mac" | | | |
+---------------------------+----------+-----+-----------+
| application/cose; cose- | | 17 | RFC 9052 |
| type="cose-mac0" | | | |
+---------------------------+----------+-----+-----------+
| application/cose-key | | 101 | RFC 9052 |
+---------------------------+----------+-----+-----------+
| application/cose-key-set | | 102 | RFC 9052 |
+---------------------------+----------+-----+-----------+
Table 2: COSE 的 CoAP Content-Formats
以下 CDDL 片段标识本文档定义的所有顶层消息. tagged 和 untagged 版本的消息分别定义了独立的非终结符.
COSE_Messages = COSE_Untagged_Message / COSE_Tagged_Message
COSE_Untagged_Message = COSE_Sign / COSE_Sign1 / COSE_Encrypt / COSE_Encrypt0 / COSE_Mac / COSE_Mac0
COSE_Tagged_Message = COSE_Sign_Tagged / COSE_Sign1_Tagged / COSE_Encrypt_Tagged / COSE_Encrypt0_Tagged / COSE_Mac_Tagged / COSE_Mac0_Tagged