Skip to content

Commit 5a27382

Browse files
committed
[FAB-3540] Integrating Error Handling in bccsp/sw
Change-Id: I39f7ad6a4d3916fff2a62a2614c8ab01a8521e30 Signed-off-by: Angelo De Caro <[email protected]>
1 parent 0fe5cb2 commit 5a27382

7 files changed

+143
-74
lines changed

bccsp/sw/hash_test.go

+29-10
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,59 @@ func TestHash(t *testing.T) {
3131
expectetMsg := []byte{1, 2, 3, 4}
3232
expectedOpts := &mocks2.HashOpts{}
3333
expectetValue := []byte{1, 2, 3, 4, 5}
34-
expectedErr := errors.New("no error")
34+
expectedErr := errors.New("Expected Error")
3535

3636
hashers := make(map[reflect.Type]Hasher)
3737
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{
3838
MsgArg: expectetMsg,
3939
OptsArg: expectedOpts,
4040
Value: expectetValue,
41-
Err: expectedErr,
41+
Err: nil,
4242
}
43-
4443
csp := impl{hashers: hashers}
45-
4644
value, err := csp.Hash(expectetMsg, expectedOpts)
4745
assert.Equal(t, expectetValue, value)
48-
assert.Equal(t, expectedErr, err)
46+
assert.Nil(t, err)
47+
48+
hashers = make(map[reflect.Type]Hasher)
49+
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{
50+
MsgArg: expectetMsg,
51+
OptsArg: expectedOpts,
52+
Value: nil,
53+
Err: expectedErr,
54+
}
55+
csp = impl{hashers: hashers}
56+
value, err = csp.Hash(expectetMsg, expectedOpts)
57+
assert.Nil(t, value)
58+
assert.Contains(t, err.Error(), expectedErr.Error())
4959
}
5060

5161
func TestGetHash(t *testing.T) {
5262
expectedOpts := &mocks2.HashOpts{}
5363
expectetValue := sha256.New()
54-
expectedErr := errors.New("no error")
64+
expectedErr := errors.New("Expected Error")
5565

5666
hashers := make(map[reflect.Type]Hasher)
5767
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{
5868
OptsArg: expectedOpts,
5969
ValueHash: expectetValue,
60-
Err: expectedErr,
70+
Err: nil,
6171
}
62-
6372
csp := impl{hashers: hashers}
64-
6573
value, err := csp.GetHash(expectedOpts)
6674
assert.Equal(t, expectetValue, value)
67-
assert.Equal(t, expectedErr, err)
75+
assert.Nil(t, err)
76+
77+
hashers = make(map[reflect.Type]Hasher)
78+
hashers[reflect.TypeOf(&mocks2.HashOpts{})] = &mocks.Hasher{
79+
OptsArg: expectedOpts,
80+
ValueHash: expectetValue,
81+
Err: expectedErr,
82+
}
83+
csp = impl{hashers: hashers}
84+
value, err = csp.GetHash(expectedOpts)
85+
assert.Nil(t, value)
86+
assert.Contains(t, err.Error(), expectedErr.Error())
6887
}
6988

7089
func TestHasher(t *testing.T) {

bccsp/sw/impl.go

+69-41
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import (
1919
"crypto/elliptic"
2020
"crypto/sha256"
2121
"crypto/sha512"
22-
"errors"
23-
"fmt"
2422
"hash"
2523
"reflect"
2624

2725
"github.com/hyperledger/fabric/bccsp"
26+
"github.com/hyperledger/fabric/common/errors"
2827
"github.com/hyperledger/fabric/common/flogging"
2928
"golang.org/x/crypto/sha3"
3029
)
@@ -38,7 +37,7 @@ var (
3837
func NewDefaultSecurityLevel(keyStorePath string) (bccsp.BCCSP, error) {
3938
ks := &fileBasedKeyStore{}
4039
if err := ks.Init(nil, keyStorePath, false); err != nil {
41-
return nil, fmt.Errorf("Failed initializing key store [%s]", err)
40+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed initializing key store at [%v]", keyStorePath).WrapError(err)
4241
}
4342

4443
return New(256, "SHA2", ks)
@@ -57,12 +56,12 @@ func New(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.B
5756
conf := &config{}
5857
err := conf.setSecurityLevel(securityLevel, hashFamily)
5958
if err != nil {
60-
return nil, fmt.Errorf("Failed initializing configuration [%s]", err)
59+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed initializing configuration at [%v,%v]", securityLevel, hashFamily).WrapError(err)
6160
}
6261

6362
// Check KeyStore
6463
if keyStore == nil {
65-
return nil, errors.New("Invalid bccsp.KeyStore instance. It must be different from nil.")
64+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid bccsp.KeyStore instance. It must be different from nil.")
6665
}
6766

6867
// Set the encryptors
@@ -159,25 +158,25 @@ type impl struct {
159158
func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
160159
// Validate arguments
161160
if opts == nil {
162-
return nil, errors.New("Invalid Opts parameter. It must not be nil.")
161+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Opts parameter. It must not be nil.")
163162
}
164163

165164
keyGenerator, found := csp.keyGenerators[reflect.TypeOf(opts)]
166165
if !found {
167-
return nil, fmt.Errorf("Unsupported 'KeyGenOpts' provided [%v]", opts)
166+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'KeyGenOpts' provided [%v]", opts)
168167
}
169168

170169
k, err = keyGenerator.KeyGen(opts)
171170
if err != nil {
172-
return nil, err
171+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed generating key with opts [%v]", opts).WrapError(err)
173172
}
174173

175174
// If the key is not Ephemeral, store it.
176175
if !opts.Ephemeral() {
177176
// Store the key
178177
err = csp.ks.StoreKey(k)
179178
if err != nil {
180-
return nil, fmt.Errorf("Failed storing key [%s]. [%s]", opts.Algorithm(), err)
179+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed storing key [%s]. [%s]", opts.Algorithm(), err)
181180
}
182181
}
183182

@@ -189,28 +188,28 @@ func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
189188
func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
190189
// Validate arguments
191190
if k == nil {
192-
return nil, errors.New("Invalid Key. It must not be nil.")
191+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Key. It must not be nil.")
193192
}
194193
if opts == nil {
195-
return nil, errors.New("Invalid opts. It must not be nil.")
194+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid opts. It must not be nil.")
196195
}
197196

198197
keyDeriver, found := csp.keyDerivers[reflect.TypeOf(k)]
199198
if !found {
200-
return nil, fmt.Errorf("Unsupported 'Key' provided [%v]", k)
199+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'Key' provided [%v]", k)
201200
}
202201

203202
k, err = keyDeriver.KeyDeriv(k, opts)
204203
if err != nil {
205-
return nil, err
204+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed deriving key with opts [%v]", opts).WrapError(err)
206205
}
207206

208207
// If the key is not Ephemeral, store it.
209208
if !opts.Ephemeral() {
210209
// Store the key
211210
err = csp.ks.StoreKey(k)
212211
if err != nil {
213-
return nil, fmt.Errorf("Failed storing key [%s]. [%s]", opts.Algorithm(), err)
212+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed storing key [%s]. [%s]", opts.Algorithm(), err)
214213
}
215214
}
216215

@@ -222,69 +221,84 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
222221
func (csp *impl) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
223222
// Validate arguments
224223
if raw == nil {
225-
return nil, errors.New("Invalid raw. It must not be nil.")
224+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid raw. It must not be nil.")
226225
}
227226
if opts == nil {
228-
return nil, errors.New("Invalid opts. It must not be nil.")
227+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid opts. It must not be nil.")
229228
}
230229

231230
keyImporter, found := csp.keyImporters[reflect.TypeOf(opts)]
232231
if !found {
233-
return nil, fmt.Errorf("Unsupported 'KeyImportOpts' provided [%v]", opts)
232+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'KeyImportOpts' provided [%v]", opts)
234233
}
235234

236235
k, err = keyImporter.KeyImport(raw, opts)
237236
if err != nil {
238-
return nil, err
237+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed importing key with opts [%v]", opts).WrapError(err)
239238
}
240239

241240
// If the key is not Ephemeral, store it.
242241
if !opts.Ephemeral() {
243242
// Store the key
244243
err = csp.ks.StoreKey(k)
245244
if err != nil {
246-
return nil, fmt.Errorf("Failed storing key [%s]. [%s]", opts.Algorithm(), err)
245+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed storing imported key with opts [%v]", opts).WrapError(err)
247246
}
248247
}
249248

250-
return k, nil
249+
return
251250
}
252251

253252
// GetKey returns the key this CSP associates to
254253
// the Subject Key Identifier ski.
255254
func (csp *impl) GetKey(ski []byte) (k bccsp.Key, err error) {
256-
return csp.ks.GetKey(ski)
255+
k, err = csp.ks.GetKey(ski)
256+
if err != nil {
257+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed getting key for SKI [%v]", ski).WrapError(err)
258+
}
259+
260+
return
257261
}
258262

259263
// Hash hashes messages msg using options opts.
260264
func (csp *impl) Hash(msg []byte, opts bccsp.HashOpts) (digest []byte, err error) {
261265
// Validate arguments
262266
if opts == nil {
263-
return nil, errors.New("Invalid opts. It must not be nil.")
267+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid opts. It must not be nil.")
264268
}
265269

266270
hasher, found := csp.hashers[reflect.TypeOf(opts)]
267271
if !found {
268-
return nil, fmt.Errorf("Unsupported 'HashOpt' provided [%v]", opts)
272+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'HashOpt' provided [%v]", opts)
269273
}
270274

271-
return hasher.Hash(msg, opts)
275+
digest, err = hasher.Hash(msg, opts)
276+
if err != nil {
277+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed hashing with opts [%v]", opts).WrapError(err)
278+
}
279+
280+
return
272281
}
273282

274283
// GetHash returns and instance of hash.Hash using options opts.
275284
// If opts is nil then the default hash function is returned.
276285
func (csp *impl) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
277286
// Validate arguments
278287
if opts == nil {
279-
return nil, errors.New("Invalid opts. It must not be nil.")
288+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid opts. It must not be nil.")
280289
}
281290

282291
hasher, found := csp.hashers[reflect.TypeOf(opts)]
283292
if !found {
284-
return nil, fmt.Errorf("Unsupported 'HashOpt' provided [%v]", opts)
293+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'HashOpt' provided [%v]", opts)
294+
}
295+
296+
h, err = hasher.GetHash(opts)
297+
if err != nil {
298+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed getting hash function with opts [%v]", opts).WrapError(err)
285299
}
286300

287-
return hasher.GetHash(opts)
301+
return
288302
}
289303

290304
// Sign signs digest using key k.
@@ -296,53 +310,62 @@ func (csp *impl) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
296310
func (csp *impl) Sign(k bccsp.Key, digest []byte, opts bccsp.SignerOpts) (signature []byte, err error) {
297311
// Validate arguments
298312
if k == nil {
299-
return nil, errors.New("Invalid Key. It must not be nil.")
313+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Key. It must not be nil.")
300314
}
301315
if len(digest) == 0 {
302-
return nil, errors.New("Invalid digest. Cannot be empty.")
316+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid digest. Cannot be empty.")
303317
}
304318

305319
signer, found := csp.signers[reflect.TypeOf(k)]
306320
if !found {
307-
return nil, fmt.Errorf("Unsupported 'SignKey' provided [%v]", k)
321+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'SignKey' provided [%v]", k)
308322
}
309323

310-
return signer.Sign(k, digest, opts)
324+
signature, err = signer.Sign(k, digest, opts)
325+
if err != nil {
326+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed signing with opts [%v]", opts).WrapError(err)
327+
}
328+
329+
return
311330
}
312331

313332
// Verify verifies signature against key k and digest
314333
func (csp *impl) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.SignerOpts) (valid bool, err error) {
315334
// Validate arguments
316335
if k == nil {
317-
return false, errors.New("Invalid Key. It must not be nil.")
336+
return false, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Key. It must not be nil.")
318337
}
319338
if len(signature) == 0 {
320-
return false, errors.New("Invalid signature. Cannot be empty.")
339+
return false, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid signature. Cannot be empty.")
321340
}
322341
if len(digest) == 0 {
323-
return false, errors.New("Invalid digest. Cannot be empty.")
342+
return false, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid digest. Cannot be empty.")
324343
}
325344

326345
verifier, found := csp.verifiers[reflect.TypeOf(k)]
327346
if !found {
328-
return false, fmt.Errorf("Unsupported 'VerifyKey' provided [%v]", k)
347+
return false, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'VerifyKey' provided [%v]", k)
329348
}
330349

331-
return verifier.Verify(k, signature, digest, opts)
350+
valid, err = verifier.Verify(k, signature, digest, opts)
351+
if err != nil {
352+
return false, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed verifing with opts [%v]", opts).WrapError(err)
353+
}
332354

355+
return
333356
}
334357

335358
// Encrypt encrypts plaintext using key k.
336359
// The opts argument should be appropriate for the primitive used.
337360
func (csp *impl) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts) (ciphertext []byte, err error) {
338361
// Validate arguments
339362
if k == nil {
340-
return nil, errors.New("Invalid Key. It must not be nil.")
363+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Key. It must not be nil.")
341364
}
342365

343366
encryptor, found := csp.encryptors[reflect.TypeOf(k)]
344367
if !found {
345-
return nil, fmt.Errorf("Unsupported 'EncryptKey' provided [%v]", k)
368+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'EncryptKey' provided [%v]", k)
346369
}
347370

348371
return encryptor.Encrypt(k, plaintext, opts)
@@ -353,13 +376,18 @@ func (csp *impl) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts
353376
func (csp *impl) Decrypt(k bccsp.Key, ciphertext []byte, opts bccsp.DecrypterOpts) (plaintext []byte, err error) {
354377
// Validate arguments
355378
if k == nil {
356-
return nil, errors.New("Invalid Key. It must not be nil.")
379+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.BadRequest, "Invalid Key. It must not be nil.")
357380
}
358381

359382
decryptor, found := csp.decryptors[reflect.TypeOf(k)]
360383
if !found {
361-
return nil, fmt.Errorf("Unsupported 'DecryptKey' provided [%v]", k)
384+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.NotFound, "Unsupported 'DecryptKey' provided [%v]", k)
385+
}
386+
387+
plaintext, err = decryptor.Decrypt(k, ciphertext, opts)
388+
if err != nil {
389+
return nil, errors.ErrorWithCallstack(errors.BCCSP, errors.Internal, "Failed decrypting with opts [%v]", opts).WrapError(err)
362390
}
363391

364-
return decryptor.Decrypt(k, ciphertext, opts)
392+
return
365393
}

bccsp/sw/keyderiv_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestKeyDeriv(t *testing.T) {
3030
expectedKey := &mocks2.MockKey{BytesValue: []byte{1, 2, 3}}
3131
expectedOpts := &mocks2.KeyDerivOpts{EphemeralValue: true}
3232
expectetValue := &mocks2.MockKey{BytesValue: []byte{1, 2, 3, 4, 5}}
33-
expectedErr := errors.New("no error")
33+
expectedErr := errors.New("Expected Error")
3434

3535
keyDerivers := make(map[reflect.Type]KeyDeriver)
3636
keyDerivers[reflect.TypeOf(&mocks2.MockKey{})] = &mocks.KeyDeriver{
@@ -41,8 +41,8 @@ func TestKeyDeriv(t *testing.T) {
4141
}
4242
csp := impl{keyDerivers: keyDerivers}
4343
value, err := csp.KeyDeriv(expectedKey, expectedOpts)
44-
assert.Equal(t, nil, value)
45-
assert.Equal(t, expectedErr, err)
44+
assert.Nil(t, value)
45+
assert.Contains(t, err.Error(), expectedErr.Error())
4646

4747
keyDerivers = make(map[reflect.Type]KeyDeriver)
4848
keyDerivers[reflect.TypeOf(&mocks2.MockKey{})] = &mocks.KeyDeriver{
@@ -54,7 +54,7 @@ func TestKeyDeriv(t *testing.T) {
5454
csp = impl{keyDerivers: keyDerivers}
5555
value, err = csp.KeyDeriv(expectedKey, expectedOpts)
5656
assert.Equal(t, expectetValue, value)
57-
assert.Equal(t, nil, err)
57+
assert.Nil(t, err)
5858
}
5959

6060
func TestECDSAPublicKeyKeyDeriver(t *testing.T) {

0 commit comments

Comments
 (0)