Skip to content

Commit

Permalink
allow relationship queries to be routed (remove routing info from pay…
Browse files Browse the repository at this point in the history
…load for affirmations)
  • Loading branch information
tweedegolf-marc authored and marlonbaeten committed May 13, 2024
1 parent 914460e commit 99e3687
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 57 deletions.
43 changes: 37 additions & 6 deletions tsp/src/async_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ impl AsyncStore {
let sender = self.inner.get_private_vid(sender)?;
let receiver = self.inner.get_verified_vid(receiver)?;

let path = route;
let route = route.map(|collection| collection.iter().map(|vid| vid.as_ref()).collect());

let (tsp_message, thread_id) = crate::crypto::seal_and_hash(
Expand All @@ -199,7 +200,12 @@ impl AsyncStore {
Payload::RequestRelationship { route },
)?;

crate::transport::send_message(receiver.endpoint(), &tsp_message).await?;
if let Some(hop_list) = path {
self.resolve_route_and_send(hop_list, &tsp_message).await?;
self.set_route_for_vid(receiver.identifier(), hop_list)?;
} else {
crate::transport::send_message(receiver.endpoint(), &tsp_message).await?;
}

self.set_relation_status_for_vid(
receiver.identifier(),
Expand All @@ -219,16 +225,19 @@ impl AsyncStore {
thread_id: Digest,
route: Option<&[&str]>,
) -> Result<(), Error> {
let route = route.map(|collection| collection.iter().map(|vid| vid.as_ref()).collect());

let (transport, message) = self.inner.seal_message_payload(
let (transport, tsp_message) = self.inner.seal_message_payload(
sender,
receiver,
None,
Payload::AcceptRelationship { thread_id, route },
Payload::AcceptRelationship { thread_id },
)?;

crate::transport::send_message(&transport, &message).await?;
if let Some(hop_list) = route {
self.resolve_route_and_send(hop_list, &tsp_message).await?;
self.set_route_for_vid(receiver, hop_list)?;
} else {
crate::transport::send_message(&transport, &tsp_message).await?;
}

self.set_relation_status_for_vid(receiver, RelationshipStatus::Bidirectional(thread_id))?;

Expand Down Expand Up @@ -272,6 +281,28 @@ impl AsyncStore {
Ok(transport)
}

/// Send a message given a route, extracting the next hop and verifying it in the process
async fn resolve_route_and_send(
&self,
hop_list: &[&str],
opaque_message: &[u8],
) -> Result<(), Error> {
let Some(next_hop) = hop_list.first() else {
return Err(Error::InvalidRoute(
"relationship route must not be empty".into(),
));
};

let next_hop = self.inner.get_verified_vid(next_hop)?;
//TODO: can we avoid the allocation here?
let path = hop_list[1..].iter().map(|x| x.as_bytes()).collect();

self.forward_routed_message(next_hop.identifier(), path, opaque_message)
.await?;

Ok(())
}

/// Pass along a in-transit routed TSP `opaque_message` that is not meant for us, given earlier resolved VID's.
/// The message is routed through the route that has been established with `receiver`.
pub async fn forward_routed_message(
Expand Down
23 changes: 5 additions & 18 deletions tsp/src/cesr/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ pub enum Payload<'a, Bytes: AsRef<[u8]>, Vid> {
/// A TSP message requesting a relationship
DirectRelationProposal { nonce: Nonce, hops: Vec<Vid> },
/// A TSP message confiming a relationship
DirectRelationAffirm {
reply: &'a Sha256Digest,
hops: Vec<Vid>,
},
DirectRelationAffirm { reply: &'a Sha256Digest },
/// A TSP message requesting a nested relationship
NestedRelationProposal { public_keys: PairedKeys<'a> },
/// A TSP message confiming a relationship
Expand Down Expand Up @@ -164,9 +161,8 @@ pub fn encode_payload(
encode_hops(hops, output)?;
encode_fixed_data(TSP_NONCE, &nonce.0, output);
}
Payload::DirectRelationAffirm { reply, hops } => {
Payload::DirectRelationAffirm { reply } => {
encode_fixed_data(TSP_TYPECODE, &msgtype::NEW_REL_REPLY, output);
encode_hops(hops, output)?;
encode_fixed_data(TSP_SHA256, reply, output);
}
Payload::NestedRelationProposal { public_keys } => {
Expand Down Expand Up @@ -258,14 +254,8 @@ pub fn decode_payload<'a, Vid: TryFrom<&'a [u8]>>(
decode_variable_data(TSP_PLAINTEXT, &mut stream)
.map(|msg| Payload::RoutedMessage(hop_list, msg))
}
msgtype::NEW_REL_REPLY => {
let hop_list = decode_hops(&mut stream)?;

decode_fixed_data(TSP_SHA256, &mut stream).map(|reply| Payload::DirectRelationAffirm {
reply,
hops: hop_list,
})
}
msgtype::NEW_REL_REPLY => decode_fixed_data(TSP_SHA256, &mut stream)
.map(|reply| Payload::DirectRelationAffirm { reply }),
msgtype::NEW_NEST_REL => {
decode_fixed_data(ED25519_PUBLICKEY, &mut stream).and_then(|signing| {
decode_fixed_data(HPKE_PUBLICKEY, &mut stream).map(|encrypting| {
Expand Down Expand Up @@ -1047,10 +1037,7 @@ mod test {
nonce: Nonce(*nonce),
hops: vec![],
});
test_turn_around(Payload::DirectRelationAffirm {
reply: nonce,
hops: vec![],
});
test_turn_around(Payload::DirectRelationAffirm { reply: nonce });
let public_keys = PairedKeys {
signing: pk1.as_slice().try_into().unwrap(),
encrypting: pk2.as_slice().try_into().unwrap(),
Expand Down
24 changes: 6 additions & 18 deletions tsp/src/crypto/tsp_hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,9 @@ where
nonce: fresh_nonce(&mut csprng),
hops: route.unwrap_or_else(Vec::new),
},
Payload::AcceptRelationship {
ref thread_id,
route,
} => crate::cesr::Payload::DirectRelationAffirm {
reply: thread_id,
hops: route.unwrap_or_else(Vec::new),
},
Payload::AcceptRelationship { ref thread_id } => {
crate::cesr::Payload::DirectRelationAffirm { reply: thread_id }
}
Payload::CancelRelationship { ref thread_id } => crate::cesr::Payload::RelationshipCancel {
nonce: fresh_nonce(&mut csprng),
reply: thread_id,
Expand Down Expand Up @@ -168,17 +164,9 @@ where
Some(hops.to_vec())
},
},
crate::cesr::Payload::DirectRelationAffirm {
reply: &thread_id,
hops,
} => Payload::AcceptRelationship {
thread_id,
route: if hops.is_empty() {
None
} else {
Some(hops.to_vec())
},
},
crate::cesr::Payload::DirectRelationAffirm { reply: &thread_id } => {
Payload::AcceptRelationship { thread_id }
}
crate::cesr::Payload::NestedRelationProposal { .. } => todo!(),
crate::cesr::Payload::NestedRelationAffirm { .. } => todo!(),
crate::cesr::Payload::RelationshipCancel {
Expand Down
18 changes: 4 additions & 14 deletions tsp/src/definitions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,9 @@ pub enum Payload<'a, Bytes: AsRef<[u8]>> {
Content(Bytes),
NestedMessage(Bytes),
RoutedMessage(Vec<VidData<'a>>, Bytes),
CancelRelationship {
thread_id: Digest,
},
RequestRelationship {
route: Option<Vec<VidData<'a>>>,
},
AcceptRelationship {
thread_id: Digest,
route: Option<Vec<VidData<'a>>>,
},
CancelRelationship { thread_id: Digest },
RequestRelationship { route: Option<Vec<VidData<'a>>> },
AcceptRelationship { thread_id: Digest },
}

impl<'a, Bytes: AsRef<[u8]>> Payload<'a, Bytes> {
Expand Down Expand Up @@ -100,10 +93,7 @@ impl<'a, Bytes: AsRef<[u8]>> fmt::Display for Payload<'a, Bytes> {
}
Payload::CancelRelationship { thread_id: _ } => write!(f, "Cancel Relationship"),
Payload::RequestRelationship { route: _ } => write!(f, "Request Relationship"),
Payload::AcceptRelationship {
thread_id: _,
route: _,
} => write!(f, "Accept Relationship"),
Payload::AcceptRelationship { thread_id: _ } => write!(f, "Accept Relationship"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsp/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl Store {
thread_id: crate::crypto::sha256(raw_bytes),
})
}
Payload::AcceptRelationship { thread_id, route } => {
Payload::AcceptRelationship { thread_id } => {
let mut vids = self.vids.write()?;
let Some(context) = vids.get_mut(&sender) else {
//TODO: should we inform the user of who sent this?
Expand Down

0 comments on commit 99e3687

Please sign in to comment.