@@ -19,12 +19,11 @@ import (
19
19
"crypto/elliptic"
20
20
"crypto/sha256"
21
21
"crypto/sha512"
22
- "errors"
23
- "fmt"
24
22
"hash"
25
23
"reflect"
26
24
27
25
"github.com/hyperledger/fabric/bccsp"
26
+ "github.com/hyperledger/fabric/common/errors"
28
27
"github.com/hyperledger/fabric/common/flogging"
29
28
"golang.org/x/crypto/sha3"
30
29
)
38
37
func NewDefaultSecurityLevel (keyStorePath string ) (bccsp.BCCSP , error ) {
39
38
ks := & fileBasedKeyStore {}
40
39
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 )
42
41
}
43
42
44
43
return New (256 , "SHA2" , ks )
@@ -57,12 +56,12 @@ func New(securityLevel int, hashFamily string, keyStore bccsp.KeyStore) (bccsp.B
57
56
conf := & config {}
58
57
err := conf .setSecurityLevel (securityLevel , hashFamily )
59
58
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 )
61
60
}
62
61
63
62
// Check KeyStore
64
63
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." )
66
65
}
67
66
68
67
// Set the encryptors
@@ -159,25 +158,25 @@ type impl struct {
159
158
func (csp * impl ) KeyGen (opts bccsp.KeyGenOpts ) (k bccsp.Key , err error ) {
160
159
// Validate arguments
161
160
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." )
163
162
}
164
163
165
164
keyGenerator , found := csp .keyGenerators [reflect .TypeOf (opts )]
166
165
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 )
168
167
}
169
168
170
169
k , err = keyGenerator .KeyGen (opts )
171
170
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 )
173
172
}
174
173
175
174
// If the key is not Ephemeral, store it.
176
175
if ! opts .Ephemeral () {
177
176
// Store the key
178
177
err = csp .ks .StoreKey (k )
179
178
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 )
181
180
}
182
181
}
183
182
@@ -189,28 +188,28 @@ func (csp *impl) KeyGen(opts bccsp.KeyGenOpts) (k bccsp.Key, err error) {
189
188
func (csp * impl ) KeyDeriv (k bccsp.Key , opts bccsp.KeyDerivOpts ) (dk bccsp.Key , err error ) {
190
189
// Validate arguments
191
190
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." )
193
192
}
194
193
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." )
196
195
}
197
196
198
197
keyDeriver , found := csp .keyDerivers [reflect .TypeOf (k )]
199
198
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 )
201
200
}
202
201
203
202
k , err = keyDeriver .KeyDeriv (k , opts )
204
203
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 )
206
205
}
207
206
208
207
// If the key is not Ephemeral, store it.
209
208
if ! opts .Ephemeral () {
210
209
// Store the key
211
210
err = csp .ks .StoreKey (k )
212
211
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 )
214
213
}
215
214
}
216
215
@@ -222,69 +221,84 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
222
221
func (csp * impl ) KeyImport (raw interface {}, opts bccsp.KeyImportOpts ) (k bccsp.Key , err error ) {
223
222
// Validate arguments
224
223
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." )
226
225
}
227
226
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." )
229
228
}
230
229
231
230
keyImporter , found := csp .keyImporters [reflect .TypeOf (opts )]
232
231
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 )
234
233
}
235
234
236
235
k , err = keyImporter .KeyImport (raw , opts )
237
236
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 )
239
238
}
240
239
241
240
// If the key is not Ephemeral, store it.
242
241
if ! opts .Ephemeral () {
243
242
// Store the key
244
243
err = csp .ks .StoreKey (k )
245
244
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 )
247
246
}
248
247
}
249
248
250
- return k , nil
249
+ return
251
250
}
252
251
253
252
// GetKey returns the key this CSP associates to
254
253
// the Subject Key Identifier ski.
255
254
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
257
261
}
258
262
259
263
// Hash hashes messages msg using options opts.
260
264
func (csp * impl ) Hash (msg []byte , opts bccsp.HashOpts ) (digest []byte , err error ) {
261
265
// Validate arguments
262
266
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." )
264
268
}
265
269
266
270
hasher , found := csp .hashers [reflect .TypeOf (opts )]
267
271
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 )
269
273
}
270
274
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
272
281
}
273
282
274
283
// GetHash returns and instance of hash.Hash using options opts.
275
284
// If opts is nil then the default hash function is returned.
276
285
func (csp * impl ) GetHash (opts bccsp.HashOpts ) (h hash.Hash , err error ) {
277
286
// Validate arguments
278
287
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." )
280
289
}
281
290
282
291
hasher , found := csp .hashers [reflect .TypeOf (opts )]
283
292
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 )
285
299
}
286
300
287
- return hasher . GetHash ( opts )
301
+ return
288
302
}
289
303
290
304
// Sign signs digest using key k.
@@ -296,53 +310,62 @@ func (csp *impl) GetHash(opts bccsp.HashOpts) (h hash.Hash, err error) {
296
310
func (csp * impl ) Sign (k bccsp.Key , digest []byte , opts bccsp.SignerOpts ) (signature []byte , err error ) {
297
311
// Validate arguments
298
312
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." )
300
314
}
301
315
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." )
303
317
}
304
318
305
319
signer , found := csp .signers [reflect .TypeOf (k )]
306
320
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 )
308
322
}
309
323
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
311
330
}
312
331
313
332
// Verify verifies signature against key k and digest
314
333
func (csp * impl ) Verify (k bccsp.Key , signature , digest []byte , opts bccsp.SignerOpts ) (valid bool , err error ) {
315
334
// Validate arguments
316
335
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." )
318
337
}
319
338
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." )
321
340
}
322
341
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." )
324
343
}
325
344
326
345
verifier , found := csp .verifiers [reflect .TypeOf (k )]
327
346
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 )
329
348
}
330
349
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
+ }
332
354
355
+ return
333
356
}
334
357
335
358
// Encrypt encrypts plaintext using key k.
336
359
// The opts argument should be appropriate for the primitive used.
337
360
func (csp * impl ) Encrypt (k bccsp.Key , plaintext []byte , opts bccsp.EncrypterOpts ) (ciphertext []byte , err error ) {
338
361
// Validate arguments
339
362
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." )
341
364
}
342
365
343
366
encryptor , found := csp .encryptors [reflect .TypeOf (k )]
344
367
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 )
346
369
}
347
370
348
371
return encryptor .Encrypt (k , plaintext , opts )
@@ -353,13 +376,18 @@ func (csp *impl) Encrypt(k bccsp.Key, plaintext []byte, opts bccsp.EncrypterOpts
353
376
func (csp * impl ) Decrypt (k bccsp.Key , ciphertext []byte , opts bccsp.DecrypterOpts ) (plaintext []byte , err error ) {
354
377
// Validate arguments
355
378
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." )
357
380
}
358
381
359
382
decryptor , found := csp .decryptors [reflect .TypeOf (k )]
360
383
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 )
362
390
}
363
391
364
- return decryptor . Decrypt ( k , ciphertext , opts )
392
+ return
365
393
}
0 commit comments