Skip to main content

oxabl_cache_crypto/
trust.rs

1//! Trust tiers: which signer a reader accepts for which namespace.
2
3use oxabl_cache_format::{Namespace, PublicKey};
4
5use crate::dictionary::Dictionary;
6use crate::error::{Error, Result};
7use crate::record_entry::Entry;
8use crate::record_open::OpenedRecord;
9use crate::roster_verify::VerifiedRoster;
10use crate::segment_open::SegmentExpectation;
11
12/// Where an accepted entry came from, for callers that keep a local copy.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct Provenance {
15    pub tier: Namespace,
16    pub signer: PublicKey,
17    pub epoch: u32,
18    pub roster_version: u64,
19}
20
21/// A record whose signer the current roster accepts for its namespace.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct AcceptedRecord {
24    entry: Entry,
25    provenance: Provenance,
26}
27
28impl OpenedRecord {
29    /// Applies the trust tier rules under the current roster.
30    ///
31    /// A `trusted` record needs a signer that is a key of a `ci` member. A `personal` record
32    /// needs the reader's own signing key, and the reader must be in the roster. A record
33    /// that names a newer roster version than the current one is rejected.
34    pub fn accept(self, roster: &VerifiedRoster, reader: &PublicKey) -> Result<AcceptedRecord> {
35        let signer = self.entry.signer;
36        let accepted = match self.context.namespace {
37            Namespace::Trusted => roster.is_ci_key(&signer),
38            Namespace::Personal => signer == *reader && roster.member_by_sign_key(reader).is_some(),
39        };
40        if !accepted {
41            return Err(Error::SignerNotAccepted);
42        }
43        if self.entry.fields.roster_version > roster.version() {
44            return Err(Error::RosterVersionAhead);
45        }
46        Ok(AcceptedRecord {
47            provenance: Provenance {
48                tier: self.context.namespace,
49                signer,
50                epoch: self.context.epoch,
51                roster_version: self.entry.fields.roster_version,
52            },
53            entry: self.entry,
54        })
55    }
56}
57
58impl AcceptedRecord {
59    pub fn entry(&self) -> &Entry {
60        &self.entry
61    }
62
63    pub fn provenance(&self) -> Provenance {
64        self.provenance
65    }
66
67    /// What opening the record's segment must satisfy. `dictionary` must be the dictionary
68    /// the record names, or `None` when it names none.
69    pub fn segment_expectation<'a>(
70        &self,
71        dictionary: Option<&'a Dictionary>,
72    ) -> Result<SegmentExpectation<'a>> {
73        let named = self.entry.fields.dict_id;
74        if named != dictionary.map(Dictionary::id) {
75            return Err(Error::Mismatch("dictionary"));
76        }
77        Ok(SegmentExpectation {
78            id: self.entry.fields.segment_id,
79            sealed_len: u64::from(self.entry.fields.segment_len),
80            plain_len: self.entry.fields.plain_len,
81            dictionary,
82        })
83    }
84}