-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_eca.go
485 lines (367 loc) · 13.5 KB
/
node_eca.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
Copyright IBM Corp. 2016 All Rights Reserved.
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 crypto
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/x509"
"time"
membersrvc "github.com/hyperledger/fabric/membersrvc/protos"
"encoding/asn1"
"errors"
"io/ioutil"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hyperledger/fabric/core/crypto/primitives"
"github.com/hyperledger/fabric/core/crypto/primitives/ecies"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
// ECertSubjectRole is the ASN1 object identifier of the subject's role.
ECertSubjectRole = asn1.ObjectIdentifier{2, 1, 3, 4, 5, 6, 7}
)
func (node *nodeImpl) retrieveECACertsChain(userID string) error {
if !node.ks.certMissing(node.conf.getECACertsChainFilename()) {
return nil
}
// Retrieve ECA certificate and verify it
ecaCertRaw, err := node.getECACertificate()
if err != nil {
node.Errorf("Failed getting ECA certificate [%s].", err.Error())
return err
}
node.Debugf("ECA certificate [% x].", ecaCertRaw)
// TODO: Test ECA cert againt root CA
// TODO: check response.Cert against rootCA
x509ECACert, err := primitives.DERToX509Certificate(ecaCertRaw)
if err != nil {
node.Errorf("Failed parsing ECA certificate [%s].", err.Error())
return err
}
// Prepare ecaCertPool
node.ecaCertPool = x509.NewCertPool()
node.ecaCertPool.AddCert(x509ECACert)
// Store ECA cert
node.Debugf("Storing ECA certificate for [%s]...", userID)
if err := node.ks.storeCert(node.conf.getECACertsChainFilename(), ecaCertRaw); err != nil {
node.Errorf("Failed storing eca certificate [%s].", err.Error())
return err
}
return nil
}
func (node *nodeImpl) retrieveEnrollmentData(enrollID, enrollPWD string) error {
if !node.ks.certMissing(node.conf.getEnrollmentCertFilename()) {
return nil
}
key, enrollCertRaw, enrollChainKey, err := node.getEnrollmentCertificateFromECA(enrollID, enrollPWD)
if err != nil {
node.Errorf("Failed getting enrollment certificate [id=%s]: [%s]", enrollID, err)
return err
}
node.Debugf("Enrollment certificate [% x].", enrollCertRaw)
node.Debugf("Storing enrollment data for user [%s]...", enrollID)
// Store enrollment id
err = ioutil.WriteFile(node.conf.getEnrollmentIDPath(), []byte(enrollID), 0700)
if err != nil {
node.Errorf("Failed storing enrollment certificate [id=%s]: [%s]", enrollID, err)
return err
}
// Store enrollment key
if err := node.ks.storePrivateKey(node.conf.getEnrollmentKeyFilename(), key); err != nil {
node.Errorf("Failed storing enrollment key [id=%s]: [%s]", enrollID, err)
return err
}
// Store enrollment cert
if err := node.ks.storeCert(node.conf.getEnrollmentCertFilename(), enrollCertRaw); err != nil {
node.Errorf("Failed storing enrollment certificate [id=%s]: [%s]", enrollID, err)
return err
}
// Code for confidentiality 1.2
// Store enrollment chain key
if node.eType == NodeValidator {
node.Debugf("Enrollment chain key for validator [%s]...", enrollID)
// enrollChainKey is a secret key
node.Debugf("key [%s]...", string(enrollChainKey))
key, err := primitives.PEMtoPrivateKey(enrollChainKey, nil)
if err != nil {
node.Errorf("Failed unmarshalling enrollment chain key [id=%s]: [%s]", enrollID, err)
return err
}
if err := node.ks.storePrivateKey(node.conf.getEnrollmentChainKeyFilename(), key); err != nil {
node.Errorf("Failed storing enrollment chain key [id=%s]: [%s]", enrollID, err)
return err
}
} else {
node.Debugf("Enrollment chain key for non-validator [%s]...", enrollID)
// enrollChainKey is a public key
key, err := primitives.PEMtoPublicKey(enrollChainKey, nil)
if err != nil {
node.Errorf("Failed unmarshalling enrollment chain key [id=%s]: [%s]", enrollID, err)
return err
}
node.Debugf("Key decoded from PEM [%s]...", enrollID)
if err := node.ks.storePublicKey(node.conf.getEnrollmentChainKeyFilename(), key); err != nil {
node.Errorf("Failed storing enrollment chain key [id=%s]: [%s]", enrollID, err)
return err
}
}
return nil
}
func (node *nodeImpl) loadEnrollmentKey() error {
node.Debug("Loading enrollment key...")
enrollPrivKey, err := node.ks.loadPrivateKey(node.conf.getEnrollmentKeyFilename())
if err != nil {
node.Errorf("Failed loading enrollment private key [%s].", err.Error())
return err
}
node.enrollPrivKey = enrollPrivKey.(*ecdsa.PrivateKey)
return nil
}
func (node *nodeImpl) loadEnrollmentCertificate() error {
node.Debug("Loading enrollment certificate...")
cert, der, err := node.ks.loadCertX509AndDer(node.conf.getEnrollmentCertFilename())
if err != nil {
node.Errorf("Failed parsing enrollment certificate [%s].", err.Error())
return err
}
node.enrollCert = cert
// TODO: move this to retrieve
pk := node.enrollCert.PublicKey.(*ecdsa.PublicKey)
err = primitives.VerifySignCapability(node.enrollPrivKey, pk)
if err != nil {
node.Errorf("Failed checking enrollment certificate against enrollment key [%s].", err.Error())
return err
}
// Set node ID
node.id = primitives.Hash(der)
node.Debugf("Setting id to [% x].", node.id)
// Set eCertHash
node.enrollCertHash = primitives.Hash(der)
node.Debugf("Setting enrollCertHash to [% x].", node.enrollCertHash)
return nil
}
func (node *nodeImpl) loadEnrollmentID() error {
node.Debugf("Loading enrollment id at [%s]...", node.conf.getEnrollmentIDPath())
enrollID, err := ioutil.ReadFile(node.conf.getEnrollmentIDPath())
if err != nil {
node.Errorf("Failed loading enrollment id [%s].", err.Error())
return err
}
// Set enrollment ID
node.enrollID = string(enrollID)
node.Debugf("Setting enrollment id to [%s].", node.enrollID)
return nil
}
func (node *nodeImpl) loadEnrollmentChainKey() error {
node.Debug("Loading enrollment chain key...")
// Code for confidentiality 1.2
if node.eType == NodeValidator {
// enrollChainKey is a secret key
enrollChainKey, err := node.ks.loadPrivateKey(node.conf.getEnrollmentChainKeyFilename())
if err != nil {
node.Errorf("Failed loading enrollment chain key: [%s]", err)
return err
}
node.enrollChainKey = enrollChainKey
} else {
// enrollChainKey is a public key
enrollChainKey, err := node.ks.loadPublicKey(node.conf.getEnrollmentChainKeyFilename())
if err != nil {
node.Errorf("Failed load enrollment chain key: [%s]", err)
return err
}
node.enrollChainKey = enrollChainKey
}
return nil
}
func (node *nodeImpl) loadECACertsChain() error {
node.Debug("Loading ECA certificates chain...")
pem, err := node.ks.loadCert(node.conf.getECACertsChainFilename())
if err != nil {
node.Errorf("Failed loading ECA certificates chain [%s].", err.Error())
return err
}
ok := node.ecaCertPool.AppendCertsFromPEM(pem)
if !ok {
node.Error("Failed appending ECA certificates chain.")
return errors.New("Failed appending ECA certificates chain.")
}
return nil
}
func (node *nodeImpl) getECAClient() (*grpc.ClientConn, membersrvc.ECAPClient, error) {
node.Debug("Getting ECA client...")
conn, err := node.getClientConn(node.conf.getECAPAddr(), node.conf.getECAServerName())
if err != nil {
node.Errorf("Failed getting client connection: [%s]", err)
}
client := membersrvc.NewECAPClient(conn)
node.Debug("Getting ECA client...done")
return conn, client, nil
}
func (node *nodeImpl) callECAReadCACertificate(ctx context.Context, opts ...grpc.CallOption) (*membersrvc.Cert, error) {
// Get an ECA Client
sock, ecaP, err := node.getECAClient()
defer sock.Close()
// Issue the request
cert, err := ecaP.ReadCACertificate(ctx, &membersrvc.Empty{}, opts...)
if err != nil {
node.Errorf("Failed requesting read certificate [%s].", err.Error())
return nil, err
}
return cert, nil
}
func (node *nodeImpl) callECAReadCertificate(ctx context.Context, in *membersrvc.ECertReadReq, opts ...grpc.CallOption) (*membersrvc.CertPair, error) {
// Get an ECA Client
sock, ecaP, err := node.getECAClient()
defer sock.Close()
// Issue the request
resp, err := ecaP.ReadCertificatePair(ctx, in, opts...)
if err != nil {
node.Errorf("Failed requesting read certificate [%s].", err.Error())
return nil, err
}
return resp, nil
}
func (node *nodeImpl) callECAReadCertificateByHash(ctx context.Context, in *membersrvc.Hash, opts ...grpc.CallOption) (*membersrvc.CertPair, error) {
// Get an ECA Client
sock, ecaP, err := node.getECAClient()
defer sock.Close()
// Issue the request
resp, err := ecaP.ReadCertificateByHash(ctx, in, opts...)
if err != nil {
node.Errorf("Failed requesting read certificate [%s].", err.Error())
return nil, err
}
return &membersrvc.CertPair{Sign: resp.Cert, Enc: nil}, nil
}
func (node *nodeImpl) getEnrollmentCertificateFromECA(id, pw string) (interface{}, []byte, []byte, error) {
// Get a new ECA Client
sock, ecaP, err := node.getECAClient()
defer sock.Close()
// Run the protocol
signPriv, err := primitives.NewECDSAKey()
if err != nil {
node.Errorf("Failed generating ECDSA key [%s].", err.Error())
return nil, nil, nil, err
}
signPub, err := x509.MarshalPKIXPublicKey(&signPriv.PublicKey)
if err != nil {
node.Errorf("Failed mashalling ECDSA key [%s].", err.Error())
return nil, nil, nil, err
}
encPriv, err := primitives.NewECDSAKey()
if err != nil {
node.Errorf("Failed generating Encryption key [%s].", err.Error())
return nil, nil, nil, err
}
encPub, err := x509.MarshalPKIXPublicKey(&encPriv.PublicKey)
if err != nil {
node.Errorf("Failed marshalling Encryption key [%s].", err.Error())
return nil, nil, nil, err
}
req := &membersrvc.ECertCreateReq{
Ts: ×tamp.Timestamp{Seconds: time.Now().Unix(), Nanos: 0},
Id: &membersrvc.Identity{Id: id},
Tok: &membersrvc.Token{Tok: []byte(pw)},
Sign: &membersrvc.PublicKey{Type: membersrvc.CryptoType_ECDSA, Key: signPub},
Enc: &membersrvc.PublicKey{Type: membersrvc.CryptoType_ECDSA, Key: encPub},
Sig: nil}
resp, err := ecaP.CreateCertificatePair(context.Background(), req)
if err != nil {
node.Errorf("Failed invoking CreateCertficatePair [%s].", err.Error())
return nil, nil, nil, err
}
if resp.FetchResult != nil && resp.FetchResult.Status != membersrvc.FetchAttrsResult_SUCCESS {
node.Warning(resp.FetchResult.Msg)
}
//out, err := rsa.DecryptPKCS1v15(rand.Reader, encPriv, resp.Tok.Tok)
spi := ecies.NewSPI()
eciesKey, err := spi.NewPrivateKey(nil, encPriv)
if err != nil {
node.Errorf("Failed parsing decrypting key [%s].", err.Error())
return nil, nil, nil, err
}
ecies, err := spi.NewAsymmetricCipherFromPublicKey(eciesKey)
if err != nil {
node.Errorf("Failed creating asymmetrinc cipher [%s].", err.Error())
return nil, nil, nil, err
}
out, err := ecies.Process(resp.Tok.Tok)
if err != nil {
node.Errorf("Failed decrypting toke [%s].", err.Error())
return nil, nil, nil, err
}
req.Tok.Tok = out
req.Sig = nil
hash := primitives.NewHash()
raw, _ := proto.Marshal(req)
hash.Write(raw)
r, s, err := ecdsa.Sign(rand.Reader, signPriv, hash.Sum(nil))
if err != nil {
node.Errorf("Failed signing [%s].", err.Error())
return nil, nil, nil, err
}
R, _ := r.MarshalText()
S, _ := s.MarshalText()
req.Sig = &membersrvc.Signature{Type: membersrvc.CryptoType_ECDSA, R: R, S: S}
resp, err = ecaP.CreateCertificatePair(context.Background(), req)
if err != nil {
node.Errorf("Failed invoking CreateCertificatePair [%s].", err.Error())
return nil, nil, nil, err
}
// Verify response
// Verify cert for signing
node.Debugf("Enrollment certificate for signing [% x]", primitives.Hash(resp.Certs.Sign))
x509SignCert, err := primitives.DERToX509Certificate(resp.Certs.Sign)
if err != nil {
node.Errorf("Failed parsing signing enrollment certificate for signing: [%s]", err)
return nil, nil, nil, err
}
_, err = primitives.GetCriticalExtension(x509SignCert, ECertSubjectRole)
if err != nil {
node.Errorf("Failed parsing ECertSubjectRole in enrollment certificate for signing: [%s]", err)
return nil, nil, nil, err
}
err = primitives.CheckCertAgainstSKAndRoot(x509SignCert, signPriv, node.ecaCertPool)
if err != nil {
node.Errorf("Failed checking signing enrollment certificate for signing: [%s]", err)
return nil, nil, nil, err
}
// Verify cert for encrypting
node.Debugf("Enrollment certificate for encrypting [% x]", primitives.Hash(resp.Certs.Enc))
x509EncCert, err := primitives.DERToX509Certificate(resp.Certs.Enc)
if err != nil {
node.Errorf("Failed parsing signing enrollment certificate for encrypting: [%s]", err)
return nil, nil, nil, err
}
_, err = primitives.GetCriticalExtension(x509EncCert, ECertSubjectRole)
if err != nil {
node.Errorf("Failed parsing ECertSubjectRole in enrollment certificate for encrypting: [%s]", err)
return nil, nil, nil, err
}
err = primitives.CheckCertAgainstSKAndRoot(x509EncCert, encPriv, node.ecaCertPool)
if err != nil {
node.Errorf("Failed checking signing enrollment certificate for encrypting: [%s]", err)
return nil, nil, nil, err
}
return signPriv, resp.Certs.Sign, resp.Pkchain, nil
}
func (node *nodeImpl) getECACertificate() ([]byte, error) {
responce, err := node.callECAReadCACertificate(context.Background())
if err != nil {
node.Errorf("Failed requesting ECA certificate [%s].", err.Error())
return nil, err
}
return responce.Cert, nil
}