github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/factory/pkcs11factory.go (about)

     1  // +build pkcs11
     2  
     3  /*
     4  Copyright IBM Corp. 2016 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/hellobchain/third_party/hyperledger/fabric/bccsp"
    22  	"github.com/hellobchain/third_party/hyperledger/fabric/bccsp/pkcs11"
    23  	"github.com/hellobchain/third_party/hyperledger/fabric/bccsp/sw"
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  const (
    28  	// PKCS11BasedFactoryName is the name of the factory of the hsm-based BCCSP implementation
    29  	PKCS11BasedFactoryName = "PKCS11"
    30  )
    31  
    32  // PKCS11Factory is the factory of the HSM-based BCCSP.
    33  type PKCS11Factory struct{}
    34  
    35  // Name returns the name of this factory
    36  func (f *PKCS11Factory) Name() string {
    37  	return PKCS11BasedFactoryName
    38  }
    39  
    40  // Get returns an instance of BCCSP using Opts.
    41  func (f *PKCS11Factory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
    42  	// Validate arguments
    43  	if config == nil || config.Pkcs11Opts == nil {
    44  		return nil, errors.New("Invalid config. It must not be nil.")
    45  	}
    46  
    47  	p11Opts := config.Pkcs11Opts
    48  
    49  	//TODO: PKCS11 does not need a keystore, but we have not migrated all of PKCS11 BCCSP to PKCS11 yet
    50  	var ks bccsp.KeyStore
    51  	if p11Opts.Ephemeral == true {
    52  		ks = sw.NewDummyKeyStore()
    53  	} else if p11Opts.FileKeystore != nil {
    54  		fks, err := sw.NewFileBasedKeyStore(nil, p11Opts.FileKeystore.KeyStorePath, false)
    55  		if err != nil {
    56  			return nil, errors.Wrapf(err, "Failed to initialize software key store")
    57  		}
    58  		ks = fks
    59  	} else {
    60  		// Default to DummyKeystore
    61  		ks = sw.NewDummyKeyStore()
    62  	}
    63  	return pkcs11.New(*p11Opts, ks)
    64  }