github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/bccsp/factory/pkcs11factory.go (about)

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