Skip to content

Commit f4e359f

Browse files
committed
BCCSP KeyStore cleanup
This change-set introduces New* methods to instantiate file-based key stores and dummy key stores. The purpose is to streamline the instantiation of keystore and allow reusability in the pkcs11 package. Change-Id: I1cf6b3e6a9c3244bdc46f6aa164a49acb1e4b1f0 Signed-off-by: Angelo De Caro <[email protected]>
1 parent e829d2e commit f4e359f

10 files changed

+54
-41
lines changed

bccsp/factory/pkcs11factory.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,16 @@ func (f *PKCS11Factory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
4949
//TODO: PKCS11 does not need a keystore, but we have not migrated all of PKCS11 BCCSP to PKCS11 yet
5050
var ks bccsp.KeyStore
5151
if p11Opts.Ephemeral == true {
52-
ks = &sw.DummyKeyStore{}
52+
ks = sw.NewDummyKeyStore()
5353
} else if p11Opts.FileKeystore != nil {
54-
fks := &sw.FileBasedKeyStore{}
55-
err := fks.Init(nil, p11Opts.FileKeystore.KeyStorePath, false)
54+
fks, err := sw.NewFileBasedKeyStore(nil, p11Opts.FileKeystore.KeyStorePath, false)
5655
if err != nil {
5756
return nil, fmt.Errorf("Failed to initialize software key store: %s", err)
5857
}
5958
ks = fks
6059
} else {
6160
// Default to DummyKeystore
62-
ks = &sw.DummyKeyStore{}
61+
ks = sw.NewDummyKeyStore()
6362
}
6463
err := pkcs11.InitPKCS11(p11Opts.Library, p11Opts.Pin, p11Opts.Label)
6564
if err != nil {

bccsp/factory/swfactory.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ func (f *SWFactory) Get(config *FactoryOpts) (bccsp.BCCSP, error) {
4747

4848
var ks bccsp.KeyStore
4949
if swOpts.Ephemeral == true {
50-
ks = &sw.DummyKeyStore{}
50+
ks = sw.NewDummyKeyStore()
5151
} else if swOpts.FileKeystore != nil {
52-
fks := &sw.FileBasedKeyStore{}
53-
err := fks.Init(nil, swOpts.FileKeystore.KeyStorePath, false)
52+
fks, err := sw.NewFileBasedKeyStore(nil, swOpts.FileKeystore.KeyStorePath, false)
5453
if err != nil {
5554
return nil, fmt.Errorf("Failed to initialize software key store: %s", err)
5655
}
5756
ks = fks
5857
} else {
5958
// Default to DummyKeystore
60-
ks = &sw.DummyKeyStore{}
59+
ks = sw.NewDummyKeyStore()
6160
}
6261

6362
return sw.New(swOpts.SecLevel, swOpts.HashFamily, ks)

bccsp/pkcs11/dummyks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/hyperledger/fabric/bccsp"
2323
)
2424

25-
// DummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
25+
// dummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
2626
type DummyKeyStore struct {
2727
}
2828

bccsp/pkcs11/fileks.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/hyperledger/fabric/bccsp/utils"
3131
)
3232

33-
// FileBasedKeyStore is a folder-based KeyStore.
33+
// fileBasedKeyStore is a folder-based KeyStore.
3434
// Each key is stored in a separated file whose name contains the key's SKI
3535
// and flags to identity the key's type. All the keys are stored in
3636
// a folder whose path is provided at initialization time.

bccsp/pkcs11/impl_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestMain(m *testing.M) {
8989
os.Exit(-1)
9090
}
9191

92-
currentSWBCCSP, err = sw.New(config.securityLevel, config.hashFamily, &sw.DummyKeyStore{})
92+
currentSWBCCSP, err = sw.New(config.securityLevel, config.hashFamily, sw.NewDummyKeyStore())
9393
if err != nil {
9494
fmt.Printf("Failed initiliazing BCCSP at [%d, %s]: [%s]", config.securityLevel, config.hashFamily, err)
9595
os.Exit(-1)

bccsp/sw/dummyks.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,29 @@ import (
2121
"github.com/hyperledger/fabric/bccsp"
2222
)
2323

24-
// DummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
25-
type DummyKeyStore struct {
24+
// NewDummyKeyStore instantiate a dummy key store
25+
// that neither loads nor stores keys
26+
func NewDummyKeyStore() bccsp.KeyStore {
27+
return &dummyKeyStore{}
28+
}
29+
30+
// dummyKeyStore is a read-only KeyStore that neither loads nor stores keys.
31+
type dummyKeyStore struct {
2632
}
2733

2834
// ReadOnly returns true if this KeyStore is read only, false otherwise.
2935
// If ReadOnly is true then StoreKey will fail.
30-
func (ks *DummyKeyStore) ReadOnly() bool {
36+
func (ks *dummyKeyStore) ReadOnly() bool {
3137
return true
3238
}
3339

3440
// GetKey returns a key object whose SKI is the one passed.
35-
func (ks *DummyKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
41+
func (ks *dummyKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
3642
return nil, errors.New("Key not found. This is a dummy KeyStore")
3743
}
3844

3945
// StoreKey stores the key k in this KeyStore.
4046
// If this KeyStore is read only then the method will fail.
41-
func (ks *DummyKeyStore) StoreKey(k bccsp.Key) (err error) {
47+
func (ks *dummyKeyStore) StoreKey(k bccsp.Key) (err error) {
4248
return errors.New("Cannot store key. This is a dummy read-only KeyStore")
4349
}

bccsp/sw/fileks.go

+28-19
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,23 @@ import (
3333
"github.com/hyperledger/fabric/bccsp/utils"
3434
)
3535

36-
// FileBasedKeyStore is a folder-based KeyStore.
36+
// NewFileBasedKeyStore instantiated a file-based key store at a given position.
37+
// The key store can be encrypted if a non-empty password is specifiec.
38+
// It can be also be set as read only. In this case, any store operation
39+
// will be forbidden
40+
func NewFileBasedKeyStore(pwd []byte, path string, readOnly bool) (bccsp.KeyStore, error) {
41+
ks := &fileBasedKeyStore{}
42+
return ks, ks.Init(pwd, path, readOnly)
43+
}
44+
45+
// fileBasedKeyStore is a folder-based KeyStore.
3746
// Each key is stored in a separated file whose name contains the key's SKI
3847
// and flags to identity the key's type. All the keys are stored in
3948
// a folder whose path is provided at initialization time.
4049
// The KeyStore can be initialized with a password, this password
4150
// is used to encrypt and decrypt the files storing the keys.
4251
// A KeyStore can be read only to avoid the overwriting of keys.
43-
type FileBasedKeyStore struct {
52+
type fileBasedKeyStore struct {
4453
path string
4554

4655
readOnly bool
@@ -62,7 +71,7 @@ type FileBasedKeyStore struct {
6271
// key-store is initialized without a password, then retrieving keys from the
6372
// KeyStore will fail.
6473
// A KeyStore can be read only to avoid the overwriting of keys.
65-
func (ks *FileBasedKeyStore) Init(pwd []byte, path string, readOnly bool) error {
74+
func (ks *fileBasedKeyStore) Init(pwd []byte, path string, readOnly bool) error {
6675
// Validate inputs
6776
// pwd can be nil
6877

@@ -97,12 +106,12 @@ func (ks *FileBasedKeyStore) Init(pwd []byte, path string, readOnly bool) error
97106

98107
// ReadOnly returns true if this KeyStore is read only, false otherwise.
99108
// If ReadOnly is true then StoreKey will fail.
100-
func (ks *FileBasedKeyStore) ReadOnly() bool {
109+
func (ks *fileBasedKeyStore) ReadOnly() bool {
101110
return ks.readOnly
102111
}
103112

104113
// GetKey returns a key object whose SKI is the one passed.
105-
func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
114+
func (ks *fileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
106115
// Validate arguments
107116
if len(ski) == 0 {
108117
return nil, errors.New("Invalid SKI. Cannot be of zero length.")
@@ -156,7 +165,7 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
156165

157166
// StoreKey stores the key k in this KeyStore.
158167
// If this KeyStore is read only then the method will fail.
159-
func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
168+
func (ks *fileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
160169
if ks.readOnly {
161170
return errors.New("Read only KeyStore.")
162171
}
@@ -212,7 +221,7 @@ func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
212221
return
213222
}
214223

215-
func (ks *FileBasedKeyStore) getSuffix(alias string) string {
224+
func (ks *fileBasedKeyStore) getSuffix(alias string) string {
216225
files, _ := ioutil.ReadDir(ks.path)
217226
for _, f := range files {
218227
if strings.HasPrefix(f.Name(), alias) {
@@ -231,7 +240,7 @@ func (ks *FileBasedKeyStore) getSuffix(alias string) string {
231240
return ""
232241
}
233242

234-
func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{}) error {
243+
func (ks *fileBasedKeyStore) storePrivateKey(alias string, privateKey interface{}) error {
235244
rawKey, err := utils.PrivateKeyToPEM(privateKey, ks.pwd)
236245
if err != nil {
237246
logger.Errorf("Failed converting private key to PEM [%s]: [%s]", alias, err)
@@ -247,7 +256,7 @@ func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{
247256
return nil
248257
}
249258

250-
func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{}) error {
259+
func (ks *fileBasedKeyStore) storePublicKey(alias string, publicKey interface{}) error {
251260
rawKey, err := utils.PublicKeyToPEM(publicKey, ks.pwd)
252261
if err != nil {
253262
logger.Errorf("Failed converting public key to PEM [%s]: [%s]", alias, err)
@@ -263,7 +272,7 @@ func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{})
263272
return nil
264273
}
265274

266-
func (ks *FileBasedKeyStore) storeKey(alias string, key []byte) error {
275+
func (ks *fileBasedKeyStore) storeKey(alias string, key []byte) error {
267276
pem, err := utils.AEStoEncryptedPEM(key, ks.pwd)
268277
if err != nil {
269278
logger.Errorf("Failed converting key to PEM [%s]: [%s]", alias, err)
@@ -279,7 +288,7 @@ func (ks *FileBasedKeyStore) storeKey(alias string, key []byte) error {
279288
return nil
280289
}
281290

282-
func (ks *FileBasedKeyStore) loadPrivateKey(alias string) (interface{}, error) {
291+
func (ks *fileBasedKeyStore) loadPrivateKey(alias string) (interface{}, error) {
283292
path := ks.getPathForAlias(alias, "sk")
284293
logger.Debugf("Loading private key [%s] at [%s]...", alias, path)
285294

@@ -300,7 +309,7 @@ func (ks *FileBasedKeyStore) loadPrivateKey(alias string) (interface{}, error) {
300309
return privateKey, nil
301310
}
302311

303-
func (ks *FileBasedKeyStore) loadPublicKey(alias string) (interface{}, error) {
312+
func (ks *fileBasedKeyStore) loadPublicKey(alias string) (interface{}, error) {
304313
path := ks.getPathForAlias(alias, "pk")
305314
logger.Debugf("Loading public key [%s] at [%s]...", alias, path)
306315

@@ -321,7 +330,7 @@ func (ks *FileBasedKeyStore) loadPublicKey(alias string) (interface{}, error) {
321330
return privateKey, nil
322331
}
323332

324-
func (ks *FileBasedKeyStore) loadKey(alias string) ([]byte, error) {
333+
func (ks *fileBasedKeyStore) loadKey(alias string) ([]byte, error) {
325334
path := ks.getPathForAlias(alias, "key")
326335
logger.Debugf("Loading key [%s] at [%s]...", alias, path)
327336

@@ -342,13 +351,13 @@ func (ks *FileBasedKeyStore) loadKey(alias string) ([]byte, error) {
342351
return key, nil
343352
}
344353

345-
func (ks *FileBasedKeyStore) close() error {
354+
func (ks *fileBasedKeyStore) close() error {
346355
ks.isOpen = false
347356
logger.Debug("Closing keystore...done!")
348357
return nil
349358
}
350359

351-
func (ks *FileBasedKeyStore) createKeyStoreIfNotExists() error {
360+
func (ks *fileBasedKeyStore) createKeyStoreIfNotExists() error {
352361
// Check keystore directory
353362
ksPath := ks.path
354363
missing, err := utils.DirMissingOrEmpty(ksPath)
@@ -365,7 +374,7 @@ func (ks *FileBasedKeyStore) createKeyStoreIfNotExists() error {
365374
return nil
366375
}
367376

368-
func (ks *FileBasedKeyStore) createKeyStore() error {
377+
func (ks *fileBasedKeyStore) createKeyStore() error {
369378
// Create keystore directory root if it doesn't exist yet
370379
ksPath := ks.path
371380
logger.Debugf("Creating KeyStore at [%s]...", ksPath)
@@ -376,13 +385,13 @@ func (ks *FileBasedKeyStore) createKeyStore() error {
376385
return nil
377386
}
378387

379-
func (ks *FileBasedKeyStore) deleteKeyStore() error {
388+
func (ks *fileBasedKeyStore) deleteKeyStore() error {
380389
logger.Debugf("Removing KeyStore at [%s].", ks.path)
381390

382391
return os.RemoveAll(ks.path)
383392
}
384393

385-
func (ks *FileBasedKeyStore) openKeyStore() error {
394+
func (ks *fileBasedKeyStore) openKeyStore() error {
386395
if ks.isOpen {
387396
return nil
388397
}
@@ -392,6 +401,6 @@ func (ks *FileBasedKeyStore) openKeyStore() error {
392401
return nil
393402
}
394403

395-
func (ks *FileBasedKeyStore) getPathForAlias(alias, suffix string) string {
404+
func (ks *fileBasedKeyStore) getPathForAlias(alias, suffix string) string {
396405
return filepath.Join(ks.path, alias+"_"+suffix)
397406
}

bccsp/sw/fileks_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
)
2424

2525
func TestInvalidStoreKey(t *testing.T) {
26-
ks := &FileBasedKeyStore{}
27-
if err := ks.Init(nil, filepath.Join(os.TempDir(), "bccspks"), false); err != nil {
26+
ks, err := NewFileBasedKeyStore(nil, filepath.Join(os.TempDir(), "bccspks"), false)
27+
if err != nil {
2828
fmt.Printf("Failed initiliazing KeyStore [%s]", err)
2929
os.Exit(-1)
3030
}
3131

32-
err := ks.StoreKey(nil)
32+
err = ks.StoreKey(nil)
3333
if err == nil {
3434
t.Fatal("Error should be different from nil in this case")
3535
}

bccsp/sw/impl.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var (
4747
// NewDefaultSecurityLevel returns a new instance of the software-based BCCSP
4848
// at security level 256, hash family SHA2 and using FolderBasedKeyStore as KeyStore.
4949
func NewDefaultSecurityLevel(keyStorePath string) (bccsp.BCCSP, error) {
50-
ks := &FileBasedKeyStore{}
50+
ks := &fileBasedKeyStore{}
5151
if err := ks.Init(nil, keyStorePath, false); err != nil {
5252
return nil, fmt.Errorf("Failed initializing key store [%s]", err)
5353
}

bccsp/sw/impl_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ type testConfig struct {
5858
}
5959

6060
func TestMain(m *testing.M) {
61-
ks := &FileBasedKeyStore{}
62-
if err := ks.Init(nil, os.TempDir(), false); err != nil {
61+
ks, err := NewFileBasedKeyStore(nil, os.TempDir(), false);
62+
if err != nil {
6363
fmt.Printf("Failed initiliazing KeyStore [%s]", err)
6464
os.Exit(-1)
6565
}

0 commit comments

Comments
 (0)