qualia_core_db/bundle/stream/protection/
wrap.rs1use crate::bundle::stream::policy::remote::{
3 ControllerRecognition, DisclosureAction, RemoteDisclosureGrant,
4};
5use crate::bundle::stream::policy::{BoundClosure, LocalWholeSourceGrant, PolicyError};
6use crate::crypto::network::{
7 authority::{AuthorityReceipt, AuthoritySource, PinAuthority, PinHandle},
8 cose,
9 errors::CryptoError,
10 object_stream::{RecipientRecord, RECIPIENT_PURPOSE, WRAP_BYTES},
11};
12use crate::net::qdnf::{
13 authority::{AuthorityBinding, AuthorityOwner, CurrentAuthorityGuard},
14 errors::QdnfError,
15};
16use crate::specialized_libs::cryptographic_library::ManagedObjectPublication;
17use std::sync::Mutex;
18use zeroize::Zeroize;
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
21pub enum WrapError {
22 Unauthorized,
23 Closed,
24 Spent,
25 Capacity,
26}
27
28pub struct RecipientAuthorityProof<'a> {
30 pub pin: PinHandle,
31 pub key_ids: cose::KeyIds,
32 pub epoch: u64,
33 pub envelope: &'a [u8],
34}
35
36pub fn authorize_and_wrap(
39 publication: &mut ManagedObjectPublication<'_>,
40 grant: &mut LocalWholeSourceGrant<'_, '_>,
41 pins: &PinAuthority,
42 clock: impl Fn() -> u64,
43 record: RecipientRecord<'_>,
44 proof: RecipientAuthorityProof<'_>,
45 workspace: &mut cose::Workspace,
46 out: &mut [u8],
47) -> Result<usize, WrapError> {
48 struct Wipe<'a> {
49 out: &'a mut [u8],
50 keep: bool,
51 }
52 impl Drop for Wipe<'_> {
53 fn drop(&mut self) {
54 if !self.keep {
55 self.out.zeroize();
56 }
57 }
58 }
59 let mut wipe = Wipe { out, keep: false };
60 if wipe.out.len() < WRAP_BYTES {
61 return Err(WrapError::Capacity);
62 }
63 let expected = grant.binding();
64 let n = grant
65 .accept_local_scoped(
66 pins,
67 || clock(),
68 |gate| {
69 gate.recheck()?;
70 match_grant(record, expected, gate.closure())?;
71 let now = clock();
72 record.valid_at(now)?;
73 let auth = record.id();
74 let binding = cose::Binding {
75 descriptor_digest: &auth.0,
76 key_ids: proof.key_ids,
77 controller_epoch: proof.epoch,
78 purpose: RECIPIENT_PURPOSE,
79 audience: &record.fields().publisher.0,
80 };
81 let receipt = pins.verify_source(
82 AuthoritySource::LocalPin(proof.pin),
83 proof.envelope,
84 &binding,
85 now,
86 workspace,
87 )?;
88 pins.recheck(&receipt, &auth.0, now)?;
89 publication
90 .wrap_for(record, wipe.out)
91 .map_err(PolicyError::from)
92 },
93 )
94 .map_err(map_policy)?;
95 wipe.keep = true;
96 Ok(n)
97}
98
99#[allow(clippy::too_many_arguments)]
103pub fn authorize_and_wrap_remote(
104 publication: &mut ManagedObjectPublication<'_>,
105 grant: &RemoteDisclosureGrant<'_>,
106 guard: &CurrentAuthorityGuard<'_>,
107 owner: &Mutex<AuthorityOwner>,
108 pins: &PinAuthority,
109 recognition: &ControllerRecognition,
110 receipt: &AuthorityReceipt,
111 clock: impl Fn() -> u64,
112 record: RecipientRecord<'_>,
113 proof: RecipientAuthorityProof<'_>,
114 audience: &[u8],
115 workspace: &mut cose::Workspace,
116 out: &mut [u8],
117) -> Result<usize, WrapError> {
118 struct Wipe<'a> {
119 out: &'a mut [u8],
120 keep: bool,
121 }
122 impl Drop for Wipe<'_> {
123 fn drop(&mut self) {
124 if !self.keep {
125 self.out.zeroize();
126 }
127 }
128 }
129 let mut wipe = Wipe { out, keep: false };
130 if wipe.out.len() < WRAP_BYTES {
131 return Err(WrapError::Capacity);
132 }
133 let now = clock();
134 grant
135 .recheck(
136 guard,
137 owner,
138 pins,
139 recognition,
140 receipt,
141 now,
142 DisclosureAction::MetadataRelease,
143 )
144 .map_err(map_policy)?;
145 match_remote_grant(record, grant)?;
146 record.valid_at(now).map_err(|e| map_policy(PolicyError::from(e)))?;
147 let auth = record.id();
148 let binding = cose::Binding {
149 descriptor_digest: &auth.0,
150 key_ids: proof.key_ids,
151 controller_epoch: proof.epoch,
152 purpose: RECIPIENT_PURPOSE,
153 audience,
154 };
155 let verified = pins
156 .verify_source(
157 AuthoritySource::LocalPin(proof.pin),
158 proof.envelope,
159 &binding,
160 now,
161 workspace,
162 )
163 .map_err(|e| map_policy(PolicyError::from(e)))?;
164 pins.recheck(&verified, &auth.0, now)
165 .map_err(|e| map_policy(PolicyError::from(e)))?;
166 let n = publication
167 .wrap_for(record, wipe.out)
168 .map_err(|e| map_policy(PolicyError::from(e)))?;
169 wipe.keep = true;
170 Ok(n)
171}
172
173fn match_remote_grant(
174 record: RecipientRecord<'_>,
175 grant: &RemoteDisclosureGrant<'_>,
176) -> Result<(), WrapError> {
177 let f = record.fields();
178 let b = grant.binding();
179 let r = grant.recognized();
180 if f.recipient != b.recipient
181 || f.purpose != b.purpose
182 || f.operation != b.operation.0
183 || f.instrument != b.instrument
184 || f.scope != b.scope
185 || f.source != r.source()
186 || f.source_length != r.source_length()
187 || f.policy != r.policy()
188 || f.publisher != r.publisher()
189 || f.purpose != r.purpose()
190 || f.operation != r.operation().0
191 {
192 Err(WrapError::Unauthorized)
193 } else {
194 Ok(())
195 }
196}
197
198fn match_grant(
199 record: RecipientRecord<'_>,
200 expected: AuthorityBinding,
201 closure: &BoundClosure<'_>,
202) -> Result<(), PolicyError> {
203 let f = record.fields();
204 let m = closure.closure().manifest().fields();
205 if f.recipient != expected.recipient
206 || f.publisher != expected.principal
207 || f.purpose != expected.purpose
208 || f.operation != expected.operation.0
209 || f.instrument != expected.instrument
210 || f.scope != expected.scope
211 || f.closure != closure.closure().manifest_digest()
212 || f.source != m.source
213 || f.source_length != m.source_length
214 || f.policy != m.policy
215 || f.consumer != m.consumer
216 {
217 Err(PolicyError::Crypto(CryptoError::Unauthorized))
218 } else {
219 Ok(())
220 }
221}
222
223fn map_policy(e: PolicyError) -> WrapError {
224 match e {
225 PolicyError::Spent => WrapError::Spent,
226 PolicyError::Capacity => WrapError::Capacity,
227 PolicyError::Crypto(CryptoError::Closed) | PolicyError::Authority(QdnfError::Closed) => {
228 WrapError::Closed
229 }
230 PolicyError::Crypto(CryptoError::Capacity)
231 | PolicyError::Authority(QdnfError::Capacity) => WrapError::Capacity,
232 _ => WrapError::Unauthorized,
233 }
234}