Compare commits

...

3 Commits

Author SHA1 Message Date
c1bc8c88f2 Refactor project 2025-03-06 15:34:38 +01:00
f506835b53 Add color to status 2025-03-06 15:11:54 +01:00
672549a86d Show hash of request and response 2025-03-06 15:07:45 +01:00
6 changed files with 631 additions and 93 deletions

504
Cargo.lock generated
View File

@ -2,6 +2,33 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "addr2line"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
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"
@ -11,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"
@ -67,18 +100,102 @@ version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
[[package]]
name = "async-trait"
version = "0.1.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d556ec1359574147ec0c4fc5eb525f3f23263a592b1a9c07e0a75b427de55c97"
dependencies = [
"proc-macro2",
"quote",
"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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-targets",
]
[[package]]
name = "base64"
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bytes"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9"
[[package]]
name = "cfg-if"
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"
@ -125,6 +242,44 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "colored"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e"
dependencies = [
"windows-sys",
]
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crypto-common"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
@ -136,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"
@ -174,12 +335,62 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "http"
version = "1.2.0"
@ -342,12 +553,27 @@ 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 = "libc"
version = "0.2.170"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828"
[[package]]
name = "litemap"
version = "0.7.5"
@ -366,6 +592,68 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5"
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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
dependencies = [
"memchr",
]
[[package]]
name = "once_cell"
version = "1.20.3"
@ -378,6 +666,12 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project-lite"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
[[package]]
name = "proc-macro2"
version = "1.0.94"
@ -396,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"
@ -425,6 +772,18 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "rustc-demangle"
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"
@ -432,9 +791,12 @@ dependencies = [
"anyhow",
"base64",
"clap",
"colored",
"env_logger",
"http",
"log",
"rasn",
"sha256",
"url",
]
@ -458,12 +820,69 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sha256"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f880fc8562bdeb709793f00eb42a2ad0e672c4f883bbe59122b926eca935c8f6"
dependencies = [
"async-trait",
"bytes",
"hex",
"sha2",
"tokio",
]
[[package]]
name = "smallvec"
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"
@ -498,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"
@ -508,6 +933,23 @@ dependencies = [
"zerovec",
]
[[package]]
name = "tokio"
version = "1.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
dependencies = [
"backtrace",
"bytes",
"pin-project-lite",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"
@ -543,6 +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"
@ -616,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"
@ -628,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"
@ -652,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"

View File

@ -10,4 +10,7 @@ clap = { version = "4.5.31", features = ["derive", "env"] }
http = { version = "1.2.0" }
anyhow = "1.0.97"
url = "2.5.4"
base64 = "0.22.1"
base64 = "0.22.1"
sha256 = "1.6.0"
colored = "3.0.0"
rasn = "0.24.0"

7
src/asn_parsing.rs Normal file
View File

@ -0,0 +1,7 @@
#[derive(Debug)]
pub struct SCEPEnveloppedPayload {}
/// Decode SCEP request
pub fn decode_request(req: &[u8]) -> anyhow::Result<SCEPEnveloppedPayload> {
todo!()
}

87
src/http_decoder.rs Normal file
View File

@ -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<usize> {
haystack.iter().position(|&x| x == needle)
}
/// Parse HTTP request
pub fn parse_request(req: &[u8]) -> anyhow::Result<http::Request<Vec<u8>>> {
// 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::<Vec<_>>();
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<http::Response<Vec<u8>>> {
// 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::<Vec<_>>();
// let version = first_line[0];
let status = first_line[1].parse::<u16>()?;
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())?)
}
}

2
src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod http_decoder;
pub mod asn_parsing;

View File

@ -1,12 +1,11 @@
use clap::Parser;
use http::StatusCode;
use std::collections::HashMap;
use std::str::FromStr;
use base64::alphabet::STANDARD;
use url::Url;
use base64::prelude::*;
use base64::Engine as _;
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)]
@ -19,92 +18,12 @@ struct Args {
/// Name of the file containing the response (WITH HTTP headers)
#[arg(short('R'), long)]
res_file: String,
/// Display decoding URL (lapo.it)
#[arg(short('d'), long)]
decode_url: bool,
}
/// Find a byte position in an array of bytes
fn find_in_bytes(haystack: &[u8], needle: u8) -> Option<usize> {
haystack.iter().position(|&x| x == needle)
}
/// Parse HTTP request
fn parse_request(req: &[u8]) -> anyhow::Result<http::Request<Vec<u8>>> {
// 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::<Vec<_>>();
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<http::Response<Vec<u8>>> {
// 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::<Vec<_>>();
// let version = first_line[0];
let status = first_line[1].parse::<u16>()?;
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 {
@ -138,15 +57,31 @@ fn main() {
_ => panic!("Unsupported operation {}!", operation),
};
let status = match response.status().as_u16() {
100..=199=> response.status().as_u16().to_string().blue(),
200..=299 => response.status().as_u16().to_string().green(),
400..=999 => response.status().as_u16().to_string().red(),
_ => response.status().as_u16().to_string().white().on_red(),
};
let date = response.headers().get("Date").unwrap().to_str().unwrap();
println!("{} - {} - {op:?}", response.headers().get("Date").unwrap().to_str().unwrap(), response.status().as_u16());
let sha_req = sha256::digest(request.body()).chars().take(10).collect::<String>();
let sha_res = sha256::digest(response.body()).chars().take(10).collect::<String>();
println!("{date} - {status} - {op:?}\t - Req: {sha_req}\t - Res: {sha_res}");
// Parse details about PKIOperation request
// https://www.rfc-editor.org/rfc/rfc8894.html#section-4.3
if op == Operation::PKIOperation {
if op == Operation::PKIOperation && args.decode_url {
let req_b64 = BASE64_URL_URL_SAFE.encode(&request.body());
let res_b64 = BASE64_URL_URL_SAFE.encode(&response.body());
println!("https://lapo.it/asn1js/#{req_b64}");
println!("https://lapo.it/asn1js/#{res_b64}");
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:?}");
}
}