Skip to content

Commit 7ea7554

Browse files
committed
BCCSP improved test coverage
This change-set improves the test coverage of the BCCSP package and fix a few bugs found during the process. This change-set comes in the context of: https://jira.hyperledger.org/browse/FAB-354 Change-Id: I78ed4a36ca32166c6271b98935569eb3d997475e Signed-off-by: Angelo De Caro <[email protected]>
1 parent 9cd44a1 commit 7ea7554

File tree

8 files changed

+541
-207
lines changed

8 files changed

+541
-207
lines changed

core/crypto/bccsp/sw/aeskey.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424
)
2525

2626
type aesPrivateKey struct {
27-
k []byte
27+
privKey []byte
2828
exportable bool
2929
}
3030

3131
// Bytes converts this key to its byte representation,
3232
// if this operation is allowed.
3333
func (k *aesPrivateKey) Bytes() (raw []byte, err error) {
3434
if k.exportable {
35-
return k.k, nil
35+
return k.privKey, nil
3636
}
3737

3838
return nil, errors.New("Not supported.")
@@ -41,7 +41,7 @@ func (k *aesPrivateKey) Bytes() (raw []byte, err error) {
4141
// SKI returns the subject key identifier of this key.
4242
func (k *aesPrivateKey) SKI() (ski []byte) {
4343
hash := sha256.New()
44-
hash.Write(k.k)
44+
hash.Write(k.privKey)
4545
return hash.Sum(nil)
4646
}
4747

core/crypto/bccsp/sw/ecdsakey.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,30 @@ import (
2222

2323
"crypto/sha256"
2424

25+
"errors"
26+
2527
"github.com/hyperledger/fabric/core/crypto/bccsp"
2628
"github.com/hyperledger/fabric/core/crypto/primitives"
2729
)
2830

2931
type ecdsaPrivateKey struct {
30-
k *ecdsa.PrivateKey
32+
privKey *ecdsa.PrivateKey
3133
}
3234

3335
// FIXME: remove as soon as there's a way to import the key more properly
3436
func NewEcdsaPrivateKey(k *ecdsa.PrivateKey) bccsp.Key {
35-
return &ecdsaPrivateKey{k: k}
37+
return &ecdsaPrivateKey{privKey: k}
3638
}
3739

3840
// Bytes converts this key to its byte representation,
3941
// if this operation is allowed.
4042
func (k *ecdsaPrivateKey) Bytes() (raw []byte, err error) {
41-
return
43+
return nil, errors.New("Not supported.")
4244
}
4345

4446
// SKI returns the subject key identifier of this key.
4547
func (k *ecdsaPrivateKey) SKI() (ski []byte) {
46-
raw, _ := primitives.PrivateKeyToDER(k.k)
48+
raw, _ := primitives.PrivateKeyToDER(k.privKey)
4749
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
4850

4951
hash := sha256.New()
@@ -66,22 +68,22 @@ func (k *ecdsaPrivateKey) Private() bool {
6668
// PublicKey returns the corresponding public key part of an asymmetric public/private key pair.
6769
// This method returns an error in symmetric key schemes.
6870
func (k *ecdsaPrivateKey) PublicKey() (bccsp.Key, error) {
69-
return &ecdsaPublicKey{&k.k.PublicKey}, nil
71+
return &ecdsaPublicKey{&k.privKey.PublicKey}, nil
7072
}
7173

7274
type ecdsaPublicKey struct {
73-
k *ecdsa.PublicKey
75+
pubKey *ecdsa.PublicKey
7476
}
7577

7678
// FIXME: remove as soon as there's a way to import the key more properly
7779
func NewEcdsaPublicKey(k *ecdsa.PublicKey) bccsp.Key {
78-
return &ecdsaPublicKey{k: k}
80+
return &ecdsaPublicKey{pubKey: k}
7981
}
8082

8183
// Bytes converts this key to its byte representation,
8284
// if this operation is allowed.
8385
func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
84-
raw, err = x509.MarshalPKIXPublicKey(k.k)
86+
raw, err = x509.MarshalPKIXPublicKey(k.pubKey)
8587
if err != nil {
8688
return nil, fmt.Errorf("Failed marshalling key [%s]", err)
8789
}
@@ -90,7 +92,7 @@ func (k *ecdsaPublicKey) Bytes() (raw []byte, err error) {
9092

9193
// SKI returns the subject key identifier of this key.
9294
func (k *ecdsaPublicKey) SKI() (ski []byte) {
93-
raw, _ := primitives.PublicKeyToPEM(k.k, nil)
95+
raw, _ := primitives.PublicKeyToPEM(k.pubKey, nil)
9496
// TODO: Error should not be thrown. Anyway, move the marshalling at initialization.
9597

9698
hash := sha256.New()

core/crypto/bccsp/sw/fileks.go

+23-20
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
125125
// Load the private key
126126
key, err := ks.loadPrivateKey(hex.EncodeToString(ski))
127127
if err != nil {
128-
return nil, fmt.Errorf("Failed loading key [%x] [%s]", ski, err)
128+
return nil, fmt.Errorf("Failed loading secret key [%x] [%s]", ski, err)
129129
}
130130

131131
switch key.(type) {
@@ -134,10 +134,25 @@ func (ks *FileBasedKeyStore) GetKey(ski []byte) (k bccsp.Key, err error) {
134134
case *rsa.PrivateKey:
135135
return &rsaPrivateKey{key.(*rsa.PrivateKey)}, nil
136136
default:
137-
return nil, errors.New("Key type not recognized")
137+
return nil, errors.New("Secret key type not recognized")
138+
}
139+
case "pk":
140+
// Load the public key
141+
key, err := ks.loadPublicKey(hex.EncodeToString(ski))
142+
if err != nil {
143+
return nil, fmt.Errorf("Failed loading public key [%x] [%s]", ski, err)
144+
}
145+
146+
switch key.(type) {
147+
case *ecdsa.PublicKey:
148+
return &ecdsaPublicKey{key.(*ecdsa.PublicKey)}, nil
149+
case *rsa.PublicKey:
150+
return &rsaPublicKey{key.(*rsa.PublicKey)}, nil
151+
default:
152+
return nil, errors.New("Public key type not recognized")
138153
}
139154
default:
140-
return nil, errors.New("Key not recognized")
155+
return nil, errors.New("Key type not recognized")
141156
}
142157
}
143158

@@ -155,39 +170,39 @@ func (ks *FileBasedKeyStore) StoreKey(k bccsp.Key) (err error) {
155170
case *ecdsaPrivateKey:
156171
kk := k.(*ecdsaPrivateKey)
157172

158-
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.k)
173+
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.privKey)
159174
if err != nil {
160175
return fmt.Errorf("Failed storing ECDSA private key [%s]", err)
161176
}
162177

163178
case *ecdsaPublicKey:
164179
kk := k.(*ecdsaPublicKey)
165180

166-
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.k)
181+
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.pubKey)
167182
if err != nil {
168183
return fmt.Errorf("Failed storing ECDSA public key [%s]", err)
169184
}
170185

171186
case *rsaPrivateKey:
172187
kk := k.(*rsaPrivateKey)
173188

174-
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.k)
189+
err = ks.storePrivateKey(hex.EncodeToString(k.SKI()), kk.privKey)
175190
if err != nil {
176191
return fmt.Errorf("Failed storing RSA private key [%s]", err)
177192
}
178193

179194
case *rsaPublicKey:
180195
kk := k.(*rsaPublicKey)
181196

182-
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.k)
197+
err = ks.storePublicKey(hex.EncodeToString(k.SKI()), kk.pubKey)
183198
if err != nil {
184199
return fmt.Errorf("Failed storing RSA public key [%s]", err)
185200
}
186201

187202
case *aesPrivateKey:
188203
kk := k.(*aesPrivateKey)
189204

190-
err = ks.storeKey(hex.EncodeToString(k.SKI()), kk.k)
205+
err = ks.storeKey(hex.EncodeToString(k.SKI()), kk.privKey)
191206
if err != nil {
192207
return fmt.Errorf("Failed storing AES key [%s]", err)
193208
}
@@ -219,10 +234,6 @@ func (ks *FileBasedKeyStore) getSuffix(alias string) string {
219234
}
220235

221236
func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{}) error {
222-
if ks.readOnly {
223-
return errors.New("Read only KeyStore.")
224-
}
225-
226237
rawKey, err := primitives.PrivateKeyToPEM(privateKey, ks.pwd)
227238
if err != nil {
228239
logger.Errorf("Failed converting private key to PEM [%s]: [%s]", alias, err)
@@ -239,10 +250,6 @@ func (ks *FileBasedKeyStore) storePrivateKey(alias string, privateKey interface{
239250
}
240251

241252
func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{}) error {
242-
if ks.readOnly {
243-
return errors.New("Read only KeyStore.")
244-
}
245-
246253
rawKey, err := primitives.PublicKeyToPEM(publicKey, ks.pwd)
247254
if err != nil {
248255
logger.Errorf("Failed converting public key to PEM [%s]: [%s]", alias, err)
@@ -259,10 +266,6 @@ func (ks *FileBasedKeyStore) storePublicKey(alias string, publicKey interface{})
259266
}
260267

261268
func (ks *FileBasedKeyStore) storeKey(alias string, key []byte) error {
262-
if ks.readOnly {
263-
return errors.New("Read only KeyStore.")
264-
}
265-
266269
pem, err := primitives.AEStoEncryptedPEM(key, ks.pwd)
267270
if err != nil {
268271
logger.Errorf("Failed converting key to PEM [%s]: [%s]", alias, err)

core/crypto/bccsp/sw/fileks_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package sw
17+
18+
import (
19+
"fmt"
20+
"os"
21+
"path/filepath"
22+
"testing"
23+
)
24+
25+
func TestInvalidStoreKey(t *testing.T) {
26+
ks := &FileBasedKeyStore{}
27+
if err := ks.Init(nil, filepath.Join(os.TempDir(), "bccspks"), false); err != nil {
28+
fmt.Printf("Failed initiliazing KeyStore [%s]", err)
29+
os.Exit(-1)
30+
}
31+
32+
err := ks.StoreKey(nil)
33+
if err == nil {
34+
t.Fatal("Error should be different from nil in this case")
35+
}
36+
37+
err = ks.StoreKey(&ecdsaPrivateKey{nil})
38+
if err == nil {
39+
t.Fatal("Error should be different from nil in this case")
40+
}
41+
42+
err = ks.StoreKey(&ecdsaPublicKey{nil})
43+
if err == nil {
44+
t.Fatal("Error should be different from nil in this case")
45+
}
46+
47+
err = ks.StoreKey(&rsaPublicKey{nil})
48+
if err == nil {
49+
t.Fatal("Error should be different from nil in this case")
50+
}
51+
52+
err = ks.StoreKey(&rsaPrivateKey{nil})
53+
if err == nil {
54+
t.Fatal("Error should be different from nil in this case")
55+
}
56+
57+
err = ks.StoreKey(&aesPrivateKey{nil, false})
58+
if err == nil {
59+
t.Fatal("Error should be different from nil in this case")
60+
}
61+
62+
err = ks.StoreKey(&aesPrivateKey{nil, true})
63+
if err == nil {
64+
t.Fatal("Error should be different from nil in this case")
65+
}
66+
}

0 commit comments

Comments
 (0)