1use super::{
4 ControllerRecognition, DisclosureAction, IndependentAdmission, PolicyError as E,
5 RecognizedController,
6};
7#[cfg(not(target_arch = "wasm32"))]
8use super::{
9 check_recipient_against_independent, encode_decision_ticket_into, DecisionTicketFields,
10 RecognitionAdmin, RecognitionHandle, DECISION_TICKET_BYTES, DISCLOSURE_DECISION_PURPOSE,
11 MAX_DECISION_LEASE_SECS, RECIPIENT_BYTES, RECIPIENT_PURPOSE,
12};
13#[cfg(not(target_arch = "wasm32"))]
14use crate::crypto::network::authority::{AuthoritySource, PinAuthority, PinHandle};
15#[cfg(not(target_arch = "wasm32"))]
16use crate::crypto::network::cose::{self, Binding, KeyIds};
17#[cfg(not(target_arch = "wasm32"))]
18use crate::crypto::network::digest::sha384;
19use crate::crypto::network::entropy::fill_os;
20use crate::crypto::network::errors::CryptoError;
21#[cfg(not(target_arch = "wasm32"))]
22use crate::crypto::network::key_provider::ProviderGrant;
23#[cfg(not(target_arch = "wasm32"))]
24use crate::crypto::network::object_stream::{encode_recipient, RecipientFields, RecipientRecord};
25use crate::net::qdnf::types::OperationId;
26#[cfg(not(target_arch = "wasm32"))]
27use crate::net::qdnf::types::StrongDigest;
28#[cfg(not(target_arch = "wasm32"))]
29use crate::specialized_libs::cryptographic_library::{
30 CryptographicLibrary, PairedSigningRequest, SigningAccess, SigningControl,
31};
32#[cfg(not(target_arch = "wasm32"))]
33use zeroize::Zeroizing;
34
35#[cfg(not(target_arch = "wasm32"))]
37pub struct ManagedSigners<'a> {
38 pub library: &'a CryptographicLibrary,
39 pub control: &'a SigningControl,
40 pub grant: ProviderGrant,
41 pub access: SigningAccess<'a>,
42 pub key_ids: KeyIds,
43 pub controller_epoch: u64,
44 pub ml_key_id: &'a str,
45 pub ed_key_id: &'a str,
46 pub ml_policy_id: &'a str,
47 pub ed_policy_id: &'a str,
48}
49
50#[cfg(not(target_arch = "wasm32"))]
52pub struct IssueRequest<'a> {
53 pub admin: &'a RecognitionAdmin,
54 pub handle: RecognitionHandle,
55 pub decision_pin: PinHandle,
56 pub independent: IndependentAdmission,
57 pub recipient: RecipientFields<'a>,
58 pub transcript: StrongDigest,
59 pub request_id: OperationId,
60 pub challenge: [u8; 32],
61 pub action: DisclosureAction,
62 pub not_before: u64,
63 pub expires: u64,
64 pub audience: &'a [u8],
65 pub signers: ManagedSigners<'a>,
66}
67
68#[cfg(not(target_arch = "wasm32"))]
70pub struct IssueBuffers<'a> {
71 pub recipient: &'a mut [u8],
72 pub recipient_envelope: &'a mut [u8],
73 pub ticket: &'a mut [u8],
74 pub ticket_envelope: &'a mut [u8],
75}
76
77#[cfg(not(target_arch = "wasm32"))]
78#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub struct IssuedLengths {
80 pub recipient: usize,
81 pub recipient_envelope: usize,
82 pub ticket: usize,
83 pub ticket_envelope: usize,
84}
85
86pub(super) fn join_enrollment(
89 recognition: &ControllerRecognition,
90 snapshot: &RecognizedController,
91 independent: &IndependentAdmission,
92 action: DisclosureAction,
93 now: u64,
94) -> Result<RecognizedController, E> {
95 let live = recognition.recognize(
96 snapshot.principal(),
97 independent.source,
98 independent.source_length,
99 independent.policy,
100 independent.publisher,
101 independent.purpose,
102 OperationId(independent.operation),
103 action,
104 now,
105 )?;
106 if live.pin() != snapshot.pin()
107 || live.controller_generation() != snapshot.controller_generation()
108 || live.source() != snapshot.source()
109 || live.source_length() != snapshot.source_length()
110 || live.policy() != snapshot.policy()
111 {
112 return Err(E::Reference);
113 }
114 Ok(live)
115}
116
117pub fn fresh_challenge() -> Result<[u8; 32], E> {
119 let mut challenge = [0u8; 32];
120 fill_os(&mut challenge).map_err(E::Crypto)?;
121 if challenge.iter().all(|b| *b == 0) {
122 return Err(E::Crypto(CryptoError::EntropyFailure));
123 }
124 Ok(challenge)
125}
126
127#[cfg(not(target_arch = "wasm32"))]
132pub fn issue_disclosure(
133 recognition: &mut ControllerRecognition,
134 pins: &PinAuthority,
135 request: IssueRequest<'_>,
136 now: u64,
137 buffers: IssueBuffers<'_>,
138) -> Result<IssuedLengths, E> {
139 issue_inner(recognition, pins, request, now, buffers)
140}
141
142#[cfg(not(target_arch = "wasm32"))]
143fn issue_inner(
144 recognition: &mut ControllerRecognition,
145 pins: &PinAuthority,
146 request: IssueRequest<'_>,
147 now: u64,
148 buffers: IssueBuffers<'_>,
149) -> Result<IssuedLengths, E> {
150 let recognized = recognition.snapshot(request.handle)?;
151 recognized.current_at(now)?;
152 validate_lease(
153 &recognized,
154 request.action,
155 request.not_before,
156 request.expires,
157 )?;
158 if request.challenge.iter().all(|b| *b == 0)
159 || request.audience.is_empty()
160 || request.signers.controller_epoch == 0
161 {
162 return Err(E::Reference);
163 }
164
165 if buffers.recipient.len() < RECIPIENT_BYTES || buffers.ticket.len() < DECISION_TICKET_BYTES {
166 return Err(E::Capacity);
167 }
168 encode_recipient(request.recipient, buffers.recipient).map_err(E::Crypto)?;
169 let record =
170 RecipientRecord::parse(&buffers.recipient[..RECIPIENT_BYTES]).map_err(E::Crypto)?;
171 check_recipient_against_independent(&record, &request.independent)?;
172 join_enrollment(
173 recognition,
174 &recognized,
175 &request.independent,
176 request.action,
177 now,
178 )?;
179 record.valid_at(now).map_err(E::Crypto)?;
180
181 let decision_id = recognition.consume_decision_id(request.admin, request.handle)?;
182 let ticket_fields = DecisionTicketFields {
183 recipient_record_id: record.id(),
184 transcript: request.transcript,
185 request_id: request.request_id,
186 challenge: request.challenge,
187 controller_generation: recognized.controller_generation(),
188 policy: recognized.policy(),
189 action: request.action,
190 not_before: request.not_before,
191 expires: request.expires,
192 decision_id,
193 };
194 encode_decision_ticket_into(ticket_fields, buffers.ticket)?;
195
196 let recipient_id = record.id();
197 let ticket_digest = sha384(&buffers.ticket[..DECISION_TICKET_BYTES]);
198 let mut workspace = cose::Workspace::new();
199 let rec_n = match sign_with_managed(
200 &request.signers,
201 Binding {
202 descriptor_digest: &recipient_id.0,
203 key_ids: request.signers.key_ids,
204 controller_epoch: request.signers.controller_epoch,
205 purpose: RECIPIENT_PURPOSE,
206 audience: request.audience,
207 },
208 now,
209 buffers.recipient_envelope,
210 &mut workspace,
211 ) {
212 Ok(n) => n,
213 Err(e) => {
214 wipe_unpublished(buffers);
215 return Err(e);
216 }
217 };
218 let tkt_n = match sign_with_managed(
219 &request.signers,
220 Binding {
221 descriptor_digest: &ticket_digest.0,
222 key_ids: request.signers.key_ids,
223 controller_epoch: request.signers.controller_epoch,
224 purpose: DISCLOSURE_DECISION_PURPOSE,
225 audience: request.audience,
226 },
227 now,
228 buffers.ticket_envelope,
229 &mut workspace,
230 ) {
231 Ok(n) => n,
232 Err(e) => {
233 wipe_unpublished(buffers);
234 return Err(e);
235 }
236 };
237 super::ticket_package_len(DECISION_TICKET_BYTES, tkt_n)?;
238
239 let rec_binding = Binding {
240 descriptor_digest: &recipient_id.0,
241 key_ids: request.signers.key_ids,
242 controller_epoch: request.signers.controller_epoch,
243 purpose: RECIPIENT_PURPOSE,
244 audience: request.audience,
245 };
246 let tkt_binding = Binding {
247 descriptor_digest: &ticket_digest.0,
248 key_ids: request.signers.key_ids,
249 controller_epoch: request.signers.controller_epoch,
250 purpose: DISCLOSURE_DECISION_PURPOSE,
251 audience: request.audience,
252 };
253 if let Err(e) = pins
254 .verify_source(
255 AuthoritySource::LocalPin(recognized.pin()),
256 &buffers.recipient_envelope[..rec_n],
257 &rec_binding,
258 now,
259 &mut workspace,
260 )
261 .and_then(|_| {
262 pins.verify_source(
263 AuthoritySource::LocalPin(request.decision_pin),
264 &buffers.ticket_envelope[..tkt_n],
265 &tkt_binding,
266 now,
267 &mut workspace,
268 )
269 })
270 .map_err(E::Crypto)
271 {
272 wipe_unpublished(buffers);
273 return Err(e);
274 }
275
276 Ok(IssuedLengths {
277 recipient: RECIPIENT_BYTES,
278 recipient_envelope: rec_n,
279 ticket: DECISION_TICKET_BYTES,
280 ticket_envelope: tkt_n,
281 })
282}
283
284#[cfg(not(target_arch = "wasm32"))]
285fn validate_lease(
286 recognized: &RecognizedController,
287 action: DisclosureAction,
288 not_before: u64,
289 expires: u64,
290) -> Result<(), E> {
291 if expires <= not_before {
292 return Err(E::Encoding);
293 }
294 let lease = expires - not_before;
295 if lease > MAX_DECISION_LEASE_SECS || lease > recognized.max_decision_lifetime() {
296 return Err(E::Limit);
297 }
298 if expires > recognized.expires_unix() {
299 return Err(E::Limit);
300 }
301 if !recognized.allowed_ops().contains(action) {
302 return Err(E::Reference);
303 }
304 Ok(())
305}
306
307#[cfg(not(target_arch = "wasm32"))]
308fn wipe_unpublished(buffers: IssueBuffers<'_>) {
309 buffers.recipient_envelope.fill(0);
310 buffers.ticket_envelope.fill(0);
311 buffers.ticket.fill(0);
312}
313
314#[cfg(not(target_arch = "wasm32"))]
316pub(super) fn sign_with_managed(
317 signers: &ManagedSigners<'_>,
318 binding: Binding<'_>,
319 now: u64,
320 out: &mut [u8],
321 workspace: &mut cose::Workspace,
322) -> Result<usize, E> {
323 let request = PairedSigningRequest {
324 ml_key_id: signers.ml_key_id,
325 ed_key_id: signers.ed_key_id,
326 ml_policy_id: signers.ml_policy_id,
327 ed_policy_id: signers.ed_policy_id,
328 binding,
329 grant: signers.grant,
330 access: signers.access,
331 };
332 let clock = || now;
333 let cap = signers
334 .library
335 .begin_paired_signing(request, signers.control, &clock)
336 .map_err(E::Crypto)?;
337 let mut scratch = Zeroizing::new([0u8; cose::MAX_ENVELOPE_BYTES]);
338 let n = match cose::sign_pair(&binding, cap, workspace, &mut scratch[..]) {
339 Ok(n) => n,
340 Err(e) => return Err(E::Crypto(e)),
341 };
342 let dest = out.get_mut(..n).ok_or(E::Capacity)?;
343 dest.copy_from_slice(&scratch[..n]);
344 Ok(n)
345}