revault_lockbox_api 0.0.1

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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use std::collections::BTreeMap;
use std::sync::Arc;

use super::Lockbox;
use crate::free_slot::FreeSlot;
use crate::page::{page_size_for_objects, PageObject, PageObjectKind};
use crate::page_cache::SecurePageAppend;
use crate::security::{validate_variable_value, validate_variable_value_ref};
use crate::variable_btree::{
    decode_variable_node_secure, encode_variable_internal, encode_variable_leaf,
    encode_variable_leaf_secure, variable_child_groups, variable_entries_from_map,
    variable_leaf_groups, VariableChild, VariableInternal, VariableLeaf, VariableNode,
    VariableTreeNode, VariableValue,
};
use crate::{crypto::derive_page_content_key, secret_vec::SecureVec};
use crate::{Error, Result, SecretString, VariableName, VariableSensitivity};
use zeroize::Zeroize;

/// Borrowed variable value yielded by `Lockbox::visit_variables`.
#[derive(Debug)]
pub enum VariableValueRef<'a> {
    /// Plain variable value.
    Normal(&'a str),
    /// Secret variable value; use `SecretString::with_str` for scoped plaintext access.
    Secret(&'a SecretString),
}

impl<State> Lockbox<State> {
    /// Store or replace a non-secret variable.
    ///
    /// Returns `Error::InvalidInput` if the value contains unsupported
    /// characters, `Error::SecurityLimitExceeded` if the value exceeds the
    /// configured variable value size limit, `Error::InvalidOperation` when
    /// attempting to overwrite an existing secret variable as non-secret, and
    /// `Error::CorruptRecord` if stored variable metadata cannot be loaded.
    pub fn set_variable(&mut self, name: &VariableName, value: &str) -> Result<()>
    where
        State: crate::WritableLockboxState,
    {
        let value = validate_variable_value(value)?;
        self.ensure_variables_loaded()?;
        let mut variables = self.variables.borrow_mut();
        let variables = variables.as_mut().ok_or(Error::CorruptRecord)?;
        if matches!(variables.get(name), Some(VariableValue::Secret(_))) {
            return Err(Error::InvalidOperation(
                "variable is secret; delete and recreate to change sensitivity".to_string(),
            ));
        }
        variables.insert(name.clone(), VariableValue::Normal(value));
        self.dirty_variables = true;
        Ok(())
    }

    /// Store or replace a secret variable.
    ///
    /// Secret values remain in secure storage. Changing an existing variable
    /// between normal and secret sensitivity requires deleting it first.
    ///
    /// Returns `Error::InvalidInput` if the secret plaintext contains
    /// unsupported characters, `Error::SecurityLimitExceeded` if the secret
    /// plaintext exceeds the configured variable value size limit,
    /// `Error::InvalidOperation` when attempting to overwrite an existing
    /// non-secret variable as secret, and `Error::CorruptRecord` if stored
    /// variable metadata cannot be loaded.
    pub fn set_secret_variable(&mut self, name: &VariableName, value: &SecretString) -> Result<()>
    where
        State: crate::WritableLockboxState,
    {
        value.with_str(validate_variable_value_ref)??;
        self.ensure_variables_loaded()?;
        let mut variables = self.variables.borrow_mut();
        let variables = variables.as_mut().ok_or(Error::CorruptRecord)?;
        if matches!(variables.get(name), Some(VariableValue::Normal(_))) {
            return Err(Error::InvalidOperation(
                "variable is not secret; delete and recreate to change sensitivity".to_string(),
            ));
        }
        variables.insert(
            name.clone(),
            VariableValue::Secret(Arc::new(value.try_clone()?)),
        );
        self.dirty_variables = true;
        Ok(())
    }

    /// Return a non-secret variable by name.
    ///
    /// Returns `Ok(None)` when the variable is absent. Returns
    /// `Error::InvalidOperation` if the variable exists but is secret, and
    /// `Error::CorruptRecord` if stored variable metadata cannot be loaded.
    pub fn get_variable(&self, name: &VariableName) -> Result<Option<String>> {
        self.ensure_variables_loaded()?;
        match self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
            .get(name)
        {
            Some(VariableValue::Normal(value)) => Ok(Some(value.clone())),
            Some(VariableValue::Secret(_)) => Err(Error::InvalidOperation(
                "variable is secret; use secret access".to_string(),
            )),
            None => Ok(None),
        }
    }

    /// Access a secret variable within a callback.
    ///
    /// The callback receives the `SecretString` handle. Use
    /// `SecretString::with_str` inside the callback when plaintext access is
    /// required.
    ///
    /// Returns `Ok(None)` when the variable is absent. Returns
    /// `Error::InvalidOperation` if the variable exists but is non-secret,
    /// and `Error::CorruptRecord` if stored variable metadata cannot be
    /// loaded.
    pub fn with_secret_variable<R>(
        &self,
        name: &VariableName,
        f: impl FnOnce(&SecretString) -> R,
    ) -> Result<Option<R>> {
        self.ensure_variables_loaded()?;
        let variables = self.variables.borrow();
        match variables.as_ref().ok_or(Error::CorruptRecord)?.get(name) {
            Some(VariableValue::Secret(value)) => Ok(Some(f(value))),
            Some(VariableValue::Normal(_)) => Err(Error::InvalidOperation(
                "variable is not secret".to_string(),
            )),
            None => Ok(None),
        }
    }

    /// Return the sensitivity of a variable, if it exists.
    ///
    /// Returns `Error::CorruptRecord` if stored variable metadata cannot be
    /// loaded.
    pub fn variable_sensitivity(&self, name: &VariableName) -> Result<Option<VariableSensitivity>> {
        self.ensure_variables_loaded()?;
        Ok(self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
            .get(name)
            .map(VariableValue::sensitivity))
    }

    /// Delete a variable if it exists.
    ///
    /// Returns `Error::CorruptRecord` if stored variable metadata cannot be
    /// loaded.
    pub fn delete_variable(&mut self, name: &VariableName) -> Result<()>
    where
        State: crate::WritableLockboxState,
    {
        self.ensure_variables_loaded()?;
        let removed = self
            .variables
            .borrow_mut()
            .as_mut()
            .ok_or(Error::CorruptRecord)?
            .remove(name)
            .is_some();
        if removed {
            self.dirty_variables = true;
        }
        Ok(())
    }

    /// List variable names with their sensitivity.
    ///
    /// Returns `Error::CorruptRecord` if stored variable metadata cannot be
    /// loaded.
    pub fn list_variables(&self) -> Result<Vec<(VariableName, VariableSensitivity)>> {
        self.ensure_variables_loaded()?;
        Ok(self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
            .iter()
            .map(|(name, value)| (name.clone(), value.sensitivity()))
            .collect())
    }

    /// Visit every variable.
    ///
    /// Normal values are provided as borrowed strings. Secret values are
    /// provided as `SecretString` references so callers must explicitly use the
    /// secret type's scoped accessors.
    ///
    /// Returns `Error::CorruptRecord` if stored variable metadata cannot be
    /// loaded, or any error returned by the visitor callback.
    pub fn visit_variables(
        &self,
        mut f: impl FnMut(&VariableName, VariableValueRef<'_>) -> Result<()>,
    ) -> Result<()> {
        self.ensure_variables_loaded()?;
        for (name, value) in self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
        {
            match value {
                VariableValue::Normal(value) => {
                    f(name, VariableValueRef::Normal(value))?;
                }
                VariableValue::Secret(value) => {
                    f(name, VariableValueRef::Secret(value))?;
                }
            }
        }
        Ok(())
    }

    pub(crate) fn clone_all_variable_values(
        &self,
    ) -> Result<BTreeMap<VariableName, VariableValue>> {
        self.ensure_variables_loaded()?;
        Ok(self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
            .clone())
    }

    pub(crate) fn set_variable_value(
        &mut self,
        name: VariableName,
        value: VariableValue,
    ) -> Result<()> {
        value.with_plaintext(validate_variable_value_ref)??;
        self.ensure_variables_loaded()?;
        self.variables
            .borrow_mut()
            .as_mut()
            .ok_or(Error::CorruptRecord)?
            .insert(name, value);
        self.dirty_variables = true;
        Ok(())
    }

    pub(crate) fn commit_variable_tree(&mut self) -> Result<u64> {
        if !self.dirty_variables {
            return Ok(self.variable_root_offset);
        }
        self.ensure_variables_loaded()?;
        let variables = self
            .variables
            .borrow()
            .as_ref()
            .ok_or(Error::CorruptRecord)?
            .clone();
        if variables.is_empty() {
            self.variable_root = None;
            self.variable_leaves.clear();
            self.dirty_variables = false;
            return Ok(0);
        }

        let entries = variable_entries_from_map(&variables);
        let mut leaves = Vec::new();
        for chunk in variable_leaf_groups(&entries)? {
            let offset = self.write_variable_leaf(chunk)?;
            leaves.push(VariableLeaf {
                offset,
                entries: chunk.to_vec(),
            });
        }
        let root_node = self.write_variable_tree_for_leaves(&leaves)?;
        let root = root_node.offset();
        self.variable_root = Some(root_node);
        self.variable_leaves = leaves;
        self.dirty_variables = false;
        Ok(root)
    }

    pub(crate) fn stage_variable_tree_redactions(&mut self) -> Result<()> {
        if !self.dirty_variables || self.variable_root_offset == 0 {
            return Ok(());
        }
        let mut redactions = Vec::new();
        self.collect_variable_tree_redactions(self.variable_root_offset, 0, &mut redactions)?;
        for (offset, object_id) in redactions {
            self.sequence += 1;
            let object = PageObject::new(
                PageObjectKind::VariableLeaf,
                object_id,
                encode_variable_leaf(&[])?,
            );
            let page_size = page_size_for_objects(std::slice::from_ref(&object)) as u64;
            self.write_decoded_page_at(offset, self.sequence, vec![object])?;
            self.record_ref_counts.remove(&offset);
            self.redacted_free_slots.push(FreeSlot {
                offset,
                len: page_size,
            });
        }
        Ok(())
    }

    fn ensure_variables_loaded(&self) -> Result<()> {
        if self.variables.borrow().is_none() {
            let variables = if self.variable_root_offset == 0 {
                BTreeMap::new()
            } else {
                let (variables, _, _) = self.decode_variable_btree(self.variable_root_offset)?;
                variables
            };
            *self.variables.borrow_mut() = Some(variables);
        }
        Ok(())
    }

    fn decode_variable_btree(
        &self,
        root_offset: u64,
    ) -> Result<(
        BTreeMap<VariableName, VariableValue>,
        VariableTreeNode,
        Vec<VariableLeaf>,
    )> {
        let mut variables = BTreeMap::new();
        let root = self.decode_variable_node_into(root_offset, &mut variables, 0)?;
        let mut leaves = Vec::new();
        root.collect_leaves(&mut leaves);
        leaves.sort_by(|left, right| {
            let left_name = left
                .entries
                .first()
                .map(|entry| entry.name.as_str())
                .unwrap_or("");
            let right_name = right
                .entries
                .first()
                .map(|entry| entry.name.as_str())
                .unwrap_or("");
            left_name.cmp(right_name)
        });
        Ok((variables, root, leaves))
    }

    fn decode_variable_node_into(
        &self,
        offset: u64,
        variables: &mut BTreeMap<VariableName, VariableValue>,
        depth: usize,
    ) -> Result<VariableTreeNode> {
        if depth > 8 {
            return Err(Error::CorruptRecord);
        }
        match self.read_variable_node(offset)? {
            VariableNode::Leaf(entries) => {
                let leaf_entries = entries.clone();
                for entry in entries {
                    variables.insert(VariableName::new(entry.name)?, entry.value);
                }
                Ok(VariableTreeNode::Leaf(VariableLeaf {
                    offset,
                    entries: leaf_entries,
                }))
            }
            VariableNode::Internal(children) => {
                let mut nodes = Vec::with_capacity(children.len());
                for child in children {
                    nodes.push(self.decode_variable_node_into(
                        child.offset,
                        variables,
                        depth + 1,
                    )?);
                }
                Ok(VariableTreeNode::Internal(VariableInternal {
                    offset,
                    children: nodes,
                }))
            }
        }
    }

    fn read_variable_node(&self, offset: u64) -> Result<VariableNode> {
        let variable_object = self.read_variable_object_secure(offset)?;
        if !matches!(
            variable_object.kind,
            PageObjectKind::VariableLeaf | PageObjectKind::VariableInternal
        ) {
            return Err(Error::CorruptRecord);
        }
        let payload = variable_object
            .secure_payload()
            .ok_or(Error::CorruptRecord)?;
        decode_variable_node_secure(payload)
    }

    fn read_variable_object_secure(&self, offset: u64) -> Result<PageObject> {
        self.with_secure_page(offset, |page| {
            if page.objects.len() != 1 {
                return Err(Error::CorruptRecord);
            }
            Ok(page.objects[0].clone())
        })
    }

    fn collect_variable_tree_redactions(
        &self,
        offset: u64,
        depth: usize,
        redactions: &mut Vec<(u64, u64)>,
    ) -> Result<()> {
        if depth > 8 {
            return Err(Error::CorruptRecord);
        }
        let variable_object = self.read_variable_object_secure(offset)?;
        if !matches!(
            variable_object.kind,
            PageObjectKind::VariableLeaf | PageObjectKind::VariableInternal
        ) {
            return Err(Error::CorruptRecord);
        }
        redactions.push((offset, variable_object.id));
        if variable_object.kind == PageObjectKind::VariableInternal {
            let payload = variable_object
                .secure_payload()
                .ok_or(Error::CorruptRecord)?;
            let VariableNode::Internal(children) = decode_variable_node_secure(payload)? else {
                return Err(Error::CorruptRecord);
            };
            for child in children {
                self.collect_variable_tree_redactions(child.offset, depth + 1, redactions)?;
            }
        }
        Ok(())
    }

    fn write_variable_tree_for_leaves(
        &mut self,
        leaves: &[VariableLeaf],
    ) -> Result<VariableTreeNode> {
        if leaves.len() == 1 {
            return Ok(VariableTreeNode::Leaf(leaves[0].clone()));
        }
        let mut level = leaves
            .iter()
            .cloned()
            .map(VariableTreeNode::Leaf)
            .collect::<Vec<_>>();

        while level.len() > 1 {
            let mut next_level = Vec::new();
            let mut child_cursor = 0usize;
            let children = level
                .iter()
                .map(|node| VariableChild {
                    first_name: node.first_name().to_string(),
                    offset: node.offset(),
                })
                .collect::<Vec<_>>();
            for chunk in variable_child_groups(&children)? {
                let offset = self.write_variable_internal(chunk)?;
                let start = child_cursor;
                let end = start + chunk.len();
                child_cursor = end;
                let child_nodes = level[start..end].to_vec();
                next_level.push(VariableTreeNode::Internal(VariableInternal {
                    offset,
                    children: child_nodes,
                }));
            }
            level = next_level;
        }

        Ok(level.remove(0))
    }

    fn write_variable_leaf(
        &mut self,
        entries: &[crate::variable_btree::VariableEntry],
    ) -> Result<u64> {
        let payload = encode_variable_leaf_secure(entries)?;
        self.sequence += 1;
        self.append_variable_page_secure(PageObjectKind::VariableLeaf, payload)
    }

    fn write_variable_internal(&mut self, children: &[VariableChild]) -> Result<u64> {
        let payload = SecureVec::try_from_vec(encode_variable_internal(children)?)?;
        self.sequence += 1;
        self.append_variable_page_secure(PageObjectKind::VariableInternal, payload)
    }

    fn append_variable_page_secure(
        &mut self,
        kind: PageObjectKind,
        mut payload: SecureVec,
    ) -> Result<u64> {
        self.flush_dirty_pages()?;
        let mut content_key = self.key.with_bytes(derive_page_content_key)?;
        let page_offset = self
            .page_manager
            .borrow_mut()
            .append_secure_single_object_page(
                &mut self.storage,
                SecurePageAppend {
                    lockbox_id: self.lockbox_id,
                    content_key: &content_key,
                    sequence: self.sequence,
                    kind,
                    object_id: self.sequence,
                    payload: &payload,
                },
            );
        content_key.zeroize();
        payload.zeroize()?;
        page_offset
    }
}