github.com/kaituanwang/hyperledger@v2.0.1+incompatible/bccsp/factory/pkcs11.go (about)

     1  // +build pkcs11
     2  
     3  /*
     4  Copyright IBM Corp. 2017 All Rights Reserved.
     5  
     6  Licensed under the Apache License, Version 2.0 (the "License");
     7  you may not use this file except in compliance with the License.
     8  You may obtain a copy of the License at
     9  
    10  		 http://www.apache.org/licenses/LICENSE-2.0
    11  
    12  Unless required by applicable law or agreed to in writing, software
    13  distributed under the License is distributed on an "AS IS" BASIS,
    14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  See the License for the specific language governing permissions and
    16  limitations under the License.
    17  */
    18  package factory
    19  
    20  import (
    21  	"github.com/hyperledger/fabric/bccsp"
    22  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    23  	"github.com/pkg/errors"
    24  )
    25  
    26  // FactoryOpts holds configuration information used to initialize factory implementations
    27  type FactoryOpts struct {
    28  	ProviderName string             `mapstructure:"default" json:"default" yaml:"Default"`
    29  	SwOpts       *SwOpts            `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
    30  	Pkcs11Opts   *pkcs11.PKCS11Opts `mapstructure:"PKCS11,omitempty" json:"PKCS11,omitempty" yaml:"PKCS11"`
    31  }
    32  
    33  // InitFactories must be called before using factory interfaces
    34  // It is acceptable to call with config = nil, in which case
    35  // some defaults will get used
    36  // Error is returned only if defaultBCCSP cannot be found
    37  func InitFactories(config *FactoryOpts) error {
    38  	factoriesInitOnce.Do(func() {
    39  		setFactories(config)
    40  	})
    41  
    42  	return factoriesInitError
    43  }
    44  
    45  func setFactories(config *FactoryOpts) error {
    46  	// Take some precautions on default opts
    47  	if config == nil {
    48  		config = GetDefaultOpts()
    49  	}
    50  
    51  	if config.ProviderName == "" {
    52  		config.ProviderName = "SW"
    53  	}
    54  
    55  	if config.SwOpts == nil {
    56  		config.SwOpts = GetDefaultOpts().SwOpts
    57  	}
    58  
    59  	// Initialize factories map
    60  	bccspMap = make(map[string]bccsp.BCCSP)
    61  
    62  	// Software-Based BCCSP
    63  	if config.SwOpts != nil {
    64  		f := &SWFactory{}
    65  		err := initBCCSP(f, config)
    66  		if err != nil {
    67  			factoriesInitError = errors.Wrap(err, "Failed initializing SW.BCCSP")
    68  		}
    69  	}
    70  
    71  	// PKCS11-Based BCCSP
    72  	if config.Pkcs11Opts != nil {
    73  		f := &PKCS11Factory{}
    74  		err := initBCCSP(f, config)
    75  		if err != nil {
    76  			factoriesInitError = errors.Wrapf(err, "Failed initializing PKCS11.BCCSP %s", factoriesInitError)
    77  		}
    78  	}
    79  
    80  	var ok bool
    81  	DefaultBCCSP, ok = bccspMap[config.ProviderName]
    82  	if !ok {
    83  		factoriesInitError = errors.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
    84  	}
    85  
    86  	return factoriesInitError
    87  }
    88  
    89  // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
    90  func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
    91  	var f BCCSPFactory
    92  	switch config.ProviderName {
    93  	case "SW":
    94  		f = &SWFactory{}
    95  	case "PKCS11":
    96  		f = &PKCS11Factory{}
    97  	default:
    98  		return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.ProviderName)
    99  	}
   100  
   101  	csp, err := f.Get(config)
   102  	if err != nil {
   103  		return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name())
   104  	}
   105  	return csp, nil
   106  }