-
Notifications
You must be signed in to change notification settings - Fork 55
/
signed_envelope.nim
162 lines (130 loc) · 4.34 KB
/
signed_envelope.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Nim-Libp2p
# Copyright (c) 2023 Status Research & Development GmbH
# 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.
## This module implements Signed Envelope.
{.push raises: [].}
import std/sugar
import pkg/stew/[results, byteutils]
import multicodec, crypto/crypto, protobuf/minprotobuf, vbuffer
export crypto
type
EnvelopeError* = enum
EnvelopeInvalidProtobuf
EnvelopeFieldMissing
EnvelopeInvalidSignature
EnvelopeWrongType
Envelope* = object
publicKey*: PublicKey
domain*: string
payloadType*: seq[byte]
payload: seq[byte]
signature*: Signature
proc mapProtobufError(e: ProtoError): EnvelopeError =
case e
of RequiredFieldMissing: EnvelopeFieldMissing
else: EnvelopeInvalidProtobuf
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)
buffer.buffer
proc decode*(
T: typedesc[Envelope], buf: seq[byte], domain: string
): Result[Envelope, EnvelopeError] =
let pb = initProtoBuffer(buf)
var envelope = Envelope()
envelope.domain = domain
?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)
if envelope.signature.verify(envelope.getSignatureBuffer(), envelope.publicKey) ==
false:
err(EnvelopeInvalidSignature)
else:
ok(envelope)
proc init*(
T: typedesc[Envelope],
privateKey: PrivateKey,
payloadType: seq[byte],
payload: seq[byte],
domain: string,
): Result[Envelope, CryptoError] =
var envelope = Envelope(
publicKey: ?privateKey.getPublicKey(),
domain: domain,
payloadType: payloadType,
payload: payload,
)
envelope.signature = ?privateKey.sign(envelope.getSignatureBuffer())
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
proc getField*(
pb: ProtoBuffer, field: int, value: var Envelope, domain: string
): ProtoResult[bool] {.inline.} =
var buffer: seq[byte]
let res = ?pb.getField(field, buffer)
if not (res):
ok(false)
else:
value = Envelope.decode(buffer, domain).valueOr:
return err(ProtoError.IncorrectBlob)
ok(true)
proc write*(pb: var ProtoBuffer, field: int, env: Envelope): Result[void, CryptoError] =
let e = ?env.encode()
pb.write(field, e)
ok()
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] =
mixin encode
let envelope =
?Envelope.init(privateKey, T.payloadType(), data.encode(), T.payloadDomain)
ok(SignedPayload[T](data: data, envelope: envelope))
proc getField*[T](
pb: ProtoBuffer, field: int, value: var SignedPayload[T]
): ProtoResult[bool] {.inline.} =
if not ?getField(pb, field, value.envelope, T.payloadDomain):
ok(false)
else:
mixin decode
value.data = ?T.decode(value.envelope.payload).mapErr(x => ProtoError.IncorrectBlob)
ok(true)
proc decode*[T](
_: typedesc[SignedPayload[T]], buffer: seq[byte]
): Result[SignedPayload[T], EnvelopeError] =
let
envelope = ?Envelope.decode(buffer, T.payloadDomain)
data = ?T.decode(envelope.payload).mapErr(x => EnvelopeInvalidProtobuf)
signedPayload = SignedPayload[T](envelope: envelope, data: data)
if envelope.payloadType != T.payloadType:
return err(EnvelopeWrongType)
when compiles(?signedPayload.checkValid()):
?signedPayload.checkValid()
ok(signedPayload)
proc encode*[T](msg: SignedPayload[T]): Result[seq[byte], CryptoError] =
msg.envelope.encode()