Skip to main content

qualia_core_db/net/qdnf/authority/
verified.rs

1//! Verified credential, authorised contact and installed keys.
2//!
3//! Constructors are crate-private. External crates cannot mint Active/Allow.
4
5#[cfg(not(target_arch = "wasm32"))]
6mod received_finished;
7
8use crate::crypto::network::types::AEAD_KEY_LEN;
9use crate::net::qdnf::errors::QdnfError;
10use crate::net::qdnf::types::{Generation, StrongDigest};
11use zeroize::Zeroize;
12
13use super::policy::{ContactState, TemporalGrant};
14
15/// Credential whose digest bindings were checked by [`super::AuthorityOwner`].
16#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub struct VerifiedCredential {
18    principal: StrongDigest,
19    issuer: StrongDigest,
20    instrument: StrongDigest,
21    generation: Generation,
22}
23
24impl VerifiedCredential {
25    /// Creates a default `VerifiedCredential`.
26    pub fn new(
27        principal: StrongDigest,
28        issuer: StrongDigest,
29        instrument: StrongDigest,
30        generation: Generation,
31    ) -> Result<Self, QdnfError> {
32        if principal.is_zero() || issuer.is_zero() || instrument.is_zero() {
33            return Err(QdnfError::Unauthorized);
34        }
35        if generation == Generation::ZERO {
36            return Err(QdnfError::Unauthorized);
37        }
38        Ok(Self {
39            principal,
40            issuer,
41            instrument,
42            generation,
43        })
44    }
45
46    #[inline]
47    /// Credential principal digest.
48    pub const fn principal(self) -> StrongDigest {
49        self.principal
50    }
51
52    #[inline]
53    /// Credential issuer digest.
54    pub const fn issuer(self) -> StrongDigest {
55        self.issuer
56    }
57
58    #[inline]
59    /// Credential instrument digest.
60    pub const fn instrument(self) -> StrongDigest {
61        self.instrument
62    }
63
64    #[inline]
65    /// Credential generation.
66    pub const fn generation(self) -> Generation {
67        self.generation
68    }
69}
70
71/// Current authorised contact. Inactive states cannot release content.
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub struct AuthorisedContact {
74    local: StrongDigest,
75    remote: StrongDigest,
76    state: ContactState,
77    generation: Generation,
78}
79
80impl AuthorisedContact {
81    /// Creates a default `AuthorisedContact`.
82    pub fn new(
83        local: StrongDigest,
84        remote: StrongDigest,
85        state: ContactState,
86        generation: Generation,
87    ) -> Result<Self, QdnfError> {
88        if local.is_zero() || remote.is_zero() || local == remote {
89            return Err(QdnfError::Unauthorized);
90        }
91        if generation == Generation::ZERO {
92            return Err(QdnfError::Unauthorized);
93        }
94        Ok(Self {
95            local,
96            remote,
97            state,
98            generation,
99        })
100    }
101
102    #[inline]
103    /// Local digest.
104    pub const fn local(self) -> StrongDigest {
105        self.local
106    }
107
108    #[inline]
109    /// Remote digest.
110    pub const fn remote(self) -> StrongDigest {
111        self.remote
112    }
113
114    #[inline]
115    /// Contact state.
116    pub const fn state(self) -> ContactState {
117        self.state
118    }
119
120    #[inline]
121    /// Contact generation.
122    pub const fn generation(self) -> Generation {
123        self.generation
124    }
125
126    /// `Ok(())` when the contact is active.
127    pub fn require_active(&self) -> Result<(), QdnfError> {
128        match self.state {
129            ContactState::Active => Ok(()),
130            ContactState::Blocked => Err(QdnfError::Denied),
131            ContactState::Suspended => Err(QdnfError::Denied),
132            ContactState::Request | ContactState::Consent => Err(QdnfError::Unauthorized),
133        }
134    }
135}
136
137/// Confirmed directional traffic secrets. Not Copy; zeroized on drop.
138pub struct InstalledSessionKeys {
139    generation: Generation,
140    send: [u8; AEAD_KEY_LEN],
141    recv: [u8; AEAD_KEY_LEN],
142    confirmed: bool,
143}
144
145impl InstalledSessionKeys {
146    /// Creates a default `InstalledSessionKeys`.
147    pub fn new(
148        generation: Generation,
149        send: [u8; AEAD_KEY_LEN],
150        recv: [u8; AEAD_KEY_LEN],
151        confirmed: bool,
152    ) -> Result<Self, QdnfError> {
153        if generation == Generation::ZERO || !confirmed {
154            return Err(QdnfError::Unauthorized);
155        }
156        if send == [0u8; AEAD_KEY_LEN] || recv == [0u8; AEAD_KEY_LEN] || send == recv {
157            return Err(QdnfError::CryptoFailure);
158        }
159        Ok(Self {
160            generation,
161            send,
162            recv,
163            confirmed,
164        })
165    }
166
167    #[inline]
168    /// Key generation.
169    pub const fn generation(&self) -> Generation {
170        self.generation
171    }
172
173    #[inline]
174    /// `true` when the keys are confirmed.
175    pub const fn confirmed(&self) -> bool {
176        self.confirmed
177    }
178
179    #[inline]
180    /// Send-direction AEAD key.
181    pub fn send_key(&self) -> &[u8; AEAD_KEY_LEN] {
182        &self.send
183    }
184
185    #[inline]
186    /// Receive-direction AEAD key.
187    pub fn recv_key(&self) -> &[u8; AEAD_KEY_LEN] {
188        &self.recv
189    }
190}
191
192impl Drop for InstalledSessionKeys {
193    fn drop(&mut self) {
194        self.send.zeroize();
195        self.recv.zeroize();
196        self.confirmed = false;
197    }
198}
199
200/// `true` when the grant matches the contact.
201pub fn grant_matches_contact(grant: &TemporalGrant, contact: &AuthorisedContact) -> bool {
202    grant.audience_digest == contact.remote()
203}
204
205#[cfg(test)]
206mod tests {
207    use super::*;
208
209    #[test]
210    fn zero_principal_is_rejected() {
211        assert_eq!(
212            VerifiedCredential::new(
213                StrongDigest::ZERO,
214                StrongDigest([1u8; 48]),
215                StrongDigest([2u8; 48]),
216                Generation(1)
217            ),
218            Err(QdnfError::Unauthorized)
219        );
220    }
221
222    #[test]
223    fn blocked_contact_cannot_be_required_active() {
224        let c = AuthorisedContact::new(
225            StrongDigest([1u8; 48]),
226            StrongDigest([2u8; 48]),
227            ContactState::Blocked,
228            Generation(1),
229        )
230        .unwrap();
231        assert_eq!(c.require_active(), Err(QdnfError::Denied));
232        let grant = TemporalGrant {
233            purpose_digest: StrongDigest([3u8; 48]),
234            audience_digest: StrongDigest([2u8; 48]),
235            authority_generation: 1,
236            not_before_unix: 0,
237            expires_unix: 10,
238            profile: crate::net::qdnf::types::ProfileId::QPR_PQ_1,
239        };
240        assert!(grant_matches_contact(&grant, &c));
241    }
242}