diff --git a/Cargo.lock b/Cargo.lock index 09a8264..7757d33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,18 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.3" @@ -26,6 +38,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "allocator-api2" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" + [[package]] name = "anstream" version = "0.6.18" @@ -93,6 +111,12 @@ dependencies = [ "syn", ] +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + [[package]] name = "backtrace" version = "0.3.74" @@ -114,6 +138,34 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bitflags" +version = "2.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" + +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "bitvec-nom2" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d988fcc40055ceaa85edc55875a08f8abd29018582647fd82ad6128dba14a5f0" +dependencies = [ + "bitvec", + "nom", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -135,6 +187,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c" +dependencies = [ + "num-traits", +] + [[package]] name = "clap" version = "4.5.31" @@ -183,11 +244,10 @@ checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" [[package]] name = "colored" -version = "2.2.0" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" +checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e" dependencies = [ - "lazy_static", "windows-sys", ] @@ -231,6 +291,12 @@ dependencies = [ "syn", ] +[[package]] +name = "either" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" + [[package]] name = "env_filter" version = "0.1.3" @@ -269,6 +335,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "generic-array" version = "0.14.7" @@ -279,12 +351,34 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + [[package]] name = "gimli" version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +dependencies = [ + "ahash", + "allocator-api2", +] + [[package]] name = "heck" version = "0.5.0" @@ -459,18 +553,21 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" -[[package]] -name = "lazy_static" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" - [[package]] name = "libc" version = "0.2.170" @@ -495,6 +592,12 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.8.5" @@ -504,6 +607,44 @@ dependencies = [ "adler2", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "object" version = "0.36.7" @@ -549,6 +690,59 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + +[[package]] +name = "rasn" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dc2d7ef9630c50239937d1eeb07e4fd3129c016441c5276b087f7975eb4e2c2" +dependencies = [ + "bitvec", + "bitvec-nom2", + "bytes", + "chrono", + "either", + "hashbrown", + "nom", + "num-bigint", + "num-integer", + "num-traits", + "once_cell", + "rasn-derive", + "serde_json", + "snafu", +] + +[[package]] +name = "rasn-derive" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5c332ddd8ace677cf246699782a34b79cbfa484c73523578f3c7d883dc8813" +dependencies = [ + "proc-macro2", + "rasn-derive-impl", + "syn", +] + +[[package]] +name = "rasn-derive-impl" +version = "0.24.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6cb5b4227db5fc7d1743a6089f4f43617b76b636d2d4293aade6144bc64a4c90" +dependencies = [ + "either", + "itertools", + "proc-macro2", + "quote", + "syn", + "uuid", +] + [[package]] name = "regex" version = "1.11.1" @@ -584,6 +778,12 @@ version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + [[package]] name = "scep_req_parser" version = "0.1.0" @@ -595,6 +795,7 @@ dependencies = [ "env_logger", "http", "log", + "rasn", "sha256", "url", ] @@ -619,6 +820,18 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + [[package]] name = "sha2" version = "0.10.8" @@ -649,6 +862,27 @@ version = "1.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcf8323ef1faaee30a44a340193b1ac6814fd9b7b4e88e9d4519a3e4abe1cfd" +[[package]] +name = "snafu" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "223891c85e2a29c3fe8fb900c1fae5e69c2e42415e3177752e8718475efa5019" +dependencies = [ + "snafu-derive", +] + +[[package]] +name = "snafu-derive" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03c3c6b7927ffe7ecaa769ee0e3994da3b8cafc8f444578982c83ecb161af917" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -683,6 +917,12 @@ dependencies = [ "syn", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tinystr" version = "0.7.6" @@ -745,12 +985,30 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0f540e3240398cce6128b64ba83fdbdd86129c16a3aa1a3a252efd66eb3d587" +dependencies = [ + "getrandom", +] + [[package]] name = "version_check" version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "windows-sys" version = "0.59.0" @@ -824,6 +1082,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + [[package]] name = "write16" version = "1.0.0" @@ -836,6 +1103,15 @@ version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "yoke" version = "0.7.5" @@ -860,6 +1136,26 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zerofrom" version = "0.1.6" diff --git a/Cargo.toml b/Cargo.toml index 52ba5ec..172aec0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,5 @@ anyhow = "1.0.97" url = "2.5.4" base64 = "0.22.1" sha256 = "1.6.0" -colored = "2" \ No newline at end of file +colored = "3.0.0" +rasn = "0.24.0" \ No newline at end of file diff --git a/src/asn_parsing.rs b/src/asn_parsing.rs new file mode 100644 index 0000000..e456ab6 --- /dev/null +++ b/src/asn_parsing.rs @@ -0,0 +1,7 @@ +#[derive(Debug)] +pub struct SCEPEnveloppedPayload {} + +/// Decode SCEP request +pub fn decode_request(req: &[u8]) -> anyhow::Result { + todo!() +} \ No newline at end of file diff --git a/src/http_decoder.rs b/src/http_decoder.rs new file mode 100644 index 0000000..dfb3561 --- /dev/null +++ b/src/http_decoder.rs @@ -0,0 +1,87 @@ +use std::str::FromStr; +use http::StatusCode; + +/// Find a byte position in an array of bytes +fn find_in_bytes(haystack: &[u8], needle: u8) -> Option { + haystack.iter().position(|&x| x == needle) +} + +/// Parse HTTP request +pub fn parse_request(req: &[u8]) -> anyhow::Result>> { + // Extract first list + let first_line_end = find_in_bytes(req, b'\r').expect("No newline found in req"); + let first_line = String::from_utf8_lossy(&req[..first_line_end]); + let first_line = first_line.split(' ').collect::>(); + let method = first_line[0]; + let uri = first_line[1]; + //let version = first_line[2]; + + let mut builder = http::Request::builder() + .method(http::method::Method::from_str(method)?) + .uri(uri) + .version(http::version::Version::HTTP_10); // FIXME: parse version properly + + let mut remainder = &req[first_line_end + 2..]; + + // Parse following lines + while remainder.len() > 2 { + let line_end = + find_in_bytes(remainder, b'\r').expect("No newline found while parsing headers"); + + if line_end == 0 { + break; + } + + let line = String::from_utf8_lossy(&remainder[..line_end]); + + let (key, value) = line.split_once(": ").unwrap(); + builder = builder.header(key, value); + + remainder = &remainder[line_end + 2..]; + } + + if remainder.len() > 2 { + Ok(builder.body(remainder[2..].to_vec())?) + } else { + Ok(builder.body(Vec::new())?) + } +} + +/// Parse HTTP response +pub fn parse_response(req: &[u8]) -> anyhow::Result>> { + // Extract first list + let first_line_end = find_in_bytes(req, b'\r').expect("No newline found in req"); + let first_line = String::from_utf8_lossy(&req[..first_line_end]); + let first_line = first_line.split(' ').collect::>(); + // let version = first_line[0]; + let status = first_line[1].parse::()?; + + let mut builder = http::Response::builder() + .status(StatusCode::from_u16(status)?) + .version(http::version::Version::HTTP_10); // FIXME: parse version properly + + let mut remainder = &req[first_line_end + 2..]; + + // Parse following lines + while remainder.len() > 2 { + let line_end = + find_in_bytes(remainder, b'\r').expect("No newline found while parsing headers"); + + if line_end == 0 { + break; + } + + let line = String::from_utf8_lossy(&remainder[..line_end]); + + let (key, value) = line.split_once(": ").unwrap(); + builder = builder.header(key, value); + + remainder = &remainder[line_end + 2..]; + } + + if remainder.len() > 2 { + Ok(builder.body(remainder[2..].to_vec())?) + } else { + Ok(builder.body(Vec::new())?) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..19675ac --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod http_decoder; +pub mod asn_parsing; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 2a0ebf1..e823c6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ use clap::Parser; -use http::StatusCode; use std::collections::HashMap; -use std::str::FromStr; -use sha256::{digest, Sha256Digest}; use url::Url; use base64::prelude::*; use base64::engine::general_purpose::URL_SAFE as BASE64_URL_URL_SAFE; use colored::Colorize; +use scep_req_parser::asn_parsing::decode_request; +use scep_req_parser::http_decoder::{parse_request, parse_response}; /// Simple SCEP parser program #[derive(Parser, Debug)] @@ -25,90 +24,6 @@ struct Args { decode_url: bool, } -/// Find a byte position in an array of bytes -fn find_in_bytes(haystack: &[u8], needle: u8) -> Option { - haystack.iter().position(|&x| x == needle) -} - -/// Parse HTTP request -fn parse_request(req: &[u8]) -> anyhow::Result>> { - // Extract first list - let first_line_end = find_in_bytes(req, b'\r').expect("No newline found in req"); - let first_line = String::from_utf8_lossy(&req[..first_line_end]); - let first_line = first_line.split(' ').collect::>(); - let method = first_line[0]; - let uri = first_line[1]; - //let version = first_line[2]; - - let mut builder = http::Request::builder() - .method(http::method::Method::from_str(method)?) - .uri(uri) - .version(http::version::Version::HTTP_10); // FIXME: parse version properly - - let mut remainder = &req[first_line_end + 2..]; - - // Parse following lines - while remainder.len() > 2 { - let line_end = - find_in_bytes(remainder, b'\r').expect("No newline found while parsing headers"); - - if line_end == 0 { - break; - } - - let line = String::from_utf8_lossy(&remainder[..line_end]); - - let (key, value) = line.split_once(": ").unwrap(); - builder = builder.header(key, value); - - remainder = &remainder[line_end + 2..]; - } - - if remainder.len() > 2 { - Ok(builder.body(remainder[2..].to_vec())?) - } else { - Ok(builder.body(Vec::new())?) - } -} - -/// Parse HTTP response -fn parse_response(req: &[u8]) -> anyhow::Result>> { - // Extract first list - let first_line_end = find_in_bytes(req, b'\r').expect("No newline found in req"); - let first_line = String::from_utf8_lossy(&req[..first_line_end]); - let first_line = first_line.split(' ').collect::>(); - // let version = first_line[0]; - let status = first_line[1].parse::()?; - - let mut builder = http::Response::builder() - .status(StatusCode::from_u16(status)?) - .version(http::version::Version::HTTP_10); // FIXME: parse version properly - - let mut remainder = &req[first_line_end + 2..]; - - // Parse following lines - while remainder.len() > 2 { - let line_end = - find_in_bytes(remainder, b'\r').expect("No newline found while parsing headers"); - - if line_end == 0 { - break; - } - - let line = String::from_utf8_lossy(&remainder[..line_end]); - - let (key, value) = line.split_once(": ").unwrap(); - builder = builder.header(key, value); - - remainder = &remainder[line_end + 2..]; - } - - if remainder.len() > 2 { - Ok(builder.body(remainder[2..].to_vec())?) - } else { - Ok(builder.body(Vec::new())?) - } -} #[derive(Debug, Copy, Clone, Eq, PartialEq)] enum Operation { @@ -163,4 +78,10 @@ fn main() { println!("=> REQ https://lapo.it/asn1js/#{req_b64}"); println!("=> RES https://lapo.it/asn1js/#{res_b64}"); } + + if op == Operation::PKIOperation { + let req = decode_request(request.body()).expect("Failed to decode request!"); + println!("{req:?}"); + } + }