@@ -20,6 +20,7 @@ import (
2020 "net/url"
2121 "os"
2222 "path"
23+ "path/filepath"
2324 "strconv"
2425 "strings"
2526
@@ -110,6 +111,17 @@ func (c *Client) Init() error {
110111 return errors .Wrap (err , "Failed to create cacerts directory" )
111112 }
112113
114+ // CA's Idemix public key
115+ c .ipkFile = filepath .Join (mspDir , "IssuerPublicKey" )
116+
117+ // Idemix credentials directory
118+ c .idemixCredsDir = path .Join (mspDir , "user" )
119+ err = os .MkdirAll (c .idemixCredsDir , 0755 )
120+ if err != nil {
121+ return errors .Wrap (err , "Failed to create Idemix credentials directory 'user'" )
122+ }
123+ c .idemixCredFile = path .Join (c .idemixCredsDir , "SignerConfig" )
124+
113125 c .csp = cfg .CSP
114126 // Create http.Client object and associate it with this client
115127 err = c .initHTTPClient ()
@@ -132,6 +144,8 @@ func (c *Client) initHTTPClient() error {
132144 if err2 != nil {
133145 return fmt .Errorf ("Failed to get client TLS config: %s" , err2 )
134146 }
147+ // set the default ciphers
148+ tlsConfig .CipherSuites = tls .DefaultCipherSuites
135149 tr .TLSClientConfig = tlsConfig
136150 }
137151 c .httpClient = & http.Client {Transport : tr }
@@ -204,6 +218,56 @@ func (c *Client) net2LocalServerInfo(net *common.CAInfoResponseNet, local *GetCA
204218 return nil
205219}
206220
221+ func (c * Client ) handleX509Enroll (req * api.EnrollmentRequest ) (* EnrollmentResponse , error ) {
222+ // Generate the CSR
223+ csrPEM , key , err := c .GenCSR (req .CSR , req .Name )
224+ if err != nil {
225+ return nil , errors .WithMessage (err , "Failure generating CSR" )
226+ }
227+
228+ reqNet := & api.EnrollmentRequestNet {
229+ CAName : req .CAName ,
230+ AttrReqs : req .AttrReqs ,
231+ }
232+
233+ if req .CSR != nil {
234+ reqNet .SignRequest .Hosts = req .CSR .Hosts
235+ }
236+ reqNet .SignRequest .Request = string (csrPEM )
237+ reqNet .SignRequest .Profile = req .Profile
238+ reqNet .SignRequest .Label = req .Label
239+
240+ body , err := util .Marshal (reqNet , "SignRequest" )
241+ if err != nil {
242+ return nil , err
243+ }
244+
245+ // Send the CSR to the fabric-ca server with basic auth header
246+ post , err := c .newPost ("enroll" , body )
247+ if err != nil {
248+ return nil , err
249+ }
250+ post .SetBasicAuth (req .Name , req .Secret )
251+ var result common.EnrollmentResponseNet
252+ err = c .SendReq (post , & result )
253+ if err != nil {
254+ return nil , err
255+ }
256+
257+ // Create the enrollment response
258+ return c .newEnrollmentResponse (& result , req .Name , key )
259+ }
260+
261+ // Handles enrollment request for an Idemix credential
262+ // 1. Sends a request with empty body to the /api/v1/idemix/credentail REST endpoint
263+ // of the server to get a Nonce from the CA
264+ // 2. Constructs a credential request using the nonce, CA's idemix public key
265+ // 3. Sends a request with the CredentialRequest object in the body to the
266+ // /api/v1/idemix/credentail REST endpoint to get a credential
267+ func (c * Client ) handleIdemixEnroll (req * api.EnrollmentRequest ) (* EnrollmentResponse , error ) {
268+ return nil , errors .New ("idemix enroll not supported" )
269+ }
270+
207271// newEnrollmentResponse creates a client enrollment response from a network response
208272// @param result The result from server
209273// @param id Name of identity being enrolled or reenrolled
@@ -218,7 +282,6 @@ func (c *Client) newEnrollmentResponse(result *common.EnrollmentResponseNet, id
218282 if err != nil {
219283 return nil , err
220284 }
221-
222285 x509Cred := x509cred .NewCredential (key , certByte , c )
223286 err = x509Cred .SetVal (signer )
224287 if err != nil {
@@ -287,6 +350,16 @@ func (c *Client) NewIdentity(creds []credential.Credential) (*Identity, error) {
287350 return NewIdentity (c , name , creds ), nil
288351}
289352
353+ // NewX509Identity creates a new identity
354+ func (c * Client ) NewX509Identity (name string , creds []credential.Credential ) x509cred.Identity {
355+ return NewIdentity (c , name , creds )
356+ }
357+
358+ // GetCSP returns BCCSP instance associated with this client
359+ func (c * Client ) GetCSP () core.CryptoSuite {
360+ return c .csp
361+ }
362+
290363// newGet create a new GET request
291364func (c * Client ) newGet (endpoint string ) (* http.Request , error ) {
292365 curl , err := c .getURL (endpoint )
@@ -442,6 +515,24 @@ func (c *Client) getURL(endpoint string) (string, error) {
442515 return rtn , nil
443516}
444517
518+ func (c * Client ) checkX509Enrollment () error {
519+ keyFileExists := util .FileExists (c .keyFile )
520+ certFileExists := util .FileExists (c .certFile )
521+ if keyFileExists && certFileExists {
522+ return nil
523+ }
524+ // If key file does not exist, but certFile does, key file is probably
525+ // stored by bccsp, so check to see if this is the case
526+ if certFileExists {
527+ _ , _ , _ , err := util .GetSignerFromCertFile (c .certFile , c .csp )
528+ if err == nil {
529+ // Yes, the key is stored by BCCSP
530+ return nil
531+ }
532+ }
533+ return errors .New ("X509 enrollment information does not exist" )
534+ }
535+
445536func newCfsslBasicKeyRequest (bkr * api.BasicKeyRequest ) * csr.BasicKeyRequest {
446537 return & csr.BasicKeyRequest {A : bkr .Algo , S : bkr .Size }
447538}
@@ -481,86 +572,3 @@ func NormalizeURL(addr string) (*url.URL, error) {
481572 }
482573 return u , nil
483574}
484-
485- // Handles enrollment request for an Idemix credential
486- // 1. Sends a request with empty body to the /api/v1/idemix/credentail REST endpoint
487- // of the server to get a Nonce from the CA
488- // 2. Constructs a credential request using the nonce, CA's idemix public key
489- // 3. Sends a request with the CredentialRequest object in the body to the
490- // /api/v1/idemix/credentail REST endpoint to get a credential
491- func (c * Client ) handleIdemixEnroll (req * api.EnrollmentRequest ) (* EnrollmentResponse , error ) {
492- log .Debugf ("Getting nonce from CA %s" , req .CAName )
493- return nil , errors .New ("idemix enroll not supported" )
494- }
495-
496- func (c * Client ) checkX509Enrollment () error {
497- keyFileExists := util .FileExists (c .keyFile )
498- certFileExists := util .FileExists (c .certFile )
499- if keyFileExists && certFileExists {
500- return nil
501- }
502- // If key file does not exist, but certFile does, key file is probably
503- // stored by bccsp, so check to see if this is the case
504- if certFileExists {
505- certBytes , err := util .ReadFile (c .certFile )
506- if err != nil {
507- return err
508- }
509- _ , _ , _ , err = util .GetSignerFromCertFile (certBytes , c .csp )
510- if err == nil {
511- // Yes, the key is stored by BCCSP
512- return nil
513- }
514- }
515- return errors .New ("X509 enrollment information does not exist" )
516- }
517-
518- func (c * Client ) handleX509Enroll (req * api.EnrollmentRequest ) (* EnrollmentResponse , error ) {
519- // Generate the CSR
520- csrPEM , key , err := c .GenCSR (req .CSR , req .Name )
521- if err != nil {
522- return nil , errors .WithMessage (err , "Failure generating CSR" )
523- }
524-
525- reqNet := & api.EnrollmentRequestNet {
526- CAName : req .CAName ,
527- AttrReqs : req .AttrReqs ,
528- }
529-
530- if req .CSR != nil {
531- reqNet .SignRequest .Hosts = req .CSR .Hosts
532- }
533- reqNet .SignRequest .Request = string (csrPEM )
534- reqNet .SignRequest .Profile = req .Profile
535- reqNet .SignRequest .Label = req .Label
536-
537- body , err := util .Marshal (reqNet , "SignRequest" )
538- if err != nil {
539- return nil , err
540- }
541-
542- // Send the CSR to the fabric-ca server with basic auth header
543- post , err := c .newPost ("enroll" , body )
544- if err != nil {
545- return nil , err
546- }
547- post .SetBasicAuth (req .Name , req .Secret )
548- var result common.EnrollmentResponseNet
549- err = c .SendReq (post , & result )
550- if err != nil {
551- return nil , err
552- }
553-
554- // Create the enrollment response
555- return c .newEnrollmentResponse (& result , req .Name , key )
556- }
557-
558- // GetCSP returns BCCSP instance associated with this client
559- func (c * Client ) GetCSP () core.CryptoSuite {
560- return c .csp
561- }
562-
563- // NewX509Identity creates a new identity
564- func (c * Client ) NewX509Identity (name string , creds []credential.Credential ) x509cred.Identity {
565- return NewIdentity (c , name , creds )
566- }
0 commit comments