@@ -28,6 +28,8 @@ import (
28
28
29
29
"hash"
30
30
31
+ "crypto/x509"
32
+
31
33
"github.com/hyperledger/fabric/core/crypto/bccsp"
32
34
"github.com/hyperledger/fabric/core/crypto/primitives"
33
35
"github.com/hyperledger/fabric/core/crypto/utils"
@@ -256,21 +258,29 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
256
258
257
259
// KeyImport imports a key from its raw representation using opts.
258
260
// The opts argument should be appropriate for the primitive used.
259
- func (csp * impl ) KeyImport (raw [] byte , opts bccsp.KeyImportOpts ) (k bccsp.Key , err error ) {
261
+ func (csp * impl ) KeyImport (raw interface {} , opts bccsp.KeyImportOpts ) (k bccsp.Key , err error ) {
260
262
// Validate arguments
263
+ if raw == nil {
264
+ return nil , errors .New ("Invalid raw. Cannot be nil" )
265
+ }
266
+
261
267
if opts == nil {
262
268
return nil , errors .New ("Invalid Opts parameter. It must not be nil." )
263
269
}
264
270
265
271
switch opts .(type ) {
266
272
267
273
case * bccsp.AES256ImportKeyOpts :
274
+ aesRaw , ok := raw .([]byte )
275
+ if ! ok {
276
+ return nil , errors .New ("[AES256ImportKeyOpts] Invalid raw material. Expected byte array." )
277
+ }
268
278
269
- if len (raw ) != 32 {
270
- return nil , fmt .Errorf ("[AES256ImportKeyOpts] Invalid Key Length [%d]. Must be 32 bytes" , len (raw ))
279
+ if len (aesRaw ) != 32 {
280
+ return nil , fmt .Errorf ("[AES256ImportKeyOpts] Invalid Key Length [%d]. Must be 32 bytes" , len (aesRaw ))
271
281
}
272
282
273
- aesK := & aesPrivateKey {utils .Clone (raw ), false }
283
+ aesK := & aesPrivateKey {utils .Clone (aesRaw ), false }
274
284
275
285
// If the key is not Ephemeral, store it.
276
286
if ! opts .Ephemeral () {
@@ -284,12 +294,16 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
284
294
return aesK , nil
285
295
286
296
case * bccsp.HMACImportKeyOpts :
297
+ aesRaw , ok := raw .([]byte )
298
+ if ! ok {
299
+ return nil , errors .New ("[HMACImportKeyOpts] Invalid raw material. Expected byte array." )
300
+ }
287
301
288
- if len (raw ) == 0 {
302
+ if len (aesRaw ) == 0 {
289
303
return nil , errors .New ("[HMACImportKeyOpts] Invalid raw. It must not be nil." )
290
304
}
291
305
292
- aesK := & aesPrivateKey {utils .Clone (raw ), false }
306
+ aesK := & aesPrivateKey {utils .Clone (aesRaw ), false }
293
307
294
308
// If the key is not Ephemeral, store it.
295
309
if ! opts .Ephemeral () {
@@ -303,12 +317,16 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
303
317
return aesK , nil
304
318
305
319
case * bccsp.ECDSAPKIXPublicKeyImportOpts :
320
+ der , ok := raw .([]byte )
321
+ if ! ok {
322
+ return nil , errors .New ("[ECDSAPKIXPublicKeyImportOpts] Invalid raw material. Expected byte array." )
323
+ }
306
324
307
- if len (raw ) == 0 {
325
+ if len (der ) == 0 {
308
326
return nil , errors .New ("[ECDSAPKIXPublicKeyImportOpts] Invalid raw. It must not be nil." )
309
327
}
310
328
311
- lowLevelKey , err := primitives .DERToPublicKey (raw )
329
+ lowLevelKey , err := primitives .DERToPublicKey (der )
312
330
if err != nil {
313
331
return nil , fmt .Errorf ("Failed converting PKIX to ECDSA public key [%s]" , err )
314
332
}
@@ -332,12 +350,16 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
332
350
return k , nil
333
351
334
352
case * bccsp.ECDSAPrivateKeyImportOpts :
353
+ der , ok := raw .([]byte )
354
+ if ! ok {
355
+ return nil , errors .New ("[ECDSADERPrivateKeyImportOpts] Invalid raw material. Expected byte array." )
356
+ }
335
357
336
- if len (raw ) == 0 {
358
+ if len (der ) == 0 {
337
359
return nil , errors .New ("[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil." )
338
360
}
339
361
340
- lowLevelKey , err := primitives .DERToPrivateKey (raw )
362
+ lowLevelKey , err := primitives .DERToPrivateKey (der )
341
363
if err != nil {
342
364
return nil , fmt .Errorf ("Failed converting PKIX to ECDSA public key [%s]" , err )
343
365
}
@@ -361,10 +383,9 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
361
383
return k , nil
362
384
363
385
case * bccsp.ECDSAGoPublicKeyImportOpts :
364
-
365
- lowLevelKey := opts .(* bccsp.ECDSAGoPublicKeyImportOpts ).PublicKey ()
366
- if lowLevelKey == nil {
367
- return nil , errors .New ("Invalid Opts. ECDSA Public key cannot be nil" )
386
+ lowLevelKey , ok := raw .(* ecdsa.PublicKey )
387
+ if ! ok {
388
+ return nil , errors .New ("[ECDSAGoPublicKeyImportOpts] Invalid raw material. Expected *ecdsa.PublicKey." )
368
389
}
369
390
370
391
k = & ecdsaPublicKey {lowLevelKey }
@@ -381,10 +402,9 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
381
402
return k , nil
382
403
383
404
case * bccsp.RSAGoPublicKeyImportOpts :
384
-
385
- lowLevelKey := opts .(* bccsp.RSAGoPublicKeyImportOpts ).PublicKey ()
386
- if lowLevelKey == nil {
387
- return nil , errors .New ("Invalid Opts. ECDSA Public key cannot be nil" )
405
+ lowLevelKey , ok := raw .(* rsa.PublicKey )
406
+ if ! ok {
407
+ return nil , errors .New ("[RSAGoPublicKeyImportOpts] Invalid raw material. Expected *rsa.PublicKey." )
388
408
}
389
409
390
410
k = & rsaPublicKey {lowLevelKey }
@@ -401,19 +421,18 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
401
421
return k , nil
402
422
403
423
case * bccsp.X509PublicKeyImportOpts :
404
-
405
- x509Cert := opts .(* bccsp.X509PublicKeyImportOpts ).Certificate ()
406
- if x509Cert == nil {
407
- return nil , errors .New ("Invalid Opts. X509 certificate cannot be nil" )
424
+ x509Cert , ok := raw .(* x509.Certificate )
425
+ if ! ok {
426
+ return nil , errors .New ("[X509PublicKeyImportOpts] Invalid raw material. Expected *x509.Certificate." )
408
427
}
409
428
410
429
pk := x509Cert .PublicKey
411
430
412
431
switch pk .(type ) {
413
432
case * ecdsa.PublicKey :
414
- return csp .KeyImport (nil , & bccsp.ECDSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral (), PK : pk .( * ecdsa. PublicKey )})
433
+ return csp .KeyImport (pk , & bccsp.ECDSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral ()})
415
434
case * rsa.PublicKey :
416
- return csp .KeyImport (nil , & bccsp.RSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral (), PK : pk .( * rsa. PublicKey )})
435
+ return csp .KeyImport (pk , & bccsp.RSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral ()})
417
436
default :
418
437
return nil , errors .New ("Certificate public key type not recognized. Supported keys: [ECDSA, RSA]" )
419
438
}
0 commit comments