Skip to content

Commit

Permalink
[FAB-17517] Only Initialize specified provider
Browse files Browse the repository at this point in the history
Code currently tries to initialize multiple providers based on
provided config Opts being nil or not.

This update ensures that only specified provider is initialized
based on ProviderName.

This fixes "Failed initializing PKCS11.BCCSP %!s(<nil>)" error
when the code complied with PKCS11 enabled expect configuration
to not be nil even when Provider is set to SW.

Signed-off-by: Ahmed Sajid <ahmed.sajid@securekey.com>
(cherry picked from commit 2be701459257f9b12243fe15ccc35edf63f4c91b)
  • Loading branch information
ahmedsajid authored and mastersingh24 committed Mar 11, 2020
1 parent 6566d1b commit 00bd23c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bccsp/factory/pkcs11.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func setFactories(config *FactoryOpts) error {
}

// PKCS11-Based BCCSP
if config.Pkcs11Opts != nil {
if config.ProviderName == "PKCS11" && config.Pkcs11Opts != nil {
f := &PKCS11Factory{}
err := initBCCSP(f, config)
if err != nil {
Expand Down
26 changes: 26 additions & 0 deletions bccsp/factory/pkcs11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ func TestSetFactories(t *testing.T) {
assert.NoError(t, err)
}

func TestSetFactoriesWithMultipleProviders(t *testing.T) {
// testing SW Provider and ensuring other providers are not initialized
factoriesInitError = nil

err := setFactories(&FactoryOpts{
ProviderName: "SW",
SwOpts: &SwOpts{},
Pkcs11Opts: &pkcs11.PKCS11Opts{},
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Failed initializing SW.BCCSP")
assert.NotContains(t, err.Error(), "Failed initializing PKCS11.BCCSP")

// testing PKCS11 Provider and ensuring other providers are not initialized
factoriesInitError = nil

err = setFactories(&FactoryOpts{
ProviderName: "PKCS11",
SwOpts: &SwOpts{},
Pkcs11Opts: &pkcs11.PKCS11Opts{},
})
assert.Error(t, err)
assert.Contains(t, err.Error(), "Failed initializing PKCS11.BCCSP")
assert.NotContains(t, err.Error(), "Failed initializing SW.BCCSP")
}

func TestSetFactoriesInvalidArgs(t *testing.T) {
err := setFactories(&FactoryOpts{
ProviderName: "SW",
Expand Down

0 comments on commit 00bd23c

Please sign in to comment.