1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! [`Delegation`], [`DelegationClaim`] and [`DelegationRole`] — the
//! parsed form of the `lys/delegation/v1` tagged `COSE_Sign1` artifact.
//!
//! # Invariants
//!
//! - **The only durable form is the COSE bytes.** These types deliberately
//! implement no `serde` traits: nothing can persist a delegation except
//! [`Delegation::to_cose_bytes`], so no second wire shape can ever
//! exist alongside the frozen COSE artifact.
//! - **Round-trip identity:** `from_cose_bytes(x.to_cose_bytes()) == x` for
//! every `Delegation` whose fields satisfy the format's constraints —
//! a non-empty subject value, a delegated key strict Ed25519 could accept,
//! and a `(subject_kind, role)` pair this version defines — and
//! `to_cose_bytes` of a parsed
//! delegation reproduces the input bytes exactly.
//!
//! **The qualification is not decoration and was added after it was
//! violated.** [`Delegation::to_cose_bytes`] is infallible by design, so
//! a hand-built value outside those constraints still encodes; the decoder
//! then refuses what the encoder produced. The unqualified claim was false for
//! a subject value of 3885 bytes. Every issuing entry point in
//! [`super::sign`]
//! refuses such a claim up front, so the only way to reach the gap is to
//! construct the struct literal yourself and call `to_cose_bytes` on it.
//! - **Canonical-encoding strictness:** [`Delegation::from_cose_bytes`]
//! rejects any input that is not byte-identical to the canonical re-encoding
//! of its parsed fields — including inputs whose signature is
//! cryptographically valid (indefinite lengths, oversized integer heads,
//! reordered maps, a non-empty unprotected bucket, tag stripping, duplicate
//! keys, trailing garbage). Without this, a non-canonical re-encoding of the
//! same statement would be a *second* valid artifact for one claim, and every
//! downstream comparison by bytes would depend on which encoding you saw.
//!
//! **Canonical encoding is normative on the wire for this format**, not a
//! by-product of how this crate verifies. RFC 9052 §9 does not require map key
//! ordering, so a conforming stranger would accept a permuted map as a second
//! valid artifact for one statement unless the format itself forbids it — and
//! it does.
//!
//! What the byte-compare *contributes* was stated wrongly here twice and is
//! corrected in the `encoding` module's docs, which carry the measurement. In
//! short: it is not what refuses a permuted map (the positional decode pins
//! do), and it cannot be justified by "the signature catches the rest", which
//! is true only of this verifier. Its own territory is the **envelope**, which
//! no signature covers under any verifier's strategy — and at this parse-only
//! entry point it is the only canonicality guard there is, because no
//! signature is checked here at all.
//! - **The tag is required.** An untagged `COSE_Sign1` is refused, because
//! RFC 9052 §4.2 allows both forms and accepting both would give one
//! statement two encodings.
//! - **Parsing is not verification.** `from_cose_bytes` checks no signature and
//! knows no expected root key and no expected subject, and is named for
//! parsing to say so.
//! Its output is attacker-chosen data until [`super::sign::verify_delegation`]
//! has run.
//! - Every parse failure collapses to [`TrustError::DelegationVerification`]
//! (non-oracle).
use crate;
use crate;
pub use DelegationSubjectKind;
/// The role a delegation confers. A **closed** enum: a value on the wire that
/// is not listed here is a decode failure, never a value carried through.
///
/// The vocabulary is **scoped per subject kind** and the two are checked as a
/// pair — see [`DelegationSubjectKind::permits`], which holds the table.
///
/// ⛔ **The vocabulary starts at `2`, not at `1`, and that is load-bearing.**
/// `1` is deliberately *not* a role: it is
/// [`DelegationSubjectKind::Domain`]'s wire value, and numbering the roles from
/// `1` would have made `subject_kind == role` for every valid `v1` delegation,
/// rendering a field swap between the two undetectable in the encoded bytes.
/// [`DelegationSubjectKind::permits`] carries the full argument. A wire `role`
/// of `1` is therefore a **decode failure**.
///
/// # Why unknown roles are refused rather than passed along
///
/// The tolerant alternative produces the single worst artifact this system can
/// hold: a consumer handed a *verified* delegation carrying `role: 7` has a
/// cryptographically perfect statement whose meaning nobody in the system
/// defines. The signature raises its apparent authority while its semantics
/// stay empty — a signed unchecked value looks checked. Adding a role later is
/// a `v2`; that is what versioned wire contracts are for.
///
/// The enum is therefore deliberately **not** `#[non_exhaustive]`: a new role
/// is a new wire version, so downstream `match`es being forced to change is the
/// correct and intended consequence rather than an inconvenience to smooth
/// over.
/// The six signature-covered payload fields of a delegation — the statement
/// itself, without the signer's identity or the signature.
///
/// The root key is **not** a field here. It is the identity of whoever is
/// making the claim, not part of the claim, and carrying it in the claim as
/// well as in the protected `kid` would create two places for one value to live
/// and therefore a way for them to disagree. The signing and verifying entry
/// points in [`super::sign`] take it alongside a claim.
///
/// # An invalid `(subject_kind, role)` pair is representable here and refused everywhere else
///
/// The fields are public, so a struct literal can pair a
/// [`Domain`](DelegationSubjectKind::Domain) with
/// [`SpeaksFor`](DelegationRole::SpeaksFor). Every issuing entry point in
/// [`super::sign`] refuses such a claim before it signs, and the decoder refuses
/// it on the way back in, so the pair cannot reach the wire through this crate
/// or be accepted off the wire by it. See [`DelegationSubjectKind::permits`] for
/// the rule and why it is checked as a pair.
/// A root key's Ed25519-signed delegation, carried on the wire as a tagged
/// `COSE_Sign1` (RFC 9052) — the `lys/delegation/v1` artifact.
///
/// Constructed by parsing artifact bytes with [`Self::from_cose_bytes`] or by
/// verifying them with [`super::sign::verify_delegation`]; produced by
/// [`super::sign::sign_delegation`] and [`super::sign::assemble_delegation`],
/// which emit bytes rather than this type because the two-phase signing path
/// exists to keep the root key on a machine that need not hold this crate's
/// types.