use crate::extensions::advertised_endpoints::AdvertisedEndpoints as _;
use crate::extensions::channel_store::ChannelStoreExt as _;
use crate::extensions::sender_kind::SenderKindExt as _;
use crate::protocol::context::{Local, PairingConfig};
use crate::protocol::stores::{StoreSet, Stores};
use crate::protocol::{
DeRecChannelStore, DeRecEvent, DeRecSecretStore, DeRecTransport, PairingKeyMaterial,
PendingAction, SecretKind, SecretValue, now_secs,
};
use crate::{
Error, Result,
derec_message::{DeRecMessageBuilder, current_timestamp},
primitives::pairing::{request, response},
types::ChannelId,
};
use derec_cryptography::pairing::PairingSecretKeyMaterial;
use derec_proto::{
ContactMessage, ContactMode, DeRecResult, MessageBody, PairRequestMessage, PairResponseMessage,
SenderKind, StatusEnum, TransportProtocol,
};
use prost::Message;
use std::collections::HashMap;
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(trace_id = trace_id, channel_id = channel_id.0))
)]
pub(in crate::protocol) async fn accept<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
pairing: &PairingConfig<'_>,
channel_id: ChannelId,
request: &PairRequestMessage,
kind: SenderKind,
trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
let secret_id = local.secret_id;
let replica_id_to_inject = super::require_replica_id_for_kind(kind, local.replica_id)?;
let Some(SecretValue::PairingSecret(pairing_secret)) = stores
.secrets
.load(secret_id, channel_id, SecretKind::PairingSecret)
.await?
else {
return Err(Error::Invariant(
"Pair accept: missing PairingSecret (initiator state lost)",
));
};
let pairing_secret = pairing_secret.to_secret()?;
let comm_info = super::build_communication_info(local.communication_info, replica_id_to_inject);
let resp = response::produce(
channel_id,
request,
&pairing_secret,
comm_info,
pairing.parameter_range.cloned(),
local.policy,
)?;
let new_channel_id = resp.channel_id;
stores
.secrets
.save(
secret_id,
new_channel_id,
SecretValue::SharedKey(resp.shared_key),
)
.await?;
let peer_transport = resp.peer_transports.clone();
let contact_mode = match stores
.secrets
.load(secret_id, channel_id, SecretKind::PairingContact)
.await?
{
Some(SecretValue::PairingContact(contact)) => Some(contact.contact_mode),
_ => None,
};
let status = completed_pairing_status(kind, contact_mode);
let peer_sender_kind = super::request_sender_kind(request)?;
let (peer_communication_info, peer_replica_id) =
super::extract_communication_info(&request.communication_info, peer_sender_kind)?;
persist_peer_record(
stores,
local,
new_channel_id,
peer_transport,
peer_communication_info.clone(),
peer_sender_kind,
peer_replica_id,
status,
)
.await?;
if kind.is_replica() {
super::persist_own_member(
stores,
local,
new_channel_id,
kind,
local.replica_id,
status,
)
.await?;
}
let envelope = crate::derec_message::apply_trace_id(&resp.envelope, trace_id)?;
stores
.transport
.send(&resp.peer_transports, envelope)
.await?;
stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingSecret)
.await?;
stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingContact)
.await?;
stores
.channels
.remove(
secret_id,
crate::protocol::types::ChannelQuery::Helper { channel_id },
)
.await?;
#[cfg(feature = "logging")]
tracing::info!(
old_channel_id = channel_id.0,
new_channel_id = new_channel_id.0,
"pairing complete (responder side); rotated to long-term channel_id"
);
Ok(pair_completion_events(
new_channel_id,
channel_id,
kind,
peer_communication_info,
peer_replica_id,
))
}
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(trace_id = trace_id, channel_id = channel_id.0))
)]
pub(in crate::protocol) async fn reject<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
channel_id: ChannelId,
request: &PairRequestMessage,
status: StatusEnum,
memo: &str,
trace_id: u64,
) -> Result<()> {
let peer_transports = local
.policy
.admit_peer_endpoints(request.advertised_endpoints())?;
let timestamp = current_timestamp();
let response = PairResponseMessage {
result: Some(DeRecResult {
status: status as i32,
memo: memo.to_owned(),
}),
nonce: request.nonce,
communication_info: None,
parameter_range: None,
timestamp: Some(timestamp),
channel_id: 0,
};
let envelope = DeRecMessageBuilder::pairing()
.channel_id(channel_id)
.timestamp(timestamp)
.message_body(MessageBody::PairResponse(response))
.trace_id(trace_id)
.encrypt_pairing(&request.ecies_public_key)?
.build()?
.encode_to_vec();
stores.transport.send(&peer_transports, envelope).await?;
stores
.secrets
.remove(local.secret_id, channel_id, SecretKind::PairingSecret)
.await?;
#[cfg(feature = "logging")]
tracing::info!("pairing request rejected");
Ok(())
}
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(trace_id = trace_id, channel_id = channel_id.0))
)]
pub(super) fn on_request(
local: &Local<'_>,
channel_id: ChannelId,
request: &PairRequestMessage,
trace_id: u64,
) -> Result<Vec<DeRecEvent>> {
let peer_kind = super::request_sender_kind(request)?;
let (peer_communication_info, _peer_replica_id) =
super::extract_communication_info(&request.communication_info, peer_kind)?;
let kind = peer_kind.counterparty();
let _ = super::require_replica_id_for_kind(kind, local.replica_id)?;
let action = PendingAction::Pairing {
channel_id,
request: request.clone(),
kind,
peer_communication_info,
trace_id,
};
Ok(vec![DeRecEvent::ActionRequired { channel_id, action }])
}
#[cfg_attr(
feature = "logging",
tracing::instrument(skip_all, fields(channel_id = channel_id.0))
)]
pub(super) async fn on_response<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
pairing: &PairingConfig<'_>,
channel_id: ChannelId,
response: &derec_proto::PairResponseMessage,
pairing_secret: &PairingSecretKeyMaterial,
) -> Result<Vec<DeRecEvent>> {
let replica_id = local.replica_id;
let secret_id = local.secret_id;
let contact = match stores
.secrets
.load(secret_id, channel_id, SecretKind::PairingContact)
.await?
{
Some(SecretValue::PairingContact(c)) => c,
_ => {
return Err(Error::InvalidInput(
"no pairing contact stored for channel — start must be called first",
));
}
};
let result = response::process(&contact, response, pairing_secret, pairing.parameter_range)?;
let new_channel_id = result.channel_id;
let helper_placeholder = stores
.channels
.load(
secret_id,
crate::protocol::types::ChannelQuery::Helper { channel_id },
)
.await?
.and_then(|r| r.as_helper().cloned());
let own_member = match (helper_placeholder.as_ref(), replica_id) {
(None, Some(own)) => stores
.channels
.load(
secret_id,
crate::protocol::types::ChannelQuery::Replica {
channel_id,
replica_id: crate::types::ReplicaId::try_from(own)?,
},
)
.await?
.and_then(|r| r.as_replica().cloned()),
_ => None,
};
let peer_kind = match (helper_placeholder.as_ref(), own_member.as_ref()) {
(Some(placeholder), _) => placeholder.peer_role,
(None, Some(mine)) => mine.role.counterparty().to_sender_kind(),
(None, None) => {
return Err(Error::Invariant(
"no pairing record on pair response — start must be called first",
));
}
};
let kind = peer_kind.counterparty();
let status = completed_pairing_status(kind, Some(contact.contact_mode));
let _ = super::require_replica_id_for_kind(kind, replica_id)?;
let (peer_communication_info, peer_replica_id) =
super::extract_communication_info(&response.communication_info, peer_kind)?;
if let Some(peer_replica_id) = peer_replica_id
&& super::replica_id_is_taken(stores, local, peer_replica_id).await?
{
abandon_pairing(stores, local, channel_id).await?;
return Err(Error::ReplicaIdConflict {
replica_id: peer_replica_id,
});
}
let peer_transport = match helper_placeholder.as_ref() {
Some(p) => p.transports.clone(),
None => local
.policy
.admit_peer_endpoints(contact.advertised_endpoints())?,
};
let mut merged_info = helper_placeholder
.as_ref()
.map(|p| p.communication_info.clone())
.unwrap_or_default();
for (k, v) in &peer_communication_info {
merged_info.insert(k.clone(), v.clone());
}
persist_peer_record(
stores,
local,
new_channel_id,
peer_transport,
merged_info,
peer_kind,
peer_replica_id,
status,
)
.await?;
if let Some(mine) = own_member {
stores
.channels
.save(
secret_id,
crate::protocol::types::ChannelRecord::Replica(
crate::protocol::types::ReplicaMember {
channel_id: new_channel_id,
..mine
},
),
)
.await?;
}
stores
.secrets
.save(
secret_id,
new_channel_id,
SecretValue::SharedKey(result.shared_key),
)
.await?;
stores
.channels
.remove(
secret_id,
crate::protocol::types::ChannelQuery::Helper { channel_id },
)
.await?;
stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingSecret)
.await?;
stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingContact)
.await?;
#[cfg(feature = "logging")]
tracing::info!(
old_channel_id = channel_id.0,
new_channel_id = new_channel_id.0,
"pairing complete (initiator side); rotated to long-term channel_id"
);
Ok(pair_completion_events(
new_channel_id,
channel_id,
kind,
peer_communication_info,
peer_replica_id,
))
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn start<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
pairing: &PairingConfig<'_>,
channel_id: ChannelId,
contact: ContactMessage,
peer_communication_info: HashMap<String, String>,
endpoints: Vec<TransportProtocol>,
kind: SenderKind,
replica_id_to_inject: Option<u64>,
trace_id: u64,
) -> Result<u64> {
super::reject_start_on_paired_channel(stores, local, channel_id).await?;
let comm_info = super::build_communication_info(local.communication_info, replica_id_to_inject);
let result = request::produce(
kind,
local.own_transports.to_vec(),
&contact,
comm_info,
pairing.parameter_range.cloned(),
)?;
stores
.secrets
.save(
local.secret_id,
channel_id,
SecretValue::PairingSecret(PairingKeyMaterial::from_secret(&result.secret_key)),
)
.await?;
stores
.secrets
.save(
local.secret_id,
channel_id,
SecretValue::PairingContact(result.initiator_contact_message),
)
.await?;
super::persist_start_record(
stores,
local,
channel_id,
endpoints.clone(),
peer_communication_info,
kind,
replica_id_to_inject,
)
.await?;
#[cfg(feature = "logging")]
tracing::info!("pairing request sent");
let envelope = crate::derec_message::apply_trace_id(&result.envelope, trace_id)?;
stores.transport.send(&endpoints, envelope).await?;
Ok(channel_id.0)
}
pub(super) async fn abandon_pairing<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
channel_id: ChannelId,
) -> Result<()> {
let secret_id = local.secret_id;
let roster = stores
.channels
.replicas_matching(secret_id, crate::protocol::types::ReplicaFilter::default())
.await?;
if let Some(own) = local.replica_id
&& roster.len() == 1
&& roster[0].replica_id.0 == own
{
stores
.channels
.remove(
secret_id,
crate::protocol::types::ChannelQuery::Replica {
channel_id: roster[0].channel_id,
replica_id: roster[0].replica_id,
},
)
.await?;
}
let _ = stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingSecret)
.await;
let _ = stores
.secrets
.remove(secret_id, channel_id, SecretKind::PairingContact)
.await;
stores
.channels
.remove(
secret_id,
crate::protocol::types::ChannelQuery::Helper { channel_id },
)
.await?;
Ok(())
}
pub(super) fn completed_pairing_status(
kind: SenderKind,
contact_mode: Option<i32>,
) -> crate::protocol::types::ChannelStatus {
let awaits_confirmation = kind.is_replica() || contact_mode == Some(ContactMode::NoKeys as i32);
if awaits_confirmation {
crate::protocol::types::ChannelStatus::Pending
} else {
crate::protocol::types::ChannelStatus::Paired
}
}
pub(super) fn pair_completion_events(
channel_id: ChannelId,
pairing_channel_id: ChannelId,
kind: SenderKind,
peer_communication_info: HashMap<String, String>,
peer_replica_id: Option<u64>,
) -> Vec<DeRecEvent> {
let mut events = vec![DeRecEvent::PairingCompleted {
channel_id,
pairing_channel_id,
kind,
peer_communication_info,
}];
if let Some(peer_replica_id) = peer_replica_id {
events.push(DeRecEvent::ReplicaPaired {
channel_id,
peer_replica_id,
});
}
events
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn persist_peer_record<S: StoreSet>(
stores: &mut Stores<'_, S>,
local: &Local<'_>,
channel_id: ChannelId,
transports: Vec<TransportProtocol>,
communication_info: HashMap<String, String>,
peer_kind: SenderKind,
peer_replica_id: Option<u64>,
status: crate::protocol::types::ChannelStatus,
) -> Result<()> {
use crate::protocol::types::{ChannelRecord, HelperChannel, ReplicaMember, ReplicaRole};
let record = match ReplicaRole::from_sender_kind(peer_kind) {
Some(role) => {
let replica_id = peer_replica_id
.ok_or(Error::Invariant(
"replica pairing completed without the peer's replica_id",
))
.and_then(crate::types::ReplicaId::try_from)?;
ChannelRecord::Replica(ReplicaMember {
channel_id,
replica_id,
transports,
communication_info: communication_info.clone(),
role,
status,
created_at: now_secs(),
})
}
None => ChannelRecord::Helper(HelperChannel {
channel_id,
transports,
communication_info: communication_info.clone(),
peer_role: peer_kind,
status,
created_at: now_secs(),
}),
};
stores.channels.save(local.secret_id, record).await?;
Ok(())
}