github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/bccsp/sw/keyimport.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sw 18 19 import ( 20 "errors" 21 "fmt" 22 23 "crypto/ecdsa" 24 "crypto/rsa" 25 "crypto/x509" 26 "reflect" 27 28 "github.com/hyperledger/fabric/bccsp" 29 "github.com/hyperledger/fabric/bccsp/utils" 30 ) 31 32 type aes256ImportKeyOptsKeyImporter struct{} 33 34 func (*aes256ImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 35 aesRaw, ok := raw.([]byte) 36 if !ok { 37 return nil, errors.New("Invalid raw material. Expected byte array.") 38 } 39 40 if aesRaw == nil { 41 return nil, errors.New("Invalid raw material. It must not be nil.") 42 } 43 44 if len(aesRaw) != 32 { 45 return nil, fmt.Errorf("Invalid Key Length [%d]. Must be 32 bytes", len(aesRaw)) 46 } 47 48 return &aesPrivateKey{utils.Clone(aesRaw), false}, nil 49 } 50 51 type hmacImportKeyOptsKeyImporter struct{} 52 53 func (*hmacImportKeyOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 54 aesRaw, ok := raw.([]byte) 55 if !ok { 56 return nil, errors.New("Invalid raw material. Expected byte array.") 57 } 58 59 if len(aesRaw) == 0 { 60 return nil, errors.New("Invalid raw material. It must not be nil.") 61 } 62 63 return &aesPrivateKey{utils.Clone(aesRaw), false}, nil 64 } 65 66 type ecdsaPKIXPublicKeyImportOptsKeyImporter struct{} 67 68 func (*ecdsaPKIXPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 69 der, ok := raw.([]byte) 70 if !ok { 71 return nil, errors.New("Invalid raw material. Expected byte array.") 72 } 73 74 if len(der) == 0 { 75 return nil, errors.New("Invalid raw. It must not be nil.") 76 } 77 78 lowLevelKey, err := utils.DERToPublicKey(der) 79 if err != nil { 80 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 81 } 82 83 ecdsaPK, ok := lowLevelKey.(*ecdsa.PublicKey) 84 if !ok { 85 return nil, errors.New("Failed casting to ECDSA public key. Invalid raw material.") 86 } 87 88 return &ecdsaPublicKey{ecdsaPK}, nil 89 } 90 91 type ecdsaPrivateKeyImportOptsKeyImporter struct{} 92 93 func (*ecdsaPrivateKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 94 der, ok := raw.([]byte) 95 if !ok { 96 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw material. Expected byte array.") 97 } 98 99 if len(der) == 0 { 100 return nil, errors.New("[ECDSADERPrivateKeyImportOpts] Invalid raw. It must not be nil.") 101 } 102 103 lowLevelKey, err := utils.DERToPrivateKey(der) 104 if err != nil { 105 return nil, fmt.Errorf("Failed converting PKIX to ECDSA public key [%s]", err) 106 } 107 108 ecdsaSK, ok := lowLevelKey.(*ecdsa.PrivateKey) 109 if !ok { 110 return nil, errors.New("Failed casting to ECDSA private key. Invalid raw material.") 111 } 112 113 return &ecdsaPrivateKey{ecdsaSK}, nil 114 } 115 116 type ecdsaGoPublicKeyImportOptsKeyImporter struct{} 117 118 func (*ecdsaGoPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 119 lowLevelKey, ok := raw.(*ecdsa.PublicKey) 120 if !ok { 121 return nil, errors.New("Invalid raw material. Expected *ecdsa.PublicKey.") 122 } 123 124 return &ecdsaPublicKey{lowLevelKey}, nil 125 } 126 127 type rsaGoPublicKeyImportOptsKeyImporter struct{} 128 129 func (*rsaGoPublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 130 lowLevelKey, ok := raw.(*rsa.PublicKey) 131 if !ok { 132 return nil, errors.New("Invalid raw material. Expected *rsa.PublicKey.") 133 } 134 135 return &rsaPublicKey{lowLevelKey}, nil 136 } 137 138 type x509PublicKeyImportOptsKeyImporter struct { 139 bccsp *impl 140 } 141 142 func (ki *x509PublicKeyImportOptsKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) { 143 x509Cert, ok := raw.(*x509.Certificate) 144 if !ok { 145 return nil, errors.New("Invalid raw material. Expected *x509.Certificate.") 146 } 147 148 pk := x509Cert.PublicKey 149 150 switch pk.(type) { 151 case *ecdsa.PublicKey: 152 return ki.bccsp.keyImporters[reflect.TypeOf(&bccsp.ECDSAGoPublicKeyImportOpts{})].KeyImport( 153 pk, 154 &bccsp.ECDSAGoPublicKeyImportOpts{Temporary: opts.Ephemeral()}) 155 case *rsa.PublicKey: 156 return ki.bccsp.keyImporters[reflect.TypeOf(&bccsp.RSAGoPublicKeyImportOpts{})].KeyImport( 157 pk, 158 &bccsp.RSAGoPublicKeyImportOpts{Temporary: opts.Ephemeral()}) 159 default: 160 return nil, errors.New("Certificate's public key type not recognized. Supported keys: [ECDSA, RSA]") 161 } 162 }