Repository navigation
perf(xmldsig): stream final Base64, hash externals once #357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.