2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2022-08-03 11:33:19 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-09-28 07:11:18 +00:00
|
|
|
import hashes
|
2020-07-16 10:06:57 +00:00
|
|
|
import chronicles, metrics, stew/[byteutils, endians2]
|
2020-08-12 00:05:49 +00:00
|
|
|
import ./messages,
|
|
|
|
./protobuf,
|
2020-07-01 06:25:09 +00:00
|
|
|
../../../peerid,
|
2019-12-07 16:36:39 +00:00
|
|
|
../../../peerinfo,
|
2019-12-06 02:16:18 +00:00
|
|
|
../../../crypto/crypto,
|
2022-02-21 15:04:17 +00:00
|
|
|
../../../protobuf/minprotobuf,
|
|
|
|
../../../protocols/pubsub/errors
|
2020-07-16 10:06:57 +00:00
|
|
|
|
2022-02-21 15:04:17 +00:00
|
|
|
export errors, messages
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "pubsubmessage"
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
const PubSubPrefix = toBytes("libp2p-pubsub:")
|
2019-12-07 16:36:39 +00:00
|
|
|
|
2020-06-07 07:41:23 +00:00
|
|
|
declareCounter(libp2p_pubsub_sig_verify_success, "pubsub successfully validated messages")
|
|
|
|
declareCounter(libp2p_pubsub_sig_verify_failure, "pubsub failed validated messages")
|
2020-06-07 07:15:21 +00:00
|
|
|
|
2022-07-27 11:47:50 +00:00
|
|
|
func defaultMsgIdProvider*(m: Message): Result[MessageId, ValidationResult] =
|
2022-02-21 15:04:17 +00:00
|
|
|
if m.seqno.len > 0 and m.fromPeer.data.len > 0:
|
|
|
|
let mid = byteutils.toHex(m.seqno) & $m.fromPeer
|
|
|
|
ok mid.toBytes()
|
|
|
|
else:
|
|
|
|
err ValidationResult.Reject
|
2020-05-27 18:33:49 +00:00
|
|
|
|
2020-09-04 16:31:43 +00:00
|
|
|
proc sign*(msg: Message, privateKey: PrivateKey): CryptoResult[seq[byte]] =
|
2020-09-25 16:39:34 +00:00
|
|
|
ok((? privateKey.sign(PubSubPrefix & encodeMessage(msg, false))).getBytes())
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
proc verify*(m: Message): bool =
|
2019-12-06 02:16:18 +00:00
|
|
|
if m.signature.len > 0 and m.key.len > 0:
|
|
|
|
var msg = m
|
|
|
|
msg.signature = @[]
|
|
|
|
msg.key = @[]
|
|
|
|
|
|
|
|
var remote: Signature
|
|
|
|
var key: PublicKey
|
|
|
|
if remote.init(m.signature) and key.init(m.key):
|
2019-12-17 05:24:03 +00:00
|
|
|
trace "verifying signature", remoteSignature = remote
|
2020-09-25 16:39:34 +00:00
|
|
|
result = remote.verify(PubSubPrefix & encodeMessage(msg, false), key)
|
2020-06-28 15:56:38 +00:00
|
|
|
|
2020-06-07 07:15:21 +00:00
|
|
|
if result:
|
|
|
|
libp2p_pubsub_sig_verify_success.inc()
|
|
|
|
else:
|
|
|
|
libp2p_pubsub_sig_verify_failure.inc()
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-06-28 15:56:38 +00:00
|
|
|
proc init*(
|
|
|
|
T: type Message,
|
2020-09-23 15:56:33 +00:00
|
|
|
peer: Option[PeerInfo],
|
2020-06-28 15:56:38 +00:00
|
|
|
data: seq[byte],
|
|
|
|
topic: string,
|
2020-09-23 15:56:33 +00:00
|
|
|
seqno: Option[uint64],
|
2021-06-02 13:39:10 +00:00
|
|
|
sign: bool = true): Message
|
|
|
|
{.gcsafe, raises: [Defect, LPError].} =
|
2020-09-23 15:56:33 +00:00
|
|
|
var msg = Message(data: data, topicIDs: @[topic])
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
# order matters, we want to include seqno in the signature
|
2020-09-25 16:39:34 +00:00
|
|
|
if seqno.isSome:
|
2020-09-23 15:56:33 +00:00
|
|
|
msg.seqno = @(seqno.get().toBytesBE())
|
2020-09-04 16:31:43 +00:00
|
|
|
|
2020-09-23 15:56:33 +00:00
|
|
|
if peer.isSome:
|
|
|
|
let peer = peer.get()
|
|
|
|
msg.fromPeer = peer.peerId
|
|
|
|
if sign:
|
2021-06-02 13:39:10 +00:00
|
|
|
msg.signature = sign(msg, peer.privateKey).expect("Couldn't sign message!")
|
2021-09-02 10:03:40 +00:00
|
|
|
msg.key = peer.privateKey.getPublicKey().expect("Invalid private key!")
|
|
|
|
.getBytes().expect("Couldn't get public key bytes!")
|
2020-09-23 15:56:33 +00:00
|
|
|
elif sign:
|
2021-06-02 13:39:10 +00:00
|
|
|
raise (ref LPError)(msg: "Cannot sign message without peer info")
|
2020-09-23 15:56:33 +00:00
|
|
|
|
|
|
|
msg
|
2021-09-08 09:07:46 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type Message,
|
|
|
|
peerId: PeerId,
|
|
|
|
data: seq[byte],
|
|
|
|
topic: string,
|
|
|
|
seqno: Option[uint64]): Message
|
|
|
|
{.gcsafe, raises: [Defect, LPError].} =
|
|
|
|
var msg = Message(data: data, topicIDs: @[topic])
|
|
|
|
msg.fromPeer = peerId
|
|
|
|
|
|
|
|
if seqno.isSome:
|
|
|
|
msg.seqno = @(seqno.get().toBytesBE())
|
|
|
|
msg
|