Skip to main content

vcon_server/
key_wrap.rs

1//! The key-wrapping backend: what holds the root of the key hierarchy and
2//! wraps each tenant key under it. The registry asks it for the root, from
3//! which the server catalog key is derived, and has it wrap and unwrap
4//! tenant keys; it never sees a data key or a body.
5//!
6//! [`FileWrap`] is the backend every release so far has used: the master key
7//! from its file is the root, and a tenant key is wrapped under it with
8//! AES-256-GCM bound to the tenant id ([`envelope::wrap`] with
9//! [`Context::Tenant`]). What it writes is byte for byte what the code before
10//! this trait wrote, so data made by an older release opens unchanged.
11
12use std::{fmt, io};
13
14use crate::envelope::{self, Context, Key, KeyError};
15use crate::ids::{KeyId, TenantId};
16
17/// Why a backend could not wrap or unwrap a key.
18#[derive(Debug)]
19#[non_exhaustive]
20pub enum WrapError {
21    /// The wrapped key did not open under the backend's key for this
22    /// tenant: it was changed, cut short, moved from another tenant's row,
23    /// or made under another key. It does not say which part was wrong.
24    Damaged,
25    /// The key's memory could not be locked or kept out of core dumps.
26    Memory(io::Error),
27}
28
29/// The I/O error itself is given by `source`, not repeated here.
30impl fmt::Display for WrapError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.write_str(match self {
33            WrapError::Damaged => "a wrapped tenant key did not open",
34            WrapError::Memory(_) => {
35                "the key's memory could not be locked or kept out of core dumps"
36            }
37        })
38    }
39}
40
41impl std::error::Error for WrapError {
42    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43        match self {
44            WrapError::Memory(e) => Some(e),
45            WrapError::Damaged => None,
46        }
47    }
48}
49
50/// A key-wrapping backend. Its methods are synchronous: the registry calls
51/// them from blocking threads.
52pub trait KeyWrap: Send + Sync + fmt::Debug {
53    /// The id of the key that wraps tenant keys. The registry counts each
54    /// wrap against it, and a key rotation records when its use began.
55    fn key_id(&self) -> KeyId;
56
57    /// The root of the hierarchy: the key the server catalog key is derived
58    /// from ([`envelope::catalog_key`]).
59    fn root(&self) -> &Key;
60
61    /// Wraps `key`, the tenant key of `tenant`, bound to that tenant.
62    ///
63    /// # Errors
64    ///
65    /// A [`WrapError`] when the backend cannot wrap it.
66    fn wrap(&self, tenant: TenantId, key: &Key) -> Result<Vec<u8>, WrapError>;
67
68    /// Unwraps a key [`KeyWrap::wrap`] made for `tenant`, into a locked page
69    /// of its own.
70    ///
71    /// # Errors
72    ///
73    /// [`WrapError::Damaged`] when `wrapped` does not open for `tenant`
74    /// under the backend's key, and [`WrapError::Memory`] when the key's page
75    /// cannot be locked.
76    fn unwrap(&self, tenant: TenantId, wrapped: &[u8]) -> Result<Key, WrapError>;
77}
78
79/// The master key file's backend: the master key is the root, and tenant
80/// keys are wrapped under it locally. Debug shows only the key's id.
81#[derive(Debug)]
82pub struct FileWrap {
83    master: Key,
84}
85
86impl FileWrap {
87    /// The backend over `master`, as loaded by [`Key::load_master`].
88    #[must_use]
89    pub fn new(master: Key) -> FileWrap {
90        FileWrap { master }
91    }
92}
93
94impl KeyWrap for FileWrap {
95    fn key_id(&self) -> KeyId {
96        self.master.id()
97    }
98
99    fn root(&self) -> &Key {
100        &self.master
101    }
102
103    fn wrap(&self, tenant: TenantId, key: &Key) -> Result<Vec<u8>, WrapError> {
104        Ok(envelope::wrap(
105            &self.master,
106            &Context::Tenant { tenant },
107            key,
108        ))
109    }
110
111    fn unwrap(&self, tenant: TenantId, wrapped: &[u8]) -> Result<Key, WrapError> {
112        // `envelope::unwrap` fails only with these two (envelope.rs, `unwrap`).
113        envelope::unwrap(&self.master, &Context::Tenant { tenant }, wrapped).map_err(|e| match e {
114            KeyError::Memory(e) => WrapError::Memory(e),
115            KeyError::Damaged | KeyError::Read(_) | KeyError::Refused(_) | KeyError::WrongSize => {
116                WrapError::Damaged
117            }
118        })
119    }
120}
121
122#[cfg(test)]
123mod tests;