Skip to main content

qualia_core_db/net/qdnf/session/
clinical.rs

1//! Known-peer clinical session controls (NET-05.21–24).
2//!
3//! Implementation lives in [`crate::net::qdnf::clinical`]. This module re-exports
4//! pairing, standing permits, ciphertext-only intermediaries and the encrypted
5//! mailbox (bounded ciphertext bytes).
6
7pub use crate::net::qdnf::clinical::{
8    forward_ciphertext, generic_log_contains_medical_payload, intermediary_decrypts,
9    public_patient_index_allowed, publish_relationship, vip_association_indexed, ClinicalBoundary,
10    ClinicalPair, GenericLog, GenericLogRecord, IntermediaryRole, Mailbox, MailboxSlot,
11    MailboxState, PairingTable, PermitTable, StandingPermit, MAX_SLOTS,
12};
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use crate::crypto::network::digest::sha384;
18    use crate::net::qdnf::authority::{ContactState, PolicyOutcome, TemporalGrant};
19    use crate::net::qdnf::errors::QdnfError;
20    use crate::net::qdnf::types::{ProfileId, StrongDigest};
21
22    fn d(tag: u8) -> StrongDigest {
23        let mut x = StrongDigest::ZERO;
24        x.0[0] = tag;
25        x
26    }
27    fn grant_until(exp: u64) -> TemporalGrant {
28        TemporalGrant {
29            purpose_digest: d(0x11),
30            audience_digest: StrongDigest::ZERO,
31            authority_generation: 1,
32            not_before_unix: 0,
33            expires_unix: exp,
34            profile: ProfileId::QDNF_CRYPTO_1,
35        }
36    }
37    fn pair(patient: u8, clinician: u8, blocked: bool) -> ClinicalPair {
38        ClinicalPair {
39            patient: d(patient),
40            clinician: d(clinician),
41            patient_contact: ContactState::Active,
42            clinician_contact: if blocked {
43                ContactState::Blocked
44            } else {
45                ContactState::Active
46            },
47            route: StrongDigest::ZERO,
48        }
49    }
50    fn auth(
51        t: &PermitTable,
52        p: &StandingPermit,
53        scope: StrongDigest,
54        recip: StrongDigest,
55        now: u64,
56    ) -> Result<(), QdnfError> {
57        t.authorize(
58            p.patient,
59            p.clinician,
60            p.purpose,
61            scope,
62            recip,
63            ContactState::Active,
64            ContactState::Active,
65            PolicyOutcome::Allow,
66            now,
67        )
68    }
69
70    #[test]
71    fn net_05_21_private_pairing_no_public_index() {
72        let mut table = PairingTable::new();
73        let p = pair(1, 2, false);
74        assert!(table.admit(p).is_ok());
75        assert_eq!(table.index_public(p), Err(QdnfError::Denied));
76        assert_eq!(
77            publish_relationship(p.patient, p.clinician),
78            Err(QdnfError::Denied)
79        );
80        assert!(!public_patient_index_allowed());
81        assert!(!vip_association_indexed());
82        let grant = grant_until(100);
83        let route = d(9);
84        assert_eq!(
85            table.exchange_route(
86                p.patient,
87                p.clinician,
88                route,
89                PolicyOutcome::Allow,
90                &grant,
91                10
92            ),
93            Ok(route)
94        );
95        let mut blocked = PairingTable::new();
96        let b = pair(1, 2, true);
97        assert!(blocked.admit(b).is_ok());
98        assert_eq!(
99            blocked.exchange_route(
100                b.patient,
101                b.clinician,
102                route,
103                PolicyOutcome::Allow,
104                &grant,
105                10
106            ),
107            Err(QdnfError::Denied)
108        );
109        assert_eq!(
110            table.exchange_route(d(3), d(4), route, PolicyOutcome::Allow, &grant, 10),
111            Err(QdnfError::Unauthorized)
112        );
113        let mut full = PairingTable::new();
114        let mut n = 1u8;
115        while n <= 8 {
116            assert!(full.admit(pair(n, n + 10, false)).is_ok());
117            n += 1;
118        }
119        assert_eq!(full.admit(pair(20, 21, false)), Err(QdnfError::Capacity));
120    }
121
122    #[test]
123    fn net_05_22_standing_scope_and_extra_recipient() {
124        let mut t = PermitTable::new();
125        let grant = grant_until(100);
126        let p = StandingPermit {
127            patient: d(1),
128            clinician: d(2),
129            purpose: d(3),
130            record_scope: d(4),
131            grant,
132        };
133        assert!(t.add(p).is_ok());
134        assert!(auth(&t, &p, p.record_scope, p.clinician, 10).is_ok());
135        assert!(auth(&t, &p, p.record_scope, p.patient, 10).is_ok());
136        assert_eq!(
137            auth(&t, &p, p.record_scope, d(9), 10),
138            Err(QdnfError::Unauthorized)
139        );
140        assert_eq!(
141            auth(&t, &p, d(8), p.clinician, 10),
142            Err(QdnfError::Unauthorized)
143        );
144        assert!(t
145            .add(StandingPermit {
146                record_scope: d(8),
147                grant,
148                ..p
149            })
150            .is_ok());
151        assert!(auth(&t, &p, d(8), p.clinician, 10).is_ok());
152        assert!(t
153            .add(StandingPermit {
154                clinician: d(9),
155                grant,
156                ..p
157            })
158            .is_ok());
159        assert!(t
160            .authorize(
161                p.patient,
162                d(9),
163                p.purpose,
164                p.record_scope,
165                d(9),
166                ContactState::Active,
167                ContactState::Active,
168                PolicyOutcome::Allow,
169                10,
170            )
171            .is_ok());
172        assert!(t
173            .add(StandingPermit {
174                grant: grant_until(5),
175                record_scope: d(7),
176                ..p
177            })
178            .is_ok());
179        assert_eq!(auth(&t, &p, d(7), p.clinician, 10), Err(QdnfError::Expired));
180    }
181
182    #[test]
183    fn net_05_23_ciphertext_only_forward_and_boundary() {
184        let ct = b"clinical-ciphertext";
185        let mut log = GenericLog::new();
186        let got = forward_ciphertext(ct, &mut log).expect("forward");
187        let rec = log.last().expect("meta");
188        assert_eq!(rec.ciphertext_len as usize, ct.len());
189        assert_eq!(rec.ciphertext_digest, got);
190        assert_eq!(got, sha384(ct));
191        assert_eq!(log.append_ciphertext_bytes(ct), Err(QdnfError::Denied));
192        assert!(!generic_log_contains_medical_payload());
193        assert!(!intermediary_decrypts());
194        assert!(!IntermediaryRole::NETWORK.decrypts);
195        assert!(IntermediaryRole::NETWORK
196            .forward(ct, &mut GenericLog::new())
197            .is_ok());
198        assert_eq!(
199            IntermediaryRole { decrypts: true }.forward(ct, &mut GenericLog::new()),
200            Err(QdnfError::Denied)
201        );
202        let svc = d(0x42);
203        let boundary = ClinicalBoundary { service: svc };
204        assert!(boundary.decrypt(svc, ct).is_ok());
205        assert_eq!(boundary.decrypt(d(0x43), ct), Err(QdnfError::Denied));
206    }
207
208    #[test]
209    fn net_05_24_mailbox_revoke_substitute_generation_states() {
210        let ct = b"pending-encrypted";
211        let clinician = d(2);
212        let other = d(3);
213        let mut mb = Mailbox::new();
214        let slot = mb.store(ct, clinician, 1).expect("store");
215        assert_eq!(mb.get(slot).unwrap().state, MailboxState::Stored);
216        assert_eq!(mb.mark_reviewed(slot), Err(QdnfError::Denied));
217        assert_eq!(
218            mb.substitute_recipient(slot, other),
219            Err(QdnfError::Unauthorized)
220        );
221        assert_eq!(mb.deliver(slot, other, 1), Err(QdnfError::Unauthorized));
222        assert_eq!(
223            mb.deliver(slot, clinician, 2),
224            Err(QdnfError::StaleGeneration)
225        );
226        assert!(mb.deliver(slot, clinician, 1).is_ok());
227        assert_eq!(mb.get(slot).unwrap().state, MailboxState::Delivered);
228        assert!(mb.mark_reviewed(slot).is_ok());
229        assert_eq!(mb.get(slot).unwrap().state, MailboxState::ClinicianReviewed);
230        let mut pending = Mailbox::new();
231        let s = pending.store(ct, clinician, 1).expect("pending");
232        assert!(pending.revoke_clinician(clinician).is_ok());
233        assert_eq!(pending.get(s).unwrap().state, MailboxState::Stored);
234        assert_eq!(pending.deliver(s, clinician, 1), Err(QdnfError::Revoked));
235        assert_eq!(pending.deliver(s, other, 1), Err(QdnfError::Unauthorized));
236        assert_eq!(pending.get(s).unwrap().ciphertext_digest, sha384(ct));
237        assert_eq!(pending.get(s).unwrap().ciphertext_len as usize, ct.len());
238    }
239}