-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathcaclient.go
1266 lines (1090 loc) · 39.3 KB
/
caclient.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright: Cognition Foundry. All Rights Reserved.
License: Apache License Version 2.0
*/
package gohfc
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// RegistrationRequest holds all data needed for new registration of new user in Certificate Authority
type CARegistrationRequest struct {
// EnrolmentId is unique name that identifies identity
EnrolmentId string `json:"id"`
// Type defines type of this identity (user,client, auditor etc...)
Type string `json:"type"`
// Secret is password that will be used for enrollment. If not provided random password will be generated
Secret string `json:"secret,omitempty"`
// MaxEnrollments define maximum number of times that identity can enroll. If not provided or is 0 there is no limit
MaxEnrollments int `json:"max_enrollments,omitempty"`
// Affiliation associates identity with particular organisation.
// for example org1.department1 makes this identity part of organisation `org1` and department `department1`
// Hierarchical structure can be created using .(dot). For example org1.dep1 will create dep1 as part of org1
Affiliation string `json:"affiliation"`
// Attrs are attributes associated with this identity
Attrs []CaRegisterAttribute `json:"attrs"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
}
// CaRegisterAttribute holds user attribute used for registration
// for example user may have attr `accountType` with value `premium`
// this attributes can be accessed in chainCode and build business logic on top of them
type CaRegisterAttribute struct {
// Name is the name of the attribute.
Name string `json:"name"`
// Value is the value of the attribute. Can be empty string
Value string `json:"value"`
// ECert define how this attribute will be included in ECert. If this value is true this attribute will be
// added to ECert automatically on Enrollment if no attributes are requested on Enrollment request.
ECert bool `json:"ecert,omitempty"`
}
// CaEnrollmentRequest holds data needed for getting ECert (enrollment) from CA server
type CaEnrollmentRequest struct {
// EnrollmentId is the unique entity identifies
EnrollmentId string
// Secret is the password for this identity
Secret string
// Profile define which CA profile to be used for signing. When this profile is empty default profile is used.
// This is the common situation when issuing and ECert.
// If request is fo generating TLS certificates then profile must be `tls`
// If operation is related to parent CA server then profile must be `ca`
// In FabricCA custom profiles can be created. In this situation use custom profile name.
Profile string `json:"profile,omitempty"`
// Label is used for hardware secure modules.
Label string `json:"label,omitempty"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
// Host is the list of valid host names for this certificate. If empty default hosts will be used
Hosts []string `json:"hosts"`
// Attrs are the attributes that must be included in ECert. This is subset of the attributes used in registration.
Attrs []CaEnrollAttribute `json:"attr_reqs,omitempty"`
}
// CaReEnrollmentRequest holds data needed for getting new ECert from CA server
type CaReEnrollmentRequest struct {
Identity *Identity
// Profile define which CA profile to be used for signing. When this profile is empty default profile is used.
// This is the common situation when issuing and ECert.
// If request is fo generating TLS certificates then profile must be `tls`
// If operation is related to parent CA server then profile must be `ca`
// In FabricCA custom profiles can be created. In this situation use custom profile name.
Profile string `json:"profile,omitempty"`
// Label is used for hardware secure modules.
Label string `json:"label,omitempty"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
// Host is the list of valid host names for this certificate. If empty default hosts will be used
Hosts []string `json:"hosts"`
// Attrs are the attributes that must be included in ECert. This is subset of the attributes used in registration.
Attrs []CaEnrollAttribute `json:"attr_reqs,omitempty"`
}
// CaEnrollAttribute describe attribute that must be included in enrollment request
type CaEnrollAttribute struct {
// Name is the name of the attribute
Name string `json:"name"`
// Optional define behaviour when required attribute is not available to user. If `true` then request will continue,
// but attribute will not be included in ECert. If `false` and attribute is missing, request will fail.
// If false and attribute is available, request will continue and attribute will be added in ECert
Optional bool `json:"optional,omitempty"`
}
// CARevocationRequest holds data needed to revoke certificate in fabric-ca
// If AKI and Serial are provided this will revoke specific certificate.
// If EnrolmentID is provided all certificated for this EnrollmentID will be revoked and all his/hers future attempts
// to enroll will fail.
type CARevocationRequest struct {
// EnrollmentId of the identity whose certificates should be revoked
// If this field is omitted, then Serial and AKI must be specified.
EnrollmentId string `json:"id,omitempty"`
// Serial number of the certificate to be revoked
// If this is omitted, then EnrollmentId must be specified
Serial string `json:"serial,omitempty"`
// AKI (Authority Key Identifier) of the certificate to be revoked
AKI string `json:"aki,omitempty"`
// Reason is the reason for revocation. See https://godoc.org/golang.org/x/crypto/ocsp for
// valid values. The default value is 0 (ocsp.Unspecified).
Reason int `json:"reason,omitempty"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
// GenCRL specifies whether to generate a CRL. CRL will be returned only when AKI and Serial are provided.
GenCRL bool `json:"gencrl,omitempty"`
}
// CAGetCertsResponse holds response from `GetCaCertificateChain`
type CAGetCertsResponse struct {
// RootCertificates is list of pem encoded certificates
RootCertificates []*pem.Block
// IntermediateCertificates is list of pem encoded intermediate certificates
IntermediateCertificates []*pem.Block
// CAName is the name of the CA server that returns this certificates
CAName string
// Version is the version of server that returns this certificates
Version string
}
// CAAddAffiliationRequest contains needed data for creating new affiliation
type CAAddAffiliationRequest struct {
// Name is the name of the affiliation. Hierarchical structure is created using .(dot) like `org1.department1`.
Name string `json:"name"`
// Force forces creation of missing parent affiliation. If `force` is false and parent/s is missing error will be returned.
Force bool `json:"force"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
}
// CARemoveAffiliationRequest contains needed data for removing existing affiliation
type CARemoveAffiliationRequest struct {
// Name is the name of the affiliation to be removed. Dot can be used to specify child like `org1.department1`.
Name string
// Force will force removal of child affiliations and any identity associated with them
Force bool
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string
}
// CAModifyAffiliationRequest holds data needed to update existing affiliation stored in FabricCa server
type CAModifyAffiliationRequest struct {
// Name is the name of the affiliation to be updated like `org1.department1`.
Name string
// New name is the new name of the affiliation.
NewName string `json:"name"`
// Force will force identities using old affiliation to use new affiliation.
Force bool `json:"force"`
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string `json:"caname,omitempty"`
}
// CAAffiliationResponse holds response for all operations with affiliation.
type CAAffiliationResponse struct {
CAAffiliationInfo
CAName string `json:"caname,omitempty"`
}
// CAAffiliationInfo represent affiliation returned from FabricCA
type CAAffiliationInfo struct {
// Name is the name of the affiliation
Name string `json:"name"`
// Affiliations is list of affiliations that are child to current one
Affiliations []CAAffiliationInfo `json:"affiliations,omitempty"`
}
// CaRevokeResult is holding result from FabricCA revoke
type CaRevokeResult struct {
// RevokedCertificates is list of revoked certificates
RevokedCertificates []CaRevokeResultCertificate `json:"RevokedCerts"`
// CRL is the certificate revocation list from the operation.
CRL string `json:"CRL"`
}
// CaRevokeResultCertificate identify revoked certificate
type CaRevokeResultCertificate struct {
// Serial is revoked certificate serial number
Serial string `json:"Serial"`
// AKI is revoked certificate AKI
AKI string `json:"AKI"`
}
// CAListAllIdentitiesResponse hold response for `ListAllIdentities` call
type CAListAllIdentitiesResponse struct {
// Name is the name of the affiliation
CAName string `json:"caname"`
// Affiliations is list of affiliations that are child to current one
Identities []CaIdentityResponse `json:"identities,omitempty"`
}
// CAGetIdentityResponse holds response from `GetIdentity` call
type CAGetIdentityResponse struct {
CaIdentityResponse
// Name is the name of the affiliation
CAName string `json:"caname"`
}
// CaIdentityResponse represent identity
type CaIdentityResponse struct {
ID string `json:"id"`
Type string `json:"type"`
Affiliation string `json:"affiliation"`
Attributes []CaRegisterAttribute `json:"attrs" mapstructure:"attrs"`
MaxEnrollments int `json:"max_enrollments" mapstructure:"max_enrollments"`
}
// CARemoveIdentityRequest contains needed data for removing existing identity
type CARemoveIdentityRequest struct {
// Name is the id of the identity to be removed.
Name string
// Force will force removal of your own identity
Force bool
// CAName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
CAName string
}
// CAModifyIdentityRequest holds data that will be used to update existing identity
type CAModifyIdentityRequest struct {
ID string `json:"-"`
Type string `json:"type"`
Affiliation string `json:"affiliation"`
Attributes []CaRegisterAttribute `json:"attrs"`
MaxEnrollments int `json:"max_enrollments"`
Secret string `json:"secret,omitempty"`
CAName string `json:"caname,omitempty"`
}
// FabricCAClient is client implementation for fabric-ca server
type FabricCAClient struct {
// Uri is access point for fabric-ca server. Port number and scheme must be provided.
// for example http://127.0.0.1:7054
Url string
// SkipTLSVerification define how connection must handle invalid TLC certificates.
// if true, all verifications are skipped. This value is overwritten by Transport property, if provided
SkipTLSVerification bool
// Crypto is CryptSuite implementation used to sign request for fabric-ca server
Crypto CryptoSuite
// Transport define transport rules for communication with fabric-ca server. If nil, default Go setting will be used
// It is responsibility of the user to provide proper TLS/certificate setting in TLS communication.
Transport *http.Transport
// MspId value will be added to Identity in Enrollment and ReEnrollment invocations.
// This value is not used anywhere in CA implementation, but is need in every call to Fabric and is added here
// for convenience, because (in general case) FabricCA is serving one MSP
// User can overwrite this value at any time.
MspId string
}
// CAResponse represents response message from fabric-ca server
type caResponse struct {
Success bool `json:"success"`
Errors []caResponseErr `json:"errors"`
Messages []string `json:"messages"`
}
type caRegisterResponse struct {
caResponse
Result caRegisterCredentialResponse `json:"result"`
}
// CARegisterCredentialResponse credentials from fabric-ca server registration request
type caRegisterCredentialResponse struct {
Secret string `json:"secret"`
}
func (c *caRegisterCredentialResponse) UnmarshalJSON(b []byte) error {
type tmpStruct struct {
Secret string `json:"secret"`
}
if len(b) > 2 {
r := new(tmpStruct)
err := json.Unmarshal(b, r)
if err != nil {
return err
}
c.Secret = r.Secret
}
return nil
}
// CAResponseErr represents error message from fabric-ca server
type caResponseErr struct {
Code int `json:"code"`
Message string `json:"message"`
}
// enrollmentResponse is response from fabric-ca server for enrolment that contains created Ecert
type enrollmentResponse struct {
caResponse
Result enrollmentResponseResult `json:"result"`
}
type enrollmentResponseResult struct {
Cert string
ServerInfo enrollmentResponseServerInfo
Version string
}
func (e *enrollmentResponseResult) UnmarshalJSON(b []byte) error {
type tmpStruct struct {
Cert string
ServerInfo enrollmentResponseServerInfo
Version string
}
if len(b) > 2 {
r := new(tmpStruct)
err := json.Unmarshal(b, r)
if err != nil {
return err
}
e.Cert = r.Cert
e.ServerInfo = r.ServerInfo
e.Version = r.Version
}
return nil
}
type caRevokeResponse struct {
caResponse
Result CaRevokeResult `json:"result"`
}
type enrollmentResponseServerInfo struct {
CAName string
CAChain string
}
// certificateRequest holds certificate request that must be signed by fabric-ca
type certificateRequest struct {
CaEnrollmentRequest
CR string `json:"certificate_request"`
}
type caInfoRequest struct {
CaName string `json:"caname,omitempty"`
}
type caInfoResponse struct {
caResponse
Result caInfoResponseResult `json:"result"`
}
type caInfoResponseResult struct {
CAName string `json:"CAName"`
CAChain string `json:"CAChain"`
Version string `json:"Version"`
}
type caAffiliationResponse struct {
caResponse
Result CAAffiliationResponse `json:"result"`
}
type caListAllIdentities struct {
caResponse
Result CAListAllIdentitiesResponse `json:"result"`
}
type caGetIdentity struct {
caResponse
Result CAGetIdentityResponse `json:"result"`
}
// We need this because FabricCa is not consistent with returned (JSON) data types.
// It is possible to have response where instead of JSON object we have empty string and default Unmarshal will fail.
func (c *caInfoResponseResult) UnmarshalJSON(b []byte) error {
type tmpStruct struct {
CAName string `json:"CAName"`
CAChain string `json:"CAChain"`
Version string `json:"Version"`
}
if len(b) > 2 {
r := new(tmpStruct)
err := json.Unmarshal(b, r)
if err != nil {
return err
}
c.CAName = r.CAName
c.Version = r.Version
c.CAChain = r.CAChain
}
return nil
}
// Register registers new user in fabric-ca server. In registration request attributes, affiliation and
// max enrolments must be set.
// It is responsibility of the SDK user to ensure passwords are with big entropy.
// Identity parameter is certificate for user that makes registration and this user MUST have the role for
// registering new users.
func (f *FabricCAClient) Register(identity *Identity, req *CARegistrationRequest) (string, error) {
if req.EnrolmentId == "" {
return "", ErrEnrolmentMissing
}
if req.Affiliation == "" {
return "", ErrAffiliationMissing
}
if req.Type == "" {
return "", ErrTypeMissing
}
if identity == nil {
return "", ErrCertificateEmpty
}
reqJson, err := json.Marshal(req)
if err != nil {
return "", err
}
httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/register", f.Url), bytes.NewBuffer(reqJson))
httpReq.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, reqJson)
if err != nil {
return "", err
}
httpReq.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(httpReq)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caRegisterResponse)
if err := json.Unmarshal(body, result); err != nil {
return "", err
}
if !result.Success {
return "", concatErrors(result.Errors)
}
if len(result.Errors) > 0 {
return "", concatErrors(result.Errors)
}
return result.Result.Secret, nil
}
return "", fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// Enroll execute enrollment request for registered user in FabricCA server.
// On success new Identity with ECert and generated csr are returned.
func (f *FabricCAClient) Enroll(request CaEnrollmentRequest) (*Identity, []byte, error) {
// create new cert and send it to CA for signing
key, err := f.Crypto.GenerateKey()
if err != nil {
return nil, nil, err
}
var hosts []string
if len(request.Hosts) == 0 {
parsedUrl, err := url.Parse(f.Url)
if err != nil {
return nil, nil, err
}
hosts = []string{parsedUrl.Host}
} else {
hosts = request.Hosts
}
csr, err := f.Crypto.CreateCertificateRequest(request.EnrollmentId, key, hosts)
if err != nil {
return nil, nil, err
}
crm, err := json.Marshal(certificateRequest{CR: string(csr), CaEnrollmentRequest: request})
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/enroll", f.Url), bytes.NewBuffer(crm))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(request.EnrollmentId, request.Secret)
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
enrResp := new(enrollmentResponse)
if err := json.Unmarshal(body, enrResp); err != nil {
return nil, nil, err
}
if !enrResp.Success {
return nil, nil, concatErrors(enrResp.Errors)
}
rawCert, err := base64.StdEncoding.DecodeString(enrResp.Result.Cert)
if err != nil {
return nil, nil, err
}
a, _ := pem.Decode(rawCert)
cert, err := x509.ParseCertificate(a.Bytes)
if err != nil {
return nil, nil, err
}
return &Identity{Certificate: cert, PrivateKey: key, MspId: f.MspId}, csr, nil
}
return nil, nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// Revoke revokes ECert in fabric-ca server.
// Note that this request will revoke certificate ONLY in FabricCa server. Peers (for now) do not know
// about this certificate revocation.
// It is responsibility of the SDK user to update peers and set this certificate in every peer revocation list.
func (f *FabricCAClient) Revoke(identity *Identity, request *CARevocationRequest) (*CaRevokeResult, error) {
reqJson, err := json.Marshal(request)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/revoke", f.Url), bytes.NewBuffer(reqJson))
httpReq.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, reqJson)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caRevokeResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
return &result.Result, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// ReEnroll create new certificate from old one. Useful when certificate is about to expire.
// Difference with `Enroll` is that `Enroll` require identity with `Registar` role.
// In re-enrolment the old certificate is used to identify the identity.
func (f *FabricCAClient) ReEnroll(request CaReEnrollmentRequest) (*Identity, []byte, error) {
if request.Identity == nil || request.Identity.EnrollmentId() == "" {
return nil, nil, ErrCertificateEmpty
}
// create new cert and send it to CA for signing
key, err := f.Crypto.GenerateKey()
if err != nil {
return nil, nil, err
}
var hosts []string
if len(request.Hosts) == 0 {
parsedUrl, err := url.Parse(f.Url)
if err != nil {
return nil, nil, err
}
hosts = []string{parsedUrl.Host}
} else {
hosts = request.Hosts
}
csr, err := f.Crypto.CreateCertificateRequest(request.Identity.EnrollmentId(), key, hosts)
if err != nil {
return nil, nil, err
}
crm, err := json.Marshal(certificateRequest{CR: string(csr), CaEnrollmentRequest: CaEnrollmentRequest{
Attrs: request.Attrs,
Profile: request.Profile,
CAName: request.CAName,
Hosts: request.Hosts,
Label: request.Label,
}})
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/reenroll", f.Url), bytes.NewBuffer(crm))
req.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(request.Identity, crm)
if err != nil {
return nil, nil, err
}
req.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
enrResp := new(enrollmentResponse)
if err := json.Unmarshal(body, enrResp); err != nil {
return nil, nil, err
}
if !enrResp.Success {
return nil, nil, concatErrors(enrResp.Errors)
}
rawCert, err := base64.StdEncoding.DecodeString(enrResp.Result.Cert)
if err != nil {
return nil, nil, err
}
a, _ := pem.Decode(rawCert)
cert, err := x509.ParseCertificate(a.Bytes)
if err != nil {
return nil, nil, err
}
return &Identity{Certificate: cert, PrivateKey: key, MspId: f.MspId}, csr, nil
}
return nil, nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// GetCaCertificateChain gets root and intermediate certificates used by FabricCA server.
// This certificates must be presented to Fabric entities (peers, orderers) as MSP so they can verify that request
// are from valid entities.
// caName is the name of the CA that should be used. FabricCa support more than one CA server on same endpoint and
// this names are used to distinguish between them. If empty default CA instance will be used.
func (f *FabricCAClient) GetCaCertificateChain(caName string) (*CAGetCertsResponse, error) {
reqJson, err := json.Marshal(caInfoRequest{CaName: caName})
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/cainfo", f.Url), bytes.NewBuffer(reqJson))
httpReq.Header.Set("Content-Type", "application/json")
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caInfoResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
certs, err := base64.StdEncoding.DecodeString(result.Result.CAChain)
if err != nil {
return nil, err
}
var root []*pem.Block
var intermediate []*pem.Block
for len(certs) > 0 {
var block *pem.Block
block, certs = pem.Decode(certs)
if block == nil {
break
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("error parsing certificate from ca chain")
}
if !cert.IsCA {
return nil, fmt.Errorf("invalid certificate in ca chain")
}
// If authority key id is not present or if it is present and equal to subject key id,
// then it is a root certificate
if len(cert.AuthorityKeyId) == 0 || bytes.Equal(cert.AuthorityKeyId, cert.SubjectKeyId) {
root = append(root, block)
} else {
intermediate = append(intermediate, block)
}
}
return &CAGetCertsResponse{
RootCertificates: root,
IntermediateCertificates: intermediate,
Version: result.Result.Version,
CAName: result.Result.CAName}, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// ListAffiliations get list of all affiliations registered in FabricCa.
// If `path` is specified result will contains only affiliations "bellow" path, else full tree will be returned.
func (f *FabricCAClient) ListAffiliations(identity *Identity, path string, caName string) (*CAAffiliationResponse, error) {
if identity == nil {
return nil, ErrCertificateEmpty
}
var uri string
if len(path) > 0 {
uri = fmt.Sprintf("%s/api/v1/affiliations/%s", f.Url, path)
} else {
uri = fmt.Sprintf("%s/api/v1/affiliations", f.Url)
}
httpReq, err := http.NewRequest("GET", uri, bytes.NewBuffer(nil))
token, err := f.createAuthToken(identity, nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)
if len(caName) > 0 {
uri := httpReq.URL.Query()
uri.Add("ca", caName)
httpReq.URL.RawQuery = uri.Encode()
}
httpClient := &http.Client{Transport: f.getTransport()}
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caAffiliationResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
if len(result.Errors) > 0 {
return nil, concatErrors(result.Errors)
}
return &result.Result, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// AddAffiliation add new affiliation to FabricCa
func (f *FabricCAClient) AddAffiliation(identity *Identity, req CAAddAffiliationRequest) (*CAAffiliationResponse, error) {
if identity == nil {
return nil, ErrCertificateEmpty
}
if len(req.Name) == 0 {
return nil, ErrAffiliationNameMissing
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/affiliations", f.Url), bytes.NewBuffer(reqJson))
httpReq.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, reqJson)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
uri := httpReq.URL.Query()
uri.Add("force", strconv.FormatBool(req.Force))
httpReq.URL.RawQuery = uri.Encode()
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caAffiliationResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
if len(result.Errors) > 0 {
return nil, concatErrors(result.Errors)
}
return &result.Result, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// RemoveAffiliation remove affiliation from FabricCa server. FabricCa server must be configured to allows removal of
// affiliations.
func (f *FabricCAClient) RemoveAffiliation(identity *Identity, req CARemoveAffiliationRequest) (*CAAffiliationResponse, error) {
if identity == nil {
return nil, ErrCertificateEmpty
}
if len(req.Name) == 0 {
return nil, ErrAffiliationNameMissing
}
httpReq, err := http.NewRequest("DELETE",
fmt.Sprintf("%s/api/v1/affiliations/%s", f.Url, req.Name),
bytes.NewBuffer(nil))
httpReq.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
uri := httpReq.URL.Query()
uri.Add("force", strconv.FormatBool(req.Force))
uri.Add("ca", req.CAName)
httpReq.URL.RawQuery = uri.Encode()
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caAffiliationResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
if len(result.Errors) > 0 {
return nil, concatErrors(result.Errors)
}
return &result.Result, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// ModifyAffiliation will modify existing affiliation
func (f *FabricCAClient) ModifyAffiliation(identity *Identity, req CAModifyAffiliationRequest) (*CAAffiliationResponse, error) {
if identity == nil {
return nil, ErrCertificateEmpty
}
if len(req.Name) == 0 {
return nil, ErrAffiliationNameMissing
}
if len(req.NewName) == 0 {
return nil, ErrAffiliationNewNameMissing
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
httpReq, err := http.NewRequest("PUT",
fmt.Sprintf("%s/api/v1/affiliations/%s", f.Url, req.Name),
bytes.NewBuffer(reqJson))
httpReq.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, reqJson)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)
httpClient := &http.Client{Transport: f.getTransport()}
uri := httpReq.URL.Query()
uri.Add("force", strconv.FormatBool(req.Force))
httpReq.URL.RawQuery = uri.Encode()
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
result := new(caAffiliationResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
if !result.Success {
return nil, concatErrors(result.Errors)
}
if len(result.Errors) > 0 {
return nil, concatErrors(result.Errors)
}
return &result.Result, nil
}
return nil, fmt.Errorf("non 200 response: %v message is: %s", resp.StatusCode, string(body))
}
// ListAllIdentities get list of all identities from FabricCa server
func (f *FabricCAClient) ListAllIdentities(identity *Identity, caName string) (*CAListAllIdentitiesResponse, error) {
if identity == nil {
return nil, ErrCertificateEmpty
}
httpReq, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/identities", f.Url), bytes.NewBuffer(nil))
token, err := f.createAuthToken(identity, nil)
if err != nil {
return nil, err
}
httpReq.Header.Set("authorization", token)