github.com/hxx258456/fabric-ca-gm@v0.0.3-0.20221111064038-a268ad7e3a37/internal/pkg/util/configurebccsp.go (about)

     1  //go:build pkcs11
     2  // +build pkcs11
     3  
     4  /*
     5  Copyright IBM Corp. All Rights Reserved.
     6  
     7  SPDX-License-Identifier: Apache-2.0
     8  */
     9  
    10  package util
    11  
    12  import (
    13  	"path"
    14  	"strings"
    15  
    16  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    17  	"github.com/hxx258456/fabric-gm/bccsp"
    18  	"github.com/hxx258456/fabric-gm/bccsp/factory"
    19  	"github.com/hxx258456/fabric-gm/bccsp/pkcs11"
    20  	"github.com/pkg/errors"
    21  )
    22  
    23  // ConfigureBCCSP configures BCCSP, using
    24  func ConfigureBCCSP(optsPtr **factory.FactoryOpts, mspDir, homeDir string) error {
    25  	var err error
    26  	if optsPtr == nil {
    27  		return errors.New("nil argument not allowed")
    28  	}
    29  	opts := *optsPtr
    30  	if opts == nil {
    31  		opts = &factory.FactoryOpts{}
    32  	}
    33  
    34  	if opts.ProviderName == "" {
    35  		opts.ProviderName = "SW"
    36  	}
    37  	SetProviderName(opts.ProviderName)
    38  	if opts.SwOpts == nil {
    39  		opts.SwOpts = &factory.SwOpts{}
    40  	}
    41  	if opts.SwOpts.HashFamily == "" {
    42  		opts.SwOpts.HashFamily = bccsp.SM3
    43  	}
    44  	if opts.SwOpts.SecLevel == 0 {
    45  		opts.SwOpts.SecLevel = 256
    46  	}
    47  	if opts.SwOpts.FileKeystore == nil {
    48  		opts.SwOpts.FileKeystore = &factory.FileKeystoreOpts{}
    49  	}
    50  	// The mspDir overrides the KeyStorePath; otherwise, if not set, set default
    51  	if mspDir != "" {
    52  		opts.SwOpts.FileKeystore.KeyStorePath = path.Join(mspDir, "keystore")
    53  	} else if opts.SwOpts.FileKeystore.KeyStorePath == "" {
    54  		opts.SwOpts.FileKeystore.KeyStorePath = path.Join("msp", "keystore")
    55  	}
    56  
    57  	err = makeFileNamesAbsolute(opts, homeDir)
    58  	if err != nil {
    59  		return errors.WithMessage(err, "Failed to make BCCSP files absolute")
    60  	}
    61  	log.Debugf("Initializing BCCSP: %+v", opts)
    62  	if opts.SwOpts != nil {
    63  		log.Debugf("Initializing BCCSP with software options %+v", opts.SwOpts)
    64  	}
    65  	if opts.Pkcs11Opts != nil {
    66  		log.Debugf("Initializing BCCSP with PKCS11 options %+v", sanitizePKCS11Opts(*opts.Pkcs11Opts))
    67  	}
    68  	// TODO 这里是否还需要调用InitFactories?
    69  	// Init the BCCSP factories
    70  	err = factory.InitFactories(opts)
    71  	if err != nil {
    72  		return errors.WithMessage(err, "Failed to initialize BCCSP Factories")
    73  	}
    74  	*optsPtr = opts
    75  	return nil
    76  }
    77  
    78  // redacts label and pin from PKCS11 opts
    79  func sanitizePKCS11Opts(opts pkcs11.PKCS11Opts) pkcs11.PKCS11Opts {
    80  	mask := strings.Repeat("*", 6)
    81  	opts.Pin = mask
    82  	opts.Label = mask
    83  	return opts
    84  }