github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/sw/keyimport.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package sw 8 9 import ( 10 "crypto/ecdsa" 11 "crypto/x509" 12 "errors" 13 "fmt" 14 "reflect" 15 16 "github.com/hyperledger/fabric/bccsp" 17 "github.com/hyperledger/fabric/bccsp/utils" 18 ) 19 20 type aes256ImportKeyOptsKeyImporter struct{} 21 22 func (*aes256ImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 23 aesRaw, ok := raw.([]byte) 24 if !ok { 25 return nil, errors.New("Invalid raw material. Expected byte array.") 26 } 27 28 if aesRaw == nil { 29 return nil, errors.New("Invalid raw material. It must not be nil.") 30 } 31 32 if len(aesRaw) != 32 { 33 return nil, fmt.Errorf("Invalid Key Length [%d]. Must be 32 bytes", len(aesRaw)) 34 } 35 36 return &aesPrivateKey{aesRaw, false}, nil 37 } 38 39 type hmacImportKeyOptsKeyImporter struct{} 40 41 func (*hmacImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 42 aesRaw, ok := raw.([]byte) 43 if !ok { 44 return nil, errors.New("Invalid raw material. Expected byte array.") 45 } 46 47 if len(aesRaw) == 0 { 48 return nil, errors.New("Invalid raw material. It must not be nil.") 49 } 50 51 return &aesPrivateKey{aesRaw, false}, nil 52 } 53 54 type ecdsaPKIXPublicKeyImportOptsKeyImporter struct{} 55 56 func (*ecdsaPKIXPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 57 der, ok := raw.([]byte) 58 if !ok { 59 return nil, errors.New("Invalid raw material. Expected byte array.") 60 } 61 62 if len(der) == 0 { 63 return nil, errors.New("Invalid raw. It must not be nil.") 64 } 65 66 lowLevelKey, err := utils.DERToPublicKey(der) 67 if err != nil { 68 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 69 } 70 71 ecdsaPK, ok := lowLevelKey.(*ecdsa.PublicKey) 72 if !ok { 73 return nil, errors.New("Failed casting to ECDSA public key. Invalid raw material.") 74 } 75 76 return &ecdsaPublicKey{ecdsaPK}, nil 77 } 78 79 type ecdsaPrivateKeyImportOptsKeyImporter struct{} 80 81 func (*ecdsaPrivateKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 82 der, ok := raw.([]byte) 83 if !ok { 84 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw material. Expected byte array.") 85 } 86 87 if len(der) == 0 { 88 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil.") 89 } 90 91 lowLevelKey, err := utils.DERToPrivateKey(der) 92 if err != nil { 93 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 94 } 95 96 ecdsaSK, ok := lowLevelKey.(*ecdsa.PrivateKey) 97 if !ok { 98 return nil, errors.New("Failed casting to ECDSA private key. Invalid raw material.") 99 } 100 101 return &ecdsaPrivateKey{ecdsaSK}, nil 102 } 103 104 type ecdsaGoPublicKeyImportOptsKeyImporter struct{} 105 106 func (*ecdsaGoPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 107 lowLevelKey, ok := raw.(*ecdsa.PublicKey) 108 if !ok { 109 return nil, errors.New("Invalid raw material. Expected *ecdsa.PublicKey.") 110 } 111 112 return &ecdsaPublicKey{lowLevelKey}, nil 113 } 114 115 type x509PublicKeyImportOptsKeyImporter struct { 116 bccsp *CSP 117 } 118 119 func (ki *x509PublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (bccsp.Key, error) { 120 x509Cert, ok := raw.(*x509.Certificate) 121 if !ok { 122 return nil, errors.New("Invalid raw material. Expected *x509.Certificate.") 123 } 124 125 pk := x509Cert.PublicKey 126 127 switch pk.(type) { 128 case *ecdsa.PublicKey: 129 return ki.bccsp.KeyImporters[reflect.TypeOf(&bccsp.ECDSAGoPublicKeyImportOpts{})].KeyImport( 130 pk, 131 &bccsp.ECDSAGoPublicKeyImportOpts{Temporary: opts.Ephemeral()}) 132 default: 133 return nil, errors.New("Certificate's public key type not recognized. Supported keys: [ECDSA]") 134 } 135 }