github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/bccsp/factory/pkcs11.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/hechain20/hechain/bccsp/pkcs11"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  const pkcs11Enabled = false
    19  
    20  // FactoryOpts holds configuration information used to initialize factory implementations
    21  type FactoryOpts struct {
    22  	Default string             `json:"default" yaml:"Default"`
    23  	SW      *SwOpts            `json:"SW,omitempty" yaml:"SW,omitempty"`
    24  	PKCS11  *pkcs11.PKCS11Opts `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.Default == "" {
    46  		config.Default = "SW"
    47  	}
    48  
    49  	if config.SW == nil {
    50  		config.SW = GetDefaultOpts().SW
    51  	}
    52  
    53  	// Software-Based BCCSP
    54  	if config.Default == "SW" && config.SW != nil {
    55  		f := &SWFactory{}
    56  		var err error
    57  		defaultBCCSP, err = initBCCSP(f, config)
    58  		if err != nil {
    59  			return errors.Wrap(err, "Failed initializing SW.BCCSP")
    60  		}
    61  	}
    62  
    63  	// PKCS11-Based BCCSP
    64  	if config.Default == "PKCS11" && config.PKCS11 != nil {
    65  		f := &PKCS11Factory{}
    66  		var err error
    67  		defaultBCCSP, err = initBCCSP(f, config)
    68  		if err != nil {
    69  			return errors.Wrapf(err, "Failed initializing PKCS11.BCCSP")
    70  		}
    71  	}
    72  
    73  	if defaultBCCSP == nil {
    74  		return errors.Errorf("Could not find default `%s` BCCSP", config.Default)
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  // GetBCCSPFromOpts returns a BCCSP created according to the options passed in input.
    81  func GetBCCSPFromOpts(config *FactoryOpts) (bccsp.BCCSP, error) {
    82  	var f BCCSPFactory
    83  	switch config.Default {
    84  	case "SW":
    85  		f = &SWFactory{}
    86  	case "PKCS11":
    87  		f = &PKCS11Factory{}
    88  	default:
    89  		return nil, errors.Errorf("Could not find BCCSP, no '%s' provider", config.Default)
    90  	}
    91  
    92  	csp, err := f.Get(config)
    93  	if err != nil {
    94  		return nil, errors.Wrapf(err, "Could not initialize BCCSP %s", f.Name())
    95  	}
    96  	return csp, nil
    97  }