qualia_core_db/bundle/stream/policy/
authenticated.rs1use super::{
2 CLOSURE_PURPOSE, ManifestView, PUBLICATION_PURPOSE, PolicyError as E, PublicationProof,
3 decode_manifest,
4};
5use crate::bundle::stream::descriptor::{
6 CandidateIdentity, DigestDomain, decode_body, domain_digest,
7};
8use crate::crypto::network::{
9 authority::{AuthorityReceipt, AuthoritySource, PinAuthority, PinHandle},
10 cose,
11};
12use crate::net::qdnf::types::StrongDigest;
13use crate::semantic_instruments::resolve::bounded_closure::ClosureCandidate;
14
15pub struct AuthenticatedClosure<'a> {
18 candidate: ClosureCandidate<'a, 'a>,
19 manifest: ManifestView<'a>,
20 receipt: AuthorityReceipt,
21 digest: StrongDigest,
22}
23pub struct BoundClosure<'a> {
27 closure: AuthenticatedClosure<'a>,
28 identity: CandidateIdentity,
29 publisher_receipt: AuthorityReceipt,
30}
31pub fn authenticate_closure<'a>(
34 candidate: ClosureCandidate<'a, 'a>,
35 manifest: &'a [u8],
36 pins: &PinAuthority,
37 pin: PinHandle,
38 key_ids: cose::KeyIds,
39 epoch: u64,
40 audience: &[u8],
41 envelope: &[u8],
42 now: u64,
43 workspace: &mut cose::Workspace,
44) -> Result<AuthenticatedClosure<'a>, E> {
45 let manifest = decode_manifest(manifest)?;
46 manifest.matches(&candidate)?;
47 let digest = manifest.digest();
48 let binding = cose::Binding {
49 descriptor_digest: &digest.0,
50 key_ids,
51 controller_epoch: epoch,
52 purpose: CLOSURE_PURPOSE,
53 audience,
54 };
55 let receipt = pins.verify_source(
56 AuthoritySource::LocalPin(pin),
57 envelope,
58 &binding,
59 now,
60 workspace,
61 )?;
62 if receipt.principal() != manifest.fields().issuer {
63 return Err(E::Reference);
64 }
65 Ok(AuthenticatedClosure {
66 candidate,
67 manifest,
68 receipt,
69 digest,
70 })
71}
72impl<'a> AuthenticatedClosure<'a> {
73 pub const fn manifest(&self) -> ManifestView<'a> {
74 self.manifest
75 }
76 pub const fn manifest_digest(&self) -> StrongDigest {
77 self.digest
78 }
79 pub fn candidate(&self) -> &ClosureCandidate<'a, 'a> {
80 &self.candidate
81 }
82 pub fn recheck(&self, pins: &PinAuthority, now: u64) -> Result<(), E> {
83 pins.recheck(&self.receipt, &self.digest.0, now)
84 .map_err(E::from)
85 }
86 pub fn bind_descriptor(
87 self,
88 identity: CandidateIdentity,
89 body: &[u8],
90 scratch: &mut [u8],
91 pins: &PinAuthority,
92 proof: PublicationProof<'_>,
93 now: u64,
94 workspace: &mut cose::Workspace,
95 ) -> Result<BoundClosure<'a>, E> {
96 let body = decode_body(body).map_err(|_| E::Descriptor)?;
97 let body_id = domain_digest(DigestDomain::Body, body.encoded_bytes(), scratch)
98 .map_err(|_| E::Descriptor)?;
99 let context = body.context();
100 let context_id = domain_digest(DigestDomain::Context, context.encoded_bytes(), scratch)
101 .map_err(|_| E::Descriptor)?;
102 let parameters = context.parameters();
103 let fields = self.manifest.fields();
104 if body_id != identity.descriptor_id()
105 || context_id != identity.context_id()
106 || body.context_id() != context_id
107 || parameters.generation != identity.generation()
108 || context.entry_count() != identity.entry_count() as usize
109 || parameters.semantic_contract != self.digest
110 || parameters.policy != fields.policy
111 || parameters.consumer_profile != fields.consumer
112 || context.source().source_digest != fields.source
113 || context.source().length != fields.source_length
114 {
115 return Err(E::Descriptor);
116 }
117 self.recheck(pins, now)?;
118 let binding = cose::Binding {
119 descriptor_digest: &body_id.0,
120 key_ids: proof.key_ids,
121 controller_epoch: proof.epoch,
122 purpose: PUBLICATION_PURPOSE,
123 audience: proof.audience,
124 };
125 let publisher_receipt = pins.verify_source(
126 AuthoritySource::LocalPin(proof.pin),
127 proof.envelope,
128 &binding,
129 now,
130 workspace,
131 )?;
132 Ok(BoundClosure {
133 closure: self,
134 identity,
135 publisher_receipt,
136 })
137 }
138}
139impl<'a> BoundClosure<'a> {
140 pub const fn identity(&self) -> CandidateIdentity {
141 self.identity
142 }
143 pub fn closure(&self) -> &AuthenticatedClosure<'a> {
144 &self.closure
145 }
146 pub fn recheck(&self, pins: &PinAuthority, now: u64) -> Result<(), E> {
147 self.closure.recheck(pins, now)?;
148 pins.recheck(
149 &self.publisher_receipt,
150 &self.identity.descriptor_id().0,
151 now,
152 )
153 .map_err(E::from)
154 }
155}