devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//! A peer link's proof of both members' keys (docs/architecture/daemon/fleet.md,
//! *Peer links*; fleet plan step 10a.4, decisions 44-46, 50-52): run by
//! `link::opener_with` / `link::answerer_with` right after the `Hello`s,
//! on the raw byte pipe before the mux, when their `Endpoint` carries an
//! [`Auth`].
//!
//! Each message is a `u32` big-endian length, then a tag byte (`0` a
//! message, `1` a refusal whose body says why in UTF-8), then the body,
//! within a bound per round. The opener writes each round's message first
//! and then reads the answerer's; the answerer reads first, then writes, so
//! neither end ever blocks writing while the other does too. An end that
//! refuses writes its refusal in place of its next message; the peer
//! reports it as the peer's refusal.
//!
//! 1. `hello`: the member name, a 32-byte nonce from the OS RNG, the
//!    membership log's [`Head`]. The name must be the one this end expects
//!    (the dialer's entry name, `fleet-link`'s `--peer`); the heads are
//!    compared ([`membership::compare`]): another fleet's or a fork refuses
//!    the link, logged.
//! 2. `sync`: what the peer lacks, as [`Wire`] JSON: the entries past its
//!    head when ours is ahead, and every introduction held. The end behind
//!    extends a copy of its log ([`Log::extend`](membership::Log::extend):
//!    each entry authority-signed and chained to its head, so any member
//!    can carry them) and holds the introductions that check; one that
//!    doesn't is logged and dropped. Then it resolves the peer's name to a
//!    key: a member's in the log, else a held introduction's (the peer is
//!    capped, `Proven::introduced_by`). An unknown name is refused.
//! 3. `proof`: the end's public key and its signature, in [`DOMAIN`], over
//!    the [`transcript`] (both names, nonces, `Hello`s and heads) and its
//!    own role. The key must be the one the name resolved to (a revoked one
//!    says so), the signature must verify. An end with a pending rotation
//!    (`member.key.next`) proves with the next key when the log it just
//!    merged lists it so (the peer carried the signed `rotate`), else with
//!    its key.
//! 4. `confirm`: the opener's verdict on the answerer's proof (empty, or a
//!    refusal), so both ends agree the link is proven before either serves.
//!
//! What the end behind received comes back as [`Proven::newer`], for the
//! caller to store through its daemon (`membership.adopt`).

use std::io::{Read, Write};
use std::path::Path;

use anyhow::{Context, Result, anyhow, bail};
use sha2::{Digest, Sha256};

use super::identity::{FileKeyStore, PublicKey, Signature, Signer, SigningKey};
use super::member;
use super::membership::{self, Compare, Head, Stored, Wire};
use super::membership_api::{lists, settle};
use super::update;

/// The domain a link proof's signature is made in.
pub const DOMAIN: &str = "devsandbox/link/v1";

/// The bound of a `hello`, `proof` or `confirm` message's body.
const SMALL: usize = 4096;
/// The bound of a `sync` message's body (entries and introductions as JSON).
const SYNC_LIMIT: usize = 8 << 20;

pub(crate) const MESSAGE: u8 = 0;
pub(crate) const REFUSAL: u8 = 1;

/// What every refusal for a missing identity ends with (decision 52).
const MAKE_AGAIN: &str = "a fleet link proves both members' keys against the fleet's membership log: make the fleet again with `devsandbox machine add`";

/// This member as a link proves it: its name, its key, its stored log, and
/// a pending rotation's next key.
#[derive(Debug, Clone)]
pub struct Identity {
    pub name: String,
    pub key: SigningKey,
    pub stored: Stored,
    pub next: Option<SigningKey>,
}

impl Identity {
    /// This host's identity, read now (each link reads it at its start):
    /// the name `member.toml` at `member_toml` gives (else `hostname`'s),
    /// `member.key` and `membership.json` next to it (a pending rotation the
    /// log lists settled first: `membership_api::settle`). No key, no log, or
    /// a log that doesn't list this host under that name and key is an
    /// error naming the fix.
    pub fn load(member_toml: &Path, hostname: &str) -> Result<Identity> {
        let name = member::load(member_toml)?.unwrap_or_default().name_or(hostname)?;
        let key_path = member::key_path(member_toml);
        let log_path = membership::path(member_toml);
        let stored = membership::load(&log_path)?.filter(|s| s.log.membership().is_some());
        let super::membership_api::Settled { key, next, .. } = settle(&FileKeyStore::new(key_path.clone()), &name, stored.as_ref())?;
        let key = key.with_context(|| format!("this host has no member key ({}): {MAKE_AGAIN}", key_path.display()))?;
        let stored =
            stored.with_context(|| format!("this host is in no fleet's membership log ({}): {MAKE_AGAIN}", log_path.display()))?;
        if !lists(&stored, &name, &key.public_key()) {
            bail!(
                "this host's membership log doesn't list it (`{name}`, {}): {MAKE_AGAIN}",
                key.public_key().fingerprint()
            );
        }
        Ok(Identity { name, key, stored, next })
    }

    /// This member's public keys: its key's, then a pending rotation's next
    /// key's (what a log names it by once the rotation is in).
    pub fn public_keys(&self) -> Vec<PublicKey> {
        std::iter::once(&self.key).chain(&self.next).map(|k| k.public_key()).collect()
    }

    /// The key to prove with against `merged` (the log after this link's
    /// sync): the pending next one when `merged` lists this host under it,
    /// else the key. A routed stream's destination proves with the key
    /// this picks against its own stored log (`fleet::route::Here`).
    pub(crate) fn signer_for(&self, merged: &Stored) -> &SigningKey {
        match &self.next {
            Some(next) if lists(merged, &self.name, &next.public_key()) => next,
            _ => &self.key,
        }
    }
}

/// What one end of a link proves, and whom it expects.
pub struct Auth {
    pub identity: Identity,
    /// The peer's member name: the dialer's registry entry, `fleet-link`'s
    /// `--peer`.
    pub expect: String,
    /// Where refusals worth a line go (a fork, a dropped introduction).
    pub log: fn(&str),
}

/// Which end of the link this is (`link`'s roles).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
    Opener,
    Answerer,
}

impl Side {
    fn tag(self) -> &'static [u8] {
        match self {
            Side::Opener => b"opener",
            Side::Answerer => b"answerer",
        }
    }

    fn other(self) -> Side {
        match self {
            Side::Opener => Side::Answerer,
            Side::Answerer => Side::Opener,
        }
    }
}

/// A peer that proved its key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Proven {
    pub name: String,
    pub key: PublicKey,
    /// Who introduced it, when the log doesn't list it yet (capped,
    /// decision 46).
    pub introduced_by: Option<String>,
    /// Our stored log and introductions with what the peer carried, when
    /// that changed them: for the daemon to adopt.
    pub newer: Option<Stored>,
}

/// One end's `hello` message.
#[derive(Debug, Clone, PartialEq, Eq)]
struct Hello {
    name: String,
    nonce: [u8; 32],
    head: Head,
}

/// `field` length-prefixed (u32 big-endian) onto `out`.
pub(crate) fn put(out: &mut Vec<u8>, field: &[u8]) {
    let len = u32::try_from(field.len()).expect("a link proof field under 4 GiB");
    out.extend_from_slice(&len.to_be_bytes());
    out.extend_from_slice(field);
}

fn put_head(out: &mut Vec<u8>, head: &Head) {
    let mut h = Vec::with_capacity(72);
    h.extend_from_slice(&head.len.to_be_bytes());
    h.extend_from_slice(&head.hash);
    h.extend_from_slice(&head.genesis);
    put(out, &h);
}

/// Reads [`put`]'s fields back, refusing anything short or left over.
pub(crate) struct Fields<'a>(pub(crate) &'a [u8]);

impl<'a> Fields<'a> {
    pub(crate) fn next(&mut self) -> Result<&'a [u8]> {
        let (len, rest) = self.0.split_at_checked(4).context("a truncated field")?;
        let len = u32::from_be_bytes(len.try_into().expect("4 bytes")) as usize;
        let (field, rest) = rest.split_at_checked(len).context("a truncated field")?;
        self.0 = rest;
        Ok(field)
    }

    pub(crate) fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
        self.next()?.try_into().map_err(|_| anyhow!("a field of the wrong size"))
    }

    /// Whether every field has been read.
    pub(crate) fn done(&self) -> bool {
        self.0.is_empty()
    }

    pub(crate) fn end(&self) -> Result<()> {
        if !self.0.is_empty() {
            bail!("trailing bytes");
        }
        Ok(())
    }
}

impl Hello {
    fn encode(&self) -> Vec<u8> {
        let mut out = Vec::new();
        put(&mut out, self.name.as_bytes());
        put(&mut out, &self.nonce);
        put_head(&mut out, &self.head);
        out
    }

    fn decode(body: &[u8]) -> Result<Hello> {
        let mut f = Fields(body);
        let name = String::from_utf8(f.next()?.to_vec()).context("a name that isn't UTF-8")?;
        let nonce = f.array()?;
        let head: [u8; 72] = f.array()?;
        f.end()?;
        let head = Head {
            len: u64::from_be_bytes(head[..8].try_into().expect("8 bytes")),
            hash: head[8..40].try_into().expect("32 bytes"),
            genesis: head[40..].try_into().expect("32 bytes"),
        };
        Ok(Hello { name, nonce, head })
    }
}

/// sha256 of a `Hello` frame's hash string and caps.
fn hello_digest(hello: &str, caps: u32) -> [u8; 32] {
    let mut out = Vec::new();
    put(&mut out, hello.as_bytes());
    put(&mut out, &caps.to_be_bytes());
    Sha256::digest(out).into()
}

/// What both ends sign: the opener's then the answerer's name, nonce,
/// `Hello` digest (its hash string and caps) and head, each
/// length-prefixed.
fn transcript(opener: (&Hello, [u8; 32]), answerer: (&Hello, [u8; 32])) -> Vec<u8> {
    let mut out = Vec::new();
    for (h, digest) in [opener, answerer] {
        put(&mut out, h.name.as_bytes());
        put(&mut out, &h.nonce);
        put(&mut out, &digest);
        put_head(&mut out, &h.head);
    }
    out
}

/// What `side` signs: the transcript and its own role, so neither end's
/// signature serves as the other's.
fn signed(transcript: &[u8], side: Side) -> Vec<u8> {
    let mut out = transcript.to_vec();
    put(&mut out, side.tag());
    out
}

pub(crate) fn write_raw(w: &mut impl Write, tag: u8, body: &[u8]) -> Result<()> {
    let len = u32::try_from(body.len() + 1).context("a link proof message too long")?;
    w.write_all(&len.to_be_bytes())?;
    w.write_all(&[tag])?;
    w.write_all(body)?;
    w.flush()?;
    Ok(())
}

/// One message read off the pipe ([`read_frame`]).
pub(crate) enum Frame {
    Message(Vec<u8>),
    /// The other end's refusal: why, in its words.
    Refusal(String),
}

/// Why [`read_frame`] read no [`Frame`], for its caller to word.
pub(crate) enum Bad {
    Closed(std::io::Error),
    /// A length of 0 or over the bound (tag byte included).
    Size(usize),
    Tag(u8),
}

/// One message, bounded by `limit` (its body; the tag byte comes on top).
pub(crate) fn read_frame(r: &mut impl Read, limit: usize) -> Result<Frame, Bad> {
    let mut len = [0u8; 4];
    r.read_exact(&mut len).map_err(Bad::Closed)?;
    let len = u32::from_be_bytes(len) as usize;
    if len == 0 || len > limit + 1 {
        return Err(Bad::Size(len));
    }
    let mut buf = vec![0u8; len];
    r.read_exact(&mut buf).map_err(Bad::Closed)?;
    match buf[0] {
        MESSAGE => Ok(Frame::Message(buf.split_off(1))),
        REFUSAL => Ok(Frame::Refusal(String::from_utf8_lossy(&buf[1..]).into_owned())),
        tag => Err(Bad::Tag(tag)),
    }
}

/// One message, bounded by `limit`: its body, or the peer's refusal as an
/// error.
fn read_raw<R: Read>(r: &mut R, limit: usize, peer: &str) -> Result<Vec<u8>> {
    match read_frame(r, limit) {
        Ok(Frame::Message(body)) => Ok(body),
        Ok(Frame::Refusal(why)) => bail!("`{peer}` refused the link: {why}"),
        Err(Bad::Closed(e)) => Err(anyhow::Error::new(e).context(format!("`{peer}` closed the link during the key proof"))),
        Err(Bad::Size(len)) => bail!("`{peer}` sent a key proof message of {len} bytes (at most {})", limit + 1),
        Err(Bad::Tag(tag)) => bail!("`{peer}` sent a key proof message tagged {tag}"),
    }
}

/// The ordered exchange of one round's messages (module doc), each read
/// with `read` (its words for a close or a refusal: [`read_raw`]'s here,
/// `route`'s for a routed opening).
pub(crate) struct Chan<'a, R, W> {
    pub(crate) side: Side,
    pub(crate) r: &'a mut R,
    pub(crate) w: &'a mut W,
    pub(crate) peer: &'a str,
    pub(crate) read: fn(&mut R, usize, &str) -> Result<Vec<u8>>,
}

impl<R: Read, W: Write> Chan<'_, R, W> {
    /// Write ours (or our refusal, which ends the proof with `Err(why)`).
    pub(crate) fn send(&mut self, ours: Result<Vec<u8>, String>) -> Result<()> {
        match ours {
            Ok(body) => write_raw(self.w, MESSAGE, &body),
            Err(why) => {
                let _ = write_raw(self.w, REFUSAL, why.as_bytes());
                Err(anyhow!("{why}"))
            }
        }
    }

    /// One round: the opener writes then reads, the answerer reads then
    /// writes.
    pub(crate) fn exchange(&mut self, ours: Result<Vec<u8>, String>, limit: usize) -> Result<Vec<u8>> {
        match self.side {
            Side::Opener => {
                self.send(ours)?;
                (self.read)(self.r, limit, self.peer)
            }
            Side::Answerer => {
                let theirs = (self.read)(self.r, limit, self.peer)?;
                self.send(ours)?;
                Ok(theirs)
            }
        }
    }
}

/// What `stored` sends a peer at `theirs` ([`Compare`] says how ours stands
/// to it): the entries it lacks when ours is ahead, and our introductions.
fn sync_for(stored: &Stored, cmp: Compare, theirs: Head) -> Wire {
    let entries = match cmp {
        Compare::Ahead => stored.log.entries_after(theirs.len).to_vec(),
        _ => Vec::new(),
    };
    Wire { entries, introductions: stored.introductions.clone() }
}

/// `stored` with what a peer at `theirs` sent (`wire`): its entries
/// continuing ours (an error when they don't, or don't reach its head
/// while ours is behind), then its introductions the result doesn't settle,
/// each held if it checks (else `log`ged and dropped).
fn merge(stored: &Stored, cmp: Compare, theirs: Head, wire: Wire, peer: &str, log: fn(&str)) -> Result<Stored> {
    let mut merged = stored.clone();
    if !wire.entries.is_empty() {
        merged.log.extend(wire.entries).with_context(|| format!("`{peer}`'s membership entries"))?;
    }
    if cmp == Compare::Behind && merged.log.head() != theirs {
        bail!("`{peer}`'s membership entries don't reach its head ({theirs})");
    }
    let m = merged.log.membership().context("no membership log")?.clone();
    m.drop_settled(&mut merged.introductions);
    for i in wire.introductions {
        if m.settles(&i) {
            continue;
        }
        if let Err(e) = merged.introduce(i.clone()) {
            log(&format!("fleet link with `{peer}`: its introduction of `{}` by `{}` is dropped: {e:#}", i.name, i.introducer));
        }
    }
    Ok(merged)
}

/// `name`'s key in `stored`: a member's in the log, else a held
/// introduction's (with its introducer).
pub(crate) fn resolve(stored: &Stored, name: &str) -> Option<(PublicKey, Option<String>)> {
    let m = stored.log.membership()?;
    if let Some(key) = m.key_of(name) {
        return Some((key, None));
    }
    stored.introductions.iter().find(|i| i.name == name).map(|i| (i.key, Some(i.introducer.clone())))
}

pub(crate) fn encode_proof(key: &PublicKey, sig: &Signature) -> Vec<u8> {
    let mut out = Vec::new();
    put(&mut out, key.as_bytes());
    put(&mut out, &sig.to_bytes());
    out
}

fn decode_proof(body: &[u8]) -> Result<(PublicKey, Signature)> {
    let mut f = Fields(body);
    let key = PublicKey::from_bytes(f.array()?)?;
    let sig = Signature::from_bytes(f.array()?);
    f.end()?;
    Ok((key, sig))
}

/// The peer's proof checked: its key is the one its name resolved to (a
/// revoked one, or another, refused) and it signed `message` with it in
/// `domain`.
pub(crate) fn check_proof(
    body: &[u8],
    expected: &PublicKey,
    stored: &Stored,
    peer: &str,
    domain: &'static str,
    message: &[u8],
) -> Result<PublicKey, String> {
    let (key, sig) = decode_proof(body).map_err(|e| format!("`{peer}`'s key proof is malformed: {e:#}"))?;
    if key != *expected {
        let revoked = stored.log.membership().is_some_and(|m| m.revoked.contains(&key));
        return Err(if revoked {
            format!("`{peer}` proved key {}, which is revoked", key.fingerprint())
        } else {
            format!("`{peer}` proved key {}, not its key in this fleet's membership log ({})", key.fingerprint(), expected.fingerprint())
        });
    }
    key.verify(domain, message, &sig).map_err(|_| format!("`{peer}`'s key proof doesn't verify: a bad signature"))?;
    Ok(key)
}

/// Both keys proven over `r`/`w` from `side` (module doc): `ours` this
/// end's `Hello` (its hash string and caps), `theirs` the peer's. An error
/// says why the link is refused (by this end or the peer).
pub fn prove(side: Side, r: &mut impl Read, w: &mut impl Write, auth: &Auth, ours: (&str, u32), theirs: (&str, u32)) -> Result<Proven> {
    let id = &auth.identity;
    let peer = auth.expect.as_str();
    let mut chan = Chan { side, r, w, peer, read: read_raw };
    let mut nonce = [0u8; 32];
    getrandom::fill(&mut nonce).map_err(|e| anyhow!("cannot read the OS random source: {e}"))?;
    let mine = Hello { name: id.name.clone(), nonce, head: id.stored.log.head() };

    // 1. hello.
    let body = chan.exchange(Ok(mine.encode()), SMALL)?;
    let them = Hello::decode(&body).map_err(|e| anyhow!("`{peer}`'s key proof hello is malformed: {e:#}"))?;
    let cmp = membership::compare(&id.stored.log, them.head);
    let checked = if them.name != peer {
        Err(format!("the peer calls itself `{}`, not `{peer}`", them.name))
    } else if let Compare::Fork { another_fleet } = cmp {
        let what = if another_fleet { "another fleet's (another genesis)" } else { "a fork: one authority signed two histories" };
        let why = format!("`{peer}`'s membership log is {what} (ours: {}; theirs: {})", id.stored.log.head(), them.head);
        (auth.log)(&format!("fleet link with `{peer}` refused: {why}"));
        Err(why)
    } else {
        serde_json::to_vec(&sync_for(&id.stored, cmp, them.head)).map_err(|e| format!("cannot encode the membership sync: {e}"))
    };

    // 2. sync.
    let body = chan.exchange(checked, SYNC_LIMIT)?;
    let resolved = serde_json::from_slice::<Wire>(&body)
        .map_err(|e| format!("`{peer}`'s membership sync is malformed: {e}"))
        .and_then(|wire| merge(&id.stored, cmp, them.head, wire, peer, auth.log).map_err(|e| format!("{e:#}")))
        .and_then(|merged| match resolve(&merged, peer) {
            Some(found) => Ok((merged, found)),
            None => Err(format!("`{peer}` is neither a member of this fleet's membership log nor introduced to it")),
        });

    // 3. proof.
    let (o, a) = match side {
        Side::Opener => ((&mine, hello_digest(ours.0, ours.1)), (&them, hello_digest(theirs.0, theirs.1))),
        Side::Answerer => ((&them, hello_digest(theirs.0, theirs.1)), (&mine, hello_digest(ours.0, ours.1))),
    };
    let t = transcript(o, a);
    let proof = |merged: &Stored| {
        let key = id.signer_for(merged);
        encode_proof(&key.public_key(), &key.sign(DOMAIN, &signed(&t, side)))
    };
    let (merged, (expected, introduced_by)) = match side {
        Side::Opener => {
            let resolved = resolved;
            let theirs_proof = chan.exchange(resolved.as_ref().map(|(merged, _)| proof(merged)).map_err(Clone::clone), SMALL)?;
            let (merged, found) = resolved.expect("refused above otherwise");
            // 4. confirm: our verdict on theirs.
            let verdict = check_proof(&theirs_proof, &found.0, &merged, peer, DOMAIN, &signed(&t, side.other()));
            chan.send(verdict.map(|_| Vec::new()))?;
            (merged, found)
        }
        Side::Answerer => {
            let theirs_proof = read_raw(chan.r, SMALL, peer)?;
            let verdict = resolved.and_then(|(merged, found)| {
                check_proof(&theirs_proof, &found.0, &merged, peer, DOMAIN, &signed(&t, side.other())).map(|_| (merged, found))
            });
            chan.send(verdict.as_ref().map(|(merged, _)| proof(merged)).map_err(Clone::clone))?;
            read_raw(chan.r, SMALL, peer)?;
            verdict.expect("refused above otherwise")
        }
    };
    let newer = (merged != id.stored).then_some(merged);
    Ok(Proven { name: peer.to_string(), key: expected, introduced_by, newer })
}

/// Why a link ends after `newer` was adopted: the control plane its log names isn't
/// the one this end's `Hello` (`hello`) said, so both ends redial saying
/// it. `None` when it is.
pub fn control_plane_moved(hello: &str, newer: &Stored) -> Option<String> {
    let control_plane = &newer.log.membership()?.control_plane;
    (update::control_plane_of(hello) != Some(control_plane.as_str()))
        .then(|| format!("took newer membership entries: the fleet's control plane is now `{control_plane}`; the link ends so both ends redial saying it"))
}

#[cfg(test)]
pub mod tests {
    use std::os::unix::net::UnixStream;

    use super::*;
    use crate::serve::fleet::membership::{Introduction, Log, Op};

    fn key(n: u8) -> SigningKey {
        SigningKey::from_seed([n; 32])
    }

    fn pk(n: u8) -> PublicKey {
        key(n).public_key()
    }

    /// `lap` (key 1) the authority, `box` (key 2) the control plane; fleet id `id`.
    fn two(id: u8) -> Stored {
        let mut log = Log::default();
        log.append(Op::genesis_with_id([id; 16], "lap", pk(1), "box"), "lap", &key(1)).unwrap();
        log.append(Op::Add { name: "box".into(), key: pk(2) }, "lap", &key(1)).unwrap();
        Stored { log, introductions: vec![] }
    }

    fn with(mut s: Stored, op: Op) -> Stored {
        s.log.append(op, "lap", &key(1)).unwrap();
        s
    }

    fn auth(name: &str, k: u8, stored: Stored, expect: &str) -> Auth {
        Auth { identity: Identity { name: name.into(), key: key(k), stored, next: None }, expect: expect.into(), log: |_| {} }
    }

    const HELLO_O: (&str, u32) = ("o hello", 1);
    const HELLO_A: (&str, u32) = ("a hello", 2);

    /// The opener (`o`) and the answerer (`a`) proving over a socket pair.
    pub fn run(o: Auth, a: Auth) -> (Result<Proven>, Result<Proven>) {
        let (os, as_) = UnixStream::pair().unwrap();
        let opener = std::thread::spawn(move || {
            let (mut r, mut w) = (os.try_clone().unwrap(), os);
            let out = prove(Side::Opener, &mut r, &mut w, &o, HELLO_O, HELLO_A);
            let _ = w.shutdown(std::net::Shutdown::Both);
            out
        });
        let (mut r, mut w) = (as_.try_clone().unwrap(), as_);
        let answered = prove(Side::Answerer, &mut r, &mut w, &a, HELLO_A, HELLO_O);
        let _ = w.shutdown(std::net::Shutdown::Both);
        (opener.join().unwrap(), answered)
    }

    fn err(r: Result<Proven>) -> String {
        format!("{:#}", r.expect_err("refused"))
    }

    #[test]
    fn both_ends_prove_their_keys() {
        let (o, a) = run(auth("box", 2, two(1), "lap"), auth("lap", 1, two(1), "box"));
        assert_eq!(o.unwrap(), Proven { name: "lap".into(), key: pk(1), introduced_by: None, newer: None });
        assert_eq!(a.unwrap(), Proven { name: "box".into(), key: pk(2), introduced_by: None, newer: None });
    }

    #[test]
    fn a_wrong_key_for_the_name_is_refused() {
        // `box` signs with key 9: the log says key 2.
        let (o, a) = run(auth("box", 9, two(1), "lap"), auth("lap", 1, two(1), "box"));
        let a = err(a);
        assert!(a.contains("`box` proved key") && a.contains("not its key"), "{a}");
        assert!(err(o).contains("`lap` refused the link: `box` proved key"));
        // The answerer's wrong key: the opener's confirm refuses it.
        let (o, a) = run(auth("box", 2, two(1), "lap"), auth("lap", 9, two(1), "box"));
        assert!(err(o).contains("`lap` proved key"));
        assert!(err(a).contains("`box` refused the link: `lap` proved key"));
    }

    #[test]
    fn an_unknown_or_unexpected_name_is_refused() {
        let (o, a) = run(auth("zed", 9, two(1), "lap"), auth("lap", 1, two(1), "zed"));
        assert!(err(a).contains("`zed` is neither a member"), "unknown");
        assert!(err(o).contains("refused the link"));
        let (o, a) = run(auth("box", 2, two(1), "lap"), auth("lap", 1, two(1), "pi"));
        assert!(err(a).contains("the peer calls itself `box`, not `pi`"));
        assert!(err(o).contains("`lap` refused the link"));
    }

    #[test]
    fn a_revoked_key_is_refused() {
        // `pi` (key 3) revoked, then added back with key 4; the old key proves.
        let s = with(with(with(two(1), Op::Add { name: "pi".into(), key: pk(3) }), Op::Revoke { name: "pi".into(), key: pk(3) }), Op::Add {
            name: "pi".into(),
            key: pk(4),
        });
        let (_, a) = run(auth("pi", 3, s.clone(), "lap"), auth("lap", 1, s.clone(), "pi"));
        assert!(err(a).contains("which is revoked"));
        // Revoked and gone: an unknown name.
        let gone = with(with(two(1), Op::Add { name: "pi".into(), key: pk(3) }), Op::Revoke { name: "pi".into(), key: pk(3) });
        let (_, a) = run(auth("pi", 3, gone.clone(), "lap"), auth("lap", 1, gone, "pi"));
        assert!(err(a).contains("neither a member"));
    }

    #[test]
    fn an_introduced_peer_is_proven_capped() {
        let s = two(1);
        let g = s.log.membership().unwrap().genesis;
        let mut new = s.clone();
        new.introduce(Introduction::sign(&g, "new", pk(5), "box", &key(2))).unwrap();
        // `lap` doesn't hold the introduction: `new` carries it.
        let (o, a) = run(auth("new", 5, new.clone(), "lap"), auth("lap", 1, s.clone(), "new"));
        let a = a.unwrap();
        assert_eq!((a.key, a.introduced_by.as_deref()), (pk(5), Some("box")));
        assert_eq!(a.newer, Some(new.clone()), "the introduction to hold");
        assert_eq!(o.unwrap().newer, None);
        // An introduction by a capped member doesn't check: dropped, the name unknown.
        let mut by_new = new.clone();
        by_new.introductions.push(Introduction::sign(&g, "x", pk(6), "new", &key(5)));
        let (_, a) = run(auth("x", 6, by_new, "lap"), auth("lap", 1, s, "x"));
        assert!(err(a).contains("`x` is neither a member"));
    }

    #[test]
    fn the_end_behind_gets_the_entries_it_lacks() {
        let s = two(1);
        let ahead = with(with(s.clone(), Op::Add { name: "pi".into(), key: pk(3) }), Op::ControlPlane { name: "pi".into() });
        let (o, a) = run(auth("box", 2, s.clone(), "lap"), auth("lap", 1, ahead.clone(), "box"));
        assert_eq!(o.unwrap().newer, Some(ahead.clone()));
        assert_eq!(a.unwrap().newer, None);
        // The peer exists only in the newer entries: resolved after them.
        let (o, a) = run(auth("pi", 3, ahead.clone(), "lap"), auth("lap", 1, s.clone(), "pi"));
        assert_eq!(a.unwrap().newer, Some(ahead.clone()));
        assert_eq!(o.unwrap().newer, None);
        assert!(control_plane_moved(&update::hello_of(false, Some("box"), false), &ahead).unwrap().contains("control plane is now `pi`"));
        assert_eq!(control_plane_moved(&update::hello_of(false, Some("pi"), false), &ahead), None);
    }

    #[test]
    fn a_fork_or_another_fleet_is_refused() {
        let s = two(1);
        let left = with(s.clone(), Op::Add { name: "pi".into(), key: pk(3) });
        let right = with(s.clone(), Op::Add { name: "zz".into(), key: pk(4) });
        let (o, a) = run(auth("box", 2, left, "lap"), auth("lap", 1, right, "box"));
        assert!(err(o).contains("a fork: one authority signed two histories"));
        assert!(err(a).contains("a fork"));
        let (o, a) = run(auth("box", 2, two(2), "lap"), auth("lap", 1, two(1), "box"));
        assert!(err(o).contains("another fleet's (another genesis)"));
        assert!(err(a).contains("another fleet's"));
    }

    /// An opener played by hand: the real `hello` and `sync`, then `proof`
    /// as `forge` makes it from the transcript the answerer signs.
    fn forged(forge: impl FnOnce(&[u8]) -> Vec<u8> + Send + 'static) -> String {
        let (os, as_) = UnixStream::pair().unwrap();
        let s = two(1);
        let opener = std::thread::spawn(move || {
            let (mut r, mut w) = (os.try_clone().unwrap(), os);
            let mine = Hello { name: "box".into(), nonce: [7; 32], head: two(1).log.head() };
            write_raw(&mut w, MESSAGE, &mine.encode()).unwrap();
            let them = Hello::decode(&read_raw(&mut r, SMALL, "lap").unwrap()).unwrap();
            write_raw(&mut w, MESSAGE, br#"{"entries":[]}"#).unwrap();
            read_raw(&mut r, SYNC_LIMIT, "lap").unwrap();
            let t = transcript((&mine, hello_digest(HELLO_O.0, HELLO_O.1)), (&them, hello_digest(HELLO_A.0, HELLO_A.1)));
            // The answerer may refuse at the length (oversize) and hang up
            // before the body is written: the test asserts its refusal.
            let _ = write_raw(&mut w, MESSAGE, &forge(&t));
            let _ = read_raw(&mut r, SMALL, "lap");
        });
        let (mut r, mut w) = (as_.try_clone().unwrap(), as_);
        let got = prove(Side::Answerer, &mut r, &mut w, &auth("lap", 1, s, "box"), HELLO_A, HELLO_O);
        drop((r, w));
        opener.join().unwrap();
        err(got)
    }

    #[test]
    fn a_tampered_proof_is_refused() {
        // The signature's bytes changed.
        let e = forged(|t| {
            let mut sig = key(2).sign(DOMAIN, &signed(t, Side::Opener)).to_bytes();
            sig[3] ^= 1;
            encode_proof(&pk(2), &Signature::from_bytes(sig))
        });
        assert!(e.contains("doesn't verify"), "{e}");
        // Another transcript (another nonce), the answerer's role, another domain.
        let e = forged(|t| {
            let mut t = t.to_vec();
            t[10] ^= 1;
            encode_proof(&pk(2), &key(2).sign(DOMAIN, &signed(&t, Side::Opener)))
        });
        assert!(e.contains("doesn't verify"), "{e}");
        let e = forged(|t| encode_proof(&pk(2), &key(2).sign(DOMAIN, &signed(t, Side::Answerer))));
        assert!(e.contains("doesn't verify"), "reflected: {e}");
        let e = forged(|t| encode_proof(&pk(2), &key(2).sign(membership::ROTATE_DOMAIN, &signed(t, Side::Opener))));
        assert!(e.contains("doesn't verify"), "{e}");
        // Malformed, oversize.
        assert!(forged(|_| b"junk".to_vec()).contains("malformed"));
        let e = forged(|_| vec![0; SMALL + 1]);
        assert!(e.contains("key proof message of"), "{e}");
    }

    #[test]
    fn identity_needs_a_key_and_a_log_listing_this_host() {
        use crate::serve::fleet::membership_api::test_support;
        let dir = std::env::temp_dir().join(format!("dsv-proof-identity-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let toml = dir.join("member.toml");
        member::write_for_test(&toml, &member::Member { name: "box".into(), ..Default::default() });
        let e = format!("{:#}", Identity::load(&toml, "host").unwrap_err());
        assert!(e.contains("no member key") && e.contains("machine add"), "{e}");
        let k = test_support::key_of(&toml);
        let e = format!("{:#}", Identity::load(&toml, "host").unwrap_err());
        assert!(e.contains("in no fleet's membership log") && e.contains("machine add"), "{e}");
        test_support::write(&toml, &test_support::fleet("lap", &key(1), "lap", &[("box", pk(9))]));
        let e = format!("{:#}", Identity::load(&toml, "host").unwrap_err());
        assert!(e.contains("doesn't list it"), "{e}");
        test_support::write(&toml, &test_support::fleet("lap", &key(1), "lap", &[("box", k.public_key())]));
        let id = Identity::load(&toml, "host").unwrap();
        assert_eq!((id.name.as_str(), id.key.public_key()), ("box", k.public_key()));
        assert!(id.next.is_none());

        // A pending rotation: carried while the log lists the key; promoted
        // once it lists the next one; neither listed is refused as before.
        use crate::serve::fleet::identity::KeyStore as _;
        let store = FileKeyStore::new(member::key_path(&toml));
        let next = store.next_key().unwrap();
        let id = Identity::load(&toml, "host").unwrap();
        assert_eq!((id.key.public_key(), id.next.map(|n| n.public_key())), (k.public_key(), Some(next.public_key())));
        test_support::write(&toml, &test_support::fleet("lap", &key(1), "lap", &[("box", pk(9))]));
        assert!(format!("{:#}", Identity::load(&toml, "host").unwrap_err()).contains("doesn't list it"));
        assert!(store.next_path().exists());
        test_support::write(&toml, &test_support::fleet("lap", &key(1), "lap", &[("box", next.public_key())]));
        let id = Identity::load(&toml, "host").unwrap();
        assert_eq!((id.key.public_key(), id.next.is_none()), (next.public_key(), true));
        assert!(!store.next_path().exists(), "promoted");
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// `box` (2) has a pending next key (9) and a log behind `lap`'s, whose
    /// last entry rotates box to 9: the merged log lists box under 9, so box
    /// proves with it and lap accepts; its old key is refused as revoked.
    /// Before the rotate is in, box proves with its key.
    #[test]
    fn a_pending_rotation_proves_with_the_key_the_merged_log_lists() {
        let s = two(1);
        let g = s.log.membership().unwrap().genesis;
        let rotate = Op::Rotate { name: "box".into(), old: pk(2), new: pk(9), by_old: membership::countersign_rotate(&g, "box", &pk(9), &key(2)) };
        let rotated = with(s.clone(), rotate);
        let pending = |stored: Stored| {
            let mut a = auth("box", 2, stored, "lap");
            a.identity.next = Some(key(9));
            a
        };
        // box opens, then answers.
        let (o, a) = run(pending(s.clone()), auth("lap", 1, rotated.clone(), "box"));
        let (o, a) = (o.unwrap(), a.unwrap());
        assert_eq!((a.key, o.newer.as_ref()), (pk(9), Some(&rotated)));
        let (o, a) = run(auth("lap", 1, rotated.clone(), "box"), pending(s.clone()));
        assert_eq!((o.unwrap().key, a.unwrap().newer), (pk(9), Some(rotated.clone())));
        // No next key: the old one is refused as revoked.
        let (_, a) = run(auth("box", 2, s.clone(), "lap"), auth("lap", 1, rotated.clone(), "box"));
        assert!(err(a).contains("which is revoked"));
        // The rotate not in: box proves with its key, next pending.
        let (o, a) = run(pending(s.clone()), auth("lap", 1, s.clone(), "box"));
        assert_eq!((o.unwrap().newer, a.unwrap().key), (None, pk(2)));
    }
}