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)
}
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)
}
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)
}
#[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(())
}
}
#[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),
})
}
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>,
}
#[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),
})
}