/
cache.go
125 lines (103 loc) · 3.21 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
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"
"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/miekg/pkcs11"
)
var logger = flogging.MustGetLogger("bccsp_p11_sessioncache")
var sessionCache = newSessionCache()
const (
privateKeyFlag = true
)
// keyPairCacheKey
type KeyPairCacheKey struct {
Mod *pkcs11.Ctx
Session pkcs11.SessionHandle
SKI []byte
KeyType bool
}
//String return string value for keyPairCacheKey
func (keyPairCacheKey *KeyPairCacheKey) String() string {
return fmt.Sprintf("%x_%t", keyPairCacheKey.SKI, keyPairCacheKey.KeyType)
}
// SessionCacheKey
type SessionCacheKey struct {
SessionID string
}
//String return string value for SessionCacheKey
func (SessionCacheKey *SessionCacheKey) String() string {
return SessionCacheKey.SessionID
}
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 ClearAllSession() {
sessionCache.DeleteAll()
}
func ClearSession(key string) {
sessionCache.Delete(&SessionCacheKey{SessionID: key})
}
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
}
return value.(*pkcs11.ObjectHandle), nil
}
return nil, fmt.Errorf("cannot find session in sessionCache")
}
func getKeyPairFromSKI(keyPairCacheKey *KeyPairCacheKey) (*pkcs11.ObjectHandle, error) {
ktype := pkcs11.CKO_PUBLIC_KEY
if keyPairCacheKey.KeyType == privateKeyFlag {
ktype = pkcs11.CKO_PRIVATE_KEY
}
template := []*pkcs11.Attribute{
pkcs11.NewAttribute(pkcs11.CKA_CLASS, ktype),
pkcs11.NewAttribute(pkcs11.CKA_ID, keyPairCacheKey.SKI),
}
if err := keyPairCacheKey.Mod.FindObjectsInit(keyPairCacheKey.Session, template); err != nil {
return nil, err
}
// single session instance, assume one hit only
objs, _, err := keyPairCacheKey.Mod.FindObjects(keyPairCacheKey.Session, 1)
if err != nil {
return nil, err
}
if err = keyPairCacheKey.Mod.FindObjectsFinal(keyPairCacheKey.Session); err != nil {
return nil, err
}
if len(objs) == 0 {
return nil, fmt.Errorf("Key not found [%s]", hex.Dump(keyPairCacheKey.SKI))
}
return &objs[0], nil
}