2023-04-05 17:03:29 -04:00
package relay
import (
"context"
"crypto/ecdsa"
2023-05-22 17:03:40 -04:00
"crypto/elliptic"
2023-05-04 11:56:56 -04:00
"encoding/binary"
2023-05-22 17:03:40 -04:00
"encoding/hex"
2023-05-04 11:56:56 -04:00
"time"
2023-04-05 17:03:29 -04:00
2023-05-03 11:59:47 -04:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
2023-04-05 17:03:29 -04:00
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/waku-org/go-waku/waku/v2/hash"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
2023-05-04 11:56:56 -04:00
"github.com/waku-org/go-waku/waku/v2/timesource"
2023-04-05 17:03:29 -04:00
"go.uber.org/zap"
proto "google.golang.org/protobuf/proto"
)
2023-07-19 12:25:35 -04:00
func msgHash ( pubSubTopic string , msg * pb . WakuMessage ) [ ] byte {
2023-05-04 11:56:56 -04:00
timestampBytes := make ( [ ] byte , 8 )
binary . LittleEndian . PutUint64 ( timestampBytes , uint64 ( msg . Timestamp ) )
var ephemeralByte byte
if msg . Ephemeral {
ephemeralByte = 1
}
return hash . SHA256 (
[ ] byte ( pubSubTopic ) ,
msg . Payload ,
[ ] byte ( msg . ContentTopic ) ,
timestampBytes ,
[ ] byte { ephemeralByte } ,
)
}
2023-07-19 12:25:35 -04:00
const messageWindowDuration = time . Minute * 5
2023-05-04 11:56:56 -04:00
func withinTimeWindow ( t timesource . Timesource , msg * pb . WakuMessage ) bool {
if msg . Timestamp == 0 {
return false
}
now := t . Now ( )
msgTime := time . Unix ( 0 , msg . Timestamp )
2023-07-19 12:25:35 -04:00
return now . Sub ( msgTime ) . Abs ( ) <= messageWindowDuration
2023-04-05 17:03:29 -04:00
}
2023-05-02 11:10:45 -04:00
type validatorFn = func ( ctx context . Context , peerID peer . ID , message * pubsub . Message ) bool
2023-05-26 10:42:25 -04:00
func validatorFnBuilder ( t timesource . Timesource , topic string , publicKey * ecdsa . PublicKey ) ( validatorFn , error ) {
2023-05-26 11:18:00 -04:00
publicKeyBytes := crypto . FromECDSAPub ( publicKey )
2023-05-02 11:10:45 -04:00
return func ( ctx context . Context , peerID peer . ID , message * pubsub . Message ) bool {
2023-04-05 17:03:29 -04:00
msg := new ( pb . WakuMessage )
err := proto . Unmarshal ( message . Data , msg )
if err != nil {
return false
}
2023-05-04 11:56:56 -04:00
if ! withinTimeWindow ( t , msg ) {
return false
}
2023-07-19 12:25:35 -04:00
msgHash := msgHash ( topic , msg )
2023-04-05 17:03:29 -04:00
signature := msg . Meta
2023-05-26 11:18:00 -04:00
return secp256k1 . VerifySignature ( publicKeyBytes , msgHash , signature )
2023-05-09 10:48:55 -04:00
} , nil
2023-05-02 11:10:45 -04:00
}
2023-07-19 12:25:35 -04:00
// AddSignedTopicValidator registers a gossipsub validator for a topic which will check that messages Meta field contains a valid ECDSA signature for the specified pubsub topic. This is used as a DoS prevention mechanism
2023-05-22 17:03:40 -04:00
func ( w * WakuRelay ) AddSignedTopicValidator ( topic string , publicKey * ecdsa . PublicKey ) error {
w . log . Info ( "adding validator to signed topic" , zap . String ( "topic" , topic ) , zap . String ( "publicKey" , hex . EncodeToString ( elliptic . Marshal ( publicKey . Curve , publicKey . X , publicKey . Y ) ) ) )
2023-05-09 10:48:55 -04:00
2023-05-26 10:42:25 -04:00
fn , err := validatorFnBuilder ( w . timesource , topic , publicKey )
2023-05-09 10:48:55 -04:00
if err != nil {
return err
}
err = w . pubsub . RegisterTopicValidator ( topic , fn )
2023-05-04 10:04:54 -04:00
if err != nil {
return err
}
if ! w . IsSubscribed ( topic ) {
w . log . Warn ( "relay is not subscribed to signed topic" , zap . String ( "topic" , topic ) )
}
return nil
2023-04-05 17:03:29 -04:00
}
2023-07-19 12:25:35 -04:00
// SignMessage adds an ECDSA signature to a WakuMessage as an opt-in mechanism for DoS prevention
2023-05-26 10:42:25 -04:00
func SignMessage ( privKey * ecdsa . PrivateKey , msg * pb . WakuMessage , pubsubTopic string ) error {
2023-07-19 12:25:35 -04:00
msgHash := msgHash ( pubsubTopic , msg )
2023-05-03 11:59:47 -04:00
sign , err := secp256k1 . Sign ( msgHash , crypto . FromECDSA ( privKey ) )
2023-04-05 17:03:29 -04:00
if err != nil {
return err
}
2023-05-26 11:18:00 -04:00
msg . Meta = sign [ 0 : 64 ] // Remove V
2023-04-05 17:03:29 -04:00
return nil
}