mirror of
https://github.com/rosenpass/rosenpass.git
synced 2025-12-18 13:24:38 +03:00
Compare commits
11 Commits
dev/karo/r
...
dev/blipp/
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf67344e86 | ||
|
|
d2539e445f | ||
|
|
6dc58cc6c1 | ||
|
|
e3d16966c9 | ||
|
|
a5e6af4b49 | ||
|
|
24a71977f0 | ||
|
|
5f0ac579d7 | ||
|
|
4df994b5f0 | ||
|
|
e4e0a9e661 | ||
|
|
742e037936 | ||
|
|
b5848af799 |
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -109,9 +109,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.94"
|
||||
version = "1.0.95"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
|
||||
checksum = "34ac096ce696dc2fcabef30516bb13c0a68a11d30131d3df6f04711467681b04"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
]
|
||||
@@ -2114,18 +2114,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.216"
|
||||
version = "1.0.217"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
|
||||
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.216"
|
||||
version = "1.0.217"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
||||
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
||||
@@ -50,9 +50,9 @@ log = { version = "0.4.22" }
|
||||
clap = { version = "4.5.23", features = ["derive"] }
|
||||
clap_mangen = "0.2.24"
|
||||
clap_complete = "4.5.40"
|
||||
serde = { version = "1.0.216", features = ["derive"] }
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
arbitrary = { version = "1.4.1", features = ["derive"] }
|
||||
anyhow = { version = "1.0.94", features = ["backtrace", "std"] }
|
||||
anyhow = { version = "1.0.95", features = ["backtrace", "std"] }
|
||||
mio = { version = "1.0.3", features = ["net", "os-poll"] }
|
||||
oqs-sys = { version = "0.9.1", default-features = false, features = [
|
||||
'classic_mceliece',
|
||||
|
||||
@@ -50,6 +50,8 @@ rustPlatform.buildRustPackage {
|
||||
cargoBuildOptions = [ "--package" package ];
|
||||
cargoTestOptions = [ "--package" package ];
|
||||
|
||||
buildFeatures = [ "experiment_api" ];
|
||||
|
||||
doCheck = true;
|
||||
|
||||
cargoLock = {
|
||||
|
||||
@@ -26,6 +26,10 @@ required-features = ["experiment_api", "internal_testing"]
|
||||
name = "api-integration-tests-api-setup"
|
||||
required-features = ["experiment_api", "internal_testing"]
|
||||
|
||||
[[test]]
|
||||
name = "gen-ipc-msg-types"
|
||||
required-features = ["experiment_api", "internal_testing", "internal_bin_gen_ipc_msg_types"]
|
||||
|
||||
[[bench]]
|
||||
name = "handshake"
|
||||
harness = false
|
||||
|
||||
@@ -8,210 +8,250 @@ use super::{
|
||||
};
|
||||
|
||||
pub trait ByteSliceRefExt: ByteSlice {
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn msg_type_maker(self) -> RefMaker<Self, RawMsgType> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
fn msg_type(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
fn msg_type_from_prefix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
fn msg_type_from_suffix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker] and
|
||||
/// [RefMakerRawMsgTypeExt::parse_request_msg_type]
|
||||
fn request_msg_type(self) -> anyhow::Result<RequestMsgType> {
|
||||
self.msg_type_maker().parse_request_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_prefix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_request_msg_type].
|
||||
fn request_msg_type_from_prefix(self) -> anyhow::Result<RequestMsgType> {
|
||||
self.msg_type_maker()
|
||||
.from_prefix()?
|
||||
.parse_request_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_suffix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_request_msg_type].
|
||||
fn request_msg_type_from_suffix(self) -> anyhow::Result<RequestMsgType> {
|
||||
self.msg_type_maker()
|
||||
.from_suffix()?
|
||||
.parse_request_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMakerRawMsgTypeExt::parse_response_msg_type].
|
||||
fn response_msg_type(self) -> anyhow::Result<ResponseMsgType> {
|
||||
self.msg_type_maker().parse_response_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_prefix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_response_msg_type].
|
||||
fn response_msg_type_from_prefix(self) -> anyhow::Result<ResponseMsgType> {
|
||||
self.msg_type_maker()
|
||||
.from_prefix()?
|
||||
.parse_response_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker],
|
||||
/// [RefMaker::from_suffix], and
|
||||
/// [RefMakerRawMsgTypeExt::parse_response_msg_type].
|
||||
fn response_msg_type_from_suffix(self) -> anyhow::Result<ResponseMsgType> {
|
||||
self.msg_type_maker()
|
||||
.from_suffix()?
|
||||
.parse_response_msg_type()
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [RequestRef::parse] in chaining.
|
||||
fn parse_request(self) -> anyhow::Result<RequestRef<Self>> {
|
||||
RequestRef::parse(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [RequestRef::parse_from_prefix] in chaining.
|
||||
fn parse_request_from_prefix(self) -> anyhow::Result<RequestRef<Self>> {
|
||||
RequestRef::parse_from_prefix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [RequestRef::parse_from_suffix] in chaining.
|
||||
fn parse_request_from_suffix(self) -> anyhow::Result<RequestRef<Self>> {
|
||||
RequestRef::parse_from_suffix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [ResponseRef::parse] in chaining.
|
||||
fn parse_response(self) -> anyhow::Result<ResponseRef<Self>> {
|
||||
ResponseRef::parse(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [ResponseRef::parse_from_prefix] in chaining.
|
||||
fn parse_response_from_prefix(self) -> anyhow::Result<ResponseRef<Self>> {
|
||||
ResponseRef::parse_from_prefix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the use of [ResponseRef::parse_from_suffix] in chaining.
|
||||
fn parse_response_from_suffix(self) -> anyhow::Result<ResponseRef<Self>> {
|
||||
ResponseRef::parse_from_suffix(self)
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn ping_request_maker(self) -> RefMaker<Self, PingRequest> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn ping_request(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn ping_request_from_prefix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn ping_request_from_suffix(self) -> anyhow::Result<Ref<Self, PingRequest>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn ping_response_maker(self) -> RefMaker<Self, PingResponse> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn ping_response(self) -> anyhow::Result<Ref<Self, PingResponse>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn ping_response_from_prefix(self) -> anyhow::Result<Ref<Self, PingResponse>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn ping_response_from_suffix(self) -> anyhow::Result<Ref<Self, PingResponse>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn supply_keypair_request(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn supply_keypair_request_from_prefix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn supply_keypair_request_from_suffix(self) -> anyhow::Result<Ref<Self, SupplyKeypairRequest>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn supply_keypair_response_maker(self) -> RefMaker<Self, SupplyKeypairResponse> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn supply_keypair_response(self) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn supply_keypair_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn supply_keypair_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, SupplyKeypairResponse>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn add_listen_socket_request(self) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_listen_socket_request_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_listen_socket_request_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketRequest>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn add_listen_socket_response_maker(self) -> RefMaker<Self, super::AddListenSocketResponse> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn add_listen_socket_response(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_listen_socket_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_listen_socket_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddListenSocketResponse>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn add_psk_broker_request(self) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_psk_broker_request_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_psk_broker_request_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerRequest>> {
|
||||
self.zk_parse_suffix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_ref_maker].
|
||||
fn add_psk_broker_response_maker(self) -> RefMaker<Self, super::AddPskBrokerResponse> {
|
||||
self.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse].
|
||||
fn add_psk_broker_response(self) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>> {
|
||||
self.zk_parse()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_prefix].
|
||||
fn add_psk_broker_response_from_prefix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>> {
|
||||
self.zk_parse_prefix()
|
||||
}
|
||||
|
||||
/// Shorthand for the typed use of [ZerocopySliceExt::zk_parse_suffix].
|
||||
fn add_psk_broker_response_from_suffix(
|
||||
self,
|
||||
) -> anyhow::Result<Ref<Self, super::AddPskBrokerResponse>> {
|
||||
|
||||
@@ -4,16 +4,41 @@ use rosenpass_util::zerocopy::RefMaker;
|
||||
|
||||
use super::RawMsgType;
|
||||
|
||||
/// Trait implemented by all the Rosenpass API message types.
|
||||
///
|
||||
/// Implemented by the message as including the message envelope; e.g.
|
||||
/// [crate::api::PingRequest] but not by [crate::api::PingRequestPayload].
|
||||
pub trait Message {
|
||||
/// The payload this API message contains. E.g. this is [crate::api::PingRequestPayload] for [[crate::api::PingRequest].
|
||||
type Payload;
|
||||
/// Either [crate::api::RequestMsgType] or [crate::api::ResponseMsgType]
|
||||
type MessageClass: Into<RawMsgType>;
|
||||
/// The specific message type in the [Self::MessageClass].
|
||||
/// E.g. this is [crate::api::RequestMsgType::Ping] for [crate::api::PingRequest]
|
||||
const MESSAGE_TYPE: Self::MessageClass;
|
||||
|
||||
/// Wraps the payload into the envelope
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [crate::api::PingRequest::from_payload]
|
||||
fn from_payload(payload: Self::Payload) -> Self;
|
||||
/// Initialize the message;
|
||||
/// just sets the message type [crate::api::Envelope::msg_type].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [crate::api::PingRequest::init]
|
||||
fn init(&mut self);
|
||||
/// Initialize the message from a raw buffer: Zeroize the buffer and then call [Self::init].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [crate::api::PingRequest::setup]
|
||||
fn setup<B: ByteSliceMut>(buf: B) -> anyhow::Result<Ref<B, Self>>;
|
||||
}
|
||||
|
||||
/// Additional convenience functions for working with [rosenpass_util::zerocopy::RefMaker]
|
||||
pub trait ZerocopyResponseMakerSetupMessageExt<B, T> {
|
||||
fn setup_msg(self) -> anyhow::Result<Ref<B, T>>;
|
||||
}
|
||||
@@ -23,6 +48,27 @@ where
|
||||
B: ByteSliceMut,
|
||||
T: Message,
|
||||
{
|
||||
/// Initialize the message using [Message::setup].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use rosenpass::api::{
|
||||
/// PingRequest, ZerocopyResponseMakerSetupMessageExt, PING_REQUEST,
|
||||
/// };
|
||||
/// use rosenpass_util::zerocopy::RefMaker;
|
||||
/// use std::mem::size_of;
|
||||
///
|
||||
/// let mut buf = [0u8; { size_of::<PingRequest>() }];
|
||||
///
|
||||
/// let rm = RefMaker::<&mut [u8], PingRequest>::new(&mut buf);
|
||||
/// let msg: zerocopy::Ref<_, PingRequest> = rm.setup_msg()?;
|
||||
///
|
||||
/// let t = msg.msg_type; // Deal with unaligned read
|
||||
/// assert_eq!(t, PING_REQUEST);
|
||||
///
|
||||
/// Ok::<(), anyhow::Error>(())
|
||||
/// ```
|
||||
fn setup_msg(self) -> anyhow::Result<Ref<B, T>> {
|
||||
T::setup(self.into_buf())
|
||||
}
|
||||
|
||||
@@ -35,10 +35,15 @@ const ADD_PSK_BROKER_REQUEST: RawMsgType =
|
||||
const ADD_PSK_BROKER_RESPONSE: RawMsgType =
|
||||
RawMsgType::from_le_bytes(hex!("bd25 e418 ffb0 6930 248b 217e 2fae e353"));
|
||||
|
||||
/// Message properties global to the message type
|
||||
pub trait MessageAttributes {
|
||||
/// Get the size of the message
|
||||
///
|
||||
/// # Exampleds
|
||||
fn message_size(&self) -> usize;
|
||||
}
|
||||
|
||||
/// API request message types as an enum
|
||||
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
|
||||
pub enum RequestMsgType {
|
||||
Ping,
|
||||
@@ -47,6 +52,7 @@ pub enum RequestMsgType {
|
||||
AddPskBroker,
|
||||
}
|
||||
|
||||
/// API response messages types as an enum
|
||||
#[derive(Hash, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
|
||||
pub enum ResponseMsgType {
|
||||
Ping,
|
||||
@@ -131,8 +137,17 @@ impl From<ResponseMsgType> for RawMsgType {
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for [RawMsgType].
|
||||
///
|
||||
/// We are using an extension trait rather than just using methods
|
||||
/// because [RawMsgType] is a type alias, so we can not define methods
|
||||
/// on it.
|
||||
pub trait RawMsgTypeExt {
|
||||
/// Try to convert this to a [RequestMsgType]; alias for the appropriate [TryFrom]
|
||||
/// implementation
|
||||
fn into_request_msg_type(self) -> Result<RequestMsgType, RosenpassError>;
|
||||
/// Try to convert this to a [ResponseMsgType]; alias for the appropriate [TryFrom]
|
||||
/// implementation
|
||||
fn into_response_msg_type(self) -> Result<ResponseMsgType, RosenpassError>;
|
||||
}
|
||||
|
||||
@@ -146,8 +161,11 @@ impl RawMsgTypeExt for RawMsgType {
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for [rosenpass_util::zerocopy::RefMaker].
|
||||
pub trait RefMakerRawMsgTypeExt {
|
||||
/// Parse a request message type from bytes
|
||||
fn parse_request_msg_type(self) -> anyhow::Result<RequestMsgType>;
|
||||
/// Parse a response message type from bytes
|
||||
fn parse_response_msg_type(self) -> anyhow::Result<ResponseMsgType>;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
//! Boring, repetitive code related to message parsing for the API.
|
||||
//!
|
||||
//! Most of this should be automatically generated though some derive macro at some point.
|
||||
|
||||
mod byte_slice_ext;
|
||||
mod message_trait;
|
||||
mod message_type;
|
||||
|
||||
@@ -3,11 +3,14 @@ use zerocopy::{AsBytes, ByteSliceMut, FromBytes, FromZeroes, Ref};
|
||||
|
||||
use super::{Message, RawMsgType, RequestMsgType, ResponseMsgType};
|
||||
|
||||
/// Size required to fit any message in binary form
|
||||
/// Size required to fit any request message in binary form
|
||||
pub const MAX_REQUEST_LEN: usize = 2500; // TODO fix this
|
||||
/// Size required to fit any response message in binary form
|
||||
pub const MAX_RESPONSE_LEN: usize = 2500; // TODO fix this
|
||||
/// Maximum number of file descriptors that can be sent in a request.
|
||||
pub const MAX_REQUEST_FDS: usize = 2;
|
||||
|
||||
/// Message envelope for API messages
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
@@ -17,9 +20,12 @@ pub struct Envelope<M: AsBytes + FromBytes> {
|
||||
pub payload: M,
|
||||
}
|
||||
|
||||
/// Message envelope for API requests
|
||||
pub type RequestEnvelope<M> = Envelope<M>;
|
||||
/// Message envelope for API responses
|
||||
pub type ResponseEnvelope<M> = Envelope<M>;
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct PingRequestPayload {
|
||||
@@ -27,9 +33,11 @@ pub struct PingRequestPayload {
|
||||
pub echo: [u8; 256],
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type PingRequest = RequestEnvelope<PingRequestPayload>;
|
||||
|
||||
impl PingRequest {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(echo: [u8; 256]) -> Self {
|
||||
Self::from_payload(PingRequestPayload { echo })
|
||||
}
|
||||
@@ -58,6 +66,7 @@ impl Message for PingRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct PingResponsePayload {
|
||||
@@ -65,9 +74,11 @@ pub struct PingResponsePayload {
|
||||
pub echo: [u8; 256],
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type PingResponse = ResponseEnvelope<PingResponsePayload>;
|
||||
|
||||
impl PingResponse {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(echo: [u8; 256]) -> Self {
|
||||
Self::from_payload(PingResponsePayload { echo })
|
||||
}
|
||||
@@ -96,10 +107,12 @@ impl Message for PingResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct SupplyKeypairRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type SupplyKeypairRequest = RequestEnvelope<SupplyKeypairRequestPayload>;
|
||||
|
||||
impl Default for SupplyKeypairRequest {
|
||||
@@ -109,6 +122,7 @@ impl Default for SupplyKeypairRequest {
|
||||
}
|
||||
|
||||
impl SupplyKeypairRequest {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new() -> Self {
|
||||
Self::from_payload(SupplyKeypairRequestPayload {})
|
||||
}
|
||||
@@ -137,25 +151,35 @@ impl Message for SupplyKeypairRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub mod supply_keypair_response_status {
|
||||
#[allow(missing_docs)]
|
||||
pub const OK: u128 = 0;
|
||||
#[allow(missing_docs)]
|
||||
pub const KEYPAIR_ALREADY_SUPPLIED: u128 = 1;
|
||||
// TODO: This is not actually part of the API. Remove.
|
||||
/// TODO: This is not actually part of the API. Remove.
|
||||
#[allow(missing_docs)]
|
||||
pub const INTERNAL_ERROR: u128 = 2;
|
||||
#[allow(missing_docs)]
|
||||
pub const INVALID_REQUEST: u128 = 3;
|
||||
/// TODO: Deprectaed, remove
|
||||
#[allow(missing_docs)]
|
||||
pub const IO_ERROR: u128 = 4;
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct SupplyKeypairResponsePayload {
|
||||
#[allow(missing_docs)]
|
||||
pub status: u128,
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type SupplyKeypairResponse = ResponseEnvelope<SupplyKeypairResponsePayload>;
|
||||
|
||||
impl SupplyKeypairResponse {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(status: u128) -> Self {
|
||||
Self::from_payload(SupplyKeypairResponsePayload { status })
|
||||
}
|
||||
@@ -184,10 +208,12 @@ impl Message for SupplyKeypairResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct AddListenSocketRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type AddListenSocketRequest = RequestEnvelope<AddListenSocketRequestPayload>;
|
||||
|
||||
impl Default for AddListenSocketRequest {
|
||||
@@ -197,6 +223,7 @@ impl Default for AddListenSocketRequest {
|
||||
}
|
||||
|
||||
impl AddListenSocketRequest {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new() -> Self {
|
||||
Self::from_payload(AddListenSocketRequestPayload {})
|
||||
}
|
||||
@@ -225,21 +252,28 @@ impl Message for AddListenSocketRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub mod add_listen_socket_response_status {
|
||||
#[allow(missing_docs)]
|
||||
pub const OK: u128 = 0;
|
||||
#[allow(missing_docs)]
|
||||
pub const INVALID_REQUEST: u128 = 1;
|
||||
#[allow(missing_docs)]
|
||||
pub const INTERNAL_ERROR: u128 = 2;
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct AddListenSocketResponsePayload {
|
||||
pub status: u128,
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type AddListenSocketResponse = ResponseEnvelope<AddListenSocketResponsePayload>;
|
||||
|
||||
impl AddListenSocketResponse {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(status: u128) -> Self {
|
||||
Self::from_payload(AddListenSocketResponsePayload { status })
|
||||
}
|
||||
@@ -268,19 +302,23 @@ impl Message for AddListenSocketResponse {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct AddPskBrokerRequestPayload {}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type AddPskBrokerRequest = RequestEnvelope<AddPskBrokerRequestPayload>;
|
||||
|
||||
impl Default for AddPskBrokerRequest {
|
||||
#[allow(missing_docs)]
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl AddPskBrokerRequest {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new() -> Self {
|
||||
Self::from_payload(AddPskBrokerRequestPayload {})
|
||||
}
|
||||
@@ -309,21 +347,28 @@ impl Message for AddPskBrokerRequest {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub mod add_psk_broker_response_status {
|
||||
#[allow(missing_docs)]
|
||||
pub const OK: u128 = 0;
|
||||
#[allow(missing_docs)]
|
||||
pub const INVALID_REQUEST: u128 = 1;
|
||||
#[allow(missing_docs)]
|
||||
pub const INTERNAL_ERROR: u128 = 2;
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
#[repr(packed)]
|
||||
#[derive(Debug, Copy, Clone, Hash, AsBytes, FromBytes, FromZeroes, PartialEq, Eq)]
|
||||
pub struct AddPskBrokerResponsePayload {
|
||||
pub status: u128,
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub type AddPskBrokerResponse = ResponseEnvelope<AddPskBrokerResponsePayload>;
|
||||
|
||||
impl AddPskBrokerResponse {
|
||||
#[allow(missing_docs)]
|
||||
pub fn new(status: u128) -> Self {
|
||||
Self::from_payload(AddPskBrokerResponsePayload { status })
|
||||
}
|
||||
|
||||
@@ -4,24 +4,63 @@ use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
|
||||
use super::{ByteSliceRefExt, MessageAttributes, PingRequest, RequestMsgType};
|
||||
|
||||
/// Helper for producing API message request references, [RequestRef].
|
||||
///
|
||||
/// This is to [RequestRef] as [rosenpass_util::zerocopy::RefMaker] is to
|
||||
/// [zerocopy::Ref].
|
||||
struct RequestRefMaker<B> {
|
||||
buf: B,
|
||||
msg_type: RequestMsgType,
|
||||
}
|
||||
|
||||
impl<B: ByteSlice> RequestRef<B> {
|
||||
/// Produce a [RequestRef] from a raw message buffer,
|
||||
/// reading the type from the buffer
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zerocopy::AsBytes;
|
||||
///
|
||||
/// use rosenpass::api::{PingRequest, RequestRef, RequestMsgType};
|
||||
///
|
||||
/// let msg = PingRequest::new([0u8; 256]);
|
||||
///
|
||||
/// // TODO: HEISENBUG: This is necessary for some reason to make the rest of the example work
|
||||
/// let typ = msg.msg_type;
|
||||
/// assert_eq!(typ, rosenpass::api::PING_REQUEST);
|
||||
///
|
||||
/// let buf = msg.as_bytes();
|
||||
/// let msg_ref = RequestRef::parse(buf)?;
|
||||
/// assert!(matches!(msg_ref, RequestRef::Ping(_)));
|
||||
///
|
||||
/// assert_eq!(msg_ref.message_type(), RequestMsgType::Ping);
|
||||
///
|
||||
/// assert!(std::ptr::eq(buf, msg_ref.bytes()));
|
||||
///
|
||||
/// Ok::<(), anyhow::Error>(())
|
||||
/// ```
|
||||
pub fn parse(buf: B) -> anyhow::Result<Self> {
|
||||
RequestRefMaker::new(buf)?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self> {
|
||||
RequestRefMaker::new(buf)?.from_prefix()?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self> {
|
||||
RequestRefMaker::new(buf)?.from_suffix()?.parse()
|
||||
}
|
||||
|
||||
/// Get the message type [Self] contains
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [Self::parse]
|
||||
pub fn message_type(&self) -> RequestMsgType {
|
||||
match self {
|
||||
Self::Ping(_) => RequestMsgType::Ping,
|
||||
@@ -110,6 +149,7 @@ impl<B: ByteSlice> RequestRefMaker<B> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference to a API message response, typed as an enum.
|
||||
pub enum RequestRef<B> {
|
||||
Ping(Ref<B, PingRequest>),
|
||||
SupplyKeypair(Ref<B, super::SupplyKeypairRequest>),
|
||||
@@ -121,6 +161,11 @@ impl<B> RequestRef<B>
|
||||
where
|
||||
B: ByteSlice,
|
||||
{
|
||||
/// Access the byte data of this reference
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [Self::parse].
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes(),
|
||||
@@ -135,6 +180,7 @@ impl<B> RequestRef<B>
|
||||
where
|
||||
B: ByteSliceMut,
|
||||
{
|
||||
/// Access the byte data of this reference; mutably
|
||||
pub fn bytes_mut(&mut self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes_mut(),
|
||||
|
||||
@@ -6,23 +6,29 @@ use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
use super::{Message, PingRequest, PingResponse};
|
||||
use super::{RequestRef, ResponseRef, ZerocopyResponseMakerSetupMessageExt};
|
||||
|
||||
/// Extension trait for [Message]s that are requests messages
|
||||
pub trait RequestMsg: Sized + Message {
|
||||
/// The response message belonging to this request message
|
||||
type ResponseMsg: ResponseMsg;
|
||||
|
||||
/// Construct a response make for this particular message
|
||||
fn zk_response_maker<B: ByteSlice>(buf: B) -> RefMaker<B, Self::ResponseMsg> {
|
||||
buf.zk_ref_maker()
|
||||
}
|
||||
|
||||
/// Setup a response maker (through [Message::setup]) for this request message type
|
||||
fn setup_response<B: ByteSliceMut>(buf: B) -> anyhow::Result<Ref<B, Self::ResponseMsg>> {
|
||||
Self::zk_response_maker(buf).setup_msg()
|
||||
}
|
||||
|
||||
/// Setup a response maker from a buffer prefix (through [Message::setup]) for this request message type
|
||||
fn setup_response_from_prefix<B: ByteSliceMut>(
|
||||
buf: B,
|
||||
) -> anyhow::Result<Ref<B, Self::ResponseMsg>> {
|
||||
Self::zk_response_maker(buf).from_prefix()?.setup_msg()
|
||||
}
|
||||
|
||||
/// Setup a response maker from a buffer suffix (through [Message::setup]) for this request message type
|
||||
fn setup_response_from_suffix<B: ByteSliceMut>(
|
||||
buf: B,
|
||||
) -> anyhow::Result<Ref<B, Self::ResponseMsg>> {
|
||||
@@ -30,6 +36,7 @@ pub trait RequestMsg: Sized + Message {
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for [Message]s that are response messages
|
||||
pub trait ResponseMsg: Message {
|
||||
type RequestMsg: RequestMsg;
|
||||
}
|
||||
@@ -66,20 +73,25 @@ impl ResponseMsg for super::AddPskBrokerResponse {
|
||||
type RequestMsg = super::AddPskBrokerRequest;
|
||||
}
|
||||
|
||||
/// Request and response for the [crate::api::RequestMsgType::Ping] message type
|
||||
pub type PingPair<B1, B2> = (Ref<B1, PingRequest>, Ref<B2, PingResponse>);
|
||||
/// Request and response for the [crate::api::RequestMsgType::SupplyKeypair] message type
|
||||
pub type SupplyKeypairPair<B1, B2> = (
|
||||
Ref<B1, super::SupplyKeypairRequest>,
|
||||
Ref<B2, super::SupplyKeypairResponse>,
|
||||
);
|
||||
/// Request and response for the [crate::api::RequestMsgType::AddListenSocket] message type
|
||||
pub type AddListenSocketPair<B1, B2> = (
|
||||
Ref<B1, super::AddListenSocketRequest>,
|
||||
Ref<B2, super::AddListenSocketResponse>,
|
||||
);
|
||||
/// Request and response for the [crate::api::RequestMsgType::AddPskBroker] message type
|
||||
pub type AddPskBrokerPair<B1, B2> = (
|
||||
Ref<B1, super::AddPskBrokerRequest>,
|
||||
Ref<B2, super::AddPskBrokerResponse>,
|
||||
);
|
||||
|
||||
/// A pair of references to messages; request and response each.
|
||||
pub enum RequestResponsePair<B1, B2> {
|
||||
Ping(PingPair<B1, B2>),
|
||||
SupplyKeypair(SupplyKeypairPair<B1, B2>),
|
||||
@@ -116,6 +128,7 @@ where
|
||||
B1: ByteSlice,
|
||||
B2: ByteSlice,
|
||||
{
|
||||
/// Returns a tuple to both the request and the response message
|
||||
pub fn both(&self) -> (RequestRef<&[u8]>, ResponseRef<&[u8]>) {
|
||||
match self {
|
||||
Self::Ping((req, res)) => {
|
||||
@@ -141,10 +154,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the request message
|
||||
pub fn request(&self) -> RequestRef<&[u8]> {
|
||||
self.both().0
|
||||
}
|
||||
|
||||
/// Returns the response message
|
||||
pub fn response(&self) -> ResponseRef<&[u8]> {
|
||||
self.both().1
|
||||
}
|
||||
@@ -155,6 +170,7 @@ where
|
||||
B1: ByteSliceMut,
|
||||
B2: ByteSliceMut,
|
||||
{
|
||||
/// Returns a mutable tuple to both the request and the response message
|
||||
pub fn both_mut(&mut self) -> (RequestRef<&mut [u8]>, ResponseRef<&mut [u8]>) {
|
||||
match self {
|
||||
Self::Ping((req, res)) => {
|
||||
@@ -180,10 +196,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the request message, mutably
|
||||
pub fn request_mut(&mut self) -> RequestRef<&mut [u8]> {
|
||||
self.both_mut().0
|
||||
}
|
||||
|
||||
/// Returns the response message, mutably
|
||||
pub fn response_mut(&mut self) -> ResponseRef<&mut [u8]> {
|
||||
self.both_mut().1
|
||||
}
|
||||
|
||||
@@ -5,24 +5,66 @@ use zerocopy::{ByteSlice, ByteSliceMut, Ref};
|
||||
|
||||
use super::{ByteSliceRefExt, MessageAttributes, PingResponse, ResponseMsgType};
|
||||
|
||||
/// Helper for producing API message response references, [ResponseRef].
|
||||
///
|
||||
/// This is to [ResponseRef] as [rosenpass_util::zerocopy::RefMaker] is to
|
||||
/// [zerocopy::Ref].
|
||||
struct ResponseRefMaker<B> {
|
||||
/// Buffer we are referencing
|
||||
buf: B,
|
||||
/// Message type we are producing
|
||||
msg_type: ResponseMsgType,
|
||||
}
|
||||
|
||||
impl<B: ByteSlice> ResponseRef<B> {
|
||||
/// Produce a [ResponseRef] from a raw message buffer,
|
||||
/// reading the type from the buffer
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use zerocopy::AsBytes;
|
||||
///
|
||||
/// use rosenpass::api::{PingResponse, ResponseRef, ResponseMsgType};
|
||||
/// // Produce the original PingResponse
|
||||
/// let msg = PingResponse::new([0u8; 256]);
|
||||
///
|
||||
/// // TODO: HEISENBUG: This is necessary for some reason to make the rest of the example work
|
||||
/// let typ = msg.msg_type;
|
||||
/// assert_eq!(typ, rosenpass::api::PING_RESPONSE);
|
||||
///
|
||||
/// // Parse as a message type
|
||||
/// let buf = msg.as_bytes();
|
||||
/// let msg_ref = ResponseRef::parse(buf)?;
|
||||
/// assert!(matches!(msg_ref, ResponseRef::Ping(_)));
|
||||
///
|
||||
/// // Buffers and message types of course match what we expect
|
||||
/// assert_eq!(msg_ref.message_type(), ResponseMsgType::Ping);
|
||||
/// assert!(std::ptr::eq(buf, msg_ref.bytes()));
|
||||
///
|
||||
/// Ok::<(), anyhow::Error>(())
|
||||
/// ```
|
||||
pub fn parse(buf: B) -> anyhow::Result<Self> {
|
||||
ResponseRefMaker::new(buf)?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_prefix(buf: B) -> anyhow::Result<Self> {
|
||||
ResponseRefMaker::new(buf)?.from_prefix()?.parse()
|
||||
}
|
||||
|
||||
/// Produce a [ResponseRef] from the prefix of a raw message buffer,
|
||||
/// reading the type from the buffer.
|
||||
pub fn parse_from_suffix(buf: B) -> anyhow::Result<Self> {
|
||||
ResponseRefMaker::new(buf)?.from_suffix()?.parse()
|
||||
}
|
||||
|
||||
/// Get the message type [Self] contains
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [Self::parse]
|
||||
pub fn message_type(&self) -> ResponseMsgType {
|
||||
match self {
|
||||
Self::Ping(_) => ResponseMsgType::Ping,
|
||||
@@ -111,6 +153,7 @@ impl<B: ByteSlice> ResponseRefMaker<B> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference to a API message response, typed.
|
||||
pub enum ResponseRef<B> {
|
||||
Ping(Ref<B, PingResponse>),
|
||||
SupplyKeypair(Ref<B, super::SupplyKeypairResponse>),
|
||||
@@ -122,6 +165,11 @@ impl<B> ResponseRef<B>
|
||||
where
|
||||
B: ByteSlice,
|
||||
{
|
||||
/// Access the byte data of this reference
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See [Self::parse].
|
||||
pub fn bytes(&self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes(),
|
||||
@@ -136,6 +184,7 @@ impl<B> ResponseRef<B>
|
||||
where
|
||||
B: ByteSliceMut,
|
||||
{
|
||||
/// Access the byte data of this reference; mutably
|
||||
pub fn bytes_mut(&mut self) -> &[u8] {
|
||||
match self {
|
||||
Self::Ping(r) => r.bytes_mut(),
|
||||
|
||||
@@ -2,10 +2,21 @@ use super::{ByteSliceRefExt, Message, PingRequest, PingResponse, RequestRef, Req
|
||||
use std::{collections::VecDeque, os::fd::OwnedFd};
|
||||
use zerocopy::{ByteSlice, ByteSliceMut};
|
||||
|
||||
/// The rosenpass API implementation functions.
|
||||
///
|
||||
/// Implemented by [crate::api::ApiHandler].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See the example of how to use the API in [crate::api].
|
||||
pub trait Server {
|
||||
/// This implements the handler for the [crate::api::RequestMsgType::Ping] API message
|
||||
///
|
||||
/// It merely takes a buffer and returns that same buffer.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See the example of how to use the API in [crate::api].
|
||||
fn ping(
|
||||
&mut self,
|
||||
req: &PingRequest,
|
||||
@@ -54,6 +65,10 @@ pub trait Server {
|
||||
/// The file descriptors for the keys need not be backed by a file on disk. You can supply a
|
||||
/// [memfd](https://man.archlinux.org/man/memfd.2.en) or [memfd_secret](https://man.archlinux.org/man/memfd_secret.2.en)
|
||||
/// backed file descriptor if the server keys are not backed by a file system file.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See the example of how to use the API in [crate::api].
|
||||
fn supply_keypair(
|
||||
&mut self,
|
||||
req: &super::SupplyKeypairRequest,
|
||||
@@ -80,8 +95,13 @@ pub trait Server {
|
||||
///
|
||||
/// # Description
|
||||
///
|
||||
/// This endpoint allows you to supply a UDP listen socket; it will be used to perform
|
||||
/// This endpoint allows you to supply a UDP listen socket; it will be used to perform key
|
||||
/// key exchanges using the Rosenpass protocol.
|
||||
/// cryptographic key exchanges via the Rosenpass protocol.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See the example of how to use the API in [crate::api].
|
||||
fn add_listen_socket(
|
||||
&mut self,
|
||||
req: &super::AddListenSocketRequest,
|
||||
@@ -89,6 +109,31 @@ pub trait Server {
|
||||
res: &mut super::AddListenSocketResponse,
|
||||
) -> anyhow::Result<()>;
|
||||
|
||||
/// Supply a new PSK broker listen socket through file descriptor passing via the API
|
||||
///
|
||||
/// This implements the handler for the [crate::api::RequestMsgType::AddPskBroker] API message.
|
||||
///
|
||||
/// # File descriptors
|
||||
///
|
||||
/// 1. The listen socket; must be backed by a unix domain stream socket
|
||||
///
|
||||
/// # API Return Status
|
||||
///
|
||||
/// 1. [crate::api::add_psk_broker_response_status::OK] - Indicates success
|
||||
/// 2. [crate::api::add_psk_broker_response_status::INVALID_REQUEST] – Malformed request; could be:
|
||||
/// - Missing file descriptors for public key
|
||||
/// - Invalid file descriptor type
|
||||
/// 3. [crate::api::add_psk_broker_response_status::INTERNAL_ERROR] – Some other, non-fatal error
|
||||
/// occured. Check the logs on log
|
||||
///
|
||||
/// # Description
|
||||
///
|
||||
/// This endpoint allows you to supply a UDP listen socket; it will be used to transmit
|
||||
/// cryptographic keys exchanged to WireGuard.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// See the example of how to use the API in [crate::api].
|
||||
fn add_psk_broker(
|
||||
&mut self,
|
||||
req: &super::AddPskBrokerRequest,
|
||||
@@ -96,6 +141,11 @@ pub trait Server {
|
||||
res: &mut super::AddPskBrokerResponse,
|
||||
) -> anyhow::Result<()>;
|
||||
|
||||
/// Similar to [Self::handle_message], but takes a [RequestResponsePair]
|
||||
/// instead of taking to separate byte buffers.
|
||||
///
|
||||
/// I.e. this function uses the explicit type tag encoded in [RequestResponsePair]
|
||||
/// rather than reading the type tag from the request buffer.
|
||||
fn dispatch<ReqBuf, ResBuf>(
|
||||
&mut self,
|
||||
p: &mut RequestResponsePair<ReqBuf, ResBuf>,
|
||||
@@ -117,6 +167,14 @@ pub trait Server {
|
||||
}
|
||||
}
|
||||
|
||||
/// Called by [crate::api::mio::MioConnection] when a new API request was received.
|
||||
///
|
||||
/// The parameters are:
|
||||
///
|
||||
/// - `req` – A buffer containing the request
|
||||
/// - `res_fds` – A list of file descriptors received during the API call (i.e. this is used
|
||||
/// with unix socket file descriptor passing)
|
||||
/// - `res` – The buffer to store the response in.
|
||||
fn handle_message<ReqBuf, ResBuf>(
|
||||
&mut self,
|
||||
req: ReqBuf,
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::config::Rosenpass as RosenpassConfig;
|
||||
|
||||
use super::config::ApiConfig;
|
||||
|
||||
/// Additional command line arguments for the API
|
||||
#[cfg(feature = "experiment_api")]
|
||||
#[derive(Args, Debug)]
|
||||
pub struct ApiCli {
|
||||
@@ -27,10 +28,14 @@ pub struct ApiCli {
|
||||
}
|
||||
|
||||
impl ApiCli {
|
||||
/// Copy the parameters set here into the [RosenpassConfig].
|
||||
/// Forwards to [Self::apply_to_api_config]:
|
||||
pub fn apply_to_config(&self, cfg: &mut RosenpassConfig) -> anyhow::Result<()> {
|
||||
self.apply_to_api_config(&mut cfg.api)
|
||||
}
|
||||
|
||||
/// Fills the values from [ApiConfig::listen_path], [ApiConfig::listen_fd], and
|
||||
/// [ApiConfig::stream_fd] with the values from [Self]
|
||||
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);
|
||||
|
||||
@@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::app_server::AppServer;
|
||||
|
||||
/// Configuration options for the Rosenpass API
|
||||
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq)]
|
||||
pub struct ApiConfig {
|
||||
/// Where in the file-system to create the unix socket the rosenpass API will be listening for
|
||||
@@ -23,6 +24,10 @@ pub struct ApiConfig {
|
||||
}
|
||||
|
||||
impl ApiConfig {
|
||||
/// Construct appropriate [UnixListener]s for each of the API
|
||||
/// listeners and connections configured in [Self] and invoke
|
||||
/// [AppServer::add_api_listener] for each to add them to the
|
||||
/// [AppServer].
|
||||
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)?)?;
|
||||
@@ -39,10 +44,12 @@ impl ApiConfig {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sum of all the API sources configured in here
|
||||
pub fn count_api_sources(&self) -> usize {
|
||||
self.listen_path.len() + self.listen_fd.len() + self.stream_fd.len()
|
||||
}
|
||||
|
||||
/// Checks if [Self::count_api_sources] is greater than zero
|
||||
pub fn has_api_sources(&self) -> bool {
|
||||
self.count_api_sources() > 0
|
||||
}
|
||||
|
||||
@@ -53,6 +53,9 @@ struct MioConnectionBuffers {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Represents a single connection with an API client.
|
||||
/// Includes the necessary buffers, the [ApiHandler],
|
||||
/// and the [UnixStream] that is used for communication.
|
||||
pub struct MioConnection {
|
||||
io: UnixStream,
|
||||
mio_token: mio::Token,
|
||||
@@ -62,6 +65,8 @@ pub struct MioConnection {
|
||||
}
|
||||
|
||||
impl MioConnection {
|
||||
/// Construct a new [Self] for the given app server from the unix socket stream
|
||||
/// to communicate on.
|
||||
pub fn new(app_server: &mut AppServer, mut io: UnixStream) -> std::io::Result<Self> {
|
||||
let mio_token = app_server.mio_token_dispenser.dispense();
|
||||
app_server
|
||||
@@ -88,6 +93,8 @@ impl MioConnection {
|
||||
})
|
||||
}
|
||||
|
||||
/// Checks if this unix stream should be closed by the enclosing
|
||||
/// structure
|
||||
pub fn should_close(&self) -> bool {
|
||||
let exhausted = self
|
||||
.buffers
|
||||
@@ -97,22 +104,30 @@ impl MioConnection {
|
||||
self.invalid_read && exhausted
|
||||
}
|
||||
|
||||
/// Close and deregister this particular API connection
|
||||
pub fn close(mut self, app_server: &mut AppServer) -> anyhow::Result<()> {
|
||||
app_server.mio_poll.registry().deregister(&mut self.io)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Retrieve the mio token
|
||||
pub fn mio_token(&self) -> mio::Token {
|
||||
self.mio_token
|
||||
}
|
||||
}
|
||||
|
||||
/// We require references to both [MioConnection] and to the [AppServer] that contains it.
|
||||
pub trait MioConnectionContext {
|
||||
/// Reference to the [MioConnection] we are focusing on
|
||||
fn mio_connection(&self) -> &MioConnection;
|
||||
/// Reference to the [AppServer] that contains the [Self::mio_connection]
|
||||
fn app_server(&self) -> &AppServer;
|
||||
/// Mutable reference to the [MioConnection] we are focusing on
|
||||
fn mio_connection_mut(&mut self) -> &mut MioConnection;
|
||||
/// Mutable reference to the [AppServer] that contains the [Self::mio_connection]
|
||||
fn app_server_mut(&mut self) -> &mut AppServer;
|
||||
|
||||
/// Called by [AppServer::poll] regularly to process any incoming (and outgoing) API messages
|
||||
fn poll(&mut self) -> anyhow::Result<()> {
|
||||
macro_rules! short {
|
||||
($e:expr) => {
|
||||
@@ -133,6 +148,7 @@ pub trait MioConnectionContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Called by [Self::poll] to process incoming messages
|
||||
fn handle_incoming_message(&mut self) -> anyhow::Result<Option<()>> {
|
||||
self.with_buffers_stolen(|this, bufs| {
|
||||
// Acquire request & response. Caller is responsible to make sure
|
||||
@@ -156,6 +172,7 @@ pub trait MioConnectionContext {
|
||||
})
|
||||
}
|
||||
|
||||
/// Called by [Self::poll] to write data in the send buffer to the unix stream
|
||||
fn flush_write_buffer(&mut self) -> anyhow::Result<Option<()>> {
|
||||
if self.write_buf_mut().exhausted() {
|
||||
return Ok(Some(()));
|
||||
@@ -194,6 +211,7 @@ pub trait MioConnectionContext {
|
||||
}
|
||||
}
|
||||
|
||||
/// Called by [Self::poll] to check for messages to receive
|
||||
fn recv(&mut self) -> anyhow::Result<Option<()>> {
|
||||
if !self.write_buf_mut().exhausted() || self.mio_connection().invalid_read {
|
||||
return Ok(None);
|
||||
@@ -257,10 +275,12 @@ pub trait MioConnectionContext {
|
||||
}
|
||||
}
|
||||
|
||||
/// Forwards to [MioConnection::mio_token]
|
||||
fn mio_token(&self) -> mio::Token {
|
||||
self.mio_connection().mio_token()
|
||||
}
|
||||
|
||||
/// Forwards to [MioConnection::should_close]
|
||||
fn should_close(&self) -> bool {
|
||||
self.mio_connection().should_close()
|
||||
}
|
||||
@@ -299,6 +319,7 @@ trait MioConnectionContextPrivate: MioConnectionContext {
|
||||
|
||||
impl<T> MioConnectionContextPrivate for T where T: ?Sized + MioConnectionContext {}
|
||||
|
||||
/// Every [MioConnectionContext] is also a [ApiHandlerContext]
|
||||
impl<T> ApiHandlerContext for T
|
||||
where
|
||||
T: ?Sized + MioConnectionContext,
|
||||
|
||||
@@ -10,41 +10,59 @@ use crate::app_server::{AppServer, AppServerIoSource};
|
||||
|
||||
use super::{MioConnection, MioConnectionContext};
|
||||
|
||||
/// This is in essence a unix listener for API connections.
|
||||
///
|
||||
/// It contains a number of [UnixListener]s and the associated [MioConnection]s encapsulating [mio::net::UnixListener]s.
|
||||
#[derive(Default, Debug)]
|
||||
pub struct MioManager {
|
||||
listeners: Vec<UnixListener>,
|
||||
connections: Vec<Option<MioConnection>>,
|
||||
}
|
||||
|
||||
/// Points at a particular source of IO events inside [MioManager]
|
||||
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
|
||||
pub enum MioManagerIoSource {
|
||||
// Source of IO events is the Nth unix socket listener (see [MioManager::listeners])
|
||||
Listener(usize),
|
||||
// Source of IO events is the Nth unix socket listener (see [MioManager::connections])
|
||||
Connection(usize),
|
||||
}
|
||||
|
||||
impl MioManager {
|
||||
/// Construct an empty [Self]
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Focus in on a particular [MioConnection] inside a [MioManager]
|
||||
///
|
||||
/// This is mainly used to implement [MioConnectionContext].
|
||||
struct MioConnectionFocus<'a, T: ?Sized + MioManagerContext> {
|
||||
/// [MioConnectionContext] to access the [MioManager] instance and [AppServer]
|
||||
ctx: &'a mut T,
|
||||
/// Index of the connection referenced to by [Self]
|
||||
conn_idx: usize,
|
||||
}
|
||||
|
||||
impl<'a, T: ?Sized + MioManagerContext> MioConnectionFocus<'a, T> {
|
||||
/// Produce a MioConnectionContext from the [MioConnectionContext] and the connection index
|
||||
fn new(ctx: &'a mut T, conn_idx: usize) -> Self {
|
||||
Self { ctx, conn_idx }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait MioManagerContext {
|
||||
/// Reference to the [MioManager]
|
||||
fn mio_manager(&self) -> &MioManager;
|
||||
/// Reference to the [MioManager], mutably
|
||||
fn mio_manager_mut(&mut self) -> &mut MioManager;
|
||||
/// Reference to the [AppServer] this [MioManager] is associated with
|
||||
fn app_server(&self) -> &AppServer;
|
||||
/// Mutable reference to the [AppServer] this [MioManager] is associated with
|
||||
fn app_server_mut(&mut self) -> &mut AppServer;
|
||||
|
||||
/// Add a new [UnixListener] to listen for API connections on
|
||||
fn add_listener(&mut self, mut listener: UnixListener) -> io::Result<()> {
|
||||
let srv = self.app_server_mut();
|
||||
let mio_token = srv.mio_token_dispenser.dispense();
|
||||
@@ -64,6 +82,7 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Add a new connection to an API client
|
||||
fn add_connection(&mut self, connection: UnixStream) -> io::Result<()> {
|
||||
let connection = MioConnection::new(self.app_server_mut(), connection)?;
|
||||
let mio_token = connection.mio_token();
|
||||
@@ -84,6 +103,7 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Poll a particular [MioManagerIoSource] in this [MioManager]
|
||||
fn poll_particular(&mut self, io_source: MioManagerIoSource) -> anyhow::Result<()> {
|
||||
use MioManagerIoSource as S;
|
||||
match io_source {
|
||||
@@ -93,12 +113,14 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check for new connections and poll all the [MioConnectionContext]s managed by [Self]
|
||||
fn poll(&mut self) -> anyhow::Result<()> {
|
||||
self.accept_connections()?;
|
||||
self.poll_connections()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check all the [UnixListener]s managed by this [MioManager] for new connections
|
||||
fn accept_connections(&mut self) -> io::Result<()> {
|
||||
for idx in 0..self.mio_manager_mut().listeners.len() {
|
||||
self.accept_from(idx)?;
|
||||
@@ -106,6 +128,7 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Check a particular [UnixListener] managed by this for new connections.
|
||||
fn accept_from(&mut self, idx: usize) -> io::Result<()> {
|
||||
// Accept connection until the socket would block or returns another error
|
||||
// TODO: This currently only adds connections--we eventually need the ability to remove
|
||||
@@ -122,6 +145,7 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Call [MioConnectionContext::poll] on all the [MioConnection]s in This
|
||||
fn poll_connections(&mut self) -> anyhow::Result<()> {
|
||||
for idx in 0..self.mio_manager().connections.len() {
|
||||
self.poll_particular_connection(idx)?;
|
||||
@@ -129,6 +153,7 @@ pub trait MioManagerContext {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Call [MioConnectionContext::poll] on a particular connection
|
||||
fn poll_particular_connection(&mut self, idx: usize) -> anyhow::Result<()> {
|
||||
if self.mio_manager().connections[idx].is_none() {
|
||||
return Ok(());
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
//! The bulk code relating to the Rosenpass unix socket API
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
#![doc = "```ignore"]
|
||||
#![doc = include_str!("../../tests/api-integration-tests-api-setup.rs")]
|
||||
#![doc = "```"]
|
||||
|
||||
mod api_handler;
|
||||
mod boilerplate;
|
||||
|
||||
@@ -8,29 +8,99 @@ use thiserror::Error;
|
||||
use super::{CryptoServer, PeerPtr, SPk, SSk, SymKey};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
/// A pair of matching public/secret keys used to launch the crypto server.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Decomposing a key pair into its individual components, then recreating it:
|
||||
///
|
||||
/// ```rust
|
||||
/// use rosenpass::protocol::Keypair;
|
||||
///
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// let random_pair = Keypair::random();
|
||||
/// let random_copy = random_pair.clone();
|
||||
/// let (sk_copy, pk_copy) = random_copy.into_parts();
|
||||
///
|
||||
/// // Re-assemble the key pair from the original secret/public key
|
||||
/// // Note that it doesn't have to be the exact same keys;
|
||||
/// // you could just as easily use a completely different pair here
|
||||
/// let reconstructed_pair = Keypair::from_parts((sk_copy, pk_copy));
|
||||
///
|
||||
/// assert_eq!(random_pair.sk.secret(), reconstructed_pair.sk.secret());
|
||||
/// assert_eq!(random_pair.pk, reconstructed_pair.pk);
|
||||
/// ```
|
||||
pub struct Keypair {
|
||||
/// Secret key matching the crypto server's public key.
|
||||
pub sk: SSk,
|
||||
/// Public key identifying the crypto server instance.
|
||||
pub pk: SPk,
|
||||
}
|
||||
|
||||
// TODO: We need a named tuple derive
|
||||
impl Keypair {
|
||||
/// Creates a new key pair from the given secret/public key components.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use rosenpass::protocol::{Keypair, SSk, SPk};
|
||||
///
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// let random_sk = SSk::random();
|
||||
/// let random_pk = SPk::random();
|
||||
/// let random_pair = Keypair::new(random_sk.clone(), random_pk.clone());
|
||||
///
|
||||
/// assert_eq!(random_sk.secret(), random_pair.sk.secret());
|
||||
/// assert_eq!(random_pk, random_pair.pk);
|
||||
/// ```
|
||||
pub fn new(sk: SSk, pk: SPk) -> Self {
|
||||
Self { sk, pk }
|
||||
}
|
||||
|
||||
/// Creates a new "empty" key pair. All bytes are initialized to zero.
|
||||
///
|
||||
/// See [SSk:zero()][crate::protocol::SSk::zero] and [SPk:zero()][crate::protocol::SPk::zero], respectively.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use rosenpass::protocol::{Keypair, SSk, SPk};
|
||||
///
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// let zero_sk = SSk::zero();
|
||||
/// let zero_pk = SPk::zero();
|
||||
/// let zero_pair = Keypair::zero();
|
||||
///
|
||||
/// assert_eq!(zero_sk.secret(), zero_pair.sk.secret());
|
||||
/// assert_eq!(zero_pk, zero_pair.pk);
|
||||
/// ```
|
||||
pub fn zero() -> Self {
|
||||
Self::new(SSk::zero(), SPk::zero())
|
||||
}
|
||||
|
||||
/// Creates a new (securely-)random key pair. The mechanism is described in [rosenpass_secret_memory::Secret].
|
||||
///
|
||||
/// See [SSk:random()][crate::protocol::SSk::random] and [SPk:random()][crate::protocol::SPk::random], respectively.
|
||||
pub fn random() -> Self {
|
||||
Self::new(SSk::random(), SPk::random())
|
||||
}
|
||||
|
||||
/// Creates a new key pair from the given public/secret key components.
|
||||
pub fn from_parts(parts: (SSk, SPk)) -> Self {
|
||||
Self::new(parts.0, parts.1)
|
||||
}
|
||||
|
||||
/// Deconstructs the key pair, yielding the individual public/secret key components.
|
||||
pub fn into_parts(self) -> (SSk, SPk) {
|
||||
(self.sk, self.pk)
|
||||
}
|
||||
@@ -38,25 +108,77 @@ impl Keypair {
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("PSK already set in BuildCryptoServer")]
|
||||
/// Error indicating that the PSK is already set.
|
||||
/// Unused in the current version of the protocol.
|
||||
pub struct PskAlreadySet;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Keypair already set in BuildCryptoServer")]
|
||||
/// Error type indicating that the public/secret key pair has already been set.
|
||||
pub struct KeypairAlreadySet;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
#[error("Can not construct CryptoServer: Missing keypair")]
|
||||
/// Error type indicating that no public/secret key pair has been provided.
|
||||
pub struct MissingKeypair;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
/// Builder for setting up a [CryptoServer] (with deferred initialization).
|
||||
///
|
||||
/// There are multiple ways of creating a crypto server:
|
||||
///
|
||||
/// 1. Provide the key pair at initialization time (using [CryptoServer::new][crate::protocol::CryptoServer::new])
|
||||
/// 2. Provide the key pair at a later time (using [BuildCryptoServer::empty])
|
||||
///
|
||||
/// With BuildCryptoServer, you can gradually configure parameters as they become available.
|
||||
/// This may be useful when they depend on runtime conditions or have to be fetched asynchronously.
|
||||
/// It's possible to use the builder multiple times; it then serves as a "blueprint" for new
|
||||
/// instances, several of which may be spawned with the same base configuration (or variations thereof).
|
||||
///
|
||||
/// Note that the server won't actually launch without a key pair (expect a [MissingKeypair] error).
|
||||
/// The setup will be much simplified if one is provided, at the cost of some flexibility.
|
||||
/// It's however possible to defer this step in case your application requires it.
|
||||
///
|
||||
/// For additional details or examples, see [AppServer::crypto_site][crate::app_server::AppServer::crypto_site] and [ConstructionSite][rosenpass_util::build::ConstructionSite].
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use rosenpass_util::build::Build;
|
||||
/// use rosenpass::protocol::{BuildCryptoServer, Keypair, PeerParams, SPk, SymKey};
|
||||
///
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// let keypair = Keypair::random();
|
||||
/// let peer1 = PeerParams { psk: Some(SymKey::random()), pk: SPk::random() };
|
||||
/// let peer2 = PeerParams { psk: None, pk: SPk::random() };
|
||||
///
|
||||
/// let mut builder = BuildCryptoServer::new(Some(keypair.clone()), vec![peer1]);
|
||||
/// builder.add_peer(peer2.psk.clone(), peer2.pk);
|
||||
///
|
||||
/// let server = builder.build().expect("build failed");
|
||||
/// assert_eq!(server.peers.len(), 2);
|
||||
/// assert_eq!(server.sskm.secret(), keypair.sk.secret());
|
||||
/// assert_eq!(server.spkm, keypair.pk);
|
||||
/// ```
|
||||
pub struct BuildCryptoServer {
|
||||
/// The key pair (secret/public key) identifying the crypto server instance.
|
||||
pub keypair: Option<Keypair>,
|
||||
/// A list of network peers that should be registered when launching the server.
|
||||
pub peers: Vec<PeerParams>,
|
||||
}
|
||||
|
||||
impl Build<CryptoServer> for BuildCryptoServer {
|
||||
type Error = anyhow::Error;
|
||||
|
||||
/// Creates a crypto server, adding all peers that have previously been registered.
|
||||
///
|
||||
/// You must provide a key pair at the time of instantiation.
|
||||
/// If the list of peers is outdated, building the server will fail.
|
||||
///
|
||||
/// In this case, make sure to remove or re-add any peers that may have changed.
|
||||
fn build(self) -> Result<CryptoServer, Self::Error> {
|
||||
let Some(Keypair { sk, pk }) = self.keypair else {
|
||||
return Err(MissingKeypair)?;
|
||||
@@ -74,20 +196,32 @@ impl Build<CryptoServer> for BuildCryptoServer {
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
/// Cryptographic key(s) identifying the connected [peer][crate::protocol::Peer] ("client")
|
||||
/// for a given session that is being managed by the crypto server.
|
||||
///
|
||||
/// Each peer must be identified by a [public key (SPk)][crate::protocol::SPk].
|
||||
/// Optionally, a [symmetric key (SymKey)][crate::protocol::SymKey]
|
||||
/// can be provided when setting up the connection.
|
||||
/// For more information on the intended usage and security considerations, see [Peer::psk][crate::protocol::Peer::psk] and [Peer::spkt][crate::protocol::Peer::spkt].
|
||||
pub struct PeerParams {
|
||||
/// Pre-shared (symmetric) encryption keys that should be used with this peer.
|
||||
pub psk: Option<SymKey>,
|
||||
/// Public key identifying the peer.
|
||||
pub pk: SPk,
|
||||
}
|
||||
|
||||
impl BuildCryptoServer {
|
||||
/// Creates a new builder instance using the given key pair and peer list.
|
||||
pub fn new(keypair: Option<Keypair>, peers: Vec<PeerParams>) -> Self {
|
||||
Self { keypair, peers }
|
||||
}
|
||||
|
||||
/// Creates an "incomplete" builder instance, without assigning a key pair.
|
||||
pub fn empty() -> Self {
|
||||
Self::new(None, Vec::new())
|
||||
}
|
||||
|
||||
/// Creates a builder instance from the given key pair and peer list components.
|
||||
pub fn from_parts(parts: (Option<Keypair>, Vec<PeerParams>)) -> Self {
|
||||
Self {
|
||||
keypair: parts.0,
|
||||
@@ -95,32 +229,165 @@ impl BuildCryptoServer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Deconstructs the current builder instance, taking ownership of its key pair and peer list.
|
||||
///
|
||||
/// Replaces all parameters with their default values, which allows extracting them
|
||||
/// while leaving the builder in a reusable state.
|
||||
pub fn take_parts(&mut self) -> (Option<Keypair>, Vec<PeerParams>) {
|
||||
(self.keypair.take(), self.peers.swap_with_default())
|
||||
}
|
||||
|
||||
/// Deconstructs the builder instance, yielding the assigned key pair and peer list.
|
||||
pub fn into_parts(mut self) -> (Option<Keypair>, Vec<PeerParams>) {
|
||||
self.take_parts()
|
||||
}
|
||||
|
||||
/// Creates a new builder instance, assigning the given keypair to it.
|
||||
///
|
||||
/// Note that only one key pair can be assigned (expect [KeypairAlreadySet] on failure).
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## Adding key pairs to an existing builder
|
||||
///
|
||||
/// ```rust
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// use rosenpass_util::build::Build;
|
||||
/// use rosenpass::protocol::{BuildCryptoServer, Keypair};
|
||||
///
|
||||
/// // Deferred initialization: Create builder first, add the key pair later
|
||||
/// let mut builder = BuildCryptoServer::empty();
|
||||
/// // Do something with the builder ...
|
||||
///
|
||||
/// // Quite some time may have passed (network/disk IO, runtime events, ...)
|
||||
/// // Now we've got a key pair that should be added to the configuration
|
||||
/// let keypair = Keypair::random();
|
||||
/// builder.with_keypair(keypair.clone()).expect("build with key pair failed");
|
||||
///
|
||||
/// // New server instances can now make use of the assigned key pair
|
||||
/// let server = builder.build().expect("build failed");
|
||||
/// assert_eq!(server.sskm.secret(), keypair.sk.secret());
|
||||
/// assert_eq!(server.spkm, keypair.pk);
|
||||
/// ```
|
||||
///
|
||||
/// ## Basic error handling: Re-assigning key pairs
|
||||
///
|
||||
/// ```rust
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// use rosenpass_util::build::Build;
|
||||
/// use rosenpass::protocol::{BuildCryptoServer, Keypair, KeypairAlreadySet};
|
||||
///
|
||||
/// // In this case, we'll create a functional builder from its various components
|
||||
/// // These could be salvaged from another builder, or obtained from disk/network (etc.)
|
||||
/// let keypair = Keypair::random();
|
||||
/// let mut builder = BuildCryptoServer::from_parts((Some(keypair.clone()), Vec::new()));
|
||||
///
|
||||
/// // The builder has already been assigned a key pair, so this won't work
|
||||
/// let err = builder.with_keypair(keypair).expect_err("should fail to reassign key pair");
|
||||
/// assert!(matches!(err, KeypairAlreadySet));
|
||||
/// ```
|
||||
pub fn with_keypair(&mut self, keypair: Keypair) -> Result<&mut Self, KeypairAlreadySet> {
|
||||
ensure_or(self.keypair.is_none(), KeypairAlreadySet)?;
|
||||
self.keypair.insert(keypair).discard_result();
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Creates a new builder instance, adding a new entry to the list of registered peers.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Adding peers to an existing builder:
|
||||
///
|
||||
/// ```rust
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// use rosenpass_util::build::Build;
|
||||
/// use rosenpass::protocol::{BuildCryptoServer, Keypair, SymKey, SPk};
|
||||
///
|
||||
/// // Deferred initialization: Create builder first, add some peers later
|
||||
/// let keypair_option = Some(Keypair::random());
|
||||
/// let mut builder = BuildCryptoServer::new(keypair_option, Vec::new());
|
||||
/// assert!(builder.peers.is_empty());
|
||||
///
|
||||
/// // Do something with the builder ...
|
||||
///
|
||||
/// // Quite some time may have passed (network/disk IO, runtime events, ...)
|
||||
/// // Now we've found a peer that should be added to the configuration
|
||||
/// let pre_shared_key = SymKey::random();
|
||||
/// let public_key = SPk::random();
|
||||
/// builder.with_added_peer(Some(pre_shared_key.clone()), public_key.clone());
|
||||
///
|
||||
/// // New server instances will then start with the peer being registered already
|
||||
/// let server = builder.build().expect("build failed");
|
||||
/// assert_eq!(server.peers.len(), 1);
|
||||
/// let peer = &server.peers[0];
|
||||
/// let peer_psk = Some(peer.psk.clone()).expect("PSK is None");
|
||||
/// assert_eq!(peer.spkt, public_key);
|
||||
/// assert_eq!(peer_psk.secret(), pre_shared_key.secret());
|
||||
/// ```
|
||||
pub fn with_added_peer(&mut self, psk: Option<SymKey>, pk: SPk) -> &mut Self {
|
||||
// TODO: Check here already whether peer was already added
|
||||
self.peers.push(PeerParams { psk, pk });
|
||||
self
|
||||
}
|
||||
|
||||
/// Add a new entry to the list of registered peers, with or without a pre-shared key.
|
||||
pub fn add_peer(&mut self, psk: Option<SymKey>, pk: SPk) -> PeerPtr {
|
||||
let id = PeerPtr(self.peers.len());
|
||||
self.with_added_peer(psk, pk);
|
||||
id
|
||||
}
|
||||
|
||||
/// Creates a new builder, taking ownership of another instance's key pair and peer list.
|
||||
/// Allows duplicating the current set of launch parameters, which can then be used to
|
||||
/// start multiple servers with the exact same configuration (or variants using it as a base).
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// Extracting the server configuration from a builder:
|
||||
///
|
||||
/// ```rust
|
||||
/// // We have to define the security policy before using Secrets.
|
||||
/// use rosenpass_secret_memory::secret_policy_use_only_malloc_secrets;
|
||||
/// secret_policy_use_only_malloc_secrets();
|
||||
///
|
||||
/// use rosenpass_util::build::Build;
|
||||
/// use rosenpass::protocol::{BuildCryptoServer, Keypair, SymKey, SPk};
|
||||
///
|
||||
/// let keypair = Keypair::random();
|
||||
/// let peer_pk = SPk::random();
|
||||
/// let mut builder = BuildCryptoServer::new(Some(keypair.clone()), vec![]);
|
||||
/// builder.add_peer(None, peer_pk);
|
||||
///
|
||||
/// // Extract configuration parameters from the decomissioned builder
|
||||
/// let (keypair_option, peers) = builder.take_parts();
|
||||
/// let extracted_keypair = keypair_option.unwrap();
|
||||
/// assert_eq!(extracted_keypair.sk.secret(), keypair.sk.secret());
|
||||
/// assert_eq!(extracted_keypair.pk, keypair.pk);
|
||||
/// assert_eq!(peers.len(), 1);
|
||||
///
|
||||
/// // Now we can create a new builder with the same configuration
|
||||
/// let parts = (Some(extracted_keypair), peers);
|
||||
/// let mut reassembled_builder = BuildCryptoServer::from_parts(parts);
|
||||
/// let new_builder = reassembled_builder.emancipate();
|
||||
///
|
||||
/// // Do something with the new builder ...
|
||||
///
|
||||
/// // ... and now, deconstruct this one as well - still using the same parts
|
||||
/// let (keypair_option, peers) = new_builder.into_parts();
|
||||
/// let extracted_keypair = keypair_option.unwrap();
|
||||
/// assert_eq!(extracted_keypair.sk.secret(), keypair.sk.secret());
|
||||
/// assert_eq!(extracted_keypair.pk, keypair.pk);
|
||||
/// assert_eq!(peers.len(), 1);
|
||||
/// ```
|
||||
pub fn emancipate(&mut self) -> Self {
|
||||
Self::from_parts(self.take_parts())
|
||||
}
|
||||
|
||||
@@ -22,8 +22,17 @@ struct KillChild(std::process::Child);
|
||||
|
||||
impl Drop for KillChild {
|
||||
fn drop(&mut self) {
|
||||
self.0.kill().discard_result();
|
||||
self.0.wait().discard_result()
|
||||
use rustix::process::{kill_process, Pid, Signal::Term};
|
||||
let pid = Pid::from_child(&self.0);
|
||||
// We seriously need to start handling signals with signalfd, our current signal handling
|
||||
// system is a bit broken; there is probably a few functions that just restart on EINTR
|
||||
// so the signal is absorbed
|
||||
loop {
|
||||
kill_process(pid, Term).discard_result();
|
||||
if self.0.try_wait().unwrap().is_some() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
rosenpass/tests/gen-ipc-msg-types.rs
Normal file
15
rosenpass/tests/gen-ipc-msg-types.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use std::{borrow::Borrow, process::Command};
|
||||
|
||||
#[test]
|
||||
fn test_gen_ipc_msg_types() -> anyhow::Result<()> {
|
||||
let out = Command::new(env!("CARGO_BIN_EXE_rosenpass-gen-ipc-msg-types")).output()?;
|
||||
assert!(out.status.success());
|
||||
|
||||
let stdout = String::from_utf8(out.stdout)?;
|
||||
|
||||
// Smoke tests only
|
||||
assert!(stdout.contains("type RawMsgType = u128;"));
|
||||
assert!(stdout.contains("const SUPPLY_KEYPAIR_RESPONSE : RawMsgType = RawMsgType::from_le_bytes(hex!(\"f2dc 49bd e261 5f10 40b7 3c16 ec61 edb9\"));"));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user