diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:39:49 +0000 |
commit | a0aa2307322cd47bbf416810ac0292925e03be87 (patch) | |
tree | 37076262a026c4b48c8a0e84f44ff9187556ca35 /rust/vendor/der-oid-macro | |
parent | Initial commit. (diff) | |
download | suricata-a0aa2307322cd47bbf416810ac0292925e03be87.tar.xz suricata-a0aa2307322cd47bbf416810ac0292925e03be87.zip |
Adding upstream version 1:7.0.3.upstream/1%7.0.3
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'rust/vendor/der-oid-macro')
-rw-r--r-- | rust/vendor/der-oid-macro/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | rust/vendor/der-oid-macro/Cargo.toml | 32 | ||||
-rw-r--r-- | rust/vendor/der-oid-macro/src/lib.rs | 90 |
3 files changed, 123 insertions, 0 deletions
diff --git a/rust/vendor/der-oid-macro/.cargo-checksum.json b/rust/vendor/der-oid-macro/.cargo-checksum.json new file mode 100644 index 0000000..caba7d7 --- /dev/null +++ b/rust/vendor/der-oid-macro/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"44c14c5e511c82cd03bb9886976bd6e2c17ed71d19a478d417a1ff0bc1e2cc0b","src/lib.rs":"4d2721a9ae4473fa1a65beb43027dbeed4874a707833cec2019dd9d9216bd973"},"package":"c73af209b6a5dc8ca7cbaba720732304792cddc933cfea3d74509c2b1ef2f436"}
\ No newline at end of file diff --git a/rust/vendor/der-oid-macro/Cargo.toml b/rust/vendor/der-oid-macro/Cargo.toml new file mode 100644 index 0000000..d7b4145 --- /dev/null +++ b/rust/vendor/der-oid-macro/Cargo.toml @@ -0,0 +1,32 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "der-oid-macro" +version = "0.5.0" +authors = ["Pierre Chifflier <chifflier@wzdftpd.net>", "Jannik Schürg <schuerg@ins.uni-bonn.de>"] +description = "Macro to encode DER oids at compile time" +homepage = "https://github.com/rusticata/der-parser" +license = "MIT/Apache-2.0" +repository = "https://github.com/rusticata/der-parser.git" + +[lib] +proc-macro = true +[dependencies.num-bigint] +version = "0.4" + +[dependencies.num-traits] +version = "0.2" + +[dependencies.syn] +version = "1.0" diff --git a/rust/vendor/der-oid-macro/src/lib.rs b/rust/vendor/der-oid-macro/src/lib.rs new file mode 100644 index 0000000..afac8c2 --- /dev/null +++ b/rust/vendor/der-oid-macro/src/lib.rs @@ -0,0 +1,90 @@ +use proc_macro::TokenStream; + +fn encode_components(components: &[num_bigint::BigUint], relative: bool) -> Vec<u8> { + use num_traits::cast::ToPrimitive; + + let mut enc = Vec::new(); + let mut dec = components; + if !relative { + if dec.len() < 2 { + if dec.len() == 1 && dec[0] == 0u8.into() { + return vec![0]; + } + panic!("Need at least two components for non-relative oid"); + } + if dec[0] >= 7u8.into() || dec[1] >= 40u8.into() { + panic!("First components are too big"); + } + enc.push(dec[0].to_u8().unwrap() * 40 + dec[1].to_u8().unwrap()); + dec = &dec[2..]; + } + + for int in dec.iter() { + let mut bytes = int.to_bytes_be(); + if bytes[0] == 0 { + enc.push(0u8); + continue; + } + let total_bits = (8 - bytes[0].leading_zeros()) as usize + (bytes.len() - 1) * 8; + let octects_needed = ((total_bits + 6) / 7).max(1); + enc.resize_with(enc.len() + octects_needed, Default::default); + + let mut pos = enc.len() - 1; + let mut extra = 0u8; + let mut extra_size = 0u8; + bytes.reverse(); + let mut bytes_stored = 0; + for byte in bytes.into_iter() { + if extra_size == 7 { + // there a seven bits in extra + enc[pos] = extra | (1 << 7); + bytes_stored += 1; + pos -= 1; + extra = 0; + extra_size = 0; + } + // make space for the extra bits + enc[pos] = (byte << extra_size) | extra | (1 << 7); + bytes_stored += 1; + if pos > 0 { + pos -= 1; + extra_size += 1; + extra = byte >> (8 - extra_size); + } + } + let last = enc.len() - 1; + if bytes_stored != octects_needed { + let first = last + 1 - octects_needed; + enc[first] = extra | (1 << 7); + } + enc[last] ^= 1 << 7; + } + enc +} + +#[proc_macro] +pub fn encode_oid(input: TokenStream) -> TokenStream { + let s = input.to_string(); + + let (rem, relative) = if s.starts_with("rel ") { + (&s[4..], true) + } else { + (s.as_ref(), false) + }; + + let ints: Vec<num_bigint::BigUint> = rem + .split('.') + .map(|segment| segment.trim()) + .map(|s| s.parse().unwrap()) + .collect(); + + let enc = encode_components(&ints, relative); + + let mut s = String::with_capacity(2 + 6 * enc.len()); + s.push('['); + for byte in enc.iter() { + s.insert_str(s.len(), &format!("0x{:02x}, ", byte)); + } + s.push(']'); + s.parse().unwrap() +} |