forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_sig_checkers.go
80 lines (62 loc) · 1.97 KB
/
events_sig_checkers.go
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
package weshnet
import (
"github.com/libp2p/go-libp2p/core/crypto"
"google.golang.org/protobuf/proto"
"berty.tech/weshnet/v2/pkg/errcode"
"berty.tech/weshnet/v2/pkg/protocoltypes"
)
type sigChecker func(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message proto.Message) error
func sigCheckerGroupSigned(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, _ proto.Message) error {
pk, err := g.GetPubKey()
if err != nil {
return err
}
ok, err := pk.Verify(metadata.Payload, metadata.Sig)
if err != nil {
return errcode.ErrCode_ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCode_ErrCryptoSignatureVerification
}
return nil
}
type eventDeviceSigned interface {
proto.Message
GetDevicePk() []byte
}
func sigCheckerDeviceSigned(_ *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message proto.Message) error {
msg, ok := message.(eventDeviceSigned)
if !ok {
return errcode.ErrCode_ErrDeserialization
}
devPK, err := crypto.UnmarshalEd25519PublicKey(msg.GetDevicePk())
if err != nil {
return errcode.ErrCode_ErrDeserialization.Wrap(err)
}
ok, err = devPK.Verify(metadata.Payload, metadata.Sig)
if err != nil {
return errcode.ErrCode_ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCode_ErrCryptoSignatureVerification
}
return nil
}
func sigCheckerGroupMemberDeviceAdded(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message proto.Message) error {
msg, ok := message.(*protocoltypes.GroupMemberDeviceAdded)
if !ok {
return errcode.ErrCode_ErrDeserialization
}
memPK, err := crypto.UnmarshalEd25519PublicKey(msg.MemberPk)
if err != nil {
return errcode.ErrCode_ErrDeserialization.Wrap(err)
}
ok, err = memPK.Verify(msg.DevicePk, msg.MemberSig)
if err != nil {
return errcode.ErrCode_ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCode_ErrCryptoSignatureVerification
}
return sigCheckerDeviceSigned(g, metadata, message)
}