···11-# JWT Implementation TODO
11+# JWT and CWT Implementation TODO
2233-RFC 7519 compliance tracking for ocaml-jwt.
33+RFC 7519 (JWT) and RFC 8392 (CWT) compliance tracking for jsonwt.
4455## Implementation Status
6677+### JSON Web Token (JWT) - RFC 7519
78- [x] Type definitions for all registered claims
89- [x] Type definitions for JOSE header parameters
910- [x] Type definitions for algorithms
···1617- [x] Structured error types
1718- [x] Comprehensive tests (30 tests passing)
18192020+### CBOR Web Token (CWT) - RFC 8392
2121+- [x] Type definitions for COSE algorithms (HMAC, ECDSA, EdDSA)
2222+- [x] Type definitions for COSE keys (Symmetric, Ec2, Okp)
2323+- [x] Claims with integer keys (1=iss, 2=sub, 3=aud, 4=exp, 5=nbf, 6=iat, 7=cti)
2424+- [x] CBOR encoding via cbort library
2525+- [x] COSE_Mac0 structure (MACed CWT)
2626+- [x] COSE_Sign1 structure (Signed CWT)
2727+- [x] Signature creation and verification (HMAC, ECDSA, EdDSA)
2828+- [x] Claims validation (same as JWT)
2929+- [x] Structured error types
3030+- [x] Comprehensive tests (28 tests passing)
3131+3232+### CBOR Codec - RFC 8949
3333+- [x] Low-level CBOR encoding primitives (Cbor_rw)
3434+- [x] Major type constants and encoding functions
3535+- [x] Integer encoding (positive and negative)
3636+- [x] Float encoding (IEEE 754 double precision)
3737+- [x] Text string and byte string encoding
3838+- [x] Array and map encoding
3939+- [x] Tag encoding
4040+- [x] Simple value encoding (false, true, null, undefined)
4141+- [x] Comprehensive tests with RFC 8949 Appendix A vectors (46 tests passing)
4242+1943---
20442145## Completed Phases
···5478- [x] `create` function for signing JWTs
5579- [x] Algorithm/key type validation
56805757-### Phase 5: Tests - DONE (30 tests passing)
8181+### Phase 5: Tests - DONE (104 tests passing)
8282+8383+#### JWT Tests (30 tests)
58845959-#### RFC Test Vectors
8585+**RFC Test Vectors:**
6086- [x] RFC 7519 Section 3.1 HS256 JWT
6187- [x] RFC 7519 Section 6.1 Unsecured JWT
62886363-#### Algorithm Coverage
8989+**Algorithm Coverage:**
6490- [x] HS256 sign/verify
6591- [x] HS384 sign/verify
6692- [x] HS512 sign/verify
···7399- [x] EdDSA sign/verify
74100- [x] none (unsecured) with opt-in
751017676-#### Validation Tests
102102+**Validation Tests:**
77103- [x] Expired token rejection
78104- [x] Not-yet-valid token rejection
79105- [x] Issuer mismatch rejection
80106- [x] Audience mismatch rejection
81107- [x] Leeway handling
821088383-#### Error Cases
109109+**Error Cases:**
84110- [x] Invalid base64url
85111- [x] Invalid JSON
86112- [x] Wrong number of parts
···88114- [x] Algorithm not in allowed list
89115- [x] Unsecured JWT without allow_none
90116117117+#### CWT Tests (28 tests)
118118+119119+**RFC Test Vectors:**
120120+- [x] RFC 8392 Appendix A claims timestamps
121121+- [x] RFC 8392 example values (hex test vectors)
122122+123123+**Algorithm Coverage:**
124124+- [x] HMAC_256_64 (alg=4)
125125+- [x] HMAC_256 (alg=5)
126126+- [x] HMAC_384 (alg=6)
127127+- [x] HMAC_512 (alg=7)
128128+- [x] ES256 (alg=-7)
129129+- [x] ES384 (alg=-35)
130130+- [x] ES512 (alg=-36)
131131+- [x] EdDSA (alg=-8)
132132+133133+**COSE Key Tests:**
134134+- [x] Symmetric key creation
135135+- [x] Ed25519 key creation
136136+- [x] P-256 key creation
137137+- [x] Key ID (kid) support
138138+139139+**Claims Tests:**
140140+- [x] Claims builder
141141+- [x] Timestamp claims (exp, nbf, iat)
142142+- [x] Single and multiple audience
143143+- [x] CWT ID (cti)
144144+- [x] CBOR serialization
145145+146146+**Validation Tests:**
147147+- [x] Expired token rejection
148148+- [x] Not-yet-valid token rejection
149149+- [x] Issuer validation
150150+- [x] Audience validation
151151+- [x] Leeway handling
152152+153153+#### CBOR Tests (46 tests)
154154+155155+**RFC 8949 Appendix A Test Vectors:**
156156+- [x] Unsigned integers (0-1000000000000)
157157+- [x] Negative integers (-1 to -1000)
158158+- [x] Booleans and null
159159+- [x] Floats (1.0, 1.1, -4.1, 1.0e+300, Infinity, NaN)
160160+- [x] Text strings (empty, ASCII, UTF-8 with Unicode)
161161+- [x] Byte strings
162162+- [x] Arrays (empty, nested, 25 items)
163163+- [x] Maps (empty, int keys, string keys, nested)
164164+- [x] Tags (epoch timestamp)
165165+- [x] Constants (major types, simple values, additional info)
166166+91167---
9216893169## Remaining Work
941709595-### RSA Signatures (RS256, RS384, RS512)
171171+### JWT: RSA Signatures (RS256, RS384, RS512)
9617297173**Status:** Stubbed - returns `Key_type_mismatch "RSA signing/verification not yet implemented"`
98174···1011772. Use `mirage-crypto-pk` for RSASSA-PKCS1-v1_5 signatures
1021783. Add tests with RFC test vectors
103179180180+### CWT: CBOR Decoding
181181+182182+**Status:** Not implemented - current implementation only encodes CWTs
183183+184184+**Required:**
185185+1. Add CBOR decoding functions to Cbor_rw module
186186+2. Implement `Cwt.parse` to decode CWT from CBOR bytes
187187+3. Add tests with RFC 8392 Appendix A encoded test vectors
188188+189189+### CWT: COSE Key Encoding/Decoding
190190+191191+**Status:** Keys are created in memory but not serialized
192192+193193+**Required:**
194194+1. Add `Cose_key.to_cbor` and `Cose_key.of_cbor` functions
195195+2. Follow RFC 9052 Section 7 (COSE Key) format
196196+3. Add tests with RFC test vectors
197197+198198+### CWT: CWT Tag Support
199199+200200+**Status:** Partial - encodes COSE structures but not outer CWT tag
201201+202202+**Required:**
203203+1. Add support for CWT tag (61) wrapping per RFC 8392 Section 2
204204+2. Add support for optional outer COSE tag per RFC 9052
205205+104206### Future Work (Not in Current Scope)
1052071062081. **JWK Set (JWKS)**: RFC 7517 Section 5 support for multiple keys
···136238137239| File | Lines | Description |
138240|------|-------|-------------|
139139-| `lib/jwt.ml` | ~1000 | Full implementation |
140140-| `lib/jwt.mli` | ~470 | Interface with RFC documentation |
141141-| `test/test_jwt.ml` | ~440 | 30 comprehensive tests |
241241+| `lib/jsonwt.ml` | ~1010 | JWT implementation |
242242+| `lib/jsonwt.mli` | ~480 | JWT interface with RFC documentation |
243243+| `lib/cwt.ml` | ~760 | CWT implementation |
244244+| `lib/cwt.mli` | ~400 | CWT interface with RFC documentation |
245245+| `cbort/cbor_rw.ml` | ~200 | Low-level CBOR encoding primitives |
246246+| `cbort/cbor_rw.mli` | ~200 | CBOR encoding interface |
247247+| `cbort/cbort.ml` | ~300 | CBOR codec for jsont types |
248248+| `cbort/cbort.mli` | ~100 | CBOR codec interface |
249249+| `test/test_jsonwt.ml` | ~440 | 30 JWT tests |
250250+| `test/test_cwt.ml` | ~500 | 28 CWT tests |
251251+| `test/test_cbor.ml` | ~320 | 46 CBOR encoding tests |
142252143253---
144254145255## References
146256257257+### JWT (JSON Web Token)
147258- [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519) - JSON Web Token (JWT)
148259- [RFC 7515](https://datatracker.ietf.org/doc/html/rfc7515) - JSON Web Signature (JWS)
149260- [RFC 7517](https://datatracker.ietf.org/doc/html/rfc7517) - JSON Web Key (JWK)
150261- [RFC 7518](https://datatracker.ietf.org/doc/html/rfc7518) - JSON Web Algorithms (JWA)
151262- [RFC 8037](https://datatracker.ietf.org/doc/html/rfc8037) - CFRG Elliptic Curve (EdDSA)
263263+264264+### CWT (CBOR Web Token)
265265+- [RFC 8392](https://datatracker.ietf.org/doc/html/rfc8392) - CBOR Web Token (CWT)
266266+- [RFC 9052](https://datatracker.ietf.org/doc/html/rfc9052) - CBOR Object Signing and Encryption (COSE) Structures
267267+- [RFC 9053](https://datatracker.ietf.org/doc/html/rfc9053) - CBOR Object Signing and Encryption (COSE) Algorithms
268268+- [RFC 8949](https://datatracker.ietf.org/doc/html/rfc8949) - Concise Binary Object Representation (CBOR)
+6-5
dune-project
···18181919(package
2020 (name jsonwt)
2121- (synopsis "JSON Web Token (JWT) implementation for OCaml")
2121+ (synopsis "JSON Web Token (JWT) and CBOR Web Token (CWT) for OCaml")
2222 (description
2323- "An implementation of RFC 7519 JSON Web Tokens (JWT) for OCaml.
2424- Supports JWT parsing, validation, and creation with HS256, RS256,
2525- ES256, and EdDSA signature algorithms. Also includes JWK (RFC 7517)
2626- support for key representation.")
2323+ "An implementation of RFC 7519 JSON Web Tokens (JWT) and RFC 8392
2424+ CBOR Web Tokens (CWT) for OCaml. Supports token parsing, validation,
2525+ and creation with HMAC, ECDSA (ES256/384/512), and EdDSA signature
2626+ algorithms. Includes JWK (RFC 7517) and COSE key support, plus
2727+ integrated CBOR codec for CWT serialization.")
2728 (depends
2829 (ocaml (>= 5.1))
2930 (jsont (>= 0.2.0))
+6-5
jsonwt.opam
···11# This file is generated by dune, edit dune-project instead
22opam-version: "2.0"
33-synopsis: "JSON Web Token (JWT) implementation for OCaml"
33+synopsis: "JSON Web Token (JWT) and CBOR Web Token (CWT) for OCaml"
44description: """
55-An implementation of RFC 7519 JSON Web Tokens (JWT) for OCaml.
66- Supports JWT parsing, validation, and creation with HS256, RS256,
77- ES256, and EdDSA signature algorithms. Also includes JWK (RFC 7517)
88- support for key representation."""
55+An implementation of RFC 7519 JSON Web Tokens (JWT) and RFC 8392
66+ CBOR Web Tokens (CWT) for OCaml. Supports token parsing, validation,
77+ and creation with HMAC, ECDSA (ES256/384/512), and EdDSA signature
88+ algorithms. Includes JWK (RFC 7517) and COSE key support, plus
99+ integrated CBOR codec for CWT serialization."""
910maintainer: ["Anil Madhavapeddy <anil@recoil.org>"]
1011authors: ["Anil Madhavapeddy"]
1112license: "ISC"
+794
lib/cwt.ml
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** CBOR Web Token (CWT) - RFC 8392 *)
77+88+(* Error handling *)
99+1010+type error =
1111+ | Invalid_cbor of string
1212+ | Invalid_cose of string
1313+ | Invalid_claims of string
1414+ | Unsupported_algorithm of string
1515+ | Algorithm_not_allowed of string
1616+ | Signature_mismatch
1717+ | Token_expired
1818+ | Token_not_yet_valid
1919+ | Invalid_issuer
2020+ | Invalid_audience
2121+ | Key_type_mismatch of string
2222+2323+let pp_error ppf = function
2424+ | Invalid_cbor s -> Format.fprintf ppf "Invalid CBOR: %s" s
2525+ | Invalid_cose s -> Format.fprintf ppf "Invalid COSE: %s" s
2626+ | Invalid_claims s -> Format.fprintf ppf "Invalid claims: %s" s
2727+ | Unsupported_algorithm s -> Format.fprintf ppf "Unsupported algorithm: %s" s
2828+ | Algorithm_not_allowed s -> Format.fprintf ppf "Algorithm not allowed: %s" s
2929+ | Signature_mismatch -> Format.fprintf ppf "Signature mismatch"
3030+ | Token_expired -> Format.fprintf ppf "Token expired"
3131+ | Token_not_yet_valid -> Format.fprintf ppf "Token not yet valid"
3232+ | Invalid_issuer -> Format.fprintf ppf "Invalid issuer"
3333+ | Invalid_audience -> Format.fprintf ppf "Invalid audience"
3434+ | Key_type_mismatch s -> Format.fprintf ppf "Key type mismatch: %s" s
3535+3636+let error_to_string e =
3737+ Format.asprintf "%a" pp_error e
3838+3939+(* COSE Algorithms - RFC 9053 *)
4040+4141+module Algorithm = struct
4242+ type t =
4343+ | ES256
4444+ | ES384
4545+ | ES512
4646+ | EdDSA
4747+ | HMAC_256_64
4848+ | HMAC_256
4949+ | HMAC_384
5050+ | HMAC_512
5151+5252+ let to_cose_int = function
5353+ | ES256 -> -7
5454+ | ES384 -> -35
5555+ | ES512 -> -36
5656+ | EdDSA -> -8
5757+ | HMAC_256_64 -> 4
5858+ | HMAC_256 -> 5
5959+ | HMAC_384 -> 6
6060+ | HMAC_512 -> 7
6161+6262+ let of_cose_int = function
6363+ | -7 -> Ok ES256
6464+ | -35 -> Ok ES384
6565+ | -36 -> Ok ES512
6666+ | -8 -> Ok EdDSA
6767+ | 4 -> Ok HMAC_256_64
6868+ | 5 -> Ok HMAC_256
6969+ | 6 -> Ok HMAC_384
7070+ | 7 -> Ok HMAC_512
7171+ | n -> Error (Unsupported_algorithm (Printf.sprintf "COSE algorithm %d" n))
7272+7373+ let to_string = function
7474+ | ES256 -> "ES256"
7575+ | ES384 -> "ES384"
7676+ | ES512 -> "ES512"
7777+ | EdDSA -> "EdDSA"
7878+ | HMAC_256_64 -> "HMAC 256/64"
7979+ | HMAC_256 -> "HMAC 256/256"
8080+ | HMAC_384 -> "HMAC 384/384"
8181+ | HMAC_512 -> "HMAC 512/512"
8282+8383+ let all = [ES256; ES384; ES512; EdDSA; HMAC_256_64; HMAC_256; HMAC_384; HMAC_512]
8484+end
8585+8686+(* COSE Key - RFC 9052 Section 7 *)
8787+8888+module Cose_key = struct
8989+ type kty =
9090+ | Okp
9191+ | Ec2
9292+ | Symmetric
9393+9494+ type crv =
9595+ | P256
9696+ | P384
9797+ | P521
9898+ | Ed25519
9999+100100+ (* COSE key labels *)
101101+ let label_kty = 1
102102+ let label_kid = 2
103103+ let label_alg = 3
104104+ let label_crv = -1
105105+ let label_x = -2
106106+ let label_y = -3
107107+ let label_d = -4
108108+ let label_k = -1 (* for symmetric *)
109109+110110+ (* COSE key type values *)
111111+ let kty_okp = 1
112112+ let kty_ec2 = 2
113113+ let kty_symmetric = 4
114114+115115+ (* COSE curve values *)
116116+ let crv_p256 = 1
117117+ let crv_p384 = 2
118118+ let crv_p521 = 3
119119+ let crv_ed25519 = 6
120120+121121+ type key_data =
122122+ | Symmetric_key of { k : string }
123123+ | Ed25519_pub of { x : string }
124124+ | Ed25519_priv of { x : string; d : string }
125125+ | P256_pub of { x : string; y : string }
126126+ | P256_priv of { x : string; y : string; d : string }
127127+ | P384_pub of { x : string; y : string }
128128+ | P384_priv of { x : string; y : string; d : string }
129129+ | P521_pub of { x : string; y : string }
130130+ | P521_priv of { x : string; y : string; d : string }
131131+132132+ type t = {
133133+ key_data : key_data;
134134+ kid : string option;
135135+ alg : Algorithm.t option;
136136+ }
137137+138138+ let symmetric k =
139139+ { key_data = Symmetric_key { k }; kid = None; alg = None }
140140+141141+ let ed25519_pub x =
142142+ { key_data = Ed25519_pub { x }; kid = None; alg = Some Algorithm.EdDSA }
143143+144144+ let ed25519_priv ~pub ~priv =
145145+ { key_data = Ed25519_priv { x = pub; d = priv }; kid = None; alg = Some Algorithm.EdDSA }
146146+147147+ let p256_pub ~x ~y =
148148+ { key_data = P256_pub { x; y }; kid = None; alg = Some Algorithm.ES256 }
149149+150150+ let p256_priv ~x ~y ~d =
151151+ { key_data = P256_priv { x; y; d }; kid = None; alg = Some Algorithm.ES256 }
152152+153153+ let p384_pub ~x ~y =
154154+ { key_data = P384_pub { x; y }; kid = None; alg = Some Algorithm.ES384 }
155155+156156+ let p384_priv ~x ~y ~d =
157157+ { key_data = P384_priv { x; y; d }; kid = None; alg = Some Algorithm.ES384 }
158158+159159+ let p521_pub ~x ~y =
160160+ { key_data = P521_pub { x; y }; kid = None; alg = Some Algorithm.ES512 }
161161+162162+ let p521_priv ~x ~y ~d =
163163+ { key_data = P521_priv { x; y; d }; kid = None; alg = Some Algorithm.ES512 }
164164+165165+ let kty t =
166166+ match t.key_data with
167167+ | Symmetric_key _ -> Symmetric
168168+ | Ed25519_pub _ | Ed25519_priv _ -> Okp
169169+ | P256_pub _ | P256_priv _ | P384_pub _ | P384_priv _ | P521_pub _ | P521_priv _ -> Ec2
170170+171171+ let kid t = t.kid
172172+ let alg t = t.alg
173173+174174+ let with_kid id t = { t with kid = Some id }
175175+ let with_alg a t = { t with alg = Some a }
176176+177177+ (* CBOR encoding/decoding for COSE keys *)
178178+ let of_cbor _bytes =
179179+ Error (Invalid_cose "COSE key parsing not yet implemented")
180180+181181+ let to_cbor t =
182182+ let open Cbort.Rw in
183183+ let buf = Buffer.create 128 in
184184+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
185185+ let e = make_encoder w in
186186+187187+ (* Count the number of map entries *)
188188+ let count = ref 1 in (* kty is always present *)
189189+ if Option.is_some t.kid then incr count;
190190+ if Option.is_some t.alg then incr count;
191191+ (match t.key_data with
192192+ | Symmetric_key _ -> incr count (* k *)
193193+ | Ed25519_pub _ -> count := !count + 2 (* crv, x *)
194194+ | Ed25519_priv _ -> count := !count + 3 (* crv, x, d *)
195195+ | P256_pub _ | P384_pub _ | P521_pub _ -> count := !count + 3 (* crv, x, y *)
196196+ | P256_priv _ | P384_priv _ | P521_priv _ -> count := !count + 4); (* crv, x, y, d *)
197197+198198+ write_map_start e !count;
199199+200200+ (* kty *)
201201+ write_int e label_kty;
202202+ (match t.key_data with
203203+ | Symmetric_key _ -> write_int e kty_symmetric
204204+ | Ed25519_pub _ | Ed25519_priv _ -> write_int e kty_okp
205205+ | _ -> write_int e kty_ec2);
206206+207207+ (* kid (optional) *)
208208+ Option.iter (fun kid ->
209209+ write_int e label_kid;
210210+ write_bytes_header e (String.length kid);
211211+ write_bytes e kid
212212+ ) t.kid;
213213+214214+ (* alg (optional) *)
215215+ Option.iter (fun alg ->
216216+ write_int e label_alg;
217217+ write_int e (Algorithm.to_cose_int alg)
218218+ ) t.alg;
219219+220220+ (* Key-type specific parameters *)
221221+ (match t.key_data with
222222+ | Symmetric_key { k } ->
223223+ write_int e label_k;
224224+ write_bytes_header e (String.length k);
225225+ write_bytes e k
226226+227227+ | Ed25519_pub { x } ->
228228+ write_int e label_crv;
229229+ write_int e crv_ed25519;
230230+ write_int e label_x;
231231+ write_bytes_header e (String.length x);
232232+ write_bytes e x
233233+234234+ | Ed25519_priv { x; d } ->
235235+ write_int e label_crv;
236236+ write_int e crv_ed25519;
237237+ write_int e label_x;
238238+ write_bytes_header e (String.length x);
239239+ write_bytes e x;
240240+ write_int e label_d;
241241+ write_bytes_header e (String.length d);
242242+ write_bytes e d
243243+244244+ | P256_pub { x; y } ->
245245+ write_int e label_crv;
246246+ write_int e crv_p256;
247247+ write_int e label_x;
248248+ write_bytes_header e (String.length x);
249249+ write_bytes e x;
250250+ write_int e label_y;
251251+ write_bytes_header e (String.length y);
252252+ write_bytes e y
253253+254254+ | P256_priv { x; y; d } ->
255255+ write_int e label_crv;
256256+ write_int e crv_p256;
257257+ write_int e label_x;
258258+ write_bytes_header e (String.length x);
259259+ write_bytes e x;
260260+ write_int e label_y;
261261+ write_bytes_header e (String.length y);
262262+ write_bytes e y;
263263+ write_int e label_d;
264264+ write_bytes_header e (String.length d);
265265+ write_bytes e d
266266+267267+ | P384_pub { x; y } ->
268268+ write_int e label_crv;
269269+ write_int e crv_p384;
270270+ write_int e label_x;
271271+ write_bytes_header e (String.length x);
272272+ write_bytes e x;
273273+ write_int e label_y;
274274+ write_bytes_header e (String.length y);
275275+ write_bytes e y
276276+277277+ | P384_priv { x; y; d } ->
278278+ write_int e label_crv;
279279+ write_int e crv_p384;
280280+ write_int e label_x;
281281+ write_bytes_header e (String.length x);
282282+ write_bytes e x;
283283+ write_int e label_y;
284284+ write_bytes_header e (String.length y);
285285+ write_bytes e y;
286286+ write_int e label_d;
287287+ write_bytes_header e (String.length d);
288288+ write_bytes e d
289289+290290+ | P521_pub { x; y } ->
291291+ write_int e label_crv;
292292+ write_int e crv_p521;
293293+ write_int e label_x;
294294+ write_bytes_header e (String.length x);
295295+ write_bytes e x;
296296+ write_int e label_y;
297297+ write_bytes_header e (String.length y);
298298+ write_bytes e y
299299+300300+ | P521_priv { x; y; d } ->
301301+ write_int e label_crv;
302302+ write_int e crv_p521;
303303+ write_int e label_x;
304304+ write_bytes_header e (String.length x);
305305+ write_bytes e x;
306306+ write_int e label_y;
307307+ write_bytes_header e (String.length y);
308308+ write_bytes e y;
309309+ write_int e label_d;
310310+ write_bytes_header e (String.length d);
311311+ write_bytes e d);
312312+313313+ flush_encoder e;
314314+ Buffer.contents buf
315315+316316+ (* Suppress unused warnings *)
317317+ let _ = (label_kty, label_kid, label_alg, label_crv, label_x, label_y, label_d, label_k)
318318+ let _ = (kty_okp, kty_ec2, kty_symmetric, crv_p256, crv_p384, crv_p521, crv_ed25519)
319319+end
320320+321321+(* CWT Claims - RFC 8392 Section 3 *)
322322+323323+module Claims = struct
324324+ (* Claim keys (integers per RFC 8392) *)
325325+ let key_iss = 1
326326+ let key_sub = 2
327327+ let key_aud = 3
328328+ let key_exp = 4
329329+ let key_nbf = 5
330330+ let key_iat = 6
331331+ let key_cti = 7
332332+333333+ type claim_key =
334334+ | Int_key of int
335335+ | String_key of string
336336+337337+ type t = {
338338+ iss : string option;
339339+ sub : string option;
340340+ aud : string list;
341341+ exp : Ptime.t option;
342342+ nbf : Ptime.t option;
343343+ iat : Ptime.t option;
344344+ cti : string option;
345345+ custom : (claim_key * string) list;
346346+ }
347347+348348+ let iss t = t.iss
349349+ let sub t = t.sub
350350+ let aud t = t.aud
351351+ let exp t = t.exp
352352+ let nbf t = t.nbf
353353+ let iat t = t.iat
354354+ let cti t = t.cti
355355+356356+ let get_int_key key t =
357357+ List.find_map (function
358358+ | (Int_key k, v) when k = key -> Some v
359359+ | _ -> None
360360+ ) t.custom
361361+362362+ let get_string_key key t =
363363+ List.find_map (function
364364+ | (String_key k, v) when k = key -> Some v
365365+ | _ -> None
366366+ ) t.custom
367367+368368+ type builder = t
369369+370370+ let empty = {
371371+ iss = None; sub = None; aud = []; exp = None;
372372+ nbf = None; iat = None; cti = None; custom = []
373373+ }
374374+375375+ let set_iss v t = { t with iss = Some v }
376376+ let set_sub v t = { t with sub = Some v }
377377+ let set_aud v t = { t with aud = v }
378378+ let set_exp v t = { t with exp = Some v }
379379+ let set_nbf v t = { t with nbf = Some v }
380380+ let set_iat v t = { t with iat = Some v }
381381+ let set_cti v t = { t with cti = Some v }
382382+ let set_int_key key value t = { t with custom = (Int_key key, value) :: t.custom }
383383+ let set_string_key key value t = { t with custom = (String_key key, value) :: t.custom }
384384+ let build t = t
385385+386386+ let of_cbor _bytes =
387387+ Error (Invalid_claims "Claims parsing not yet implemented")
388388+389389+ let to_cbor t =
390390+ let open Cbort.Rw in
391391+ let buf = Buffer.create 128 in
392392+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
393393+ let e = make_encoder w in
394394+395395+ (* Count the number of map entries *)
396396+ let count = ref 0 in
397397+ if Option.is_some t.iss then incr count;
398398+ if Option.is_some t.sub then incr count;
399399+ if t.aud <> [] then incr count;
400400+ if Option.is_some t.exp then incr count;
401401+ if Option.is_some t.nbf then incr count;
402402+ if Option.is_some t.iat then incr count;
403403+ if Option.is_some t.cti then incr count;
404404+ count := !count + List.length t.custom;
405405+406406+ write_map_start e !count;
407407+408408+ (* iss (1) *)
409409+ Option.iter (fun iss ->
410410+ write_int e key_iss;
411411+ write_text e iss
412412+ ) t.iss;
413413+414414+ (* sub (2) *)
415415+ Option.iter (fun sub ->
416416+ write_int e key_sub;
417417+ write_text e sub
418418+ ) t.sub;
419419+420420+ (* aud (3) *)
421421+ if t.aud <> [] then begin
422422+ write_int e key_aud;
423423+ if List.length t.aud = 1 then
424424+ write_text e (List.hd t.aud)
425425+ else begin
426426+ write_array_start e (List.length t.aud);
427427+ List.iter (write_text e) t.aud
428428+ end
429429+ end;
430430+431431+ (* exp (4) - NumericDate as integer seconds since epoch *)
432432+ Option.iter (fun exp ->
433433+ write_int e key_exp;
434434+ let secs = Ptime.to_float_s exp |> Float.to_int in
435435+ write_int e secs
436436+ ) t.exp;
437437+438438+ (* nbf (5) *)
439439+ Option.iter (fun nbf ->
440440+ write_int e key_nbf;
441441+ let secs = Ptime.to_float_s nbf |> Float.to_int in
442442+ write_int e secs
443443+ ) t.nbf;
444444+445445+ (* iat (6) *)
446446+ Option.iter (fun iat ->
447447+ write_int e key_iat;
448448+ let secs = Ptime.to_float_s iat |> Float.to_int in
449449+ write_int e secs
450450+ ) t.iat;
451451+452452+ (* cti (7) - byte string *)
453453+ Option.iter (fun cti ->
454454+ write_int e key_cti;
455455+ write_bytes_header e (String.length cti);
456456+ write_bytes e cti
457457+ ) t.cti;
458458+459459+ (* Custom claims *)
460460+ List.iter (fun (key, value) ->
461461+ (match key with
462462+ | Int_key k -> write_int e k
463463+ | String_key k -> write_text e k);
464464+ (* Value is already CBOR-encoded, write it raw *)
465465+ write_bytes e value
466466+ ) t.custom;
467467+468468+ flush_encoder e;
469469+ Buffer.contents buf
470470+471471+ (* Suppress unused warnings *)
472472+ let _ = (key_iss, key_sub, key_aud, key_exp, key_nbf, key_iat, key_cti)
473473+end
474474+475475+(* CWT Token *)
476476+477477+(* COSE tags *)
478478+let cose_sign1_tag = 18
479479+let cose_mac0_tag = 17
480480+481481+(* COSE header labels *)
482482+let header_alg = 1
483483+let header_kid = 4
484484+485485+type t = {
486486+ claims : Claims.t;
487487+ algorithm : Algorithm.t option;
488488+ kid : string option;
489489+ protected_header : string; (* CBOR-encoded protected header *)
490490+ signature : string; (* Signature or MAC tag *)
491491+ raw : string; (* Original CBOR bytes *)
492492+}
493493+494494+let claims t = t.claims
495495+let algorithm t = t.algorithm
496496+let kid t = t.kid
497497+let raw t = t.raw
498498+499499+let parse _bytes =
500500+ Error (Invalid_cbor "CWT parsing not yet implemented")
501501+502502+(* Cryptographic operations *)
503503+504504+let hmac_sign alg key payload =
505505+ let module Hash = Digestif in
506506+ match alg with
507507+ | Algorithm.HMAC_256_64 ->
508508+ let mac = Hash.SHA256.hmac_string ~key payload in
509509+ String.sub (Hash.SHA256.to_raw_string mac) 0 8
510510+ | Algorithm.HMAC_256 ->
511511+ let mac = Hash.SHA256.hmac_string ~key payload in
512512+ Hash.SHA256.to_raw_string mac
513513+ | Algorithm.HMAC_384 ->
514514+ let mac = Hash.SHA384.hmac_string ~key payload in
515515+ Hash.SHA384.to_raw_string mac
516516+ | Algorithm.HMAC_512 ->
517517+ let mac = Hash.SHA512.hmac_string ~key payload in
518518+ Hash.SHA512.to_raw_string mac
519519+ | _ -> failwith "Not an HMAC algorithm"
520520+521521+let hmac_verify alg key payload expected_mac =
522522+ let computed = hmac_sign alg key payload in
523523+ Eqaf.equal computed expected_mac
524524+525525+let p256_sign ~priv payload =
526526+ match Mirage_crypto_ec.P256.Dsa.priv_of_octets priv with
527527+ | Error _ -> Error (Key_type_mismatch "Invalid P-256 private key")
528528+ | Ok priv ->
529529+ let hash = Digestif.SHA256.(digest_string payload |> to_raw_string) in
530530+ let (r, s) = Mirage_crypto_ec.P256.Dsa.sign ~key:priv hash in
531531+ let pad32 s =
532532+ let len = String.length s in
533533+ if len >= 32 then String.sub s (len - 32) 32
534534+ else String.make (32 - len) '\x00' ^ s
535535+ in
536536+ Ok (pad32 r ^ pad32 s)
537537+538538+let p384_sign ~priv payload =
539539+ match Mirage_crypto_ec.P384.Dsa.priv_of_octets priv with
540540+ | Error _ -> Error (Key_type_mismatch "Invalid P-384 private key")
541541+ | Ok priv ->
542542+ let hash = Digestif.SHA384.(digest_string payload |> to_raw_string) in
543543+ let (r, s) = Mirage_crypto_ec.P384.Dsa.sign ~key:priv hash in
544544+ let pad48 s =
545545+ let len = String.length s in
546546+ if len >= 48 then String.sub s (len - 48) 48
547547+ else String.make (48 - len) '\x00' ^ s
548548+ in
549549+ Ok (pad48 r ^ pad48 s)
550550+551551+let p521_sign ~priv payload =
552552+ match Mirage_crypto_ec.P521.Dsa.priv_of_octets priv with
553553+ | Error _ -> Error (Key_type_mismatch "Invalid P-521 private key")
554554+ | Ok priv ->
555555+ let hash = Digestif.SHA512.(digest_string payload |> to_raw_string) in
556556+ let (r, s) = Mirage_crypto_ec.P521.Dsa.sign ~key:priv hash in
557557+ let pad66 s =
558558+ let len = String.length s in
559559+ if len >= 66 then String.sub s (len - 66) 66
560560+ else String.make (66 - len) '\x00' ^ s
561561+ in
562562+ Ok (pad66 r ^ pad66 s)
563563+564564+let ed25519_sign ~priv payload =
565565+ match Mirage_crypto_ec.Ed25519.priv_of_octets priv with
566566+ | Error _ -> Error (Key_type_mismatch "Invalid Ed25519 private key")
567567+ | Ok priv -> Ok (Mirage_crypto_ec.Ed25519.sign ~key:priv payload)
568568+569569+let verify ~key ?allowed_algs t =
570570+ (* Check algorithm is allowed *)
571571+ let alg = match t.algorithm with
572572+ | None -> Error (Invalid_cose "No algorithm in protected header")
573573+ | Some a -> Ok a
574574+ in
575575+ match alg with
576576+ | Error e -> Error e
577577+ | Ok alg ->
578578+ let allowed = match allowed_algs with
579579+ | None -> Algorithm.all
580580+ | Some l -> l
581581+ in
582582+ if not (List.mem alg allowed) then
583583+ Error (Algorithm_not_allowed (Algorithm.to_string alg))
584584+ else
585585+ (* Build Sig_structure or MAC_structure for verification *)
586586+ let sig_structure =
587587+ let open Cbort.Rw in
588588+ let buf = Buffer.create 128 in
589589+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
590590+ let e = make_encoder w in
591591+ write_array_start e 4;
592592+ write_text e (match alg with
593593+ | Algorithm.HMAC_256_64 | Algorithm.HMAC_256
594594+ | Algorithm.HMAC_384 | Algorithm.HMAC_512 -> "MAC0"
595595+ | _ -> "Signature1");
596596+ write_bytes_header e (String.length t.protected_header);
597597+ write_bytes e t.protected_header;
598598+ write_bytes_header e 0; (* external_aad = empty *)
599599+ let payload = Claims.to_cbor t.claims in
600600+ write_bytes_header e (String.length payload);
601601+ write_bytes e payload;
602602+ flush_encoder e;
603603+ Buffer.contents buf
604604+ in
605605+ (* Verify based on algorithm *)
606606+ let verified = match alg, key.Cose_key.key_data with
607607+ | (Algorithm.HMAC_256_64 | Algorithm.HMAC_256
608608+ | Algorithm.HMAC_384 | Algorithm.HMAC_512), Cose_key.Symmetric_key { k } ->
609609+ hmac_verify alg k sig_structure t.signature
610610+ | Algorithm.EdDSA, (Cose_key.Ed25519_pub { x } | Cose_key.Ed25519_priv { x; _ }) ->
611611+ (match Mirage_crypto_ec.Ed25519.pub_of_octets x with
612612+ | Ok pub ->
613613+ Mirage_crypto_ec.Ed25519.verify ~key:pub t.signature ~msg:sig_structure
614614+ | Error _ -> false)
615615+ | Algorithm.ES256, (Cose_key.P256_pub { x; y } | Cose_key.P256_priv { x; y; _ }) ->
616616+ (match Mirage_crypto_ec.P256.Dsa.pub_of_octets ("\x04" ^ x ^ y) with
617617+ | Ok pub ->
618618+ let hash = Digestif.SHA256.(digest_string sig_structure |> to_raw_string) in
619619+ let r = String.sub t.signature 0 32 in
620620+ let s = String.sub t.signature 32 32 in
621621+ Mirage_crypto_ec.P256.Dsa.verify ~key:pub (r, s) hash
622622+ | Error _ -> false)
623623+ | _ -> false
624624+ in
625625+ if verified then Ok ()
626626+ else Error Signature_mismatch
627627+628628+let validate ~now ?iss ?aud ?leeway t =
629629+ let leeway = Option.value leeway ~default:Ptime.Span.zero in
630630+ (* Check exp *)
631631+ let check_exp () =
632632+ match t.claims.exp with
633633+ | Some exp ->
634634+ (match Ptime.add_span exp leeway with
635635+ | Some exp' when Ptime.is_later now ~than:exp' -> Error Token_expired
636636+ | _ -> Ok ())
637637+ | None -> Ok ()
638638+ in
639639+ (* Check nbf *)
640640+ let check_nbf () =
641641+ match t.claims.nbf with
642642+ | Some nbf ->
643643+ (match Ptime.sub_span nbf leeway with
644644+ | Some nbf' when Ptime.is_earlier now ~than:nbf' -> Error Token_not_yet_valid
645645+ | _ -> Ok ())
646646+ | None -> Ok ()
647647+ in
648648+ (* Check iss *)
649649+ let check_iss () =
650650+ match iss with
651651+ | Some expected_iss ->
652652+ (match t.claims.iss with
653653+ | Some actual_iss when actual_iss = expected_iss -> Ok ()
654654+ | _ -> Error Invalid_issuer)
655655+ | None -> Ok ()
656656+ in
657657+ (* Check aud *)
658658+ let check_aud () =
659659+ match aud with
660660+ | Some expected_aud ->
661661+ if List.mem expected_aud t.claims.aud then Ok ()
662662+ else Error Invalid_audience
663663+ | None -> Ok ()
664664+ in
665665+ match check_exp () with
666666+ | Error _ as e -> e
667667+ | Ok () ->
668668+ match check_nbf () with
669669+ | Error _ as e -> e
670670+ | Ok () ->
671671+ match check_iss () with
672672+ | Error _ as e -> e
673673+ | Ok () -> check_aud ()
674674+675675+let verify_and_validate ~key ~now ?allowed_algs ?iss ?aud ?leeway t =
676676+ match verify ~key ?allowed_algs t with
677677+ | Error _ as e -> e
678678+ | Ok () -> validate ~now ?iss ?aud ?leeway t
679679+680680+let create ~algorithm ~claims ~key =
681681+ (* Encode protected header *)
682682+ let protected_header =
683683+ let open Cbort.Rw in
684684+ let buf = Buffer.create 32 in
685685+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
686686+ let e = make_encoder w in
687687+ write_map_start e 1;
688688+ write_int e header_alg;
689689+ write_int e (Algorithm.to_cose_int algorithm);
690690+ flush_encoder e;
691691+ Buffer.contents buf
692692+ in
693693+694694+ (* Build Sig_structure or MAC_structure *)
695695+ let structure_name = match algorithm with
696696+ | Algorithm.HMAC_256_64 | Algorithm.HMAC_256
697697+ | Algorithm.HMAC_384 | Algorithm.HMAC_512 -> "MAC0"
698698+ | _ -> "Signature1"
699699+ in
700700+ let payload = Claims.to_cbor claims in
701701+ let to_be_signed =
702702+ let open Cbort.Rw in
703703+ let buf = Buffer.create 128 in
704704+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
705705+ let e = make_encoder w in
706706+ write_array_start e 4;
707707+ write_text e structure_name;
708708+ write_bytes_header e (String.length protected_header);
709709+ write_bytes e protected_header;
710710+ write_bytes_header e 0; (* external_aad = empty *)
711711+ write_bytes_header e (String.length payload);
712712+ write_bytes e payload;
713713+ flush_encoder e;
714714+ Buffer.contents buf
715715+ in
716716+717717+ (* Sign or MAC *)
718718+ let signature_result = match algorithm, key.Cose_key.key_data with
719719+ | (Algorithm.HMAC_256_64 | Algorithm.HMAC_256
720720+ | Algorithm.HMAC_384 | Algorithm.HMAC_512), Cose_key.Symmetric_key { k } ->
721721+ Ok (hmac_sign algorithm k to_be_signed)
722722+ | Algorithm.EdDSA, Cose_key.Ed25519_priv { d; _ } ->
723723+ ed25519_sign ~priv:d to_be_signed
724724+ | Algorithm.ES256, Cose_key.P256_priv { d; _ } ->
725725+ p256_sign ~priv:d to_be_signed
726726+ | Algorithm.ES384, Cose_key.P384_priv { d; _ } ->
727727+ p384_sign ~priv:d to_be_signed
728728+ | Algorithm.ES512, Cose_key.P521_priv { d; _ } ->
729729+ p521_sign ~priv:d to_be_signed
730730+ | _ ->
731731+ Error (Key_type_mismatch "Key type doesn't match algorithm")
732732+ in
733733+734734+ match signature_result with
735735+ | Error e -> Error e
736736+ | Ok signature ->
737737+ (* Encode COSE_Sign1 or COSE_Mac0 structure *)
738738+ let raw =
739739+ let open Cbort.Rw in
740740+ let buf = Buffer.create 256 in
741741+ let w = Bytesrw.Bytes.Writer.of_buffer buf in
742742+ let e = make_encoder w in
743743+ (* Tag *)
744744+ let tag = match algorithm with
745745+ | Algorithm.HMAC_256_64 | Algorithm.HMAC_256
746746+ | Algorithm.HMAC_384 | Algorithm.HMAC_512 -> cose_mac0_tag
747747+ | _ -> cose_sign1_tag
748748+ in
749749+ write_type_arg e major_tag tag;
750750+ write_array_start e 4;
751751+ (* protected header as bstr *)
752752+ write_bytes_header e (String.length protected_header);
753753+ write_bytes e protected_header;
754754+ (* unprotected header (empty map) *)
755755+ write_map_start e 0;
756756+ (* payload *)
757757+ write_bytes_header e (String.length payload);
758758+ write_bytes e payload;
759759+ (* signature/tag *)
760760+ write_bytes_header e (String.length signature);
761761+ write_bytes e signature;
762762+ flush_encoder e;
763763+ Buffer.contents buf
764764+ in
765765+ Ok {
766766+ claims;
767767+ algorithm = Some algorithm;
768768+ kid = key.Cose_key.kid;
769769+ protected_header;
770770+ signature;
771771+ raw;
772772+ }
773773+774774+let encode t = t.raw
775775+776776+let is_expired ~now ?leeway t =
777777+ match t.claims.exp with
778778+ | None -> false
779779+ | Some exp ->
780780+ let leeway = Option.value leeway ~default:Ptime.Span.zero in
781781+ match Ptime.add_span exp leeway with
782782+ | Some exp' -> Ptime.is_later now ~than:exp'
783783+ | None -> true
784784+785785+let time_to_expiry ~now t =
786786+ match t.claims.exp with
787787+ | None -> None
788788+ | Some exp ->
789789+ let diff = Ptime.diff exp now in
790790+ if Ptime.Span.compare diff Ptime.Span.zero <= 0 then None
791791+ else Some diff
792792+793793+(* Suppress unused warnings *)
794794+let _ = (cose_sign1_tag, cose_mac0_tag, header_alg, header_kid)
+417
lib/cwt.mli
···11+(*---------------------------------------------------------------------------
22+ Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved.
33+ SPDX-License-Identifier: ISC
44+ ---------------------------------------------------------------------------*)
55+66+(** CBOR Web Token (CWT) - RFC 8392
77+88+ This module implements CBOR Web Tokens as specified in
99+ {{:https://datatracker.ietf.org/doc/html/rfc8392}RFC 8392}.
1010+1111+ CWTs are the CBOR-based equivalent of JWTs, designed for constrained
1212+ environments where compact binary representation is important. CWTs use
1313+ COSE ({{:https://datatracker.ietf.org/doc/html/rfc9052}RFC 9052}) for
1414+ cryptographic protection.
1515+1616+ {2 Quick Start}
1717+1818+ {[
1919+ (* Create claims *)
2020+ let claims = Cwt.Claims.(empty
2121+ |> set_iss "https://example.com"
2222+ |> set_sub "user123"
2323+ |> set_exp (Ptime.add_span (Ptime_clock.now ()) (Ptime.Span.of_int_s 3600) |> Option.get)
2424+ |> build)
2525+2626+ (* Create a symmetric key *)
2727+ let key = Cwt.Cose_key.symmetric (Bytes.of_string "my-secret-key-32-bytes-long!!!!!")
2828+2929+ (* Create and encode the CWT *)
3030+ let cwt = Cwt.create ~algorithm:Cwt.Algorithm.HMAC_256 ~claims ~key |> Result.get_ok
3131+ let encoded = Cwt.encode cwt
3232+3333+ (* Parse and verify *)
3434+ let parsed = Cwt.parse encoded |> Result.get_ok
3535+ let () = Cwt.verify ~key parsed |> Result.get_ok
3636+ ]}
3737+3838+ {2 References}
3939+ {ul
4040+ {- {{:https://datatracker.ietf.org/doc/html/rfc8392}RFC 8392} - CBOR Web Token (CWT)}
4141+ {- {{:https://datatracker.ietf.org/doc/html/rfc9052}RFC 9052} - CBOR Object Signing and Encryption (COSE) Structures}
4242+ {- {{:https://datatracker.ietf.org/doc/html/rfc9053}RFC 9053} - CBOR Object Signing and Encryption (COSE) Algorithms}
4343+ {- {{:https://datatracker.ietf.org/doc/html/rfc8949}RFC 8949} - Concise Binary Object Representation (CBOR)}} *)
4444+4545+(** {1 Error Handling} *)
4646+4747+type error =
4848+ | Invalid_cbor of string
4949+ (** CBOR parsing failed *)
5050+ | Invalid_cose of string
5151+ (** COSE structure validation failed *)
5252+ | Invalid_claims of string
5353+ (** Claims validation failed *)
5454+ | Unsupported_algorithm of string
5555+ (** Unknown COSE algorithm identifier *)
5656+ | Algorithm_not_allowed of string
5757+ (** Algorithm rejected by allowed_algs policy *)
5858+ | Signature_mismatch
5959+ (** Signature/MAC verification failed *)
6060+ | Token_expired
6161+ (** exp claim validation failed per
6262+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.4}RFC 8392 Section 3.1.4} *)
6363+ | Token_not_yet_valid
6464+ (** nbf claim validation failed per
6565+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.5}RFC 8392 Section 3.1.5} *)
6666+ | Invalid_issuer
6767+ (** iss claim mismatch per
6868+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.1}RFC 8392 Section 3.1.1} *)
6969+ | Invalid_audience
7070+ (** aud claim mismatch per
7171+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.3}RFC 8392 Section 3.1.3} *)
7272+ | Key_type_mismatch of string
7373+ (** Key doesn't match algorithm *)
7474+7575+val pp_error : Format.formatter -> error -> unit
7676+(** Pretty-print an error. *)
7777+7878+val error_to_string : error -> string
7979+(** Convert error to human-readable string. *)
8080+8181+(** {1 COSE Algorithms}
8282+8383+ Cryptographic algorithms for COSE as specified in
8484+ {{:https://datatracker.ietf.org/doc/html/rfc9053}RFC 9053}.
8585+8686+ Each algorithm has a registered integer identifier in the IANA
8787+ COSE Algorithms registry. *)
8888+8989+module Algorithm : sig
9090+ type t =
9191+ | ES256 (** ECDSA w/ SHA-256, COSE alg = -7 *)
9292+ | ES384 (** ECDSA w/ SHA-384, COSE alg = -35 *)
9393+ | ES512 (** ECDSA w/ SHA-512, COSE alg = -36 *)
9494+ | EdDSA (** EdDSA (Ed25519), COSE alg = -8 *)
9595+ | HMAC_256_64 (** HMAC w/ SHA-256 truncated to 64 bits, COSE alg = 4 *)
9696+ | HMAC_256 (** HMAC w/ SHA-256 (256 bits), COSE alg = 5 *)
9797+ | HMAC_384 (** HMAC w/ SHA-384, COSE alg = 6 *)
9898+ | HMAC_512 (** HMAC w/ SHA-512, COSE alg = 7 *)
9999+100100+ val to_cose_int : t -> int
101101+ (** Convert to COSE algorithm identifier (negative for signatures). *)
102102+103103+ val of_cose_int : int -> (t, error) result
104104+ (** Parse from COSE algorithm identifier. *)
105105+106106+ val to_string : t -> string
107107+ (** Human-readable name for the algorithm. *)
108108+109109+ val all : t list
110110+ (** All supported algorithms. *)
111111+end
112112+113113+(** {1 COSE Key}
114114+115115+ Key representation for COSE operations.
116116+ See {{:https://datatracker.ietf.org/doc/html/rfc9052#section-7}RFC 9052 Section 7}
117117+ and {{:https://datatracker.ietf.org/doc/html/rfc9053}RFC 9053}. *)
118118+119119+module Cose_key : sig
120120+121121+ (** Key type per COSE Key Type registry.
122122+ See {{:https://www.iana.org/assignments/cose/cose.xhtml#key-type}IANA COSE Key Types}. *)
123123+ type kty =
124124+ | Okp (** Octet Key Pair (kty = 1), used for EdDSA *)
125125+ | Ec2 (** Elliptic Curve with x,y coordinates (kty = 2) *)
126126+ | Symmetric (** Symmetric key (kty = 4) *)
127127+128128+ (** Elliptic curve identifiers per COSE Elliptic Curves registry. *)
129129+ type crv =
130130+ | P256 (** NIST P-256, crv = 1 *)
131131+ | P384 (** NIST P-384, crv = 2 *)
132132+ | P521 (** NIST P-521, crv = 3 *)
133133+ | Ed25519 (** Ed25519 for EdDSA, crv = 6 *)
134134+135135+ (** A COSE key. *)
136136+ type t
137137+138138+ (** {2 Constructors} *)
139139+140140+ val symmetric : string -> t
141141+ (** [symmetric k] creates a symmetric COSE key from raw bytes.
142142+ Used for HMAC algorithms. The key should be at least as long
143143+ as the hash output (32 bytes for HMAC_256, etc.). *)
144144+145145+ val ed25519_pub : string -> t
146146+ (** [ed25519_pub pub] creates an Ed25519 public key from the 32-byte
147147+ public key value. *)
148148+149149+ val ed25519_priv : pub:string -> priv:string -> t
150150+ (** [ed25519_priv ~pub ~priv] creates an Ed25519 private key.
151151+ [pub] is the 32-byte public key, [priv] is the 32-byte seed. *)
152152+153153+ val p256_pub : x:string -> y:string -> t
154154+ (** [p256_pub ~x ~y] creates a P-256 public key from the x and y
155155+ coordinates (each 32 bytes). *)
156156+157157+ val p256_priv : x:string -> y:string -> d:string -> t
158158+ (** [p256_priv ~x ~y ~d] creates a P-256 private key.
159159+ [d] is the 32-byte private key value. *)
160160+161161+ val p384_pub : x:string -> y:string -> t
162162+ (** [p384_pub ~x ~y] creates a P-384 public key (coordinates are 48 bytes each). *)
163163+164164+ val p384_priv : x:string -> y:string -> d:string -> t
165165+ (** [p384_priv ~x ~y ~d] creates a P-384 private key. *)
166166+167167+ val p521_pub : x:string -> y:string -> t
168168+ (** [p521_pub ~x ~y] creates a P-521 public key (coordinates are 66 bytes each). *)
169169+170170+ val p521_priv : x:string -> y:string -> d:string -> t
171171+ (** [p521_priv ~x ~y ~d] creates a P-521 private key. *)
172172+173173+ (** {2 Accessors} *)
174174+175175+ val kty : t -> kty
176176+ (** Get the key type. *)
177177+178178+ val kid : t -> string option
179179+ (** Get the key ID if set (COSE label 2). *)
180180+181181+ val alg : t -> Algorithm.t option
182182+ (** Get the intended algorithm if set (COSE label 3). *)
183183+184184+ val with_kid : string -> t -> t
185185+ (** [with_kid id key] returns key with kid set to [id]. *)
186186+187187+ val with_alg : Algorithm.t -> t -> t
188188+ (** [with_alg alg key] returns key with alg set to [alg]. *)
189189+190190+ (** {2 Serialization} *)
191191+192192+ val of_cbor : string -> (t, error) result
193193+ (** Parse a COSE key from CBOR bytes. *)
194194+195195+ val to_cbor : t -> string
196196+ (** Serialize a COSE key to CBOR bytes. *)
197197+end
198198+199199+(** {1 CWT Claims}
200200+201201+ CWT Claims Set using CBOR integer keys for compactness.
202202+ See {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3}RFC 8392 Section 3}.
203203+204204+ {2 Claim Key Mapping}
205205+206206+ | Claim | Integer Key | Type |
207207+ |-------|-------------|------|
208208+ | iss | 1 | text string |
209209+ | sub | 2 | text string |
210210+ | aud | 3 | text string |
211211+ | exp | 4 | integer (NumericDate) |
212212+ | nbf | 5 | integer (NumericDate) |
213213+ | iat | 6 | integer (NumericDate) |
214214+ | cti | 7 | byte string | *)
215215+216216+module Claims : sig
217217+ type t
218218+219219+ (** {2 Registered Claim Names}
220220+221221+ See {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1}RFC 8392 Section 3.1}. *)
222222+223223+ val iss : t -> string option
224224+ (** Issuer claim (key 1) per
225225+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.1}Section 3.1.1}. *)
226226+227227+ val sub : t -> string option
228228+ (** Subject claim (key 2) per
229229+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.2}Section 3.1.2}. *)
230230+231231+ val aud : t -> string list
232232+ (** Audience claim (key 3) per
233233+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.3}Section 3.1.3}.
234234+ Returns empty list if not present. May be single string or array in CWT. *)
235235+236236+ val exp : t -> Ptime.t option
237237+ (** Expiration time claim (key 4) per
238238+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.4}Section 3.1.4}. *)
239239+240240+ val nbf : t -> Ptime.t option
241241+ (** Not Before claim (key 5) per
242242+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.5}Section 3.1.5}. *)
243243+244244+ val iat : t -> Ptime.t option
245245+ (** Issued At claim (key 6) per
246246+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.6}Section 3.1.6}. *)
247247+248248+ val cti : t -> string option
249249+ (** CWT ID claim (key 7) per
250250+ {{:https://datatracker.ietf.org/doc/html/rfc8392#section-3.1.7}Section 3.1.7}.
251251+ Note: Unlike JWT's jti which is a string, CWT's cti is a byte string. *)
252252+253253+ (** {2 Custom Claims}
254254+255255+ CWT supports both integer and text string keys for custom claims. *)
256256+257257+ val get_int_key : int -> t -> string option
258258+ (** [get_int_key key claims] returns the raw CBOR value of custom claim
259259+ with integer key [key]. *)
260260+261261+ val get_string_key : string -> t -> string option
262262+ (** [get_string_key key claims] returns the raw CBOR value of custom claim
263263+ with string key [key]. *)
264264+265265+ (** {2 Construction} *)
266266+267267+ type builder
268268+ (** Builder for constructing claims. *)
269269+270270+ val empty : builder
271271+ (** Empty claims builder. *)
272272+273273+ val set_iss : string -> builder -> builder
274274+ (** Set issuer claim. *)
275275+276276+ val set_sub : string -> builder -> builder
277277+ (** Set subject claim. *)
278278+279279+ val set_aud : string list -> builder -> builder
280280+ (** Set audience claim. *)
281281+282282+ val set_exp : Ptime.t -> builder -> builder
283283+ (** Set expiration time claim. *)
284284+285285+ val set_nbf : Ptime.t -> builder -> builder
286286+ (** Set not-before claim. *)
287287+288288+ val set_iat : Ptime.t -> builder -> builder
289289+ (** Set issued-at claim. *)
290290+291291+ val set_cti : string -> builder -> builder
292292+ (** Set CWT ID claim (raw bytes). *)
293293+294294+ val set_int_key : int -> string -> builder -> builder
295295+ (** [set_int_key key value builder] sets a custom claim with integer key.
296296+ [value] should be CBOR-encoded. *)
297297+298298+ val set_string_key : string -> string -> builder -> builder
299299+ (** [set_string_key key value builder] sets a custom claim with string key.
300300+ [value] should be CBOR-encoded. *)
301301+302302+ val build : builder -> t
303303+ (** Build the claims set. *)
304304+305305+ (** {2 Serialization} *)
306306+307307+ val of_cbor : string -> (t, error) result
308308+ (** Parse claims from CBOR-encoded CWT Claims Set. *)
309309+310310+ val to_cbor : t -> string
311311+ (** Serialize claims to CBOR-encoded CWT Claims Set. *)
312312+end
313313+314314+(** {1 CWT Token} *)
315315+316316+type t
317317+(** A parsed CWT token (COSE_Sign1, COSE_Mac0, or COSE_Encrypt0 structure). *)
318318+319319+(** {2 Parsing}
320320+321321+ Parse CWT from CBOR bytes. The CWT may be tagged (with COSE tag)
322322+ or untagged per {{:https://datatracker.ietf.org/doc/html/rfc8392#section-2}RFC 8392 Section 2}. *)
323323+324324+val parse : string -> (t, error) result
325325+(** [parse cwt_bytes] parses a CWT from CBOR bytes.
326326+327327+ This parses the COSE structure and extracts the claims, but does NOT
328328+ verify the signature/MAC. Use {!verify} to validate cryptographic
329329+ protection after parsing. *)
330330+331331+(** {2 Accessors} *)
332332+333333+val claims : t -> Claims.t
334334+(** [claims t] returns the claims set. *)
335335+336336+val algorithm : t -> Algorithm.t option
337337+(** [algorithm t] returns the COSE algorithm from the protected header. *)
338338+339339+val kid : t -> string option
340340+(** [kid t] returns the key ID from headers if present. *)
341341+342342+val raw : t -> string
343343+(** [raw t] returns the original CBOR bytes. *)
344344+345345+(** {2 Verification}
346346+347347+ Verify cryptographic protection and validate claims. *)
348348+349349+val verify :
350350+ key:Cose_key.t ->
351351+ ?allowed_algs:Algorithm.t list ->
352352+ t ->
353353+ (unit, error) result
354354+(** [verify ~key ?allowed_algs t] verifies the COSE signature or MAC.
355355+356356+ @param key The key to verify with (must match algorithm)
357357+ @param allowed_algs List of acceptable algorithms. Default: all. *)
358358+359359+val validate :
360360+ now:Ptime.t ->
361361+ ?iss:string ->
362362+ ?aud:string ->
363363+ ?leeway:Ptime.Span.t ->
364364+ t ->
365365+ (unit, error) result
366366+(** [validate ~now ?iss ?aud ?leeway t] validates CWT claims.
367367+368368+ @param now Current time (required, no implicit clock)
369369+ @param iss Expected issuer (if provided, must match exactly)
370370+ @param aud Expected audience (if provided, must be in audience list)
371371+ @param leeway Clock skew tolerance for exp/nbf checks (default 0s) *)
372372+373373+val verify_and_validate :
374374+ key:Cose_key.t ->
375375+ now:Ptime.t ->
376376+ ?allowed_algs:Algorithm.t list ->
377377+ ?iss:string ->
378378+ ?aud:string ->
379379+ ?leeway:Ptime.Span.t ->
380380+ t ->
381381+ (unit, error) result
382382+(** [verify_and_validate ~key ~now ...] verifies signature and validates claims. *)
383383+384384+(** {2 Creation}
385385+386386+ Create and sign CWTs. *)
387387+388388+val create :
389389+ algorithm:Algorithm.t ->
390390+ claims:Claims.t ->
391391+ key:Cose_key.t ->
392392+ (t, error) result
393393+(** [create ~algorithm ~claims ~key] creates and signs a new CWT.
394394+395395+ Creates a COSE_Sign1 structure for signature algorithms (ES256, ES384, ES512, EdDSA)
396396+ or COSE_Mac0 for MAC algorithms (HMAC_256, HMAC_384, HMAC_512).
397397+398398+ The [key] must be appropriate for the algorithm:
399399+ - HMAC algorithms: symmetric key
400400+ - ES256: P-256 private key
401401+ - ES384: P-384 private key
402402+ - ES512: P-521 private key
403403+ - EdDSA: Ed25519 private key *)
404404+405405+val encode : t -> string
406406+(** [encode t] returns the CBOR serialization of the CWT.
407407+ The result is a tagged COSE structure (COSE_Sign1 or COSE_Mac0). *)
408408+409409+(** {1 Utilities} *)
410410+411411+val is_expired : now:Ptime.t -> ?leeway:Ptime.Span.t -> t -> bool
412412+(** [is_expired ~now ?leeway t] checks if the token has expired.
413413+ Returns false if no exp claim present. *)
414414+415415+val time_to_expiry : now:Ptime.t -> t -> Ptime.Span.t option
416416+(** [time_to_expiry ~now t] returns time until expiration, or [None] if
417417+ no expiration claim or already expired. *)
···10041004 let diff = Ptime.diff exp_time now in
10051005 if Ptime.Span.compare diff Ptime.Span.zero <= 0 then None
10061006 else Some diff
10071007+10081008+(** CBOR Web Token (CWT) support *)
10091009+module Cwt = Cwt
+6
lib/jsonwt.mli
···470470471471val base64url_decode : string -> (string, error) result
472472(** Base64url decode, handling missing padding. *)
473473+474474+(** {1 CBOR Web Token (CWT)}
475475+476476+ See {{:https://datatracker.ietf.org/doc/html/rfc8392}RFC 8392}. *)
477477+478478+module Cwt = Cwt
+1403
spec/rfc8392.txt
···11+22+33+44+55+66+77+Internet Engineering Task Force (IETF) M. Jones
88+Request for Comments: 8392 Microsoft
99+Category: Standards Track E. Wahlstroem
1010+ISSN: 2070-1721
1111+ S. Erdtman
1212+ Spotify AB
1313+ H. Tschofenig
1414+ ARM Ltd.
1515+ May 2018
1616+1717+1818+ CBOR Web Token (CWT)
1919+2020+Abstract
2121+2222+ CBOR Web Token (CWT) is a compact means of representing claims to be
2323+ transferred between two parties. The claims in a CWT are encoded in
2424+ the Concise Binary Object Representation (CBOR), and CBOR Object
2525+ Signing and Encryption (COSE) is used for added application-layer
2626+ security protection. A claim is a piece of information asserted
2727+ about a subject and is represented as a name/value pair consisting of
2828+ a claim name and a claim value. CWT is derived from JSON Web Token
2929+ (JWT) but uses CBOR rather than JSON.
3030+3131+Status of This Memo
3232+3333+ This is an Internet Standards Track document.
3434+3535+ This document is a product of the Internet Engineering Task Force
3636+ (IETF). It represents the consensus of the IETF community. It has
3737+ received public review and has been approved for publication by the
3838+ Internet Engineering Steering Group (IESG). Further information on
3939+ Internet Standards is available in Section 2 of RFC 7841.
4040+4141+ Information about the current status of this document, any errata,
4242+ and how to provide feedback on it may be obtained at
4343+ https://www.rfc-editor.org/info/rfc8392.
4444+4545+4646+4747+4848+4949+5050+5151+5252+5353+5454+5555+5656+5757+5858+Jones, et al. Standards Track [Page 1]
5959+6060+RFC 8392 CBOR Web Token May 2018
6161+6262+6363+Copyright Notice
6464+6565+ Copyright (c) 2018 IETF Trust and the persons identified as the
6666+ document authors. All rights reserved.
6767+6868+ This document is subject to BCP 78 and the IETF Trust's Legal
6969+ Provisions Relating to IETF Documents
7070+ (https://trustee.ietf.org/license-info) in effect on the date of
7171+ publication of this document. Please review these documents
7272+ carefully, as they describe your rights and restrictions with respect
7373+ to this document. Code Components extracted from this document must
7474+ include Simplified BSD License text as described in Section 4.e of
7575+ the Trust Legal Provisions and are provided without warranty as
7676+ described in the Simplified BSD License.
7777+7878+7979+8080+8181+8282+8383+8484+8585+8686+8787+8888+8989+9090+9191+9292+9393+9494+9595+9696+9797+9898+9999+100100+101101+102102+103103+104104+105105+106106+107107+108108+109109+110110+111111+112112+113113+114114+Jones, et al. Standards Track [Page 2]
115115+116116+RFC 8392 CBOR Web Token May 2018
117117+118118+119119+Table of Contents
120120+121121+ 1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . 4
122122+ 1.1. CBOR-Related Terminology . . . . . . . . . . . . . . . . 4
123123+ 2. Terminology . . . . . . . . . . . . . . . . . . . . . . . . . 4
124124+ 3. Claims . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
125125+ 3.1. Registered Claims . . . . . . . . . . . . . . . . . . . . 5
126126+ 3.1.1. iss (Issuer) Claim . . . . . . . . . . . . . . . . . 5
127127+ 3.1.2. sub (Subject) Claim . . . . . . . . . . . . . . . . . 6
128128+ 3.1.3. aud (Audience) Claim . . . . . . . . . . . . . . . . 6
129129+ 3.1.4. exp (Expiration Time) Claim . . . . . . . . . . . . . 6
130130+ 3.1.5. nbf (Not Before) Claim . . . . . . . . . . . . . . . 6
131131+ 3.1.6. iat (Issued At) Claim . . . . . . . . . . . . . . . . 6
132132+ 3.1.7. cti (CWT ID) Claim . . . . . . . . . . . . . . . . . 6
133133+ 4. Summary of the Claim Names, Keys, and Value Types . . . . . . 7
134134+ 5. CBOR Tags and Claim Values . . . . . . . . . . . . . . . . . 7
135135+ 6. CWT CBOR Tag . . . . . . . . . . . . . . . . . . . . . . . . 7
136136+ 7. Creating and Validating CWTs . . . . . . . . . . . . . . . . 8
137137+ 7.1. Creating a CWT . . . . . . . . . . . . . . . . . . . . . 8
138138+ 7.2. Validating a CWT . . . . . . . . . . . . . . . . . . . . 9
139139+ 8. Security Considerations . . . . . . . . . . . . . . . . . . . 10
140140+ 9. IANA Considerations . . . . . . . . . . . . . . . . . . . . . 11
141141+ 9.1. CBOR Web Token (CWT) Claims Registry . . . . . . . . . . 11
142142+ 9.1.1. Registration Template . . . . . . . . . . . . . . . . 12
143143+ 9.1.2. Initial Registry Contents . . . . . . . . . . . . . . 12
144144+ 9.2. Media Type Registration . . . . . . . . . . . . . . . . . 14
145145+ 9.2.1. Registry Contents . . . . . . . . . . . . . . . . . . 14
146146+ 9.3. CoAP Content-Formats Registration . . . . . . . . . . . . 14
147147+ 9.3.1. Registry Contents . . . . . . . . . . . . . . . . . . 15
148148+ 9.4. CBOR Tag registration . . . . . . . . . . . . . . . . . . 15
149149+ 9.4.1. Registry Contents . . . . . . . . . . . . . . . . . . 15
150150+ 10. References . . . . . . . . . . . . . . . . . . . . . . . . . 15
151151+ 10.1. Normative References . . . . . . . . . . . . . . . . . . 15
152152+ 10.2. Informative References . . . . . . . . . . . . . . . . . 16
153153+ Appendix A. Examples . . . . . . . . . . . . . . . . . . . . . . 17
154154+ A.1. Example CWT Claims Set . . . . . . . . . . . . . . . . . 17
155155+ A.2. Example Keys . . . . . . . . . . . . . . . . . . . . . . 17
156156+ A.2.1. 128-Bit Symmetric Key . . . . . . . . . . . . . . . . 18
157157+ A.2.2. 256-Bit Symmetric Key . . . . . . . . . . . . . . . . 18
158158+ A.2.3. Elliptic Curve Digital Signature Algorithm (ECDSA)
159159+ P-256 256-Bit COSE Key . . . . . . . . . . . . . . . 19
160160+ A.3. Example Signed CWT . . . . . . . . . . . . . . . . . . . 19
161161+ A.4. Example MACed CWT . . . . . . . . . . . . . . . . . . . . 20
162162+ A.5. Example Encrypted CWT . . . . . . . . . . . . . . . . . . 21
163163+ A.6. Example Nested CWT . . . . . . . . . . . . . . . . . . . 22
164164+ A.7. Example MACed CWT with a Floating-Point Value . . . . . . 23
165165+ Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . 24
166166+ Authors' Addresses . . . . . . . . . . . . . . . . . . . . . . . 25
167167+168168+169169+170170+Jones, et al. Standards Track [Page 3]
171171+172172+RFC 8392 CBOR Web Token May 2018
173173+174174+175175+1. Introduction
176176+177177+ The JSON Web Token (JWT) [RFC7519] is a standardized security token
178178+ format that has found use in OAuth 2.0 and OpenID Connect
179179+ deployments, among other applications. JWT uses JSON Web Signature
180180+ (JWS) [RFC7515] and JSON Web Encryption (JWE) [RFC7516] to secure the
181181+ contents of the JWT, which is a set of claims represented in JSON.
182182+ The use of JSON for encoding information is popular for Web and
183183+ native applications, but it is considered inefficient for some
184184+ Internet of Things (IoT) systems that use low-power radio
185185+ technologies.
186186+187187+ An alternative encoding of claims is defined in this document.
188188+ Instead of using JSON, as provided by JWTs, this specification uses
189189+ CBOR [RFC7049] and calls this new structure "CBOR Web Token (CWT)",
190190+ which is a compact means of representing secured claims to be
191191+ transferred between two parties. CWT is closely related to JWT. It
192192+ references the JWT claims and both its name and pronunciation are
193193+ derived from JWT (the suggested pronunciation of CWT is the same as
194194+ the English word "cot"). To protect the claims contained in CWTs,
195195+ the CBOR Object Signing and Encryption (COSE) [RFC8152] specification
196196+ is used.
197197+198198+1.1. CBOR-Related Terminology
199199+200200+ In JSON, maps are called objects and only have one kind of map key: a
201201+ string. CBOR uses strings, negative integers, and unsigned integers
202202+ as map keys. The integers are used for compactness of encoding and
203203+ easy comparison. The inclusion of strings allows for an additional
204204+ range of short encoded values to be used.
205205+206206+2. Terminology
207207+208208+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
209209+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
210210+ "OPTIONAL" in this document are to be interpreted as described in
211211+ BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all
212212+ capitals, as shown here.
213213+214214+ This document reuses terminology from JWT [RFC7519] and COSE
215215+ [RFC8152].
216216+217217+ StringOrURI
218218+ The "StringOrURI" term in this specification has the same meaning
219219+ and processing rules as the JWT "StringOrURI" term defined in
220220+ Section 2 of [RFC7519], except that it is represented as a CBOR
221221+ text string instead of a JSON text string.
222222+223223+224224+225225+226226+Jones, et al. Standards Track [Page 4]
227227+228228+RFC 8392 CBOR Web Token May 2018
229229+230230+231231+ NumericDate
232232+ The "NumericDate" term in this specification has the same meaning
233233+ and processing rules as the JWT "NumericDate" term defined in
234234+ Section 2 of [RFC7519], except that it is represented as a CBOR
235235+ numeric date (from Section 2.4.1 of [RFC7049]) instead of a JSON
236236+ number. The encoding is modified so that the leading tag 1
237237+ (epoch-based date/time) MUST be omitted.
238238+239239+ Claim Name
240240+ The human-readable name used to identify a claim.
241241+242242+ Claim Key
243243+ The CBOR map key used to identify a claim.
244244+245245+ Claim Value
246246+ The CBOR map value representing the value of the claim.
247247+248248+ CWT Claims Set
249249+ The CBOR map that contains the claims conveyed by the CWT.
250250+251251+3. Claims
252252+253253+ The set of claims that a CWT must contain to be considered valid is
254254+ context dependent and is outside the scope of this specification.
255255+ Specific applications of CWTs will require implementations to
256256+ understand and process some claims in particular ways. However, in
257257+ the absence of such requirements, all claims that are not understood
258258+ by implementations MUST be ignored.
259259+260260+ To keep CWTs as small as possible, the Claim Keys are represented
261261+ using integers or text strings. Section 4 summarizes all keys used
262262+ to identify the claims defined in this document.
263263+264264+3.1. Registered Claims
265265+266266+ None of the claims defined below are intended to be mandatory to use
267267+ or implement. Rather, they provide a starting point for a set of
268268+ useful, interoperable claims. Applications using CWTs should define
269269+ which specific claims they use and when they are required or
270270+ optional.
271271+272272+3.1.1. iss (Issuer) Claim
273273+274274+ The "iss" (issuer) claim has the same meaning and processing rules as
275275+ the "iss" claim defined in Section 4.1.1 of [RFC7519], except that
276276+ the value is a StringOrURI, as defined in Section 2 of this
277277+ specification. The Claim Key 1 is used to identify this claim.
278278+279279+280280+281281+282282+Jones, et al. Standards Track [Page 5]
283283+284284+RFC 8392 CBOR Web Token May 2018
285285+286286+287287+3.1.2. sub (Subject) Claim
288288+289289+ The "sub" (subject) claim has the same meaning and processing rules
290290+ as the "sub" claim defined in Section 4.1.2 of [RFC7519], except that
291291+ the value is a StringOrURI, as defined in Section 2 of this
292292+ specification. The Claim Key 2 is used to identify this claim.
293293+294294+3.1.3. aud (Audience) Claim
295295+296296+ The "aud" (audience) claim has the same meaning and processing rules
297297+ as the "aud" claim defined in Section 4.1.3 of [RFC7519], except that
298298+ the value of the audience claim is a StringOrURI when it is not an
299299+ array or each of the audience array element values is a StringOrURI
300300+ when the audience claim value is an array. (StringOrURI is defined
301301+ in Section 2 of this specification.) The Claim Key 3 is used to
302302+ identify this claim.
303303+304304+3.1.4. exp (Expiration Time) Claim
305305+306306+ The "exp" (expiration time) claim has the same meaning and processing
307307+ rules as the "exp" claim defined in Section 4.1.4 of [RFC7519],
308308+ except that the value is a NumericDate, as defined in Section 2 of
309309+ this specification. The Claim Key 4 is used to identify this claim.
310310+311311+3.1.5. nbf (Not Before) Claim
312312+313313+ The "nbf" (not before) claim has the same meaning and processing
314314+ rules as the "nbf" claim defined in Section 4.1.5 of [RFC7519],
315315+ except that the value is a NumericDate, as defined in Section 2 of
316316+ this specification. The Claim Key 5 is used to identify this claim.
317317+318318+3.1.6. iat (Issued At) Claim
319319+320320+ The "iat" (issued at) claim has the same meaning and processing rules
321321+ as the "iat" claim defined in Section 4.1.6 of [RFC7519], except that
322322+ the value is a NumericDate, as defined in Section 2 of this
323323+ specification. The Claim Key 6 is used to identify this claim.
324324+325325+3.1.7. cti (CWT ID) Claim
326326+327327+ The "cti" (CWT ID) claim has the same meaning and processing rules as
328328+ the "jti" claim defined in Section 4.1.7 of [RFC7519], except that
329329+ the value is a byte string. The Claim Key 7 is used to identify this
330330+ claim.
331331+332332+333333+334334+335335+336336+337337+338338+Jones, et al. Standards Track [Page 6]
339339+340340+RFC 8392 CBOR Web Token May 2018
341341+342342+343343+4. Summary of the Claim Names, Keys, and Value Types
344344+345345+ +------+-----+----------------------------------+
346346+ | Name | Key | Value Type |
347347+ +------+-----+----------------------------------+
348348+ | iss | 1 | text string |
349349+ | sub | 2 | text string |
350350+ | aud | 3 | text string |
351351+ | exp | 4 | integer or floating-point number |
352352+ | nbf | 5 | integer or floating-point number |
353353+ | iat | 6 | integer or floating-point number |
354354+ | cti | 7 | byte string |
355355+ +------+-----+----------------------------------+
356356+357357+ Table 1: Summary of the Claim Names, Keys, and Value Types
358358+359359+5. CBOR Tags and Claim Values
360360+361361+ The claim values defined in this specification MUST NOT be prefixed
362362+ with any CBOR tag. For instance, while CBOR tag 1 (epoch-based date/
363363+ time) could logically be prefixed to values of the "exp", "nbf", and
364364+ "iat" claims, this is unnecessary since the representation of the
365365+ claim values is already specified by the claim definitions. Tagging
366366+ claim values would only take up extra space without adding
367367+ information. However, this does not prohibit future claim
368368+ definitions from requiring the use of CBOR tags for those specific
369369+ claims.
370370+371371+6. CWT CBOR Tag
372372+373373+ How to determine that a CBOR data structure is a CWT is application
374374+ dependent. In some cases, this information is known from the
375375+ application context, such as from the position of the CWT in a data
376376+ structure at which the value must be a CWT. One method of indicating
377377+ that a CBOR object is a CWT is the use of the "application/cwt"
378378+ content type by a transport protocol.
379379+380380+ This section defines the CWT CBOR tag as another means for
381381+ applications to declare that a CBOR data structure is a CWT. Its use
382382+ is optional and is intended for use in cases in which this
383383+ information would not otherwise be known.
384384+385385+386386+387387+388388+389389+390390+391391+392392+393393+394394+Jones, et al. Standards Track [Page 7]
395395+396396+RFC 8392 CBOR Web Token May 2018
397397+398398+399399+ If present, the CWT tag MUST prefix a tagged object using one of the
400400+ COSE CBOR tags. In this example, the COSE_Mac0 tag is used. The
401401+ actual COSE_Mac0 object has been excluded from this example.
402402+403403+ / CWT CBOR tag / 61(
404404+ / COSE_Mac0 CBOR tag / 17(
405405+ / COSE_Mac0 object /
406406+ )
407407+ )
408408+409409+ Figure 1: Example of CWT Tag Usage
410410+411411+7. Creating and Validating CWTs
412412+413413+7.1. Creating a CWT
414414+415415+ To create a CWT, the following steps are performed. The order of the
416416+ steps is not significant in cases where there are no dependencies
417417+ between the inputs and outputs of the steps.
418418+419419+ 1. Create a CWT Claims Set containing the desired claims.
420420+421421+ 2. Let the Message be the binary representation of the CWT Claims
422422+ Set.
423423+424424+ 3. Create a COSE Header containing the desired set of Header
425425+ Parameters. The COSE Header MUST be valid per the [RFC8152]
426426+ specification.
427427+428428+ 4. Depending upon whether the CWT is signed, MACed, or encrypted,
429429+ there are three cases:
430430+431431+ * If the CWT is signed, create a COSE_Sign/COSE_Sign1 object
432432+ using the Message as the COSE_Sign/COSE_Sign1 Payload; all
433433+ steps specified in [RFC8152] for creating a COSE_Sign/
434434+ COSE_Sign1 object MUST be followed.
435435+436436+ * Else, if the CWT is MACed, create a COSE_Mac/COSE_Mac0 object
437437+ using the Message as the COSE_Mac/COSE_Mac0 Payload; all steps
438438+ specified in [RFC8152] for creating a COSE_Mac/COSE_Mac0
439439+ object MUST be followed.
440440+441441+ * Else, if the CWT is a COSE_Encrypt/COSE_Encrypt0 object,
442442+ create a COSE_Encrypt/COSE_Encrypt0 using the Message as the
443443+ plaintext for the COSE_Encrypt/COSE_Encrypt0 object; all steps
444444+ specified in [RFC8152] for creating a COSE_Encrypt/
445445+ COSE_Encrypt0 object MUST be followed.
446446+447447+448448+449449+450450+Jones, et al. Standards Track [Page 8]
451451+452452+RFC 8392 CBOR Web Token May 2018
453453+454454+455455+ 5. If a nested signing, MACing, or encryption operation will be
456456+ performed, let the Message be the tagged COSE_Sign/COSE_Sign1,
457457+ COSE_Mac/COSE_Mac0, or COSE_Encrypt/COSE_Encrypt0, and return to
458458+ Step 3.
459459+460460+ 6. If needed by the application, prepend the COSE object with the
461461+ appropriate COSE CBOR tag to indicate the type of the COSE
462462+ object. If needed by the application, prepend the COSE object
463463+ with the CWT CBOR tag to indicate that the COSE object is a CWT.
464464+465465+7.2. Validating a CWT
466466+467467+ When validating a CWT, the following steps are performed. The order
468468+ of the steps is not significant in cases where there are no
469469+ dependencies between the inputs and outputs of the steps. If any of
470470+ the listed steps fail, then the CWT MUST be rejected -- that is,
471471+ treated by the application as invalid input.
472472+473473+ 1. Verify that the CWT is a valid CBOR object.
474474+475475+ 2. If the object begins with the CWT CBOR tag, remove it and verify
476476+ that one of the COSE CBOR tags follows it.
477477+478478+ 3. If the object is tagged with one of the COSE CBOR tags, remove it
479479+ and use it to determine the type of the CWT, COSE_Sign/
480480+ COSE_Sign1, COSE_Mac/COSE_Mac0, or COSE_Encrypt/COSE_Encrypt0.
481481+ If the object does not have a COSE CBOR tag, the COSE message
482482+ type is determined from the application context.
483483+484484+ 4. Verify that the resulting COSE Header includes only parameters
485485+ and values whose syntax and semantics are both understood and
486486+ supported or that are specified as being ignored when not
487487+ understood.
488488+489489+ 5. Depending upon whether the CWT is a signed, MACed, or encrypted,
490490+ there are three cases:
491491+492492+ * If the CWT is a COSE_Sign/COSE_Sign1, follow the steps
493493+ specified in Section 4 of [RFC8152] ("Signing Objects") for
494494+ validating a COSE_Sign/COSE_Sign1 object. Let the Message be
495495+ the COSE_Sign/COSE_Sign1 payload.
496496+497497+ * Else, if the CWT is a COSE_Mac/COSE_Mac0, follow the steps
498498+ specified in Section 6 of [RFC8152] ("MAC Objects") for
499499+ validating a COSE_Mac/COSE_Mac0 object. Let the Message be
500500+ the COSE_Mac/COSE_Mac0 payload.
501501+502502+503503+504504+505505+506506+Jones, et al. Standards Track [Page 9]
507507+508508+RFC 8392 CBOR Web Token May 2018
509509+510510+511511+ * Else, if the CWT is a COSE_Encrypt/COSE_Encrypt0 object,
512512+ follow the steps specified in Section 5 of [RFC8152]
513513+ ("Encryption Objects") for validating a COSE_Encrypt/
514514+ COSE_Encrypt0 object. Let the Message be the resulting
515515+ plaintext.
516516+517517+ 6. If the Message begins with a COSE CBOR tag, then the Message is a
518518+ CWT that was the subject of nested signing, MACing, or encryption
519519+ operations. In this case, return to Step 1, using the Message as
520520+ the CWT.
521521+522522+ 7. Verify that the Message is a valid CBOR map; let the CWT Claims
523523+ Set be this CBOR map.
524524+525525+8. Security Considerations
526526+527527+ The security of the CWT relies upon on the protections offered by
528528+ COSE. Unless the claims in a CWT are protected, an adversary can
529529+ modify, add, or remove claims.
530530+531531+ Since the claims conveyed in a CWT may be used to make authorization
532532+ decisions, it is not only important to protect the CWT in transit but
533533+ also to ensure that the recipient can authenticate the party that
534534+ assembled the claims and created the CWT. Without trust of the
535535+ recipient in the party that created the CWT, no sensible
536536+ authorization decision can be made. Furthermore, the creator of the
537537+ CWT needs to carefully evaluate each claim value prior to including
538538+ it in the CWT so that the recipient can be assured of the validity of
539539+ the information provided.
540540+541541+ Syntactically, the signing and encryption operations for Nested CWTs
542542+ may be applied in any order; however, if both signing and encryption
543543+ are necessary, producers normally should sign the message and then
544544+ encrypt the result (thus encrypting the signature). This prevents
545545+ attacks in which the signature is stripped, leaving just an encrypted
546546+ message, as well as providing privacy for the signer. Furthermore,
547547+ signatures over encrypted text are not considered valid in many
548548+ jurisdictions.
549549+550550+551551+552552+553553+554554+555555+556556+557557+558558+559559+560560+561561+562562+Jones, et al. Standards Track [Page 10]
563563+564564+RFC 8392 CBOR Web Token May 2018
565565+566566+567567+9. IANA Considerations
568568+569569+9.1. CBOR Web Token (CWT) Claims Registry
570570+571571+ IANA has created the "CBOR Web Token (CWT) Claims" registry
572572+ [IANA.CWT.Claims].
573573+574574+ Registration requests are evaluated using the criteria described in
575575+ the Claim Key instructions in the registration template below after a
576576+ three-week review period on the cwt-reg-review@ietf.org mailing list,
577577+ on the advice of one or more Designated Experts [RFC8126]. However,
578578+ to allow for the allocation of values prior to publication, the
579579+ Designated Experts may approve registration once they are satisfied
580580+ that such a specification will be published.
581581+582582+ Registration requests sent to the mailing list for review should use
583583+ an appropriate subject (e.g., "Request to register claim: example").
584584+ Registration requests that are undetermined for a period longer than
585585+ 21 days can be brought to the IESG's attention (using the
586586+ iesg@ietf.org mailing list) for resolution.
587587+588588+ Criteria that should be applied by the Designated Experts includes
589589+ determining whether the proposed registration duplicates existing
590590+ functionality, whether it is likely to be of general applicability or
591591+ whether it is useful only for a single application, and whether the
592592+ registration description is clear. Registrations for the limited set
593593+ of values between -256 and 255 and strings of length 1 are to be
594594+ restricted to claims with general applicability.
595595+596596+ IANA must only accept registry updates from the Designated Experts
597597+ and should direct all requests for registration to the review mailing
598598+ list.
599599+600600+ It is suggested that multiple Designated Experts be appointed who are
601601+ able to represent the perspectives of different applications using
602602+ this specification in order to enable broadly informed review of
603603+ registration decisions. In cases where a registration decision could
604604+ be perceived as creating a conflict of interest for a particular
605605+ Expert, that Expert should defer to the judgment of the other
606606+ Experts.
607607+608608+ Since a high degree of overlap is expected between the contents of
609609+ the "CBOR Web Token (CWT) Claims" registry and the "JSON Web Token
610610+ Claims" registry, overlap in the corresponding pools of Designated
611611+ Experts would be useful to help ensure that an appropriate level of
612612+ coordination between the registries is maintained.
613613+614614+615615+616616+617617+618618+Jones, et al. Standards Track [Page 11]
619619+620620+RFC 8392 CBOR Web Token May 2018
621621+622622+623623+9.1.1. Registration Template
624624+625625+ Claim Name:
626626+ The human-readable name requested (e.g., "iss").
627627+628628+ Claim Description:
629629+ Brief description of the claim (e.g., "Issuer").
630630+631631+ JWT Claim Name:
632632+ Claim Name of the equivalent JWT claim, as registered in
633633+ [IANA.JWT.Claims]. CWT claims should normally have a
634634+ corresponding JWT claim. If a corresponding JWT claim would not
635635+ make sense, the Designated Experts can choose to accept
636636+ registrations for which the JWT Claim Name is listed as "N/A".
637637+638638+ Claim Key:
639639+ CBOR map key for the claim. Different ranges of values use
640640+ different registration policies [RFC8126]. Integer values from
641641+ -256 to 255 and strings of length 1 are designated as Standards
642642+ Action. Integer values from -65536 to -257 and from 256 to 65535
643643+ along with strings of length 2 are designated as Specification
644644+ Required. Integer values greater than 65535 and strings of length
645645+ greater than 2 are designated as Expert Review. Integer values
646646+ less than -65536 are marked as Private Use.
647647+648648+ Claim Value Type(s):
649649+ CBOR types that can be used for the claim value.
650650+651651+ Change Controller:
652652+ For Standards Track RFCs, list the "IESG". For others, give the
653653+ name of the responsible party. Other details (e.g., postal
654654+ address, email address, home page URI) may also be included.
655655+656656+ Specification Document(s):
657657+ Reference to the document or documents that specify the parameter,
658658+ preferably including URIs that can be used to retrieve copies of
659659+ the documents. An indication of the relevant sections may also be
660660+ included but is not required.
661661+662662+9.1.2. Initial Registry Contents
663663+664664+ o Claim Name: (RESERVED)
665665+ o Claim Description: This registration reserves the key value 0.
666666+ o JWT Claim Name: N/A
667667+ o Claim Key: 0
668668+ o Claim Value Type(s): N/A
669669+ o Change Controller: IESG
670670+ o Specification Document(s): [RFC8392]
671671+672672+673673+674674+Jones, et al. Standards Track [Page 12]
675675+676676+RFC 8392 CBOR Web Token May 2018
677677+678678+679679+ o Claim Name: iss
680680+ o Claim Description: Issuer
681681+ o JWT Claim Name: iss
682682+ o Claim Key: 1
683683+ o Claim Value Type(s): text string
684684+ o Change Controller: IESG
685685+ o Specification Document(s): Section 3.1.1 of [RFC8392]
686686+687687+ o Claim Name: sub
688688+ o Claim Description: Subject
689689+ o JWT Claim Name: sub
690690+ o Claim Key: 2
691691+ o Claim Value Type(s): text string
692692+ o Change Controller: IESG
693693+ o Specification Document(s): Section 3.1.2 of [RFC8392]
694694+695695+ o Claim Name: aud
696696+ o Claim Description: Audience
697697+ o JWT Claim Name: aud
698698+ o Claim Key: 3
699699+ o Claim Value Type(s): text string
700700+ o Change Controller: IESG
701701+ o Specification Document(s): Section 3.1.3 of [RFC8392]
702702+703703+ o Claim Name: exp
704704+ o Claim Description: Expiration Time
705705+ o JWT Claim Name: exp
706706+ o Claim Key: 4
707707+ o Claim Value Type(s): integer or floating-point number
708708+ o Change Controller: IESG
709709+ o Specification Document(s): Section 3.1.4 of [RFC8392]
710710+711711+ o Claim Name: nbf
712712+ o Claim Description: Not Before
713713+ o JWT Claim Name: nbf
714714+ o Claim Key: 5
715715+ o Claim Value Type(s): integer or floating-point number
716716+ o Change Controller: IESG
717717+ o Specification Document(s): Section 3.1.5 of [RFC8392]
718718+719719+ o Claim Name: iat
720720+ o Claim Description: Issued At
721721+ o JWT Claim Name: iat
722722+ o Claim Key: 6
723723+ o Claim Value Type(s): integer or floating-point number
724724+ o Change Controller: IESG
725725+ o Specification Document(s): Section 3.1.6 of [RFC8392]
726726+727727+728728+729729+730730+Jones, et al. Standards Track [Page 13]
731731+732732+RFC 8392 CBOR Web Token May 2018
733733+734734+735735+ o Claim Name: cti
736736+ o Claim Description: CWT ID
737737+ o JWT Claim Name: jti
738738+ o Claim Key: 7
739739+ o Claim Value Type(s): byte string
740740+ o Change Controller: IESG
741741+ o Specification Document(s): Section 3.1.7 of [RFC8392]
742742+743743+9.2. Media Type Registration
744744+745745+ IANA has registered the "application/cwt" media type in the "Media
746746+ Types" registry [IANA.MediaTypes] in the manner described in RFC 6838
747747+ [RFC6838], which can be used to indicate that the content is a CWT.
748748+749749+9.2.1. Registry Contents
750750+751751+ o Type name: application
752752+ o Subtype name: cwt
753753+ o Required parameters: N/A
754754+ o Optional parameters: N/A
755755+ o Encoding considerations: binary
756756+ o Security considerations: See the Security Considerations section
757757+ of [RFC8392]
758758+ o Interoperability considerations: N/A
759759+ o Published specification: [RFC8392]
760760+ o Applications that use this media type: IoT applications sending
761761+ security tokens over HTTP(S), CoAP(S), and other transports.
762762+ o Fragment identifier considerations: N/A
763763+ o Additional information:
764764+765765+ Magic number(s): N/A
766766+ File extension(s): N/A
767767+ Macintosh file type code(s): N/A
768768+769769+ o Person & email address to contact for further information:
770770+ IESG, iesg@ietf.org
771771+ o Intended usage: COMMON
772772+ o Restrictions on usage: none
773773+ o Author: Michael B. Jones, mbj@microsoft.com
774774+ o Change controller: IESG
775775+ o Provisional registration? No
776776+777777+9.3. CoAP Content-Formats Registration
778778+779779+ IANA has registered the CoAP Content-Format ID for the "application/
780780+ cwt" media type in the "CoAP Content-Formats" registry
781781+ [IANA.CoAP.Content-Formats].
782782+783783+784784+785785+786786+Jones, et al. Standards Track [Page 14]
787787+788788+RFC 8392 CBOR Web Token May 2018
789789+790790+791791+9.3.1. Registry Contents
792792+793793+ o Media Type: application/cwt
794794+ o Encoding: -
795795+ o Id: 61
796796+ o Reference: [RFC8392]
797797+798798+9.4. CBOR Tag registration
799799+800800+ IANA has registered the CWT CBOR tag in the "CBOR Tags" registry
801801+ [IANA.CBOR.Tags].
802802+803803+9.4.1. Registry Contents
804804+805805+ o CBOR Tag: 61
806806+ o Data Item: CBOR Web Token (CWT)
807807+ o Semantics: CBOR Web Token (CWT), as defined in [RFC8392]
808808+ o Reference: [RFC8392]
809809+ o Point of Contact: Michael B. Jones, mbj@microsoft.com
810810+811811+10. References
812812+813813+10.1. Normative References
814814+815815+ [IANA.CBOR.Tags]
816816+ IANA, "Concise Binary Object Representation (CBOR) Tags",
817817+ <http://www.iana.org/assignments/cbor-tags/>.
818818+819819+ [IANA.CoAP.Content-Formats]
820820+ IANA, "CoAP Content-Formats",
821821+ <https://www.iana.org/assignments/core-parameters/>.
822822+823823+ [IANA.CWT.Claims]
824824+ IANA, "CBOR Web Token (CWT) Claims",
825825+ <http://www.iana.org/assignments/cwt>.
826826+827827+ [IANA.MediaTypes]
828828+ IANA, "Media Types",
829829+ <http://www.iana.org/assignments/media-types>.
830830+831831+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
832832+ Requirement Levels", BCP 14, RFC 2119,
833833+ DOI 10.17487/RFC2119, March 1997,
834834+ <https://www.rfc-editor.org/info/rfc2119>.
835835+836836+ [RFC7049] Bormann, C. and P. Hoffman, "Concise Binary Object
837837+ Representation (CBOR)", RFC 7049, DOI 10.17487/RFC7049,
838838+ October 2013, <https://www.rfc-editor.org/info/rfc7049>.
839839+840840+841841+842842+Jones, et al. Standards Track [Page 15]
843843+844844+RFC 8392 CBOR Web Token May 2018
845845+846846+847847+ [RFC7519] Jones, M., Bradley, J., and N. Sakimura, "JSON Web Token
848848+ (JWT)", RFC 7519, DOI 10.17487/RFC7519, May 2015,
849849+ <https://www.rfc-editor.org/info/rfc7519>.
850850+851851+ [RFC8152] Schaad, J., "CBOR Object Signing and Encryption (COSE)",
852852+ RFC 8152, DOI 10.17487/RFC8152, July 2017,
853853+ <https://www.rfc-editor.org/info/rfc8152>.
854854+855855+ [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
856856+ 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
857857+ May 2017, <https://www.rfc-editor.org/info/rfc8174>.
858858+859859+10.2. Informative References
860860+861861+ [IANA.JWT.Claims]
862862+ IANA, "JSON Web Token Claims",
863863+ <http://www.iana.org/assignments/jwt>.
864864+865865+ [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type
866866+ Specifications and Registration Procedures", BCP 13,
867867+ RFC 6838, DOI 10.17487/RFC6838, January 2013,
868868+ <https://www.rfc-editor.org/info/rfc6838>.
869869+870870+ [RFC7515] Jones, M., Bradley, J., and N. Sakimura, "JSON Web
871871+ Signature (JWS)", RFC 7515, DOI 10.17487/RFC7515, May
872872+ 2015, <https://www.rfc-editor.org/info/rfc7515>.
873873+874874+ [RFC7516] Jones, M. and J. Hildebrand, "JSON Web Encryption (JWE)",
875875+ RFC 7516, DOI 10.17487/RFC7516, May 2015,
876876+ <https://www.rfc-editor.org/info/rfc7516>.
877877+878878+ [RFC8126] Cotton, M., Leiba, B., and T. Narten, "Guidelines for
879879+ Writing an IANA Considerations Section in RFCs", BCP 26,
880880+ RFC 8126, DOI 10.17487/RFC8126, June 2017,
881881+ <https://www.rfc-editor.org/info/rfc8126>.
882882+883883+884884+885885+886886+887887+888888+889889+890890+891891+892892+893893+894894+895895+896896+897897+898898+Jones, et al. Standards Track [Page 16]
899899+900900+RFC 8392 CBOR Web Token May 2018
901901+902902+903903+Appendix A. Examples
904904+905905+ This appendix includes a set of CWT examples that show how the CWT
906906+ Claims Set can be protected. There are examples that are signed,
907907+ MACed, encrypted, and that use nested signing and encryption. To
908908+ make the examples easier to read, they are presented both as hex
909909+ strings and in the extended CBOR diagnostic notation described in
910910+ Section 6 of [RFC7049].
911911+912912+ Where a byte string is to carry an embedded CBOR-encoded item, the
913913+ diagnostic notation for this CBOR data item can be enclosed in '<<'
914914+ and '>>' to notate the byte string resulting from encoding the data
915915+ item, e.g., h'63666F6F' translates to <<"foo">>.
916916+917917+A.1. Example CWT Claims Set
918918+919919+ The CWT Claims Set used for the different examples displays usage of
920920+ all the defined claims. For signed and MACed examples, the CWT
921921+ Claims Set is the CBOR encoding as a byte string.
922922+923923+ a70175636f61703a2f2f61732e6578616d706c652e636f6d02656572696b7703
924924+ 7818636f61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0
925925+ 051a5610d9f0061a5610d9f007420b71
926926+927927+ Figure 2: Example CWT Claims Set as Hex String
928928+929929+ {
930930+ / iss / 1: "coap://as.example.com",
931931+ / sub / 2: "erikw",
932932+ / aud / 3: "coap://light.example.com",
933933+ / exp / 4: 1444064944,
934934+ / nbf / 5: 1443944944,
935935+ / iat / 6: 1443944944,
936936+ / cti / 7: h'0b71'
937937+ }
938938+939939+ Figure 3: Example CWT Claims Set in CBOR Diagnostic Notation
940940+941941+A.2. Example Keys
942942+943943+ This section contains the keys used to sign, MAC, and encrypt the
944944+ messages in this appendix. Line breaks are for display purposes
945945+ only.
946946+947947+948948+949949+950950+951951+952952+953953+954954+Jones, et al. Standards Track [Page 17]
955955+956956+RFC 8392 CBOR Web Token May 2018
957957+958958+959959+A.2.1. 128-Bit Symmetric Key
960960+961961+ a42050231f4c4d4d3051fdc2ec0a3851d5b3830104024c53796d6d6574726963
962962+ 313238030a
963963+964964+ Figure 4: 128-Bit Symmetric COSE_Key as Hex String
965965+966966+ {
967967+ / k / -1: h'231f4c4d4d3051fdc2ec0a3851d5b383'
968968+ / kty / 1: 4 / Symmetric /,
969969+ / kid / 2: h'53796d6d6574726963313238' / 'Symmetric128' /,
970970+ / alg / 3: 10 / AES-CCM-16-64-128 /
971971+ }
972972+973973+ Figure 5: 128-Bit Symmetric COSE_Key in CBOR Diagnostic Notation
974974+975975+A.2.2. 256-Bit Symmetric Key
976976+977977+ a4205820403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d
978978+ 795693880104024c53796d6d6574726963323536030a
979979+980980+ Figure 6: 256-Bit Symmetric COSE_Key as Hex String
981981+982982+ {
983983+ / k / -1: h'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1
984984+ ec99192d79569388'
985985+ / kty / 1: 4 / Symmetric /,
986986+ / kid / 4: h'53796d6d6574726963323536' / 'Symmetric256' /,
987987+ / alg / 3: 4 / HMAC 256/64 /
988988+ }
989989+990990+ Figure 7: 256-Bit Symmetric COSE_Key in CBOR Diagnostic Notation
991991+992992+993993+994994+995995+996996+997997+998998+999999+10001000+10011001+10021002+10031003+10041004+10051005+10061006+10071007+10081008+10091009+10101010+Jones, et al. Standards Track [Page 18]
10111011+10121012+RFC 8392 CBOR Web Token May 2018
10131013+10141014+10151015+A.2.3. Elliptic Curve Digital Signature Algorithm (ECDSA) P-256 256-Bit
10161016+ COSE Key
10171017+10181018+ a72358206c1382765aec5358f117733d281c1c7bdc39884d04a45a1e6c67c858
10191019+ bc206c1922582060f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168db
10201020+ 9529971a36e7b9215820143329cce7868e416927599cf65a34f3ce2ffda55a7e
10211021+ ca69ed8919a394d42f0f2001010202524173796d6d6574726963454344534132
10221022+ 35360326
10231023+10241024+ Figure 8: ECDSA 256-Bit COSE Key as Hex String
10251025+10261026+ {
10271027+ / d / -4: h'6c1382765aec5358f117733d281c1c7bdc39884d04a45a1e
10281028+ 6c67c858bc206c19',
10291029+ / y / -3: h'60f7f1a780d8a783bfb7a2dd6b2796e8128dbbcef9d3d168
10301030+ db9529971a36e7b9',
10311031+ / x / -2: h'143329cce7868e416927599cf65a34f3ce2ffda55a7eca69
10321032+ ed8919a394d42f0f',
10331033+ / crv / -1: 1 / P-256 /,
10341034+ / kty / 1: 2 / EC2 /,
10351035+ / kid / 2: h'4173796d6d657472696345434453413
10361036+ 23536' / 'AsymmetricECDSA256' /,
10371037+ / alg / 3: -7 / ECDSA 256 /
10381038+ }
10391039+10401040+ Figure 9: ECDSA 256-Bit COSE Key in CBOR Diagnostic Notation
10411041+10421042+A.3. Example Signed CWT
10431043+10441044+ This section shows a signed CWT with a single recipient and a full
10451045+ CWT Claims Set.
10461046+10471047+ The signature is generated using the private key listed in
10481048+ Appendix A.2.3, and it can be validated using the public key from
10491049+ Appendix A.2.3. Line breaks are for display purposes only.
10501050+10511051+ d28443a10126a104524173796d6d657472696345434453413235365850a701756
10521052+ 36f61703a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f
10531053+ 61703a2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d
10541054+ 9f0061a5610d9f007420b7158405427c1ff28d23fbad1f29c4c7c6a555e601d6f
10551055+ a29f9179bc3d7438bacaca5acd08c8d4d4f96131680c429a01f85951ecee743a5
10561056+ 2b9b63632c57209120e1c9e30
10571057+10581058+ Figure 10: Signed CWT as Hex String
10591059+10601060+10611061+10621062+10631063+10641064+10651065+10661066+Jones, et al. Standards Track [Page 19]
10671067+10681068+RFC 8392 CBOR Web Token May 2018
10691069+10701070+10711071+ 18(
10721072+ [
10731073+ / protected / << {
10741074+ / alg / 1: -7 / ECDSA 256 /
10751075+ } >>,
10761076+ / unprotected / {
10771077+ / kid / 4: h'4173796d6d657472696345434453413
10781078+ 23536' / 'AsymmetricECDSA256' /
10791079+ },
10801080+ / payload / << {
10811081+ / iss / 1: "coap://as.example.com",
10821082+ / sub / 2: "erikw",
10831083+ / aud / 3: "coap://light.example.com",
10841084+ / exp / 4: 1444064944,
10851085+ / nbf / 5: 1443944944,
10861086+ / iat / 6: 1443944944,
10871087+ / cti / 7: h'0b71'
10881088+ } >>,
10891089+ / signature / h'5427c1ff28d23fbad1f29c4c7c6a555e601d6fa29f
10901090+ 9179bc3d7438bacaca5acd08c8d4d4f96131680c42
10911091+ 9a01f85951ecee743a52b9b63632c57209120e1c9e
10921092+ 30'
10931093+ ]
10941094+ )
10951095+10961096+ Figure 11: Signed CWT in CBOR Diagnostic Notation
10971097+10981098+A.4. Example MACed CWT
10991099+11001100+ This section shows a MACed CWT with a single recipient, a full CWT
11011101+ Claims Set, and a CWT tag.
11021102+11031103+ The MAC is generated using the 256-bit symmetric key from
11041104+ Appendix A.2.2 with a 64-bit truncation. Line breaks are for display
11051105+ purposes only.
11061106+11071107+ d83dd18443a10104a1044c53796d6d65747269633235365850a70175636f6170
11081108+ 3a2f2f61732e6578616d706c652e636f6d02656572696b77037818636f61703a
11091109+ 2f2f6c696768742e6578616d706c652e636f6d041a5612aeb0051a5610d9f006
11101110+ 1a5610d9f007420b7148093101ef6d789200
11111111+11121112+ Figure 12: MACed CWT with CWT Tag as Hex String
11131113+11141114+11151115+11161116+11171117+11181118+11191119+11201120+11211121+11221122+Jones, et al. Standards Track [Page 20]
11231123+11241124+RFC 8392 CBOR Web Token May 2018
11251125+11261126+11271127+ 61(
11281128+ 17(
11291129+ [
11301130+ / protected / << {
11311131+ / alg / 1: 4 / HMAC-256-64 /
11321132+ } >>,
11331133+ / unprotected / {
11341134+ / kid / 4: h'53796d6d6574726963323536' / 'Symmetric256' /
11351135+ },
11361136+ / payload / << {
11371137+ / iss / 1: "coap://as.example.com",
11381138+ / sub / 2: "erikw",
11391139+ / aud / 3: "coap://light.example.com",
11401140+ / exp / 4: 1444064944,
11411141+ / nbf / 5: 1443944944,
11421142+ / iat / 6: 1443944944,
11431143+ / cti / 7: h'0b71'
11441144+ } >>,
11451145+ / tag / h'093101ef6d789200'
11461146+ ]
11471147+ )
11481148+ )
11491149+11501150+ Figure 13: MACed CWT with CWT Tag in CBOR Diagnostic Notation
11511151+11521152+A.5. Example Encrypted CWT
11531153+11541154+ This section shows an encrypted CWT with a single recipient and a
11551155+ full CWT Claims Set.
11561156+11571157+ The encryption is done with AES-CCM mode using the 128-bit symmetric
11581158+ key from Appendix A.2.1 with a 64-bit tag and 13-byte nonce, i.e.,
11591159+ COSE AES-CCM-16-64-128. Line breaks are for display purposes only.
11601160+11611161+ d08343a1010aa2044c53796d6d6574726963313238054d99a0d7846e762c49ff
11621162+ e8a63e0b5858b918a11fd81e438b7f973d9e2e119bcb22424ba0f38a80f27562
11631163+ f400ee1d0d6c0fdb559c02421fd384fc2ebe22d7071378b0ea7428fff157444d
11641164+ 45f7e6afcda1aae5f6495830c58627087fc5b4974f319a8707a635dd643b
11651165+11661166+ Figure 14: Encrypted CWT as Hex String
11671167+11681168+11691169+11701170+11711171+11721172+11731173+11741174+11751175+11761176+11771177+11781178+Jones, et al. Standards Track [Page 21]
11791179+11801180+RFC 8392 CBOR Web Token May 2018
11811181+11821182+11831183+ 16(
11841184+ [
11851185+ / protected / << {
11861186+ / alg / 1: 10 / AES-CCM-16-64-128 /
11871187+ } >>,
11881188+ / unprotected / {
11891189+ / kid / 4: h'53796d6d6574726963313238' / 'Symmetric128' /,
11901190+ / iv / 5: h'99a0d7846e762c49ffe8a63e0b'
11911191+ },
11921192+ / ciphertext / h'b918a11fd81e438b7f973d9e2e119bcb22424ba0f38
11931193+ a80f27562f400ee1d0d6c0fdb559c02421fd384fc2e
11941194+ be22d7071378b0ea7428fff157444d45f7e6afcda1a
11951195+ ae5f6495830c58627087fc5b4974f319a8707a635dd
11961196+ 643b'
11971197+ ]
11981198+ )
11991199+12001200+ Figure 15: Encrypted CWT in CBOR Diagnostic Notation
12011201+12021202+A.6. Example Nested CWT
12031203+12041204+ This section shows a Nested CWT, signed and then encrypted, with a
12051205+ single recipient and a full CWT Claims Set.
12061206+12071207+ The signature is generated using the private ECDSA key from
12081208+ Appendix A.2.3, and it can be validated using the public ECDSA parts
12091209+ from Appendix A.2.3. The encryption is done with AES-CCM mode using
12101210+ the 128-bit symmetric key from Appendix A.2.1 with a 64-bit tag and
12111211+ 13-byte nonce, i.e., COSE AES-CCM-16-64-128. The content type is set
12121212+ to CWT to indicate that there are multiple layers of COSE protection
12131213+ before finding the CWT Claims Set. The decrypted ciphertext will be a
12141214+ COSE_sign1 structure. In this example, it is the same one as in
12151215+ Appendix A.3, i.e., a Signed CWT Claims Set. Note that there is no
12161216+ limitation to the number of layers; this is an example with two
12171217+ layers. Line breaks are for display purposes only.
12181218+12191219+ d08343a1010aa2044c53796d6d6574726963313238054d4a0694c0e69ee6b595
12201220+ 6655c7b258b7f6b0914f993de822cc47e5e57a188d7960b528a747446fe12f0e
12211221+ 7de05650dec74724366763f167a29c002dfd15b34d8993391cf49bc91127f545
12221222+ dba8703d66f5b7f1ae91237503d371e6333df9708d78c4fb8a8386c8ff09dc49
12231223+ af768b23179deab78d96490a66d5724fb33900c60799d9872fac6da3bdb89043
12241224+ d67c2a05414ce331b5b8f1ed8ff7138f45905db2c4d5bc8045ab372bff142631
12251225+ 610a7e0f677b7e9b0bc73adefdcee16d9d5d284c616abeab5d8c291ce0
12261226+12271227+ Figure 16: Signed and Encrypted CWT as Hex String
12281228+12291229+12301230+12311231+12321232+12331233+12341234+Jones, et al. Standards Track [Page 22]
12351235+12361236+RFC 8392 CBOR Web Token May 2018
12371237+12381238+12391239+ 16(
12401240+ [
12411241+ / protected / << {
12421242+ / alg / 1: 10 / AES-CCM-16-64-128 /
12431243+ } >>,
12441244+ / unprotected / {
12451245+ / kid / 4: h'53796d6d6574726963313238' / 'Symmetric128' /,
12461246+ / iv / 5: h'4a0694c0e69ee6b5956655c7b2'
12471247+ },
12481248+ / ciphertext / h'f6b0914f993de822cc47e5e57a188d7960b528a7474
12491249+ 46fe12f0e7de05650dec74724366763f167a29c002d
12501250+ fd15b34d8993391cf49bc91127f545dba8703d66f5b
12511251+ 7f1ae91237503d371e6333df9708d78c4fb8a8386c8
12521252+ ff09dc49af768b23179deab78d96490a66d5724fb33
12531253+ 900c60799d9872fac6da3bdb89043d67c2a05414ce3
12541254+ 31b5b8f1ed8ff7138f45905db2c4d5bc8045ab372bf
12551255+ f142631610a7e0f677b7e9b0bc73adefdcee16d9d5d
12561256+ 284c616abeab5d8c291ce0'
12571257+ ]
12581258+ )
12591259+12601260+ Figure 17: Signed and Encrypted CWT in CBOR Diagnostic Notation
12611261+12621262+A.7. Example MACed CWT with a Floating-Point Value
12631263+12641264+ This section shows a MACed CWT with a single recipient and a simple
12651265+ CWT Claims Set. The CWT Claims Set with a floating-point 'iat' value.
12661266+12671267+ The MAC is generated using the 256-bit symmetric key from
12681268+ Appendix A.2.2 with a 64-bit truncation. Line breaks are for display
12691269+ purposes only.
12701270+12711271+ d18443a10104a1044c53796d6d65747269633235364ba106fb41d584367c2000
12721272+ 0048b8816f34c0542892
12731273+12741274+ Figure 18: MACed CWT with a Floating-Point Value as Hex String
12751275+12761276+12771277+12781278+12791279+12801280+12811281+12821282+12831283+12841284+12851285+12861286+12871287+12881288+12891289+12901290+Jones, et al. Standards Track [Page 23]
12911291+12921292+RFC 8392 CBOR Web Token May 2018
12931293+12941294+12951295+ 17(
12961296+ [
12971297+ / protected / << {
12981298+ / alg / 1: 4 / HMAC-256-64 /
12991299+ } >>,
13001300+ / unprotected / {
13011301+ / kid / 4: h'53796d6d6574726963323536' / 'Symmetric256' /,
13021302+ },
13031303+ / payload / << {
13041304+ / iat / 6: 1443944944.5
13051305+ } >>,
13061306+ / tag / h'b8816f34c0542892'
13071307+ ]
13081308+ )
13091309+13101310+ Figure 19: MACed CWT with a Floating-Point Value
13111311+ in CBOR Diagnostic Notation
13121312+13131313+Acknowledgements
13141314+13151315+ This specification is based on JSON Web Token (JWT) [RFC7519], the
13161316+ authors of which also include Nat Sakimura and John Bradley. It also
13171317+ incorporates suggestions made by many people, including Carsten
13181318+ Bormann, Alissa Cooper, Esko Dijk, Benjamin Kaduk, Warren Kumari,
13191319+ Carlos Martinez, Alexey Melnikov, Kathleen Moriarty, Eric Rescorla,
13201320+ Dan Romascanu, Adam Roach, Kyle Rose, Jim Schaad, Ludwig Seitz, and
13211321+ Goeran Selander.
13221322+13231323+13241324+13251325+13261326+13271327+13281328+13291329+13301330+13311331+13321332+13331333+13341334+13351335+13361336+13371337+13381338+13391339+13401340+13411341+13421342+13431343+13441344+13451345+13461346+Jones, et al. Standards Track [Page 24]
13471347+13481348+RFC 8392 CBOR Web Token May 2018
13491349+13501350+13511351+Authors' Addresses
13521352+13531353+ Michael B. Jones
13541354+ Microsoft
13551355+13561356+ Email: mbj@microsoft.com
13571357+ URI: http://self-issued.info/
13581358+13591359+13601360+ Erik Wahlstroem
13611361+ Sweden
13621362+13631363+ Email: erik@wahlstromstekniska.se
13641364+13651365+ Samuel Erdtman
13661366+ Spotify AB
13671367+ Birger Jarlsgatan 61, 4tr
13681368+ Stockholm 113 56
13691369+ Sweden
13701370+13711371+ Phone: +46702691499
13721372+ Email: erdtman@spotify.com
13731373+13741374+13751375+ Hannes Tschofenig
13761376+ ARM Ltd.
13771377+ Hall in Tirol 6060
13781378+ Austria
13791379+13801380+ Email: Hannes.Tschofenig@arm.com
13811381+13821382+13831383+13841384+13851385+13861386+13871387+13881388+13891389+13901390+13911391+13921392+13931393+13941394+13951395+13961396+13971397+13981398+13991399+14001400+14011401+14021402+Jones, et al. Standards Track [Page 25]
14031403+
+3674
spec/rfc8949.txt
···11+
22+33+44+55+Internet Engineering Task Force (IETF) C. Bormann
66+Request for Comments: 8949 Universität Bremen TZI
77+STD: 94 P. Hoffman
88+Obsoletes: 7049 ICANN
99+Category: Standards Track December 2020
1010+ISSN: 2070-1721
1111+1212+1313+ Concise Binary Object Representation (CBOR)
1414+1515+Abstract
1616+1717+ The Concise Binary Object Representation (CBOR) is a data format
1818+ whose design goals include the possibility of extremely small code
1919+ size, fairly small message size, and extensibility without the need
2020+ for version negotiation. These design goals make it different from
2121+ earlier binary serializations such as ASN.1 and MessagePack.
2222+2323+ This document obsoletes RFC 7049, providing editorial improvements,
2424+ new details, and errata fixes while keeping full compatibility with
2525+ the interchange format of RFC 7049. It does not create a new version
2626+ of the format.
2727+2828+Status of This Memo
2929+3030+ This is an Internet Standards Track document.
3131+3232+ This document is a product of the Internet Engineering Task Force
3333+ (IETF). It represents the consensus of the IETF community. It has
3434+ received public review and has been approved for publication by the
3535+ Internet Engineering Steering Group (IESG). Further information on
3636+ Internet Standards is available in Section 2 of RFC 7841.
3737+3838+ Information about the current status of this document, any errata,
3939+ and how to provide feedback on it may be obtained at
4040+ https://www.rfc-editor.org/info/rfc8949.
4141+4242+Copyright Notice
4343+4444+ Copyright (c) 2020 IETF Trust and the persons identified as the
4545+ document authors. All rights reserved.
4646+4747+ This document is subject to BCP 78 and the IETF Trust's Legal
4848+ Provisions Relating to IETF Documents
4949+ (https://trustee.ietf.org/license-info) in effect on the date of
5050+ publication of this document. Please review these documents
5151+ carefully, as they describe your rights and restrictions with respect
5252+ to this document. Code Components extracted from this document must
5353+ include Simplified BSD License text as described in Section 4.e of
5454+ the Trust Legal Provisions and are provided without warranty as
5555+ described in the Simplified BSD License.
5656+5757+Table of Contents
5858+5959+ 1. Introduction
6060+ 1.1. Objectives
6161+ 1.2. Terminology
6262+ 2. CBOR Data Models
6363+ 2.1. Extended Generic Data Models
6464+ 2.2. Specific Data Models
6565+ 3. Specification of the CBOR Encoding
6666+ 3.1. Major Types
6767+ 3.2. Indefinite Lengths for Some Major Types
6868+ 3.2.1. The "break" Stop Code
6969+ 3.2.2. Indefinite-Length Arrays and Maps
7070+ 3.2.3. Indefinite-Length Byte Strings and Text Strings
7171+ 3.2.4. Summary of Indefinite-Length Use of Major Types
7272+ 3.3. Floating-Point Numbers and Values with No Content
7373+ 3.4. Tagging of Items
7474+ 3.4.1. Standard Date/Time String
7575+ 3.4.2. Epoch-Based Date/Time
7676+ 3.4.3. Bignums
7777+ 3.4.4. Decimal Fractions and Bigfloats
7878+ 3.4.5. Content Hints
7979+ 3.4.5.1. Encoded CBOR Data Item
8080+ 3.4.5.2. Expected Later Encoding for CBOR-to-JSON Converters
8181+ 3.4.5.3. Encoded Text
8282+ 3.4.6. Self-Described CBOR
8383+ 4. Serialization Considerations
8484+ 4.1. Preferred Serialization
8585+ 4.2. Deterministically Encoded CBOR
8686+ 4.2.1. Core Deterministic Encoding Requirements
8787+ 4.2.2. Additional Deterministic Encoding Considerations
8888+ 4.2.3. Length-First Map Key Ordering
8989+ 5. Creating CBOR-Based Protocols
9090+ 5.1. CBOR in Streaming Applications
9191+ 5.2. Generic Encoders and Decoders
9292+ 5.3. Validity of Items
9393+ 5.3.1. Basic validity
9494+ 5.3.2. Tag validity
9595+ 5.4. Validity and Evolution
9696+ 5.5. Numbers
9797+ 5.6. Specifying Keys for Maps
9898+ 5.6.1. Equivalence of Keys
9999+ 5.7. Undefined Values
100100+ 6. Converting Data between CBOR and JSON
101101+ 6.1. Converting from CBOR to JSON
102102+ 6.2. Converting from JSON to CBOR
103103+ 7. Future Evolution of CBOR
104104+ 7.1. Extension Points
105105+ 7.2. Curating the Additional Information Space
106106+ 8. Diagnostic Notation
107107+ 8.1. Encoding Indicators
108108+ 9. IANA Considerations
109109+ 9.1. CBOR Simple Values Registry
110110+ 9.2. CBOR Tags Registry
111111+ 9.3. Media Types Registry
112112+ 9.4. CoAP Content-Format Registry
113113+ 9.5. Structured Syntax Suffix Registry
114114+ 10. Security Considerations
115115+ 11. References
116116+ 11.1. Normative References
117117+ 11.2. Informative References
118118+ Appendix A. Examples of Encoded CBOR Data Items
119119+ Appendix B. Jump Table for Initial Byte
120120+ Appendix C. Pseudocode
121121+ Appendix D. Half-Precision
122122+ Appendix E. Comparison of Other Binary Formats to CBOR's Design
123123+ Objectives
124124+ E.1. ASN.1 DER, BER, and PER
125125+ E.2. MessagePack
126126+ E.3. BSON
127127+ E.4. MSDTP: RFC 713
128128+ E.5. Conciseness on the Wire
129129+ Appendix F. Well-Formedness Errors and Examples
130130+ F.1. Examples of CBOR Data Items That Are Not Well-Formed
131131+ Appendix G. Changes from RFC 7049
132132+ G.1. Errata Processing and Clerical Changes
133133+ G.2. Changes in IANA Considerations
134134+ G.3. Changes in Suggestions and Other Informational Components
135135+ Acknowledgements
136136+ Authors' Addresses
137137+138138+1. Introduction
139139+140140+ There are hundreds of standardized formats for binary representation
141141+ of structured data (also known as binary serialization formats). Of
142142+ those, some are for specific domains of information, while others are
143143+ generalized for arbitrary data. In the IETF, probably the best-known
144144+ formats in the latter category are ASN.1's BER and DER [ASN.1].
145145+146146+ The format defined here follows some specific design goals that are
147147+ not well met by current formats. The underlying data model is an
148148+ extended version of the JSON data model [RFC8259]. It is important
149149+ to note that this is not a proposal that the grammar in RFC 8259 be
150150+ extended in general, since doing so would cause a significant
151151+ backwards incompatibility with already deployed JSON documents.
152152+ Instead, this document simply defines its own data model that starts
153153+ from JSON.
154154+155155+ Appendix E lists some existing binary formats and discusses how well
156156+ they do or do not fit the design objectives of the Concise Binary
157157+ Object Representation (CBOR).
158158+159159+ This document obsoletes [RFC7049], providing editorial improvements,
160160+ new details, and errata fixes while keeping full compatibility with
161161+ the interchange format of RFC 7049. It does not create a new version
162162+ of the format.
163163+164164+1.1. Objectives
165165+166166+ The objectives of CBOR, roughly in decreasing order of importance,
167167+ are:
168168+169169+ 1. The representation must be able to unambiguously encode most
170170+ common data formats used in Internet standards.
171171+172172+ * It must represent a reasonable set of basic data types and
173173+ structures using binary encoding. "Reasonable" here is
174174+ largely influenced by the capabilities of JSON, with the major
175175+ addition of binary byte strings. The structures supported are
176176+ limited to arrays and trees; loops and lattice-style graphs
177177+ are not supported.
178178+179179+ * There is no requirement that all data formats be uniquely
180180+ encoded; that is, it is acceptable that the number "7" might
181181+ be encoded in multiple different ways.
182182+183183+ 2. The code for an encoder or decoder must be able to be compact in
184184+ order to support systems with very limited memory, processor
185185+ power, and instruction sets.
186186+187187+ * An encoder and a decoder need to be implementable in a very
188188+ small amount of code (for example, in class 1 constrained
189189+ nodes as defined in [RFC7228]).
190190+191191+ * The format should use contemporary machine representations of
192192+ data (for example, not requiring binary-to-decimal
193193+ conversion).
194194+195195+ 3. Data must be able to be decoded without a schema description.
196196+197197+ * Similar to JSON, encoded data should be self-describing so
198198+ that a generic decoder can be written.
199199+200200+ 4. The serialization must be reasonably compact, but data
201201+ compactness is secondary to code compactness for the encoder and
202202+ decoder.
203203+204204+ * "Reasonable" here is bounded by JSON as an upper bound in size
205205+ and by the implementation complexity, which limits the amount
206206+ of effort that can go into achieving that compactness. Using
207207+ either general compression schemes or extensive bit-fiddling
208208+ violates the complexity goals.
209209+210210+ 5. The format must be applicable to both constrained nodes and high-
211211+ volume applications.
212212+213213+ * This means it must be reasonably frugal in CPU usage for both
214214+ encoding and decoding. This is relevant both for constrained
215215+ nodes and for potential usage in applications with a very high
216216+ volume of data.
217217+218218+ 6. The format must support all JSON data types for conversion to and
219219+ from JSON.
220220+221221+ * It must support a reasonable level of conversion as long as
222222+ the data represented is within the capabilities of JSON. It
223223+ must be possible to define a unidirectional mapping towards
224224+ JSON for all types of data.
225225+226226+ 7. The format must be extensible, and the extended data must be
227227+ decodable by earlier decoders.
228228+229229+ * The format is designed for decades of use.
230230+231231+ * The format must support a form of extensibility that allows
232232+ fallback so that a decoder that does not understand an
233233+ extension can still decode the message.
234234+235235+ * The format must be able to be extended in the future by later
236236+ IETF standards.
237237+238238+1.2. Terminology
239239+240240+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
241241+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
242242+ "OPTIONAL" in this document are to be interpreted as described in
243243+ BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all
244244+ capitals, as shown here.
245245+246246+ The term "byte" is used in its now-customary sense as a synonym for
247247+ "octet". All multi-byte values are encoded in network byte order
248248+ (that is, most significant byte first, also known as "big-endian").
249249+250250+ This specification makes use of the following terminology:
251251+252252+ Data item: A single piece of CBOR data. The structure of a data
253253+ item may contain zero, one, or more nested data items. The term
254254+ is used both for the data item in representation format and for
255255+ the abstract idea that can be derived from that by a decoder; the
256256+ former can be addressed specifically by using the term "encoded
257257+ data item".
258258+259259+ Decoder: A process that decodes a well-formed encoded CBOR data item
260260+ and makes it available to an application. Formally speaking, a
261261+ decoder contains a parser to break up the input using the syntax
262262+ rules of CBOR, as well as a semantic processor to prepare the data
263263+ in a form suitable to the application.
264264+265265+ Encoder: A process that generates the (well-formed) representation
266266+ format of a CBOR data item from application information.
267267+268268+ Data Stream: A sequence of zero or more data items, not further
269269+ assembled into a larger containing data item (see [RFC8742] for
270270+ one application). The independent data items that make up a data
271271+ stream are sometimes also referred to as "top-level data items".
272272+273273+ Well-formed: A data item that follows the syntactic structure of
274274+ CBOR. A well-formed data item uses the initial bytes and the byte
275275+ strings and/or data items that are implied by their values as
276276+ defined in CBOR and does not include following extraneous data.
277277+ CBOR decoders by definition only return contents from well-formed
278278+ data items.
279279+280280+ Valid: A data item that is well-formed and also follows the semantic
281281+ restrictions that apply to CBOR data items (Section 5.3).
282282+283283+ Expected: Besides its normal English meaning, the term "expected" is
284284+ used to describe requirements beyond CBOR validity that an
285285+ application has on its input data. Well-formed (processable at
286286+ all), valid (checked by a validity-checking generic decoder), and
287287+ expected (checked by the application) form a hierarchy of layers
288288+ of acceptability.
289289+290290+ Stream decoder: A process that decodes a data stream and makes each
291291+ of the data items in the sequence available to an application as
292292+ they are received.
293293+294294+ Terms and concepts for floating-point values such as Infinity, NaN
295295+ (not a number), negative zero, and subnormal are defined in
296296+ [IEEE754].
297297+298298+ Where bit arithmetic or data types are explained, this document uses
299299+ the notation familiar from the programming language C [C], except
300300+ that ".." denotes a range that includes both ends given, and
301301+ superscript notation denotes exponentiation. For example, 2 to the
302302+ power of 64 is notated: 2^(64). In the plain-text version of this
303303+ specification, superscript notation is not available and therefore is
304304+ rendered by a surrogate notation. That notation is not optimized for
305305+ this RFC; it is unfortunately ambiguous with C's exclusive-or (which
306306+ is only used in the appendices, which in turn do not use
307307+ exponentiation) and requires circumspection from the reader of the
308308+ plain-text version.
309309+310310+ Examples and pseudocode assume that signed integers use two's
311311+ complement representation and that right shifts of signed integers
312312+ perform sign extension; these assumptions are also specified in
313313+ Sections 6.8.1 (basic.fundamental) and 7.6.7 (expr.shift) of the 2020
314314+ version of C++ (currently available as a final draft, [Cplusplus20]).
315315+316316+ Similar to the "0x" notation for hexadecimal numbers, numbers in
317317+ binary notation are prefixed with "0b". Underscores can be added to
318318+ a number solely for readability, so 0b00100001 (0x21) might be
319319+ written 0b001_00001 to emphasize the desired interpretation of the
320320+ bits in the byte; in this case, it is split into three bits and five
321321+ bits. Encoded CBOR data items are sometimes given in the "0x" or
322322+ "0b" notation; these values are first interpreted as numbers as in C
323323+ and are then interpreted as byte strings in network byte order,
324324+ including any leading zero bytes expressed in the notation.
325325+326326+ Words may be _italicized_ for emphasis; in the plain text form of
327327+ this specification, this is indicated by surrounding words with
328328+ underscore characters. Verbatim text (e.g., names from a programming
329329+ language) may be set in "monospace" type; in plain text, this is
330330+ approximated somewhat ambiguously by surrounding the text in double
331331+ quotes (which also retain their usual meaning).
332332+333333+2. CBOR Data Models
334334+335335+ CBOR is explicit about its generic data model, which defines the set
336336+ of all data items that can be represented in CBOR. Its basic generic
337337+ data model is extensible by the registration of "simple values" and
338338+ tags. Applications can then create a subset of the resulting
339339+ extended generic data model to build their specific data models.
340340+341341+ Within environments that can represent the data items in the generic
342342+ data model, generic CBOR encoders and decoders can be implemented
343343+ (which usually involves defining additional implementation data types
344344+ for those data items that do not already have a natural
345345+ representation in the environment). The ability to provide generic
346346+ encoders and decoders is an explicit design goal of CBOR; however,
347347+ many applications will provide their own application-specific
348348+ encoders and/or decoders.
349349+350350+ In the basic (unextended) generic data model defined in Section 3, a
351351+ data item is one of the following:
352352+353353+ * an integer in the range -2^(64)..2^(64)-1 inclusive
354354+355355+ * a simple value, identified by a number between 0 and 255, but
356356+ distinct from that number itself
357357+358358+ * a floating-point value, distinct from an integer, out of the set
359359+ representable by IEEE 754 binary64 (including non-finites)
360360+ [IEEE754]
361361+362362+ * a sequence of zero or more bytes ("byte string")
363363+364364+ * a sequence of zero or more Unicode code points ("text string")
365365+366366+ * a sequence of zero or more data items ("array")
367367+368368+ * a mapping (mathematical function) from zero or more data items
369369+ ("keys") each to a data item ("values"), ("map")
370370+371371+ * a tagged data item ("tag"), comprising a tag number (an integer in
372372+ the range 0..2^(64)-1) and the tag content (a data item)
373373+374374+ Note that integer and floating-point values are distinct in this
375375+ model, even if they have the same numeric value.
376376+377377+ Also note that serialization variants are not visible at the generic
378378+ data model level. This deliberate absence of visibility includes the
379379+ number of bytes of the encoded floating-point value. It also
380380+ includes the choice of encoding for an "argument" (see Section 3)
381381+ such as the encoding for an integer, the encoding for the length of a
382382+ text or byte string, the encoding for the number of elements in an
383383+ array or pairs in a map, or the encoding for a tag number.
384384+385385+2.1. Extended Generic Data Models
386386+387387+ This basic generic data model has been extended in this document by
388388+ the registration of a number of simple values and tag numbers, such
389389+ as:
390390+391391+ * "false", "true", "null", and "undefined" (simple values identified
392392+ by 20..23, Section 3.3)
393393+394394+ * integer and floating-point values with a larger range and
395395+ precision than the above (tag numbers 2 to 5, Section 3.4)
396396+397397+ * application data types such as a point in time or date/time string
398398+ defined in RFC 3339 (tag numbers 1 and 0, Section 3.4)
399399+400400+ Additional elements of the extended generic data model can be (and
401401+ have been) defined via the IANA registries created for CBOR. Even if
402402+ such an extension is unknown to a generic encoder or decoder, data
403403+ items using that extension can be passed to or from the application
404404+ by representing them at the application interface within the basic
405405+ generic data model, i.e., as generic simple values or generic tags.
406406+407407+ In other words, the basic generic data model is stable as defined in
408408+ this document, while the extended generic data model expands by the
409409+ registration of new simple values or tag numbers, but never shrinks.
410410+411411+ While there is a strong expectation that generic encoders and
412412+ decoders can represent "false", "true", and "null" ("undefined" is
413413+ intentionally omitted) in the form appropriate for their programming
414414+ environment, the implementation of the data model extensions created
415415+ by tags is truly optional and a matter of implementation quality.
416416+417417+2.2. Specific Data Models
418418+419419+ The specific data model for a CBOR-based protocol usually takes a
420420+ subset of the extended generic data model and assigns application
421421+ semantics to the data items within this subset and its components.
422422+ When documenting such specific data models and specifying the types
423423+ of data items, it is preferable to identify the types by their
424424+ generic data model names ("negative integer", "array") instead of
425425+ referring to aspects of their CBOR representation ("major type 1",
426426+ "major type 4").
427427+428428+ Specific data models can also specify value equivalency (including
429429+ values of different types) for the purposes of map keys and encoder
430430+ freedom. For example, in the generic data model, a valid map MAY
431431+ have both "0" and "0.0" as keys, and an encoder MUST NOT encode "0.0"
432432+ as an integer (major type 0, Section 3.1). However, if a specific
433433+ data model declares that floating-point and integer representations
434434+ of integral values are equivalent, using both map keys "0" and "0.0"
435435+ in a single map would be considered duplicates, even while encoded as
436436+ different major types, and so invalid; and an encoder could encode
437437+ integral-valued floats as integers or vice versa, perhaps to save
438438+ encoded bytes.
439439+440440+3. Specification of the CBOR Encoding
441441+442442+ A CBOR data item (Section 2) is encoded to or decoded from a byte
443443+ string carrying a well-formed encoded data item as described in this
444444+ section. The encoding is summarized in Table 7 in Appendix B,
445445+ indexed by the initial byte. An encoder MUST produce only well-
446446+ formed encoded data items. A decoder MUST NOT return a decoded data
447447+ item when it encounters input that is not a well-formed encoded CBOR
448448+ data item (this does not detract from the usefulness of diagnostic
449449+ and recovery tools that might make available some information from a
450450+ damaged encoded CBOR data item).
451451+452452+ The initial byte of each encoded data item contains both information
453453+ about the major type (the high-order 3 bits, described in
454454+ Section 3.1) and additional information (the low-order 5 bits). With
455455+ a few exceptions, the additional information's value describes how to
456456+ load an unsigned integer "argument":
457457+458458+ Less than 24: The argument's value is the value of the additional
459459+ information.
460460+461461+ 24, 25, 26, or 27: The argument's value is held in the following 1,
462462+ 2, 4, or 8 bytes, respectively, in network byte order. For major
463463+ type 7 and additional information value 25, 26, 27, these bytes
464464+ are not used as an integer argument, but as a floating-point value
465465+ (see Section 3.3).
466466+467467+ 28, 29, 30: These values are reserved for future additions to the
468468+ CBOR format. In the present version of CBOR, the encoded item is
469469+ not well-formed.
470470+471471+ 31: No argument value is derived. If the major type is 0, 1, or 6,
472472+ the encoded item is not well-formed. For major types 2 to 5, the
473473+ item's length is indefinite, and for major type 7, the byte does
474474+ not constitute a data item at all but terminates an indefinite-
475475+ length item; all are described in Section 3.2.
476476+477477+ The initial byte and any additional bytes consumed to construct the
478478+ argument are collectively referred to as the _head_ of the data item.
479479+480480+ The meaning of this argument depends on the major type. For example,
481481+ in major type 0, the argument is the value of the data item itself
482482+ (and in major type 1, the value of the data item is computed from the
483483+ argument); in major type 2 and 3, it gives the length of the string
484484+ data in bytes that follow; and in major types 4 and 5, it is used to
485485+ determine the number of data items enclosed.
486486+487487+ If the encoded sequence of bytes ends before the end of a data item,
488488+ that item is not well-formed. If the encoded sequence of bytes still
489489+ has bytes remaining after the outermost encoded item is decoded, that
490490+ encoding is not a single well-formed CBOR item. Depending on the
491491+ application, the decoder may either treat the encoding as not well-
492492+ formed or just identify the start of the remaining bytes to the
493493+ application.
494494+495495+ A CBOR decoder implementation can be based on a jump table with all
496496+ 256 defined values for the initial byte (Table 7). A decoder in a
497497+ constrained implementation can instead use the structure of the
498498+ initial byte and following bytes for more compact code (see
499499+ Appendix C for a rough impression of how this could look).
500500+501501+3.1. Major Types
502502+503503+ The following lists the major types and the additional information
504504+ and other bytes associated with the type.
505505+506506+ Major type 0:
507507+ An unsigned integer in the range 0..2^(64)-1 inclusive. The value
508508+ of the encoded item is the argument itself. For example, the
509509+ integer 10 is denoted as the one byte 0b000_01010 (major type 0,
510510+ additional information 10). The integer 500 would be 0b000_11001
511511+ (major type 0, additional information 25) followed by the two
512512+ bytes 0x01f4, which is 500 in decimal.
513513+514514+ Major type 1:
515515+ A negative integer in the range -2^(64)..-1 inclusive. The value
516516+ of the item is -1 minus the argument. For example, the integer
517517+ -500 would be 0b001_11001 (major type 1, additional information
518518+ 25) followed by the two bytes 0x01f3, which is 499 in decimal.
519519+520520+ Major type 2:
521521+ A byte string. The number of bytes in the string is equal to the
522522+ argument. For example, a byte string whose length is 5 would have
523523+ an initial byte of 0b010_00101 (major type 2, additional
524524+ information 5 for the length), followed by 5 bytes of binary
525525+ content. A byte string whose length is 500 would have 3 initial
526526+ bytes of 0b010_11001 (major type 2, additional information 25 to
527527+ indicate a two-byte length) followed by the two bytes 0x01f4 for a
528528+ length of 500, followed by 500 bytes of binary content.
529529+530530+ Major type 3:
531531+ A text string (Section 2) encoded as UTF-8 [RFC3629]. The number
532532+ of bytes in the string is equal to the argument. A string
533533+ containing an invalid UTF-8 sequence is well-formed but invalid
534534+ (Section 1.2). This type is provided for systems that need to
535535+ interpret or display human-readable text, and allows the
536536+ differentiation between unstructured bytes and text that has a
537537+ specified repertoire (that of Unicode) and encoding (UTF-8). In
538538+ contrast to formats such as JSON, the Unicode characters in this
539539+ type are never escaped. Thus, a newline character (U+000A) is
540540+ always represented in a string as the byte 0x0a, and never as the
541541+ bytes 0x5c6e (the characters "\" and "n") nor as 0x5c7530303061
542542+ (the characters "\", "u", "0", "0", "0", and "a").
543543+544544+ Major type 4:
545545+ An array of data items. In other formats, arrays are also called
546546+ lists, sequences, or tuples (a "CBOR sequence" is something
547547+ slightly different, though [RFC8742]). The argument is the number
548548+ of data items in the array. Items in an array do not need to all
549549+ be of the same type. For example, an array that contains 10 items
550550+ of any type would have an initial byte of 0b100_01010 (major type
551551+ 4, additional information 10 for the length) followed by the 10
552552+ remaining items.
553553+554554+ Major type 5:
555555+ A map of pairs of data items. Maps are also called tables,
556556+ dictionaries, hashes, or objects (in JSON). A map is comprised of
557557+ pairs of data items, each pair consisting of a key that is
558558+ immediately followed by a value. The argument is the number of
559559+ _pairs_ of data items in the map. For example, a map that
560560+ contains 9 pairs would have an initial byte of 0b101_01001 (major
561561+ type 5, additional information 9 for the number of pairs) followed
562562+ by the 18 remaining items. The first item is the first key, the
563563+ second item is the first value, the third item is the second key,
564564+ and so on. Because items in a map come in pairs, their total
565565+ number is always even: a map that contains an odd number of items
566566+ (no value data present after the last key data item) is not well-
567567+ formed. A map that has duplicate keys may be well-formed, but it
568568+ is not valid, and thus it causes indeterminate decoding; see also
569569+ Section 5.6.
570570+571571+ Major type 6:
572572+ A tagged data item ("tag") whose tag number, an integer in the
573573+ range 0..2^(64)-1 inclusive, is the argument and whose enclosed
574574+ data item (_tag content_) is the single encoded data item that
575575+ follows the head. See Section 3.4.
576576+577577+ Major type 7:
578578+ Floating-point numbers and simple values, as well as the "break"
579579+ stop code. See Section 3.3.
580580+581581+ These eight major types lead to a simple table showing which of the
582582+ 256 possible values for the initial byte of a data item are used
583583+ (Table 7).
584584+585585+ In major types 6 and 7, many of the possible values are reserved for
586586+ future specification. See Section 9 for more information on these
587587+ values.
588588+589589+ Table 1 summarizes the major types defined by CBOR, ignoring
590590+ Section 3.2 for now. The number N in this table stands for the
591591+ argument.
592592+593593+ +============+=======================+=========================+
594594+ | Major Type | Meaning | Content |
595595+ +============+=======================+=========================+
596596+ | 0 | unsigned integer N | - |
597597+ +------------+-----------------------+-------------------------+
598598+ | 1 | negative integer -1-N | - |
599599+ +------------+-----------------------+-------------------------+
600600+ | 2 | byte string | N bytes |
601601+ +------------+-----------------------+-------------------------+
602602+ | 3 | text string | N bytes (UTF-8 text) |
603603+ +------------+-----------------------+-------------------------+
604604+ | 4 | array | N data items (elements) |
605605+ +------------+-----------------------+-------------------------+
606606+ | 5 | map | 2N data items (key/ |
607607+ | | | value pairs) |
608608+ +------------+-----------------------+-------------------------+
609609+ | 6 | tag of number N | 1 data item |
610610+ +------------+-----------------------+-------------------------+
611611+ | 7 | simple/float | - |
612612+ +------------+-----------------------+-------------------------+
613613+614614+ Table 1: Overview over the Definite-Length Use of CBOR Major
615615+ Types (N = Argument)
616616+617617+3.2. Indefinite Lengths for Some Major Types
618618+619619+ Four CBOR items (arrays, maps, byte strings, and text strings) can be
620620+ encoded with an indefinite length using additional information value
621621+ 31. This is useful if the encoding of the item needs to begin before
622622+ the number of items inside the array or map, or the total length of
623623+ the string, is known. (The ability to start sending a data item
624624+ before all of it is known is often referred to as "streaming" within
625625+ that data item.)
626626+627627+ Indefinite-length arrays and maps are dealt with differently than
628628+ indefinite-length strings (byte strings and text strings).
629629+630630+3.2.1. The "break" Stop Code
631631+632632+ The "break" stop code is encoded with major type 7 and additional
633633+ information value 31 (0b111_11111). It is not itself a data item: it
634634+ is just a syntactic feature to close an indefinite-length item.
635635+636636+ If the "break" stop code appears where a data item is expected, other
637637+ than directly inside an indefinite-length string, array, or map --
638638+ for example, directly inside a definite-length array or map -- the
639639+ enclosing item is not well-formed.
640640+641641+3.2.2. Indefinite-Length Arrays and Maps
642642+643643+ Indefinite-length arrays and maps are represented using their major
644644+ type with the additional information value of 31, followed by an
645645+ arbitrary-length sequence of zero or more items for an array or key/
646646+ value pairs for a map, followed by the "break" stop code
647647+ (Section 3.2.1). In other words, indefinite-length arrays and maps
648648+ look identical to other arrays and maps except for beginning with the
649649+ additional information value of 31 and ending with the "break" stop
650650+ code.
651651+652652+ If the "break" stop code appears after a key in a map, in place of
653653+ that key's value, the map is not well-formed.
654654+655655+ There is no restriction against nesting indefinite-length array or
656656+ map items. A "break" only terminates a single item, so nested
657657+ indefinite-length items need exactly as many "break" stop codes as
658658+ there are type bytes starting an indefinite-length item.
659659+660660+ For example, assume an encoder wants to represent the abstract array
661661+ [1, [2, 3], [4, 5]]. The definite-length encoding would be
662662+ 0x8301820203820405:
663663+664664+ 83 -- Array of length 3
665665+ 01 -- 1
666666+ 82 -- Array of length 2
667667+ 02 -- 2
668668+ 03 -- 3
669669+ 82 -- Array of length 2
670670+ 04 -- 4
671671+ 05 -- 5
672672+673673+ Indefinite-length encoding could be applied independently to each of
674674+ the three arrays encoded in this data item, as required, leading to
675675+ representations such as:
676676+677677+ 0x9f018202039f0405ffff
678678+ 9F -- Start indefinite-length array
679679+ 01 -- 1
680680+ 82 -- Array of length 2
681681+ 02 -- 2
682682+ 03 -- 3
683683+ 9F -- Start indefinite-length array
684684+ 04 -- 4
685685+ 05 -- 5
686686+ FF -- "break" (inner array)
687687+ FF -- "break" (outer array)
688688+689689+ 0x9f01820203820405ff
690690+ 9F -- Start indefinite-length array
691691+ 01 -- 1
692692+ 82 -- Array of length 2
693693+ 02 -- 2
694694+ 03 -- 3
695695+ 82 -- Array of length 2
696696+ 04 -- 4
697697+ 05 -- 5
698698+ FF -- "break"
699699+700700+ 0x83018202039f0405ff
701701+ 83 -- Array of length 3
702702+ 01 -- 1
703703+ 82 -- Array of length 2
704704+ 02 -- 2
705705+ 03 -- 3
706706+ 9F -- Start indefinite-length array
707707+ 04 -- 4
708708+ 05 -- 5
709709+ FF -- "break"
710710+711711+ 0x83019f0203ff820405
712712+ 83 -- Array of length 3
713713+ 01 -- 1
714714+ 9F -- Start indefinite-length array
715715+ 02 -- 2
716716+ 03 -- 3
717717+ FF -- "break"
718718+ 82 -- Array of length 2
719719+ 04 -- 4
720720+ 05 -- 5
721721+722722+ An example of an indefinite-length map (that happens to have two key/
723723+ value pairs) might be:
724724+725725+ 0xbf6346756ef563416d7421ff
726726+ BF -- Start indefinite-length map
727727+ 63 -- First key, UTF-8 string length 3
728728+ 46756e -- "Fun"
729729+ F5 -- First value, true
730730+ 63 -- Second key, UTF-8 string length 3
731731+ 416d74 -- "Amt"
732732+ 21 -- Second value, -2
733733+ FF -- "break"
734734+735735+3.2.3. Indefinite-Length Byte Strings and Text Strings
736736+737737+ Indefinite-length strings are represented by a byte containing the
738738+ major type for byte string or text string with an additional
739739+ information value of 31, followed by a series of zero or more strings
740740+ of the specified type ("chunks") that have definite lengths, and
741741+ finished by the "break" stop code (Section 3.2.1). The data item
742742+ represented by the indefinite-length string is the concatenation of
743743+ the chunks. If no chunks are present, the data item is an empty
744744+ string of the specified type. Zero-length chunks, while not
745745+ particularly useful, are permitted.
746746+747747+ If any item between the indefinite-length string indicator
748748+ (0b010_11111 or 0b011_11111) and the "break" stop code is not a
749749+ definite-length string item of the same major type, the string is not
750750+ well-formed.
751751+752752+ The design does not allow nesting indefinite-length strings as chunks
753753+ into indefinite-length strings. If it were allowed, it would require
754754+ decoder implementations to keep a stack, or at least a count, of
755755+ nesting levels. It is unnecessary on the encoder side because the
756756+ inner indefinite-length string would consist of chunks, and these
757757+ could instead be put directly into the outer indefinite-length
758758+ string.
759759+760760+ If any definite-length text string inside an indefinite-length text
761761+ string is invalid, the indefinite-length text string is invalid.
762762+ Note that this implies that the UTF-8 bytes of a single Unicode code
763763+ point (scalar value) cannot be spread between chunks: a new chunk of
764764+ a text string can only be started at a code point boundary.
765765+766766+ For example, assume an encoded data item consisting of the bytes:
767767+768768+ 0b010_11111 0b010_00100 0xaabbccdd 0b010_00011 0xeeff99 0b111_11111
769769+ 5F -- Start indefinite-length byte string
770770+ 44 -- Byte string of length 4
771771+ aabbccdd -- Bytes content
772772+ 43 -- Byte string of length 3
773773+ eeff99 -- Bytes content
774774+ FF -- "break"
775775+776776+ After decoding, this results in a single byte string with seven
777777+ bytes: 0xaabbccddeeff99.
778778+779779+3.2.4. Summary of Indefinite-Length Use of Major Types
780780+781781+ Table 2 summarizes the major types defined by CBOR as used for
782782+ indefinite-length encoding (with additional information set to 31).
783783+784784+ +============+===================+==================================+
785785+ | Major Type | Meaning | Enclosed up to "break" Stop Code |
786786+ +============+===================+==================================+
787787+ | 0 | (not well- | - |
788788+ | | formed) | |
789789+ +------------+-------------------+----------------------------------+
790790+ | 1 | (not well- | - |
791791+ | | formed) | |
792792+ +------------+-------------------+----------------------------------+
793793+ | 2 | byte string | definite-length byte strings |
794794+ +------------+-------------------+----------------------------------+
795795+ | 3 | text string | definite-length text strings |
796796+ +------------+-------------------+----------------------------------+
797797+ | 4 | array | data items (elements) |
798798+ +------------+-------------------+----------------------------------+
799799+ | 5 | map | data items (key/value pairs) |
800800+ +------------+-------------------+----------------------------------+
801801+ | 6 | (not well- | - |
802802+ | | formed) | |
803803+ +------------+-------------------+----------------------------------+
804804+ | 7 | "break" stop | - |
805805+ | | code | |
806806+ +------------+-------------------+----------------------------------+
807807+808808+ Table 2: Overview of the Indefinite-Length Use of CBOR Major
809809+ Types (Additional Information = 31)
810810+811811+3.3. Floating-Point Numbers and Values with No Content
812812+813813+ Major type 7 is for two types of data: floating-point numbers and
814814+ "simple values" that do not need any content. Each value of the
815815+ 5-bit additional information in the initial byte has its own separate
816816+ meaning, as defined in Table 3. Like the major types for integers,
817817+ items of this major type do not carry content data; all the
818818+ information is in the initial bytes (the head).
819819+820820+ +=============+===================================================+
821821+ | 5-Bit Value | Semantics |
822822+ +=============+===================================================+
823823+ | 0..23 | Simple value (value 0..23) |
824824+ +-------------+---------------------------------------------------+
825825+ | 24 | Simple value (value 32..255 in following byte) |
826826+ +-------------+---------------------------------------------------+
827827+ | 25 | IEEE 754 Half-Precision Float (16 bits follow) |
828828+ +-------------+---------------------------------------------------+
829829+ | 26 | IEEE 754 Single-Precision Float (32 bits follow) |
830830+ +-------------+---------------------------------------------------+
831831+ | 27 | IEEE 754 Double-Precision Float (64 bits follow) |
832832+ +-------------+---------------------------------------------------+
833833+ | 28-30 | Reserved, not well-formed in the present document |
834834+ +-------------+---------------------------------------------------+
835835+ | 31 | "break" stop code for indefinite-length items |
836836+ | | (Section 3.2.1) |
837837+ +-------------+---------------------------------------------------+
838838+839839+ Table 3: Values for Additional Information in Major Type 7
840840+841841+ As with all other major types, the 5-bit value 24 signifies a single-
842842+ byte extension: it is followed by an additional byte to represent the
843843+ simple value. (To minimize confusion, only the values 32 to 255 are
844844+ used.) This maintains the structure of the initial bytes: as for the
845845+ other major types, the length of these always depends on the
846846+ additional information in the first byte. Table 4 lists the numeric
847847+ values assigned and available for simple values.
848848+849849+ +=========+==============+
850850+ | Value | Semantics |
851851+ +=========+==============+
852852+ | 0..19 | (unassigned) |
853853+ +---------+--------------+
854854+ | 20 | false |
855855+ +---------+--------------+
856856+ | 21 | true |
857857+ +---------+--------------+
858858+ | 22 | null |
859859+ +---------+--------------+
860860+ | 23 | undefined |
861861+ +---------+--------------+
862862+ | 24..31 | (reserved) |
863863+ +---------+--------------+
864864+ | 32..255 | (unassigned) |
865865+ +---------+--------------+
866866+867867+ Table 4: Simple Values
868868+869869+ An encoder MUST NOT issue two-byte sequences that start with 0xf8
870870+ (major type 7, additional information 24) and continue with a byte
871871+ less than 0x20 (32 decimal). Such sequences are not well-formed.
872872+ (This implies that an encoder cannot encode "false", "true", "null",
873873+ or "undefined" in two-byte sequences and that only the one-byte
874874+ variants of these are well-formed; more generally speaking, each
875875+ simple value only has a single representation variant).
876876+877877+ The 5-bit values of 25, 26, and 27 are for 16-bit, 32-bit, and 64-bit
878878+ IEEE 754 binary floating-point values [IEEE754]. These floating-
879879+ point values are encoded in the additional bytes of the appropriate
880880+ size. (See Appendix D for some information about 16-bit floating-
881881+ point numbers.)
882882+883883+3.4. Tagging of Items
884884+885885+ In CBOR, a data item can be enclosed by a tag to give it some
886886+ additional semantics, as uniquely identified by a _tag number_. The
887887+ tag is major type 6, its argument (Section 3) indicates the tag
888888+ number, and it contains a single enclosed data item, the _tag
889889+ content_. (If a tag requires further structure to its content, this
890890+ structure is provided by the enclosed data item.) We use the term
891891+ _tag_ for the entire data item consisting of both a tag number and
892892+ the tag content: the tag content is the data item that is being
893893+ tagged.
894894+895895+ For example, assume that a byte string of length 12 is marked with a
896896+ tag of number 2 to indicate it is an unsigned _bignum_
897897+ (Section 3.4.3). The encoded data item would start with a byte
898898+ 0b110_00010 (major type 6, additional information 2 for the tag
899899+ number) followed by the encoded tag content: 0b010_01100 (major type
900900+ 2, additional information 12 for the length) followed by the 12 bytes
901901+ of the bignum.
902902+903903+ In the extended generic data model, a tag number's definition
904904+ describes the additional semantics conveyed with the tag number.
905905+ These semantics may include equivalence of some tagged data items
906906+ with other data items, including some that can be represented in the
907907+ basic generic data model. For instance, 0xc24101, a bignum the tag
908908+ content of which is the byte string with the single byte 0x01, is
909909+ equivalent to an integer 1, which could also be encoded as 0x01,
910910+ 0x1801, or 0x190001. The tag definition may specify a preferred
911911+ serialization (Section 4.1) that is recommended for generic encoders;
912912+ this may prefer basic generic data model representations over ones
913913+ that employ a tag.
914914+915915+ The tag definition usually defines which nested data items are valid
916916+ for such tags. Tag definitions may restrict their content to a very
917917+ specific syntactic structure, as the tags defined in this document
918918+ do, or they may define their content more semantically. An example
919919+ for the latter is how tags 40 and 1040 accept multiple ways to
920920+ represent arrays [RFC8746].
921921+922922+ As a matter of convention, many tags do not accept "null" or
923923+ "undefined" values as tag content; instead, the expectation is that a
924924+ "null" or "undefined" value can be used in place of the entire tag;
925925+ Section 3.4.2 provides some further considerations for one specific
926926+ tag about the handling of this convention in application protocols
927927+ and in mapping to platform types.
928928+929929+ Decoders do not need to understand tags of every tag number, and tags
930930+ may be of little value in applications where the implementation
931931+ creating a particular CBOR data item and the implementation decoding
932932+ that stream know the semantic meaning of each item in the data flow.
933933+ The primary purpose of tags in this specification is to define common
934934+ data types such as dates. A secondary purpose is to provide
935935+ conversion hints when it is foreseen that the CBOR data item needs to
936936+ be translated into a different format, requiring hints about the
937937+ content of items. Understanding the semantics of tags is optional
938938+ for a decoder; it can simply present both the tag number and the tag
939939+ content to the application, without interpreting the additional
940940+ semantics of the tag.
941941+942942+ A tag applies semantics to the data item it encloses. Tags can nest:
943943+ if tag A encloses tag B, which encloses data item C, tag A applies to
944944+ the result of applying tag B on data item C.
945945+946946+ IANA maintains a registry of tag numbers as described in Section 9.2.
947947+ Table 5 provides a list of tag numbers that were defined in [RFC7049]
948948+ with definitions in the rest of this section. (Tag number 35 was
949949+ also defined in [RFC7049]; a discussion of this tag number follows in
950950+ Section 3.4.5.3.) Note that many other tag numbers have been defined
951951+ since the publication of [RFC7049]; see the registry described at
952952+ Section 9.2 for the complete list.
953953+954954+ +=======+=============+==================================+
955955+ | Tag | Data Item | Semantics |
956956+ +=======+=============+==================================+
957957+ | 0 | text string | Standard date/time string; see |
958958+ | | | Section 3.4.1 |
959959+ +-------+-------------+----------------------------------+
960960+ | 1 | integer or | Epoch-based date/time; see |
961961+ | | float | Section 3.4.2 |
962962+ +-------+-------------+----------------------------------+
963963+ | 2 | byte string | Unsigned bignum; see |
964964+ | | | Section 3.4.3 |
965965+ +-------+-------------+----------------------------------+
966966+ | 3 | byte string | Negative bignum; see |
967967+ | | | Section 3.4.3 |
968968+ +-------+-------------+----------------------------------+
969969+ | 4 | array | Decimal fraction; see |
970970+ | | | Section 3.4.4 |
971971+ +-------+-------------+----------------------------------+
972972+ | 5 | array | Bigfloat; see Section 3.4.4 |
973973+ +-------+-------------+----------------------------------+
974974+ | 21 | (any) | Expected conversion to base64url |
975975+ | | | encoding; see Section 3.4.5.2 |
976976+ +-------+-------------+----------------------------------+
977977+ | 22 | (any) | Expected conversion to base64 |
978978+ | | | encoding; see Section 3.4.5.2 |
979979+ +-------+-------------+----------------------------------+
980980+ | 23 | (any) | Expected conversion to base16 |
981981+ | | | encoding; see Section 3.4.5.2 |
982982+ +-------+-------------+----------------------------------+
983983+ | 24 | byte string | Encoded CBOR data item; see |
984984+ | | | Section 3.4.5.1 |
985985+ +-------+-------------+----------------------------------+
986986+ | 32 | text string | URI; see Section 3.4.5.3 |
987987+ +-------+-------------+----------------------------------+
988988+ | 33 | text string | base64url; see Section 3.4.5.3 |
989989+ +-------+-------------+----------------------------------+
990990+ | 34 | text string | base64; see Section 3.4.5.3 |
991991+ +-------+-------------+----------------------------------+
992992+ | 36 | text string | MIME message; see |
993993+ | | | Section 3.4.5.3 |
994994+ +-------+-------------+----------------------------------+
995995+ | 55799 | (any) | Self-described CBOR; see |
996996+ | | | Section 3.4.6 |
997997+ +-------+-------------+----------------------------------+
998998+999999+ Table 5: Tag Numbers Defined in RFC 7049
10001000+10011001+ Conceptually, tags are interpreted in the generic data model, not at
10021002+ (de-)serialization time. A small number of tags (at this time, tag
10031003+ number 25 and tag number 29 [IANA.cbor-tags]) have been registered
10041004+ with semantics that may require processing at (de-)serialization
10051005+ time: the decoder needs to be aware of, and the encoder needs to be
10061006+ in control of, the exact sequence in which data items are encoded
10071007+ into the CBOR data item. This means these tags cannot be implemented
10081008+ on top of an arbitrary generic CBOR encoder/decoder (which might not
10091009+ reflect the serialization order for entries in a map at the data
10101010+ model level and vice versa); their implementation therefore typically
10111011+ needs to be integrated into the generic encoder/decoder. The
10121012+ definition of new tags with this property is NOT RECOMMENDED.
10131013+10141014+ IANA allocated tag numbers 65535, 4294967295, and
10151015+ 18446744073709551615 (binary all-ones in 16-bit, 32-bit, and 64-bit).
10161016+ These can be used as a convenience for implementers who want a
10171017+ single-integer data structure to indicate either the presence of a
10181018+ specific tag or absence of a tag. That allocation is described in
10191019+ Section 10 of [CBOR-TAGS]. These tags are not intended to occur in
10201020+ actual CBOR data items; implementations MAY flag such an occurrence
10211021+ as an error.
10221022+10231023+ Protocols can extend the generic data model (Section 2) with data
10241024+ items representing points in time by using tag numbers 0 and 1, with
10251025+ arbitrarily sized integers by using tag numbers 2 and 3, and with
10261026+ floating-point values of arbitrary size and precision by using tag
10271027+ numbers 4 and 5.
10281028+10291029+3.4.1. Standard Date/Time String
10301030+10311031+ Tag number 0 contains a text string in the standard format described
10321032+ by the "date-time" production in [RFC3339], as refined by Section 3.3
10331033+ of [RFC4287], representing the point in time described there. A
10341034+ nested item of another type or a text string that doesn't match the
10351035+ format described in [RFC4287] is invalid.
10361036+10371037+3.4.2. Epoch-Based Date/Time
10381038+10391039+ Tag number 1 contains a numerical value counting the number of
10401040+ seconds from 1970-01-01T00:00Z in UTC time to the represented point
10411041+ in civil time.
10421042+10431043+ The tag content MUST be an unsigned or negative integer (major types
10441044+ 0 and 1) or a floating-point number (major type 7 with additional
10451045+ information 25, 26, or 27). Other contained types are invalid.
10461046+10471047+ Nonnegative values (major type 0 and nonnegative floating-point
10481048+ numbers) stand for time values on or after 1970-01-01T00:00Z UTC and
10491049+ are interpreted according to POSIX [TIME_T]. (POSIX time is also
10501050+ known as "UNIX Epoch time".) Leap seconds are handled specially by
10511051+ POSIX time, and this results in a 1-second discontinuity several
10521052+ times per decade. Note that applications that require the expression
10531053+ of times beyond early 2106 cannot leave out support of 64-bit
10541054+ integers for the tag content.
10551055+10561056+ Negative values (major type 1 and negative floating-point numbers)
10571057+ are interpreted as determined by the application requirements as
10581058+ there is no universal standard for UTC count-of-seconds time before
10591059+ 1970-01-01T00:00Z (this is particularly true for points in time that
10601060+ precede discontinuities in national calendars). The same applies to
10611061+ non-finite values.
10621062+10631063+ To indicate fractional seconds, floating-point values can be used
10641064+ within tag number 1 instead of integer values. Note that this
10651065+ generally requires binary64 support, as binary16 and binary32 provide
10661066+ nonzero fractions of seconds only for a short period of time around
10671067+ early 1970. An application that requires tag number 1 support may
10681068+ restrict the tag content to be an integer (or a floating-point value)
10691069+ only.
10701070+10711071+ Note that platform types for date/time may include "null" or
10721072+ "undefined" values, which may also be desirable at an application
10731073+ protocol level. While emitting tag number 1 values with non-finite
10741074+ tag content values (e.g., with NaN for undefined date/time values or
10751075+ with Infinity for an expiry date that is not set) may seem an obvious
10761076+ way to handle this, using untagged "null" or "undefined" avoids the
10771077+ use of non-finites and results in a shorter encoding. Application
10781078+ protocol designers are encouraged to consider these cases and include
10791079+ clear guidelines for handling them.
10801080+10811081+3.4.3. Bignums
10821082+10831083+ Protocols using tag numbers 2 and 3 extend the generic data model
10841084+ (Section 2) with "bignums" representing arbitrarily sized integers.
10851085+ In the basic generic data model, bignum values are not equal to
10861086+ integers from the same model, but the extended generic data model
10871087+ created by this tag definition defines equivalence based on numeric
10881088+ value, and preferred serialization (Section 4.1) never makes use of
10891089+ bignums that also can be expressed as basic integers (see below).
10901090+10911091+ Bignums are encoded as a byte string data item, which is interpreted
10921092+ as an unsigned integer n in network byte order. Contained items of
10931093+ other types are invalid. For tag number 2, the value of the bignum
10941094+ is n. For tag number 3, the value of the bignum is -1 - n. The
10951095+ preferred serialization of the byte string is to leave out any
10961096+ leading zeroes (note that this means the preferred serialization for
10971097+ n = 0 is the empty byte string, but see below). Decoders that
10981098+ understand these tags MUST be able to decode bignums that do have
10991099+ leading zeroes. The preferred serialization of an integer that can
11001100+ be represented using major type 0 or 1 is to encode it this way
11011101+ instead of as a bignum (which means that the empty string never
11021102+ occurs in a bignum when using preferred serialization). Note that
11031103+ this means the non-preferred choice of a bignum representation
11041104+ instead of a basic integer for encoding a number is not intended to
11051105+ have application semantics (just as the choice of a longer basic
11061106+ integer representation than needed, such as 0x1800 for 0x00, does
11071107+ not).
11081108+11091109+ For example, the number 18446744073709551616 (2^(64)) is represented
11101110+ as 0b110_00010 (major type 6, tag number 2), followed by 0b010_01001
11111111+ (major type 2, length 9), followed by 0x010000000000000000 (one byte
11121112+ 0x01 and eight bytes 0x00). In hexadecimal:
11131113+11141114+ C2 -- Tag 2
11151115+ 49 -- Byte string of length 9
11161116+ 010000000000000000 -- Bytes content
11171117+11181118+3.4.4. Decimal Fractions and Bigfloats
11191119+11201120+ Protocols using tag number 4 extend the generic data model with data
11211121+ items representing arbitrary-length decimal fractions of the form
11221122+ m*(10^(e)). Protocols using tag number 5 extend the generic data
11231123+ model with data items representing arbitrary-length binary fractions
11241124+ of the form m*(2^(e)). As with bignums, values of different types
11251125+ are not equal in the generic data model.
11261126+11271127+ Decimal fractions combine an integer mantissa with a base-10 scaling
11281128+ factor. They are most useful if an application needs the exact
11291129+ representation of a decimal fraction such as 1.1 because there is no
11301130+ exact representation for many decimal fractions in binary floating-
11311131+ point representations.
11321132+11331133+ "Bigfloats" combine an integer mantissa with a base-2 scaling factor.
11341134+ They are binary floating-point values that can exceed the range or
11351135+ the precision of the three IEEE 754 formats supported by CBOR
11361136+ (Section 3.3). Bigfloats may also be used by constrained
11371137+ applications that need some basic binary floating-point capability
11381138+ without the need for supporting IEEE 754.
11391139+11401140+ A decimal fraction or a bigfloat is represented as a tagged array
11411141+ that contains exactly two integer numbers: an exponent e and a
11421142+ mantissa m. Decimal fractions (tag number 4) use base-10 exponents;
11431143+ the value of a decimal fraction data item is m*(10^(e)). Bigfloats
11441144+ (tag number 5) use base-2 exponents; the value of a bigfloat data
11451145+ item is m*(2^(e)). The exponent e MUST be represented in an integer
11461146+ of major type 0 or 1, while the mantissa can also be a bignum
11471147+ (Section 3.4.3). Contained items with other structures are invalid.
11481148+11491149+ An example of a decimal fraction is the representation of the number
11501150+ 273.15 as 0b110_00100 (major type 6 for tag, additional information 4
11511151+ for the tag number), followed by 0b100_00010 (major type 4 for the
11521152+ array, additional information 2 for the length of the array),
11531153+ followed by 0b001_00001 (major type 1 for the first integer,
11541154+ additional information 1 for the value of -2), followed by
11551155+ 0b000_11001 (major type 0 for the second integer, additional
11561156+ information 25 for a two-byte value), followed by 0b0110101010110011
11571157+ (27315 in two bytes). In hexadecimal:
11581158+11591159+ C4 -- Tag 4
11601160+ 82 -- Array of length 2
11611161+ 21 -- -2
11621162+ 19 6ab3 -- 27315
11631163+11641164+ An example of a bigfloat is the representation of the number 1.5 as
11651165+ 0b110_00101 (major type 6 for tag, additional information 5 for the
11661166+ tag number), followed by 0b100_00010 (major type 4 for the array,
11671167+ additional information 2 for the length of the array), followed by
11681168+ 0b001_00000 (major type 1 for the first integer, additional
11691169+ information 0 for the value of -1), followed by 0b000_00011 (major
11701170+ type 0 for the second integer, additional information 3 for the value
11711171+ of 3). In hexadecimal:
11721172+11731173+ C5 -- Tag 5
11741174+ 82 -- Array of length 2
11751175+ 20 -- -1
11761176+ 03 -- 3
11771177+11781178+ Decimal fractions and bigfloats provide no representation of
11791179+ Infinity, -Infinity, or NaN; if these are needed in place of a
11801180+ decimal fraction or bigfloat, the IEEE 754 half-precision
11811181+ representations from Section 3.3 can be used.
11821182+11831183+3.4.5. Content Hints
11841184+11851185+ The tags in this section are for content hints that might be used by
11861186+ generic CBOR processors. These content hints do not extend the
11871187+ generic data model.
11881188+11891189+3.4.5.1. Encoded CBOR Data Item
11901190+11911191+ Sometimes it is beneficial to carry an embedded CBOR data item that
11921192+ is not meant to be decoded immediately at the time the enclosing data
11931193+ item is being decoded. Tag number 24 (CBOR data item) can be used to
11941194+ tag the embedded byte string as a single data item encoded in CBOR
11951195+ format. Contained items that aren't byte strings are invalid. A
11961196+ contained byte string is valid if it encodes a well-formed CBOR data
11971197+ item; validity checking of the decoded CBOR item is not required for
11981198+ tag validity (but could be offered by a generic decoder as a special
11991199+ option).
12001200+12011201+3.4.5.2. Expected Later Encoding for CBOR-to-JSON Converters
12021202+12031203+ Tag numbers 21 to 23 indicate that a byte string might require a
12041204+ specific encoding when interoperating with a text-based
12051205+ representation. These tags are useful when an encoder knows that the
12061206+ byte string data it is writing is likely to be later converted to a
12071207+ particular JSON-based usage. That usage specifies that some strings
12081208+ are encoded as base64, base64url, and so on. The encoder uses byte
12091209+ strings instead of doing the encoding itself to reduce the message
12101210+ size, to reduce the code size of the encoder, or both. The encoder
12111211+ does not know whether or not the converter will be generic, and
12121212+ therefore wants to say what it believes is the proper way to convert
12131213+ binary strings to JSON.
12141214+12151215+ The data item tagged can be a byte string or any other data item. In
12161216+ the latter case, the tag applies to all of the byte string data items
12171217+ contained in the data item, except for those contained in a nested
12181218+ data item tagged with an expected conversion.
12191219+12201220+ These three tag numbers suggest conversions to three of the base data
12211221+ encodings defined in [RFC4648]. Tag number 21 suggests conversion to
12221222+ base64url encoding (Section 5 of [RFC4648]) where padding is not used
12231223+ (see Section 3.2 of [RFC4648]); that is, all trailing equals signs
12241224+ ("=") are removed from the encoded string. Tag number 22 suggests
12251225+ conversion to classical base64 encoding (Section 4 of [RFC4648]) with
12261226+ padding as defined in RFC 4648. For both base64url and base64,
12271227+ padding bits are set to zero (see Section 3.5 of [RFC4648]), and the
12281228+ conversion to alternate encoding is performed on the contents of the
12291229+ byte string (that is, without adding any line breaks, whitespace, or
12301230+ other additional characters). Tag number 23 suggests conversion to
12311231+ base16 (hex) encoding with uppercase alphabetics (see Section 8 of
12321232+ [RFC4648]). Note that, for all three tag numbers, the encoding of
12331233+ the empty byte string is the empty text string.
12341234+12351235+3.4.5.3. Encoded Text
12361236+12371237+ Some text strings hold data that have formats widely used on the
12381238+ Internet, and sometimes those formats can be validated and presented
12391239+ to the application in appropriate form by the decoder. There are
12401240+ tags for some of these formats.
12411241+12421242+ * Tag number 32 is for URIs, as defined in [RFC3986]. If the text
12431243+ string doesn't match the "URI-reference" production, the string is
12441244+ invalid.
12451245+12461246+ * Tag numbers 33 and 34 are for base64url- and base64-encoded text
12471247+ strings, respectively, as defined in [RFC4648]. If any of the
12481248+ following apply:
12491249+12501250+ - the encoded text string contains non-alphabet characters or
12511251+ only 1 alphabet character in the last block of 4 (where
12521252+ alphabet is defined by Section 5 of [RFC4648] for tag number 33
12531253+ and Section 4 of [RFC4648] for tag number 34), or
12541254+12551255+ - the padding bits in a 2- or 3-character block are not 0, or
12561256+12571257+ - the base64 encoding has the wrong number of padding characters,
12581258+ or
12591259+12601260+ - the base64url encoding has padding characters,
12611261+12621262+ the string is invalid.
12631263+12641264+ * Tag number 36 is for MIME messages (including all headers), as
12651265+ defined in [RFC2045]. A text string that isn't a valid MIME
12661266+ message is invalid. (For this tag, validity checking may be
12671267+ particularly onerous for a generic decoder and might therefore not
12681268+ be offered. Note that many MIME messages are general binary data
12691269+ and therefore cannot be represented in a text string;
12701270+ [IANA.cbor-tags] lists a registration for tag number 257 that is
12711271+ similar to tag number 36 but uses a byte string as its tag
12721272+ content.)
12731273+12741274+ Note that tag numbers 33 and 34 differ from 21 and 22 in that the
12751275+ data is transported in base-encoded form for the former and in raw
12761276+ byte string form for the latter.
12771277+12781278+ [RFC7049] also defined a tag number 35 for regular expressions that
12791279+ are in Perl Compatible Regular Expressions (PCRE/PCRE2) form [PCRE]
12801280+ or in JavaScript regular expression syntax [ECMA262]. The state of
12811281+ the art in these regular expression specifications has since advanced
12821282+ and is continually advancing, so this specification does not attempt
12831283+ to update the references. Instead, this tag remains available (as
12841284+ registered in [RFC7049]) for applications that specify the particular
12851285+ regular expression variant they use out-of-band (possibly by limiting
12861286+ the usage to a defined common subset of both PCRE and ECMA262). As
12871287+ this specification clarifies tag validity beyond [RFC7049], we note
12881288+ that due to the open way the tag was defined in [RFC7049], any
12891289+ contained string value needs to be valid at the CBOR tag level (but
12901290+ then may not be "expected" at the application level).
12911291+12921292+3.4.6. Self-Described CBOR
12931293+12941294+ In many applications, it will be clear from the context that CBOR is
12951295+ being employed for encoding a data item. For instance, a specific
12961296+ protocol might specify the use of CBOR, or a media type is indicated
12971297+ that specifies its use. However, there may be applications where
12981298+ such context information is not available, such as when CBOR data is
12991299+ stored in a file that does not have disambiguating metadata. Here,
13001300+ it may help to have some distinguishing characteristics for the data
13011301+ itself.
13021302+13031303+ Tag number 55799 is defined for this purpose, specifically for use at
13041304+ the start of a stored encoded CBOR data item as specified by an
13051305+ application. It does not impart any special semantics on the data
13061306+ item that it encloses; that is, the semantics of the tag content
13071307+ enclosed in tag number 55799 is exactly identical to the semantics of
13081308+ the tag content itself.
13091309+13101310+ The serialization of this tag's head is 0xd9d9f7, which does not
13111311+ appear to be in use as a distinguishing mark for any frequently used
13121312+ file types. In particular, 0xd9d9f7 is not a valid start of a
13131313+ Unicode text in any Unicode encoding if it is followed by a valid
13141314+ CBOR data item.
13151315+13161316+ For instance, a decoder might be able to decode both CBOR and JSON.
13171317+ Such a decoder would need to mechanically distinguish the two
13181318+ formats. An easy way for an encoder to help the decoder would be to
13191319+ tag the entire CBOR item with tag number 55799, the serialization of
13201320+ which will never be found at the beginning of a JSON text.
13211321+13221322+4. Serialization Considerations
13231323+13241324+4.1. Preferred Serialization
13251325+13261326+ For some values at the data model level, CBOR provides multiple
13271327+ serializations. For many applications, it is desirable that an
13281328+ encoder always chooses a preferred serialization (preferred
13291329+ encoding); however, the present specification does not put the burden
13301330+ of enforcing this preference on either the encoder or decoder.
13311331+13321332+ Some constrained decoders may be limited in their ability to decode
13331333+ non-preferred serializations: for example, if only integers below
13341334+ 1_000_000_000 (one billion) are expected in an application, the
13351335+ decoder may leave out the code that would be needed to decode 64-bit
13361336+ arguments in integers. An encoder that always uses preferred
13371337+ serialization ("preferred encoder") interoperates with this decoder
13381338+ for the numbers that can occur in this application. Generally
13391339+ speaking, a preferred encoder is more universally interoperable (and
13401340+ also less wasteful) than one that, say, always uses 64-bit integers.
13411341+13421342+ Similarly, a constrained encoder may be limited in the variety of
13431343+ representation variants it supports such that it does not emit
13441344+ preferred serializations ("variant encoder"). For instance, a
13451345+ constrained encoder could be designed to always use the 32-bit
13461346+ variant for an integer that it encodes even if a short representation
13471347+ is available (assuming that there is no application need for integers
13481348+ that can only be represented with the 64-bit variant). A decoder
13491349+ that does not rely on receiving only preferred serializations
13501350+ ("variation-tolerant decoder") can therefore be said to be more
13511351+ universally interoperable (it might very well optimize for the case
13521352+ of receiving preferred serializations, though). Full implementations
13531353+ of CBOR decoders are by definition variation tolerant; the
13541354+ distinction is only relevant if a constrained implementation of a
13551355+ CBOR decoder meets a variant encoder.
13561356+13571357+ The preferred serialization always uses the shortest form of
13581358+ representing the argument (Section 3); it also uses the shortest
13591359+ floating-point encoding that preserves the value being encoded.
13601360+13611361+ The preferred serialization for a floating-point value is the
13621362+ shortest floating-point encoding that preserves its value, e.g.,
13631363+ 0xf94580 for the number 5.5, and 0xfa45ad9c00 for the number 5555.5.
13641364+ For NaN values, a shorter encoding is preferred if zero-padding the
13651365+ shorter significand towards the right reconstitutes the original NaN
13661366+ value (for many applications, the single NaN encoding 0xf97e00 will
13671367+ suffice).
13681368+13691369+ Definite-length encoding is preferred whenever the length is known at
13701370+ the time the serialization of the item starts.
13711371+13721372+4.2. Deterministically Encoded CBOR
13731373+13741374+ Some protocols may want encoders to only emit CBOR in a particular
13751375+ deterministic format; those protocols might also have the decoders
13761376+ check that their input is in that deterministic format. Those
13771377+ protocols are free to define what they mean by a "deterministic
13781378+ format" and what encoders and decoders are expected to do. This
13791379+ section defines a set of restrictions that can serve as the base of
13801380+ such a deterministic format.
13811381+13821382+4.2.1. Core Deterministic Encoding Requirements
13831383+13841384+ A CBOR encoding satisfies the "core deterministic encoding
13851385+ requirements" if it satisfies the following restrictions:
13861386+13871387+ * Preferred serialization MUST be used. In particular, this means
13881388+ that arguments (see Section 3) for integers, lengths in major
13891389+ types 2 through 5, and tags MUST be as short as possible, for
13901390+ instance:
13911391+13921392+ - 0 to 23 and -1 to -24 MUST be expressed in the same byte as the
13931393+ major type;
13941394+13951395+ - 24 to 255 and -25 to -256 MUST be expressed only with an
13961396+ additional uint8_t;
13971397+13981398+ - 256 to 65535 and -257 to -65536 MUST be expressed only with an
13991399+ additional uint16_t;
14001400+14011401+ - 65536 to 4294967295 and -65537 to -4294967296 MUST be expressed
14021402+ only with an additional uint32_t.
14031403+14041404+ Floating-point values also MUST use the shortest form that
14051405+ preserves the value, e.g., 1.5 is encoded as 0xf93e00 (binary16)
14061406+ and 1000000.5 as 0xfa49742408 (binary32). (One implementation of
14071407+ this is to have all floats start as a 64-bit float, then do a test
14081408+ conversion to a 32-bit float; if the result is the same numeric
14091409+ value, use the shorter form and repeat the process with a test
14101410+ conversion to a 16-bit float. This also works to select 16-bit
14111411+ float for positive and negative Infinity as well.)
14121412+14131413+ * Indefinite-length items MUST NOT appear. They can be encoded as
14141414+ definite-length items instead.
14151415+14161416+ * The keys in every map MUST be sorted in the bytewise lexicographic
14171417+ order of their deterministic encodings. For example, the
14181418+ following keys are sorted correctly:
14191419+14201420+ 1. 10, encoded as 0x0a.
14211421+14221422+ 2. 100, encoded as 0x1864.
14231423+14241424+ 3. -1, encoded as 0x20.
14251425+14261426+ 4. "z", encoded as 0x617a.
14271427+14281428+ 5. "aa", encoded as 0x626161.
14291429+14301430+ 6. [100], encoded as 0x811864.
14311431+14321432+ 7. [-1], encoded as 0x8120.
14331433+14341434+ 8. false, encoded as 0xf4.
14351435+14361436+ | Implementation note: the self-delimiting nature of the CBOR
14371437+ | encoding means that there are no two well-formed CBOR encoded
14381438+ | data items where one is a prefix of the other. The bytewise
14391439+ | lexicographic comparison of deterministic encodings of
14401440+ | different map keys therefore always ends in a position where
14411441+ | the byte differs between the keys, before the end of a key is
14421442+ | reached.
14431443+14441444+4.2.2. Additional Deterministic Encoding Considerations
14451445+14461446+ CBOR tags present additional considerations for deterministic
14471447+ encoding. If a CBOR-based protocol were to provide the same
14481448+ semantics for the presence and absence of a specific tag (e.g., by
14491449+ allowing both tag 1 data items and raw numbers in a date/time
14501450+ position, treating the latter as if they were tagged), the
14511451+ deterministic format would not allow the presence of the tag, based
14521452+ on the "shortest form" principle. For example, a protocol might give
14531453+ encoders the choice of representing a URL as either a text string or,
14541454+ using Section 3.4.5.3, tag number 32 containing a text string. This
14551455+ protocol's deterministic encoding needs either to require that the
14561456+ tag is present or to require that it is absent, not allow either one.
14571457+14581458+ In a protocol that does require tags in certain places to obtain
14591459+ specific semantics, the tag needs to appear in the deterministic
14601460+ format as well. Deterministic encoding considerations also apply to
14611461+ the content of tags.
14621462+14631463+ If a protocol includes a field that can express integers with an
14641464+ absolute value of 2^(64) or larger using tag numbers 2 or 3
14651465+ (Section 3.4.3), the protocol's deterministic encoding needs to
14661466+ specify whether smaller integers are also expressed using these tags
14671467+ or using major types 0 and 1. Preferred serialization uses the
14681468+ latter choice, which is therefore recommended.
14691469+14701470+ Protocols that include floating-point values, whether represented
14711471+ using basic floating-point values (Section 3.3) or using tags (or
14721472+ both), may need to define extra requirements on their deterministic
14731473+ encodings, such as:
14741474+14751475+ * Although IEEE floating-point values can represent both positive
14761476+ and negative zero as distinct values, the application might not
14771477+ distinguish these and might decide to represent all zero values
14781478+ with a positive sign, disallowing negative zero. (The application
14791479+ may also want to restrict the precision of floating-point values
14801480+ in such a way that there is never a need to represent 64-bit -- or
14811481+ even 32-bit -- floating-point values.)
14821482+14831483+ * If a protocol includes a field that can express floating-point
14841484+ values, with a specific data model that declares integer and
14851485+ floating-point values to be interchangeable, the protocol's
14861486+ deterministic encoding needs to specify whether, for example, the
14871487+ integer 1.0 is encoded as 0x01 (unsigned integer), 0xf93c00
14881488+ (binary16), 0xfa3f800000 (binary32), or 0xfb3ff0000000000000
14891489+ (binary64). Example rules for this are:
14901490+14911491+ 1. Encode integral values that fit in 64 bits as values from
14921492+ major types 0 and 1, and other values as the preferred
14931493+ (smallest of 16-, 32-, or 64-bit) floating-point
14941494+ representation that accurately represents the value,
14951495+14961496+ 2. Encode all values as the preferred floating-point
14971497+ representation that accurately represents the value, even for
14981498+ integral values, or
14991499+15001500+ 3. Encode all values as 64-bit floating-point representations.
15011501+15021502+ Rule 1 straddles the boundaries between integers and floating-
15031503+ point values, and Rule 3 does not use preferred serialization, so
15041504+ Rule 2 may be a good choice in many cases.
15051505+15061506+ * If NaN is an allowed value, and there is no intent to support NaN
15071507+ payloads or signaling NaNs, the protocol needs to pick a single
15081508+ representation, typically 0xf97e00. If that simple choice is not
15091509+ possible, specific attention will be needed for NaN handling.
15101510+15111511+ * Subnormal numbers (nonzero numbers with the lowest possible
15121512+ exponent of a given IEEE 754 number format) may be flushed to zero
15131513+ outputs or be treated as zero inputs in some floating-point
15141514+ implementations. A protocol's deterministic encoding may want to
15151515+ specifically accommodate such implementations while creating an
15161516+ onus on other implementations by excluding subnormal numbers from
15171517+ interchange, interchanging zero instead.
15181518+15191519+ * The same number can be represented by different decimal fractions,
15201520+ by different bigfloats, and by different forms under other tags
15211521+ that may be defined to express numeric values. Depending on the
15221522+ implementation, it may not always be practical to determine
15231523+ whether any of these forms (or forms in the basic generic data
15241524+ model) are equivalent. An application protocol that presents
15251525+ choices of this kind for the representation format of numbers
15261526+ needs to be explicit about how the formats for deterministic
15271527+ encoding are to be chosen.
15281528+15291529+4.2.3. Length-First Map Key Ordering
15301530+15311531+ The core deterministic encoding requirements (Section 4.2.1) sort map
15321532+ keys in a different order from the one suggested by Section 3.9 of
15331533+ [RFC7049] (called "Canonical CBOR" there). Protocols that need to be
15341534+ compatible with the order specified in [RFC7049] can instead be
15351535+ specified in terms of this specification's "length-first core
15361536+ deterministic encoding requirements":
15371537+15381538+ A CBOR encoding satisfies the "length-first core deterministic
15391539+ encoding requirements" if it satisfies the core deterministic
15401540+ encoding requirements except that the keys in every map MUST be
15411541+ sorted such that:
15421542+15431543+ 1. If two keys have different lengths, the shorter one sorts
15441544+ earlier;
15451545+15461546+ 2. If two keys have the same length, the one with the lower value in
15471547+ (bytewise) lexical order sorts earlier.
15481548+15491549+ For example, under the length-first core deterministic encoding
15501550+ requirements, the following keys are sorted correctly:
15511551+15521552+ 1. 10, encoded as 0x0a.
15531553+15541554+ 2. -1, encoded as 0x20.
15551555+15561556+ 3. false, encoded as 0xf4.
15571557+15581558+ 4. 100, encoded as 0x1864.
15591559+15601560+ 5. "z", encoded as 0x617a.
15611561+15621562+ 6. [-1], encoded as 0x8120.
15631563+15641564+ 7. "aa", encoded as 0x626161.
15651565+15661566+ 8. [100], encoded as 0x811864.
15671567+15681568+ | Although [RFC7049] used the term "Canonical CBOR" for its form
15691569+ | of requirements on deterministic encoding, this document avoids
15701570+ | this term because "canonicalization" is often associated with
15711571+ | specific uses of deterministic encoding only. The terms are
15721572+ | essentially interchangeable, however, and the set of core
15731573+ | requirements in this document could also be called "Canonical
15741574+ | CBOR", while the length-first-ordered version of that could be
15751575+ | called "Old Canonical CBOR".
15761576+15771577+5. Creating CBOR-Based Protocols
15781578+15791579+ Data formats such as CBOR are often used in environments where there
15801580+ is no format negotiation. A specific design goal of CBOR is to not
15811581+ need any included or assumed schema: a decoder can take a CBOR item
15821582+ and decode it with no other knowledge.
15831583+15841584+ Of course, in real-world implementations, the encoder and the decoder
15851585+ will have a shared view of what should be in a CBOR data item. For
15861586+ example, an agreed-to format might be "the item is an array whose
15871587+ first value is a UTF-8 string, second value is an integer, and
15881588+ subsequent values are zero or more floating-point numbers" or "the
15891589+ item is a map that has byte strings for keys and contains a pair
15901590+ whose key is 0xab01".
15911591+15921592+ CBOR-based protocols MUST specify how their decoders handle invalid
15931593+ and other unexpected data. CBOR-based protocols MAY specify that
15941594+ they treat arbitrary valid data as unexpected. Encoders for CBOR-
15951595+ based protocols MUST produce only valid items, that is, the protocol
15961596+ cannot be designed to make use of invalid items. An encoder can be
15971597+ capable of encoding as many or as few types of values as is required
15981598+ by the protocol in which it is used; a decoder can be capable of
15991599+ understanding as many or as few types of values as is required by the
16001600+ protocols in which it is used. This lack of restrictions allows CBOR
16011601+ to be used in extremely constrained environments.
16021602+16031603+ The rest of this section discusses some considerations in creating
16041604+ CBOR-based protocols. With few exceptions, it is advisory only and
16051605+ explicitly excludes any language from BCP 14 [RFC2119] [RFC8174]
16061606+ other than words that could be interpreted as "MAY" in the sense of
16071607+ BCP 14. The exceptions aim at facilitating interoperability of CBOR-
16081608+ based protocols while making use of a wide variety of both generic
16091609+ and application-specific encoders and decoders.
16101610+16111611+5.1. CBOR in Streaming Applications
16121612+16131613+ In a streaming application, a data stream may be composed of a
16141614+ sequence of CBOR data items concatenated back-to-back. In such an
16151615+ environment, the decoder immediately begins decoding a new data item
16161616+ if data is found after the end of a previous data item.
16171617+16181618+ Not all of the bytes making up a data item may be immediately
16191619+ available to the decoder; some decoders will buffer additional data
16201620+ until a complete data item can be presented to the application.
16211621+ Other decoders can present partial information about a top-level data
16221622+ item to an application, such as the nested data items that could
16231623+ already be decoded, or even parts of a byte string that hasn't
16241624+ completely arrived yet. Such an application also MUST have a
16251625+ matching streaming security mechanism, where the desired protection
16261626+ is available for incremental data presented to the application.
16271627+16281628+ Note that some applications and protocols will not want to use
16291629+ indefinite-length encoding. Using indefinite-length encoding allows
16301630+ an encoder to not need to marshal all the data for counting, but it
16311631+ requires a decoder to allocate increasing amounts of memory while
16321632+ waiting for the end of the item. This might be fine for some
16331633+ applications but not others.
16341634+16351635+5.2. Generic Encoders and Decoders
16361636+16371637+ A generic CBOR decoder can decode all well-formed encoded CBOR data
16381638+ items and present the data items to an application. See Appendix C.
16391639+ (The diagnostic notation, Section 8, may be used to present well-
16401640+ formed CBOR values to humans.)
16411641+16421642+ Generic CBOR encoders provide an application interface that allows
16431643+ the application to specify any well-formed value to be encoded as a
16441644+ CBOR data item, including simple values and tags unknown to the
16451645+ encoder.
16461646+16471647+ Even though CBOR attempts to minimize these cases, not all well-
16481648+ formed CBOR data is valid: for example, the encoded text string
16491649+ "0x62c0ae" does not contain valid UTF-8 (because [RFC3629] requires
16501650+ always using the shortest form) and so is not a valid CBOR item.
16511651+ Also, specific tags may make semantic constraints that may be
16521652+ violated, for instance, by a bignum tag enclosing another tag or by
16531653+ an instance of tag number 0 containing a byte string or containing a
16541654+ text string with contents that do not match the "date-time"
16551655+ production of [RFC3339]. There is no requirement that generic
16561656+ encoders and decoders make unnatural choices for their application
16571657+ interface to enable the processing of invalid data. Generic encoders
16581658+ and decoders are expected to forward simple values and tags even if
16591659+ their specific codepoints are not registered at the time the encoder/
16601660+ decoder is written (Section 5.4).
16611661+16621662+5.3. Validity of Items
16631663+16641664+ A well-formed but invalid CBOR data item (Section 1.2) presents a
16651665+ problem with interpreting the data encoded in it in the CBOR data
16661666+ model. A CBOR-based protocol could be specified in several layers,
16671667+ in which the lower layers don't process the semantics of some of the
16681668+ CBOR data they forward. These layers can't notice any validity
16691669+ errors in data they don't process and MUST forward that data as-is.
16701670+ The first layer that does process the semantics of an invalid CBOR
16711671+ item MUST pick one of two choices:
16721672+16731673+ 1. Replace the problematic item with an error marker and continue
16741674+ with the next item, or
16751675+16761676+ 2. Issue an error and stop processing altogether.
16771677+16781678+ A CBOR-based protocol MUST specify which of these options its
16791679+ decoders take for each kind of invalid item they might encounter.
16801680+16811681+ Such problems might occur at the basic validity level of CBOR or in
16821682+ the context of tags (tag validity).
16831683+16841684+5.3.1. Basic validity
16851685+16861686+ Two kinds of validity errors can occur in the basic generic data
16871687+ model:
16881688+16891689+ Duplicate keys in a map: Generic decoders (Section 5.2) make data
16901690+ available to applications using the native CBOR data model. That
16911691+ data model includes maps (key-value mappings with unique keys),
16921692+ not multimaps (key-value mappings where multiple entries can have
16931693+ the same key). Thus, a generic decoder that gets a CBOR map item
16941694+ that has duplicate keys will decode to a map with only one
16951695+ instance of that key, or it might stop processing altogether. On
16961696+ the other hand, a "streaming decoder" may not even be able to
16971697+ notice. See Section 5.6 for more discussion of keys in maps.
16981698+16991699+ Invalid UTF-8 string: A decoder might or might not want to verify
17001700+ that the sequence of bytes in a UTF-8 string (major type 3) is
17011701+ actually valid UTF-8 and react appropriately.
17021702+17031703+5.3.2. Tag validity
17041704+17051705+ Two additional kinds of validity errors are introduced by adding tags
17061706+ to the basic generic data model:
17071707+17081708+ Inadmissible type for tag content: Tag numbers (Section 3.4) specify
17091709+ what type of data item is supposed to be used as their tag
17101710+ content; for example, the tag numbers for unsigned or negative
17111711+ bignums are supposed to be put on byte strings. A decoder that
17121712+ decodes the tagged data item into a native representation (a
17131713+ native big integer in this example) is expected to check the type
17141714+ of the data item being tagged. Even decoders that don't have such
17151715+ native representations available in their environment may perform
17161716+ the check on those tags known to them and react appropriately.
17171717+17181718+ Inadmissible value for tag content: The type of data item may be
17191719+ admissible for a tag's content, but the specific value may not be;
17201720+ e.g., a value of "yesterday" is not acceptable for the content of
17211721+ tag 0, even though it properly is a text string. A decoder that
17221722+ normally ingests such tags into equivalent platform types might
17231723+ present this tag to the application in a similar way to how it
17241724+ would present a tag with an unknown tag number (Section 5.4).
17251725+17261726+5.4. Validity and Evolution
17271727+17281728+ A decoder with validity checking will expend the effort to reliably
17291729+ detect data items with validity errors. For example, such a decoder
17301730+ needs to have an API that reports an error (and does not return data)
17311731+ for a CBOR data item that contains any of the validity errors listed
17321732+ in the previous subsection.
17331733+17341734+ The set of tags defined in the "Concise Binary Object Representation
17351735+ (CBOR) Tags" registry (Section 9.2), as well as the set of simple
17361736+ values defined in the "Concise Binary Object Representation (CBOR)
17371737+ Simple Values" registry (Section 9.1), can grow at any time beyond
17381738+ the set understood by a generic decoder. A validity-checking decoder
17391739+ can do one of two things when it encounters such a case that it does
17401740+ not recognize:
17411741+17421742+ * It can report an error (and not return data). Note that treating
17431743+ this case as an error can cause ossification and is thus not
17441744+ encouraged. This error is not a validity error, per se. This
17451745+ kind of error is more likely to be raised by a decoder that would
17461746+ be performing validity checking if this were a known case.
17471747+17481748+ * It can emit the unknown item (type, value, and, for tags, the
17491749+ decoded tagged data item) to the application calling the decoder,
17501750+ and then give the application an indication that the decoder did
17511751+ not recognize that tag number or simple value.
17521752+17531753+ The latter approach, which is also appropriate for decoders that do
17541754+ not support validity checking, provides forward compatibility with
17551755+ newly registered tags and simple values without the requirement to
17561756+ update the encoder at the same time as the calling application. (For
17571757+ this, the decoder's API needs the ability to mark unknown items so
17581758+ that the calling application can handle them in a manner appropriate
17591759+ for the program.)
17601760+17611761+ Since some of the processing needed for validity checking may have an
17621762+ appreciable cost (in particular with duplicate detection for maps),
17631763+ support of validity checking is not a requirement placed on all CBOR
17641764+ decoders.
17651765+17661766+ Some encoders will rely on their applications to provide input data
17671767+ in such a way that valid CBOR results from the encoder. A generic
17681768+ encoder may also want to provide a validity-checking mode where it
17691769+ reliably limits its output to valid CBOR, independent of whether or
17701770+ not its application is indeed providing API-conformant data.
17711771+17721772+5.5. Numbers
17731773+17741774+ CBOR-based protocols should take into account that different language
17751775+ environments pose different restrictions on the range and precision
17761776+ of numbers that are representable. For example, the basic JavaScript
17771777+ number system treats all numbers as floating-point values, which may
17781778+ result in the silent loss of precision in decoding integers with more
17791779+ than 53 significant bits. Another example is that, since CBOR keeps
17801780+ the sign bit for its integer representation in the major type, it has
17811781+ one bit more for signed numbers of a certain length (e.g.,
17821782+ -2^(64)..2^(64)-1 for 1+8-byte integers) than the typical platform
17831783+ signed integer representation of the same length (-2^(63)..2^(63)-1
17841784+ for 8-byte int64_t). A protocol that uses numbers should define its
17851785+ expectations on the handling of nontrivial numbers in decoders and
17861786+ receiving applications.
17871787+17881788+ A CBOR-based protocol that includes floating-point numbers can
17891789+ restrict which of the three formats (half-precision, single-
17901790+ precision, and double-precision) are to be supported. For an
17911791+ integer-only application, a protocol may want to completely exclude
17921792+ the use of floating-point values.
17931793+17941794+ A CBOR-based protocol designed for compactness may want to exclude
17951795+ specific integer encodings that are longer than necessary for the
17961796+ application, such as to save the need to implement 64-bit integers.
17971797+ There is an expectation that encoders will use the most compact
17981798+ integer representation that can represent a given value. However, a
17991799+ compact application that does not require deterministic encoding
18001800+ should accept values that use a longer-than-needed encoding (such as
18011801+ encoding "0" as 0b000_11001 followed by two bytes of 0x00) as long as
18021802+ the application can decode an integer of the given size. Similar
18031803+ considerations apply to floating-point values; decoding both
18041804+ preferred serializations and longer-than-needed ones is recommended.
18051805+18061806+ CBOR-based protocols for constrained applications that provide a
18071807+ choice between representing a specific number as an integer and as a
18081808+ decimal fraction or bigfloat (such as when the exponent is small and
18091809+ nonnegative) might express a quality-of-implementation expectation
18101810+ that the integer representation is used directly.
18111811+18121812+5.6. Specifying Keys for Maps
18131813+18141814+ The encoding and decoding applications need to agree on what types of
18151815+ keys are going to be used in maps. In applications that need to
18161816+ interwork with JSON-based applications, conversion is simplified by
18171817+ limiting keys to text strings only; otherwise, there has to be a
18181818+ specified mapping from the other CBOR types to text strings, and this
18191819+ often leads to implementation errors. In applications where keys are
18201820+ numeric in nature, and numeric ordering of keys is important to the
18211821+ application, directly using the numbers for the keys is useful.
18221822+18231823+ If multiple types of keys are to be used, consideration should be
18241824+ given to how these types would be represented in the specific
18251825+ programming environments that are to be used. For example, in
18261826+ JavaScript Maps [ECMA262], a key of integer 1 cannot be distinguished
18271827+ from a key of floating-point 1.0. This means that, if integer keys
18281828+ are used, the protocol needs to avoid the use of floating-point keys
18291829+ the values of which happen to be integer numbers in the same map.
18301830+18311831+ Decoders that deliver data items nested within a CBOR data item
18321832+ immediately on decoding them ("streaming decoders") often do not keep
18331833+ the state that is necessary to ascertain uniqueness of a key in a
18341834+ map. Similarly, an encoder that can start encoding data items before
18351835+ the enclosing data item is completely available ("streaming encoder")
18361836+ may want to reduce its overhead significantly by relying on its data
18371837+ source to maintain uniqueness.
18381838+18391839+ A CBOR-based protocol MUST define what to do when a receiving
18401840+ application sees multiple identical keys in a map. The resulting
18411841+ rule in the protocol MUST respect the CBOR data model: it cannot
18421842+ prescribe a specific handling of the entries with the identical keys,
18431843+ except that it might have a rule that having identical keys in a map
18441844+ indicates a malformed map and that the decoder has to stop with an
18451845+ error. When processing maps that exhibit entries with duplicate
18461846+ keys, a generic decoder might do one of the following:
18471847+18481848+ * Not accept maps with duplicate keys (that is, enforce validity for
18491849+ maps, see also Section 5.4). These generic decoders are
18501850+ universally useful. An application may still need to perform its
18511851+ own duplicate checking based on application rules (for instance,
18521852+ if the application equates integers and floating-point values in
18531853+ map key positions for specific maps).
18541854+18551855+ * Pass all map entries to the application, including ones with
18561856+ duplicate keys. This requires that the application handle (check
18571857+ against) duplicate keys, even if the application rules are
18581858+ identical to the generic data model rules.
18591859+18601860+ * Lose some entries with duplicate keys, e.g., deliver only the
18611861+ final (or first) entry out of the entries with the same key. With
18621862+ such a generic decoder, applications may get different results for
18631863+ a specific key on different runs, and with different generic
18641864+ decoders, which value is returned is based on generic decoder
18651865+ implementation and the actual order of keys in the map. In
18661866+ particular, applications cannot validate key uniqueness on their
18671867+ own as they do not necessarily see all entries; they may not be
18681868+ able to use such a generic decoder if they need to validate key
18691869+ uniqueness. These generic decoders can only be used in situations
18701870+ where the data source and transfer always provide valid maps; this
18711871+ is not possible if the data source and transfer can be attacked.
18721872+18731873+ Generic decoders need to document which of these three approaches
18741874+ they implement.
18751875+18761876+ The CBOR data model for maps does not allow ascribing semantics to
18771877+ the order of the key/value pairs in the map representation. Thus, a
18781878+ CBOR-based protocol MUST NOT specify that changing the key/value pair
18791879+ order in a map changes the semantics, except to specify that some
18801880+ orders are disallowed, for example, where they would not meet the
18811881+ requirements of a deterministic encoding (Section 4.2). (Any
18821882+ secondary effects of map ordering such as on timing, cache usage, and
18831883+ other potential side channels are not considered part of the
18841884+ semantics but may be enough reason on their own for a protocol to
18851885+ require a deterministic encoding format.)
18861886+18871887+ Applications for constrained devices should consider using small
18881888+ integers as keys if they have maps with a small number of frequently
18891889+ used keys; for instance, a set of 24 or fewer keys can be encoded in
18901890+ a single byte as unsigned integers, up to 48 if negative integers are
18911891+ also used. Less frequently occurring keys can then use integers with
18921892+ longer encodings.
18931893+18941894+5.6.1. Equivalence of Keys
18951895+18961896+ The specific data model that applies to a CBOR data item is used to
18971897+ determine whether keys occurring in maps are duplicates or distinct.
18981898+18991899+ At the generic data model level, numerically equivalent integer and
19001900+ floating-point values are distinct from each other, as they are from
19011901+ the various big numbers (Tags 2 to 5). Similarly, text strings are
19021902+ distinct from byte strings, even if composed of the same bytes. A
19031903+ tagged value is distinct from an untagged value or from a value
19041904+ tagged with a different tag number.
19051905+19061906+ Within each of these groups, numeric values are distinct unless they
19071907+ are numerically equal (specifically, -0.0 is equal to 0.0); for the
19081908+ purpose of map key equivalence, NaN values are equivalent if they
19091909+ have the same significand after zero-extending both significands at
19101910+ the right to 64 bits.
19111911+19121912+ Both byte strings and text strings are compared byte by byte, arrays
19131913+ are compared element by element, and are equal if they have the same
19141914+ number of bytes/elements and the same values at the same positions.
19151915+ Two maps are equal if they have the same set of pairs regardless of
19161916+ their order; pairs are equal if both the key and value are equal.
19171917+19181918+ Tagged values are equal if both the tag number and the tag content
19191919+ are equal. (Note that a generic decoder that provides processing for
19201920+ a specific tag may not be able to distinguish some semantically
19211921+ equivalent values, e.g., if leading zeroes occur in the content of
19221922+ tag 2 or tag 3 (Section 3.4.3).) Simple values are equal if they
19231923+ simply have the same value. Nothing else is equal in the generic
19241924+ data model; a simple value 2 is not equivalent to an integer 2, and
19251925+ an array is never equivalent to a map.
19261926+19271927+ As discussed in Section 2.2, specific data models can make values
19281928+ equivalent for the purpose of comparing map keys that are distinct in
19291929+ the generic data model. Note that this implies that a generic
19301930+ decoder may deliver a decoded map to an application that needs to be
19311931+ checked for duplicate map keys by that application (alternatively,
19321932+ the decoder may provide a programming interface to perform this
19331933+ service for the application). Specific data models are not able to
19341934+ distinguish values for map keys that are equal for this purpose at
19351935+ the generic data model level.
19361936+19371937+5.7. Undefined Values
19381938+19391939+ In some CBOR-based protocols, the simple value (Section 3.3) of
19401940+ "undefined" might be used by an encoder as a substitute for a data
19411941+ item with an encoding problem, in order to allow the rest of the
19421942+ enclosing data items to be encoded without harm.
19431943+19441944+6. Converting Data between CBOR and JSON
19451945+19461946+ This section gives non-normative advice about converting between CBOR
19471947+ and JSON. Implementations of converters MAY use whichever advice
19481948+ here they want.
19491949+19501950+ It is worth noting that a JSON text is a sequence of characters, not
19511951+ an encoded sequence of bytes, while a CBOR data item consists of
19521952+ bytes, not characters.
19531953+19541954+6.1. Converting from CBOR to JSON
19551955+19561956+ Most of the types in CBOR have direct analogs in JSON. However, some
19571957+ do not, and someone implementing a CBOR-to-JSON converter has to
19581958+ consider what to do in those cases. The following non-normative
19591959+ advice deals with these by converting them to a single substitute
19601960+ value, such as a JSON null.
19611961+19621962+ * An integer (major type 0 or 1) becomes a JSON number.
19631963+19641964+ * A byte string (major type 2) that is not embedded in a tag that
19651965+ specifies a proposed encoding is encoded in base64url without
19661966+ padding and becomes a JSON string.
19671967+19681968+ * A UTF-8 string (major type 3) becomes a JSON string. Note that
19691969+ JSON requires escaping certain characters ([RFC8259], Section 7):
19701970+ quotation mark (U+0022), reverse solidus (U+005C), and the "C0
19711971+ control characters" (U+0000 through U+001F). All other characters
19721972+ are copied unchanged into the JSON UTF-8 string.
19731973+19741974+ * An array (major type 4) becomes a JSON array.
19751975+19761976+ * A map (major type 5) becomes a JSON object. This is possible
19771977+ directly only if all keys are UTF-8 strings. A converter might
19781978+ also convert other keys into UTF-8 strings (such as by converting
19791979+ integers into strings containing their decimal representation);
19801980+ however, doing so introduces a danger of key collision. Note also
19811981+ that, if tags on UTF-8 strings are ignored as proposed below, this
19821982+ will cause a key collision if the tags are different but the
19831983+ strings are the same.
19841984+19851985+ * False (major type 7, additional information 20) becomes a JSON
19861986+ false.
19871987+19881988+ * True (major type 7, additional information 21) becomes a JSON
19891989+ true.
19901990+19911991+ * Null (major type 7, additional information 22) becomes a JSON
19921992+ null.
19931993+19941994+ * A floating-point value (major type 7, additional information 25
19951995+ through 27) becomes a JSON number if it is finite (that is, it can
19961996+ be represented in a JSON number); if the value is non-finite (NaN,
19971997+ or positive or negative Infinity), it is represented by the
19981998+ substitute value.
19991999+20002000+ * Any other simple value (major type 7, any additional information
20012001+ value not yet discussed) is represented by the substitute value.
20022002+20032003+ * A bignum (major type 6, tag number 2 or 3) is represented by
20042004+ encoding its byte string in base64url without padding and becomes
20052005+ a JSON string. For tag number 3 (negative bignum), a "~" (ASCII
20062006+ tilde) is inserted before the base-encoded value. (The conversion
20072007+ to a binary blob instead of a number is to prevent a likely
20082008+ numeric overflow for the JSON decoder.)
20092009+20102010+ * A byte string with an encoding hint (major type 6, tag number 21
20112011+ through 23) is encoded as described by the hint and becomes a JSON
20122012+ string.
20132013+20142014+ * For all other tags (major type 6, any other tag number), the tag
20152015+ content is represented as a JSON value; the tag number is ignored.
20162016+20172017+ * Indefinite-length items are made definite before conversion.
20182018+20192019+ A CBOR-to-JSON converter may want to keep to the JSON profile I-JSON
20202020+ [RFC7493], to maximize interoperability and increase confidence that
20212021+ the JSON output can be processed with predictable results. For
20222022+ example, this has implications on the range of integers that can be
20232023+ represented reliably, as well as on the top-level items that may be
20242024+ supported by older JSON implementations.
20252025+20262026+6.2. Converting from JSON to CBOR
20272027+20282028+ All JSON values, once decoded, directly map into one or more CBOR
20292029+ values. As with any kind of CBOR generation, decisions have to be
20302030+ made with respect to number representation. In a suggested
20312031+ conversion:
20322032+20332033+ * JSON numbers without fractional parts (integer numbers) are
20342034+ represented as integers (major types 0 and 1, possibly major type
20352035+ 6, tag number 2 and 3), choosing the shortest form; integers
20362036+ longer than an implementation-defined threshold may instead be
20372037+ represented as floating-point values. The default range that is
20382038+ represented as integer is -2^(53)+1..2^(53)-1 (fully exploiting
20392039+ the range for exact integers in the binary64 representation often
20402040+ used for decoding JSON [RFC7493]). A CBOR-based protocol, or a
20412041+ generic converter implementation, may choose -2^(32)..2^(32)-1 or
20422042+ -2^(64)..2^(64)-1 (fully using the integer ranges available in
20432043+ CBOR with uint32_t or uint64_t, respectively) or even
20442044+ -2^(31)..2^(31)-1 or -2^(63)..2^(63)-1 (using popular ranges for
20452045+ two's complement signed integers). (If the JSON was generated
20462046+ from a JavaScript implementation, its precision is already limited
20472047+ to 53 bits maximum.)
20482048+20492049+ * Numbers with fractional parts are represented as floating-point
20502050+ values, performing the decimal-to-binary conversion based on the
20512051+ precision provided by IEEE 754 binary64. The mathematical value
20522052+ of the JSON number is converted to binary64 using the
20532053+ roundTiesToEven procedure in Section 4.3.1 of [IEEE754]. Then,
20542054+ when encoding in CBOR, the preferred serialization uses the
20552055+ shortest floating-point representation exactly representing this
20562056+ conversion result; for instance, 1.5 is represented in a 16-bit
20572057+ floating-point value (not all implementations will be capable of
20582058+ efficiently finding the minimum form, though). Instead of using
20592059+ the default binary64 precision, there may be an implementation-
20602060+ defined limit to the precision of the conversion that will affect
20612061+ the precision of the represented values. Decimal representation
20622062+ should only be used on the CBOR side if that is specified in a
20632063+ protocol.
20642064+20652065+ CBOR has been designed to generally provide a more compact encoding
20662066+ than JSON. One implementation strategy that might come to mind is to
20672067+ perform a JSON-to-CBOR encoding in place in a single buffer. This
20682068+ strategy would need to carefully consider a number of pathological
20692069+ cases, such as that some strings represented with no or very few
20702070+ escapes and longer (or much longer) than 255 bytes may expand when
20712071+ encoded as UTF-8 strings in CBOR. Similarly, a few of the binary
20722072+ floating-point representations might cause expansion from some short
20732073+ decimal representations (1.1, 1e9) in JSON. This may be hard to get
20742074+ right, and any ensuing vulnerabilities may be exploited by an
20752075+ attacker.
20762076+20772077+7. Future Evolution of CBOR
20782078+20792079+ Successful protocols evolve over time. New ideas appear,
20802080+ implementation platforms improve, related protocols are developed and
20812081+ evolve, and new requirements from applications and protocols are
20822082+ added. Facilitating protocol evolution is therefore an important
20832083+ design consideration for any protocol development.
20842084+20852085+ For protocols that will use CBOR, CBOR provides some useful
20862086+ mechanisms to facilitate their evolution. Best practices for this
20872087+ are well known, particularly from JSON format development of JSON-
20882088+ based protocols. Therefore, such best practices are outside the
20892089+ scope of this specification.
20902090+20912091+ However, facilitating the evolution of CBOR itself is very well
20922092+ within its scope. CBOR is designed to both provide a stable basis
20932093+ for development of CBOR-based protocols and to be able to evolve.
20942094+ Since a successful protocol may live for decades, CBOR needs to be
20952095+ designed for decades of use and evolution. This section provides
20962096+ some guidance for the evolution of CBOR. It is necessarily more
20972097+ subjective than other parts of this document. It is also necessarily
20982098+ incomplete, lest it turn into a textbook on protocol development.
20992099+21002100+7.1. Extension Points
21012101+21022102+ In a protocol design, opportunities for evolution are often included
21032103+ in the form of extension points. For example, there may be a
21042104+ codepoint space that is not fully allocated from the outset, and the
21052105+ protocol is designed to tolerate and embrace implementations that
21062106+ start using more codepoints than initially allocated.
21072107+21082108+ Sizing the codepoint space may be difficult because the range
21092109+ required may be hard to predict. Protocol designs should attempt to
21102110+ make the codepoint space large enough so that it can slowly be filled
21112111+ over the intended lifetime of the protocol.
21122112+21132113+ CBOR has three major extension points:
21142114+21152115+ the "simple" space (values in major type 7): Of the 24 efficient
21162116+ (and 224 slightly less efficient) values, only a small number have
21172117+ been allocated. Implementations receiving an unknown simple data
21182118+ item may easily be able to process it as such, given that the
21192119+ structure of the value is indeed simple. The IANA registry in
21202120+ Section 9.1 is the appropriate way to address the extensibility of
21212121+ this codepoint space.
21222122+21232123+ the "tag" space (values in major type 6): The total codepoint space
21242124+ is abundant; only a tiny part of it has been allocated. However,
21252125+ not all of these codepoints are equally efficient: the first 24
21262126+ only consume a single ("1+0") byte, and half of them have already
21272127+ been allocated. The next 232 values only consume two ("1+1")
21282128+ bytes, with nearly a quarter already allocated. These subspaces
21292129+ need some curation to last for a few more decades.
21302130+ Implementations receiving an unknown tag number can choose to
21312131+ process just the enclosed tag content or, preferably, to process
21322132+ the tag as an unknown tag number wrapping the tag content. The
21332133+ IANA registry in Section 9.2 is the appropriate way to address the
21342134+ extensibility of this codepoint space.
21352135+21362136+ the "additional information" space: An implementation receiving an
21372137+ unknown additional information value has no way to continue
21382138+ decoding, so allocating codepoints in this space is a major step
21392139+ beyond just exercising an extension point. There are also very
21402140+ few codepoints left. See also Section 7.2.
21412141+21422142+7.2. Curating the Additional Information Space
21432143+21442144+ The human mind is sometimes drawn to filling in little perceived gaps
21452145+ to make something neat. We expect the remaining gaps in the
21462146+ codepoint space for the additional information values to be an
21472147+ attractor for new ideas, just because they are there.
21482148+21492149+ The present specification does not manage the additional information
21502150+ codepoint space by an IANA registry. Instead, allocations out of
21512151+ this space can only be done by updating this specification.
21522152+21532153+ For an additional information value of n >= 24, the size of the
21542154+ additional data typically is 2^(n-24) bytes. Therefore, additional
21552155+ information values 28 and 29 should be viewed as candidates for
21562156+ 128-bit and 256-bit quantities, in case a need arises to add them to
21572157+ the protocol. Additional information value 30 is then the only
21582158+ additional information value available for general allocation, and
21592159+ there should be a very good reason for allocating it before assigning
21602160+ it through an update of the present specification.
21612161+21622162+8. Diagnostic Notation
21632163+21642164+ CBOR is a binary interchange format. To facilitate documentation and
21652165+ debugging, and in particular to facilitate communication between
21662166+ entities cooperating in debugging, this section defines a simple
21672167+ human-readable diagnostic notation. All actual interchange always
21682168+ happens in the binary format.
21692169+21702170+ Note that this truly is a diagnostic format; it is not meant to be
21712171+ parsed. Therefore, no formal definition (as in ABNF) is given in
21722172+ this document. (Implementers looking for a text-based format for
21732173+ representing CBOR data items in configuration files may also want to
21742174+ consider YAML [YAML].)
21752175+21762176+ The diagnostic notation is loosely based on JSON as it is defined in
21772177+ RFC 8259, extending it where needed.
21782178+21792179+ The notation borrows the JSON syntax for numbers (integer and
21802180+ floating-point), True (>true<), False (>false<), Null (>null<), UTF-8
21812181+ strings, arrays, and maps (maps are called objects in JSON; the
21822182+ diagnostic notation extends JSON here by allowing any data item in
21832183+ the key position). Undefined is written >undefined< as in
21842184+ JavaScript. The non-finite floating-point numbers Infinity,
21852185+ -Infinity, and NaN are written exactly as in this sentence (this is
21862186+ also a way they can be written in JavaScript, although JSON does not
21872187+ allow them). A tag is written as an integer number for the tag
21882188+ number, followed by the tag content in parentheses; for instance, a
21892189+ date in the format specified by RFC 3339 (ISO 8601) could be notated
21902190+ as:
21912191+21922192+ 0("2013-03-21T20:04:00Z")
21932193+21942194+ or the equivalent relative time as the following:
21952195+21962196+ 1(1363896240)
21972197+21982198+ Byte strings are notated in one of the base encodings, without
21992199+ padding, enclosed in single quotes, prefixed by >h< for base16, >b32<
22002200+ for base32, >h32< for base32hex, >b64< for base64 or base64url (the
22012201+ actual encodings do not overlap, so the string remains unambiguous).
22022202+ For example, the byte string 0x12345678 could be written h'12345678',
22032203+ b32'CI2FM6A', or b64'EjRWeA'.
22042204+22052205+ Unassigned simple values are given as "simple()" with the appropriate
22062206+ integer in the parentheses. For example, "simple(42)" indicates
22072207+ major type 7, value 42.
22082208+22092209+ A number of useful extensions to the diagnostic notation defined here
22102210+ are provided in Appendix G of [RFC8610], "Extended Diagnostic
22112211+ Notation" (EDN). Similarly, this notation could be extended in a
22122212+ separate document to provide documentation for NaN payloads, which
22132213+ are not covered in this document.
22142214+22152215+8.1. Encoding Indicators
22162216+22172217+ Sometimes it is useful to indicate in the diagnostic notation which
22182218+ of several alternative representations were actually used; for
22192219+ example, a data item written >1.5< by a diagnostic decoder might have
22202220+ been encoded as a half-, single-, or double-precision float.
22212221+22222222+ The convention for encoding indicators is that anything starting with
22232223+ an underscore and all following characters that are alphanumeric or
22242224+ underscore is an encoding indicator, and can be ignored by anyone not
22252225+ interested in this information. For example, "_" or "_3". Encoding
22262226+ indicators are always optional.
22272227+22282228+ A single underscore can be written after the opening brace of a map
22292229+ or the opening bracket of an array to indicate that the data item was
22302230+ represented in indefinite-length format. For example, [_ 1, 2]
22312231+ contains an indicator that an indefinite-length representation was
22322232+ used to represent the data item [1, 2].
22332233+22342234+ An underscore followed by a decimal digit n indicates that the
22352235+ preceding item (or, for arrays and maps, the item starting with the
22362236+ preceding bracket or brace) was encoded with an additional
22372237+ information value of 24+n. For example, 1.5_1 is a half-precision
22382238+ floating-point number, while 1.5_3 is encoded as double precision.
22392239+ This encoding indicator is not shown in Appendix A. (Note that the
22402240+ encoding indicator "_" is thus an abbreviation of the full form "_7",
22412241+ which is not used.)
22422242+22432243+ The detailed chunk structure of byte and text strings of indefinite
22442244+ length can be notated in the form (_ h'0123', h'4567') and (_ "foo",
22452245+ "bar"). However, for an indefinite-length string with no chunks
22462246+ inside, (_ ) would be ambiguous as to whether a byte string (0x5fff)
22472247+ or a text string (0x7fff) is meant and is therefore not used. The
22482248+ basic forms ''_ and ""_ can be used instead and are reserved for the
22492249+ case of no chunks only -- not as short forms for the (permitted, but
22502250+ not really useful) encodings with only empty chunks, which need to be
22512251+ notated as (_ ''), (_ ""), etc., to preserve the chunk structure.
22522252+22532253+9. IANA Considerations
22542254+22552255+ IANA has created two registries for new CBOR values. The registries
22562256+ are separate, that is, not under an umbrella registry, and follow the
22572257+ rules in [RFC8126]. IANA has also assigned a new media type, an
22582258+ associated CoAP Content-Format entry, and a structured syntax suffix.
22592259+22602260+9.1. CBOR Simple Values Registry
22612261+22622262+ IANA has created the "Concise Binary Object Representation (CBOR)
22632263+ Simple Values" registry at [IANA.cbor-simple-values]. The initial
22642264+ values are shown in Table 4.
22652265+22662266+ New entries in the range 0 to 19 are assigned by Standards Action
22672267+ [RFC8126]. It is suggested that IANA allocate values starting with
22682268+ the number 16 in order to reserve the lower numbers for contiguous
22692269+ blocks (if any).
22702270+22712271+ New entries in the range 32 to 255 are assigned by Specification
22722272+ Required.
22732273+22742274+9.2. CBOR Tags Registry
22752275+22762276+ IANA has created the "Concise Binary Object Representation (CBOR)
22772277+ Tags" registry at [IANA.cbor-tags]. The tags that were defined in
22782278+ [RFC7049] are described in detail in Section 3.4, and other tags have
22792279+ already been defined since then.
22802280+22812281+ New entries in the range 0 to 23 ("1+0") are assigned by Standards
22822282+ Action. New entries in the ranges 24 to 255 ("1+1") and 256 to 32767
22832283+ (lower half of "1+2") are assigned by Specification Required. New
22842284+ entries in the range 32768 to 18446744073709551615 (upper half of
22852285+ "1+2", "1+4", and "1+8") are assigned by First Come First Served.
22862286+ The template for registration requests is:
22872287+22882288+ * Data item
22892289+22902290+ * Semantics (short form)
22912291+22922292+ In addition, First Come First Served requests should include:
22932293+22942294+ * Point of contact
22952295+22962296+ * Description of semantics (URL) -- This description is optional;
22972297+ the URL can point to something like an Internet-Draft or a web
22982298+ page.
22992299+23002300+ Applicants exercising the First Come First Served range and making a
23012301+ suggestion for a tag number that is not representable in 32 bits
23022302+ (i.e., larger than 4294967295) should be aware that this could reduce
23032303+ interoperability with implementations that do not support 64-bit
23042304+ numbers.
23052305+23062306+9.3. Media Types Registry
23072307+23082308+ The Internet media type [RFC6838] ("MIME type") for a single encoded
23092309+ CBOR data item is "application/cbor" as defined in the "Media Types"
23102310+ registry [IANA.media-types]:
23112311+23122312+ Type name: application
23132313+23142314+ Subtype name: cbor
23152315+23162316+ Required parameters: n/a
23172317+23182318+ Optional parameters: n/a
23192319+23202320+ Encoding considerations: Binary
23212321+23222322+ Security considerations: See Section 10 of RFC 8949.
23232323+23242324+ Interoperability considerations: n/a
23252325+23262326+ Published specification: RFC 8949
23272327+23282328+ Applications that use this media type: Many
23292329+23302330+ Additional information:
23312331+23322332+ Magic number(s): n/a
23332333+ File extension(s): .cbor
23342334+ Macintosh file type code(s): n/a
23352335+23362336+ Person & email address to contact for further information: IETF CBOR
23372337+ Working Group (cbor@ietf.org) or IETF Applications and Real-Time
23382338+ Area (art@ietf.org)
23392339+23402340+ Intended usage: COMMON
23412341+23422342+ Restrictions on usage: none
23432343+23442344+ Author: IETF CBOR Working Group (cbor@ietf.org)
23452345+23462346+ Change controller: The IESG (iesg@ietf.org)
23472347+23482348+9.4. CoAP Content-Format Registry
23492349+23502350+ The CoAP Content-Format for CBOR has been registered in the "CoAP
23512351+ Content-Formats" subregistry within the "Constrained RESTful
23522352+ Environments (CoRE) Parameters" registry [IANA.core-parameters]:
23532353+23542354+ Media Type: application/cbor
23552355+23562356+ Encoding: -
23572357+23582358+ ID: 60
23592359+23602360+ Reference: RFC 8949
23612361+23622362+9.5. Structured Syntax Suffix Registry
23632363+23642364+ The structured syntax suffix [RFC6838] for media types based on a
23652365+ single encoded CBOR data item is +cbor, which IANA has registered in
23662366+ the "Structured Syntax Suffixes" registry [IANA.structured-suffix]:
23672367+23682368+ Name: Concise Binary Object Representation (CBOR)
23692369+23702370+ +suffix: +cbor
23712371+23722372+ References: RFC 8949
23732373+23742374+ Encoding Considerations: CBOR is a binary format.
23752375+23762376+ Interoperability Considerations: n/a
23772377+23782378+ Fragment Identifier Considerations: The syntax and semantics of
23792379+ fragment identifiers specified for +cbor SHOULD be as specified
23802380+ for "application/cbor". (At publication of RFC 8949, there is no
23812381+ fragment identification syntax defined for "application/cbor".)
23822382+23832383+ The syntax and semantics for fragment identifiers for a specific
23842384+ "xxx/yyy+cbor" SHOULD be processed as follows:
23852385+23862386+ * For cases defined in +cbor, where the fragment identifier
23872387+ resolves per the +cbor rules, then process as specified in
23882388+ +cbor.
23892389+23902390+ * For cases defined in +cbor, where the fragment identifier does
23912391+ not resolve per the +cbor rules, then process as specified in
23922392+ "xxx/yyy+cbor".
23932393+23942394+ * For cases not defined in +cbor, then process as specified in
23952395+ "xxx/yyy+cbor".
23962396+23972397+ Security Considerations: See Section 10 of RFC 8949.
23982398+23992399+ Contact: IETF CBOR Working Group (cbor@ietf.org) or IETF
24002400+ Applications and Real-Time Area (art@ietf.org)
24012401+24022402+ Author/Change Controller: IETF
24032403+24042404+10. Security Considerations
24052405+24062406+ A network-facing application can exhibit vulnerabilities in its
24072407+ processing logic for incoming data. Complex parsers are well known
24082408+ as a likely source of such vulnerabilities, such as the ability to
24092409+ remotely crash a node, or even remotely execute arbitrary code on it.
24102410+ CBOR attempts to narrow the opportunities for introducing such
24112411+ vulnerabilities by reducing parser complexity, by giving the entire
24122412+ range of encodable values a meaning where possible.
24132413+24142414+ Because CBOR decoders are often used as a first step in processing
24152415+ unvalidated input, they need to be fully prepared for all types of
24162416+ hostile input that may be designed to corrupt, overrun, or achieve
24172417+ control of the system decoding the CBOR data item. A CBOR decoder
24182418+ needs to assume that all input may be hostile even if it has been
24192419+ checked by a firewall, has come over a secure channel such as TLS, is
24202420+ encrypted or signed, or has come from some other source that is
24212421+ presumed trusted.
24222422+24232423+ Section 4.1 gives examples of limitations in interoperability when
24242424+ using a constrained CBOR decoder with input from a CBOR encoder that
24252425+ uses a non-preferred serialization. When a single data item is
24262426+ consumed both by such a constrained decoder and a full decoder, it
24272427+ can lead to security issues that can be exploited by an attacker who
24282428+ can inject or manipulate content.
24292429+24302430+ As discussed throughout this document, there are many values that can
24312431+ be considered "equivalent" in some circumstances and "not equivalent"
24322432+ in others. As just one example, the numeric value for the number
24332433+ "one" might be expressed as an integer or a bignum. A system
24342434+ interpreting CBOR input might accept either form for the number
24352435+ "one", or might reject one (or both) forms. Such acceptance or
24362436+ rejection can have security implications in the program that is using
24372437+ the interpreted input.
24382438+24392439+ Hostile input may be constructed to overrun buffers, to overflow or
24402440+ underflow integer arithmetic, or to cause other decoding disruption.
24412441+ CBOR data items might have lengths or sizes that are intentionally
24422442+ extremely large or too short. Resource exhaustion attacks might
24432443+ attempt to lure a decoder into allocating very big data items
24442444+ (strings, arrays, maps, or even arbitrary precision numbers) or
24452445+ exhaust the stack depth by setting up deeply nested items. Decoders
24462446+ need to have appropriate resource management to mitigate these
24472447+ attacks. (Items for which very large sizes are given can also
24482448+ attempt to exploit integer overflow vulnerabilities.)
24492449+24502450+ A CBOR decoder, by definition, only accepts well-formed CBOR; this is
24512451+ the first step to its robustness. Input that is not well-formed CBOR
24522452+ causes no further processing from the point where the lack of well-
24532453+ formedness was detected. If possible, any data decoded up to this
24542454+ point should have no impact on the application using the CBOR
24552455+ decoder.
24562456+24572457+ In addition to ascertaining well-formedness, a CBOR decoder might
24582458+ also perform validity checks on the CBOR data. Alternatively, it can
24592459+ leave those checks to the application using the decoder. This choice
24602460+ needs to be clearly documented in the decoder. Beyond the validity
24612461+ at the CBOR level, an application also needs to ascertain that the
24622462+ input is in alignment with the application protocol that is
24632463+ serialized in CBOR.
24642464+24652465+ The input check itself may consume resources. This is usually linear
24662466+ in the size of the input, which means that an attacker has to spend
24672467+ resources that are commensurate to the resources spent by the
24682468+ defender on input validation. However, an attacker might be able to
24692469+ craft inputs that will take longer for a target decoder to process
24702470+ than for the attacker to produce. Processing for arbitrary-precision
24712471+ numbers may exceed linear effort. Also, some hash-table
24722472+ implementations that are used by decoders to build in-memory
24732473+ representations of maps can be attacked to spend quadratic effort,
24742474+ unless a secret key (see Section 7 of [SIPHASH_LNCS], also
24752475+ [SIPHASH_OPEN]) or some other mitigation is employed. Such
24762476+ superlinear efforts can be exploited by an attacker to exhaust
24772477+ resources at or before the input validator; they therefore need to be
24782478+ avoided in a CBOR decoder implementation. Note that tag number
24792479+ definitions and their implementations can add security considerations
24802480+ of this kind; this should then be discussed in the security
24812481+ considerations of the tag number definition.
24822482+24832483+ CBOR encoders do not receive input directly from the network and are
24842484+ thus not directly attackable in the same way as CBOR decoders.
24852485+ However, CBOR encoders often have an API that takes input from
24862486+ another level in the implementation and can be attacked through that
24872487+ API. The design and implementation of that API should assume the
24882488+ behavior of its caller may be based on hostile input or on coding
24892489+ mistakes. It should check inputs for buffer overruns, overflow and
24902490+ underflow of integer arithmetic, and other such errors that are aimed
24912491+ to disrupt the encoder.
24922492+24932493+ Protocols should be defined in such a way that potential multiple
24942494+ interpretations are reliably reduced to a single interpretation. For
24952495+ example, an attacker could make use of invalid input such as
24962496+ duplicate keys in maps, or exploit different precision in processing
24972497+ numbers to make one application base its decisions on a different
24982498+ interpretation than the one that will be used by a second
24992499+ application. To facilitate consistent interpretation, encoder and
25002500+ decoder implementations should provide a validity-checking mode of
25012501+ operation (Section 5.4). Note, however, that a generic decoder
25022502+ cannot know about all requirements that an application poses on its
25032503+ input data; it is therefore not relieving the application from
25042504+ performing its own input checking. Also, since the set of defined
25052505+ tag numbers evolves, the application may employ a tag number that is
25062506+ not yet supported for validity checking by the generic decoder it
25072507+ uses. Generic decoders therefore need to document which tag numbers
25082508+ they support and what validity checking they provide for those tag
25092509+ numbers as well as for basic CBOR (UTF-8 checking, duplicate map key
25102510+ checking).
25112511+25122512+ Section 3.4.3 notes that using the non-preferred choice of a bignum
25132513+ representation instead of a basic integer for encoding a number is
25142514+ not intended to have application semantics, but it can have such
25152515+ semantics if an application receiving CBOR data is using a decoder in
25162516+ the basic generic data model. This disparity causes a security issue
25172517+ if the two sets of semantics differ. Thus, applications using CBOR
25182518+ need to specify the data model that they are using for each use of
25192519+ CBOR data.
25202520+25212521+ It is common to convert CBOR data to other formats. In many cases,
25222522+ CBOR has more expressive types than other formats; this is
25232523+ particularly true for the common conversion to JSON. The loss of
25242524+ type information can cause security issues for the systems that are
25252525+ processing the less-expressive data.
25262526+25272527+ Section 6.2 describes a possibly common usage scenario of converting
25282528+ between CBOR and JSON that could allow an attack if the attacker
25292529+ knows that the application is performing the conversion.
25302530+25312531+ Security considerations for the use of base16 and base64 from
25322532+ [RFC4648], and the use of UTF-8 from [RFC3629], are relevant to CBOR
25332533+ as well.
25342534+25352535+11. References
25362536+25372537+11.1. Normative References
25382538+25392539+ [C] International Organization for Standardization,
25402540+ "Information technology - Programming languages - C",
25412541+ Fourth Edition, ISO/IEC 9899:2018, June 2018,
25422542+ <https://www.iso.org/standard/74528.html>.
25432543+25442544+ [Cplusplus20]
25452545+ International Organization for Standardization,
25462546+ "Programming languages - C++", Sixth Edition, ISO/IEC DIS
25472547+ 14882, ISO/IEC ISO/IEC JTC1 SC22 WG21 N 4860, March 2020,
25482548+ <https://isocpp.org/files/papers/N4860.pdf>.
25492549+25502550+ [IEEE754] IEEE, "IEEE Standard for Floating-Point Arithmetic", IEEE
25512551+ Std 754-2019, DOI 10.1109/IEEESTD.2019.8766229,
25522552+ <https://ieeexplore.ieee.org/document/8766229>.
25532553+25542554+ [RFC2045] Freed, N. and N. Borenstein, "Multipurpose Internet Mail
25552555+ Extensions (MIME) Part One: Format of Internet Message
25562556+ Bodies", RFC 2045, DOI 10.17487/RFC2045, November 1996,
25572557+ <https://www.rfc-editor.org/info/rfc2045>.
25582558+25592559+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
25602560+ Requirement Levels", BCP 14, RFC 2119,
25612561+ DOI 10.17487/RFC2119, March 1997,
25622562+ <https://www.rfc-editor.org/info/rfc2119>.
25632563+25642564+ [RFC3339] Klyne, G. and C. Newman, "Date and Time on the Internet:
25652565+ Timestamps", RFC 3339, DOI 10.17487/RFC3339, July 2002,
25662566+ <https://www.rfc-editor.org/info/rfc3339>.
25672567+25682568+ [RFC3629] Yergeau, F., "UTF-8, a transformation format of ISO
25692569+ 10646", STD 63, RFC 3629, DOI 10.17487/RFC3629, November
25702570+ 2003, <https://www.rfc-editor.org/info/rfc3629>.
25712571+25722572+ [RFC3986] Berners-Lee, T., Fielding, R., and L. Masinter, "Uniform
25732573+ Resource Identifier (URI): Generic Syntax", STD 66,
25742574+ RFC 3986, DOI 10.17487/RFC3986, January 2005,
25752575+ <https://www.rfc-editor.org/info/rfc3986>.
25762576+25772577+ [RFC4287] Nottingham, M., Ed. and R. Sayre, Ed., "The Atom
25782578+ Syndication Format", RFC 4287, DOI 10.17487/RFC4287,
25792579+ December 2005, <https://www.rfc-editor.org/info/rfc4287>.
25802580+25812581+ [RFC4648] Josefsson, S., "The Base16, Base32, and Base64 Data
25822582+ Encodings", RFC 4648, DOI 10.17487/RFC4648, October 2006,
25832583+ <https://www.rfc-editor.org/info/rfc4648>.
25842584+25852585+ [RFC8126] Cotton, M., Leiba, B., and T. Narten, "Guidelines for
25862586+ Writing an IANA Considerations Section in RFCs", BCP 26,
25872587+ RFC 8126, DOI 10.17487/RFC8126, June 2017,
25882588+ <https://www.rfc-editor.org/info/rfc8126>.
25892589+25902590+ [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
25912591+ 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
25922592+ May 2017, <https://www.rfc-editor.org/info/rfc8174>.
25932593+25942594+ [TIME_T] The Open Group, "The Open Group Base Specifications",
25952595+ Section 4.16, 'Seconds Since the Epoch', Issue 7, 2018
25962596+ Edition, IEEE Std 1003.1, 2018,
25972597+ <https://pubs.opengroup.org/onlinepubs/9699919799/
25982598+ basedefs/V1_chap04.html#tag_04_16>.
25992599+26002600+11.2. Informative References
26012601+26022602+ [ASN.1] International Telecommunication Union, "Information
26032603+ Technology - ASN.1 encoding rules: Specification of Basic
26042604+ Encoding Rules (BER), Canonical Encoding Rules (CER) and
26052605+ Distinguished Encoding Rules (DER)", ITU-T Recommendation
26062606+ X.690, 2015,
26072607+ <https://www.itu.int/rec/T-REC-X.690-201508-I/en>.
26082608+26092609+ [BSON] Various, "BSON - Binary JSON", <http://bsonspec.org/>.
26102610+26112611+ [CBOR-TAGS]
26122612+ Bormann, C., "Notable CBOR Tags", Work in Progress,
26132613+ Internet-Draft, draft-bormann-cbor-notable-tags-02, 25
26142614+ June 2020, <https://tools.ietf.org/html/draft-bormann-
26152615+ cbor-notable-tags-02>.
26162616+26172617+ [ECMA262] Ecma International, "ECMAScript 2020 Language
26182618+ Specification", Standard ECMA-262, 11th Edition, June
26192619+ 2020, <https://www.ecma-
26202620+ international.org/publications/standards/Ecma-262.htm>.
26212621+26222622+ [Err3764] RFC Errata, Erratum ID 3764, RFC 7049,
26232623+ <https://www.rfc-editor.org/errata/eid3764>.
26242624+26252625+ [Err3770] RFC Errata, Erratum ID 3770, RFC 7049,
26262626+ <https://www.rfc-editor.org/errata/eid3770>.
26272627+26282628+ [Err4294] RFC Errata, Erratum ID 4294, RFC 7049,
26292629+ <https://www.rfc-editor.org/errata/eid4294>.
26302630+26312631+ [Err4409] RFC Errata, Erratum ID 4409, RFC 7049,
26322632+ <https://www.rfc-editor.org/errata/eid4409>.
26332633+26342634+ [Err4963] RFC Errata, Erratum ID 4963, RFC 7049,
26352635+ <https://www.rfc-editor.org/errata/eid4963>.
26362636+26372637+ [Err4964] RFC Errata, Erratum ID 4964, RFC 7049,
26382638+ <https://www.rfc-editor.org/errata/eid4964>.
26392639+26402640+ [Err5434] RFC Errata, Erratum ID 5434, RFC 7049,
26412641+ <https://www.rfc-editor.org/errata/eid5434>.
26422642+26432643+ [Err5763] RFC Errata, Erratum ID 5763, RFC 7049,
26442644+ <https://www.rfc-editor.org/errata/eid5763>.
26452645+26462646+ [Err5917] RFC Errata, Erratum ID 5917, RFC 7049,
26472647+ <https://www.rfc-editor.org/errata/eid5917>.
26482648+26492649+ [IANA.cbor-simple-values]
26502650+ IANA, "Concise Binary Object Representation (CBOR) Simple
26512651+ Values",
26522652+ <https://www.iana.org/assignments/cbor-simple-values>.
26532653+26542654+ [IANA.cbor-tags]
26552655+ IANA, "Concise Binary Object Representation (CBOR) Tags",
26562656+ <https://www.iana.org/assignments/cbor-tags>.
26572657+26582658+ [IANA.core-parameters]
26592659+ IANA, "Constrained RESTful Environments (CoRE)
26602660+ Parameters",
26612661+ <https://www.iana.org/assignments/core-parameters>.
26622662+26632663+ [IANA.media-types]
26642664+ IANA, "Media Types",
26652665+ <https://www.iana.org/assignments/media-types>.
26662666+26672667+ [IANA.structured-suffix]
26682668+ IANA, "Structured Syntax Suffixes",
26692669+ <https://www.iana.org/assignments/media-type-structured-
26702670+ suffix>.
26712671+26722672+ [MessagePack]
26732673+ Furuhashi, S., "MessagePack", <https://msgpack.org/>.
26742674+26752675+ [PCRE] Hazel, P., "PCRE - Perl Compatible Regular Expressions",
26762676+ <https://www.pcre.org/>.
26772677+26782678+ [RFC0713] Haverty, J., "MSDTP-Message Services Data Transmission
26792679+ Protocol", RFC 713, DOI 10.17487/RFC0713, April 1976,
26802680+ <https://www.rfc-editor.org/info/rfc713>.
26812681+26822682+ [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type
26832683+ Specifications and Registration Procedures", BCP 13,
26842684+ RFC 6838, DOI 10.17487/RFC6838, January 2013,
26852685+ <https://www.rfc-editor.org/info/rfc6838>.
26862686+26872687+ [RFC7049] Bormann, C. and P. Hoffman, "Concise Binary Object
26882688+ Representation (CBOR)", RFC 7049, DOI 10.17487/RFC7049,
26892689+ October 2013, <https://www.rfc-editor.org/info/rfc7049>.
26902690+26912691+ [RFC7228] Bormann, C., Ersue, M., and A. Keranen, "Terminology for
26922692+ Constrained-Node Networks", RFC 7228,
26932693+ DOI 10.17487/RFC7228, May 2014,
26942694+ <https://www.rfc-editor.org/info/rfc7228>.
26952695+26962696+ [RFC7493] Bray, T., Ed., "The I-JSON Message Format", RFC 7493,
26972697+ DOI 10.17487/RFC7493, March 2015,
26982698+ <https://www.rfc-editor.org/info/rfc7493>.
26992699+27002700+ [RFC7991] Hoffman, P., "The "xml2rfc" Version 3 Vocabulary",
27012701+ RFC 7991, DOI 10.17487/RFC7991, December 2016,
27022702+ <https://www.rfc-editor.org/info/rfc7991>.
27032703+27042704+ [RFC8259] Bray, T., Ed., "The JavaScript Object Notation (JSON) Data
27052705+ Interchange Format", STD 90, RFC 8259,
27062706+ DOI 10.17487/RFC8259, December 2017,
27072707+ <https://www.rfc-editor.org/info/rfc8259>.
27082708+27092709+ [RFC8610] Birkholz, H., Vigano, C., and C. Bormann, "Concise Data
27102710+ Definition Language (CDDL): A Notational Convention to
27112711+ Express Concise Binary Object Representation (CBOR) and
27122712+ JSON Data Structures", RFC 8610, DOI 10.17487/RFC8610,
27132713+ June 2019, <https://www.rfc-editor.org/info/rfc8610>.
27142714+27152715+ [RFC8618] Dickinson, J., Hague, J., Dickinson, S., Manderson, T.,
27162716+ and J. Bond, "Compacted-DNS (C-DNS): A Format for DNS
27172717+ Packet Capture", RFC 8618, DOI 10.17487/RFC8618, September
27182718+ 2019, <https://www.rfc-editor.org/info/rfc8618>.
27192719+27202720+ [RFC8742] Bormann, C., "Concise Binary Object Representation (CBOR)
27212721+ Sequences", RFC 8742, DOI 10.17487/RFC8742, February 2020,
27222722+ <https://www.rfc-editor.org/info/rfc8742>.
27232723+27242724+ [RFC8746] Bormann, C., Ed., "Concise Binary Object Representation
27252725+ (CBOR) Tags for Typed Arrays", RFC 8746,
27262726+ DOI 10.17487/RFC8746, February 2020,
27272727+ <https://www.rfc-editor.org/info/rfc8746>.
27282728+27292729+ [SIPHASH_LNCS]
27302730+ Aumasson, J. and D. Bernstein, "SipHash: A Fast Short-
27312731+ Input PRF", Progress in Cryptology - INDOCRYPT 2012, pp.
27322732+ 489-508, DOI 10.1007/978-3-642-34931-7_28, 2012,
27332733+ <https://doi.org/10.1007/978-3-642-34931-7_28>.
27342734+27352735+ [SIPHASH_OPEN]
27362736+ Aumasson, J. and D.J. Bernstein, "SipHash: a fast short-
27372737+ input PRF", <https://www.aumasson.jp/siphash/siphash.pdf>.
27382738+27392739+ [YAML] Ben-Kiki, O., Evans, C., and I.d. Net, "YAML Ain't Markup
27402740+ Language (YAML[TM]) Version 1.2", 3rd Edition, October
27412741+ 2009, <https://www.yaml.org/spec/1.2/spec.html>.
27422742+27432743+Appendix A. Examples of Encoded CBOR Data Items
27442744+27452745+ The following table provides some CBOR-encoded values in hexadecimal
27462746+ (right column), together with diagnostic notation for these values
27472747+ (left column). Note that the string "\u00fc" is one form of
27482748+ diagnostic notation for a UTF-8 string containing the single Unicode
27492749+ character U+00FC (LATIN SMALL LETTER U WITH DIAERESIS, "ü").
27502750+ Similarly, "\u6c34" is a UTF-8 string in diagnostic notation with a
27512751+ single character U+6C34 (CJK UNIFIED IDEOGRAPH-6C34, "水"), often
27522752+ representing "water", and "\ud800\udd51" is a UTF-8 string in
27532753+ diagnostic notation with a single character U+10151 (GREEK ACROPHONIC
27542754+ ATTIC FIFTY STATERS, "𐅑"). (Note that all these single-character
27552755+ strings could also be represented in native UTF-8 in diagnostic
27562756+ notation, just not if an ASCII-only specification is required.) In
27572757+ the diagnostic notation provided for bignums, their intended numeric
27582758+ value is shown as a decimal number (such as 18446744073709551616)
27592759+ instead of a tagged byte string (such as 2(h'010000000000000000')).
27602760+27612761+ +==============================+====================================+
27622762+ |Diagnostic | Encoded |
27632763+ +==============================+====================================+
27642764+ |0 | 0x00 |
27652765+ +------------------------------+------------------------------------+
27662766+ |1 | 0x01 |
27672767+ +------------------------------+------------------------------------+
27682768+ |10 | 0x0a |
27692769+ +------------------------------+------------------------------------+
27702770+ |23 | 0x17 |
27712771+ +------------------------------+------------------------------------+
27722772+ |24 | 0x1818 |
27732773+ +------------------------------+------------------------------------+
27742774+ |25 | 0x1819 |
27752775+ +------------------------------+------------------------------------+
27762776+ |100 | 0x1864 |
27772777+ +------------------------------+------------------------------------+
27782778+ |1000 | 0x1903e8 |
27792779+ +------------------------------+------------------------------------+
27802780+ |1000000 | 0x1a000f4240 |
27812781+ +------------------------------+------------------------------------+
27822782+ |1000000000000 | 0x1b000000e8d4a51000 |
27832783+ +------------------------------+------------------------------------+
27842784+ |18446744073709551615 | 0x1bffffffffffffffff |
27852785+ +------------------------------+------------------------------------+
27862786+ |18446744073709551616 | 0xc249010000000000000000 |
27872787+ +------------------------------+------------------------------------+
27882788+ |-18446744073709551616 | 0x3bffffffffffffffff |
27892789+ +------------------------------+------------------------------------+
27902790+ |-18446744073709551617 | 0xc349010000000000000000 |
27912791+ +------------------------------+------------------------------------+
27922792+ |-1 | 0x20 |
27932793+ +------------------------------+------------------------------------+
27942794+ |-10 | 0x29 |
27952795+ +------------------------------+------------------------------------+
27962796+ |-100 | 0x3863 |
27972797+ +------------------------------+------------------------------------+
27982798+ |-1000 | 0x3903e7 |
27992799+ +------------------------------+------------------------------------+
28002800+ |0.0 | 0xf90000 |
28012801+ +------------------------------+------------------------------------+
28022802+ |-0.0 | 0xf98000 |
28032803+ +------------------------------+------------------------------------+
28042804+ |1.0 | 0xf93c00 |
28052805+ +------------------------------+------------------------------------+
28062806+ |1.1 | 0xfb3ff199999999999a |
28072807+ +------------------------------+------------------------------------+
28082808+ |1.5 | 0xf93e00 |
28092809+ +------------------------------+------------------------------------+
28102810+ |65504.0 | 0xf97bff |
28112811+ +------------------------------+------------------------------------+
28122812+ |100000.0 | 0xfa47c35000 |
28132813+ +------------------------------+------------------------------------+
28142814+ |3.4028234663852886e+38 | 0xfa7f7fffff |
28152815+ +------------------------------+------------------------------------+
28162816+ |1.0e+300 | 0xfb7e37e43c8800759c |
28172817+ +------------------------------+------------------------------------+
28182818+ |5.960464477539063e-8 | 0xf90001 |
28192819+ +------------------------------+------------------------------------+
28202820+ |0.00006103515625 | 0xf90400 |
28212821+ +------------------------------+------------------------------------+
28222822+ |-4.0 | 0xf9c400 |
28232823+ +------------------------------+------------------------------------+
28242824+ |-4.1 | 0xfbc010666666666666 |
28252825+ +------------------------------+------------------------------------+
28262826+ |Infinity | 0xf97c00 |
28272827+ +------------------------------+------------------------------------+
28282828+ |NaN | 0xf97e00 |
28292829+ +------------------------------+------------------------------------+
28302830+ |-Infinity | 0xf9fc00 |
28312831+ +------------------------------+------------------------------------+
28322832+ |Infinity | 0xfa7f800000 |
28332833+ +------------------------------+------------------------------------+
28342834+ |NaN | 0xfa7fc00000 |
28352835+ +------------------------------+------------------------------------+
28362836+ |-Infinity | 0xfaff800000 |
28372837+ +------------------------------+------------------------------------+
28382838+ |Infinity | 0xfb7ff0000000000000 |
28392839+ +------------------------------+------------------------------------+
28402840+ |NaN | 0xfb7ff8000000000000 |
28412841+ +------------------------------+------------------------------------+
28422842+ |-Infinity | 0xfbfff0000000000000 |
28432843+ +------------------------------+------------------------------------+
28442844+ |false | 0xf4 |
28452845+ +------------------------------+------------------------------------+
28462846+ |true | 0xf5 |
28472847+ +------------------------------+------------------------------------+
28482848+ |null | 0xf6 |
28492849+ +------------------------------+------------------------------------+
28502850+ |undefined | 0xf7 |
28512851+ +------------------------------+------------------------------------+
28522852+ |simple(16) | 0xf0 |
28532853+ +------------------------------+------------------------------------+
28542854+ |simple(255) | 0xf8ff |
28552855+ +------------------------------+------------------------------------+
28562856+ |0("2013-03-21T20:04:00Z") | 0xc074323031332d30332d32315432303a |
28572857+ | | 30343a30305a |
28582858+ +------------------------------+------------------------------------+
28592859+ |1(1363896240) | 0xc11a514b67b0 |
28602860+ +------------------------------+------------------------------------+
28612861+ |1(1363896240.5) | 0xc1fb41d452d9ec200000 |
28622862+ +------------------------------+------------------------------------+
28632863+ |23(h'01020304') | 0xd74401020304 |
28642864+ +------------------------------+------------------------------------+
28652865+ |24(h'6449455446') | 0xd818456449455446 |
28662866+ +------------------------------+------------------------------------+
28672867+ |32("http://www.example.com") | 0xd82076687474703a2f2f7777772e6578 |
28682868+ | | 616d706c652e636f6d |
28692869+ +------------------------------+------------------------------------+
28702870+ |h'' | 0x40 |
28712871+ +------------------------------+------------------------------------+
28722872+ |h'01020304' | 0x4401020304 |
28732873+ +------------------------------+------------------------------------+
28742874+ |"" | 0x60 |
28752875+ +------------------------------+------------------------------------+
28762876+ |"a" | 0x6161 |
28772877+ +------------------------------+------------------------------------+
28782878+ |"IETF" | 0x6449455446 |
28792879+ +------------------------------+------------------------------------+
28802880+ |"\"\\" | 0x62225c |
28812881+ +------------------------------+------------------------------------+
28822882+ |"\u00fc" | 0x62c3bc |
28832883+ +------------------------------+------------------------------------+
28842884+ |"\u6c34" | 0x63e6b0b4 |
28852885+ +------------------------------+------------------------------------+
28862886+ |"\ud800\udd51" | 0x64f0908591 |
28872887+ +------------------------------+------------------------------------+
28882888+ |[] | 0x80 |
28892889+ +------------------------------+------------------------------------+
28902890+ |[1, 2, 3] | 0x83010203 |
28912891+ +------------------------------+------------------------------------+
28922892+ |[1, [2, 3], [4, 5]] | 0x8301820203820405 |
28932893+ +------------------------------+------------------------------------+
28942894+ |[1, 2, 3, 4, 5, 6, 7, 8, 9, | 0x98190102030405060708090a0b0c0d0e |
28952895+ |10, 11, 12, 13, 14, 15, 16, | 0f101112131415161718181819 |
28962896+ |17, 18, 19, 20, 21, 22, 23, | |
28972897+ |24, 25] | |
28982898+ +------------------------------+------------------------------------+
28992899+ |{} | 0xa0 |
29002900+ +------------------------------+------------------------------------+
29012901+ |{1: 2, 3: 4} | 0xa201020304 |
29022902+ +------------------------------+------------------------------------+
29032903+ |{"a": 1, "b": [2, 3]} | 0xa26161016162820203 |
29042904+ +------------------------------+------------------------------------+
29052905+ |["a", {"b": "c"}] | 0x826161a161626163 |
29062906+ +------------------------------+------------------------------------+
29072907+ |{"a": "A", "b": "B", "c": "C",| 0xa5616161416162614261636143616461 |
29082908+ |"d": "D", "e": "E"} | 4461656145 |
29092909+ +------------------------------+------------------------------------+
29102910+ |(_ h'0102', h'030405') | 0x5f42010243030405ff |
29112911+ +------------------------------+------------------------------------+
29122912+ |(_ "strea", "ming") | 0x7f657374726561646d696e67ff |
29132913+ +------------------------------+------------------------------------+
29142914+ |[_ ] | 0x9fff |
29152915+ +------------------------------+------------------------------------+
29162916+ |[_ 1, [2, 3], [_ 4, 5]] | 0x9f018202039f0405ffff |
29172917+ +------------------------------+------------------------------------+
29182918+ |[_ 1, [2, 3], [4, 5]] | 0x9f01820203820405ff |
29192919+ +------------------------------+------------------------------------+
29202920+ |[1, [2, 3], [_ 4, 5]] | 0x83018202039f0405ff |
29212921+ +------------------------------+------------------------------------+
29222922+ |[1, [_ 2, 3], [4, 5]] | 0x83019f0203ff820405 |
29232923+ +------------------------------+------------------------------------+
29242924+ |[_ 1, 2, 3, 4, 5, 6, 7, 8, 9, | 0x9f0102030405060708090a0b0c0d0e0f |
29252925+ |10, 11, 12, 13, 14, 15, 16, | 101112131415161718181819ff |
29262926+ |17, 18, 19, 20, 21, 22, 23, | |
29272927+ |24, 25] | |
29282928+ +------------------------------+------------------------------------+
29292929+ |{_ "a": 1, "b": [_ 2, 3]} | 0xbf61610161629f0203ffff |
29302930+ +------------------------------+------------------------------------+
29312931+ |["a", {_ "b": "c"}] | 0x826161bf61626163ff |
29322932+ +------------------------------+------------------------------------+
29332933+ |{_ "Fun": true, "Amt": -2} | 0xbf6346756ef563416d7421ff |
29342934+ +------------------------------+------------------------------------+
29352935+29362936+ Table 6: Examples of Encoded CBOR Data Items
29372937+29382938+Appendix B. Jump Table for Initial Byte
29392939+29402940+ For brevity, this jump table does not show initial bytes that are
29412941+ reserved for future extension. It also only shows a selection of the
29422942+ initial bytes that can be used for optional features. (All unsigned
29432943+ integers are in network byte order.)
29442944+29452945+ +============+================================================+
29462946+ | Byte | Structure/Semantics |
29472947+ +============+================================================+
29482948+ | 0x00..0x17 | unsigned integer 0x00..0x17 (0..23) |
29492949+ +------------+------------------------------------------------+
29502950+ | 0x18 | unsigned integer (one-byte uint8_t follows) |
29512951+ +------------+------------------------------------------------+
29522952+ | 0x19 | unsigned integer (two-byte uint16_t follows) |
29532953+ +------------+------------------------------------------------+
29542954+ | 0x1a | unsigned integer (four-byte uint32_t follows) |
29552955+ +------------+------------------------------------------------+
29562956+ | 0x1b | unsigned integer (eight-byte uint64_t follows) |
29572957+ +------------+------------------------------------------------+
29582958+ | 0x20..0x37 | negative integer -1-0x00..-1-0x17 (-1..-24) |
29592959+ +------------+------------------------------------------------+
29602960+ | 0x38 | negative integer -1-n (one-byte uint8_t for n |
29612961+ | | follows) |
29622962+ +------------+------------------------------------------------+
29632963+ | 0x39 | negative integer -1-n (two-byte uint16_t for n |
29642964+ | | follows) |
29652965+ +------------+------------------------------------------------+
29662966+ | 0x3a | negative integer -1-n (four-byte uint32_t for |
29672967+ | | n follows) |
29682968+ +------------+------------------------------------------------+
29692969+ | 0x3b | negative integer -1-n (eight-byte uint64_t for |
29702970+ | | n follows) |
29712971+ +------------+------------------------------------------------+
29722972+ | 0x40..0x57 | byte string (0x00..0x17 bytes follow) |
29732973+ +------------+------------------------------------------------+
29742974+ | 0x58 | byte string (one-byte uint8_t for n, and then |
29752975+ | | n bytes follow) |
29762976+ +------------+------------------------------------------------+
29772977+ | 0x59 | byte string (two-byte uint16_t for n, and then |
29782978+ | | n bytes follow) |
29792979+ +------------+------------------------------------------------+
29802980+ | 0x5a | byte string (four-byte uint32_t for n, and |
29812981+ | | then n bytes follow) |
29822982+ +------------+------------------------------------------------+
29832983+ | 0x5b | byte string (eight-byte uint64_t for n, and |
29842984+ | | then n bytes follow) |
29852985+ +------------+------------------------------------------------+
29862986+ | 0x5f | byte string, byte strings follow, terminated |
29872987+ | | by "break" |
29882988+ +------------+------------------------------------------------+
29892989+ | 0x60..0x77 | UTF-8 string (0x00..0x17 bytes follow) |
29902990+ +------------+------------------------------------------------+
29912991+ | 0x78 | UTF-8 string (one-byte uint8_t for n, and then |
29922992+ | | n bytes follow) |
29932993+ +------------+------------------------------------------------+
29942994+ | 0x79 | UTF-8 string (two-byte uint16_t for n, and |
29952995+ | | then n bytes follow) |
29962996+ +------------+------------------------------------------------+
29972997+ | 0x7a | UTF-8 string (four-byte uint32_t for n, and |
29982998+ | | then n bytes follow) |
29992999+ +------------+------------------------------------------------+
30003000+ | 0x7b | UTF-8 string (eight-byte uint64_t for n, and |
30013001+ | | then n bytes follow) |
30023002+ +------------+------------------------------------------------+
30033003+ | 0x7f | UTF-8 string, UTF-8 strings follow, terminated |
30043004+ | | by "break" |
30053005+ +------------+------------------------------------------------+
30063006+ | 0x80..0x97 | array (0x00..0x17 data items follow) |
30073007+ +------------+------------------------------------------------+
30083008+ | 0x98 | array (one-byte uint8_t for n, and then n data |
30093009+ | | items follow) |
30103010+ +------------+------------------------------------------------+
30113011+ | 0x99 | array (two-byte uint16_t for n, and then n |
30123012+ | | data items follow) |
30133013+ +------------+------------------------------------------------+
30143014+ | 0x9a | array (four-byte uint32_t for n, and then n |
30153015+ | | data items follow) |
30163016+ +------------+------------------------------------------------+
30173017+ | 0x9b | array (eight-byte uint64_t for n, and then n |
30183018+ | | data items follow) |
30193019+ +------------+------------------------------------------------+
30203020+ | 0x9f | array, data items follow, terminated by |
30213021+ | | "break" |
30223022+ +------------+------------------------------------------------+
30233023+ | 0xa0..0xb7 | map (0x00..0x17 pairs of data items follow) |
30243024+ +------------+------------------------------------------------+
30253025+ | 0xb8 | map (one-byte uint8_t for n, and then n pairs |
30263026+ | | of data items follow) |
30273027+ +------------+------------------------------------------------+
30283028+ | 0xb9 | map (two-byte uint16_t for n, and then n pairs |
30293029+ | | of data items follow) |
30303030+ +------------+------------------------------------------------+
30313031+ | 0xba | map (four-byte uint32_t for n, and then n |
30323032+ | | pairs of data items follow) |
30333033+ +------------+------------------------------------------------+
30343034+ | 0xbb | map (eight-byte uint64_t for n, and then n |
30353035+ | | pairs of data items follow) |
30363036+ +------------+------------------------------------------------+
30373037+ | 0xbf | map, pairs of data items follow, terminated by |
30383038+ | | "break" |
30393039+ +------------+------------------------------------------------+
30403040+ | 0xc0 | text-based date/time (data item follows; see |
30413041+ | | Section 3.4.1) |
30423042+ +------------+------------------------------------------------+
30433043+ | 0xc1 | epoch-based date/time (data item follows; see |
30443044+ | | Section 3.4.2) |
30453045+ +------------+------------------------------------------------+
30463046+ | 0xc2 | unsigned bignum (data item "byte string" |
30473047+ | | follows) |
30483048+ +------------+------------------------------------------------+
30493049+ | 0xc3 | negative bignum (data item "byte string" |
30503050+ | | follows) |
30513051+ +------------+------------------------------------------------+
30523052+ | 0xc4 | decimal Fraction (data item "array" follows; |
30533053+ | | see Section 3.4.4) |
30543054+ +------------+------------------------------------------------+
30553055+ | 0xc5 | bigfloat (data item "array" follows; see |
30563056+ | | Section 3.4.4) |
30573057+ +------------+------------------------------------------------+
30583058+ | 0xc6..0xd4 | (tag) |
30593059+ +------------+------------------------------------------------+
30603060+ | 0xd5..0xd7 | expected conversion (data item follows; see |
30613061+ | | Section 3.4.5.2) |
30623062+ +------------+------------------------------------------------+
30633063+ | 0xd8..0xdb | (more tags; 1/2/4/8 bytes of tag number and |
30643064+ | | then a data item follow) |
30653065+ +------------+------------------------------------------------+
30663066+ | 0xe0..0xf3 | (simple value) |
30673067+ +------------+------------------------------------------------+
30683068+ | 0xf4 | false |
30693069+ +------------+------------------------------------------------+
30703070+ | 0xf5 | true |
30713071+ +------------+------------------------------------------------+
30723072+ | 0xf6 | null |
30733073+ +------------+------------------------------------------------+
30743074+ | 0xf7 | undefined |
30753075+ +------------+------------------------------------------------+
30763076+ | 0xf8 | (simple value, one byte follows) |
30773077+ +------------+------------------------------------------------+
30783078+ | 0xf9 | half-precision float (two-byte IEEE 754) |
30793079+ +------------+------------------------------------------------+
30803080+ | 0xfa | single-precision float (four-byte IEEE 754) |
30813081+ +------------+------------------------------------------------+
30823082+ | 0xfb | double-precision float (eight-byte IEEE 754) |
30833083+ +------------+------------------------------------------------+
30843084+ | 0xff | "break" stop code |
30853085+ +------------+------------------------------------------------+
30863086+30873087+ Table 7: Jump Table for Initial Byte
30883088+30893089+Appendix C. Pseudocode
30903090+30913091+ The well-formedness of a CBOR item can be checked by the pseudocode
30923092+ in Figure 1. The data is well-formed if and only if:
30933093+30943094+ * the pseudocode does not "fail";
30953095+30963096+ * after execution of the pseudocode, no bytes are left in the input
30973097+ (except in streaming applications).
30983098+30993099+ The pseudocode has the following prerequisites:
31003100+31013101+ * take(n) reads n bytes from the input data and returns them as a
31023102+ byte string. If n bytes are no longer available, take(n) fails.
31033103+31043104+ * uint() converts a byte string into an unsigned integer by
31053105+ interpreting the byte string in network byte order.
31063106+31073107+ * Arithmetic works as in C.
31083108+31093109+ * All variables are unsigned integers of sufficient range.
31103110+31113111+ Note that "well_formed" returns the major type for well-formed
31123112+ definite-length items, but 99 for an indefinite-length item (or -1
31133113+ for a "break" stop code, only if "breakable" is set). This is used
31143114+ in "well_formed_indefinite" to ascertain that indefinite-length
31153115+ strings only contain definite-length strings as chunks.
31163116+31173117+ well_formed(breakable = false) {
31183118+ // process initial bytes
31193119+ ib = uint(take(1));
31203120+ mt = ib >> 5;
31213121+ val = ai = ib & 0x1f;
31223122+ switch (ai) {
31233123+ case 24: val = uint(take(1)); break;
31243124+ case 25: val = uint(take(2)); break;
31253125+ case 26: val = uint(take(4)); break;
31263126+ case 27: val = uint(take(8)); break;
31273127+ case 28: case 29: case 30: fail();
31283128+ case 31:
31293129+ return well_formed_indefinite(mt, breakable);
31303130+ }
31313131+ // process content
31323132+ switch (mt) {
31333133+ // case 0, 1, 7 do not have content; just use val
31343134+ case 2: case 3: take(val); break; // bytes/UTF-8
31353135+ case 4: for (i = 0; i < val; i++) well_formed(); break;
31363136+ case 5: for (i = 0; i < val*2; i++) well_formed(); break;
31373137+ case 6: well_formed(); break; // 1 embedded data item
31383138+ case 7: if (ai == 24 && val < 32) fail(); // bad simple
31393139+ }
31403140+ return mt; // definite-length data item
31413141+ }
31423142+31433143+ well_formed_indefinite(mt, breakable) {
31443144+ switch (mt) {
31453145+ case 2: case 3:
31463146+ while ((it = well_formed(true)) != -1)
31473147+ if (it != mt) // need definite-length chunk
31483148+ fail(); // of same type
31493149+ break;
31503150+ case 4: while (well_formed(true) != -1); break;
31513151+ case 5: while (well_formed(true) != -1) well_formed(); break;
31523152+ case 7:
31533153+ if (breakable)
31543154+ return -1; // signal break out
31553155+ else fail(); // no enclosing indefinite
31563156+ default: fail(); // wrong mt
31573157+ }
31583158+ return 99; // indefinite-length data item
31593159+ }
31603160+31613161+ Figure 1: Pseudocode for Well-Formedness Check
31623162+31633163+ Note that the remaining complexity of a complete CBOR decoder is
31643164+ about presenting data that has been decoded to the application in an
31653165+ appropriate form.
31663166+31673167+ Major types 0 and 1 are designed in such a way that they can be
31683168+ encoded in C from a signed integer without actually doing an if-then-
31693169+ else for positive/negative (Figure 2). This uses the fact that
31703170+ (-1-n), the transformation for major type 1, is the same as ~n
31713171+ (bitwise complement) in C unsigned arithmetic; ~n can then be
31723172+ expressed as (-1)^n for the negative case, while 0^n leaves n
31733173+ unchanged for nonnegative. The sign of a number can be converted to
31743174+ -1 for negative and 0 for nonnegative (0 or positive) by arithmetic-
31753175+ shifting the number by one bit less than the bit length of the number
31763176+ (for example, by 63 for 64-bit numbers).
31773177+31783178+ void encode_sint(int64_t n) {
31793179+ uint64t ui = n >> 63; // extend sign to whole length
31803180+ unsigned mt = ui & 0x20; // extract (shifted) major type
31813181+ ui ^= n; // complement negatives
31823182+ if (ui < 24)
31833183+ *p++ = mt + ui;
31843184+ else if (ui < 256) {
31853185+ *p++ = mt + 24;
31863186+ *p++ = ui;
31873187+ } else
31883188+ ...
31893189+31903190+ Figure 2: Pseudocode for Encoding a Signed Integer
31913191+31923192+ See Section 1.2 for some specific assumptions about the profile of
31933193+ the C language used in these pieces of code.
31943194+31953195+Appendix D. Half-Precision
31963196+31973197+ As half-precision floating-point numbers were only added to IEEE 754
31983198+ in 2008 [IEEE754], today's programming platforms often still only
31993199+ have limited support for them. It is very easy to include at least
32003200+ decoding support for them even without such support. An example of a
32013201+ small decoder for half-precision floating-point numbers in the C
32023202+ language is shown in Figure 3. A similar program for Python is in
32033203+ Figure 4; this code assumes that the 2-byte value has already been
32043204+ decoded as an (unsigned short) integer in network byte order (as
32053205+ would be done by the pseudocode in Appendix C).
32063206+32073207+ #include <math.h>
32083208+32093209+ double decode_half(unsigned char *halfp) {
32103210+ unsigned half = (halfp[0] << 8) + halfp[1];
32113211+ unsigned exp = (half >> 10) & 0x1f;
32123212+ unsigned mant = half & 0x3ff;
32133213+ double val;
32143214+ if (exp == 0) val = ldexp(mant, -24);
32153215+ else if (exp != 31) val = ldexp(mant + 1024, exp - 25);
32163216+ else val = mant == 0 ? INFINITY : NAN;
32173217+ return half & 0x8000 ? -val : val;
32183218+ }
32193219+32203220+ Figure 3: C Code for a Half-Precision Decoder
32213221+32223222+ import struct
32233223+ from math import ldexp
32243224+32253225+ def decode_single(single):
32263226+ return struct.unpack("!f", struct.pack("!I", single))[0]
32273227+32283228+ def decode_half(half):
32293229+ valu = (half & 0x7fff) << 13 | (half & 0x8000) << 16
32303230+ if ((half & 0x7c00) != 0x7c00):
32313231+ return ldexp(decode_single(valu), 112)
32323232+ return decode_single(valu | 0x7f800000)
32333233+32343234+ Figure 4: Python Code for a Half-Precision Decoder
32353235+32363236+Appendix E. Comparison of Other Binary Formats to CBOR's Design
32373237+ Objectives
32383238+32393239+ The proposal for CBOR follows a history of binary formats that is as
32403240+ long as the history of computers themselves. Different formats have
32413241+ had different objectives. In most cases, the objectives of the
32423242+ format were never stated, although they can sometimes be implied by
32433243+ the context where the format was first used. Some formats were meant
32443244+ to be universally usable, although history has proven that no binary
32453245+ format meets the needs of all protocols and applications.
32463246+32473247+ CBOR differs from many of these formats due to it starting with a set
32483248+ of objectives and attempting to meet just those. This section
32493249+ compares a few of the dozens of formats with CBOR's objectives in
32503250+ order to help the reader decide if they want to use CBOR or a
32513251+ different format for a particular protocol or application.
32523252+32533253+ Note that the discussion here is not meant to be a criticism of any
32543254+ format: to the best of our knowledge, no format before CBOR was meant
32553255+ to cover CBOR's objectives in the priority we have assigned them. A
32563256+ brief recap of the objectives from Section 1.1 is:
32573257+32583258+ 1. unambiguous encoding of most common data formats from Internet
32593259+ standards
32603260+32613261+ 2. code compactness for encoder or decoder
32623262+32633263+ 3. no schema description needed
32643264+32653265+ 4. reasonably compact serialization
32663266+32673267+ 5. applicability to constrained and unconstrained applications
32683268+32693269+ 6. good JSON conversion
32703270+32713271+ 7. extensibility
32723272+32733273+ A discussion of CBOR and other formats with respect to a different
32743274+ set of design objectives is provided in Section 5 and Appendix C of
32753275+ [RFC8618].
32763276+32773277+E.1. ASN.1 DER, BER, and PER
32783278+32793279+ [ASN.1] has many serializations. In the IETF, DER and BER are the
32803280+ most common. The serialized output is not particularly compact for
32813281+ many items, and the code needed to decode numeric items can be
32823282+ complex on a constrained device.
32833283+32843284+ Few (if any) IETF protocols have adopted one of the several variants
32853285+ of Packed Encoding Rules (PER). There could be many reasons for
32863286+ this, but one that is commonly stated is that PER makes use of the
32873287+ schema even for parsing the surface structure of the data item,
32883288+ requiring significant tool support. There are different versions of
32893289+ the ASN.1 schema language in use, which has also hampered adoption.
32903290+32913291+E.2. MessagePack
32923292+32933293+ [MessagePack] is a concise, widely implemented counted binary
32943294+ serialization format, similar in many properties to CBOR, although
32953295+ somewhat less regular. While the data model can be used to represent
32963296+ JSON data, MessagePack has also been used in many remote procedure
32973297+ call (RPC) applications and for long-term storage of data.
32983298+32993299+ MessagePack has been essentially stable since it was first published
33003300+ around 2011; it has not yet had a transition. The evolution of
33013301+ MessagePack is impeded by an imperative to maintain complete
33023302+ backwards compatibility with existing stored data, while only few
33033303+ bytecodes are still available for extension. Repeated requests over
33043304+ the years from the MessagePack user community to separate out binary
33053305+ and text strings in the encoding recently have led to an extension
33063306+ proposal that would leave MessagePack's "raw" data ambiguous between
33073307+ its usages for binary and text data. The extension mechanism for
33083308+ MessagePack remains unclear.
33093309+33103310+E.3. BSON
33113311+33123312+ [BSON] is a data format that was developed for the storage of JSON-
33133313+ like maps (JSON objects) in the MongoDB database. Its major
33143314+ distinguishing feature is the capability for in-place update, which
33153315+ prevents a compact representation. BSON uses a counted
33163316+ representation except for map keys, which are null-byte terminated.
33173317+ While BSON can be used for the representation of JSON-like objects on
33183318+ the wire, its specification is dominated by the requirements of the
33193319+ database application and has become somewhat baroque. The status of
33203320+ how BSON extensions will be implemented remains unclear.
33213321+33223322+E.4. MSDTP: RFC 713
33233323+33243324+ Message Services Data Transmission (MSDTP) is a very early example of
33253325+ a compact message format; it is described in [RFC0713], written in
33263326+ 1976. It is included here for its historical value, not because it
33273327+ was ever widely used.
33283328+33293329+E.5. Conciseness on the Wire
33303330+33313331+ While CBOR's design objective of code compactness for encoders and
33323332+ decoders is a higher priority than its objective of conciseness on
33333333+ the wire, many people focus on the wire size. Table 8 shows some
33343334+ encoding examples for the simple nested array [1, [2, 3]]; where some
33353335+ form of indefinite-length encoding is supported by the encoding,
33363336+ [_ 1, [2, 3]] (indefinite length on the outer array) is also shown.
33373337+33383338+ +=============+============================+================+
33393339+ | Format | [1, [2, 3]] | [_ 1, [2, 3]] |
33403340+ +=============+============================+================+
33413341+ | RFC 713 | c2 05 81 c2 02 82 83 | |
33423342+ +-------------+----------------------------+----------------+
33433343+ | ASN.1 BER | 30 0b 02 01 01 30 06 02 01 | 30 80 02 01 01 |
33443344+ | | 02 02 01 03 | 30 06 02 01 02 |
33453345+ | | | 02 01 03 00 00 |
33463346+ +-------------+----------------------------+----------------+
33473347+ | MessagePack | 92 01 92 02 03 | |
33483348+ +-------------+----------------------------+----------------+
33493349+ | BSON | 22 00 00 00 10 30 00 01 00 | |
33503350+ | | 00 00 04 31 00 13 00 00 00 | |
33513351+ | | 10 30 00 02 00 00 00 10 31 | |
33523352+ | | 00 03 00 00 00 00 00 | |
33533353+ +-------------+----------------------------+----------------+
33543354+ | CBOR | 82 01 82 02 03 | 9f 01 82 02 03 |
33553355+ | | | ff |
33563356+ +-------------+----------------------------+----------------+
33573357+33583358+ Table 8: Examples for Different Levels of Conciseness
33593359+33603360+Appendix F. Well-Formedness Errors and Examples
33613361+33623362+ There are three basic kinds of well-formedness errors that can occur
33633363+ in decoding a CBOR data item:
33643364+33653365+ Too much data: There are input bytes left that were not consumed.
33663366+ This is only an error if the application assumed that the input
33673367+ bytes would span exactly one data item. Where the application
33683368+ uses the self-delimiting nature of CBOR encoding to permit
33693369+ additional data after the data item, as is done in CBOR sequences
33703370+ [RFC8742], for example, the CBOR decoder can simply indicate which
33713371+ part of the input has not been consumed.
33723372+33733373+ Too little data: The input data available would need additional
33743374+ bytes added at their end for a complete CBOR data item. This may
33753375+ indicate the input is truncated; it is also a common error when
33763376+ trying to decode random data as CBOR. For some applications,
33773377+ however, this may not actually be an error, as the application may
33783378+ not be certain it has all the data yet and can obtain or wait for
33793379+ additional input bytes. Some of these applications may have an
33803380+ upper limit for how much additional data can appear; here the
33813381+ decoder may be able to indicate that the encoded CBOR data item
33823382+ cannot be completed within this limit.
33833383+33843384+ Syntax error: The input data are not consistent with the
33853385+ requirements of the CBOR encoding, and this cannot be remedied by
33863386+ adding (or removing) data at the end.
33873387+33883388+ In Appendix C, errors of the first kind are addressed in the first
33893389+ paragraph and bullet list (requiring "no bytes are left"), and errors
33903390+ of the second kind are addressed in the second paragraph/bullet list
33913391+ (failing "if n bytes are no longer available"). Errors of the third
33923392+ kind are identified in the pseudocode by specific instances of
33933393+ calling fail(), in order:
33943394+33953395+ * a reserved value is used for additional information (28, 29, 30)
33963396+33973397+ * major type 7, additional information 24, value < 32 (incorrect)
33983398+33993399+ * incorrect substructure of indefinite-length byte string or text
34003400+ string (may only contain definite-length strings of the same major
34013401+ type)
34023402+34033403+ * "break" stop code (major type 7, additional information 31) occurs
34043404+ in a value position of a map or except at a position directly in
34053405+ an indefinite-length item where also another enclosed data item
34063406+ could occur
34073407+34083408+ * additional information 31 used with major type 0, 1, or 6
34093409+34103410+F.1. Examples of CBOR Data Items That Are Not Well-Formed
34113411+34123412+ This subsection shows a few examples for CBOR data items that are not
34133413+ well-formed. Each example is a sequence of bytes, each shown in
34143414+ hexadecimal; multiple examples in a list are separated by commas.
34153415+34163416+ Examples for well-formedness error kind 1 (too much data) can easily
34173417+ be formed by adding data to a well-formed encoded CBOR data item.
34183418+34193419+ Similarly, examples for well-formedness error kind 2 (too little
34203420+ data) can be formed by truncating a well-formed encoded CBOR data
34213421+ item. In test suites, it may be beneficial to specifically test with
34223422+ incomplete data items that would require large amounts of addition to
34233423+ be completed (for instance by starting the encoding of a string of a
34243424+ very large size).
34253425+34263426+ A premature end of the input can occur in a head or within the
34273427+ enclosed data, which may be bare strings or enclosed data items that
34283428+ are either counted or should have been ended by a "break" stop code.
34293429+34303430+ End of input in a head: 18, 19, 1a, 1b, 19 01, 1a 01 02, 1b 01 02 03
34313431+ 04 05 06 07, 38, 58, 78, 98, 9a 01 ff 00, b8, d8, f8, f9 00, fa 00
34323432+ 00, fb 00 00 00
34333433+34343434+ Definite-length strings with short data: 41, 61, 5a ff ff ff ff 00,
34353435+ 5b ff ff ff ff ff ff ff ff 01 02 03, 7a ff ff ff ff 00, 7b 7f ff
34363436+ ff ff ff ff ff ff 01 02 03
34373437+34383438+ Definite-length maps and arrays not closed with enough items: 81, 81
34393439+ 81 81 81 81 81 81 81 81, 82 00, a1, a2 01 02, a1 00, a2 00 00 00
34403440+34413441+ Tag number not followed by tag content: c0
34423442+34433443+ Indefinite-length strings not closed by a "break" stop code: 5f 41
34443444+ 00, 7f 61 00
34453445+34463446+ Indefinite-length maps and arrays not closed by a "break" stop
34473447+ code: 9f, 9f 01 02, bf, bf 01 02 01 02, 81 9f, 9f 80 00, 9f 9f 9f 9f
34483448+ 9f ff ff ff ff, 9f 81 9f 81 9f 9f ff ff ff
34493449+34503450+ A few examples for the five subkinds of well-formedness error kind 3
34513451+ (syntax error) are shown below.
34523452+34533453+ Subkind 1:
34543454+ Reserved additional information values: 1c, 1d, 1e, 3c, 3d, 3e,
34553455+ 5c, 5d, 5e, 7c, 7d, 7e, 9c, 9d, 9e, bc, bd, be, dc, dd, de, fc,
34563456+ fd, fe,
34573457+34583458+ Subkind 2:
34593459+ Reserved two-byte encodings of simple values: f8 00, f8 01, f8
34603460+ 18, f8 1f
34613461+34623462+ Subkind 3:
34633463+ Indefinite-length string chunks not of the correct type: 5f 00
34643464+ ff, 5f 21 ff, 5f 61 00 ff, 5f 80 ff, 5f a0 ff, 5f c0 00 ff, 5f
34653465+ e0 ff, 7f 41 00 ff
34663466+34673467+ Indefinite-length string chunks not definite length: 5f 5f 41 00
34683468+ ff ff, 7f 7f 61 00 ff ff
34693469+34703470+ Subkind 4:
34713471+ Break occurring on its own outside of an indefinite-length
34723472+ item: ff
34733473+34743474+ Break occurring in a definite-length array or map or a tag: 81
34753475+ ff, 82 00 ff, a1 ff, a1 ff 00, a1 00 ff, a2 00 00 ff, 9f 81 ff,
34763476+ 9f 82 9f 81 9f 9f ff ff ff ff
34773477+34783478+ Break in an indefinite-length map that would lead to an odd
34793479+ number of items (break in a value position): bf 00 ff, bf 00 00
34803480+ 00 ff
34813481+34823482+ Subkind 5:
34833483+ Major type 0, 1, 6 with additional information 31: 1f, 3f, df
34843484+34853485+Appendix G. Changes from RFC 7049
34863486+34873487+ As discussed in the introduction, this document formally obsoletes
34883488+ RFC 7049 while keeping full compatibility with the interchange format
34893489+ from RFC 7049. This document provides editorial improvements, added
34903490+ detail, and fixed errata. This document does not create a new
34913491+ version of the format.
34923492+34933493+G.1. Errata Processing and Clerical Changes
34943494+34953495+ The two verified errata on RFC 7049, [Err3764] and [Err3770],
34963496+ concerned two encoding examples in the text that have been corrected
34973497+ (Section 3.4.3: "29" -> "49", Section 5.5: "0b000_11101" ->
34983498+ "0b000_11001"). Also, RFC 7049 contained an example using the
34993499+ numeric value 24 for a simple value [Err5917], which is not well-
35003500+ formed; this example has been removed. Errata report 5763 [Err5763]
35013501+ pointed to an error in the wording of the definition of tags; this
35023502+ was resolved during a rewrite of Section 3.4. Errata report 5434
35033503+ [Err5434] pointed out that the Universal Binary JSON (UBJSON) example
35043504+ in Appendix E no longer complied with the version of UBJSON current
35053505+ at the time of the errata report submission. It turned out that the
35063506+ UBJSON specification had completely changed since 2013; this example
35073507+ therefore was removed. Other errata reports [Err4409] [Err4963]
35083508+ [Err4964] complained that the map key sorting rules for canonical
35093509+ encoding were onerous; these led to a reconsideration of the
35103510+ canonical encoding suggestions and replacement by the deterministic
35113511+ encoding suggestions (described below). An editorial suggestion in
35123512+ errata report 4294 [Err4294] was also implemented (improved symmetry
35133513+ by adding "Second value" to a comment to the last example in
35143514+ Section 3.2.2).
35153515+35163516+ Other clerical changes include:
35173517+35183518+ * the use of new xml2rfc functionality [RFC7991];
35193519+35203520+ * more explanation of the notation used;
35213521+35223522+ * the update of references, e.g., from RFC 4627 to [RFC8259], from
35233523+ CNN-TERMS to [RFC7228], and from the 5.1 edition to the 11th
35243524+ edition of [ECMA262]; the addition of a reference to [IEEE754] and
35253525+ importation of required definitions; the addition of references to
35263526+ [C] and [Cplusplus20]; and the addition of a reference to
35273527+ [RFC8618] that further illustrates the discussion in Appendix E;
35283528+35293529+ * in the discussion of diagnostic notation (Section 8), the
35303530+ "Extended Diagnostic Notation" (EDN) defined in [RFC8610] is now
35313531+ mentioned, the gap in representing NaN payloads is now
35323532+ highlighted, and an explanation of representing indefinite-length
35333533+ strings with no chunks has been added (Section 8.1);
35343534+35353535+ * the addition of this appendix.
35363536+35373537+G.2. Changes in IANA Considerations
35383538+35393539+ The IANA considerations were generally updated (clerical changes,
35403540+ e.g., now pointing to the CBOR Working Group as the author of the
35413541+ specification). References to the respective IANA registries were
35423542+ added to the informative references.
35433543+35443544+ In the "Concise Binary Object Representation (CBOR) Tags" registry
35453545+ [IANA.cbor-tags], tags in the space from 256 to 32767 (lower half of
35463546+ "1+2") are no longer assigned by First Come First Served; this range
35473547+ is now Specification Required.
35483548+35493549+G.3. Changes in Suggestions and Other Informational Components
35503550+35513551+ While revising the document, beyond the addressing of the errata
35523552+ reports, the working group drew upon nearly seven years of experience
35533553+ with CBOR in a diverse set of applications. This led to a number of
35543554+ editorial changes, including adding tables for illustration, but also
35553555+ emphasizing some aspects and de-emphasizing others.
35563556+35573557+ A significant addition is Section 2, which discusses the CBOR data
35583558+ model and its small variations involved in the processing of CBOR.
35593559+ The introduction of terms for those variations (basic generic,
35603560+ extended generic, specific) enables more concise language in other
35613561+ places of the document and also helps to clarify expectations of
35623562+ implementations and of the extensibility features of the format.
35633563+35643564+ As a format derived from the JSON ecosystem, RFC 7049 was influenced
35653565+ by the JSON number system that was in turn inherited from JavaScript
35663566+ at the time. JSON does not provide distinct integers and floating-
35673567+ point values (and the latter are decimal in the format). CBOR
35683568+ provides binary representations of numbers, which do differ between
35693569+ integers and floating-point values. Experience from implementation
35703570+ and use suggested that the separation between these two number
35713571+ domains should be more clearly drawn in the document; language that
35723572+ suggested an integer could seamlessly stand in for a floating-point
35733573+ value was removed. Also, a suggestion (based on I-JSON [RFC7493])
35743574+ was added for handling these types when converting JSON to CBOR, and
35753575+ the use of a specific rounding mechanism has been recommended.
35763576+35773577+ For a single value in the data model, CBOR often provides multiple
35783578+ encoding options. A new section (Section 4) introduces the term
35793579+ "preferred serialization" (Section 4.1) and defines it for various
35803580+ kinds of data items. On the basis of this terminology, the section
35813581+ then discusses how a CBOR-based protocol can define "deterministic
35823582+ encoding" (Section 4.2), which avoids terms "canonical" and
35833583+ "canonicalization" from RFC 7049. The suggestion of "Core
35843584+ Deterministic Encoding Requirements" (Section 4.2.1) enables generic
35853585+ support for such protocol-defined encoding requirements. This
35863586+ document further eases the implementation of deterministic encoding
35873587+ by simplifying the map ordering suggested in RFC 7049 to a simple
35883588+ lexicographic ordering of encoded keys. A description of the older
35893589+ suggestion is kept as an alternative, now termed "length-first map
35903590+ key ordering" (Section 4.2.3).
35913591+35923592+ The terminology for well-formed and valid data was sharpened and more
35933593+ stringently used, avoiding less well-defined alternative terms such
35943594+ as "syntax error", "decoding error", and "strict mode" outside of
35953595+ examples. Also, a third level of requirements that an application
35963596+ has on its input data beyond CBOR-level validity is now explicitly
35973597+ called out. Well-formed (processable at all), valid (checked by a
35983598+ validity-checking generic decoder), and expected input (as checked by
35993599+ the application) are treated as a hierarchy of layers of
36003600+ acceptability.
36013601+36023602+ The handling of non-well-formed simple values was clarified in text
36033603+ and pseudocode. Appendix F was added to discuss well-formedness
36043604+ errors and provide examples for them. The pseudocode was updated to
36053605+ be more portable, and some portability considerations were added.
36063606+36073607+ The discussion of validity has been sharpened in two areas. Map
36083608+ validity (handling of duplicate keys) was clarified, and the domain
36093609+ of applicability of certain implementation choices explained. Also,
36103610+ while streamlining the terminology for tags, tag numbers, and tag
36113611+ content, discussion was added on tag validity, and the restrictions
36123612+ were clarified on tag content, in general and specifically for tag 1.
36133613+36143614+ An implementation note (and note for future tag definitions) was
36153615+ added to Section 3.4 about defining tags with semantics that depend
36163616+ on serialization order.
36173617+36183618+ Tag 35 is not defined by this document; the registration based on the
36193619+ definition in RFC 7049 remains in place.
36203620+36213621+ Terminology was introduced in Section 3 for "argument" and "head",
36223622+ simplifying further discussion.
36233623+36243624+ The security considerations (Section 10) were mostly rewritten and
36253625+ significantly expanded; in multiple other places, the document is now
36263626+ more explicit that a decoder cannot simply condone well-formedness
36273627+ errors.
36283628+36293629+Acknowledgements
36303630+36313631+ CBOR was inspired by MessagePack. MessagePack was developed and
36323632+ promoted by Sadayuki Furuhashi ("frsyuki"). This reference to
36333633+ MessagePack is solely for attribution; CBOR is not intended as a
36343634+ version of, or replacement for, MessagePack, as it has different
36353635+ design goals and requirements.
36363636+36373637+ The need for functionality beyond the original MessagePack
36383638+ specification became obvious to many people at about the same time
36393639+ around the year 2012. BinaryPack is a minor derivation of
36403640+ MessagePack that was developed by Eric Zhang for the binaryjs
36413641+ project. A similar, but different, extension was made by Tim Caswell
36423642+ for his msgpack-js and msgpack-js-browser projects. Many people have
36433643+ contributed to the discussion about extending MessagePack to separate
36443644+ text string representation from byte string representation.
36453645+36463646+ The encoding of the additional information in CBOR was inspired by
36473647+ the encoding of length information designed by Klaus Hartke for CoAP.
36483648+36493649+ This document also incorporates suggestions made by many people,
36503650+ notably Dan Frost, James Manger, Jeffrey Yasskin, Joe Hildebrand,
36513651+ Keith Moore, Laurence Lundblade, Matthew Lepinski, Michael
36523652+ Richardson, Nico Williams, Peter Occil, Phillip Hallam-Baker, Ray
36533653+ Polk, Stuart Cheshire, Tim Bray, Tony Finch, Tony Hansen, and Yaron
36543654+ Sheffer. Benjamin Kaduk provided an extensive review during IESG
36553655+ processing. Éric Vyncke, Erik Kline, Robert Wilton, and Roman Danyliw
36563656+ provided further IESG comments, which included an IoT directorate
36573657+ review by Eve Schooler.
36583658+36593659+Authors' Addresses
36603660+36613661+ Carsten Bormann
36623662+ Universität Bremen TZI
36633663+ Postfach 330440
36643664+ D-28359 Bremen
36653665+ Germany
36663666+36673667+ Phone: +49-421-218-63921
36683668+ Email: cabo@tzi.org
36693669+36703670+36713671+ Paul Hoffman
36723672+ ICANN
36733673+36743674+ Email: paul.hoffman@icann.org
+3603
spec/rfc9052.txt
···11+
22+33+44+55+Internet Engineering Task Force (IETF) J. Schaad
66+Request for Comments: 9052 August Cellars
77+STD: 96 August 2022
88+Obsoletes: 8152
99+Category: Standards Track
1010+ISSN: 2070-1721
1111+1212+1313+ CBOR Object Signing and Encryption (COSE): Structures and Process
1414+1515+Abstract
1616+1717+ Concise Binary Object Representation (CBOR) is a data format designed
1818+ for small code size and small message size. There is a need to be
1919+ able to define basic security services for this data format. This
2020+ document defines the CBOR Object Signing and Encryption (COSE)
2121+ protocol. This specification describes how to create and process
2222+ signatures, message authentication codes, and encryption using CBOR
2323+ for serialization. This specification additionally describes how to
2424+ represent cryptographic keys using CBOR.
2525+2626+ This document, along with RFC 9053, obsoletes RFC 8152.
2727+2828+Status of This Memo
2929+3030+ This is an Internet Standards Track document.
3131+3232+ This document is a product of the Internet Engineering Task Force
3333+ (IETF). It represents the consensus of the IETF community. It has
3434+ received public review and has been approved for publication by the
3535+ Internet Engineering Steering Group (IESG). Further information on
3636+ Internet Standards is available in Section 2 of RFC 7841.
3737+3838+ Information about the current status of this document, any errata,
3939+ and how to provide feedback on it may be obtained at
4040+ https://www.rfc-editor.org/info/rfc9052.
4141+4242+Copyright Notice
4343+4444+ Copyright (c) 2022 IETF Trust and the persons identified as the
4545+ document authors. All rights reserved.
4646+4747+ This document is subject to BCP 78 and the IETF Trust's Legal
4848+ Provisions Relating to IETF Documents
4949+ (https://trustee.ietf.org/license-info) in effect on the date of
5050+ publication of this document. Please review these documents
5151+ carefully, as they describe your rights and restrictions with respect
5252+ to this document. Code Components extracted from this document must
5353+ include Revised BSD License text as described in Section 4.e of the
5454+ Trust Legal Provisions and are provided without warranty as described
5555+ in the Revised BSD License.
5656+5757+Table of Contents
5858+5959+ 1. Introduction
6060+ 1.1. Requirements Terminology
6161+ 1.2. Changes from RFC 8152
6262+ 1.3. Design Changes from JOSE
6363+ 1.4. CDDL Grammar for CBOR Data Structures
6464+ 1.5. CBOR-Related Terminology
6565+ 1.6. Document Terminology
6666+ 2. Basic COSE Structure
6767+ 3. Header Parameters
6868+ 3.1. Common COSE Header Parameters
6969+ 4. Signing Objects
7070+ 4.1. Signing with One or More Signers
7171+ 4.2. Signing with One Signer
7272+ 4.3. Externally Supplied Data
7373+ 4.4. Signing and Verification Process
7474+ 5. Encryption Objects
7575+ 5.1. Enveloped COSE Structure
7676+ 5.1.1. Content Key Distribution Methods
7777+ 5.2. Single Recipient Encrypted
7878+ 5.3. How to Encrypt and Decrypt for AEAD Algorithms
7979+ 5.4. How to Encrypt and Decrypt for AE Algorithms
8080+ 6. MAC Objects
8181+ 6.1. MACed Message with Recipients
8282+ 6.2. MACed Messages with Implicit Key
8383+ 6.3. How to Compute and Verify a MAC
8484+ 7. Key Objects
8585+ 7.1. COSE Key Common Parameters
8686+ 8. Taxonomy of Algorithms Used by COSE
8787+ 8.1. Signature Algorithms
8888+ 8.2. Message Authentication Code (MAC) Algorithms
8989+ 8.3. Content Encryption Algorithms
9090+ 8.4. Key Derivation Functions (KDFs)
9191+ 8.5. Content Key Distribution Methods
9292+ 8.5.1. Direct Encryption
9393+ 8.5.2. Key Wrap
9494+ 8.5.3. Key Transport
9595+ 8.5.4. Direct Key Agreement
9696+ 8.5.5. Key Agreement with Key Wrap
9797+ 9. CBOR Encoding Restrictions
9898+ 10. Application Profiling Considerations
9999+ 11. IANA Considerations
100100+ 11.1. COSE Header Parameters Registry
101101+ 11.2. COSE Key Common Parameters Registry
102102+ 11.3. Media Type Registrations
103103+ 11.3.1. COSE Security Message
104104+ 11.3.2. COSE Key Media Type
105105+ 11.4. CoAP Content-Formats Registry
106106+ 11.5. CBOR Tags Registry
107107+ 11.6. Expert Review Instructions
108108+ 12. Security Considerations
109109+ 13. References
110110+ 13.1. Normative References
111111+ 13.2. Informative References
112112+ Appendix A. Guidelines for External Data Authentication of
113113+ Algorithms
114114+ Appendix B. Two Layers of Recipient Information
115115+ Appendix C. Examples
116116+ C.1. Examples of Signed Messages
117117+ C.1.1. Single Signature
118118+ C.1.2. Multiple Signers
119119+ C.1.3. Signature with Criticality
120120+ C.2. Single Signer Examples
121121+ C.2.1. Single ECDSA Signature
122122+ C.3. Examples of Enveloped Messages
123123+ C.3.1. Direct ECDH
124124+ C.3.2. Direct Plus Key Derivation
125125+ C.3.3. Encrypted Content with External Data
126126+ C.4. Examples of Encrypted Messages
127127+ C.4.1. Simple Encrypted Message
128128+ C.4.2. Encrypted Message with a Partial IV
129129+ C.5. Examples of MACed Messages
130130+ C.5.1. Shared Secret Direct MAC
131131+ C.5.2. ECDH Direct MAC
132132+ C.5.3. Wrapped MAC
133133+ C.5.4. Multi-Recipient MACed Message
134134+ C.6. Examples of MAC0 Messages
135135+ C.6.1. Shared-Secret Direct MAC
136136+ C.7. COSE Keys
137137+ C.7.1. Public Keys
138138+ C.7.2. Private Keys
139139+ Acknowledgments
140140+ Author's Address
141141+142142+1. Introduction
143143+144144+ There has been an increased focus on small, constrained devices that
145145+ make up the Internet of Things (IoT). One of the standards that has
146146+ come out of this process is "Concise Binary Object Representation
147147+ (CBOR)" [STD94]. CBOR extended the data model of JavaScript Object
148148+ Notation (JSON) [STD90] by allowing for binary data, among other
149149+ changes. CBOR has been adopted by several of the IETF working groups
150150+ dealing with the IoT world as their method of encoding data
151151+ structures. CBOR was designed specifically to be small in terms of
152152+ both messages transported and implementation size and to have a
153153+ schema-free decoder. A need exists to provide message security
154154+ services for IoT, and using CBOR as the message-encoding format makes
155155+ sense.
156156+157157+ The JOSE Working Group produced a set of documents [RFC7515]
158158+ [RFC7516] [RFC7517] [RFC7518] that specified how to process
159159+ encryption, signatures, and Message Authentication Code (MAC)
160160+ operations and how to encode keys using JSON. This document defines
161161+ the CBOR Object Signing and Encryption (COSE) standard, which does
162162+ the same thing for the CBOR encoding format. This document is
163163+ combined with [RFC9053], which provides an initial set of algorithms.
164164+ While there is a strong attempt to keep the flavor of the original
165165+ JSON Object Signing and Encryption (JOSE) documents, two
166166+ considerations are taken into account:
167167+168168+ * CBOR has capabilities that are not present in JSON and are
169169+ appropriate to use. One example of this is the fact that CBOR has
170170+ a method of encoding binary data directly without first converting
171171+ it into a base64-encoded text string.
172172+173173+ * COSE is not a direct copy of the JOSE specification. In the
174174+ process of creating COSE, decisions that were made for JOSE were
175175+ re-examined. In many cases, different results were decided on, as
176176+ the criteria were not always the same.
177177+178178+ This document contains:
179179+180180+ * The description of the structure for the CBOR objects that are
181181+ transmitted over the wire. Two objects each are defined for
182182+ encryption, signing, and message authentication. One object is
183183+ defined for transporting keys and one for transporting groups of
184184+ keys.
185185+186186+ * The procedures used to build the inputs to the cryptographic
187187+ functions required for each of the structures.
188188+189189+ * A set of attributes that apply to the different security objects.
190190+191191+ This document does not contain the rules and procedures for using
192192+ specific cryptographic algorithms. Details on specific algorithms
193193+ can be found in [RFC9053] and [RFC8230]. Details for additional
194194+ algorithms are expected to be defined in future documents.
195195+196196+ COSE was initially designed as part of a solution to provide security
197197+ to Constrained RESTful Environments (CoRE), and this is done using
198198+ [RFC8613] and [CORE-GROUPCOMM]. However, COSE is not restricted to
199199+ just these cases and can be used in any place where one would
200200+ consider either JOSE or Cryptographic Message Syntax (CMS) [RFC5652]
201201+ for the purpose of providing security services. COSE, like JOSE and
202202+ CMS, is only for use in store-and-forward or offline protocols. The
203203+ use of COSE in online protocols needing encryption requires that an
204204+ online key establishment process be done before sending objects back
205205+ and forth. Any application that uses COSE for security services
206206+ first needs to determine what security services are required and then
207207+ select the appropriate COSE structures and cryptographic algorithms
208208+ based on those needs. Section 10 provides additional information on
209209+ what applications need to specify when using COSE.
210210+211211+ One feature that is present in CMS that is not present in this
212212+ standard is a digest structure. This omission is deliberate. It is
213213+ better for the structure to be defined in each protocol as different
214214+ protocols will want to include a different set of fields as part of
215215+ the structure. While an algorithm identifier and the digest value
216216+ are going to be common to all applications, the two values may not
217217+ always be adjacent, as the algorithm could be defined once with
218218+ multiple values. Applications may additionally want to define
219219+ additional data fields as part of the structure. One such
220220+ application-specific element would be to include a URI or other
221221+ pointer to where the data that is being hashed can be obtained.
222222+ [RFC9054] contains one such possible structure and defines a set of
223223+ digest algorithms.
224224+225225+ During the process of advancing COSE to Internet Standard, it was
226226+ noticed that the description of the security properties of
227227+ countersignatures was incorrect for the COSE_Sign1 structure. Since
228228+ the security properties that were described -- those of a true
229229+ countersignature -- were those that the working group desired, the
230230+ decision was made to remove all of the countersignature text from
231231+ this document and create a new document [COSE-COUNTERSIGN] to both
232232+ deprecate the old countersignature algorithm and header parameters
233233+ and define a new algorithm and header parameters with the desired
234234+ security properties.
235235+236236+1.1. Requirements Terminology
237237+238238+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
239239+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
240240+ "OPTIONAL" in this document are to be interpreted as described in
241241+ BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all
242242+ capitals, as shown here.
243243+244244+1.2. Changes from RFC 8152
245245+246246+ * Split the original document into this document and [RFC9053].
247247+248248+ * Added some text describing why there is no digest structure
249249+ defined by COSE.
250250+251251+ * Made text clarifications and changes in terminology.
252252+253253+ * Removed all of the details relating to countersignatures and
254254+ placed them in [COSE-COUNTERSIGN].
255255+256256+1.3. Design Changes from JOSE
257257+258258+ * A single overall message structure has been defined so that
259259+ encrypted, signed, and MACed messages can easily be identified and
260260+ still have a consistent view.
261261+262262+ * Signed messages distinguish between the protected and unprotected
263263+ header parameters that relate to the content and those that relate
264264+ to the signature.
265265+266266+ * MACed messages are separated from signed messages.
267267+268268+ * MACed messages have the ability to use the same set of recipient
269269+ algorithms as enveloped messages for obtaining the MAC
270270+ authentication key.
271271+272272+ * Binary encodings are used, rather than base64url encodings, to
273273+ encode binary data.
274274+275275+ * The authentication tag for encryption algorithms has been combined
276276+ with the ciphertext.
277277+278278+ * The set of cryptographic algorithms has been expanded in some
279279+ directions and trimmed in others.
280280+281281+1.4. CDDL Grammar for CBOR Data Structures
282282+283283+ When COSE was originally written, the Concise Data Definition
284284+ Language (CDDL) [RFC8610] had not yet been published in an RFC, so it
285285+ could not be used as the data description language to normatively
286286+ describe the CBOR data structures employed by COSE. For that reason,
287287+ the CBOR data objects defined here are described in prose.
288288+ Additional (non-normative) descriptions of the COSE data objects are
289289+ provided in a subset of CDDL, described below.
290290+291291+ This document was developed by first working on the grammar and then
292292+ developing the prose to go with it. An artifact of this is that the
293293+ prose was written using the primitive-type strings defined by Concise
294294+ Data Definition Language (CDDL) [RFC8610]. In this specification,
295295+ the following primitive types are used:
296296+297297+ any: A nonspecific value that permits all CBOR values to be placed
298298+ here.
299299+300300+ bool: A boolean value (true: major type 7, value 21; false: major
301301+ type 7, value 20).
302302+303303+ bstr: Byte string (major type 2).
304304+305305+ int: An unsigned integer or a negative integer.
306306+307307+ nil: A null value (major type 7, value 22).
308308+309309+ nint: A negative integer (major type 1).
310310+311311+ tstr: A UTF-8 text string (major type 3).
312312+313313+ uint: An unsigned integer (major type 0).
314314+315315+ Three syntaxes from CDDL appear in this document as shorthand. These
316316+ are:
317317+318318+ FOO / BAR: Indicates that either FOO or BAR can appear here.
319319+320320+ [+ FOO]: Indicates that the type FOO appears one or more times in an
321321+ array.
322322+323323+ * FOO: Indicates that the type FOO appears zero or more times.
324324+325325+ Two of the constraints defined by CDDL are also used in this
326326+ document. These are:
327327+328328+ type1 .cbor type2: Indicates that the contents of type1, usually
329329+ bstr, contains a value of type2.
330330+331331+ type1 .size integer: Indicates that the contents of type1 is integer
332332+ bytes long.
333333+334334+ As well as the prose description, a grammar for the CBOR data
335335+ structures is presented in the subset of CDDL described previously.
336336+ The CDDL grammar is informational; the prose description is
337337+ normative.
338338+339339+ The collected CDDL can be extracted from the XML version of this
340340+ document via the XPath expression below. (Depending on the XPath
341341+ evaluator one is using, it may be necessary to deal with > as an
342342+ entity.)
343343+344344+ //sourcecode[@type='cddl']/text()
345345+346346+ CDDL expects the initial nonterminal symbol to be the first symbol in
347347+ the file. For this reason, the first fragment of CDDL is presented
348348+ here.
349349+350350+ start = COSE_Messages / COSE_Key / COSE_KeySet / Internal_Types
351351+352352+ ; This is defined to make the tool quieter:
353353+ Internal_Types = Sig_structure / Enc_structure / MAC_structure
354354+355355+ The nonterminal Internal_Types is defined for dealing with the
356356+ automated validation tools used during the writing of this document.
357357+ It references those nonterminals that are used for security
358358+ computations but are not emitted for transport.
359359+360360+1.5. CBOR-Related Terminology
361361+362362+ In JSON, maps are called objects and only have one kind of map key: a
363363+ text string. In COSE, we use text strings, negative integers, and
364364+ unsigned integers as map keys. The integers are used for compactness
365365+ of encoding and easy comparison. The inclusion of text strings
366366+ allows for an additional range of short encoded values to be used as
367367+ well. Since the word "key" is mainly used in its other meaning, as a
368368+ cryptographic key, we use the term "label" for this usage as a map
369369+ key.
370370+371371+ In a CBOR map defined by this specification, the presence a label
372372+ that is neither a text string nor an integer is an error.
373373+ Applications can either fail processing or process messages by
374374+ ignoring incorrect labels; however, they MUST NOT create messages
375375+ with incorrect labels.
376376+377377+ A CDDL grammar fragment defines the nonterminal "label", as in the
378378+ previous paragraph, and "values", which permits any value to be used.
379379+380380+ label = int / tstr
381381+ values = any
382382+383383+1.6. Document Terminology
384384+385385+ In this document, we use the following terminology:
386386+387387+ Byte: A synonym for octet.
388388+389389+ Constrained Application Protocol (CoAP): A specialized web transfer
390390+ protocol for use in constrained systems. It is defined in
391391+ [RFC7252].
392392+393393+ Authenticated Encryption (AE) algorithms [RFC5116]: Encryption
394394+ algorithms that provide an authentication check of the contents
395395+ along with the encryption service. An example of an AE algorithm
396396+ used in COSE is AES Key Wrap [RFC3394]. These algorithms are used
397397+ for key encryption, but Authenticated Encryption with Associated
398398+ Data (AEAD) algorithms would be preferred.
399399+400400+ AEAD algorithms [RFC5116]: Encryption algorithms that provide the
401401+ same authentication service of the content as AE algorithms do,
402402+ and also allow associated data that is not part of the encrypted
403403+ body to be included in the authentication service. An example of
404404+ an AEAD algorithm used in COSE is AES-GCM [RFC5116]. These
405405+ algorithms are used for content encryption and can be used for key
406406+ encryption as well.
407407+408408+ "Context" is used throughout the document to represent information
409409+ that is not part of the COSE message. Information that is part of
410410+ the context can come from several different sources, including
411411+ protocol interactions, associated key structures, and program
412412+ configuration. The context to use can be implicit, identified using
413413+ the "kid context" header parameter defined in [RFC8613], or
414414+ identified by a protocol-specific identifier. Context should
415415+ generally be included in the cryptographic construction; for more
416416+ details, see Section 4.3.
417417+418418+ The term "byte string" is used for sequences of bytes, while the term
419419+ "text string" is used for sequences of characters.
420420+421421+2. Basic COSE Structure
422422+423423+ The COSE object structure is designed so that there can be a large
424424+ amount of common code when parsing and processing the different types
425425+ of security messages. All of the message structures are built on the
426426+ CBOR array type. The first three elements of the array always
427427+ contain the same information:
428428+429429+ 1. The protected header parameters, encoded and wrapped in a bstr.
430430+431431+ 2. The unprotected header parameters as a map.
432432+433433+ 3. The content of the message. The content is either the plaintext
434434+ or the ciphertext, as appropriate. The content may be detached
435435+ (i.e., transported separately from the COSE structure), but the
436436+ location is still used. The content is wrapped in a bstr when
437437+ present and is a nil value when detached.
438438+439439+ Elements after this point are dependent on the specific message type.
440440+441441+ COSE messages are built using the concept of layers to separate
442442+ different types of cryptographic concepts. As an example of how this
443443+ works, consider the COSE_Encrypt message (Section 5.1). This message
444444+ type is broken into two layers: the content layer and the recipient
445445+ layer. The content layer contains the encrypted plaintext and
446446+ information about the encrypted message. The recipient layer
447447+ contains the encrypted content encryption key (CEK) and information
448448+ about how it is encrypted, for each recipient. A single-layer
449449+ version of the encryption message COSE_Encrypt0 (Section 5.2) is
450450+ provided for cases where the CEK is preshared.
451451+452452+ Identification of which type of message has been presented is done by
453453+ the following methods:
454454+455455+ 1. The specific message type is known from the context. This may be
456456+ defined by a marker in the containing structure or by
457457+ restrictions specified by the application protocol.
458458+459459+ 2. The message type is identified by a CBOR tag. Messages with a
460460+ CBOR tag are known in this specification as tagged messages,
461461+ while those without the CBOR tag are known as untagged messages.
462462+ This document defines a CBOR tag for each of the message
463463+ structures. These tags can be found in Table 1.
464464+465465+ 3. When a COSE object is carried in a media type of "application/
466466+ cose", the optional parameter "cose-type" can be used to identify
467467+ the embedded object. The parameter is OPTIONAL if the tagged
468468+ version of the structure is used. The parameter is REQUIRED if
469469+ the untagged version of the structure is used. The value to use
470470+ with the parameter for each of the structures can be found in
471471+ Table 1.
472472+473473+ 4. When a COSE object is carried as a CoAP payload, the CoAP
474474+ Content-Format Option can be used to identify the message
475475+ content. The CoAP Content-Format values can be found in Table 2.
476476+ The CBOR tag for the message structure is not required, as each
477477+ security message is uniquely identified.
478478+479479+ +==========+===============+===============+=======================+
480480+ | CBOR Tag | cose-type | Data Item | Semantics |
481481+ +==========+===============+===============+=======================+
482482+ | 98 | cose-sign | COSE_Sign | COSE Signed Data |
483483+ | | | | Object |
484484+ +----------+---------------+---------------+-----------------------+
485485+ | 18 | cose-sign1 | COSE_Sign1 | COSE Single Signer |
486486+ | | | | Data Object |
487487+ +----------+---------------+---------------+-----------------------+
488488+ | 96 | cose-encrypt | COSE_Encrypt | COSE Encrypted Data |
489489+ | | | | Object |
490490+ +----------+---------------+---------------+-----------------------+
491491+ | 16 | cose-encrypt0 | COSE_Encrypt0 | COSE Single Recipient |
492492+ | | | | Encrypted Data Object |
493493+ +----------+---------------+---------------+-----------------------+
494494+ | 97 | cose-mac | COSE_Mac | COSE MACed Data |
495495+ | | | | Object |
496496+ +----------+---------------+---------------+-----------------------+
497497+ | 17 | cose-mac0 | COSE_Mac0 | COSE Mac w/o |
498498+ | | | | Recipients Object |
499499+ +----------+---------------+---------------+-----------------------+
500500+501501+ Table 1: COSE Message Identification
502502+503503+ +===========================+==========+=====+===========+
504504+ | Media Type | Encoding | ID | Reference |
505505+ +===========================+==========+=====+===========+
506506+ | application/cose; cose- | | 98 | RFC 9052 |
507507+ | type="cose-sign" | | | |
508508+ +---------------------------+----------+-----+-----------+
509509+ | application/cose; cose- | | 18 | RFC 9052 |
510510+ | type="cose-sign1" | | | |
511511+ +---------------------------+----------+-----+-----------+
512512+ | application/cose; cose- | | 96 | RFC 9052 |
513513+ | type="cose-encrypt" | | | |
514514+ +---------------------------+----------+-----+-----------+
515515+ | application/cose; cose- | | 16 | RFC 9052 |
516516+ | type="cose-encrypt0" | | | |
517517+ +---------------------------+----------+-----+-----------+
518518+ | application/cose; cose- | | 97 | RFC 9052 |
519519+ | type="cose-mac" | | | |
520520+ +---------------------------+----------+-----+-----------+
521521+ | application/cose; cose- | | 17 | RFC 9052 |
522522+ | type="cose-mac0" | | | |
523523+ +---------------------------+----------+-----+-----------+
524524+ | application/cose-key | | 101 | RFC 9052 |
525525+ +---------------------------+----------+-----+-----------+
526526+ | application/cose-key-set | | 102 | RFC 9052 |
527527+ +---------------------------+----------+-----+-----------+
528528+529529+ Table 2: CoAP Content-Formats for COSE
530530+531531+ The following CDDL fragment identifies all of the top messages
532532+ defined in this document. Separate nonterminals are defined for the
533533+ tagged and untagged versions of the messages.
534534+535535+ COSE_Messages = COSE_Untagged_Message / COSE_Tagged_Message
536536+537537+ COSE_Untagged_Message = COSE_Sign / COSE_Sign1 /
538538+ COSE_Encrypt / COSE_Encrypt0 /
539539+ COSE_Mac / COSE_Mac0
540540+541541+ COSE_Tagged_Message = COSE_Sign_Tagged / COSE_Sign1_Tagged /
542542+ COSE_Encrypt_Tagged / COSE_Encrypt0_Tagged /
543543+ COSE_Mac_Tagged / COSE_Mac0_Tagged
544544+545545+3. Header Parameters
546546+547547+ The structure of COSE has been designed to have two buckets of
548548+ information that are not considered to be part of the payload itself,
549549+ but are used for holding information about content, algorithms, keys,
550550+ or evaluation hints for the processing of the layer. These two
551551+ buckets are available for use in all of the structures except for
552552+ keys. While these buckets are present, they may not always be usable
553553+ in all instances. For example, while the protected bucket is defined
554554+ as part of the recipient structure, some of the algorithms used for
555555+ recipient structures do not provide for authenticated data. If this
556556+ is the case, the protected bucket is left empty.
557557+558558+ Both buckets are implemented as CBOR maps. The map key is a "label"
559559+ (Section 1.5). The value portion is dependent on the definition for
560560+ the label. Both maps use the same set of label/value pairs. The
561561+ integer and text-string values for labels have been divided into
562562+ several sections, including a standard range, a private use range,
563563+ and a range that is dependent on the algorithm selected. The defined
564564+ labels can be found in the "COSE Header Parameters" IANA registry
565565+ (Section 11.1).
566566+567567+ The two buckets are:
568568+569569+ protected: Contains parameters about the current layer that are
570570+ cryptographically protected. This bucket MUST be empty if it is
571571+ not going to be included in a cryptographic computation. This
572572+ bucket is encoded in the message as a binary object. This value
573573+ is obtained by CBOR encoding the protected map and wrapping it in
574574+ a bstr object. Senders SHOULD encode a zero-length map as a zero-
575575+ length byte string rather than as a zero-length map (encoded as
576576+ h'a0'). The zero-length byte string encoding is preferred,
577577+ because it is both shorter and the version used in the
578578+ serialization structures for cryptographic computation.
579579+ Recipients MUST accept both a zero-length byte string and a zero-
580580+ length map encoded in a byte string.
581581+582582+ Wrapping the encoding with a byte string allows the protected map
583583+ to be transported with a greater chance that it will not be
584584+ altered accidentally in transit. (Badly behaved intermediates
585585+ could decode and re-encode, but this will result in a failure to
586586+ verify unless the re-encoded byte string is identical to the
587587+ decoded byte string.) This avoids the problem of all parties
588588+ needing to be able to do a common canonical encoding of the map
589589+ for input to cryptographic operations.
590590+591591+ unprotected: Contains parameters about the current layer that are
592592+ not cryptographically protected.
593593+594594+ Only header parameters that deal with the current layer are to be
595595+ placed at that layer. As an example of this, the header parameter
596596+ "content type" describes the content of the message being carried in
597597+ the message. As such, this header parameter is placed only in the
598598+ content layer and is not placed in the recipient or signature layers.
599599+ In principle, one should be able to process any given layer without
600600+ reference to any other layer. With the exception of the COSE_Sign
601601+ structure, the only data that needs to cross layers is the
602602+ cryptographic key.
603603+604604+ The buckets are present in all of the security objects defined in
605605+ this document. The fields, in order, are the "protected" bucket (as
606606+ a CBOR "bstr" type) and then the "unprotected" bucket (as a CBOR
607607+ "map" type). The presence of both buckets is required. The header
608608+ parameters that go into the buckets come from the IANA "COSE Header
609609+ Parameters" registry (Section 11.1). Some header parameters are
610610+ defined in the next section.
611611+612612+ Labels in each of the maps MUST be unique. When processing messages,
613613+ if a label appears multiple times, the message MUST be rejected as
614614+ malformed. Applications SHOULD verify that the same label does not
615615+ occur in both the protected and unprotected header parameters. If
616616+ the message is not rejected as malformed, attributes MUST be obtained
617617+ from the protected bucket, and only if an attribute is not found in
618618+ the protected bucket can that attribute be obtained from the
619619+ unprotected bucket.
620620+621621+ The following CDDL fragment represents the two header-parameter
622622+ buckets. A group "Headers" is defined in CDDL that represents the
623623+ two buckets in which attributes are placed. This group is used to
624624+ provide these two fields consistently in all locations. A type is
625625+ also defined that represents the map of common header parameters.
626626+627627+ Headers = (
628628+ protected : empty_or_serialized_map,
629629+ unprotected : header_map
630630+ )
631631+632632+ header_map = {
633633+ Generic_Headers,
634634+ * label => values
635635+ }
636636+637637+ empty_or_serialized_map = bstr .cbor header_map / bstr .size 0
638638+639639+3.1. Common COSE Header Parameters
640640+641641+ This section defines a set of common header parameters. A summary of
642642+ these header parameters can be found in Table 3. This table should
643643+ be consulted to determine the value of the label and the type of the
644644+ value.
645645+646646+ The set of header parameters defined in this section is as follows:
647647+648648+ alg: This header parameter is used to indicate the algorithm used
649649+ for the security processing. This header parameter MUST be
650650+ authenticated where the ability to do so exists. This support is
651651+ provided by AEAD algorithms or construction (e.g., COSE_Sign and
652652+ COSE_Mac0). This authentication can be done either by placing the
653653+ header parameter in the protected-header-parameters bucket or as
654654+ part of the externally supplied data (Section 4.3). The value is
655655+ taken from the "COSE Algorithms" registry (see [COSE.Algorithms]).
656656+657657+ crit: This header parameter is used to indicate which protected
658658+ header parameters an application that is processing a message is
659659+ required to understand. Header parameters defined in this
660660+ document do not need to be included, as they should be understood
661661+ by all implementations. Additionally, the header parameter
662662+ "counter signature" (label 7) defined by [RFC8152] must be
663663+ understood by new implementations, to remain compatible with
664664+ senders that adhere to that document and assume all
665665+ implementations will understand it. When present, the "crit"
666666+ header parameter MUST be placed in the protected-header-parameters
667667+ bucket. The array MUST have at least one value in it.
668668+669669+ Not all header-parameter labels need to be included in the "crit"
670670+ header parameter. The rules for deciding which header parameters
671671+ are placed in the array are:
672672+673673+ * Integer labels in the range of 0 to 7 SHOULD be omitted.
674674+675675+ * Integer labels in the range -1 to -128 can be omitted.
676676+ Algorithms can assign labels in this range where the ability to
677677+ process the content of the label is considered to be core to
678678+ implementing the algorithm. Algorithms can assign labels
679679+ outside of this range and include them in the "crit" header
680680+ parameter when the ability to process the content of the label
681681+ is not considered to be core functionality of the algorithm but
682682+ does need to be understood to correctly process this instance.
683683+ Integer labels in the range -129 to -65536 SHOULD be included,
684684+ as these would be less common header parameters that might not
685685+ be generally supported.
686686+687687+ * Labels for header parameters required for an application MAY be
688688+ omitted. Applications should have a statement declaring
689689+ whether or not the label can be omitted.
690690+691691+ The header parameters indicated by "crit" can be processed by
692692+ either the security-library code or an application using a
693693+ security library; the only requirement is that the header
694694+ parameter is processed. If the "crit" value list includes a label
695695+ for which the header parameter is not in the protected-header-
696696+ parameters bucket, this is a fatal error in processing the
697697+ message.
698698+699699+ content type: This header parameter is used to indicate the content
700700+ type of the data in the "payload" or "ciphertext" field. Integers
701701+ are from the "CoAP Content-Formats" IANA registry table
702702+ [COAP.Formats]. Text values follow the syntax of "<type-
703703+ name>/<subtype-name>", where <type-name> and <subtype-name> are
704704+ defined in Section 4.2 of [RFC6838]. Leading and trailing
705705+ whitespace is not permitted. Textual content type values, along
706706+ with parameters and subparameters, can be located using the IANA
707707+ "Media Types" registry. Applications SHOULD provide this header
708708+ parameter if the content structure is potentially ambiguous.
709709+710710+ kid: This header parameter identifies one piece of data that can be
711711+ used as input to find the needed cryptographic key. The value of
712712+ this header parameter can be matched against the "kid" member in a
713713+ COSE_Key structure. Other methods of key distribution can define
714714+ an equivalent field to be matched. Applications MUST NOT assume
715715+ that "kid" values are unique. There may be more than one key with
716716+ the same "kid" value, so all of the keys associated with this
717717+ "kid" may need to be checked. The internal structure of "kid"
718718+ values is not defined and cannot be relied on by applications.
719719+ Key identifier values are hints about which key to use. This is
720720+ not a security-critical field. For this reason, it can be placed
721721+ in the unprotected-header-parameters bucket.
722722+723723+ IV: This header parameter holds the Initialization Vector (IV)
724724+ value. For some symmetric encryption algorithms, this may be
725725+ referred to as a nonce. The IV can be placed in the unprotected
726726+ bucket, since for AE and AEAD algorithms, modifying the IV will
727727+ cause the decryption to fail.
728728+729729+ Partial IV: This header parameter holds a part of the IV value.
730730+ When using the COSE_Encrypt0 structure, a portion of the IV can be
731731+ part of the context associated with the key (Context IV), while a
732732+ portion can be changed with each message (Partial IV). This field
733733+ is used to carry a value that causes the IV to be changed for each
734734+ message. The Partial IV can be placed in the unprotected bucket,
735735+ as modifying the value will cause the decryption to yield
736736+ plaintext that is readily detectable as garbled. The
737737+ "Initialization Vector" and "Partial Initialization Vector" header
738738+ parameters MUST NOT both be present in the same security layer.
739739+740740+ The message IV is generated by the following steps:
741741+742742+ 1. Left-pad the Partial IV with zeros to the length of IV
743743+ (determined by the algorithm).
744744+745745+ 2. XOR the padded Partial IV with the Context IV.
746746+747747+ +=========+=======+========+=====================+==================+
748748+ | Name | Label | Value | Value Registry | Description |
749749+ | | | Type | | |
750750+ +=========+=======+========+=====================+==================+
751751+ | alg | 1 | int / | COSE Algorithms | Cryptographic |
752752+ | | | tstr | registry | algorithm to use |
753753+ +---------+-------+--------+---------------------+------------------+
754754+ | crit | 2 | [+ | COSE Header | Critical header |
755755+ | | | label] | Parameters | parameters to be |
756756+ | | | | registry | understood |
757757+ +---------+-------+--------+---------------------+------------------+
758758+ | content | 3 | tstr / | CoAP Content- | Content type of |
759759+ | type | | uint | Formats or Media | the payload |
760760+ | | | | Types registries | |
761761+ +---------+-------+--------+---------------------+------------------+
762762+ | kid | 4 | bstr | | Key identifier |
763763+ +---------+-------+--------+---------------------+------------------+
764764+ | IV | 5 | bstr | | Full |
765765+ | | | | | Initialization |
766766+ | | | | | Vector |
767767+ +---------+-------+--------+---------------------+------------------+
768768+ | Partial | 6 | bstr | | Partial |
769769+ | IV | | | | Initialization |
770770+ | | | | | Vector |
771771+ +---------+-------+--------+---------------------+------------------+
772772+773773+ Table 3: Common Header Parameters
774774+775775+ The CDDL fragment that represents the set of header parameters
776776+ defined in this section is given below. Each of the header
777777+ parameters is tagged as optional, because they do not need to be in
778778+ every map; header parameters required in specific maps are discussed
779779+ above.
780780+781781+ Generic_Headers = (
782782+ ? 1 => int / tstr, ; algorithm identifier
783783+ ? 2 => [+label], ; criticality
784784+ ? 3 => tstr / int, ; content type
785785+ ? 4 => bstr, ; key identifier
786786+ ? ( 5 => bstr // ; IV
787787+ 6 => bstr ) ; Partial IV
788788+ )
789789+790790+4. Signing Objects
791791+792792+ COSE supports two different signature structures. COSE_Sign allows
793793+ for one or more signatures to be applied to the same content.
794794+ COSE_Sign1 is restricted to a single signer. The structures cannot
795795+ be converted between each other; as the signature computation
796796+ includes a parameter identifying which structure is being used, the
797797+ converted structure will fail signature validation.
798798+799799+4.1. Signing with One or More Signers
800800+801801+ The COSE_Sign structure allows for one or more signatures to be
802802+ applied to a message payload. Header parameters relating to the
803803+ content and header parameters relating to the signature are carried
804804+ along with the signature itself. These header parameters may be
805805+ authenticated by the signature, or just be present. An example of a
806806+ header parameter about the content is the content type header
807807+ parameter. An example of a header parameter about the signature
808808+ would be the algorithm and key used to create the signature.
809809+810810+ [RFC5652] indicates that:
811811+812812+ | When more than one signature is present, the successful validation
813813+ | of one signature associated with a given signer is usually treated
814814+ | as a successful signature by that signer. However, there are some
815815+ | application environments where other rules are needed. An
816816+ | application that employs a rule other than one valid signature for
817817+ | each signer must specify those rules. Also, where simple matching
818818+ | of the signer identifier is not sufficient to determine whether
819819+ | the signatures were generated by the same signer, the application
820820+ | specification must describe how to determine which signatures were
821821+ | generated by the same signer. Support of different communities of
822822+ | recipients is the primary reason that signers choose to include
823823+ | more than one signature.
824824+825825+ For example, the COSE_Sign structure might include signatures
826826+ generated with the Edwards-curve Digital Signature Algorithm (EdDSA)
827827+ [RFC8032] and the Elliptic Curve Digital Signature Algorithm (ECDSA)
828828+ [DSS]. This allows recipients to verify the signature associated
829829+ with one algorithm or the other. More detailed information on
830830+ multiple signature evaluations can be found in [RFC5752].
831831+832832+ The signature structure can be encoded as either tagged or untagged,
833833+ depending on the context it will be used in. A tagged COSE_Sign
834834+ structure is identified by the CBOR tag 98. The CDDL fragment that
835835+ represents this is:
836836+837837+ COSE_Sign_Tagged = #6.98(COSE_Sign)
838838+839839+ A COSE Signed Message is defined in two parts. The CBOR object that
840840+ carries the body and information about the message is called the
841841+ COSE_Sign structure. The CBOR object that carries the signature and
842842+ information about the signature is called the COSE_Signature
843843+ structure. Examples of COSE Signed Messages can be found in
844844+ Appendix C.1.
845845+846846+ The COSE_Sign structure is a CBOR array. The fields of the array, in
847847+ order, are:
848848+849849+ protected: This is as described in Section 3.
850850+851851+ unprotected: This is as described in Section 3.
852852+853853+ payload: This field contains the serialized content to be signed.
854854+ If the payload is not present in the message, the application is
855855+ required to supply the payload separately. The payload is wrapped
856856+ in a bstr to ensure that it is transported without changes. If
857857+ the payload is transported separately ("detached content"), then a
858858+ nil CBOR object is placed in this location, and it is the
859859+ responsibility of the application to ensure that it will be
860860+ transported without changes.
861861+862862+ Note: When a signature with a message recovery algorithm is used
863863+ (Section 8.1), the maximum number of bytes that can be recovered
864864+ is the length of the original payload. The size of the encoded
865865+ payload is reduced by the number of bytes that will be recovered.
866866+ If all of the bytes of the original payload are consumed, then the
867867+ transmitted payload is encoded as a zero-length byte string rather
868868+ than as being absent.
869869+870870+ signatures: This field is an array of signatures. Each signature is
871871+ represented as a COSE_Signature structure.
872872+873873+ The CDDL fragment that represents the above text for COSE_Sign
874874+ follows.
875875+876876+ COSE_Sign = [
877877+ Headers,
878878+ payload : bstr / nil,
879879+ signatures : [+ COSE_Signature]
880880+ ]
881881+882882+ The COSE_Signature structure is a CBOR array. The fields of the
883883+ array, in order, are:
884884+885885+ protected: This is as described in Section 3.
886886+887887+ unprotected: This is as described in Section 3.
888888+889889+ signature: This field contains the computed signature value. The
890890+ type of the field is a bstr. Algorithms MUST specify padding if
891891+ the signature value is not a multiple of 8 bits.
892892+893893+ The CDDL fragment that represents the above text for COSE_Signature
894894+ follows.
895895+896896+ COSE_Signature = [
897897+ Headers,
898898+ signature : bstr
899899+ ]
900900+901901+4.2. Signing with One Signer
902902+903903+ The COSE_Sign1 signature structure is used when only one signature is
904904+ going to be placed on a message. The header parameters dealing with
905905+ the content and the signature are placed in the same pair of buckets,
906906+ rather than having the separation of COSE_Sign.
907907+908908+ The structure can be encoded as either tagged or untagged depending
909909+ on the context it will be used in. A tagged COSE_Sign1 structure is
910910+ identified by the CBOR tag 18. The CDDL fragment that represents
911911+ this is:
912912+913913+ COSE_Sign1_Tagged = #6.18(COSE_Sign1)
914914+915915+ The CBOR object that carries the body, the signature, and the
916916+ information about the body and signature is called the COSE_Sign1
917917+ structure. Examples of COSE_Sign1 messages can be found in
918918+ Appendix C.2.
919919+920920+ The COSE_Sign1 structure is a CBOR array. The fields of the array,
921921+ in order, are:
922922+923923+ protected: This is as described in Section 3.
924924+925925+ unprotected: This is as described in Section 3.
926926+927927+ payload: This is as described in Section 4.1.
928928+929929+ signature: This field contains the computed signature value. The
930930+ type of the field is a bstr.
931931+932932+ The CDDL fragment that represents the above text for COSE_Sign1
933933+ follows.
934934+935935+ COSE_Sign1 = [
936936+ Headers,
937937+ payload : bstr / nil,
938938+ signature : bstr
939939+ ]
940940+941941+4.3. Externally Supplied Data
942942+943943+ One of the features offered in COSE is the ability for applications
944944+ to provide additional data that is to be authenticated but is not
945945+ carried as part of the COSE object. The primary reason for
946946+ supporting this can be seen by looking at the CoAP message structure
947947+ [RFC7252], where the facility exists for options to be carried before
948948+ the payload. Examples of data that can be placed in this location
949949+ would be the CoAP code or CoAP options. If the data is in the
950950+ headers of the CoAP message, then it is available for proxies to help
951951+ in performing proxying operations. For example, the Accept option
952952+ can be used by a proxy to determine if an appropriate value is in the
953953+ proxy's cache. The sender can use the additional-data functionality
954954+ to enable detection of any changes to the set of Accept values made
955955+ by a proxy or an attacker. By including the field in the externally
956956+ supplied data, any subsequent modification will cause the server
957957+ processing of the message to result in failure.
958958+959959+ This document describes the process for using a byte array of
960960+ externally supplied authenticated data; the method of constructing
961961+ the byte array is a function of the application. Applications that
962962+ use this feature need to define how the externally supplied
963963+ authenticated data is to be constructed. Such a construction needs
964964+ to take into account the following issues:
965965+966966+ * If multiple items are included, applications need to ensure that
967967+ the same byte string cannot be produced if there are different
968968+ inputs. An example of how the problematic scenario could arise
969969+ would be by concatenating the text strings "AB" and "CDE" or by
970970+ concatenating the text strings "ABC" and "DE". This is usually
971971+ addressed by making fields a fixed width and/or encoding the
972972+ length of the field as part of the output. Using options from
973973+ CoAP [RFC7252] as an example, these fields use a TLV structure so
974974+ they can be concatenated without any problems.
975975+976976+ * If multiple items are included, an order for the items needs to be
977977+ defined. Using options from CoAP as an example, an application
978978+ could state that the fields are to be ordered by the option
979979+ number.
980980+981981+ * Applications need to ensure that the byte string is going to be
982982+ the same on both sides. Using options from CoAP might give a
983983+ problem if the same relative numbering is kept. An intermediate
984984+ node could insert or remove an option, changing how the relative
985985+ numbering is done. An application would need to specify that the
986986+ relative number must be re-encoded to be relative only to the
987987+ options that are in the external data.
988988+989989+4.4. Signing and Verification Process
990990+991991+ In order to create a signature, a well-defined byte string is needed.
992992+ The Sig_structure is used to create the canonical form. This signing
993993+ and verification process takes in the body information (COSE_Sign or
994994+ COSE_Sign1), the signer information (COSE_Signature), and the
995995+ application data (external source). A Sig_structure is a CBOR array.
996996+ The fields of the Sig_structure, in order, are:
997997+998998+ 1. A context text string identifying the context of the signature.
999999+ The context text string is:
10001000+10011001+ "Signature" for signatures using the COSE_Signature structure.
10021002+10031003+ "Signature1" for signatures using the COSE_Sign1 structure.
10041004+10051005+ 2. The protected attributes from the body structure, encoded in a
10061006+ bstr type. If there are no protected attributes, a zero-length
10071007+ byte string is used.
10081008+10091009+ 3. The protected attributes from the signer structure, encoded in a
10101010+ bstr type. If there are no protected attributes, a zero-length
10111011+ byte string is used. This field is omitted for the COSE_Sign1
10121012+ signature structure.
10131013+10141014+ 4. The externally supplied data from the application, encoded in a
10151015+ bstr type. If this field is not supplied, it defaults to a zero-
10161016+ length byte string. (See Section 4.3 for application guidance on
10171017+ constructing this field.)
10181018+10191019+ 5. The payload to be signed, encoded in a bstr type. The full
10201020+ payload is used here, independent of how it is transported.
10211021+10221022+ The CDDL fragment that describes the above text is:
10231023+10241024+ Sig_structure = [
10251025+ context : "Signature" / "Signature1",
10261026+ body_protected : empty_or_serialized_map,
10271027+ ? sign_protected : empty_or_serialized_map,
10281028+ external_aad : bstr,
10291029+ payload : bstr
10301030+ ]
10311031+10321032+ How to compute a signature:
10331033+10341034+ 1. Create a Sig_structure and populate it with the appropriate
10351035+ fields.
10361036+10371037+ 2. Create the value ToBeSigned by encoding the Sig_structure to a
10381038+ byte string, using the encoding described in Section 9.
10391039+10401040+ 3. Call the signature creation algorithm, passing in K (the key to
10411041+ sign with), alg (the algorithm to sign with), and ToBeSigned (the
10421042+ value to sign).
10431043+10441044+ 4. Place the resulting signature value in the correct location.
10451045+ This is the "signature" field of the COSE_Signature or COSE_Sign1
10461046+ structure.
10471047+10481048+ The steps for verifying a signature are:
10491049+10501050+ 1. Create a Sig_structure and populate it with the appropriate
10511051+ fields.
10521052+10531053+ 2. Create the value ToBeSigned by encoding the Sig_structure to a
10541054+ byte string, using the encoding described in Section 9.
10551055+10561056+ 3. Call the signature verification algorithm, passing in K (the key
10571057+ to verify with), alg (the algorithm used to sign with),
10581058+ ToBeSigned (the value to sign), and sig (the signature to be
10591059+ verified).
10601060+10611061+ In addition to performing the signature verification, the application
10621062+ performs the appropriate checks to ensure that the key is correctly
10631063+ paired with the signing identity and that the signing identity is
10641064+ authorized before performing actions.
10651065+10661066+5. Encryption Objects
10671067+10681068+ COSE supports two different encryption structures. COSE_Encrypt0 is
10691069+ used when a recipient structure is not needed because the key to be
10701070+ used is known implicitly. COSE_Encrypt is used the rest of the time.
10711071+ This includes cases where there are multiple recipients or a
10721072+ recipient algorithm other than direct (i.e., preshared secret) is
10731073+ used.
10741074+10751075+5.1. Enveloped COSE Structure
10761076+10771077+ The enveloped structure allows for one or more recipients of a
10781078+ message. There are provisions for header parameters about the
10791079+ content and header parameters about the recipient information to be
10801080+ carried in the message. The protected header parameters associated
10811081+ with the content are authenticated by the content encryption
10821082+ algorithm. The protected header parameters associated with the
10831083+ recipient (when the algorithm supports it) are authenticated by the
10841084+ recipient algorithm. Examples of header parameters about the content
10851085+ are the type of the content and the content encryption algorithm.
10861086+ Examples of header parameters about the recipient are the recipient's
10871087+ key identifier and the recipient's encryption algorithm.
10881088+10891089+ The same techniques and nearly the same structure are used for
10901090+ encrypting both the plaintext and the keys. This is different from
10911091+ the approach used by both "Cryptographic Message Syntax (CMS)"
10921092+ [RFC5652] and "JSON Web Encryption (JWE)" [RFC7516], where different
10931093+ structures are used for the content layer and the recipient layer.
10941094+ Two structures are defined: COSE_Encrypt to hold the encrypted
10951095+ content and COSE_recipient to hold the encrypted keys for recipients.
10961096+ Examples of enveloped messages can be found in Appendix C.3.
10971097+10981098+ The COSE_Encrypt structure can be encoded as either tagged or
10991099+ untagged, depending on the context it will be used in. A tagged
11001100+ COSE_Encrypt structure is identified by the CBOR tag 96. The CDDL
11011101+ fragment that represents this is:
11021102+11031103+ COSE_Encrypt_Tagged = #6.96(COSE_Encrypt)
11041104+11051105+ The COSE_Encrypt structure is a CBOR array. The fields of the array,
11061106+ in order, are:
11071107+11081108+ protected: This is as described in Section 3.
11091109+11101110+ unprotected: This is as described in Section 3.
11111111+11121112+ ciphertext: This field contains the ciphertext, encoded as a bstr.
11131113+ If the ciphertext is to be transported independently of the
11141114+ control information about the encryption process (i.e., detached
11151115+ content), then the field is encoded as a nil value.
11161116+11171117+ recipients: This field contains an array of recipient information
11181118+ structures. The type for the recipient information structure is a
11191119+ COSE_recipient.
11201120+11211121+ The CDDL fragment that corresponds to the above text is:
11221122+11231123+ COSE_Encrypt = [
11241124+ Headers,
11251125+ ciphertext : bstr / nil,
11261126+ recipients : [+COSE_recipient]
11271127+ ]
11281128+11291129+ The COSE_recipient structure is a CBOR array. The fields of the
11301130+ array, in order, are:
11311131+11321132+ protected: This is as described in Section 3.
11331133+11341134+ unprotected: This is as described in Section 3.
11351135+11361136+ ciphertext: This field contains the encrypted key, encoded as a
11371137+ bstr. All encoded keys are symmetric keys; the binary value of
11381138+ the key is the content. If there is not an encrypted key, then
11391139+ this field is encoded as a nil value.
11401140+11411141+ recipients: This field contains an array of recipient information
11421142+ structures. The type for the recipient information structure is a
11431143+ COSE_recipient (an example of this can be found in Appendix B).
11441144+ If there are no recipient information structures, this element is
11451145+ absent.
11461146+11471147+ The CDDL fragment that corresponds to the above text for
11481148+ COSE_recipient is:
11491149+11501150+ COSE_recipient = [
11511151+ Headers,
11521152+ ciphertext : bstr / nil,
11531153+ ? recipients : [+COSE_recipient]
11541154+ ]
11551155+11561156+5.1.1. Content Key Distribution Methods
11571157+11581158+ An encrypted message consists of an encrypted content and an
11591159+ encrypted CEK for one or more recipients. The CEK is encrypted for
11601160+ each recipient, using a key specific to that recipient. The details
11611161+ of this encryption depend on which class the recipient algorithm
11621162+ falls into. Specific details on each of the classes can be found in
11631163+ Section 8.5. A short summary of the five content key distribution
11641164+ methods is:
11651165+11661166+ direct: The CEK is the same as the identified previously distributed
11671167+ symmetric key or is derived from a previously distributed secret.
11681168+ No CEK is transported in the message.
11691169+11701170+ symmetric key-encryption keys (KEKs): The CEK is encrypted using a
11711171+ previously distributed symmetric KEK. Also known as key wrap.
11721172+11731173+ key agreement: The recipient's public key and a sender's private key
11741174+ are used to generate a pairwise secret, a Key Derivation Function
11751175+ (KDF) is applied to derive a key, and then the CEK is either the
11761176+ derived key or encrypted by the derived key.
11771177+11781178+ key transport: The CEK is encrypted with the recipient's public key.
11791179+11801180+ passwords: The CEK is encrypted in a KEK that is derived from a
11811181+ password. As of when this document was published, no password
11821182+ algorithms have been defined.
11831183+11841184+5.2. Single Recipient Encrypted
11851185+11861186+ The COSE_Encrypt0 encrypted structure does not have the ability to
11871187+ specify recipients of the message. The structure assumes that the
11881188+ recipient of the object will already know the identity of the key to
11891189+ be used in order to decrypt the message. If a key needs to be
11901190+ identified to the recipient, the enveloped structure ought to be
11911191+ used.
11921192+11931193+ Examples of encrypted messages can be found in Appendix C.4.
11941194+11951195+ The COSE_Encrypt0 structure can be encoded as either tagged or
11961196+ untagged, depending on the context it will be used in. A tagged
11971197+ COSE_Encrypt0 structure is identified by the CBOR tag 16. The CDDL
11981198+ fragment that represents this is:
11991199+12001200+ COSE_Encrypt0_Tagged = #6.16(COSE_Encrypt0)
12011201+12021202+ The COSE_Encrypt0 structure is a CBOR array. The fields of the
12031203+ array, in order, are:
12041204+12051205+ protected: This is as described in Section 3.
12061206+12071207+ unprotected: This is as described in Section 3.
12081208+12091209+ ciphertext: This is as described in Section 5.1.
12101210+12111211+ The CDDL fragment for COSE_Encrypt0 that corresponds to the above
12121212+ text is:
12131213+12141214+ COSE_Encrypt0 = [
12151215+ Headers,
12161216+ ciphertext : bstr / nil,
12171217+ ]
12181218+12191219+5.3. How to Encrypt and Decrypt for AEAD Algorithms
12201220+12211221+ The encryption algorithm for AEAD algorithms is fairly simple. The
12221222+ first step is to create a consistent byte string for the
12231223+ authenticated data structure. For this purpose, we use an
12241224+ Enc_structure. The Enc_structure is a CBOR array. The fields of the
12251225+ Enc_structure, in order, are:
12261226+12271227+ 1. A context text string identifying the context of the
12281228+ authenticated data structure. The context text string is:
12291229+12301230+ "Encrypt0" for the content encryption of a COSE_Encrypt0 data
12311231+ structure.
12321232+12331233+ "Encrypt" for the first layer of a COSE_Encrypt data structure
12341234+ (i.e., for content encryption).
12351235+12361236+ "Enc_Recipient" for a recipient encoding to be placed in a
12371237+ COSE_Encrypt data structure.
12381238+12391239+ "Mac_Recipient" for a recipient encoding to be placed in a
12401240+ MACed message structure.
12411241+12421242+ "Rec_Recipient" for a recipient encoding to be placed in a
12431243+ recipient structure.
12441244+12451245+ 2. The protected attributes from the body structure, encoded in a
12461246+ bstr type. If there are no protected attributes, a zero-length
12471247+ byte string is used.
12481248+12491249+ 3. The externally supplied data from the application encoded in a
12501250+ bstr type. If this field is not supplied, it defaults to a zero-
12511251+ length byte string. (See Section 4.3 for application guidance on
12521252+ constructing this field.)
12531253+12541254+ The CDDL fragment that describes the above text is:
12551255+12561256+ Enc_structure = [
12571257+ context : "Encrypt" / "Encrypt0" / "Enc_Recipient" /
12581258+ "Mac_Recipient" / "Rec_Recipient",
12591259+ protected : empty_or_serialized_map,
12601260+ external_aad : bstr
12611261+ ]
12621262+12631263+ How to encrypt a message:
12641264+12651265+ 1. Create an Enc_structure and populate it with the appropriate
12661266+ fields.
12671267+12681268+ 2. Encode the Enc_structure to a byte string (Additional
12691269+ Authenticated Data (AAD)), using the encoding described in
12701270+ Section 9.
12711271+12721272+ 3. Determine the encryption key (K). This step is dependent on the
12731273+ class of recipient algorithm being used. For:
12741274+12751275+ No Recipients: The key to be used is determined by the algorithm
12761276+ and key at the current layer. Examples are key wrap keys
12771277+ (Section 8.5.2) and preshared secrets.
12781278+12791279+ Direct Encryption and Direct Key Agreement: The key is
12801280+ determined by the key and algorithm in the recipient
12811281+ structure. The encryption algorithm and size of the key to be
12821282+ used are inputs into the KDF used for the recipient. (For
12831283+ direct, the KDF can be thought of as the identity operation.)
12841284+ Examples of these algorithms are found in Sections 6.1 and 6.3
12851285+ of [RFC9053].
12861286+12871287+ Other: The key is randomly generated.
12881288+12891289+ 4. Call the encryption algorithm with K (the encryption key), P (the
12901290+ plaintext), and AAD. Place the returned ciphertext into the
12911291+ "ciphertext" field of the structure.
12921292+12931293+ 5. For recipients of the message using non-direct algorithms,
12941294+ recursively perform the encryption algorithm for that recipient,
12951295+ using K (the encryption key) as the plaintext.
12961296+12971297+ How to decrypt a message:
12981298+12991299+ 1. Create an Enc_structure and populate it with the appropriate
13001300+ fields.
13011301+13021302+ 2. Encode the Enc_structure to a byte string (AAD), using the
13031303+ encoding described in Section 9.
13041304+13051305+ 3. Determine the decryption key. This step is dependent on the
13061306+ class of recipient algorithm being used. For:
13071307+13081308+ No Recipients: The key to be used is determined by the algorithm
13091309+ and key at the current layer. Examples are key wrap keys
13101310+ (Section 8.5.2) and preshared secrets.
13111311+13121312+ Direct Encryption and Direct Key Agreement: The key is
13131313+ determined by the key and algorithm in the recipient
13141314+ structure. The encryption algorithm and size of the key to be
13151315+ used are inputs into the KDF used for the recipient. (For
13161316+ direct, the KDF can be thought of as the identity operation.)
13171317+13181318+ Other: The key is determined by decoding and decrypting one of
13191319+ the recipient structures.
13201320+13211321+ 4. Call the decryption algorithm with K (the decryption key to use),
13221322+ C (the ciphertext), and AAD.
13231323+13241324+5.4. How to Encrypt and Decrypt for AE Algorithms
13251325+13261326+ How to encrypt a message:
13271327+13281328+ 1. Verify that the "protected" field is a zero-length byte string.
13291329+13301330+ 2. Verify that there was no external additional authenticated data
13311331+ supplied for this operation.
13321332+13331333+ 3. Determine the encryption key. This step is dependent on the
13341334+ class of recipient algorithm being used. For:
13351335+13361336+ No Recipients: The key to be used is determined by the algorithm
13371337+ and key at the current layer. Examples are key wrap keys
13381338+ (Section 8.5.2) and preshared secrets.
13391339+13401340+ Direct Encryption and Direct Key Agreement: The key is
13411341+ determined by the key and algorithm in the recipient
13421342+ structure. The encryption algorithm and size of the key to be
13431343+ used are inputs into the KDF used for the recipient. (For
13441344+ direct, the KDF can be thought of as the identity operation.)
13451345+ Examples of these algorithms are found in Sections 6.1 and 6.3
13461346+ of [RFC9053].
13471347+13481348+ Other: The key is randomly generated.
13491349+13501350+ 4. Call the encryption algorithm with K (the encryption key to use)
13511351+ and P (the plaintext). Place the returned ciphertext into the
13521352+ "ciphertext" field of the structure.
13531353+13541354+ 5. For recipients of the message using non-direct algorithms,
13551355+ recursively perform the encryption algorithm for that recipient,
13561356+ using K (the encryption key) as the plaintext.
13571357+13581358+ How to decrypt a message:
13591359+13601360+ 1. Verify that the "protected" field is a zero-length byte string.
13611361+13621362+ 2. Verify that there was no external additional authenticated data
13631363+ supplied for this operation.
13641364+13651365+ 3. Determine the decryption key. This step is dependent on the
13661366+ class of recipient algorithm being used. For:
13671367+13681368+ No Recipients: The key to be used is determined by the algorithm
13691369+ and key at the current layer. Examples are key wrap keys
13701370+ (Section 8.5.2) and preshared secrets.
13711371+13721372+ Direct Encryption and Direct Key Agreement: The key is
13731373+ determined by the key and algorithm in the recipient
13741374+ structure. The encryption algorithm and size of the key to be
13751375+ used are inputs into the KDF used for the recipient. (For
13761376+ direct, the KDF can be thought of as the identity operation.)
13771377+ Examples of these algorithms are found in Sections 6.1 and 6.3
13781378+ of [RFC9053].
13791379+13801380+ Other: The key is determined by decoding and decrypting one of
13811381+ the recipient structures.
13821382+13831383+ 4. Call the decryption algorithm with K (the decryption key to use)
13841384+ and C (the ciphertext).
13851385+13861386+6. MAC Objects
13871387+13881388+ COSE supports two different MAC structures. COSE_Mac0 is used when a
13891389+ recipient structure is not needed because the key to be used is
13901390+ implicitly known. COSE_Mac is used for all other cases. These
13911391+ include a requirement for multiple recipients, the key being unknown,
13921392+ or a recipient algorithm other than direct.
13931393+13941394+ In this section, we describe the structure and methods to be used
13951395+ when doing MAC authentication in COSE. This document allows for the
13961396+ use of all of the same classes of recipient algorithms as are allowed
13971397+ for encryption.
13981398+13991399+ There are two modes in which MAC operations can be used. The first
14001400+ is just a check that the content has not been changed since the MAC
14011401+ was computed. Any class of recipient algorithm can be used for this
14021402+ purpose. The second mode is to both check that the content has not
14031403+ been changed since the MAC was computed and use the recipient
14041404+ algorithm to verify who sent it. The classes of recipient algorithms
14051405+ that support this are those that use a preshared secret or do Static-
14061406+ Static (SS) key agreement (without the key wrap step). In both of
14071407+ these cases, the entity that created and sent the message MAC can be
14081408+ validated. (This knowledge of the sender assumes that there are only
14091409+ two parties involved and that you did not send the message to
14101410+ yourself.) The origination property can be obtained with both of the
14111411+ MAC message structures.
14121412+14131413+6.1. MACed Message with Recipients
14141414+14151415+ A multiple-recipient MACed message uses two structures: the COSE_Mac
14161416+ structure defined in this section for carrying the body and the
14171417+ COSE_recipient structure (Section 5.1) to hold the key used for the
14181418+ MAC computation. Examples of MACed messages can be found in
14191419+ Appendix C.5.
14201420+14211421+ The MAC structure can be encoded as either tagged or untagged
14221422+ depending on the context it will be used in. A tagged COSE_Mac
14231423+ structure is identified by the CBOR tag 97. The CDDL fragment that
14241424+ represents this is:
14251425+14261426+ COSE_Mac_Tagged = #6.97(COSE_Mac)
14271427+14281428+ The COSE_Mac structure is a CBOR array. The fields of the array, in
14291429+ order, are:
14301430+14311431+ protected: This is as described in Section 3.
14321432+14331433+ unprotected: This is as described in Section 3.
14341434+14351435+ payload: This field contains the serialized content to be MACed. If
14361436+ the payload is not present in the message, the application is
14371437+ required to supply the payload separately. The payload is wrapped
14381438+ in a bstr to ensure that it is transported without changes. If
14391439+ the payload is transported separately (i.e., detached content),
14401440+ then a nil CBOR value is placed in this location, and it is the
14411441+ responsibility of the application to ensure that it will be
14421442+ transported without changes.
14431443+14441444+ tag: This field contains the MAC value.
14451445+14461446+ recipients: This is as described in Section 5.1.
14471447+14481448+ The CDDL fragment that represents the above text for COSE_Mac
14491449+ follows.
14501450+14511451+ COSE_Mac = [
14521452+ Headers,
14531453+ payload : bstr / nil,
14541454+ tag : bstr,
14551455+ recipients : [+COSE_recipient]
14561456+ ]
14571457+14581458+6.2. MACed Messages with Implicit Key
14591459+14601460+ In this section, we describe the structure and methods to be used
14611461+ when doing MAC authentication for those cases where the recipient is
14621462+ implicitly known.
14631463+14641464+ The MACed message uses the COSE_Mac0 structure defined in this
14651465+ section for carrying the body. Examples of MACed messages with an
14661466+ implicit key can be found in Appendix C.6.
14671467+14681468+ The MAC structure can be encoded as either tagged or untagged,
14691469+ depending on the context it will be used in. A tagged COSE_Mac0
14701470+ structure is identified by the CBOR tag 17. The CDDL fragment that
14711471+ represents this is:
14721472+14731473+ COSE_Mac0_Tagged = #6.17(COSE_Mac0)
14741474+14751475+ The COSE_Mac0 structure is a CBOR array. The fields of the array, in
14761476+ order, are:
14771477+14781478+ protected: This is as described in Section 3.
14791479+14801480+ unprotected: This is as described in Section 3.
14811481+14821482+ payload: This is as described in Section 6.1.
14831483+14841484+ tag: This field contains the MAC value.
14851485+14861486+ The CDDL fragment that corresponds to the above text is:
14871487+14881488+ COSE_Mac0 = [
14891489+ Headers,
14901490+ payload : bstr / nil,
14911491+ tag : bstr,
14921492+ ]
14931493+14941494+6.3. How to Compute and Verify a MAC
14951495+14961496+ In order to get a consistent encoding of the data to be
14971497+ authenticated, the MAC_structure is used to create the canonical
14981498+ form. The MAC_structure is a CBOR array. The fields of the
14991499+ MAC_structure, in order, are:
15001500+15011501+ 1. A context text string that identifies the structure that is being
15021502+ encoded. This context text string is "MAC" for the COSE_Mac
15031503+ structure. This context text string is "MAC0" for the COSE_Mac0
15041504+ structure.
15051505+15061506+ 2. The protected attributes from the body structure. If there are
15071507+ no protected attributes, a zero-length bstr is used.
15081508+15091509+ 3. The externally supplied data from the application, encoded as a
15101510+ bstr type. If this field is not supplied, it defaults to a zero-
15111511+ length byte string. (See Section 4.3 for application guidance on
15121512+ constructing this field.)
15131513+15141514+ 4. The payload to be MACed, encoded in a bstr type. The full
15151515+ payload is used here, independent of how it is transported.
15161516+15171517+ The CDDL fragment that corresponds to the above text is:
15181518+15191519+ MAC_structure = [
15201520+ context : "MAC" / "MAC0",
15211521+ protected : empty_or_serialized_map,
15221522+ external_aad : bstr,
15231523+ payload : bstr
15241524+ ]
15251525+15261526+ The steps to compute a MAC are:
15271527+15281528+ 1. Create a MAC_structure and populate it with the appropriate
15291529+ fields.
15301530+15311531+ 2. Create the value ToBeMaced by encoding the MAC_structure to a
15321532+ byte string, using the encoding described in Section 9.
15331533+15341534+ 3. Call the MAC creation algorithm, passing in K (the key to use),
15351535+ alg (the algorithm to MAC with), and ToBeMaced (the value to
15361536+ compute the MAC on).
15371537+15381538+ 4. Place the resulting MAC in the "tag" field of the COSE_Mac or
15391539+ COSE_Mac0 structure.
15401540+15411541+ 5. For COSE_Mac structures, encrypt and encode the MAC key for each
15421542+ recipient of the message.
15431543+15441544+ The steps to verify a MAC are:
15451545+15461546+ 1. Create a MAC_structure and populate it with the appropriate
15471547+ fields.
15481548+15491549+ 2. Create the value ToBeMaced by encoding the MAC_structure to a
15501550+ byte string, using the encoding described in Section 9.
15511551+15521552+ 3. For COSE_Mac structures, obtain the cryptographic key by decoding
15531553+ and decrypting one of the recipient structures.
15541554+15551555+ 4. Call the MAC creation algorithm, passing in K (the key to use),
15561556+ alg (the algorithm to MAC with), and ToBeMaced (the value to
15571557+ compute the MAC on).
15581558+15591559+ 5. Compare the MAC value to the "tag" field of the COSE_Mac or
15601560+ COSE_Mac0 structure.
15611561+15621562+7. Key Objects
15631563+15641564+ A COSE Key structure is built on a CBOR map. The set of common
15651565+ parameters that can appear in a COSE Key can be found in the IANA
15661566+ "COSE Key Common Parameters" registry [COSE.KeyParameters] (see
15671567+ Section 11.2). Additional parameters defined for specific key types
15681568+ can be found in the IANA "COSE Key Type Parameters" registry
15691569+ [COSE.KeyTypes].
15701570+15711571+ A COSE Key Set uses a CBOR array object as its underlying type. The
15721572+ values of the array elements are COSE Keys. A COSE Key Set MUST have
15731573+ at least one element in the array. Examples of COSE Key Sets can be
15741574+ found in Appendix C.7.
15751575+15761576+ Each element in a COSE Key Set MUST be processed independently. If
15771577+ one element in a COSE Key Set is either malformed or uses a key that
15781578+ is not understood by an application, that key is ignored, and the
15791579+ other keys are processed normally.
15801580+15811581+ The element "kty" is a required element in a COSE_Key map.
15821582+15831583+ The CDDL grammar describing COSE_Key and COSE_KeySet is:
15841584+15851585+ COSE_Key = {
15861586+ 1 => tstr / int, ; kty
15871587+ ? 2 => bstr, ; kid
15881588+ ? 3 => tstr / int, ; alg
15891589+ ? 4 => [+ (tstr / int) ], ; key_ops
15901590+ ? 5 => bstr, ; Base IV
15911591+ * label => values
15921592+ }
15931593+15941594+ COSE_KeySet = [+COSE_Key]
15951595+15961596+7.1. COSE Key Common Parameters
15971597+15981598+ This document defines a set of common parameters for a COSE Key
15991599+ object. Table 4 provides a summary of the parameters defined in this
16001600+ section. There are also parameters that are defined for specific key
16011601+ types. Key-type-specific parameters can be found in [RFC9053].
16021602+16031603+ +=========+=======+========+============+====================+
16041604+ | Name | Label | CBOR | Value | Description |
16051605+ | | | Type | Registry | |
16061606+ +=========+=======+========+============+====================+
16071607+ | kty | 1 | tstr / | COSE Key | Identification of |
16081608+ | | | int | Types | the key type |
16091609+ +---------+-------+--------+------------+--------------------+
16101610+ | kid | 2 | bstr | | Key identification |
16111611+ | | | | | value -- match to |
16121612+ | | | | | "kid" in message |
16131613+ +---------+-------+--------+------------+--------------------+
16141614+ | alg | 3 | tstr / | COSE | Key usage |
16151615+ | | | int | Algorithms | restriction to |
16161616+ | | | | | this algorithm |
16171617+ +---------+-------+--------+------------+--------------------+
16181618+ | key_ops | 4 | [+ | | Restrict set of |
16191619+ | | | (tstr/ | | permissible |
16201620+ | | | int)] | | operations |
16211621+ +---------+-------+--------+------------+--------------------+
16221622+ | Base IV | 5 | bstr | | Base IV to be xor- |
16231623+ | | | | | ed with Partial |
16241624+ | | | | | IVs |
16251625+ +---------+-------+--------+------------+--------------------+
16261626+16271627+ Table 4: Key Map Labels
16281628+16291629+ kty: This parameter is used to identify the family of keys for this
16301630+ structure and, thus, the set of key-type-specific parameters to be
16311631+ found. The set of values defined in this document can be found in
16321632+ [COSE.KeyTypes]. This parameter MUST be present in a key object.
16331633+ Implementations MUST verify that the key type is appropriate for
16341634+ the algorithm being processed. The key type MUST be included as
16351635+ part of the trust-decision process.
16361636+16371637+ alg: This parameter is used to restrict the algorithm that is used
16381638+ with the key. If this parameter is present in the key structure,
16391639+ the application MUST verify that this algorithm matches the
16401640+ algorithm for which the key is being used. If the algorithms do
16411641+ not match, then this key object MUST NOT be used to perform the
16421642+ cryptographic operation. Note that the same key can be in a
16431643+ different key structure with a different or no algorithm
16441644+ specified; however, this is considered to be a poor security
16451645+ practice.
16461646+16471647+ kid: This parameter is used to give an identifier for a key. The
16481648+ identifier is not structured and can be anything from a user-
16491649+ provided byte string to a value computed on the public portion of
16501650+ the key. This field is intended for matching against a "kid"
16511651+ parameter in a message in order to filter down the set of keys
16521652+ that need to be checked. The value of the identifier is not a
16531653+ unique value and can occur in other key objects, even for
16541654+ different keys.
16551655+16561656+ key_ops: This parameter is defined to restrict the set of operations
16571657+ that a key is to be used for. The value of the field is an array
16581658+ of values from Table 5. Algorithms define the values of key ops
16591659+ that are permitted to appear and are required for specific
16601660+ operations. The set of values matches that in [RFC7517] and
16611661+ [W3C.WebCrypto].
16621662+16631663+ Base IV: This parameter is defined to carry the base portion of an
16641664+ IV. It is designed to be used with the Partial IV header
16651665+ parameter defined in Section 3.1. This field provides the ability
16661666+ to associate a Base IV with a key that is then modified on a per-
16671667+ message basis with the Partial IV.
16681668+16691669+ Extreme care needs to be taken when using a Base IV in an
16701670+ application. Many encryption algorithms lose security if the same
16711671+ IV is used twice.
16721672+16731673+ If different keys are derived for each sender, starting at the
16741674+ same Base IV is likely to satisfy this condition. If the same key
16751675+ is used for multiple senders, then the application needs to
16761676+ provide for a method of dividing the IV space up between the
16771677+ senders. This could be done by providing a different base point
16781678+ to start from or a different Partial IV to start with and
16791679+ restricting the number of messages to be sent before rekeying.
16801680+16811681+ +=========+=======+==============================================+
16821682+ | Name | Value | Description |
16831683+ +=========+=======+==============================================+
16841684+ | sign | 1 | The key is used to create signatures. |
16851685+ | | | Requires private key fields. |
16861686+ +---------+-------+----------------------------------------------+
16871687+ | verify | 2 | The key is used for verification of |
16881688+ | | | signatures. |
16891689+ +---------+-------+----------------------------------------------+
16901690+ | encrypt | 3 | The key is used for key transport |
16911691+ | | | encryption. |
16921692+ +---------+-------+----------------------------------------------+
16931693+ | decrypt | 4 | The key is used for key transport |
16941694+ | | | decryption. Requires private key fields. |
16951695+ +---------+-------+----------------------------------------------+
16961696+ | wrap | 5 | The key is used for key wrap encryption. |
16971697+ | key | | |
16981698+ +---------+-------+----------------------------------------------+
16991699+ | unwrap | 6 | The key is used for key wrap decryption. |
17001700+ | key | | Requires private key fields. |
17011701+ +---------+-------+----------------------------------------------+
17021702+ | derive | 7 | The key is used for deriving keys. Requires |
17031703+ | key | | private key fields. |
17041704+ +---------+-------+----------------------------------------------+
17051705+ | derive | 8 | The key is used for deriving bits not to be |
17061706+ | bits | | used as a key. Requires private key fields. |
17071707+ +---------+-------+----------------------------------------------+
17081708+ | MAC | 9 | The key is used for creating MACs. |
17091709+ | create | | |
17101710+ +---------+-------+----------------------------------------------+
17111711+ | MAC | 10 | The key is used for validating MACs. |
17121712+ | verify | | |
17131713+ +---------+-------+----------------------------------------------+
17141714+17151715+ Table 5: Key Operation Values
17161716+17171717+8. Taxonomy of Algorithms Used by COSE
17181718+17191719+ In this section, a taxonomy of the different algorithm types that can
17201720+ be used in COSE is laid out. This taxonomy should not be considered
17211721+ to be exhaustive. New algorithms will be created that will not fit
17221722+ into this taxonomy.
17231723+17241724+8.1. Signature Algorithms
17251725+17261726+ Signature algorithms provide data-origination and data-integrity
17271727+ services. Data origination provides the ability to infer who
17281728+ originated the data based on who signed the data. Data integrity
17291729+ provides the ability to verify that the data has not been modified
17301730+ since it was signed.
17311731+17321732+ There are two general signature algorithm schemes. The first is
17331733+ signature with appendix. In this scheme, the message content is
17341734+ processed and a signature is produced; the signature is called the
17351735+ appendix. This is the scheme used by algorithms such as ECDSA and
17361736+ the RSA Probabilistic Signature Scheme (RSASSA-PSS). (In fact, the
17371737+ SSA in RSASSA-PSS stands for Signature Scheme with Appendix.)
17381738+17391739+ The signature functions for this scheme are:
17401740+17411741+ signature = Sign(message content, key)
17421742+17431743+ valid = Verification(message content, key, signature)
17441744+17451745+ The second scheme is signature with message recovery; an example of
17461746+ such an algorithm is [PVSig]. In this scheme, the message content is
17471747+ processed, but part of it is included in the signature. Moving bytes
17481748+ of the message content into the signature allows for smaller signed
17491749+ messages; the signature size is still potentially large, but the
17501750+ message content has shrunk. This has implications for systems
17511751+ implementing these algorithms and applications that use them. The
17521752+ first is that the message content is not fully available until after
17531753+ a signature has been validated. Until that point, the part of the
17541754+ message contained inside of the signature is unrecoverable. The
17551755+ second implication is that the security analysis of the strength of
17561756+ the signature can be very much dependent on the structure of the
17571757+ message content. Finally, in the event that multiple signatures are
17581758+ applied to a message, all of the signature algorithms are going to be
17591759+ required to consume the same bytes of message content. This means
17601760+ that the mixing of the signature-with-message-recovery and signature-
17611761+ with-appendix schemes in a single message is not supported.
17621762+17631763+ The signature functions for this scheme are:
17641764+17651765+ signature, message sent = Sign(message content, key)
17661766+17671767+ valid, message content = Verification(message sent, key, signature)
17681768+17691769+ No message recovery signature algorithms have been formally defined
17701770+ for COSE yet. Given the new constraints arising from this scheme,
17711771+ while some issues have already been identified, there is a high
17721772+ probability that additional issues will arise when integrating
17731773+ message recovery signature algorithms. The first algorithm defined
17741774+ is going to need to make decisions about these issues, and those
17751775+ decisions are likely to be binding on any further algorithms defined.
17761776+17771777+ We use the following terms below:
17781778+17791779+ message content bytes: The byte string provided by the application
17801780+ to be signed.
17811781+17821782+ to-be-signed bytes: The byte string passed into the signature
17831783+ algorithm.
17841784+17851785+ recovered bytes: The bytes recovered during the signature
17861786+ verification process.
17871787+17881788+ Some of the issues that have already been identified are:
17891789+17901790+ * The to-be-signed bytes are not the same as the message content
17911791+ bytes. This is because we build a larger to-be-signed message
17921792+ during the signature processing. The length of the recovered
17931793+ bytes may exceed the length of the message content, but not the
17941794+ length of the to-be-signed bytes. This may lead to privacy
17951795+ considerations if, for example, the externally supplied data
17961796+ contains confidential information.
17971797+17981798+ * There may be difficulties in determining where the recovered bytes
17991799+ match up with the to-be-signed bytes, because the recovered bytes
18001800+ contain data not in the message content bytes. One possible
18011801+ option would be to create a padding scheme to prevent that.
18021802+18031803+ * Not all message recovery signature algorithms take the recovered
18041804+ bytes from the end of the to-be-signed bytes. This is a problem,
18051805+ because the message content bytes are at the end of the to-be-
18061806+ signed bytes. If the bytes to be recovered are taken from the
18071807+ start of the to-be-signed bytes, then, by default, none of the
18081808+ message content bytes may be included in the recovered bytes. One
18091809+ possible option to deal with this is to reverse the to-be-signed
18101810+ data in the event that recovered bytes are taken from the start
18111811+ rather than the end of the to-be-signed bytes.
18121812+18131813+ Signature algorithms are used with the COSE_Signature and COSE_Sign1
18141814+ structures. At the time of this writing, only signatures with
18151815+ appendices are defined for use with COSE; however, considerable
18161816+ interest has been expressed in using a signature-with-message-
18171817+ recovery algorithm, due to the effective size reduction that is
18181818+ possible.
18191819+18201820+8.2. Message Authentication Code (MAC) Algorithms
18211821+18221822+ Message Authentication Codes (MACs) provide data authentication and
18231823+ integrity protection. They provide either no or very limited data
18241824+ origination. A MAC, for example, cannot be used to prove the
18251825+ identity of the sender to a third party.
18261826+18271827+ MACs use the same scheme as signature-with-appendix algorithms. The
18281828+ message content is processed, and an authentication code is produced.
18291829+ The authentication code is frequently called a tag.
18301830+18311831+ The MAC functions are:
18321832+18331833+ tag = MAC_Create(message content, key)
18341834+18351835+ valid = MAC_Verify(message content, key, tag)
18361836+18371837+ MAC algorithms can be based on either a block cipher algorithm (i.e.,
18381838+ AES-MAC) or a hash algorithm (i.e., a Hash-based Message
18391839+ Authentication Code (HMAC)). [RFC9053] defines a MAC algorithm using
18401840+ each of these constructions.
18411841+18421842+ MAC algorithms are used in the COSE_Mac and COSE_Mac0 structures.
18431843+18441844+8.3. Content Encryption Algorithms
18451845+18461846+ Content encryption algorithms provide data confidentiality for
18471847+ potentially large blocks of data using a symmetric key. They provide
18481848+ integrity on the data that was encrypted; however, they provide
18491849+ either no or very limited data origination. (One cannot, for
18501850+ example, be used to prove the identity of the sender to a third
18511851+ party.) The ability to provide data origination is linked to how the
18521852+ CEK is obtained.
18531853+18541854+ COSE restricts the set of legal content encryption algorithms to
18551855+ those that support authentication both of the content and additional
18561856+ data. The encryption process will generate some type of
18571857+ authentication value, but that value may be either explicit or
18581858+ implicit in terms of the algorithm definition. For simplicity's
18591859+ sake, the authentication code will normally be defined as being
18601860+ appended to the ciphertext stream. The encryption functions are:
18611861+18621862+ ciphertext = Encrypt(message content, key, additional data)
18631863+18641864+ valid, message content = Decrypt(ciphertext, key, additional data)
18651865+18661866+ Most AEAD algorithms are logically defined as returning the message
18671867+ content only if the decryption is valid. Many, but not all,
18681868+ implementations will follow this convention. The message content
18691869+ MUST NOT be used if the decryption does not validate.
18701870+18711871+ These algorithms are used in COSE_Encrypt and COSE_Encrypt0.
18721872+18731873+8.4. Key Derivation Functions (KDFs)
18741874+18751875+ KDFs are used to take some secret value and generate a different one.
18761876+ The secret value comes in three flavors:
18771877+18781878+ * Secrets that are uniformly random. This is the type of secret
18791879+ that is created by a good random number generator.
18801880+18811881+ * Secrets that are not uniformly random. This is the type of secret
18821882+ that is created by operations like key agreement.
18831883+18841884+ * Secrets that are not random. This is the type of secret that
18851885+ people generate for things like passwords.
18861886+18871887+ General KDFs work well with the first type of secret, can do
18881888+ reasonably well with the second type of secret, and generally do
18891889+ poorly with the last type of secret. Functions like Argon2 [RFC9106]
18901890+ need to be used for nonrandom secrets.
18911891+18921892+ The same KDF can be set up to deal with the first two types of
18931893+ secrets in different ways. The KDF defined in Section 5.1 of
18941894+ [RFC9053] is such a function. This is reflected in the set of
18951895+ algorithms defined around the HMAC-based Extract-and-Expand Key
18961896+ Derivation Function (HKDF).
18971897+18981898+ When using KDFs, one component that is included is context
18991899+ information. Context information is used to allow for different
19001900+ keying information to be derived from the same secret. The use of
19011901+ context-based keying material is considered to be a good security
19021902+ practice.
19031903+19041904+8.5. Content Key Distribution Methods
19051905+19061906+ Content key distribution methods (recipient algorithms) can be
19071907+ defined into a number of different classes. COSE has the ability to
19081908+ support many classes of recipient algorithms. In this section, a
19091909+ number of classes are listed. For the recipient algorithm classes
19101910+ defined in [RFC7516], the same names are used. Other specifications
19111911+ use different terms for the recipient algorithm classes or do not
19121912+ support some of the recipient algorithm classes.
19131913+19141914+8.5.1. Direct Encryption
19151915+19161916+ The Direct Encryption class of algorithms share a secret between the
19171917+ sender and the recipient that is used either directly or after
19181918+ manipulation as the CEK. When direct-encryption mode is used, it
19191919+ MUST be the only mode used on the message.
19201920+19211921+ The COSE_Recipient structure for the recipient is organized as
19221922+ follows:
19231923+19241924+ * The "protected" field MUST be a zero-length byte string unless it
19251925+ is used in the computation of the content key.
19261926+19271927+ * The "alg" header parameter MUST be present.
19281928+19291929+ * A header parameter identifying the shared secret SHOULD be
19301930+ present.
19311931+19321932+ * The "ciphertext" field MUST be a zero-length byte string.
19331933+19341934+ * The "recipients" field MUST be absent.
19351935+19361936+8.5.2. Key Wrap
19371937+19381938+ In key wrap mode, the CEK is randomly generated, and that key is then
19391939+ encrypted by a shared secret between the sender and the recipient.
19401940+ All of the currently defined key wrap algorithms for COSE are AE
19411941+ algorithms. Key wrap mode is considered to be superior to Direct
19421942+ Encryption if the system has any capability for doing random-key
19431943+ generation. This is because the shared key is used to wrap random
19441944+ data rather than data that has some degree of organization and may in
19451945+ fact be repeating the same content. The use of key wrap loses the
19461946+ weak data origination that is provided by the direct-encryption
19471947+ algorithms.
19481948+19491949+ The COSE_Recipient structure for the recipient is organized as
19501950+ follows:
19511951+19521952+ * The "protected" field MUST be a zero-length byte string if the key
19531953+ wrap algorithm is an AE algorithm.
19541954+19551955+ * The "recipients" field is normally absent but can be used.
19561956+ Applications MUST deal with a recipient field being present that
19571957+ has an unsupported algorithm. Failing to decrypt that specific
19581958+ recipient is an acceptable way of dealing with it. Failing to
19591959+ process the message is not an acceptable way of dealing with it.
19601960+19611961+ * The plaintext to be encrypted is the key from the next layer down
19621962+ (usually the content layer).
19631963+19641964+ * At a minimum, the "unprotected" field MUST contain the "alg"
19651965+ header parameter and SHOULD contain a header parameter identifying
19661966+ the shared secret.
19671967+19681968+8.5.3. Key Transport
19691969+19701970+ Key transport mode is also called key encryption mode in some
19711971+ standards. Key transport mode differs from key wrap mode in that it
19721972+ uses an asymmetric encryption algorithm rather than a symmetric
19731973+ encryption algorithm to protect the key. A set of key transport
19741974+ algorithms is defined in [RFC8230].
19751975+19761976+ When using a key transport algorithm, the COSE_Recipient structure
19771977+ for the recipient is organized as follows:
19781978+19791979+ * The "protected" field MUST be a zero-length byte string.
19801980+19811981+ * The plaintext to be encrypted is the key from the next layer down
19821982+ (usually the content layer).
19831983+19841984+ * At a minimum, the "unprotected" field MUST contain the "alg"
19851985+ header parameter and SHOULD contain a parameter identifying the
19861986+ asymmetric key.
19871987+19881988+8.5.4. Direct Key Agreement
19891989+19901990+ The Direct Key Agreement class of recipient algorithms uses a key
19911991+ agreement method to create a shared secret. A KDF is then applied to
19921992+ the shared secret to derive a key to be used in protecting the data.
19931993+ This key is normally used as a CEK or MAC key but could be used for
19941994+ other purposes if more than two layers are in use (see Appendix B).
19951995+19961996+ The most commonly used key agreement algorithm is Diffie-Hellman, but
19971997+ other variants exist. Since COSE is designed for a store-and-forward
19981998+ environment rather than an online environment, many of the DH
19991999+ variants cannot be used, as the receiver of the message cannot
20002000+ provide any dynamic key material. One side effect of this is that
20012001+ forward secrecy (see [RFC4949]) is not achievable. A static key will
20022002+ always be used for the receiver of the COSE object.
20032003+20042004+ Two variants of DH that are supported are:
20052005+20062006+ Ephemeral-Static (ES) DH: The sender of the message creates a one-
20072007+ time DH key and uses a static key for the recipient. The use of
20082008+ the ephemeral sender key means that no additional random input is
20092009+ needed, as this is randomly generated for each message.
20102010+20112011+ Static-Static (SS) DH: A static key is used for both the sender and
20122012+ the recipient. The use of static keys allows for the recipient to
20132013+ get a weak version of data origination for the message. When
20142014+ Static-Static key agreement is used, then some piece of unique
20152015+ data for the KDF is required to ensure that a different key is
20162016+ created for each message.
20172017+20182018+ When direct key agreement mode is used, there MUST be only one
20192019+ recipient in the message. This method creates the key directly, and
20202020+ that makes it difficult to mix with additional recipients. If
20212021+ multiple recipients are needed, then the version with key wrap needs
20222022+ to be used.
20232023+20242024+ The COSE_Recipient structure for the recipient is organized as
20252025+ follows:
20262026+20272027+ * At a minimum, headers MUST contain the "alg" header parameter and
20282028+ SHOULD contain a header parameter identifying the recipient's
20292029+ asymmetric key.
20302030+20312031+ * The headers SHOULD identify the sender's key for the Static-Static
20322032+ versions and MUST contain the sender's ephemeral key for the
20332033+ ephemeral-static versions.
20342034+20352035+8.5.5. Key Agreement with Key Wrap
20362036+20372037+ Key Agreement with Key Wrap uses a randomly generated CEK. The CEK
20382038+ is then encrypted using a key wrap algorithm and a key derived from
20392039+ the shared secret computed by the key agreement algorithm. The
20402040+ function for this would be:
20412041+20422042+ encryptedKey = KeyWrap(KDF(DH-Shared, context), CEK)
20432043+20442044+ The COSE_Recipient structure for the recipient is organized as
20452045+ follows:
20462046+20472047+ * The "protected" field is fed into the KDF context structure.
20482048+20492049+ * The plaintext to be encrypted is the key from the next layer down
20502050+ (usually the content layer).
20512051+20522052+ * The "alg" header parameter MUST be present in the layer.
20532053+20542054+ * A header parameter identifying the recipient's key SHOULD be
20552055+ present. A header parameter identifying the sender's key SHOULD
20562056+ be present.
20572057+20582058+9. CBOR Encoding Restrictions
20592059+20602060+ This document limits the restrictions it imposes on how the CBOR
20612061+ Encoder needs to work. The new encoding restrictions are aligned
20622062+ with the Core Deterministic Encoding Requirements specified in
20632063+ Section 4.2.1 of RFC 8949 [STD94]. It has been narrowed down to the
20642064+ following restrictions:
20652065+20662066+ * The restriction applies to the encoding of the Sig_structure, the
20672067+ Enc_structure, and the MAC_structure.
20682068+20692069+ * Encoding MUST be done using definite lengths, and the length of
20702070+ the (encoded) argument MUST be the minimum possible length. This
20712071+ means that the integer 1 is encoded as "0x01" and not "0x1801".
20722072+20732073+ * Applications MUST NOT generate messages with the same label used
20742074+ twice as a key in a single map. Applications MUST NOT parse and
20752075+ process messages with the same label used twice as a key in a
20762076+ single map. Applications can enforce the parse-and-process
20772077+ requirement by using parsers that will fail the parse step or by
20782078+ using parsers that will pass all keys to the application, and the
20792079+ application can perform the check for duplicate keys.
20802080+20812081+10. Application Profiling Considerations
20822082+20832083+ This document is designed to provide a set of security services but
20842084+ not impose algorithm implementation requirements for specific usage.
20852085+ The interoperability requirements are provided for how each of the
20862086+ individual services are used and how the algorithms are to be used
20872087+ for interoperability. The requirements about which algorithms and
20882088+ which services are needed are deferred to each application.
20892089+20902090+ An example of a profile can be found in [RFC8613], where one was
20912091+ developed for carrying content in combination with CoAP headers.
20922092+20932093+ It is intended that a profile of this document be created that
20942094+ defines the interoperability requirements for that specific
20952095+ application. This section provides a set of guidelines and topics
20962096+ that need to be considered when profiling this document.
20972097+20982098+ * Applications need to determine the set of messages defined in this
20992099+ document that they will be using. The set of messages corresponds
21002100+ fairly directly to the needed set of security services and
21012101+ security levels.
21022102+21032103+ * Applications may define new header parameters for a specific
21042104+ purpose. Applications will oftentimes select specific header
21052105+ parameters to use or not to use. For example, an application
21062106+ would normally state a preference for using either the IV or the
21072107+ Partial IV header parameter. If the Partial IV header parameter
21082108+ is specified, then the application also needs to define how the
21092109+ fixed portion of the IV is determined.
21102110+21112111+ * When applications use externally defined authenticated data, they
21122112+ need to define how that data is encoded. This document assumes
21132113+ that the data will be provided as a byte string. More information
21142114+ can be found in Section 4.3.
21152115+21162116+ * Applications need to determine the set of security algorithms that
21172117+ is to be used. When selecting the algorithms to be used as the
21182118+ mandatory-to-implement set, consideration should be given to
21192119+ choosing different types of algorithms when two are chosen for a
21202120+ specific purpose. An example of this would be choosing HMAC-
21212121+ SHA512 and AES-CMAC (Cipher-Based Message Authentication Code) as
21222122+ different MAC algorithms; the construction is vastly different
21232123+ between these two algorithms. This means that a weakening of one
21242124+ algorithm would be unlikely to lead to a weakening of the other
21252125+ algorithms. Of course, these algorithms do not provide the same
21262126+ level of security and thus may not be comparable for the desired
21272127+ security functionality. Additional guidance can be found in
21282128+ [BCP201].
21292129+21302130+ * Applications may need to provide some type of negotiation or
21312131+ discovery method if multiple algorithms or message structures are
21322132+ permitted. The method can range from something as simple as
21332133+ requiring preconfiguration of the set of algorithms to providing a
21342134+ discovery method built into the protocol. S/MIME provided a
21352135+ number of different ways to approach the problem that applications
21362136+ could follow:
21372137+21382138+ - Advertising in the message (S/MIME capabilities) [RFC8551].
21392139+21402140+ - Advertising in the certificate (capabilities extension)
21412141+ [RFC4262].
21422142+21432143+ - Minimum requirements for the S/MIME, which have been updated
21442144+ over time [RFC2633] [RFC3851] [RFC5751] [RFC8551]. (Note that
21452145+ [RFC2633] was obsoleted by [RFC3851], which was obsoleted by
21462146+ [RFC5751], which was obsoleted by [RFC8551].)
21472147+21482148+11. IANA Considerations
21492149+21502150+ The registries and registrations listed below were defined by RFC
21512151+ 8152 [RFC8152]. The majority of the following actions are to update
21522152+ the references to point to this document.
21532153+21542154+ Note that while [RFC9053] also updates the registries and
21552155+ registrations originally established by [RFC8152], the requested
21562156+ updates are mutually exclusive. The updates requested in this
21572157+ document do not conflict or overlap with the updates requested in
21582158+ [RFC9053], and vice versa.
21592159+21602160+11.1. COSE Header Parameters Registry
21612161+21622162+ The "COSE Header Parameters" registry was defined by [RFC8152]. IANA
21632163+ has updated the reference for this registry to point to this document
21642164+ instead of [RFC8152]. IANA has also updated all entries that
21652165+ referenced [RFC8152], except "counter signature" and
21662166+ "CounterSignature0", to refer to this document. The references for
21672167+ "counter signature" and "CounterSignature0" continue to reference
21682168+ [RFC8152].
21692169+21702170+11.2. COSE Key Common Parameters Registry
21712171+21722172+ The "COSE Key Common Parameters" registry [COSE.KeyParameters] was
21732173+ defined in [RFC8152]. IANA has updated the reference for this
21742174+ registry to point to this document instead of [RFC8152]. IANA has
21752175+ also updated the entries that referenced [RFC8152] to refer to this
21762176+ document.
21772177+21782178+11.3. Media Type Registrations
21792179+21802180+11.3.1. COSE Security Message
21812181+21822182+ IANA has registered the "application/cose" media type in the "Media
21832183+ Types" registry. This media type is used to indicate that the
21842184+ content is a COSE message.
21852185+21862186+ Type name: application
21872187+21882188+ Subtype name: cose
21892189+21902190+ Required parameters: N/A
21912191+21922192+ Optional parameters: cose-type
21932193+21942194+ Encoding considerations: binary
21952195+21962196+ Security considerations: See the Security Considerations section of
21972197+ RFC 9052.
21982198+21992199+ Interoperability considerations: N/A
22002200+22012201+ Published specification: RFC 9052
22022202+22032203+ Applications that use this media type: IoT applications sending
22042204+ security content over HTTP(S) transports.
22052205+22062206+ Fragment identifier considerations: N/A
22072207+22082208+ Additional information:
22092209+ * Deprecated alias names for this type: N/A
22102210+22112211+ * Magic number(s): N/A
22122212+22132213+ * File extension(s): cbor
22142214+22152215+ * Macintosh file type code(s): N/A
22162216+22172217+ Person & email address to contact for further information:
22182218+ iesg@ietf.org
22192219+22202220+ Intended usage: COMMON
22212221+22222222+ Restrictions on usage: N/A
22232223+22242224+ Author: Jim Schaad
22252225+22262226+ Change Controller: IESG
22272227+22282228+ Provisional registration? No
22292229+22302230+11.3.2. COSE Key Media Type
22312231+22322232+ IANA has registered the "application/cose-key" and "application/cose-
22332233+ key-set" media types in the "Media Types" registry. These media
22342234+ types are used to indicate, respectively, that the content is a
22352235+ COSE_Key or COSE_KeySet object.
22362236+22372237+ The template for "application/cose-key" is as follows:
22382238+22392239+ Type name: application
22402240+22412241+ Subtype name: cose-key
22422242+22432243+ Required parameters: N/A
22442244+22452245+ Optional parameters: N/A
22462246+22472247+ Encoding considerations: binary
22482248+22492249+ Security considerations: See the Security Considerations section of
22502250+ RFC 9052.
22512251+22522252+ Interoperability considerations: N/A
22532253+22542254+ Published specification: RFC 9052
22552255+22562256+ Applications that use this media type: Distribution of COSE-based
22572257+ keys for IoT applications.
22582258+22592259+ Fragment identifier considerations: N/A
22602260+22612261+ Additional information:
22622262+ * Deprecated alias names for this type: N/A
22632263+22642264+ * Magic number(s): N/A
22652265+22662266+ * File extension(s): cbor
22672267+22682268+ * Macintosh file type code(s): N/A
22692269+22702270+ Person & email address to contact for further information:
22712271+ iesg@ietf.org
22722272+22732273+ Intended usage: COMMON
22742274+22752275+ Restrictions on usage: N/A
22762276+22772277+ Author: Jim Schaad
22782278+22792279+ Change Controller: IESG
22802280+22812281+ Provisional registration? No
22822282+22832283+ The template for registering "application/cose-key-set" is:
22842284+22852285+ Type name: application
22862286+22872287+ Subtype name: cose-key-set
22882288+22892289+ Required parameters: N/A
22902290+22912291+ Optional parameters: N/A
22922292+22932293+ Encoding considerations: binary
22942294+22952295+ Security considerations: See the Security Considerations section of
22962296+ RFC 9052.
22972297+22982298+ Interoperability considerations: N/A
22992299+23002300+ Published specification: RFC 9052
23012301+23022302+ Applications that use this media type: Distribution of COSE-based
23032303+ keys for IoT applications.
23042304+23052305+ Fragment identifier considerations: N/A
23062306+23072307+ Additional information:
23082308+ * Deprecated alias names for this type: N/A
23092309+23102310+ * Magic number(s): N/A
23112311+23122312+ * File extension(s): cbor
23132313+23142314+ * Macintosh file type code(s): N/A
23152315+23162316+ Person & email address to contact for further information: iesg@ietf
23172317+ .org
23182318+23192319+ Intended usage: COMMON
23202320+23212321+ Restrictions on usage: N/A
23222322+23232323+ Author: Jim Schaad
23242324+23252325+ Change Controller: IESG
23262326+23272327+ Provisional registration? No
23282328+23292329+11.4. CoAP Content-Formats Registry
23302330+23312331+ IANA added entries to the "CoAP Content-Formats" registry as
23322332+ indicated in [RFC8152]. IANA has updated the reference to point to
23332333+ this document instead of [RFC8152].
23342334+23352335+11.5. CBOR Tags Registry
23362336+23372337+ IANA added entries to the "CBOR Tags" registry as indicated in
23382338+ [RFC8152]. IANA has updated the references to point to this document
23392339+ instead of [RFC8152].
23402340+23412341+11.6. Expert Review Instructions
23422342+23432343+ All of the IANA registries established by [RFC8152] are, at least in
23442344+ part, defined as Expert Review [RFC8126]. This section gives some
23452345+ general guidelines for what the experts should be looking for, but
23462346+ they are being designated as experts for a reason, so they should be
23472347+ given substantial latitude.
23482348+23492349+ Expert reviewers should take the following into consideration:
23502350+23512351+ * Point squatting should be discouraged. Reviewers are encouraged
23522352+ to get sufficient information for registration requests to ensure
23532353+ that the usage is not going to duplicate an existing registration
23542354+ and that the code point is likely to be used in deployments. The
23552355+ ranges tagged as private use are intended for testing purposes and
23562356+ closed environments; code points in other ranges should not be
23572357+ assigned for testing.
23582358+23592359+ * Standards Track or BCP RFCs are required to register a code point
23602360+ in the Standards Action range. Specifications should exist for
23612361+ Specification Required ranges, but early assignment before an RFC
23622362+ is available is considered to be permissible. Specifications are
23632363+ needed for the first-come, first-served range if the points are
23642364+ expected to be used outside of closed environments in an
23652365+ interoperable way. When specifications are not provided, the
23662366+ description provided needs to have sufficient information to
23672367+ identify what the point is being used for.
23682368+23692369+ * Experts should take into account the expected usage of fields when
23702370+ approving code point assignment. The fact that the Standards
23712371+ Action range is only available to Standards Track documents does
23722372+ not mean that a Standards Track document cannot have points
23732373+ assigned outside of that range. The length of the encoded value
23742374+ should be weighed against how many code points of that length are
23752375+ left and the size of device it will be used on.
23762376+23772377+ * When algorithms are registered, vanity registrations should be
23782378+ discouraged. One way to do this is to require registrations to
23792379+ provide additional documentation on security analysis of the
23802380+ algorithm. Another thing that should be considered is requesting
23812381+ an opinion on the algorithm from the Crypto Forum Research Group
23822382+ (CFRG). Algorithms are expected to meet the security requirements
23832383+ of the community and the requirements of the message structures in
23842384+ order to be suitable for registration.
23852385+23862386+12. Security Considerations
23872387+23882388+ There are a number of security considerations that need to be taken
23892389+ into account by implementers of this specification. While some
23902390+ considerations have been highlighted here, additional considerations
23912391+ may be found in the documents listed in the references.
23922392+23932393+ Implementations need to protect the private key material for all
23942394+ individuals. Some cases in this document need to be highlighted with
23952395+ regard to this issue.
23962396+23972397+ * Use of the same key for two different algorithms can leak
23982398+ information about the key. It is therefore recommended that keys
23992399+ be restricted to a single algorithm.
24002400+24012401+ * Use of "direct" as a recipient algorithm combined with a second
24022402+ recipient algorithm exposes the direct key to the second
24032403+ recipient; Section 8.5 forbids combining "direct" recipient
24042404+ algorithms with other modes.
24052405+24062406+ * Several of the algorithms in [RFC9053] have limits on the number
24072407+ of times that a key can be used without leaking information about
24082408+ the key.
24092409+24102410+ The use of Elliptic Curve Diffie-Hellman (ECDH) and direct plus KDF
24112411+ (with no key wrap) will not directly lead to the private key being
24122412+ leaked; the one-way function of the KDF will prevent that. There is,
24132413+ however, a different issue that needs to be addressed. Having two
24142414+ recipients requires that the CEK be shared between two recipients.
24152415+ The second recipient therefore has a CEK that was derived from
24162416+ material that can be used for the weak proof of origin. The second
24172417+ recipient could create a message using the same CEK and send it to
24182418+ the first recipient; the first recipient would, for either Static-
24192419+ Static ECDH or direct plus KDF, make an assumption that the CEK could
24202420+ be used for proof of origin, even though it is from the wrong entity.
24212421+ If the key wrap step is added, then no proof of origin is implied and
24222422+ this is not an issue.
24232423+24242424+ Although it has been mentioned before, it bears repeating that the
24252425+ use of a single key for multiple algorithms has been demonstrated in
24262426+ some cases to leak information about a key, providing the opportunity
24272427+ for attackers to forge integrity tags or gain information about
24282428+ encrypted content. Binding a key to a single algorithm prevents
24292429+ these problems. Key creators and key consumers are strongly
24302430+ encouraged to not only create new keys for each different algorithm,
24312431+ but to include that selection of algorithm in any distribution of key
24322432+ material and strictly enforce the matching of algorithms in the key
24332433+ structure to algorithms in the message structure. In addition to
24342434+ checking that algorithms are correct, the key form needs to be
24352435+ checked as well. Do not use an "EC2" key where an "OKP" key is
24362436+ expected.
24372437+24382438+ Before using a key for transmission, or before acting on information
24392439+ received, a trust decision on a key needs to be made. Is the data or
24402440+ action something that the entity associated with the key has a right
24412441+ to see or a right to request? A number of factors are associated
24422442+ with this trust decision. Some highlighted here are:
24432443+24442444+ * What are the permissions associated with the key owner?
24452445+24462446+ * Is the cryptographic algorithm acceptable in the current context?
24472447+24482448+ * Have the restrictions associated with the key, such as algorithm
24492449+ or freshness, been checked, and are they correct?
24502450+24512451+ * Is the request something that is reasonable, given the current
24522452+ state of the application?
24532453+24542454+ * Have any security considerations that are part of the message been
24552455+ enforced (as specified by the application or "crit" header
24562456+ parameter)?
24572457+24582458+ One area that has been getting exposure is traffic analysis of
24592459+ encrypted messages based on the length of the message. This
24602460+ specification does not provide a uniform method for providing padding
24612461+ as part of the message structure. An observer can distinguish
24622462+ between two different messages (for example, "YES" and "NO") based on
24632463+ the length for all of the content encryption algorithms that are
24642464+ defined in [RFC9053]. This means that it is up to the applications
24652465+ to document how content padding is to be done in order to prevent or
24662466+ discourage such analysis. (For example, the text strings could be
24672467+ defined as "YES" and "NO ".)
24682468+24692469+13. References
24702470+24712471+13.1. Normative References
24722472+24732473+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
24742474+ Requirement Levels", BCP 14, RFC 2119,
24752475+ DOI 10.17487/RFC2119, March 1997,
24762476+ <https://www.rfc-editor.org/info/rfc2119>.
24772477+24782478+ [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
24792479+ 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
24802480+ May 2017, <https://www.rfc-editor.org/info/rfc8174>.
24812481+24822482+ [RFC9053] Schaad, J., "CBOR Object Signing and Encryption (COSE):
24832483+ Initial Algorithms", RFC 9053, DOI 10.17487/RFC9053,
24842484+ August 2022, <https://www.rfc-editor.org/info/rfc9053>.
24852485+24862486+ [STD94] Bormann, C. and P. Hoffman, "Concise Binary Object
24872487+ Representation (CBOR)", STD 94, RFC 8949, December 2020,
24882488+ <https://www.rfc-editor.org/info/std94>.
24892489+24902490+13.2. Informative References
24912491+24922492+ [BCP201] Housley, R., "Guidelines for Cryptographic Algorithm
24932493+ Agility and Selecting Mandatory-to-Implement Algorithms",
24942494+ BCP 201, RFC 7696, November 2015,
24952495+ <https://www.rfc-editor.org/info/bcp201>.
24962496+24972497+ [COAP.Formats]
24982498+ IANA, "CoAP Content-Formats",
24992499+ <https://www.iana.org/assignments/core-parameters/>.
25002500+25012501+ [CORE-GROUPCOMM]
25022502+ Dijk, E., Wang, C., and M. Tiloca, "Group Communication
25032503+ for the Constrained Application Protocol (CoAP)", Work in
25042504+ Progress, Internet-Draft, draft-ietf-core-groupcomm-bis-
25052505+ 07, 11 July 2022, <https://datatracker.ietf.org/doc/html/
25062506+ draft-ietf-core-groupcomm-bis-07>.
25072507+25082508+ [COSE-COUNTERSIGN]
25092509+ Schaad, J. and R. Housley, "CBOR Object Signing and
25102510+ Encryption (COSE): Countersignatures", Work in Progress,
25112511+ Internet-Draft, draft-ietf-cose-countersign-08, 22 August
25122512+ 2022, <https://datatracker.ietf.org/doc/html/draft-ietf-
25132513+ cose-countersign-08>.
25142514+25152515+ [COSE.Algorithms]
25162516+ IANA, "COSE Algorithms",
25172517+ <https://www.iana.org/assignments/cose/>.
25182518+25192519+ [COSE.KeyParameters]
25202520+ IANA, "COSE Key Common Parameters",
25212521+ <https://www.iana.org/assignments/cose/>.
25222522+25232523+ [COSE.KeyTypes]
25242524+ IANA, "COSE Key Types",
25252525+ <https://www.iana.org/assignments/cose/>.
25262526+25272527+ [DSS] National Institute of Standards and Technology, "Digital
25282528+ Signature Standard (DSS)", FIPS 186-4,
25292529+ DOI 10.6028/NIST.FIPS.186-4, July 2013,
25302530+ <https://nvlpubs.nist.gov/nistpubs/FIPS/
25312531+ NIST.FIPS.186-4.pdf>.
25322532+25332533+ [GitHub-Examples]
25342534+ "GitHub Examples of COSE", commit 3221310, 3 June 2020,
25352535+ <https://github.com/cose-wg/Examples>.
25362536+25372537+ [PVSig] Brown, D.R.L. and D.B. Johnson, "Formal Security Proofs
25382538+ for a Signature Scheme with Partial Message Recovery",
25392539+ LNCS Volume 2020, DOI 10.1007/3-540-45353-9_11, June 2000,
25402540+ <https://www.certicom.com/content/dam/certicom/images/
25412541+ pdfs/CerticomWP-PVSigSec_login.pdf>.
25422542+25432543+ [RFC2633] Ramsdell, B., Ed., "S/MIME Version 3 Message
25442544+ Specification", RFC 2633, DOI 10.17487/RFC2633, June 1999,
25452545+ <https://www.rfc-editor.org/info/rfc2633>.
25462546+25472547+ [RFC3394] Schaad, J. and R. Housley, "Advanced Encryption Standard
25482548+ (AES) Key Wrap Algorithm", RFC 3394, DOI 10.17487/RFC3394,
25492549+ September 2002, <https://www.rfc-editor.org/info/rfc3394>.
25502550+25512551+ [RFC3851] Ramsdell, B., Ed., "Secure/Multipurpose Internet Mail
25522552+ Extensions (S/MIME) Version 3.1 Message Specification",
25532553+ RFC 3851, DOI 10.17487/RFC3851, July 2004,
25542554+ <https://www.rfc-editor.org/info/rfc3851>.
25552555+25562556+ [RFC4262] Santesson, S., "X.509 Certificate Extension for Secure/
25572557+ Multipurpose Internet Mail Extensions (S/MIME)
25582558+ Capabilities", RFC 4262, DOI 10.17487/RFC4262, December
25592559+ 2005, <https://www.rfc-editor.org/info/rfc4262>.
25602560+25612561+ [RFC4949] Shirey, R., "Internet Security Glossary, Version 2",
25622562+ FYI 36, RFC 4949, DOI 10.17487/RFC4949, August 2007,
25632563+ <https://www.rfc-editor.org/info/rfc4949>.
25642564+25652565+ [RFC5116] McGrew, D., "An Interface and Algorithms for Authenticated
25662566+ Encryption", RFC 5116, DOI 10.17487/RFC5116, January 2008,
25672567+ <https://www.rfc-editor.org/info/rfc5116>.
25682568+25692569+ [RFC5652] Housley, R., "Cryptographic Message Syntax (CMS)", STD 70,
25702570+ RFC 5652, DOI 10.17487/RFC5652, September 2009,
25712571+ <https://www.rfc-editor.org/info/rfc5652>.
25722572+25732573+ [RFC5751] Ramsdell, B. and S. Turner, "Secure/Multipurpose Internet
25742574+ Mail Extensions (S/MIME) Version 3.2 Message
25752575+ Specification", RFC 5751, DOI 10.17487/RFC5751, January
25762576+ 2010, <https://www.rfc-editor.org/info/rfc5751>.
25772577+25782578+ [RFC5752] Turner, S. and J. Schaad, "Multiple Signatures in
25792579+ Cryptographic Message Syntax (CMS)", RFC 5752,
25802580+ DOI 10.17487/RFC5752, January 2010,
25812581+ <https://www.rfc-editor.org/info/rfc5752>.
25822582+25832583+ [RFC5990] Randall, J., Kaliski, B., Brainard, J., and S. Turner,
25842584+ "Use of the RSA-KEM Key Transport Algorithm in the
25852585+ Cryptographic Message Syntax (CMS)", RFC 5990,
25862586+ DOI 10.17487/RFC5990, September 2010,
25872587+ <https://www.rfc-editor.org/info/rfc5990>.
25882588+25892589+ [RFC6838] Freed, N., Klensin, J., and T. Hansen, "Media Type
25902590+ Specifications and Registration Procedures", BCP 13,
25912591+ RFC 6838, DOI 10.17487/RFC6838, January 2013,
25922592+ <https://www.rfc-editor.org/info/rfc6838>.
25932593+25942594+ [RFC7252] Shelby, Z., Hartke, K., and C. Bormann, "The Constrained
25952595+ Application Protocol (CoAP)", RFC 7252,
25962596+ DOI 10.17487/RFC7252, June 2014,
25972597+ <https://www.rfc-editor.org/info/rfc7252>.
25982598+25992599+ [RFC7515] Jones, M., Bradley, J., and N. Sakimura, "JSON Web
26002600+ Signature (JWS)", RFC 7515, DOI 10.17487/RFC7515, May
26012601+ 2015, <https://www.rfc-editor.org/info/rfc7515>.
26022602+26032603+ [RFC7516] Jones, M. and J. Hildebrand, "JSON Web Encryption (JWE)",
26042604+ RFC 7516, DOI 10.17487/RFC7516, May 2015,
26052605+ <https://www.rfc-editor.org/info/rfc7516>.
26062606+26072607+ [RFC7517] Jones, M., "JSON Web Key (JWK)", RFC 7517,
26082608+ DOI 10.17487/RFC7517, May 2015,
26092609+ <https://www.rfc-editor.org/info/rfc7517>.
26102610+26112611+ [RFC7518] Jones, M., "JSON Web Algorithms (JWA)", RFC 7518,
26122612+ DOI 10.17487/RFC7518, May 2015,
26132613+ <https://www.rfc-editor.org/info/rfc7518>.
26142614+26152615+ [RFC8032] Josefsson, S. and I. Liusvaara, "Edwards-Curve Digital
26162616+ Signature Algorithm (EdDSA)", RFC 8032,
26172617+ DOI 10.17487/RFC8032, January 2017,
26182618+ <https://www.rfc-editor.org/info/rfc8032>.
26192619+26202620+ [RFC8126] Cotton, M., Leiba, B., and T. Narten, "Guidelines for
26212621+ Writing an IANA Considerations Section in RFCs", BCP 26,
26222622+ RFC 8126, DOI 10.17487/RFC8126, June 2017,
26232623+ <https://www.rfc-editor.org/info/rfc8126>.
26242624+26252625+ [RFC8152] Schaad, J., "CBOR Object Signing and Encryption (COSE)",
26262626+ RFC 8152, DOI 10.17487/RFC8152, July 2017,
26272627+ <https://www.rfc-editor.org/info/rfc8152>.
26282628+26292629+ [RFC8230] Jones, M., "Using RSA Algorithms with CBOR Object Signing
26302630+ and Encryption (COSE) Messages", RFC 8230,
26312631+ DOI 10.17487/RFC8230, September 2017,
26322632+ <https://www.rfc-editor.org/info/rfc8230>.
26332633+26342634+ [RFC8551] Schaad, J., Ramsdell, B., and S. Turner, "Secure/
26352635+ Multipurpose Internet Mail Extensions (S/MIME) Version 4.0
26362636+ Message Specification", RFC 8551, DOI 10.17487/RFC8551,
26372637+ April 2019, <https://www.rfc-editor.org/info/rfc8551>.
26382638+26392639+ [RFC8610] Birkholz, H., Vigano, C., and C. Bormann, "Concise Data
26402640+ Definition Language (CDDL): A Notational Convention to
26412641+ Express Concise Binary Object Representation (CBOR) and
26422642+ JSON Data Structures", RFC 8610, DOI 10.17487/RFC8610,
26432643+ June 2019, <https://www.rfc-editor.org/info/rfc8610>.
26442644+26452645+ [RFC8613] Selander, G., Mattsson, J., Palombini, F., and L. Seitz,
26462646+ "Object Security for Constrained RESTful Environments
26472647+ (OSCORE)", RFC 8613, DOI 10.17487/RFC8613, July 2019,
26482648+ <https://www.rfc-editor.org/info/rfc8613>.
26492649+26502650+ [RFC9054] Schaad, J., "CBOR Object Signing and Encryption (COSE):
26512651+ Hash Algorithms", RFC 9054, DOI 10.17487/RFC9054, August
26522652+ 2022, <https://www.rfc-editor.org/info/rfc9054>.
26532653+26542654+ [RFC9106] Biryukov, A., Dinu, D., Khovratovich, D., and S.
26552655+ Josefsson, "Argon2 Memory-Hard Function for Password
26562656+ Hashing and Proof-of-Work Applications", RFC 9106,
26572657+ DOI 10.17487/RFC9106, September 2021,
26582658+ <https://www.rfc-editor.org/info/rfc9106>.
26592659+26602660+ [STD90] Bray, T., Ed., "The JavaScript Object Notation (JSON) Data
26612661+ Interchange Format", STD 90, RFC 8259, December 2017,
26622662+ <https://www.rfc-editor.org/info/std90>.
26632663+26642664+ [W3C.WebCrypto]
26652665+ Watson, M., Ed., "Web Cryptography API", W3C
26662666+ Recommendation, 26 January 2017,
26672667+ <https://www.w3.org/TR/WebCryptoAPI/>.
26682668+26692669+Appendix A. Guidelines for External Data Authentication of Algorithms
26702670+26712671+ During development of COSE, the requirement that the algorithm
26722672+ identifier be located in the protected attributes was relaxed from a
26732673+ must to a should. Two basic reasons have been advanced to support
26742674+ this position. First, the resulting message will be smaller if the
26752675+ algorithm identifier is omitted from the most common messages in a
26762676+ CoAP environment. Second, there is a potential bug that will arise
26772677+ if full checking is not done correctly between the different places
26782678+ that an algorithm identifier could be placed (the message itself, an
26792679+ application statement, the key structure that the sender possesses,
26802680+ and the key structure the recipient possesses).
26812681+26822682+ This appendix lays out how such a change can be made and the details
26832683+ that an application needs to specify in order to use this option.
26842684+ Two different sets of details are specified: those needed to omit an
26852685+ algorithm identifier and those needed to use the variant on the
26862686+ countersignature attribute that contains no attributes about itself.
26872687+26882688+ Three sets of recommendations are laid out. The first set of
26892689+ recommendations applies to having an implicit algorithm identified
26902690+ for a single layer of a COSE object. The second set of
26912691+ recommendations applies to having multiple implicit algorithms
26922692+ identified for multiple layers of a COSE object. The third set of
26932693+ recommendations applies to having implicit algorithms for multiple
26942694+ COSE object constructs.
26952695+26962696+ The key words from BCP 14 ([RFC2119] and [RFC8174]) are deliberately
26972697+ not used here. This specification can provide recommendations, but
26982698+ it cannot enforce them.
26992699+27002700+ This set of recommendations applies to the case where an application
27012701+ is distributing a fixed algorithm along with the key information for
27022702+ use in a single COSE object. This normally applies to the smallest
27032703+ of the COSE objects -- specifically, COSE_Sign1, COSE_Mac0, and
27042704+ COSE_Encrypt0 -- but could apply to the other structures as well.
27052705+27062706+ The following items should be taken into account:
27072707+27082708+ * Applications need to list the set of COSE structures that implicit
27092709+ algorithms are to be used in. Applications need to require that
27102710+ the receipt of an explicit algorithm identifier in one of these
27112711+ structures will lead to the message being rejected. This
27122712+ requirement is stated so that there will never be a case where
27132713+ there is any ambiguity about the question of which algorithm
27142714+ should be used, the implicit or the explicit one. This applies
27152715+ even if the transported algorithm identifier is a protected
27162716+ attribute. This applies even if the transported algorithm is the
27172717+ same as the implicit algorithm.
27182718+27192719+ * Applications need to define the set of information that is to be
27202720+ considered to be part of a context when omitting algorithm
27212721+ identifiers. At a minimum, this would be the key identifier (if
27222722+ needed), the key, the algorithm, and the COSE structure it is used
27232723+ with. Applications should restrict the use of a single key to a
27242724+ single algorithm. As noted for some of the algorithms in
27252725+ [RFC9053], the use of the same key in different, related
27262726+ algorithms can lead to leakage of information about the key,
27272727+ leakage about the data, or the ability to perform forgeries.
27282728+27292729+ * In many cases, applications that make the algorithm identifier
27302730+ implicit will also want to make the context identifier implicit
27312731+ for the same reason. That is, omitting the context identifier
27322732+ will decrease the message size (potentially significantly,
27332733+ depending on the length of the identifier). Applications that do
27342734+ this will need to describe the circumstances where the context
27352735+ identifier is to be omitted and how the context identifier is to
27362736+ be inferred in these cases. (An exhaustive search over all of the
27372737+ keys would normally not be considered to be acceptable.) An
27382738+ example of how this can be done is to tie the context to a
27392739+ transaction identifier. Both would be sent on the original
27402740+ message, but only the transaction identifier would need to be sent
27412741+ after that point, as the context is tied into the transaction
27422742+ identifier. Another way would be to associate a context with a
27432743+ network address. All messages coming from a single network
27442744+ address can be assumed to be associated with a specific context.
27452745+ (In this case, the address would normally be distributed as part
27462746+ of the context.)
27472747+27482748+ * Applications cannot rely on key identifiers being unique unless
27492749+ they take significant efforts to ensure that they are computed in
27502750+ such a way as to create this guarantee. Even when an application
27512751+ does this, the uniqueness might be violated if the application is
27522752+ run in different contexts (i.e., with a different context
27532753+ provider) or if the system combines the security contexts from
27542754+ different applications together into a single store.
27552755+27562756+ * Applications should continue the practice of protecting the
27572757+ algorithm identifier. Since this is not done by placing it in the
27582758+ protected attributes field, applications should define an
27592759+ application-specific external data structure that includes this
27602760+ value. This external data field can be used as such for content
27612761+ encryption, MAC, and signature algorithms. It can be used in the
27622762+ SuppPrivInfo field for those algorithms that use a KDF to derive a
27632763+ key value. Applications may also want to protect other
27642764+ information that is part of the context structure as well. It
27652765+ should be noted that those fields, such as the key or a Base IV,
27662766+ that are protected by virtue of being used in the cryptographic
27672767+ computation do not need to be included in the external data field.
27682768+27692769+ The second case is having multiple implicit algorithm identifiers
27702770+ specified for a multiple-layer COSE object. An example of how this
27712771+ would work is the encryption context that an application specifies,
27722772+ which contains a content encryption algorithm, a key wrap algorithm,
27732773+ a key identifier, and a shared secret. The sender omits sending the
27742774+ algorithm identifier for both the content layer and the recipient
27752775+ layer, leaving only the key identifier. The receiver then uses the
27762776+ key identifier to get the implicit algorithm identifiers.
27772777+27782778+ The following additional items need to be taken into consideration:
27792779+27802780+ * Applications that want to support this will need to define a
27812781+ structure that allows for, and clearly identifies, both the COSE
27822782+ structure to be used with a given key and the structure and
27832783+ algorithm to be used for the secondary layer. The key for the
27842784+ secondary layer is computed as normal from the recipient layer.
27852785+27862786+ The third case is having multiple implicit algorithm identifiers, but
27872787+ targeted at potentially unrelated layers or different COSE objects.
27882788+ There are a number of different scenarios where this might be
27892789+ applicable. Some of these scenarios are:
27902790+27912791+ * Two contexts are distributed as a pair. Each of the contexts is
27922792+ for use with a COSE_Encrypt message. Each context will consist of
27932793+ distinct secret keys and IVs and potentially even different
27942794+ algorithms. One context is for sending messages from party A to
27952795+ party B, and the second context is for sending messages from party
27962796+ B to party A. This means that there is no chance for a reflection
27972797+ attack to occur, as each party uses different secret keys to send
27982798+ its messages; a message that is reflected back to it would fail to
27992799+ decrypt.
28002800+28012801+ * Two contexts are distributed as a pair. The first context is used
28022802+ for encryption of the message, and the second context is used to
28032803+ place a countersignature on the message. The intention is that
28042804+ the second context can be distributed to other entities
28052805+ independently of the first context. This allows these entities to
28062806+ validate that the message came from an individual without being
28072807+ able to decrypt the message and see the content.
28082808+28092809+ * Two contexts are distributed as a pair. The first context
28102810+ contains a key for dealing with MACed messages, and the second
28112811+ context contains a different key for dealing with encrypted
28122812+ messages. This allows for a unified distribution of keys to
28132813+ participants for different types of messages that have different
28142814+ keys, but where the keys may be used in a coordinated manner.
28152815+28162816+ For these cases, the following additional items need to be
28172817+ considered:
28182818+28192819+ * Applications need to ensure that the multiple contexts stay
28202820+ associated. If one of the contexts is invalidated for any reason,
28212821+ all of the contexts associated with it should also be invalidated.
28222822+28232823+Appendix B. Two Layers of Recipient Information
28242824+28252825+ All of the currently defined recipient algorithm classes only use two
28262826+ layers of the COSE structure. The first layer (COSE_Encrypt) is the
28272827+ message content, and the second layer (COSE_Recipient) is the content
28282828+ key encryption. However, if one uses a recipient algorithm such as
28292829+ the RSA Key Encapsulation Mechanism (RSA-KEM) (see Appendix A of RSA-
28302830+ KEM [RFC5990]), then it makes sense to have two layers of the
28312831+ COSE_Recipient structure.
28322832+28332833+ These layers would be:
28342834+28352835+ * Layer 0: The content encryption layer. This layer contains the
28362836+ payload of the message.
28372837+28382838+ * Layer 1: The encryption of the CEK by a KEK.
28392839+28402840+ * Layer 2: The encryption of a long random secret using an RSA key
28412841+ and a key derivation function to convert that secret into the KEK.
28422842+28432843+ This is an example of what a triple-layer message would look like.
28442844+ To make it easier to read, it is presented using the extended CBOR
28452845+ diagnostic notation (defined in [RFC8610]) rather than as a binary
28462846+ dump. The message has the following layers:
28472847+28482848+ * Layer 0: Has content encrypted with AES-GCM using a 128-bit key.
28492849+28502850+ * Layer 1: Uses the AES Key Wrap algorithm with a 128-bit key.
28512851+28522852+ * Layer 2: Uses ECDH Ephemeral-Static direct to generate the Layer 1
28532853+ key.
28542854+28552855+ In effect, this example is a decomposed version of using the ECDH-
28562856+ ES+A128KW algorithm.
28572857+28582858+ Size of binary file is 183 bytes
28592859+28602860+ 96(
28612861+ [ / COSE_Encrypt /
28622862+ / protected h'a10101' / << {
28632863+ / alg / 1:1 / AES-GCM 128 /
28642864+ } >>,
28652865+ / unprotected / {
28662866+ / iv / 5:h'02d1f7e6f26c43d4868d87ce'
28672867+ },
28682868+ / ciphertext / h'64f84d913ba60a76070a9a48f26e97e863e2852948658f0
28692869+ 811139868826e89218a75715b',
28702870+ / recipients / [
28712871+ [ / COSE_Recipient /
28722872+ / protected / h'',
28732873+ / unprotected / {
28742874+ / alg / 1:-3 / A128KW /
28752875+ },
28762876+ / ciphertext / h'dbd43c4e9d719c27c6275c67d628d493f090593db82
28772877+ 18f11',
28782878+ / recipients / [
28792879+ [ / COSE_Recipient /
28802880+ / protected h'a1013818' / << {
28812881+ / alg / 1:-25 / ECDH-ES + HKDF-256 /
28822882+ } >> ,
28832883+ / unprotected / {
28842884+ / ephemeral / -1:{
28852885+ / kty / 1:2,
28862886+ / crv / -1:1,
28872887+ / x / -2:h'b2add44368ea6d641f9ca9af308b4079aeb519f11
28882888+ e9b8a55a600b21233e86e68',
28892889+ / y / -3:false
28902890+ },
28912891+ / kid / 4:'meriadoc.brandybuck@buckland.example'
28922892+ },
28932893+ / ciphertext / h''
28942894+ ]
28952895+ ]
28962896+ ]
28972897+ ]
28982898+ ]
28992899+ )
29002900+29012901+Appendix C. Examples
29022902+29032903+ This appendix includes a set of examples that show the different
29042904+ features and message types that have been defined in this document.
29052905+ To make the examples easier to read, they are presented using the
29062906+ extended CBOR diagnostic notation (defined in [RFC8610]) rather than
29072907+ as a binary dump.
29082908+29092909+ A GitHub project has been created at [GitHub-Examples] that contains
29102910+ not only the examples presented in this document, but a more complete
29112911+ set of testing examples as well. Each example is found in a JSON
29122912+ file that contains the inputs used to create the example, some of the
29132913+ intermediate values that can be used in debugging the example, and
29142914+ the output of the example presented both as a hex dump and in CBOR
29152915+ diagnostic notation format. Some of the examples at the site are
29162916+ designed to be failure-testing cases; these are clearly marked as
29172917+ such in the JSON file. If errors in the examples in this document
29182918+ are found, the examples on GitHub will be updated, and a note to that
29192919+ effect will be placed in the JSON file.
29202920+29212921+ As noted, the examples are presented using CBOR's diagnostic
29222922+ notation. A Ruby-based tool exists that can convert between the
29232923+ diagnostic notation and binary. This tool can be installed with the
29242924+ command line:
29252925+29262926+ gem install cbor-diag
29272927+29282928+ The diagnostic notation can be converted into binary files using the
29292929+ following command line:
29302930+29312931+ diag2cbor.rb < inputfile > outputfile
29322932+29332933+ The examples can be extracted from the XML version of this document
29342934+ via an XPath expression, as all of the source code is tagged with the
29352935+ attribute type='cbor-diag'. (Depending on the XPath evaluator one is
29362936+ using, it may be necessary to deal with > as an entity.)
29372937+29382938+ //sourcecode[@type='cbor-diag']/text()
29392939+29402940+C.1. Examples of Signed Messages
29412941+29422942+C.1.1. Single Signature
29432943+29442944+ This example uses the following:
29452945+29462946+ * Signature Algorithm: ECDSA w/ SHA-256, Curve P-256
29472947+29482948+ Size of binary file is 103 bytes
29492949+29502950+ 98(
29512951+ [
29522952+ / protected / h'',
29532953+ / unprotected / {},
29542954+ / payload / 'This is the content.',
29552955+ / signatures / [
29562956+ [
29572957+ / protected h'a10126' / << {
29582958+ / alg / 1:-7 / ECDSA 256 /
29592959+ } >>,
29602960+ / unprotected / {
29612961+ / kid / 4:'11'
29622962+ },
29632963+ / signature / h'e2aeafd40d69d19dfe6e52077c5d7ff4e408282cbefb
29642964+ 5d06cbf414af2e19d982ac45ac98b8544c908b4507de1e90b717c3d34816fe926a2b
29652965+ 98f53afd2fa0f30a'
29662966+ ]
29672967+ ]
29682968+ ]
29692969+ )
29702970+29712971+C.1.2. Multiple Signers
29722972+29732973+ This example uses the following:
29742974+29752975+ * Signature Algorithm: ECDSA w/ SHA-256, Curve P-256
29762976+29772977+ * Signature Algorithm: ECDSA w/ SHA-512, Curve P-521
29782978+29792979+ Size of binary file is 277 bytes
29802980+29812981+ 98(
29822982+ [
29832983+ / protected / h'',
29842984+ / unprotected / {},
29852985+ / payload / 'This is the content.',
29862986+ / signatures / [
29872987+ [
29882988+ / protected h'a10126' / << {
29892989+ / alg / 1:-7 / ECDSA 256 /
29902990+ } >>,
29912991+ / unprotected / {
29922992+ / kid / 4:'11'
29932993+ },
29942994+ / signature / h'e2aeafd40d69d19dfe6e52077c5d7ff4e408282cbefb
29952995+ 5d06cbf414af2e19d982ac45ac98b8544c908b4507de1e90b717c3d34816fe926a2b
29962996+ 98f53afd2fa0f30a'
29972997+ ],
29982998+ [
29992999+ / protected h'a1013823' / << {
30003000+ / alg / 1:-36 / ECDSA 521 /
30013001+ } >> ,
30023002+ / unprotected / {
30033003+ / kid / 4:'bilbo.baggins@hobbiton.example'
30043004+ },
30053005+ / signature / h'00a2d28a7c2bdb1587877420f65adf7d0b9a06635dd1
30063006+ de64bb62974c863f0b160dd2163734034e6ac003b01e8705524c5c4ca479a952f024
30073007+ 7ee8cb0b4fb7397ba08d009e0c8bf482270cc5771aa143966e5a469a09f613488030
30083008+ c5b07ec6d722e3835adb5b2d8c44e95ffb13877dd2582866883535de3bb03d01753f
30093009+ 83ab87bb4f7a0297'
30103010+ ]
30113011+ ]
30123012+ ]
30133013+ )
30143014+30153015+C.1.3. Signature with Criticality
30163016+30173017+ This example uses the following:
30183018+30193019+ * Signature Algorithm: ECDSA w/ SHA-256, Curve P-256
30203020+30213021+ * There is a criticality marker on the "reserved" header parameter.
30223022+30233023+ Size of binary file is 125 bytes
30243024+30253025+ 98(
30263026+ [
30273027+ / protected h'a2687265736572766564f40281687265736572766564' /
30283028+ << {
30293029+ "reserved":false,
30303030+ / crit / 2:[
30313031+ "reserved"
30323032+ ]
30333033+ } >>,
30343034+ / unprotected / {},
30353035+ / payload / 'This is the content.',
30363036+ / signatures / [
30373037+ [
30383038+ / protected h'a10126' / << {
30393039+ / alg / 1:-7 / ECDSA 256 /
30403040+ } >>,
30413041+ / unprotected / {
30423042+ / kid / 4:'11'
30433043+ },
30443044+ / signature / h'3fc54702aa56e1b2cb20284294c9106a63f91bac658d
30453045+ 69351210a031d8fc7c5ff3e4be39445b1a3e83e1510d1aca2f2e8a7c081c7645042b
30463046+ 18aba9d1fad1bd9c'
30473047+ ]
30483048+ ]
30493049+ ]
30503050+ )
30513051+30523052+C.2. Single Signer Examples
30533053+30543054+C.2.1. Single ECDSA Signature
30553055+30563056+ This example uses the following:
30573057+30583058+ * Signature Algorithm: ECDSA w/ SHA-256, Curve P-256
30593059+30603060+ Size of binary file is 98 bytes
30613061+30623062+ 18(
30633063+ [
30643064+ / protected h'a10126' / << {
30653065+ / alg / 1:-7 / ECDSA 256 /
30663066+ } >>,
30673067+ / unprotected / {
30683068+ / kid / 4:'11'
30693069+ },
30703070+ / payload / 'This is the content.',
30713071+ / signature / h'8eb33e4ca31d1c465ab05aac34cc6b23d58fef5c083106c4
30723072+ d25a91aef0b0117e2af9a291aa32e14ab834dc56ed2a223444547e01f11d3b0916e5
30733073+ a4c345cacb36'
30743074+ ]
30753075+ )
30763076+30773077+C.3. Examples of Enveloped Messages
30783078+30793079+C.3.1. Direct ECDH
30803080+30813081+ This example uses the following:
30823082+30833083+ * CEK: AES-GCM w/ 128-bit key
30843084+30853085+ * Recipient class: ECDH Ephemeral-Static, Curve P-256
30863086+30873087+ Size of binary file is 151 bytes
30883088+30893089+ 96(
30903090+ [
30913091+ / protected h'a10101' / << {
30923092+ / alg / 1:1 / AES-GCM 128 /
30933093+ } >>,
30943094+ / unprotected / {
30953095+ / iv / 5:h'c9cf4df2fe6c632bf7886413'
30963096+ },
30973097+ / ciphertext / h'7adbe2709ca818fb415f1e5df66f4e1a51053ba6d65a1a0
30983098+ c52a357da7a644b8070a151b0',
30993099+ / recipients / [
31003100+ [
31013101+ / protected h'a1013818' / << {
31023102+ / alg / 1:-25 / ECDH-ES + HKDF-256 /
31033103+ } >>,
31043104+ / unprotected / {
31053105+ / ephemeral / -1:{
31063106+ / kty / 1:2,
31073107+ / crv / -1:1,
31083108+ / x / -2:h'98f50a4ff6c05861c8860d13a638ea56c3f5ad7590bbf
31093109+ bf054e1c7b4d91d6280',
31103110+ / y / -3:true
31113111+ },
31123112+ / kid / 4:'meriadoc.brandybuck@buckland.example'
31133113+ },
31143114+ / ciphertext / h''
31153115+ ]
31163116+ ]
31173117+ ]
31183118+ )
31193119+31203120+C.3.2. Direct Plus Key Derivation
31213121+31223122+ This example uses the following:
31233123+31243124+ * CEK: AES-CCM w/ 128-bit key, truncate the tag to 64 bits
31253125+31263126+ * Recipient class: Use HKDF on a shared secret with the following
31273127+ implicit fields as part of the context.
31283128+31293129+ - salt: "aabbccddeeffgghh"
31303130+31313131+ - PartyU identity: "lighting-client"
31323132+31333133+ - PartyV identity: "lighting-server"
31343134+31353135+ - Supplementary Public Other: "Encryption Example 02"
31363136+31373137+ Size of binary file is 91 bytes
31383138+31393139+ 96(
31403140+ [
31413141+ / protected h'a1010a' / << {
31423142+ / alg / 1:10 / AES-CCM-16-64-128 /
31433143+ } >>,
31443144+ / unprotected / {
31453145+ / iv / 5:h'89f52f65a1c580933b5261a76c'
31463146+ },
31473147+ / ciphertext / h'753548a19b1307084ca7b2056924ed95f2e3b17006dfe93
31483148+ 1b687b847',
31493149+ / recipients / [
31503150+ [
31513151+ / protected h'a10129' / << {
31523152+ / alg / 1:-10
31533153+ } >>,
31543154+ / unprotected / {
31553155+ / salt / -20:'aabbccddeeffgghh',
31563156+ / kid / 4:'our-secret'
31573157+ },
31583158+ / ciphertext / h''
31593159+ ]
31603160+ ]
31613161+ ]
31623162+ )
31633163+31643164+C.3.3. Encrypted Content with External Data
31653165+31663166+ This example uses the following:
31673167+31683168+ * CEK: AES-GCM w/ 128-bit key
31693169+31703170+ * Recipient class: ECDH Static-Static, Curve P-256 with AES Key Wrap
31713171+31723172+ * Externally Supplied AAD: h'0011bbcc22dd44ee55ff660077'
31733173+31743174+ Size of binary file is 173 bytes
31753175+31763176+ 96(
31773177+ [
31783178+ / protected h'a10101' / << {
31793179+ / alg / 1:1 / AES-GCM 128 /
31803180+ } >> ,
31813181+ / unprotected / {
31823182+ / iv / 5:h'02d1f7e6f26c43d4868d87ce'
31833183+ },
31843184+ / ciphertext / h'64f84d913ba60a76070a9a48f26e97e863e28529d8f5335
31853185+ e5f0165eee976b4a5f6c6f09d',
31863186+ / recipients / [
31873187+ [
31883188+ / protected / h'a101381f' / {
31893189+ \ alg \ 1:-32 \ ECDH-SS+A128KW \
31903190+ } / ,
31913191+ / unprotected / {
31923192+ / static kid / -3:'peregrin.took@tuckborough.example',
31933193+ / kid / 4:'meriadoc.brandybuck@buckland.example',
31943194+ / U nonce / -22:h'0101'
31953195+ },
31963196+ / ciphertext / h'41e0d76f579dbd0d936a662d54d8582037de2e366fd
31973197+ e1c62'
31983198+ ]
31993199+ ]
32003200+ ]
32013201+ )
32023202+32033203+C.4. Examples of Encrypted Messages
32043204+32053205+C.4.1. Simple Encrypted Message
32063206+32073207+ This example uses the following:
32083208+32093209+ * CEK: AES-CCM w/ 128-bit key and a 64-bit tag
32103210+32113211+ Size of binary file is 52 bytes
32123212+32133213+ 16(
32143214+ [
32153215+ / protected h'a1010a' / << {
32163216+ / alg / 1:10 / AES-CCM-16-64-128 /
32173217+ } >> ,
32183218+ / unprotected / {
32193219+ / iv / 5:h'89f52f65a1c580933b5261a78c'
32203220+ },
32213221+ / ciphertext / h'5974e1b99a3a4cc09a659aa2e9e7fff161d38ce71cb45ce
32223222+ 460ffb569'
32233223+ ]
32243224+ )
32253225+32263226+C.4.2. Encrypted Message with a Partial IV
32273227+32283228+ This example uses the following:
32293229+32303230+ * CEK: AES-CCM w/ 128-bit key and a 64-bit tag
32313231+32323232+ * Prefix for IV is 89F52F65A1C580933B52
32333233+32343234+ Size of binary file is 41 bytes
32353235+32363236+ 16(
32373237+ [
32383238+ / protected h'a1010a' / << {
32393239+ / alg / 1:10 / AES-CCM-16-64-128 /
32403240+ } >> ,
32413241+ / unprotected / {
32423242+ / partial iv / 6:h'61a7'
32433243+ },
32443244+ / ciphertext / h'252a8911d465c125b6764739700f0141ed09192de139e05
32453245+ 3bd09abca'
32463246+ ]
32473247+ )
32483248+32493249+C.5. Examples of MACed Messages
32503250+32513251+C.5.1. Shared Secret Direct MAC
32523252+32533253+ This example uses the following:
32543254+32553255+ * MAC: AES-CMAC, 256-bit key, truncated to 64 bits
32563256+32573257+ * Recipient class: direct shared secret
32583258+32593259+ Size of binary file is 57 bytes
32603260+32613261+ 97(
32623262+ [
32633263+ / protected h'a1010f' / << {
32643264+ / alg / 1:15 / AES-CBC-MAC-256//64 /
32653265+ } >> ,
32663266+ / unprotected / {},
32673267+ / payload / 'This is the content.',
32683268+ / tag / h'9e1226ba1f81b848',
32693269+ / recipients / [
32703270+ [
32713271+ / protected / h'',
32723272+ / unprotected / {
32733273+ / alg / 1:-6 / direct /,
32743274+ / kid / 4:'our-secret'
32753275+ },
32763276+ / ciphertext / h''
32773277+ ]
32783278+ ]
32793279+ ]
32803280+ )
32813281+32823282+C.5.2. ECDH Direct MAC
32833283+32843284+ This example uses the following:
32853285+32863286+ * MAC: HMAC w/SHA-256, 256-bit key
32873287+32883288+ * Recipient class: ECDH key agreement, two static keys, HKDF w/
32893289+ context structure
32903290+32913291+ Size of binary file is 214 bytes
32923292+32933293+ 97(
32943294+ [
32953295+ / protected h'a10105' / << {
32963296+ / alg / 1:5 / HMAC 256//256 /
32973297+ } >> ,
32983298+ / unprotected / {},
32993299+ / payload / 'This is the content.',
33003300+ / tag / h'81a03448acd3d305376eaa11fb3fe416a955be2cbe7ec96f012c99
33013301+ 4bc3f16a41',
33023302+ / recipients / [
33033303+ [
33043304+ / protected h'a101381a' / << {
33053305+ / alg / 1:-27 / ECDH-SS + HKDF-256 /
33063306+ } >> ,
33073307+ / unprotected / {
33083308+ / static kid / -3:'peregrin.took@tuckborough.example',
33093309+ / kid / 4:'meriadoc.brandybuck@buckland.example',
33103310+ / U nonce / -22:h'4d8553e7e74f3c6a3a9dd3ef286a8195cbf8a23d
33113311+ 19558ccfec7d34b824f42d92bd06bd2c7f0271f0214e141fb779ae2856abf585a583
33123312+ 68b017e7f2a9e5ce4db5'
33133313+ },
33143314+ / ciphertext / h''
33153315+ ]
33163316+ ]
33173317+ ]
33183318+ )
33193319+33203320+C.5.3. Wrapped MAC
33213321+33223322+ This example uses the following:
33233323+33243324+ * MAC: AES-MAC, 128-bit key, truncated to 64 bits
33253325+33263326+ * Recipient class: AES Key Wrap w/ a preshared 256-bit key
33273327+33283328+ Size of binary file is 109 bytes
33293329+33303330+ 97(
33313331+ [
33323332+ / protected h'a1010e' / << {
33333333+ / alg / 1:14 / AES-CBC-MAC-128//64 /
33343334+ } >> ,
33353335+ / unprotected / {},
33363336+ / payload / 'This is the content.',
33373337+ / tag / h'36f5afaf0bab5d43',
33383338+ / recipients / [
33393339+ [
33403340+ / protected / h'',
33413341+ / unprotected / {
33423342+ / alg / 1:-5 / A256KW /,
33433343+ / kid / 4:'018c0ae5-4d9b-471b-bfd6-eef314bc7037'
33443344+ },
33453345+ / ciphertext / h'711ab0dc2fc4585dce27effa6781c8093eba906f227
33463346+ b6eb0'
33473347+ ]
33483348+ ]
33493349+ ]
33503350+ )
33513351+33523352+C.5.4. Multi-Recipient MACed Message
33533353+33543354+ This example uses the following:
33553355+33563356+ * MAC: HMAC w/ SHA-256, 128-bit key
33573357+33583358+ * Recipient class: Uses two different methods.
33593359+33603360+ 1. ECDH Ephemeral-Static, Curve P-521, AES Key Wrap w/ 128-bit
33613361+ key
33623362+33633363+ 2. AES Key Wrap w/ 256-bit key
33643364+33653365+ Size of binary file is 309 bytes
33663366+33673367+ 97(
33683368+ [
33693369+ / protected h'a10105' / << {
33703370+ / alg / 1:5 / HMAC 256//256 /
33713371+ } >> ,
33723372+ / unprotected / {},
33733373+ / payload / 'This is the content.',
33743374+ / tag / h'bf48235e809b5c42e995f2b7d5fa13620e7ed834e337f6aa43df16
33753375+ 1e49e9323e',
33763376+ / recipients / [
33773377+ [
33783378+ / protected h'a101381c' / << {
33793379+ / alg / 1:-29 / ECDH-ES+A128KW /
33803380+ } >> ,
33813381+ / unprotected / {
33823382+ / ephemeral / -1:{
33833383+ / kty / 1:2,
33843384+ / crv / -1:3,
33853385+ / x / -2:h'0043b12669acac3fd27898ffba0bcd2e6c366d53bc4db
33863386+ 71f909a759304acfb5e18cdc7ba0b13ff8c7636271a6924b1ac63c02688075b55ef2
33873387+ d613574e7dc242f79c3',
33883388+ / y / -3:true
33893389+ },
33903390+ / kid / 4:'bilbo.baggins@hobbiton.example'
33913391+ },
33923392+ / ciphertext / h'339bc4f79984cdc6b3e6ce5f315a4c7d2b0ac466fce
33933393+ a69e8c07dfbca5bb1f661bc5f8e0df9e3eff5'
33943394+ ],
33953395+ [
33963396+ / protected / h'',
33973397+ / unprotected / {
33983398+ / alg / 1:-5 / A256KW /,
33993399+ / kid / 4:'018c0ae5-4d9b-471b-bfd6-eef314bc7037'
34003400+ },
34013401+ / ciphertext / h'0b2c7cfce04e98276342d6476a7723c090dfdd15f9a
34023402+ 518e7736549e998370695e6d6a83b4ae507bb'
34033403+ ]
34043404+ ]
34053405+ ]
34063406+ )
34073407+34083408+C.6. Examples of MAC0 Messages
34093409+34103410+C.6.1. Shared-Secret Direct MAC
34113411+34123412+ This example uses the following:
34133413+34143414+ * MAC: AES-CMAC, 256-bit key, truncated to 64 bits
34153415+34163416+ * Recipient class: direct shared secret
34173417+34183418+ Size of binary file is 37 bytes
34193419+34203420+ 17(
34213421+ [
34223422+ / protected h'a1010f' / << {
34233423+ / alg / 1:15 / AES-CBC-MAC-256//64 /
34243424+ } >> ,
34253425+ / unprotected / {},
34263426+ / payload / 'This is the content.',
34273427+ / tag / h'726043745027214f'
34283428+ ]
34293429+ )
34303430+34313431+ Note that this example uses the same inputs as Appendix C.5.1.
34323432+34333433+C.7. COSE Keys
34343434+34353435+C.7.1. Public Keys
34363436+34373437+ This is an example of a COSE Key Set. This example includes the
34383438+ public keys for all of the previous examples.
34393439+34403440+ In order, the keys are:
34413441+34423442+ * An EC key with a kid of "meriadoc.brandybuck@buckland.example"
34433443+34443444+ * An EC key with a kid of "11"
34453445+34463446+ * An EC key with a kid of "bilbo.baggins@hobbiton.example"
34473447+34483448+ * An EC key with a kid of "peregrin.took@tuckborough.example"
34493449+34503450+ Size of binary file is 481 bytes
34513451+34523452+ [
34533453+ {
34543454+ -1:1,
34553455+ -2:h'65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c0
34563456+ 8551d',
34573457+ -3:h'1e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd008
34583458+ 4d19c',
34593459+ 1:2,
34603460+ 2:'meriadoc.brandybuck@buckland.example'
34613461+ },
34623462+ {
34633463+ -1:1,
34643464+ -2:h'bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a
34653465+ 09eff',
34663466+ -3:h'20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbf
34673467+ c117e',
34683468+ 1:2,
34693469+ 2:'11'
34703470+ },
34713471+ {
34723472+ -1:3,
34733473+ -2:h'0072992cb3ac08ecf3e5c63dedec0d51a8c1f79ef2f82f94f3c737bf5de
34743474+ 7986671eac625fe8257bbd0394644caaa3aaf8f27a4585fbbcad0f2457620085e5c8
34753475+ f42ad',
34763476+ -3:h'01dca6947bce88bc5790485ac97427342bc35f887d86d65a089377e247e
34773477+ 60baa55e4e8501e2ada5724ac51d6909008033ebc10ac999b9d7f5cc2519f3fe1ea1
34783478+ d9475',
34793479+ 1:2,
34803480+ 2:'bilbo.baggins@hobbiton.example'
34813481+ },
34823482+ {
34833483+ -1:1,
34843484+ -2:h'98f50a4ff6c05861c8860d13a638ea56c3f5ad7590bbfbf054e1c7b4d91
34853485+ d6280',
34863486+ -3:h'f01400b089867804b8e9fc96c3932161f1934f4223069170d924b7e03bf
34873487+ 822bb',
34883488+ 1:2,
34893489+ 2:'peregrin.took@tuckborough.example'
34903490+ }
34913491+ ]
34923492+34933493+C.7.2. Private Keys
34943494+34953495+ This is an example of a COSE Key Set. This example includes the
34963496+ private keys for all of the previous examples.
34973497+34983498+ In order the keys are:
34993499+35003500+ * An EC key with a kid of "meriadoc.brandybuck@buckland.example"
35013501+35023502+ * An EC key with a kid of "11"
35033503+35043504+ * An EC key with a kid of "bilbo.baggins@hobbiton.example"
35053505+35063506+ * A shared-secret key with a kid of "our-secret"
35073507+35083508+ * An EC key with a kid of "peregrin.took@tuckborough.example"
35093509+35103510+ * A shared-secret key with kid "our-secret2"
35113511+35123512+ * A shared-secret key with a kid of "018c0ae5-4d9b-471b-
35133513+ bfd6-eef314bc7037"
35143514+35153515+ Size of binary file is 816 bytes
35163516+35173517+ [
35183518+ {
35193519+ 1:2,
35203520+ 2:'meriadoc.brandybuck@buckland.example',
35213521+ -1:1,
35223522+ -2:h'65eda5a12577c2bae829437fe338701a10aaa375e1bb5b5de108de439c0
35233523+ 8551d',
35243524+ -3:h'1e52ed75701163f7f9e40ddf9f341b3dc9ba860af7e0ca7ca7e9eecd008
35253525+ 4d19c',
35263526+ -4:h'aff907c99f9ad3aae6c4cdf21122bce2bd68b5283e6907154ad911840fa
35273527+ 208cf'
35283528+ },
35293529+ {
35303530+ 1:2,
35313531+ 2:'11',
35323532+ -1:1,
35333533+ -2:h'bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a
35343534+ 09eff',
35353535+ -3:h'20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbf
35363536+ c117e',
35373537+ -4:h'57c92077664146e876760c9520d054aa93c3afb04e306705db609030850
35383538+ 7b4d3'
35393539+ },
35403540+ {
35413541+ 1:2,
35423542+ 2:'bilbo.baggins@hobbiton.example',
35433543+ -1:3,
35443544+ -2:h'0072992cb3ac08ecf3e5c63dedec0d51a8c1f79ef2f82f94f3c737bf5de
35453545+ 7986671eac625fe8257bbd0394644caaa3aaf8f27a4585fbbcad0f2457620085e5c8
35463546+ f42ad',
35473547+ -3:h'01dca6947bce88bc5790485ac97427342bc35f887d86d65a089377e247e
35483548+ 60baa55e4e8501e2ada5724ac51d6909008033ebc10ac999b9d7f5cc2519f3fe1ea1
35493549+ d9475',
35503550+ -4:h'00085138ddabf5ca975f5860f91a08e91d6d5f9a76ad4018766a476680b
35513551+ 55cd339e8ab6c72b5facdb2a2a50ac25bd086647dd3e2e6e99e84ca2c3609fdf177f
35523552+ eb26d'
35533553+ },
35543554+ {
35553555+ 1:4,
35563556+ 2:'our-secret',
35573557+ -1:h'849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c4
35583558+ 27188'
35593559+ },
35603560+ {
35613561+ 1:2,
35623562+ -1:1,
35633563+ 2:'peregrin.took@tuckborough.example',
35643564+ -2:h'98f50a4ff6c05861c8860d13a638ea56c3f5ad7590bbfbf054e1c7b4d91
35653565+ d6280',
35663566+ -3:h'f01400b089867804b8e9fc96c3932161f1934f4223069170d924b7e03bf
35673567+ 822bb',
35683568+ -4:h'02d1f7e6f26c43d4868d87ceb2353161740aacf1f7163647984b522a848
35693569+ df1c3'
35703570+ },
35713571+ {
35723572+ 1:4,
35733573+ 2:'our-secret2',
35743574+ -1:h'849b5786457c1491be3a76dcea6c4271'
35753575+ },
35763576+ {
35773577+ 1:4,
35783578+ 2:'018c0ae5-4d9b-471b-bfd6-eef314bc7037',
35793579+ -1:h'849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c4
35803580+ 27188'
35813581+ }
35823582+ ]
35833583+35843584+Acknowledgments
35853585+35863586+ This document is a product of the COSE Working Group of the IETF.
35873587+35883588+ The following individuals are to blame for getting me started on this
35893589+ project in the first place: Richard Barnes, Matt Miller, and Martin
35903590+ Thomson.
35913591+35923592+ The initial draft version of the specification was based to some
35933593+ degree on the outputs of the JOSE and S/MIME Working Groups.
35943594+35953595+ The following individuals provided input into the final form of the
35963596+ document: Carsten Bormann, John Bradley, Brian Campbell, Michael
35973597+ B. Jones, Ilari Liusvaara, Francesca Palombini, Ludwig Seitz, and
35983598+ Göran Selander.
35993599+36003600+Author's Address
36013601+36023602+ Jim Schaad
36033603+ August Cellars
+2443
spec/rfc9053.txt
···11+
22+33+44+55+Internet Engineering Task Force (IETF) J. Schaad
66+Request for Comments: 9053 August Cellars
77+Obsoletes: 8152 August 2022
88+Category: Informational
99+ISSN: 2070-1721
1010+1111+1212+ CBOR Object Signing and Encryption (COSE): Initial Algorithms
1313+1414+Abstract
1515+1616+ Concise Binary Object Representation (CBOR) is a data format designed
1717+ for small code size and small message size. There is a need to be
1818+ able to define basic security services for this data format. This
1919+ document defines a set of algorithms that can be used with the CBOR
2020+ Object Signing and Encryption (COSE) protocol (RFC 9052).
2121+2222+ This document, along with RFC 9052, obsoletes RFC 8152.
2323+2424+Status of This Memo
2525+2626+ This document is not an Internet Standards Track specification; it is
2727+ published for informational purposes.
2828+2929+ This document is a product of the Internet Engineering Task Force
3030+ (IETF). It represents the consensus of the IETF community. It has
3131+ received public review and has been approved for publication by the
3232+ Internet Engineering Steering Group (IESG). Not all documents
3333+ approved by the IESG are candidates for any level of Internet
3434+ Standard; see Section 2 of RFC 7841.
3535+3636+ Information about the current status of this document, any errata,
3737+ and how to provide feedback on it may be obtained at
3838+ https://www.rfc-editor.org/info/rfc9053.
3939+4040+Copyright Notice
4141+4242+ Copyright (c) 2022 IETF Trust and the persons identified as the
4343+ document authors. All rights reserved.
4444+4545+ This document is subject to BCP 78 and the IETF Trust's Legal
4646+ Provisions Relating to IETF Documents
4747+ (https://trustee.ietf.org/license-info) in effect on the date of
4848+ publication of this document. Please review these documents
4949+ carefully, as they describe your rights and restrictions with respect
5050+ to this document. Code Components extracted from this document must
5151+ include Revised BSD License text as described in Section 4.e of the
5252+ Trust Legal Provisions and are provided without warranty as described
5353+ in the Revised BSD License.
5454+5555+Table of Contents
5656+5757+ 1. Introduction
5858+ 1.1. Requirements Terminology
5959+ 1.2. Changes from RFC 8152
6060+ 1.3. Document Terminology
6161+ 1.4. CDDL Grammar for CBOR Data Structures
6262+ 1.5. Examples
6363+ 2. Signature Algorithms
6464+ 2.1. ECDSA
6565+ 2.1.1. Security Considerations for ECDSA
6666+ 2.2. Edwards-Curve Digital Signature Algorithm (EdDSA)
6767+ 2.2.1. Security Considerations for EdDSA
6868+ 3. Message Authentication Code (MAC) Algorithms
6969+ 3.1. Hash-Based Message Authentication Codes (HMACs)
7070+ 3.1.1. Security Considerations for HMAC
7171+ 3.2. AES Message Authentication Code (AES-CBC-MAC)
7272+ 3.2.1. Security Considerations for AES-CBC-MAC
7373+ 4. Content Encryption Algorithms
7474+ 4.1. AES-GCM
7575+ 4.1.1. Security Considerations for AES-GCM
7676+ 4.2. AES-CCM
7777+ 4.2.1. Security Considerations for AES-CCM
7878+ 4.3. ChaCha20 and Poly1305
7979+ 4.3.1. Security Considerations for ChaCha20/Poly1305
8080+ 5. Key Derivation Functions (KDFs)
8181+ 5.1. HMAC-Based Extract-and-Expand Key Derivation Function
8282+ (HKDF)
8383+ 5.2. Context Information Structure
8484+ 6. Content Key Distribution Methods
8585+ 6.1. Direct Encryption
8686+ 6.1.1. Direct Key
8787+ 6.1.2. Direct Key with KDF
8888+ 6.2. Key Wrap
8989+ 6.2.1. AES Key Wrap
9090+ 6.3. Direct Key Agreement
9191+ 6.3.1. Direct ECDH
9292+ 6.4. Key Agreement with Key Wrap
9393+ 6.4.1. ECDH with Key Wrap
9494+ 7. Key Object Parameters
9595+ 7.1. Elliptic Curve Keys
9696+ 7.1.1. Double Coordinate Curves
9797+ 7.2. Octet Key Pair
9898+ 7.3. Symmetric Keys
9999+ 8. COSE Capabilities
100100+ 8.1. Assignments for Existing Algorithms
101101+ 8.2. Assignments for Existing Key Types
102102+ 8.3. Examples
103103+ 9. CBOR Encoding Restrictions
104104+ 10. IANA Considerations
105105+ 10.1. Changes to the "COSE Key Types" Registry
106106+ 10.2. Changes to the "COSE Algorithms" Registry
107107+ 10.3. Changes to the "COSE Key Type Parameters" Registry
108108+ 10.4. Expert Review Instructions
109109+ 11. Security Considerations
110110+ 12. References
111111+ 12.1. Normative References
112112+ 12.2. Informative References
113113+ Acknowledgments
114114+ Author's Address
115115+116116+1. Introduction
117117+118118+ There has been an increased focus on small, constrained devices that
119119+ make up the Internet of Things (IoT). One of the standards that has
120120+ come out of this process is "Concise Binary Object Representation
121121+ (CBOR)" [STD94]. CBOR extended the data model of JavaScript Object
122122+ Notation (JSON) [STD90] by allowing for binary data, among other
123123+ changes. CBOR has been adopted by several of the IETF working groups
124124+ dealing with the IoT world as their method of encoding data
125125+ structures. CBOR was designed specifically to be small in terms of
126126+ both messages transported and implementation size and to have a
127127+ schema-free decoder. A need exists to provide message security
128128+ services for IoT, and using CBOR as the message-encoding format makes
129129+ sense.
130130+131131+ The core COSE specification consists of two documents. [RFC9052]
132132+ contains the serialization structures and the procedures for using
133133+ the different cryptographic algorithms. This document provides an
134134+ initial set of algorithms for use with those structures.
135135+136136+1.1. Requirements Terminology
137137+138138+ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT",
139139+ "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED", "MAY", and
140140+ "OPTIONAL" in this document are to be interpreted as described in
141141+ BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all
142142+ capitals, as shown here.
143143+144144+1.2. Changes from RFC 8152
145145+146146+ * Extracted the sections dealing with specific algorithms and placed
147147+ them into this document. The sections dealing with structure and
148148+ general processing rules are placed in [RFC9052].
149149+150150+ * Made text clarifications and changes in terminology.
151151+152152+ * Removed all of the details relating to countersignatures and
153153+ placed them in [COUNTERSIGN].
154154+155155+1.3. Document Terminology
156156+157157+ In this document, we use the following terminology:
158158+159159+ Byte: A synonym for octet.
160160+161161+ Constrained Application Protocol (CoAP): A specialized web transfer
162162+ protocol for use in constrained systems. It is defined in
163163+ [RFC7252].
164164+165165+ Authenticated Encryption (AE) algorithms [RFC5116]: Encryption
166166+ algorithms that provide an authentication check of the contents
167167+ along with the encryption service. An example of an AE algorithm
168168+ used in COSE is AES Key Wrap [RFC3394]. These algorithms are used
169169+ for key encryption, but Authenticated Encryption with Associated
170170+ Data (AEAD) algorithms would be preferred.
171171+172172+ AEAD algorithms [RFC5116]: Encryption algorithms that provide the
173173+ same authentication service of the content as AE algorithms do,
174174+ and also allow associated data that is not part of the encrypted
175175+ body to be included in the authentication service. An example of
176176+ an AEAD algorithm used in COSE is AES-GCM [RFC5116]. These
177177+ algorithms are used for content encryption and can be used for key
178178+ encryption as well.
179179+180180+ The term "byte string" is used for sequences of bytes, while the term
181181+ "text string" is used for sequences of characters.
182182+183183+ The tables for algorithms contain the following columns:
184184+185185+ * A name for the algorithm for use in documents.
186186+187187+ * The value used on the wire for the algorithm. One place this is
188188+ used is the algorithm header parameter of a message.
189189+190190+ * A short description so that the algorithm can be easily identified
191191+ when scanning the IANA registry.
192192+193193+ Additional columns may be present in a table depending on the
194194+ algorithms.
195195+196196+1.4. CDDL Grammar for CBOR Data Structures
197197+198198+ When COSE was originally written, the Concise Data Definition
199199+ Language (CDDL) [RFC8610] had not yet been published in an RFC, so it
200200+ could not be used as the data description language to normatively
201201+ describe the CBOR data structures employed by COSE. For that reason,
202202+ the CBOR data objects defined here are described in prose.
203203+ Additional (non-normative) descriptions of the COSE data objects are
204204+ provided in a subset of CDDL, described in [RFC9052].
205205+206206+1.5. Examples
207207+208208+ A GitHub project has been created at [GitHub-Examples] that contains
209209+ a set of testing examples. Each example is found in a JSON file that
210210+ contains the inputs used to create the example, some of the
211211+ intermediate values that can be used for debugging, and the output of
212212+ the example. The results are encoded using both hexadecimal and CBOR
213213+ diagnostic notation format.
214214+215215+ Some of the examples are designed to be failure-testing cases; these
216216+ are clearly marked as such in the JSON file.
217217+218218+2. Signature Algorithms
219219+220220+ Section 8.1 of [RFC9052] contains a generic description of signature
221221+ algorithms. This document defines signature algorithm identifiers
222222+ for two signature algorithms.
223223+224224+2.1. ECDSA
225225+226226+ The Elliptic Curve Digital Signature Algorithm (ECDSA) [DSS] defines
227227+ a signature algorithm using Elliptic Curve Cryptography (ECC).
228228+ Implementations SHOULD use a deterministic version of ECDSA such as
229229+ the one defined in [RFC6979]. The use of a deterministic signature
230230+ algorithm allows systems to avoid relying on random number generators
231231+ in order to avoid generating the same value of "k" (the per-message
232232+ random value). Biased generation of the value "k" can be attacked,
233233+ and collisions of this value lead to leaked keys. It additionally
234234+ allows performing deterministic tests for the signature algorithm.
235235+ The use of deterministic ECDSA does not lessen the need to have good
236236+ random number generation when creating the private key.
237237+238238+ The ECDSA signature algorithm is parameterized with a hash function
239239+ (h). In the event that the length of the hash function output is
240240+ greater than the group of the key, the leftmost bytes of the hash
241241+ output are used.
242242+243243+ The algorithms defined in this document can be found in Table 1.
244244+245245+ +=======+=======+=========+==================+
246246+ | Name | Value | Hash | Description |
247247+ +=======+=======+=========+==================+
248248+ | ES256 | -7 | SHA-256 | ECDSA w/ SHA-256 |
249249+ +-------+-------+---------+------------------+
250250+ | ES384 | -35 | SHA-384 | ECDSA w/ SHA-384 |
251251+ +-------+-------+---------+------------------+
252252+ | ES512 | -36 | SHA-512 | ECDSA w/ SHA-512 |
253253+ +-------+-------+---------+------------------+
254254+255255+ Table 1: ECDSA Algorithm Values
256256+257257+ This document defines ECDSA as working only with the curves P-256,
258258+ P-384, and P-521. This document requires that the curves be encoded
259259+ using the "EC2" (two coordinate elliptic curve) key type.
260260+ Implementations need to check that the key type and curve are correct
261261+ when creating and verifying a signature. Future documents may define
262262+ it to work with other curves and key types in the future.
263263+264264+ In order to promote interoperability, it is suggested that SHA-256 be
265265+ used only with curve P-256, SHA-384 be used only with curve P-384,
266266+ and SHA-512 be used only with curve P-521. This is aligned with the
267267+ recommendation in Section 4 of [RFC5480].
268268+269269+ The signature algorithm results in a pair of integers (R, S). These
270270+ integers will be the same length as the length of the key used for
271271+ the signature process. The signature is encoded by converting the
272272+ integers into byte strings of the same length as the key size. The
273273+ length is rounded up to the nearest byte and is left padded with zero
274274+ bits to get to the correct length. The two integers are then
275275+ concatenated together to form a byte string that is the resulting
276276+ signature.
277277+278278+ Using the function defined in [RFC8017], the signature is:
279279+280280+ Signature = I2OSP(R, n) | I2OSP(S, n)
281281+282282+ where n = ceiling(key_length / 8)
283283+284284+ When using a COSE key for this algorithm, the following checks are
285285+ made:
286286+287287+ * The "kty" field MUST be present, and it MUST be "EC2".
288288+289289+ * If the "alg" field is present, it MUST match the ECDSA signature
290290+ algorithm being used.
291291+292292+ * If the "key_ops" field is present, it MUST include "sign" when
293293+ creating an ECDSA signature.
294294+295295+ * If the "key_ops" field is present, it MUST include "verify" when
296296+ verifying an ECDSA signature.
297297+298298+2.1.1. Security Considerations for ECDSA
299299+300300+ The security strength of the signature is no greater than the minimum
301301+ of the security strength associated with the bit length of the key
302302+ and the security strength of the hash function.
303303+304304+ Note: Use of a deterministic signature technique is a good idea even
305305+ when good random number generation exists. Doing so both reduces the
306306+ possibility of having the same value of "k" in two signature
307307+ operations and allows for reproducible signature values, which helps
308308+ testing. There have been recent attacks involving faulting the
309309+ device in order to extract the key. This can be addressed by
310310+ combining both randomness and determinism [CFRG-DET-SIGS].
311311+312312+ There are two substitution attacks that can theoretically be mounted
313313+ against the ECDSA signature algorithm.
314314+315315+ * Changing the curve used to validate the signature: If one changes
316316+ the curve used to validate the signature, then potentially one
317317+ could have two messages with the same signature, each computed
318318+ under a different curve. The only requirements on the new curve
319319+ are that its order be the same as the old one and that it be
320320+ acceptable to the client. An example would be to change from
321321+ using the curve secp256r1 (aka P-256) to using secp256k1. (Both
322322+ are 256-bit curves.) We currently do not have any way to deal
323323+ with this version of the attack except to restrict the overall set
324324+ of curves that can be used.
325325+326326+ * Changing the hash function used to validate the signature: If one
327327+ either has two different hash functions of the same length or can
328328+ truncate a hash function, then one could potentially find
329329+ collisions between the hash functions rather than within a single
330330+ hash function. For example, truncating SHA-512 to 256 bits might
331331+ collide with a SHA-256 bit hash value. As the hash algorithm is
332332+ part of the signature algorithm identifier, this attack is
333333+ mitigated by including a signature algorithm identifier in the
334334+ protected-header bucket.
335335+336336+2.2. Edwards-Curve Digital Signature Algorithm (EdDSA)
337337+338338+ [RFC8032] describes the elliptic curve signature scheme Edwards-curve
339339+ Digital Signature Algorithm (EdDSA). In that document, the signature
340340+ algorithm is instantiated using parameters for the edwards25519 and
341341+ edwards448 curves. The document additionally describes two variants
342342+ of the EdDSA algorithm: Pure EdDSA, where no hash function is applied
343343+ to the content before signing, and HashEdDSA, where a hash function
344344+ is applied to the content before signing and the result of that hash
345345+ function is signed. For EdDSA, the content to be signed (either the
346346+ message or the prehash value) is processed twice inside of the
347347+ signature algorithm. For use with COSE, only the pure EdDSA version
348348+ is used. This is because it is not expected that extremely large
349349+ contents are going to be needed and, based on the arrangement of the
350350+ message structure, the entire message is going to need to be held in
351351+ memory in order to create or verify a signature. Therefore, there
352352+ does not appear to be a need to be able to do block updates of the
353353+ hash, followed by eliminating the message from memory. Applications
354354+ can provide the same features by defining the content of the message
355355+ as a hash value and transporting the COSE object (with the hash
356356+ value) and the content as separate items.
357357+358358+ The algorithm defined in this document can be found in Table 2. A
359359+ single signature algorithm is defined, which can be used for multiple
360360+ curves.
361361+362362+ +=======+=======+=============+
363363+ | Name | Value | Description |
364364+ +=======+=======+=============+
365365+ | EdDSA | -8 | EdDSA |
366366+ +-------+-------+-------------+
367367+368368+ Table 2: EdDSA Algorithm Value
369369+370370+ [RFC8032] describes the method of encoding the signature value.
371371+372372+ When using a COSE key for this algorithm, the following checks are
373373+ made:
374374+375375+ * The "kty" field MUST be present, and it MUST be "OKP" (Octet Key
376376+ Pair).
377377+378378+ * The "crv" field MUST be present, and it MUST be a curve defined
379379+ for this signature algorithm.
380380+381381+ * If the "alg" field is present, it MUST match "EdDSA".
382382+383383+ * If the "key_ops" field is present, it MUST include "sign" when
384384+ creating an EdDSA signature.
385385+386386+ * If the "key_ops" field is present, it MUST include "verify" when
387387+ verifying an EdDSA signature.
388388+389389+2.2.1. Security Considerations for EdDSA
390390+391391+ Public values are computed differently in EdDSA and Elliptic Curve
392392+ Diffie-Hellman (ECDH); for this reason, the public key from one
393393+ should not be used with the other algorithm.
394394+395395+ If batch signature verification is performed, a well-seeded
396396+ cryptographic random number generator is REQUIRED (Section 8.2 of
397397+ [RFC8032]). Signing and nonbatch signature verification are
398398+ deterministic operations and do not need random numbers of any kind.
399399+400400+3. Message Authentication Code (MAC) Algorithms
401401+402402+ Section 8.2 of [RFC9052] contains a generic description of MAC
403403+ algorithms. This section defines the conventions for two MAC
404404+ algorithms.
405405+406406+3.1. Hash-Based Message Authentication Codes (HMACs)
407407+408408+ HMAC [RFC2104] [RFC4231] was designed to deal with length extension
409409+ attacks. The HMAC algorithm was also designed to allow new hash
410410+ functions to be directly plugged in without changes to the hash
411411+ function. The HMAC design process has been shown to be solid;
412412+ although the security of hash functions such as MD5 has decreased
413413+ over time, the security of HMAC combined with MD5 has not yet been
414414+ shown to be compromised [RFC6151].
415415+416416+ The HMAC algorithm is parameterized by an inner and outer padding, a
417417+ hash function (h), and an authentication tag value length. For this
418418+ specification, the inner and outer padding are fixed to the values
419419+ set in [RFC2104]. The length of the authentication tag corresponds
420420+ to the difficulty of producing a forgery. For use in constrained
421421+ environments, we define one HMAC algorithm that is truncated. There
422422+ are currently no known issues with truncation; however, the security
423423+ strength of the message tag is correspondingly reduced in strength.
424424+ When truncating, the leftmost tag-length bits are kept and
425425+ transmitted.
426426+427427+ The algorithms defined in this document can be found in Table 3.
428428+429429+ +=============+=======+=========+============+======================+
430430+ | Name | Value | Hash | Tag Length | Description |
431431+ +=============+=======+=========+============+======================+
432432+ | HMAC | 4 | SHA-256 | 64 | HMAC w/ SHA-256 |
433433+ | 256/64 | | | | truncated to 64 bits |
434434+ +-------------+-------+---------+------------+----------------------+
435435+ | HMAC | 5 | SHA-256 | 256 | HMAC w/ SHA-256 |
436436+ | 256/256 | | | | |
437437+ +-------------+-------+---------+------------+----------------------+
438438+ | HMAC | 6 | SHA-384 | 384 | HMAC w/ SHA-384 |
439439+ | 384/384 | | | | |
440440+ +-------------+-------+---------+------------+----------------------+
441441+ | HMAC | 7 | SHA-512 | 512 | HMAC w/ SHA-512 |
442442+ | 512/512 | | | | |
443443+ +-------------+-------+---------+------------+----------------------+
444444+445445+ Table 3: HMAC Algorithm Values
446446+447447+ Some recipient algorithms transport the key, while others derive a
448448+ key from secret data. For those algorithms that transport the key
449449+ (such as AES Key Wrap), the size of the HMAC key SHOULD be the same
450450+ size as the output of the underlying hash function. For those
451451+ algorithms that derive the key (such as ECDH), the derived key MUST
452452+ be the same size as the output of the underlying hash function.
453453+454454+ When using a COSE key for this algorithm, the following checks are
455455+ made:
456456+457457+ * The "kty" field MUST be present, and it MUST be "Symmetric".
458458+459459+ * If the "alg" field is present, it MUST match the HMAC algorithm
460460+ being used.
461461+462462+ * If the "key_ops" field is present, it MUST include "MAC create"
463463+ when creating an HMAC authentication tag.
464464+465465+ * If the "key_ops" field is present, it MUST include "MAC verify"
466466+ when verifying an HMAC authentication tag.
467467+468468+ Implementations creating and validating MAC values MUST validate that
469469+ the key type, key length, and algorithm are correct and appropriate
470470+ for the entities involved.
471471+472472+3.1.1. Security Considerations for HMAC
473473+474474+ HMAC has proved to be resistant to attack even when used with
475475+ weakened hash algorithms. The current best known attack is to brute
476476+ force the key. This means that key size is going to be directly
477477+ related to the security of an HMAC operation.
478478+479479+3.2. AES Message Authentication Code (AES-CBC-MAC)
480480+481481+ AES-CBC-MAC is the instantiation of the CBC-MAC construction (defined
482482+ in [MAC]) using AES as the block cipher. For brevity, we also use
483483+ "AES-MAC" to refer to AES-CBC-MAC. (Note that this is not the same
484484+ algorithm as AES Cipher-Based Message Authentication Code (AES-CMAC)
485485+ [RFC4493].)
486486+487487+ AES-CBC-MAC is parameterized by the key length, the authentication
488488+ tag length, and the Initialization Vector (IV) used. For all of
489489+ these algorithms, the IV is fixed to all zeros. We provide an array
490490+ of algorithms for various key and tag lengths. The algorithms
491491+ defined in this document are found in Table 4.
492492+493493+ +=========+=======+============+============+==================+
494494+ | Name | Value | Key Length | Tag Length | Description |
495495+ +=========+=======+============+============+==================+
496496+ | AES-MAC | 14 | 128 | 64 | AES-MAC 128-bit |
497497+ | 128/64 | | | | key, 64-bit tag |
498498+ +---------+-------+------------+------------+------------------+
499499+ | AES-MAC | 15 | 256 | 64 | AES-MAC 256-bit |
500500+ | 256/64 | | | | key, 64-bit tag |
501501+ +---------+-------+------------+------------+------------------+
502502+ | AES-MAC | 25 | 128 | 128 | AES-MAC 128-bit |
503503+ | 128/128 | | | | key, 128-bit tag |
504504+ +---------+-------+------------+------------+------------------+
505505+ | AES-MAC | 26 | 256 | 128 | AES-MAC 256-bit |
506506+ | 256/128 | | | | key, 128-bit tag |
507507+ +---------+-------+------------+------------+------------------+
508508+509509+ Table 4: AES-MAC Algorithm Values
510510+511511+ Keys may be obtained from either a key structure or a recipient
512512+ structure. Implementations creating and validating MAC values MUST
513513+ validate that the key type, key length, and algorithm are correct and
514514+ appropriate for the entities involved.
515515+516516+ When using a COSE key for this algorithm, the following checks are
517517+ made:
518518+519519+ * The "kty" field MUST be present, and it MUST be "Symmetric".
520520+521521+ * If the "alg" field is present, it MUST match the AES-MAC algorithm
522522+ being used.
523523+524524+ * If the "key_ops" field is present, it MUST include "MAC create"
525525+ when creating an AES-MAC authentication tag.
526526+527527+ * If the "key_ops" field is present, it MUST include "MAC verify"
528528+ when verifying an AES-MAC authentication tag.
529529+530530+3.2.1. Security Considerations for AES-CBC-MAC
531531+532532+ A number of attacks exist against Cipher Block Chaining Message
533533+ Authentication Code (CBC-MAC) that need to be considered.
534534+535535+ * A single key must only be used for messages of a fixed or known
536536+ length. If this is not the case, an attacker will be able to
537537+ generate a message with a valid tag given two message and tag
538538+ pairs. This can be addressed by using different keys for messages
539539+ of different lengths. The current structure mitigates this
540540+ problem, as a specific encoding structure that includes lengths is
541541+ built and signed. (CMAC also addresses this issue.)
542542+543543+ * In Cipher Block Chaining (CBC) mode, if the same key is used for
544544+ both encryption and authentication operations, an attacker can
545545+ produce messages with a valid authentication code.
546546+547547+ * If the IV can be modified, then messages can be forged. This is
548548+ addressed by fixing the IV to all zeros.
549549+550550+4. Content Encryption Algorithms
551551+552552+ Section 8.3 of [RFC9052] contains a generic description of content
553553+ encryption algorithms. This document defines the identifier and
554554+ usages for three content encryption algorithms.
555555+556556+4.1. AES-GCM
557557+558558+ The Galois/Counter Mode (GCM) mode is a generic AEAD block cipher
559559+ mode defined in [AES-GCM]. The GCM mode is combined with the AES
560560+ block encryption algorithm to define an AEAD cipher.
561561+562562+ The GCM mode is parameterized by the size of the authentication tag
563563+ and the size of the nonce. This document fixes the size of the nonce
564564+ at 96 bits. The size of the authentication tag is limited to a small
565565+ set of values. For this document, however, the size of the
566566+ authentication tag is fixed at 128 bits.
567567+568568+ The set of algorithms defined in this document is in Table 5.
569569+570570+ +=========+=======+==========================================+
571571+ | Name | Value | Description |
572572+ +=========+=======+==========================================+
573573+ | A128GCM | 1 | AES-GCM mode w/ 128-bit key, 128-bit tag |
574574+ +---------+-------+------------------------------------------+
575575+ | A192GCM | 2 | AES-GCM mode w/ 192-bit key, 128-bit tag |
576576+ +---------+-------+------------------------------------------+
577577+ | A256GCM | 3 | AES-GCM mode w/ 256-bit key, 128-bit tag |
578578+ +---------+-------+------------------------------------------+
579579+580580+ Table 5: Algorithm Values for AES-GCM
581581+582582+ Keys may be obtained from either a key structure or a recipient
583583+ structure. Implementations that are encrypting or decrypting MUST
584584+ validate that the key type, key length, and algorithm are correct and
585585+ appropriate for the entities involved.
586586+587587+ When using a COSE key for this algorithm, the following checks are
588588+ made:
589589+590590+ * The "kty" field MUST be present, and it MUST be "Symmetric".
591591+592592+ * If the "alg" field is present, it MUST match the AES-GCM algorithm
593593+ being used.
594594+595595+ * If the "key_ops" field is present, it MUST include "encrypt" or
596596+ "wrap key" when encrypting.
597597+598598+ * If the "key_ops" field is present, it MUST include "decrypt" or
599599+ "unwrap key" when decrypting.
600600+601601+4.1.1. Security Considerations for AES-GCM
602602+603603+ When using AES-GCM, the following restrictions MUST be enforced:
604604+605605+ * The key and nonce pair MUST be unique for every message encrypted.
606606+607607+ * The total number of messages encrypted for a single key MUST NOT
608608+ exceed 2^32 [SP800-38D]. An explicit check is required only in
609609+ environments where it is expected that this limit might be
610610+ exceeded.
611611+612612+ * [RFC8446] contains an analysis on the use of AES-CGM for its
613613+ environment. Based on that recommendation, one should restrict
614614+ the number of messages encrypted to 2^24.5.
615615+616616+ * A more recent analysis in [ROBUST] indicates that the number of
617617+ failed decryptions needs to be taken into account as part of
618618+ determining when a key rollover is to be done. Following the
619619+ recommendation in DTLS (Section 4.5.3 of [RFC9147]), the number of
620620+ failed message decryptions should be limited to 2^36.
621621+622622+ Consideration was given to supporting smaller tag values; the
623623+ constrained community would desire tag sizes in the 64-bit range.
624624+ Such use drastically changes both the maximum message size (generally
625625+ not an issue) and the number of times that a key can be used. Given
626626+ that Counter with CBC-MAC (CCM) is the usual mode for constrained
627627+ environments, restricted modes are not supported.
628628+629629+4.2. AES-CCM
630630+631631+ CCM is a generic authentication encryption block cipher mode defined
632632+ in [RFC3610]. The CCM mode is combined with the AES block encryption
633633+ algorithm to define an AEAD cipher that is commonly used in
634634+ constrained devices.
635635+636636+ The CCM mode has two parameter choices. The first choice is M, the
637637+ size of the authentication field. The choice of the value for M
638638+ involves a trade-off between message growth (from the tag) and the
639639+ probability that an attacker can undetectably modify a message. The
640640+ second choice is L, the size of the length field. This value
641641+ requires a trade-off between the maximum message size and the size of
642642+ the nonce.
643643+644644+ It is unfortunate that the specification for CCM specified L and M as
645645+ a count of bytes rather than a count of bits. This leads to possible
646646+ misunderstandings where AES-CCM-8 is frequently used to refer to a
647647+ version of CCM mode where the size of the authentication is 64 bits
648648+ and not 8 bits. In most cryptographic algorithm specifications,
649649+ these values have traditionally been specified as bit counts rather
650650+ than byte counts. This document will follow the convention of using
651651+ bit counts so that it is easier to compare the different algorithms
652652+ presented in this document.
653653+654654+ We define a matrix of algorithms in this document over the values of
655655+ L and M. Constrained devices are usually operating in situations
656656+ where they use short messages and want to avoid doing recipient-
657657+ specific cryptographic operations. This favors smaller values of
658658+ both L and M. Less-constrained devices will want to be able to use
659659+ larger messages and are more willing to generate new keys for every
660660+ operation. This favors larger values of L and M.
661661+662662+ The following values are used for L:
663663+664664+ 16 bits (2): This limits messages to 2^16 bytes (64 KiB) in length.
665665+ This is sufficiently long for messages in the constrained world.
666666+ The nonce length is 13 bytes allowing for 2^104 possible values of
667667+ the nonce without repeating.
668668+669669+ 64 bits (8): This limits messages to 2^64 bytes in length. The
670670+ nonce length is 7 bytes, allowing for 2^56 possible values of the
671671+ nonce without repeating.
672672+673673+ The following values are used for M:
674674+675675+ 64 bits (8): This produces a 64-bit authentication tag. This
676676+ implies that there is a 1 in 2^64 chance that a modified message
677677+ will authenticate.
678678+679679+ 128 bits (16): This produces a 128-bit authentication tag. This
680680+ implies that there is a 1 in 2^128 chance that a modified message
681681+ will authenticate.
682682+683683+ +====================+=======+====+=====+========+===============+
684684+ | Name | Value | L | M | Key | Description |
685685+ | | | | | Length | |
686686+ +====================+=======+====+=====+========+===============+
687687+ | AES-CCM-16-64-128 | 10 | 16 | 64 | 128 | AES-CCM mode |
688688+ | | | | | | 128-bit key, |
689689+ | | | | | | 64-bit tag, |
690690+ | | | | | | 13-byte nonce |
691691+ +--------------------+-------+----+-----+--------+---------------+
692692+ | AES-CCM-16-64-256 | 11 | 16 | 64 | 256 | AES-CCM mode |
693693+ | | | | | | 256-bit key, |
694694+ | | | | | | 64-bit tag, |
695695+ | | | | | | 13-byte nonce |
696696+ +--------------------+-------+----+-----+--------+---------------+
697697+ | AES-CCM-64-64-128 | 12 | 64 | 64 | 128 | AES-CCM mode |
698698+ | | | | | | 128-bit key, |
699699+ | | | | | | 64-bit tag, |
700700+ | | | | | | 7-byte nonce |
701701+ +--------------------+-------+----+-----+--------+---------------+
702702+ | AES-CCM-64-64-256 | 13 | 64 | 64 | 256 | AES-CCM mode |
703703+ | | | | | | 256-bit key, |
704704+ | | | | | | 64-bit tag, |
705705+ | | | | | | 7-byte nonce |
706706+ +--------------------+-------+----+-----+--------+---------------+
707707+ | AES-CCM-16-128-128 | 30 | 16 | 128 | 128 | AES-CCM mode |
708708+ | | | | | | 128-bit key, |
709709+ | | | | | | 128-bit tag, |
710710+ | | | | | | 13-byte nonce |
711711+ +--------------------+-------+----+-----+--------+---------------+
712712+ | AES-CCM-16-128-256 | 31 | 16 | 128 | 256 | AES-CCM mode |
713713+ | | | | | | 256-bit key, |
714714+ | | | | | | 128-bit tag, |
715715+ | | | | | | 13-byte nonce |
716716+ +--------------------+-------+----+-----+--------+---------------+
717717+ | AES-CCM-64-128-128 | 32 | 64 | 128 | 128 | AES-CCM mode |
718718+ | | | | | | 128-bit key, |
719719+ | | | | | | 128-bit tag, |
720720+ | | | | | | 7-byte nonce |
721721+ +--------------------+-------+----+-----+--------+---------------+
722722+ | AES-CCM-64-128-256 | 33 | 64 | 128 | 256 | AES-CCM mode |
723723+ | | | | | | 256-bit key, |
724724+ | | | | | | 128-bit tag, |
725725+ | | | | | | 7-byte nonce |
726726+ +--------------------+-------+----+-----+--------+---------------+
727727+728728+ Table 6: Algorithm Values for AES-CCM
729729+730730+ Keys may be obtained from either a key structure or a recipient
731731+ structure. Implementations that are encrypting or decrypting MUST
732732+ validate that the key type, key length, and algorithm are correct and
733733+ appropriate for the entities involved.
734734+735735+ When using a COSE key for this algorithm, the following checks are
736736+ made:
737737+738738+ * The "kty" field MUST be present, and it MUST be "Symmetric".
739739+740740+ * If the "alg" field is present, it MUST match the AES-CCM algorithm
741741+ being used.
742742+743743+ * If the "key_ops" field is present, it MUST include "encrypt" or
744744+ "wrap key" when encrypting.
745745+746746+ * If the "key_ops" field is present, it MUST include "decrypt" or
747747+ "unwrap key" when decrypting.
748748+749749+4.2.1. Security Considerations for AES-CCM
750750+751751+ When using AES-CCM, the following restrictions MUST be enforced:
752752+753753+ * The key and nonce pair MUST be unique for every message encrypted.
754754+ Note that the value of L influences the number of unique nonces.
755755+756756+ * The total number of times the AES block cipher is used MUST NOT
757757+ exceed 2^61 operations. This limit is the sum of times the block
758758+ cipher is used in computing the MAC value and performing stream
759759+ encryption operations. An explicit check is required only in
760760+ environments where it is expected that this limit might be
761761+ exceeded.
762762+763763+ * [RFC9147] contains an analysis on the use of AES-CCM for its
764764+ environment. Based on that recommendation, one should restrict
765765+ the number of messages encrypted to 2^23.
766766+767767+ * In addition to the number of messages successfully decrypted, the
768768+ number of failed decryptions needs to be tracked as well.
769769+ Following the recommendation in DTLS (Section 4.5.3 of [RFC9147]),
770770+ the number of failed message decryptions should be limited to
771771+ 2^23.5. If one is using the 64-bit tag, then the limits are
772772+ significantly smaller if one wants to keep the same integrity
773773+ limits. A protocol recommending this needs to analyze what level
774774+ of integrity is acceptable for the smaller tag size. It may be
775775+ that, to keep the desired level of integrity, one needs to rekey
776776+ as often as every 2^7 messages.
777777+778778+ [RFC3610] additionally calls out one other consideration of note. It
779779+ is possible to do a precomputation attack against the algorithm in
780780+ cases where portions of the plaintext are highly predictable. This
781781+ reduces the security of the key size by half. Ways to deal with this
782782+ attack include adding a random portion to the nonce value and/or
783783+ increasing the key size used. Using a portion of the nonce for a
784784+ random value will decrease the number of messages that a single key
785785+ can be used for. Increasing the key size may require more resources
786786+ in the constrained device. See Sections 5 and 10 of [RFC3610] for
787787+ more information.
788788+789789+4.3. ChaCha20 and Poly1305
790790+791791+ ChaCha20 and Poly1305 combined together is an AEAD mode that is
792792+ defined in [RFC8439]. This is an algorithm defined using a cipher
793793+ that is not AES and thus would not suffer from any future weaknesses
794794+ found in AES. These cryptographic functions are designed to be fast
795795+ in software-only implementations.
796796+797797+ The ChaCha20/Poly1305 AEAD construction defined in [RFC8439] has no
798798+ parameterization. It takes as inputs a 256-bit key and a 96-bit
799799+ nonce, as well as the plaintext and additional data, and produces the
800800+ ciphertext as an output. We define one algorithm identifier for this
801801+ algorithm in Table 7.
802802+803803+ +===================+=======+==========================+
804804+ | Name | Value | Description |
805805+ +===================+=======+==========================+
806806+ | ChaCha20/Poly1305 | 24 | ChaCha20/Poly1305 w/ |
807807+ | | | 256-bit key, 128-bit tag |
808808+ +-------------------+-------+--------------------------+
809809+810810+ Table 7: Algorithm Value for ChaCha20/Poly1305
811811+812812+ Keys may be obtained from either a key structure or a recipient
813813+ structure. Implementations that are encrypting or decrypting MUST
814814+ validate that the key type, key length, and algorithm are correct and
815815+ appropriate for the entities involved.
816816+817817+ When using a COSE key for this algorithm, the following checks are
818818+ made:
819819+820820+ * The "kty" field MUST be present, and it MUST be "Symmetric".
821821+822822+ * If the "alg" field is present, it MUST match the ChaCha20/Poly1305
823823+ algorithm being used.
824824+825825+ * If the "key_ops" field is present, it MUST include "encrypt" or
826826+ "wrap key" when encrypting.
827827+828828+ * If the "key_ops" field is present, it MUST include "decrypt" or
829829+ "unwrap key" when decrypting.
830830+831831+4.3.1. Security Considerations for ChaCha20/Poly1305
832832+833833+ The key and nonce values MUST be a unique pair for every invocation
834834+ of the algorithm. Nonce counters are considered to be an acceptable
835835+ way of ensuring that they are unique.
836836+837837+ A more recent analysis in [ROBUST] indicates that the number of
838838+ failed decryptions needs to be taken into account as part of
839839+ determining when a key rollover is to be done. Following the
840840+ recommendation in DTLS (Section 4.5.3 of [RFC9147]), the number of
841841+ failed message decryptions should be limited to 2^36.
842842+843843+ [RFC8446] notes that the (64-bit) record sequence number would wrap
844844+ before the safety limit is reached for ChaCha20/Poly1305. COSE
845845+ implementations should not send more than 2^64 messages encrypted
846846+ using a single ChaCha20/Poly1305 key.
847847+848848+5. Key Derivation Functions (KDFs)
849849+850850+ Section 8.4 of [RFC9052] contains a generic description of key
851851+ derivation functions. This document defines a single context
852852+ structure and a single KDF. These elements are used for all of the
853853+ recipient algorithms defined in this document that require a KDF
854854+ process. These algorithms are defined in Sections 6.1.2, 6.3.1, and
855855+ 6.4.1.
856856+857857+5.1. HMAC-Based Extract-and-Expand Key Derivation Function (HKDF)
858858+859859+ The HKDF key derivation algorithm is defined in [RFC5869] and [HKDF].
860860+861861+ The HKDF algorithm takes these inputs:
862862+863863+ secret: A shared value that is secret. Secrets may be either
864864+ previously shared or derived from operations like a Diffie-Hellman
865865+ (DH) key agreement.
866866+867867+ salt: An optional value that is used to change the generation
868868+ process. The salt value can be either public or private. If the
869869+ salt is public and carried in the message, then the "salt"
870870+ algorithm header parameter defined in Table 9 is used. While
871871+ [RFC5869] suggests that the length of the salt be the same as the
872872+ length of the underlying hash value, any positive salt length will
873873+ improve the security, as different key values will be generated.
874874+ This parameter is protected by being included in the key
875875+ computation and does not need to be separately authenticated. The
876876+ salt value does not need to be unique for every message sent.
877877+878878+ length: The number of bytes of output that need to be generated.
879879+880880+ context information: Information that describes the context in which
881881+ the resulting value will be used. Making this information
882882+ specific to the context in which the material is going to be used
883883+ ensures that the resulting material will always be tied to that
884884+ usage. The context structure defined in Section 5.2 is used by
885885+ the KDFs in this document.
886886+887887+ PRF: The underlying pseudorandom function to be used in the HKDF
888888+ algorithm. The PRF is encoded into the HKDF algorithm selection.
889889+890890+ HKDF is defined to use HMAC as the underlying PRF. However, it is
891891+ possible to use other functions in the same construct to provide a
892892+ different KDF that is more appropriate in the constrained world.
893893+ Specifically, one can use AES-CBC-MAC as the PRF for the expand step,
894894+ but not for the extract step. When using a good random shared secret
895895+ of the correct length, the extract step can be skipped. For the AES
896896+ algorithm versions, the extract step is always skipped.
897897+898898+ The extract step cannot be skipped if the secret is not uniformly
899899+ random -- for example, if it is the result of an ECDH key agreement
900900+ step. This implies that the AES HKDF version cannot be used with
901901+ ECDH. If the extract step is skipped, the "salt" value is not used
902902+ as part of the HKDF functionality.
903903+904904+ The algorithms defined in this document are found in Table 8.
905905+906906+ +==============+===================+========================+
907907+ | Name | PRF | Description |
908908+ +==============+===================+========================+
909909+ | HKDF SHA-256 | HMAC with SHA-256 | HKDF using HMAC |
910910+ | | | SHA-256 as the PRF |
911911+ +--------------+-------------------+------------------------+
912912+ | HKDF SHA-512 | HMAC with SHA-512 | HKDF using HMAC |
913913+ | | | SHA-512 as the PRF |
914914+ +--------------+-------------------+------------------------+
915915+ | HKDF AES- | AES-CBC-MAC-128 | HKDF using AES-MAC as |
916916+ | MAC-128 | | the PRF w/ 128-bit key |
917917+ +--------------+-------------------+------------------------+
918918+ | HKDF AES- | AES-CBC-MAC-256 | HKDF using AES-MAC as |
919919+ | MAC-256 | | the PRF w/ 256-bit key |
920920+ +--------------+-------------------+------------------------+
921921+922922+ Table 8: HKDF Algorithms
923923+924924+ +======+=======+======+============================+=============+
925925+ | Name | Label | Type | Algorithm | Description |
926926+ +======+=======+======+============================+=============+
927927+ | salt | -20 | bstr | direct+HKDF-SHA-256, | Random salt |
928928+ | | | | direct+HKDF-SHA-512, | |
929929+ | | | | direct+HKDF-AES-128, | |
930930+ | | | | direct+HKDF-AES-256, ECDH- | |
931931+ | | | | ES+HKDF-256, ECDH-ES+HKDF- | |
932932+ | | | | 512, ECDH-SS+HKDF-256, | |
933933+ | | | | ECDH-SS+HKDF-512, ECDH- | |
934934+ | | | | ES+A128KW, ECDH-ES+A192KW, | |
935935+ | | | | ECDH-ES+A256KW, ECDH- | |
936936+ | | | | SS+A128KW, ECDH-SS+A192KW, | |
937937+ | | | | ECDH-SS+A256KW | |
938938+ +------+-------+------+----------------------------+-------------+
939939+940940+ Table 9: HKDF Algorithm Parameters
941941+942942+5.2. Context Information Structure
943943+944944+ The context information structure is used to ensure that the derived
945945+ keying material is "bound" to the context of the transaction. The
946946+ context information structure used here is based on that defined in
947947+ [SP800-56A]. By using CBOR for the encoding of the context
948948+ information structure, we automatically get the same type and length
949949+ separation of fields that is obtained by the use of ASN.1. This
950950+ means that there is no need to encode the lengths for the base
951951+ elements, as it is done by the encoding used in JSON Object Signing
952952+ and Encryption (JOSE) (Section 4.6.2 of [RFC7518]).
953953+954954+ The context information structure refers to PartyU and PartyV as the
955955+ two parties that are doing the key derivation. Unless the
956956+ application protocol defines differently, we assign PartyU to the
957957+ entity that is creating the message and PartyV to the entity that is
958958+ receiving the message. By defining this association, different keys
959959+ will be derived for each direction, as the context information is
960960+ different in each direction.
961961+962962+ The context structure is built from information that is known to both
963963+ entities. This information can be obtained from a variety of
964964+ sources:
965965+966966+ * Fields can be defined by the application. This is commonly used
967967+ to assign fixed names to parties, but it can be used for other
968968+ items such as nonces.
969969+970970+ * Fields can be defined by usage of the output. Examples of this
971971+ are the algorithm and key size that are being generated.
972972+973973+ * Fields can be defined by parameters from the message. We define a
974974+ set of header parameters in Table 10 that can be used to carry the
975975+ values associated with the context structure. Examples of this
976976+ are identities and nonce values. These header parameters are
977977+ designed to be placed in the unprotected bucket of the recipient
978978+ structure; they do not need to be in the protected bucket, since
979979+ they are already included in the cryptographic computation by
980980+ virtue of being included in the context structure.
981981+982982+ +==========+=======+======+===========================+=============+
983983+ | Name | Label | Type | Algorithm | Description |
984984+ +==========+=======+======+===========================+=============+
985985+ | PartyU | -21 | bstr | direct+HKDF-SHA-256, | PartyU |
986986+ | identity | | | direct+HKDF-SHA-512, | identity |
987987+ | | | | direct+HKDF-AES-128, | information |
988988+ | | | | direct+HKDF-AES-256, | |
989989+ | | | | ECDH-ES+HKDF-256, | |
990990+ | | | | ECDH-ES+HKDF-512, | |
991991+ | | | | ECDH-SS+HKDF-256, | |
992992+ | | | | ECDH-SS+HKDF-512, | |
993993+ | | | | ECDH-ES+A128KW, | |
994994+ | | | | ECDH-ES+A192KW, | |
995995+ | | | | ECDH-ES+A256KW, | |
996996+ | | | | ECDH-SS+A128KW, | |
997997+ | | | | ECDH-SS+A192KW, | |
998998+ | | | | ECDH-SS+A256KW | |
999999+ +----------+-------+------+---------------------------+-------------+
10001000+ | PartyU | -22 | bstr | direct+HKDF-SHA-256, | PartyU |
10011001+ | nonce | | / | direct+HKDF-SHA-512, | provided |
10021002+ | | | int | direct+HKDF-AES-128, | nonce |
10031003+ | | | | direct+HKDF-AES-256, | |
10041004+ | | | | ECDH-ES+HKDF-256, | |
10051005+ | | | | ECDH-ES+HKDF-512, | |
10061006+ | | | | ECDH-SS+HKDF-256, | |
10071007+ | | | | ECDH-SS+HKDF-512, | |
10081008+ | | | | ECDH-ES+A128KW, | |
10091009+ | | | | ECDH-ES+A192KW, | |
10101010+ | | | | ECDH-ES+A256KW, | |
10111011+ | | | | ECDH-SS+A128KW, | |
10121012+ | | | | ECDH-SS+A192KW, | |
10131013+ | | | | ECDH-SS+A256KW | |
10141014+ +----------+-------+------+---------------------------+-------------+
10151015+ | PartyU | -23 | bstr | direct+HKDF-SHA-256, | PartyU |
10161016+ | other | | | direct+HKDF-SHA-512, | other |
10171017+ | | | | direct+HKDF-AES-128, | provided |
10181018+ | | | | direct+HKDF-AES-256, | information |
10191019+ | | | | ECDH-ES+HKDF-256, | |
10201020+ | | | | ECDH-ES+HKDF-512, | |
10211021+ | | | | ECDH-SS+HKDF-256, | |
10221022+ | | | | ECDH-SS+HKDF-512, | |
10231023+ | | | | ECDH-ES+A128KW, | |
10241024+ | | | | ECDH-ES+A192KW, | |
10251025+ | | | | ECDH-ES+A256KW, | |
10261026+ | | | | ECDH-SS+A128KW, | |
10271027+ | | | | ECDH-SS+A192KW, | |
10281028+ | | | | ECDH-SS+A256KW | |
10291029+ +----------+-------+------+---------------------------+-------------+
10301030+ | PartyV | -24 | bstr | direct+HKDF-SHA-256, | PartyV |
10311031+ | identity | | | direct+HKDF-SHA-512, | identity |
10321032+ | | | | direct+HKDF-AES-128, | information |
10331033+ | | | | direct+HKDF-AES-256, | |
10341034+ | | | | ECDH-ES+HKDF-256, | |
10351035+ | | | | ECDH-ES+HKDF-512, | |
10361036+ | | | | ECDH-SS+HKDF-256, | |
10371037+ | | | | ECDH-SS+HKDF-512, | |
10381038+ | | | | ECDH-ES+A128KW, | |
10391039+ | | | | ECDH-ES+A192KW, | |
10401040+ | | | | ECDH-ES+A256KW, | |
10411041+ | | | | ECDH-SS+A128KW, | |
10421042+ | | | | ECDH-SS+A192KW, | |
10431043+ | | | | ECDH-SS+A256KW | |
10441044+ +----------+-------+------+---------------------------+-------------+
10451045+ | PartyV | -25 | bstr | direct+HKDF-SHA-256, | PartyV |
10461046+ | nonce | | / | direct+HKDF-SHA-512, | provided |
10471047+ | | | int | direct+HKDF-AES-128, | nonce |
10481048+ | | | | direct+HKDF-AES-256, | |
10491049+ | | | | ECDH-ES+HKDF-256, | |
10501050+ | | | | ECDH-ES+HKDF-512, | |
10511051+ | | | | ECDH-SS+HKDF-256, | |
10521052+ | | | | ECDH-SS+HKDF-512, | |
10531053+ | | | | ECDH-ES+A128KW, | |
10541054+ | | | | ECDH-ES+A192KW, | |
10551055+ | | | | ECDH-ES+A256KW, | |
10561056+ | | | | ECDH-SS+A128KW, | |
10571057+ | | | | ECDH-SS+A192KW, | |
10581058+ | | | | ECDH-SS+A256KW | |
10591059+ +----------+-------+------+---------------------------+-------------+
10601060+ | PartyV | -26 | bstr | direct+HKDF-SHA-256, | PartyV |
10611061+ | other | | | direct+HKDF-SHA-512, | other |
10621062+ | | | | direct+HKDF-AES-128, | provided |
10631063+ | | | | direct+HKDF-AES-256, | information |
10641064+ | | | | ECDH-ES+HKDF-256, | |
10651065+ | | | | ECDH-ES+HKDF-512, | |
10661066+ | | | | ECDH-SS+HKDF-256, | |
10671067+ | | | | ECDH-SS+HKDF-512, | |
10681068+ | | | | ECDH-ES+A128KW, | |
10691069+ | | | | ECDH-ES+A192KW, | |
10701070+ | | | | ECDH-ES+A256KW, | |
10711071+ | | | | ECDH-SS+A128KW, | |
10721072+ | | | | ECDH-SS+A192KW, | |
10731073+ | | | | ECDH-SS+A256KW | |
10741074+ +----------+-------+------+---------------------------+-------------+
10751075+10761076+ Table 10: Context Algorithm Parameters
10771077+10781078+ We define a CBOR object to hold the context information. This object
10791079+ is referred to as COSE_KDF_Context. The object is based on a CBOR
10801080+ array type. The fields in the array are:
10811081+10821082+ AlgorithmID: This field indicates the algorithm for which the key
10831083+ material will be used. This normally is either a key wrap
10841084+ algorithm identifier or a content encryption algorithm identifier.
10851085+ The values are from the "COSE Algorithms" registry. This field is
10861086+ required to be present. The field exists in the context
10871087+ information so that a different key is generated for each
10881088+ algorithm even if all of the other context information is the
10891089+ same. In practice, this means if algorithm A is broken and thus
10901090+ finding the key is relatively easy, the key derived for algorithm
10911091+ B will not be the same as the key derived for algorithm A.
10921092+10931093+ PartyUInfo: This field holds information about PartyU. The
10941094+ PartyUInfo is encoded as a CBOR array. The elements of PartyUInfo
10951095+ are encoded in the order presented below. The elements of the
10961096+ PartyUInfo array are:
10971097+10981098+ identity: This contains the identity information for PartyU. The
10991099+ identities can be assigned in one of two manners. First, a
11001100+ protocol can assign identities based on roles. For example,
11011101+ the roles of "client" and "server" may be assigned to different
11021102+ entities in the protocol. Each entity would then use the
11031103+ correct label for the data it sends or receives. The second
11041104+ way for a protocol to assign identities is to use a name based
11051105+ on a naming system (i.e., DNS or X.509 names).
11061106+11071107+ We define an algorithm parameter, "PartyU identity", that can
11081108+ be used to carry identity information in the message. However,
11091109+ identity information is often known as part of the protocol and
11101110+ can thus be inferred rather than made explicit. If identity
11111111+ information is carried in the message, applications SHOULD have
11121112+ a way of validating the supplied identity information. The
11131113+ identity information does not need to be specified and is set
11141114+ to nil in that case.
11151115+11161116+ nonce: This contains a nonce value. The nonce can be either
11171117+ implicit from the protocol or carried as a value in the
11181118+ unprotected header bucket.
11191119+11201120+ We define an algorithm parameter, "PartyU nonce", that can be
11211121+ used to carry this value in the message; however, the nonce
11221122+ value could be determined by the application and its value
11231123+ obtained in a different manner.
11241124+11251125+ This option does not need to be specified; if not needed, it is
11261126+ set to nil.
11271127+11281128+ other: This contains other information that is defined by the
11291129+ protocol. This option does not need to be specified; if not
11301130+ needed, it is set to nil.
11311131+11321132+ PartyVInfo: This field holds information about PartyV. The content
11331133+ of the structure is the same as for the PartyUInfo but for PartyV.
11341134+11351135+ SuppPubInfo: This field contains public information that is mutually
11361136+ known to both parties, and is encoded as a CBOR array.
11371137+11381138+ keyDataLength: This is set to the number of bits of the desired
11391139+ output value. This practice means if algorithm A can use two
11401140+ different key lengths, the key derived for the longer key size
11411141+ will not contain the key for the shorter key size as a prefix.
11421142+11431143+ protected: This field contains the protected parameter field. If
11441144+ there are no elements in the "protected" field, then use a
11451145+ zero-length bstr.
11461146+11471147+ other: This field is for free-form data defined by the
11481148+ application. For example, an application could define two
11491149+ different byte strings to be placed here to generate different
11501150+ keys for a data stream versus a control stream. This field is
11511151+ optional and will only be present if the application defines a
11521152+ structure for this information. Applications that define this
11531153+ SHOULD use CBOR to encode the data so that types and lengths
11541154+ are correctly included.
11551155+11561156+ SuppPrivInfo: This field contains private information that is
11571157+ mutually known private information. An example of this
11581158+ information would be a pre-existing shared secret. (This could,
11591159+ for example, be used in combination with an ECDH key agreement to
11601160+ provide a secondary proof of identity.) The field is optional and
11611161+ will only be present if the application defines a structure for
11621162+ this information. Applications that define this SHOULD use CBOR
11631163+ to encode the data so that types and lengths are correctly
11641164+ included.
11651165+11661166+ The following CDDL fragment corresponds to the text above.
11671167+11681168+ PartyInfo = (
11691169+ identity : bstr / nil,
11701170+ nonce : bstr / int / nil,
11711171+ other : bstr / nil
11721172+ )
11731173+11741174+ COSE_KDF_Context = [
11751175+ AlgorithmID : int / tstr,
11761176+ PartyUInfo : [ PartyInfo ],
11771177+ PartyVInfo : [ PartyInfo ],
11781178+ SuppPubInfo : [
11791179+ keyDataLength : uint,
11801180+ protected : empty_or_serialized_map,
11811181+ ? other : bstr
11821182+ ],
11831183+ ? SuppPrivInfo : bstr
11841184+ ]
11851185+11861186+6. Content Key Distribution Methods
11871187+11881188+ Section 8.5 of [RFC9052] contains a generic description of content
11891189+ key distribution methods. This document defines the identifiers and
11901190+ usage for a number of content key distribution methods.
11911191+11921192+6.1. Direct Encryption
11931193+11941194+ A direct encryption algorithm is defined in Section 8.5.1 of
11951195+ [RFC9052]. Information about how to fill in the COSE_Recipient
11961196+ structure is detailed there.
11971197+11981198+6.1.1. Direct Key
11991199+12001200+ This recipient algorithm is the simplest; the identified key is
12011201+ directly used as the key for the next layer down in the message.
12021202+ There are no algorithm parameters defined for this algorithm. The
12031203+ algorithm identifier value is assigned in Table 11.
12041204+12051205+ When this algorithm is used, the "protected" field MUST be zero
12061206+ length. The key type MUST be "Symmetric".
12071207+12081208+ +========+=======+============================================+
12091209+ | Name | Value | Description |
12101210+ +========+=======+============================================+
12111211+ | direct | -6 | Direct use of content encryption key (CEK) |
12121212+ +--------+-------+--------------------------------------------+
12131213+12141214+ Table 11: Direct Key
12151215+12161216+6.1.1.1. Security Considerations for Direct Key
12171217+12181218+ This recipient algorithm has several potential problems that need to
12191219+ be considered:
12201220+12211221+ * These keys need to have some method of being regularly updated
12221222+ over time. All of the content encryption algorithms specified in
12231223+ this document have limits on how many times a key can be used
12241224+ without significant loss of security.
12251225+12261226+ * These keys need to be dedicated to a single algorithm. There have
12271227+ been a number of attacks developed over time when a single key is
12281228+ used for multiple different algorithms. One example of this is
12291229+ the use of a single key for both the CBC encryption mode and the
12301230+ CBC-MAC authentication mode.
12311231+12321232+ * Breaking one message means all messages are broken. If an
12331233+ adversary succeeds in determining the key for a single message,
12341234+ then the key for all messages is also determined.
12351235+12361236+6.1.2. Direct Key with KDF
12371237+12381238+ These recipient algorithms take a common shared secret between the
12391239+ two parties and apply the HKDF function (Section 5.1), using the
12401240+ context structure defined in Section 5.2 to transform the shared
12411241+ secret into the CEK. The "protected" field can be of nonzero length.
12421242+ Either the "salt" parameter for HKDF (Table 9) or the "PartyU nonce"
12431243+ parameter for the context structure (Table 10) MUST be present (both
12441244+ can be present if desired). The value in the "salt"/"nonce"
12451245+ parameter can be generated either randomly or deterministically. The
12461246+ requirement is that it be a unique value for the shared secret in
12471247+ question.
12481248+12491249+ If the salt/nonce value is generated randomly, then it is suggested
12501250+ that the length of the random value be the same length as the output
12511251+ of the hash function underlying HKDF. While there is no way to
12521252+ guarantee that it will be unique, there is a high probability that it
12531253+ will be unique. If the salt/nonce value is generated
12541254+ deterministically, it can be guaranteed to be unique, and thus there
12551255+ is no length requirement.
12561256+12571257+ A new IV must be used for each message if the same key is used. The
12581258+ IV can be modified in a predictable manner, a random manner, or an
12591259+ unpredictable manner (e.g., encrypting a counter).
12601260+12611261+ The IV used for a key can also be generated using the same HKDF
12621262+ functionality used to generate the key. If HKDF is used for
12631263+ generating the IV, the algorithm identifier is set to 34 ("IV-
12641264+ GENERATION").
12651265+12661266+ The set of algorithms defined in this document can be found in
12671267+ Table 12.
12681268+12691269+ +=====================+=======+==============+=====================+
12701270+ | Name | Value | KDF | Description |
12711271+ +=====================+=======+==============+=====================+
12721272+ | direct+HKDF-SHA-256 | -10 | HKDF SHA-256 | Shared secret w/ |
12731273+ | | | | HKDF and SHA-256 |
12741274+ +---------------------+-------+--------------+---------------------+
12751275+ | direct+HKDF-SHA-512 | -11 | HKDF SHA-512 | Shared secret w/ |
12761276+ | | | | HKDF and SHA-512 |
12771277+ +---------------------+-------+--------------+---------------------+
12781278+ | direct+HKDF-AES-128 | -12 | HKDF AES- | Shared secret w/ |
12791279+ | | | MAC-128 | AES-MAC 128-bit key |
12801280+ +---------------------+-------+--------------+---------------------+
12811281+ | direct+HKDF-AES-256 | -13 | HKDF AES- | Shared secret w/ |
12821282+ | | | MAC-256 | AES-MAC 256-bit key |
12831283+ +---------------------+-------+--------------+---------------------+
12841284+12851285+ Table 12: Direct Key with KDF
12861286+12871287+ When using a COSE key for this algorithm, the following checks are
12881288+ made:
12891289+12901290+ * The "kty" field MUST be present, and it MUST be "Symmetric".
12911291+12921292+ * If the "alg" field is present, it MUST match the algorithm being
12931293+ used.
12941294+12951295+ * If the "key_ops" field is present, it MUST include "derive key" or
12961296+ "derive bits".
12971297+12981298+6.1.2.1. Security Considerations for Direct Key with KDF
12991299+13001300+ The shared secret needs to have some method of being regularly
13011301+ updated over time. The shared secret forms the basis of trust.
13021302+ Although not used directly, it should still be subject to scheduled
13031303+ rotation.
13041304+13051305+ These methods do not provide for perfect forward secrecy, as the same
13061306+ shared secret is used for all of the keys generated; however, if the
13071307+ key for any single message is discovered, only the message or series
13081308+ of messages using that derived key are compromised. A new key
13091309+ derivation step will generate a new key that requires the same amount
13101310+ of work to get the key.
13111311+13121312+6.2. Key Wrap
13131313+13141314+ Key wrap is defined in Section 8.5.2 of [RFC9052]. Information about
13151315+ how to fill in the COSE_Recipient structure is detailed there.
13161316+13171317+6.2.1. AES Key Wrap
13181318+13191319+ The AES Key Wrap algorithm is defined in [RFC3394]. This algorithm
13201320+ uses an AES key to wrap a value that is a multiple of 64 bits. As
13211321+ such, it can be used to wrap a key for any of the content encryption
13221322+ algorithms defined in this document. The algorithm requires a single
13231323+ fixed parameter, the initial value. This is fixed to the value
13241324+ specified in Section 2.2.3.1 of [RFC3394]. There are no public key
13251325+ parameters that vary on a per-invocation basis. The protected header
13261326+ bucket MUST be empty.
13271327+13281328+ Keys may be obtained from either a key structure or a recipient
13291329+ structure. Implementations that are encrypting or decrypting MUST
13301330+ validate that the key type, key length, and algorithm are correct and
13311331+ appropriate for the entities involved.
13321332+13331333+ When using a COSE key for this algorithm, the following checks are
13341334+ made:
13351335+13361336+ * The "kty" field MUST be present, and it MUST be "Symmetric".
13371337+13381338+ * If the "alg" field is present, it MUST match the AES Key Wrap
13391339+ algorithm being used.
13401340+13411341+ * If the "key_ops" field is present, it MUST include "encrypt" or
13421342+ "wrap key" when encrypting.
13431343+13441344+ * If the "key_ops" field is present, it MUST include "decrypt" or
13451345+ "unwrap key" when decrypting.
13461346+13471347+ +========+=======+==========+=============================+
13481348+ | Name | Value | Key Size | Description |
13491349+ +========+=======+==========+=============================+
13501350+ | A128KW | -3 | 128 | AES Key Wrap w/ 128-bit key |
13511351+ +--------+-------+----------+-----------------------------+
13521352+ | A192KW | -4 | 192 | AES Key Wrap w/ 192-bit key |
13531353+ +--------+-------+----------+-----------------------------+
13541354+ | A256KW | -5 | 256 | AES Key Wrap w/ 256-bit key |
13551355+ +--------+-------+----------+-----------------------------+
13561356+13571357+ Table 13: AES Key Wrap Algorithm Values
13581358+13591359+6.2.1.1. Security Considerations for AES Key Wrap
13601360+13611361+ The shared secret needs to have some method of being regularly
13621362+ updated over time. The shared secret is the basis of trust.
13631363+13641364+6.3. Direct Key Agreement
13651365+13661366+ Direct Key Agreement is defined in Section 8.5.4 of [RFC9052].
13671367+ Information about how to fill in the COSE_Recipient structure is
13681368+ detailed there.
13691369+13701370+6.3.1. Direct ECDH
13711371+13721372+ The mathematics for ECDH can be found in [RFC6090]. In this
13731373+ document, the algorithm is extended to be used with the two curves
13741374+ defined in [RFC7748].
13751375+13761376+ ECDH is parameterized by the following:
13771377+13781378+ Curve Type/Curve: The curve selected controls not only the size of
13791379+ the shared secret, but the mathematics for computing the shared
13801380+ secret. The curve selected also controls how a point in the curve
13811381+ is represented and what happens for the identity points on the
13821382+ curve. In this specification, we allow for a number of different
13831383+ curves to be used. A set of curves is defined in Table 18.
13841384+13851385+ The math used to obtain the computed secret is based on the curve
13861386+ selected and not on the ECDH algorithm. For this reason, a new
13871387+ algorithm does not need to be defined for each of the curves.
13881388+13891389+ Computed Secret to Shared Secret: Once the computed secret is known,
13901390+ the resulting value needs to be converted to a byte string to run
13911391+ the KDF. The x-coordinate is used for all of the curves defined
13921392+ in this document. For curves X25519 and X448, the resulting value
13931393+ is used directly, as it is a byte string of a known length. For
13941394+ the P-256, P-384, and P-521 curves, the x-coordinate is run
13951395+ through the Integer-to-Octet-String primitive (I2OSP) function
13961396+ defined in [RFC8017], using the same computation for n as is
13971397+ defined in Section 2.1.
13981398+13991399+ Ephemeral-Static or Static-Static: The key agreement process may be
14001400+ done using either a static or an ephemeral key for the sender's
14011401+ side. When using ephemeral keys, the sender MUST generate a new
14021402+ ephemeral key for every key agreement operation. The ephemeral
14031403+ key is placed in the "ephemeral key" parameter and MUST be present
14041404+ for all algorithm identifiers that use ephemeral keys. When using
14051405+ static keys, the sender MUST either generate a new random value or
14061406+ create a unique value for use as a KDF input. For the KDFs used,
14071407+ this means that either the "salt" parameter for HKDF (Table 9) or
14081408+ the "PartyU nonce" parameter for the context structure (Table 10)
14091409+ MUST be present (both can be present if desired). The value in
14101410+ the parameter MUST be unique for the pair of keys being used. It
14111411+ is acceptable to use a global counter that is incremented for
14121412+ every Static-Static operation and use the resulting value. Care
14131413+ must be taken that the counter is saved to permanent storage in a
14141414+ way that avoids reuse of that counter value. When using static
14151415+ keys, the static key should be identified to the recipient. The
14161416+ static key can be identified by providing either the key ("static
14171417+ key") or a key identifier for the static key ("static key id").
14181418+ Both of these header parameters are defined in Table 15.
14191419+14201420+ Key Derivation Algorithm: The result of an ECDH key agreement
14211421+ process does not provide a uniformly random secret. As such, it
14221422+ needs to be run through a KDF in order to produce a usable key.
14231423+ Processing the secret through a KDF also allows for the
14241424+ introduction of context material: how the key is going to be used
14251425+ and one-time material for Static-Static key agreement. All of the
14261426+ algorithms defined in this document use one of the HKDF algorithms
14271427+ defined in Section 5.1 with the context structure defined in
14281428+ Section 5.2.
14291429+14301430+ Key Wrap Algorithm: No key wrap algorithm is used. This is
14311431+ represented in Table 14 as "none". The key size for the context
14321432+ structure is the content layer encryption algorithm size.
14331433+14341434+ COSE does not have an Ephemeral-Ephemeral version defined. The
14351435+ reason for this is that COSE is not an online protocol by itself and
14361436+ thus does not have a method of establishing ephemeral secrets on both
14371437+ sides. The expectation is that a protocol would establish the
14381438+ secrets for both sides, and then they would be used as Static-Static
14391439+ for the purposes of COSE, or that the protocol would generate a
14401440+ shared secret and a direct encryption would be used.
14411441+14421442+ The set of direct ECDH algorithms defined in this document is found
14431443+ in Table 14.
14441444+14451445+ +==========+=======+=========+==================+=====+=============+
14461446+ |Name | Value | KDF | Ephemeral-Static |Key |Description |
14471447+ | | | | |Wrap | |
14481448+ +==========+=======+=========+==================+=====+=============+
14491449+ |ECDH-ES + | -25 | HKDF -- | yes |none |ECDH ES w/ |
14501450+ |HKDF-256 | | SHA-256 | | |HKDF -- |
14511451+ | | | | | |generate key |
14521452+ | | | | | |directly |
14531453+ +----------+-------+---------+------------------+-----+-------------+
14541454+ |ECDH-ES + | -26 | HKDF -- | yes |none |ECDH ES w/ |
14551455+ |HKDF-512 | | SHA-512 | | |HKDF -- |
14561456+ | | | | | |generate key |
14571457+ | | | | | |directly |
14581458+ +----------+-------+---------+------------------+-----+-------------+
14591459+ |ECDH-SS + | -27 | HKDF -- | no |none |ECDH SS w/ |
14601460+ |HKDF-256 | | SHA-256 | | |HKDF -- |
14611461+ | | | | | |generate key |
14621462+ | | | | | |directly |
14631463+ +----------+-------+---------+------------------+-----+-------------+
14641464+ |ECDH-SS + | -28 | HKDF -- | no |none |ECDH SS w/ |
14651465+ |HKDF-512 | | SHA-512 | | |HKDF -- |
14661466+ | | | | | |generate key |
14671467+ | | | | | |directly |
14681468+ +----------+-------+---------+------------------+-----+-------------+
14691469+14701470+ Table 14: ECDH Algorithm Values
14711471+14721472+ +===========+=======+==========+===================+=============+
14731473+ | Name | Label | Type | Algorithm | Description |
14741474+ +===========+=======+==========+===================+=============+
14751475+ | ephemeral | -1 | COSE_Key | ECDH-ES+HKDF-256, | Ephemeral |
14761476+ | key | | | ECDH-ES+HKDF-512, | public key |
14771477+ | | | | ECDH-ES+A128KW, | for the |
14781478+ | | | | ECDH-ES+A192KW, | sender |
14791479+ | | | | ECDH-ES+A256KW | |
14801480+ +-----------+-------+----------+-------------------+-------------+
14811481+ | static | -2 | COSE_Key | ECDH-SS+HKDF-256, | Static |
14821482+ | key | | | ECDH-SS+HKDF-512, | public key |
14831483+ | | | | ECDH-SS+A128KW, | for the |
14841484+ | | | | ECDH-SS+A192KW, | sender |
14851485+ | | | | ECDH-SS+A256KW | |
14861486+ +-----------+-------+----------+-------------------+-------------+
14871487+ | static | -3 | bstr | ECDH-SS+HKDF-256, | Static |
14881488+ | key id | | | ECDH-SS+HKDF-512, | public key |
14891489+ | | | | ECDH-SS+A128KW, | identifier |
14901490+ | | | | ECDH-SS+A192KW, | for the |
14911491+ | | | | ECDH-SS+A256KW | sender |
14921492+ +-----------+-------+----------+-------------------+-------------+
14931493+14941494+ Table 15: ECDH Algorithm Parameters
14951495+14961496+ This document defines these algorithms to be used with the curves
14971497+ P-256, P-384, P-521, X25519, and X448. Implementations MUST verify
14981498+ that the key type and curve are correct. Different curves are
14991499+ restricted to different key types. Implementations MUST verify that
15001500+ the curve and algorithm are appropriate for the entities involved.
15011501+15021502+ When using a COSE key for this algorithm, the following checks are
15031503+ made:
15041504+15051505+ * The "kty" field MUST be present, and it MUST be "EC2" or "OKP".
15061506+15071507+ * If the "alg" field is present, it MUST match the key agreement
15081508+ algorithm being used.
15091509+15101510+ * If the "key_ops" field is present, it MUST include "derive key" or
15111511+ "derive bits" for the private key.
15121512+15131513+ * If the "key_ops" field is present, it MUST be empty for the public
15141514+ key.
15151515+15161516+6.3.1.1. Security Considerations for ECDH
15171517+15181518+ There is a method of checking that points provided from external
15191519+ entities are valid. For the "EC2" key format, this can be done by
15201520+ checking that the x and y values form a point on the curve. For the
15211521+ "OKP" format, there is no simple way to perform point validation.
15221522+15231523+ Consideration was given to requiring that the public keys of both
15241524+ entities be provided as part of the key derivation process (as
15251525+ recommended in Section 6.1 of [RFC7748]). This was not done, because
15261526+ COSE is used in a store-and-forward format rather than in online key
15271527+ exchange. In order for this to be a problem, either the receiver
15281528+ public key has to be chosen maliciously or the sender has to be
15291529+ malicious. In either case, all security evaporates anyway.
15301530+15311531+ A proof of possession of the private key associated with the public
15321532+ key is recommended when a key is moved from untrusted to trusted
15331533+ (either by the end user or by the entity that is responsible for
15341534+ making trust statements on keys).
15351535+15361536+6.4. Key Agreement with Key Wrap
15371537+15381538+ Key Agreement with Key Wrap is defined in Section 8.5.5 of [RFC9052].
15391539+ Information about how to fill in the COSE_Recipient structure is
15401540+ detailed there.
15411541+15421542+6.4.1. ECDH with Key Wrap
15431543+15441544+ These algorithms are defined in Table 16.
15451545+15461546+ ECDH with Key Agreement is parameterized by the same header
15471547+ parameters as for ECDH; see Section 6.3.1, with the following
15481548+ modifications:
15491549+15501550+ Key Wrap Algorithm: Any of the key wrap algorithms defined in
15511551+ Section 6.2 are supported. The size of the key used for the key
15521552+ wrap algorithm is fed into the KDF. The set of identifiers is
15531553+ found in Table 16.
15541554+15551555+ +=========+=====+=========+==================+========+=============+
15561556+ |Name |Value| KDF | Ephemeral-Static |Key Wrap|Description |
15571557+ +=========+=====+=========+==================+========+=============+
15581558+ |ECDH-ES +|-29 | HKDF -- | yes |A128KW |ECDH ES w/ |
15591559+ |A128KW | | SHA-256 | | |HKDF and AES |
15601560+ | | | | | |Key Wrap w/ |
15611561+ | | | | | |128-bit key |
15621562+ +---------+-----+---------+------------------+--------+-------------+
15631563+ |ECDH-ES +|-30 | HKDF -- | yes |A192KW |ECDH ES w/ |
15641564+ |A192KW | | SHA-256 | | |HKDF and AES |
15651565+ | | | | | |Key Wrap w/ |
15661566+ | | | | | |192-bit key |
15671567+ +---------+-----+---------+------------------+--------+-------------+
15681568+ |ECDH-ES +|-31 | HKDF -- | yes |A256KW |ECDH ES w/ |
15691569+ |A256KW | | SHA-256 | | |HKDF and AES |
15701570+ | | | | | |Key Wrap w/ |
15711571+ | | | | | |256-bit key |
15721572+ +---------+-----+---------+------------------+--------+-------------+
15731573+ |ECDH-SS +|-32 | HKDF -- | no |A128KW |ECDH SS w/ |
15741574+ |A128KW | | SHA-256 | | |HKDF and AES |
15751575+ | | | | | |Key Wrap w/ |
15761576+ | | | | | |128-bit key |
15771577+ +---------+-----+---------+------------------+--------+-------------+
15781578+ |ECDH-SS +|-33 | HKDF -- | no |A192KW |ECDH SS w/ |
15791579+ |A192KW | | SHA-256 | | |HKDF and AES |
15801580+ | | | | | |Key Wrap w/ |
15811581+ | | | | | |192-bit key |
15821582+ +---------+-----+---------+------------------+--------+-------------+
15831583+ |ECDH-SS +|-34 | HKDF -- | no |A256KW |ECDH SS w/ |
15841584+ |A256KW | | SHA-256 | | |HKDF and AES |
15851585+ | | | | | |Key Wrap w/ |
15861586+ | | | | | |256-bit key |
15871587+ +---------+-----+---------+------------------+--------+-------------+
15881588+15891589+ Table 16: ECDH Algorithm Values with Key Wrap
15901590+15911591+ When using a COSE key for this algorithm, the following checks are
15921592+ made:
15931593+15941594+ * The "kty" field MUST be present, and it MUST be "EC2" or "OKP".
15951595+15961596+ * If the "alg" field is present, it MUST match the key agreement
15971597+ algorithm being used.
15981598+15991599+ * If the "key_ops" field is present, it MUST include "derive key" or
16001600+ "derive bits" for the private key.
16011601+16021602+ * If the "key_ops" field is present, it MUST be empty for the public
16031603+ key.
16041604+16051605+7. Key Object Parameters
16061606+16071607+ The COSE_Key object defines a way to hold a single key object. It is
16081608+ still required that the members of individual key types be defined.
16091609+ This section of the document is where we define an initial set of
16101610+ members for specific key types.
16111611+16121612+ For each of the key types, we define both public and private members.
16131613+ The public members are what is transmitted to others for their usage.
16141614+ Private members allow individuals to archive keys. However, there
16151615+ are some circumstances in which private keys may be distributed to
16161616+ entities in a protocol. Examples include: entities that have poor
16171617+ random number generation, centralized key creation for multicast-type
16181618+ operations, and protocols in which a shared secret is used as a
16191619+ bearer token for authorization purposes.
16201620+16211621+ Key types are identified by the "kty" member of the COSE_Key object.
16221622+ In this document, we define four values for the member:
16231623+16241624+ +===========+=======+==========================+
16251625+ | Name | Value | Description |
16261626+ +===========+=======+==========================+
16271627+ | OKP | 1 | Octet Key Pair |
16281628+ +-----------+-------+--------------------------+
16291629+ | EC2 | 2 | Elliptic Curve Keys w/ |
16301630+ | | | x- and y-coordinate pair |
16311631+ +-----------+-------+--------------------------+
16321632+ | Symmetric | 4 | Symmetric Keys |
16331633+ +-----------+-------+--------------------------+
16341634+ | Reserved | 0 | This value is reserved |
16351635+ +-----------+-------+--------------------------+
16361636+16371637+ Table 17: Key Type Values
16381638+16391639+7.1. Elliptic Curve Keys
16401640+16411641+ Two different key structures are defined for elliptic curve keys.
16421642+ One version uses both an x-coordinate and a y-coordinate, potentially
16431643+ with point compression ("EC2"). This is the conventional elliptic
16441644+ curve (EC) point representation that is used in [RFC5480]. The other
16451645+ version uses only the x-coordinate, as the y-coordinate is either to
16461646+ be recomputed or not needed for the key agreement operation ("OKP").
16471647+16481648+ Applications MUST check that the curve and the key type are
16491649+ consistent and reject a key if they are not.
16501650+16511651+ +=========+=======+==========+=====================================+
16521652+ | Name | Value | Key Type | Description |
16531653+ +=========+=======+==========+=====================================+
16541654+ | P-256 | 1 | EC2 | NIST P-256, also known as secp256r1 |
16551655+ +---------+-------+----------+-------------------------------------+
16561656+ | P-384 | 2 | EC2 | NIST P-384, also known as secp384r1 |
16571657+ +---------+-------+----------+-------------------------------------+
16581658+ | P-521 | 3 | EC2 | NIST P-521, also known as secp521r1 |
16591659+ +---------+-------+----------+-------------------------------------+
16601660+ | X25519 | 4 | OKP | X25519 for use w/ ECDH only |
16611661+ +---------+-------+----------+-------------------------------------+
16621662+ | X448 | 5 | OKP | X448 for use w/ ECDH only |
16631663+ +---------+-------+----------+-------------------------------------+
16641664+ | Ed25519 | 6 | OKP | Ed25519 for use w/ EdDSA only |
16651665+ +---------+-------+----------+-------------------------------------+
16661666+ | Ed448 | 7 | OKP | Ed448 for use w/ EdDSA only |
16671667+ +---------+-------+----------+-------------------------------------+
16681668+16691669+ Table 18: Elliptic Curves
16701670+16711671+7.1.1. Double Coordinate Curves
16721672+16731673+ Generally, protocols transmit elliptic-curve points as either the
16741674+ x-coordinate and y-coordinate or the x-coordinate and a sign bit for
16751675+ the y-coordinate. The latter encoding has not been recommended by
16761676+ the IETF due to potential IPR issues. However, for operations in
16771677+ constrained environments, the ability to shrink a message by not
16781678+ sending the y-coordinate is potentially useful.
16791679+16801680+ For EC keys with both coordinates, the "kty" member is set to 2
16811681+ (EC2). The key parameters defined in this section are summarized in
16821682+ Table 19. The members that are defined for this key type are:
16831683+16841684+ crv: This contains an identifier of the curve to be used with the
16851685+ key. The curves defined in this document for this key type can
16861686+ be found in Table 18. Other curves may be registered in the
16871687+ future, and private curves can be used as well.
16881688+16891689+ x: This contains the x-coordinate for the EC point. The integer
16901690+ is converted to a byte string as defined in [SEC1]. Leading-
16911691+ zero octets MUST be preserved.
16921692+16931693+ y: This contains either the sign bit or the value of the
16941694+ y-coordinate for the EC point. When encoding the value y, the
16951695+ integer is converted to a byte string (as defined in [SEC1])
16961696+ and encoded as a CBOR bstr. Leading-zero octets MUST be
16971697+ preserved. Compressed point encoding is also supported.
16981698+ Compute the sign bit as laid out in the Elliptic-Curve-Point-
16991699+ to-Octet-String Conversion function of [SEC1]. If the sign bit
17001700+ is zero, then encode y as a CBOR false value; otherwise, encode
17011701+ y as a CBOR true value. The encoding of the infinity point is
17021702+ not supported.
17031703+17041704+ d: This contains the private key.
17051705+17061706+ For public keys, it is REQUIRED that "crv", "x", and "y" be present
17071707+ in the structure. For private keys, it is REQUIRED that "crv" and
17081708+ "d" be present in the structure. For private keys, it is RECOMMENDED
17091709+ that "x" and "y" also be present, but they can be recomputed from the
17101710+ required elements, and omitting them saves on space.
17111711+17121712+ +======+======+=======+========+=================================+
17131713+ | Key | Name | Label | CBOR | Description |
17141714+ | Type | | | Type | |
17151715+ +======+======+=======+========+=================================+
17161716+ | 2 | crv | -1 | int / | EC identifier -- Taken from the |
17171717+ | | | | tstr | "COSE Elliptic Curves" registry |
17181718+ +------+------+-------+--------+---------------------------------+
17191719+ | 2 | x | -2 | bstr | x-coordinate |
17201720+ +------+------+-------+--------+---------------------------------+
17211721+ | 2 | y | -3 | bstr / | y-coordinate |
17221722+ | | | | bool | |
17231723+ +------+------+-------+--------+---------------------------------+
17241724+ | 2 | d | -4 | bstr | Private key |
17251725+ +------+------+-------+--------+---------------------------------+
17261726+17271727+ Table 19: EC Key Parameters
17281728+17291729+7.2. Octet Key Pair
17301730+17311731+ A new key type is defined for Octet Key Pairs (OKPs). Do not assume
17321732+ that keys using this type are elliptic curves. This key type could
17331733+ be used for other curve types (for example, mathematics based on
17341734+ hyper-elliptic surfaces).
17351735+17361736+ The key parameters defined in this section are summarized in
17371737+ Table 20. The members that are defined for this key type are:
17381738+17391739+ crv: This contains an identifier of the curve to be used with the
17401740+ key. The curves defined in this document for this key type can
17411741+ be found in Table 18. Other curves may be registered in the
17421742+ future, and private curves can be used as well.
17431743+17441744+ x: This contains the public key. The byte string contains the
17451745+ public key as defined by the algorithm. (For X25519,
17461746+ internally it is a little-endian integer.)
17471747+17481748+ d: This contains the private key.
17491749+17501750+ For public keys, it is REQUIRED that "crv" and "x" be present in the
17511751+ structure. For private keys, it is REQUIRED that "crv" and "d" be
17521752+ present in the structure. For private keys, it is RECOMMENDED that
17531753+ "x" also be present, but it can be recomputed from the required
17541754+ elements, and omitting it saves on space.
17551755+17561756+ +======+==========+=======+=======+=================================+
17571757+ | Name | Key | Label | Type | Description |
17581758+ | | Type | | | |
17591759+ +======+==========+=======+=======+=================================+
17601760+ | crv | 1 | -1 | int / | EC identifier -- Taken from the |
17611761+ | | | | tstr | "COSE Elliptic Curves" registry |
17621762+ +------+----------+-------+-------+---------------------------------+
17631763+ | x | 1 | -2 | bstr | Public Key |
17641764+ +------+----------+-------+-------+---------------------------------+
17651765+ | d | 1 | -4 | bstr | Private key |
17661766+ +------+----------+-------+-------+---------------------------------+
17671767+17681768+ Table 20: Octet Key Pair Parameters
17691769+17701770+7.3. Symmetric Keys
17711771+17721772+ Occasionally, it is required that a symmetric key be transported
17731773+ between entities. This key structure allows for that to happen.
17741774+17751775+ For symmetric keys, the "kty" member is set to 4 ("Symmetric"). The
17761776+ member that is defined for this key type is:
17771777+17781778+ k: This contains the value of the key.
17791779+17801780+ This key structure does not have a form that contains only public
17811781+ members. As it is expected that this key structure is going to be
17821782+ transmitted, care must be taken that it is never transmitted
17831783+ accidentally or insecurely. For symmetric keys, it is REQUIRED that
17841784+ "k" be present in the structure.
17851785+17861786+ +======+==========+=======+======+=============+
17871787+ | Name | Key Type | Label | Type | Description |
17881788+ +======+==========+=======+======+=============+
17891789+ | k | 4 | -1 | bstr | Key Value |
17901790+ +------+----------+-------+------+-------------+
17911791+17921792+ Table 21: Symmetric Key Parameters
17931793+17941794+8. COSE Capabilities
17951795+17961796+ The capabilities of an algorithm or key type need to be specified in
17971797+ some situations. This has a counterpart in the S/MIME
17981798+ specifications, where SMIMECapabilities is defined in Section 2.5.2
17991799+ of [RFC8551]. This document defines the same concept for COSE.
18001800+18011801+ The algorithm identifier is not included in the capabilities data, as
18021802+ it should be encoded elsewhere in the message. The key type
18031803+ identifier is included in the capabilities data, as it is not
18041804+ expected to be encoded elsewhere.
18051805+18061806+ Two different types of capabilities are defined: capabilities for
18071807+ algorithms and capabilities for key type. Once defined by
18081808+ registration with IANA, the list of capabilities for an algorithm or
18091809+ key type is immutable. If it is later found that a new capability is
18101810+ needed for a key type or algorithm, it will require that a new code
18111811+ point be assigned to deal with that. As a general rule, the
18121812+ capabilities are going to map to algorithm-specific header parameters
18131813+ or key parameters, but they do not need to do so. An example of this
18141814+ is the HSS-LMS key type capabilities defined below, where the hash
18151815+ algorithm used is included.
18161816+18171817+ The capability structure is an array of values; the values included
18181818+ in the structure are dependent on a specific algorithm or key type.
18191819+ For algorithm capabilities, the first element should always be a key
18201820+ type value if applicable, but the items that are specific to a key
18211821+ (for example, a curve) should not be included in the algorithm
18221822+ capabilities. This means that if one wishes to enumerate all of the
18231823+ capabilities for a device that implements ECDH, it requires that all
18241824+ of the combinations of algorithms and key pairs be specified. The
18251825+ last example of Section 8.3 provides a case where this is done by
18261826+ allowing for a cross product to be specified between an array of
18271827+ algorithm capabilities and key type capabilities (see the ECDH-
18281828+ ES+A25KW element). For a key, the first element should be the key
18291829+ type value. While this means that the key type value will be
18301830+ duplicated if both an algorithm and key capability are used, the key
18311831+ type is needed in order to understand the rest of the values.
18321832+18331833+8.1. Assignments for Existing Algorithms
18341834+18351835+ For the current set of algorithms in the registry other than IV-
18361836+ GENERATION (those in this document as well as those in [RFC8230],
18371837+ [RFC8778], and [RFC9021]), the capabilities list is an array with one
18381838+ element, the key type (from the "COSE Key Types" Registry). It is
18391839+ expected that future registered algorithms could have zero, one, or
18401840+ multiple elements.
18411841+18421842+8.2. Assignments for Existing Key Types
18431843+18441844+ There are a number of pre-existing key types; the following deals
18451845+ with creating the capability definition for those structures:
18461846+18471847+ * OKP, EC2: The list of capabilities is:
18481848+18491849+ - The key type value. (1 for OKP or 2 for EC2.)
18501850+18511851+ - One curve for that key type from the "COSE Elliptic Curves"
18521852+ registry.
18531853+18541854+ * RSA: The list of capabilities is:
18551855+18561856+ - The key type value (3).
18571857+18581858+ * Symmetric: The list of capabilities is:
18591859+18601860+ - The key type value (4).
18611861+18621862+ * HSS-LMS: The list of capabilities is:
18631863+18641864+ - The key type value (5).
18651865+18661866+ - Algorithm identifier for the underlying hash function from the
18671867+ "COSE Algorithms" registry.
18681868+18691869+ * WalnutDSA: The list of capabilities is:
18701870+18711871+ - The key type value (6).
18721872+18731873+ - The N value (group and matrix size) for the key, a uint.
18741874+18751875+ - The q value (finite field order) for the key, a uint.
18761876+18771877+8.3. Examples
18781878+18791879+ Capabilities can be used in a key derivation process to make sure
18801880+ that both sides are using the same parameters. The three examples
18811881+ below show different ways that one might utilize parameters in
18821882+ specifying an application protocol:
18831883+18841884+ * Only an algorithm capability: This is useful if the protocol wants
18851885+ to require a specific algorithm, such as ES256, but it is agnostic
18861886+ about which curve is being used. This requires that the algorithm
18871887+ identifier be specified in the protocol. See the first example.
18881888+18891889+ * Only a key type capability: This is useful if the protocol wants
18901890+ to require a specific key type and curve, such as P-256, but will
18911891+ accept any algorithm using that curve (e.g., both ECDSA and ECDH).
18921892+ See the second example.
18931893+18941894+ * Both algorithm and key type capabilities: This is used if the
18951895+ protocol needs to nail down all of the options surrounding an
18961896+ algorithm -- e.g., EdDSA with the curve Ed25519. As with the
18971897+ first example, the algorithm identifier needs to be specified in
18981898+ the protocol. See the third example, which just concatenates the
18991899+ two capabilities together.
19001900+19011901+ Algorithm ES256
19021902+19031903+ 0x8102 / [2 \ EC2 \ ] /
19041904+19051905+ Key type EC2 with P-256 curve:
19061906+19071907+ 0x820201 / [2 \ EC2 \, 1 \ P-256 \] /
19081908+19091909+ ECDH-ES + A256KW with an X25519 curve:
19101910+19111911+ 0x8101820104 / [1 \ OKP \],[1 \ OKP \, 4 \ X25519 \] /
19121912+19131913+ The capabilities can also be used by an entity to advertise what it
19141914+ is capable of doing. The decoded example below is one of many
19151915+ encodings that could be used for that purpose. Each array element
19161916+ includes three fields: the algorithm identifier, one or more
19171917+ algorithm capabilities, and one or more key type capabilities.
19181918+19191919+ [
19201920+ [-8 / EdDSA /,
19211921+ [1 / OKP key type /],
19221922+ [
19231923+ [1 / OKP /, 6 / Ed25519 / ],
19241924+ [1 /OKP/, 7 /Ed448 /]
19251925+ ]
19261926+ ],
19271927+ [-7 / ECDSA with SHA-256/,
19281928+ [2 /EC2 key type/],
19291929+ [
19301930+ [2 /EC2/, 1 /P-256/],
19311931+ [2 /EC2/, 3 /P-521/]
19321932+ ]
19331933+ ],
19341934+ [ -31 / ECDH-ES+A256KW/,
19351935+ [
19361936+ [ 2 /EC2/],
19371937+ [1 /OKP/ ]
19381938+ ],
19391939+ [
19401940+ [2 /EC2/, 1 /P-256/],
19411941+ [1 /OKP/, 4 / X25519/ ]
19421942+ ]
19431943+ ],
19441944+ [ 1 / A128GCM /,
19451945+ [ 4 / Symmetric / ],
19461946+ [ 4 / Symmetric /]
19471947+ ]
19481948+ ]
19491949+19501950+ Examining the above:
19511951+19521952+ * The first element indicates that the entity supports EdDSA with
19531953+ curves Ed25519 and Ed448.
19541954+19551955+ * The second element indicates that the entity supports ECDSA with
19561956+ SHA-256 with curves P-256 and P-521.
19571957+19581958+ * The third element indicates that the entity supports Ephemeral-
19591959+ Static ECDH using AES256 key wrap. The entity can support the
19601960+ P-256 curve with an EC2 key type and the X25519 curve with an OKP
19611961+ key type.
19621962+19631963+ * The last element indicates that the entity supports AES-GCM of 128
19641964+ bits for content encryption.
19651965+19661966+ The entity does not advertise that it supports any MAC algorithms.
19671967+19681968+9. CBOR Encoding Restrictions
19691969+19701970+ This document limits the restrictions it imposes on how the CBOR
19711971+ Encoder needs to work. The new encoding restrictions are aligned
19721972+ with the Core Deterministic Encoding Requirements specified in
19731973+ Section 4.2.1 of RFC 8949 [STD94]. It has been narrowed down to the
19741974+ following restrictions:
19751975+19761976+ * The restriction applies to the encoding of the COSE_KDF_Context.
19771977+19781978+ * Encoding MUST be done using definite lengths, and the length of
19791979+ the (encoded) argument MUST be the minimum possible length. This
19801980+ means that the integer 1 is encoded as "0x01" and not "0x1801".
19811981+19821982+ * Applications MUST NOT generate messages with the same label used
19831983+ twice as a key in a single map. Applications MUST NOT parse and
19841984+ process messages with the same label used twice as a key in a
19851985+ single map. Applications can enforce the parse-and-process
19861986+ requirement by using parsers that will fail the parse step or by
19871987+ using parsers that will pass all keys to the application, and the
19881988+ application can perform the check for duplicate keys.
19891989+19901990+10. IANA Considerations
19911991+19921992+ IANA has updated all COSE registries except for "COSE Header
19931993+ Parameters" and "COSE Key Common Parameters" to point to this
19941994+ document instead of [RFC8152].
19951995+19961996+10.1. Changes to the "COSE Key Types" Registry
19971997+19981998+ IANA has added a new column in the "COSE Key Types" registry. The
19991999+ new column is labeled "Capabilities" and has been populated according
20002000+ to the entries in Table 22.
20012001+20022002+ +=======+===========+============================+
20032003+ | Value | Name | Capabilities |
20042004+ +=======+===========+============================+
20052005+ | 1 | OKP | [kty(1), crv] |
20062006+ +-------+-----------+----------------------------+
20072007+ | 2 | EC2 | [kty(2), crv] |
20082008+ +-------+-----------+----------------------------+
20092009+ | 3 | RSA | [kty(3)] |
20102010+ +-------+-----------+----------------------------+
20112011+ | 4 | Symmetric | [kty(4)] |
20122012+ +-------+-----------+----------------------------+
20132013+ | 5 | HSS-LMS | [kty(5), hash algorithm] |
20142014+ +-------+-----------+----------------------------+
20152015+ | 6 | WalnutDSA | [kty(6), N value, q value] |
20162016+ +-------+-----------+----------------------------+
20172017+20182018+ Table 22: Key Type Capabilities
20192019+20202020+10.2. Changes to the "COSE Algorithms" Registry
20212021+20222022+ IANA has added a new column in the "COSE Algorithms" registry. The
20232023+ new column is labeled "Capabilities" and has been populated with
20242024+ "[kty]" for all current, nonprovisional registrations.
20252025+20262026+ IANA has updated the Reference column in the "COSE Algorithms"
20272027+ registry to include this document as a reference for all rows where
20282028+ it was not already present.
20292029+20302030+ IANA has added a new row to the "COSE Algorithms" registry.
20312031+20322032+ +===============+=======+===============+===========+=============+
20332033+ | Name | Value | Description | Reference | Recommended |
20342034+ +===============+=======+===============+===========+=============+
20352035+ | IV-GENERATION | 34 | For doing IV | RFC 9053 | No |
20362036+ | | | generation | | |
20372037+ | | | for symmetric | | |
20382038+ | | | algorithms. | | |
20392039+ +---------------+-------+---------------+-----------+-------------+
20402040+20412041+ Table 23: New entry in the COSE Algorithms registry
20422042+20432043+ The Capabilities column for this registration is to be empty.
20442044+20452045+10.3. Changes to the "COSE Key Type Parameters" Registry
20462046+20472047+ IANA has modified the description to "Public Key" for the line with
20482048+ "Key Type" of 1 and the "Name" of "x". See Table 20, which has been
20492049+ modified with this change.
20502050+20512051+10.4. Expert Review Instructions
20522052+20532053+ All of the IANA registries established by [RFC8152] are, at least in
20542054+ part, defined as Expert Review [RFC8126]. This section gives some
20552055+ general guidelines for what the experts should be looking for, but
20562056+ they are being designated as experts for a reason, so they should be
20572057+ given substantial latitude.
20582058+20592059+ Expert reviewers should take the following into consideration:
20602060+20612061+ * Point squatting should be discouraged. Reviewers are encouraged
20622062+ to get sufficient information for registration requests to ensure
20632063+ that the usage is not going to duplicate an existing registration
20642064+ and that the code point is likely to be used in deployments. The
20652065+ ranges tagged as private use are intended for testing purposes and
20662066+ closed environments; code points in other ranges should not be
20672067+ assigned for testing.
20682068+20692069+ * Standards Track or BCP RFCs are required to register a code point
20702070+ in the Standards Action range. Specifications should exist for
20712071+ Specification Required ranges, but early assignment before an RFC
20722072+ is available is considered to be permissible. Specifications are
20732073+ needed for the first-come, first-served range if the points are
20742074+ expected to be used outside of closed environments in an
20752075+ interoperable way. When specifications are not provided, the
20762076+ description provided needs to have sufficient information to
20772077+ identify what the point is being used for.
20782078+20792079+ * Experts should take into account the expected usage of fields when
20802080+ approving code point assignment. The fact that the Standards
20812081+ Action range is only available to Standards Track documents does
20822082+ not mean that a Standards Track document cannot have points
20832083+ assigned outside of that range. The length of the encoded value
20842084+ should be weighed against how many code points of that length are
20852085+ left and the size of device it will be used on.
20862086+20872087+ * When algorithms are registered, vanity registrations should be
20882088+ discouraged. One way to do this is to require registrations to
20892089+ provide additional documentation on security analysis of the
20902090+ algorithm. Another thing that should be considered is requesting
20912091+ an opinion on the algorithm from the Crypto Forum Research Group
20922092+ (CFRG). Algorithms are expected to meet the security requirements
20932093+ of the community and the requirements of the message structures in
20942094+ order to be suitable for registration.
20952095+20962096+11. Security Considerations
20972097+20982098+ There are a number of security considerations that need to be taken
20992099+ into account by implementers of this specification. The security
21002100+ considerations that are specific to an individual algorithm are
21012101+ placed next to the description of the algorithm. While some
21022102+ considerations have been highlighted here, additional considerations
21032103+ may be found in the documents listed in the references.
21042104+21052105+ Implementations need to protect the private key material for all
21062106+ individuals. Some cases in this document need to be highlighted with
21072107+ regard to this issue.
21082108+21092109+ * Use of the same key for two different algorithms can leak
21102110+ information about the key. It is therefore recommended that keys
21112111+ be restricted to a single algorithm.
21122112+21132113+ * Use of "direct" as a recipient algorithm combined with a second
21142114+ recipient algorithm exposes the direct key to the second
21152115+ recipient; Section 8.5 of [RFC9052] forbids combining "direct"
21162116+ recipient algorithms with other modes.
21172117+21182118+ * Several of the algorithms in this document have limits on the
21192119+ number of times that a key can be used without leaking information
21202120+ about the key.
21212121+21222122+ The use of ECDH and direct plus KDF (with no key wrap) will not
21232123+ directly lead to the private key being leaked; the one-way function
21242124+ of the KDF will prevent that. There is, however, a different issue
21252125+ that needs to be addressed. Having two recipients requires that the
21262126+ CEK be shared between two recipients. The second recipient therefore
21272127+ has a CEK that was derived from material that can be used for the
21282128+ weak proof of origin. The second recipient could create a message
21292129+ using the same CEK and send it to the first recipient; the first
21302130+ recipient would, for either Static-Static ECDH or direct plus KDF,
21312131+ make an assumption that the CEK could be used for proof of origin,
21322132+ even though it is from the wrong entity. If the key wrap step is
21332133+ added, then no proof of origin is implied and this is not an issue.
21342134+21352135+ Although it has been mentioned before, it bears repeating that the
21362136+ use of a single key for multiple algorithms has been demonstrated in
21372137+ some cases to leak information about a key, providing the opportunity
21382138+ for attackers to forge integrity tags or gain information about
21392139+ encrypted content. Binding a key to a single algorithm prevents
21402140+ these problems. Key creators and key consumers are strongly
21412141+ encouraged to not only create new keys for each different algorithm,
21422142+ but to include that selection of algorithm in any distribution of key
21432143+ material and strictly enforce the matching of algorithms in the key
21442144+ structure to algorithms in the message structure. In addition to
21452145+ checking that algorithms are correct, the key form needs to be
21462146+ checked as well. Do not use an "EC2" key where an "OKP" key is
21472147+ expected.
21482148+21492149+ Before using a key for transmission, or before acting on information
21502150+ received, a trust decision on a key needs to be made. Is the data or
21512151+ action something that the entity associated with the key has a right
21522152+ to see or a right to request? A number of factors are associated
21532153+ with this trust decision. Some highlighted here are:
21542154+21552155+ * What are the permissions associated with the key owner?
21562156+21572157+ * Is the cryptographic algorithm acceptable in the current context?
21582158+21592159+ * Have the restrictions associated with the key, such as algorithm
21602160+ or freshness, been checked, and are they correct?
21612161+21622162+ * Is the request something that is reasonable, given the current
21632163+ state of the application?
21642164+21652165+ * Have any security considerations that are part of the message been
21662166+ enforced (as specified by the application or "crit" header
21672167+ parameter)?
21682168+21692169+ There are a large number of algorithms presented in this document
21702170+ that use nonce values. For all of the nonces defined in this
21712171+ document, there is some type of restriction on the nonce being a
21722172+ unique value for either a key or some other conditions. In all of
21732173+ these cases, there is no known requirement on the nonce being both
21742174+ unique and unpredictable; under these circumstances, it's reasonable
21752175+ to use a counter for creation of the nonce. In cases where one wants
21762176+ the pattern of the nonce to be unpredictable as well as unique, one
21772177+ can use a key created for that purpose and encrypt the counter to
21782178+ produce the nonce value.
21792179+21802180+ One area that has been getting exposure is traffic analysis of
21812181+ encrypted messages based on the length of the message. This
21822182+ specification does not provide a uniform method for providing padding
21832183+ as part of the message structure. An observer can distinguish
21842184+ between two different messages (for example, "YES" and "NO") based on
21852185+ the length for all of the content encryption algorithms that are
21862186+ defined in this document. This means that it is up to the
21872187+ applications to document how content padding is to be done in order
21882188+ to prevent or discourage such analysis. (For example, the text
21892189+ strings could be defined as "YES" and "NO ".)
21902190+21912191+ The analysis done in [RFC9147] is based on the number of records that
21922192+ are sent. This should map well to the number of messages sent when
21932193+ using COSE, so that analysis should hold here as well, under the
21942194+ assumption that the COSE messages are roughly the same size as DTLS
21952195+ records. It needs to be noted that the limits are based on the
21962196+ number of messages, but QUIC and DTLS are always pairwise-based
21972197+ endpoints. In contrast, [OSCORE-GROUPCOMM] uses COSE in a group
21982198+ communication scenario. Under these circumstances, it may be that no
21992199+ one single entity will see all of the messages that are encrypted,
22002200+ and therefore no single entity can trigger the rekey operation.
22012201+22022202+12. References
22032203+22042204+12.1. Normative References
22052205+22062206+ [AES-GCM] Dworkin, M., "Recommendation for Block Cipher Modes of
22072207+ Operation: Galois/Counter Mode (GCM) and GMAC", NIST
22082208+ Special Publication 800-38D, DOI 10.6028/NIST.SP.800-38D,
22092209+ November 2007, <https://csrc.nist.gov/publications/
22102210+ nistpubs/800-38D/SP-800-38D.pdf>.
22112211+22122212+ [DSS] National Institute of Standards and Technology, "Digital
22132213+ Signature Standard (DSS)", FIPS PUB 186-4,
22142214+ DOI 10.6028/NIST.FIPS.186-4, July 2013,
22152215+ <https://nvlpubs.nist.gov/nistpubs/FIPS/
22162216+ NIST.FIPS.186-4.pdf>.
22172217+22182218+ [MAC] Menezes, A., van Oorschot, P., and S. Vanstone, "Handbook
22192219+ of Applied Cryptography", CRC Press, Boca Raton, 1996,
22202220+ <https://cacr.uwaterloo.ca/hac/>.
22212221+22222222+ [RFC2104] Krawczyk, H., Bellare, M., and R. Canetti, "HMAC: Keyed-
22232223+ Hashing for Message Authentication", RFC 2104,
22242224+ DOI 10.17487/RFC2104, February 1997,
22252225+ <https://www.rfc-editor.org/info/rfc2104>.
22262226+22272227+ [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate
22282228+ Requirement Levels", BCP 14, RFC 2119,
22292229+ DOI 10.17487/RFC2119, March 1997,
22302230+ <https://www.rfc-editor.org/info/rfc2119>.
22312231+22322232+ [RFC3394] Schaad, J. and R. Housley, "Advanced Encryption Standard
22332233+ (AES) Key Wrap Algorithm", RFC 3394, DOI 10.17487/RFC3394,
22342234+ September 2002, <https://www.rfc-editor.org/info/rfc3394>.
22352235+22362236+ [RFC3610] Whiting, D., Housley, R., and N. Ferguson, "Counter with
22372237+ CBC-MAC (CCM)", RFC 3610, DOI 10.17487/RFC3610, September
22382238+ 2003, <https://www.rfc-editor.org/info/rfc3610>.
22392239+22402240+ [RFC5869] Krawczyk, H. and P. Eronen, "HMAC-based Extract-and-Expand
22412241+ Key Derivation Function (HKDF)", RFC 5869,
22422242+ DOI 10.17487/RFC5869, May 2010,
22432243+ <https://www.rfc-editor.org/info/rfc5869>.
22442244+22452245+ [RFC6090] McGrew, D., Igoe, K., and M. Salter, "Fundamental Elliptic
22462246+ Curve Cryptography Algorithms", RFC 6090,
22472247+ DOI 10.17487/RFC6090, February 2011,
22482248+ <https://www.rfc-editor.org/info/rfc6090>.
22492249+22502250+ [RFC6979] Pornin, T., "Deterministic Usage of the Digital Signature
22512251+ Algorithm (DSA) and Elliptic Curve Digital Signature
22522252+ Algorithm (ECDSA)", RFC 6979, DOI 10.17487/RFC6979, August
22532253+ 2013, <https://www.rfc-editor.org/info/rfc6979>.
22542254+22552255+ [RFC7748] Langley, A., Hamburg, M., and S. Turner, "Elliptic Curves
22562256+ for Security", RFC 7748, DOI 10.17487/RFC7748, January
22572257+ 2016, <https://www.rfc-editor.org/info/rfc7748>.
22582258+22592259+ [RFC8017] Moriarty, K., Ed., Kaliski, B., Jonsson, J., and A. Rusch,
22602260+ "PKCS #1: RSA Cryptography Specifications Version 2.2",
22612261+ RFC 8017, DOI 10.17487/RFC8017, November 2016,
22622262+ <https://www.rfc-editor.org/info/rfc8017>.
22632263+22642264+ [RFC8032] Josefsson, S. and I. Liusvaara, "Edwards-Curve Digital
22652265+ Signature Algorithm (EdDSA)", RFC 8032,
22662266+ DOI 10.17487/RFC8032, January 2017,
22672267+ <https://www.rfc-editor.org/info/rfc8032>.
22682268+22692269+ [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC
22702270+ 2119 Key Words", BCP 14, RFC 8174, DOI 10.17487/RFC8174,
22712271+ May 2017, <https://www.rfc-editor.org/info/rfc8174>.
22722272+22732273+ [RFC8439] Nir, Y. and A. Langley, "ChaCha20 and Poly1305 for IETF
22742274+ Protocols", RFC 8439, DOI 10.17487/RFC8439, June 2018,
22752275+ <https://www.rfc-editor.org/info/rfc8439>.
22762276+22772277+ [RFC9052] Schaad, J., "CBOR Object Signing and Encryption (COSE):
22782278+ Structures and Process", STD 96, RFC 9052,
22792279+ DOI 10.17487/RFC9052, August 2022,
22802280+ <https://www.rfc-editor.org/info/rfc9052>.
22812281+22822282+ [SEC1] Certicom Research, "SEC 1: Elliptic Curve Cryptography",
22832283+ Standards for Efficient Cryptography, May 2009,
22842284+ <https://www.secg.org/sec1-v2.pdf>.
22852285+22862286+ [STD94] Bormann, C. and P. Hoffman, "Concise Binary Object
22872287+ Representation (CBOR)", STD 94, RFC 8949, December 2020,
22882288+ <https://www.rfc-editor.org/info/std94>.
22892289+22902290+12.2. Informative References
22912291+22922292+ [CFRG-DET-SIGS]
22932293+ Mattsson, J. P., Thormarker, E., and S. Ruohomaa,
22942294+ "Deterministic ECDSA and EdDSA Signatures with Additional
22952295+ Randomness", Work in Progress, Internet-Draft, draft-
22962296+ mattsson-cfrg-det-sigs-with-noise-04, 15 February 2022,
22972297+ <https://datatracker.ietf.org/doc/html/draft-mattsson-
22982298+ cfrg-det-sigs-with-noise-04>.
22992299+23002300+ [COUNTERSIGN]
23012301+ Schaad, J. and R. Housley, "CBOR Object Signing and
23022302+ Encryption (COSE): Countersignatures", Work in Progress,
23032303+ Internet-Draft, draft-ietf-cose-countersign-08, 22 August
23042304+ 2022, <https://datatracker.ietf.org/doc/html/draft-ietf-
23052305+ cose-countersign-08>.
23062306+23072307+ [GitHub-Examples]
23082308+ "GitHub Examples of COSE", commit 3221310, 3 June 2020,
23092309+ <https://github.com/cose-wg/Examples>.
23102310+23112311+ [HKDF] Krawczyk, H., "Cryptographic Extraction and Key
23122312+ Derivation: The HKDF Scheme", 2010,
23132313+ <https://eprint.iacr.org/2010/264.pdf>.
23142314+23152315+ [OSCORE-GROUPCOMM]
23162316+ Tiloca, M., Selander, G., Palombini, F., Mattsson, J. P.,
23172317+ and J. Park, "Group OSCORE - Secure Group Communication
23182318+ for CoAP", Work in Progress, Internet-Draft, draft-ietf-
23192319+ core-oscore-groupcomm-14, 7 March 2022,
23202320+ <https://datatracker.ietf.org/doc/html/draft-ietf-core-
23212321+ oscore-groupcomm-14>.
23222322+23232323+ [RFC4231] Nystrom, M., "Identifiers and Test Vectors for HMAC-SHA-
23242324+ 224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512",
23252325+ RFC 4231, DOI 10.17487/RFC4231, December 2005,
23262326+ <https://www.rfc-editor.org/info/rfc4231>.
23272327+23282328+ [RFC4493] Song, JH., Poovendran, R., Lee, J., and T. Iwata, "The
23292329+ AES-CMAC Algorithm", RFC 4493, DOI 10.17487/RFC4493, June
23302330+ 2006, <https://www.rfc-editor.org/info/rfc4493>.
23312331+23322332+ [RFC5116] McGrew, D., "An Interface and Algorithms for Authenticated
23332333+ Encryption", RFC 5116, DOI 10.17487/RFC5116, January 2008,
23342334+ <https://www.rfc-editor.org/info/rfc5116>.
23352335+23362336+ [RFC5480] Turner, S., Brown, D., Yiu, K., Housley, R., and T. Polk,
23372337+ "Elliptic Curve Cryptography Subject Public Key
23382338+ Information", RFC 5480, DOI 10.17487/RFC5480, March 2009,
23392339+ <https://www.rfc-editor.org/info/rfc5480>.
23402340+23412341+ [RFC6151] Turner, S. and L. Chen, "Updated Security Considerations
23422342+ for the MD5 Message-Digest and the HMAC-MD5 Algorithms",
23432343+ RFC 6151, DOI 10.17487/RFC6151, March 2011,
23442344+ <https://www.rfc-editor.org/info/rfc6151>.
23452345+23462346+ [RFC7252] Shelby, Z., Hartke, K., and C. Bormann, "The Constrained
23472347+ Application Protocol (CoAP)", RFC 7252,
23482348+ DOI 10.17487/RFC7252, June 2014,
23492349+ <https://www.rfc-editor.org/info/rfc7252>.
23502350+23512351+ [RFC7518] Jones, M., "JSON Web Algorithms (JWA)", RFC 7518,
23522352+ DOI 10.17487/RFC7518, May 2015,
23532353+ <https://www.rfc-editor.org/info/rfc7518>.
23542354+23552355+ [RFC8126] Cotton, M., Leiba, B., and T. Narten, "Guidelines for
23562356+ Writing an IANA Considerations Section in RFCs", BCP 26,
23572357+ RFC 8126, DOI 10.17487/RFC8126, June 2017,
23582358+ <https://www.rfc-editor.org/info/rfc8126>.
23592359+23602360+ [RFC8152] Schaad, J., "CBOR Object Signing and Encryption (COSE)",
23612361+ RFC 8152, DOI 10.17487/RFC8152, July 2017,
23622362+ <https://www.rfc-editor.org/info/rfc8152>.
23632363+23642364+ [RFC8230] Jones, M., "Using RSA Algorithms with CBOR Object Signing
23652365+ and Encryption (COSE) Messages", RFC 8230,
23662366+ DOI 10.17487/RFC8230, September 2017,
23672367+ <https://www.rfc-editor.org/info/rfc8230>.
23682368+23692369+ [RFC8446] Rescorla, E., "The Transport Layer Security (TLS) Protocol
23702370+ Version 1.3", RFC 8446, DOI 10.17487/RFC8446, August 2018,
23712371+ <https://www.rfc-editor.org/info/rfc8446>.
23722372+23732373+ [RFC8551] Schaad, J., Ramsdell, B., and S. Turner, "Secure/
23742374+ Multipurpose Internet Mail Extensions (S/MIME) Version 4.0
23752375+ Message Specification", RFC 8551, DOI 10.17487/RFC8551,
23762376+ April 2019, <https://www.rfc-editor.org/info/rfc8551>.
23772377+23782378+ [RFC8610] Birkholz, H., Vigano, C., and C. Bormann, "Concise Data
23792379+ Definition Language (CDDL): A Notational Convention to
23802380+ Express Concise Binary Object Representation (CBOR) and
23812381+ JSON Data Structures", RFC 8610, DOI 10.17487/RFC8610,
23822382+ June 2019, <https://www.rfc-editor.org/info/rfc8610>.
23832383+23842384+ [RFC8778] Housley, R., "Use of the HSS/LMS Hash-Based Signature
23852385+ Algorithm with CBOR Object Signing and Encryption (COSE)",
23862386+ RFC 8778, DOI 10.17487/RFC8778, April 2020,
23872387+ <https://www.rfc-editor.org/info/rfc8778>.
23882388+23892389+ [RFC9021] Atkins, D., "Use of the Walnut Digital Signature Algorithm
23902390+ with CBOR Object Signing and Encryption (COSE)", RFC 9021,
23912391+ DOI 10.17487/RFC9021, May 2021,
23922392+ <https://www.rfc-editor.org/info/rfc9021>.
23932393+23942394+ [RFC9147] Rescorla, E., Tschofenig, H., and N. Modadugu, "The
23952395+ Datagram Transport Layer Security (DTLS) Protocol Version
23962396+ 1.3", RFC 9147, DOI 10.17487/RFC9147, April 2022,
23972397+ <https://www.rfc-editor.org/info/rfc9147>.
23982398+23992399+ [ROBUST] Fischlin, M., Günther, F., and C. Janson, "Robust
24002400+ Channels: Handling Unreliable Networks in the Record
24012401+ Layers of QUIC and DTLS", February 2020,
24022402+ <https://eprint.iacr.org/2020/718.pdf>.
24032403+24042404+ [SP800-38D]
24052405+ Dworkin, M., "Recommendation for Block Cipher Modes of
24062406+ Operation: Galois/Counter Mode (GCM) and GMAC", NIST
24072407+ Special Publication 800-38D, November 2007,
24082408+ <https://nvlpubs.nist.gov/nistpubs/Legacy/SP/
24092409+ nistspecialpublication800-38d.pdf>.
24102410+24112411+ [SP800-56A]
24122412+ Barker, E., Chen, L., Roginsky, A., Vassilev, A., and R.
24132413+ Davis, "Recommendation for Pair-Wise Key Establishment
24142414+ Schemes Using Discrete Logarithm Cryptography", NIST
24152415+ Special Publication 800-56A, Revision 3,
24162416+ DOI 10.6028/NIST.SP.800-56Ar3, April 2018,
24172417+ <https://nvlpubs.nist.gov/nistpubs/SpecialPublications/
24182418+ NIST.SP.800-56Ar2.pdf>.
24192419+24202420+ [STD90] Bray, T., Ed., "The JavaScript Object Notation (JSON) Data
24212421+ Interchange Format", STD 90, RFC 8259, December 2017,
24222422+ <https://www.rfc-editor.org/info/std90>.
24232423+24242424+Acknowledgments
24252425+24262426+ This document is a product of the COSE Working Group of the IETF.
24272427+24282428+ The following individuals are to blame for getting me started on this
24292429+ project in the first place: Richard Barnes, Matt Miller, and Martin
24302430+ Thomson.
24312431+24322432+ The initial draft version of the specification was based to some
24332433+ degree on the outputs of the JOSE and S/MIME Working Groups.
24342434+24352435+ The following individuals provided input into the final form of the
24362436+ document: Carsten Bormann, John Bradley, Brian Campbell, Michael
24372437+ B. Jones, Ilari Liusvaara, Francesca Palombini, Ludwig Seitz, and
24382438+ Göran Selander.
24392439+24402440+Author's Address
24412441+24422442+ Jim Schaad
24432443+ August Cellars