-
Notifications
You must be signed in to change notification settings - Fork 28
/
events_sig_checkers.go
80 lines (62 loc) · 1.89 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/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p/core/crypto"
"berty.tech/weshnet/pkg/errcode"
"berty.tech/weshnet/pkg/protocoltypes"
)
type sigChecker func(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message proto.Message) error
func sigCheckerGroupSigned(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message 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.ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCryptoSignatureVerification
}
return nil
}
type eventDeviceSigned interface {
proto.Message
GetDevicePK() []byte
}
func sigCheckerDeviceSigned(g *protocoltypes.Group, metadata *protocoltypes.GroupMetadata, message proto.Message) error {
msg, ok := message.(eventDeviceSigned)
if !ok {
return errcode.ErrDeserialization
}
devPK, err := crypto.UnmarshalEd25519PublicKey(msg.GetDevicePK())
if err != nil {
return errcode.ErrDeserialization.Wrap(err)
}
ok, err = devPK.Verify(metadata.Payload, metadata.Sig)
if err != nil {
return errcode.ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return 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.ErrDeserialization
}
memPK, err := crypto.UnmarshalEd25519PublicKey(msg.MemberPK)
if err != nil {
return errcode.ErrDeserialization.Wrap(err)
}
ok, err = memPK.Verify(msg.DevicePK, msg.MemberSig)
if err != nil {
return errcode.ErrCryptoSignatureVerification.Wrap(err)
}
if !ok {
return errcode.ErrCryptoSignatureVerification
}
return sigCheckerDeviceSigned(g, metadata, message)
}