Skip to content

Commit c8766d0

Browse files
committed
fix(p2p/pubsub): verify signature
1 parent 29cdba5 commit c8766d0

File tree

13 files changed

+219
-137
lines changed

13 files changed

+219
-137
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tracing-wasm = "0.2"
3232
[target.'cfg(not(target_family = "wasm"))'.dependencies]
3333
redux = { workspace = true, features=["serializable_callbacks"] }
3434
tracing-subscriber = { version = "0.3.17", features = ["json", "env-filter"] }
35+
libp2p-identity = { version = "=0.2.7", features = ["ed25519", "rand", "serde"] }
3536

3637
[features]
3738
p2p-webrtc = ["node/p2p-webrtc"]

node/common/src/service/p2p.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,13 @@ impl P2pCryptoService for NodeService {
8787
.sign(&msg)
8888
.expect("unable to create signature")
8989
}
90+
91+
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool {
92+
let Ok(pk) = libp2p_identity::PublicKey::try_decode_protobuf(pk) else {
93+
return false;
94+
};
95+
println!("pk {:?}", pk);
96+
let msg: Vec<u8> = [b"libp2p-pubsub:", publication].concat();
97+
pk.verify(&msg, sig)
98+
}
9099
}

node/src/action_kind.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ pub enum ActionKind {
307307
P2pNetworkPubsubBroadcastSigned,
308308
P2pNetworkPubsubGraft,
309309
P2pNetworkPubsubIncomingData,
310+
P2pNetworkPubsubIncomingMessage,
310311
P2pNetworkPubsubNewStream,
311312
P2pNetworkPubsubOutgoingData,
312313
P2pNetworkPubsubOutgoingMessage,
@@ -540,7 +541,7 @@ pub enum ActionKind {
540541
}
541542

542543
impl ActionKind {
543-
pub const COUNT: u16 = 448;
544+
pub const COUNT: u16 = 449;
544545
}
545546

546547
impl std::fmt::Display for ActionKind {
@@ -1464,6 +1465,7 @@ impl ActionKindGet for P2pNetworkPubsubAction {
14641465
match self {
14651466
Self::NewStream { .. } => ActionKind::P2pNetworkPubsubNewStream,
14661467
Self::IncomingData { .. } => ActionKind::P2pNetworkPubsubIncomingData,
1468+
Self::IncomingMessage { .. } => ActionKind::P2pNetworkPubsubIncomingMessage,
14671469
Self::Graft { .. } => ActionKind::P2pNetworkPubsubGraft,
14681470
Self::Prune { .. } => ActionKind::P2pNetworkPubsubPrune,
14691471
Self::Broadcast { .. } => ActionKind::P2pNetworkPubsubBroadcast,

node/testing/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ hex = "0.4.3"
5555

5656
[target.'cfg(not(target_family = "wasm"))'.dependencies]
5757
redux = { workspace = true, features=["serializable_callbacks"] }
58+
libp2p-identity = { version = "=0.2.7", features = ["ed25519", "rand", "serde"] }
5859

5960
[features]
6061
default = ["p2p-libp2p", "scenario-generators"]

node/testing/src/service/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ impl P2pCryptoService for NodeTestingService {
296296
fn sign_publication(&mut self, publication: &[u8]) -> Vec<u8> {
297297
self.real.sign_publication(publication)
298298
}
299+
300+
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool {
301+
self.real.verify_publication(pk, publication, sig)
302+
}
299303
}
300304

301305
impl node::ledger::LedgerService for NodeTestingService {

p2p/src/network/p2p_network_service.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub trait P2pCryptoService: redux::Service {
3636
fn sign_key(&mut self, key: &[u8; 32]) -> Vec<u8>;
3737

3838
fn sign_publication(&mut self, publication: &[u8]) -> Vec<u8>;
39+
fn verify_publication(&mut self, pk: &[u8], publication: &[u8], sig: &[u8]) -> bool;
3940
}
4041

4142
#[derive(Debug, thiserror::Error)]

p2p/src/network/pubsub/p2p_network_pubsub_actions.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ pub enum P2pNetworkPubsubAction {
2020
data: Data,
2121
seen_limit: usize,
2222
},
23+
IncomingMessage {
24+
peer_id: PeerId,
25+
message: pb::Message,
26+
seen_limit: usize,
27+
},
2328
Graft {
2429
peer_id: PeerId,
2530
topic_id: String,

p2p/src/network/pubsub/p2p_network_pubsub_effects.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,42 @@ impl P2pNetworkPubsubAction {
125125
}
126126
}
127127
P2pNetworkPubsubAction::BroadcastSigned { .. } => broadcast(store),
128-
P2pNetworkPubsubAction::IncomingData { peer_id, .. } => {
128+
P2pNetworkPubsubAction::IncomingData {
129+
peer_id,
130+
seen_limit,
131+
..
132+
} => {
133+
let Some(state) = state.clients.get(&peer_id) else {
134+
return;
135+
};
136+
let messages = state.incoming_messages.clone();
137+
138+
for mut message in messages {
139+
if let (Some(signature), Some(from)) =
140+
(message.signature.take(), message.from.clone())
141+
{
142+
message.key = None;
143+
let mut data = vec![];
144+
if prost::Message::encode(&message, &mut data).is_err() {
145+
continue;
146+
} else if !store
147+
.service()
148+
.verify_publication(&from[2..], &data, &signature)
149+
{
150+
continue;
151+
}
152+
} else {
153+
// the message doesn't contain signature or it doesn't contain verifying key
154+
continue;
155+
}
156+
store.dispatch(P2pNetworkPubsubAction::IncomingMessage {
157+
peer_id,
158+
message,
159+
seen_limit,
160+
});
161+
}
162+
}
163+
P2pNetworkPubsubAction::IncomingMessage { peer_id, .. } => {
129164
// println!("(pubsub) {this} <- {peer_id}");
130165

131166
let incoming_block = state.incoming_block.as_ref().cloned();

0 commit comments

Comments
 (0)