Skip to main content

gent_crypto/authority/succession/
readiness.rs

1//! Successor readiness, authenticated before the current owner stops signing.
2//! Receipts prove installed configuration origins. The successor's signatures
3//! additionally attest that its runtime checked the required local data links.
4use super::*;
5use crate::stream::Receipt;
6
7pub const MAX_READINESS_BYTES: usize = 1536 * 1024;
8const NODE_DOMAIN: &[u8] = b"gent/owner-readiness/v1\0";
9const KEY_DOMAIN: &[u8] = b"gent/owner-readiness-key/v1\0";
10const GRANT_DOMAIN: &[u8] = b"gent/owner-readiness-grant/v1\0";
11
12#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
13#[serde(deny_unknown_fields)]
14pub struct Peer {
15    pub reader: String,
16    pub grant: Digest,
17    pub receipt: Receipt,
18}
19
20#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
21#[serde(deny_unknown_fields)]
22pub struct Readiness {
23    pub version: u32,
24    pub candidate: Digest,
25    pub issued_at: u64,
26    pub expires_at: u64,
27    pub peers: Vec<Peer>,
28}
29
30#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
31#[serde(deny_unknown_fields)]
32pub struct SignedReadiness {
33    pub readiness: Readiness,
34    pub node_signature: Vec<u8>,
35    pub key_signature: Vec<u8>,
36}
37
38fn grant_digest(grant: &SignedConfigurationGrant) -> Result<Digest> {
39    digest(GRANT_DOMAIN, grant, MAX_CONFIGURATION_GRANT_BYTES)
40}
41
42impl SignedReadiness {
43    pub fn encode(&self) -> Result<Vec<u8>> {
44        ensure!(
45            self.readiness.peers.len() <= 1023,
46            "too many readiness peers"
47        );
48        encode(self, MAX_READINESS_BYTES)
49    }
50
51    pub fn decode(wire: &[u8]) -> Result<Self> {
52        let report: Self = decode(wire, MAX_READINESS_BYTES)?;
53        report.encode()?;
54        Ok(report)
55    }
56
57    /// Grants must come from the current owner's independently saved approvals.
58    /// Their order matches the report's canonical ascending reader order.
59    pub fn verify(
60        &self,
61        state: &VerifiedState,
62        candidate: &SignedOwnerCandidate,
63        grants: &[SignedConfigurationGrant],
64        now: u64,
65    ) -> Result<()> {
66        candidate.verify(state, now)?;
67        let r = &self.readiness;
68        ensure!(
69            r.version == 1
70                && r.candidate == candidate.digest()?
71                && r.issued_at >= candidate.candidate.issued_at
72                && r.expires_at == candidate.candidate.expires_at,
73            "readiness belongs to another candidate or approval window"
74        );
75        fresh(r.issued_at, r.expires_at, now)?;
76        let expected: Vec<_> = state
77            .state()
78            .nodes
79            .keys()
80            .filter(|id| id.as_str() != candidate.candidate.node.id)
81            .collect();
82        ensure!(
83            r.peers.len() == expected.len() && grants.len() == expected.len(),
84            "readiness must cover every enrolled peer exactly once"
85        );
86        let mut groups = BTreeSet::new();
87        for ((peer, reader), grant) in r.peers.iter().zip(expected).zip(grants) {
88            ensure!(
89                &peer.reader == reader,
90                "readiness peers are missing, duplicated or unordered"
91            );
92            grant.verify_owner_candidate(candidate)?;
93            grant.verify_initial(state, now)?;
94            let g = &grant.grant;
95            ensure!(
96                g.reader == peer.reader
97                    && peer.grant == grant_digest(grant)?
98                    && peer.receipt.group() == g.group
99                    && peer.receipt.sequence() == 0
100                    && peer.receipt.digest() == g.origin
101                    && groups.insert(g.group.as_slice()),
102                "readiness does not match the approved configuration origin"
103            );
104            peer.receipt.verify(&g.reader_key)?;
105        }
106        verify(
107            &candidate.candidate.node.signing_key,
108            NODE_DOMAIN,
109            r,
110            &self.node_signature,
111            MAX_READINESS_BYTES,
112        )?;
113        verify(
114            &candidate.candidate.owner_key,
115            KEY_DOMAIN,
116            r,
117            &self.key_signature,
118            MAX_READINESS_BYTES,
119        )?;
120        self.encode()?;
121        Ok(())
122    }
123}
124
125impl NodeIdentity {
126    /// The trusted runtime must check durable local links and keys before signing.
127    pub fn sign_owner_readiness(
128        &self,
129        state: &VerifiedState,
130        key: &OwnerKey,
131        candidate: &SignedOwnerCandidate,
132        connections: &[(SignedConfigurationGrant, Receipt)],
133        now: u64,
134    ) -> Result<SignedReadiness> {
135        ensure!(
136            candidate.candidate.node.id == self.id()
137                && candidate.candidate.node.signing_key == self.public_key()
138                && candidate.candidate.owner_key == key.public_key(),
139            "readiness requires the candidate's node and owner keys"
140        );
141        let mut ordered: Vec<_> = connections.iter().collect();
142        ordered.sort_by(|a, b| a.0.grant.reader.cmp(&b.0.grant.reader));
143        let peers = ordered
144            .iter()
145            .map(|(grant, receipt)| {
146                Ok(Peer {
147                    reader: grant.grant.reader.clone(),
148                    grant: grant_digest(grant)?,
149                    receipt: receipt.clone(),
150                })
151            })
152            .collect::<Result<_>>()?;
153        let readiness = Readiness {
154            version: 1,
155            candidate: candidate.digest()?,
156            issued_at: now,
157            expires_at: candidate.candidate.expires_at,
158            peers,
159        };
160        let signed = SignedReadiness {
161            node_signature: self
162                .key
163                .sign(&signing_input(
164                    NODE_DOMAIN,
165                    &readiness,
166                    MAX_READINESS_BYTES,
167                )?)
168                .to_bytes()
169                .to_vec(),
170            key_signature: key
171                .0
172                .sign(&signing_input(KEY_DOMAIN, &readiness, MAX_READINESS_BYTES)?)
173                .to_bytes()
174                .to_vec(),
175            readiness,
176        };
177        let grants: Vec<_> = ordered.iter().map(|(g, _)| g.clone()).collect();
178        signed.verify(state, candidate, &grants, now)?;
179        Ok(signed)
180    }
181}