revault_lockbox_api 0.0.48

reVault lockbox API to create and manage lockboxes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Experimental domain-bound segmentation for full-size form texts. Descriptors are
//! metadata only; callers must authenticate membership and physical ownership.
//! Plaintext archive images remain publicly readable. Guarded processing does
//! not add at-rest confidentiality or replace selected owner authentication.
use super::allocation_map::Extent;
use super::page::{
    decode_single_object_page_secure_with_format, secure_page_size, PageObjectKind,
    SecureSingleObjectPage,
};
use crate::creation_options::FormatMode;
use crate::crypto::strong_checksum;
use crate::secret_vec::SecureVec;
use crate::storage::Storage;
use crate::{Error, LockboxId, Result, SecretString, VariableSensitivity};

pub(crate) const SEGMENT_BYTES: usize = 64 * 1024;
const MAX_VALUE: usize = crate::constants::MAX_VARIABLE_VALUE_BYTES;
const META: usize = 88;
const PREFIX: usize = 128;

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Layout {
    pub id: [u8; 16],
    pub context: [u8; 32],
    pub revision: u64,
    pub sensitivity: VariableSensitivity,
    pub mode: FormatMode,
    pub length: usize,
    pub extents: Vec<Extent>,
}
impl Layout {
    fn count(&self) -> usize {
        self.length.div_ceil(SEGMENT_BYTES).max(1)
    }
    fn part_len(&self, ordinal: usize) -> Result<usize> {
        if ordinal >= self.count() {
            return Err(Error::CorruptRecord);
        }
        Ok(self
            .length
            .saturating_sub(ordinal * SEGMENT_BYTES)
            .min(SEGMENT_BYTES))
    }
    fn page_id(&self, ordinal: usize) -> u64 {
        u64::from_le_bytes(self.id[..8].try_into().unwrap()) ^ ordinal as u64
    }
    fn metadata(&self) -> [u8; META] {
        let mut bytes = [0; META];
        bytes[..8].copy_from_slice(b"RV4TXT01");
        bytes[8..24].copy_from_slice(&self.id);
        bytes[24..32].copy_from_slice(&self.revision.to_le_bytes());
        bytes[32..34].copy_from_slice(&self.mode.0.to_le_bytes());
        bytes[34] = match self.sensitivity {
            VariableSensitivity::Normal => 0,
            VariableSensitivity::Secret => 1,
        };
        bytes[40..48].copy_from_slice(&(self.length as u64).to_le_bytes());
        bytes[48..52].copy_from_slice(&(self.count() as u32).to_le_bytes());
        bytes[56..88].copy_from_slice(&self.context);
        bytes
    }
    fn prefix(&self, archive: LockboxId, ordinal: usize) -> [u8; PREFIX] {
        let mut bytes = [0; PREFIX];
        bytes[..META].copy_from_slice(&self.metadata());
        bytes[..8].copy_from_slice(b"RV4FTX01");
        bytes[88..92].copy_from_slice(&(ordinal as u32).to_le_bytes());
        bytes[96..104].copy_from_slice(&((ordinal * SEGMENT_BYTES) as u64).to_le_bytes());
        bytes[104..112].copy_from_slice(&self.page_id(ordinal).to_le_bytes());
        bytes[112..128].copy_from_slice(archive.as_bytes());
        bytes
    }
    pub(crate) fn validate(&self, sealed: u64) -> Result<()> {
        FormatMode::parse(self.mode.0)?;
        if self.mode.0 == 0 {
            return Err(Error::CorruptRecord);
        }
        if self.id == [0; 16]
            || self.context == [0; 32]
            || self.revision == 0
            || self.length > MAX_VALUE
            || self.extents.len() != self.count()
        {
            return Err(Error::CorruptRecord);
        }
        let mut spans = Vec::with_capacity(self.count());
        for (ordinal, extent) in self.extents.iter().enumerate() {
            let expected = secure_page_size(PREFIX + self.part_len(ordinal)?, self.mode)?;
            let end = extent
                .start
                .checked_add(extent.len)
                .ok_or(Error::CorruptRecord)?;
            if extent.start < super::publication_anchor::REGION_LEN as u64
                || extent.len != expected as u64
                || end > sealed
            {
                return Err(Error::CorruptRecord);
            }
            spans.push((extent.start, end));
        }
        spans.sort_unstable();
        if spans.windows(2).any(|pair| pair[0].1 > pair[1].0) {
            return Err(Error::CorruptRecord);
        }
        Ok(())
    }
    pub(crate) fn encode_metadata(&self) -> Vec<u8> {
        let mut out = self.metadata().to_vec();
        for extent in &self.extents {
            out.extend_from_slice(&extent.start.to_le_bytes());
            out.extend_from_slice(&extent.len.to_le_bytes());
            out.extend_from_slice(&extent.digest);
        }
        out
    }
    pub(crate) fn decode_metadata(bytes: &[u8], mode: FormatMode, sealed: u64) -> Result<Self> {
        if bytes.len() < META
            || &bytes[..8] != b"RV4TXT01"
            || bytes[35..40]
                .iter()
                .chain(&bytes[52..56])
                .any(|byte| *byte != 0)
            || u16::from_le_bytes(bytes[32..34].try_into().unwrap()) != mode.0
        {
            return Err(Error::CorruptRecord);
        }
        let length = u64::from_le_bytes(bytes[40..48].try_into().unwrap());
        if length > MAX_VALUE as u64 {
            return Err(Error::CorruptRecord);
        }
        let count = (length as usize).div_ceil(SEGMENT_BYTES).max(1);
        if u32::from_le_bytes(bytes[48..52].try_into().unwrap()) as usize != count
            || bytes.len() != META + count * 48
        {
            return Err(Error::CorruptRecord);
        }
        let layout = Self {
            id: bytes[8..24].try_into().unwrap(),
            context: bytes[56..88].try_into().unwrap(),
            revision: u64::from_le_bytes(bytes[24..32].try_into().unwrap()),
            sensitivity: match bytes[34] {
                0 => VariableSensitivity::Normal,
                1 => VariableSensitivity::Secret,
                _ => return Err(Error::CorruptRecord),
            },
            mode,
            length: length as usize,
            extents: bytes[META..]
                .chunks_exact(48)
                .map(|part| Extent {
                    start: u64::from_le_bytes(part[..8].try_into().unwrap()),
                    len: u64::from_le_bytes(part[8..16].try_into().unwrap()),
                    digest: part[16..48].try_into().unwrap(),
                })
                .collect(),
        };
        layout.validate(sealed)?;
        Ok(layout)
    }
    /// Rebind only placement. A caller cannot substitute differently sized or
    /// differently authenticated staged pages through an allocation callback.
    pub(crate) fn rebind(&mut self, extents: &[Extent]) -> Result<()> {
        if extents.len() != self.extents.len()
            || extents
                .iter()
                .zip(&self.extents)
                .any(|(new, old)| new.len != old.len || new.digest != old.digest)
        {
            return Err(Error::CorruptRecord);
        }
        self.extents = extents.to_vec();
        self.validate(u64::MAX)
    }
    /// The caller supplies a descriptor from the currently selected authenticated
    /// graph. This checks stored bytes/context; possession of this metadata alone
    /// does not authorize a read or prove current membership.
    pub(crate) fn read(
        &self,
        storage: &impl Storage,
        archive: LockboxId,
        content_key: &[u8; 32],
        sealed: u64,
    ) -> Result<SecureVec> {
        self.validate(sealed)?;
        let mut value = SecureVec::new();
        for ordinal in 0..self.count() {
            self.append_part(storage, archive, content_key, sealed, ordinal, &mut value)?;
        }
        if value.len() != self.length {
            return Err(Error::CorruptRecord);
        }
        value.with_bytes(|bytes| {
            let text = std::str::from_utf8(bytes).map_err(|_| Error::CorruptRecord)?;
            crate::security::validate_variable_value_ref(text).map_err(|_| Error::CorruptRecord)
        })??;
        Ok(value)
    }
    /// Validate one stored segment before appending only its body. Individual
    /// segments may split UTF-8; full-text semantic admission remains separate.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn append_part(
        &self,
        storage: &impl Storage,
        archive: LockboxId,
        content_key: &[u8; 32],
        sealed: u64,
        ordinal: usize,
        target: &mut SecureVec,
    ) -> Result<()> {
        self.validate(sealed)?;
        let extent = self.extents.get(ordinal).ok_or(Error::CorruptRecord)?;
        let mut page = storage.read_at_secure(extent.start, extent.len as usize)?;
        if page.with_bytes(strong_checksum)? != extent.digest {
            return Err(Error::CorruptRecord);
        }
        let decoded = decode_single_object_page_secure_with_format(
            &mut page,
            archive,
            content_key,
            self.mode,
        )?;
        if decoded.page_id != self.page_id(ordinal)
            || decoded.sequence != self.revision
            || decoded.objects.len() != 1
        {
            return Err(Error::CorruptRecord);
        }
        let object = &decoded.objects[0];
        if object.kind != PageObjectKind::FormLeaf || object.id != self.page_id(ordinal) {
            return Err(Error::CorruptRecord);
        }
        let payload = object.secure_payload().ok_or(Error::CorruptRecord)?;
        let length = self.part_len(ordinal)?;
        if payload.len() != PREFIX + length
            || !payload.with_bytes(|bytes| bytes[..PREFIX] == self.prefix(archive, ordinal))?
        {
            return Err(Error::CorruptRecord);
        }
        target.try_extend_secure_range(payload, PREFIX, length)?;
        Ok(())
    }
}

/// Borrowed input: guarded secret/byte sources retain scoped access; ordinary
/// normal text copies only the requested range into guarded segment storage.
#[derive(Clone, Copy)]
pub(crate) enum Source<'a> {
    Bytes(&'a SecureVec),
    Secret(&'a SecretString),
    Text(&'a str),
}
impl Source<'_> {
    pub(crate) fn with_bytes<R>(&self, f: impl FnOnce(&[u8]) -> R) -> Result<R> {
        match self {
            Self::Bytes(value) => Ok(value.with_bytes(f)?),
            Self::Secret(value) => Ok(value.with_bytes(f)?),
            Self::Text(value) => Ok(f(value.as_bytes())),
        }
    }
    fn append_range(&self, target: &mut SecureVec, offset: usize, len: usize) -> Result<()> {
        match self {
            Self::Bytes(value) => target.try_extend_secure_range(value, offset, len)?,
            Self::Secret(value) => value.append_range_to_secure_vec(target, offset, len)?,
            Self::Text(value) => {
                let end = offset.checked_add(len).ok_or(Error::CorruptRecord)?;
                let bytes = value
                    .as_bytes()
                    .get(offset..end)
                    .ok_or(Error::CorruptRecord)?;
                target.try_extend_from_slice(bytes)?;
            }
        }
        Ok(())
    }
}

#[allow(clippy::too_many_arguments)]
fn source_layout(
    value: Source<'_>,
    mode: FormatMode,
    id: [u8; 16],
    context: [u8; 32],
    revision: u64,
    sensitivity: VariableSensitivity,
) -> Result<Layout> {
    FormatMode::parse(mode.0)?;
    if mode.0 == 0 {
        return Err(Error::CorruptRecord);
    }
    let length = value.with_bytes(|bytes| -> Result<usize> {
        if bytes.len() > MAX_VALUE {
            return Err(Error::SecurityLimitExceeded(
                "form text exceeds 1 MiB".into(),
            ));
        }
        if id == [0; 16] || context == [0; 32] || revision == 0 {
            return Err(Error::CorruptRecord);
        }
        let text = std::str::from_utf8(bytes)
            .map_err(|_| Error::InvalidInput("form text must be UTF-8".into()))?;
        crate::security::validate_variable_value_ref(text)?;
        Ok(bytes.len())
    })??;
    Ok(Layout {
        id,
        context,
        revision,
        sensitivity,
        mode,
        length,
        extents: Vec::new(),
    })
}
pub(crate) struct PreparedSegments<'a> {
    pub layout: Layout,
    pub pages: super::page::secure_storage::PreparedSecurePages,
    pub payload: Box<dyn FnMut(usize) -> Result<SecureVec> + 'a>,
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn prepare_source<'a>(
    value: Source<'a>,
    archive: LockboxId,
    mode: FormatMode,
    content_key: &[u8; 32],
    id: [u8; 16],
    context: [u8; 32],
    revision: u64,
    sensitivity: VariableSensitivity,
) -> Result<PreparedSegments<'a>> {
    let mut layout = source_layout(value, mode, id, context, revision, sensitivity)?;
    let template = layout.clone();
    let payload = move |ordinal: usize| -> Result<SecureVec> {
        if ordinal >= template.count() {
            return Err(Error::CorruptRecord);
        }
        let mut payload = SecureVec::try_from_slice(&template.prefix(archive, ordinal))?;
        value.append_range(
            &mut payload,
            ordinal * SEGMENT_BYTES,
            template.part_len(ordinal)?,
        )?;
        Ok(payload)
    };
    let mut pages = super::page::secure_storage::PreparedSecurePages::new(layout.count())?;
    for ordinal in 0..layout.count() {
        let raw = payload(ordinal)?;
        pages.push(SecureSingleObjectPage {
            format_mode: mode,
            page_size: secure_page_size(raw.len(), mode)?,
            lockbox_id: archive,
            page_id: layout.page_id(ordinal),
            sequence: revision,
            content_key,
            kind: PageObjectKind::FormLeaf,
            id: layout.page_id(ordinal),
            payload: &raw,
        })?;
        layout.extents.push(Extent {
            start: 0,
            len: pages.size(ordinal)? as u64,
            digest: pages.digest(ordinal)?,
        });
    }
    Ok(PreparedSegments {
        layout,
        pages,
        payload: Box::new(payload),
    })
}

/// Bind a text to its immutable definition identity or stable record UUID and
/// semantic role. Mutable paths/current record references are intentionally absent.
pub(crate) fn context(
    parent: &[u8],
    role: u8,
    field: Option<(&str, crate::FormFieldKind)>,
) -> [u8; 32] {
    use sha2::{Digest, Sha256};
    let mut hash = Sha256::new();
    hash.update(b"revault-experimental-form-text-v1");
    hash.update((parent.len() as u64).to_le_bytes());
    hash.update(parent);
    hash.update([role]);
    match field {
        Some((id, kind)) => {
            hash.update([1]);
            hash.update((id.len() as u64).to_le_bytes());
            hash.update(id.as_bytes());
            hash.update([kind.code()]);
        }
        None => hash.update([0]),
    }
    hash.finalize().into()
}

#[cfg(test)]
mod tests;

pub(crate) struct PreparedStoredSegments {
    pub layout: Layout,
    pub pages: super::page::secure_storage::PreparedSecurePages,
    pub payload: super::publication_anchor::shared::tree::StoragePayloadCallback<'static>,
}
/// Source capability is admitted before this first full semantic read. Rendering
/// later revalidates each old segment and the prepared destination fingerprint.
#[allow(clippy::too_many_arguments)]
pub(crate) fn prepare_stored(
    source: &Layout,
    view: super::publication_anchor::shared::tree::ReadView<'_>,
    archive: LockboxId,
    mode: FormatMode,
    content_key: &[u8; 32],
    id: [u8; 16],
    context: [u8; 32],
    revision: u64,
    sensitivity: VariableSensitivity,
) -> Result<PreparedStoredSegments> {
    if source.mode != mode {
        return Err(Error::CorruptRecord);
    }
    let sealed = view.len()?;
    let validated = source.read(&view, archive, content_key, sealed)?;
    let mut layout = source_layout(
        Source::Bytes(&validated),
        mode,
        id,
        context,
        revision,
        sensitivity,
    )?;
    drop(validated);
    let template = layout.clone();
    let source = source.clone();
    let source_key = zeroize::Zeroizing::new(*content_key);
    let payload = move |ordinal: usize,
                        view: super::publication_anchor::shared::tree::ReadView<'_>|
          -> Result<SecureVec> {
        if ordinal >= template.count() {
            return Err(Error::CorruptRecord);
        }
        let mut raw = SecureVec::try_from_slice(&template.prefix(archive, ordinal))?;
        source.append_part(&view, archive, &source_key, view.len()?, ordinal, &mut raw)?;
        Ok(raw)
    };
    let mut pages = super::page::secure_storage::PreparedSecurePages::new(layout.count())?;
    for ordinal in 0..layout.count() {
        let raw = payload(ordinal, view)?;
        pages.push(SecureSingleObjectPage {
            format_mode: mode,
            page_size: secure_page_size(raw.len(), mode)?,
            lockbox_id: archive,
            page_id: layout.page_id(ordinal),
            sequence: revision,
            content_key,
            kind: PageObjectKind::FormLeaf,
            id: layout.page_id(ordinal),
            payload: &raw,
        })?;
        layout.extents.push(Extent {
            start: 0,
            len: pages.size(ordinal)? as u64,
            digest: pages.digest(ordinal)?,
        });
    }
    Ok(PreparedStoredSegments {
        layout,
        pages,
        payload: Box::new(payload),
    })
}