github.com/hyperledger/fabric-ca@v2.0.0-alpha.0.20201120210307-7b4f34729db1+incompatible/internal/pkg/util/configurebccsp.go (about)

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