vcon 0.3.0

A library for vCons. It reads and checks a vCon: form, compression, uuid and critical extensions.
Documentation
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! The signed form (a General JWS JSON Serialization, core-04 5.2) and the
//! encrypted form (a General JWE JSON Serialization, core-04 5.3) of a
//! vCon. The cryptography is the caller's: it comes through [`Signer`],
//! [`KeyEncrypter`] and [`ContentEncrypter`], so this module holds no key
//! beyond the content key of one JWE and draws no random numbers itself.

use std::fmt;

use base64::Engine;
use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};
use serde_json::{Map, Value};
use uuid::Uuid;
pub use zeroize::Zeroizing;

use super::emit::{Node, Obj, render};
use super::{Date, Vcon, WriteError, gzip, is_https};
use crate::Form;
use crate::form::form_of;
use crate::gzip::inflate;
use crate::identity::{parse_uuid_text, unsigned_identity};
use crate::json::parse_object;
use crate::read::{Limits, MAX_SIGNED_LAYERS};

/// The first two bytes of every gzip member (RFC 1952, section 2.3.1).
const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];

/// Whether a payload or plaintext is gzipped first.
///
/// # Examples
///
/// ```
/// use vcon::write::Compression;
///
/// assert_ne!(Compression::None, Compression::Gzip, "two choices");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Compression {
    /// The bytes as they are, marked `application/vcon`.
    None,
    /// The bytes gzipped, marked `application/vcon+gzip`.
    Gzip,
}

/// The certificates that identify a signer (core-04 5.2.2): DER
/// certificates written in `x5c`, or an https URL written in `x5u`.
///
/// # Examples
///
/// ```
/// use vcon::write::Chain;
///
/// let at = Chain::X5u("https://example.com/chain.pem".to_owned());
/// assert_ne!(at, Chain::X5c(vec![vec![0x30, 0x03]]), "a URL, not certificates");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Chain {
    /// DER certificates, the signer's first; written standard base64.
    X5c(Vec<Vec<u8>>),
    /// An https URL where the chain is.
    X5u(String),
}

/// The output of a [`ContentEncrypter`]: the initialization vector, the
/// ciphertext and the authentication tag.
///
/// # Examples
///
/// ```
/// let sealed = vcon::write::Sealed::new(vec![0; 16], vec![1, 2, 3], vec![9; 32]);
/// assert_eq!(sealed.clone(), sealed, "a sealed plaintext");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Sealed {
    iv: Vec<u8>,
    ciphertext: Vec<u8>,
    tag: Vec<u8>,
}

impl Sealed {
    /// The three outputs of an authenticated encryption.
    ///
    /// # Examples
    ///
    /// ```
    /// let sealed = vcon::write::Sealed::new(vec![0; 16], vec![1, 2, 3], vec![9; 32]);
    /// assert_eq!(sealed.clone(), sealed, "a sealed plaintext");
    /// ```
    #[must_use]
    pub fn new(iv: Vec<u8>, ciphertext: Vec<u8>, tag: Vec<u8>) -> Sealed {
        Sealed {
            iv,
            ciphertext,
            tag,
        }
    }
}

/// Why a [`Signer`], [`KeyEncrypter`] or [`ContentEncrypter`] failed, in its
/// own words.
///
/// # Examples
///
/// ```
/// let e = vcon::write::CryptoError::new("the key is not loaded");
/// assert_eq!(e.to_string(), "the key is not loaded", "its message");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[must_use]
pub struct CryptoError(String);

impl CryptoError {
    /// A failure described by `message`.
    ///
    /// # Examples
    ///
    /// ```
    /// let e = vcon::write::CryptoError::new("the key is not loaded");
    /// assert_eq!(e.clone(), e, "an error");
    /// ```
    pub fn new(message: impl Into<String>) -> CryptoError {
        CryptoError(message.into())
    }
}

impl fmt::Display for CryptoError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for CryptoError {}

/// Signs JWS signing input with the caller's key (core-04 5.2.1).
///
/// # Examples
///
/// ```
/// use vcon::write::{Chain, CryptoError, Signer};
///
/// /// A signer for this example only: it does not sign anything.
/// struct Example(Chain);
///
/// impl Signer for Example {
///     fn alg(&self) -> &str {
///         "RS256"
///     }
///     fn chain(&self) -> &Chain {
///         &self.0
///     }
///     fn sign(&self, input: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(input.iter().rev().copied().collect())
///     }
/// }
///
/// let s = Example(Chain::X5u("https://example.com/chain.pem".to_owned()));
/// assert_eq!(s.alg(), "RS256", "the algorithm written in the header");
/// ```
pub trait Signer {
    /// The JWS `alg` of the signatures it makes, such as `RS256`.
    fn alg(&self) -> &str;

    /// The certificates that identify it.
    fn chain(&self) -> &Chain;

    /// The signature of `input`, ASCII(protected) "." ASCII(payload).
    ///
    /// # Errors
    ///
    /// A [`CryptoError`] in the signer's words if it cannot sign.
    fn sign(&self, input: &[u8]) -> Result<Vec<u8>, CryptoError>;
}

/// Wraps a JWE's content key for one recipient (core-04 5.3.2).
///
/// # Examples
///
/// ```
/// use vcon::write::{CryptoError, KeyEncrypter};
///
/// /// A key encrypter for this example only: it does not encrypt.
/// struct Example;
///
/// impl KeyEncrypter for Example {
///     fn alg(&self) -> &str {
///         "RSA-OAEP"
///     }
///     fn encrypt_key(&self, cek: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(cek.to_vec())
///     }
/// }
///
/// assert_eq!(Example.alg(), "RSA-OAEP", "the algorithm written for the recipient");
/// ```
pub trait KeyEncrypter {
    /// The JWE `alg`, such as `RSA-OAEP`.
    fn alg(&self) -> &str;

    /// The content key `cek`, encrypted for this recipient.
    ///
    /// # Errors
    ///
    /// A [`CryptoError`] in the encrypter's words if it cannot.
    fn encrypt_key(&self, cek: &[u8]) -> Result<Vec<u8>, CryptoError>;
}

/// Encrypts a JWE's plaintext under a content key it makes (core-04 5.3).
///
/// # Examples
///
/// ```
/// use vcon::write::{ContentEncrypter, CryptoError, Sealed, Zeroizing};
///
/// /// A content encrypter for this example only: it does not encrypt.
/// struct Example;
///
/// impl ContentEncrypter for Example {
///     fn enc(&self) -> &str {
///         "A256CBC-HS512"
///     }
///     fn new_key(&self) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
///         Ok(Zeroizing::new(vec![7; 64]))
///     }
///     fn seal(&self, _cek: &[u8], plaintext: &[u8], _aad: &[u8]) -> Result<Sealed, CryptoError> {
///         Ok(Sealed::new(vec![0; 16], plaintext.to_vec(), vec![0; 32]))
///     }
/// }
///
/// assert_eq!(Example.enc(), "A256CBC-HS512", "the enc written in unprotected");
/// ```
pub trait ContentEncrypter {
    /// The JWE `enc`, such as `A256CBC-HS512`.
    fn enc(&self) -> &str;

    /// A new content key for one JWE; it is zeroed when dropped.
    ///
    /// # Errors
    ///
    /// A [`CryptoError`] in the encrypter's words if it cannot make one.
    fn new_key(&self) -> Result<Zeroizing<Vec<u8>>, CryptoError>;

    /// `plaintext` encrypted under `cek`, authenticating `aad` too.
    ///
    /// # Errors
    ///
    /// A [`CryptoError`] in the encrypter's words if it cannot.
    fn seal(&self, cek: &[u8], plaintext: &[u8], aad: &[u8]) -> Result<Sealed, CryptoError>;
}

/// The signed form of `vcon` (core-04 5.2): a General JWS JSON
/// Serialization whose payload is the vCon with `updated_at` set to
/// `signed_at`, gzipped first if `compression` says so, with one signature
/// per signer. The caller's model is not changed.
///
/// # Errors
///
/// [`WriteError::NoSigner`] if `signers` is empty, [`WriteError::BadChain`]
/// if a signer's chain is empty or not https, [`WriteError::Crypto`] if a
/// signer fails, and the errors of [`Vcon::to_json`].
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// use vcon::read::{Limits, inspect};
/// use vcon::write::{Chain, Compression, CryptoError, Signer, Vcon, sign};
///
/// /// A signer for this example only: its signature proves nothing.
/// struct Example(Chain);
///
/// impl Signer for Example {
///     fn alg(&self) -> &str {
///         "RS256"
///     }
///     fn chain(&self) -> &Chain {
///         &self.0
///     }
///     fn sign(&self, input: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(input.len().to_be_bytes().to_vec())
///     }
/// }
///
/// let signer = Example(Chain::X5u("https://example.com/chain.pem".to_owned()));
/// let uuid = vcon::write::uuid_v8(0, 0, "vcon.example.com");
/// let v = Vcon::new(uuid, "2026-10-27T00:00:00Z".parse().expect("RFC 3339"));
/// let at = "2026-10-27T01:00:00Z".parse().expect("RFC 3339");
/// let signed = sign(&v, at, Compression::None, &[&signer]).expect("signed");
/// let limits = Limits::new(1 << 20);
/// let found = inspect(&signed, None, &limits, &BTreeSet::new()).expect("a vCon");
/// assert_eq!(found.uuid, uuid, "the signed vCon's uuid");
/// ```
pub fn sign(
    vcon: &Vcon,
    signed_at: Date,
    compression: Compression,
    signers: &[&dyn Signer],
) -> Result<Vec<u8>, WriteError> {
    check_signers(signers)?;
    let mut signed = vcon.clone();
    signed.set_updated_at(signed_at);
    let payload = packed(signed.to_json()?, compression);
    jws(&payload, vcon.uuid(), signers)
}

/// A signed vCon signed again, as the payload of a new JWS, for archives
/// kept longer than their signing certificates (core-04 5.2.3). The new
/// header's `uuid` is the innermost vCon's. The inner signatures are not
/// verified: that is the caller's step before signing again.
///
/// # Errors
///
/// [`WriteError::NotSigned`] if `signed` is not a signed vCon read under
/// `limits`, [`WriteError::TooManyLayers`] if the result would have more
/// than [`crate::read::MAX_SIGNED_LAYERS`] signed layers,
/// [`WriteError::BadChain`] and [`WriteError::Crypto`] as for [`sign`].
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// use vcon::read::{Limits, inspect};
/// use vcon::write::{Chain, Compression, CryptoError, Signer, Vcon, sign, sign_again};
///
/// /// A signer for this example only: its signature proves nothing.
/// struct Example(Chain);
///
/// impl Signer for Example {
///     fn alg(&self) -> &str {
///         "RS256"
///     }
///     fn chain(&self) -> &Chain {
///         &self.0
///     }
///     fn sign(&self, input: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(input.len().to_be_bytes().to_vec())
///     }
/// }
///
/// let signer = Example(Chain::X5u("https://example.com/chain.pem".to_owned()));
/// let uuid = vcon::write::uuid_v8(0, 0, "vcon.example.com");
/// let v = Vcon::new(uuid, "2026-10-27T00:00:00Z".parse().expect("RFC 3339"));
/// let at = "2026-10-27T01:00:00Z".parse().expect("RFC 3339");
/// let signed = sign(&v, at, Compression::None, &[&signer]).expect("signed");
/// let limits = Limits::new(1 << 20);
/// let archived = sign_again(&signed, Compression::Gzip, &signer, limits).expect("signed again");
/// let found = inspect(&archived, None, &limits, &BTreeSet::new()).expect("a vCon");
/// assert_eq!(found.uuid, uuid, "the innermost uuid, through both layers");
/// ```
pub fn sign_again(
    signed: &[u8],
    compression: Compression,
    signer: &dyn Signer,
    limits: Limits,
) -> Result<Vec<u8>, WriteError> {
    let inner = read_signed(signed, limits)?;
    if inner.layers >= MAX_SIGNED_LAYERS {
        return Err(WriteError::TooManyLayers);
    }
    check_signers(&[signer])?;
    let payload = packed(signed.to_vec(), compression);
    jws(&payload, inner.uuid, &[signer])
}

/// The encrypted form of a signed vCon (core-04 5.3): a General JWE JSON
/// Serialization whose plaintext is `signed`, gzipped first if
/// `compression` says so, under one new content key wrapped for each
/// recipient. There is no protected header, so the additional
/// authenticated data is empty.
///
/// # Errors
///
/// [`WriteError::NotSigned`] if `signed` is not a signed vCon read under
/// `limits`, [`WriteError::NoRecipient`] if `recipients` is empty, and
/// [`WriteError::Crypto`] if an encrypter fails.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// use vcon::read::{Limits, inspect};
/// use vcon::write::{Chain, Compression, CryptoError, ContentEncrypter, KeyEncrypter, Sealed, Signer, Vcon, Zeroizing, encrypt, sign};
///
/// /// A signer for this example only: its signature proves nothing.
/// struct Example(Chain);
///
/// impl Signer for Example {
///     fn alg(&self) -> &str {
///         "RS256"
///     }
///     fn chain(&self) -> &Chain {
///         &self.0
///     }
///     fn sign(&self, input: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(input.len().to_be_bytes().to_vec())
///     }
/// }
///
/// let signer = Example(Chain::X5u("https://example.com/chain.pem".to_owned()));
/// let uuid = vcon::write::uuid_v8(0, 0, "vcon.example.com");
/// let v = Vcon::new(uuid, "2026-10-27T00:00:00Z".parse().expect("RFC 3339"));
/// let at = "2026-10-27T01:00:00Z".parse().expect("RFC 3339");
/// let signed = sign(&v, at, Compression::None, &[&signer]).expect("signed");
///
/// /// An encrypter for this example only: it does not encrypt.
/// struct Plain;
///
/// impl ContentEncrypter for Plain {
///     fn enc(&self) -> &str {
///         "A256CBC-HS512"
///     }
///     fn new_key(&self) -> Result<Zeroizing<Vec<u8>>, CryptoError> {
///         Ok(Zeroizing::new(vec![0; 64]))
///     }
///     fn seal(&self, _cek: &[u8], plaintext: &[u8], _aad: &[u8]) -> Result<Sealed, CryptoError> {
///         Ok(Sealed::new(vec![0; 16], plaintext.to_vec(), vec![0; 32]))
///     }
/// }
///
/// impl KeyEncrypter for Plain {
///     fn alg(&self) -> &str {
///         "RSA-OAEP"
///     }
///     fn encrypt_key(&self, cek: &[u8]) -> Result<Vec<u8>, CryptoError> {
///         Ok(cek.to_vec())
///     }
/// }
///
/// let limits = Limits::new(1 << 20);
/// let jwe = encrypt(&signed, Compression::Gzip, &Plain, &[&Plain], limits).expect("encrypted");
/// let found = inspect(&jwe, None, &limits, &BTreeSet::new()).expect("a vCon");
/// assert_eq!(found.form, vcon::Form::Encrypted, "the encrypted form");
/// ```
pub fn encrypt(
    signed: &[u8],
    compression: Compression,
    content: &dyn ContentEncrypter,
    recipients: &[&dyn KeyEncrypter],
    limits: Limits,
) -> Result<Vec<u8>, WriteError> {
    let inner = read_signed(signed, limits)?;
    if recipients.is_empty() {
        return Err(WriteError::NoRecipient);
    }
    let plaintext = packed(signed.to_vec(), compression);
    let cek = content.new_key().map_err(WriteError::Crypto)?;
    // No protected header: the additional authenticated data is
    // ASCII(""), RFC 7516 5.1 step 14.
    let sealed = content
        .seal(&cek, &plaintext.0, b"")
        .map_err(WriteError::Crypto)?;
    let mut wrapped = Vec::new();
    for recipient in recipients {
        let key = recipient.encrypt_key(&cek).map_err(WriteError::Crypto)?;
        let mut header = Obj::default();
        header.put("alg", Node::str(recipient.alg()));
        let mut entry = Obj::default();
        entry.put("header", header.done());
        entry.put("encrypted_key", Node::Str(URL_SAFE_NO_PAD.encode(key)));
        wrapped.push(entry.done());
    }
    let mut unprotected = Obj::default();
    unprotected.put("cty", Node::str(plaintext.cty()));
    unprotected.put("enc", Node::str(content.enc()));
    unprotected.put("uuid", Node::str(&inner.uuid.hyphenated().to_string()));
    let mut top = Obj::default();
    top.put("unprotected", unprotected.done());
    top.put("recipients", Node::Arr(wrapped));
    top.put("iv", Node::Str(URL_SAFE_NO_PAD.encode(&sealed.iv)));
    top.put(
        "ciphertext",
        Node::Str(URL_SAFE_NO_PAD.encode(&sealed.ciphertext)),
    );
    top.put("tag", Node::Str(URL_SAFE_NO_PAD.encode(&sealed.tag)));
    render(&top.done())
}

/// Bytes to sign or encrypt, gzipped if asked, with the media type that
/// says what they are.
struct Packed(Vec<u8>);

impl Packed {
    /// `application/vcon+gzip` exactly when the bytes are gzip, whether
    /// this crate gzipped them or they arrived so.
    fn cty(&self) -> &'static str {
        if self.0.starts_with(&GZIP_MAGIC) {
            "application/vcon+gzip"
        } else {
            "application/vcon"
        }
    }
}

fn packed(bytes: Vec<u8>, compression: Compression) -> Packed {
    match compression {
        Compression::None => Packed(bytes),
        Compression::Gzip => Packed(gzip(&bytes)),
    }
}

/// At least one signer, each with a usable chain, before anything is
/// signed.
fn check_signers(signers: &[&dyn Signer]) -> Result<(), WriteError> {
    if signers.is_empty() {
        return Err(WriteError::NoSigner);
    }
    for signer in signers {
        match signer.chain() {
            Chain::X5c(certificates) if certificates.is_empty() => {
                return Err(WriteError::BadChain);
            }
            Chain::X5u(url) if !is_https(url) => return Err(WriteError::BadChain),
            _ => {}
        }
    }
    Ok(())
}

/// A General JWS JSON Serialization of `payload`, one signature per
/// signer, each header naming `uuid` (core-04 5.2.1, 5.2.2).
fn jws(payload: &Packed, uuid: Uuid, signers: &[&dyn Signer]) -> Result<Vec<u8>, WriteError> {
    let mut cty = Obj::default();
    cty.put("cty", Node::str(payload.cty()));
    let protected = URL_SAFE_NO_PAD.encode(render(&cty.done())?);
    let encoded = URL_SAFE_NO_PAD.encode(&payload.0);
    let input = format!("{protected}.{encoded}");
    let mut signatures = Vec::new();
    for signer in signers {
        let signature = signer.sign(input.as_bytes()).map_err(WriteError::Crypto)?;
        let mut header = Obj::default();
        header.put("alg", Node::str(signer.alg()));
        match signer.chain() {
            Chain::X5c(certificates) => header.put(
                "x5c",
                Node::Arr(
                    certificates
                        .iter()
                        .map(|der| Node::Str(STANDARD.encode(der)))
                        .collect(),
                ),
            ),
            Chain::X5u(url) => header.put("x5u", Node::str(url)),
        }
        header.put("uuid", Node::str(&uuid.hyphenated().to_string()));
        let mut entry = Obj::default();
        entry.put("protected", Node::str(&protected));
        entry.put("header", header.done());
        entry.put("signature", Node::Str(URL_SAFE_NO_PAD.encode(signature)));
        signatures.push(entry.done());
    }
    let mut top = Obj::default();
    top.put("payload", Node::Str(encoded));
    top.put("signatures", Node::Arr(signatures));
    render(&top.done())
}

/// What a signed vCon holds: the innermost uuid and how many signed layers
/// lead to it.
struct Inner {
    uuid: Uuid,
    layers: usize,
}

/// Reads `signed` as a signed vCon: every layer a JWS whose payload is the
/// next, gzipped or not, under `limits`, down to an unsigned vCon, with
/// every header `uuid` that vCon's. Unlike `read::inspect` it does not
/// check `critical`: signing or encrypting does not process the vCon.
fn read_signed(signed: &[u8], limits: Limits) -> Result<Inner, WriteError> {
    let not_signed = |_| WriteError::NotSigned;
    let mut budget = limits.inflated;
    let mut object = open(signed, &mut budget).map_err(not_signed)?;
    if form_of(&object).map_err(not_signed)? != Form::Signed {
        return Err(WriteError::NotSigned);
    }
    let mut header_uuids = Vec::new();
    let mut layers = 0;
    loop {
        layers += 1;
        if layers > MAX_SIGNED_LAYERS {
            return Err(WriteError::NotSigned);
        }
        header_uuids.extend(header_uuids_of(&object)?);
        let Some(Value::String(payload)) = object.get("payload") else {
            return Err(WriteError::NotSigned);
        };
        let bytes = URL_SAFE_NO_PAD
            .decode(payload)
            .map_err(|_| WriteError::NotSigned)?;
        object = open(&bytes, &mut budget).map_err(not_signed)?;
        match form_of(&object).map_err(not_signed)? {
            Form::Signed => {}
            Form::Unsigned => break,
            _ => return Err(WriteError::NotSigned),
        }
    }
    let uuid = unsigned_identity(&object).map_err(not_signed)?;
    for header_uuid in header_uuids {
        match header_uuid {
            Value::String(text) if parse_uuid_text(&text) == Ok(uuid) => {}
            _ => return Err(WriteError::NotSigned),
        }
    }
    Ok(Inner { uuid, layers })
}

/// One JSON object, inflated first if it is gzip, counting the inflated
/// bytes against `budget`.
fn open(bytes: &[u8], budget: &mut u64) -> Result<Map<String, Value>, crate::read::Rejection> {
    let (inner, was_gzip) = inflate(bytes, None, *budget)?;
    if was_gzip {
        *budget = budget.saturating_sub(inner.len() as u64);
    }
    parse_object(&inner)
}

/// The `uuid` of each signature's header that has one.
fn header_uuids_of(object: &Map<String, Value>) -> Result<Vec<Value>, WriteError> {
    let Some(Value::Array(signatures)) = object.get("signatures") else {
        return Err(WriteError::NotSigned);
    };
    let mut uuids = Vec::new();
    for signature in signatures {
        match signature.get("header") {
            None => {}
            Some(Value::Object(header)) => uuids.extend(header.get("uuid").cloned()),
            Some(_) => return Err(WriteError::NotSigned),
        }
    }
    Ok(uuids)
}