@@ -33,14 +33,23 @@ import (
33
33
"github.com/hyperledger/fabric/bccsp/utils"
34
34
)
35
35
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.
37
46
// Each key is stored in a separated file whose name contains the key's SKI
38
47
// and flags to identity the key's type. All the keys are stored in
39
48
// a folder whose path is provided at initialization time.
40
49
// The KeyStore can be initialized with a password, this password
41
50
// is used to encrypt and decrypt the files storing the keys.
42
51
// A KeyStore can be read only to avoid the overwriting of keys.
43
- type FileBasedKeyStore struct {
52
+ type fileBasedKeyStore struct {
44
53
path string
45
54
46
55
readOnly bool
@@ -62,7 +71,7 @@ type FileBasedKeyStore struct {
62
71
// key-store is initialized without a password, then retrieving keys from the
63
72
// KeyStore will fail.
64
73
// 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 {
66
75
// Validate inputs
67
76
// pwd can be nil
68
77
@@ -97,12 +106,12 @@ func (ks *FileBasedKeyStore) Init(pwd []byte, path string, readOnly bool) error
97
106
98
107
// ReadOnly returns true if this KeyStore is read only, false otherwise.
99
108
// If ReadOnly is true then StoreKey will fail.
100
- func (ks * FileBasedKeyStore ) ReadOnly () bool {
109
+ func (ks * fileBasedKeyStore ) ReadOnly () bool {
101
110
return ks .readOnly
102
111
}
103
112
104
113
// 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 ) {
106
115
// Validate arguments
107
116
if len (ski ) == 0 {
108
117
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) {
156
165
157
166
// StoreKey stores the key k in this KeyStore.
158
167
// 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 ) {
160
169
if ks .readOnly {
161
170
return errors .New ("Read only KeyStore." )
162
171
}
@@ -212,7 +221,7 @@ func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
212
221
return
213
222
}
214
223
215
- func (ks * FileBasedKeyStore ) getSuffix (alias string ) string {
224
+ func (ks * fileBasedKeyStore ) getSuffix (alias string ) string {
216
225
files , _ := ioutil .ReadDir (ks .path )
217
226
for _ , f := range files {
218
227
if strings .HasPrefix (f .Name (), alias ) {
@@ -231,7 +240,7 @@ func (ks *FileBasedKeyStore) getSuffix(alias string) string {
231
240
return ""
232
241
}
233
242
234
- func (ks * FileBasedKeyStore ) storePrivateKey (alias string , privateKey interface {}) error {
243
+ func (ks * fileBasedKeyStore ) storePrivateKey (alias string , privateKey interface {}) error {
235
244
rawKey , err := utils .PrivateKeyToPEM (privateKey , ks .pwd )
236
245
if err != nil {
237
246
logger .Errorf ("Failed converting private key to PEM [%s]: [%s]" , alias , err )
@@ -247,7 +256,7 @@ func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{
247
256
return nil
248
257
}
249
258
250
- func (ks * FileBasedKeyStore ) storePublicKey (alias string , publicKey interface {}) error {
259
+ func (ks * fileBasedKeyStore ) storePublicKey (alias string , publicKey interface {}) error {
251
260
rawKey , err := utils .PublicKeyToPEM (publicKey , ks .pwd )
252
261
if err != nil {
253
262
logger .Errorf ("Failed converting public key to PEM [%s]: [%s]" , alias , err )
@@ -263,7 +272,7 @@ func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{})
263
272
return nil
264
273
}
265
274
266
- func (ks * FileBasedKeyStore ) storeKey (alias string , key []byte ) error {
275
+ func (ks * fileBasedKeyStore ) storeKey (alias string , key []byte ) error {
267
276
pem , err := utils .AEStoEncryptedPEM (key , ks .pwd )
268
277
if err != nil {
269
278
logger .Errorf ("Failed converting key to PEM [%s]: [%s]" , alias , err )
@@ -279,7 +288,7 @@ func (ks *FileBasedKeyStore) storeKey(alias string, key []byte) error {
279
288
return nil
280
289
}
281
290
282
- func (ks * FileBasedKeyStore ) loadPrivateKey (alias string ) (interface {}, error ) {
291
+ func (ks * fileBasedKeyStore ) loadPrivateKey (alias string ) (interface {}, error ) {
283
292
path := ks .getPathForAlias (alias , "sk" )
284
293
logger .Debugf ("Loading private key [%s] at [%s]..." , alias , path )
285
294
@@ -300,7 +309,7 @@ func (ks *FileBasedKeyStore) loadPrivateKey(alias string) (interface{}, error) {
300
309
return privateKey , nil
301
310
}
302
311
303
- func (ks * FileBasedKeyStore ) loadPublicKey (alias string ) (interface {}, error ) {
312
+ func (ks * fileBasedKeyStore ) loadPublicKey (alias string ) (interface {}, error ) {
304
313
path := ks .getPathForAlias (alias , "pk" )
305
314
logger .Debugf ("Loading public key [%s] at [%s]..." , alias , path )
306
315
@@ -321,7 +330,7 @@ func (ks *FileBasedKeyStore) loadPublicKey(alias string) (interface{}, error) {
321
330
return privateKey , nil
322
331
}
323
332
324
- func (ks * FileBasedKeyStore ) loadKey (alias string ) ([]byte , error ) {
333
+ func (ks * fileBasedKeyStore ) loadKey (alias string ) ([]byte , error ) {
325
334
path := ks .getPathForAlias (alias , "key" )
326
335
logger .Debugf ("Loading key [%s] at [%s]..." , alias , path )
327
336
@@ -342,13 +351,13 @@ func (ks *FileBasedKeyStore) loadKey(alias string) ([]byte, error) {
342
351
return key , nil
343
352
}
344
353
345
- func (ks * FileBasedKeyStore ) close () error {
354
+ func (ks * fileBasedKeyStore ) close () error {
346
355
ks .isOpen = false
347
356
logger .Debug ("Closing keystore...done!" )
348
357
return nil
349
358
}
350
359
351
- func (ks * FileBasedKeyStore ) createKeyStoreIfNotExists () error {
360
+ func (ks * fileBasedKeyStore ) createKeyStoreIfNotExists () error {
352
361
// Check keystore directory
353
362
ksPath := ks .path
354
363
missing , err := utils .DirMissingOrEmpty (ksPath )
@@ -365,7 +374,7 @@ func (ks *FileBasedKeyStore) createKeyStoreIfNotExists() error {
365
374
return nil
366
375
}
367
376
368
- func (ks * FileBasedKeyStore ) createKeyStore () error {
377
+ func (ks * fileBasedKeyStore ) createKeyStore () error {
369
378
// Create keystore directory root if it doesn't exist yet
370
379
ksPath := ks .path
371
380
logger .Debugf ("Creating KeyStore at [%s]..." , ksPath )
@@ -376,13 +385,13 @@ func (ks *FileBasedKeyStore) createKeyStore() error {
376
385
return nil
377
386
}
378
387
379
- func (ks * FileBasedKeyStore ) deleteKeyStore () error {
388
+ func (ks * fileBasedKeyStore ) deleteKeyStore () error {
380
389
logger .Debugf ("Removing KeyStore at [%s]." , ks .path )
381
390
382
391
return os .RemoveAll (ks .path )
383
392
}
384
393
385
- func (ks * FileBasedKeyStore ) openKeyStore () error {
394
+ func (ks * fileBasedKeyStore ) openKeyStore () error {
386
395
if ks .isOpen {
387
396
return nil
388
397
}
@@ -392,6 +401,6 @@ func (ks *FileBasedKeyStore) openKeyStore() error {
392
401
return nil
393
402
}
394
403
395
- func (ks * FileBasedKeyStore ) getPathForAlias (alias , suffix string ) string {
404
+ func (ks * fileBasedKeyStore ) getPathForAlias (alias , suffix string ) string {
396
405
return filepath .Join (ks .path , alias + "_" + suffix )
397
406
}
0 commit comments