github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/factory/pkcs11.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 factory 10 11 import ( 12 "github.com/hyperledger/fabric/bccsp" 13 "github.com/hyperledger/fabric/bccsp/pkcs11" 14 "github.com/pkg/errors" 15 ) 16 17 const pkcs11Enabled = false 18 19 // FactoryOpts holds configuration information used to initialize factory implementations 20 type FactoryOpts struct { 21 ProviderName string `mapstructure:"default" json:"default" yaml:"Default"` 22 SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"` 23 Pkcs11Opts *pkcs11.PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"` 24 } 25 26 // InitFactories must be called before using factory interfaces 27 // It is acceptable to call with config = nil, in which case 28 // some defaults will get used 29 // Error is returned only if defaultBCCSP cannot be found 30 func InitFactories(config *FactoryOpts) error { 31 factoriesInitOnce.Do(func() { 32 factoriesInitError = initFactories(config) 33 }) 34 35 return factoriesInitError 36 } 37 38 func initFactories(config *FactoryOpts) error { 39 // Take some precautions on default opts 40 if config == nil { 41 config = GetDefaultOpts() 42 } 43 44 if config.ProviderName == "" { 45 config.ProviderName = "SW" 46 } 47 48 if config.SwOpts == nil { 49 config.SwOpts = GetDefaultOpts().SwOpts 50 } 51 52 // Software-Based BCCSP 53 if config.ProviderName == "SW" && config.SwOpts != nil { 54 f := &SWFactory{} 55 var err error 56 defaultBCCSP, err = initBCCSP(f, config) 57 if err != nil { 58 return errors.Wrap(err, "Failed initializing SW.BCCSP") 59 } 60 } 61 62 // PKCS11-Based BCCSP 63 if config.ProviderName == "PKCS11" && config.Pkcs11Opts != nil { 64 f := &PKCS11Factory{} 65 var err error 66 defaultBCCSP, err = initBCCSP(f, config) 67 if err != nil { 68 return errors.Wrapf(err, "Failed initializing PKCS11.BCCSP") 69 } 70 } 71 72 if defaultBCCSP == nil { 73 return errors.Errorf("Could not find default `%s` BCCSP", config.ProviderName) 74 } 75 76 return nil 77 } 78 79 // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input. 80 func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) { 81 var f BCCSPFactory 82 switch config.ProviderName { 83 case "SW": 84 f = &SWFactory{} 85 case "PKCS11": 86 f = &PKCS11Factory{} 87 default: 88 return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName) 89 } 90 91 csp, err := f.Get(config) 92 if err != nil { 93 return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name()) 94 } 95 return csp, nil 96 }