From 77698525d4160f636471bd7575b78361d1b99454 Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Fri, 3 May 2024 17:46:42 +0200 Subject: [PATCH] Working state --- Cargo.lock | 454 ++++++++++++++++++++++++++++++++++-------------- Cargo.toml | 12 +- src/api.rs | 120 ------------- src/auth.rs | 39 +++++ src/main.rs | 9 +- src/parser.rs | 44 ++--- src/resolver.rs | 13 +- src/structs.rs | 24 ++- 8 files changed, 401 insertions(+), 314 deletions(-) delete mode 100644 src/api.rs create mode 100644 src/auth.rs diff --git a/Cargo.lock b/Cargo.lock index 8b6b12b..68590f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,10 +33,64 @@ dependencies = [ ] [[package]] -name = "bytes" +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + +[[package]] +name = "base64" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" + +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +dependencies = [ + "block-padding", + "byte-tools", + "byteorder", + "generic-array 0.12.4", +] + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array 0.14.7", +] + +[[package]] +name = "block-padding" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" +dependencies = [ + "byte-tools", +] + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" + +[[package]] +name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" @@ -53,6 +107,69 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "const-oid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" + +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +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 0.14.7", + "typenum", +] + +[[package]] +name = "curve25519-dalek" +version = "4.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" +dependencies = [ + "cfg-if", + "cpufeatures", + "curve25519-dalek-derive", + "digest 0.10.7", + "fiat-crypto", + "platforms", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "der" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f55bf8e7b65898637379c1b74eb1551107c8294ed26d855ceb9fd1a09cfc9bc0" +dependencies = [ + "const-oid", + "zeroize", +] + [[package]] name = "deranged" version = "0.3.11" @@ -94,6 +211,25 @@ dependencies = [ "syn", ] +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +dependencies = [ + "generic-array 0.12.4", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer 0.10.4", + "crypto-common", +] + [[package]] name = "dotenvy" version = "0.15.7" @@ -101,51 +237,69 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" [[package]] -name = "fnv" -version = "1.0.7" +name = "ed25519" +version = "2.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +checksum = "115531babc129696a58c64a4fef0a8bf9e9698629fb97e9e40767d235cfbcd53" dependencies = [ - "percent-encoding", + "pkcs8", + "signature", ] [[package]] -name = "futures-channel" -version = "0.3.30" +name = "ed25519-dalek" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" +checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ - "futures-core", + "curve25519-dalek", + "ed25519", + "serde", + "sha2 0.10.8", + "subtle", + "zeroize", ] [[package]] -name = "futures-core" -version = "0.3.30" +name = "fake-simd" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" [[package]] -name = "futures-task" -version = "0.3.30" +name = "fiat-crypto" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f" [[package]] -name = "futures-util" -version = "0.3.30" +name = "generic-array" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" dependencies = [ - "futures-core", - "futures-task", - "pin-project-lite", - "pin-utils", + "typenum", +] + +[[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.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" +dependencies = [ + "cfg-if", + "libc", + "wasi", ] [[package]] @@ -160,87 +314,6 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" -[[package]] -name = "http" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" -dependencies = [ - "bytes", - "fnv", - "itoa", -] - -[[package]] -name = "http-body" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cac85db508abc24a2e48553ba12a996e87244a0395ce011e62b37158745d643" -dependencies = [ - "bytes", - "http", -] - -[[package]] -name = "http-body-util" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "41cb79eb393015dadd30fc252023adb0b2400a0caee0fa2a077e6e21a551e840" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "pin-project-lite", -] - -[[package]] -name = "httparse" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" - -[[package]] -name = "httpdate" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" - -[[package]] -name = "hyper" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "186548d73ac615b32a73aafe38fb4f56c0d340e110e5a200bcadbaf2e199263a" -dependencies = [ - "bytes", - "futures-channel", - "futures-util", - "http", - "http-body", - "httparse", - "httpdate", - "itoa", - "pin-project-lite", - "smallvec", - "tokio", -] - -[[package]] -name = "hyper-util" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca38ef113da30126bbff9cd1705f9273e15d45498615d138b0c20279ac7a76aa" -dependencies = [ - "bytes", - "futures-util", - "http", - "http-body", - "hyper", - "pin-project-lite", - "socket2", - "tokio", -] - [[package]] name = "itoa" version = "1.0.10" @@ -315,10 +388,10 @@ dependencies = [ ] [[package]] -name = "percent-encoding" -version = "2.3.1" +name = "opaque-debug" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" [[package]] name = "pin-project-lite" @@ -327,10 +400,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] -name = "pin-utils" -version = "0.1.0" +name = "pkcs8" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +checksum = "f950b2377845cebe5cf8b5165cb3cc1a5e0fa5cfa3e1f7f55707d8fd82e0a7b7" +dependencies = [ + "der", + "spki", +] [[package]] name = "pkg-config" @@ -338,6 +415,12 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "platforms" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db23d408679286588f4d4644f965003d056e3dd5abcaaa938116871d7ce2fee7" + [[package]] name = "powerfmt" version = "0.2.0" @@ -362,6 +445,30 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "ring" +version = "0.17.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c17fa4cb658e3583423e915b9f3acc01cceaee1860e33d59ebae66adc3a2dc0d" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "spin", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -369,10 +476,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] -name = "ryu" -version = "1.0.17" +name = "rustc_version" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + +[[package]] +name = "semver" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca" [[package]] name = "serde" @@ -395,21 +511,36 @@ dependencies = [ ] [[package]] -name = "serde_json" -version = "1.0.114" +name = "sha2" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5f09b1bd632ef549eaa9f60a1f8de742bdbc698e6cee2095fc84dde5f549ae0" +checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" dependencies = [ - "itoa", - "ryu", - "serde", + "block-buffer 0.7.3", + "digest 0.8.1", + "fake-simd", + "opaque-debug", ] [[package]] -name = "smallvec" -version = "1.13.1" +name = "sha2" +version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest 0.10.7", +] + +[[package]] +name = "signature" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77549399552de45a898a580c1b41d445bf730df867cc44e6c0233bbc4b8329de" +dependencies = [ + "rand_core", +] [[package]] name = "socket2" @@ -421,6 +552,39 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "spki" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "sshkeys" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c926cb006a77964474a13a86aa0135ea82c9fd43e6793a1151cc54143db6637c" +dependencies = [ + "base64 0.12.3", + "byteorder", + "sha2 0.8.2", +] + +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + [[package]] name = "syn" version = "2.0.49" @@ -490,18 +654,36 @@ dependencies = [ "syn", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + [[package]] name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" @@ -640,17 +822,21 @@ version = "0.52.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +[[package]] +name = "zeroize" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "525b4ec142c6b68a2d10f01f7bbf6755599ca3f81ea53b8431b7dd348f5fdb2d" + [[package]] name = "zeusns" version = "0.1.0" dependencies = [ + "base64 0.22.0", "diesel", "dotenvy", - "form_urlencoded", - "http-body-util", - "hyper", - "hyper-util", - "serde", - "serde_json", + "ed25519-dalek", + "ring", + "sshkeys", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index dd80bca..73f55a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,8 @@ edition = "2021" [dependencies] diesel = { version = "2.1.4", features = ["sqlite"] } dotenvy = "0.15" -form_urlencoded = "1.2" -tokio = {version = "1.36.0", features = ["macros","rt-multi-thread"], default-features = false} -hyper = {version = "1.2.0", features = ["server", "http1"], default-features = false} -hyper-util = { version = "0.1", features = ["server","http1", "tokio"], default-features = false} -http-body-util = "0.1" -serde_json = {version="^1.0", default-features = false, features=["std"]} -serde = {version="^1.0", features = ["derive"]} +tokio = {version = "1.36.0", features = ["macros","rt-multi-thread","net"], default-features = false} +sshkeys = "0.3.2" +ring = "0.17.8" +base64 = "0.22.0" +ed25519-dalek = "2.1.1" diff --git a/src/api.rs b/src/api.rs deleted file mode 100644 index b6218ba..0000000 --- a/src/api.rs +++ /dev/null @@ -1,120 +0,0 @@ -use std::collections::HashMap; -use std::net::SocketAddr; - -use http_body_util::{BodyExt, Full}; -use hyper::body::{Buf, Bytes}; -use hyper::server::conn::http1; -use hyper::service::service_fn; -use hyper::{Method, Request, Response, StatusCode}; -use hyper_util::rt::TokioIo; -use serde::Deserialize; -use tokio::net::TcpListener; - -use crate::db::models::insert_into_database; -use crate::structs::{Class, RRClass, Type, RR}; - -type GenericError = Box; -type Result = std::result::Result; -type BoxBody = http_body_util::combinators::BoxBody; - -static INTERNAL_SERVER_ERROR: &[u8] = b"Internal Server Error"; -static NOTFOUND: &[u8] = b"Not Found"; - -#[derive(Deserialize)] -struct Record { - name: Vec, - #[serde(rename = "type")] - _type: Type, - ttl: i32, - data: String, -} - -async fn create_record(req: Request) -> Result> { - let whole_body = req.collect().await?.aggregate(); - - match serde_json::from_reader::<_, Record>(whole_body.reader()) { - Ok(record) => { - let rdata = record - ._type - .to_data(&record.data) - .map_err(|e| e.to_string())?; - match insert_into_database(RR { - name: record.name, - _type: record._type, - class: Class::Class(RRClass::IN), - ttl: record.ttl, - rdlength: rdata.len() as u16, - rdata, - }) - .await - { - Ok(_) => Ok(Response::builder() - .status(StatusCode::OK) - .body(full("Successfully Created"))?), - Err(e) => { - eprintln!("{}", e.to_string()); - Ok(Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(full(INTERNAL_SERVER_ERROR))?) - } - } - } - Err(e) => Ok(Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(full(e.to_string()))?), - } -} - -async fn get_record(req: Request) -> Result> { - if let Some(q) = req.uri().query() { - let params = form_urlencoded::parse(q.as_bytes()) - .into_owned() - .collect::>(); - if let Some(domain) = params.get("domain_name") { - return Ok(Response::builder() - .status(StatusCode::OK) - .body(full(domain.to_owned()))?); - } - } - - Ok(Response::builder() - .status(StatusCode::UNPROCESSABLE_ENTITY) - .body(full("Missing domain_name query parameter"))?) -} - -async fn routes(req: Request) -> Result> { - match (req.method(), req.uri().path()) { - (&Method::POST, "/add") => create_record(req).await, - (&Method::GET, "/get") => get_record(req).await, - _ => Ok(Response::builder() - .status(StatusCode::NOT_FOUND) - .body(full(NOTFOUND)) - .unwrap()), - } -} - -fn full>(chunk: T) -> BoxBody { - Full::new(chunk.into()) - .map_err(|never| match never {}) - .boxed() -} - -pub async fn api_listener_loop( - addr: SocketAddr, -) -> Result> { - let listener = TcpListener::bind(addr).await?; - loop { - let (stream, _) = listener.accept().await?; - - let io = TokioIo::new(stream); - - tokio::task::spawn(async move { - if let Err(err) = http1::Builder::new() - .serve_connection(io, service_fn(routes)) - .await - { - println!("Error serving connection: {:?}", err); - } - }); - } -} diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..c3b5f7c --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,39 @@ +use std::{ + fs::{read_to_string, File}, + io::{Read, Write}, +}; + +use base64::prelude::*; +use ring::signature::Ed25519KeyPair; + +pub fn verify(user: String, signature: &[u8], message: &[u8]) -> bool { + let str = read_to_string("/home/xander/Desktop/dnsclient/dns.pub").unwrap(); + let key_split: Vec<&str> = str.split_ascii_whitespace().collect(); + let blob = BASE64_STANDARD.decode(key_split[1]).unwrap(); + + let mut prev = vec![ 0x30, 0x2a, 0x30,0x05, 0x06,0x03,0x2b,0x65, 0x70, 0x03, 0x21, 0x00]; + prev.extend_from_slice(&blob.as_slice()[19..]); + let s = prev.as_slice(); + println!("{:#?}", &blob.as_slice()[19..]); + + + let mut file = File::create("foo.txt").unwrap(); + file.write_all(s); + + let mut pem = File::open("/home/xander/Desktop/dnsclient/cert.der").unwrap(); + let mut pem_buf = Vec::::new(); + pem.read_to_end(&mut pem_buf).unwrap(); + let key = Ed25519KeyPair::from_pkcs8_maybe_unchecked(&pem_buf).unwrap(); + let mut pem = File::open("/home/xander/Desktop/dnsclient/der").unwrap(); + let mut pem_buf = Vec::::new(); + pem.read_to_end(&mut pem_buf).unwrap(); + + // let rng = rand::SystemRandom::new(); + // let mut signature = vec![]; + // key.sign(&signature::RSA_PKCS1_SHA256, &rng, MESSAGE, &mut signature); + let k = ring::signature::UnparsedPublicKey::new(&ring::signature::ED25519, &blob.as_slice()[19..]); + println!("{:#?}",k.verify(message, signature.as_ref())); + + + return false; +} diff --git a/src/main.rs b/src/main.rs index 33e1517..2e50706 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,21 @@ use std::{error::Error, net::SocketAddr}; -use crate::{api::api_listener_loop, resolver::resolver_listener_loop}; +use crate::resolver::resolver_listener_loop; -mod api; mod db; mod errors; mod parser; mod resolver; +mod auth; mod structs; mod utils; #[tokio::main] async fn main() -> Result<(), Box> { - let api_addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + let resolver_add = SocketAddr::from(([127, 0, 0, 1], 8080)); - let (_, _) = tokio::join!( + let _ = tokio::join!( resolver_listener_loop(resolver_add), - api_listener_loop(api_addr) ); Ok(()) } diff --git a/src/parser.rs b/src/parser.rs index 3cd1483..834813a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,7 +3,7 @@ use std::{mem::size_of, vec}; use crate::{ errors::ParseError, structs::{ - Class, Header, KeyRData, LabelString, Message, Opcode, OptRR, Question, RRClass, RRType, + Class, Header, KeyRData, LabelString, Message, Opcode, Question, RRClass, RRType, Type, RR, }, }; @@ -85,27 +85,6 @@ pub trait FromBytes { Self: Sized; } -pub fn parse_opt_type(bytes: &[u8]) -> Result> { - let mut pairs: Vec = vec![]; - let mut i: usize = 0; - while i + 4 <= bytes.len() { - let length = u16::from_be_bytes(bytes[i + 2..i + 4].try_into().unwrap()); - pairs.push(OptRR { - code: u16::from_be_bytes(bytes[i..i + 2].try_into().unwrap()), - length, - rdata: bytes[i + 4..i + 4 + length as usize] - .try_into() - .map_err(|_| ParseError { - object: String::from("Type::OPT"), - message: String::from("Invalid OPT DATA"), - })?, - }); - i += 4 + length as usize; - } - - Ok(pairs) -} - impl Type { pub fn to_data(&self, text: &String) -> Result> { match self { @@ -180,34 +159,35 @@ impl FromBytes for Header { impl FromBytes for LabelString { fn from_bytes(bytes: &[u8], i: &mut usize) -> Result { - let mut qname = vec![]; + let mut out = vec![]; // Parse qname labels while bytes[*i] != 0 && (bytes[*i] & 0b11000000 == 0) && bytes[*i] as usize + *i < bytes.len() { - qname.push( + out.push( String::from_utf8(bytes[*i + 1..bytes[*i] as usize + 1 + *i].to_vec()).unwrap(), ); *i += bytes[*i] as usize + 1; } if bytes[*i] & 0b11000000 != 0 { - let offset = u16::from_be_bytes(bytes[*i..*i + 2].try_into().unwrap()) & 0b00111111; + println!("YOOW"); + let offset = u16::from_be_bytes(bytes[*i..*i + 2].try_into().unwrap()) & 0b0011111111111111; if *i <= offset as usize { return Err(ParseError { object: String::from("Label"), message: String::from("Invalid PTR"), }); } else { - qname.extend(LabelString::from_bytes(bytes, &mut (offset as usize))?); + out.extend(LabelString::from_bytes(bytes, &mut (offset as usize))?); *i += 1; } } *i += 1; - Ok(qname) + Ok(out) } fn to_bytes(name: Self) -> Vec { @@ -307,7 +287,7 @@ impl FromBytes for RR { result.extend(u16::to_be_bytes(rr._type.into())); result.extend(u16::to_be_bytes(rr.class.into())); result.extend(i32::to_be_bytes(rr.ttl.to_owned())); - result.extend(u16::to_be_bytes(4 as u16)); + result.extend(u16::to_be_bytes(rr.rdata.len() as u16)); result.extend(rr.rdata); result } @@ -370,14 +350,14 @@ impl FromBytes for Message { } impl FromBytes for KeyRData { - fn from_bytes(bytes: &[u8], _: &mut usize) -> Result { + fn from_bytes(bytes: &[u8], i: &mut usize) -> Result { if bytes.len() < 18 { Err(ParseError { object: String::from("KeyRData"), message: String::from("invalid rdata"), }) } else { - let mut i = 18; + *i = 18; Ok(KeyRData { type_covered: u16::from_be_bytes(bytes[0..2].try_into().unwrap()), algo: bytes[2], @@ -386,8 +366,8 @@ impl FromBytes for KeyRData { signature_expiration: u32::from_be_bytes(bytes[8..12].try_into().unwrap()), signature_inception: u32::from_be_bytes(bytes[12..16].try_into().unwrap()), key_tag: u16::from_be_bytes(bytes[16..18].try_into().unwrap()), - signer: LabelString::from_bytes(bytes, &mut i)?, - signature: bytes[i..bytes.len()].to_vec(), + signer: LabelString::from_bytes(bytes, i)?, + signature: bytes[*i..bytes.len()].to_vec(), }) } } diff --git a/src/resolver.rs b/src/resolver.rs index 65e6057..64b57ab 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -4,6 +4,7 @@ use std::sync::Arc; use tokio::net::UdpSocket; +use crate::auth::verify; use crate::db::models::{delete_from_database, get_from_database, insert_into_database}; use crate::errors::ParseError; use crate::parser::FromBytes; @@ -69,8 +70,14 @@ async fn handle_update(message: Message) -> Message { // Check Requestor Permission for rr in &message.additional { if rr._type == Type::Type(RRType::KEY) { - let key = KeyRData::from_bytes(&rr.rdata, &mut 0).unwrap(); - println!("{:#?}",key); + let mut data = message.clone(); + data.header.arcount -= 1; + data.additional = vec![data.additional[0].clone()]; + let mut i = 0; + let key = KeyRData::from_bytes(&rr.rdata, &mut i).unwrap(); + let mut bytes = rr.rdata[0..i].to_vec(); + bytes.extend(Message::to_bytes(data).to_vec()); + let _ = verify(String::from("xander"), &key.signature, &bytes.as_slice()); } } @@ -169,6 +176,8 @@ pub async fn resolver_listener_loop(addr: SocketAddr) -> Result<(), Box, + pub qname: LabelString, pub qtype: Type, // NOTE: should be QTYPE, right now not really needed pub qclass: Class, //NOTE: should be QCLASS, right now not really needed } @@ -81,7 +77,7 @@ pub struct Message { #[derive(Debug, Clone)] pub struct RR { - pub name: Vec, + pub name: LabelString, pub _type: Type, pub class: Class, pub ttl: i32, @@ -93,7 +89,7 @@ pub struct RR { pub struct OptRR { pub code: u16, pub length: u16, - pub rdata: Vec + pub rdata: Vec, } pub type LabelString = Vec; @@ -108,5 +104,5 @@ pub struct KeyRData { pub signature_inception: u32, pub key_tag: u16, pub signer: LabelString, - pub signature: Vec + pub signature: Vec, }