mirror of
https://github.com/storytold/craft-anything.git
synced 2026-11-19 05:50:53 +01:00
formats: reuse shared definite TLV reader (partial PKCS#7 cutover) (#2149)
* formats: cut over pkcs7 and der to craft-pkcs7 * Preserve signature inspection while reusing shared definite TLV reader
This commit is contained in:
1 parent
a9130cabed
commit
c28f8ba58d
5 files changed
+768
-108
No files matched your search
Generated
+728
-83
File diff suppressed because it is too large.
Load diff
@@ -46,6 +46,7 @@ craft-wasm = { git = "https://github.com/storytold/craft-libs", rev = "d118983bf
|
||||
craft-plist = { git = "https://github.com/storytold/craft-libs", rev = "d118983bf922ac96c68b885dc55da30435adbc7d" }
|
||||
craft-sniff = { git = "https://github.com/storytold/craft-libs", rev = "d118983bf922ac96c68b885dc55da30435adbc7d" }
|
||||
craft-dex = { git = "https://github.com/storytold/craft-libs", rev = "35afed3956e14fb813f8c27bc82d65c2bce00028" }
|
||||
craft-pkcs7 = { git = "https://github.com/storytold/craft-libs", rev = "010702ee9b030844c852939095b1da0d0656a890" }
|
||||
thiserror = "2"
|
||||
serde = { version = "1", features = ["derive", "rc"] }
|
||||
serde_json = "1"
|
||||
|
||||
@@ -25,6 +25,7 @@ craft-mime = { workspace = true }
|
||||
craft-plist = { workspace = true }
|
||||
craft-zip = { workspace = true }
|
||||
craft-tar = { workspace = true }
|
||||
craft-pkcs7 = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -48,31 +48,15 @@ impl Tlv {
|
||||
/// Read one TLV at `off`. Only single-byte tags and definite lengths of up to 4 bytes.
|
||||
/// Redundant length encodings are retained and flagged, rather than hiding their contents.
|
||||
pub fn read(data: &[u8], off: usize) -> Option<Tlv> {
|
||||
let tag = *data.get(off)?;
|
||||
if tag & 0x1f == 0x1f {
|
||||
return None;
|
||||
}
|
||||
let l0 = *data.get(off.checked_add(1)?)?;
|
||||
let (len, hdr, non_minimal_length) = if l0 & 0x80 == 0 {
|
||||
(usize::from(l0), 2usize, false)
|
||||
} else {
|
||||
let n = usize::from(l0 & 0x7f);
|
||||
if n == 0 || n > 4 {
|
||||
return None;
|
||||
}
|
||||
let leading_zero = *data.get(off.checked_add(2)?)? == 0;
|
||||
let mut v = 0usize;
|
||||
for i in 0..n {
|
||||
v = v.checked_mul(256)?.checked_add(usize::from(*data.get(off.checked_add(2 + i)?)?))?;
|
||||
}
|
||||
(v, 2 + n, leading_zero || v < 128)
|
||||
};
|
||||
let content = off.checked_add(hdr)?;
|
||||
let end = content.checked_add(len)?;
|
||||
if end > data.len() {
|
||||
return None;
|
||||
}
|
||||
Some(Tlv { tag, start: off, content, len, non_minimal_length })
|
||||
let input = data.get(off..)?;
|
||||
// Keep the app's definite-length contract. In particular, signed bytes must not be
|
||||
// reinterpreted through the BER indefinite-length path during this partial cutover.
|
||||
let (value, _) = craft_pkcs7::der::Tlv::parse(input).ok()?;
|
||||
let header = value.raw.len().checked_sub(value.value.len())?;
|
||||
let length_octet = *input.get(1)?;
|
||||
// The shared parser accepts redundant definite lengths too; report them as before.
|
||||
let non_minimal_length = length_octet & 0x80 != 0 && (input.get(2) == Some(&0) || value.value.len() < 128);
|
||||
Some(Tlv { tag: value.tag, start: off, content: off.checked_add(header)?, len: value.value.len(), non_minimal_length })
|
||||
}
|
||||
|
||||
/// Whether a non-minimal length is found while walking constructed elements within the
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
//! The partial shared-reader cutover preserves offsets and the app's definite-length contract.
|
||||
use craft_anything_formats::der;
|
||||
|
||||
#[test]
|
||||
fn shared_tlv_preserves_offsets_raw_bytes_and_length_notes() {
|
||||
for (encoding, non_minimal) in [
|
||||
(vec![4, 3, 1, 2, 3], false),
|
||||
(vec![4, 0x81, 3, 1, 2, 3], true),
|
||||
(vec![4, 0x82, 0, 3, 1, 2, 3], true),
|
||||
([vec![4, 0x81, 128], vec![0; 128]].concat(), false),
|
||||
] {
|
||||
let mut bytes = vec![0xaa, 0xbb];
|
||||
bytes.extend_from_slice(&encoding);
|
||||
bytes.push(0xcc);
|
||||
let value = der::read(&bytes, 2).expect("definite-length element");
|
||||
assert_eq!(value.start, 2);
|
||||
assert_eq!(value.end(), 2 + encoding.len());
|
||||
assert_eq!(value.whole(&bytes), Some(encoding.as_slice()));
|
||||
assert_eq!(value.non_minimal_length, non_minimal);
|
||||
assert_eq!(value.bytes(&bytes).unwrap().len(), value.len);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_tlv_keeps_definite_only_policy() {
|
||||
// A valid BER indefinite SEQUENCE is outside this application's DER reader contract.
|
||||
assert!(der::read(&[0x30, 0x80, 0x05, 0, 0, 0], 0).is_none());
|
||||
assert!(der::read(&[0x30, 2, 0x05, 0], 0).is_some());
|
||||
}
|
||||
Reference in new issue
Block a user