qualia_core_db/bundle/stream/policy/remote/
codec.rs1use super::{PolicyError as E, MAX_DECISION_LEASE_SECS};
3use crate::net::qdnf::types::{Generation, OperationId, StrongDigest};
4
5const MAGIC: &[u8; 12] = b"QBDL-PDD-V1\0";
6pub const DECISION_TICKET_BYTES: usize = 256;
7pub const MAX_TICKET_PACKAGE_BYTES: usize = 8192;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11#[repr(u8)]
12pub enum DisclosureAction {
13 MetadataRelease = 1,
14 PayloadUse = 2,
15}
16
17impl DisclosureAction {
18 pub fn from_wire(v: u8) -> Result<Self, E> {
19 match v {
20 1 => Ok(Self::MetadataRelease),
21 2 => Ok(Self::PayloadUse),
22 _ => Err(E::Encoding),
23 }
24 }
25
26 pub const fn to_wire(self) -> u8 {
27 self as u8
28 }
29
30 pub const fn bit(self) -> u16 {
31 match self {
32 Self::MetadataRelease => 1,
33 Self::PayloadUse => 2,
34 }
35 }
36}
37
38#[derive(Clone, Copy, Debug, PartialEq, Eq)]
39pub struct DecisionTicketFields {
40 pub recipient_record_id: StrongDigest,
41 pub transcript: StrongDigest,
42 pub request_id: OperationId,
43 pub challenge: [u8; 32],
44 pub controller_generation: Generation,
45 pub policy: StrongDigest,
46 pub action: DisclosureAction,
47 pub not_before: u64,
48 pub expires: u64,
49 pub decision_id: u64,
50}
51
52impl DecisionTicketFields {
53 pub fn validate(&self) -> Result<(), E> {
54 for d in [self.recipient_record_id, self.transcript, self.policy] {
55 if d.is_zero() {
56 return Err(E::Reference);
57 }
58 }
59 if self.request_id.is_zero()
60 || self.challenge.iter().all(|b| *b == 0)
61 || self.controller_generation == Generation::ZERO
62 || self.decision_id == 0
63 {
64 return Err(E::Reference);
65 }
66 if self.expires <= self.not_before {
67 return Err(E::Encoding);
68 }
69 let lease = self.expires - self.not_before;
70 if lease > MAX_DECISION_LEASE_SECS {
71 return Err(E::Limit);
72 }
73 Ok(())
74 }
75
76 pub fn valid_at(&self, now: u64) -> Result<(), E> {
77 self.validate()?;
78 if now < self.not_before || now >= self.expires {
79 Err(E::Crypto(
80 crate::crypto::network::errors::CryptoError::Expired,
81 ))
82 } else {
83 Ok(())
84 }
85 }
86
87 pub fn lease_secs(&self) -> Result<u64, E> {
88 self.validate()?;
89 Ok(self.expires - self.not_before)
90 }
91}
92
93pub fn encode_decision_ticket_into(
94 fields: DecisionTicketFields,
95 out: &mut [u8],
96) -> Result<usize, E> {
97 fields.validate()?;
98 let out = out.get_mut(..DECISION_TICKET_BYTES).ok_or(E::Capacity)?;
99 out.fill(0);
100 let mut pos = 0;
101 put(out, &mut pos, MAGIC);
102 put(out, &mut pos, &1u16.to_be_bytes());
103 put(out, &mut pos, &0u16.to_be_bytes());
104 put(out, &mut pos, &fields.recipient_record_id.0);
105 put(out, &mut pos, &fields.transcript.0);
106 put(out, &mut pos, &fields.request_id.0);
107 put(out, &mut pos, &fields.challenge);
108 put(out, &mut pos, &fields.controller_generation.0.to_be_bytes());
109 put(out, &mut pos, &fields.policy.0);
110 put(out, &mut pos, &[fields.action.to_wire()]);
111 pos += 7; put(out, &mut pos, &fields.not_before.to_be_bytes());
113 put(out, &mut pos, &fields.expires.to_be_bytes());
114 put(out, &mut pos, &fields.decision_id.to_be_bytes());
115 pos += 8; debug_assert_eq!(pos, DECISION_TICKET_BYTES);
117 Ok(DECISION_TICKET_BYTES)
118}
119
120pub fn decode_decision_ticket(bytes: &[u8]) -> Result<DecisionTicketFields, E> {
121 if bytes.len() < DECISION_TICKET_BYTES {
122 return Err(E::Encoding);
123 }
124 if bytes.len() > DECISION_TICKET_BYTES {
125 return Err(E::Encoding);
126 }
127 if !bytes.starts_with(MAGIC) {
128 return Err(E::Encoding);
129 }
130 let mut pos = 12;
131 if u16::from_be_bytes(bytes[pos..pos + 2].try_into().unwrap()) != 1 {
132 return Err(E::Encoding);
133 }
134 pos += 2;
135 if u16::from_be_bytes(bytes[pos..pos + 2].try_into().unwrap()) != 0 {
136 return Err(E::Encoding);
137 }
138 pos += 2;
139 let recipient_record_id = digest(&bytes[pos..pos + 48]);
140 pos += 48;
141 let transcript = digest(&bytes[pos..pos + 48]);
142 pos += 48;
143 let request_id = OperationId(bytes[pos..pos + 16].try_into().unwrap());
144 pos += 16;
145 let challenge: [u8; 32] = bytes[pos..pos + 32].try_into().unwrap();
146 pos += 32;
147 let controller_generation =
148 Generation(u64::from_be_bytes(bytes[pos..pos + 8].try_into().unwrap()));
149 pos += 8;
150 let policy = digest(&bytes[pos..pos + 48]);
151 pos += 48;
152 let action = DisclosureAction::from_wire(bytes[pos])?;
153 pos += 1;
154 if bytes[pos..pos + 7] != [0; 7] {
155 return Err(E::Encoding);
156 }
157 pos += 7;
158 let not_before = u64::from_be_bytes(bytes[pos..pos + 8].try_into().unwrap());
159 pos += 8;
160 let expires = u64::from_be_bytes(bytes[pos..pos + 8].try_into().unwrap());
161 pos += 8;
162 let decision_id = u64::from_be_bytes(bytes[pos..pos + 8].try_into().unwrap());
163 pos += 8;
164 if bytes[pos..pos + 8] != [0; 8] {
165 return Err(E::Encoding);
166 }
167 let fields = DecisionTicketFields {
168 recipient_record_id,
169 transcript,
170 request_id,
171 challenge,
172 controller_generation,
173 policy,
174 action,
175 not_before,
176 expires,
177 decision_id,
178 };
179 fields.validate()?;
180 Ok(fields)
181}
182
183pub fn ticket_package_len(ticket_len: usize, envelope_len: usize) -> Result<usize, E> {
185 if ticket_len != DECISION_TICKET_BYTES {
186 return Err(E::Limit);
187 }
188 let n = ticket_len.checked_add(envelope_len).ok_or(E::Limit)?;
189 if n > MAX_TICKET_PACKAGE_BYTES {
190 Err(E::Limit)
191 } else {
192 Ok(n)
193 }
194}
195
196fn digest(bytes: &[u8]) -> StrongDigest {
197 StrongDigest(bytes.try_into().expect("validated fixed-width digest"))
198}
199
200fn put(out: &mut [u8], pos: &mut usize, bytes: &[u8]) {
201 out[*pos..*pos + bytes.len()].copy_from_slice(bytes);
202 *pos += bytes.len();
203}