-
Notifications
You must be signed in to change notification settings - Fork 38
/
sign.go
54 lines (44 loc) · 1.17 KB
/
sign.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
package control
import (
"bytes"
"crypto/ecdsa"
"errors"
control "github.com/nspcc-dev/neofs-node/pkg/services/control/ir"
"github.com/nspcc-dev/neofs-sdk-go/util/signature"
)
// SignedMessage is an interface of Control service message.
type SignedMessage interface {
signature.DataSource
GetSignature() *control.Signature
SetSignature(*control.Signature)
}
var errDisallowedKey = errors.New("key is not in the allowed list")
func (s *Server) isValidRequest(req SignedMessage) error {
var (
sign = req.GetSignature()
key = sign.GetKey()
allowed = false
)
// check if key is allowed
for i := range s.allowedKeys {
if allowed = bytes.Equal(s.allowedKeys[i], key); allowed {
break
}
}
if !allowed {
return errDisallowedKey
}
// verify signature
return signature.VerifyDataWithSource(req, func() ([]byte, []byte) {
return key, sign.GetSign()
})
}
// SignMessage signs Control service message with private key.
func SignMessage(key *ecdsa.PrivateKey, msg SignedMessage) error {
return signature.SignDataWithHandler(key, msg, func(key []byte, sig []byte) {
s := new(control.Signature)
s.SetKey(key)
s.SetSign(sig)
msg.SetSignature(s)
})
}