From 48fedf4d23e332e4990987fc43f0be77bfb86ade Mon Sep 17 00:00:00 2001 From: Max Niederman Date: Sat, 10 Feb 2024 00:55:27 -0800 Subject: [PATCH] feat: simplify control protocol --- Cargo.lock | 166 +++++++++++++----------- packages/centipede_control/Cargo.toml | 5 + packages/centipede_control/src/lib.rs | 68 ++++++++++ packages/centipede_proto/src/control.rs | 146 ++++++--------------- 4 files changed, 201 insertions(+), 184 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac592a3..4e6a680 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,13 @@ dependencies = [ [[package]] name = "centipede_control" version = "0.1.0" +dependencies = [ + "centipede_proto", + "centipede_router", + "chacha20poly1305", + "ed25519-dalek", + "x25519-dalek", +] [[package]] name = "centipede_proto" @@ -169,15 +176,15 @@ dependencies = [ [[package]] name = "chrono" -version = "0.4.31" +version = "0.4.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" +checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb" dependencies = [ "android-tzdata", "iana-time-zone", "num-traits", "serde", - "windows-targets 0.48.5", + "windows-targets 0.52.0", ] [[package]] @@ -202,9 +209,9 @@ dependencies = [ [[package]] name = "const-oid" -version = "0.9.5" +version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28c122c3980598d243d63d9a704629a2d748d101f278052ff068be5a4423ab6f" +checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" [[package]] name = "core-foundation-sys" @@ -214,9 +221,9 @@ checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" [[package]] name = "cpufeatures" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" dependencies = [ "libc", ] @@ -234,9 +241,9 @@ dependencies = [ [[package]] name = "curve25519-dalek" -version = "4.1.1" +version = "4.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e89b8c6a2e4b1f45971ad09761aafb85514a84744b67a95e32c3cc1352d1f65c" +checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" dependencies = [ "cfg-if", "cpufeatures", @@ -263,9 +270,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.3" +version = "0.20.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0209d94da627ab5605dcccf08bb18afa5009cfbef48d8a8b7d7bdbc79be25c5e" +checksum = "fc5d6b04b3fd0ba9926f945895de7d806260a2d7431ba82e7edaecb043c4c6b8" dependencies = [ "darling_core", "darling_macro", @@ -273,9 +280,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.3" +version = "0.20.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "177e3443818124b357d8e76f53be906d60937f0d3a90773a664fa63fa253e621" +checksum = "04e48a959bcd5c761246f5d090ebc2fbf7b9cd527a492b07a67510c108f1e7e3" dependencies = [ "fnv", "ident_case", @@ -287,9 +294,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.3" +version = "0.20.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" +checksum = "1d1545d67a2149e1d93b7e5c7752dce5a7426eb5d1357ddcfd89336b94444f77" dependencies = [ "darling_core", "quote", @@ -339,9 +346,9 @@ dependencies = [ [[package]] name = "ed25519-dalek" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f628eaec48bfd21b865dc2950cfa014450c01d2fa2b69a86c2fd5844ec523c0" +checksum = "4a3daa8e81a3963a60642bcc1f90a670680bd4a77535faa384e9d1c79d620871" dependencies = [ "curve25519-dalek", "ed25519", @@ -359,9 +366,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "fiat-crypto" -version = "0.2.5" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27573eac26f4dd11e2b1916c3fe1baa56407c83c71a773a8ba17ec0bca03b6b7" +checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382" [[package]] name = "fnv" @@ -381,9 +388,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.11" +version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f" +checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ "cfg-if", "libc", @@ -404,9 +411,9 @@ checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "hermit-abi" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" +checksum = "d0c62115964e08cb8039170eb33c1d0e2388a256930279edca206fff675f82c3" [[package]] name = "hex" @@ -427,9 +434,9 @@ dependencies = [ [[package]] name = "iana-time-zone" -version = "0.1.59" +version = "0.1.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" dependencies = [ "android_system_properties", "core-foundation-sys", @@ -467,9 +474,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.1.0" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520" dependencies = [ "equivalent", "hashbrown 0.14.3", @@ -499,18 +506,18 @@ checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" [[package]] name = "js-sys" -version = "0.3.67" +version = "0.3.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a1d36f1235bc969acba30b7f5990b864423a6068a10f7c90ae8f0112e3a59d1" +checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" -version = "0.2.150" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "log" @@ -536,11 +543,17 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", ] @@ -579,9 +592,9 @@ dependencies = [ [[package]] name = "platforms" -version = "3.2.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14e6ab3f592e6fb464fc9712d8d6e6912de6473954635fd76a589d832cffcbb0" +checksum = "626dec3cac7cc0e1577a2ec3fc496277ec2baa084bebad95bb6fdbfae235f84c" [[package]] name = "poly1305" @@ -602,9 +615,9 @@ checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -644,24 +657,24 @@ checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" [[package]] name = "semver" -version = "1.0.20" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +checksum = "b97ed7a9823b74f99c7742f5336af7be5ecd3eeafcb1507d1fa93347b1d589b0" [[package]] name = "serde" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" dependencies = [ "proc-macro2", "quote", @@ -670,9 +683,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.113" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79" dependencies = [ "itoa", "ryu", @@ -690,16 +703,17 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.5.0" +version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f58c3a1b3e418f61c25b2aeb43fc6c95eaa252b8cecdda67f401943e9e08d33f" +checksum = "15d167997bd841ec232f5b2b8e0e26606df2e7caa4c31b95ea9ca52b200bd270" dependencies = [ "base64", "chrono", "hex", "indexmap 1.9.3", - "indexmap 2.1.0", + "indexmap 2.2.2", "serde", + "serde_derive", "serde_json", "serde_with_macros", "time", @@ -707,9 +721,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.5.0" +version = "3.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2068b437a31fc68f25dd7edc296b078f04b45145c199d8eed9866e45f1ff274" +checksum = "865f9743393e638991566a8b7a479043c2c8da94a33e0a31f18214c9cae0a64d" dependencies = [ "darling", "proc-macro2", @@ -749,9 +763,9 @@ dependencies = [ [[package]] name = "spki" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d1e996ef02c474957d681f1b05213dfb0abab947b446a62d37770b23500184a" +checksum = "d91ed6c858b01f942cd56b37a94b3e0a1798290327d1236e4d9cf4eaca44d29d" dependencies = [ "base64ct", "der", @@ -802,12 +816,13 @@ dependencies = [ [[package]] name = "time" -version = "0.3.31" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f657ba42c3f86e7680e53c8cd3af8abbe56b5491790b46e22e19c0d57463583e" +checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749" dependencies = [ "deranged", "itoa", + "num-conv", "powerfmt", "serde", "time-core", @@ -822,18 +837,19 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26197e33420244aeb70c3e8c78376ca46571bc4e701e4791c2cd9f57dcb3a43f" +checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774" dependencies = [ + "num-conv", "time-core", ] [[package]] name = "toml" -version = "0.8.8" +version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1a195ec8c9da26928f773888e0742ca3ca1040c6cd859c919c9f59c1954ab35" +checksum = "9a9aad4a3066010876e8dcf5a8a06e70a558751117a145c6ce2b82c2e2054290" dependencies = [ "serde", "serde_spanned", @@ -852,11 +868,11 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.21.0" +version = "0.22.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" +checksum = "0c9ffdf896f8daaabf9b66ba8e77ea1ed5ed0f72821b398aba62352e95062951" dependencies = [ - "indexmap 2.1.0", + "indexmap 2.2.2", "serde", "serde_spanned", "toml_datetime", @@ -899,9 +915,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -909,9 +925,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ "bumpalo", "log", @@ -924,9 +940,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -934,9 +950,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", @@ -947,9 +963,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" [[package]] name = "windows-core" @@ -1085,18 +1101,18 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.34" +version = "0.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7cf47b659b318dccbd69cc4797a39ae128f533dce7902a1096044d1967b9c16" +checksum = "5389a154b01683d28c77f8f68f49dea75f0a4da32557a58f68ee51ebba472d29" dependencies = [ "memchr", ] [[package]] name = "x25519-dalek" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb66477291e7e8d2b0ff1bcb900bf29489a9692816d79874bea351e7a8b6de96" +checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" dependencies = [ "curve25519-dalek", "rand_core", diff --git a/packages/centipede_control/Cargo.toml b/packages/centipede_control/Cargo.toml index 102159f..762db29 100644 --- a/packages/centipede_control/Cargo.toml +++ b/packages/centipede_control/Cargo.toml @@ -6,3 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +centipede_proto = { version = "0.1.0", path = "../centipede_proto" } +centipede_router = { version = "0.1.0", path = "../centipede_router" } +chacha20poly1305 = "0.10.1" +ed25519-dalek = "2.1.0" +x25519-dalek = "2.0.0" diff --git a/packages/centipede_control/src/lib.rs b/packages/centipede_control/src/lib.rs index 8b13789..45dcbc5 100644 --- a/packages/centipede_control/src/lib.rs +++ b/packages/centipede_control/src/lib.rs @@ -1 +1,69 @@ +use std::{ + alloc::System, + collections::{BTreeMap, HashMap}, + net::SocketAddr, + task::Poll, + time::SystemTime, +}; +use centipede_proto::{control::Message, marker::auth}; +use chacha20poly1305::ChaCha20Poly1305; + +/// A Centipede control daemon. +/// +/// Implemented as a state machine to be driven by an external event loop. +pub struct Controller { + /// Peer state, by public key. + peers: HashMap, + + /// Actions to be taken at some point in the future. + timers: BTreeMap, +} + +/// The state of the controller w.r.t. a peer. +enum PeerState {} + +/// An action to be taken when a timer expires. +enum TimerAction {} + +impl Controller { + /// Create a new, empty controller. + pub fn new(now: SystemTime) -> Self { + todo!() + } + + /// Register a new peer and start listening for incoming connections. + pub fn listen(&mut self, now: SystemTime, public_key: ed25519_dalek::VerifyingKey) { + todo!() + } + + /// Initiate a connection to a peer. Must be called after `listen`. + pub fn initiate( + &mut self, + now: SystemTime, + public_key: ed25519_dalek::VerifyingKey, + known_addrs: Vec, + ) { + todo!() + } + + /// Disconnect from a peer. + pub fn disconnect(&mut self, now: SystemTime, public_key: ed25519_dalek::VerifyingKey) { + todo!() + } + + /// Handle an incoming message, transitioning the state machine. + pub fn handle_incoming(&mut self, now: SystemTime, message: Message) { + todo!() + } + + /// Poll for outgoing messages. + pub fn poll_outgoing(&mut self, now: SystemTime) -> Poll> { + todo!() + } + + /// Poll for new router configurations. + pub fn poll_router_config(&mut self, now: SystemTime) -> Poll { + todo!() + } +} diff --git a/packages/centipede_proto/src/control.rs b/packages/centipede_proto/src/control.rs index e4d84c3..0d6317d 100644 --- a/packages/centipede_proto/src/control.rs +++ b/packages/centipede_proto/src/control.rs @@ -1,4 +1,4 @@ -use std::{fmt::Debug, marker::PhantomData, net::SocketAddr, ops::Deref}; +use std::{fmt::Debug, marker::PhantomData, net::SocketAddr, ops::Deref, time::Duration}; use ed25519_dalek::Signer; use serde::{Deserialize, Serialize}; @@ -22,55 +22,39 @@ where sequence_number: u64, /// The content of the message - content: MessageKind, + content: Content, /// Marker for the authentication status. _auth: PhantomData, } -/// The kind of a control message. -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum MessageKind { - /// A payload. - Payload(Payload), - - /// An acknowledgement. - Ack, -} - -/// The payload of a control message. +/// The body of a control message. /// Carries the actual content without authentication. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] -pub enum Payload { +pub enum Content { /// Initiate a connection. - /// - /// Sent by the initiator to the responder. Initiate { - /// The ephemeral Diffie-Hellman public key of the sender. - /// This is used to derive the shared secret. + /// Timestamp of the initiation, measured from the UNIX epoch. + /// Used to identify and order the handshake, and prevent replay attacks. + timestamp: Duration, + + /// The initiator's ECDH public key. ecdh_public_key: x25519_dalek::PublicKey, }, - /// Acknowledge a connection initiation. - /// - /// Sent by the responder to the initiator. - InitiateAck { - /// The ephemeral Diffie-Hellman public key of the sender. - /// This is used to derive the shared secret. - ecdh_public_key: x25519_dalek::PublicKey, + /// Acknowledge a connection. + InitiateAcknowledge { + /// Timestamp of the initiation, measured from the UNIX epoch. + /// Used to match the acknowledgement to the initiation. + timestamp: Duration, - /// Socket addresses on which the responder is listening. - responder_addresses: Vec, + /// The responder's ECDH public key. + ecdh_public_key: x25519_dalek::PublicKey, }, - /// Update the addresses of the sender on the receiver. - /// - /// Sent by either peer to the other. - /// In particular, sent by the initiator to the responder after receiving an `InitiateAck`. - UpdateAddresses { - /// Socket addresses on which the sender is listening. - addresses: Vec, - }, + /// Inform the receiver that the initiator is listening on + /// (and reachable at) the address from which the message was sent. + Heartbeat, } impl Message { @@ -85,7 +69,7 @@ impl Message { } /// The claimed content of this message. - pub fn claimed_content(&self) -> &MessageKind { + pub fn claimed_content(&self) -> &Content { &self.content } } @@ -102,7 +86,7 @@ impl Message { } /// The validated content of this message. - pub fn content(&self) -> &MessageKind { + pub fn content(&self) -> &Content { self.claimed_content() } @@ -110,13 +94,9 @@ impl Message { pub fn serialize( signing_key: &ed25519_dalek::SigningKey, sequence_number: u64, - content: MessageKind, + content: &Content, ) -> Vec { - let size = PAYLOAD_RANGE.start - + match &content { - MessageKind::Payload(payload) => bincode::serialized_size(payload).unwrap_or(0), - MessageKind::Ack => 0, - } as usize; + let size = CONTENT_RANGE.start + bincode::serialized_size(content).unwrap_or(0) as usize; let mut buffer = vec![0; size]; @@ -126,15 +106,7 @@ impl Message { buffer[SEQUENCE_NUMBER_RANGE].copy_from_slice(&sequence_number.to_be_bytes()); - match content { - MessageKind::Payload(payload) => { - buffer[KIND_RANGE].copy_from_slice(&KIND_PAYLOAD.to_be_bytes()); - bincode::serialize_into(&mut buffer[PAYLOAD_RANGE], &payload).unwrap(); - } - MessageKind::Ack => { - buffer[KIND_RANGE].copy_from_slice(&KIND_ACK.to_be_bytes()); - } - } + bincode::serialize_into(&mut buffer[CONTENT_RANGE], content).unwrap(); let signature = signing_key.sign(&buffer[SIGNED_RANGE]); buffer[SIGNATURE_RANGE].copy_from_slice(&signature.to_bytes()); @@ -153,25 +125,15 @@ impl Message { let sequence_number = u64::from_be_bytes(buffer[SEQUENCE_NUMBER_RANGE].try_into().unwrap()); - let kind = u32::from_be_bytes(buffer[KIND_RANGE].try_into().unwrap()); - - let content = match kind { - KIND_PAYLOAD => { - let payload = bincode::deserialize(&buffer[PAYLOAD_RANGE]) - .map_err(ParseError::Deserialize)?; - - MessageKind::Payload(payload) - } - KIND_ACK => MessageKind::Ack, - _ => return Err(ParseError::InvalidKind(kind).into()), - }; + let payload = + bincode::deserialize(&buffer[CONTENT_RANGE]).map_err(ParseError::Deserialize)?; match sender.verify_strict(&buffer[SIGNED_RANGE], &signature) { Ok(_) => Ok(Message { sender, signature, sequence_number, - content, + content: payload, _auth: PhantomData::, }), Err(e) => Err(ValidateError { @@ -179,7 +141,7 @@ impl Message { sender, signature, sequence_number, - content, + content: payload, _auth: PhantomData::, }), reason: e, @@ -230,17 +192,12 @@ const TAG_RANGE: std::ops::Range = 0..8; const SENDER_KEY_RANGE: std::ops::Range = 8..40; const SIGNATURE_RANGE: std::ops::Range = 40..104; const SEQUENCE_NUMBER_RANGE: std::ops::Range = 104..112; -const KIND_RANGE: std::ops::Range = 112..116; -const PAYLOAD_RANGE: std::ops::RangeFrom = 116..; +const CONTENT_RANGE: std::ops::RangeFrom = 112..; const SIGNED_RANGE: std::ops::RangeFrom = 104..; /// The tag of every control message. pub(crate) const CONTROL_TAG: u64 = 1 << 63; -// Kinds of control messages. -const KIND_PAYLOAD: u32 = 0; -const KIND_ACK: u32 = 1; - /// An error representing a failure to parse or validate a control message. #[derive(Debug, Error)] pub enum ParseValidateError { @@ -288,38 +245,17 @@ mod tests { let verifying_key = signing_key.verifying_key(); let sequence_number = 42; - let payload = Payload::Initiate { + let content = Content::Initiate { ecdh_public_key: x25519_dalek::PublicKey::from([0; 32]), }; - let buffer = Message::::serialize( - &signing_key, - sequence_number, - MessageKind::Payload(payload.clone()), - ); - - let message = Message::::parse_and_validate(&buffer).unwrap(); - - assert_eq!(message.sender(), &verifying_key); - assert_eq!(message.sequence_number(), sequence_number); - assert_eq!(*message.content(), MessageKind::Payload(payload)); - } - - #[test] - fn serialize_deserialize_ack() { - let signing_key = ed25519_dalek::SigningKey::from_bytes(&[1; 32]); - let verifying_key = signing_key.verifying_key(); - - let sequence_number = 43; - - let buffer = - Message::::serialize(&signing_key, sequence_number, MessageKind::Ack); + let buffer = Message::::serialize(&signing_key, sequence_number, &content); let message = Message::::parse_and_validate(&buffer).unwrap(); assert_eq!(message.sender(), &verifying_key); assert_eq!(message.sequence_number(), sequence_number); - assert_eq!(*message.content(), MessageKind::Ack); + assert_eq!(*message.content(), content); } #[test] @@ -328,15 +264,11 @@ mod tests { let verifying_key = signing_key.verifying_key(); let sequence_number = 44; - let payload = Payload::Initiate { + let content = Content::Initiate { ecdh_public_key: x25519_dalek::PublicKey::from([0; 32]), }; - let mut buffer = Message::::serialize( - &signing_key, - sequence_number, - MessageKind::Payload(payload.clone()), - ); + let mut buffer = Message::::serialize(&signing_key, sequence_number, &content); buffer[SIGNATURE_RANGE][0] ^= 1; @@ -344,7 +276,7 @@ mod tests { Err(ParseValidateError::Validate(ValidateError { message, .. })) => { assert_eq!(*message.claimed_sender(), verifying_key); assert_eq!(message.claimed_sequence_number(), sequence_number); - assert_eq!(*message.claimed_content(), MessageKind::Payload(payload)); + assert_eq!(*message.claimed_content(), content); } Err(e) => panic!("unexpected error: {}", e), Ok(_) => panic!("unexpected success"), @@ -356,15 +288,11 @@ mod tests { let signing_key = ed25519_dalek::SigningKey::from_bytes(&[1; 32]); let sequence_number = 42; - let payload = Payload::Initiate { + let content = Content::Initiate { ecdh_public_key: x25519_dalek::PublicKey::from([0; 32]), }; - let buffer = Message::::serialize( - &signing_key, - sequence_number, - MessageKind::Payload(payload.clone()), - ); + let buffer = Message::::serialize(&signing_key, sequence_number, &content); assert_eq!(discriminate(buffer).unwrap(), MessageDiscriminant::Control); }