@@ -256,18 +256,16 @@ func (csp *impl) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, e
256256// The opts argument should be appropriate for the primitive used.
257257func (csp * impl ) KeyImport (raw []byte , opts bccsp.KeyImportOpts ) (k bccsp.Key , err error ) {
258258 // Validate arguments
259- if len (raw ) == 0 {
260- return nil , errors .New ("Invalid raw. Zero length." )
261- }
262259 if opts == nil {
263260 return nil , errors .New ("Invalid Opts parameter. It must not be nil." )
264261 }
265262
266263 switch opts .(type ) {
264+
267265 case * bccsp.AES256ImportKeyOpts :
268266
269267 if len (raw ) != 32 {
270- return nil , fmt .Errorf ("Invalid Key Length [%d]. Must be 32 bytes" , len (raw ))
268+ return nil , fmt .Errorf ("[AES256ImportKeyOpts] Invalid Key Length [%d]. Must be 32 bytes" , len (raw ))
271269 }
272270
273271 aesK := & aesPrivateKey {utils .Clone (raw ), false }
@@ -282,8 +280,13 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
282280 }
283281
284282 return aesK , nil
283+
285284 case * bccsp.HMACImportKeyOpts :
286285
286+ if len (raw ) == 0 {
287+ return nil , errors .New ("[HMACImportKeyOpts] Invalid raw. It must not be nil." )
288+ }
289+
287290 aesK := & aesPrivateKey {utils .Clone (raw ), false }
288291
289292 // If the key is not Ephemeral, store it.
@@ -296,6 +299,123 @@ func (csp *impl) KeyImport(raw []byte, opts bccsp.KeyImportOpts) (k bccsp.Key, e
296299 }
297300
298301 return aesK , nil
302+
303+ case * bccsp.ECDSAPKIXPublicKeyImportOpts :
304+
305+ if len (raw ) == 0 {
306+ return nil , errors .New ("[ECDSAPKIXPublicKeyImportOpts] Invalid raw. It must not be nil." )
307+ }
308+
309+ lowLevelKey , err := primitives .DERToPublicKey (raw )
310+ if err != nil {
311+ return nil , fmt .Errorf ("Failed converting PKIX to ECDSA public key [%s]" , err )
312+ }
313+
314+ ecdsaPK , ok := lowLevelKey .(* ecdsa.PublicKey )
315+ if ! ok {
316+ return nil , errors .New ("Failed casting to ECDSA public key. Invalid raw material." )
317+ }
318+
319+ k = & ecdsaPublicKey {ecdsaPK }
320+
321+ // If the key is not Ephemeral, store it.
322+ if ! opts .Ephemeral () {
323+ // Store the key
324+ err = csp .ks .storePublicKey (hex .EncodeToString (k .SKI ()), lowLevelKey )
325+ if err != nil {
326+ return nil , fmt .Errorf ("Failed storing ECDSA key [%s]" , err )
327+ }
328+ }
329+
330+ return k , nil
331+
332+ case * bccsp.ECDSAPrivateKeyImportOpts :
333+
334+ if len (raw ) == 0 {
335+ return nil , errors .New ("[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil." )
336+ }
337+
338+ lowLevelKey , err := primitives .DERToPrivateKey (raw )
339+ if err != nil {
340+ return nil , fmt .Errorf ("Failed converting PKIX to ECDSA public key [%s]" , err )
341+ }
342+
343+ ecdsaSK , ok := lowLevelKey .(* ecdsa.PrivateKey )
344+ if ! ok {
345+ return nil , errors .New ("Failed casting to ECDSA public key. Invalid raw material." )
346+ }
347+
348+ k = & ecdsaPrivateKey {ecdsaSK }
349+
350+ // If the key is not Ephemeral, store it.
351+ if ! opts .Ephemeral () {
352+ // Store the key
353+ err = csp .ks .storePrivateKey (hex .EncodeToString (k .SKI ()), lowLevelKey )
354+ if err != nil {
355+ return nil , fmt .Errorf ("Failed storing ECDSA key [%s]" , err )
356+ }
357+ }
358+
359+ return k , nil
360+
361+ case * bccsp.ECDSAGoPublicKeyImportOpts :
362+
363+ lowLevelKey := opts .(* bccsp.ECDSAGoPublicKeyImportOpts ).PublicKey ()
364+ if lowLevelKey == nil {
365+ return nil , errors .New ("Invalid Opts. ECDSA Public key cannot be nil" )
366+ }
367+
368+ k = & ecdsaPublicKey {lowLevelKey }
369+
370+ // If the key is not Ephemeral, store it.
371+ if ! opts .Ephemeral () {
372+ // Store the key
373+ err = csp .ks .storePublicKey (hex .EncodeToString (k .SKI ()), lowLevelKey )
374+ if err != nil {
375+ return nil , fmt .Errorf ("Failed storing ECDSA key [%s]" , err )
376+ }
377+ }
378+
379+ return k , nil
380+
381+ case * bccsp.RSAGoPublicKeyImportOpts :
382+
383+ lowLevelKey := opts .(* bccsp.RSAGoPublicKeyImportOpts ).PublicKey ()
384+ if lowLevelKey == nil {
385+ return nil , errors .New ("Invalid Opts. ECDSA Public key cannot be nil" )
386+ }
387+
388+ k = & rsaPublicKey {lowLevelKey }
389+
390+ // If the key is not Ephemeral, store it.
391+ if ! opts .Ephemeral () {
392+ // Store the key
393+ err = csp .ks .storePublicKey (hex .EncodeToString (k .SKI ()), lowLevelKey )
394+ if err != nil {
395+ return nil , fmt .Errorf ("Failed storing ECDSA key [%s]" , err )
396+ }
397+ }
398+
399+ return k , nil
400+
401+ case * bccsp.X509PublicKeyImportOpts :
402+
403+ x509Cert := opts .(* bccsp.X509PublicKeyImportOpts ).Certificate ()
404+ if x509Cert == nil {
405+ return nil , errors .New ("Invalid Opts. X509 certificate cannot be nil" )
406+ }
407+
408+ pk := x509Cert .PublicKey
409+
410+ switch pk .(type ) {
411+ case * ecdsa.PublicKey :
412+ return csp .KeyImport (nil , & bccsp.ECDSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral (), PK : pk .(* ecdsa.PublicKey )})
413+ case * rsa.PublicKey :
414+ return csp .KeyImport (nil , & bccsp.RSAGoPublicKeyImportOpts {Temporary : opts .Ephemeral (), PK : pk .(* rsa.PublicKey )})
415+ default :
416+ return nil , errors .New ("Certificate public key type not recognized. Supported keys: [ECDSA, RSA]" )
417+ }
418+
299419 default :
300420 return nil , errors .New ("Import Key Options not recognized" )
301421 }
@@ -407,16 +527,38 @@ func (csp *impl) Verify(k bccsp.Key, signature, digest []byte, opts bccsp.Signer
407527 }
408528
409529 return ecdsa .Verify (& (k .(* ecdsaPrivateKey ).k .PublicKey ), digest , ecdsaSignature .R , ecdsaSignature .S ), nil
530+ case * ecdsaPublicKey :
531+ ecdsaSignature := new (primitives.ECDSASignature )
532+ _ , err := asn1 .Unmarshal (signature , ecdsaSignature )
533+ if err != nil {
534+ return false , fmt .Errorf ("Failed unmashalling signature [%s]" , err )
535+ }
536+
537+ return ecdsa .Verify (k .(* ecdsaPublicKey ).k , digest , ecdsaSignature .R , ecdsaSignature .S ), nil
410538 case * rsaPrivateKey :
411539 if opts == nil {
412- return false , errors .New ("Invalid options. Nil ." )
540+ return false , errors .New ("Invalid options. It must not be nil ." )
413541 }
414542 switch opts .(type ) {
415543 case * rsa.PSSOptions :
416544 err := rsa .VerifyPSS (& (k .(* rsaPrivateKey ).k .PublicKey ),
417545 (opts .(* rsa.PSSOptions )).Hash ,
418546 digest , signature , opts .(* rsa.PSSOptions ))
419547
548+ return err == nil , err
549+ default :
550+ return false , fmt .Errorf ("Opts type not recognized [%s]" , opts )
551+ }
552+ case * rsaPublicKey :
553+ if opts == nil {
554+ return false , errors .New ("Invalid options. It must not be nil." )
555+ }
556+ switch opts .(type ) {
557+ case * rsa.PSSOptions :
558+ err := rsa .VerifyPSS (k .(* rsaPublicKey ).k ,
559+ (opts .(* rsa.PSSOptions )).Hash ,
560+ digest , signature , opts .(* rsa.PSSOptions ))
561+
420562 return err == nil , err
421563 default :
422564 return false , fmt .Errorf ("Opts type not recognized [%s]" , opts )
0 commit comments