pub(super) mod discovery;
pub(super) mod pairing;
pub(super) mod recovery;
pub(super) mod remove_replica;
pub(super) mod restore;
pub(super) mod sharing;
pub(super) mod sync_check;
pub(super) mod unpairing;
pub(super) mod update_channel_info;
pub(super) mod verification;
use super::{
DeRecChannelStore, DeRecEvent, DeRecSecretStore, DeRecShareStore, DeRecStateStore,
DeRecTransport,
};
use crate::{
Error, Result,
derec_message::{DeRecMessageBuilder, current_timestamp},
protocol::types::{ChannelQuery, Target},
types::{ChannelId, SharedKey},
};
use derec_cryptography::pairing::PairingSecretKeyMaterial;
use derec_proto::{DeRecMessage, MessageBody, SenderKind, TransportProtocol};
use prost::Message;
use std::collections::HashMap;
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(channel_id = channel_id.0))
)]
#[allow(clippy::too_many_arguments)]
pub(super) async fn handle<
Ch: DeRecChannelStore,
Sh: DeRecShareStore,
Ss: DeRecSecretStore,
Us: super::DeRecUserSecretStore,
T: super::DeRecTransport,
St: DeRecStateStore,
>(
channel_store: &mut Ch,
share_store: &mut Sh,
secret_store: &mut Ss,
user_secret_store: &mut Us,
transport: &T,
state_store: &mut St,
own_transport: &derec_proto::TransportProtocol,
message: &DeRecMessage,
secret_id: u64,
channel_id: ChannelId,
shared_key: &SharedKey,
local_replica_id: Option<u64>,
transport_policy: crate::transport::TransportPolicy,
) -> Result<Vec<DeRecEvent>> {
let inner = crate::derec_message::extract_inner_message(&message.message, shared_key)?;
for endpoint in peer_supplied_endpoints(&inner) {
transport_policy.check_peer(endpoint)?;
}
if let Some(expected) = expected_role_for_inbound(&inner) {
require_role(channel_store, secret_id, &[channel_id], expected).await?;
}
let inbound_trace_id = message.trace_id;
match &inner {
MessageBody::StoreShareRequest(_) | MessageBody::StoreShareResponse(_) => {
let author = match &inner {
MessageBody::StoreShareRequest(r) => r.replica_id,
MessageBody::StoreShareResponse(r) => r.replica_id,
_ => None,
};
match (author, &inner) {
(Some(author), MessageBody::StoreShareRequest(request)) => {
let member =
load_replica_member(channel_store, secret_id, channel_id, author).await?;
sharing::handle_replica_request(
channel_store,
share_store,
secret_store,
user_secret_store,
transport,
&member,
request.clone(),
*shared_key,
inbound_trace_id,
local_replica_id,
)
.await
}
(Some(author), MessageBody::StoreShareResponse(response)) => {
let member =
load_replica_member(channel_store, secret_id, channel_id, author).await?;
sharing::handle_replica_response(
channel_store,
secret_store,
secret_id,
channel_id,
&member,
response,
)
.await
}
(Some(_), _) => Err(Error::Invariant(
"replica identity on a message that is not a store-share exchange",
)),
(None, _) => {
let channel = channel_store
.load(secret_id, ChannelQuery::Helper { channel_id })
.await?
.and_then(|r| r.as_helper().cloned())
.ok_or(Error::InvalidInput(
"channel id not present in channel store",
))?;
match (channel.peer_role, &inner) {
(SenderKind::Owner, MessageBody::StoreShareRequest(_))
| (SenderKind::Helper, MessageBody::StoreShareResponse(_)) => {
sharing::handle(channel_id, inner, *shared_key, inbound_trace_id)
}
_ => Err(Error::RoleMismatch {
channel_id,
expected: SenderKind::Owner,
actual: channel.peer_role,
}),
}
}
}
}
MessageBody::VerifyShareRequest(_) | MessageBody::VerifyShareResponse(_) => {
verification::handle(
share_store,
state_store,
secret_id,
channel_id,
inner,
*shared_key,
inbound_trace_id,
)
.await
}
MessageBody::GetSecretIdsVersionsRequest(_)
| MessageBody::GetSecretIdsVersionsResponse(_) => {
let peer = match &inner {
MessageBody::GetSecretIdsVersionsRequest(r) => r.replica_id,
MessageBody::GetSecretIdsVersionsResponse(r) => r.replica_id,
_ => None,
};
match (peer, &inner) {
(Some(peer), MessageBody::GetSecretIdsVersionsRequest(request)) => {
let member =
load_replica_member(channel_store, secret_id, channel_id, peer).await?;
sync_check::answer_versions(
user_secret_store,
transport,
&member,
request,
*shared_key,
secret_id,
local_replica_id,
inbound_trace_id,
)
.await
}
(Some(peer), MessageBody::GetSecretIdsVersionsResponse(response)) => {
let from = crate::types::ReplicaId::try_from(peer)?;
sync_check::collect_version(
channel_store,
secret_store,
state_store,
transport,
secret_id,
from,
response,
local_replica_id,
own_transport,
)
.await
}
_ => {
let expected = match &inner {
MessageBody::GetSecretIdsVersionsRequest(_) => SenderKind::Owner,
_ => SenderKind::Helper,
};
require_role(channel_store, secret_id, &[channel_id], expected).await?;
discovery::handle(channel_id, inner, *shared_key, inbound_trace_id)
}
}
}
MessageBody::GetShareRequest(_) | MessageBody::GetShareResponse(_) => {
let peer = match &inner {
MessageBody::GetShareRequest(r) => r.replica_id,
MessageBody::GetShareResponse(r) => r.replica_id,
_ => None,
};
match (peer, &inner) {
(Some(peer), MessageBody::GetShareRequest(request)) => {
let member =
load_replica_member(channel_store, secret_id, channel_id, peer).await?;
sync_check::answer_share(
channel_store,
secret_store,
user_secret_store,
transport,
&member,
request,
*shared_key,
secret_id,
local_replica_id,
inbound_trace_id,
)
.await
}
(Some(peer), MessageBody::GetShareResponse(response)) => {
let from = crate::types::ReplicaId::try_from(peer)?;
sync_check::accept_share(
channel_store,
secret_store,
user_secret_store,
secret_id,
from,
response,
)
.await
}
_ => {
let expected = match &inner {
MessageBody::GetShareRequest(_) => SenderKind::Owner,
_ => SenderKind::Helper,
};
require_role(channel_store, secret_id, &[channel_id], expected).await?;
recovery::handle(
state_store,
channel_id,
inner,
*shared_key,
inbound_trace_id,
secret_id,
)
.await
}
}
}
MessageBody::UnpairRequest(request) if request.replica_id.is_some() => {
let target = request.replica_id.expect("guarded by the match");
remove_replica::handle_request(channel_store, secret_id, request, target).await
}
MessageBody::UnpairRequest(_) | MessageBody::UnpairResponse(_) => {
if matches!(inner, MessageBody::UnpairRequest(_)) {
require_role(channel_store, secret_id, &[channel_id], SenderKind::Owner).await?;
}
unpairing::handle(
channel_store,
share_store,
secret_store,
state_store,
secret_id,
channel_id,
inner,
*shared_key,
inbound_trace_id,
)
.await
}
MessageBody::UpdateChannelInfoRequest(_) | MessageBody::UpdateChannelInfoResponse(_) => {
update_channel_info::handle(channel_id, inner, *shared_key, inbound_trace_id).await
}
_ => Err(Error::Invariant(
"unexpected MessageBody variant in channel message",
)),
}
}
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(channel_id = channel_id.0))
)]
#[allow(clippy::too_many_arguments)]
pub(in crate::protocol) async fn handle_pairing<
Ch: DeRecChannelStore,
Ss: DeRecSecretStore,
T: DeRecTransport,
>(
channel_store: &mut Ch,
secret_store: &mut Ss,
transport: &T,
communication_info: &HashMap<String, String>,
message: &DeRecMessage,
secret_id: u64,
channel_id: ChannelId,
pairing_secret: &PairingSecretKeyMaterial,
replica_id: Option<u64>,
parameter_range: Option<&derec_proto::ParameterRange>,
transport_policy: crate::transport::TransportPolicy,
) -> Result<Vec<DeRecEvent>> {
let inner =
crate::derec_message::extract_inner_pairing_message(&message.message, pairing_secret)?;
for endpoint in peer_supplied_endpoints(&inner) {
transport_policy.check_peer(endpoint)?;
}
pairing::handle(
channel_store,
secret_store,
transport,
communication_info,
&inner,
secret_id,
channel_id,
pairing_secret,
message.trace_id,
replica_id,
parameter_range,
)
.await
}
pub(super) async fn require_role<Ch: DeRecChannelStore>(
channel_store: &Ch,
secret_id: u64,
channel_ids: &[ChannelId],
expected: SenderKind,
) -> Result<()> {
for channel_id in channel_ids {
let channel = channel_store
.load(
secret_id,
ChannelQuery::Helper {
channel_id: *channel_id,
},
)
.await?
.and_then(|r| r.as_helper().cloned())
.ok_or(Error::InvalidInput(
"channel id not present in channel store",
))?;
if channel.peer_role != expected {
return Err(Error::RoleMismatch {
channel_id: *channel_id,
expected,
actual: channel.peer_role,
});
}
}
Ok(())
}
pub(super) async fn resolve_target<Ch: DeRecChannelStore>(
channel_store: &mut Ch,
secret_id: u64,
target: Target,
) -> Result<Vec<ChannelId>> {
let all_channels = channel_store.helpers(secret_id).await?;
let all_channel_ids: Vec<ChannelId> = all_channels.iter().map(|c| c.channel_id).collect();
Ok(match target {
Target::All => all_channel_ids,
Target::Single(id) => {
if all_channel_ids.contains(&id) {
vec![id]
} else {
vec![]
}
}
Target::Many(ids) => ids
.into_iter()
.filter(|id| all_channel_ids.contains(id))
.collect(),
})
}
pub(super) async fn peer_endpoint<Ch: DeRecChannelStore>(
channel_store: &mut Ch,
secret_id: u64,
channel_id: ChannelId,
) -> Result<TransportProtocol> {
let channel = channel_store
.load(secret_id, ChannelQuery::Helper { channel_id })
.await?;
channel
.map(|ch| ch.transport().clone())
.ok_or(Error::InvalidInput("no transport endpoint for channel"))
}
pub(super) async fn resolve_response_endpoint<Ch: DeRecChannelStore>(
channel_store: &mut Ch,
secret_id: u64,
channel_id: ChannelId,
reply_to: Option<&TransportProtocol>,
) -> Result<TransportProtocol> {
if let Some(endpoint) = reply_to {
return Ok(endpoint.clone());
}
peer_endpoint(channel_store, secret_id, channel_id).await
}
pub(super) fn fresh_trace_id() -> u64 {
use rand::Rng as _;
rand::rng().next_u64()
}
pub(super) fn apply_trace_id(envelope_bytes: Vec<u8>, trace_id: u64) -> Result<Vec<u8>> {
crate::derec_message::apply_trace_id(&envelope_bytes, trace_id)
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn send_channel_message<Ch: DeRecChannelStore, T: DeRecTransport>(
channel_store: &mut Ch,
transport: &T,
secret_id: u64,
channel_id: ChannelId,
body: MessageBody,
shared_key: &SharedKey,
inbound_trace_id: u64,
reply_to: Option<&TransportProtocol>,
) -> Result<()> {
let envelope = DeRecMessageBuilder::channel()
.channel_id(channel_id)
.timestamp(current_timestamp())
.message_body(body)
.trace_id(inbound_trace_id)
.encrypt(shared_key)?
.build()?;
let wire_bytes = envelope.encode_to_vec();
let endpoint =
resolve_response_endpoint(channel_store, secret_id, channel_id, reply_to).await?;
transport.send(&endpoint, wire_bytes).await?;
Ok(())
}
pub(in crate::protocol) fn peer_supplied_endpoints(
body: &MessageBody,
) -> impl Iterator<Item = &TransportProtocol> {
let endpoint = match body {
MessageBody::StoreShareRequest(r) => r.reply_to.as_ref(),
MessageBody::VerifyShareRequest(r) => r.reply_to.as_ref(),
MessageBody::GetSecretIdsVersionsRequest(r) => r.reply_to.as_ref(),
MessageBody::GetShareRequest(r) => r.reply_to.as_ref(),
MessageBody::UnpairRequest(r) => r.reply_to.as_ref(),
MessageBody::UpdateChannelInfoRequest(r) => r.transport_protocol.as_ref(),
MessageBody::PairRequest(r) => r.transport_protocol.as_ref(),
MessageBody::PrePairRequest(r) => r.transport_protocol.as_ref(),
_ => None,
};
endpoint.into_iter()
}
fn expected_role_for_inbound(body: &MessageBody) -> Option<SenderKind> {
match body {
MessageBody::StoreShareRequest(_) | MessageBody::StoreShareResponse(_) => None,
MessageBody::GetSecretIdsVersionsRequest(_)
| MessageBody::GetSecretIdsVersionsResponse(_)
| MessageBody::GetShareRequest(_)
| MessageBody::GetShareResponse(_) => None,
MessageBody::VerifyShareRequest(_) => Some(SenderKind::Owner),
MessageBody::UnpairRequest(_) => None,
MessageBody::VerifyShareResponse(_) | MessageBody::UnpairResponse(_) => {
Some(SenderKind::Helper)
}
MessageBody::UpdateChannelInfoRequest(_) | MessageBody::UpdateChannelInfoResponse(_) => {
None
}
_ => None,
}
}
async fn load_replica_member<Ch: DeRecChannelStore>(
channel_store: &Ch,
secret_id: u64,
channel_id: ChannelId,
author: u64,
) -> Result<crate::protocol::types::ReplicaMember> {
let replica_id = crate::types::ReplicaId::try_from(author)?;
channel_store
.load(
secret_id,
ChannelQuery::Replica {
channel_id,
replica_id,
},
)
.await?
.and_then(|r| r.as_replica().cloned())
.ok_or(Error::InvalidInput(
"replica sync names a member that is not in the group",
))
}
#[cfg(test)]
mod replica_id_discrimination_tests {
use super::*;
use crate::protocol::test::{InMemChannelStore, run_async};
use crate::protocol::types::{ChannelRecord, ChannelStatus, HelperChannel};
use crate::types::ChannelId;
const SECRET_ID: u64 = 0xD15C;
#[test]
fn a_store_share_naming_an_unknown_member_is_refused() {
run_async(async {
let mut channels = InMemChannelStore::default();
channels
.save(
SECRET_ID,
ChannelRecord::Helper(HelperChannel {
channel_id: ChannelId(9001),
transport: derec_proto::TransportProtocol {
uri: "https://helper.example".to_owned(),
protocol: derec_proto::Protocol::Https as i32,
},
communication_info: std::collections::HashMap::new(),
peer_role: derec_proto::SenderKind::Helper,
status: ChannelStatus::Paired,
created_at: 0,
}),
)
.await
.expect("seed helper channel");
let err = super::load_replica_member(&channels, SECRET_ID, ChannelId(9001), 1002)
.await
.expect_err("a helper channel holds no member, so this must not resolve");
assert!(
matches!(err, Error::InvalidInput(_)),
"expected an InvalidInput protocol error, got {err:?}"
);
});
}
#[test]
fn replica_id_zero_never_names_a_member() {
run_async(async {
let channels = InMemChannelStore::default();
let err = super::load_replica_member(&channels, SECRET_ID, ChannelId(9001), 0)
.await
.expect_err("zero is the absent sentinel and must be rejected");
assert!(
matches!(err, Error::InvalidInput(_)),
"expected an InvalidInput protocol error, got {err:?}"
);
});
}
}