forked from CognitionFoundry/gohfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caclient.go
418 lines (361 loc) · 12.7 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
/*
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"
)
// CAClient is common interface for Certificate authority services.
type CAClient interface {
// Enroll enrolls user and returns ECert,CSR used for certificate and error
Enroll(enrollmentId, password string) (*Identity, []byte, error)
// Register registers new user in fabric-ca server.
Register(identity *Identity, req *CARegistrationRequest) (*CAResponse, error)
// Revoke revokes ECert in fabric-ca server.
Revoke(identity *Identity, req *CARevocationRequest) (*CAResponse, error)
// ReEnroll create new certificate from old (valid) one.
ReEnroll(identity *Identity) (*Identity, error)
}
// 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`
Affiliation string `json:"affiliation"`
// Attrs are attributes associated with this identity
Attrs []*CARegistrationRequestAttr `json:"attrs"`
}
// CARegistrationRequestAttr 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 CARegistrationRequestAttr struct {
Name string `json:"name"`
Value string `json:"value"`
}
// 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"`
}
// CAResponse represents response message from fabric-ca server
type CAResponse struct {
Success bool `json:"success"`
Result CARegisterCredentialResponse `json:"result"`
Errors []CAResponseErr `json:"errors"`
Messages []string `json:"messages"`
}
// CARegisterCredentialResponse credentials from fabric-ca server registration request
type CARegisterCredentialResponse struct {
Secret string `json:"secret"`
}
// CAResponseErr represents error message from fabric-ca server
type CAResponseErr struct {
Code int `json:"code"`
Message string `json:"message"`
}
// certificateRequest holds certificate request that must be signed by fabric-ca
type CertificateRequest struct {
CR string `json:"certificate_request"`
}
// FabricCAClientImpl is client implementation for fabric-ca server
type FabricCAClientImpl 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
}
// enrollmentResponse is response from fabric-ca server for enrolment that contains created Ecert
type enrollmentResponse struct {
Success bool `json:"success"`
Result enrollmentResponseResult `json:"result"`
Errors []CAResponseErr `json:"errors"`
Messages []string `json:"messages"`
}
type enrollmentResponseResult struct {
Cert string
ServerInfo enrollmentResponseServerInfo
}
type enrollmentResponseServerInfo struct {
CAName string
CAChain string
}
// Register registers new user in fabric-ca server. In registration request attributes, affiliation and
// max enrolments must be set. On success, password will be in CAResponse.Result.Credential.
// If password is not provided, random secret will be generated.
// It is responsibility of the SDK user to ensure passwords are with big entropy.
// Certificate parameter is certificate for user that makes registration and this user MUST have the role for
// registering new users.
func (f *FabricCAClientImpl) Register(identity *Identity, req *CARegistrationRequest) (*CAResponse, error) {
if req.EnrolmentId == "" {
return nil, ErrEnrolmentMissing
}
if req.Affiliation == "" {
return nil, ErrAffiliationMissing
}
if req.Type == "" {
return nil, ErrTypeMissing
}
if identity == nil {
return nil, ErrCertificateEmpty
}
reqJson, err := json.Marshal(req)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/api/v1/register", f.Url)
httpReq, err := http.NewRequest("POST", 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)
var tr *http.Transport
if f.Transport == nil {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: f.SkipTLSVerification},
}
} else {
tr = f.Transport
}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
result := new(CAResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
return result, nil
}
// Enroll execute enrollment request for registered user in fabric-ca server.
// On success new Identity with ECert is returned
func (f *FabricCAClientImpl) Enroll(enrollmentId, password string) (*Identity, []byte, error) {
if len(enrollmentId) < 1 {
return nil, nil, ErrEnrollmentIdMissing
}
// create new cert and send it to CA for signing
key, err := f.Crypto.GenerateKey()
if err != nil {
return nil, nil, err
}
csr, err := f.Crypto.CreateCertificateRequest(enrollmentId, key)
if err != nil {
return nil, nil, err
}
url := fmt.Sprintf("%s/api/v1/enroll", f.Url)
crm, err := json.Marshal(CertificateRequest{CR: string(csr)})
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(crm))
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(enrollmentId, password)
var tr *http.Transport
if f.Transport == nil {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: f.SkipTLSVerification},
}
} else {
tr = f.Transport
}
httpClient := &http.Client{Transport: tr}
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
}
enrResp := new(enrollmentResponse)
if err := json.Unmarshal(body, enrResp); err != nil {
return nil, nil, err
}
if !enrResp.Success {
return nil, nil, ErrEnrollment
}
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}, csr, nil
}
// Revoke revokes ECert in fabric-ca server.
// Note that this request will revoke certificate ONLY in fabric-ca 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 *FabricCAClientImpl) Revoke(identity *Identity, request *CARevocationRequest) (*CAResponse, error) {
reqJson, err := json.Marshal(request)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/api/v1/revoke", f.Url)
httpReq, err := http.NewRequest("POST", 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)
var tr *http.Transport
if f.Transport == nil {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: f.SkipTLSVerification},
}
} else {
tr = f.Transport
}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
result := new(CAResponse)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
return result, nil
}
// ReEnroll create new certificate from old one. Useful when certificate is about to expire. Attributes are preserved.
func (f *FabricCAClientImpl) ReEnroll(identity *Identity) (*Identity, error) {
if identity == nil || identity.EnrollmentId() == "" {
return nil, ErrCertificateEmpty
}
// create new cert and send it to CA for signing
key, err := f.Crypto.GenerateKey()
if err != nil {
return nil, err
}
csr, err := f.Crypto.CreateCertificateRequest(identity.EnrollmentId(), key)
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/api/v1/reenroll", f.Url)
crm, err := json.Marshal(CertificateRequest{CR: string(csr)})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(crm))
req.Header.Set("Content-Type", "application/json")
token, err := f.createAuthToken(identity, crm)
if err != nil {
return nil, err
}
req.Header.Set("authorization", token)
var tr *http.Transport
if f.Transport == nil {
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: f.SkipTLSVerification},
}
} else {
tr = f.Transport
}
httpClient := &http.Client{Transport: tr}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
enrResp := new(enrollmentResponse)
if err := json.Unmarshal(body, enrResp); err != nil {
return nil, err
}
if !enrResp.Success {
return nil, ErrEnrollment
}
rawCert, err := base64.StdEncoding.DecodeString(enrResp.Result.Cert)
if err != nil {
return nil, err
}
a, _ := pem.Decode(rawCert)
cert, err := x509.ParseCertificate(a.Bytes)
if err != nil {
return nil, err
}
return &Identity{Certificate: cert, PrivateKey: key}, nil
}
// createAuthToken creates http authorization header token to verify the request.
// it is composed by base64 encoded Cert concatenated by base64 encoded request signed with Cert private key
func (f *FabricCAClientImpl) createAuthToken(identity *Identity, request []byte) (string, error) {
encPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: identity.Certificate.Raw})
encCert := base64.StdEncoding.EncodeToString(encPem)
body := base64.StdEncoding.EncodeToString(request)
sigString := body + "." + encCert
sig, err := f.Crypto.Sign([]byte(sigString), identity.PrivateKey)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%s", encCert, base64.StdEncoding.EncodeToString(sig)), nil
}
// NewFabricCAClient creates new FabricCAClientImpl
func NewCAClient(path string, transport *http.Transport) (CAClient, error) {
config,err:=NewCAConfig(path)
if err!=nil{
return nil,err
}
var crypto CryptoSuite
switch config.CryptoConfig.Family {
case "ecdsa":
crypto, err = NewECCryptSuiteFromConfig(config.CryptoConfig)
if err != nil {
return nil, err
}
default:
return nil, ErrInvalidAlgorithmFamily
}
return &FabricCAClientImpl{SkipTLSVerification: config.SkipTLSValidation,
Url: config.Uri,
Crypto: crypto,
Transport: transport}, nil
}