forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_sig_checkers.go
79 lines (62 loc) · 1.87 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
package bertyprotocol
import (
"berty.tech/berty/v2/go/pkg/bertytypes"
"berty.tech/berty/v2/go/pkg/errcode"
"github.com/gogo/protobuf/proto"
"github.com/libp2p/go-libp2p-core/crypto"
)
type sigChecker func(g *bertytypes.Group, metadata *bertytypes.GroupMetadata, message proto.Message) error
func sigCheckerGroupSigned(g *bertytypes.Group, metadata *bertytypes.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 *bertytypes.Group, metadata *bertytypes.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 sigCheckerMemberDeviceAdded(g *bertytypes.Group, metadata *bertytypes.GroupMetadata, message proto.Message) error {
msg, ok := message.(*bertytypes.GroupAddMemberDevice)
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)
}