-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate.go
262 lines (226 loc) · 8.22 KB
/
certificate.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
// Copyright 2019 Insolar Technologies GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package certificate
import (
"crypto"
"encoding/json"
"io"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/insolar/insolar/insolar"
"github.com/insolar/insolar/log"
"github.com/insolar/insolar/platformpolicy"
"github.com/pkg/errors"
)
// BootstrapNode holds info about bootstrap nodes
type BootstrapNode struct {
PublicKey string `json:"public_key"`
Host string `json:"host"`
NetworkSign []byte `json:"network_sign"`
NodeSign []byte `json:"node_sign"`
NodeRef string `json:"node_ref"`
NodeRole string `json:"node_role"`
// preprocessed fields
nodePublicKey crypto.PublicKey
}
func NewBootstrapNode(pubKey crypto.PublicKey, publicKey, host, noderef, role string) *BootstrapNode {
return &BootstrapNode{
PublicKey: publicKey,
Host: host,
NodeRef: noderef,
nodePublicKey: pubKey,
NodeRole: role,
}
}
// GetNodeRef returns reference of bootstrap node
func (bn *BootstrapNode) GetNodeRef() *insolar.Reference {
ref, err := insolar.NewReferenceFromBase58(bn.NodeRef)
if err != nil {
log.Errorf("Invalid bootstrap node reference: %s\n", bn.NodeRef)
return nil
}
return ref
}
// GetPublicKey returns public key reference of bootstrap node
func (bn *BootstrapNode) GetPublicKey() crypto.PublicKey {
return bn.nodePublicKey
}
// GetHost returns host of bootstrap node
func (bn *BootstrapNode) GetHost() string {
return bn.Host
}
// GetRole returns role of bootstrap node
func (bn *BootstrapNode) GetRole() insolar.StaticRole {
return insolar.GetStaticRoleFromString(bn.NodeRole)
}
// NodeSign returns signed information about some node
func (bn *BootstrapNode) GetNodeSign() []byte {
return bn.NodeSign
}
var scheme = platformpolicy.NewPlatformCryptographyScheme()
// Certificate holds info about certificate
type Certificate struct {
AuthorizationCertificate
MajorityRule int `json:"majority_rule"`
MinRoles struct {
Virtual uint `json:"virtual"`
HeavyMaterial uint `json:"heavy_material"`
LightMaterial uint `json:"light_material"`
} `json:"min_roles"`
PulsarPublicKeys []string `json:"pulsar_public_keys"`
RootDomainReference string `json:"root_domain_ref"`
BootstrapNodes []BootstrapNode `json:"bootstrap_nodes"`
// preprocessed fields
pulsarPublicKey []crypto.PublicKey
}
func newCertificate(publicKey crypto.PublicKey, keyProcessor insolar.KeyProcessor, data []byte) (*Certificate, error) {
cert := Certificate{}
err := json.Unmarshal(data, &cert)
if err != nil {
return nil, errors.Wrap(err, "[ newCertificate ] failed to parse certificate json")
}
pub, err := keyProcessor.ExportPublicKeyPEM(publicKey)
if err != nil {
return nil, errors.Wrap(err, "[ newCertificate ] failed to retrieve public key from node private key")
}
if cert.PublicKey != string(pub) {
return nil, errors.New("[ newCertificate ] Different public keys")
}
err = cert.fillExtraFields(keyProcessor)
if err != nil {
return nil, errors.Wrap(err, "[ newCertificate ] Incorrect fields")
}
cert.DiscoverySigns = make(map[insolar.Reference][]byte)
for _, node := range cert.BootstrapNodes {
cert.DiscoverySigns[*(node.GetNodeRef())] = node.NodeSign
}
return &cert, nil
}
func (cert *Certificate) SerializeNetworkPart() []byte {
out := strconv.Itoa(cert.MajorityRule) + strconv.Itoa(int(cert.MinRoles.Virtual)) +
strconv.Itoa(int(cert.MinRoles.HeavyMaterial)) + strconv.Itoa(int(cert.MinRoles.LightMaterial)) +
cert.RootDomainReference
sort.Strings(cert.PulsarPublicKeys)
out += strings.Join(cert.PulsarPublicKeys, "")
nodes := make([]string, len(cert.BootstrapNodes))
for i, node := range cert.BootstrapNodes {
nodes[i] = node.PublicKey + node.NodeRef + node.Host + node.NodeRole
}
sort.Strings(nodes)
out += strings.Join(nodes, "")
return []byte(out)
}
// SignNetworkPart signs network part in certificate
func (cert *Certificate) SignNetworkPart(key crypto.PrivateKey) ([]byte, error) {
signer := scheme.DataSigner(key, scheme.IntegrityHasher())
sign, err := signer.Sign(cert.SerializeNetworkPart())
if err != nil {
return nil, errors.Wrap(err, "[ SignNetworkPart ] Can't Sign")
}
return sign.Bytes(), nil
}
func (cert *Certificate) fillExtraFields(keyProcessor insolar.KeyProcessor) error {
importedNodePubKey, err := keyProcessor.ImportPublicKeyPEM([]byte(cert.PublicKey))
if err != nil {
return errors.Wrapf(err, "[ fillExtraFields ] Bad PublicKey: %s", cert.PublicKey)
}
cert.nodePublicKey = importedNodePubKey
for _, pulsarKey := range cert.PulsarPublicKeys {
importedPulsarPubKey, err := keyProcessor.ImportPublicKeyPEM([]byte(pulsarKey))
if err != nil {
return errors.Wrapf(err, "[ fillExtraFields ] Bad pulsarKey: %s", pulsarKey)
}
cert.pulsarPublicKey = append(cert.pulsarPublicKey, importedPulsarPubKey)
}
for i := 0; i < len(cert.BootstrapNodes); i++ {
currentNode := &cert.BootstrapNodes[i]
importedBNodePubKey, err := keyProcessor.ImportPublicKeyPEM([]byte(currentNode.PublicKey))
if err != nil {
return errors.Wrapf(err, "[ fillExtraFields ] Bad Bootstrap PublicKey: %s", currentNode.PublicKey)
}
currentNode.nodePublicKey = importedBNodePubKey
}
return nil
}
// GetRootDomainReference returns RootDomain reference
func (cert *Certificate) GetRootDomainReference() *insolar.Reference {
ref, err := insolar.NewReferenceFromBase58(cert.RootDomainReference)
if err != nil {
log.Errorf("Invalid domain reference in cert: %s\n", cert.Reference)
return nil
}
return ref
}
// GetDiscoveryNodes return bootstrap nodes array
func (cert *Certificate) GetDiscoveryNodes() []insolar.DiscoveryNode {
result := make([]insolar.DiscoveryNode, 0)
for i := 0; i < len(cert.BootstrapNodes); i++ {
// we get node by pointer, so ranged for loop does not suite
result = append(result, &cert.BootstrapNodes[i])
}
return result
}
func (cert *Certificate) GetMinRoles() (uint, uint, uint) {
return cert.MinRoles.Virtual, cert.MinRoles.HeavyMaterial, cert.MinRoles.LightMaterial
}
// GetMajorityRule returns majority rule number
func (cert *Certificate) GetMajorityRule() int {
return cert.MajorityRule
}
// Dump returns all info about certificate in json format
func (cert *Certificate) Dump() (string, error) {
result, err := json.MarshalIndent(cert, "", " ")
if err != nil {
return "", errors.Wrap(err, "[ Certificate::Dump ]")
}
return string(result), nil
}
// ReadCertificate constructor creates new Certificate component
func ReadCertificate(publicKey crypto.PublicKey, keyProcessor insolar.KeyProcessor, certPath string) (*Certificate, error) {
data, err := ioutil.ReadFile(filepath.Clean(certPath))
if err != nil {
return nil, errors.Wrapf(err, "[ ReadCertificate ] failed to read certificate from: %s", certPath)
}
cert, err := newCertificate(publicKey, keyProcessor, data)
if err != nil {
return nil, errors.Wrap(err, "[ ReadCertificate ]")
}
return cert, nil
}
// ReadCertificateFromReader constructor creates new Certificate component
func ReadCertificateFromReader(publicKey crypto.PublicKey, keyProcessor insolar.KeyProcessor, reader io.Reader) (*Certificate, error) {
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, errors.Wrapf(err, "[ ReadCertificateFromReader ] failed to read certificate data")
}
cert, err := newCertificate(publicKey, keyProcessor, data)
if err != nil {
return nil, errors.Wrap(err, "[ ReadCertificateFromReader ]")
}
return cert, nil
}
// SignCert is used for signing certificate by Discovery node
func SignCert(signer insolar.Signer, pKey, role, registeredNodeRef string) (*insolar.Signature, error) {
data := []byte(pKey + registeredNodeRef + role)
sign, err := signer.Sign(data)
if err != nil {
return nil, errors.Wrap(err, "[ SignCert ] Couldn't sign")
}
return sign, nil
}