github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/bccsp/factory/pkcs11.go (about)

     1  // +build !nopkcs11
     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  	"fmt"
    22  
    23  	"github.com/hyperledger/fabric/bccsp"
    24  	"github.com/hyperledger/fabric/bccsp/pkcs11"
    25  )
    26  
    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  		// Take some precautions on default opts
    40  		if config == nil {
    41  			config = &DefaultOpts
    42  		}
    43  
    44  		if config.ProviderName == "" {
    45  			config.ProviderName = "SW"
    46  		}
    47  
    48  		if config.SwOpts == nil {
    49  			config.SwOpts = DefaultOpts.SwOpts
    50  		}
    51  
    52  		// Initialize factories map
    53  		bccspMap = make(map[string]bccsp.BCCSP)
    54  
    55  		// Software-Based BCCSP
    56  		if config.SwOpts != nil {
    57  			f := &SWFactory{}
    58  			err := initBCCSP(f, config)
    59  			if err != nil {
    60  				factoriesInitError = fmt.Errorf("[%s]", err)
    61  			}
    62  		}
    63  
    64  		// PKCS11-Based BCCSP
    65  		if config.Pkcs11Opts != nil {
    66  			f := &PKCS11Factory{}
    67  			err := initBCCSP(f, config)
    68  			if err != nil {
    69  				factoriesInitError = fmt.Errorf("%s\n[%s]", factoriesInitError, err)
    70  			}
    71  		}
    72  
    73  		var ok bool
    74  		defaultBCCSP, ok = bccspMap[config.ProviderName]
    75  		if !ok {
    76  			factoriesInitError = fmt.Errorf("%s\nCould not find default `%s` BCCSP", factoriesInitError, config.ProviderName)
    77  		}
    78  	})
    79  
    80  	return factoriesInitError
    81  }
    82  
    83  // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
    84  func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
    85  	var f BCCSPFactory
    86  	switch config.ProviderName {
    87  	case "SW":
    88  		f = &SWFactory{}
    89  	case "PKCS11":
    90  		f = &PKCS11Factory{}
    91  	}
    92  
    93  	csp, err := f.Get(config)
    94  	if err != nil {
    95  		return nil, fmt.Errorf("Could not initialize BCCSP %s [%s]", f.Name(), err)
    96  	}
    97  	return csp, nil
    98  }