github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/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/hellobchain/third_party/hyperledger/fabric/bccsp" 13 "github.com/hellobchain/third_party/hyperledger/fabric/bccsp/pkcs11" 14 "github.com/pkg/errors" 15 ) 16 17 const pkcs11Enabled = true 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 PluginOpts *PluginOpts `mapstructure:"PLUGIN,omitempty" json:"PLUGIN,omitempty" yaml:"PluginOpts"` 24 Pkcs11Opts *pkcs11.PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"` 25 } 26 27 // InitFactories must be called before using factory interfaces 28 // It is acceptable to call with config = nil, in which case 29 // some defaults will get used 30 // Error is returned only if defaultBCCSP cannot be found 31 func InitFactories(config *FactoryOpts) error { 32 factoriesInitOnce.Do(func() { 33 factoriesInitError = initFactories(config) 34 }) 35 36 return factoriesInitError 37 } 38 39 func initFactories(config *FactoryOpts) error { 40 // Take some precautions on default opts 41 if config == nil { 42 config = GetDefaultOpts() 43 } 44 45 if config.ProviderName == "" { 46 config.ProviderName = "SW" 47 } 48 49 if config.SwOpts == nil { 50 config.SwOpts = GetDefaultOpts().SwOpts 51 } 52 53 // Initialize factories map 54 bccspMap = make(map[string]bccsp.BCCSP) 55 56 // Software-Based BCCSP 57 if config.ProviderName == "SW" && config.SwOpts != nil { 58 f := &SWFactory{} 59 err := initBCCSP(f, config) 60 if err != nil { 61 return errors.Wrap(err, "Failed initializing SW.BCCSP") 62 } 63 } 64 65 // PKCS11-Based BCCSP 66 if config.ProviderName == "PKCS11" && config.Pkcs11Opts != nil { 67 f := &PKCS11Factory{} 68 err := initBCCSP(f, config) 69 if err != nil { 70 return errors.Wrapf(err, "Failed initializing PKCS11.BCCSP") 71 } 72 } 73 74 // BCCSP Plugin 75 if config.ProviderName == "PLUGIN" && config.PluginOpts != nil { 76 f := &PluginFactory{} 77 err := initBCCSP(f, config) 78 if err != nil { 79 return errors.Wrapf(err, "Failed initializing PLUGIN.BCCSP") 80 } 81 } 82 83 var ok bool 84 defaultBCCSP, ok = bccspMap[config.ProviderName] 85 if !ok { 86 return errors.Errorf("Could not find default `%s` BCCSP", config.ProviderName) 87 } 88 89 return nil 90 } 91 92 // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input. 93 func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) { 94 var f BCCSPFactory 95 switch config.ProviderName { 96 case "SW": 97 f = &SWFactory{} 98 case "PKCS11": 99 f = &PKCS11Factory{} 100 case "PLUGIN": 101 f = &PluginFactory{} 102 default: 103 return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName) 104 } 105 106 csp, err := f.Get(config) 107 if err != nil { 108 return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name()) 109 } 110 return csp, nil 111 }