forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabcaadapter.go
628 lines (506 loc) · 19 KB
/
fabcaadapter.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
"github.com/pkg/errors"
"encoding/json"
caapi "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/api"
calib "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client/credential"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/lib/client/credential/x509"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config/endpoint"
"github.com/hyperledger/fabric-sdk-go/pkg/msp/api"
)
// fabricCAAdapter translates between SDK lingo and native Fabric CA API
type fabricCAAdapter struct {
config msp.IdentityConfig
cryptoSuite core.CryptoSuite
caClient *calib.Client
}
func newFabricCAAdapter(orgName string, cryptoSuite core.CryptoSuite, config msp.IdentityConfig) (*fabricCAAdapter, error) {
caClient, err := createFabricCAClient(orgName, cryptoSuite, config)
if err != nil {
return nil, err
}
a := &fabricCAAdapter{
config: config,
cryptoSuite: cryptoSuite,
caClient: caClient,
}
return a, nil
}
// Enroll handles enrollment.
func (c *fabricCAAdapter) Enroll(request *api.EnrollmentRequest) ([]byte, error) {
logger.Debugf("Enrolling user [%s]", request.Name)
// TODO add attributes
careq := &caapi.EnrollmentRequest{
CAName: c.caClient.Config.CAName,
Name: request.Name,
Secret: request.Secret,
Profile: request.Profile,
Type: request.Type,
Label: request.Label,
}
if len(request.AttrReqs) > 0 {
attrs := make([]*caapi.AttributeRequest, len(request.AttrReqs))
for i, a := range request.AttrReqs {
attrs[i] = &caapi.AttributeRequest{Name: a.Name, Optional: a.Optional}
}
careq.AttrReqs = attrs
}
caresp, err := c.caClient.Enroll(careq)
if err != nil {
return nil, errors.WithMessage(err, "enroll failed")
}
return caresp.Identity.GetECert().Cert(), nil
}
// Reenroll handles re-enrollment
func (c *fabricCAAdapter) Reenroll(key core.Key, cert []byte, request *api.ReenrollmentRequest) ([]byte, error) {
logger.Debugf("Re Enrolling user with provided key/cert pair for CA [%s]", c.caClient.Config.CAName)
careq := &caapi.ReenrollmentRequest{
CAName: c.caClient.Config.CAName,
Profile: request.Profile,
Label: request.Label,
}
if len(request.AttrReqs) > 0 {
attrs := make([]*caapi.AttributeRequest, len(request.AttrReqs))
for i, a := range request.AttrReqs {
attrs[i] = &caapi.AttributeRequest{Name: a.Name, Optional: a.Optional}
}
careq.AttrReqs = attrs
}
caidentity, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.WithMessage(err, "failed to create CA signing identity")
}
caresp, err := caidentity.Reenroll(careq)
if err != nil {
return nil, errors.WithMessage(err, "reenroll failed")
}
return caresp.Identity.GetECert().Cert(), nil
}
// Register handles user registration
// key: registrar private key
// cert: registrar enrollment certificate
// request: Registration Request
// Returns Enrolment Secret
func (c *fabricCAAdapter) Register(key core.Key, cert []byte, request *api.RegistrationRequest) (string, error) {
// Construct request for Fabric CA client
var attributes []caapi.Attribute
for i := range request.Attributes {
attributes = append(attributes, caapi.Attribute{Name: request.Attributes[i].Name, Value: request.Attributes[i].Value, ECert: request.Attributes[i].ECert})
}
var req = caapi.RegistrationRequest{
CAName: request.CAName,
Name: request.Name,
Type: request.Type,
MaxEnrollments: request.MaxEnrollments,
Affiliation: request.Affiliation,
Secret: request.Secret,
Attributes: attributes}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return "", errors.Wrap(err, "failed to create CA signing identity")
}
response, err := registrar.Register(&req)
if err != nil {
return "", errors.Wrap(err, "failed to register user")
}
return response.Secret, nil
}
// Revoke handles user revocation.
// key: registrar private key
// cert: registrar enrollment certificate
// request: Revocation Request
func (c *fabricCAAdapter) Revoke(key core.Key, cert []byte, request *api.RevocationRequest) (*api.RevocationResponse, error) {
// Create revocation request
var req = caapi.RevocationRequest{
CAName: request.CAName,
Name: request.Name,
Serial: request.Serial,
AKI: request.AKI,
Reason: request.Reason,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
resp, err := registrar.Revoke(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to revoke")
}
var revokedCerts []api.RevokedCert
for i := range resp.RevokedCerts {
revokedCerts = append(
revokedCerts,
api.RevokedCert{
Serial: resp.RevokedCerts[i].Serial,
AKI: resp.RevokedCerts[i].AKI,
})
}
return &api.RevocationResponse{
RevokedCerts: revokedCerts,
CRL: resp.CRL,
}, nil
}
// GetCAInfo returns generic CA information
func (c *fabricCAAdapter) GetCAInfo(caname string) (*api.GetCAInfoResponse, error) {
logger.Debugf("Get CA info [%s]", caname)
req := &caapi.GetCAInfoRequest{CAName: caname}
resp, err := c.caClient.GetCAInfo(req)
if err != nil {
return nil, errors.WithMessage(err, "GetCAInfo failed")
}
return getCAInfoResponse(resp), nil
}
func getCAInfoResponse(response *calib.GetCAInfoResponse) *api.GetCAInfoResponse {
return &api.GetCAInfoResponse{
CAName: response.CAName,
CAChain: response.CAChain[:],
IssuerPublicKey: response.IssuerPublicKey[:],
IssuerRevocationPublicKey: response.IssuerRevocationPublicKey[:],
Version: response.Version,
}
}
// CreateIdentity creates new identity
// key: registrar private key
// cert: registrar enrollment certificate
func (c *fabricCAAdapter) CreateIdentity(key core.Key, cert []byte, request *api.IdentityRequest) (*api.IdentityResponse, error) {
logger.Debugf("Creating identity [%s:%s]", request.ID, request.Affiliation)
var attributes []caapi.Attribute
for i := range request.Attributes {
attributes = append(attributes, caapi.Attribute{Name: request.Attributes[i].Name, Value: request.Attributes[i].Value, ECert: request.Attributes[i].ECert})
}
// Create add identity request
req := caapi.AddIdentityRequest{
CAName: request.CAName,
ID: request.ID,
Affiliation: request.Affiliation,
Attributes: attributes,
Type: request.Type,
MaxEnrollments: request.MaxEnrollments,
Secret: request.Secret,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
response, err := registrar.AddIdentity(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to add identity")
}
return getIdentityResponse(response), nil
}
// ModifyIdentity modifies identity
// key: registrar private key
// cert: registrar enrollment certificate
func (c *fabricCAAdapter) ModifyIdentity(key core.Key, cert []byte, request *api.IdentityRequest) (*api.IdentityResponse, error) {
logger.Debugf("Updating identity [%s:%s]", request.ID, request.Affiliation)
var attributes []caapi.Attribute
for i := range request.Attributes {
attributes = append(attributes, caapi.Attribute{Name: request.Attributes[i].Name, Value: request.Attributes[i].Value, ECert: request.Attributes[i].ECert})
}
// Create modify identity request
req := caapi.ModifyIdentityRequest{
CAName: request.CAName,
ID: request.ID,
Affiliation: request.Affiliation,
Attributes: attributes,
Type: request.Type,
MaxEnrollments: request.MaxEnrollments,
Secret: request.Secret,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
response, err := registrar.ModifyIdentity(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to modify identity")
}
return getIdentityResponse(response), nil
}
// RemoveIdentity removes identity
// key: registrar private key
// cert: registrar enrollment certificate
func (c *fabricCAAdapter) RemoveIdentity(key core.Key, cert []byte, request *api.RemoveIdentityRequest) (*api.IdentityResponse, error) {
logger.Debugf("Removing identity [%s]", request.ID)
// Create remove request
req := caapi.RemoveIdentityRequest{
CAName: request.CAName,
Force: request.Force,
ID: request.ID,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
response, err := registrar.RemoveIdentity(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to remove identity")
}
return getIdentityResponse(response), nil
}
func getIdentityResponse(response *caapi.IdentityResponse) *api.IdentityResponse {
var attributes []api.Attribute
for i := range response.Attributes {
attributes = append(attributes, api.Attribute{Name: response.Attributes[i].Name, Value: response.Attributes[i].Value, ECert: response.Attributes[i].ECert})
}
ret := &api.IdentityResponse{ID: response.ID,
Affiliation: response.Affiliation,
Type: response.Type,
Attributes: attributes,
MaxEnrollments: response.MaxEnrollments,
Secret: response.Secret,
CAName: response.CAName,
}
return ret
}
// GetIdentity retrieves identity information
// key: registrar private key
// cert: registrar enrollment certificate
// id: identity id
func (c *fabricCAAdapter) GetIdentity(key core.Key, cert []byte, id, caname string) (*api.IdentityResponse, error) {
logger.Debugf("Retrieving identity [%s]", id)
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
response, err := registrar.GetIdentity(id, caname)
if err != nil {
return nil, errors.Wrap(err, "failed to get identity")
}
var attributes []api.Attribute
for i := range response.Attributes {
attributes = append(attributes, api.Attribute{Name: response.Attributes[i].Name, Value: response.Attributes[i].Value, ECert: response.Attributes[i].ECert})
}
ret := &api.IdentityResponse{ID: response.ID,
Affiliation: response.Affiliation,
Type: response.Type,
Attributes: attributes,
MaxEnrollments: response.MaxEnrollments,
CAName: response.CAName,
}
return ret, nil
}
// GetAllIdentities returns all identities that the caller is authorized to see
// key: registrar private key
// cert: registrar enrollment certificate
func (c *fabricCAAdapter) GetAllIdentities(key core.Key, cert []byte, caname string) ([]*api.IdentityResponse, error) {
logger.Debug("Retrieving all identities")
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
var identities []caapi.IdentityInfo
err = registrar.GetAllIdentities(caname, func(decoder *json.Decoder) error {
var identity caapi.IdentityInfo
decodeErr := decoder.Decode(&identity)
if decodeErr != nil {
return decodeErr
}
identities = append(identities, identity)
return nil
})
if err != nil {
return nil, errors.Wrap(err, "failed to get identities")
}
return getIdentityResponses(c.caClient.Config.CAName, identities), nil
}
// GetAffiliation returns information about the requested affiliation
func (c *fabricCAAdapter) GetAffiliation(key core.Key, cert []byte, affiliation, caname string) (*api.AffiliationResponse, error) {
logger.Debugf("Retrieving affiliation [%s]", affiliation)
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
r, err := registrar.GetAffiliation(affiliation, caname)
if err != nil {
return nil, errors.Wrap(err, "failed to get affiliation")
}
resp := &api.AffiliationResponse{CAName: r.CAName, AffiliationInfo: api.AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
return resp, err
}
// GetAllAffiliations returns all affiliations that the caller is authorized to see
func (c *fabricCAAdapter) GetAllAffiliations(key core.Key, cert []byte, caname string) (*api.AffiliationResponse, error) {
logger.Debugf("Retrieving all affiliations")
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
r, err := registrar.GetAllAffiliations(caname)
if err != nil {
return nil, errors.Wrap(err, "failed to get affiliations")
}
resp := &api.AffiliationResponse{CAName: r.CAName, AffiliationInfo: api.AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
return resp, err
}
// AddAffiliation add new affiliation
func (c *fabricCAAdapter) AddAffiliation(key core.Key, cert []byte, request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
logger.Debugf("Add affiliation [%s]", request.Name)
req := caapi.AddAffiliationRequest{
CAName: request.CAName,
Name: request.Name,
Force: request.Force,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
r, err := registrar.AddAffiliation(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to add affiliation")
}
resp := &api.AffiliationResponse{CAName: r.CAName, AffiliationInfo: api.AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
return resp, err
}
// ModifyAffiliation renames an existing affiliation on the server
func (c *fabricCAAdapter) ModifyAffiliation(key core.Key, cert []byte, request *api.ModifyAffiliationRequest) (*api.AffiliationResponse, error) {
logger.Debugf("Updating affiliation [%s => %s]", request.Name, request.NewName)
req := caapi.ModifyAffiliationRequest{
CAName: request.CAName,
Name: request.Name,
NewName: request.NewName,
Force: request.Force,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
r, err := registrar.ModifyAffiliation(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to modify affiliation")
}
resp := &api.AffiliationResponse{CAName: r.CAName, AffiliationInfo: api.AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
return resp, err
}
// RemoveAffiliation removes an existing affiliation from the server
func (c *fabricCAAdapter) RemoveAffiliation(key core.Key, cert []byte, request *api.AffiliationRequest) (*api.AffiliationResponse, error) {
logger.Debugf("Removing affiliation [%s]", request.Name)
// Create remove request
req := caapi.RemoveAffiliationRequest{
CAName: request.CAName,
Name: request.Name,
Force: request.Force,
}
registrar, err := c.newIdentity(key, cert)
if err != nil {
return nil, errors.Wrap(err, "failed to create CA signing identity")
}
r, err := registrar.RemoveAffiliation(&req)
if err != nil {
return nil, errors.Wrap(err, "failed to remove affiliation")
}
resp := &api.AffiliationResponse{CAName: r.CAName, AffiliationInfo: api.AffiliationInfo{}}
err = fillAffiliationInfo(&resp.AffiliationInfo, r.Name, r.Affiliations, r.Identities)
return resp, err
}
func fillAffiliationInfo(info *api.AffiliationInfo, name string, affiliations []caapi.AffiliationInfo, identities []caapi.IdentityInfo) error {
info.Name = name
// Add identities which have this affiliation
idents := []api.IdentityInfo{}
for _, identity := range identities {
idents = append(idents, api.IdentityInfo{ID: identity.ID, Type: identity.Type, Affiliation: identity.Affiliation, Attributes: getAllAttributes(identity.Attributes), MaxEnrollments: identity.MaxEnrollments})
}
if len(idents) > 0 {
info.Identities = idents
}
// Create child affiliations (if any)
children := []api.AffiliationInfo{}
for _, aff := range affiliations {
childAff := api.AffiliationInfo{Name: aff.Name}
err := fillAffiliationInfo(&childAff, aff.Name, aff.Affiliations, aff.Identities)
if err != nil {
return err
}
children = append(children, childAff)
}
if len(children) > 0 {
info.Affiliations = children
}
return nil
}
func getAllAttributes(attrs []caapi.Attribute) []api.Attribute {
attriburtes := []api.Attribute{}
for _, attr := range attrs {
attriburtes = append(attriburtes, api.Attribute{Name: attr.Name, Value: attr.Value, ECert: attr.ECert})
}
return attriburtes
}
func (c *fabricCAAdapter) newIdentity(key core.Key, cert []byte) (*calib.Identity, error) {
x509Cred := x509.NewCredential(key, cert, c.caClient)
signer, err := x509.NewSigner(key, cert)
if err != nil {
return nil, err
}
err = x509Cred.SetVal(signer)
if err != nil {
return nil, err
}
return c.caClient.NewIdentity([]credential.Credential{x509Cred})
}
func getIdentityResponses(ca string, responses []caapi.IdentityInfo) []*api.IdentityResponse {
ret := make([]*api.IdentityResponse, len(responses))
for j, response := range responses {
var attributes []api.Attribute
for i := range response.Attributes {
attributes = append(attributes, api.Attribute{Name: response.Attributes[i].Name, Value: response.Attributes[i].Value, ECert: response.Attributes[i].ECert})
}
ret[j] = &api.IdentityResponse{ID: response.ID,
Affiliation: response.Affiliation,
Type: response.Type,
Attributes: attributes,
MaxEnrollments: response.MaxEnrollments,
CAName: ca,
}
}
return ret
}
func createFabricCAClient(org string, cryptoSuite core.CryptoSuite, config msp.IdentityConfig) (*calib.Client, error) {
// Create new Fabric-ca client without configs
c := &calib.Client{
Config: &calib.ClientConfig{},
}
conf, ok := config.CAConfig(org)
if !ok {
return nil, errors.Errorf("Organization [%s] have no corresponding CA in the configs", org)
}
//set server CAName
c.Config.CAName = conf.CAName
//set server URL
c.Config.URL = endpoint.ToAddress(conf.URL)
//set server name
c.Config.ServerName, _ = conf.GRPCOptions["ssl-target-name-override"].(string)
//certs file list
c.Config.TLS.CertFiles, ok = config.CAServerCerts(org)
if !ok {
return nil, errors.Errorf("Organization [%s] have no corresponding server certs in the configs", org)
}
// set key file and cert file
c.Config.TLS.Client.CertFile, ok = config.CAClientCert(org)
if !ok {
return nil, errors.Errorf("Organization [%s] have no corresponding client certs in the configs", org)
}
c.Config.TLS.Client.KeyFile, ok = config.CAClientKey(org)
if !ok {
return nil, errors.Errorf("Organization [%s] have no corresponding client keys in the configs", org)
}
//TLS flag enabled/disabled
c.Config.TLS.Enabled = endpoint.IsTLSEnabled(conf.URL)
c.Config.MSPDir = config.CAKeyStorePath()
//Factory opts
c.Config.CSP = cryptoSuite
err := c.Init()
if err != nil {
return nil, errors.Wrap(err, "CA Client init failed")
}
return c, nil
}