Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 28c91d2

Browse files
sudeshrshettytroyronda
authored andcommitted
[FABG-721] PKCS11 resilience
- added ContextHandler which encapsulates pkcs11.Ctx behavior and manages customizable session pool - added tests including one resilience scenario for invalid session - Concurrency handled Change-Id: I98c09b3c07f175a05ad02b888e7366d0e724b919 Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent ee69064 commit 28c91d2

File tree

7 files changed

+1070
-276
lines changed

7 files changed

+1070
-276
lines changed

internal/github.com/hyperledger/fabric/bccsp/pkcs11/impl.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ import (
2828
"math/big"
2929
"os"
3030

31-
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/cachebridge"
32-
33-
"sync"
34-
3531
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp"
3632
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp/sw"
3733
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/bccsp/utils"
3834
flogging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
39-
"github.com/miekg/pkcs11"
35+
handle "github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite/common/pkcs11"
4036
"github.com/pkg/errors"
4137
)
4238

@@ -65,19 +61,14 @@ func New(opts PKCS11Opts, keyStore bccsp.KeyStore) (bccsp.BCCSP, error) {
6561
return nil, errors.New("Invalid bccsp.KeyStore instance. It must be different from nil")
6662
}
6763

68-
lib := opts.Library
69-
pin := opts.Pin
70-
label := opts.Label
71-
ctx, slot, session, err := loadLib(lib, pin, label)
64+
//Load PKCS11 context handle
65+
pkcs11Ctx, err := loadContext(opts.Library, opts.Pin, opts.Label)
7266
if err != nil {
73-
return nil, errors.Wrapf(err, "Failed initializing PKCS11 library %s %s",
74-
lib, label)
67+
return nil, errors.Wrapf(err, "Failed initializing PKCS11 context")
7568
}
7669

77-
sessions := make(chan pkcs11.SessionHandle, sessionCacheSize)
78-
csp := &impl{BCCSP: swCSP, conf: conf, ks: keyStore, ctx: ctx, sessions: sessions, slot: slot, lib: lib, privImport: opts.Sensitive, softVerify: opts.SoftVerify}
79-
csp.returnSession(*session)
80-
cachebridge.ClearAllSession()
70+
csp := &impl{BCCSP: swCSP, conf: conf, ks: keyStore, privImport: opts.Sensitive, softVerify: opts.SoftVerify, pkcs11Ctx: pkcs11Ctx}
71+
8172
return csp, nil
8273
}
8374

@@ -87,16 +78,12 @@ type impl struct {
8778
conf *config
8879
ks bccsp.KeyStore
8980

90-
ctx *pkcs11.Ctx
91-
sessions chan pkcs11.SessionHandle
92-
slot uint
93-
94-
lib string
9581
privImport bool
9682
softVerify bool
9783

98-
opts PKCS11Opts
99-
ctxlock sync.RWMutex
84+
opts PKCS11Opts
85+
86+
pkcs11Ctx *handle.ContextHandle
10087
}
10188

10289
// KeyGen generates a key using opts.

0 commit comments

Comments
 (0)