Skip to content

Commit

Permalink
[FAB-11135] pinning scripts for pkcs11 object handle
Browse files Browse the repository at this point in the history
Change-Id: I5dcb195648a72f2c61438654d0a945fb6b30d614
Signed-off-by: Sudesh Shetty <[email protected]>
  • Loading branch information
sudeshrshetty authored and troyronda committed Jul 18, 2018
1 parent b564dd5 commit 4fadae5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ SPDX-License-Identifier: Apache-2.0
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/
/*
Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
Please review third_party pinning scripts and patches for more details.
*/

package cachebridge

Expand Down
9 changes: 3 additions & 6 deletions scripts/third_party_pins/fabric/apply_fabric_client_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,17 +161,14 @@ gofilter() {
echo "Modifying go source files"
FILTER_FILENAME="bccsp/pkcs11/impl.go"
sed -i'' -e 's/impl{swCSP, conf, keyStore, ctx, sessions, slot, lib, opts.Sensitive, opts.SoftVerify}/impl{BCCSP: swCSP, conf: conf, ks: keyStore, ctx: ctx, sessions: sessions, slot: slot, lib: lib, noPrivImport: opts.Sensitive, softVerify: opts.SoftVerify}/g' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/"github.com\/hyperledger"/a "sync"/' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/"math\/big"/a "github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\/sdkpatch\/cachebridge"' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/softVerify bool/a rwMtx sync.RWMutex' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/csp.returnSession(\*session)/a cachebridge.ClearAllSession(csp.rwMtx)' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/csp.returnSession(\*session)/a cachebridge.ClearAllSession()' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"

FILTER_FILENAME="bccsp/pkcs11/pkcs11.go"
sed -i'' -e '/"github.com\/hyperledger"/a "time"/' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/"math\/big"/a "github.com\/hyperledger\/fabric-sdk-go\/internal\/github.com\/hyperledger\/fabric\/sdkpatch\/cachebridge"' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/session = s/a cachebridge.ClearSession(csp.rwMtx, fmt.Sprintf("%d", session))' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/session = s/a cachebridge.ClearSession(fmt.Sprintf("%d", session))' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e 's/= findKeyPairFromSKI/= csp.findKeyPairFromSKI/g' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/return session/i cachebridge.AddSession(csp.rwMtx, fmt.Sprintf("%d", session))' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/privateKey, err := csp.findKeyPairFromSKI(p11lib,/a defer timeTrack(time.Now(), fmt.Sprintf("signing [session: %d]", session))' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e 's/func findKeyPairFromSKI(mod/func (csp \*impl) findKeyPairFromSKI(mod/g' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
START_LINE=`grep -n "func (csp \*impl) findKeyPairFromSKI(mod" "${TMP_PROJECT_PATH}/${FILTER_FILENAME}" | head -n 1 | awk -F':' '{print $1}'`
Expand All @@ -180,7 +177,7 @@ for i in {1..27}
do
sed -i'' -e ${START_LINE}'d' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
done
sed -i'' -e '/func (csp \*impl) findKeyPairFromSKI(mod/a return cachebridge.GetKeyPairFromSessionSKI(csp.rwMtx, &cachebridge.KeyPairCacheKey{Mod: mod, Session: session, SKI: ski, KeyType: keyType})' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/func (csp \*impl) findKeyPairFromSKI(mod/a return cachebridge.GetKeyPairFromSessionSKI(&cachebridge.KeyPairCacheKey{Mod: mod, Session: session, SKI: ski, KeyType: keyType})' "${TMP_PROJECT_PATH}/${FILTER_FILENAME}"
sed -i'' -e '/func (csp \*impl) findKeyPairFromSKI(mod/i \
func timeTrack(start time.Time, msg string) {\
elapsed := time.Since(start)\
Expand Down
121 changes: 46 additions & 75 deletions scripts/third_party_pins/fabric/patches/0004-sessioncache.patch
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
From 9bf4cedf434a3e41e2527c81515bbb33d4cbad83 Mon Sep 17 00:00:00 2001
From e371cf7ebf4d7c5d9168bcd744e15a733294d74f Mon Sep 17 00:00:00 2001
From: Sudesh Shetty <[email protected]>
Date: Thu, 12 Jul 2018 16:05:30 -0400
Subject: [PATCH] session cache bridge
Date: Wed, 18 Jul 2018 11:23:55 -0400
Subject: [PATCH] [PATCH] session cache bridge

Change-Id: Ibeaaecf690311afc65c1db8dc58236fa3c7e617e
Change-Id: I18d8f7b432b535e6daae2630729e5ddd7688a4e6
Signed-off-by: Sudesh Shetty <[email protected]>
---
sdkpatch/cachebridge/cache.go | 149 +++++++++++++++++++++
1 file changed, 149 insertions(+)
sdkpatch/cachebridge/cache.go | 121 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 121 insertions(+)
create mode 100644 sdkpatch/cachebridge/cache.go

diff --git a/sdkpatch/cachebridge/cache.go b/sdkpatch/cachebridge/cache.go
new file mode 100644
index 0000000..4772d51
index 0000000..90d423b
--- /dev/null
+++ b/sdkpatch/cachebridge/cache.go
@@ -0,0 +1,149 @@
@@ -0,0 +1,121 @@
+/*
+Copyright SecureKey Technologies Inc. All Rights Reserved.
+
+SPDX-License-Identifier: Apache-2.0
+*/
+/*
+Notice: This file has been modified for Hyperledger Fabric SDK Go usage.
+Please review third_party pinning scripts and patches for more details.
+*/
+
+package cachebridge
+
+import (
+ "fmt"
+ "time"
+
+ "sync"
+
+ "encoding/hex"
+
+ flogging "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/sdkpatch/logbridge"
+ "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"
+ "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref"
+ "github.com/miekg/pkcs11"
+)
+
+var sessionCache map[string]*lazycache.Cache
+
+var logger = flogging.MustGetLogger("bccsp_p11_sessioncache")
+
+var sessionCache = newSessionCache()
+
+const (
+ privateKeyFlag = true
+)
Expand All @@ -58,80 +51,59 @@ index 0000000..4772d51
+ KeyType bool
+}
+
+//String return string value for config key
+//String return string value for keyPairCacheKey
+func (keyPairCacheKey *KeyPairCacheKey) String() string {
+ return fmt.Sprintf("%x_%t", keyPairCacheKey.SKI, keyPairCacheKey.KeyType)
+}
+
+func timeTrack(start time.Time, msg string) {
+ elapsed := time.Since(start)
+ logger.Debugf("%s took %s", msg, elapsed)
+// SessionCacheKey
+type SessionCacheKey struct {
+ SessionID string
+}
+
+func ClearAllSession(rwMtx sync.RWMutex) {
+
+ if sessionCache != nil && len(sessionCache) > 0 {
+ rwMtx.Lock()
+ for _, val := range sessionCache {
+ val.Close()
+ }
+ sessionCache = nil
+ rwMtx.Unlock()
+ }
+//String return string value for SessionCacheKey
+func (SessionCacheKey *SessionCacheKey) String() string {
+ return SessionCacheKey.SessionID
+}
+
+func ClearSession(rwMtx sync.RWMutex, key string) {
+ rwMtx.RLock()
+ val, ok := sessionCache[key]
+ rwMtx.RUnlock()
+ if ok {
+ rwMtx.Lock()
+ val.Close()
+ sessionCache[key] = nil
+ rwMtx.Unlock()
+func newSessionCache() *lazycache.Cache {
+ return lazycache.New(
+ "Session_Resolver_Cache",
+ func(key lazycache.Key) (interface{}, error) {
+ return lazycache.New(
+ "KeyPair_Resolver_Cache",
+ func(key lazycache.Key) (interface{}, error) {
+ return getKeyPairFromSKI(key.(*KeyPairCacheKey))
+ }), nil
+ })
+}
+
+ }
+func timeTrack(start time.Time, msg string) {
+ elapsed := time.Since(start)
+ logger.Debugf("%s took %s", msg, elapsed)
+}
+
+func AddSession(rwMtx sync.RWMutex, key string) {
+ rwMtx.RLock()
+ _, ok := sessionCache[key]
+ rwMtx.RUnlock()
+func ClearAllSession() {
+ sessionCache.DeleteAll()
+}
+
+ if !ok {
+ rwMtx.Lock()
+ if sessionCache == nil {
+ sessionCache = make(map[string]*lazycache.Cache)
+ }
+ sessionCache[key] = lazycache.New(
+ "KeyPair_Resolver_Cache",
+ func(key lazycache.Key) (interface{}, error) {
+ return lazyref.New(
+ func() (interface{}, error) {
+ return getKeyPairFromSKI(key.(*KeyPairCacheKey))
+ },
+ ), nil
+ })
+ rwMtx.Unlock()
+ }
+func ClearSession(key string) {
+ sessionCache.Delete(&SessionCacheKey{SessionID: key})
+}
+
+func GetKeyPairFromSessionSKI(rwMtx sync.RWMutex, keyPairCacheKey *KeyPairCacheKey) (*pkcs11.ObjectHandle, error) {
+ rwMtx.RLock()
+ val, ok := sessionCache[fmt.Sprintf("%d", keyPairCacheKey.Session)]
+ rwMtx.RUnlock()
+ if ok {
+func GetKeyPairFromSessionSKI(keyPairCacheKey *KeyPairCacheKey) (*pkcs11.ObjectHandle, error) {
+ keyPairCache, err := sessionCache.Get(&SessionCacheKey{SessionID: fmt.Sprintf("%d", keyPairCacheKey.Session)})
+ if err != nil {
+ return nil, err
+ }
+ if keyPairCache != nil {
+ val := keyPairCache.(*lazycache.Cache)
+ defer timeTrack(time.Now(), fmt.Sprintf("finding key [session: %d] [ski: %x]", keyPairCacheKey.Session, keyPairCacheKey.SKI))
+ value, err := val.Get(keyPairCacheKey)
+ if err != nil {
+ return nil, err
+ }
+ lazyRef := value.(*lazyref.Reference)
+ resolver, err := lazyRef.Get()
+ if err != nil {
+ return nil, err
+ }
+ return resolver.(*pkcs11.ObjectHandle), nil
+ return value.(*pkcs11.ObjectHandle), nil
+ }
+ return nil, fmt.Errorf("cannot find session in sessionCache")
+}
Expand Down Expand Up @@ -165,6 +137,5 @@ index 0000000..4772d51
+
+ return &objs[0], nil
+}
--
2.7.4

--
2.7.

0 comments on commit 4fadae5

Please sign in to comment.