1pub mod envelope;
8pub mod grants;
9pub mod help;
10pub mod mailbox;
11pub mod pairing;
12pub mod permit;
13
14pub use envelope::{
15 forward_ciphertext, generic_log_contains_medical_payload, inherit_response_label,
16 intermediary_decrypts, restore_labelled_backup, seal_envelope, verify_envelope,
17 visible_recipients, ClinicalBoundary, GenericLog, GenericLogRecord, IntermediaryRole,
18 PayloadEnvelope,
19};
20pub use grants::{CareGrant, CareGrantKind, GrantTable, MAX_VISIBLE};
21pub use help::{
22 admit_confidential_help, notify_guardian, notify_guardian_automatically, CapacityMandatePolicy,
23};
24pub use mailbox::{
25 digest_attachment_pages, enqueue_offline, export_interchange, network_asserts_clinical_review,
26 patient_record_associated, release_queued, stream_attachment, InterchangeAdapter, Mailbox,
27 MailboxSlot, MailboxState, OfflineQueue, OfflineSlot, MAILBOX_CIPHERTEXT_BYTES,
28 STREAM_PAGE_BYTES,
29};
30pub use pairing::{
31 diagnosis_discovery_allowed, public_patient_index_allowed, publish_relationship,
32 vip_association_indexed, ClinicalPair, PairingTable,
33};
34pub use permit::{PermitTable, StandingPermit};
35
36use crate::net::qdnf::authority::{ContactState, PolicyOutcome};
37use crate::net::qdnf::errors::QdnfError;
38
39pub const MAX_SLOTS: usize = 8;
41
42fn first_empty<T: Copy>(slots: &[Option<T>; MAX_SLOTS]) -> Option<usize> {
43 let mut i = 0usize;
44 while i < MAX_SLOTS {
45 if slots[i].is_none() {
46 return Some(i);
47 }
48 i += 1;
49 }
50 None
51}
52
53fn admit_outcome(outcome: PolicyOutcome) -> Result<(), QdnfError> {
55 match outcome {
56 PolicyOutcome::Allow => Ok(()),
57 PolicyOutcome::Deny => Err(QdnfError::Denied),
58 PolicyOutcome::Challenge => Err(QdnfError::Challenge),
59 PolicyOutcome::NeedsHuman => Err(QdnfError::NeedsHuman),
60 PolicyOutcome::Incomplete => Err(QdnfError::Incomplete),
61 PolicyOutcome::Error => Err(QdnfError::Unauthorized),
62 }
63}
64
65fn require_active(state: ContactState) -> Result<(), QdnfError> {
66 match state {
67 ContactState::Active => Ok(()),
68 ContactState::Blocked | ContactState::Suspended => Err(QdnfError::Denied),
69 ContactState::Request | ContactState::Consent => Err(QdnfError::Unauthorized),
70 }
71}
72
73fn require_active_mutual(patient: ContactState, clinician: ContactState) -> Result<(), QdnfError> {
74 require_active(patient)?;
75 require_active(clinician)
76}
77
78#[cfg(test)]
79mod tests {
80 use super::*;
81 use crate::net::qdnf::authority::{ContactState, PolicyOutcome, TemporalGrant};
82 use crate::net::qdnf::errors::QdnfError;
83 use crate::net::qdnf::policy_labels::{
84 encode_label_into, join_one, project_sink, verify_label, Confidentiality, DerivedSink,
85 JobLabelContext, LabelFields, VerifiedLabel, NO_PUBLIC_INDEX, NO_TRAINING,
86 };
87 use crate::net::qdnf::types::{ProfileId, StrongDigest};
88
89 fn d(tag: u8) -> StrongDigest {
90 let mut x = StrongDigest::ZERO;
91 x.0[0] = tag;
92 x.0[47] = 0xE1;
93 x
94 }
95
96 fn grant_until(audience: StrongDigest, exp: u64) -> TemporalGrant {
97 TemporalGrant {
98 purpose_digest: d(0x11),
99 audience_digest: audience,
100 authority_generation: 1,
101 not_before_unix: 0,
102 expires_unix: exp,
103 profile: ProfileId::QDNF_CRYPTO_1,
104 }
105 }
106
107 fn verified(conf: Confidentiality, iss: StrongDigest, audience: StrongDigest) -> VerifiedLabel {
108 let mut fields = LabelFields::request(conf, iss);
109 fields.audience = audience;
110 fields.restriction_bits = NO_TRAINING | NO_PUBLIC_INDEX;
111 let mut buf = [0u8; 256];
112 let n = encode_label_into(&fields, &mut buf).unwrap();
113 verify_label(fields, &buf[..n]).unwrap()
114 }
115
116 #[test]
117 fn e14_7_offline_queue_expiry_stays_sealed() {
118 let mut q = OfflineQueue::new();
119 let slot = enqueue_offline(
120 &mut q,
121 OfflineSlot {
122 recipient: d(2),
123 expires_unix: 50,
124 ciphertext_len: 16,
125 ciphertext_digest: d(9),
126 },
127 10,
128 )
129 .unwrap();
130 assert_eq!(
131 release_queued(&mut q, slot, 50, ContactState::Active, ContactState::Active),
132 Err(QdnfError::Expired)
133 );
134 assert_eq!(q.get(slot).unwrap().ciphertext_digest, d(9));
135 assert_eq!(q.get(slot).unwrap().ciphertext_len, 16);
136 }
137
138 #[test]
139 fn e14_7_clinician_departure_revoke_is_denied() {
140 let mut grants = GrantTable::new();
141 let clinician = d(2);
142 grants
143 .add(CareGrant {
144 kind: CareGrantKind::Clinician,
145 recipient: clinician,
146 grant: grant_until(clinician, 100),
147 })
148 .unwrap();
149 grants.revoke(CareGrantKind::Clinician, clinician).unwrap();
150 assert_eq!(
151 grants.authorize_kind(
152 CareGrantKind::Clinician,
153 clinician,
154 ContactState::Active,
155 ContactState::Active,
156 10,
157 ),
158 Err(QdnfError::Denied)
159 );
160 let mut mb = Mailbox::new();
161 let s = mb.store(b"pending-ct", clinician, 1).unwrap();
162 mb.revoke_clinician(clinician).unwrap();
163 assert_eq!(mb.deliver(s, clinician, 1), Err(QdnfError::Revoked));
164 assert!(!network_asserts_clinical_review());
165 }
166
167 #[test]
168 fn e14_7_referral_requires_separate_grant() {
169 let mut grants = GrantTable::new();
170 let clinician = d(2);
171 let referral = d(4);
172 grants
173 .add(CareGrant {
174 kind: CareGrantKind::Clinician,
175 recipient: clinician,
176 grant: grant_until(clinician, 100),
177 })
178 .unwrap();
179 assert_eq!(
180 grants.authorize_kind(
181 CareGrantKind::Referral,
182 referral,
183 ContactState::Active,
184 ContactState::Active,
185 10,
186 ),
187 Err(QdnfError::Unauthorized)
188 );
189 grants
190 .add(CareGrant {
191 kind: CareGrantKind::Referral,
192 recipient: referral,
193 grant: grant_until(referral, 100),
194 })
195 .unwrap();
196 assert!(grants
197 .authorize_kind(
198 CareGrantKind::Referral,
199 referral,
200 ContactState::Active,
201 ContactState::Active,
202 10,
203 )
204 .is_ok());
205 }
206
207 #[test]
208 fn e14_7_mistaken_recipient_denied() {
209 let clinician = d(2);
210 let other = d(9);
211 let label = verified(Confidentiality::C2Sensitive, d(1), clinician);
212 let mut grants = GrantTable::new();
213 grants
214 .add(CareGrant {
215 kind: CareGrantKind::Clinician,
216 recipient: clinician,
217 grant: grant_until(clinician, 100),
218 })
219 .unwrap();
220 let env = seal_envelope(
221 &grants,
222 &label,
223 d(0x21),
224 d(0x11),
225 100,
226 b"sealed-ct",
227 PolicyOutcome::Allow,
228 ContactState::Active,
229 ContactState::Active,
230 10,
231 )
232 .unwrap();
233 assert_eq!(verify_envelope(&env, other, 10), Err(QdnfError::Denied));
234 assert!(verify_envelope(&env, clinician, 10).is_ok());
235 let seen = visible_recipients(&env);
236 assert_eq!(seen[0], clinician);
237 assert_eq!(seen[1], StrongDigest::ZERO);
238 }
239
240 #[test]
241 fn e14_7_backup_restore_does_not_drop_label() {
242 let stored = verified(Confidentiality::C2Sensitive, d(1), d(2));
243 let mut ctx = JobLabelContext::new(*stored.fields()).unwrap();
244 let backup = project_sink(&ctx, DerivedSink::Backup).unwrap();
245 assert_eq!(backup.confidentiality, Confidentiality::C2Sensitive);
246 assert_eq!(
247 backup.restriction_bits & (NO_TRAINING | NO_PUBLIC_INDEX),
248 NO_TRAINING | NO_PUBLIC_INDEX
249 );
250 assert!(restore_labelled_backup(&stored, &backup).is_ok());
251 let mut dropped = backup;
252 dropped.confidentiality = Confidentiality::C0Public;
253 dropped.restriction_bits = 0;
254 assert_eq!(
255 restore_labelled_backup(&stored, &dropped),
256 Err(QdnfError::Denied)
257 );
258 ctx.seal();
259 }
260
261 #[test]
262 fn e14_7_response_inherits_request_label() {
263 let request = LabelFields::request(Confidentiality::C1Private, d(1));
264 let consulted = verified(Confidentiality::C2Sensitive, d(1), d(2));
265 let mut out = LabelFields::blank();
266 inherit_response_label(&request, &consulted, &mut out).unwrap();
267 assert_eq!(out.confidentiality, Confidentiality::C2Sensitive);
268 assert_eq!(out.restriction_bits & NO_TRAINING, NO_TRAINING);
269 let mut acc = request;
270 join_one(&mut acc, consulted.fields()).unwrap();
271 assert_eq!(acc.confidentiality, out.confidentiality);
272 assert_eq!(acc.restriction_bits, out.restriction_bits);
273 }
274}