Skip to content

Commit

Permalink
Re-add signing operations
Browse files Browse the repository at this point in the history
  • Loading branch information
rnijveld committed Oct 7, 2024
1 parent abf40f4 commit fff98b4
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
81 changes: 73 additions & 8 deletions statime/src/port/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use arrayvec::ArrayVec;

use super::{state::PortState, ForwardedTLVProvider, Port, PortActionIterator, Running};
use crate::{
crypto::SecurityAssociationProvider,
datastructures::{
common::{PortIdentity, Tlv, TlvSetBuilder, TlvType},
messages::{DelayReqMessage, Header, Message, MAX_DATA_LEN},
Expand All @@ -12,17 +13,28 @@ use crate::{
time::Time,
};

impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R, C, F, P, S> {
impl<'a, A, C, F: Filter, R, P: SecurityAssociationProvider, S: PtpInstanceStateMutex>
Port<'a, Running, A, R, C, F, P, S>
{
pub(super) fn send_sync(&mut self) -> PortActionIterator {
if matches!(self.port_state, PortState::Master) {
log::trace!("sending sync message");

let seq_id = self.sync_seq_ids.generate();
let packet_length = match self
let mut message = self
.instance_state
.with_ref(|state| Message::sync(&state.default_ds, self.port_identity, seq_id))
.serialize(&mut self.packet_buffer)
{
.with_ref(|state| Message::sync(&state.default_ds, self.port_identity, seq_id));
let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if message
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not add signature to sync, sending without")
}
}

let packet_length = match message.serialize(&mut self.packet_buffer) {
Ok(message) => message,
Err(error) => {
log::error!("Statime bug: Could not serialize sync: {:?}", error);
Expand All @@ -49,6 +61,19 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R

pub(super) fn handle_sync_timestamp(&mut self, id: u16, timestamp: Time) -> PortActionIterator {
if matches!(self.port_state, PortState::Master) {
let mut message = self.instance_state.with_ref(|state| {
Message::follow_up(&state.default_ds, self.port_identity, id, timestamp)
});
let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if message
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign follow up message, sending unsigned");
}
}

let packet_length = match self
.instance_state
.with_ref(|state| {
Expand Down Expand Up @@ -141,6 +166,16 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R

message.suffix = tlv_builder.build();

let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if message
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign announce message, sending unsigned");
}
}

let packet_length = match message.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Err(error) => {
Expand Down Expand Up @@ -174,14 +209,24 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R
) -> PortActionIterator {
if matches!(self.port_state, PortState::Master) {
log::debug!("Received DelayReq");
let delay_resp_message = Message::delay_resp(
let mut delay_resp_message = Message::delay_resp(
header,
message,
self.port_identity,
self.config.min_delay_req_interval(),
timestamp,
);

let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if delay_resp_message
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign delay response message, sending unsigned");
}
}

let packet_length = match delay_resp_message.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Err(error) => {
Expand All @@ -205,10 +250,20 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R
timestamp: Time,
) -> PortActionIterator {
log::debug!("Received PDelayReq");
let pdelay_resp_message = self.instance_state.with_ref(|state| {
let mut pdelay_resp_message = self.instance_state.with_ref(|state| {
Message::pdelay_resp(&state.default_ds, self.port_identity, header, timestamp)
});

let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if pdelay_resp_message
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign pdelay response message, sending unsigned");
}
}

let packet_length = match pdelay_resp_message.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Err(error) => {
Expand All @@ -235,7 +290,7 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R
requestor_identity: PortIdentity,
timestamp: Time,
) -> PortActionIterator {
let pdelay_resp_follow_up_messgae = self.instance_state.with_ref(|state| {
let mut pdelay_resp_follow_up_messgae = self.instance_state.with_ref(|state| {
Message::pdelay_resp_follow_up(
&state.default_ds,
self.port_identity,
Expand All @@ -245,6 +300,16 @@ impl<'a, A, C, F: Filter, R, P, S: PtpInstanceStateMutex> Port<'a, Running, A, R
)
});

let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if pdelay_resp_follow_up_messgae
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign pdelay response follow up message, sending unsigned");
}
}

let packet_length = match pdelay_resp_follow_up_messgae.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Err(error) => {
Expand Down
1 change: 1 addition & 0 deletions statime/src/port/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ impl<
}
}

#[allow(clippy::type_complexity)]
impl<'a, A, C, F: Filter, R, P, S> Port<'a, InBmca, A, R, C, F, P, S> {
/// End a BMCA cycle and make the
/// [`handle_*`](`Port::handle_send_timestamp`) methods available again
Expand Down
39 changes: 34 additions & 5 deletions statime/src/port/slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ use super::{
};
use crate::{
config::DelayMechanism,
crypto::SecurityAssociationProvider,
datastructures::messages::{
DelayRespMessage, FollowUpMessage, Header, Message, PDelayRespFollowUpMessage,
PDelayRespMessage, SyncMessage,
},
filters::Filter,
port::{actions::TimestampContextInner, state::SyncState, PortAction, TimestampContext},
port::{
actions::TimestampContextInner, state::SyncState, PortAction, TimestampContext,
MAX_DATA_LEN,
},
ptp_instance::PtpInstanceStateMutex,
time::{Duration, Interval, Time},
Clock,
Expand Down Expand Up @@ -456,8 +460,15 @@ impl<'a, A, C: Clock, F: Filter, R, P, S> Port<'a, Running, A, R, C, F, P, S> {
}
}

impl<'a, A, C: Clock, F: Filter, R: Rng, P, S: PtpInstanceStateMutex>
Port<'a, Running, A, R, C, F, P, S>
impl<
'a,
A,
C: Clock,
F: Filter,
R: Rng,
P: SecurityAssociationProvider,
S: PtpInstanceStateMutex,
> Port<'a, Running, A, R, C, F, P, S>
{
pub(super) fn send_delay_request(&mut self) -> PortActionIterator {
match self.config.delay_mechanism {
Expand All @@ -472,9 +483,18 @@ impl<'a, A, C: Clock, F: Filter, R: Rng, P, S: PtpInstanceStateMutex>
) -> PortActionIterator {
let pdelay_id = self.pdelay_seq_ids.generate();

let pdelay_req = self.instance_state.with_ref(|state| {
let mut pdelay_req = self.instance_state.with_ref(|state| {
Message::pdelay_req(&state.default_ds, self.port_identity, pdelay_id)
});
let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if pdelay_req
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign delay request, sending without signature");
}
}
let message_length = match pdelay_req.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Err(error) => {
Expand Down Expand Up @@ -519,9 +539,18 @@ impl<'a, A, C: Clock, F: Filter, R: Rng, P, S: PtpInstanceStateMutex>
log::debug!("Starting new delay measurement");

let delay_id = self.delay_seq_ids.generate();
let delay_req = self.instance_state.with_ref(|state| {
let mut delay_req = self.instance_state.with_ref(|state| {
Message::delay_req(&state.default_ds, self.port_identity, delay_id)
});
let mut temp_buffer = [0; MAX_DATA_LEN];
if let Some(spp) = self.config.spp {
if delay_req
.add_signature(spp, &self.security_provider, &mut temp_buffer)
.is_err()
{
log::error!("Could not sign delay request, sending without signature");
}
}

let message_length = match delay_req.serialize(&mut self.packet_buffer) {
Ok(length) => length,
Expand Down

0 comments on commit fff98b4

Please sign in to comment.