Skip to main content

gent_daemon/store/handoff/
streams.rs

1//! Trusted owner operations used to prepare connections before freezing signing.
2use super::*;
3use crate::encrypted::ordered::{
4    OrderedEndpoint,
5    configuration::{
6        ConfigurationInvitation,
7        standby::{self, ConfigurationPreparation},
8    },
9};
10use gent_crypto::{SignedConfigurationGrant, stream::Receipt};
11
12impl Store {
13    pub fn prepare_owner_configuration(
14        &self,
15        credential: &str,
16        candidate_id: &str,
17        request: Uuid,
18        peer: (&str, &[u8]),
19    ) -> Result<ConfigurationPreparation> {
20        ensure!(hash(credential) == self.owner_hash, "unauthorized");
21        let _lock = config::state_lock(&self.home, "owner-handoff.lock")?;
22        let (_, candidate) = read_key(&candidate_path(&self.home, candidate_id)?)?;
23        let wire: Vec<u8> = self.db.query_row(
24            "SELECT candidate FROM owner_candidates WHERE id=?1",
25            [candidate_id],
26            |r| r.get(0),
27        )?;
28        ensure!(candidate.encode()? == wire, "owner candidate changed");
29        OrderedEndpoint::open_runtime(&self.home, &self.identity.node)?
30            .prepare_standby_configuration(request, &candidate, peer, now() as u64)
31    }
32
33    /// Authorize one exact Welcome durably. Once a handoff is issued only
34    /// already committed authorizations may be returned; no new grant is signed.
35    pub fn authorize_owner_configuration(
36        &mut self,
37        credential: &str,
38        preparation: &ConfigurationPreparation,
39    ) -> Result<SignedConfigurationGrant> {
40        ensure!(hash(credential) == self.owner_hash, "unauthorized");
41        let tx = self
42            .db
43            .transaction_with_behavior(TransactionBehavior::Immediate)?;
44        let state = self.identity.state(&tx)?;
45        let candidate = &preparation.candidate;
46        ensure!(
47            candidate.candidate.node.id != self.identity.node.id(),
48            "owner handoff requires another node"
49        );
50        for (id, key) in [
51            (
52                &candidate.candidate.node.id,
53                &candidate.candidate.node.signing_key,
54            ),
55            (&preparation.reader, &preparation.reader_key),
56        ] {
57            ensure!(
58                state
59                    .state()
60                    .nodes
61                    .get(id)
62                    .is_some_and(|n| &n.signing_key == key),
63                "standby endpoint is no longer enrolled"
64            );
65        }
66        let previous: Option<(Vec<u8>, Vec<u8>)> = tx
67            .query_row(
68                "SELECT candidate,grant FROM owner_configuration_grants WHERE group_id=?1",
69                [preparation.packet.group()],
70                |r| Ok((r.get(0)?, r.get(1)?)),
71            )
72            .optional()?;
73        if let Some((pinned, wire)) = previous {
74            ensure!(
75                pinned == candidate.encode()?,
76                "standby owner candidate changed"
77            );
78            let grant = SignedConfigurationGrant::decode(&wire)?;
79            preparation.verify_grant(&grant)?;
80            ensure!(
81                &grant.grant.anchor == state.anchor()
82                    && state
83                        .state()
84                        .owner_transitions
85                        .starts_with(&grant.grant.owner_transitions),
86                "standby grant authority changed"
87            );
88            return Ok(grant);
89        }
90        let owner = &*self.identity.signing_owner(&tx)?;
91        let timestamp = requests::clock(&tx, now())? as u64;
92        let grant = owner.authorize_standby_configuration_stream(
93            &state,
94            candidate,
95            preparation.packet.group(),
96            preparation.packet.digest()?,
97            &preparation.reader,
98            timestamp,
99        )?;
100        preparation.verify_grant(&grant)?;
101        let candidate = candidate.encode()?;
102        let wire = grant.encode()?;
103        standby::capacity(
104            &tx,
105            candidate.len() + wire.len() + preparation.packet.group().len(),
106        )?;
107        tx.execute(
108            "INSERT INTO owner_configuration_grants VALUES(?1,?2,?3)",
109            params![preparation.packet.group(), candidate, wire],
110        )?;
111        tx.commit()?;
112        Ok(grant)
113    }
114
115    pub fn install_owner_configuration(
116        &self,
117        credential: &str,
118        request: Uuid,
119        grant: &SignedConfigurationGrant,
120    ) -> Result<ConfigurationInvitation> {
121        ensure!(hash(credential) == self.owner_hash, "unauthorized");
122        let _lock = config::state_lock(&self.home, "owner-handoff.lock")?;
123        let wire: Vec<u8> = self.db.query_row(
124            "SELECT candidate FROM configuration_preparations WHERE id=?1",
125            [request.to_string()],
126            |r| r.get(0),
127        )?;
128        let candidate = SignedOwnerCandidate::decode(&wire)?;
129        let (_, pinned) = read_key(&candidate_path(&self.home, &candidate.candidate.id)?)?;
130        ensure!(candidate == pinned, "owner candidate changed");
131        OrderedEndpoint::open_runtime(&self.home, &self.identity.node)?
132            .install_standby_configuration(request, grant, now() as u64)
133    }
134
135    /// The receipt proves this reader installed the exact origin durably. It
136    /// proves transport readiness only, never acceptance of an owner handoff.
137    pub fn receive_owner_configuration(
138        &self,
139        credential: &str,
140        invitation: &ConfigurationInvitation,
141    ) -> Result<Receipt> {
142        ensure!(hash(credential) == self.owner_hash, "unauthorized");
143        ensure!(
144            invitation.grant.grant.owner_candidate.is_some(),
145            "expected a standby configuration grant"
146        );
147        let mut endpoint = OrderedEndpoint::open_runtime(&self.home, &self.identity.node)?;
148        endpoint.join_configuration(invitation, now() as u64)?;
149        endpoint.receipt(invitation.packet.group(), now() as u64)
150    }
151
152    pub fn confirm_owner_configuration(&self, credential: &str, receipt: &Receipt) -> Result<()> {
153        ensure!(hash(credential) == self.owner_hash, "unauthorized");
154        let wire: Vec<u8> = self.db.query_row(
155            "SELECT grant FROM configuration_streams WHERE group_id=?1",
156            [receipt.group()],
157            |r| r.get(0),
158        )?;
159        let grant = SignedConfigurationGrant::decode(&wire)?;
160        ensure!(
161            grant.grant.owner_candidate.is_some(),
162            "expected a standby configuration grant"
163        );
164        OrderedEndpoint::open_runtime(&self.home, &self.identity.node)?.confirm(
165            receipt,
166            &grant.grant.reader,
167            now() as u64,
168        )
169    }
170}