github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/bccsp/factory/nopkcs11.go (about) 1 //go:build !pkcs11 2 // +build !pkcs11 3 4 /* 5 Copyright hechain. All Rights Reserved. 6 7 SPDX-License-Identifier: Apache-2.0 8 */ 9 10 package factory 11 12 import ( 13 "github.com/hechain20/hechain/bccsp" 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 Default string `json:"default" yaml:"Default"` 22 SW *SwOpts `json:"SW,omitempty" yaml:"SW,omitempty"` 23 } 24 25 // InitFactories must be called before using factory interfaces 26 // It is acceptable to call with config = nil, in which case 27 // some defaults will get used 28 // Error is returned only if defaultBCCSP cannot be found 29 func InitFactories(config *FactoryOpts) error { 30 factoriesInitOnce.Do(func() { 31 factoriesInitError = initFactories(config) 32 }) 33 34 return factoriesInitError 35 } 36 37 func initFactories(config *FactoryOpts) error { 38 // Take some precautions on default opts 39 if config == nil { 40 config = GetDefaultOpts() 41 } 42 43 if config.Default == "" { 44 config.Default = "SW" 45 } 46 47 if config.SW == nil { 48 config.SW = GetDefaultOpts().SW 49 } 50 51 // Software-Based BCCSP 52 if config.Default == "SW" && config.SW != nil { 53 f := &SWFactory{} 54 var err error 55 defaultBCCSP, err = initBCCSP(f, config) 56 if err != nil { 57 return errors.Wrapf(err, "Failed initializing BCCSP") 58 } 59 } 60 61 if defaultBCCSP == nil { 62 return errors.Errorf("Could not find default `%s` BCCSP", config.Default) 63 } 64 65 return nil 66 } 67 68 // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input. 69 func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) { 70 var f BCCSPFactory 71 switch config.Default { 72 case "SW": 73 f = &SWFactory{} 74 default: 75 return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.Default) 76 } 77 78 csp, err := f.Get(config) 79 if err != nil { 80 return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name()) 81 } 82 return csp, nil 83 }