Skip to content

Commit 49182bc

Browse files
aviveStebalien
authored andcommitted
Don't rely on local peer store for remote nodes public keys
1 parent efdab23 commit 49182bc

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<h1 align="center">
23
<a href="libp2p.io"><img width="250" src="https://github.com/libp2p/libp2p/blob/master/logo/alternates/libp2p-logo-alt-2.png?raw=true" alt="libp2p hex logo" /></a>
34
</h1>

examples/multipro/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Juan Batiz-Benet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

examples/multipro/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ From `multipro` base source folder:
3030
## Details
3131

3232
The example creates two LibP2P Hosts supporting 2 protocols: ping and echo.
33+
3334
Each protocol consists RPC-style requests and respones and each request and response is a typed protobufs message (and a go data object).
35+
3436
This is a different pattern then defining a whole p2p protocol as 1 protobuf message with lots of optional fields (as can be observed in various p2p-lib protocols using protobufs such as dht).
37+
3538
The example shows how to match async received responses with their requests. This is useful when processing a response requires access to the request data.
3639

40+
## Author
41+
42+
@avive
3743

examples/multipro/node.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"github.com/gogo/protobuf/proto"
66
host "gx/ipfs/QmRS46AyqtpJBsf1zmQdeizSDEzo1qkWR7rdEuPFAv8237/go-libp2p-host"
77
peer "gx/ipfs/QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB/go-libp2p-peer"
8+
9+
crypto "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
810
"log"
911
)
1012

@@ -46,7 +48,7 @@ func (n *Node) authenticateMessage(message proto.Message, data *p2p.MessageData)
4648
return false
4749
}
4850

49-
return n.verifyData(bin, []byte(sign), peerId, []byte(data.NodePubKey))
51+
return n.verifyData(bin, []byte(sign), peerId, data.NodePubKey)
5052
}
5153

5254
func (n *Node) signProtoMessage(message proto.Message) ([]byte, error) {
@@ -66,20 +68,31 @@ func (n *Node) signData(data []byte) ([]byte, error) {
6668
// precondition: we have info about the signer peer in the local peer store
6769
func (n *Node) verifyData(data []byte, signature []byte, peerId peer.ID, pubKeyData []byte) bool {
6870

69-
// todo: restore pub key from message and not from the local peer store and use it
70-
key := n.Peerstore().PubKey(peerId)
71+
key, err := crypto.UnmarshalPublicKey(pubKeyData)
72+
73+
if err != nil {
74+
log.Println(err, "Failed to extract key from message key data")
75+
return false
76+
}
77+
78+
// extract node id from the provided public key
79+
idFromKey, err := peer.IDFromPublicKey(key)
7180

72-
//todo: fix this
73-
//key, err := key.UnmarshalPublicKey(pubKeyData)
81+
if err != nil {
82+
log.Println(err, "Failed to extract peer id from public key")
83+
return false
84+
}
7485

75-
if key == nil {
76-
log.Println("Failed to find public key for %s in local peer store.", peerId.String())
86+
// verify that message author node id matches the provided public key
87+
if idFromKey != peerId {
88+
log.Println(err, "Node id and provided public key mismatch")
7789
return false
7890
}
7991

8092
res, err := key.Verify(data, signature)
93+
8194
if err != nil {
82-
log.Println ("Error authenticating data")
95+
log.Println(err, "Error authenticating data")
8396
return false
8497
}
8598

examples/multipro/protocol.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ func sendProtoMessage(data proto.Message, s inet.Stream) bool {
3333
// messageId - unique for requests, copied from request for responses
3434
func NewMessageData(node *Node, messageId string, gossip bool) *p2p.MessageData {
3535

36-
// Create protobufs bin data for a public key
36+
// Add protobufs bin data for message author public key
37+
// this is useful for authenticating messages forwarded by a node authored by another node
3738
nodePubKey, err := node.Peerstore().PubKey(node.ID()).Bytes()
3839

3940
if err != nil {

0 commit comments

Comments
 (0)