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.
|
2021-11-24 20:03:40 +00:00
|
|
|
|
|
|
|
## This module implements Signed Envelope.
|
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-11-24 20:03:40 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
import std/sugar
|
2021-11-24 20:03:40 +00:00
|
|
|
import pkg/stew/[results, byteutils]
|
2024-06-11 15:18:06 +00:00
|
|
|
import multicodec, crypto/crypto, protobuf/minprotobuf, vbuffer
|
2021-11-24 20:03:40 +00:00
|
|
|
|
|
|
|
export crypto
|
|
|
|
|
|
|
|
type
|
|
|
|
EnvelopeError* = enum
|
2024-06-11 15:18:06 +00:00
|
|
|
EnvelopeInvalidProtobuf
|
|
|
|
EnvelopeFieldMissing
|
|
|
|
EnvelopeInvalidSignature
|
2022-03-14 08:39:30 +00:00
|
|
|
EnvelopeWrongType
|
2021-11-24 20:03:40 +00:00
|
|
|
|
|
|
|
Envelope* = object
|
|
|
|
publicKey*: PublicKey
|
|
|
|
domain*: string
|
|
|
|
payloadType*: seq[byte]
|
|
|
|
payload: seq[byte]
|
|
|
|
signature*: Signature
|
|
|
|
|
|
|
|
proc mapProtobufError(e: ProtoError): EnvelopeError =
|
2024-06-11 15:18:06 +00:00
|
|
|
case e
|
|
|
|
of RequiredFieldMissing: EnvelopeFieldMissing
|
|
|
|
else: EnvelopeInvalidProtobuf
|
2021-11-24 20:03:40 +00:00
|
|
|
|
|
|
|
proc getSignatureBuffer(e: Envelope): seq[byte] =
|
|
|
|
var buffer = initVBuffer()
|
|
|
|
|
|
|
|
let domainBytes = e.domain.toBytes()
|
|
|
|
buffer.writeSeq(domainBytes)
|
|
|
|
buffer.writeSeq(e.payloadType)
|
|
|
|
buffer.writeSeq(e.payload)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
buffer.buffer
|
2021-11-24 20:03:40 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc decode*(
|
|
|
|
T: typedesc[Envelope], buf: seq[byte], domain: string
|
|
|
|
): Result[Envelope, EnvelopeError] =
|
2021-11-24 20:03:40 +00:00
|
|
|
let pb = initProtoBuffer(buf)
|
|
|
|
var envelope = Envelope()
|
|
|
|
|
|
|
|
envelope.domain = domain
|
2024-06-11 15:18:06 +00:00
|
|
|
?pb.getRequiredField(1, envelope.publicKey).mapErr(mapProtobufError)
|
|
|
|
discard ?pb.getField(2, envelope.payloadType).mapErr(mapProtobufError)
|
|
|
|
?pb.getRequiredField(3, envelope.payload).mapErr(mapProtobufError)
|
|
|
|
?pb.getRequiredField(5, envelope.signature).mapErr(mapProtobufError)
|
2021-11-24 20:03:40 +00:00
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
if envelope.signature.verify(envelope.getSignatureBuffer(), envelope.publicKey) ==
|
|
|
|
false:
|
2021-11-24 20:03:40 +00:00
|
|
|
err(EnvelopeInvalidSignature)
|
|
|
|
else:
|
|
|
|
ok(envelope)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc init*(
|
|
|
|
T: typedesc[Envelope],
|
2021-11-24 20:03:40 +00:00
|
|
|
privateKey: PrivateKey,
|
|
|
|
payloadType: seq[byte],
|
|
|
|
payload: seq[byte],
|
2024-06-11 15:18:06 +00:00
|
|
|
domain: string,
|
|
|
|
): Result[Envelope, CryptoError] =
|
2021-11-24 20:03:40 +00:00
|
|
|
var envelope = Envelope(
|
2024-06-11 15:18:06 +00:00
|
|
|
publicKey: ?privateKey.getPublicKey(),
|
2021-11-24 20:03:40 +00:00
|
|
|
domain: domain,
|
|
|
|
payloadType: payloadType,
|
|
|
|
payload: payload,
|
|
|
|
)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
envelope.signature = ?privateKey.sign(envelope.getSignatureBuffer())
|
2021-11-24 20:03:40 +00:00
|
|
|
|
|
|
|
ok(envelope)
|
|
|
|
|
|
|
|
proc encode*(env: Envelope): Result[seq[byte], CryptoError] =
|
|
|
|
var pb = initProtoBuffer()
|
|
|
|
|
|
|
|
try:
|
|
|
|
pb.write(1, env.publicKey)
|
|
|
|
pb.write(2, env.payloadType)
|
|
|
|
pb.write(3, env.payload)
|
|
|
|
pb.write(5, env.signature)
|
|
|
|
except ResultError[CryptoError] as exc:
|
|
|
|
return err(exc.error)
|
|
|
|
|
|
|
|
pb.finish()
|
|
|
|
ok(pb.buffer)
|
|
|
|
|
|
|
|
proc payload*(env: Envelope): seq[byte] =
|
|
|
|
# Payload is readonly
|
|
|
|
env.payload
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc getField*(
|
|
|
|
pb: ProtoBuffer, field: int, value: var Envelope, domain: string
|
|
|
|
): ProtoResult[bool] {.inline.} =
|
2021-11-24 20:03:40 +00:00
|
|
|
var buffer: seq[byte]
|
2024-06-11 15:18:06 +00:00
|
|
|
let res = ?pb.getField(field, buffer)
|
|
|
|
if not (res):
|
2021-11-24 20:03:40 +00:00
|
|
|
ok(false)
|
|
|
|
else:
|
2024-06-11 15:18:06 +00:00
|
|
|
value = Envelope.decode(buffer, domain).valueOr:
|
|
|
|
return err(ProtoError.IncorrectBlob)
|
2023-06-28 14:44:58 +00:00
|
|
|
ok(true)
|
2022-03-14 08:39:30 +00:00
|
|
|
|
2022-08-01 12:31:22 +00:00
|
|
|
proc write*(pb: var ProtoBuffer, field: int, env: Envelope): Result[void, CryptoError] =
|
2024-06-11 15:18:06 +00:00
|
|
|
let e = ?env.encode()
|
2023-06-28 14:44:58 +00:00
|
|
|
pb.write(field, e)
|
2022-08-01 12:31:22 +00:00
|
|
|
ok()
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
type SignedPayload*[T] = object
|
|
|
|
# T needs to have .encode(), .decode(), .payloadType(), .domain()
|
|
|
|
envelope*: Envelope
|
|
|
|
data*: T
|
|
|
|
|
|
|
|
proc init*[T](
|
|
|
|
_: typedesc[SignedPayload[T]], privateKey: PrivateKey, data: T
|
|
|
|
): Result[SignedPayload[T], CryptoError] =
|
2022-03-14 08:39:30 +00:00
|
|
|
mixin encode
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
let envelope =
|
|
|
|
?Envelope.init(privateKey, T.payloadType(), data.encode(), T.payloadDomain)
|
2023-06-28 14:44:58 +00:00
|
|
|
|
2022-03-14 08:39:30 +00:00
|
|
|
ok(SignedPayload[T](data: data, envelope: envelope))
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
proc getField*[T](
|
|
|
|
pb: ProtoBuffer, field: int, value: var SignedPayload[T]
|
|
|
|
): ProtoResult[bool] {.inline.} =
|
|
|
|
if not ?getField(pb, field, value.envelope, T.payloadDomain):
|
2022-03-14 08:39:30 +00:00
|
|
|
ok(false)
|
|
|
|
else:
|
|
|
|
mixin decode
|
2024-06-11 15:18:06 +00:00
|
|
|
value.data = ?T.decode(value.envelope.payload).mapErr(x => ProtoError.IncorrectBlob)
|
2022-03-14 08:39:30 +00:00
|
|
|
ok(true)
|
|
|
|
|
|
|
|
proc decode*[T](
|
2024-06-11 15:18:06 +00:00
|
|
|
_: typedesc[SignedPayload[T]], buffer: seq[byte]
|
|
|
|
): Result[SignedPayload[T], EnvelopeError] =
|
2022-03-14 08:39:30 +00:00
|
|
|
let
|
2024-06-11 15:18:06 +00:00
|
|
|
envelope = ?Envelope.decode(buffer, T.payloadDomain)
|
|
|
|
data = ?T.decode(envelope.payload).mapErr(x => EnvelopeInvalidProtobuf)
|
2022-03-14 08:39:30 +00:00
|
|
|
signedPayload = SignedPayload[T](envelope: envelope, data: data)
|
|
|
|
|
|
|
|
if envelope.payloadType != T.payloadType:
|
|
|
|
return err(EnvelopeWrongType)
|
|
|
|
|
2024-06-11 15:18:06 +00:00
|
|
|
when compiles(?signedPayload.checkValid()):
|
|
|
|
?signedPayload.checkValid()
|
2022-03-14 08:39:30 +00:00
|
|
|
|
|
|
|
ok(signedPayload)
|
|
|
|
|
|
|
|
proc encode*[T](msg: SignedPayload[T]): Result[seq[byte], CryptoError] =
|
|
|
|
msg.envelope.encode()
|