2023-04-04 08:58:45 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import
|
2023-05-05 08:12:49 +00:00
|
|
|
std/math,
|
2023-04-04 08:58:45 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2023-05-02 14:20:38 +00:00
|
|
|
metrics,
|
2023-04-04 08:58:45 +00:00
|
|
|
stew/byteutils,
|
2023-05-05 08:12:49 +00:00
|
|
|
stew/endians2,
|
2023-04-04 08:58:45 +00:00
|
|
|
libp2p/protocols/pubsub/gossipsub,
|
|
|
|
libp2p/protocols/pubsub/rpc/messages,
|
|
|
|
libp2p/protocols/pubsub/errors,
|
|
|
|
nimcrypto/sha2,
|
|
|
|
secp256k1
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
const MessageWindowInSec = 5 * 60 # +- 5 minutes
|
2023-05-05 08:12:49 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import ./external_config, ../waku_relay/protocol, ../waku_core
|
2023-05-02 14:20:38 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
declarePublicCounter waku_msg_validator_signed_outcome,
|
|
|
|
"number of messages for each validation outcome", ["result"]
|
2023-04-04 08:58:45 +00:00
|
|
|
|
2023-05-04 13:38:52 +00:00
|
|
|
# Application level message hash
|
2023-04-04 08:58:45 +00:00
|
|
|
proc msgHash*(pubSubTopic: string, msg: WakuMessage): array[32, byte] =
|
|
|
|
var ctx: sha256
|
|
|
|
ctx.init()
|
2024-03-15 23:08:47 +00:00
|
|
|
defer:
|
|
|
|
ctx.clear()
|
2023-04-04 08:58:45 +00:00
|
|
|
|
|
|
|
ctx.update(pubsubTopic.toBytes())
|
|
|
|
ctx.update(msg.payload)
|
|
|
|
ctx.update(msg.contentTopic.toBytes())
|
2023-05-05 08:12:49 +00:00
|
|
|
ctx.update(msg.timestamp.uint64.toBytes(Endianness.littleEndian))
|
2024-03-15 23:08:47 +00:00
|
|
|
ctx.update(
|
|
|
|
if msg.ephemeral:
|
|
|
|
@[1.byte]
|
|
|
|
else:
|
|
|
|
@[0.byte]
|
|
|
|
)
|
2023-04-04 08:58:45 +00:00
|
|
|
|
|
|
|
return ctx.finish()
|
|
|
|
|
2023-05-05 08:12:49 +00:00
|
|
|
proc withinTimeWindow*(msg: WakuMessage): bool =
|
|
|
|
# Returns true if the message timestamp is:
|
|
|
|
# abs(now - msg.timestamp) < MessageWindowInSec
|
|
|
|
let ts = msg.timestamp
|
|
|
|
let now = getNowInNanosecondTime()
|
|
|
|
let window = getNanosecondTime(MessageWindowInSec)
|
|
|
|
|
|
|
|
if abs(now - ts) < window:
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
2024-02-01 17:16:10 +00:00
|
|
|
proc addSignedTopicsValidator*(w: WakuRelay, protectedTopics: seq[ProtectedTopic]) =
|
|
|
|
debug "adding validator to signed topics"
|
2023-04-04 08:58:45 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
proc validator(
|
|
|
|
topic: string, msg: WakuMessage
|
|
|
|
): Future[errors.ValidationResult] {.async.} =
|
2023-05-02 14:20:38 +00:00
|
|
|
var outcome = errors.ValidationResult.Reject
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
for protectedTopic in protectedTopics:
|
|
|
|
if (protectedTopic.topic == topic):
|
2024-02-01 17:16:10 +00:00
|
|
|
if msg.timestamp != 0:
|
|
|
|
if msg.withinTimeWindow():
|
|
|
|
let msgHash = SkMessage(topic.msgHash(msg))
|
|
|
|
let recoveredSignature = SkSignature.fromRaw(msg.meta)
|
|
|
|
if recoveredSignature.isOk():
|
|
|
|
if recoveredSignature.get.verify(msgHash, protectedTopic.key):
|
|
|
|
outcome = errors.ValidationResult.Accept
|
2023-05-02 14:20:38 +00:00
|
|
|
|
2024-02-01 17:16:10 +00:00
|
|
|
if outcome != errors.ValidationResult.Accept:
|
2024-03-15 23:08:47 +00:00
|
|
|
debug "signed topic validation failed",
|
|
|
|
topic = topic, publicTopicKey = protectedTopic.key
|
2024-02-01 17:16:10 +00:00
|
|
|
waku_msg_validator_signed_outcome.inc(labelValues = [$outcome])
|
|
|
|
return outcome
|
2023-04-04 08:58:45 +00:00
|
|
|
|
2024-02-01 17:16:10 +00:00
|
|
|
return errors.ValidationResult.Accept
|
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
w.addValidator(validator, "signed topic validation failed")
|