diff --git a/Cargo.lock b/Cargo.lock index 7f318b6..bc92f88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1074,6 +1074,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46" + [[package]] name = "home" version = "0.5.9" @@ -1930,6 +1936,9 @@ dependencies = [ "criterion", "derive_builder 0.20.0", "env_logger", + "heck", + "hex", + "hex-literal", "home", "log", "memoffset 0.9.1", @@ -1948,10 +1957,12 @@ dependencies = [ "serial_test", "stacker", "static_assertions", + "tempfile", "test_bin", "thiserror", "toml", "zerocopy", + "zeroize", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ead707c..ab829ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,9 @@ derive_builder = "0.20.0" tokio = { version = "1.39", features = ["macros", "rt-multi-thread"] } postcard= {version = "1.0.8", features = ["alloc"]} libcrux = { version = "0.0.2-pre.2" } +hex-literal = { version = "0.4.1" } +hex = { version = "0.4.3" } +heck = { version = "0.5.0" } #Dev dependencies serial_test = "3.1.1" diff --git a/rosenpass/Cargo.toml b/rosenpass/Cargo.toml index e574ad5..11cf7ec 100644 --- a/rosenpass/Cargo.toml +++ b/rosenpass/Cargo.toml @@ -13,6 +13,15 @@ readme = "readme.md" name = "rosenpass" path = "src/main.rs" +[[bin]] +name = "rosenpass-gen-ipc-msg-types" +path = "src/bin/gen-ipc-msg-types.rs" +required-features = ["experiment_api", "internal_bin_gen_ipc_msg_types"] + +[[test]] +name = "api-integration-tests" +required-features = ["experiment_api", "internal_testing"] + [[bench]] name = "handshake" harness = false @@ -40,6 +49,10 @@ zerocopy = { workspace = true } home = { workspace = true } derive_builder = {workspace = true} rosenpass-wireguard-broker = {workspace = true} +zeroize = { workspace = true } +hex-literal = { workspace = true, optional = true } +hex = { workspace = true, optional = true } +heck = { workspace = true, optional = true } [build-dependencies] anyhow = { workspace = true } @@ -50,8 +63,12 @@ test_bin = { workspace = true } stacker = { workspace = true } serial_test = {workspace = true} procspawn = {workspace = true} +tempfile = { workspace = true } [features] enable_broker_api = ["rosenpass-wireguard-broker/enable_broker_api"] experiment_memfd_secret = [] experiment_libcrux = ["rosenpass-ciphers/experiment_libcrux"] +experiment_api = ["hex-literal"] +internal_testing = [] +internal_bin_gen_ipc_msg_types = ["hex", "heck"] diff --git a/rosenpass/src/api/boilerplate/byte_slice_ext.rs b/rosenpass/src/api/boilerplate/byte_slice_ext.rs new file mode 100644 index 0000000..dd20b49 --- /dev/null +++ b/rosenpass/src/api/boilerplate/byte_slice_ext.rs @@ -0,0 +1,116 @@ +use zerocopy::{ByteSlice, Ref}; + +use rosenpass_util::zerocopy::{RefMaker, ZerocopySliceExt}; + +use super::{ + PingRequest, PingResponse, RawMsgType, RefMakerRawMsgTypeExt, RequestMsgType, RequestRef, + ResponseMsgType, ResponseRef, +}; + +pub trait ByteSliceRefExt: ByteSlice { + fn msg_type_maker(self) -> RefMaker { + self.zk_ref_maker() + } + + fn msg_type(self) -> anyhow::Result> { + self.zk_parse() + } + + fn msg_type_from_prefix(self) -> anyhow::Result> { + self.zk_parse_prefix() + } + + fn msg_type_from_suffix(self) -> anyhow::Result> { + self.zk_parse_suffix() + } + + fn request_msg_type(self) -> anyhow::Result { + self.msg_type_maker().parse_request_msg_type() + } + + fn request_msg_type_from_prefix(self) -> anyhow::Result { + self.msg_type_maker() + .from_prefix()? + .parse_request_msg_type() + } + + fn request_msg_type_from_suffix(self) -> anyhow::Result { + self.msg_type_maker() + .from_suffix()? + .parse_request_msg_type() + } + + fn response_msg_type(self) -> anyhow::Result { + self.msg_type_maker().parse_response_msg_type() + } + + fn response_msg_type_from_prefix(self) -> anyhow::Result { + self.msg_type_maker() + .from_prefix()? + .parse_response_msg_type() + } + + fn response_msg_type_from_suffix(self) -> anyhow::Result { + self.msg_type_maker() + .from_suffix()? + .parse_response_msg_type() + } + + fn parse_request(self) -> anyhow::Result> { + RequestRef::parse(self) + } + + fn parse_request_from_prefix(self) -> anyhow::Result> { + RequestRef::parse_from_prefix(self) + } + + fn parse_request_from_suffix(self) -> anyhow::Result> { + RequestRef::parse_from_suffix(self) + } + + fn parse_response(self) -> anyhow::Result> { + ResponseRef::parse(self) + } + + fn parse_response_from_prefix(self) -> anyhow::Result> { + ResponseRef::parse_from_prefix(self) + } + + fn parse_response_from_suffix(self) -> anyhow::Result> { + ResponseRef::parse_from_suffix(self) + } + + fn ping_request_maker(self) -> RefMaker { + self.zk_ref_maker() + } + + fn ping_request(self) -> anyhow::Result> { + self.zk_parse() + } + + fn ping_request_from_prefix(self) -> anyhow::Result> { + self.zk_parse_prefix() + } + + fn ping_request_from_suffix(self) -> anyhow::Result> { + self.zk_parse_suffix() + } + + fn ping_response_maker(self) -> RefMaker { + self.zk_ref_maker() + } + + fn ping_response(self) -> anyhow::Result> { + self.zk_parse() + } + + fn ping_response_from_prefix(self) -> anyhow::Result> { + self.zk_parse_prefix() + } + + fn ping_response_from_suffix(self) -> anyhow::Result> { + self.zk_parse_suffix() + } +} + +impl ByteSliceRefExt for B {} diff --git a/rosenpass/src/api/boilerplate/message_trait.rs b/rosenpass/src/api/boilerplate/message_trait.rs new file mode 100644 index 0000000..de4105b --- /dev/null +++ b/rosenpass/src/api/boilerplate/message_trait.rs @@ -0,0 +1,29 @@ +use zerocopy::{ByteSliceMut, Ref}; + +use rosenpass_util::zerocopy::RefMaker; + +use super::RawMsgType; + +pub trait Message { + type Payload; + type MessageClass: Into; + const MESSAGE_TYPE: Self::MessageClass; + + fn from_payload(payload: Self::Payload) -> Self; + fn init(&mut self); + fn setup(buf: B) -> anyhow::Result>; +} + +pub trait ZerocopyResponseMakerSetupMessageExt { + fn setup_msg(self) -> anyhow::Result>; +} + +impl ZerocopyResponseMakerSetupMessageExt for RefMaker +where + B: ByteSliceMut, + T: Message, +{ + fn setup_msg(self) -> anyhow::Result> { + T::setup(self.into_buf()) + } +} diff --git a/rosenpass/src/api/boilerplate/message_type.rs b/rosenpass/src/api/boilerplate/message_type.rs new file mode 100644 index 0000000..8fca792 --- /dev/null +++ b/rosenpass/src/api/boilerplate/message_type.rs @@ -0,0 +1,116 @@ +use hex_literal::hex; +use rosenpass_util::zerocopy::RefMaker; +use zerocopy::ByteSlice; + +use crate::RosenpassError::{self, InvalidApiMessageType}; + +pub type RawMsgType = u128; + +// hash domain hash of: Rosenpass IPC API -> Rosenpass Protocol Server -> Ping Request +pub const PING_REQUEST: RawMsgType = + RawMsgType::from_le_bytes(hex!("2397 3ecc c441 704d 0b02 ea31 45d3 4999")); +// hash domain hash of: Rosenpass IPC API -> Rosenpass Protocol Server -> Ping Response +pub const PING_RESPONSE: RawMsgType = + RawMsgType::from_le_bytes(hex!("4ec7 f6f0 2bbc ba64 48f1 da14 c7cf 0260")); + +pub trait MessageAttributes { + fn message_size(&self) -> usize; +} + +#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)] +pub enum RequestMsgType { + Ping, +} + +#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)] +pub enum ResponseMsgType { + Ping, +} + +impl MessageAttributes for RequestMsgType { + fn message_size(&self) -> usize { + match self { + Self::Ping => std::mem::size_of::(), + } + } +} + +impl MessageAttributes for ResponseMsgType { + fn message_size(&self) -> usize { + match self { + Self::Ping => std::mem::size_of::(), + } + } +} + +impl TryFrom for RequestMsgType { + type Error = RosenpassError; + + fn try_from(value: u128) -> Result { + use RequestMsgType as E; + Ok(match value { + self::PING_REQUEST => E::Ping, + _ => return Err(InvalidApiMessageType(value)), + }) + } +} + +impl From for RawMsgType { + fn from(val: RequestMsgType) -> Self { + use RequestMsgType as E; + match val { + E::Ping => self::PING_REQUEST, + } + } +} + +impl TryFrom for ResponseMsgType { + type Error = RosenpassError; + + fn try_from(value: u128) -> Result { + use ResponseMsgType as E; + Ok(match value { + self::PING_RESPONSE => E::Ping, + _ => return Err(InvalidApiMessageType(value)), + }) + } +} + +impl From for RawMsgType { + fn from(val: ResponseMsgType) -> Self { + use ResponseMsgType as E; + match val { + E::Ping => self::PING_RESPONSE, + } + } +} + +pub trait RawMsgTypeExt { + fn into_request_msg_type(self) -> Result; + fn into_response_msg_type(self) -> Result; +} + +impl RawMsgTypeExt for RawMsgType { + fn into_request_msg_type(self) -> Result { + self.try_into() + } + + fn into_response_msg_type(self) -> Result { + self.try_into() + } +} + +pub trait RefMakerRawMsgTypeExt { + fn parse_request_msg_type(self) -> anyhow::Result; + fn parse_response_msg_type(self) -> anyhow::Result; +} + +impl RefMakerRawMsgTypeExt for RefMaker { + fn parse_request_msg_type(self) -> anyhow::Result { + Ok(self.parse()?.read().try_into()?) + } + + fn parse_response_msg_type(self) -> anyhow::Result { + Ok(self.parse()?.read().try_into()?) + } +} diff --git a/rosenpass/src/api/boilerplate/mod.rs b/rosenpass/src/api/boilerplate/mod.rs new file mode 100644 index 0000000..d60f075 --- /dev/null +++ b/rosenpass/src/api/boilerplate/mod.rs @@ -0,0 +1,17 @@ +mod byte_slice_ext; +mod message_trait; +mod message_type; +mod payload; +mod request_ref; +mod request_response; +mod response_ref; +mod server; + +pub use byte_slice_ext::*; +pub use message_trait::*; +pub use message_type::*; +pub use payload::*; +pub use request_ref::*; +pub use request_response::*; +pub use response_ref::*; +pub use server::*; diff --git a/rosenpass/src/api/boilerplate/payload.rs b/rosenpass/src/api/boilerplate/payload.rs new file mode 100644 index 0000000..7537ee5 --- /dev/null +++ b/rosenpass/src/api/boilerplate/payload.rs @@ -0,0 +1,96 @@ +use rosenpass_util::zerocopy::ZerocopyMutSliceExt; +use zerocopy::{AsBytes, ByteSliceMut, FromBytes, FromZeroes, Ref}; + +use super::{Message, RawMsgType, RequestMsgType, ResponseMsgType}; + +/// Size required to fit any message in binary form +pub const MAX_REQUEST_LEN: usize = 2500; // TODO fix this +pub const MAX_RESPONSE_LEN: usize = 2500; // TODO fix this + +#[repr(packed)] +#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)] +pub struct Envelope { + /// Which message this is + pub msg_type: RawMsgType, + /// The actual Paylod + pub payload: M, +} + +pub type RequestEnvelope = Envelope; +pub type ResponseEnvelope = Envelope; + +#[repr(packed)] +#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)] +pub struct PingRequestPayload { + /// Randomly generated connection id + pub echo: [u8; 256], +} + +pub type PingRequest = RequestEnvelope; + +impl PingRequest { + pub fn new(echo: [u8; 256]) -> Self { + Self::from_payload(PingRequestPayload { echo }) + } +} + +impl Message for PingRequest { + type Payload = PingRequestPayload; + type MessageClass = RequestMsgType; + const MESSAGE_TYPE: Self::MessageClass = RequestMsgType::Ping; + + fn from_payload(payload: Self::Payload) -> Self { + Self { + msg_type: Self::MESSAGE_TYPE.into(), + payload, + } + } + + fn setup(buf: B) -> anyhow::Result> { + let mut r: Ref = buf.zk_zeroized()?; + r.init(); + Ok(r) + } + + fn init(&mut self) { + self.msg_type = Self::MESSAGE_TYPE.into(); + } +} + +#[repr(packed)] +#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)] +pub struct PingResponsePayload { + /// Randomly generated connection id + pub echo: [u8; 256], +} + +pub type PingResponse = ResponseEnvelope; + +impl PingResponse { + pub fn new(echo: [u8; 256]) -> Self { + Self::from_payload(PingResponsePayload { echo }) + } +} + +impl Message for PingResponse { + type Payload = PingResponsePayload; + type MessageClass = ResponseMsgType; + const MESSAGE_TYPE: Self::MessageClass = ResponseMsgType::Ping; + + fn from_payload(payload: Self::Payload) -> Self { + Self { + msg_type: Self::MESSAGE_TYPE.into(), + payload, + } + } + + fn setup(buf: B) -> anyhow::Result> { + let mut r: Ref = buf.zk_zeroized()?; + r.init(); + Ok(r) + } + + fn init(&mut self) { + self.msg_type = Self::MESSAGE_TYPE.into(); + } +} diff --git a/rosenpass/src/api/boilerplate/request_ref.rs b/rosenpass/src/api/boilerplate/request_ref.rs new file mode 100644 index 0000000..22f6bd1 --- /dev/null +++ b/rosenpass/src/api/boilerplate/request_ref.rs @@ -0,0 +1,107 @@ +use anyhow::ensure; + +use zerocopy::{ByteSlice, ByteSliceMut, Ref}; + +use super::{ByteSliceRefExt, MessageAttributes, PingRequest, RequestMsgType}; + +struct RequestRefMaker { + buf: B, + msg_type: RequestMsgType, +} + +impl RequestRef { + pub fn parse(buf: B) -> anyhow::Result { + RequestRefMaker::new(buf)?.parse() + } + + pub fn parse_from_prefix(buf: B) -> anyhow::Result { + RequestRefMaker::new(buf)?.from_prefix()?.parse() + } + + pub fn parse_from_suffix(buf: B) -> anyhow::Result { + RequestRefMaker::new(buf)?.from_suffix()?.parse() + } + + pub fn message_type(&self) -> RequestMsgType { + match self { + Self::Ping(_) => RequestMsgType::Ping, + } + } +} + +impl From> for RequestRef { + fn from(v: Ref) -> Self { + Self::Ping(v) + } +} + +impl RequestRefMaker { + fn new(buf: B) -> anyhow::Result { + let msg_type = buf.deref().request_msg_type_from_prefix()?; + Ok(Self { buf, msg_type }) + } + + fn target_size(&self) -> usize { + self.msg_type.message_size() + } + + fn parse(self) -> anyhow::Result> { + Ok(match self.msg_type { + RequestMsgType::Ping => RequestRef::Ping(self.buf.ping_request()?), + }) + } + + #[allow(clippy::wrong_self_convention)] + fn from_prefix(self) -> anyhow::Result { + self.ensure_fit()?; + let point = self.target_size(); + let Self { buf, msg_type } = self; + let (buf, _) = buf.split_at(point); + Ok(Self { buf, msg_type }) + } + + #[allow(clippy::wrong_self_convention)] + fn from_suffix(self) -> anyhow::Result { + self.ensure_fit()?; + let point = self.buf.len() - self.target_size(); + let Self { buf, msg_type } = self; + let (buf, _) = buf.split_at(point); + Ok(Self { buf, msg_type }) + } + + pub fn ensure_fit(&self) -> anyhow::Result<()> { + let have = self.buf.len(); + let need = self.target_size(); + ensure!( + need <= have, + "Buffer is undersized at {have} bytes (need {need} bytes)!" + ); + Ok(()) + } +} + +pub enum RequestRef { + Ping(Ref), +} + +impl RequestRef +where + B: ByteSlice, +{ + pub fn bytes(&self) -> &[u8] { + match self { + Self::Ping(r) => r.bytes(), + } + } +} + +impl RequestRef +where + B: ByteSliceMut, +{ + pub fn bytes_mut(&mut self) -> &[u8] { + match self { + Self::Ping(r) => r.bytes_mut(), + } + } +} diff --git a/rosenpass/src/api/boilerplate/request_response.rs b/rosenpass/src/api/boilerplate/request_response.rs new file mode 100644 index 0000000..5af7a20 --- /dev/null +++ b/rosenpass/src/api/boilerplate/request_response.rs @@ -0,0 +1,103 @@ +use rosenpass_util::zerocopy::{ + RefMaker, ZerocopyEmancipateExt, ZerocopyEmancipateMutExt, ZerocopySliceExt, +}; +use zerocopy::{ByteSlice, ByteSliceMut, Ref}; + +use super::{Message, PingRequest, PingResponse}; +use super::{RequestRef, ResponseRef, ZerocopyResponseMakerSetupMessageExt}; + +pub trait RequestMsg: Sized + Message { + type ResponseMsg: ResponseMsg; + + fn zk_response_maker(buf: B) -> RefMaker { + buf.zk_ref_maker() + } + + fn setup_response(buf: B) -> anyhow::Result> { + Self::zk_response_maker(buf).setup_msg() + } + + fn setup_response_from_prefix( + buf: B, + ) -> anyhow::Result> { + Self::zk_response_maker(buf).from_prefix()?.setup_msg() + } + + fn setup_response_from_suffix( + buf: B, + ) -> anyhow::Result> { + Self::zk_response_maker(buf).from_prefix()?.setup_msg() + } +} + +pub trait ResponseMsg: Message { + type RequestMsg: RequestMsg; +} + +impl RequestMsg for PingRequest { + type ResponseMsg = PingResponse; +} + +impl ResponseMsg for PingResponse { + type RequestMsg = PingRequest; +} + +pub type PingPair = (Ref, Ref); + +pub enum RequestResponsePair { + Ping(PingPair), +} + +impl From> for RequestResponsePair { + fn from(v: PingPair) -> Self { + RequestResponsePair::Ping(v) + } +} + +impl RequestResponsePair +where + B1: ByteSlice, + B2: ByteSlice, +{ + pub fn both(&self) -> (RequestRef<&[u8]>, ResponseRef<&[u8]>) { + match self { + Self::Ping((req, res)) => { + let req = RequestRef::Ping(req.emancipate()); + let res = ResponseRef::Ping(res.emancipate()); + (req, res) + } + } + } + + pub fn request(&self) -> RequestRef<&[u8]> { + self.both().0 + } + + pub fn response(&self) -> ResponseRef<&[u8]> { + self.both().1 + } +} + +impl RequestResponsePair +where + B1: ByteSliceMut, + B2: ByteSliceMut, +{ + pub fn both_mut(&mut self) -> (RequestRef<&mut [u8]>, ResponseRef<&mut [u8]>) { + match self { + Self::Ping((req, res)) => { + let req = RequestRef::Ping(req.emancipate_mut()); + let res = ResponseRef::Ping(res.emancipate_mut()); + (req, res) + } + } + } + + pub fn request_mut(&mut self) -> RequestRef<&mut [u8]> { + self.both_mut().0 + } + + pub fn response_mut(&mut self) -> ResponseRef<&mut [u8]> { + self.both_mut().1 + } +} diff --git a/rosenpass/src/api/boilerplate/response_ref.rs b/rosenpass/src/api/boilerplate/response_ref.rs new file mode 100644 index 0000000..38b1e75 --- /dev/null +++ b/rosenpass/src/api/boilerplate/response_ref.rs @@ -0,0 +1,108 @@ +// TODO: This is copied verbatim from ResponseRef…not pretty +use anyhow::ensure; + +use zerocopy::{ByteSlice, ByteSliceMut, Ref}; + +use super::{ByteSliceRefExt, MessageAttributes, PingResponse, ResponseMsgType}; + +struct ResponseRefMaker { + buf: B, + msg_type: ResponseMsgType, +} + +impl ResponseRef { + pub fn parse(buf: B) -> anyhow::Result { + ResponseRefMaker::new(buf)?.parse() + } + + pub fn parse_from_prefix(buf: B) -> anyhow::Result { + ResponseRefMaker::new(buf)?.from_prefix()?.parse() + } + + pub fn parse_from_suffix(buf: B) -> anyhow::Result { + ResponseRefMaker::new(buf)?.from_suffix()?.parse() + } + + pub fn message_type(&self) -> ResponseMsgType { + match self { + Self::Ping(_) => ResponseMsgType::Ping, + } + } +} + +impl From> for ResponseRef { + fn from(v: Ref) -> Self { + Self::Ping(v) + } +} + +impl ResponseRefMaker { + fn new(buf: B) -> anyhow::Result { + let msg_type = buf.deref().response_msg_type_from_prefix()?; + Ok(Self { buf, msg_type }) + } + + fn target_size(&self) -> usize { + self.msg_type.message_size() + } + + fn parse(self) -> anyhow::Result> { + Ok(match self.msg_type { + ResponseMsgType::Ping => ResponseRef::Ping(self.buf.ping_response()?), + }) + } + + #[allow(clippy::wrong_self_convention)] + fn from_prefix(self) -> anyhow::Result { + self.ensure_fit()?; + let point = self.target_size(); + let Self { buf, msg_type } = self; + let (buf, _) = buf.split_at(point); + Ok(Self { buf, msg_type }) + } + + #[allow(clippy::wrong_self_convention)] + fn from_suffix(self) -> anyhow::Result { + self.ensure_fit()?; + let point = self.buf.len() - self.target_size(); + let Self { buf, msg_type } = self; + let (buf, _) = buf.split_at(point); + Ok(Self { buf, msg_type }) + } + + pub fn ensure_fit(&self) -> anyhow::Result<()> { + let have = self.buf.len(); + let need = self.target_size(); + ensure!( + need <= have, + "Buffer is undersized at {have} bytes (need {need} bytes)!" + ); + Ok(()) + } +} + +pub enum ResponseRef { + Ping(Ref), +} + +impl ResponseRef +where + B: ByteSlice, +{ + pub fn bytes(&self) -> &[u8] { + match self { + Self::Ping(r) => r.bytes(), + } + } +} + +impl ResponseRef +where + B: ByteSliceMut, +{ + pub fn bytes_mut(&mut self) -> &[u8] { + match self { + Self::Ping(r) => r.bytes_mut(), + } + } +} diff --git a/rosenpass/src/api/boilerplate/server.rs b/rosenpass/src/api/boilerplate/server.rs new file mode 100644 index 0000000..d4e42db --- /dev/null +++ b/rosenpass/src/api/boilerplate/server.rs @@ -0,0 +1,40 @@ +use zerocopy::{ByteSlice, ByteSliceMut}; + +use super::{ByteSliceRefExt, Message, PingRequest, PingResponse, RequestRef, RequestResponsePair}; + +pub trait Server { + fn ping(&mut self, req: &PingRequest, res: &mut PingResponse) -> anyhow::Result<()>; + + fn dispatch( + &mut self, + p: &mut RequestResponsePair, + ) -> anyhow::Result<()> + where + ReqBuf: ByteSlice, + ResBuf: ByteSliceMut, + { + match p { + RequestResponsePair::Ping((req, res)) => self.ping(req, res), + } + } + + fn handle_message(&mut self, req: ReqBuf, res: ResBuf) -> anyhow::Result + where + ReqBuf: ByteSlice, + ResBuf: ByteSliceMut, + { + let req = req.parse_request_from_prefix()?; + // TODO: This is not pretty; This match should be moved into RequestRef + let mut pair = match req { + RequestRef::Ping(req) => { + let mut res = res.ping_response_from_prefix()?; + res.init(); + RequestResponsePair::Ping((req, res)) + } + }; + self.dispatch(&mut pair)?; + + let res_len = pair.request().bytes().len(); + Ok(res_len) + } +} diff --git a/rosenpass/src/api/cli.rs b/rosenpass/src/api/cli.rs new file mode 100644 index 0000000..b160a5c --- /dev/null +++ b/rosenpass/src/api/cli.rs @@ -0,0 +1,40 @@ +use std::path::PathBuf; + +use clap::Args; + +use crate::config::Rosenpass as RosenpassConfig; + +use super::config::ApiConfig; + +#[cfg(feature = "experiment_api")] +#[derive(Args, Debug)] +pub struct ApiCli { + /// Where in the file-system to create the unix socket the rosenpass API will be listening for + /// connections on + #[arg(long)] + api_listen_path: Vec, + + /// When rosenpass is called from another process, the other process can open and bind the + /// unix socket for the Rosenpass API to use themselves, passing it to this process. In Rust this can be achieved + /// using the [command-fds](https://docs.rs/command-fds/latest/command_fds/) crate. + #[arg(long)] + api_listen_fd: Vec, + + /// When rosenpass is called from another process, the other process can connect the unix socket for the API + /// themselves, for instance using the `socketpair(2)` system call. + #[arg(long)] + api_stream_fd: Vec, +} + +impl ApiCli { + pub fn apply_to_config(&self, cfg: &mut RosenpassConfig) -> anyhow::Result<()> { + self.apply_to_api_config(&mut cfg.api) + } + + pub fn apply_to_api_config(&self, cfg: &mut ApiConfig) -> anyhow::Result<()> { + cfg.listen_path.extend_from_slice(&self.api_listen_path); + cfg.listen_fd.extend_from_slice(&self.api_listen_fd); + cfg.stream_fd.extend_from_slice(&self.api_stream_fd); + Ok(()) + } +} diff --git a/rosenpass/src/api/config.rs b/rosenpass/src/api/config.rs new file mode 100644 index 0000000..0a410a4 --- /dev/null +++ b/rosenpass/src/api/config.rs @@ -0,0 +1,41 @@ +use std::path::PathBuf; + +use mio::net::UnixListener; +use rosenpass_util::mio::{UnixListenerExt, UnixStreamExt}; +use serde::{Deserialize, Serialize}; + +use crate::app_server::AppServer; + +#[derive(Debug, Serialize, Deserialize, Default, Clone)] +pub struct ApiConfig { + /// Where in the file-system to create the unix socket the rosenpass API will be listening for + /// connections on + pub listen_path: Vec, + + /// When rosenpass is called from another process, the other process can open and bind the + /// unix socket for the Rosenpass API to use themselves, passing it to this process. In Rust this can be achieved + /// using the [command-fds](https://docs.rs/command-fds/latest/command_fds/) crate. + pub listen_fd: Vec, + + /// When rosenpass is called from another process, the other process can connect the unix socket for the API + /// themselves, for instance using the `socketpair(2)` system call. + pub stream_fd: Vec, +} + +impl ApiConfig { + pub fn apply_to_app_server(&self, srv: &mut AppServer) -> anyhow::Result<()> { + for path in self.listen_path.iter() { + srv.add_api_listener(UnixListener::bind(path)?)?; + } + + for fd in self.listen_fd.iter() { + srv.add_api_listener(UnixListenerExt::claim_fd(*fd)?)?; + } + + for fd in self.stream_fd.iter() { + srv.add_api_connection(UnixStreamExt::claim_fd(*fd)?)?; + } + + Ok(()) + } +} diff --git a/rosenpass/src/api/crypto_server_api_handler.rs b/rosenpass/src/api/crypto_server_api_handler.rs new file mode 100644 index 0000000..003f1d8 --- /dev/null +++ b/rosenpass/src/api/crypto_server_api_handler.rs @@ -0,0 +1,44 @@ +use rosenpass_to::{ops::copy_slice, To}; + +use crate::protocol::CryptoServer; + +use super::Server as ApiServer; + +#[derive(Debug)] +pub struct CryptoServerApiState { + _dummy: (), +} + +impl CryptoServerApiState { + #[allow(clippy::new_without_default)] + pub fn new() -> Self { + Self { _dummy: () } + } + + pub fn acquire_backend<'a>( + &'a mut self, + crypto: &'a mut Option, + ) -> CryptoServerApiHandler<'a> { + let state = self; + CryptoServerApiHandler { state, crypto } + } +} + +pub struct CryptoServerApiHandler<'a> { + #[allow(unused)] // TODO: Remove + crypto: &'a mut Option, + #[allow(unused)] // TODO: Remove + state: &'a mut CryptoServerApiState, +} + +impl<'a> ApiServer for CryptoServerApiHandler<'a> { + fn ping( + &mut self, + req: &super::PingRequest, + res: &mut super::PingResponse, + ) -> anyhow::Result<()> { + let (req, res) = (&req.payload, &mut res.payload); + copy_slice(&req.echo).to(&mut res.echo); + Ok(()) + } +} diff --git a/rosenpass/src/api/mio/connection.rs b/rosenpass/src/api/mio/connection.rs new file mode 100644 index 0000000..e2d196a --- /dev/null +++ b/rosenpass/src/api/mio/connection.rs @@ -0,0 +1,167 @@ +use mio::{net::UnixStream, Interest}; +use rosenpass_util::{ + io::{IoResultKindHintExt, TryIoResultKindHintExt}, + length_prefix_encoding::{ + decoder::{self as lpe_decoder, LengthPrefixDecoder}, + encoder::{self as lpe_encoder, LengthPrefixEncoder}, + }, +}; +use zeroize::Zeroize; + +use crate::{api::Server, app_server::MioTokenDispenser, protocol::CryptoServer}; + +use super::super::{CryptoServerApiState, MAX_REQUEST_LEN, MAX_RESPONSE_LEN}; + +#[derive(Debug)] +pub struct MioConnection { + io: UnixStream, + invalid_read: bool, + read_buffer: LengthPrefixDecoder<[u8; MAX_REQUEST_LEN]>, + write_buffer: LengthPrefixEncoder<[u8; MAX_RESPONSE_LEN]>, + api_state: CryptoServerApiState, +} + +impl MioConnection { + pub fn new( + mut io: UnixStream, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, // TODO: We should actually start using tokens… + ) -> std::io::Result { + registry.register( + &mut io, + token_dispenser.dispense(), + Interest::READABLE | Interest::WRITABLE, + )?; + + let invalid_read = false; + let read_buffer = LengthPrefixDecoder::new([0u8; MAX_REQUEST_LEN]); + let write_buffer = LengthPrefixEncoder::from_buffer([0u8; MAX_RESPONSE_LEN]); + let api_state = CryptoServerApiState::new(); + Ok(Self { + io, + invalid_read, + read_buffer, + write_buffer, + api_state, + }) + } + + pub fn poll(&mut self, crypto: &mut Option) -> anyhow::Result<()> { + self.flush_write_buffer()?; + if self.write_buffer.exhausted() { + self.recv(crypto)?; + } + Ok(()) + } + + // This is *exclusively* called by recv if the read_buffer holds a message + fn handle_incoming_message(&mut self, crypto: &mut Option) -> anyhow::Result<()> { + // Unwrap is allowed because recv() confirms before the call that a message was + // received + let req = self.read_buffer.message().unwrap().unwrap(); + + // TODO: The API should not return anyhow::Result + let response_len = self + .api_state + .acquire_backend(crypto) + .handle_message(req, self.write_buffer.buffer_bytes_mut())?; + self.read_buffer.zeroize(); // clear for new message to read + self.write_buffer + .restart_write_with_new_message(response_len)?; + + self.flush_write_buffer()?; + Ok(()) + } + + fn flush_write_buffer(&mut self) -> anyhow::Result<()> { + if self.write_buffer.exhausted() { + return Ok(()); + } + + loop { + use lpe_encoder::WriteToIoReturn as Ret; + use std::io::ErrorKind as K; + + match self + .write_buffer + .write_to_stdio(&self.io) + .io_err_kind_hint() + { + // Done + Ok(Ret { done: true, .. }) => { + self.write_buffer.zeroize(); // clear for new message to write + break; + } + + // Would block + Ok(Ret { + bytes_written: 0, .. + }) => break, + Err((_e, K::WouldBlock)) => break, + + // Just continue + Ok(_) => continue, /* Ret { bytes_written > 0, done = false } acc. to previous cases*/ + Err((_e, K::Interrupted)) => continue, + + // Other errors + Err((e, _ek)) => Err(e)?, + } + } + + Ok(()) + } + + fn recv(&mut self, crypto: &mut Option) -> anyhow::Result<()> { + if !self.write_buffer.exhausted() || self.invalid_read { + return Ok(()); + } + + loop { + use lpe_decoder::{ReadFromIoError as E, ReadFromIoReturn as Ret}; + use std::io::ErrorKind as K; + + match self + .read_buffer + .read_from_stdio(&self.io) + .try_io_err_kind_hint() + { + // We actually received a proper message + // (Impl below match to appease borrow checker) + Ok(Ret { + message: Some(_msg), + .. + }) => {} + + // Message does not fit in buffer + Err((e @ E::MessageTooLargeError { .. }, _)) => { + log::warn!("Received message on API that was too big to fit in our buffers; \ + looks like the client is broken. Stopping to process messages of the client.\n\ + Error: {e:?}"); + // TODO: We should properly close down the socket in this case, but to do that, + // we need to have the facilities in the Rosenpass IO handling system to close + // open connections. + // Just leaving the API connections dangling for now. + // This should be fixed for non-experimental use of the API. + self.invalid_read = true; + break; + } + + // Would block + Ok(Ret { bytes_read: 0, .. }) => break, + Err((_, Some(K::WouldBlock))) => break, + + // Just keep going + Ok(Ret { bytes_read: _, .. }) => continue, + Err((_, Some(K::Interrupted))) => continue, + + // Other IO Error (just pass on to the caller) + Err((E::IoError(e), _)) => Err(e)?, + }; + + self.handle_incoming_message(crypto)?; + break; // Handle just one message, leave some room for other IO handlers + } + + Ok(()) + } +} diff --git a/rosenpass/src/api/mio/manager.rs b/rosenpass/src/api/mio/manager.rs new file mode 100644 index 0000000..172addf --- /dev/null +++ b/rosenpass/src/api/mio/manager.rs @@ -0,0 +1,91 @@ +use std::io; + +use mio::net::{UnixListener, UnixStream}; + +use rosenpass_util::{io::nonblocking_handle_io_errors, mio::interest::RW as MIO_RW}; + +use crate::{app_server::MioTokenDispenser, protocol::CryptoServer}; + +use super::MioConnection; + +#[derive(Default, Debug)] +pub struct MioManager { + listeners: Vec, + connections: Vec, +} + +impl MioManager { + pub fn new() -> Self { + Self::default() + } + + pub fn add_listener( + &mut self, + mut listener: UnixListener, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, + ) -> io::Result<()> { + registry.register(&mut listener, token_dispenser.dispense(), MIO_RW)?; + self.listeners.push(listener); + Ok(()) + } + + pub fn add_connection( + &mut self, + connection: UnixStream, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, + ) -> io::Result<()> { + let connection = MioConnection::new(connection, registry, token_dispenser)?; + self.connections.push(connection); + Ok(()) + } + + pub fn poll( + &mut self, + crypto: &mut Option, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, + ) -> anyhow::Result<()> { + self.accept_connections(registry, token_dispenser)?; + self.poll_connections(crypto)?; + Ok(()) + } + + fn accept_connections( + &mut self, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, + ) -> io::Result<()> { + for idx in 0..self.listeners.len() { + self.accept_from(idx, registry, token_dispenser)?; + } + Ok(()) + } + + fn accept_from( + &mut self, + idx: usize, + registry: &mio::Registry, + token_dispenser: &mut MioTokenDispenser, + ) -> io::Result<()> { + // Accept connection until the socket would block or returns another error + loop { + match nonblocking_handle_io_errors(|| self.listeners[idx].accept())? { + None => break, + Some((conn, _addr)) => { + self.add_connection(conn, registry, token_dispenser)?; + } + }; + } + + Ok(()) + } + + fn poll_connections(&mut self, crypto: &mut Option) -> anyhow::Result<()> { + for conn in self.connections.iter_mut() { + conn.poll(crypto)? + } + Ok(()) + } +} diff --git a/rosenpass/src/api/mio/mod.rs b/rosenpass/src/api/mio/mod.rs new file mode 100644 index 0000000..c284c8b --- /dev/null +++ b/rosenpass/src/api/mio/mod.rs @@ -0,0 +1,5 @@ +mod connection; +mod manager; + +pub use connection::*; +pub use manager::*; diff --git a/rosenpass/src/api/mod.rs b/rosenpass/src/api/mod.rs new file mode 100644 index 0000000..9c9781d --- /dev/null +++ b/rosenpass/src/api/mod.rs @@ -0,0 +1,9 @@ +mod boilerplate; +mod crypto_server_api_handler; + +pub use boilerplate::*; +pub use crypto_server_api_handler::*; + +pub mod cli; +pub mod config; +pub mod mio; diff --git a/rosenpass/src/app_server.rs b/rosenpass/src/app_server.rs index 55ceeea..15353bc 100644 --- a/rosenpass/src/app_server.rs +++ b/rosenpass/src/app_server.rs @@ -1,5 +1,6 @@ use anyhow::bail; +use anyhow::Context; use anyhow::Result; use derive_builder::Builder; use log::{error, info, warn}; @@ -65,7 +66,7 @@ pub struct MioTokenDispenser { } impl MioTokenDispenser { - fn dispense(&mut self) -> Token { + pub fn dispense(&mut self) -> Token { let r = self.counter; self.counter += 1; Token(r) @@ -146,7 +147,7 @@ pub struct AppServerTest { // TODO add user control via unix domain socket and stdin/stdout #[derive(Debug)] pub struct AppServer { - pub crypt: CryptoServer, + pub crypt: Option, pub sockets: Vec, pub events: mio::Events, pub mio_poll: mio::Poll, @@ -161,6 +162,8 @@ pub struct AppServer { pub unpolled_count: usize, pub last_update_time: Instant, pub test_helpers: Option, + #[cfg(feature = "experiment_api")] + pub api_manager: crate::api::mio::MioManager, } /// A socket pointer is an index assigned to a socket; @@ -603,7 +606,7 @@ impl AppServer { // TODO use mio::net::UnixStream together with std::os::unix::net::UnixStream for Linux Ok(Self { - crypt: CryptoServer::new(sk, pk), + crypt: Some(CryptoServer::new(sk, pk)), peers: Vec::new(), verbosity, sockets, @@ -618,9 +621,23 @@ impl AppServer { unpolled_count: 0, last_update_time: Instant::now(), test_helpers, + #[cfg(feature = "experiment_api")] + api_manager: crate::api::mio::MioManager::default(), }) } + pub fn crypto_server(&self) -> anyhow::Result<&CryptoServer> { + self.crypt + .as_ref() + .context("Cryptography handler not initialized") + } + + pub fn crypto_server_mut(&mut self) -> anyhow::Result<&mut CryptoServer> { + self.crypt + .as_mut() + .context("Cryptography handler not initialized") + } + pub fn verbose(&self) -> bool { matches!(self.verbosity, Verbosity::Verbose) } @@ -671,7 +688,7 @@ impl AppServer { broker_peer: Option, hostname: Option, ) -> anyhow::Result { - let PeerPtr(pn) = self.crypt.add_peer(psk, pk)?; + let PeerPtr(pn) = self.crypto_server_mut()?.add_peer(psk, pk)?; assert!(pn == self.peers.len()); let initial_endpoint = hostname .map(Endpoint::discovery_from_hostname) @@ -714,7 +731,7 @@ impl AppServer { ); if tries_left > 0 { error!("re-initializing networking in {sleep}! {tries_left} tries left."); - std::thread::sleep(self.crypt.timebase.dur(sleep)); + std::thread::sleep(self.crypto_server_mut()?.timebase.dur(sleep)); continue; } @@ -760,11 +777,11 @@ impl AppServer { match self.poll(&mut *rx)? { #[allow(clippy::redundant_closure_call)] SendInitiation(peer) => tx_maybe_with!(peer, || self - .crypt + .crypto_server_mut()? .initiate_handshake(peer.lower(), &mut *tx))?, #[allow(clippy::redundant_closure_call)] SendRetransmission(peer) => tx_maybe_with!(peer, || self - .crypt + .crypto_server_mut()? .retransmit_handshake(peer.lower(), &mut *tx))?, DeleteKey(peer) => { self.output_key(peer, Stale, &SymKey::random())?; @@ -785,7 +802,9 @@ impl AppServer { DoSOperation::UnderLoad => { self.handle_msg_under_load(&endpoint, &rx[..len], &mut *tx) } - DoSOperation::Normal => self.crypt.handle_msg(&rx[..len], &mut *tx), + DoSOperation::Normal => { + self.crypto_server_mut()?.handle_msg(&rx[..len], &mut *tx) + } }; match msg_result { Err(ref e) => { @@ -813,7 +832,8 @@ impl AppServer { ap.get_app_mut(self).current_endpoint = Some(endpoint); // TODO: Maybe we should rather call the key "rosenpass output"? - self.output_key(ap, Exchanged, &self.crypt.osk(p)?)?; + let osk = &self.crypto_server_mut()?.osk(p)?; + self.output_key(ap, Exchanged, osk)?; } } } @@ -829,9 +849,9 @@ impl AppServer { tx: &mut [u8], ) -> Result { match endpoint { - Endpoint::SocketBoundAddress(socket) => { - self.crypt.handle_msg_under_load(rx, &mut *tx, socket) - } + Endpoint::SocketBoundAddress(socket) => self + .crypto_server_mut()? + .handle_msg_under_load(rx, &mut *tx, socket), Endpoint::Discovery(_) => { anyhow::bail!("Host-path discovery is not supported under load") } @@ -844,7 +864,7 @@ impl AppServer { why: KeyOutputReason, key: &SymKey, ) -> anyhow::Result<()> { - let peerid = peer.lower().get(&self.crypt).pidt()?; + let peerid = peer.lower().get(self.crypto_server()?).pidt()?; if self.verbose() { let msg = match why { @@ -891,7 +911,7 @@ impl AppServer { use crate::protocol::PollResult as C; use AppPollResult as A; loop { - return Ok(match self.crypt.poll()? { + return Ok(match self.crypto_server_mut()?.poll()? { C::DeleteKey(PeerPtr(no)) => A::DeleteKey(AppPeerPtr(no)), C::SendInitiation(PeerPtr(no)) => A::SendInitiation(AppPeerPtr(no)), C::SendRetransmission(PeerPtr(no)) => A::SendRetransmission(AppPeerPtr(no)), @@ -1019,6 +1039,33 @@ impl AppServer { broker.process_poll()?; } + // API poll + + #[cfg(feature = "experiment_api")] + self.api_manager.poll( + &mut self.crypt, + self.mio_poll.registry(), + &mut self.mio_token_dispenser, + )?; + Ok(None) } + + #[cfg(feature = "experiment_api")] + pub fn add_api_connection(&mut self, connection: mio::net::UnixStream) -> std::io::Result<()> { + self.api_manager.add_connection( + connection, + self.mio_poll.registry(), + &mut self.mio_token_dispenser, + ) + } + + #[cfg(feature = "experiment_api")] + pub fn add_api_listener(&mut self, listener: mio::net::UnixListener) -> std::io::Result<()> { + self.api_manager.add_listener( + listener, + self.mio_poll.registry(), + &mut self.mio_token_dispenser, + ) + } } diff --git a/rosenpass/src/bin/gen-ipc-msg-types.rs b/rosenpass/src/bin/gen-ipc-msg-types.rs new file mode 100644 index 0000000..2e128de --- /dev/null +++ b/rosenpass/src/bin/gen-ipc-msg-types.rs @@ -0,0 +1,86 @@ +use anyhow::{Context, Result}; +use heck::ToShoutySnakeCase; + +use rosenpass_ciphers::{hash_domain::HashDomain, KEY_LEN}; + +fn calculate_hash_value(hd: HashDomain, values: &[&str]) -> Result<[u8; KEY_LEN]> { + match values.split_first() { + Some((head, tail)) => calculate_hash_value(hd.mix(head.as_bytes())?, tail), + None => Ok(hd.into_value()), + } +} + +fn print_literal(path: &[&str]) -> Result<()> { + let val = calculate_hash_value(HashDomain::zero(), path)?; + let (last, prefix) = path.split_last().context("developer error!")?; + let var_name = last.to_shouty_snake_case(); + + print!("// hash domain hash of: "); + for n in prefix.iter() { + print!("{n} -> "); + } + println!("{last}"); + + let c = hex::encode(val) + .chars() + .collect::>() + .chunks_exact(4) + .map(|chunk| chunk.iter().collect::()) + .collect::>(); + println!("const {var_name} : RawMsgType = RawMsgType::from_le_bytes(hex!(\"{} {} {} {} {} {} {} {}\"));", + c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7]); + + Ok(()) +} + +#[derive(Debug, Clone)] +enum Tree { + Branch(String, Vec), + Leaf(String), +} + +impl Tree { + fn name(&self) -> &str { + match self { + Self::Branch(name, _) => name, + Self::Leaf(name) => name, + } + } + + fn gen_code_inner(&self, prefix: &[&str]) -> Result<()> { + let mut path = prefix.to_owned(); + path.push(self.name()); + + match self { + Self::Branch(_, ref children) => { + for c in children.iter() { + c.gen_code_inner(&path)? + } + } + Self::Leaf(_) => print_literal(&path)?, + }; + + Ok(()) + } + + fn gen_code(&self) -> Result<()> { + self.gen_code_inner(&[]) + } +} + +fn main() -> Result<()> { + let tree = Tree::Branch( + "Rosenpass IPC API".to_owned(), + vec![Tree::Branch( + "Rosenpass Protocol Server".to_owned(), + vec![ + Tree::Leaf("Ping Request".to_owned()), + Tree::Leaf("Ping Response".to_owned()), + ], + )], + ); + + println!("type RawMsgType = u128;"); + println!(); + tree.gen_code() +} diff --git a/rosenpass/src/cli.rs b/rosenpass/src/cli.rs index 473b30f..fae2f88 100644 --- a/rosenpass/src/cli.rs +++ b/rosenpass/src/cli.rs @@ -32,11 +32,21 @@ pub struct CliArgs { #[arg(short, long, group = "log-level")] quiet: bool, + #[command(flatten)] + #[cfg(feature = "experiment_api")] + api: crate::api::cli::ApiCli, + #[command(subcommand)] pub command: CliCommand, } impl CliArgs { + pub fn apply_to_config(&self, _cfg: &mut config::Rosenpass) -> anyhow::Result<()> { + #[cfg(feature = "experiment_api")] + self.api.apply_to_config(_cfg)?; + Ok(()) + } + /// returns the log level filter set by CLI args /// returns `None` if the user did not specify any log level filter via CLI /// @@ -259,8 +269,10 @@ impl CliArgs { "config file '{config_file:?}' does not exist" ); - let config = config::Rosenpass::load(config_file)?; + let mut config = config::Rosenpass::load(config_file)?; config.validate()?; + self.apply_to_config(&mut config)?; + Self::event_loop(config, test_helpers)?; } @@ -279,6 +291,8 @@ impl CliArgs { config.config_file_path.clone_from(p); } config.validate()?; + self.apply_to_config(&mut config)?; + Self::event_loop(config, test_helpers)?; } @@ -320,6 +334,8 @@ impl CliArgs { test_helpers, )?); + config.apply_to_app_server(&mut srv)?; + let broker_store_ptr = srv.register_broker(Box::new(NativeUnixBroker::new()))?; fn cfg_err_map(e: NativeUnixBrokerConfigBaseBuilderError) -> anyhow::Error { @@ -367,3 +383,15 @@ fn generate_and_save_keypair(secret_key: PathBuf, public_key: PathBuf) -> anyhow ssk.store_secret(secret_key)?; spk.store(public_key) } + +#[cfg(feature = "internal_testing")] +pub mod testing { + use super::*; + + pub fn generate_and_save_keypair( + secret_key: PathBuf, + public_key: PathBuf, + ) -> anyhow::Result<()> { + super::generate_and_save_keypair(secret_key, public_key) + } +} diff --git a/rosenpass/src/config.rs b/rosenpass/src/config.rs index ab320ee..e7538d3 100644 --- a/rosenpass/src/config.rs +++ b/rosenpass/src/config.rs @@ -19,6 +19,8 @@ use anyhow::{bail, ensure}; use rosenpass_util::file::{fopen_w, Visibility}; use serde::{Deserialize, Serialize}; +use crate::app_server::AppServer; + #[derive(Debug, Serialize, Deserialize)] pub struct Rosenpass { /// path to the public key file @@ -27,6 +29,10 @@ pub struct Rosenpass { /// path to the secret key file pub secret_key: PathBuf, + /// Location of the API listen sockets + #[cfg(feature = "experiment_api")] + pub api: crate::api::config::ApiConfig, + /// list of [`SocketAddr`] to listen on /// /// Examples: @@ -54,7 +60,7 @@ pub struct Rosenpass { /// ## TODO /// - replace this type with [`log::LevelFilter`], also see -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Copy, Clone)] pub enum Verbosity { Quiet, Verbose, @@ -157,6 +163,12 @@ impl Rosenpass { self.store(&self.config_file_path) } + pub fn apply_to_app_server(&self, _srv: &mut AppServer) -> anyhow::Result<()> { + #[cfg(feature = "experiment_api")] + self.api.apply_to_app_server(_srv)?; + Ok(()) + } + /// Validate a configuration /// /// ## TODO @@ -206,6 +218,8 @@ impl Rosenpass { public_key: PathBuf::from(public_key.as_ref()), secret_key: PathBuf::from(secret_key.as_ref()), listen: vec![], + #[cfg(feature = "experiment_api")] + api: crate::api::config::ApiConfig::default(), verbosity: Verbosity::Quiet, peers: vec![], config_file_path: PathBuf::new(), diff --git a/rosenpass/src/lib.rs b/rosenpass/src/lib.rs index a561dc0..b186bd2 100644 --- a/rosenpass/src/lib.rs +++ b/rosenpass/src/lib.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "experiment_api")] +pub mod api; pub mod app_server; pub mod cli; pub mod config; @@ -11,4 +13,8 @@ pub enum RosenpassError { BufferSizeMismatch, #[error("invalid message type")] InvalidMessageType(u8), + #[error("invalid API message type")] + InvalidApiMessageType(u128), + #[error("could not parse API message")] + InvalidApiMessage, } diff --git a/rosenpass/tests/api-integration-tests.rs b/rosenpass/tests/api-integration-tests.rs new file mode 100644 index 0000000..da9c84a --- /dev/null +++ b/rosenpass/tests/api-integration-tests.rs @@ -0,0 +1,180 @@ +use std::{ + io::{BufRead, BufReader}, + net::ToSocketAddrs, + os::unix::net::UnixStream, + process::Stdio, +}; + +use anyhow::{bail, Context}; +use rosenpass::api; +use rosenpass_to::{ops::copy_slice_least_src, To}; +use rosenpass_util::zerocopy::ZerocopySliceExt; +use rosenpass_util::{ + file::LoadValueB64, + length_prefix_encoding::{decoder::LengthPrefixDecoder, encoder::LengthPrefixEncoder}, +}; +use tempfile::TempDir; +use zerocopy::AsBytes; + +use rosenpass::protocol::SymKey; + +#[test] +fn api_integration_test() -> anyhow::Result<()> { + rosenpass_secret_memory::policy::secret_policy_use_only_malloc_secrets(); + + let dir = TempDir::with_prefix("rosenpass-api-integration-test")?; + + macro_rules! tempfile { + ($($lst:expr),+) => {{ + let mut buf = dir.path().to_path_buf(); + $(buf.push($lst);)* + buf + }} + } + + let peer_a_endpoint = "[::1]:61423"; + let peer_a_osk = tempfile!("a.osk"); + let peer_b_osk = tempfile!("b.osk"); + + use rosenpass::config; + let peer_a = config::Rosenpass { + config_file_path: tempfile!("a.config"), + secret_key: tempfile!("a.sk"), + public_key: tempfile!("a.pk"), + listen: peer_a_endpoint.to_socket_addrs()?.collect(), // TODO: This could collide by accident + verbosity: config::Verbosity::Verbose, + api: api::config::ApiConfig { + listen_path: vec![tempfile!("a.sock")], + listen_fd: vec![], + stream_fd: vec![], + }, + peers: vec![config::RosenpassPeer { + public_key: tempfile!("b.pk"), + key_out: Some(peer_a_osk.clone()), + endpoint: None, + pre_shared_key: None, + wg: None, + }], + }; + + let peer_b = config::Rosenpass { + config_file_path: tempfile!("b.config"), + secret_key: tempfile!("b.sk"), + public_key: tempfile!("b.pk"), + listen: vec![], + verbosity: config::Verbosity::Verbose, + api: api::config::ApiConfig { + listen_path: vec![tempfile!("b.sock")], + listen_fd: vec![], + stream_fd: vec![], + }, + peers: vec![config::RosenpassPeer { + public_key: tempfile!("a.pk"), + key_out: Some(peer_b_osk.clone()), + endpoint: Some(peer_a_endpoint.to_owned()), + pre_shared_key: None, + wg: None, + }], + }; + + // Generate the keys + rosenpass::cli::testing::generate_and_save_keypair( + peer_a.secret_key.clone(), + peer_a.public_key.clone(), + )?; + rosenpass::cli::testing::generate_and_save_keypair( + peer_b.secret_key.clone(), + peer_b.public_key.clone(), + )?; + + // Write the configuration files + peer_a.commit()?; + peer_b.commit()?; + + // Start peer a + let proc_a = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) + .args([ + "exchange-config", + peer_a.config_file_path.to_str().context("")?, + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .spawn()?; + + // Start peer b + let proc_b = std::process::Command::new(env!("CARGO_BIN_EXE_rosenpass")) + .args([ + "exchange-config", + peer_b.config_file_path.to_str().context("")?, + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .spawn()?; + + // Acquire stdout + let mut out_a = BufReader::new(proc_a.stdout.context("")?).lines(); + let mut out_b = BufReader::new(proc_b.stdout.context("")?).lines(); + + // Wait for the keys to successfully exchange a key + let mut attempt = 0; + loop { + let line_a = out_a.next().context("")??; + let line_b = out_b.next().context("")??; + + let words_a = line_a.split(' ').collect::>(); + let words_b = line_b.split(' ').collect::>(); + + // FIXED FIXED PEER-ID FIXED FILENAME STATUS + // output-key peer KZqXTZ4l2aNnkJtLPhs4D8JxHTGmRSL9w3Qr+X8JxFk= key-file "client-A-osk" exchanged + let peer_a_id = words_b + .get(2) + .with_context(|| format!("Bad rosenpass output: `{line_b}`"))?; + let peer_b_id = words_a + .get(2) + .with_context(|| format!("Bad rosenpass output: `{line_a}`"))?; + assert_eq!( + line_a, + format!( + "output-key peer {peer_b_id} key-file \"{}\" exchanged", + peer_a_osk.to_str().context("")? + ) + ); + assert_eq!( + line_b, + format!( + "output-key peer {peer_a_id} key-file \"{}\" exchanged", + peer_b_osk.to_str().context("")? + ) + ); + + // Read OSKs + let osk_a = SymKey::load_b64::<64, _>(peer_a_osk.clone())?; + let osk_b = SymKey::load_b64::<64, _>(peer_b_osk.clone())?; + match osk_a.secret() == osk_b.secret() { + true => break, + false if attempt > 10 => bail!("Peers did not produce a matching key even after ten attempts. Something is wrong with the key exchange!"), + false => {}, + }; + + attempt += 1; + } + + // Now connect to the peers + let api_a = UnixStream::connect(&peer_a.api.listen_path[0])?; + let api_b = UnixStream::connect(&peer_b.api.listen_path[0])?; + + for conn in ([api_a, api_b]).iter() { + let mut echo = [0u8; 256]; + copy_slice_least_src("Hello World".as_bytes()).to(&mut echo); + + let req = api::PingRequest::new(echo); + LengthPrefixEncoder::from_message(req.as_bytes()).write_all_to_stdio(conn)?; + + let mut decoder = LengthPrefixDecoder::new([0u8; api::MAX_RESPONSE_LEN]); + let res = decoder.read_all_from_stdio(conn)?; + let res = res.zk_parse::()?; + assert_eq!(*res, api::PingResponse::new(echo)); + } + + Ok(()) +}