Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,22 @@ pub(crate) enum OperationResourceIdentity {
document: usize,
node: crate::NodeId,
},
/// Caller's immutable resource map, valid only while its operation borrows
/// it. Never used as a cross-operation or persistent cache identity.
#[cfg(feature = "xmldsig")]
DocumentNode(NodeIdentity),
External {
BorrowedResourceSet(usize),
/// One entry of that map, identified without reading its bytes.
#[cfg(feature = "xmldsig")]
BorrowedExternal {
uri: String,
fingerprint: [u8; 32],
address: usize,
length: usize,
},
#[cfg(feature = "xmldsig")]
DocumentNode(NodeIdentity),
#[cfg(feature = "xmlenc")]
External { uri: String, fingerprint: [u8; 32] },
#[cfg(feature = "xmldsig")]
// Absence has no bytes to cache. The originating graph Reference retains
// provenance; cloning absence must not clone an unaccounted URI string.
MissingExternal,
Expand All @@ -93,6 +102,7 @@ pub(crate) enum OperationResourceIdentity {
}

impl OperationResourceIdentity {
#[cfg(feature = "xmlenc")]
pub(crate) fn external(uri: impl Into<String>, bytes: &[u8]) -> Self {
use sha2::Digest;

Expand Down
6 changes: 3 additions & 3 deletions src/operation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ fn stage_regression_and_repeat_execution_are_rejected() {
#[cfg(feature = "xmldsig")]
#[test]
fn resource_identity_is_checked_before_the_action_runs() {
let expected = OperationResourceIdentity::external("urn:test", b"expected");
let observed = OperationResourceIdentity::external("urn:test", b"changed");
let expected = OperationResourceIdentity::Generated("urn:test", 0);
let observed = OperationResourceIdentity::Generated("urn:test", 1);
let mut context = OperationExecutionContext::new((), (), None);
let node = context.add_node(
OperationNodeKind::Digest { index: 0 },
Expand All @@ -300,7 +300,7 @@ fn resource_identity_is_checked_before_the_action_runs() {
fn resource_bound_node_requires_an_observed_identity() {
// A caller cannot accidentally bypass provenance validation by using the
// generic execution entry point for a resource-bound operation node.
let expected = OperationResourceIdentity::external("urn:test", b"expected");
let expected = OperationResourceIdentity::Generated("urn:test", 0);
let mut context = OperationExecutionContext::new((), (), None);
let node = context.add_node(
OperationNodeKind::Digest { index: 0 },
Expand Down
152 changes: 152 additions & 0 deletions src/xmldsig/base64_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//! Streaming decoder of the `XMLDSig` Base64 transform.

#[cfg(test)]
mod tests;

/// Alphabet bytes are decoded in blocks of whole quanta; the same `STANDARD`
/// engine as the resident transform enforces padding. A decoded block spans a
/// whole digest block, so the digest hashes it in place.
const BASE64_BLOCK: usize = 8192;

/// RFC 2045 section 6.8: decoders ignore every byte outside the alphabet.
pub(crate) fn is_rfc2045_base64_byte(byte: u8) -> bool {
byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'/' | b'=')
}

pub(crate) struct Base64Stream {
block: [u8; BASE64_BLOCK],
filled: usize,
/// Alphabet bytes accepted so far: the offset the resident decoder reports
/// within its filtered input.
offset: usize,
/// Offset of the padding that ended the last decoded block.
padding: Option<usize>,
failure: Option<base64::DecodeError>,
}

impl Base64Stream {
pub(crate) fn new() -> Self {
Self {
block: [0; BASE64_BLOCK],
filled: 0,
offset: 0,
padding: None,
failure: None,
}
}

/// Whether decoding still runs; a decoding error ends it.
pub(crate) fn active(&self) -> bool {
self.failure.is_none()
}

/// Decode `chunk` and hand each decoded block to `sink`. A decoding error
/// is kept and stops this stream only.
pub(crate) fn feed<E>(
&mut self,
chunk: &[u8],
sink: &mut impl FnMut(&[u8]) -> Result<(), E>,
) -> Result<(), E> {
if self.failure.is_some() {
return Ok(());
}
let mut chunk = chunk;
while !chunk.is_empty() {
if let Some(padding) = self.padding {
// Padding ended the encoded data in an earlier block; the
// resident decoder reports its first byte when more follows.
if chunk.iter().any(|byte| is_rfc2045_base64_byte(*byte)) {
self.failure = Some(base64::DecodeError::InvalidByte(padding, b'='));
}
return Ok(());
}
let run = chunk.len().min(BASE64_BLOCK - self.filled);
let (head, tail) = chunk.split_at(run);
chunk = tail;
let start = self.filled;
// Encoded runs hold only alphabet bytes, so they are copied whole.
if head
.iter()
.fold(true, |clean, &byte| clean & is_rfc2045_base64_byte(byte))
{
self.block[start..start + run].copy_from_slice(head);
self.filled += run;
} else {
// `run` fits the free space, so even the last write is in bounds.
for &byte in head {
self.block[self.filled] = byte;
self.filled += usize::from(is_rfc2045_base64_byte(byte));
}
}
// Bounded by the caller's input limit, which fits usize.
self.offset += self.filled - start;
if self.filled == BASE64_BLOCK {
self.flush(sink)?;
if self.failure.is_some() {
return Ok(());
}
}
}
Ok(())
}

fn flush<E>(&mut self, sink: &mut impl FnMut(&[u8]) -> Result<(), E>) -> Result<(), E> {
use base64::Engine as _;

let mut decoded = [0_u8; BASE64_BLOCK / 4 * 3];
let encoded = &self.block[..self.filled];
// The block holds the last `filled` accepted bytes.
let base = self.offset - encoded.len();
let padding = encoded
.iter()
.rev()
.take_while(|byte| **byte == b'=')
.count();
self.padding = (padding > 0).then(|| self.offset - padding);
self.filled = 0;
match base64::engine::general_purpose::STANDARD.decode_slice(encoded, &mut decoded) {
Ok(written) => sink(&decoded[..written]),
Err(error) => {
self.failure = Some(match error {
base64::DecodeSliceError::DecodeError(base64::DecodeError::InvalidByte(
index,
byte,
)) => base64::DecodeError::InvalidByte(base + index, byte),
base64::DecodeSliceError::DecodeError(
base64::DecodeError::InvalidLastSymbol {
offset,
symbol,
symbol_value,
},
) => base64::DecodeError::InvalidLastSymbol {
offset: base + offset,
symbol,
symbol_value,
},
base64::DecodeSliceError::DecodeError(base64::DecodeError::InvalidLength(
length,
)) => base64::DecodeError::InvalidLength(base + length),
base64::DecodeSliceError::DecodeError(error) => error,
// The output block holds every quantum of a full input
// block, so its size cannot be the failure.
base64::DecodeSliceError::OutputSliceTooSmall => {
base64::DecodeError::InvalidLength(encoded.len())
}
});
Ok(())
}
}
}

/// Decode the final partial block; an incomplete quantum is the padding
/// error the resident `STANDARD` decoder reports for the whole input.
pub(crate) fn finish<E>(
mut self,
sink: &mut impl FnMut(&[u8]) -> Result<(), E>,
) -> Result<Option<base64::DecodeError>, E> {
if self.failure.is_none() && self.filled > 0 {
self.flush(sink)?;
}
Ok(self.failure)
}
}
141 changes: 141 additions & 0 deletions src/xmldsig/base64_stream/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
use super::{Base64Stream, is_rfc2045_base64_byte};
use alloc::{format, string::String, vec::Vec};
use base64::Engine as _;

/// The decoded bytes and error of one streamed input.
fn streamed(input: &[u8], chunk: usize) -> (Vec<u8>, Option<base64::DecodeError>) {
let mut output = Vec::new();
let mut sink = |decoded: &[u8]| -> Result<(), ()> {
output.extend_from_slice(decoded);
Ok(())
};
let mut stream = Base64Stream::new();
for piece in input.chunks(chunk) {
stream
.feed(piece, &mut sink)
.expect("the sink never refuses");
}
let failure = stream.finish(&mut sink).expect("the sink never refuses");
(output, failure)
}

fn resident(input: &[u8]) -> Result<Vec<u8>, base64::DecodeError> {
let filtered: Vec<u8> = input
.iter()
.copied()
.filter(|byte| is_rfc2045_base64_byte(*byte))
.collect();
base64::engine::general_purpose::STANDARD.decode(filtered)
}

/// A decoded payload long enough to cross several decoding blocks.
fn encoded(length: usize) -> String {
let payload: Vec<u8> = [0_u8, 1, 2, 250, 251, 255]
.into_iter()
.cycle()
.take(length)
.collect();
base64::engine::general_purpose::STANDARD.encode(payload)
}

fn assert_matches_resident(input: &[u8]) {
// The chunk boundary decides which feed path a byte takes, so it must
// never change the outcome.
for chunk in [1, 3, 4, 7, 4095, 8191, 8192, 8193, input.len().max(1)] {
let (output, failure) = streamed(input, chunk);
match resident(input) {
Ok(expected) => {
assert_eq!(failure, None, "chunk {chunk}");
assert_eq!(output, expected, "chunk {chunk}");
}
Err(expected) => assert_eq!(failure, Some(expected), "chunk {chunk}"),
}
}
}

#[test]
fn valid_input_matches_the_resident_decoder_at_every_block_boundary() {
// Lengths around one block cover a final quantum on and past the boundary.
for length in [0, 1, 2, 3, 6141, 6142, 6143, 6144, 6145, 6146, 20_000] {
assert_matches_resident(encoded(length).as_bytes());
}
}

#[test]
fn ignored_bytes_match_the_resident_filter_wherever_they_fall() {
// RFC 2045 section 6.8: a foreign byte moves a run onto the byte filter.
let plain = encoded(20_000);
let lines = plain
.as_bytes()
.chunks(76)
.collect::<Vec<_>>()
.join(&b"\r\n"[..]);
assert_matches_resident(&lines);
let mut late = plain.into_bytes();
late.insert(10_000, b' ');
assert_matches_resident(&late);
assert_matches_resident(b" \n\t");
}

#[test]
fn padding_errors_match_the_resident_decoder_inside_and_across_blocks() {
let block = "QUJD".repeat(2048);
for input in [
// Data after padding inside one block.
"QQ==QUJD".to_owned(),
// Padding ends a block; the data follows in the next one.
format!("{}QQ==QUJD", "QUJD".repeat(2047)),
// The same with a separator hiding the next alphabet byte.
format!("{}QQ==\nQUJD", "QUJD".repeat(2047)),
// Data after padding beyond the first block.
format!("{block}QQ==QUJD"),
// Padding alone, in excess, and in an incomplete quantum.
"====".to_owned(),
"QQ===".to_owned(),
format!("{block}Q"),
format!("{block}QQ"),
format!("{block}QQ="),
// Non-canonical trailing bits.
"QR==".to_owned(),
] {
assert_matches_resident(input.as_bytes());
}
}

#[test]
fn a_sink_error_stops_the_stream_with_that_error() {
let input = encoded(20_000);
let mut calls = 0;
let mut stream = Base64Stream::new();
let error = stream
.feed(input.as_bytes(), &mut |_: &[u8]| {
calls += 1;
Err("refused")
})
.expect_err("a sink refusal ends the feed");
assert_eq!(error, "refused");
assert_eq!(calls, 1);
}

#[test]
fn a_decoding_failure_ends_the_stream_and_ignores_later_input() {
let mut stream = Base64Stream::new();
let mut sink = |_: &[u8]| -> Result<(), ()> { Ok(()) };
stream
.feed(b"QQ==", &mut sink)
.expect("the sink never refuses");
let block = "QUJD".repeat(2048);
stream
.feed(block.as_bytes(), &mut sink)
.expect("the sink never refuses");
assert!(!stream.active());
stream
.feed(b"anything", &mut sink)
.expect("the sink never refuses");
assert!(
stream
.finish(&mut sink)
.expect("the sink never refuses")
.is_some()
);
}
1 change: 1 addition & 0 deletions src/xmldsig/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
//! ```
#![doc = include_str!("../../docs/xmldsig.md")]

mod base64_stream;
Comment thread
polaz marked this conversation as resolved.
Resolved
#[cfg(feature = "xmldsig-sign")]
pub mod builder;
#[cfg(feature = "cms")]
Expand Down
Loading
Loading